gzguts.h 6.5 KB

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