gzlib.c 16 KB

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