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