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