zstdlib 0.9.0-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +6 -0
  3. data/CHANGES.md +78 -0
  4. data/Gemfile +3 -0
  5. data/README.md +107 -0
  6. data/Rakefile +59 -0
  7. data/ext/zstdlib_c/extconf.rb +54 -0
  8. data/ext/zstdlib_c/ruby/zlib-2.2/zstdlib.c +4675 -0
  9. data/ext/zstdlib_c/ruby/zlib-2.3/zstdlib.c +4702 -0
  10. data/ext/zstdlib_c/ruby/zlib-2.4/zstdlib.c +4859 -0
  11. data/ext/zstdlib_c/ruby/zlib-2.5/zstdlib.c +4864 -0
  12. data/ext/zstdlib_c/ruby/zlib-2.6/zstdlib.c +4906 -0
  13. data/ext/zstdlib_c/ruby/zlib-2.7/zstdlib.c +4895 -0
  14. data/ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c +4994 -0
  15. data/ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c +5076 -0
  16. data/ext/zstdlib_c/zlib-1.2.11/adler32.c +186 -0
  17. data/ext/zstdlib_c/zlib-1.2.11/compress.c +86 -0
  18. data/ext/zstdlib_c/zlib-1.2.11/crc32.c +442 -0
  19. data/ext/zstdlib_c/zlib-1.2.11/crc32.h +441 -0
  20. data/ext/zstdlib_c/zlib-1.2.11/deflate.c +2163 -0
  21. data/ext/zstdlib_c/zlib-1.2.11/deflate.h +349 -0
  22. data/ext/zstdlib_c/zlib-1.2.11/gzclose.c +25 -0
  23. data/ext/zstdlib_c/zlib-1.2.11/gzguts.h +218 -0
  24. data/ext/zstdlib_c/zlib-1.2.11/gzlib.c +637 -0
  25. data/ext/zstdlib_c/zlib-1.2.11/gzread.c +654 -0
  26. data/ext/zstdlib_c/zlib-1.2.11/gzwrite.c +665 -0
  27. data/ext/zstdlib_c/zlib-1.2.11/infback.c +640 -0
  28. data/ext/zstdlib_c/zlib-1.2.11/inffast.c +323 -0
  29. data/ext/zstdlib_c/zlib-1.2.11/inffast.h +11 -0
  30. data/ext/zstdlib_c/zlib-1.2.11/inffixed.h +94 -0
  31. data/ext/zstdlib_c/zlib-1.2.11/inflate.c +1561 -0
  32. data/ext/zstdlib_c/zlib-1.2.11/inflate.h +125 -0
  33. data/ext/zstdlib_c/zlib-1.2.11/inftrees.c +304 -0
  34. data/ext/zstdlib_c/zlib-1.2.11/inftrees.h +62 -0
  35. data/ext/zstdlib_c/zlib-1.2.11/trees.c +1203 -0
  36. data/ext/zstdlib_c/zlib-1.2.11/trees.h +128 -0
  37. data/ext/zstdlib_c/zlib-1.2.11/uncompr.c +93 -0
  38. data/ext/zstdlib_c/zlib-1.2.11/zconf.h +534 -0
  39. data/ext/zstdlib_c/zlib-1.2.11/zlib.h +1912 -0
  40. data/ext/zstdlib_c/zlib-1.2.11/zutil.c +325 -0
  41. data/ext/zstdlib_c/zlib-1.2.11/zutil.h +271 -0
  42. data/ext/zstdlib_c/zlib.mk +14 -0
  43. data/ext/zstdlib_c/zlibwrapper/zlibwrapper.c +10 -0
  44. data/ext/zstdlib_c/zlibwrapper.mk +14 -0
  45. data/ext/zstdlib_c/zstd-1.5.2/lib/common/bitstream.h +478 -0
  46. data/ext/zstdlib_c/zstd-1.5.2/lib/common/compiler.h +335 -0
  47. data/ext/zstdlib_c/zstd-1.5.2/lib/common/cpu.h +213 -0
  48. data/ext/zstdlib_c/zstd-1.5.2/lib/common/debug.c +24 -0
  49. data/ext/zstdlib_c/zstd-1.5.2/lib/common/debug.h +107 -0
  50. data/ext/zstdlib_c/zstd-1.5.2/lib/common/entropy_common.c +368 -0
  51. data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.c +56 -0
  52. data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.h +159 -0
  53. data/ext/zstdlib_c/zstd-1.5.2/lib/common/fse.h +717 -0
  54. data/ext/zstdlib_c/zstd-1.5.2/lib/common/fse_decompress.c +403 -0
  55. data/ext/zstdlib_c/zstd-1.5.2/lib/common/huf.h +364 -0
  56. data/ext/zstdlib_c/zstd-1.5.2/lib/common/mem.h +442 -0
  57. data/ext/zstdlib_c/zstd-1.5.2/lib/common/pool.c +355 -0
  58. data/ext/zstdlib_c/zstd-1.5.2/lib/common/pool.h +84 -0
  59. data/ext/zstdlib_c/zstd-1.5.2/lib/common/portability_macros.h +137 -0
  60. data/ext/zstdlib_c/zstd-1.5.2/lib/common/threading.c +122 -0
  61. data/ext/zstdlib_c/zstd-1.5.2/lib/common/threading.h +155 -0
  62. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.c +24 -0
  63. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.h +5686 -0
  64. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_common.c +83 -0
  65. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_deps.h +111 -0
  66. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_internal.h +493 -0
  67. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_trace.h +163 -0
  68. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/clevels.h +134 -0
  69. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/fse_compress.c +741 -0
  70. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/hist.c +181 -0
  71. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/hist.h +75 -0
  72. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/huf_compress.c +1370 -0
  73. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress.c +6327 -0
  74. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_internal.h +1458 -0
  75. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_literals.c +159 -0
  76. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_literals.h +31 -0
  77. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_sequences.c +442 -0
  78. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_sequences.h +54 -0
  79. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.c +573 -0
  80. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.h +32 -0
  81. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_cwksp.h +676 -0
  82. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_double_fast.c +696 -0
  83. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_double_fast.h +38 -0
  84. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_fast.c +675 -0
  85. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_fast.h +37 -0
  86. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.c +2104 -0
  87. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.h +125 -0
  88. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm.c +724 -0
  89. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm.h +117 -0
  90. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm_geartab.h +106 -0
  91. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_opt.c +1446 -0
  92. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_opt.h +56 -0
  93. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.c +1859 -0
  94. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.h +113 -0
  95. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress.c +1889 -0
  96. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress_amd64.S +585 -0
  97. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_ddict.c +244 -0
  98. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_ddict.h +44 -0
  99. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress.c +2230 -0
  100. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_block.c +2072 -0
  101. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_block.h +68 -0
  102. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_internal.h +236 -0
  103. data/ext/zstdlib_c/zstd-1.5.2/lib/zdict.h +452 -0
  104. data/ext/zstdlib_c/zstd-1.5.2/lib/zstd.h +2575 -0
  105. data/ext/zstdlib_c/zstd-1.5.2/lib/zstd_errors.h +95 -0
  106. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzclose.c +28 -0
  107. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzcompatibility.h +68 -0
  108. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzguts.h +229 -0
  109. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzlib.c +640 -0
  110. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzread.c +678 -0
  111. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzwrite.c +671 -0
  112. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/zstd_zlibwrapper.c +1198 -0
  113. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/zstd_zlibwrapper.h +88 -0
  114. data/ext/zstdlib_c/zstd.mk +15 -0
  115. data/lib/2.4/zstdlib_c.bundle +0 -0
  116. data/lib/2.5/zstdlib_c.bundle +0 -0
  117. data/lib/2.6/zstdlib_c.bundle +0 -0
  118. data/lib/2.7/zstdlib_c.bundle +0 -0
  119. data/lib/3.0/zstdlib_c.bundle +0 -0
  120. data/lib/3.1/zstdlib_c.bundle +0 -0
  121. data/lib/zstdlib.rb +6 -0
  122. data/test/zstdlib_test.rb +21 -0
  123. metadata +237 -0
