2 4ca7b755 2018-01-26 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 4ca7b755 2018-01-26 stsp * Permission to use, copy, modify, and distribute this software for any
5 4ca7b755 2018-01-26 stsp * purpose with or without fee is hereby granted, provided that the above
6 4ca7b755 2018-01-26 stsp * copyright notice and this permission notice appear in all copies.
8 4ca7b755 2018-01-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 4ca7b755 2018-01-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 4ca7b755 2018-01-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 4ca7b755 2018-01-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 4ca7b755 2018-01-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 4ca7b755 2018-01-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 4ca7b755 2018-01-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4ca7b755 2018-01-26 stsp #include <sys/queue.h>
19 4ca7b755 2018-01-26 stsp #include <stdio.h>
20 4ca7b755 2018-01-26 stsp #include <stdlib.h>
21 4ca7b755 2018-01-26 stsp #include <string.h>
22 4ca7b755 2018-01-26 stsp #include <sha1.h>
23 4ca7b755 2018-01-26 stsp #include <zlib.h>
25 4ca7b755 2018-01-26 stsp #include "got_error.h"
26 4ca7b755 2018-01-26 stsp #include "got_object.h"
28 885d3e02 2018-01-27 stsp #include "path.h"
29 4ca7b755 2018-01-26 stsp #include "zb.h"
31 4ca7b755 2018-01-26 stsp const struct got_error *
32 4ca7b755 2018-01-26 stsp got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
34 4ca7b755 2018-01-26 stsp const struct got_error *err = NULL;
36 4ca7b755 2018-01-26 stsp memset(zb, 0, sizeof(*zb));
38 4ca7b755 2018-01-26 stsp zb->z.zalloc = Z_NULL;
39 4ca7b755 2018-01-26 stsp zb->z.zfree = Z_NULL;
40 4ca7b755 2018-01-26 stsp if (inflateInit(&zb->z) != Z_OK) {
41 4ca7b755 2018-01-26 stsp err = got_error(GOT_ERR_IO);
45 4ca7b755 2018-01-26 stsp zb->inlen = zb->outlen = bufsize;
47 4ca7b755 2018-01-26 stsp zb->inbuf = calloc(1, zb->inlen);
48 4ca7b755 2018-01-26 stsp if (zb->inbuf == NULL) {
49 4ca7b755 2018-01-26 stsp err = got_error(GOT_ERR_NO_MEM);
53 4ca7b755 2018-01-26 stsp zb->outbuf = calloc(1, zb->outlen);
54 4ca7b755 2018-01-26 stsp if (zb->outbuf == NULL) {
55 4ca7b755 2018-01-26 stsp err = got_error(GOT_ERR_NO_MEM);
61 4ca7b755 2018-01-26 stsp got_inflate_end(zb);
65 4ca7b755 2018-01-26 stsp const struct got_error *
66 885d3e02 2018-01-27 stsp got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *inlenp,
67 885d3e02 2018-01-27 stsp size_t *outlenp)
69 4ca7b755 2018-01-26 stsp size_t last_total_out = zb->z.total_out;
70 4ca7b755 2018-01-26 stsp z_stream *z = &zb->z;
73 4ca7b755 2018-01-26 stsp z->next_out = zb->outbuf;
74 4ca7b755 2018-01-26 stsp z->avail_out = zb->outlen;
76 6336b73f 2018-01-27 stsp *outlenp = 0;
78 885d3e02 2018-01-27 stsp *inlenp = 0;
80 4ca7b755 2018-01-26 stsp if (z->avail_in == 0) {
81 885d3e02 2018-01-27 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
82 4ca7b755 2018-01-26 stsp if (n == 0) {
83 4ca7b755 2018-01-26 stsp if (ferror(f))
84 4ca7b755 2018-01-26 stsp return got_ferror(f, GOT_ERR_IO);
85 6336b73f 2018-01-27 stsp break; /* EOF */
87 4ca7b755 2018-01-26 stsp z->next_in = zb->inbuf;
88 4ca7b755 2018-01-26 stsp z->avail_in = n;
90 885d3e02 2018-01-27 stsp *inlenp += n;
92 4ca7b755 2018-01-26 stsp ret = inflate(z, Z_SYNC_FLUSH);
93 4ca7b755 2018-01-26 stsp } while (ret == Z_OK && z->avail_out > 0);
95 4ca7b755 2018-01-26 stsp if (ret != Z_OK) {
96 4ca7b755 2018-01-26 stsp if (ret != Z_STREAM_END)
97 4ca7b755 2018-01-26 stsp return got_error(GOT_ERR_DECOMPRESSION);
98 4ca7b755 2018-01-26 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
101 4ca7b755 2018-01-26 stsp *outlenp = z->total_out - last_total_out;
102 4ca7b755 2018-01-26 stsp return NULL;
106 4ca7b755 2018-01-26 stsp got_inflate_end(struct got_zstream_buf *zb)
108 4ca7b755 2018-01-26 stsp free(zb->inbuf);
109 4ca7b755 2018-01-26 stsp free(zb->outbuf);
110 4ca7b755 2018-01-26 stsp inflateEnd(&zb->z);
113 885d3e02 2018-01-27 stsp const struct got_error *
114 885d3e02 2018-01-27 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f, size_t insize)
116 885d3e02 2018-01-27 stsp const struct got_error *err;
117 885d3e02 2018-01-27 stsp size_t inbytes, consumed, avail;
118 885d3e02 2018-01-27 stsp struct got_zstream_buf zb;
119 885d3e02 2018-01-27 stsp void *newbuf;
121 885d3e02 2018-01-27 stsp err = got_inflate_init(&zb, 8192);
123 885d3e02 2018-01-27 stsp return err;
125 885d3e02 2018-01-27 stsp *outbuf = NULL;
126 885d3e02 2018-01-27 stsp *outlen = 0;
127 885d3e02 2018-01-27 stsp inbytes = 0;
129 54382dcc 2018-02-11 stsp while (1) {
130 885d3e02 2018-01-27 stsp err = got_inflate_read(&zb, f, &consumed, &avail);
132 885d3e02 2018-01-27 stsp return err;
133 885d3e02 2018-01-27 stsp inbytes += consumed;
134 885d3e02 2018-01-27 stsp if (avail == 0) {
135 54382dcc 2018-02-11 stsp if (insize && inbytes < insize)
136 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
139 885d3e02 2018-01-27 stsp newbuf = reallocarray(*outbuf, 1, *outlen + avail);
140 885d3e02 2018-01-27 stsp if (newbuf == NULL) {
141 885d3e02 2018-01-27 stsp free(*outbuf);
142 885d3e02 2018-01-27 stsp *outbuf = NULL;
143 885d3e02 2018-01-27 stsp *outlen = 0;
144 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_NO_MEM);
147 885d3e02 2018-01-27 stsp memcpy(newbuf + *outlen, zb.outbuf, avail);
148 885d3e02 2018-01-27 stsp *outbuf = newbuf;
149 885d3e02 2018-01-27 stsp *outlen += avail;
153 885d3e02 2018-01-27 stsp got_inflate_end(&zb);
154 885d3e02 2018-01-27 stsp return err;
157 885d3e02 2018-01-27 stsp const struct got_error *
158 3606d7fc 2018-02-11 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
160 885d3e02 2018-01-27 stsp const struct got_error *err;
161 885d3e02 2018-01-27 stsp size_t avail;
162 885d3e02 2018-01-27 stsp struct got_zstream_buf zb;
163 885d3e02 2018-01-27 stsp void *newbuf;
165 885d3e02 2018-01-27 stsp err = got_inflate_init(&zb, 8192);
169 885d3e02 2018-01-27 stsp *outlen = 0;
172 3606d7fc 2018-02-11 stsp err = got_inflate_read(&zb, infile, NULL, &avail);
174 885d3e02 2018-01-27 stsp return err;
175 885d3e02 2018-01-27 stsp if (avail > 0) {
177 3606d7fc 2018-02-11 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
178 885d3e02 2018-01-27 stsp if (n != 1) {
179 3606d7fc 2018-02-11 stsp err = got_ferror(outfile, GOT_ERR_IO);
182 885d3e02 2018-01-27 stsp *outlen += avail;
184 885d3e02 2018-01-27 stsp } while (avail > 0);
187 3606d7fc 2018-02-11 stsp if (err == NULL)
188 3606d7fc 2018-02-11 stsp rewind(outfile);
189 885d3e02 2018-01-27 stsp got_inflate_end(&zb);
190 885d3e02 2018-01-27 stsp return err;