Look for gzip header, set up for inflate or copy. state.state->x.have must be 0. If this is the first time in, allocate required memory. state.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 inp
(state)
| 100 | a user buffer. If decompressing, the inflate state will be initialized. |
| 101 | gz_look() will return 0 on success or -1 on failure. */ |
| 102 | local int gz_look(state) |
| 103 | gz_statep state; |
| 104 | { |
| 105 | z_streamp strm = &(state.state->strm); |
| 106 | |
| 107 | /* allocate read buffers and inflate memory */ |
| 108 | if (state.state->size == 0) { |
| 109 | /* allocate buffers */ |
| 110 | state.state->in = (unsigned char *)malloc(state.state->want); |
| 111 | state.state->out = (unsigned char *)malloc(state.state->want << 1); |
| 112 | if (state.state->in == NULL || state.state->out == NULL) { |
| 113 | free(state.state->out); |
| 114 | free(state.state->in); |
| 115 | gz_error(state, Z_MEM_ERROR, "out of memory"); |
| 116 | return -1; |
| 117 | } |
| 118 | state.state->size = state.state->want; |
| 119 | |
| 120 | /* allocate inflate memory */ |
| 121 | state.state->strm.zalloc = Z_NULL; |
| 122 | state.state->strm.zfree = Z_NULL; |
| 123 | state.state->strm.opaque = Z_NULL; |
| 124 | state.state->strm.avail_in = 0; |
| 125 | state.state->strm.next_in = Z_NULL; |
| 126 | if (inflateInit2(&(state.state->strm), 15 + 16) != Z_OK) { /* gunzip */ |
| 127 | free(state.state->out); |
| 128 | free(state.state->in); |
| 129 | state.state->size = 0; |
| 130 | gz_error(state, Z_MEM_ERROR, "out of memory"); |
| 131 | return -1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* get at least the magic bytes in the input buffer */ |
| 136 | if (strm->avail_in < 2) { |
| 137 | if (gz_avail(state) == -1) |
| 138 | return -1; |
| 139 | if (strm->avail_in == 0) |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* look for gzip magic bytes -- if there, do gzip decoding (note: there is |
| 144 | a logical dilemma here when considering the case of a partially written |
| 145 | gzip file, to wit, if a single 31 byte is written, then we cannot tell |
| 146 | whether this is a single-byte file, or just a partially written gzip |
| 147 | file -- for here we assume that if a gzip file is being written, then |
| 148 | the header will be written in a single operation, so that reading a |
| 149 | single byte is sufficient indication that it is not a gzip file) */ |
| 150 | if (strm->avail_in > 1 && |
| 151 | ((strm->next_in[0] == 31 && strm->next_in[1] == 139) /* gz header */ |
| 152 | || (strm->next_in[0] == 40 && strm->next_in[1] == 181))) { /* zstd header */ |
| 153 | inflateReset(strm); |
| 154 | state.state->how = GZIP; |
| 155 | state.state->direct = 0; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* no gzip header -- if we were decoding gzip before, then this is trailing |