Lines Matching refs:state
17 state->fd, and update state->eof, state->err, and state->msg as appropriate.
20 local int gz_load(state, buf, len, have)
21 gz_statep state;
30 ret = read(state->fd, buf + *have, len - *have);
36 gz_error(state, Z_ERRNO, zstrerror());
40 state->eof = 1;
51 local int gz_avail(state)
52 gz_statep state;
55 z_streamp strm = &(state->strm);
57 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
59 if (state->eof == 0) {
61 memmove(state->in, strm->next_in, strm->avail_in);
62 if (gz_load(state, state->in + strm->avail_in,
63 state->size - strm->avail_in, &got) == -1)
66 strm->next_in = state->in;
71 /* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
72 If this is the first time in, allocate required memory. state->how will be
78 a user buffer. If decompressing, the inflate state will be initialized.
80 local int gz_look(state)
81 gz_statep state;
83 z_streamp strm = &(state->strm);
86 if (state->size == 0) {
88 state->in = malloc(state->want);
89 state->out = malloc(state->want << 1);
90 if (state->in == NULL || state->out == NULL) {
91 if (state->out != NULL)
92 free(state->out);
93 if (state->in != NULL)
94 free(state->in);
95 gz_error(state, Z_MEM_ERROR, "out of memory");
98 state->size = state->want;
101 state->strm.zalloc = Z_NULL;
102 state->strm.zfree = Z_NULL;
103 state->strm.opaque = Z_NULL;
104 state->strm.avail_in = 0;
105 state->strm.next_in = Z_NULL;
106 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
107 free(state->out);
108 free(state->in);
109 state->size = 0;
110 gz_error(state, Z_MEM_ERROR, "out of memory");
117 if (gz_avail(state) == -1)
133 state->how = GZIP;
134 state->direct = 0;
140 if (state->direct == 0) {
142 state->eof = 1;
143 state->x.have = 0;
150 state->x.next = state->out;
152 memcpy(state->x.next, strm->next_in, strm->avail_in);
153 state->x.have = strm->avail_in;
156 state->how = COPY;
157 state->direct = 1;
161 /* Decompress from input to the provided next_out and avail_out in the state.
162 On return, state->x.have and state->x.next point to the just decompressed
163 data. If the gzip stream completes, state->how is reset to LOOK to look for
164 the next gzip stream or raw data, once state->x.have is depleted. Returns 0
166 local int gz_decomp(state)
167 gz_statep state;
171 z_streamp strm = &(state->strm);
177 if (strm->avail_in == 0 && gz_avail(state) == -1)
180 gz_error(state, Z_BUF_ERROR, "unexpected end of file");
187 gz_error(state, Z_STREAM_ERROR,
192 gz_error(state, Z_MEM_ERROR, "out of memory");
196 gz_error(state, Z_DATA_ERROR,
203 state->x.have = had - strm->avail_out;
204 state->x.next = strm->next_out - state->x.have;
208 state->how = LOOK;
214 /* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
216 file depending on state->how. If state->how is LOOK, then a gzip header is
218 otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
220 local int gz_fetch(state)
221 gz_statep state;
223 z_streamp strm = &(state->strm);
226 switch(state->how) {
228 if (gz_look(state) == -1)
230 if (state->how == LOOK)
234 if (gz_load(state, state->out, state->size << 1, &(state->x.have))
237 state->x.next = state->out;
240 strm->avail_out = state->size << 1;
241 strm->next_out = state->out;
242 if (gz_decomp(state) == -1)
245 } while (state->x.have == 0 && (!state->eof || strm->avail_in));
250 local int gz_skip(state, len)
251 gz_statep state;
259 if (state->x.have) {
260 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
261 (unsigned)len : state->x.have;
262 state->x.have -= n;
263 state->x.next += n;
264 state->x.pos += n;
269 else if (state->eof && state->strm.avail_in == 0)
275 if (gz_fetch(state) == -1)
288 gz_statep state;
294 state = (gz_statep)file;
295 strm = &(state->strm);
298 if (state->mode != GZ_READ ||
299 (state->err != Z_OK && state->err != Z_BUF_ERROR))
305 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
314 if (state->seek) {
315 state->seek = 0;
316 if (gz_skip(state, state->skip) == -1)
324 if (state->x.have) {
325 n = state->x.have > len ? len : state->x.have;
326 memcpy(buf, state->x.next, n);
327 state->x.next += n;
328 state->x.have -= n;
332 else if (state->eof && strm->avail_in == 0) {
333 state->past = 1; /* tried to read past end */
339 else if (state->how == LOOK || len < (state->size << 1)) {
341 if (gz_fetch(state) == -1)
349 else if (state->how == COPY) { /* read directly */
350 if (gz_load(state, buf, len, &n) == -1)
355 else { /* state->how == GZIP */
358 if (gz_decomp(state) == -1)
360 n = state->x.have;
361 state->x.have = 0;
368 state->x.pos += n;
381 gz_statep state;
386 state = (gz_statep)file;
389 if (state->mode != GZ_READ ||
390 (state->err != Z_OK && state->err != Z_BUF_ERROR))
394 if (state->x.have) {
395 state->x.have--;
396 state->x.pos++;
397 return *(state->x.next)++;
417 gz_statep state;
422 state = (gz_statep)file;
425 if (state->mode != GZ_READ ||
426 (state->err != Z_OK && state->err != Z_BUF_ERROR))
430 if (state->seek) {
431 state->seek = 0;
432 if (gz_skip(state, state->skip) == -1)
441 if (state->x.have == 0) {
442 state->x.have = 1;
443 state->x.next = state->out + (state->size << 1) - 1;
444 state->x.next[0] = c;
445 state->x.pos--;
446 state->past = 0;
451 if (state->x.have == (state->size << 1)) {
452 gz_error(state, Z_DATA_ERROR, "out of room to push characters");
457 if (state->x.next == state->out) {
458 unsigned char *src = state->out + state->x.have;
459 unsigned char *dest = state->out + (state->size << 1);
460 while (src > state->out)
462 state->x.next = dest;
464 state->x.have++;
465 state->x.next--;
466 state->x.next[0] = c;
467 state->x.pos--;
468 state->past = 0;
481 gz_statep state;
486 state = (gz_statep)file;
489 if (state->mode != GZ_READ ||
490 (state->err != Z_OK && state->err != Z_BUF_ERROR))
494 if (state->seek) {
495 state->seek = 0;
496 if (gz_skip(state, state->skip) == -1)
507 if (state->x.have == 0 && gz_fetch(state) == -1)
509 if (state->x.have == 0) { /* end of file */
510 state->past = 1; /* read past end */
515 n = state->x.have > left ? left : state->x.have;
516 eol = memchr(state->x.next, '\n', n);
518 n = (unsigned)(eol - state->x.next) + 1;
521 memcpy(buf, state->x.next, n);
522 state->x.have -= n;
523 state->x.next += n;
524 state->x.pos += n;
540 gz_statep state;
545 state = (gz_statep)file;
547 /* if the state is not known, but we can find out, then do so (this is
549 if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
550 (void)gz_look(state);
553 return state->direct;
561 gz_statep state;
566 state = (gz_statep)file;
569 if (state->mode != GZ_READ)
573 if (state->size) {
574 inflateEnd(&(state->strm));
575 free(state->out);
576 free(state->in);
578 err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
579 gz_error(state, Z_OK, NULL);
580 free(state->path);
581 ret = close(state->fd);
582 free(state);