@@ -0,0 +1,640 @@
1
+ /* gzlib.c contains minimal changes required to be compiled with zlibWrapper:
2
+ * - gz_statep was converted to union to work with -Wstrict-aliasing=1 */
3
+
4
+ /* gzlib.c -- zlib functions common to reading and writing gzip files
5
+ * Copyright (C) 2004-2017 Mark Adler
6
+ * For conditions of distribution and use, see http://www.zlib.net/zlib_license.html
7
+ */
8
+
9
+ #include "gzguts.h"
10
+
11
+ #if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__)
12
+ # define LSEEK _lseeki64
13
+ #else
14
+ #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
15
+ # define LSEEK lseek64
16
+ #else
17
+ # define LSEEK lseek
18
+ #endif
19
+ #endif
20
+
21
+ /* Local functions */
22
+ local void gz_reset OF((gz_statep));
23
+ local gzFile gz_open OF((const void *, int, const char *));
24
+
25
+ #if defined UNDER_CE
26
+
27
+ /* Map the Windows error number in ERROR to a locale-dependent error message
28
+ string and return a pointer to it. Typically, the values for ERROR come
29
+ from GetLastError.
30
+
31
+ The string pointed to shall not be modified by the application, but may be
32
+ overwritten by a subsequent call to gz_strwinerror
33
+
34
+ The gz_strwinerror function does not change the current setting of
35
+ GetLastError. */
36
+ char ZLIB_INTERNAL *gz_strwinerror (error)
37
+ DWORD error;
38
+ {
39
+ static char buf[1024];
40
+
41
+ wchar_t *msgbuf;
42
+ DWORD lasterr = GetLastError();
43
+ DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
44
+ | FORMAT_MESSAGE_ALLOCATE_BUFFER,
45
+ NULL,
46
+ error,
47
+ 0, /* Default language */
48
+ (LPVOID)&msgbuf,
49
+ 0,
50
+ NULL);
51
+ if (chars != 0) {
52
+ /* If there is an \r\n appended, zap it. */
53
+ if (chars >= 2
54
+ && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
55
+ chars -= 2;
56
+ msgbuf[chars] = 0;
57
+ }
58
+
59
+ if (chars > sizeof (buf) - 1) {
60
+ chars = sizeof (buf) - 1;
61
+ msgbuf[chars] = 0;
62
+ }
63
+
64
+ wcstombs(buf, msgbuf, chars + 1);
65
+ LocalFree(msgbuf);
66
+ }
67
+ else {
68
+ sprintf(buf, "unknown win32 error (%ld)", error);
69
+ }
70
+
71
+ SetLastError(lasterr);
72
+ return buf;
73
+ }
74
+
75
+ #endif /* UNDER_CE */
76
+
77
+ /* Reset gzip file state */
78
+ local void gz_reset(state)
79
+ gz_statep state;
80
+ {
81
+ state.state->x.have = 0; /* no output data available */
82
+ if (state.state->mode == GZ_READ) { /* for reading ... */
83
+ state.state->eof = 0; /* not at end of file */
84
+ state.state->past = 0; /* have not read past end yet */
85
+ state.state->how = LOOK; /* look for gzip header */
86
+ }
87
+ state.state->seek = 0; /* no seek request pending */
88
+ gz_error(state, Z_OK, NULL); /* clear error */
89
+ state.state->x.pos = 0; /* no uncompressed data yet */
90
+ state.state->strm.avail_in = 0; /* no input data yet */
91
+ }
92
+
93
+ /* Open a gzip file either by name or file descriptor. */
94
+ local gzFile gz_open(path, fd, mode)
95
+ const void *path;
96
+ int fd;
97
+ const char *mode;
98
+ {
99
+ gz_statep state;
100
+ z_size_t len;
101
+ int oflag;
102
+ #ifdef O_CLOEXEC
103
+ int cloexec = 0;
104
+ #endif
105
+ #ifdef O_EXCL
106
+ int exclusive = 0;
107
+ #endif
108
+
109
+ /* check input */
110
+ if (path == NULL)
111
+ return NULL;
112
+
113
+ /* allocate gzFile structure to return */
114
+ state.state = (gz_state*)malloc(sizeof(gz_state));
115
+ if (state.state == NULL)
116
+ return NULL;
117
+ state.state->size = 0; /* no buffers allocated yet */
118
+ state.state->want = GZBUFSIZE; /* requested buffer size */
119
+ state.state->msg = NULL; /* no error message yet */
120
+
121
+ /* interpret mode */
122
+ state.state->mode = GZ_NONE;
123
+ state.state->level = Z_DEFAULT_COMPRESSION;
124
+ state.state->strategy = Z_DEFAULT_STRATEGY;
125
+ state.state->direct = 0;
126
+ while (*mode) {
127
+ if (*mode >= '0' && *mode <= '9')
128
+ state.state->level = *mode - '0';
129
+ else
130
+ switch (*mode) {
131
+ case 'r':
132
+ state.state->mode = GZ_READ;
133
+ break;
134
+ #ifndef NO_GZCOMPRESS
135
+ case 'w':
136
+ state.state->mode = GZ_WRITE;
137
+ break;
138
+ case 'a':
139
+ state.state->mode = GZ_APPEND;
140
+ break;
141
+ #endif
142
+ case '+': /* can't read and write at the same time */
143
+ free(state.state);
144
+ return NULL;
145
+ case 'b': /* ignore -- will request binary anyway */
146
+ break;
147
+ #ifdef O_CLOEXEC
148
+ case 'e':
149
+ cloexec = 1;
150
+ break;
151
+ #endif
152
+ #ifdef O_EXCL
153
+ case 'x':
154
+ exclusive = 1;
155
+ break;
156
+ #endif
157
+ case 'f':
158
+ state.state->strategy = Z_FILTERED;
159
+ break;
160
+ case 'h':
161
+ state.state->strategy = Z_HUFFMAN_ONLY;
162
+ break;
163
+ case 'R':
164
+ state.state->strategy = Z_RLE;
165
+ break;
166
+ case 'F':
167
+ state.state->strategy = Z_FIXED;
168
+ break;
169
+ case 'T':
170
+ state.state->direct = 1;
171
+ break;
172
+ default: /* could consider as an error, but just ignore */
173
+ ;
174
+ }
175
+ mode++;
176
+ }
177
+
178
+ /* must provide an "r", "w", or "a" */
179
+ if (state.state->mode == GZ_NONE) {
180
+ free(state.state);
181
+ return NULL;
182
+ }
183
+
184
+ /* can't force transparent read */
185
+ if (state.state->mode == GZ_READ) {
186
+ if (state.state->direct) {
187
+ free(state.state);
188
+ return NULL;
189
+ }
190
+ state.state->direct = 1; /* for empty file */
191
+ }
192
+
193
+ /* save the path name for error messages */
194
+ #ifdef WIDECHAR
195
+ if (fd == -2) {
196
+ len = wcstombs(NULL, path, 0);
197
+ if (len == (z_size_t)-1)
198
+ len = 0;
199
+ }
200
+ else
201
+ #endif
202
+ len = strlen((const char *)path);
203
+ state.state->path = (char *)malloc(len + 1);
204
+ if (state.state->path == NULL) {
205
+ free(state.state);
206
+ return NULL;
207
+ }
208
+ #ifdef WIDECHAR
209
+ if (fd == -2)
210
+ if (len)
211
+ wcstombs(state.state->path, path, len + 1);
212
+ else
213
+ *(state.state->path) = 0;
214
+ else
215
+ #endif
216
+ #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
217
+ (void)snprintf(state.state->path, len + 1, "%s", (const char *)path);
218
+ #else
219
+ strcpy(state.state->path, (const char*)path);
220
+ #endif
221
+
222
+ /* compute the flags for open() */
223
+ oflag =
224
+ #ifdef O_LARGEFILE
225
+ O_LARGEFILE |
226
+ #endif
227
+ #ifdef O_BINARY
228
+ O_BINARY |
229
+ #endif
230
+ #ifdef O_CLOEXEC
231
+ (cloexec ? O_CLOEXEC : 0) |
232
+ #endif
233
+ (state.state->mode == GZ_READ ?
234
+ O_RDONLY :
235
+ (O_WRONLY | O_CREAT |
236
+ #ifdef O_EXCL
237
+ (exclusive ? O_EXCL : 0) |
238
+ #endif
239
+ (state.state->mode == GZ_WRITE ?
240
+ O_TRUNC :
241
+ O_APPEND)));
242
+
243
+ /* open the file with the appropriate flags (or just use fd) */
244
+ state.state->fd = fd > -1 ? fd : (
245
+ #ifdef WIDECHAR
246
+ fd == -2 ? _wopen(path, oflag, 0666) :
247
+ #endif
248
+ open((const char *)path, oflag, 0666));
249
+ if (state.state->fd == -1) {
250
+ free(state.state->path);
251
+ free(state.state);
252
+ return NULL;
253
+ }
254
+ if (state.state->mode == GZ_APPEND) {
255
+ LSEEK(state.state->fd, 0, SEEK_END); /* so gzoffset() is correct */
256
+ state.state->mode = GZ_WRITE; /* simplify later checks */
257
+ }
258
+
259
+ /* save the current position for rewinding (only if reading) */
260
+ if (state.state->mode == GZ_READ) {
261
+ state.state->start = LSEEK(state.state->fd, 0, SEEK_CUR);
262
+ if (state.state->start == -1) state.state->start = 0;
263
+ }
264
+
265
+ /* initialize stream */
266
+ gz_reset(state);
267
+
268
+ /* return stream */
269
+ return state.file;
270
+ }
271
+
272
+ /* -- see zlib.h -- */
273
+ gzFile ZEXPORT gzopen(path, mode)
274
+ const char *path;
275
+ const char *mode;
276
+ {
277
+ return gz_open(path, -1, mode);
278
+ }
279
+
280
+ /* -- see zlib.h -- */
281
+ gzFile ZEXPORT gzopen64(path, mode)
282
+ const char *path;
283
+ const char *mode;
284
+ {
285
+ return gz_open(path, -1, mode);
286
+ }
287
+
288
+ /* -- see zlib.h -- */
289
+ gzFile ZEXPORT gzdopen(fd, mode)
290
+ int fd;
291
+ const char *mode;
292
+ {
293
+ char *path; /* identifier for error messages */
294
+ gzFile gz;
295
+
296
+ if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
297
+ return NULL;
298
+ #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
299
+ (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
300
+ #else
301
+ sprintf(path, "<fd:%d>", fd); /* for debugging */
302
+ #endif
303
+ gz = gz_open(path, fd, mode);
304
+ free(path);
305
+ return gz;
306
+ }
307
+
308
+ /* -- see zlib.h -- */
309
+ #ifdef WIDECHAR
310
+ gzFile ZEXPORT gzopen_w(path, mode)
311
+ const wchar_t *path;
312
+ const char *mode;
313
+ {
314
+ return gz_open(path, -2, mode);
315
+ }
316
+ #endif
317
+
318
+ /* -- see zlib.h -- */
319
+ int ZEXPORT gzbuffer(file, size)
320
+ gzFile file;
321
+ unsigned size;
322
+ {
323
+ gz_statep state;
324
+
325
+ /* get internal structure and check integrity */
326
+ if (file == NULL)
327
+ return -1;
328
+ state.file = file;
329
+ if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
330
+ return -1;
331
+
332
+ /* make sure we haven't already allocated memory */
333
+ if (state.state->size != 0)
334
+ return -1;
335
+
336
+ /* check and set requested size */
337
+ if ((size << 1) < size)
338
+ return -1; /* need to be able to double it */
339
+ if (size < 2)
340
+ size = 2; /* need two bytes to check magic header */
341
+ state.state->want = size;
342
+ return 0;
343
+ }
344
+
345
+ /* -- see zlib.h -- */
346
+ int ZEXPORT gzrewind(file)
347
+ gzFile file;
348
+ {
349
+ gz_statep state;
350
+
351
+ /* get internal structure */
352
+ if (file == NULL)
353
+ return -1;
354
+ state.file = file;
355
+
356
+ /* check that we're reading and that there's no error */
357
+ if (state.state->mode != GZ_READ ||
358
+ (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
359
+ return -1;
360
+
361
+ /* back up and start over */
362
+ if (LSEEK(state.state->fd, state.state->start, SEEK_SET) == -1)
363
+ return -1;
364
+ gz_reset(state);
365
+ return 0;
366
+ }
367
+
368
+ /* -- see zlib.h -- */
369
+ z_off64_t ZEXPORT gzseek64(file, offset, whence)
370
+ gzFile file;
371
+ z_off64_t offset;
372
+ int whence;
373
+ {
374
+ unsigned n;
375
+ z_off64_t ret;
376
+ gz_statep state;
377
+
378
+ /* get internal structure and check integrity */
379
+ if (file == NULL)
380
+ return -1;
381
+ state.file = file;
382
+ if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
383
+ return -1;
384
+
385
+ /* check that there's no error */
386
+ if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
387
+ return -1;
388
+
389
+ /* can only seek from start or relative to current position */
390
+ if (whence != SEEK_SET && whence != SEEK_CUR)
391
+ return -1;
392
+
393
+ /* normalize offset to a SEEK_CUR specification */
394
+ if (whence == SEEK_SET)
395
+ offset -= state.state->x.pos;
396
+ else if (state.state->seek)
397
+ offset += state.state->skip;
398
+ state.state->seek = 0;
399
+
400
+ /* if within raw area while reading, just go there */
401
+ if (state.state->mode == GZ_READ && state.state->how == COPY &&
402
+ state.state->x.pos + offset >= 0) {
403
+ ret = LSEEK(state.state->fd, offset - state.state->x.have, SEEK_CUR);
404
+ if (ret == -1)
405
+ return -1;
406
+ state.state->x.have = 0;
407
+ state.state->eof = 0;
408
+ state.state->past = 0;
409
+ state.state->seek = 0;
410
+ gz_error(state, Z_OK, NULL);
411
+ state.state->strm.avail_in = 0;
412
+ state.state->x.pos += offset;
413
+ return state.state->x.pos;
414
+ }
415
+
416
+ /* calculate skip amount, rewinding if needed for back seek when reading */
417
+ if (offset < 0) {
418
+ if (state.state->mode != GZ_READ) /* writing -- can't go backwards */
419
+ return -1;
420
+ offset += state.state->x.pos;
421
+ if (offset < 0) /* before start of file! */
422
+ return -1;
423
+ if (gzrewind(file) == -1) /* rewind, then skip to offset */
424
+ return -1;
425
+ }
426
+
427
+ /* if reading, skip what's in output buffer (one less gzgetc() check) */
428
+ if (state.state->mode == GZ_READ) {
429
+ n = GT_OFF(state.state->x.have) || (z_off64_t)state.state->x.have > offset ?
430
+ (unsigned)offset : state.state->x.have;
431
+ state.state->x.have -= n;
432
+ state.state->x.next += n;
433
+ state.state->x.pos += n;
434
+ offset -= n;
435
+ }
436
+
437
+ /* request skip (if not zero) */
438
+ if (offset) {
439
+ state.state->seek = 1;
440
+ state.state->skip = offset;
441
+ }
442
+ return state.state->x.pos + offset;
443
+ }
444
+
445
+ /* -- see zlib.h -- */
446
+ z_off_t ZEXPORT gzseek(file, offset, whence)
447
+ gzFile file;
448
+ z_off_t offset;
449
+ int whence;
450
+ {
451
+ z_off64_t ret;
452
+
453
+ ret = gzseek64(file, (z_off64_t)offset, whence);
454
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
455
+ }
456
+
457
+ /* -- see zlib.h -- */
458
+ z_off64_t ZEXPORT gztell64(file)
459
+ gzFile file;
460
+ {
461
+ gz_statep state;
462
+
463
+ /* get internal structure and check integrity */
464
+ if (file == NULL)
465
+ return -1;
466
+ state.file = file;
467
+ if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
468
+ return -1;
469
+
470
+ /* return position */
471
+ return state.state->x.pos + (state.state->seek ? state.state->skip : 0);
472
+ }
473
+
474
+ /* -- see zlib.h -- */
475
+ z_off_t ZEXPORT gztell(file)
476
+ gzFile file;
477
+ {
478
+ z_off64_t ret;
479
+
480
+ ret = gztell64(file);
481
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
482
+ }
483
+
484
+ /* -- see zlib.h -- */
485
+ z_off64_t ZEXPORT gzoffset64(file)
486
+ gzFile file;
487
+ {
488
+ z_off64_t offset;
489
+ gz_statep state;
490
+
491
+ /* get internal structure and check integrity */
492
+ if (file == NULL)
493
+ return -1;
494
+ state.file = file;
495
+ if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
496
+ return -1;
497
+
498
+ /* compute and return effective offset in file */
499
+ offset = LSEEK(state.state->fd, 0, SEEK_CUR);
500
+ if (offset == -1)
501
+ return -1;
502
+ if (state.state->mode == GZ_READ) /* reading */
503
+ offset -= state.state->strm.avail_in; /* don't count buffered input */
504
+ return offset;
505
+ }
506
+
507
+ /* -- see zlib.h -- */
508
+ z_off_t ZEXPORT gzoffset(file)
509
+ gzFile file;
510
+ {
511
+ z_off64_t ret;
512
+
513
+ ret = gzoffset64(file);
514
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
515
+ }
516
+
517
+ /* -- see zlib.h -- */
518
+ int ZEXPORT gzeof(file)
519
+ gzFile file;
520
+ {
521
+ gz_statep state;
522
+
523
+ /* get internal structure and check integrity */
524
+ if (file == NULL)
525
+ return 0;
526
+ state.file = file;
527
+ if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
528
+ return 0;
529
+
530
+ /* return end-of-file state */
531
+ return state.state->mode == GZ_READ ? state.state->past : 0;
532
+ }
533
+
534
+ /* -- see zlib.h -- */
535
+ const char * ZEXPORT gzerror(file, errnum)
536
+ gzFile file;
537
+ int *errnum;
538
+ {
539
+ gz_statep state;
540
+
541
+ /* get internal structure and check integrity */
542
+ if (file == NULL)
543
+ return NULL;
544
+ state.file = file;
545
+ if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
546
+ return NULL;
547
+
548
+ /* return error information */
549
+ if (errnum != NULL)
550
+ *errnum = state.state->err;
551
+ return state.state->err == Z_MEM_ERROR ? "out of memory" :
552
+ (state.state->msg == NULL ? "" : state.state->msg);
553
+ }
554
+
555
+ /* -- see zlib.h -- */
556
+ void ZEXPORT gzclearerr(file)
557
+ gzFile file;
558
+ {
559
+ gz_statep state;
560
+
561
+ /* get internal structure and check integrity */
562
+ if (file == NULL)
563
+ return;
564
+ state.file = file;
565
+ if (state.state->mode != GZ_READ && state.state->mode != GZ_WRITE)
566
+ return;
567
+
568
+ /* clear error and end-of-file */
569
+ if (state.state->mode == GZ_READ) {
570
+ state.state->eof = 0;
571
+ state.state->past = 0;
572
+ }
573
+ gz_error(state, Z_OK, NULL);
574
+ }
575
+
576
+ /* Create an error message in allocated memory and set state.state->err and
577
+ state.state->msg accordingly. Free any previous error message already there. Do
578
+ not try to free or allocate space if the error is Z_MEM_ERROR (out of
579
+ memory). Simply save the error message as a static string. If there is an
580
+ allocation failure constructing the error message, then convert the error to
581
+ out of memory. */
582
+ void ZLIB_INTERNAL gz_error(state, err, msg)
583
+ gz_statep state;
584
+ int err;
585
+ const char *msg;
586
+ {
587
+ /* free previously allocated message and clear */
588
+ if (state.state->msg != NULL) {
589
+ if (state.state->err != Z_MEM_ERROR)
590
+ free(state.state->msg);
591
+ state.state->msg = NULL;
592
+ }
593
+
594
+ /* if fatal, set state.state->x.have to 0 so that the gzgetc() macro fails */
595
+ if (err != Z_OK && err != Z_BUF_ERROR)
596
+ state.state->x.have = 0;
597
+
598
+ /* set error code, and if no message, then done */
599
+ state.state->err = err;
600
+ if (msg == NULL)
601
+ return;
602
+
603
+ /* for an out of memory error, return literal string when requested */
604
+ if (err == Z_MEM_ERROR)
605
+ return;
606
+
607
+ /* construct error message with path */
608
+ if ((state.state->msg = (char *)malloc(strlen(state.state->path) + strlen(msg) + 3)) ==
609
+ NULL) {
610
+ state.state->err = Z_MEM_ERROR;
611
+ return;
612
+ }
613
+ #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
614
+ (void)snprintf(state.state->msg, strlen(state.state->path) + strlen(msg) + 3,
615
+ "%s%s%s", state.state->path, ": ", msg);
616
+ #else
617
+ strcpy(state.state->msg, state.state->path);
618
+ strcat(state.state->msg, ": ");
619
+ strcat(state.state->msg, msg);
620
+ #endif
621
+ }
622
+
623
+ #ifndef INT_MAX
624
+ /* portably return maximum value for an int (when limits.h presumed not
625
+ available) -- we need to do this to cover cases where 2's complement not
626
+ used, since C standard permits 1's complement and sign-bit representations,
627
+ otherwise we could just use ((unsigned)-1) >> 1 */
628
+ unsigned ZLIB_INTERNAL gz_intmax()
629
+ {
630
+ unsigned p, q;
631
+
632
+ p = 1;
633
+ do {
634
+ q = p;
635
+ p <<= 1;
636
+ p++;
637
+ } while (p > q);
638
+ return q >> 1;
639
+ }
640
+ #endif