gzlib.c 16 KB

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