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