gzlib.c 16 KB

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