Blame


1 a76a38d9 2018-03-11 stsp /*
2 a76a38d9 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 a76a38d9 2018-03-11 stsp *
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.
7 a76a38d9 2018-03-11 stsp *
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.
15 a76a38d9 2018-03-11 stsp */
16 a76a38d9 2018-03-11 stsp
17 a76a38d9 2018-03-11 stsp #include <sys/queue.h>
18 a76a38d9 2018-03-11 stsp
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>
24 a76a38d9 2018-03-11 stsp
25 a76a38d9 2018-03-11 stsp #include "got_error.h"
26 a76a38d9 2018-03-11 stsp #include "got_object.h"
27 a76a38d9 2018-03-11 stsp
28 32cb896c 2018-03-11 stsp #include "got_path_lib.h"
29 32cb896c 2018-03-11 stsp #include "got_zbuf_lib.h"
30 a76a38d9 2018-03-11 stsp
31 a76a38d9 2018-03-11 stsp const struct got_error *
32 a76a38d9 2018-03-11 stsp got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
33 a76a38d9 2018-03-11 stsp {
34 a76a38d9 2018-03-11 stsp const struct got_error *err = NULL;
35 a76a38d9 2018-03-11 stsp
36 a76a38d9 2018-03-11 stsp memset(zb, 0, sizeof(*zb));
37 a76a38d9 2018-03-11 stsp
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);
42 a76a38d9 2018-03-11 stsp goto done;
43 a76a38d9 2018-03-11 stsp }
44 a76a38d9 2018-03-11 stsp
45 a76a38d9 2018-03-11 stsp zb->inlen = zb->outlen = bufsize;
46 a76a38d9 2018-03-11 stsp
47 a76a38d9 2018-03-11 stsp zb->inbuf = calloc(1, zb->inlen);
48 a76a38d9 2018-03-11 stsp if (zb->inbuf == NULL) {
49 a76a38d9 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
50 a76a38d9 2018-03-11 stsp goto done;
51 a76a38d9 2018-03-11 stsp }
52 a76a38d9 2018-03-11 stsp
53 a76a38d9 2018-03-11 stsp zb->outbuf = calloc(1, zb->outlen);
54 a76a38d9 2018-03-11 stsp if (zb->outbuf == NULL) {
55 a76a38d9 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
56 a76a38d9 2018-03-11 stsp goto done;
57 a76a38d9 2018-03-11 stsp }
58 a76a38d9 2018-03-11 stsp
59 a76a38d9 2018-03-11 stsp done:
60 a76a38d9 2018-03-11 stsp if (err)
61 a76a38d9 2018-03-11 stsp got_inflate_end(zb);
62 a76a38d9 2018-03-11 stsp return err;
63 a76a38d9 2018-03-11 stsp }
64 a76a38d9 2018-03-11 stsp
65 a76a38d9 2018-03-11 stsp const struct got_error *
66 a76a38d9 2018-03-11 stsp got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
67 a76a38d9 2018-03-11 stsp {
68 a76a38d9 2018-03-11 stsp size_t last_total_out = zb->z.total_out;
69 a76a38d9 2018-03-11 stsp z_stream *z = &zb->z;
70 a76a38d9 2018-03-11 stsp int ret;
71 a76a38d9 2018-03-11 stsp
72 a76a38d9 2018-03-11 stsp z->next_out = zb->outbuf;
73 a76a38d9 2018-03-11 stsp z->avail_out = zb->outlen;
74 a76a38d9 2018-03-11 stsp
75 a76a38d9 2018-03-11 stsp *outlenp = 0;
76 a76a38d9 2018-03-11 stsp do {
77 a76a38d9 2018-03-11 stsp if (z->avail_in == 0) {
78 a76a38d9 2018-03-11 stsp size_t n = fread(zb->inbuf, 1, zb->inlen, f);
79 a76a38d9 2018-03-11 stsp if (n == 0) {
80 a76a38d9 2018-03-11 stsp if (ferror(f))
81 a76a38d9 2018-03-11 stsp return got_ferror(f, GOT_ERR_IO);
82 a76a38d9 2018-03-11 stsp break; /* EOF */
83 a76a38d9 2018-03-11 stsp }
84 a76a38d9 2018-03-11 stsp z->next_in = zb->inbuf;
85 a76a38d9 2018-03-11 stsp z->avail_in = n;
86 a76a38d9 2018-03-11 stsp }
87 a76a38d9 2018-03-11 stsp ret = inflate(z, Z_SYNC_FLUSH);
88 a76a38d9 2018-03-11 stsp } while (ret == Z_OK && z->avail_out > 0);
89 a76a38d9 2018-03-11 stsp
90 a76a38d9 2018-03-11 stsp if (ret == Z_OK) {
91 a76a38d9 2018-03-11 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
92 a76a38d9 2018-03-11 stsp } else {
93 a76a38d9 2018-03-11 stsp if (ret != Z_STREAM_END)
94 a76a38d9 2018-03-11 stsp return got_error(GOT_ERR_DECOMPRESSION);
95 a76a38d9 2018-03-11 stsp zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
96 a76a38d9 2018-03-11 stsp }
97 a76a38d9 2018-03-11 stsp
98 a76a38d9 2018-03-11 stsp *outlenp = z->total_out - last_total_out;
99 a76a38d9 2018-03-11 stsp return NULL;
100 a76a38d9 2018-03-11 stsp }
101 a76a38d9 2018-03-11 stsp
102 a76a38d9 2018-03-11 stsp void
103 a76a38d9 2018-03-11 stsp got_inflate_end(struct got_zstream_buf *zb)
104 a76a38d9 2018-03-11 stsp {
105 a76a38d9 2018-03-11 stsp free(zb->inbuf);
106 a76a38d9 2018-03-11 stsp free(zb->outbuf);
107 a76a38d9 2018-03-11 stsp inflateEnd(&zb->z);
108 a76a38d9 2018-03-11 stsp }
109 a76a38d9 2018-03-11 stsp
110 a76a38d9 2018-03-11 stsp const struct got_error *
111 a76a38d9 2018-03-11 stsp got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
112 a76a38d9 2018-03-11 stsp {
113 a76a38d9 2018-03-11 stsp const struct got_error *err;
114 a76a38d9 2018-03-11 stsp size_t avail;
115 a76a38d9 2018-03-11 stsp struct got_zstream_buf zb;
116 a76a38d9 2018-03-11 stsp void *newbuf;
117 a76a38d9 2018-03-11 stsp
118 a76a38d9 2018-03-11 stsp err = got_inflate_init(&zb, 8192);
119 a76a38d9 2018-03-11 stsp if (err)
120 a76a38d9 2018-03-11 stsp return err;
121 a76a38d9 2018-03-11 stsp
122 a76a38d9 2018-03-11 stsp *outbuf = NULL;
123 a76a38d9 2018-03-11 stsp *outlen = 0;
124 a76a38d9 2018-03-11 stsp
125 a76a38d9 2018-03-11 stsp do {
126 a76a38d9 2018-03-11 stsp err = got_inflate_read(&zb, f, &avail);
127 a76a38d9 2018-03-11 stsp if (err)
128 a76a38d9 2018-03-11 stsp return err;
129 a76a38d9 2018-03-11 stsp if (avail > 0) {
130 a76a38d9 2018-03-11 stsp newbuf = reallocarray(*outbuf, 1, *outlen + avail);
131 a76a38d9 2018-03-11 stsp if (newbuf == NULL) {
132 a76a38d9 2018-03-11 stsp free(*outbuf);
133 a76a38d9 2018-03-11 stsp *outbuf = NULL;
134 a76a38d9 2018-03-11 stsp *outlen = 0;
135 a76a38d9 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
136 a76a38d9 2018-03-11 stsp goto done;
137 a76a38d9 2018-03-11 stsp }
138 a76a38d9 2018-03-11 stsp memcpy(newbuf + *outlen, zb.outbuf, avail);
139 a76a38d9 2018-03-11 stsp *outbuf = newbuf;
140 a76a38d9 2018-03-11 stsp *outlen += avail;
141 a76a38d9 2018-03-11 stsp }
142 a76a38d9 2018-03-11 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
143 a76a38d9 2018-03-11 stsp
144 a76a38d9 2018-03-11 stsp done:
145 a76a38d9 2018-03-11 stsp got_inflate_end(&zb);
146 a76a38d9 2018-03-11 stsp return err;
147 a76a38d9 2018-03-11 stsp }
148 a76a38d9 2018-03-11 stsp
149 a76a38d9 2018-03-11 stsp const struct got_error *
150 a76a38d9 2018-03-11 stsp got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
151 a76a38d9 2018-03-11 stsp {
152 a76a38d9 2018-03-11 stsp const struct got_error *err;
153 a76a38d9 2018-03-11 stsp size_t avail;
154 a76a38d9 2018-03-11 stsp struct got_zstream_buf zb;
155 a76a38d9 2018-03-11 stsp void *newbuf;
156 a76a38d9 2018-03-11 stsp
157 a76a38d9 2018-03-11 stsp err = got_inflate_init(&zb, 8192);
158 a76a38d9 2018-03-11 stsp if (err)
159 a76a38d9 2018-03-11 stsp goto done;
160 a76a38d9 2018-03-11 stsp
161 a76a38d9 2018-03-11 stsp *outlen = 0;
162 a76a38d9 2018-03-11 stsp
163 a76a38d9 2018-03-11 stsp do {
164 a76a38d9 2018-03-11 stsp err = got_inflate_read(&zb, infile, &avail);
165 a76a38d9 2018-03-11 stsp if (err)
166 a76a38d9 2018-03-11 stsp return err;
167 a76a38d9 2018-03-11 stsp if (avail > 0) {
168 a76a38d9 2018-03-11 stsp size_t n;
169 a76a38d9 2018-03-11 stsp n = fwrite(zb.outbuf, avail, 1, outfile);
170 a76a38d9 2018-03-11 stsp if (n != 1) {
171 a76a38d9 2018-03-11 stsp err = got_ferror(outfile, GOT_ERR_IO);
172 a76a38d9 2018-03-11 stsp goto done;
173 a76a38d9 2018-03-11 stsp }
174 a76a38d9 2018-03-11 stsp *outlen += avail;
175 a76a38d9 2018-03-11 stsp }
176 a76a38d9 2018-03-11 stsp } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
177 a76a38d9 2018-03-11 stsp
178 a76a38d9 2018-03-11 stsp done:
179 a76a38d9 2018-03-11 stsp if (err == NULL)
180 a76a38d9 2018-03-11 stsp rewind(outfile);
181 a76a38d9 2018-03-11 stsp got_inflate_end(&zb);
182 a76a38d9 2018-03-11 stsp return err;
183 a76a38d9 2018-03-11 stsp }