| 1 | #include "test-tool.h" |
| 2 | #include "git-zlib.h" |
| 3 | #include "strbuf.h" |
| 4 | |
| 5 | static const char *zlib_usage = "test-tool zlib [inflate|deflate]"; |
| 6 | |
| 7 | static void do_zlib(struct git_zstream *stream, |
| 8 | int (*zlib_func)(git_zstream *, int), |
| 9 | int fd_in, int fd_out) |
| 10 | { |
| 11 | struct strbuf buf_in = STRBUF_INIT; |
| 12 | int status = Z_OK; |
| 13 | |
| 14 | if (strbuf_read(&buf_in, fd_in, 0) < 0) |
| 15 | die_errno("read error"); |
| 16 | |
| 17 | stream->next_in = (unsigned char *)buf_in.buf; |
| 18 | stream->avail_in = buf_in.len; |
| 19 | |
| 20 | while (status == Z_OK || |
| 21 | (status == Z_BUF_ERROR && !stream->avail_out)) { |
| 22 | unsigned char buf_out[4096]; |
| 23 | |
| 24 | stream->next_out = buf_out; |
| 25 | stream->avail_out = sizeof(buf_out); |
| 26 | |
| 27 | status = zlib_func(stream, Z_FINISH); |
| 28 | if (write_in_full(fd_out, buf_out, |
| 29 | sizeof(buf_out) - stream->avail_out) < 0) |
| 30 | die_errno("write error"); |
| 31 | } |
| 32 | |
| 33 | if (status != Z_STREAM_END) |
| 34 | die("zlib error %d", status); |
| 35 | |
| 36 | strbuf_release(&buf_in); |
| 37 | } |
| 38 | |
| 39 | int cmd__zlib(int argc, const char **argv) |
| 40 | { |
| 41 | git_zstream stream; |
| 42 | |
| 43 | if (argc != 2) |
| 44 | usage(zlib_usage); |
| 45 | |
| 46 | memset(&stream, 0, sizeof(stream)); |
| 47 | |
| 48 | if (!strcmp(argv[1], "inflate")) { |
| 49 | git_inflate_init(&stream); |
| 50 | do_zlib(&stream, git_inflate, 0, 1); |
| 51 | git_inflate_end(&stream); |
| 52 | } else if (!strcmp(argv[1], "deflate")) { |
| 53 | git_deflate_init(&stream, Z_DEFAULT_COMPRESSION); |
| 54 | do_zlib(&stream, git_deflate, 0, 1); |
| 55 | git_deflate_end(&stream); |
| 56 | } else { |
| 57 | error("unknown mode: %s", argv[1]); |
| 58 | usage(zlib_usage); |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |