gzguts.h 7.0 KB

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