annotate zlib/gzlib.c @ 144:8f4e72ab4e11

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