2 a76a38d9 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 a76a38d9 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 a76a38d9 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 a76a38d9 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
8 a76a38d9 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a76a38d9 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a76a38d9 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a76a38d9 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a76a38d9 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a76a38d9 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a76a38d9 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a76a38d9 2018-03-11 stsp #include <sys/queue.h>
19 a76a38d9 2018-03-11 stsp #include <stdio.h>
20 a76a38d9 2018-03-11 stsp #include <stdlib.h>
21 a76a38d9 2018-03-11 stsp #include <string.h>
22 a76a38d9 2018-03-11 stsp #include <sha1.h>
23 a76a38d9 2018-03-11 stsp #include <zlib.h>
25 a76a38d9 2018-03-11 stsp #include "got_error.h"
26 a76a38d9 2018-03-11 stsp #include "got_object.h"
28 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
29 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
31 a76a38d9 2018-03-11 stsp const struct got_error *
32 19d747f7 2018-03-16 stsp got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
34 a76a38d9 2018-03-11 stsp const struct got_error *err = NULL;
36 a76a38d9 2018-03-11 stsp memset(zb, 0, sizeof(*zb));
38 a76a38d9 2018-03-11 stsp zb->z.zalloc = Z_NULL;
39 a76a38d9 2018-03-11 stsp zb->z.zfree = Z_NULL;
40 a76a38d9 2018-03-11 stsp if (inflateInit(&zb->z) != Z_OK) {
41 a76a38d9 2018-03-11 stsp err = got_error(GOT_ERR_IO);
45 a76a38d9 2018-03-11 stsp zb->inlen = zb->outlen = bufsize;
47 a76a38d9 2018-03-11 stsp zb->inbuf = calloc(1, zb->inlen);
48 a76a38d9 2018-03-11 stsp if (zb->inbuf == NULL) {
49 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
53 19d747f7 2018-03-16 stsp if (outbuf == NULL) {
54 19d747f7 2018-03-16 stsp zb->outbuf = calloc(1, zb->outlen);
55 19d747f7 2018-03-16 stsp if (zb->outbuf == NULL) {
56 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
59 19d747f7 2018-03-16 stsp zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
61 19d747f7 2018-03-16 stsp zb->outbuf = outbuf;
65 a76a38d9 2018-03-11 stsp got_inflate_end(zb);
69 a76a38d9 2018-03-11 stsp const struct got_error *
70 a76a38d9 2018-03-11 stsp got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
72 a76a38d9 2018-03-11 stsp size_t last_total_out = zb->z.total_out;
73 a76a38d9 2018-03-11 stsp z_stream *z = &zb->z;
74 92088384 2018-04-01 stsp int ret = Z_ERRNO;
76 a76a38d9 2018-03-11 stsp z->next_out = zb->outbuf;
77 a76a38d9 2018-03-11 stsp z->avail_out = zb->outlen;
79 a76a38d9 2018-03-11 stsp *outlenp = 0;
81 a76a38d9 2018-03-11 stsp if (z->avail_in == 0) {
82 a76a38d9 2018-03-11 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
83 a76a38d9 2018-03-11 stsp if (n == 0) {
84 a76a38d9 2018-03-11 stsp if (ferror(f))
85 a76a38d9 2018-03-11 stsp return got_ferror(f, GOT_ERR_IO);
87 92088384 2018-04-01 stsp ret = Z_STREAM_END;
90 a76a38d9 2018-03-11 stsp z->next_in = zb->inbuf;
91 a76a38d9 2018-03-11 stsp z->avail_in = n;
93 a76a38d9 2018-03-11 stsp ret = inflate(z, Z_SYNC_FLUSH);
94 a76a38d9 2018-03-11 stsp } while (ret == Z_OK && z->avail_out > 0);
96 a76a38d9 2018-03-11 stsp if (ret == Z_OK) {
97 a76a38d9 2018-03-11 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
99 a76a38d9 2018-03-11 stsp if (ret != Z_STREAM_END)
100 a76a38d9 2018-03-11 stsp return got_error(GOT_ERR_DECOMPRESSION);
101 a76a38d9 2018-03-11 stsp zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
104 a76a38d9 2018-03-11 stsp *outlenp = z->total_out - last_total_out;
105 a76a38d9 2018-03-11 stsp return NULL;
109 a76a38d9 2018-03-11 stsp got_inflate_end(struct got_zstream_buf *zb)
111 a76a38d9 2018-03-11 stsp free(zb->inbuf);
112 19d747f7 2018-03-16 stsp if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
113 19d747f7 2018-03-16 stsp free(zb->outbuf);
114 a76a38d9 2018-03-11 stsp inflateEnd(&zb->z);
117 a76a38d9 2018-03-11 stsp const struct got_error *
118 a76a38d9 2018-03-11 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
120 a76a38d9 2018-03-11 stsp const struct got_error *err;
121 a76a38d9 2018-03-11 stsp size_t avail;
122 a76a38d9 2018-03-11 stsp struct got_zstream_buf zb;
123 a76a38d9 2018-03-11 stsp void *newbuf;
125 19d747f7 2018-03-16 stsp *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
126 19d747f7 2018-03-16 stsp if (*outbuf == NULL)
127 0a585a0d 2018-03-17 stsp return got_error_from_errno();
128 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
130 a76a38d9 2018-03-11 stsp return err;
132 a76a38d9 2018-03-11 stsp *outlen = 0;
135 a76a38d9 2018-03-11 stsp err = got_inflate_read(&zb, f, &avail);
137 a76a38d9 2018-03-11 stsp return err;
138 19d747f7 2018-03-16 stsp *outlen += avail;
139 19d747f7 2018-03-16 stsp if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
140 19d747f7 2018-03-16 stsp newbuf = reallocarray(*outbuf, 1,
141 19d747f7 2018-03-16 stsp *outlen + GOT_ZSTREAM_BUFSIZE);
142 a76a38d9 2018-03-11 stsp if (newbuf == NULL) {
143 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
144 a76a38d9 2018-03-11 stsp free(*outbuf);
145 a76a38d9 2018-03-11 stsp *outbuf = NULL;
146 a76a38d9 2018-03-11 stsp *outlen = 0;
149 a76a38d9 2018-03-11 stsp *outbuf = newbuf;
150 19d747f7 2018-03-16 stsp zb.outbuf = newbuf + *outlen;
151 19d747f7 2018-03-16 stsp zb.outlen = GOT_ZSTREAM_BUFSIZE;
153 a76a38d9 2018-03-11 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
156 a76a38d9 2018-03-11 stsp got_inflate_end(&zb);
157 a76a38d9 2018-03-11 stsp return err;
160 a76a38d9 2018-03-11 stsp const struct got_error *
161 a76a38d9 2018-03-11 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
163 a76a38d9 2018-03-11 stsp const struct got_error *err;
164 a76a38d9 2018-03-11 stsp size_t avail;
165 a76a38d9 2018-03-11 stsp struct got_zstream_buf zb;
167 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
171 a76a38d9 2018-03-11 stsp *outlen = 0;
174 a76a38d9 2018-03-11 stsp err = got_inflate_read(&zb, infile, &avail);
176 a76a38d9 2018-03-11 stsp return err;
177 a76a38d9 2018-03-11 stsp if (avail > 0) {
179 a76a38d9 2018-03-11 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
180 a76a38d9 2018-03-11 stsp if (n != 1) {
181 a76a38d9 2018-03-11 stsp err = got_ferror(outfile, GOT_ERR_IO);
184 a76a38d9 2018-03-11 stsp *outlen += avail;
186 a76a38d9 2018-03-11 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
189 a76a38d9 2018-03-11 stsp if (err == NULL)
190 a76a38d9 2018-03-11 stsp rewind(outfile);
191 a76a38d9 2018-03-11 stsp got_inflate_end(&zb);
192 a76a38d9 2018-03-11 stsp return err;