Lines Matching refs:state
29 state.state->fd, and update state.state->eof, state.state->err, and state.state->msg as appropriate.
32 local int gz_load(gz_statep state, unsigned char *buf, unsigned len,
42 ret = read(state.state->fd, buf + *have, get);
48 gz_error(state, Z_ERRNO, zstrerror());
52 state.state->eof = 1;
63 local int gz_avail(gz_statep state)
66 z_streamp strm = &(state.state->strm);
68 if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
70 if (state.state->eof == 0) {
72 unsigned char *p = state.state->in;
79 if (gz_load(state, state.state->in + strm->avail_in,
80 state.state->size - strm->avail_in, &got) == -1)
83 strm->next_in = state.state->in;
88 /* Look for gzip header, set up for inflate or copy. state.state->x.have must be 0.
89 If this is the first time in, allocate required memory. state.state->how will be
95 a user buffer. If decompressing, the inflate state will be initialized.
97 local int gz_look(gz_statep state) {
98 z_streamp strm = &(state.state->strm);
101 if (state.state->size == 0) {
103 state.state->in = (unsigned char *)malloc(state.state->want);
104 state.state->out = (unsigned char *)malloc(state.state->want << 1);
105 if (state.state->in == NULL || state.state->out == NULL) {
106 free(state.state->out);
107 free(state.state->in);
108 gz_error(state, Z_MEM_ERROR, "out of memory");
111 state.state->size = state.state->want;
114 state.state->strm.zalloc = Z_NULL;
115 state.state->strm.zfree = Z_NULL;
116 state.state->strm.opaque = Z_NULL;
117 state.state->strm.avail_in = 0;
118 state.state->strm.next_in = Z_NULL;
119 if (inflateInit2(&(state.state->strm), 15 + 16) != Z_OK) { /* gunzip */
120 free(state.state->out);
121 free(state.state->in);
122 state.state->size = 0;
123 gz_error(state, Z_MEM_ERROR, "out of memory");
130 if (gz_avail(state) == -1)
147 state.state->how = GZIP;
148 state.state->direct = 0;
154 if (state.state->direct == 0) {
156 state.state->eof = 1;
157 state.state->x.have = 0;
164 state.state->x.next = state.state->out;
166 memcpy(state.state->x.next, strm->next_in, strm->avail_in);
167 state.state->x.have = strm->avail_in;
170 state.state->how = COPY;
171 state.state->direct = 1;
175 /* Decompress from input to the provided next_out and avail_out in the state.
176 On return, state.state->x.have and state.state->x.next point to the just decompressed
177 data. If the gzip stream completes, state.state->how is reset to LOOK to look for
178 the next gzip stream or raw data, once state.state->x.have is depleted. Returns 0
180 local int gz_decomp(gz_statep state) {
183 z_streamp strm = &(state.state->strm);
189 if (strm->avail_in == 0 && gz_avail(state) == -1)
192 gz_error(state, Z_BUF_ERROR, "unexpected end of file");
199 gz_error(state, Z_STREAM_ERROR,
204 gz_error(state, Z_MEM_ERROR, "out of memory");
208 gz_error(state, Z_DATA_ERROR,
215 state.state->x.have = had - strm->avail_out;
216 state.state->x.next = strm->next_out - state.state->x.have;
220 state.state->how = LOOK;
226 /* Fetch data and put it in the output buffer. Assumes state.state->x.have is 0.
228 file depending on state.state->how. If state.state->how is LOOK, then a gzip header is
230 otherwise 0. gz_fetch() will leave state.state->how as COPY or GZIP unless the
232 local int gz_fetch(gz_statep state) {
233 z_streamp strm = &(state.state->strm);
236 switch(state.state->how) {
238 if (gz_look(state) == -1)
240 if (state.state->how == LOOK)
244 if (gz_load(state, state.state->out, state.state->size << 1, &(state.state->x.have))
247 state.state->x.next = state.state->out;
250 strm->avail_out = state.state->size << 1;
251 strm->next_out = state.state->out;
252 if (gz_decomp(state) == -1)
255 } while (state.state->x.have == 0 && (!state.state->eof || strm->avail_in));
260 local int gz_skip(gz_statep state, z_off64_t len) {
266 if (state.state->x.have) {
267 n = GT_OFF(state.state->x.have) || (z_off64_t)state.state->x.have > len ?
268 (unsigned)len : state.state->x.have;
269 state.state->x.have -= n;
270 state.state->x.next += n;
271 state.state->x.pos += n;
276 else if (state.state->eof && state.state->strm.avail_in == 0)
282 if (gz_fetch(state) == -1)
290 end of file was reached, or there was an error. state.state->err must be
292 local z_size_t gz_read(gz_statep state, voidp buf, z_size_t len) {
301 if (state.state->seek) {
302 state.state->seek = 0;
303 if (gz_skip(state, state.state->skip) == -1)
316 if (state.state->x.have) {
317 if (state.state->x.have < n)
318 n = state.state->x.have;
319 memcpy(buf, state.state->x.next, n);
320 state.state->x.next += n;
321 state.state->x.have -= n;
325 else if (state.state->eof && state.state->strm.avail_in == 0) {
326 state.state->past = 1; /* tried to read past end */
332 else if (state.state->how == LOOK || n < (state.state->size << 1)) {
334 if (gz_fetch(state) == -1)
342 else if (state.state->how == COPY) { /* read directly */
343 if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
348 else { /* state.state->how == GZIP */
349 state.state->strm.avail_out = n;
350 state.state->strm.next_out = (unsigned char *)buf;
351 if (gz_decomp(state) == -1)
353 n = state.state->x.have;
354 state.state->x.have = 0;
361 state.state->x.pos += n;
370 gz_statep state;
375 state.file = file;
378 if (state.state->mode != GZ_READ ||
379 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
385 gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
390 len = (unsigned)gz_read(state, buf, len);
393 if (len == 0 && state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
404 gz_statep state;
409 state.file = file;
412 if (state.state->mode != GZ_READ ||
413 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
419 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
424 return len ? gz_read(state, buf, len) / size : 0;
448 gz_statep state;
453 state.file = file;
456 if (state.state->mode != GZ_READ ||
457 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
461 if (state.state->x.have) {
462 state.state->x.have--;
463 state.state->x.pos++;
464 return *(state.state->x.next)++;
468 ret = (int)gz_read(state, buf, 1);
478 gz_statep state;
483 state.file = file;
486 if (state.state->mode != GZ_READ ||
487 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
491 if (state.state->seek) {
492 state.state->seek = 0;
493 if (gz_skip(state, state.state->skip) == -1)
502 if (state.state->x.have == 0) {
503 state.state->x.have = 1;
504 state.state->x.next = state.state->out + (state.state->size << 1) - 1;
505 state.state->x.next[0] = (unsigned char)c;
506 state.state->x.pos--;
507 state.state->past = 0;
512 if (state.state->x.have == (state.state->size << 1)) {
513 gz_error(state, Z_DATA_ERROR, "out of room to push characters");
518 if (state.state->x.next == state.state->out) {
519 unsigned char *src = state.state->out + state.state->x.have;
520 unsigned char *dest = state.state->out + (state.state->size << 1);
521 while (src > state.state->out)
523 state.state->x.next = dest;
525 state.state->x.have++;
526 state.state->x.next--;
527 state.state->x.next[0] = (unsigned char)c;
528 state.state->x.pos--;
529 state.state->past = 0;
538 gz_statep state;
543 state.file = file;
546 if (state.state->mode != GZ_READ ||
547 (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
551 if (state.state->seek) {
552 state.state->seek = 0;
553 if (gz_skip(state, state.state->skip) == -1)
564 if (state.state->x.have == 0 && gz_fetch(state) == -1)
566 if (state.state->x.have == 0) { /* end of file */
567 state.state->past = 1; /* read past end */
572 n = state.state->x.have > left ? left : state.state->x.have;
573 eol = (unsigned char *)memchr(state.state->x.next, '\n', n);
575 n = (unsigned)(eol - state.state->x.next) + 1;
578 memcpy(buf, state.state->x.next, n);
579 state.state->x.have -= n;
580 state.state->x.next += n;
581 state.state->x.pos += n;
595 gz_statep state;
600 state.file = file;
602 /* if the state is not known, but we can find out, then do so (this is
604 if (state.state->mode == GZ_READ && state.state->how == LOOK && state.state->x.have == 0)
605 (void)gz_look(state);
608 return state.state->direct;
614 gz_statep state;
619 state.file = file;
622 if (state.state->mode != GZ_READ)
626 if (state.state->size) {
627 inflateEnd(&(state.state->strm));
628 free(state.state->out);
629 free(state.state->in);
631 err = state.state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
632 gz_error(state, Z_OK, NULL);
633 free(state.state->path);
634 ret = close(state.state->fd);
635 free(state.state);