annotate src/zlib-1.2.7/gzread.c @ 4:e13257ea84a4

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