1 | |
2 | |
3 | |
4 | |
5 | |
6 | #include "gzguts.h" |
7 | |
8 | #ifdef _LARGEFILE64_SOURCE |
9 | # define LSEEKlseek lseek64 |
10 | #else |
11 | # define LSEEKlseek lseek |
12 | #endif |
13 | |
14 | |
15 | localstatic void gz_reset OF((gz_statep))(gz_statep); |
16 | localstatic gzFilez_gzFile gz_open OF((const char *, int, const char *))(const char *, int, const char *); |
17 | |
18 | #if defined UNDER_CE && defined NO_ERRNO_H |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | char ZEXPORT *gz_strwinerrorz_gz_strwinerror (error) |
30 | DWORD error; |
31 | { |
32 | static char buf[1024]; |
33 | |
34 | wchar_t *msgbuf; |
35 | DWORD lasterr = GetLastError(); |
36 | DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
37 | | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
38 | NULL((void*)0), |
39 | error, |
40 | 0, |
41 | (LPVOID)&msgbuf, |
42 | 0, |
43 | NULL((void*)0)); |
44 | if (chars != 0) { |
45 | |
46 | if (chars >= 2 |
47 | && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { |
48 | chars -= 2; |
49 | msgbuf[chars] = 0; |
50 | } |
51 | |
52 | if (chars > sizeof (buf) - 1) { |
53 | chars = sizeof (buf) - 1; |
54 | msgbuf[chars] = 0; |
55 | } |
56 | |
57 | wcstombs(buf, msgbuf, chars + 1); |
58 | LocalFree(msgbuf); |
59 | } |
60 | else { |
61 | sprintf(buf, "unknown win32 error (%ld)", error); |
62 | } |
63 | |
64 | SetLastError(lasterr); |
65 | return buf; |
66 | } |
67 | |
68 | #endif /* UNDER_CE && NO_ERRNO_H */ |
69 | |
70 | |
71 | localstatic void gz_reset(state) |
72 | gz_statep state; |
73 | { |
74 | if (state->mode == GZ_READ7247) { |
75 | state->have = 0; |
76 | state->eof = 0; |
77 | state->how = LOOK0; |
78 | state->direct = 1; |
79 | } |
80 | state->seek = 0; |
81 | gz_errorz_gz_error(state, Z_OK0, NULL((void*)0)); |
82 | state->pos = 0; |
83 | state->strm.avail_in = 0; |
84 | } |
85 | |
86 | |
87 | localstatic gzFilez_gzFile gz_open(path, fd, mode) |
88 | const char *path; |
89 | int fd; |
90 | const char *mode; |
91 | { |
92 | gz_statep state; |
93 | |
94 | |
95 | state = malloc(sizeof(gz_state)); |
96 | if (state == NULL((void*)0)) |
| |
97 | return NULL((void*)0); |
98 | state->size = 0; |
99 | state->want = GZBUFSIZE8192; |
100 | state->msg = NULL((void*)0); |
101 | |
102 | |
103 | state->mode = GZ_NONE0; |
104 | state->level = Z_DEFAULT_COMPRESSION(-1); |
105 | state->strategy = Z_DEFAULT_STRATEGY0; |
106 | while (*mode) { |
| 2 | Loop condition is true. Entering loop body |
|
| 6 | Loop condition is false. Execution continues on line 145 |
|
107 | if (*mode >= '0' && *mode <= '9') |
| |
108 | state->level = *mode - '0'; |
109 | else |
110 | switch (*mode) { |
| 4 | Control jumps to 'case 114:' at line 111 |
|
111 | case 'r': |
112 | state->mode = GZ_READ7247; |
113 | break; |
| 5 | Execution continues on line 141 |
|
114 | #ifndef NO_GZCOMPRESS |
115 | case 'w': |
116 | state->mode = GZ_WRITE31153; |
117 | break; |
118 | case 'a': |
119 | state->mode = GZ_APPEND1; |
120 | break; |
121 | #endif |
122 | case '+': |
123 | free(state); |
124 | return NULL((void*)0); |
125 | case 'b': |
126 | break; |
127 | case 'f': |
128 | state->strategy = Z_FILTERED1; |
129 | break; |
130 | case 'h': |
131 | state->strategy = Z_HUFFMAN_ONLY2; |
132 | break; |
133 | case 'R': |
134 | state->strategy = Z_RLE3; |
135 | break; |
136 | case 'F': |
137 | state->strategy = Z_FIXED4; |
138 | default: |
139 | ; |
140 | } |
141 | mode++; |
142 | } |
143 | |
144 | |
145 | if (state->mode == GZ_NONE0) { |
| |
146 | free(state); |
147 | return NULL((void*)0); |
148 | } |
149 | |
150 | |
151 | state->path = malloc(strlen(path) + 1); |
152 | if (state->path == NULL((void*)0)) { |
| |
153 | free(state); |
154 | return NULL((void*)0); |
155 | } |
156 | strcpy(state->path, path); |
157 | |
158 | |
159 | state->fd = fd != -1 ? fd : |
| |
160 | open(path, |
161 | #ifdef O_LARGEFILE |
162 | O_LARGEFILE | |
163 | #endif |
164 | #ifdef O_BINARY |
165 | O_BINARY | |
166 | #endif |
167 | (state->mode == GZ_READ7247 ? |
| |
168 | O_RDONLY00 : |
169 | (O_WRONLY01 | O_CREAT0100 | ( |
170 | state->mode == GZ_WRITE31153 ? |
171 | O_TRUNC01000 : |
172 | O_APPEND02000))), |
173 | 0666); |
174 | if (state->fd == -1) { |
| |
175 | free(state); |
176 | return NULL((void*)0); |
| 12 | Allocated memory never released. Potential memory leak |
|
177 | } |
178 | if (state->mode == GZ_APPEND1) |
179 | state->mode = GZ_WRITE31153; |
180 | |
181 | |
182 | if (state->mode == GZ_READ7247) { |
183 | state->start = LSEEKlseek(state->fd, 0, SEEK_CUR1); |
184 | if (state->start == -1) state->start = 0; |
185 | } |
186 | |
187 | |
188 | gz_reset(state); |
189 | |
190 | |
191 | return (gzFilez_gzFile)state; |
192 | } |
193 | |
194 | |
195 | gzFilez_gzFile ZEXPORT gzopenz_gzopen(path, mode) |
196 | const char *path; |
197 | const char *mode; |
198 | { |
199 | return gz_open(path, -1, mode); |
200 | } |
201 | |
202 | #if (defined(_LARGEFILE64_SOURCE)||(_FILE_OFFSET_BITS == 64)) |
203 | |
204 | gzFilez_gzFile ZEXPORT gzopen64z_gzopen64(path, mode) |
205 | const char *path; |
206 | const char *mode; |
207 | { |
208 | return gz_open(path, -1, mode); |
209 | } |
210 | #endif /* (defined(_LARGEFILE64_SOURCE)||(_FILE_OFFSET_BITS == 64)) */ |
211 | |
212 | |
213 | gzFilez_gzFile ZEXPORT gzdopenz_gzdopen(fd, mode) |
214 | int fd; |
215 | const char *mode; |
216 | { |
217 | char *path; |
218 | gzFilez_gzFile gz; |
219 | |
220 | if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL((void*)0)) |
221 | return NULL((void*)0); |
222 | sprintf(path, "<fd:%d>", fd); |
223 | gz = gz_open(path, fd, mode); |
224 | free(path); |
225 | return gz; |
226 | } |
227 | |
228 | |
229 | int ZEXPORT gzbufferz_gzbuffer(file, size) |
230 | gzFilez_gzFile file; |
231 | unsigned size; |
232 | { |
233 | gz_statep state; |
234 | |
235 | |
236 | if (file == NULL((void*)0)) |
237 | return -1; |
238 | state = (gz_statep)file; |
239 | if (state->mode != GZ_READ7247 && state->mode != GZ_WRITE31153) |
240 | return -1; |
241 | |
242 | |
243 | if (state->size != 0) |
244 | return -1; |
245 | |
246 | |
247 | if (size == 0) |
248 | return -1; |
249 | state->want = size; |
250 | return 0; |
251 | } |
252 | |
253 | |
254 | int ZEXPORT gzrewindz_gzrewind(file) |
255 | gzFilez_gzFile file; |
256 | { |
257 | gz_statep state; |
258 | |
259 | |
260 | if (file == NULL((void*)0)) |
261 | return -1; |
262 | state = (gz_statep)file; |
263 | |
264 | |
265 | if (state->mode != GZ_READ7247 || state->err != Z_OK0) |
266 | return -1; |
267 | |
268 | |
269 | if (LSEEKlseek(state->fd, state->start, SEEK_SET0) == -1) |
270 | return -1; |
271 | gz_reset(state); |
272 | return 0; |
273 | } |
274 | |
275 | |
276 | z_off64_toff_t ZEXPORT gzseek64z_gzseek64(file, offset, whence) |
277 | gzFilez_gzFile file; |
278 | z_off64_toff_t offset; |
279 | int whence; |
280 | { |
281 | unsigned n; |
282 | z_off64_toff_t ret; |
283 | gz_statep state; |
284 | |
285 | |
286 | if (file == NULL((void*)0)) |
287 | return -1; |
288 | state = (gz_statep)file; |
289 | if (state->mode != GZ_READ7247 && state->mode != GZ_WRITE31153) |
290 | return -1; |
291 | |
292 | |
293 | if (state->err != Z_OK0) |
294 | return -1; |
295 | |
296 | |
297 | if (whence != SEEK_SET0 && whence != SEEK_CUR1) |
298 | return -1; |
299 | |
300 | |
301 | if (whence == SEEK_SET0) |
302 | offset -= state->pos; |
303 | else if (state->seek) |
304 | offset += state->skip; |
305 | state->seek = 0; |
306 | |
307 | |
308 | if (state->mode == GZ_READ7247 && state->how == COPY1 && |
309 | state->pos + offset >= state->raw) { |
310 | ret = LSEEKlseek(state->fd, offset, SEEK_CUR1); |
311 | if (ret == -1) |
312 | return -1; |
313 | state->have = 0; |
314 | state->eof = 0; |
315 | state->seek = 0; |
316 | gz_errorz_gz_error(state, Z_OK0, NULL((void*)0)); |
317 | state->strm.avail_in = 0; |
318 | state->pos += offset; |
319 | return state->pos; |
320 | } |
321 | |
322 | |
323 | if (offset < 0) { |
324 | if (state->mode != GZ_READ7247) |
325 | return -1; |
326 | offset += state->pos; |
327 | if (offset < 0) |
328 | return -1; |
329 | if (gzrewindz_gzrewind(file) == -1) |
330 | return -1; |
331 | } |
332 | |
333 | |
334 | if (state->mode == GZ_READ7247) { |
335 | n = GT_OFF(state->have)(sizeof(int) == sizeof(off_t) && (state->have) > 2147483647) || (z_off64_toff_t)state->have > offset ? |
336 | (unsigned)offset : state->have; |
337 | state->have -= n; |
338 | state->next += n; |
339 | state->pos += n; |
340 | offset -= n; |
341 | } |
342 | |
343 | |
344 | if (offset) { |
345 | state->seek = 1; |
346 | state->skip = offset; |
347 | } |
348 | return state->pos + offset; |
349 | } |
350 | |
351 | |
352 | z_off_toff_t ZEXPORT gzseekz_gzseek(file, offset, whence) |
353 | gzFilez_gzFile file; |
354 | z_off_toff_t offset; |
355 | int whence; |
356 | { |
357 | z_off64_toff_t ret; |
358 | |
359 | ret = gzseek64z_gzseek64(file, (z_off64_toff_t)offset, whence); |
360 | return ret == (z_off_toff_t)ret ? (z_off_toff_t)ret : -1; |
361 | } |
362 | |
363 | |
364 | z_off64_toff_t ZEXPORT gztell64z_gztell64(file) |
365 | gzFilez_gzFile file; |
366 | { |
367 | gz_statep state; |
368 | |
369 | |
370 | if (file == NULL((void*)0)) |
371 | return -1; |
372 | state = (gz_statep)file; |
373 | if (state->mode != GZ_READ7247 && state->mode != GZ_WRITE31153) |
374 | return -1; |
375 | |
376 | |
377 | return state->pos + (state->seek ? state->skip : 0); |
378 | } |
379 | |
380 | |
381 | z_off_toff_t ZEXPORT gztellz_gztell(file) |
382 | gzFilez_gzFile file; |
383 | { |
384 | z_off64_toff_t ret; |
385 | |
386 | ret = gztell64z_gztell64(file); |
387 | return ret == (z_off_toff_t)ret ? (z_off_toff_t)ret : -1; |
388 | } |
389 | |
390 | |
391 | |
392 | z_off64_toff_t ZEXPORT gzoffset64z_gzoffset64(file) |
393 | gzFilez_gzFile file; |
394 | { |
395 | z_off64_toff_t offset; |
396 | gz_statep state; |
397 | |
398 | |
399 | if (file == NULL((void*)0)) |
400 | return -1; |
401 | state = (gz_statep)file; |
402 | if (state->mode != GZ_READ7247 && state->mode != GZ_WRITE31153) |
403 | return -1; |
404 | |
405 | |
406 | offset = LSEEKlseek(state->fd, 0, SEEK_CUR1); |
407 | if (offset == -1) |
408 | return -1; |
409 | if (state->mode == GZ_READ7247) |
410 | offset -= state->strm.avail_in; |
411 | return offset; |
412 | } |
413 | |
414 | |
415 | z_off_toff_t ZEXPORT gzoffsetz_gzoffset(file) |
416 | gzFilez_gzFile file; |
417 | { |
418 | z_off64_toff_t ret; |
419 | |
420 | ret = gzoffset64z_gzoffset64(file); |
421 | return ret == (z_off_toff_t)ret ? (z_off_toff_t)ret : -1; |
422 | } |
423 | |
424 | |
425 | int ZEXPORT gzeofz_gzeof(file) |
426 | gzFilez_gzFile file; |
427 | { |
428 | gz_statep state; |
429 | |
430 | |
431 | if (file == NULL((void*)0)) |
432 | return 0; |
433 | state = (gz_statep)file; |
434 | if (state->mode != GZ_READ7247 && state->mode != GZ_WRITE31153) |
435 | return 0; |
436 | |
437 | |
438 | return state->mode == GZ_READ7247 ? (state->eof && state->have == 0) : 0; |
439 | } |
440 | |
441 | |
442 | const char * ZEXPORT gzerrorz_gzerror(file, errnum) |
443 | gzFilez_gzFile file; |
444 | int *errnum; |
445 | { |
446 | gz_statep state; |
447 | |
448 | |
449 | if (file == NULL((void*)0)) |
450 | return NULL((void*)0); |
451 | state = (gz_statep)file; |
452 | if (state->mode != GZ_READ7247 && state->mode != GZ_WRITE31153) |
453 | return NULL((void*)0); |
454 | |
455 | |
456 | if (errnum != NULL((void*)0)) |
457 | *errnum = state->err; |
458 | return state->msg == NULL((void*)0) ? "" : state->msg; |
459 | } |
460 | |
461 | |
462 | void ZEXPORT gzclearerrz_gzclearerr(file) |
463 | gzFilez_gzFile file; |
464 | { |
465 | gz_statep state; |
466 | |
467 | |
468 | if (file == NULL((void*)0)) |
469 | return; |
470 | state = (gz_statep)file; |
471 | if (state->mode != GZ_READ7247 && state->mode != GZ_WRITE31153) |
472 | return; |
473 | |
474 | |
475 | if (state->mode == GZ_READ7247) |
476 | state->eof = 0; |
477 | gz_errorz_gz_error(state, Z_OK0, NULL((void*)0)); |
478 | } |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | void ZEXPORT gz_errorz_gz_error(state, err, msg) |
487 | gz_statep state; |
488 | int err; |
489 | const char *msg; |
490 | { |
491 | |
492 | if (state->msg != NULL((void*)0)) { |
493 | if (state->err != Z_MEM_ERROR(-4)) |
494 | free(state->msg); |
495 | state->msg = NULL((void*)0); |
496 | } |
497 | |
498 | |
499 | state->err = err; |
500 | if (msg == NULL((void*)0)) |
501 | return; |
502 | |
503 | |
504 | if (err == Z_MEM_ERROR(-4)) { |
505 | state->msg = (char *)msg; |
506 | return; |
507 | } |
508 | |
509 | |
510 | if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL((void*)0)) { |
511 | state->err = Z_MEM_ERROR(-4); |
512 | state->msg = (char *)"out of memory"; |
513 | return; |
514 | } |
515 | strcpy(state->msg, state->path); |
516 | strcat(state->msg, ": "); |
517 | strcat(state->msg, msg); |
518 | return; |
519 | } |
520 | |
521 | #ifndef INT_MAX2147483647 |
522 | |
523 | |
524 | |
525 | |
526 | unsigned ZEXPORT gz_intmaxz_gz_intmax() |
527 | { |
528 | unsigned p, q; |
529 | |
530 | p = 1; |
531 | do { |
532 | q = p; |
533 | p <<= 1; |
534 | p++; |
535 | } while (p > q); |
536 | return q >> 1; |
537 | } |
538 | #endif |