gzlib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /* gzlib.c -- zlib functions common to reading and writing gzip files
  2. * Copyright (C) 2004-2024 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "gzguts.h"
  6. #if defined(_WIN32) && !defined(__BORLANDC__)
  7. # define LSEEK _lseeki64
  8. #else
  9. #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
  10. # define LSEEK lseek64
  11. #else
  12. # define LSEEK lseek
  13. #endif
  14. #endif
  15. #if defined UNDER_CE
  16. /* Map the Windows error number in ERROR to a locale-dependent error message
  17. string and return a pointer to it. Typically, the values for ERROR come
  18. from GetLastError.
  19. The string pointed to shall not be modified by the application, but may be
  20. overwritten by a subsequent call to gz_strwinerror
  21. The gz_strwinerror function does not change the current setting of
  22. GetLastError. */
  23. char ZLIB_INTERNAL *gz_strwinerror(DWORD error) {
  24. static char buf[1024];
  25. wchar_t *msgbuf;
  26. DWORD lasterr = GetLastError();
  27. DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
  28. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  29. NULL,
  30. error,
  31. 0, /* Default language */
  32. (LPVOID)&msgbuf,
  33. 0,
  34. NULL);
  35. if (chars != 0) {
  36. /* If there is an \r\n appended, zap it. */
  37. if (chars >= 2
  38. && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
  39. chars -= 2;
  40. msgbuf[chars] = 0;
  41. }
  42. if (chars > sizeof (buf) - 1) {
  43. chars = sizeof (buf) - 1;
  44. msgbuf[chars] = 0;
  45. }
  46. wcstombs(buf, msgbuf, chars + 1);
  47. LocalFree(msgbuf);
  48. }
  49. else {
  50. sprintf(buf, "unknown win32 error (%ld)", error);
  51. }
  52. SetLastError(lasterr);
  53. return buf;
  54. }
  55. #endif /* UNDER_CE */
  56. /* Reset gzip file state */
  57. local void gz_reset(gz_statep state) {
  58. state->x.have = 0; /* no output data available */
  59. if (state->mode == GZ_READ) { /* for reading ... */
  60. state->eof = 0; /* not at end of file */
  61. state->past = 0; /* have not read past end yet */
  62. state->how = LOOK; /* look for gzip header */
  63. }
  64. else /* for writing ... */
  65. state->reset = 0; /* no deflateReset pending */
  66. state->seek = 0; /* no seek request pending */
  67. gz_error(state, Z_OK, NULL); /* clear error */
  68. state->x.pos = 0; /* no uncompressed data yet */
  69. state->strm.avail_in = 0; /* no input data yet */
  70. }
  71. /* Open a gzip file either by name or file descriptor. */
  72. local gzFile gz_open(const void *path, int fd, const char *mode) {
  73. gz_statep state;
  74. z_size_t len;
  75. int oflag;
  76. #ifdef O_CLOEXEC
  77. int cloexec = 0;
  78. #endif
  79. #ifdef O_EXCL
  80. int exclusive = 0;
  81. #endif
  82. /* check input */
  83. if (path == NULL)
  84. return NULL;
  85. /* allocate gzFile structure to return */
  86. state = (gz_statep)malloc(sizeof(gz_state));
  87. if (state == NULL)
  88. return NULL;
  89. state->size = 0; /* no buffers allocated yet */
  90. state->want = GZBUFSIZE; /* requested buffer size */
  91. state->msg = NULL; /* no error message yet */
  92. /* interpret mode */
  93. state->mode = GZ_NONE;
  94. state->level = Z_DEFAULT_COMPRESSION;
  95. state->strategy = Z_DEFAULT_STRATEGY;
  96. state->direct = 0;
  97. while (*mode) {
  98. if (*mode >= '0' && *mode <= '9')
  99. state->level = *mode - '0';
  100. else
  101. switch (*mode) {
  102. case 'r':
  103. state->mode = GZ_READ;
  104. break;
  105. #ifndef NO_GZCOMPRESS
  106. case 'w':
  107. state->mode = GZ_WRITE;
  108. break;
  109. case 'a':
  110. state->mode = GZ_APPEND;
  111. break;
  112. #endif
  113. case '+': /* can't read and write at the same time */
  114. free(state);
  115. return NULL;
  116. case 'b': /* ignore -- will request binary anyway */
  117. break;
  118. #ifdef O_CLOEXEC
  119. case 'e':
  120. cloexec = 1;
  121. break;
  122. #endif
  123. #ifdef O_EXCL
  124. case 'x':
  125. exclusive = 1;
  126. break;
  127. #endif
  128. case 'f':
  129. state->strategy = Z_FILTERED;
  130. break;
  131. case 'h':
  132. state->strategy = Z_HUFFMAN_ONLY;
  133. break;
  134. case 'R':
  135. state->strategy = Z_RLE;
  136. break;
  137. case 'F':
  138. state->strategy = Z_FIXED;
  139. break;
  140. case 'T':
  141. state->direct = 1;
  142. break;
  143. default: /* could consider as an error, but just ignore */
  144. ;
  145. }
  146. mode++;
  147. }
  148. /* must provide an "r", "w", or "a" */
  149. if (state->mode == GZ_NONE) {
  150. free(state);
  151. return NULL;
  152. }
  153. /* can't force transparent read */
  154. if (state->mode == GZ_READ) {
  155. if (state->direct) {
  156. free(state);
  157. return NULL;
  158. }
  159. state->direct = 1; /* for empty file */
  160. }
  161. /* save the path name for error messages */
  162. #ifdef WIDECHAR
  163. if (fd == -2) {
  164. len = wcstombs(NULL, path, 0);
  165. if (len == (z_size_t)-1)
  166. len = 0;
  167. }
  168. else
  169. #endif
  170. len = strlen((const char *)path);
  171. state->path = (char *)malloc(len + 1);
  172. if (state->path == NULL) {
  173. free(state);
  174. return NULL;
  175. }
  176. #ifdef WIDECHAR
  177. if (fd == -2)
  178. if (len)
  179. wcstombs(state->path, path, len + 1);
  180. else
  181. *(state->path) = 0;
  182. else
  183. #endif
  184. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  185. (void)snprintf(state->path, len + 1, "%s", (const char *)path);
  186. #else
  187. strcpy(state->path, path);
  188. #endif
  189. /* compute the flags for open() */
  190. oflag =
  191. #ifdef O_LARGEFILE
  192. O_LARGEFILE |
  193. #endif
  194. #ifdef O_BINARY
  195. O_BINARY |
  196. #endif
  197. #ifdef O_CLOEXEC
  198. (cloexec ? O_CLOEXEC : 0) |
  199. #endif
  200. (state->mode == GZ_READ ?
  201. O_RDONLY :
  202. (O_WRONLY | O_CREAT |
  203. #ifdef O_EXCL
  204. (exclusive ? O_EXCL : 0) |
  205. #endif
  206. (state->mode == GZ_WRITE ?
  207. O_TRUNC :
  208. O_APPEND)));
  209. /* open the file with the appropriate flags (or just use fd) */
  210. state->fd = fd > -1 ? fd : (
  211. #ifdef WIDECHAR
  212. fd == -2 ? _wopen(path, oflag, 0666) :
  213. #endif
  214. open((const char *)path, oflag, 0666));
  215. if (state->fd == -1) {
  216. free(state->path);
  217. free(state);
  218. return NULL;
  219. }
  220. if (state->mode == GZ_APPEND) {
  221. LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
  222. state->mode = GZ_WRITE; /* simplify later checks */
  223. }
  224. /* save the current position for rewinding (only if reading) */
  225. if (state->mode == GZ_READ) {
  226. state->start = LSEEK(state->fd, 0, SEEK_CUR);
  227. if (state->start == -1) state->start = 0;
  228. }
  229. /* initialize stream */
  230. gz_reset(state);
  231. /* return stream */
  232. return (gzFile)state;
  233. }
  234. /* -- see zlib.h -- */
  235. gzFile ZEXPORT gzopen(const char *path, const char *mode) {
  236. return gz_open(path, -1, mode);
  237. }
  238. /* -- see zlib.h -- */
  239. gzFile ZEXPORT gzopen64(const char *path, const char *mode) {
  240. return gz_open(path, -1, mode);
  241. }
  242. /* -- see zlib.h -- */
  243. gzFile ZEXPORT gzdopen(int fd, const char *mode) {
  244. char *path; /* identifier for error messages */
  245. gzFile gz;
  246. if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
  247. return NULL;
  248. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  249. (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
  250. #else
  251. sprintf(path, "<fd:%d>", fd); /* for debugging */
  252. #endif
  253. gz = gz_open(path, fd, mode);
  254. free(path);
  255. return gz;
  256. }
  257. /* -- see zlib.h -- */
  258. #ifdef WIDECHAR
  259. gzFile ZEXPORT gzopen_w(const wchar_t *path, const char *mode) {
  260. return gz_open(path, -2, mode);
  261. }
  262. #endif
  263. /* -- see zlib.h -- */
  264. int ZEXPORT gzbuffer(gzFile file, unsigned size) {
  265. gz_statep state;
  266. /* get internal structure and check integrity */
  267. if (file == NULL)
  268. return -1;
  269. state = (gz_statep)file;
  270. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  271. return -1;
  272. /* make sure we haven't already allocated memory */
  273. if (state->size != 0)
  274. return -1;
  275. /* check and set requested size */
  276. if ((size << 1) < size)
  277. return -1; /* need to be able to double it */
  278. if (size < 8)
  279. size = 8; /* needed to behave well with flushing */
  280. state->want = size;
  281. return 0;
  282. }
  283. /* -- see zlib.h -- */
  284. int ZEXPORT gzrewind(gzFile file) {
  285. gz_statep state;
  286. /* get internal structure */
  287. if (file == NULL)
  288. return -1;
  289. state = (gz_statep)file;
  290. /* check that we're reading and that there's no error */
  291. if (state->mode != GZ_READ ||
  292. (state->err != Z_OK && state->err != Z_BUF_ERROR))
  293. return -1;
  294. /* back up and start over */
  295. if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
  296. return -1;
  297. gz_reset(state);
  298. return 0;
  299. }
  300. /* -- see zlib.h -- */
  301. z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) {
  302. unsigned n;
  303. z_off64_t ret;
  304. gz_statep state;
  305. /* get internal structure and check integrity */
  306. if (file == NULL)
  307. return -1;
  308. state = (gz_statep)file;
  309. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  310. return -1;
  311. /* check that there's no error */
  312. if (state->err != Z_OK && state->err != Z_BUF_ERROR)
  313. return -1;
  314. /* can only seek from start or relative to current position */
  315. if (whence != SEEK_SET && whence != SEEK_CUR)
  316. return -1;
  317. /* normalize offset to a SEEK_CUR specification */
  318. if (whence == SEEK_SET)
  319. offset -= state->x.pos;
  320. else if (state->seek)
  321. offset += state->skip;
  322. state->seek = 0;
  323. /* if within raw area while reading, just go there */
  324. if (state->mode == GZ_READ && state->how == COPY &&
  325. state->x.pos + offset >= 0) {
  326. ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR);
  327. if (ret == -1)
  328. return -1;
  329. state->x.have = 0;
  330. state->eof = 0;
  331. state->past = 0;
  332. state->seek = 0;
  333. gz_error(state, Z_OK, NULL);
  334. state->strm.avail_in = 0;
  335. state->x.pos += offset;
  336. return state->x.pos;
  337. }
  338. /* calculate skip amount, rewinding if needed for back seek when reading */
  339. if (offset < 0) {
  340. if (state->mode != GZ_READ) /* writing -- can't go backwards */
  341. return -1;
  342. offset += state->x.pos;
  343. if (offset < 0) /* before start of file! */
  344. return -1;
  345. if (gzrewind(file) == -1) /* rewind, then skip to offset */
  346. return -1;
  347. }
  348. /* if reading, skip what's in output buffer (one less gzgetc() check) */
  349. if (state->mode == GZ_READ) {
  350. n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
  351. (unsigned)offset : state->x.have;
  352. state->x.have -= n;
  353. state->x.next += n;
  354. state->x.pos += n;
  355. offset -= n;
  356. }
  357. /* request skip (if not zero) */
  358. if (offset) {
  359. state->seek = 1;
  360. state->skip = offset;
  361. }
  362. return state->x.pos + offset;
  363. }
  364. /* -- see zlib.h -- */
  365. z_off_t ZEXPORT gzseek(gzFile file, z_off_t offset, int whence) {
  366. z_off64_t ret;
  367. ret = gzseek64(file, (z_off64_t)offset, whence);
  368. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  369. }
  370. /* -- see zlib.h -- */
  371. z_off64_t ZEXPORT gztell64(gzFile file) {
  372. gz_statep state;
  373. /* get internal structure and check integrity */
  374. if (file == NULL)
  375. return -1;
  376. state = (gz_statep)file;
  377. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  378. return -1;
  379. /* return position */
  380. return state->x.pos + (state->seek ? state->skip : 0);
  381. }
  382. /* -- see zlib.h -- */
  383. z_off_t ZEXPORT gztell(gzFile file) {
  384. z_off64_t ret;
  385. ret = gztell64(file);
  386. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  387. }
  388. /* -- see zlib.h -- */
  389. z_off64_t ZEXPORT gzoffset64(gzFile file) {
  390. z_off64_t offset;
  391. gz_statep state;
  392. /* get internal structure and check integrity */
  393. if (file == NULL)
  394. return -1;
  395. state = (gz_statep)file;
  396. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  397. return -1;
  398. /* compute and return effective offset in file */
  399. offset = LSEEK(state->fd, 0, SEEK_CUR);
  400. if (offset == -1)
  401. return -1;
  402. if (state->mode == GZ_READ) /* reading */
  403. offset -= state->strm.avail_in; /* don't count buffered input */
  404. return offset;
  405. }
  406. /* -- see zlib.h -- */
  407. z_off_t ZEXPORT gzoffset(gzFile file) {
  408. z_off64_t ret;
  409. ret = gzoffset64(file);
  410. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  411. }
  412. /* -- see zlib.h -- */
  413. int ZEXPORT gzeof(gzFile file) {
  414. gz_statep state;
  415. /* get internal structure and check integrity */
  416. if (file == NULL)
  417. return 0;
  418. state = (gz_statep)file;
  419. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  420. return 0;
  421. /* return end-of-file state */
  422. return state->mode == GZ_READ ? state->past : 0;
  423. }
  424. /* -- see zlib.h -- */
  425. const char * ZEXPORT gzerror(gzFile file, int *errnum) {
  426. gz_statep state;
  427. /* get internal structure and check integrity */
  428. if (file == NULL)
  429. return NULL;
  430. state = (gz_statep)file;
  431. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  432. return NULL;
  433. /* return error information */
  434. if (errnum != NULL)
  435. *errnum = state->err;
  436. return state->err == Z_MEM_ERROR ? "out of memory" :
  437. (state->msg == NULL ? "" : state->msg);
  438. }
  439. /* -- see zlib.h -- */
  440. void ZEXPORT gzclearerr(gzFile file) {
  441. gz_statep state;
  442. /* get internal structure and check integrity */
  443. if (file == NULL)
  444. return;
  445. state = (gz_statep)file;
  446. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  447. return;
  448. /* clear error and end-of-file */
  449. if (state->mode == GZ_READ) {
  450. state->eof = 0;
  451. state->past = 0;
  452. }
  453. gz_error(state, Z_OK, NULL);
  454. }
  455. /* Create an error message in allocated memory and set state->err and
  456. state->msg accordingly. Free any previous error message already there. Do
  457. not try to free or allocate space if the error is Z_MEM_ERROR (out of
  458. memory). Simply save the error message as a static string. If there is an
  459. allocation failure constructing the error message, then convert the error to
  460. out of memory. */
  461. void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
  462. /* free previously allocated message and clear */
  463. if (state->msg != NULL) {
  464. if (state->err != Z_MEM_ERROR)
  465. free(state->msg);
  466. state->msg = NULL;
  467. }
  468. /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
  469. if (err != Z_OK && err != Z_BUF_ERROR)
  470. state->x.have = 0;
  471. /* set error code, and if no message, then done */
  472. state->err = err;
  473. if (msg == NULL)
  474. return;
  475. /* for an out of memory error, return literal string when requested */
  476. if (err == Z_MEM_ERROR)
  477. return;
  478. /* construct error message with path */
  479. if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
  480. NULL) {
  481. state->err = Z_MEM_ERROR;
  482. return;
  483. }
  484. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  485. (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
  486. "%s%s%s", state->path, ": ", msg);
  487. #else
  488. strcpy(state->msg, state->path);
  489. strcat(state->msg, ": ");
  490. strcat(state->msg, msg);
  491. #endif
  492. }
  493. /* portably return maximum value for an int (when limits.h presumed not
  494. available) -- we need to do this to cover cases where 2's complement not
  495. used, since C standard permits 1's complement and sign-bit representations,
  496. otherwise we could just use ((unsigned)-1) >> 1 */
  497. unsigned ZLIB_INTERNAL gz_intmax(void) {
  498. #ifdef INT_MAX
  499. return INT_MAX;
  500. #else
  501. unsigned p = 1, q;
  502. do {
  503. q = p;
  504. p <<= 1;
  505. p++;
  506. } while (p > q);
  507. return q >> 1;
  508. #endif
  509. }