Deleted Added
full compact
gzlib.c (206905) gzlib.c (230837)
1/* gzlib.c -- zlib functions common to reading and writing gzip files
1/* gzlib.c -- zlib functions common to reading and writing gzip files
2 * Copyright (C) 2004, 2010 Mark Adler
2 * Copyright (C) 2004, 2010, 2011 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "gzguts.h"
7
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "gzguts.h"
7
8#if defined(_WIN32) && !defined(__BORLANDC__)
9# define LSEEK _lseeki64
10#else
8#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
9# define LSEEK lseek64
10#else
11# define LSEEK lseek
12#endif
11#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
12# define LSEEK lseek64
13#else
14# define LSEEK lseek
15#endif
16#endif
13
14/* Local functions */
15local void gz_reset OF((gz_statep));
16local gzFile gz_open OF((const char *, int, const char *));
17
18#if defined UNDER_CE
19
20/* Map the Windows error number in ERROR to a locale-dependent error message

--- 45 unchanged lines hidden (view full) ---

66}
67
68#endif /* UNDER_CE */
69
70/* Reset gzip file state */
71local void gz_reset(state)
72 gz_statep state;
73{
17
18/* Local functions */
19local void gz_reset OF((gz_statep));
20local gzFile gz_open OF((const char *, int, const char *));
21
22#if defined UNDER_CE
23
24/* Map the Windows error number in ERROR to a locale-dependent error message

--- 45 unchanged lines hidden (view full) ---

70}
71
72#endif /* UNDER_CE */
73
74/* Reset gzip file state */
75local void gz_reset(state)
76 gz_statep state;
77{
78 state->x.have = 0; /* no output data available */
74 if (state->mode == GZ_READ) { /* for reading ... */
79 if (state->mode == GZ_READ) { /* for reading ... */
75 state->have = 0; /* no output data available */
76 state->eof = 0; /* not at end of file */
80 state->eof = 0; /* not at end of file */
81 state->past = 0; /* have not read past end yet */
77 state->how = LOOK; /* look for gzip header */
82 state->how = LOOK; /* look for gzip header */
78 state->direct = 1; /* default for empty file */
79 }
80 state->seek = 0; /* no seek request pending */
81 gz_error(state, Z_OK, NULL); /* clear error */
83 }
84 state->seek = 0; /* no seek request pending */
85 gz_error(state, Z_OK, NULL); /* clear error */
82 state->pos = 0; /* no uncompressed data yet */
86 state->x.pos = 0; /* no uncompressed data yet */
83 state->strm.avail_in = 0; /* no input data yet */
84}
85
86/* Open a gzip file either by name or file descriptor. */
87local gzFile gz_open(path, fd, mode)
88 const char *path;
89 int fd;
90 const char *mode;
91{
92 gz_statep state;
93
87 state->strm.avail_in = 0; /* no input data yet */
88}
89
90/* Open a gzip file either by name or file descriptor. */
91local gzFile gz_open(path, fd, mode)
92 const char *path;
93 int fd;
94 const char *mode;
95{
96 gz_statep state;
97
98 /* check input */
99 if (path == NULL)
100 return NULL;
101
94 /* allocate gzFile structure to return */
95 state = malloc(sizeof(gz_state));
96 if (state == NULL)
97 return NULL;
98 state->size = 0; /* no buffers allocated yet */
99 state->want = GZBUFSIZE; /* requested buffer size */
100 state->msg = NULL; /* no error message yet */
101
102 /* interpret mode */
103 state->mode = GZ_NONE;
104 state->level = Z_DEFAULT_COMPRESSION;
105 state->strategy = Z_DEFAULT_STRATEGY;
102 /* allocate gzFile structure to return */
103 state = malloc(sizeof(gz_state));
104 if (state == NULL)
105 return NULL;
106 state->size = 0; /* no buffers allocated yet */
107 state->want = GZBUFSIZE; /* requested buffer size */
108 state->msg = NULL; /* no error message yet */
109
110 /* interpret mode */
111 state->mode = GZ_NONE;
112 state->level = Z_DEFAULT_COMPRESSION;
113 state->strategy = Z_DEFAULT_STRATEGY;
114 state->direct = 0;
106 while (*mode) {
107 if (*mode >= '0' && *mode <= '9')
108 state->level = *mode - '0';
109 else
110 switch (*mode) {
111 case 'r':
112 state->mode = GZ_READ;
113 break;

--- 16 unchanged lines hidden (view full) ---

130 case 'h':
131 state->strategy = Z_HUFFMAN_ONLY;
132 break;
133 case 'R':
134 state->strategy = Z_RLE;
135 break;
136 case 'F':
137 state->strategy = Z_FIXED;
115 while (*mode) {
116 if (*mode >= '0' && *mode <= '9')
117 state->level = *mode - '0';
118 else
119 switch (*mode) {
120 case 'r':
121 state->mode = GZ_READ;
122 break;

--- 16 unchanged lines hidden (view full) ---

139 case 'h':
140 state->strategy = Z_HUFFMAN_ONLY;
141 break;
142 case 'R':
143 state->strategy = Z_RLE;
144 break;
145 case 'F':
146 state->strategy = Z_FIXED;
147 case 'T':
148 state->direct = 1;
138 default: /* could consider as an error, but just ignore */
139 ;
140 }
141 mode++;
142 }
143
144 /* must provide an "r", "w", or "a" */
145 if (state->mode == GZ_NONE) {
146 free(state);
147 return NULL;
148 }
149
149 default: /* could consider as an error, but just ignore */
150 ;
151 }
152 mode++;
153 }
154
155 /* must provide an "r", "w", or "a" */
156 if (state->mode == GZ_NONE) {
157 free(state);
158 return NULL;
159 }
160
161 /* can't force transparent read */
162 if (state->mode == GZ_READ) {
163 if (state->direct) {
164 free(state);
165 return NULL;
166 }
167 state->direct = 1; /* for empty file */
168 }
169
150 /* save the path name for error messages */
151 state->path = malloc(strlen(path) + 1);
152 if (state->path == NULL) {
153 free(state);
154 return NULL;
155 }
156 strcpy(state->path, path);
157

--- 80 unchanged lines hidden (view full) ---

238 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
239 return -1;
240
241 /* make sure we haven't already allocated memory */
242 if (state->size != 0)
243 return -1;
244
245 /* check and set requested size */
170 /* save the path name for error messages */
171 state->path = malloc(strlen(path) + 1);
172 if (state->path == NULL) {
173 free(state);
174 return NULL;
175 }
176 strcpy(state->path, path);
177

--- 80 unchanged lines hidden (view full) ---

258 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
259 return -1;
260
261 /* make sure we haven't already allocated memory */
262 if (state->size != 0)
263 return -1;
264
265 /* check and set requested size */
246 if (size == 0)
247 return -1;
266 if (size < 2)
267 size = 2; /* need two bytes to check magic header */
248 state->want = size;
249 return 0;
250}
251
252/* -- see zlib.h -- */
253int ZEXPORT gzrewind(file)
254 gzFile file;
255{
256 gz_statep state;
257
258 /* get internal structure */
259 if (file == NULL)
260 return -1;
261 state = (gz_statep)file;
262
263 /* check that we're reading and that there's no error */
268 state->want = size;
269 return 0;
270}
271
272/* -- see zlib.h -- */
273int ZEXPORT gzrewind(file)
274 gzFile file;
275{
276 gz_statep state;
277
278 /* get internal structure */
279 if (file == NULL)
280 return -1;
281 state = (gz_statep)file;
282
283 /* check that we're reading and that there's no error */
264 if (state->mode != GZ_READ || state->err != Z_OK)
284 if (state->mode != GZ_READ ||
285 (state->err != Z_OK && state->err != Z_BUF_ERROR))
265 return -1;
266
267 /* back up and start over */
268 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
269 return -1;
270 gz_reset(state);
271 return 0;
272}

--- 11 unchanged lines hidden (view full) ---

284 /* get internal structure and check integrity */
285 if (file == NULL)
286 return -1;
287 state = (gz_statep)file;
288 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
289 return -1;
290
291 /* check that there's no error */
286 return -1;
287
288 /* back up and start over */
289 if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
290 return -1;
291 gz_reset(state);
292 return 0;
293}

