gzread.c (237691) | gzread.c (254069) |
---|---|
1/* gzread.c -- zlib functions for reading gzip files | 1/* gzread.c -- zlib functions for reading gzip files |
2 * Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler | 2 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler |
3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 | 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 |
6/* $FreeBSD: stable/9/lib/libz/gzread.c 237691 2012-06-28 07:08:48Z delphij $ */ | 6/* $FreeBSD: stable/9/lib/libz/gzread.c 254069 2013-08-07 19:42:17Z delphij $ */ |
7 8#include "gzguts.h" 9#include <unistd.h> 10 11/* Local functions */ 12local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *)); 13local int gz_avail OF((gz_statep)); 14local int gz_look OF((gz_statep)); --- 41 unchanged lines hidden (view full) --- 56{ 57 unsigned got; 58 z_streamp strm = &(state->strm); 59 60 if (state->err != Z_OK && state->err != Z_BUF_ERROR) 61 return -1; 62 if (state->eof == 0) { 63 if (strm->avail_in) { /* copy what's there to the start */ | 7 8#include "gzguts.h" 9#include <unistd.h> 10 11/* Local functions */ 12local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *)); 13local int gz_avail OF((gz_statep)); 14local int gz_look OF((gz_statep)); --- 41 unchanged lines hidden (view full) --- 56{ 57 unsigned got; 58 z_streamp strm = &(state->strm); 59 60 if (state->err != Z_OK && state->err != Z_BUF_ERROR) 61 return -1; 62 if (state->eof == 0) { 63 if (strm->avail_in) { /* copy what's there to the start */ |
64 unsigned char *p = state->in, *q = strm->next_in; | 64 unsigned char *p = state->in; 65 unsigned const char *q = strm->next_in; |
65 unsigned n = strm->avail_in; 66 do { 67 *p++ = *q++; 68 } while (--n); 69 } 70 if (gz_load(state, state->in + strm->avail_in, 71 state->size - strm->avail_in, &got) == -1) 72 return -1; --- 15 unchanged lines hidden (view full) --- 88local int gz_look(state) 89 gz_statep state; 90{ 91 z_streamp strm = &(state->strm); 92 93 /* allocate read buffers and inflate memory */ 94 if (state->size == 0) { 95 /* allocate buffers */ | 66 unsigned n = strm->avail_in; 67 do { 68 *p++ = *q++; 69 } while (--n); 70 } 71 if (gz_load(state, state->in + strm->avail_in, 72 state->size - strm->avail_in, &got) == -1) 73 return -1; --- 15 unchanged lines hidden (view full) --- 89local int gz_look(state) 90 gz_statep state; 91{ 92 z_streamp strm = &(state->strm); 93 94 /* allocate read buffers and inflate memory */ 95 if (state->size == 0) { 96 /* allocate buffers */ |
96 state->in = malloc(state->want); 97 state->out = malloc(state->want << 1); | 97 state->in = (unsigned char *)malloc(state->want); 98 state->out = (unsigned char *)malloc(state->want << 1); |
98 if (state->in == NULL || state->out == NULL) { 99 if (state->out != NULL) 100 free(state->out); 101 if (state->in != NULL) 102 free(state->in); 103 gz_error(state, Z_MEM_ERROR, "out of memory"); 104 return -1; 105 } --- 244 unchanged lines hidden (view full) --- 350 return -1; 351 continue; /* no progress yet -- go back to copy above */ 352 /* the copy above assures that we will leave with space in the 353 output buffer, allowing at least one gzungetc() to succeed */ 354 } 355 356 /* large len -- read directly into user buffer */ 357 else if (state->how == COPY) { /* read directly */ | 99 if (state->in == NULL || state->out == NULL) { 100 if (state->out != NULL) 101 free(state->out); 102 if (state->in != NULL) 103 free(state->in); 104 gz_error(state, Z_MEM_ERROR, "out of memory"); 105 return -1; 106 } --- 244 unchanged lines hidden (view full) --- 351 return -1; 352 continue; /* no progress yet -- go back to copy above */ 353 /* the copy above assures that we will leave with space in the 354 output buffer, allowing at least one gzungetc() to succeed */ 355 } 356 357 /* large len -- read directly into user buffer */ 358 else if (state->how == COPY) { /* read directly */ |
358 if (gz_load(state, buf, len, &n) == -1) | 359 if (gz_load(state, (unsigned char *)buf, len, &n) == -1) |
359 return -1; 360 } 361 362 /* large len -- decompress directly into user buffer */ 363 else { /* state->how == GZIP */ 364 strm->avail_out = len; | 360 return -1; 361 } 362 363 /* large len -- decompress directly into user buffer */ 364 else { /* state->how == GZIP */ 365 strm->avail_out = len; |
365 strm->next_out = buf; | 366 strm->next_out = (unsigned char *)buf; |
366 if (gz_decomp(state) == -1) 367 return -1; 368 n = state->x.have; 369 state->x.have = 0; 370 } 371 372 /* update progress */ 373 len -= n; 374 buf = (char *)buf + n; 375 got += n; 376 state->x.pos += n; 377 } while (len); 378 379 /* return number of bytes read into user buffer (will fit in int) */ 380 return (int)got; 381} 382 383/* -- see zlib.h -- */ | 367 if (gz_decomp(state) == -1) 368 return -1; 369 n = state->x.have; 370 state->x.have = 0; 371 } 372 373 /* update progress */ 374 len -= n; 375 buf = (char *)buf + n; 376 got += n; 377 state->x.pos += n; 378 } while (len); 379 380 /* return number of bytes read into user buffer (will fit in int) */ 381 return (int)got; 382} 383 384/* -- see zlib.h -- */ |
384#undef gzgetc | 385#ifdef Z_PREFIX_SET 386# undef z_gzgetc 387#else 388# undef gzgetc 389#endif |
385int ZEXPORT gzgetc(file) 386 gzFile file; 387{ 388 int ret; 389 unsigned char buf[1]; 390 gz_statep state; 391 392 /* get internal structure */ --- 123 unchanged lines hidden (view full) --- 516 return NULL; /* error */ 517 if (state->x.have == 0) { /* end of file */ 518 state->past = 1; /* read past end */ 519 break; /* return what we have */ 520 } 521 522 /* look for end-of-line in current output buffer */ 523 n = state->x.have > left ? left : state->x.have; | 390int ZEXPORT gzgetc(file) 391 gzFile file; 392{ 393 int ret; 394 unsigned char buf[1]; 395 gz_statep state; 396 397 /* get internal structure */ --- 123 unchanged lines hidden (view full) --- 521 return NULL; /* error */ 522 if (state->x.have == 0) { /* end of file */ 523 state->past = 1; /* read past end */ 524 break; /* return what we have */ 525 } 526 527 /* look for end-of-line in current output buffer */ 528 n = state->x.have > left ? left : state->x.have; |
524 eol = memchr(state->x.next, '\n', n); | 529 eol = (unsigned char *)memchr(state->x.next, '\n', n); |
525 if (eol != NULL) 526 n = (unsigned)(eol - state->x.next) + 1; 527 528 /* copy through end-of-line, or remainder if not found */ 529 memcpy(buf, state->x.next, n); 530 state->x.have -= n; 531 state->x.next += n; 532 state->x.pos += n; --- 60 unchanged lines hidden --- | 530 if (eol != NULL) 531 n = (unsigned)(eol - state->x.next) + 1; 532 533 /* copy through end-of-line, or remainder if not found */ 534 memcpy(buf, state->x.next, n); 535 state->x.have -= n; 536 state->x.next += n; 537 state->x.pos += n; --- 60 unchanged lines hidden --- |