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