--- 11 unchanged lines hidden (view full) ---

305 /* get internal structure and check integrity */
306 if (file == NULL)
307 return -1;
308 state = (gz_statep)file;
309 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
310 return -1;
311
312 /* check that there's no error */
292 if (state->err != Z_OK)
313 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
293 return -1;
294
295 /* can only seek from start or relative to current position */
296 if (whence != SEEK_SET && whence != SEEK_CUR)
297 return -1;
298
299 /* normalize offset to a SEEK_CUR specification */
300 if (whence == SEEK_SET)
314 return -1;
315
316 /* can only seek from start or relative to current position */
317 if (whence != SEEK_SET && whence != SEEK_CUR)
318 return -1;
319
320 /* normalize offset to a SEEK_CUR specification */
321 if (whence == SEEK_SET)
301 offset -= state->pos;
322 offset -= state->x.pos;
302 else if (state->seek)
303 offset += state->skip;
304 state->seek = 0;
305
306 /* if within raw area while reading, just go there */
307 if (state->mode == GZ_READ && state->how == COPY &&
323 else if (state->seek)
324 offset += state->skip;
325 state->seek = 0;
326
327 /* if within raw area while reading, just go there */
328 if (state->mode == GZ_READ && state->how == COPY &&
308 state->pos + offset >= state->raw) {
309 ret = LSEEK(state->fd, offset - state->have, SEEK_CUR);
329 state->x.pos + offset >= 0) {
330 ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
310 if (ret == -1)
311 return -1;
331 if (ret == -1)
332 return -1;
312 state->have = 0;
333 state->x.have = 0;
313 state->eof = 0;
334 state->eof = 0;
335 state->past = 0;
314 state->seek = 0;
315 gz_error(state, Z_OK, NULL);
316 state->strm.avail_in = 0;
336 state->seek = 0;
337 gz_error(state, Z_OK, NULL);
338 state->strm.avail_in = 0;
317 state->pos += offset;
318 return state->pos;
339 state->x.pos += offset;
340 return state->x.pos;
319 }
320
321 /* calculate skip amount, rewinding if needed for back seek when reading */
322 if (offset < 0) {
323 if (state->mode != GZ_READ) /* writing -- can't go backwards */
324 return -1;
341 }
342
343 /* calculate skip amount, rewinding if needed for back seek when reading */
344 if (offset < 0) {
345 if (state->mode != GZ_READ) /* writing -- can't go backwards */
346 return -1;
325 offset += state->pos;
347 offset += state->x.pos;
326 if (offset < 0) /* before start of file! */
327 return -1;
328 if (gzrewind(file) == -1) /* rewind, then skip to offset */
329 return -1;
330 }
331
332 /* if reading, skip what's in output buffer (one less gzgetc() check) */
333 if (state->mode == GZ_READ) {
348 if (offset < 0) /* before start of file! */
349 return -1;
350 if (gzrewind(file) == -1) /* rewind, then skip to offset */
351 return -1;
352 }
353
354 /* if reading, skip what's in output buffer (one less gzgetc() check) */
355 if (state->mode == GZ_READ) {
334 n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
335 (unsigned)offset : state->have;
336 state->have -= n;
337 state->next += n;
338 state->pos += n;
356 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
357 (unsigned)offset : state->x.have;
358 state->x.have -= n;
359 state->x.next += n;
360 state->x.pos += n;
339 offset -= n;
340 }
341
342 /* request skip (if not zero) */
343 if (offset) {
344 state->seek = 1;
345 state->skip = offset;
346 }
361 offset -= n;
362 }
363
364 /* request skip (if not zero) */
365 if (offset) {
366 state->seek = 1;
367 state->skip = offset;
368 }
347 return state->pos + offset;
369 return state->x.pos + offset;
348}
349
350/* -- see zlib.h -- */
351z_off_t ZEXPORT gzseek(file, offset, whence)
352 gzFile file;
353 z_off_t offset;
354 int whence;
355{

--- 12 unchanged lines hidden (view full) ---

368 /* get internal structure and check integrity */
369 if (file == NULL)
370 return -1;
371 state = (gz_statep)file;
372 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
373 return -1;
374
375 /* return position */
370}
371
372/* -- see zlib.h -- */
373z_off_t ZEXPORT gzseek(file, offset, whence)
374 gzFile file;
375 z_off_t offset;
376 int whence;
377{

--- 12 unchanged lines hidden (view full) ---

390 /* get internal structure and check integrity */
391 if (file == NULL)
392 return -1;
393 state = (gz_statep)file;
394 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
395 return -1;
396
397 /* return position */
376 return state->pos + (state->seek ? state->skip : 0);
398 return state->x.pos + (state->seek ? state->skip : 0);
377}
378
379/* -- see zlib.h -- */
380z_off_t ZEXPORT gztell(file)
381 gzFile file;
382{
383 z_off64_t ret;
384

--- 43 unchanged lines hidden (view full) ---

428 /* get internal structure and check integrity */
429 if (file == NULL)
430 return 0;
431 state = (gz_statep)file;
432 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
433 return 0;
434
435 /* return end-of-file state */
399}
400
401/* -- see zlib.h -- */
402z_off_t ZEXPORT gztell(file)
403 gzFile file;
404{
405 z_off64_t ret;
406

--- 43 unchanged lines hidden (view full) ---

450 /* get internal structure and check integrity */
451 if (file == NULL)
452 return 0;
453 state = (gz_statep)file;
454 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
455 return 0;
456
457 /* return end-of-file state */
436 return state->mode == GZ_READ ?
437 (state->eof && state->strm.avail_in == 0 && state->have == 0) : 0;
458 return state->mode == GZ_READ ? state->past : 0;
438}
439
440/* -- see zlib.h -- */
441const char * ZEXPORT gzerror(file, errnum)
442 gzFile file;
443 int *errnum;
444{
445 gz_statep state;

--- 20 unchanged lines hidden (view full) ---

466 /* get internal structure and check integrity */
467 if (file == NULL)
468 return;
469 state = (gz_statep)file;
470 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
471 return;
472
473 /* clear error and end-of-file */
459}
460
461/* -- see zlib.h -- */
462const char * ZEXPORT gzerror(file, errnum)
463 gzFile file;
464 int *errnum;
465{
466 gz_statep state;

--- 20 unchanged lines hidden (view full) ---

487 /* get internal structure and check integrity */
488 if (file == NULL)
489 return;
490 state = (gz_statep)file;
491 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
492 return;
493
494 /* clear error and end-of-file */
474 if (state->mode == GZ_READ)
495 if (state->mode == GZ_READ) {
475 state->eof = 0;
496 state->eof = 0;
497 state->past = 0;
498 }
476 gz_error(state, Z_OK, NULL);
477}
478
479/* Create an error message in allocated memory and set state->err and
480 state->msg accordingly. Free any previous error message already there. Do
481 not try to free or allocate space if the error is Z_MEM_ERROR (out of
482 memory). Simply save the error message as a static string. If there is an
483 allocation failure constructing the error message, then convert the error to

--- 5 unchanged lines hidden (view full) ---

489{
490 /* free previously allocated message and clear */
491 if (state->msg != NULL) {
492 if (state->err != Z_MEM_ERROR)
493 free(state->msg);
494 state->msg = NULL;
495 }
496
499 gz_error(state, Z_OK, NULL);
500}
501
502/* Create an error message in allocated memory and set state->err and
503 state->msg accordingly. Free any previous error message already there. Do
504 not try to free or allocate space if the error is Z_MEM_ERROR (out of
505 memory). Simply save the error message as a static string. If there is an
506 allocation failure constructing the error message, then convert the error to

--- 5 unchanged lines hidden (view full) ---

512{
513 /* free previously allocated message and clear */
514 if (state->msg != NULL) {
515 if (state->err != Z_MEM_ERROR)
516 free(state->msg);
517 state->msg = NULL;
518 }
519
520 /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
521 if (err != Z_OK && err != Z_BUF_ERROR)
522 state->x.have = 0;
523
497 /* set error code, and if no message, then done */
498 state->err = err;
499 if (msg == NULL)
500 return;
501
502 /* for an out of memory error, save as static string */
503 if (err == Z_MEM_ERROR) {
504 state->msg = (char *)msg;

--- 33 unchanged lines hidden ---
524 /* set error code, and if no message, then done */
525 state->err = err;
526 if (msg == NULL)
527 return;
528
529 /* for an out of memory error, save as static string */
530 if (err == Z_MEM_ERROR) {
531 state->msg = (char *)msg;

--- 33 unchanged lines hidden ---