commit a76a38d94e01aee74bea1e13c38979f9d2214291 from: Stefan Sperling date: Sun Mar 11 12:17:56 2018 UTC rename zb.c to zbuf.c commit - c88eb298659dd382406fb947a26d767c08acfebd commit + a76a38d94e01aee74bea1e13c38979f9d2214291 blob - 0b27e5b0b969460a111f3e967b488e6c5629add7 (mode 644) blob + /dev/null --- lib/zb.c +++ /dev/null @@ -1,183 +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 "got_error.h" -#include "got_object.h" - -#include "got_path_priv.h" -#include "got_zb_priv.h" - -const struct got_error * -got_inflate_init(struct got_zstream_buf *zb, size_t bufsize) -{ - const struct got_error *err = NULL; - - memset(zb, 0, sizeof(*zb)); - - 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(GOT_ERR_NO_MEM); - goto done; - } - - zb->outbuf = calloc(1, zb->outlen); - if (zb->outbuf == NULL) { - err = got_error(GOT_ERR_NO_MEM); - goto done; - } - -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->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); - break; /* EOF */ - } - 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; -} - -void -got_inflate_end(struct got_zstream_buf *zb) -{ - free(zb->inbuf); - 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; - - err = got_inflate_init(&zb, 8192); - if (err) - return err; - - *outbuf = NULL; - *outlen = 0; - - do { - err = got_inflate_read(&zb, f, &avail); - if (err) - return err; - if (avail > 0) { - newbuf = reallocarray(*outbuf, 1, *outlen + avail); - if (newbuf == NULL) { - free(*outbuf); - *outbuf = NULL; - *outlen = 0; - err = got_error(GOT_ERR_NO_MEM); - goto done; - } - memcpy(newbuf + *outlen, zb.outbuf, avail); - *outbuf = newbuf; - *outlen += avail; - } - } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); - -done: - 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; - void *newbuf; - - err = got_inflate_init(&zb, 8192); - 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; -} blob - /dev/null blob + 0b27e5b0b969460a111f3e967b488e6c5629add7 (mode 644) --- /dev/null +++ lib/zbuf.c @@ -0,0 +1,183 @@ +/* + * 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 "got_error.h" +#include "got_object.h" + +#include "got_path_priv.h" +#include "got_zb_priv.h" + +const struct got_error * +got_inflate_init(struct got_zstream_buf *zb, size_t bufsize) +{ + const struct got_error *err = NULL; + + memset(zb, 0, sizeof(*zb)); + + 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(GOT_ERR_NO_MEM); + goto done; + } + + zb->outbuf = calloc(1, zb->outlen); + if (zb->outbuf == NULL) { + err = got_error(GOT_ERR_NO_MEM); + goto done; + } + +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->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); + break; /* EOF */ + } + 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; +} + +void +got_inflate_end(struct got_zstream_buf *zb) +{ + free(zb->inbuf); + 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; + + err = got_inflate_init(&zb, 8192); + if (err) + return err; + + *outbuf = NULL; + *outlen = 0; + + do { + err = got_inflate_read(&zb, f, &avail); + if (err) + return err; + if (avail > 0) { + newbuf = reallocarray(*outbuf, 1, *outlen + avail); + if (newbuf == NULL) { + free(*outbuf); + *outbuf = NULL; + *outlen = 0; + err = got_error(GOT_ERR_NO_MEM); + goto done; + } + memcpy(newbuf + *outlen, zb.outbuf, avail); + *outbuf = newbuf; + *outlen += avail; + } + } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE); + +done: + 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; + void *newbuf; + + err = got_inflate_init(&zb, 8192); + 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; +} blob - 50c015abeb22bec57c9b6c433abd755912f97d8b blob + dd454247623767c4d4e65c09c9a876123e36a53f --- regress/delta/Makefile +++ regress/delta/Makefile @@ -1,7 +1,7 @@ .PATH:${.CURDIR}/../../lib PROG = delta_test -SRCS = delta.c error.c path.c zb.c delta_test.c +SRCS = delta.c error.c path.c zbuf.c delta_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib LDADD = -lz blob - 628910d979b4cf3e355218d5ffb6bb645337dfe1 blob + bba0a308884833e6516a3bc570e9b0c592f98cdf --- regress/repository/Makefile +++ regress/repository/Makefile @@ -2,7 +2,7 @@ PROG = repository_test SRCS = path.c repository.c error.c refs.c object.c sha1.c diff.c \ - diffreg.c pack.c delta.c zb.c repository_test.c + diffreg.c pack.c delta.c zbuf.c repository_test.c CPPFLAGS = -I${.CURDIR}/../../include LDADD = -lutil -lz blob - e10b20fba2f9e06be8895d91675f8fc91f3ff522 blob + 5d751acef4a4112fea37f987e888afca0651f9bd --- regress/worktree/Makefile +++ regress/worktree/Makefile @@ -2,7 +2,7 @@ PROG = worktree_test SRCS = worktree.c repository.c object.c path.c error.c refs.c sha1.c pack.c \ - delta.c zb.c worktree_test.c + delta.c zbuf.c worktree_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib LDADD = -lutil -lz