ยปCore Development>Code coverage>Modules/zlib/gzread.c

Python code coverage for Modules/zlib/gzread.c

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