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

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

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