gzguts.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* gzguts.h -- zlib internal header definitions for gz* operations
  2. * Copyright (C) 2004-2024 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #ifdef _LARGEFILE64_SOURCE
  6. # ifndef _LARGEFILE_SOURCE
  7. # define _LARGEFILE_SOURCE 1
  8. # endif
  9. # undef _FILE_OFFSET_BITS
  10. # undef _TIME_BITS
  11. #endif
  12. #ifdef HAVE_HIDDEN
  13. # define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
  14. #else
  15. # define ZLIB_INTERNAL
  16. #endif
  17. #include <stdio.h>
  18. #include "zlib.h"
  19. #ifdef STDC
  20. # include <string.h>
  21. # include <stdlib.h>
  22. # include <limits.h>
  23. #endif
  24. #ifndef _POSIX_SOURCE
  25. # define _POSIX_SOURCE
  26. #endif
  27. #include <fcntl.h>
  28. #ifdef _WIN32
  29. # include <stddef.h>
  30. #else
  31. # include <unistd.h>
  32. #endif
  33. #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
  34. # include <io.h>
  35. #endif
  36. #if defined(_WIN32)
  37. # define WIDECHAR
  38. #endif
  39. #ifdef WINAPI_FAMILY
  40. # define open _open
  41. # define read _read
  42. # define write _write
  43. # define close _close
  44. #endif
  45. #ifdef NO_DEFLATE /* for compatibility with old definition */
  46. # define NO_GZCOMPRESS
  47. #endif
  48. #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
  49. # ifndef HAVE_VSNPRINTF
  50. # define HAVE_VSNPRINTF
  51. # endif
  52. #endif
  53. #if defined(__CYGWIN__)
  54. # ifndef HAVE_VSNPRINTF
  55. # define HAVE_VSNPRINTF
  56. # endif
  57. #endif
  58. #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
  59. # ifndef HAVE_VSNPRINTF
  60. # define HAVE_VSNPRINTF
  61. # endif
  62. #endif
  63. #ifndef HAVE_VSNPRINTF
  64. # ifdef MSDOS
  65. /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
  66. but for now we just assume it doesn't. */
  67. # define NO_vsnprintf
  68. # endif
  69. # ifdef __TURBOC__
  70. # define NO_vsnprintf
  71. # endif
  72. # ifdef WIN32
  73. /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
  74. # if !defined(vsnprintf) && !defined(NO_vsnprintf)
  75. # if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
  76. # define vsnprintf _vsnprintf
  77. # endif
  78. # endif
  79. # endif
  80. # ifdef __SASC
  81. # define NO_vsnprintf
  82. # endif
  83. # ifdef VMS
  84. # define NO_vsnprintf
  85. # endif
  86. # ifdef __OS400__
  87. # define NO_vsnprintf
  88. # endif
  89. # ifdef __MVS__
  90. # define NO_vsnprintf
  91. # endif
  92. #endif
  93. /* unlike snprintf (which is required in C99), _snprintf does not guarantee
  94. null termination of the result -- however this is only used in gzlib.c where
  95. the result is assured to fit in the space provided */
  96. #if defined(_MSC_VER) && _MSC_VER < 1900
  97. # define snprintf _snprintf
  98. #endif
  99. #ifndef local
  100. # define local static
  101. #endif
  102. /* since "static" is used to mean two completely different things in C, we
  103. define "local" for the non-static meaning of "static", for readability
  104. (compile with -Dlocal if your debugger can't find static symbols) */
  105. /* gz* functions always use library allocation functions */
  106. #ifndef STDC
  107. extern voidp malloc(uInt size);
  108. extern void free(voidpf ptr);
  109. #endif
  110. /* get errno and strerror definition */
  111. #if defined UNDER_CE
  112. # include <windows.h>
  113. # define zstrerror() gz_strwinerror((DWORD)GetLastError())
  114. #else
  115. # ifndef NO_STRERROR
  116. # include <errno.h>
  117. # define zstrerror() strerror(errno)
  118. # else
  119. # define zstrerror() "stdio error (consult errno)"
  120. # endif
  121. #endif
  122. /* provide prototypes for these when building zlib without LFS */
  123. #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
  124. ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *);
  125. ZEXTERN z_off64_t ZEXPORT gzseek64(gzFile, z_off64_t, int);
  126. ZEXTERN z_off64_t ZEXPORT gztell64(gzFile);
  127. ZEXTERN z_off64_t ZEXPORT gzoffset64(gzFile);
  128. #endif
  129. /* default memLevel */
  130. #if MAX_MEM_LEVEL >= 8
  131. # define DEF_MEM_LEVEL 8
  132. #else
  133. # define DEF_MEM_LEVEL MAX_MEM_LEVEL
  134. #endif
  135. /* default i/o buffer size -- double this for output when reading (this and
  136. twice this must be able to fit in an unsigned type) */
  137. #define GZBUFSIZE 8192
  138. /* gzip modes, also provide a little integrity check on the passed structure */
  139. #define GZ_NONE 0
  140. #define GZ_READ 7247
  141. #define GZ_WRITE 31153
  142. #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */
  143. /* values for gz_state how */
  144. #define LOOK 0 /* look for a gzip header */
  145. #define COPY 1 /* copy input directly */
  146. #define GZIP 2 /* decompress a gzip stream */
  147. /* internal gzip file state data structure */
  148. typedef struct {
  149. /* exposed contents for gzgetc() macro */
  150. struct gzFile_s x; /* "x" for exposed */
  151. /* x.have: number of bytes available at x.next */
  152. /* x.next: next output data to deliver or write */
  153. /* x.pos: current position in uncompressed data */
  154. /* used for both reading and writing */
  155. int mode; /* see gzip modes above */
  156. int fd; /* file descriptor */
  157. char *path; /* path or fd for error messages */
  158. unsigned size; /* buffer size, zero if not allocated yet */
  159. unsigned want; /* requested buffer size, default is GZBUFSIZE */
  160. unsigned char *in; /* input buffer (double-sized when writing) */
  161. unsigned char *out; /* output buffer (double-sized when reading) */
  162. int direct; /* 0 if processing gzip, 1 if transparent */
  163. /* just for reading */
  164. int how; /* 0: get header, 1: copy, 2: decompress */
  165. z_off64_t start; /* where the gzip data started, for rewinding */
  166. int eof; /* true if end of input file reached */
  167. int past; /* true if read requested past end */
  168. /* just for writing */
  169. int level; /* compression level */
  170. int strategy; /* compression strategy */
  171. int reset; /* true if a reset is pending after a Z_FINISH */
  172. /* seek request */
  173. z_off64_t skip; /* amount to skip (already rewound if backwards) */
  174. int seek; /* true if seek request pending */
  175. /* error information */
  176. int err; /* error code */
  177. char *msg; /* error message */
  178. /* zlib inflate or deflate stream */
  179. z_stream strm; /* stream structure in-place (not a pointer) */
  180. } gz_state;
  181. typedef gz_state FAR *gz_statep;
  182. /* shared functions */
  183. void ZLIB_INTERNAL gz_error(gz_statep, int, const char *);
  184. #if defined UNDER_CE
  185. char ZLIB_INTERNAL *gz_strwinerror(DWORD error);
  186. #endif
  187. /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
  188. value -- needed when comparing unsigned to z_off64_t, which is signed
  189. (possible z_off64_t types off_t, off64_t, and long are all signed) */
  190. unsigned ZLIB_INTERNAL gz_intmax(void);
  191. #define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())