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