gzlib.c 15 KB

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