gzlib.c 15 KB

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