File indexing completed on 2025-05-11 08:24:11
0001
0002
0003
0004
0005
0006 #include "gzguts.h"
0007
0008
0009 local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
0010 local int gz_avail OF((gz_statep));
0011 local int gz_look OF((gz_statep));
0012 local int gz_decomp OF((gz_statep));
0013 local int gz_fetch OF((gz_statep));
0014 local int gz_skip OF((gz_statep, z_off64_t));
0015 local z_size_t gz_read OF((gz_statep, voidp, z_size_t));
0016
0017
0018
0019
0020
0021 local int gz_load(state, buf, len, have)
0022 gz_statep state;
0023 unsigned char *buf;
0024 unsigned len;
0025 unsigned *have;
0026 {
0027 int ret;
0028 unsigned get, max = ((unsigned)-1 >> 2) + 1;
0029
0030 *have = 0;
0031 do {
0032 get = len - *have;
0033 if (get > max)
0034 get = max;
0035 ret = read(state->fd, buf + *have, get);
0036 if (ret <= 0)
0037 break;
0038 *have += (unsigned)ret;
0039 } while (*have < len);
0040 if (ret < 0) {
0041 gz_error(state, Z_ERRNO, zstrerror());
0042 return -1;
0043 }
0044 if (ret == 0)
0045 state->eof = 1;
0046 return 0;
0047 }
0048
0049
0050
0051
0052
0053
0054
0055
0056 local int gz_avail(state)
0057 gz_statep state;
0058 {
0059 unsigned got;
0060 z_streamp strm = &(state->strm);
0061
0062 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
0063 return -1;
0064 if (state->eof == 0) {
0065 if (strm->avail_in) {
0066 unsigned char *p = state->in;
0067 unsigned const char *q = strm->next_in;
0068 unsigned n = strm->avail_in;
0069 do {
0070 *p++ = *q++;
0071 } while (--n);
0072 }
0073 if (gz_load(state, state->in + strm->avail_in,
0074 state->size - strm->avail_in, &got) == -1)
0075 return -1;
0076 strm->avail_in += got;
0077 strm->next_in = state->in;
0078 }
0079 return 0;
0080 }
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091 local int gz_look(state)
0092 gz_statep state;
0093 {
0094 z_streamp strm = &(state->strm);
0095
0096
0097 if (state->size == 0) {
0098
0099 state->in = (unsigned char *)malloc(state->want);
0100 state->out = (unsigned char *)malloc(state->want << 1);
0101 if (state->in == NULL || state->out == NULL) {
0102 free(state->out);
0103 free(state->in);
0104 gz_error(state, Z_MEM_ERROR, "out of memory");
0105 return -1;
0106 }
0107 state->size = state->want;
0108
0109
0110 state->strm.zalloc = Z_NULL;
0111 state->strm.zfree = Z_NULL;
0112 state->strm.opaque = Z_NULL;
0113 state->strm.avail_in = 0;
0114 state->strm.next_in = Z_NULL;
0115 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) {
0116 free(state->out);
0117 free(state->in);
0118 state->size = 0;
0119 gz_error(state, Z_MEM_ERROR, "out of memory");
0120 return -1;
0121 }
0122 }
0123
0124
0125 if (strm->avail_in < 2) {
0126 if (gz_avail(state) == -1)
0127 return -1;
0128 if (strm->avail_in == 0)
0129 return 0;
0130 }
0131
0132
0133
0134
0135
0136
0137
0138
0139 if (strm->avail_in > 1 &&
0140 strm->next_in[0] == 31 && strm->next_in[1] == 139) {
0141 inflateReset(strm);
0142 state->how = GZIP;
0143 state->direct = 0;
0144 return 0;
0145 }
0146
0147
0148
0149 if (state->direct == 0) {
0150 strm->avail_in = 0;
0151 state->eof = 1;
0152 state->x.have = 0;
0153 return 0;
0154 }
0155
0156
0157
0158
0159 state->x.next = state->out;
0160 memcpy(state->x.next, strm->next_in, strm->avail_in);
0161 state->x.have = strm->avail_in;
0162 strm->avail_in = 0;
0163 state->how = COPY;
0164 state->direct = 1;
0165 return 0;
0166 }
0167
0168
0169
0170
0171
0172
0173 local int gz_decomp(state)
0174 gz_statep state;
0175 {
0176 int ret = Z_OK;
0177 unsigned had;
0178 z_streamp strm = &(state->strm);
0179
0180
0181 had = strm->avail_out;
0182 do {
0183
0184 if (strm->avail_in == 0 && gz_avail(state) == -1)
0185 return -1;
0186 if (strm->avail_in == 0) {
0187 gz_error(state, Z_BUF_ERROR, "unexpected end of file");
0188 break;
0189 }
0190
0191
0192 ret = inflate(strm, Z_NO_FLUSH);
0193 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
0194 gz_error(state, Z_STREAM_ERROR,
0195 "internal error: inflate stream corrupt");
0196 return -1;
0197 }
0198 if (ret == Z_MEM_ERROR) {
0199 gz_error(state, Z_MEM_ERROR, "out of memory");
0200 return -1;
0201 }
0202 if (ret == Z_DATA_ERROR) {
0203 gz_error(state, Z_DATA_ERROR,
0204 strm->msg == NULL ? "compressed data error" : strm->msg);
0205 return -1;
0206 }
0207 } while (strm->avail_out && ret != Z_STREAM_END);
0208
0209
0210 state->x.have = had - strm->avail_out;
0211 state->x.next = strm->next_out - state->x.have;
0212
0213
0214 if (ret == Z_STREAM_END)
0215 state->how = LOOK;
0216
0217
0218 return 0;
0219 }
0220
0221
0222
0223
0224
0225
0226
0227 local int gz_fetch(state)
0228 gz_statep state;
0229 {
0230 z_streamp strm = &(state->strm);
0231
0232 do {
0233 switch(state->how) {
0234 case LOOK:
0235 if (gz_look(state) == -1)
0236 return -1;
0237 if (state->how == LOOK)
0238 return 0;
0239 break;
0240 case COPY:
0241 if (gz_load(state, state->out, state->size << 1, &(state->x.have))
0242 == -1)
0243 return -1;
0244 state->x.next = state->out;
0245 return 0;
0246 case GZIP:
0247 strm->avail_out = state->size << 1;
0248 strm->next_out = state->out;
0249 if (gz_decomp(state) == -1)
0250 return -1;
0251 }
0252 } while (state->x.have == 0 && (!state->eof || strm->avail_in));
0253 return 0;
0254 }
0255
0256
0257 local int gz_skip(state, len)
0258 gz_statep state;
0259 z_off64_t len;
0260 {
0261 unsigned n;
0262
0263
0264 while (len)
0265
0266 if (state->x.have) {
0267 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
0268 (unsigned)len : state->x.have;
0269 state->x.have -= n;
0270 state->x.next += n;
0271 state->x.pos += n;
0272 len -= n;
0273 }
0274
0275
0276 else if (state->eof && state->strm.avail_in == 0)
0277 break;
0278
0279
0280 else {
0281
0282 if (gz_fetch(state) == -1)
0283 return -1;
0284 }
0285 return 0;
0286 }
0287
0288
0289
0290
0291
0292 local z_size_t gz_read(state, buf, len)
0293 gz_statep state;
0294 voidp buf;
0295 z_size_t len;
0296 {
0297 z_size_t got;
0298 unsigned n;
0299
0300
0301 if (len == 0)
0302 return 0;
0303
0304
0305 if (state->seek) {
0306 state->seek = 0;
0307 if (gz_skip(state, state->skip) == -1)
0308 return 0;
0309 }
0310
0311
0312 got = 0;
0313 do {
0314
0315 n = (unsigned)-1;
0316 if (n > len)
0317 n = (unsigned)len;
0318
0319
0320 if (state->x.have) {
0321 if (state->x.have < n)
0322 n = state->x.have;
0323 memcpy(buf, state->x.next, n);
0324 state->x.next += n;
0325 state->x.have -= n;
0326 }
0327
0328
0329 else if (state->eof && state->strm.avail_in == 0) {
0330 state->past = 1;
0331 break;
0332 }
0333
0334
0335
0336 else if (state->how == LOOK || n < (state->size << 1)) {
0337
0338 if (gz_fetch(state) == -1)
0339 return 0;
0340 continue;
0341
0342
0343 }
0344
0345
0346 else if (state->how == COPY) {
0347 if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
0348 return 0;
0349 }
0350
0351
0352 else {
0353 state->strm.avail_out = n;
0354 state->strm.next_out = (unsigned char *)buf;
0355 if (gz_decomp(state) == -1)
0356 return 0;
0357 n = state->x.have;
0358 state->x.have = 0;
0359 }
0360
0361
0362 len -= n;
0363 buf = (char *)buf + n;
0364 got += n;
0365 state->x.pos += n;
0366 } while (len);
0367
0368
0369 return got;
0370 }
0371
0372
0373 int ZEXPORT gzread(file, buf, len)
0374 gzFile file;
0375 voidp buf;
0376 unsigned len;
0377 {
0378 gz_statep state;
0379
0380
0381 if (file == NULL)
0382 return -1;
0383 state = (gz_statep)file;
0384
0385
0386 if (state->mode != GZ_READ ||
0387 (state->err != Z_OK && state->err != Z_BUF_ERROR))
0388 return -1;
0389
0390
0391
0392 if ((int)len < 0) {
0393 gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
0394 return -1;
0395 }
0396
0397
0398 len = (unsigned)gz_read(state, buf, len);
0399
0400
0401 if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
0402 return -1;
0403
0404
0405 return (int)len;
0406 }
0407
0408
0409 z_size_t ZEXPORT gzfread(buf, size, nitems, file)
0410 voidp buf;
0411 z_size_t size;
0412 z_size_t nitems;
0413 gzFile file;
0414 {
0415 z_size_t len;
0416 gz_statep state;
0417
0418
0419 if (file == NULL)
0420 return 0;
0421 state = (gz_statep)file;
0422
0423
0424 if (state->mode != GZ_READ ||
0425 (state->err != Z_OK && state->err != Z_BUF_ERROR))
0426 return 0;
0427
0428
0429 len = nitems * size;
0430 if (size && len / size != nitems) {
0431 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
0432 return 0;
0433 }
0434
0435
0436 return len ? gz_read(state, buf, len) / size : 0;
0437 }
0438
0439
0440 #ifdef Z_PREFIX_SET
0441 # undef z_gzgetc
0442 #else
0443 # undef gzgetc
0444 #endif
0445 int ZEXPORT gzgetc(file)
0446 gzFile file;
0447 {
0448 unsigned char buf[1];
0449 gz_statep state;
0450
0451
0452 if (file == NULL)
0453 return -1;
0454 state = (gz_statep)file;
0455
0456
0457 if (state->mode != GZ_READ ||
0458 (state->err != Z_OK && state->err != Z_BUF_ERROR))
0459 return -1;
0460
0461
0462 if (state->x.have) {
0463 state->x.have--;
0464 state->x.pos++;
0465 return *(state->x.next)++;
0466 }
0467
0468
0469 return gz_read(state, buf, 1) < 1 ? -1 : buf[0];
0470 }
0471
0472 int ZEXPORT gzgetc_(file)
0473 gzFile file;
0474 {
0475 return gzgetc(file);
0476 }
0477
0478
0479 int ZEXPORT gzungetc(c, file)
0480 int c;
0481 gzFile file;
0482 {
0483 gz_statep state;
0484
0485
0486 if (file == NULL)
0487 return -1;
0488 state = (gz_statep)file;
0489
0490
0491 if (state->mode != GZ_READ ||
0492 (state->err != Z_OK && state->err != Z_BUF_ERROR))
0493 return -1;
0494
0495
0496 if (state->seek) {
0497 state->seek = 0;
0498 if (gz_skip(state, state->skip) == -1)
0499 return -1;
0500 }
0501
0502
0503 if (c < 0)
0504 return -1;
0505
0506
0507 if (state->x.have == 0) {
0508 state->x.have = 1;
0509 state->x.next = state->out + (state->size << 1) - 1;
0510 state->x.next[0] = (unsigned char)c;
0511 state->x.pos--;
0512 state->past = 0;
0513 return c;
0514 }
0515
0516
0517 if (state->x.have == (state->size << 1)) {
0518 gz_error(state, Z_DATA_ERROR, "out of room to push characters");
0519 return -1;
0520 }
0521
0522
0523 if (state->x.next == state->out) {
0524 unsigned char *src = state->out + state->x.have;
0525 unsigned char *dest = state->out + (state->size << 1);
0526 while (src > state->out)
0527 *--dest = *--src;
0528 state->x.next = dest;
0529 }
0530 state->x.have++;
0531 state->x.next--;
0532 state->x.next[0] = (unsigned char)c;
0533 state->x.pos--;
0534 state->past = 0;
0535 return c;
0536 }
0537
0538
0539 char * ZEXPORT gzgets(file, buf, len)
0540 gzFile file;
0541 char *buf;
0542 int len;
0543 {
0544 unsigned left, n;
0545 char *str;
0546 unsigned char *eol;
0547 gz_statep state;
0548
0549
0550 if (file == NULL || buf == NULL || len < 1)
0551 return NULL;
0552 state = (gz_statep)file;
0553
0554
0555 if (state->mode != GZ_READ ||
0556 (state->err != Z_OK && state->err != Z_BUF_ERROR))
0557 return NULL;
0558
0559
0560 if (state->seek) {
0561 state->seek = 0;
0562 if (gz_skip(state, state->skip) == -1)
0563 return NULL;
0564 }
0565
0566
0567
0568
0569 str = buf;
0570 left = (unsigned)len - 1;
0571 if (left) do {
0572
0573 if (state->x.have == 0 && gz_fetch(state) == -1)
0574 return NULL;
0575 if (state->x.have == 0) {
0576 state->past = 1;
0577 break;
0578 }
0579
0580
0581 n = state->x.have > left ? left : state->x.have;
0582 eol = (unsigned char *)memchr(state->x.next, '\n', n);
0583 if (eol != NULL)
0584 n = (unsigned)(eol - state->x.next) + 1;
0585
0586
0587 memcpy(buf, state->x.next, n);
0588 state->x.have -= n;
0589 state->x.next += n;
0590 state->x.pos += n;
0591 left -= n;
0592 buf += n;
0593 } while (left && eol == NULL);
0594
0595
0596 if (buf == str)
0597 return NULL;
0598 buf[0] = 0;
0599 return str;
0600 }
0601
0602
0603 int ZEXPORT gzdirect(file)
0604 gzFile file;
0605 {
0606 gz_statep state;
0607
0608
0609 if (file == NULL)
0610 return 0;
0611 state = (gz_statep)file;
0612
0613
0614
0615 if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
0616 (void)gz_look(state);
0617
0618
0619 return state->direct;
0620 }
0621
0622
0623 int ZEXPORT gzclose_r(file)
0624 gzFile file;
0625 {
0626 int ret, err;
0627 gz_statep state;
0628
0629
0630 if (file == NULL)
0631 return Z_STREAM_ERROR;
0632 state = (gz_statep)file;
0633
0634
0635 if (state->mode != GZ_READ)
0636 return Z_STREAM_ERROR;
0637
0638
0639 if (state->size) {
0640 inflateEnd(&(state->strm));
0641 free(state->out);
0642 free(state->in);
0643 }
0644 err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
0645 gz_error(state, Z_OK, NULL);
0646 free(state->path);
0647 ret = close(state->fd);
0648 free(state);
0649 return ret ? Z_ERRNO : err;
0650 }