Lines Matching defs:state

11    state->fd, and update state->eof, state->err, and state->msg as appropriate.
14 local int gz_load(gz_statep state, unsigned char *buf, unsigned len,
27 ret = read(state->fd, buf + *have, get);
35 gz_error(state, Z_ERRNO, zstrerror());
40 state->eof = 1;
52 local int gz_avail(gz_statep state)
55 z_streamp strm = &(state->strm);
57 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
61 if (state->eof == 0) {
63 unsigned char *p = state->in;
70 if (gz_load(state, state->in + strm->avail_in,
71 state->size - strm->avail_in, &got) == -1)
76 strm->next_in = state->in;
81 /* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
82 If this is the first time in, allocate required memory. state->how will be
88 a user buffer. If decompressing, the inflate state will be initialized.
90 local int gz_look(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 free(state->out);
101 free(state->in);
102 gz_error(state, Z_MEM_ERROR, "out of memory");
105 state->size = state->want;
108 state->strm.zalloc = Z_NULL;
109 state->strm.zfree = Z_NULL;
110 state->strm.opaque = Z_NULL;
111 state->strm.avail_in = 0;
112 state->strm.next_in = Z_NULL;
113 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
114 free(state->out);
115 free(state->in);
116 state->size = 0;
117 gz_error(state, Z_MEM_ERROR, "out of memory");
124 if (gz_avail(state) == -1)
144 state->how = GZIP;
145 state->direct = 0;
151 if (state->direct == 0) {
153 state->eof = 1;
154 state->x.have = 0;
161 state->x.next = state->out;
162 memcpy(state->x.next, strm->next_in, strm->avail_in);
163 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(gz_statep state)
179 z_streamp strm = &(state->strm);
185 if (strm->avail_in == 0 && gz_avail(state) == -1)
190 gz_error(state, Z_BUF_ERROR, "unexpected end of file");
197 gz_error(state, Z_STREAM_ERROR,
202 gz_error(state, Z_MEM_ERROR, "out of memory");
206 gz_error(state, Z_DATA_ERROR,
213 state->x.have = had - strm->avail_out;
214 state->x.next = strm->next_out - state->x.have;
218 state->how = LOOK;
224 /* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
226 file depending on state->how. If state->how is LOOK, then a gzip header is
228 otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
230 local int gz_fetch(gz_statep state)
232 z_streamp strm = &(state->strm);
235 switch(state->how) {
237 if (gz_look(state) == -1)
241 if (state->how == LOOK)
247 if (gz_load(state, state->out, state->size << 1, &(state->x.have))
252 state->x.next = state->out;
255 strm->avail_out = state->size << 1;
256 strm->next_out = state->out;
257 if (gz_decomp(state) == -1)
262 } while (state->x.have == 0 && (!state->eof || strm->avail_in));
267 local int gz_skip(gz_statep state, z_off64_t len)
274 if (state->x.have) {
275 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
276 (unsigned)len : state->x.have;
277 state->x.have -= n;
278 state->x.next += n;
279 state->x.pos += n;
284 else if (state->eof && state->strm.avail_in == 0)
292 if (gz_fetch(state) == -1)
302 end of file was reached, or there was an error. state->err must be
304 local z_size_t gz_read(gz_statep state, voidp buf, z_size_t len)
316 if (state->seek) {
317 state->seek = 0;
318 if (gz_skip(state, state->skip) == -1)
335 if (state->x.have) {
336 if (state->x.have < n)
338 n = state->x.have;
340 memcpy(buf, state->x.next, n);
341 state->x.next += n;
342 state->x.have -= n;
346 else if (state->eof && state->strm.avail_in == 0) {
347 state->past = 1; /* tried to read past end */
353 else if (state->how == LOOK || n < (state->size << 1)) {
355 if (gz_fetch(state) == -1)
365 else if (state->how == COPY) { /* read directly */
366 if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
373 else { /* state->how == GZIP */
374 state->strm.avail_out = n;
375 state->strm.next_out = (unsigned char *)buf;
376 if (gz_decomp(state) == -1)
380 n = state->x.have;
381 state->x.have = 0;
388 state->x.pos += n;
398 gz_statep state;
405 state = (gz_statep)file;
408 if (state->mode != GZ_READ ||
409 (state->err != Z_OK && state->err != Z_BUF_ERROR))
417 gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
422 len = (unsigned)gz_read(state, buf, len);
425 if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
438 gz_statep state;
445 state = (gz_statep)file;
448 if (state->mode != GZ_READ ||
449 (state->err != Z_OK && state->err != Z_BUF_ERROR))
457 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
462 return len ? gz_read(state, buf, len) / size : 0;
474 gz_statep state;
481 state = (gz_statep)file;
484 if (state->mode != GZ_READ ||
485 (state->err != Z_OK && state->err != Z_BUF_ERROR))
491 if (state->x.have) {
492 state->x.have--;
493 state->x.pos++;
494 return *(state->x.next)++;
498 return gz_read(state, buf, 1) < 1 ? -1 : buf[0];
509 gz_statep state;
516 state = (gz_statep)file;
519 if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
521 (void)gz_look(state);
525 if (state->mode != GZ_READ ||
526 (state->err != Z_OK && state->err != Z_BUF_ERROR))
532 if (state->seek) {
533 state->seek = 0;
534 if (gz_skip(state, state->skip) == -1)
547 if (state->x.have == 0) {
548 state->x.have = 1;
549 state->x.next = state->out + (state->size << 1) - 1;
550 state->x.next[0] = (unsigned char)c;
551 state->x.pos--;
552 state->past = 0;
557 if (state->x.have == (state->size << 1)) {
558 gz_error(state, Z_DATA_ERROR, "out of room to push characters");
563 if (state->x.next == state->out) {
564 unsigned char *src = state->out + state->x.have;
565 unsigned char *dest = state->out + (state->size << 1);
566 while (src > state->out){
569 state->x.next = dest;
571 state->x.have++;
572 state->x.next--;
573 state->x.next[0] = (unsigned char)c;
574 state->x.pos--;
575 state->past = 0;
585 gz_statep state;
592 state = (gz_statep)file;
595 if (state->mode != GZ_READ ||
596 (state->err != Z_OK && state->err != Z_BUF_ERROR))
602 if (state->seek) {
603 state->seek = 0;
604 if (gz_skip(state, state->skip) == -1)
617 if (state->x.have == 0 && gz_fetch(state) == -1)
621 if (state->x.have == 0) { /* end of file */
622 state->past = 1; /* read past end */
627 n = state->x.have > left ? left : state->x.have;
628 eol = (unsigned char *)memchr(state->x.next, '\n', n);
631 n = (unsigned)(eol - state->x.next) + 1;
635 memcpy(buf, state->x.next, n);
636 state->x.have -= n;
637 state->x.next += n;
638 state->x.pos += n;
655 gz_statep state;
662 state = (gz_statep)file;
664 /* if the state is not known, but we can find out, then do so (this is
666 if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
668 (void)gz_look(state);
672 return state->direct;
679 gz_statep state;
686 state = (gz_statep)file;
689 if (state->mode != GZ_READ)
695 if (state->size) {
696 inflateEnd(&(state->strm));
697 free(state->out);
698 free(state->in);
700 err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
701 gz_error(state, Z_OK, NULL);
702 free(state->path);
703 ret = close(state->fd);
704 free(state);