2
0

gzlib.c 17 KB

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