Line | Count | Source |
1 | | /* gzread.c -- zlib functions for reading gzip files |
2 | | * Copyright (C) 2004-2026 Mark Adler |
3 | | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | | */ |
5 | | |
6 | | #include "zbuild.h" |
7 | | #include "zutil_p.h" |
8 | | #include "gzguts.h" |
9 | | #include "gzread_mangle.h" |
10 | | |
11 | | /* Local functions */ |
12 | | static int gz_read_init(gz_state *state); |
13 | | static int gz_load(gz_state *, unsigned char *, unsigned, unsigned *); |
14 | | static int gz_avail(gz_state *); |
15 | | static int gz_look(gz_state *); |
16 | | static int gz_decomp(gz_state *); |
17 | | static int gz_fetch(gz_state *); |
18 | | static int gz_skip(gz_state *); |
19 | | static size_t gz_read(gz_state *, void *, size_t); |
20 | | |
21 | 14.5k | static int gz_read_init(gz_state *state) { |
22 | | /* Allocate gz buffers */ |
23 | 14.5k | if (gz_buffer_alloc(state) != 0) { |
24 | 0 | PREFIX(gz_error)(state, Z_MEM_ERROR, "out of memory"); |
25 | 0 | return -1; |
26 | 0 | } |
27 | | |
28 | | /* Initialize inflate state */ |
29 | 14.5k | int ret = PREFIX(inflateInit2)(&(state->strm), MAX_WBITS + 16); |
30 | 14.5k | if (ret != Z_OK) { |
31 | 0 | gz_buffer_free(state); |
32 | 0 | if (ret == Z_MEM_ERROR) { |
33 | 0 | PREFIX(gz_error)(state, Z_MEM_ERROR, "out of memory"); |
34 | 0 | } else { |
35 | 0 | PREFIX(gz_error)(state, Z_STREAM_ERROR, "invalid compression parameters"); |
36 | 0 | } |
37 | 0 | return -1; |
38 | 0 | } |
39 | 14.5k | return 0; |
40 | 14.5k | } |
41 | | |
42 | | /* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from |
43 | | state->fd, and update state->eof, state->err, and state->msg as appropriate. |
44 | | This function needs to loop on read(), since read() is not guaranteed to |
45 | | read the number of bytes requested, depending on the type of descriptor. */ |
46 | 16.7k | static int gz_load(gz_state *state, unsigned char *buf, unsigned len, unsigned *have) { |
47 | 16.7k | ssize_t ret; |
48 | | |
49 | 16.7k | *have = 0; |
50 | 31.3k | do { |
51 | 31.3k | ret = read(state->fd, buf + *have, len - *have); |
52 | 31.3k | if (ret <= 0) |
53 | 14.5k | break; |
54 | 16.7k | *have += (unsigned)ret; |
55 | 16.7k | } while (*have < len); |
56 | 16.7k | if (ret < 0) { |
57 | 0 | PREFIX(gz_error)(state, Z_ERRNO, zstrerror()); |
58 | 0 | return -1; |
59 | 0 | } |
60 | 16.7k | if (ret == 0) |
61 | 14.5k | state->eof = 1; |
62 | 16.7k | return 0; |
63 | 16.7k | } |
64 | | |
65 | | /* Load up input buffer and set eof flag if last data loaded -- return -1 on |
66 | | error, 0 otherwise. Note that the eof flag is set when the end of the input |
67 | | file is reached, even though there may be unused data in the buffer. Once |
68 | | that data has been used, no more attempts will be made to read the file. |
69 | | If strm->avail_in != 0, then the current data is moved to the beginning of |
70 | | the input buffer, and then the remainder of the buffer is loaded with the |
71 | | available data from the input file. */ |
72 | 16.7k | static int gz_avail(gz_state *state) { |
73 | 16.7k | unsigned got; |
74 | 16.7k | PREFIX3(stream) *strm = &(state->strm); |
75 | | |
76 | 16.7k | if (state->err != Z_OK && state->err != Z_BUF_ERROR) |
77 | 0 | return -1; |
78 | 16.7k | if (state->eof == 0) { |
79 | 16.7k | if (strm->avail_in) { /* copy what's there to the start */ |
80 | 0 | unsigned char *p = state->in; |
81 | 0 | unsigned const char *q = strm->next_in; |
82 | 0 | if (q != p) { |
83 | 0 | unsigned n = strm->avail_in; |
84 | 0 | do { |
85 | 0 | *p++ = *q++; |
86 | 0 | } while (--n); |
87 | 0 | } |
88 | 0 | } |
89 | 16.7k | if (gz_load(state, state->in + strm->avail_in, state->size - strm->avail_in, &got) == -1) |
90 | 0 | return -1; |
91 | 16.7k | strm->avail_in += got; |
92 | 16.7k | strm->next_in = state->in; |
93 | 16.7k | } |
94 | 16.7k | return 0; |
95 | 16.7k | } |
96 | | |
97 | | /* Look for gzip header, set up for inflate or copy. state->x.have must be 0. |
98 | | If this is the first time in, allocate required memory. state->how will be |
99 | | left unchanged if there is no more input data available, will be set to COPY |
100 | | if there is no gzip header and direct copying will be performed, or it will |
101 | | be set to GZIP for decompression. If direct copying, then leftover input |
102 | | data from the input buffer will be copied to the output buffer. In that |
103 | | case, all further file reads will be directly to either the output buffer or |
104 | | a user buffer. If decompressing, the inflate state will be initialized. |
105 | | gz_look() will return 0 on success or -1 on failure. */ |
106 | 14.5k | static int gz_look(gz_state *state) { |
107 | 14.5k | PREFIX3(stream) *strm = &(state->strm); |
108 | | |
109 | | /* allocate memory if this is the first time through */ |
110 | 14.5k | if (state->size == 0 && gz_read_init(state) == -1) |
111 | 0 | return -1; |
112 | | |
113 | | /* if transparent reading is disabled, which would only be at the start, or |
114 | | if we're looking for a gzip member after the first one, which is not at |
115 | | the start, then proceed directly to look for a gzip member next */ |
116 | 14.5k | if (state->direct == -1) { |
117 | 0 | PREFIX(inflateReset)(strm); |
118 | 0 | state->how = GZIP; |
119 | 0 | state->direct = 0; |
120 | 0 | return 0; |
121 | 0 | } |
122 | | |
123 | | /* get at least the magic bytes in the input buffer */ |
124 | 14.5k | if (strm->avail_in < 2) { |
125 | 14.5k | if (gz_avail(state) == -1) |
126 | 0 | return -1; |
127 | 14.5k | if (strm->avail_in == 0) |
128 | 6 | return 0; |
129 | 14.5k | } |
130 | | |
131 | | /* look for gzip magic bytes -- if there, do gzip decoding (note: there is |
132 | | a logical dilemma here when considering the case of a partially written |
133 | | gzip file, to wit, if a single 31 byte is written, then we cannot tell |
134 | | whether this is a single-byte file, or just a partially written gzip |
135 | | file -- for here we assume that if a gzip file is being written, then |
136 | | the header will be written in a single operation, so that reading a |
137 | | single byte is sufficient indication that it is not a gzip file) */ |
138 | 14.5k | if (strm->avail_in > 1 && |
139 | 14.5k | strm->next_in[0] == 31 && strm->next_in[1] == 139) { |
140 | 14.5k | PREFIX(inflateReset)(strm); |
141 | 14.5k | state->how = GZIP; |
142 | 14.5k | state->direct = 0; |
143 | 14.5k | return 0; |
144 | 14.5k | } |
145 | | |
146 | | /* no gzip header -- if we were decoding gzip before, then this is trailing |
147 | | garbage. Ignore the trailing garbage and finish. */ |
148 | 0 | if (state->direct == 0) { |
149 | 0 | strm->avail_in = 0; |
150 | 0 | state->eof = 1; |
151 | 0 | state->x.have = 0; |
152 | 0 | return 0; |
153 | 0 | } |
154 | | |
155 | | /* doing raw i/o, copy any leftover input to output -- this assumes that |
156 | | the output buffer is larger than the input buffer, which also assures |
157 | | space for gzungetc() */ |
158 | 0 | state->x.next = state->out; |
159 | 0 | memcpy(state->x.next, strm->next_in, strm->avail_in); |
160 | 0 | state->x.have = strm->avail_in; |
161 | 0 | strm->avail_in = 0; |
162 | 0 | state->how = COPY; |
163 | 0 | state->direct = 1; |
164 | 0 | return 0; |
165 | 0 | } |
166 | | |
167 | | /* Decompress from input to the provided next_out and avail_out in the state. |
168 | | On return, state->x.have and state->x.next point to the just decompressed |
169 | | data. If the gzip stream completes, state->how is reset to LOOK to look for |
170 | | the next gzip stream or raw data, once state->x.have is depleted. Returns 0 |
171 | | on success, -1 on failure. */ |
172 | 15.6k | static int gz_decomp(gz_state *state) { |
173 | 15.6k | int ret = Z_OK; |
174 | 15.6k | unsigned had; |
175 | 15.6k | PREFIX3(stream) *strm = &(state->strm); |
176 | | |
177 | | /* fill output buffer up to end of deflate stream */ |
178 | 15.6k | had = strm->avail_out; |
179 | 17.9k | do { |
180 | | /* get more input for inflate() */ |
181 | 17.9k | if (strm->avail_in == 0 && gz_avail(state) == -1) |
182 | 0 | return -1; |
183 | 17.9k | if (strm->avail_in == 0) { |
184 | 0 | PREFIX(gz_error)(state, Z_BUF_ERROR, "unexpected end of file"); |
185 | 0 | break; |
186 | 0 | } |
187 | | |
188 | | /* decompress and handle errors */ |
189 | 17.9k | ret = PREFIX(inflate)(strm, Z_NO_FLUSH); |
190 | 17.9k | if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) { |
191 | 0 | PREFIX(gz_error)(state, Z_STREAM_ERROR, "internal error: inflate stream corrupt"); |
192 | 0 | return -1; |
193 | 0 | } |
194 | 17.9k | if (ret == Z_MEM_ERROR) { |
195 | 0 | PREFIX(gz_error)(state, Z_MEM_ERROR, "out of memory"); |
196 | 0 | return -1; |
197 | 0 | } |
198 | 17.9k | if (ret == Z_DATA_ERROR) { /* deflate stream invalid */ |
199 | 0 | PREFIX(gz_error)(state, Z_DATA_ERROR, strm->msg == NULL ? "compressed data error" : strm->msg); |
200 | 0 | return -1; |
201 | 0 | } |
202 | 17.9k | } while (strm->avail_out && ret != Z_STREAM_END); |
203 | | |
204 | | /* update available output */ |
205 | 15.6k | state->x.have = had - strm->avail_out; |
206 | 15.6k | state->x.next = strm->next_out - state->x.have; |
207 | | |
208 | | /* if the gzip stream completed successfully, look for another */ |
209 | 15.6k | if (ret == Z_STREAM_END) |
210 | 14.5k | state->how = LOOK; |
211 | | |
212 | | /* good decompression */ |
213 | 15.6k | return 0; |
214 | 15.6k | } |
215 | | |
216 | | /* Fetch data and put it in the output buffer. Assumes state->x.have is 0. |
217 | | Data is either copied from the input file or decompressed from the input |
218 | | file depending on state->how. If state->how is LOOK, then a gzip header is |
219 | | looked for to determine whether to copy or decompress. Returns -1 on error, |
220 | | otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the |
221 | | end of the input file has been reached and all data has been processed. */ |
222 | 15.6k | static int gz_fetch(gz_state *state) { |
223 | 15.6k | PREFIX3(stream) *strm = &(state->strm); |
224 | 15.6k | Assert(state->x.have == 0, "Invalid state->x.have"); |
225 | | |
226 | 30.2k | do { |
227 | 30.2k | switch (state->how) { |
228 | 14.5k | case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */ |
229 | 14.5k | if (gz_look(state) == -1) |
230 | 0 | return -1; |
231 | 14.5k | if (state->how == LOOK) |
232 | 6 | return 0; |
233 | 14.5k | break; |
234 | 14.5k | case COPY: /* -> COPY */ |
235 | 0 | if (gz_load(state, state->out, state->size << 1, &(state->x.have)) |
236 | 0 | == -1) |
237 | 0 | return -1; |
238 | 0 | state->x.next = state->out; |
239 | 0 | return 0; |
240 | 15.6k | case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */ |
241 | 15.6k | strm->avail_out = state->size << 1; |
242 | 15.6k | strm->next_out = state->out; |
243 | 15.6k | if (gz_decomp(state) == -1) |
244 | 0 | return -1; |
245 | 15.6k | continue; |
246 | 15.6k | default: // Can't happen |
247 | 0 | Z_UNREACHABLE(); |
248 | 0 | #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 202311L |
249 | 0 | return -1; |
250 | 30.2k | #endif |
251 | 30.2k | } |
252 | 30.2k | } while (state->x.have == 0 && (!state->eof || strm->avail_in)); |
253 | 15.6k | return 0; |
254 | 15.6k | } |
255 | | |
256 | | /* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */ |
257 | | /* Skip state->skip (> 0) uncompressed bytes of output. Return -1 on error, 0 |
258 | | on success. */ |
259 | 0 | static int gz_skip(gz_state *state) { |
260 | 0 | unsigned n; |
261 | | |
262 | | /* skip over state->skip bytes or reach end-of-file, whichever comes first */ |
263 | 0 | do { |
264 | | /* skip over whatever is in output buffer */ |
265 | 0 | if (state->x.have) { |
266 | 0 | n = GT_OFF(state->x.have) || |
267 | 0 | (z_off64_t)state->x.have > state->skip ? |
268 | 0 | (unsigned)state->skip : state->x.have; |
269 | 0 | state->x.have -= n; |
270 | 0 | state->x.next += n; |
271 | 0 | state->x.pos += n; |
272 | 0 | state->skip -= n; |
273 | 0 | } else if (state->eof && state->strm.avail_in == 0) { |
274 | | /* output buffer empty -- return if we're at the end of the input */ |
275 | 0 | break; |
276 | 0 | } else { |
277 | | /* need more data to skip -- load up output buffer */ |
278 | | /* get more output, looking for header if required */ |
279 | 0 | if (gz_fetch(state) == -1) |
280 | 0 | return -1; |
281 | 0 | } |
282 | 0 | } while (state->skip); |
283 | 0 | return 0; |
284 | 0 | } |
285 | | |
286 | | /* Read len bytes into buf from file, or less than len up to the end of the |
287 | | input. Return the number of bytes read. If zero is returned, either the |
288 | | end of file was reached, or there was an error. state->err must be |
289 | | consulted in that case to determine which. */ |
290 | 39.6k | static size_t gz_read(gz_state *state, void *buf, size_t len) { |
291 | 39.6k | size_t got; |
292 | 39.6k | unsigned n; |
293 | | |
294 | | /* if len is zero, avoid unnecessary operations */ |
295 | 39.6k | if (len == 0) |
296 | 0 | return 0; |
297 | | |
298 | | /* process a skip request */ |
299 | 39.6k | if (state->skip && gz_skip(state) == -1) |
300 | 0 | return 0; |
301 | | |
302 | | /* get len bytes to buf, or less than len if at the end */ |
303 | 39.6k | got = 0; |
304 | 70.9k | do { |
305 | | /* set n to the maximum amount of len that fits in an unsigned int */ |
306 | 70.9k | n = (unsigned)-1; |
307 | 70.9k | if (n > len) |
308 | 70.9k | n = (unsigned)len; |
309 | | |
310 | | /* first just try copying data from the output buffer */ |
311 | 70.9k | if (state->x.have) { |
312 | 26.1k | if (state->x.have < n) |
313 | 15.5k | n = state->x.have; |
314 | 26.1k | memcpy(buf, state->x.next, n); |
315 | 26.1k | state->x.next += n; |
316 | 26.1k | state->x.have -= n; |
317 | 26.1k | } |
318 | | |
319 | | /* output buffer empty -- return if we're at the end of the input */ |
320 | 44.7k | else if (state->eof && state->strm.avail_in == 0) { |
321 | 29.0k | state->past = 1; /* tried to read past end */ |
322 | 29.0k | break; |
323 | 29.0k | } |
324 | | |
325 | | /* need output data -- for small len or new stream load up our output |
326 | | buffer */ |
327 | 15.6k | else if (state->how == LOOK || n < (state->size << 1)) { |
328 | | /* get more output, looking for header if required */ |
329 | 15.6k | if (gz_fetch(state) == -1) |
330 | 0 | return 0; |
331 | 15.6k | continue; /* no progress yet -- go back to copy above */ |
332 | | /* the copy above assures that we will leave with space in the |
333 | | output buffer, allowing at least one gzungetc() to succeed */ |
334 | 15.6k | } |
335 | | |
336 | | /* large len -- read directly into user buffer */ |
337 | 0 | else if (state->how == COPY) { /* read directly */ |
338 | 0 | if (gz_load(state, (unsigned char *)buf, n, &n) == -1) |
339 | 0 | return 0; |
340 | 0 | } |
341 | | |
342 | | /* large len -- decompress directly into user buffer */ |
343 | 0 | else { /* state->how == GZIP */ |
344 | 0 | state->strm.avail_out = n; |
345 | 0 | state->strm.next_out = (unsigned char *)buf; |
346 | 0 | if (gz_decomp(state) == -1) |
347 | 0 | return 0; |
348 | 0 | n = state->x.have; |
349 | 0 | state->x.have = 0; |
350 | 0 | } |
351 | | |
352 | | /* update progress */ |
353 | 26.1k | len -= n; |
354 | 26.1k | buf = (char *)buf + n; |
355 | 26.1k | got += n; |
356 | 26.1k | state->x.pos += n; |
357 | 41.8k | } while (len); |
358 | | |
359 | | /* return number of bytes read into user buffer */ |
360 | 39.6k | return got; |
361 | 39.6k | } |
362 | | |
363 | | /* -- see zlib.h -- */ |
364 | 39.6k | z_int32_t Z_EXPORT PREFIX(gzread)(gzFile file, void *buf, z_uint32_t len) { |
365 | 39.6k | gz_state *state; |
366 | | |
367 | | /* get internal structure */ |
368 | 39.6k | if (file == NULL) |
369 | 0 | return -1; |
370 | | /* reject a NULL buffer with nonzero length before it is written to */ |
371 | 39.6k | if (buf == NULL && len != 0) |
372 | 0 | return -1; |
373 | 39.6k | state = (gz_state *)file; |
374 | | |
375 | | /* check that we're reading and that there's no (serious) error */ |
376 | 39.6k | if (state->mode != GZ_READ || |
377 | 39.6k | (state->err != Z_OK && state->err != Z_BUF_ERROR)) |
378 | 0 | return -1; |
379 | | |
380 | | /* since an int is returned, make sure len fits in one, otherwise return |
381 | | with an error (this avoids a flaw in the interface) */ |
382 | 39.6k | if ((z_int32_t)len < 0) { |
383 | 0 | PREFIX(gz_error)(state, Z_STREAM_ERROR, "request does not fit in an int"); |
384 | 0 | return -1; |
385 | 0 | } |
386 | | |
387 | | /* read len or fewer bytes to buf */ |
388 | 39.6k | len = (unsigned)gz_read(state, buf, len); |
389 | | |
390 | | /* check for an error */ |
391 | 39.6k | if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR) |
392 | 0 | return -1; |
393 | | |
394 | | /* return the number of bytes read (this is assured to fit in an int) */ |
395 | 39.6k | return (z_int32_t)len; |
396 | 39.6k | } |
397 | | |
398 | | /* -- see zlib.h -- */ |
399 | 0 | size_t Z_EXPORT PREFIX(gzfread)(void *buf, size_t size, size_t nitems, gzFile file) { |
400 | 0 | size_t len; |
401 | 0 | gz_state *state; |
402 | | |
403 | | /* Exit early if size is zero, also prevents potential division by zero */ |
404 | 0 | if (size == 0) |
405 | 0 | return 0; |
406 | | |
407 | | /* get internal structure */ |
408 | 0 | if (file == NULL) |
409 | 0 | return 0; |
410 | 0 | state = (gz_state *)file; |
411 | | |
412 | | /* check that we're reading and that there's no (serious) error */ |
413 | 0 | if (state->mode != GZ_READ || |
414 | 0 | (state->err != Z_OK && state->err != Z_BUF_ERROR)) |
415 | 0 | return 0; |
416 | | |
417 | | /* compute bytes to read -- error on overflow */ |
418 | 0 | if (size && SIZE_MAX / size < nitems) { |
419 | 0 | PREFIX(gz_error)(state, Z_STREAM_ERROR, "request does not fit in a size_t"); |
420 | 0 | return 0; |
421 | 0 | } |
422 | 0 | len = nitems * size; |
423 | | |
424 | | /* read len or fewer bytes to buf, return the number of full items read */ |
425 | 0 | return len ? gz_read(state, buf, len) / size : 0; |
426 | 0 | } |
427 | | |
428 | | /* -- see zlib.h -- */ |
429 | 0 | z_int32_t Z_EXPORT PREFIX(gzgetc)(gzFile file) { |
430 | 0 | unsigned char buf[1]; |
431 | 0 | gz_state *state; |
432 | | |
433 | | /* get internal structure */ |
434 | 0 | if (file == NULL) |
435 | 0 | return -1; |
436 | 0 | state = (gz_state *)file; |
437 | | |
438 | | /* check that we're reading and that there's no (serious) error */ |
439 | 0 | if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR)) |
440 | 0 | return -1; |
441 | | |
442 | | /* try output buffer (no need to check for skip request) */ |
443 | 0 | if (state->x.have) { |
444 | 0 | state->x.have--; |
445 | 0 | state->x.pos++; |
446 | 0 | return *(state->x.next)++; |
447 | 0 | } |
448 | | |
449 | | /* nothing there -- try gz_read() */ |
450 | 0 | return gz_read(state, buf, 1) < 1 ? -1 : buf[0]; |
451 | 0 | } |
452 | | |
453 | | #ifdef ZLIB_COMPAT |
454 | | int Z_EXPORT PREFIX(gzgetc_)(gzFile file) { |
455 | | return PREFIX(gzgetc)(file); |
456 | | } |
457 | | #endif |
458 | | |
459 | | /* -- see zlib.h -- */ |
460 | 0 | z_int32_t Z_EXPORT PREFIX(gzungetc)(z_int32_t c, gzFile file) { |
461 | 0 | gz_state *state; |
462 | | |
463 | | /* get internal structure */ |
464 | 0 | if (file == NULL) |
465 | 0 | return -1; |
466 | 0 | state = (gz_state *)file; |
467 | | |
468 | | /* in case this was just opened, set up the input buffer */ |
469 | 0 | if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) |
470 | 0 | (void)gz_look(state); |
471 | | |
472 | | /* check that we're reading and that there's no (serious) error */ |
473 | 0 | if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR)) |
474 | 0 | return -1; |
475 | | |
476 | | /* process a skip request */ |
477 | 0 | if (state->skip && gz_skip(state) == -1) |
478 | 0 | return -1; |
479 | | |
480 | | /* can't push EOF */ |
481 | 0 | if (c < 0) |
482 | 0 | return -1; |
483 | | |
484 | | /* if output buffer empty, put byte at end (allows more pushing) */ |
485 | 0 | if (state->x.have == 0) { |
486 | 0 | state->x.have = 1; |
487 | 0 | state->x.next = state->out + (state->size << 1) - 1; |
488 | 0 | state->x.next[0] = (unsigned char)c; |
489 | 0 | state->x.pos--; |
490 | 0 | state->past = 0; |
491 | 0 | return c; |
492 | 0 | } |
493 | | |
494 | | /* if no room, give up (must have already done a gzungetc()) */ |
495 | 0 | if (state->x.have == (state->size << 1)) { |
496 | 0 | PREFIX(gz_error)(state, Z_DATA_ERROR, "out of room to push characters"); |
497 | 0 | return -1; |
498 | 0 | } |
499 | | |
500 | | /* slide output data if needed and insert byte before existing data */ |
501 | 0 | if (state->x.next == state->out) { |
502 | 0 | unsigned char *src = state->out + state->x.have; |
503 | 0 | unsigned char *dest = state->out + (state->size << 1); |
504 | 0 | while (src > state->out) |
505 | 0 | *--dest = *--src; |
506 | 0 | state->x.next = dest; |
507 | 0 | } |
508 | 0 | state->x.have++; |
509 | 0 | state->x.next--; |
510 | 0 | state->x.next[0] = (unsigned char)c; |
511 | 0 | state->x.pos--; |
512 | 0 | state->past = 0; |
513 | 0 | return c; |
514 | 0 | } |
515 | | |
516 | | /* -- see zlib.h -- */ |
517 | 0 | char * Z_EXPORT PREFIX(gzgets)(gzFile file, char *buf, z_int32_t len) { |
518 | 0 | unsigned left, n; |
519 | 0 | char *str; |
520 | 0 | unsigned char *eol; |
521 | 0 | gz_state *state; |
522 | | |
523 | | /* check parameters and get internal structure */ |
524 | 0 | if (file == NULL || buf == NULL || len < 1) |
525 | 0 | return NULL; |
526 | 0 | state = (gz_state *)file; |
527 | | |
528 | | /* check that we're reading and that there's no (serious) error */ |
529 | 0 | if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR)) |
530 | 0 | return NULL; |
531 | | |
532 | | /* process a skip request */ |
533 | 0 | if (state->skip && gz_skip(state) == -1) |
534 | 0 | return NULL; |
535 | | |
536 | | /* copy output bytes up to new line or len - 1, whichever comes first -- |
537 | | append a terminating zero to the string (we don't check for a zero in |
538 | | the contents, let the user worry about that) */ |
539 | 0 | str = buf; |
540 | 0 | left = (unsigned)len - 1; |
541 | 0 | if (left) { |
542 | 0 | do { |
543 | | /* assure that something is in the output buffer */ |
544 | 0 | if (state->x.have == 0 && gz_fetch(state) == -1) |
545 | 0 | return NULL; /* error */ |
546 | 0 | if (state->x.have == 0) { /* end of file */ |
547 | 0 | state->past = 1; /* read past end */ |
548 | 0 | break; /* return what we have */ |
549 | 0 | } |
550 | | |
551 | | /* look for end-of-line in current output buffer */ |
552 | 0 | n = MIN(state->x.have, left); |
553 | 0 | eol = (unsigned char *)memchr(state->x.next, '\n', n); |
554 | 0 | if (eol != NULL) |
555 | 0 | n = (unsigned)(eol - state->x.next) + 1; |
556 | | |
557 | | /* copy through end-of-line, or remainder if not found */ |
558 | 0 | memcpy(buf, state->x.next, n); |
559 | 0 | state->x.have -= n; |
560 | 0 | state->x.next += n; |
561 | 0 | state->x.pos += n; |
562 | 0 | left -= n; |
563 | 0 | buf += n; |
564 | 0 | } while (left && eol == NULL); |
565 | 0 | } |
566 | | |
567 | | /* return terminated string, or if nothing, end of file */ |
568 | 0 | if (buf == str) |
569 | 0 | return NULL; |
570 | 0 | buf[0] = 0; |
571 | 0 | return str; |
572 | 0 | } |
573 | | |
574 | | /* -- see zlib.h -- */ |
575 | 0 | z_int32_t Z_EXPORT PREFIX(gzdirect)(gzFile file) { |
576 | 0 | gz_state *state; |
577 | | |
578 | | /* get internal structure */ |
579 | 0 | if (file == NULL) |
580 | 0 | return 0; |
581 | | |
582 | 0 | state = (gz_state *)file; |
583 | | |
584 | | /* if the state is not known, but we can find out, then do so (this is |
585 | | mainly for right after a gzopen() or gzdopen()) */ |
586 | 0 | if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) |
587 | 0 | (void)gz_look(state); |
588 | | |
589 | | /* return 1 if transparent, 0 if processing a gzip stream */ |
590 | 0 | return state->direct == 1; |
591 | 0 | } |
592 | | |
593 | | /* -- see zlib.h -- */ |
594 | 14.5k | z_int32_t Z_EXPORT PREFIX(gzclose_r)(gzFile file) { |
595 | 14.5k | int ret, err; |
596 | 14.5k | gz_state *state; |
597 | | |
598 | | /* get internal structure */ |
599 | 14.5k | if (file == NULL) |
600 | 0 | return Z_STREAM_ERROR; |
601 | | |
602 | 14.5k | state = (gz_state *)file; |
603 | | |
604 | | /* check that we're reading */ |
605 | 14.5k | if (state->mode != GZ_READ) |
606 | 0 | return Z_STREAM_ERROR; |
607 | | |
608 | | /* free memory and close file */ |
609 | 14.5k | if (state->size) { |
610 | 14.5k | PREFIX(inflateEnd)(&(state->strm)); |
611 | 14.5k | gz_buffer_free(state); |
612 | 14.5k | } |
613 | 14.5k | err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK; |
614 | 14.5k | PREFIX(gz_error)(state, Z_OK, NULL); |
615 | 14.5k | free(state->path); |
616 | 14.5k | ret = close(state->fd); |
617 | 14.5k | zng_free(state); |
618 | 14.5k | return ret ? Z_ERRNO : err; |
619 | 14.5k | } |