gzlib.c 16 KB

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