annotate src/zlib-1.2.8/gzread.c @ 56:af97cad61ff0

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