Lines Matching refs:state

21    state->fd, and update state->eof, state->err, and state->msg as appropriate.
24 local int gz_load(state, buf, len, have)
25 gz_statep state;
38 ret = read(state->fd, buf + *have, get);
44 gz_error(state, Z_ERRNO, zstrerror());
48 state->eof = 1;
59 local int gz_avail(state)
60 gz_statep state;
63 z_streamp strm = &(state->strm);
65 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
67 if (state->eof == 0) {
69 unsigned char *p = state->in;
76 if (gz_load(state, state->in + strm->avail_in,
77 state->size - strm->avail_in, &got) == -1)
80 strm->next_in = state->in;
85 /* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
86 If this is the first time in, allocate required memory. state->how will be
92 a user buffer. If decompressing, the inflate state will be initialized.
94 local int gz_look(state)
95 gz_statep state;
97 z_streamp strm = &(state->strm);
100 if (state->size == 0) {
102 state->in = (unsigned char *)malloc(state->want);
103 state->out = (unsigned char *)malloc(state->want << 1);
104 if (state->in == NULL || state->out == NULL) {
105 free(state->out);
106 free(state->in);
107 gz_error(state, Z_MEM_ERROR, "out of memory");
110 state->size = state->want;
113 state->strm.zalloc = Z_NULL;
114 state->strm.zfree = Z_NULL;
115 state->strm.opaque = Z_NULL;
116 state->strm.avail_in = 0;
117 state->strm.next_in = Z_NULL;
118 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
119 free(state->out);
120 free(state->in);
121 state->size = 0;
122 gz_error(state, Z_MEM_ERROR, "out of memory");
129 if (gz_avail(state) == -1)
145 state->how = GZIP;
146 state->direct = 0;
152 if (state->direct == 0) {
154 state->eof = 1;
155 state->x.have = 0;
162 state->x.next = state->out;
164 memcpy(state->x.next, strm->next_in, strm->avail_in);
165 state->x.have = strm->avail_in;
168 state->how = COPY;
169 state->direct = 1;
173 /* Decompress from input to the provided next_out and avail_out in the state.
174 On return, state->x.have and state->x.next point to the just decompressed
175 data. If the gzip stream completes, state->how is reset to LOOK to look for
176 the next gzip stream or raw data, once state->x.have is depleted. Returns 0
178 local int gz_decomp(state)
179 gz_statep state;
183 z_streamp strm = &(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->x.have = had - strm->avail_out;
216 state->x.next = strm->next_out - state->x.have;
220 state->how = LOOK;
226 /* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
228 file depending on state->how. If state->how is LOOK, then a gzip header is
230 otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
232 local int gz_fetch(state)
233 gz_statep state;
235 z_streamp strm = &(state->strm);
238 switch(state->how) {
240 if (gz_look(state) == -1)
242 if (state->how == LOOK)
246 if (gz_load(state, state->out, state->size << 1, &(state->x.have))
249 state->x.next = state->out;
252 strm->avail_out = state->size << 1;
253 strm->next_out = state->out;
254 if (gz_decomp(state) == -1)
257 } while (state->x.have == 0 && (!state->eof || strm->avail_in));
262 local int gz_skip(state, len)
263 gz_statep state;
271 if (state->x.have) {
272 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
273 (unsigned)len : state->x.have;
274 state->x.have -= n;
275 state->x.next += n;
276 state->x.pos += n;
281 else if (state->eof && state->strm.avail_in == 0)
287 if (gz_fetch(state) == -1)
295 end of file was reached, or there was an error. state->err must be
297 local z_size_t gz_read(state, buf, len)
298 gz_statep state;
310 if (state->seek) {
311 state->seek = 0;
312 if (gz_skip(state, state->skip) == -1)
325 if (state->x.have) {
326 if (state->x.have < n)
327 n = state->x.have;
328 memcpy(buf, state->x.next, n);
329 state->x.next += n;
330 state->x.have -= n;
334 else if (state->eof && state->strm.avail_in == 0) {
335 state->past = 1; /* tried to read past end */
341 else if (state->how == LOOK || n < (state->size << 1)) {
343 if (gz_fetch(state) == -1)
351 else if (state->how == COPY) { /* read directly */
352 if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
357 else { /* state->how == GZIP */
358 state->strm.avail_out = n;
359 state->strm.next_out = (unsigned char *)buf;
360 if (gz_decomp(state) == -1)
362 n = state->x.have;
363 state->x.have = 0;
370 state->x.pos += n;
383 gz_statep state;
388 state = (gz_statep)file;
391 if (state->mode != GZ_READ ||
392 (state->err != Z_OK && state->err != Z_BUF_ERROR))
398 gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
403 len = gz_read(state, buf, len);
406 if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
421 gz_statep state;
426 state = (gz_statep)file;
429 if (state->mode != GZ_READ ||
430 (state->err != Z_OK && state->err != Z_BUF_ERROR))
436 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
441 return len ? gz_read(state, buf, len) / size : 0;
455 gz_statep state;
460 state = (gz_statep)file;
463 if (state->mode != GZ_READ ||
464 (state->err != Z_OK && state->err != Z_BUF_ERROR))
468 if (state->x.have) {
469 state->x.have--;
470 state->x.pos++;
471 return *(state->x.next)++;
475 ret = gz_read(state, buf, 1);
490 gz_statep state;
495 state = (gz_statep)file;
498 if (state->mode != GZ_READ ||
499 (state->err != Z_OK && state->err != Z_BUF_ERROR))
503 if (state->seek) {
504 state->seek = 0;
505 if (gz_skip(state, state->skip) == -1)
514 if (state->x.have == 0) {
515 state->x.have = 1;
516 state->x.next = state->out + (state->size << 1) - 1;
517 state->x.next[0] = (unsigned char)c;
518 state->x.pos--;
519 state->past = 0;
524 if (state->x.have == (state->size << 1)) {
525 gz_error(state, Z_DATA_ERROR, "out of room to push characters");
530 if (state->x.next == state->out) {
531 unsigned char *src = state->out + state->x.have;
532 unsigned char *dest = state->out + (state->size << 1);
533 while (src > state->out)
535 state->x.next = dest;
537 state->x.have++;
538 state->x.next--;
539 state->x.next[0] = (unsigned char)c;
540 state->x.pos--;
541 state->past = 0;
554 gz_statep state;
559 state = (gz_statep)file;
562 if (state->mode != GZ_READ ||
563 (state->err != Z_OK && state->err != Z_BUF_ERROR))
567 if (state->seek) {
568 state->seek = 0;
569 if (gz_skip(state, state->skip) == -1)
580 if (state->x.have == 0 && gz_fetch(state) == -1)
582 if (state->x.have == 0) { /* end of file */
583 state->past = 1; /* read past end */
588 n = state->x.have > left ? left : state->x.have;
589 eol = (unsigned char *)memchr(state->x.next, '\n', n);
591 n = (unsigned)(eol - state->x.next) + 1;
594 memcpy(buf, state->x.next, n);
595 state->x.have -= n;
596 state->x.next += n;
597 state->x.pos += n;
613 gz_statep state;
618 state = (gz_statep)file;
620 /* if the state is not known, but we can find out, then do so (this is
622 if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
623 (void)gz_look(state);
626 return state->direct;
634 gz_statep state;
639 state = (gz_statep)file;
642 if (state->mode != GZ_READ)
646 if (state->size) {
647 inflateEnd(&(state->strm));
648 free(state->out);
649 free(state->in);
651 err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
652 gz_error(state, Z_OK, NULL);
653 free(state->path);
654 ret = close(state->fd);
655 free(state);