MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/SPlisHSPlasH / gz_look

Function gz_look

extern/zlib/src/gzread.c:76–149  ·  view source on GitHub ↗

Look for gzip header, set up for inflate or copy. state->x.have must be 0. If this is the first time in, allocate required memory. state->how will be left unchanged if there is no more input data available, will be set to COPY if there is no gzip header and direct copying will be performed, or it will be set to GZIP for decompression. If direct copying, then leftover input data f

Source from the content-addressed store, hash-verified

74 a user buffer. If decompressing, the inflate state will be initialized.
75 gz_look() will return 0 on success or -1 on failure. */
76local int gz_look(gz_statep state) {
77 z_streamp strm = &(state->strm);
78
79 /* allocate read buffers and inflate memory */
80 if (state->size == 0) {
81 /* allocate buffers */
82 state->in = (unsigned char *)malloc(state->want);
83 state->out = (unsigned char *)malloc(state->want << 1);
84 if (state->in == NULL || state->out == NULL) {
85 free(state->out);
86 free(state->in);
87 gz_error(state, Z_MEM_ERROR, "out of memory");
88 return -1;
89 }
90 state->size = state->want;
91
92 /* allocate inflate memory */
93 state->strm.zalloc = Z_NULL;
94 state->strm.zfree = Z_NULL;
95 state->strm.opaque = Z_NULL;
96 state->strm.avail_in = 0;
97 state->strm.next_in = Z_NULL;
98 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
99 free(state->out);
100 free(state->in);
101 state->size = 0;
102 gz_error(state, Z_MEM_ERROR, "out of memory");
103 return -1;
104 }
105 }
106
107 /* get at least the magic bytes in the input buffer */
108 if (strm->avail_in < 2) {
109 if (gz_avail(state) == -1)
110 return -1;
111 if (strm->avail_in == 0)
112 return 0;
113 }
114
115 /* look for gzip magic bytes -- if there, do gzip decoding (note: there is
116 a logical dilemma here when considering the case of a partially written
117 gzip file, to wit, if a single 31 byte is written, then we cannot tell
118 whether this is a single-byte file, or just a partially written gzip
119 file -- for here we assume that if a gzip file is being written, then
120 the header will be written in a single operation, so that reading a
121 single byte is sufficient indication that it is not a gzip file) */
122 if (strm->avail_in > 1 &&
123 strm->next_in[0] == 31 && strm->next_in[1] == 139) {
124 inflateReset(strm);
125 state->how = GZIP;
126 state->direct = 0;
127 return 0;
128 }
129
130 /* no gzip header -- if we were decoding gzip before, then this is trailing
131 garbage. Ignore the trailing garbage and finish. */
132 if (state->direct == 0) {
133 strm->avail_in = 0;

Callers 3

gz_fetchFunction · 0.85
gzungetcFunction · 0.85
gzdirectFunction · 0.85

Calls 3

gz_errorFunction · 0.85
gz_availFunction · 0.85
inflateResetFunction · 0.85

Tested by

no test coverage detected