Lines Matching refs:state

20    state->fd, and update state->eof, state->err, and state->msg as appropriate.
23 local int gz_load(state, buf, len, have)
24 gz_statep state;
33 ret = read(state->fd, buf + *have, len - *have);
39 gz_error(state, Z_ERRNO, zstrerror());
43 state->eof = 1;
54 local int gz_avail(state)
55 gz_statep state;
58 z_streamp strm = &(state->strm);
60 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
62 if (state->eof == 0) {
64 unsigned char *p = state->in;
71 if (gz_load(state, state->in + strm->avail_in,
72 state->size - strm->avail_in, &got) == -1)
75 strm->next_in = state->in;
80 /* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
81 If this is the first time in, allocate required memory. state->how will be
87 a user buffer. If decompressing, the inflate state will be initialized.
89 local int gz_look(state)
90 gz_statep state;
92 z_streamp strm = &(state->strm);
95 if (state->size == 0) {
97 state->in = (unsigned char *)malloc(state->want);
98 state->out = (unsigned char *)malloc(state->want << 1);
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");
107 state->size = state->want;
110 state->strm.zalloc = Z_NULL;
111 state->strm.zfree = Z_NULL;
112 state->strm.opaque = Z_NULL;
113 state->strm.avail_in = 0;
114 state->strm.next_in = Z_NULL;
115 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
116 free(state->out);
117 free(state->in);
118 state->size = 0;
119 gz_error(state, Z_MEM_ERROR, "out of memory");
126 if (gz_avail(state) == -1)
142 state->how = GZIP;
143 state->direct = 0;
149 if (state->direct == 0) {
151 state->eof = 1;
152 state->x.have = 0;
159 state->x.next = state->out;
161 memcpy(state->x.next, strm->next_in, strm->avail_in);
162 state->x.have = strm->avail_in;
165 state->how = COPY;
166 state->direct = 1;
170 /* Decompress from input to the provided next_out and avail_out in the state.
171 On return, state->x.have and state->x.next point to the just decompressed
172 data. If the gzip stream completes, state->how is reset to LOOK to look for
173 the next gzip stream or raw data, once state->x.have is depleted. Returns 0
175 local int gz_decomp(state)
176 gz_statep state;
180 z_streamp strm = &(state->strm);
186 if (strm->avail_in == 0 && gz_avail(state) == -1)
189 gz_error(state, Z_BUF_ERROR, "unexpected end of file");
196 gz_error(state, Z_STREAM_ERROR,
201 gz_error(state, Z_MEM_ERROR, "out of memory");
205 gz_error(state, Z_DATA_ERROR,
212 state->x.have = had - strm->avail_out;
213 state->x.next = strm->next_out - state->x.have;
217 state->how = LOOK;
223 /* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
225 file depending on state->how. If state->how is LOOK, then a gzip header is
227 otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
229 local int gz_fetch(state)
230 gz_statep state;
232 z_streamp strm = &(state->strm);
235 switch(state->how) {
237 if (gz_look(state) == -1)
239 if (state->how == LOOK)
243 if (gz_load(state, state->out, state->size << 1, &(state->x.have))
246 state->x.next = state->out;
249 strm->avail_out = state->size << 1;
250 strm->next_out = state->out;
251 if (gz_decomp(state) == -1)
254 } while (state->x.have == 0 && (!state->eof || strm->avail_in));
259 local int gz_skip(state, len)
260 gz_statep state;
268 if (state->x.have) {
269 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
270 (unsigned)len : state->x.have;
271 state->x.have -= n;
272 state->x.next += n;
273 state->x.pos += n;
278 else if (state->eof && state->strm.avail_in == 0)
284 if (gz_fetch(state) == -1)
297 gz_statep state;
303 state = (gz_statep)file;
304 strm = &(state->strm);
307 if (state->mode != GZ_READ ||
308 (state->err != Z_OK && state->err != Z_BUF_ERROR))
314 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
323 if (state->seek) {
324 state->seek = 0;
325 if (gz_skip(state, state->skip) == -1)
333 if (state->x.have) {
334 n = state->x.have > len ? len : state->x.have;
335 memcpy(buf, state->x.next, n);
336 state->x.next += n;
337 state->x.have -= n;
341 else if (state->eof && strm->avail_in == 0) {
342 state->past = 1; /* tried to read past end */
348 else if (state->how == LOOK || len < (state->size << 1)) {
350 if (gz_fetch(state) == -1)
358 else if (state->how == COPY) { /* read directly */
359 if (gz_load(state, (unsigned char *)buf, len, &n) == -1)
364 else { /* state->how == GZIP */
367 if (gz_decomp(state) == -1)
369 n = state->x.have;
370 state->x.have = 0;
377 state->x.pos += n;
395 gz_statep state;
400 state = (gz_statep)file;
403 if (state->mode != GZ_READ ||
404 (state->err != Z_OK && state->err != Z_BUF_ERROR))
408 if (state->x.have) {
409 state->x.have--;
410 state->x.pos++;
411 return *(state->x.next)++;
430 gz_statep state;
435 state = (gz_statep)file;
438 if (state->mode != GZ_READ ||
439 (state->err != Z_OK && state->err != Z_BUF_ERROR))
443 if (state->seek) {
444 state->seek = 0;
445 if (gz_skip(state, state->skip) == -1)
454 if (state->x.have == 0) {
455 state->x.have = 1;
456 state->x.next = state->out + (state->size << 1) - 1;
457 state->x.next[0] = c;
458 state->x.pos--;
459 state->past = 0;
464 if (state->x.have == (state->size << 1)) {
465 gz_error(state, Z_DATA_ERROR, "out of room to push characters");
470 if (state->x.next == state->out) {
471 unsigned char *src = state->out + state->x.have;
472 unsigned char *dest = state->out + (state->size << 1);
473 while (src > state->out)
475 state->x.next = dest;
477 state->x.have++;
478 state->x.next--;
479 state->x.next[0] = c;
480 state->x.pos--;
481 state->past = 0;
494 gz_statep state;
499 state = (gz_statep)file;
502 if (state->mode != GZ_READ ||
503 (state->err != Z_OK && state->err != Z_BUF_ERROR))
507 if (state->seek) {
508 state->seek = 0;
509 if (gz_skip(state, state->skip) == -1)
520 if (state->x.have == 0 && gz_fetch(state) == -1)
522 if (state->x.have == 0) { /* end of file */
523 state->past = 1; /* read past end */
528 n = state->x.have > left ? left : state->x.have;
529 eol = (unsigned char *)memchr(state->x.next, '\n', n);
531 n = (unsigned)(eol - state->x.next) + 1;
534 memcpy(buf, state->x.next, n);
535 state->x.have -= n;
536 state->x.next += n;
537 state->x.pos += n;
553 gz_statep state;
558 state = (gz_statep)file;
560 /* if the state is not known, but we can find out, then do so (this is
562 if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
563 (void)gz_look(state);
566 return state->direct;
574 gz_statep state;
579 state = (gz_statep)file;
582 if (state->mode != GZ_READ)
586 if (state->size) {
587 inflateEnd(&(state->strm));
588 free(state->out);
589 free(state->in);
591 err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
592 gz_error(state, Z_OK, NULL);
593 free(state->path);
594 ret = close(state->fd);
595 free(state);