commit 63581804340e880bf611c6a4a59eda26c503799f from: Stefan Sperling date: Mon Jul 09 20:23:00 2018 UTC rename zbuf to inflate commit - fc79a48d6d45b863032ca7d3a161781cc8e89f95 commit + 63581804340e880bf611c6a4a59eda26c503799f blob - 2563d2de603cf0699d20c0a5f5a4144c115d5bf7 blob + 1dcf38b4c06596714780a7ea6b58464c1ad509ec --- got/Makefile +++ got/Makefile @@ -4,7 +4,7 @@ PROG= got SRCS= got.c blame.c commit_graph.c delta.c diff.c diffreg.c error.c \ fileindex.c object.c object_idcache.c object_idset.c opentemp.c \ path.c pack.c privsep.c reference.c repository.c sha1.c \ - worktree.c zbuf.c + worktree.c inflate.c CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib LDADD = -lutil -lz blob - 48042ccd5a831871aa5a26afc51f938b22dfb5a3 blob + 7f25ad2d36b9f21d0db48ab55c858472cf281a7e --- lib/blame.c +++ lib/blame.c @@ -30,7 +30,7 @@ #include "got_blame.h" #include "got_opentemp.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_delta.h" #include "got_lib_object.h" #include "got_lib_diff.h" blob - 4023b5de72d091b968bdf6942e5153a6055d06f2 blob + e973de6181ecc3d2fe1b5be6836948d252ec8279 --- lib/commit_graph.c +++ lib/commit_graph.c @@ -31,7 +31,7 @@ #include "got_commit_graph.h" #include "got_lib_delta.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_object.h" #include "got_lib_object_idset.h" blob - 5d81bff851248d53b410b6439315d28e2f467c7f blob + 4b51bee37b494cecde84a05b8a558f64ee8d940a --- lib/delta.c +++ lib/delta.c @@ -29,7 +29,7 @@ #include "got_lib_delta.h" #include "got_lib_path.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #ifndef MIN #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b)) blob - /dev/null blob + 45fce72d13ba62cbd804455e3befe0d050f112be (mode 644) --- /dev/null +++ lib/got_lib_inflate.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 Stefan Sperling + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +struct got_zstream_buf { + z_stream z; + char *inbuf; + size_t inlen; + char *outbuf; + size_t outlen; + int flags; +#define GOT_ZSTREAM_F_HAVE_MORE 0x01 +#define GOT_ZSTREAM_F_OWN_OUTBUF 0x02 +}; + +#define GOT_ZSTREAM_BUFSIZE 8192 + +const struct got_error *got_inflate_init(struct got_zstream_buf *, uint8_t *, + size_t); +const struct got_error *got_inflate_read(struct got_zstream_buf *, FILE *, + size_t *); +const struct got_error *got_inflate_read_fd(struct got_zstream_buf *, int, + size_t *); +const struct got_error *got_inflate_read_mmap(struct got_zstream_buf *, + uint8_t *, size_t, size_t, size_t *, size_t *); +void got_inflate_end(struct got_zstream_buf *); +const struct got_error *got_inflate_to_mem(uint8_t **, size_t *, FILE *); +const struct got_error *got_inflate_to_mem_fd(uint8_t **, size_t *, int); +const struct got_error *got_inflate_to_mem_mmap(uint8_t **, size_t *, uint8_t *, + size_t, size_t); +const struct got_error *got_inflate_to_file(size_t *, FILE *, FILE *); +const struct got_error *got_inflate_to_file_fd(size_t *, int, FILE *); +const struct got_error *got_inflate_to_fd(size_t *, FILE *, int); +const struct got_error *got_inflate_to_file_mmap(size_t *, uint8_t *, size_t, + size_t, FILE *); blob - 45fce72d13ba62cbd804455e3befe0d050f112be (mode 644) blob + /dev/null --- lib/got_lib_zbuf.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2018 Stefan Sperling - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -struct got_zstream_buf { - z_stream z; - char *inbuf; - size_t inlen; - char *outbuf; - size_t outlen; - int flags; -#define GOT_ZSTREAM_F_HAVE_MORE 0x01 -#define GOT_ZSTREAM_F_OWN_OUTBUF 0x02 -}; - -#define GOT_ZSTREAM_BUFSIZE 8192 - -const struct got_error *got_inflate_init(struct got_zstream_buf *, uint8_t *, - size_t); -const struct got_error *got_inflate_read(struct got_zstream_buf *, FILE *, - size_t *); -const struct got_error *got_inflate_read_fd(struct got_zstream_buf *, int, - size_t *); -const struct got_error *got_inflate_read_mmap(struct got_zstream_buf *, - uint8_t *, size_t, size_t, size_t *, size_t *); -void got_inflate_end(struct got_zstream_buf *); -const struct got_error *got_inflate_to_mem(uint8_t **, size_t *, FILE *); -const struct got_error *got_inflate_to_mem_fd(uint8_t **, size_t *, int); -const struct got_error *got_inflate_to_mem_mmap(uint8_t **, size_t *, uint8_t *, - size_t, size_t); -const struct got_error *got_inflate_to_file(size_t *, FILE *, FILE *); -const struct got_error *got_inflate_to_file_fd(size_t *, int, FILE *); -const struct got_error *got_inflate_to_fd(size_t *, FILE *, int); -const struct got_error *got_inflate_to_file_mmap(size_t *, uint8_t *, size_t, - size_t, FILE *); blob - a55e09d2bb2813248ba66fd654251513c7aee429 blob + 4cf62dd8461ada927c3e2522739feadbe7a4c211 --- lib/object.c +++ lib/object.c @@ -43,7 +43,7 @@ #include "got_lib_delta.h" #include "got_lib_pack.h" #include "got_lib_path.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_object.h" #include "got_lib_privsep.h" #include "got_lib_repository.h" blob - /dev/null blob + c8bef69378fb819bd6da341a564b69f4c607c911 (mode 644) --- /dev/null +++ lib/inflate.c @@ -0,0 +1,485 @@ +/* + * Copyright (c) 2018 Stefan Sperling + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include "got_error.h" +#include "got_object.h" + +#include "got_lib_path.h" +#include "got_lib_inflate.h" + +#ifndef MIN +#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b)) +#endif + +const struct got_error * +got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize) +{ + const struct got_error *err = NULL; + + memset(&zb->z, 0, sizeof(zb->z)); + + zb->z.zalloc = Z_NULL; + zb->z.zfree = Z_NULL; + if (inflateInit(&zb->z) != Z_OK) { + err = got_error(GOT_ERR_IO); + goto done; + } + + zb->inlen = zb->outlen = bufsize; + + zb->inbuf = calloc(1, zb->inlen); + if (zb->inbuf == NULL) { + err = got_error_from_errno(); + goto done; + } + + zb->flags = 0; + if (outbuf == NULL) { + zb->outbuf = calloc(1, zb->outlen); + if (zb->outbuf == NULL) { + err = got_error_from_errno(); + goto done; + } + zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF; + } else + zb->outbuf = outbuf; + +done: + if (err) + got_inflate_end(zb); + return err; +} + +const struct got_error * +got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp) +{ + size_t last_total_out = zb->z.total_out; + z_stream *z = &zb->z; + int ret = Z_ERRNO; + + z->next_out = zb->outbuf; + z->avail_out = zb->outlen; + + *outlenp = 0; + do { + if (z->avail_in == 0) { + size_t n = fread(zb->inbuf, 1, zb->inlen, f); + if (n == 0) { + if (ferror(f)) + return got_ferror(f, GOT_ERR_IO); + /* EOF */ + ret = Z_STREAM_END; + break; + } + z->next_in = zb->inbuf; + z->avail_in = n; + } + ret = inflate(z, Z_SYNC_FLUSH); + } while (ret == Z_OK && z->avail_out > 0); + + if (ret == Z_OK) { + zb->flags |= GOT_ZSTREAM_F_HAVE_MORE; + } else { + if (ret != Z_STREAM_END) + return got_error(GOT_ERR_DECOMPRESSION); + zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE; + } + + *outlenp = z->total_out - last_total_out; + return NULL; +} + +const struct got_error * +got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp) +{ + size_t last_total_out = zb->z.total_out; + z_stream *z = &zb->z; + int ret = Z_ERRNO; + + z->next_out = zb->outbuf; + z->avail_out = zb->outlen; + + *outlenp = 0; + do { + if (z->avail_in == 0) { + ssize_t n = read(fd, zb->inbuf, zb->inlen); + if (n < 0) + return got_error_from_errno(); + else if (n == 0) { + /* EOF */ + ret = Z_STREAM_END; + break; + } + z->next_in = zb->inbuf; + z->avail_in = n; + } + ret = inflate(z, Z_SYNC_FLUSH); + } while (ret == Z_OK && z->avail_out > 0); + + if (ret == Z_OK) { + zb->flags |= GOT_ZSTREAM_F_HAVE_MORE; + } else { + if (ret != Z_STREAM_END) + return got_error(GOT_ERR_DECOMPRESSION); + zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE; + } + + *outlenp = z->total_out - last_total_out; + return NULL; +} + +const struct got_error * +got_inflate_read_mmap(struct got_zstream_buf *zb, uint8_t *map, size_t offset, + size_t len, size_t *outlenp, size_t *consumed) +{ + size_t last_total_out = zb->z.total_out; + z_stream *z = &zb->z; + int ret = Z_ERRNO; + + z->next_out = zb->outbuf; + z->avail_out = zb->outlen; + + *outlenp = 0; + *consumed = 0; + + do { + if (z->avail_in == 0) { + if (len == 0) { + /* EOF */ + ret = Z_STREAM_END; + break; + } + z->next_in = map + offset; + z->avail_in = MIN(zb->inlen, len); + *consumed += z->avail_in; + offset += z->avail_in; + len -= z->avail_in; + } + ret = inflate(z, Z_SYNC_FLUSH); + } while (ret == Z_OK && z->avail_out > 0); + + if (ret == Z_OK) { + zb->flags |= GOT_ZSTREAM_F_HAVE_MORE; + } else { + if (ret != Z_STREAM_END) + return got_error(GOT_ERR_DECOMPRESSION); + zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE; + } + + *outlenp = z->total_out - last_total_out; + return NULL; +} + +void +got_inflate_end(struct got_zstream_buf *zb) +{ + free(zb->inbuf); + if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF) + free(zb->outbuf); + inflateEnd(&zb->z); +} + +const struct got_error * +got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f) +{ + const struct got_error *err; + size_t avail; + struct got_zstream_buf zb; + void *newbuf; + + *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE); + if (*outbuf == NULL) + return got_error_from_errno(); + err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE); + if (err) + return err; + + *outlen = 0; + + do { + err = got_inflate_read(&zb, f, &avail); + if (err) + return err; + *outlen += avail; + if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) { + newbuf = reallocarray(*outbuf, 1, + *outlen + GOT_ZSTREAM_BUFSIZE); + if (newbuf == NULL) { + err = got_error_from_errno(); + free(*outbuf); + *outbuf = NULL; + *outlen = 0; + goto done; + } + *outbuf = newbuf; + zb.outbuf = newbuf + *outlen; + zb.outlen = GOT_ZSTREAM_BUFSIZE; + } + } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); + +done: + got_inflate_end(&zb); + return err; +} + +const struct got_error * +got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd) +{ + const struct got_error *err; + size_t avail; + struct got_zstream_buf zb; + void *newbuf; + + *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE); + if (*outbuf == NULL) + return got_error_from_errno(); + err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE); + if (err) + return err; + + *outlen = 0; + + do { + err = got_inflate_read_fd(&zb, infd, &avail); + if (err) + return err; + *outlen += avail; + if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) { + newbuf = reallocarray(*outbuf, 1, + *outlen + GOT_ZSTREAM_BUFSIZE); + if (newbuf == NULL) { + err = got_error_from_errno(); + free(*outbuf); + *outbuf = NULL; + *outlen = 0; + goto done; + } + *outbuf = newbuf; + zb.outbuf = newbuf + *outlen; + zb.outlen = GOT_ZSTREAM_BUFSIZE; + } + } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); + +done: + got_inflate_end(&zb); + return err; +} + +const struct got_error * +got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map, + size_t offset, size_t len) +{ + const struct got_error *err; + size_t avail; + struct got_zstream_buf zb; + void *newbuf; + size_t consumed; + + *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE); + if (*outbuf == NULL) + return got_error_from_errno(); + err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE); + if (err) + return err; + + *outlen = 0; + + do { + err = got_inflate_read_mmap(&zb, map, offset, len, &avail, + &consumed); + if (err) + return err; + offset += consumed; + len -= consumed; + *outlen += avail; + if (len == 0) + break; + if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) { + newbuf = reallocarray(*outbuf, 1, + *outlen + GOT_ZSTREAM_BUFSIZE); + if (newbuf == NULL) { + err = got_error_from_errno(); + free(*outbuf); + *outbuf = NULL; + *outlen = 0; + goto done; + } + *outbuf = newbuf; + zb.outbuf = newbuf + *outlen; + zb.outlen = GOT_ZSTREAM_BUFSIZE; + } + } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); + +done: + got_inflate_end(&zb); + return err; +} + +const struct got_error * +got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd) +{ + const struct got_error *err = NULL; + size_t avail; + struct got_zstream_buf zb; + + err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE); + if (err) + goto done; + + *outlen = 0; + + do { + err = got_inflate_read(&zb, infile, &avail); + if (err) + return err; + if (avail > 0) { + ssize_t n; + n = write(outfd, zb.outbuf, avail); + if (n != avail) { + err = got_error_from_errno(); + goto done; + } + *outlen += avail; + } + } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); + +done: + if (err == NULL) { + if (lseek(outfd, SEEK_SET, 0) == -1) + err = got_error_from_errno(); + } + got_inflate_end(&zb); + return err; +} + +const struct got_error * +got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile) +{ + const struct got_error *err; + size_t avail; + struct got_zstream_buf zb; + + err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE); + if (err) + goto done; + + *outlen = 0; + + do { + err = got_inflate_read(&zb, infile, &avail); + if (err) + return err; + if (avail > 0) { + size_t n; + n = fwrite(zb.outbuf, avail, 1, outfile); + if (n != 1) { + err = got_ferror(outfile, GOT_ERR_IO); + goto done; + } + *outlen += avail; + } + } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); + +done: + if (err == NULL) + rewind(outfile); + got_inflate_end(&zb); + return err; +} + +const struct got_error * +got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile) +{ + const struct got_error *err; + size_t avail; + struct got_zstream_buf zb; + + err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE); + if (err) + goto done; + + *outlen = 0; + + do { + err = got_inflate_read_fd(&zb, infd, &avail); + if (err) + return err; + if (avail > 0) { + size_t n; + n = fwrite(zb.outbuf, avail, 1, outfile); + if (n != 1) { + err = got_ferror(outfile, GOT_ERR_IO); + goto done; + } + *outlen += avail; + } + } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); + +done: + if (err == NULL) + rewind(outfile); + got_inflate_end(&zb); + return err; +} + +const struct got_error * +got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset, + size_t len, FILE *outfile) +{ + const struct got_error *err; + size_t avail; + struct got_zstream_buf zb; + size_t consumed; + + err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE); + if (err) + goto done; + + *outlen = 0; + + do { + err = got_inflate_read_mmap(&zb, map, offset, len, &avail, + &consumed); + if (err) + return err; + offset += consumed; + len -= consumed; + if (avail > 0) { + size_t n; + n = fwrite(zb.outbuf, avail, 1, outfile); + if (n != 1) { + err = got_ferror(outfile, GOT_ERR_IO); + goto done; + } + *outlen += avail; + } + } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); + +done: + if (err == NULL) + rewind(outfile); + got_inflate_end(&zb); + return err; +} blob - 2c206beb86a5cae32d59676d22db74b24023fb64 blob + 3e8e8bdecc4f1c9f112fc17e565ec3a89fee66dd --- lib/object_idcache.c +++ lib/object_idcache.c @@ -28,7 +28,7 @@ #include "got_error.h" #include "got_lib_delta.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_object.h" #include "got_lib_object_idcache.h" blob - 2c7c696978afa9567d80dfffd1a651122ec6bad9 blob + 56035401c75a86173b08407b67496f8f59f81183 --- lib/object_idset.c +++ lib/object_idset.c @@ -28,7 +28,7 @@ #include "got_error.h" #include "got_lib_delta.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_object.h" #include "got_lib_object_idset.h" blob - a603e1a81cf8e0bd501567e926adb3470545bec8 blob + c18f5bd03edda1728be9634cd012b8ccaa192ddd --- lib/pack.c +++ lib/pack.c @@ -40,7 +40,7 @@ #include "got_lib_pack.h" #include "got_lib_path.h" #include "got_lib_delta.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_object.h" #include "got_lib_repository.h" blob - a0f4d62e2af51c7ee2dbc2004b2b0dfabd11df26 blob + 1a17fecb11d62ea191129249f2495e89a2182902 --- lib/privsep.c +++ lib/privsep.c @@ -34,7 +34,7 @@ #include "got_lib_sha1.h" #include "got_lib_delta.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_object.h" #include "got_lib_privsep.h" blob - 635623e6ce2338f4495a8e2021c539a29cf39b14 blob + f672627472f988a3b6eaf5da33809177a8eaa389 --- lib/reference.c +++ lib/reference.c @@ -33,7 +33,7 @@ #include "got_lib_sha1.h" #include "got_lib_path.h" #include "got_lib_delta.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_object.h" #define GOT_REF_HEADS "heads" blob - 02939023000c3b4ff481dcc502717377033f97e7 blob + c0e12e9125c2e8f5f4361f2c2dca7bda35222a0c --- lib/repository.c +++ lib/repository.c @@ -32,7 +32,7 @@ #include "got_lib_path.h" #include "got_lib_delta.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_object.h" #include "got_lib_pack.h" #include "got_lib_repository.h" blob - dc82efb90529cd31db50160dcc23a2ef55132cb9 blob + 3274af20084b72b6a316f580c06eea4bb22a4ac7 --- lib/worktree.c +++ lib/worktree.c @@ -39,7 +39,7 @@ #include "got_lib_path.h" #include "got_lib_sha1.h" #include "got_lib_fileindex.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_delta.h" #include "got_lib_object.h" blob - a66a9a6d3d15d38eaf238856a1e994e335d9579a (mode 644) blob + /dev/null --- lib/zbuf.c +++ /dev/null @@ -1,485 +0,0 @@ -/* - * Copyright (c) 2018 Stefan Sperling - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include -#include -#include -#include -#include -#include - -#include "got_error.h" -#include "got_object.h" - -#include "got_lib_path.h" -#include "got_lib_zbuf.h" - -#ifndef MIN -#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b)) -#endif - -const struct got_error * -got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize) -{ - const struct got_error *err = NULL; - - memset(&zb->z, 0, sizeof(zb->z)); - - zb->z.zalloc = Z_NULL; - zb->z.zfree = Z_NULL; - if (inflateInit(&zb->z) != Z_OK) { - err = got_error(GOT_ERR_IO); - goto done; - } - - zb->inlen = zb->outlen = bufsize; - - zb->inbuf = calloc(1, zb->inlen); - if (zb->inbuf == NULL) { - err = got_error_from_errno(); - goto done; - } - - zb->flags = 0; - if (outbuf == NULL) { - zb->outbuf = calloc(1, zb->outlen); - if (zb->outbuf == NULL) { - err = got_error_from_errno(); - goto done; - } - zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF; - } else - zb->outbuf = outbuf; - -done: - if (err) - got_inflate_end(zb); - return err; -} - -const struct got_error * -got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp) -{ - size_t last_total_out = zb->z.total_out; - z_stream *z = &zb->z; - int ret = Z_ERRNO; - - z->next_out = zb->outbuf; - z->avail_out = zb->outlen; - - *outlenp = 0; - do { - if (z->avail_in == 0) { - size_t n = fread(zb->inbuf, 1, zb->inlen, f); - if (n == 0) { - if (ferror(f)) - return got_ferror(f, GOT_ERR_IO); - /* EOF */ - ret = Z_STREAM_END; - break; - } - z->next_in = zb->inbuf; - z->avail_in = n; - } - ret = inflate(z, Z_SYNC_FLUSH); - } while (ret == Z_OK && z->avail_out > 0); - - if (ret == Z_OK) { - zb->flags |= GOT_ZSTREAM_F_HAVE_MORE; - } else { - if (ret != Z_STREAM_END) - return got_error(GOT_ERR_DECOMPRESSION); - zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE; - } - - *outlenp = z->total_out - last_total_out; - return NULL; -} - -const struct got_error * -got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp) -{ - size_t last_total_out = zb->z.total_out; - z_stream *z = &zb->z; - int ret = Z_ERRNO; - - z->next_out = zb->outbuf; - z->avail_out = zb->outlen; - - *outlenp = 0; - do { - if (z->avail_in == 0) { - ssize_t n = read(fd, zb->inbuf, zb->inlen); - if (n < 0) - return got_error_from_errno(); - else if (n == 0) { - /* EOF */ - ret = Z_STREAM_END; - break; - } - z->next_in = zb->inbuf; - z->avail_in = n; - } - ret = inflate(z, Z_SYNC_FLUSH); - } while (ret == Z_OK && z->avail_out > 0); - - if (ret == Z_OK) { - zb->flags |= GOT_ZSTREAM_F_HAVE_MORE; - } else { - if (ret != Z_STREAM_END) - return got_error(GOT_ERR_DECOMPRESSION); - zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE; - } - - *outlenp = z->total_out - last_total_out; - return NULL; -} - -const struct got_error * -got_inflate_read_mmap(struct got_zstream_buf *zb, uint8_t *map, size_t offset, - size_t len, size_t *outlenp, size_t *consumed) -{ - size_t last_total_out = zb->z.total_out; - z_stream *z = &zb->z; - int ret = Z_ERRNO; - - z->next_out = zb->outbuf; - z->avail_out = zb->outlen; - - *outlenp = 0; - *consumed = 0; - - do { - if (z->avail_in == 0) { - if (len == 0) { - /* EOF */ - ret = Z_STREAM_END; - break; - } - z->next_in = map + offset; - z->avail_in = MIN(zb->inlen, len); - *consumed += z->avail_in; - offset += z->avail_in; - len -= z->avail_in; - } - ret = inflate(z, Z_SYNC_FLUSH); - } while (ret == Z_OK && z->avail_out > 0); - - if (ret == Z_OK) { - zb->flags |= GOT_ZSTREAM_F_HAVE_MORE; - } else { - if (ret != Z_STREAM_END) - return got_error(GOT_ERR_DECOMPRESSION); - zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE; - } - - *outlenp = z->total_out - last_total_out; - return NULL; -} - -void -got_inflate_end(struct got_zstream_buf *zb) -{ - free(zb->inbuf); - if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF) - free(zb->outbuf); - inflateEnd(&zb->z); -} - -const struct got_error * -got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f) -{ - const struct got_error *err; - size_t avail; - struct got_zstream_buf zb; - void *newbuf; - - *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE); - if (*outbuf == NULL) - return got_error_from_errno(); - err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE); - if (err) - return err; - - *outlen = 0; - - do { - err = got_inflate_read(&zb, f, &avail); - if (err) - return err; - *outlen += avail; - if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) { - newbuf = reallocarray(*outbuf, 1, - *outlen + GOT_ZSTREAM_BUFSIZE); - if (newbuf == NULL) { - err = got_error_from_errno(); - free(*outbuf); - *outbuf = NULL; - *outlen = 0; - goto done; - } - *outbuf = newbuf; - zb.outbuf = newbuf + *outlen; - zb.outlen = GOT_ZSTREAM_BUFSIZE; - } - } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); - -done: - got_inflate_end(&zb); - return err; -} - -const struct got_error * -got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen, int infd) -{ - const struct got_error *err; - size_t avail; - struct got_zstream_buf zb; - void *newbuf; - - *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE); - if (*outbuf == NULL) - return got_error_from_errno(); - err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE); - if (err) - return err; - - *outlen = 0; - - do { - err = got_inflate_read_fd(&zb, infd, &avail); - if (err) - return err; - *outlen += avail; - if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) { - newbuf = reallocarray(*outbuf, 1, - *outlen + GOT_ZSTREAM_BUFSIZE); - if (newbuf == NULL) { - err = got_error_from_errno(); - free(*outbuf); - *outbuf = NULL; - *outlen = 0; - goto done; - } - *outbuf = newbuf; - zb.outbuf = newbuf + *outlen; - zb.outlen = GOT_ZSTREAM_BUFSIZE; - } - } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); - -done: - got_inflate_end(&zb); - return err; -} - -const struct got_error * -got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map, - size_t offset, size_t len) -{ - const struct got_error *err; - size_t avail; - struct got_zstream_buf zb; - void *newbuf; - size_t consumed; - - *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE); - if (*outbuf == NULL) - return got_error_from_errno(); - err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE); - if (err) - return err; - - *outlen = 0; - - do { - err = got_inflate_read_mmap(&zb, map, offset, len, &avail, - &consumed); - if (err) - return err; - offset += consumed; - len -= consumed; - *outlen += avail; - if (len == 0) - break; - if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) { - newbuf = reallocarray(*outbuf, 1, - *outlen + GOT_ZSTREAM_BUFSIZE); - if (newbuf == NULL) { - err = got_error_from_errno(); - free(*outbuf); - *outbuf = NULL; - *outlen = 0; - goto done; - } - *outbuf = newbuf; - zb.outbuf = newbuf + *outlen; - zb.outlen = GOT_ZSTREAM_BUFSIZE; - } - } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); - -done: - got_inflate_end(&zb); - return err; -} - -const struct got_error * -got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd) -{ - const struct got_error *err = NULL; - size_t avail; - struct got_zstream_buf zb; - - err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE); - if (err) - goto done; - - *outlen = 0; - - do { - err = got_inflate_read(&zb, infile, &avail); - if (err) - return err; - if (avail > 0) { - ssize_t n; - n = write(outfd, zb.outbuf, avail); - if (n != avail) { - err = got_error_from_errno(); - goto done; - } - *outlen += avail; - } - } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); - -done: - if (err == NULL) { - if (lseek(outfd, SEEK_SET, 0) == -1) - err = got_error_from_errno(); - } - got_inflate_end(&zb); - return err; -} - -const struct got_error * -got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile) -{ - const struct got_error *err; - size_t avail; - struct got_zstream_buf zb; - - err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE); - if (err) - goto done; - - *outlen = 0; - - do { - err = got_inflate_read(&zb, infile, &avail); - if (err) - return err; - if (avail > 0) { - size_t n; - n = fwrite(zb.outbuf, avail, 1, outfile); - if (n != 1) { - err = got_ferror(outfile, GOT_ERR_IO); - goto done; - } - *outlen += avail; - } - } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); - -done: - if (err == NULL) - rewind(outfile); - got_inflate_end(&zb); - return err; -} - -const struct got_error * -got_inflate_to_file_fd(size_t *outlen, int infd, FILE *outfile) -{ - const struct got_error *err; - size_t avail; - struct got_zstream_buf zb; - - err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE); - if (err) - goto done; - - *outlen = 0; - - do { - err = got_inflate_read_fd(&zb, infd, &avail); - if (err) - return err; - if (avail > 0) { - size_t n; - n = fwrite(zb.outbuf, avail, 1, outfile); - if (n != 1) { - err = got_ferror(outfile, GOT_ERR_IO); - goto done; - } - *outlen += avail; - } - } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); - -done: - if (err == NULL) - rewind(outfile); - got_inflate_end(&zb); - return err; -} - -const struct got_error * -got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset, - size_t len, FILE *outfile) -{ - const struct got_error *err; - size_t avail; - struct got_zstream_buf zb; - size_t consumed; - - err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE); - if (err) - goto done; - - *outlen = 0; - - do { - err = got_inflate_read_mmap(&zb, map, offset, len, &avail, - &consumed); - if (err) - return err; - offset += consumed; - len -= consumed; - if (avail > 0) { - size_t n; - n = fwrite(zb.outbuf, avail, 1, outfile); - if (n != 1) { - err = got_ferror(outfile, GOT_ERR_IO); - goto done; - } - *outlen += avail; - } - } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); - -done: - if (err == NULL) - rewind(outfile); - got_inflate_end(&zb); - return err; -} blob - 3c421aacff588dc89a664f8b861ef2f60a071b52 blob + 4f7ab90c63f49171d8ec3118b7c0de69fbeafaa8 --- regress/delta/Makefile +++ regress/delta/Makefile @@ -1,7 +1,7 @@ .PATH:${.CURDIR}/../../lib PROG = delta_test -SRCS = delta.c error.c opentemp.c path.c zbuf.c delta_test.c +SRCS = delta.c error.c opentemp.c path.c inflate.c delta_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib LDADD = -lz blob - 7b2e4c1cc0f0fdf61d47bafcc829ab5a949db6df blob + 38e4d55fd16735f3347afa277906a156fcf932ce --- regress/idset/Makefile +++ regress/idset/Makefile @@ -1,7 +1,7 @@ .PATH:${.CURDIR}/../../lib PROG = idset_test -SRCS = error.c object.c privsep.c sha1.c pack.c zbuf.c path.c opentemp.c \ +SRCS = error.c object.c privsep.c sha1.c pack.c inflate.c path.c opentemp.c \ delta.c repository.c reference.c worktree.c fileindex.c \ object_idcache.c object_idset.c idset_test.c blob - cc88d708abda2769b4f800637914f7387beef06e blob + ebadf49748d6cc69d4783ae31b3a1464bacf51c3 --- regress/idset/idset_test.c +++ regress/idset/idset_test.c @@ -30,7 +30,7 @@ #include "got_lib_object_idset.h" #include "got_lib_sha1.h" -#include "got_lib_zbuf.h" +#include "got_lib_inflate.h" #include "got_lib_delta.h" #include "got_lib_object.h" blob - fff00c3507ab78c3b9aab9cca4628b60b28c94a3 blob + 5c429a7ec6ecc0e5e3229fa40e747a5071499c03 --- regress/repository/Makefile +++ regress/repository/Makefile @@ -3,7 +3,7 @@ PROG = repository_test SRCS = path.c repository.c error.c reference.c object.c object_idcache.c \ object_idset.c opentemp.c sha1.c diff.c diffreg.c pack.c privsep.c \ - delta.c fileindex.c worktree.c zbuf.c repository_test.c + delta.c fileindex.c worktree.c inflate.c repository_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib LDADD = -lutil -lz blob - 699a6f2edc3886636e3055d238df66eaf6f338fa blob + f8c274e725508881d9373433bb656d1dfb801112 --- regress/worktree/Makefile +++ regress/worktree/Makefile @@ -3,7 +3,7 @@ PROG = worktree_test SRCS = worktree.c repository.c object.c object_idcache.c object_idset.c \ opentemp.c path.c error.c reference.c sha1.c pack.c privsep.c delta.c \ - zbuf.c fileindex.c worktree_test.c + inflate.c fileindex.c worktree_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib LDADD = -lutil -lz blob - 636134505907c48c61f1d68a1f39a814ef6e14a6 blob + 76ebd6a1e50dc0e61ce0a0a9ad35306dff853b52 --- tog/Makefile +++ tog/Makefile @@ -4,7 +4,7 @@ PROG= tog SRCS= tog.c blame.c commit_graph.c delta.c diff.c diffreg.c error.c \ fileindex.c object.c object_idcache.c object_idset.c \ opentemp.c path.c pack.c privsep.c reference.c repository.c \ - sha1.c worktree.c utf8.c zbuf.c + sha1.c worktree.c utf8.c inflate.c CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib LDADD = -lpanel -lncursesw -lutil -lz