gzlib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* gzlib.c -- zlib functions common to reading and writing gzip files
  2. * Copyright (C) 2004, 2010, 2011, 2012, 2013 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. 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(path, fd, mode)
  78. const void *path;
  79. int fd;
  80. const char *mode;
  81. {
  82. gz_statep state;
  83. 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 _WIN32
  172. if (fd == -2) {
  173. len = wcstombs(NULL, path, 0);
  174. if (len == (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 _WIN32
  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. 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 _WIN32
  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. state->mode = GZ_WRITE; /* simplify later checks */
  231. /* save the current position for rewinding (only if reading) */
  232. if (state->mode == GZ_READ) {
  233. state->start = LSEEK(state->fd, 0, SEEK_CUR);
  234. if (state->start == -1) state->start = 0;
  235. }
  236. /* initialize stream */
  237. gz_reset(state);
  238. /* return stream */
  239. return (gzFile)state;
  240. }
  241. /* -- see zlib.h -- */
  242. gzFile ZEXPORT gzopen(path, mode)
  243. const char *path;
  244. const char *mode;
  245. {
  246. return gz_open(path, -1, mode);
  247. }
  248. /* -- see zlib.h -- */
  249. gzFile ZEXPORT gzopen64(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 gzdopen(fd, mode)
  257. int fd;
  258. const char *mode;
  259. {
  260. char *path; /* identifier for error messages */
  261. gzFile gz;
  262. if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
  263. return NULL;
  264. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  265. snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
  266. #else
  267. sprintf(path, "<fd:%d>", fd); /* for debugging */
  268. #endif
  269. gz = gz_open(path, fd, mode);
  270. free(path);
  271. return gz;
  272. }
  273. /* -- see zlib.h -- */
  274. #ifdef _WIN32
  275. gzFile ZEXPORT gzopen_w(path, mode)
  276. const wchar_t *path;
  277. const char *mode;
  278. {
  279. return gz_open(path, -2, mode);
  280. }
  281. #endif
  282. /* -- see zlib.h -- */
  283. int ZEXPORT gzbuffer(file, size)
  284. gzFile file;
  285. unsigned size;
  286. {
  287. gz_statep state;
  288. /* get internal structure and check integrity */
  289. if (file == NULL)
  290. return -1;
  291. state = (gz_statep)file;
  292. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  293. return -1;
  294. /* make sure we haven't already allocated memory */
  295. if (state->size != 0)
  296. return -1;
  297. /* check and set requested size */
  298. if (size < 2)
  299. size = 2; /* need two bytes to check magic header */
  300. state->want = size;
  301. return 0;
  302. }
  303. /* -- see zlib.h -- */
  304. int ZEXPORT gzrewind(file)
  305. gzFile file;
  306. {
  307. gz_statep state;
  308. /* get internal structure */
  309. if (file == NULL)
  310. return -1;
  311. state = (gz_statep)file;
  312. /* check that we're reading and that there's no error */
  313. if (state->mode != GZ_READ ||
  314. (state->err != Z_OK && state->err != Z_BUF_ERROR))
  315. return -1;
  316. /* back up and start over */
  317. if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
  318. return -1;
  319. gz_reset(state);
  320. return 0;
  321. }
  322. /* -- see zlib.h -- */
  323. z_off64_t ZEXPORT gzseek64(file, offset, whence)
  324. gzFile file;
  325. z_off64_t offset;
  326. int whence;
  327. {
  328. unsigned n;
  329. z_off64_t ret;
  330. gz_statep state;
  331. /* get internal structure and check integrity */
  332. if (file == NULL)
  333. return -1;
  334. state = (gz_statep)file;
  335. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  336. return -1;
  337. /* check that there's no error */
  338. if (state->err != Z_OK && state->err != Z_BUF_ERROR)
  339. return -1;
  340. /* can only seek from start or relative to current position */
  341. if (whence != SEEK_SET && whence != SEEK_CUR)
  342. return -1;
  343. /* normalize offset to a SEEK_CUR specification */
  344. if (whence == SEEK_SET)
  345. offset -= state->x.pos;
  346. else if (state->seek)
  347. offset += state->skip;
  348. state->seek = 0;
  349. /* if within raw area while reading, just go there */
  350. if (state->mode == GZ_READ && state->how == COPY &&
  351. state->x.pos + offset >= 0) {
  352. ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
  353. if (ret == -1)
  354. return -1;
  355. state->x.have = 0;
  356. state->eof = 0;
  357. state->past = 0;
  358. state->seek = 0;
  359. gz_error(state, Z_OK, NULL);
  360. state->strm.avail_in = 0;
  361. state->x.pos += offset;
  362. return state->x.pos;
  363. }
  364. /* calculate skip amount, rewinding if needed for back seek when reading */
  365. if (offset < 0) {
  366. if (state->mode != GZ_READ) /* writing -- can't go backwards */
  367. return -1;
  368. offset += state->x.pos;
  369. if (offset < 0) /* before start of file! */
  370. return -1;
  371. if (gzrewind(file) == -1) /* rewind, then skip to offset */
  372. return -1;
  373. }
  374. /* if reading, skip what's in output buffer (one less gzgetc() check) */
  375. if (state->mode == GZ_READ) {
  376. n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
  377. (unsigned)offset : state->x.have;
  378. state->x.have -= n;
  379. state->x.next += n;
  380. state->x.pos += n;
  381. offset -= n;
  382. }
  383. /* request skip (if not zero) */
  384. if (offset) {
  385. state->seek = 1;
  386. state->skip = offset;
  387. }
  388. return state->x.pos + offset;
  389. }
  390. /* -- see zlib.h -- */
  391. z_off_t ZEXPORT gzseek(file, offset, whence)
  392. gzFile file;
  393. z_off_t offset;
  394. int whence;
  395. {
  396. z_off64_t ret;
  397. ret = gzseek64(file, (z_off64_t)offset, whence);
  398. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  399. }
  400. /* -- see zlib.h -- */
  401. z_off64_t ZEXPORT gztell64(file)
  402. gzFile file;
  403. {
  404. gz_statep state;
  405. /* get internal structure and check integrity */
  406. if (file == NULL)
  407. return -1;
  408. state = (gz_statep)file;
  409. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  410. return -1;
  411. /* return position */
  412. return state->x.pos + (state->seek ? state->skip : 0);
  413. }
  414. /* -- see zlib.h -- */
  415. z_off_t ZEXPORT gztell(file)
  416. gzFile file;
  417. {
  418. z_off64_t ret;
  419. ret = gztell64(file);
  420. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  421. }
  422. /* -- see zlib.h -- */
  423. z_off64_t ZEXPORT gzoffset64(file)
  424. gzFile file;
  425. {
  426. z_off64_t offset;
  427. gz_statep state;
  428. /* get internal structure and check integrity */
  429. if (file == NULL)
  430. return -1;
  431. state = (gz_statep)file;
  432. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  433. return -1;
  434. /* compute and return effective offset in file */
  435. offset = LSEEK(state->fd, 0, SEEK_CUR);
  436. if (offset == -1)
  437. return -1;
  438. if (state->mode == GZ_READ) /* reading */
  439. offset -= state->strm.avail_in; /* don't count buffered input */
  440. return offset;
  441. }
  442. /* -- see zlib.h -- */
  443. z_off_t ZEXPORT gzoffset(file)
  444. gzFile file;
  445. {
  446. z_off64_t ret;
  447. ret = gzoffset64(file);
  448. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  449. }
  450. /* -- see zlib.h -- */
  451. int ZEXPORT gzeof(file)
  452. gzFile file;
  453. {
  454. gz_statep state;
  455. /* get internal structure and check integrity */
  456. if (file == NULL)
  457. return 0;
  458. state = (gz_statep)file;
  459. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  460. return 0;
  461. /* return end-of-file state */
  462. return state->mode == GZ_READ ? state->past : 0;
  463. }
  464. /* -- see zlib.h -- */
  465. const char * ZEXPORT gzerror(file, errnum)
  466. gzFile file;
  467. int *errnum;
  468. {
  469. gz_statep state;
  470. /* get internal structure and check integrity */
  471. if (file == NULL)
  472. return NULL;
  473. state = (gz_statep)file;
  474. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  475. return NULL;
  476. /* return error information */
  477. if (errnum != NULL)
  478. *errnum = state->err;
  479. return state->err == Z_MEM_ERROR ? "out of memory" :
  480. (state->msg == NULL ? "" : state->msg);
  481. }
  482. /* -- see zlib.h -- */
  483. void ZEXPORT gzclearerr(file)
  484. gzFile file;
  485. {
  486. gz_statep state;
  487. /* get internal structure and check integrity */
  488. if (file == NULL)
  489. return;
  490. state = (gz_statep)file;
  491. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  492. return;
  493. /* clear error and end-of-file */
  494. if (state->mode == GZ_READ) {
  495. state->eof = 0;
  496. state->past = 0;
  497. }
  498. gz_error(state, Z_OK, NULL);
  499. }
  500. /* Create an error message in allocated memory and set state->err and
  501. state->msg accordingly. Free any previous error message already there. Do
  502. not try to free or allocate space if the error is Z_MEM_ERROR (out of
  503. memory). Simply save the error message as a static string. If there is an
  504. allocation failure constructing the error message, then convert the error to
  505. out of memory. */
  506. void ZLIB_INTERNAL gz_error(state, err, msg)
  507. gz_statep state;
  508. int err;
  509. const char *msg;
  510. {
  511. /* free previously allocated message and clear */
  512. if (state->msg != NULL) {
  513. if (state->err != Z_MEM_ERROR)
  514. free(state->msg);
  515. state->msg = NULL;
  516. }
  517. /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
  518. if (err != Z_OK && err != Z_BUF_ERROR)
  519. state->x.have = 0;
  520. /* set error code, and if no message, then done */
  521. state->err = err;
  522. if (msg == NULL)
  523. return;
  524. /* for an out of memory error, return literal string when requested */
  525. if (err == Z_MEM_ERROR)
  526. return;
  527. /* construct error message with path */
  528. if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
  529. NULL) {
  530. state->err = Z_MEM_ERROR;
  531. return;
  532. }
  533. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  534. snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
  535. "%s%s%s", state->path, ": ", msg);
  536. #else
  537. strcpy(state->msg, state->path);
  538. strcat(state->msg, ": ");
  539. strcat(state->msg, msg);
  540. #endif
  541. return;
  542. }
  543. #ifndef INT_MAX
  544. /* portably return maximum value for an int (when limits.h presumed not
  545. available) -- we need to do this to cover cases where 2's complement not
  546. used, since C standard permits 1's complement and sign-bit representations,
  547. otherwise we could just use ((unsigned)-1) >> 1 */
  548. unsigned ZLIB_INTERNAL gz_intmax()
  549. {
  550. unsigned p, q;
  551. p = 1;
  552. do {
  553. q = p;
  554. p <<= 1;
  555. p++;
  556. } while (p > q);
  557. return q >> 1;
  558. }
  559. #endif