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

Python code coverage for Modules/zlib/gzlib.c

#countcontent
1n/a/* gzlib.c -- zlib functions common to reading and writing gzip files
2n/a * Copyright (C) 2004-2017 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#if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__)
9n/a# define LSEEK _lseeki64
10n/a#else
11n/a#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
12n/a# define LSEEK lseek64
13n/a#else
14n/a# define LSEEK lseek
15n/a#endif
16n/a#endif
17n/a
18n/a/* Local functions */
19n/alocal void gz_reset OF((gz_statep));
20n/alocal gzFile gz_open OF((const void *, int, const char *));
21n/a
22n/a#if defined UNDER_CE
23n/a
24n/a/* Map the Windows error number in ERROR to a locale-dependent error message
25n/a string and return a pointer to it. Typically, the values for ERROR come
26n/a from GetLastError.
27n/a
28n/a The string pointed to shall not be modified by the application, but may be
29n/a overwritten by a subsequent call to gz_strwinerror
30n/a
31n/a The gz_strwinerror function does not change the current setting of
32n/a GetLastError. */
33n/achar ZLIB_INTERNAL *gz_strwinerror (error)
34n/a DWORD error;
35n/a{
36n/a static char buf[1024];
37n/a
38n/a wchar_t *msgbuf;
39n/a DWORD lasterr = GetLastError();
40n/a DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
41n/a | FORMAT_MESSAGE_ALLOCATE_BUFFER,
42n/a NULL,
43n/a error,
44n/a 0, /* Default language */
45n/a (LPVOID)&msgbuf,
46n/a 0,
47n/a NULL);
48n/a if (chars != 0) {
49n/a /* If there is an \r\n appended, zap it. */
50n/a if (chars >= 2
51n/a && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
52n/a chars -= 2;
53n/a msgbuf[chars] = 0;
54n/a }
55n/a
56n/a if (chars > sizeof (buf) - 1) {
57n/a chars = sizeof (buf) - 1;
58n/a msgbuf[chars] = 0;
59n/a }
60n/a
61n/a wcstombs(buf, msgbuf, chars + 1);
62n/a LocalFree(msgbuf);
63n/a }
64n/a else {
65n/a sprintf(buf, "unknown win32 error (%ld)", error);
66n/a }
67n/a
68n/a SetLastError(lasterr);
69n/a return buf;
70n/a}
71n/a
72n/a#endif /* UNDER_CE */
73n/a
74n/a/* Reset gzip file state */
75n/alocal void gz_reset(state)
76n/a gz_statep state;
77n/a{
78n/a state->x.have = 0; /* no output data available */
79n/a if (state->mode == GZ_READ) { /* for reading ... */
80n/a state->eof = 0; /* not at end of file */
81n/a state->past = 0; /* have not read past end yet */
82n/a state->how = LOOK; /* look for gzip header */
83n/a }
84n/a state->seek = 0; /* no seek request pending */
85n/a gz_error(state, Z_OK, NULL); /* clear error */
86n/a state->x.pos = 0; /* no uncompressed data yet */
87n/a state->strm.avail_in = 0; /* no input data yet */
88n/a}
89n/a
90n/a/* Open a gzip file either by name or file descriptor. */
91n/alocal gzFile gz_open(path, fd, mode)
92n/a const void *path;
93n/a int fd;
94n/a const char *mode;
95n/a{
96n/a gz_statep state;
97n/a z_size_t len;
98n/a int oflag;
99n/a#ifdef O_CLOEXEC
100n/a int cloexec = 0;
101n/a#endif
102n/a#ifdef O_EXCL
103n/a int exclusive = 0;
104n/a#endif
105n/a
106n/a /* check input */
107n/a if (path == NULL)
108n/a return NULL;
109n/a
110n/a /* allocate gzFile structure to return */
111n/a state = (gz_statep)malloc(sizeof(gz_state));
112n/a if (state == NULL)
113n/a return NULL;
114n/a state->size = 0; /* no buffers allocated yet */
115n/a state->want = GZBUFSIZE; /* requested buffer size */
116n/a state->msg = NULL; /* no error message yet */
117n/a
118n/a /* interpret mode */
119n/a state->mode = GZ_NONE;
120n/a state->level = Z_DEFAULT_COMPRESSION;
121n/a state->strategy = Z_DEFAULT_STRATEGY;
122n/a state->direct = 0;
123n/a while (*mode) {
124n/a if (*mode >= '0' && *mode <= '9')
125n/a state->level = *mode - '0';
126n/a else
127n/a switch (*mode) {
128n/a case 'r':
129n/a state->mode = GZ_READ;
130n/a break;
131n/a#ifndef NO_GZCOMPRESS
132n/a case 'w':
133n/a state->mode = GZ_WRITE;
134n/a break;
135n/a case 'a':
136n/a state->mode = GZ_APPEND;
137n/a break;
138n/a#endif
139n/a case '+': /* can't read and write at the same time */
140n/a free(state);
141n/a return NULL;
142n/a case 'b': /* ignore -- will request binary anyway */
143n/a break;
144n/a#ifdef O_CLOEXEC
145n/a case 'e':
146n/a cloexec = 1;
147n/a break;
148n/a#endif
149n/a#ifdef O_EXCL
150n/a case 'x':
151n/a exclusive = 1;
152n/a break;
153n/a#endif
154n/a case 'f':
155n/a state->strategy = Z_FILTERED;
156n/a break;
157n/a case 'h':
158n/a state->strategy = Z_HUFFMAN_ONLY;
159n/a break;
160n/a case 'R':
161n/a state->strategy = Z_RLE;
162n/a break;
163n/a case 'F':
164n/a state->strategy = Z_FIXED;
165n/a break;
166n/a case 'T':
167n/a state->direct = 1;
168n/a break;
169n/a default: /* could consider as an error, but just ignore */
170n/a ;
171n/a }
172n/a mode++;
173n/a }
174n/a
175n/a /* must provide an "r", "w", or "a" */
176n/a if (state->mode == GZ_NONE) {
177n/a free(state);
178n/a return NULL;
179n/a }
180n/a
181n/a /* can't force transparent read */
182n/a if (state->mode == GZ_READ) {
183n/a if (state->direct) {
184n/a free(state);
185n/a return NULL;
186n/a }
187n/a state->direct = 1; /* for empty file */
188n/a }
189n/a
190n/a /* save the path name for error messages */
191n/a#ifdef WIDECHAR
192n/a if (fd == -2) {
193n/a len = wcstombs(NULL, path, 0);
194n/a if (len == (z_size_t)-1)
195n/a len = 0;
196n/a }
197n/a else
198n/a#endif
199n/a len = strlen((const char *)path);
200n/a state->path = (char *)malloc(len + 1);
201n/a if (state->path == NULL) {
202n/a free(state);
203n/a return NULL;
204n/a }
205n/a#ifdef WIDECHAR
206n/a if (fd == -2)
207n/a if (len)
208n/a wcstombs(state->path, path, len + 1);
209n/a else
210n/a *(state->path) = 0;
211n/a else
212n/a#endif
213n/a#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
214n/a (void)snprintf(state->path, len + 1, "%s", (const char *)path);
215n/a#else
216n/a strcpy(state->path, path);
217n/a#endif
218n/a
219n/a /* compute the flags for open() */
220n/a oflag =
221n/a#ifdef O_LARGEFILE
222n/a O_LARGEFILE |
223n/a#endif
224n/a#ifdef O_BINARY
225n/a O_BINARY |
226n/a#endif
227n/a#ifdef O_CLOEXEC
228n/a (cloexec ? O_CLOEXEC : 0) |
229n/a#endif
230n/a (state->mode == GZ_READ ?
231n/a O_RDONLY :
232n/a (O_WRONLY | O_CREAT |
233n/a#ifdef O_EXCL
234n/a (exclusive ? O_EXCL : 0) |
235n/a#endif
236n/a (state->mode == GZ_WRITE ?
237n/a O_TRUNC :
238n/a O_APPEND)));
239n/a
240n/a /* open the file with the appropriate flags (or just use fd) */
241n/a state->fd = fd > -1 ? fd : (
242n/a#ifdef WIDECHAR
243n/a fd == -2 ? _wopen(path, oflag, 0666) :
244n/a#endif
245n/a open((const char *)path, oflag, 0666));
246n/a if (state->fd == -1) {
247n/a free(state->path);
248n/a free(state);
249n/a return NULL;
250n/a }
251n/a if (state->mode == GZ_APPEND) {
252n/a LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
253n/a state->mode = GZ_WRITE; /* simplify later checks */
254n/a }
255n/a
256n/a /* save the current position for rewinding (only if reading) */
257n/a if (state->mode == GZ_READ) {
258n/a state->start = LSEEK(state->fd, 0, SEEK_CUR);
259n/a if (state->start == -1) state->start = 0;
260n/a }
261n/a
262n/a /* initialize stream */
263n/a gz_reset(state);
264n/a
265n/a /* return stream */
266n/a return (gzFile)state;
267n/a}
268n/a
269n/a/* -- see zlib.h -- */
270n/agzFile ZEXPORT gzopen(path, mode)
271n/a const char *path;
272n/a const char *mode;
273n/a{
274n/a return gz_open(path, -1, mode);
275n/a}
276n/a
277n/a/* -- see zlib.h -- */
278n/agzFile ZEXPORT gzopen64(path, mode)
279n/a const char *path;
280n/a const char *mode;
281n/a{
282n/a return gz_open(path, -1, mode);
283n/a}
284n/a
285n/a/* -- see zlib.h -- */
286n/agzFile ZEXPORT gzdopen(fd, mode)
287n/a int fd;
288n/a const char *mode;
289n/a{
290n/a char *path; /* identifier for error messages */
291n/a gzFile gz;
292n/a
293n/a if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
294n/a return NULL;
295n/a#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
296n/a (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
297n/a#else
298n/a sprintf(path, "<fd:%d>", fd); /* for debugging */
299n/a#endif
300n/a gz = gz_open(path, fd, mode);
301n/a free(path);
302n/a return gz;
303n/a}
304n/a
305n/a/* -- see zlib.h -- */
306n/a#ifdef WIDECHAR
307n/agzFile ZEXPORT gzopen_w(path, mode)
308n/a const wchar_t *path;
309n/a const char *mode;
310n/a{
311n/a return gz_open(path, -2, mode);
312n/a}
313n/a#endif
314n/a
315n/a/* -- see zlib.h -- */
316n/aint ZEXPORT gzbuffer(file, size)
317n/a gzFile file;
318n/a unsigned size;
319n/a{
320n/a gz_statep state;
321n/a
322n/a /* get internal structure and check integrity */
323n/a if (file == NULL)
324n/a return -1;
325n/a state = (gz_statep)file;
326n/a if (state->mode != GZ_READ && state->mode != GZ_WRITE)
327n/a return -1;
328n/a
329n/a /* make sure we haven't already allocated memory */
330n/a if (state->size != 0)
331n/a return -1;
332n/a
333n/a /* check and set requested size */
334n/a if ((size << 1) < size)
335n/a return -1; /* need to be able to double it */
336n/a if (size < 2)
337n/a size = 2; /* need two bytes to check magic header */
338n/a state->want = size;
339n/a return 0;
340n/a}
341n/a
342n/a/* -- see zlib.h -- */
343n/aint ZEXPORT gzrewind(file)
344n/a gzFile file;
345n/a{
346n/a gz_statep state;
347n/a
348n/a /* get internal structure */
349n/a if (file == NULL)
350n/a return -1;
351n/a state = (gz_statep)file;
352n/a
353n/a /* check that we're reading and that there's no error */
354n/a if (state->mode != GZ_READ ||
355n/a (state->err != Z_OK && state->err != Z_BUF_ERROR))
356n/a return -1;
357n/a
358n/a /* back up and start over */
359n/a if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
360n/a return -1;
361n/a gz_reset(state);
362n/a return 0;
363n/a}
364n/a
365n/a/* -- see zlib.h -- */
366n/az_off64_t ZEXPORT gzseek64(file, offset, whence)
367n/a gzFile file;
368n/a z_off64_t offset;
369n/a int whence;
370n/a{
371n/a unsigned n;
372n/a z_off64_t ret;
373n/a gz_statep state;
374n/a
375n/a /* get internal structure and check integrity */
376n/a if (file == NULL)
377n/a return -1;
378n/a state = (gz_statep)file;
379n/a if (state->mode != GZ_READ && state->mode != GZ_WRITE)
380n/a return -1;
381n/a
382n/a /* check that there's no error */
383n/a if (state->err != Z_OK && state->err != Z_BUF_ERROR)
384n/a return -1;
385n/a
386n/a /* can only seek from start or relative to current position */
387n/a if (whence != SEEK_SET && whence != SEEK_CUR)
388n/a return -1;
389n/a
390n/a /* normalize offset to a SEEK_CUR specification */
391n/a if (whence == SEEK_SET)
392n/a offset -= state->x.pos;
393n/a else if (state->seek)
394n/a offset += state->skip;
395n/a state->seek = 0;
396n/a
397n/a /* if within raw area while reading, just go there */
398n/a if (state->mode == GZ_READ && state->how == COPY &&
399n/a state->x.pos + offset >= 0) {
400n/a ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
401n/a if (ret == -1)
402n/a return -1;
403n/a state->x.have = 0;
404n/a state->eof = 0;
405n/a state->past = 0;
406n/a state->seek = 0;
407n/a gz_error(state, Z_OK, NULL);
408n/a state->strm.avail_in = 0;
409n/a state->x.pos += offset;
410n/a return state->x.pos;
411n/a }
412n/a
413n/a /* calculate skip amount, rewinding if needed for back seek when reading */
414n/a if (offset < 0) {
415n/a if (state->mode != GZ_READ) /* writing -- can't go backwards */
416n/a return -1;
417n/a offset += state->x.pos;
418n/a if (offset < 0) /* before start of file! */
419n/a return -1;
420n/a if (gzrewind(file) == -1) /* rewind, then skip to offset */
421n/a return -1;
422n/a }
423n/a
424n/a /* if reading, skip what's in output buffer (one less gzgetc() check) */
425n/a if (state->mode == GZ_READ) {
426n/a n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
427n/a (unsigned)offset : state->x.have;
428n/a state->x.have -= n;
429n/a state->x.next += n;
430n/a state->x.pos += n;
431n/a offset -= n;
432n/a }
433n/a
434n/a /* request skip (if not zero) */
435n/a if (offset) {
436n/a state->seek = 1;
437n/a state->skip = offset;
438n/a }
439n/a return state->x.pos + offset;
440n/a}
441n/a
442n/a/* -- see zlib.h -- */
443n/az_off_t ZEXPORT gzseek(file, offset, whence)
444n/a gzFile file;
445n/a z_off_t offset;
446n/a int whence;
447n/a{
448n/a z_off64_t ret;
449n/a
450n/a ret = gzseek64(file, (z_off64_t)offset, whence);
451n/a return ret == (z_off_t)ret ? (z_off_t)ret : -1;
452n/a}
453n/a
454n/a/* -- see zlib.h -- */
455n/az_off64_t ZEXPORT gztell64(file)
456n/a gzFile file;
457n/a{
458n/a gz_statep state;
459n/a
460n/a /* get internal structure and check integrity */
461n/a if (file == NULL)
462n/a return -1;
463n/a state = (gz_statep)file;
464n/a if (state->mode != GZ_READ && state->mode != GZ_WRITE)
465n/a return -1;
466n/a
467n/a /* return position */
468n/a return state->x.pos + (state->seek ? state->skip : 0);
469n/a}
470n/a
471n/a/* -- see zlib.h -- */
472n/az_off_t ZEXPORT gztell(file)
473n/a gzFile file;
474n/a{
475n/a z_off64_t ret;
476n/a
477n/a ret = gztell64(file);
478n/a return ret == (z_off_t)ret ? (z_off_t)ret : -1;
479n/a}
480n/a
481n/a/* -- see zlib.h -- */
482n/az_off64_t ZEXPORT gzoffset64(file)
483n/a gzFile file;
484n/a{
485n/a z_off64_t offset;
486n/a gz_statep state;
487n/a
488n/a /* get internal structure and check integrity */
489n/a if (file == NULL)
490n/a return -1;
491n/a state = (gz_statep)file;
492n/a if (state->mode != GZ_READ && state->mode != GZ_WRITE)
493n/a return -1;
494n/a
495n/a /* compute and return effective offset in file */
496n/a offset = LSEEK(state->fd, 0, SEEK_CUR);
497n/a if (offset == -1)
498n/a return -1;
499n/a if (state->mode == GZ_READ) /* reading */
500n/a offset -= state->strm.avail_in; /* don't count buffered input */
501n/a return offset;
502n/a}
503n/a
504n/a/* -- see zlib.h -- */
505n/az_off_t ZEXPORT gzoffset(file)
506n/a gzFile file;
507n/a{
508n/a z_off64_t ret;
509n/a
510n/a ret = gzoffset64(file);
511n/a return ret == (z_off_t)ret ? (z_off_t)ret : -1;
512n/a}
513n/a
514n/a/* -- see zlib.h -- */
515n/aint ZEXPORT gzeof(file)
516n/a gzFile file;
517n/a{
518n/a gz_statep state;
519n/a
520n/a /* get internal structure and check integrity */
521n/a if (file == NULL)
522n/a return 0;
523n/a state = (gz_statep)file;
524n/a if (state->mode != GZ_READ && state->mode != GZ_WRITE)
525n/a return 0;
526n/a
527n/a /* return end-of-file state */
528n/a return state->mode == GZ_READ ? state->past : 0;
529n/a}
530n/a
531n/a/* -- see zlib.h -- */
532n/aconst char * ZEXPORT gzerror(file, errnum)
533n/a gzFile file;
534n/a int *errnum;
535n/a{
536n/a gz_statep state;
537n/a
538n/a /* get internal structure and check integrity */
539n/a if (file == NULL)
540n/a return NULL;
541n/a state = (gz_statep)file;
542n/a if (state->mode != GZ_READ && state->mode != GZ_WRITE)
543n/a return NULL;
544n/a
545n/a /* return error information */
546n/a if (errnum != NULL)
547n/a *errnum = state->err;
548n/a return state->err == Z_MEM_ERROR ? "out of memory" :
549n/a (state->msg == NULL ? "" : state->msg);
550n/a}
551n/a
552n/a/* -- see zlib.h -- */
553n/avoid ZEXPORT gzclearerr(file)
554n/a gzFile file;
555n/a{
556n/a gz_statep state;
557n/a
558n/a /* get internal structure and check integrity */
559n/a if (file == NULL)
560n/a return;
561n/a state = (gz_statep)file;
562n/a if (state->mode != GZ_READ && state->mode != GZ_WRITE)
563n/a return;
564n/a
565n/a /* clear error and end-of-file */
566n/a if (state->mode == GZ_READ) {
567n/a state->eof = 0;
568n/a state->past = 0;
569n/a }
570n/a gz_error(state, Z_OK, NULL);
571n/a}
572n/a
573n/a/* Create an error message in allocated memory and set state->err and
574n/a state->msg accordingly. Free any previous error message already there. Do
575n/a not try to free or allocate space if the error is Z_MEM_ERROR (out of
576n/a memory). Simply save the error message as a static string. If there is an
577n/a allocation failure constructing the error message, then convert the error to
578n/a out of memory. */
579n/avoid ZLIB_INTERNAL gz_error(state, err, msg)
580n/a gz_statep state;
581n/a int err;
582n/a const char *msg;
583n/a{
584n/a /* free previously allocated message and clear */
585n/a if (state->msg != NULL) {
586n/a if (state->err != Z_MEM_ERROR)
587n/a free(state->msg);
588n/a state->msg = NULL;
589n/a }
590n/a
591n/a /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
592n/a if (err != Z_OK && err != Z_BUF_ERROR)
593n/a state->x.have = 0;
594n/a
595n/a /* set error code, and if no message, then done */
596n/a state->err = err;
597n/a if (msg == NULL)
598n/a return;
599n/a
600n/a /* for an out of memory error, return literal string when requested */
601n/a if (err == Z_MEM_ERROR)
602n/a return;
603n/a
604n/a /* construct error message with path */
605n/a if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
606n/a NULL) {
607n/a state->err = Z_MEM_ERROR;
608n/a return;
609n/a }
610n/a#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
611n/a (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
612n/a "%s%s%s", state->path, ": ", msg);
613n/a#else
614n/a strcpy(state->msg, state->path);
615n/a strcat(state->msg, ": ");
616n/a strcat(state->msg, msg);
617n/a#endif
618n/a}
619n/a
620n/a#ifndef INT_MAX
621n/a/* portably return maximum value for an int (when limits.h presumed not
622n/a available) -- we need to do this to cover cases where 2's complement not
623n/a used, since C standard permits 1's complement and sign-bit representations,
624n/a otherwise we could just use ((unsigned)-1) >> 1 */
625n/aunsigned ZLIB_INTERNAL gz_intmax()
626n/a{
627n/a unsigned p, q;
628n/a
629n/a p = 1;
630n/a do {
631n/a q = p;
632n/a p <<= 1;
633n/a p++;
634n/a } while (p > q);
635n/a return q >> 1;
636n/a}
637n/a#endif