Lines Matching defs:state
9 state->fd, and update state->eof, state->err, and state->msg as appropriate.
12 local int gz_load(gz_statep state, unsigned char *buf, unsigned len,
22 ret = read(state->fd, buf + *have, get);
28 gz_error(state, Z_ERRNO, zstrerror());
32 state->eof = 1;
43 local int gz_avail(gz_statep state) {
45 z_streamp strm = &(state->strm);
47 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
49 if (state->eof == 0) {
51 unsigned char *p = state->in;
58 if (gz_load(state, state->in + strm->avail_in,
59 state->size - strm->avail_in, &got) == -1)
62 strm->next_in = state->in;
67 /* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
68 If this is the first time in, allocate required memory. state->how will be
74 a user buffer. If decompressing, the inflate state will be initialized.
76 local int gz_look(gz_statep state) {
77 z_streamp strm = &(state->strm);
80 if (state->size == 0) {
82 state->in = (unsigned char *)malloc(state->want);
83 state->out = (unsigned char *)malloc(state->want << 1);
84 if (state->in == NULL || state->out == NULL) {
85 free(state->out);
86 free(state->in);
87 gz_error(state, Z_MEM_ERROR, "out of memory");
90 state->size = state->want;
93 state->strm.zalloc = Z_NULL;
94 state->strm.zfree = Z_NULL;
95 state->strm.opaque = Z_NULL;
96 state->strm.avail_in = 0;
97 state->strm.next_in = Z_NULL;
98 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
99 free(state->out);
100 free(state->in);
101 state->size = 0;
102 gz_error(state, Z_MEM_ERROR, "out of memory");
109 if (gz_avail(state) == -1)
125 state->how = GZIP;
126 state->direct = 0;
132 if (state->direct == 0) {
134 state->eof = 1;
135 state->x.have = 0;
142 state->x.next = state->out;
143 memcpy(state->x.next, strm->next_in, strm->avail_in);
144 state->x.have = strm->avail_in;
146 state->how = COPY;
147 state->direct = 1;
151 /* Decompress from input to the provided next_out and avail_out in the state.
152 On return, state->x.have and state->x.next point to the just decompressed
153 data. If the gzip stream completes, state->how is reset to LOOK to look for
154 the next gzip stream or raw data, once state->x.have is depleted. Returns 0
156 local int gz_decomp(gz_statep state) {
159 z_streamp strm = &(state->strm);
165 if (strm->avail_in == 0 && gz_avail(state) == -1)
168 gz_error(state, Z_BUF_ERROR, "unexpected end of file");
175 gz_error(state, Z_STREAM_ERROR,
180 gz_error(state, Z_MEM_ERROR, "out of memory");
184 gz_error(state, Z_DATA_ERROR,
191 state->x.have = had - strm->avail_out;
192 state->x.next = strm->next_out - state->x.have;
196 state->how = LOOK;
202 /* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
204 file depending on state->how. If state->how is LOOK, then a gzip header is
206 otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
208 local int gz_fetch(gz_statep state) {
209 z_streamp strm = &(state->strm);
212 switch(state->how) {
214 if (gz_look(state) == -1)
216 if (state->how == LOOK)
220 if (gz_load(state, state->out, state->size << 1, &(state->x.have))
223 state->x.next = state->out;
226 strm->avail_out = state->size << 1;
227 strm->next_out = state->out;
228 if (gz_decomp(state) == -1)
231 } while (state->x.have == 0 && (!state->eof || strm->avail_in));
236 local int gz_skip(gz_statep state, z_off64_t len) {
242 if (state->x.have) {
243 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
244 (unsigned)len : state->x.have;
245 state->x.have -= n;
246 state->x.next += n;
247 state->x.pos += n;
252 else if (state->eof && state->strm.avail_in == 0)
258 if (gz_fetch(state) == -1)
266 end of file was reached, or there was an error. state->err must be
268 local z_size_t gz_read(gz_statep state, voidp buf, z_size_t len) {
277 if (state->seek) {
278 state->seek = 0;
279 if (gz_skip(state, state->skip) == -1)
292 if (state->x.have) {
293 if (state->x.have < n)
294 n = state->x.have;
295 memcpy(buf, state->x.next, n);
296 state->x.next += n;
297 state->x.have -= n;
301 else if (state->eof && state->strm.avail_in == 0) {
302 state->past = 1; /* tried to read past end */
308 else if (state->how == LOOK || n < (state->size << 1)) {
310 if (gz_fetch(state) == -1)
318 else if (state->how == COPY) { /* read directly */
319 if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
324 else { /* state->how == GZIP */
325 state->strm.avail_out = n;
326 state->strm.next_out = (unsigned char *)buf;
327 if (gz_decomp(state) == -1)
329 n = state->x.have;
330 state->x.have = 0;
337 state->x.pos += n;
346 gz_statep state;
351 state = (gz_statep)file;
354 if (state->mode != GZ_READ ||
355 (state->err != Z_OK && state->err != Z_BUF_ERROR))
361 gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
366 len = (unsigned)gz_read(state, buf, len);
369 if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
379 gz_statep state;
384 state = (gz_statep)file;
387 if (state->mode != GZ_READ ||
388 (state->err != Z_OK && state->err != Z_BUF_ERROR))
394 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
399 return len ? gz_read(state, buf, len) / size : 0;
414 gz_statep state;
419 state = (gz_statep)file;
422 if (state->mode != GZ_READ ||
423 (state->err != Z_OK && state->err != Z_BUF_ERROR))
427 if (state->x.have) {
428 state->x.have--;
429 state->x.pos++;
430 return *(state->x.next)++;
434 return gz_read(state, buf, 1) < 1 ? -1 : buf[0];
443 gz_statep state;
448 state = (gz_statep)file;
451 if (state->mode != GZ_READ ||
452 (state->err != Z_OK && state->err != Z_BUF_ERROR))
456 if (state->seek) {
457 state->seek = 0;
458 if (gz_skip(state, state->skip) == -1)
467 if (state->x.have == 0) {
468 state->x.have = 1;
469 state->x.next = state->out + (state->size << 1) - 1;
470 state->x.next[0] = (unsigned char)c;
471 state->x.pos--;
472 state->past = 0;
477 if (state->x.have == (state->size << 1)) {
478 gz_error(state, Z_DATA_ERROR, "out of room to push characters");
483 if (state->x.next == state->out) {
484 unsigned char *src = state->out + state->x.have;
485 unsigned char *dest = state->out + (state->size << 1);
486 while (src > state->out)
488 state->x.next = dest;
490 state->x.have++;
491 state->x.next--;
492 state->x.next[0] = (unsigned char)c;
493 state->x.pos--;
494 state->past = 0;
503 gz_statep state;
508 state = (gz_statep)file;
511 if (state->mode != GZ_READ ||
512 (state->err != Z_OK && state->err != Z_BUF_ERROR))
516 if (state->seek) {
517 state->seek = 0;
518 if (gz_skip(state, state->skip) == -1)
529 if (state->x.have == 0 && gz_fetch(state) == -1)
531 if (state->x.have == 0) { /* end of file */
532 state->past = 1; /* read past end */
537 n = state->x.have > left ? left : state->x.have;
538 eol = (unsigned char *)memchr(state->x.next, '\n', n);
540 n = (unsigned)(eol - state->x.next) + 1;
543 memcpy(buf, state->x.next, n);
544 state->x.have -= n;
545 state->x.next += n;
546 state->x.pos += n;
560 gz_statep state;
565 state = (gz_statep)file;
567 /* if the state is not known, but we can find out, then do so (this is
569 if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
570 (void)gz_look(state);
573 return state->direct;
579 gz_statep state;
584 state = (gz_statep)file;
587 if (state->mode != GZ_READ)
591 if (state->size) {
592 inflateEnd(&(state->strm));
593 free(state->out);
594 free(state->in);
596 err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
597 gz_error(state, Z_OK, NULL);
598 free(state->path);
599 ret = close(state->fd);
600 free(state);