comparison zlib/gzlib.c @ 2690:9ef72ee5c0b0

Update vendored zlib to 1.3.1
author Michael Pavone <pavone@retrodev.com>
date Sun, 15 Jun 2025 15:39:33 -0700
parents 00d788dac91a
children
comparison
equal deleted inserted replaced
2689:bd6e33de0972 2690:9ef72ee5c0b0
1 /* gzlib.c -- zlib functions common to reading and writing gzip files 1 /* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004-2017 Mark Adler 2 * Copyright (C) 2004-2024 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6 #include "gzguts.h" 6 #include "gzguts.h"
7 7
8 #if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__) 8 #if defined(_WIN32) && !defined(__BORLANDC__)
9 # define LSEEK _lseeki64 9 # define LSEEK _lseeki64
10 #else 10 #else
11 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 11 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
12 # define LSEEK lseek64 12 # define LSEEK lseek64
13 #else 13 #else
14 # define LSEEK lseek 14 # define LSEEK lseek
15 #endif 15 #endif
16 #endif 16 #endif
17 17
18 /* Local functions */
19 local void gz_reset OF((gz_statep));
20 local gzFile gz_open OF((const void *, int, const char *));
21
22 #if defined UNDER_CE 18 #if defined UNDER_CE
23 19
24 /* Map the Windows error number in ERROR to a locale-dependent error message 20 /* Map the Windows error number in ERROR to a locale-dependent error message
25 string and return a pointer to it. Typically, the values for ERROR come 21 string and return a pointer to it. Typically, the values for ERROR come
26 from GetLastError. 22 from GetLastError.
28 The string pointed to shall not be modified by the application, but may be 24 The string pointed to shall not be modified by the application, but may be
29 overwritten by a subsequent call to gz_strwinerror 25 overwritten by a subsequent call to gz_strwinerror
30 26
31 The gz_strwinerror function does not change the current setting of 27 The gz_strwinerror function does not change the current setting of
32 GetLastError. */ 28 GetLastError. */
33 char ZLIB_INTERNAL *gz_strwinerror (error) 29 char ZLIB_INTERNAL *gz_strwinerror(DWORD error) {
34 DWORD error;
35 {
36 static char buf[1024]; 30 static char buf[1024];
37 31
38 wchar_t *msgbuf; 32 wchar_t *msgbuf;
39 DWORD lasterr = GetLastError(); 33 DWORD lasterr = GetLastError();
40 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM 34 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
70 } 64 }
71 65
72 #endif /* UNDER_CE */ 66 #endif /* UNDER_CE */
73 67
74 /* Reset gzip file state */ 68 /* Reset gzip file state */
75 local void gz_reset(state) 69 local void gz_reset(gz_statep state) {
76 gz_statep state;
77 {
78 state->x.have = 0; /* no output data available */ 70 state->x.have = 0; /* no output data available */
79 if (state->mode == GZ_READ) { /* for reading ... */ 71 if (state->mode == GZ_READ) { /* for reading ... */
80 state->eof = 0; /* not at end of file */ 72 state->eof = 0; /* not at end of file */
81 state->past = 0; /* have not read past end yet */ 73 state->past = 0; /* have not read past end yet */
82 state->how = LOOK; /* look for gzip header */ 74 state->how = LOOK; /* look for gzip header */
83 } 75 }
76 else /* for writing ... */
77 state->reset = 0; /* no deflateReset pending */
84 state->seek = 0; /* no seek request pending */ 78 state->seek = 0; /* no seek request pending */
85 gz_error(state, Z_OK, NULL); /* clear error */ 79 gz_error(state, Z_OK, NULL); /* clear error */
86 state->x.pos = 0; /* no uncompressed data yet */ 80 state->x.pos = 0; /* no uncompressed data yet */
87 state->strm.avail_in = 0; /* no input data yet */ 81 state->strm.avail_in = 0; /* no input data yet */
88 } 82 }
89 83
90 /* Open a gzip file either by name or file descriptor. */ 84 /* Open a gzip file either by name or file descriptor. */
91 local gzFile gz_open(path, fd, mode) 85 local gzFile gz_open(const void *path, int fd, const char *mode) {
92 const void *path;
93 int fd;
94 const char *mode;
95 {
96 gz_statep state; 86 gz_statep state;
97 z_size_t len; 87 z_size_t len;
98 int oflag; 88 int oflag;
99 #ifdef O_CLOEXEC 89 #ifdef O_CLOEXEC
100 int cloexec = 0; 90 int cloexec = 0;
265 /* return stream */ 255 /* return stream */
266 return (gzFile)state; 256 return (gzFile)state;
267 } 257 }
268 258
269 /* -- see zlib.h -- */ 259 /* -- see zlib.h -- */
270 gzFile ZEXPORT gzopen(path, mode) 260 gzFile ZEXPORT gzopen(const char *path, const char *mode) {
271 const char *path;
272 const char *mode;
273 {
274 return gz_open(path, -1, mode); 261 return gz_open(path, -1, mode);
275 } 262 }
276 263
277 /* -- see zlib.h -- */ 264 /* -- see zlib.h -- */
278 gzFile ZEXPORT gzopen64(path, mode) 265 gzFile ZEXPORT gzopen64(const char *path, const char *mode) {
279 const char *path;
280 const char *mode;
281 {
282 return gz_open(path, -1, mode); 266 return gz_open(path, -1, mode);
283 } 267 }
284 268
285 /* -- see zlib.h -- */ 269 /* -- see zlib.h -- */
286 gzFile ZEXPORT gzdopen(fd, mode) 270 gzFile ZEXPORT gzdopen(int fd, const char *mode) {
287 int fd;
288 const char *mode;
289 {
290 char *path; /* identifier for error messages */ 271 char *path; /* identifier for error messages */
291 gzFile gz; 272 gzFile gz;
292 273
293 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) 274 if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
294 return NULL; 275 return NULL;
302 return gz; 283 return gz;
303 } 284 }
304 285
305 /* -- see zlib.h -- */ 286 /* -- see zlib.h -- */
306 #ifdef WIDECHAR 287 #ifdef WIDECHAR
307 gzFile ZEXPORT gzopen_w(path, mode) 288 gzFile ZEXPORT gzopen_w(const wchar_t *path, const char *mode) {
308 const wchar_t *path;
309 const char *mode;
310 {
311 return gz_open(path, -2, mode); 289 return gz_open(path, -2, mode);
312 } 290 }
313 #endif 291 #endif
314 292
315 /* -- see zlib.h -- */ 293 /* -- see zlib.h -- */
316 int ZEXPORT gzbuffer(file, size) 294 int ZEXPORT gzbuffer(gzFile file, unsigned size) {
317 gzFile file;
318 unsigned size;
319 {
320 gz_statep state; 295 gz_statep state;
321 296
322 /* get internal structure and check integrity */ 297 /* get internal structure and check integrity */
323 if (file == NULL) 298 if (file == NULL)
324 return -1; 299 return -1;
331 return -1; 306 return -1;
332 307
333 /* check and set requested size */ 308 /* check and set requested size */
334 if ((size << 1) < size) 309 if ((size << 1) < size)
335 return -1; /* need to be able to double it */ 310 return -1; /* need to be able to double it */
336 if (size < 2) 311 if (size < 8)
337 size = 2; /* need two bytes to check magic header */ 312 size = 8; /* needed to behave well with flushing */
338 state->want = size; 313 state->want = size;
339 return 0; 314 return 0;
340 } 315 }
341 316
342 /* -- see zlib.h -- */ 317 /* -- see zlib.h -- */
343 int ZEXPORT gzrewind(file) 318 int ZEXPORT gzrewind(gzFile file) {
344 gzFile file;
345 {
346 gz_statep state; 319 gz_statep state;
347 320
348 /* get internal structure */ 321 /* get internal structure */
349 if (file == NULL) 322 if (file == NULL)
350 return -1; 323 return -1;
361 gz_reset(state); 334 gz_reset(state);
362 return 0; 335 return 0;
363 } 336 }
364 337
365 /* -- see zlib.h -- */ 338 /* -- see zlib.h -- */
366 z_off64_t ZEXPORT gzseek64(file, offset, whence) 339 z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) {
367 gzFile file;
368 z_off64_t offset;
369 int whence;
370 {
371 unsigned n; 340 unsigned n;
372 z_off64_t ret; 341 z_off64_t ret;
373 gz_statep state; 342 gz_statep state;
374 343
375 /* get internal structure and check integrity */ 344 /* get internal structure and check integrity */
395 state->seek = 0; 364 state->seek = 0;
396 365
397 /* if within raw area while reading, just go there */ 366 /* if within raw area while reading, just go there */
398 if (state->mode == GZ_READ && state->how == COPY && 367 if (state->mode == GZ_READ && state->how == COPY &&
399 state->x.pos + offset >= 0) { 368 state->x.pos + offset >= 0) {
400 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR); 369 ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR);
401 if (ret == -1) 370 if (ret == -1)
402 return -1; 371 return -1;
403 state->x.have = 0; 372 state->x.have = 0;
404 state->eof = 0; 373 state->eof = 0;
405 state->past = 0; 374 state->past = 0;
438 } 407 }
439 return state->x.pos + offset; 408 return state->x.pos + offset;
440 } 409 }
441 410
442 /* -- see zlib.h -- */ 411 /* -- see zlib.h -- */
443 z_off_t ZEXPORT gzseek(file, offset, whence) 412 z_off_t ZEXPORT gzseek(gzFile file, z_off_t offset, int whence) {
444 gzFile file;
445 z_off_t offset;
446 int whence;
447 {
448 z_off64_t ret; 413 z_off64_t ret;
449 414
450 ret = gzseek64(file, (z_off64_t)offset, whence); 415 ret = gzseek64(file, (z_off64_t)offset, whence);
451 return ret == (z_off_t)ret ? (z_off_t)ret : -1; 416 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
452 } 417 }
453 418
454 /* -- see zlib.h -- */ 419 /* -- see zlib.h -- */
455 z_off64_t ZEXPORT gztell64(file) 420 z_off64_t ZEXPORT gztell64(gzFile file) {
456 gzFile file;
457 {
458 gz_statep state; 421 gz_statep state;
459 422
460 /* get internal structure and check integrity */ 423 /* get internal structure and check integrity */
461 if (file == NULL) 424 if (file == NULL)
462 return -1; 425 return -1;
467 /* return position */ 430 /* return position */
468 return state->x.pos + (state->seek ? state->skip : 0); 431 return state->x.pos + (state->seek ? state->skip : 0);
469 } 432 }
470 433
471 /* -- see zlib.h -- */ 434 /* -- see zlib.h -- */
472 z_off_t ZEXPORT gztell(file) 435 z_off_t ZEXPORT gztell(gzFile file) {
473 gzFile file;
474 {
475 z_off64_t ret; 436 z_off64_t ret;
476 437
477 ret = gztell64(file); 438 ret = gztell64(file);
478 return ret == (z_off_t)ret ? (z_off_t)ret : -1; 439 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
479 } 440 }
480 441
481 /* -- see zlib.h -- */ 442 /* -- see zlib.h -- */
482 z_off64_t ZEXPORT gzoffset64(file) 443 z_off64_t ZEXPORT gzoffset64(gzFile file) {
483 gzFile file;
484 {
485 z_off64_t offset; 444 z_off64_t offset;
486 gz_statep state; 445 gz_statep state;
487 446
488 /* get internal structure and check integrity */ 447 /* get internal structure and check integrity */
489 if (file == NULL) 448 if (file == NULL)
500 offset -= state->strm.avail_in; /* don't count buffered input */ 459 offset -= state->strm.avail_in; /* don't count buffered input */
501 return offset; 460 return offset;
502 } 461 }
503 462
504 /* -- see zlib.h -- */ 463 /* -- see zlib.h -- */
505 z_off_t ZEXPORT gzoffset(file) 464 z_off_t ZEXPORT gzoffset(gzFile file) {
506 gzFile file;
507 {
508 z_off64_t ret; 465 z_off64_t ret;
509 466
510 ret = gzoffset64(file); 467 ret = gzoffset64(file);
511 return ret == (z_off_t)ret ? (z_off_t)ret : -1; 468 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
512 } 469 }
513 470
514 /* -- see zlib.h -- */ 471 /* -- see zlib.h -- */
515 int ZEXPORT gzeof(file) 472 int ZEXPORT gzeof(gzFile file) {
516 gzFile file;
517 {
518 gz_statep state; 473 gz_statep state;
519 474
520 /* get internal structure and check integrity */ 475 /* get internal structure and check integrity */
521 if (file == NULL) 476 if (file == NULL)
522 return 0; 477 return 0;
527 /* return end-of-file state */ 482 /* return end-of-file state */
528 return state->mode == GZ_READ ? state->past : 0; 483 return state->mode == GZ_READ ? state->past : 0;
529 } 484 }
530 485
531 /* -- see zlib.h -- */ 486 /* -- see zlib.h -- */
532 const char * ZEXPORT gzerror(file, errnum) 487 const char * ZEXPORT gzerror(gzFile file, int *errnum) {
533 gzFile file;
534 int *errnum;
535 {
536 gz_statep state; 488 gz_statep state;
537 489
538 /* get internal structure and check integrity */ 490 /* get internal structure and check integrity */
539 if (file == NULL) 491 if (file == NULL)
540 return NULL; 492 return NULL;
548 return state->err == Z_MEM_ERROR ? "out of memory" : 500 return state->err == Z_MEM_ERROR ? "out of memory" :
549 (state->msg == NULL ? "" : state->msg); 501 (state->msg == NULL ? "" : state->msg);
550 } 502 }
551 503
552 /* -- see zlib.h -- */ 504 /* -- see zlib.h -- */
553 void ZEXPORT gzclearerr(file) 505 void ZEXPORT gzclearerr(gzFile file) {
554 gzFile file;
555 {
556 gz_statep state; 506 gz_statep state;
557 507
558 /* get internal structure and check integrity */ 508 /* get internal structure and check integrity */
559 if (file == NULL) 509 if (file == NULL)
560 return; 510 return;
574 state->msg accordingly. Free any previous error message already there. Do 524 state->msg accordingly. Free any previous error message already there. Do
575 not try to free or allocate space if the error is Z_MEM_ERROR (out of 525 not try to free or allocate space if the error is Z_MEM_ERROR (out of
576 memory). Simply save the error message as a static string. If there is an 526 memory). Simply save the error message as a static string. If there is an
577 allocation failure constructing the error message, then convert the error to 527 allocation failure constructing the error message, then convert the error to
578 out of memory. */ 528 out of memory. */
579 void ZLIB_INTERNAL gz_error(state, err, msg) 529 void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
580 gz_statep state;
581 int err;
582 const char *msg;
583 {
584 /* free previously allocated message and clear */ 530 /* free previously allocated message and clear */
585 if (state->msg != NULL) { 531 if (state->msg != NULL) {
586 if (state->err != Z_MEM_ERROR) 532 if (state->err != Z_MEM_ERROR)
587 free(state->msg); 533 free(state->msg);
588 state->msg = NULL; 534 state->msg = NULL;
615 strcat(state->msg, ": "); 561 strcat(state->msg, ": ");
616 strcat(state->msg, msg); 562 strcat(state->msg, msg);
617 #endif 563 #endif
618 } 564 }
619 565
620 #ifndef INT_MAX
621 /* portably return maximum value for an int (when limits.h presumed not 566 /* portably return maximum value for an int (when limits.h presumed not
622 available) -- we need to do this to cover cases where 2's complement not 567 available) -- we need to do this to cover cases where 2's complement not
623 used, since C standard permits 1's complement and sign-bit representations, 568 used, since C standard permits 1's complement and sign-bit representations,
624 otherwise we could just use ((unsigned)-1) >> 1 */ 569 otherwise we could just use ((unsigned)-1) >> 1 */
625 unsigned ZLIB_INTERNAL gz_intmax() 570 unsigned ZLIB_INTERNAL gz_intmax(void) {
626 { 571 #ifdef INT_MAX
627 unsigned p, q; 572 return INT_MAX;
628 573 #else
629 p = 1; 574 unsigned p = 1, q;
630 do { 575 do {
631 q = p; 576 q = p;
632 p <<= 1; 577 p <<= 1;
633 p++; 578 p++;
634 } while (p > q); 579 } while (p > q);
635 return q >> 1; 580 return q >> 1;
636 } 581 #endif
637 #endif 582 }