Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: third_party/zlib/gzlib.c

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

Powered by Google App Engine
This is Rietveld 408576698