Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sha1.h>
23 #include <zlib.h>
25 #include "got_error.h"
26 #include "got_object.h"
28 #include "got_path_priv.h"
29 #include "got_zb_priv.h"
31 const struct got_error *
32 got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
33 {
34 const struct got_error *err = NULL;
36 memset(zb, 0, sizeof(*zb));
38 zb->z.zalloc = Z_NULL;
39 zb->z.zfree = Z_NULL;
40 if (inflateInit(&zb->z) != Z_OK) {
41 err = got_error(GOT_ERR_IO);
42 goto done;
43 }
45 zb->inlen = zb->outlen = bufsize;
47 zb->inbuf = calloc(1, zb->inlen);
48 if (zb->inbuf == NULL) {
49 err = got_error(GOT_ERR_NO_MEM);
50 goto done;
51 }
53 zb->outbuf = calloc(1, zb->outlen);
54 if (zb->outbuf == NULL) {
55 err = got_error(GOT_ERR_NO_MEM);
56 goto done;
57 }
59 done:
60 if (err)
61 got_inflate_end(zb);
62 return err;
63 }
65 const struct got_error *
66 got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
67 {
68 size_t last_total_out = zb->z.total_out;
69 z_stream *z = &zb->z;
70 int ret;
72 z->next_out = zb->outbuf;
73 z->avail_out = zb->outlen;
75 *outlenp = 0;
76 do {
77 if (z->avail_in == 0) {
78 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
79 if (n == 0) {
80 if (ferror(f))
81 return got_ferror(f, GOT_ERR_IO);
82 break; /* EOF */
83 }
84 z->next_in = zb->inbuf;
85 z->avail_in = n;
86 }
87 ret = inflate(z, Z_SYNC_FLUSH);
88 } while (ret == Z_OK && z->avail_out > 0);
90 if (ret == Z_OK) {
91 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
92 } else {
93 if (ret != Z_STREAM_END)
94 return got_error(GOT_ERR_DECOMPRESSION);
95 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
96 }
98 *outlenp = z->total_out - last_total_out;
99 return NULL;
102 void
103 got_inflate_end(struct got_zstream_buf *zb)
105 free(zb->inbuf);
106 free(zb->outbuf);
107 inflateEnd(&zb->z);
110 const struct got_error *
111 got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
113 const struct got_error *err;
114 size_t avail;
115 struct got_zstream_buf zb;
116 void *newbuf;
118 err = got_inflate_init(&zb, 8192);
119 if (err)
120 return err;
122 *outbuf = NULL;
123 *outlen = 0;
125 do {
126 err = got_inflate_read(&zb, f, &avail);
127 if (err)
128 return err;
129 if (avail > 0) {
130 newbuf = reallocarray(*outbuf, 1, *outlen + avail);
131 if (newbuf == NULL) {
132 free(*outbuf);
133 *outbuf = NULL;
134 *outlen = 0;
135 err = got_error(GOT_ERR_NO_MEM);
136 goto done;
138 memcpy(newbuf + *outlen, zb.outbuf, avail);
139 *outbuf = newbuf;
140 *outlen += avail;
142 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
144 done:
145 got_inflate_end(&zb);
146 return err;
149 const struct got_error *
150 got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
152 const struct got_error *err;
153 size_t avail;
154 struct got_zstream_buf zb;
155 void *newbuf;
157 err = got_inflate_init(&zb, 8192);
158 if (err)
159 goto done;
161 *outlen = 0;
163 do {
164 err = got_inflate_read(&zb, infile, &avail);
165 if (err)
166 return err;
167 if (avail > 0) {
168 size_t n;
169 n = fwrite(zb.outbuf, avail, 1, outfile);
170 if (n != 1) {
171 err = got_ferror(outfile, GOT_ERR_IO);
172 goto done;
174 *outlen += avail;
176 } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
178 done:
179 if (err == NULL)
180 rewind(outfile);
181 got_inflate_end(&zb);
182 return err;