gzguts.h 6.9 KB

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