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