gzlib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /* gzlib.c -- zlib functions common to reading and writing gzip files
  2. * Copyright (C) 2004-2024 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "zbuild.h"
  6. #include "zutil_p.h"
  7. #include "gzguts.h"
  8. #if defined(_WIN32)
  9. # define LSEEK _lseeki64
  10. #else
  11. #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
  12. # define LSEEK lseek64
  13. #else
  14. # define LSEEK lseek
  15. #endif
  16. #endif
  17. /* Local functions */
  18. static void gz_reset(gz_state *);
  19. static gzFile gz_open(const void *, int, const char *);
  20. /* Reset gzip file state */
  21. static void gz_reset(gz_state *state) {
  22. state->x.have = 0; /* no output data available */
  23. if (state->mode == GZ_READ) { /* for reading ... */
  24. state->eof = 0; /* not at end of file */
  25. state->past = 0; /* have not read past end yet */
  26. state->how = LOOK; /* look for gzip header */
  27. }
  28. else /* for writing ... */
  29. state->reset = 0; /* no deflateReset pending */
  30. state->seek = 0; /* no seek request pending */
  31. gz_error(state, Z_OK, NULL); /* clear error */
  32. state->x.pos = 0; /* no uncompressed data yet */
  33. state->strm.avail_in = 0; /* no input data yet */
  34. }
  35. /* Open a gzip file either by name or file descriptor. */
  36. static gzFile gz_open(const void *path, int fd, const char *mode) {
  37. gz_state *state;
  38. size_t len;
  39. int oflag;
  40. #ifdef O_CLOEXEC
  41. int cloexec = 0;
  42. #endif
  43. #ifdef O_EXCL
  44. int exclusive = 0;
  45. #endif
  46. /* check input */
  47. if (path == NULL)
  48. return NULL;
  49. /* allocate gzFile structure to return */
  50. state = (gz_state *)zng_alloc(sizeof(gz_state));
  51. if (state == NULL)
  52. return NULL;
  53. state->size = 0; /* no buffers allocated yet */
  54. state->want = GZBUFSIZE; /* requested buffer size */
  55. state->msg = NULL; /* no error message yet */
  56. /* interpret mode */
  57. state->mode = GZ_NONE;
  58. state->level = Z_DEFAULT_COMPRESSION;
  59. state->strategy = Z_DEFAULT_STRATEGY;
  60. state->direct = 0;
  61. while (*mode) {
  62. if (*mode >= '0' && *mode <= '9') {
  63. state->level = *mode - '0';
  64. } else {
  65. switch (*mode) {
  66. case 'r':
  67. state->mode = GZ_READ;
  68. break;
  69. #ifndef NO_GZCOMPRESS
  70. case 'w':
  71. state->mode = GZ_WRITE;
  72. break;
  73. case 'a':
  74. state->mode = GZ_APPEND;
  75. break;
  76. #endif
  77. case '+': /* can't read and write at the same time */
  78. zng_free(state);
  79. return NULL;
  80. case 'b': /* ignore -- will request binary anyway */
  81. break;
  82. #ifdef O_CLOEXEC
  83. case 'e':
  84. cloexec = 1;
  85. break;
  86. #endif
  87. #ifdef O_EXCL
  88. case 'x':
  89. exclusive = 1;
  90. break;
  91. #endif
  92. case 'f':
  93. state->strategy = Z_FILTERED;
  94. break;
  95. case 'h':
  96. state->strategy = Z_HUFFMAN_ONLY;
  97. break;
  98. case 'R':
  99. state->strategy = Z_RLE;
  100. break;
  101. case 'F':
  102. state->strategy = Z_FIXED;
  103. break;
  104. case 'T':
  105. state->direct = 1;
  106. break;
  107. default: /* could consider as an error, but just ignore */
  108. {}
  109. }
  110. }
  111. mode++;
  112. }
  113. /* must provide an "r", "w", or "a" */
  114. if (state->mode == GZ_NONE) {
  115. zng_free(state);
  116. return NULL;
  117. }
  118. /* can't force transparent read */
  119. if (state->mode == GZ_READ) {
  120. if (state->direct) {
  121. zng_free(state);
  122. return NULL;
  123. }
  124. state->direct = 1; /* for empty file */
  125. }
  126. /* save the path name for error messages */
  127. #ifdef WIDECHAR
  128. if (fd == -2) {
  129. len = wcstombs(NULL, (const wchar_t *)path, 0);
  130. if (len == (size_t)-1)
  131. len = 0;
  132. } else
  133. #endif
  134. len = strlen((const char *)path);
  135. state->path = (char *)malloc(len + 1);
  136. if (state->path == NULL) {
  137. zng_free(state);
  138. return NULL;
  139. }
  140. #ifdef WIDECHAR
  141. if (fd == -2)
  142. if (len) {
  143. wcstombs(state->path, (const wchar_t *)path, len + 1);
  144. } else {
  145. *(state->path) = 0;
  146. }
  147. else
  148. #endif
  149. (void)snprintf(state->path, len + 1, "%s", (const char *)path);
  150. /* compute the flags for open() */
  151. oflag =
  152. #ifdef O_LARGEFILE
  153. O_LARGEFILE |
  154. #endif
  155. #ifdef O_BINARY
  156. O_BINARY |
  157. #endif
  158. #ifdef O_CLOEXEC
  159. (cloexec ? O_CLOEXEC : 0) |
  160. #endif
  161. (state->mode == GZ_READ ?
  162. O_RDONLY :
  163. (O_WRONLY | O_CREAT |
  164. #ifdef O_EXCL
  165. (exclusive ? O_EXCL : 0) |
  166. #endif
  167. (state->mode == GZ_WRITE ?
  168. O_TRUNC :
  169. O_APPEND)));
  170. /* open the file with the appropriate flags (or just use fd) */
  171. state->fd = fd > -1 ? fd : (
  172. #if defined(_WIN32)
  173. fd == -2 ? _wopen((const wchar_t *)path, oflag, 0666) :
  174. #elif __CYGWIN__
  175. fd == -2 ? open(state->path, oflag, 0666) :
  176. #endif
  177. open((const char *)path, oflag, 0666));
  178. if (state->fd == -1) {
  179. free(state->path);
  180. zng_free(state);
  181. return NULL;
  182. }
  183. if (state->mode == GZ_APPEND) {
  184. LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
  185. state->mode = GZ_WRITE; /* simplify later checks */
  186. }
  187. /* save the current position for rewinding (only if reading) */
  188. if (state->mode == GZ_READ) {
  189. state->start = LSEEK(state->fd, 0, SEEK_CUR);
  190. if (state->start == -1) state->start = 0;
  191. }
  192. /* initialize stream */
  193. gz_reset(state);
  194. /* return stream */
  195. return (gzFile)state;
  196. }
  197. /* -- see zlib.h -- */
  198. gzFile Z_EXPORT PREFIX(gzopen)(const char *path, const char *mode) {
  199. return gz_open(path, -1, mode);
  200. }
  201. #ifdef ZLIB_COMPAT
  202. gzFile Z_EXPORT PREFIX4(gzopen)(const char *path, const char *mode) {
  203. return gz_open(path, -1, mode);
  204. }
  205. #endif
  206. /* -- see zlib.h -- */
  207. gzFile Z_EXPORT PREFIX(gzdopen)(int fd, const char *mode) {
  208. char *path; /* identifier for error messages */
  209. gzFile gz;
  210. if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
  211. return NULL;
  212. (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
  213. gz = gz_open(path, fd, mode);
  214. free(path);
  215. return gz;
  216. }
  217. /* -- see zlib.h -- */
  218. #ifdef WIDECHAR
  219. gzFile Z_EXPORT PREFIX(gzopen_w)(const wchar_t *path, const char *mode) {
  220. return gz_open(path, -2, mode);
  221. }
  222. #endif
  223. int Z_EXPORT PREFIX(gzclose)(gzFile file) {
  224. #ifndef NO_GZCOMPRESS
  225. gz_state *state;
  226. if (file == NULL)
  227. return Z_STREAM_ERROR;
  228. state = (gz_state *)file;
  229. return state->mode == GZ_READ ? PREFIX(gzclose_r)(file) : PREFIX(gzclose_w)(file);
  230. #else
  231. return PREFIX(gzclose_r)(file);
  232. #endif
  233. }
  234. /* -- see zlib.h -- */
  235. int Z_EXPORT PREFIX(gzbuffer)(gzFile file, unsigned size) {
  236. gz_state *state;
  237. /* get internal structure and check integrity */
  238. if (file == NULL)
  239. return -1;
  240. state = (gz_state *)file;
  241. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  242. return -1;
  243. /* make sure we haven't already allocated memory */
  244. if (state->size != 0)
  245. return -1;
  246. /* check and set requested size */
  247. if ((size << 1) < size)
  248. return -1; /* need to be able to double it */
  249. if (size < 8)
  250. size = 8; /* needed to behave well with flushing */
  251. state->want = size;
  252. return 0;
  253. }
  254. /* -- see zlib.h -- */
  255. int Z_EXPORT PREFIX(gzrewind)(gzFile file) {
  256. gz_state *state;
  257. /* get internal structure */
  258. if (file == NULL)
  259. return -1;
  260. state = (gz_state *)file;
  261. /* check that we're reading and that there's no error */
  262. if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
  263. return -1;
  264. /* back up and start over */
  265. if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
  266. return -1;
  267. gz_reset(state);
  268. return 0;
  269. }
  270. /* -- see zlib.h -- */
  271. z_off64_t Z_EXPORT PREFIX4(gzseek)(gzFile file, z_off64_t offset, int whence) {
  272. unsigned n;
  273. z_off64_t ret;
  274. gz_state *state;
  275. /* get internal structure and check integrity */
  276. if (file == NULL)
  277. return -1;
  278. state = (gz_state *)file;
  279. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  280. return -1;
  281. /* check that there's no error */
  282. if (state->err != Z_OK && state->err != Z_BUF_ERROR)
  283. return -1;
  284. /* can only seek from start or relative to current position */
  285. if (whence != SEEK_SET && whence != SEEK_CUR)
  286. return -1;
  287. /* normalize offset to a SEEK_CUR specification */
  288. if (whence == SEEK_SET)
  289. offset -= state->x.pos;
  290. else if (state->seek)
  291. offset += state->skip;
  292. state->seek = 0;
  293. /* if within raw area while reading, just go there */
  294. if (state->mode == GZ_READ && state->how == COPY && state->x.pos + offset >= 0) {
  295. ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR);
  296. if (ret == -1)
  297. return -1;
  298. state->x.have = 0;
  299. state->eof = 0;
  300. state->past = 0;
  301. state->seek = 0;
  302. gz_error(state, Z_OK, NULL);
  303. state->strm.avail_in = 0;
  304. state->x.pos += offset;
  305. return state->x.pos;
  306. }
  307. /* calculate skip amount, rewinding if needed for back seek when reading */
  308. if (offset < 0) {
  309. if (state->mode != GZ_READ) /* writing -- can't go backwards */
  310. return -1;
  311. offset += state->x.pos;
  312. if (offset < 0) /* before start of file! */
  313. return -1;
  314. if (PREFIX(gzrewind)(file) == -1) /* rewind, then skip to offset */
  315. return -1;
  316. }
  317. /* if reading, skip what's in output buffer (one less gzgetc() check) */
  318. if (state->mode == GZ_READ) {
  319. n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? (unsigned)offset : state->x.have;
  320. state->x.have -= n;
  321. state->x.next += n;
  322. state->x.pos += n;
  323. offset -= n;
  324. }
  325. /* request skip (if not zero) */
  326. if (offset) {
  327. state->seek = 1;
  328. state->skip = offset;
  329. }
  330. return state->x.pos + offset;
  331. }
  332. /* -- see zlib.h -- */
  333. #ifdef ZLIB_COMPAT
  334. z_off_t Z_EXPORT PREFIX(gzseek)(gzFile file, z_off_t offset, int whence) {
  335. z_off64_t ret;
  336. ret = PREFIX4(gzseek)(file, (z_off64_t)offset, whence);
  337. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  338. }
  339. #endif
  340. /* -- see zlib.h -- */
  341. z_off64_t Z_EXPORT PREFIX4(gztell)(gzFile file) {
  342. gz_state *state;
  343. /* get internal structure and check integrity */
  344. if (file == NULL)
  345. return -1;
  346. state = (gz_state *)file;
  347. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  348. return -1;
  349. /* return position */
  350. return state->x.pos + (state->seek ? state->skip : 0);
  351. }
  352. /* -- see zlib.h -- */
  353. #ifdef ZLIB_COMPAT
  354. z_off_t Z_EXPORT PREFIX(gztell)(gzFile file) {
  355. z_off64_t ret;
  356. ret = PREFIX4(gztell)(file);
  357. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  358. }
  359. #endif
  360. /* -- see zlib.h -- */
  361. z_off64_t Z_EXPORT PREFIX4(gzoffset)(gzFile file) {
  362. z_off64_t offset;
  363. gz_state *state;
  364. /* get internal structure and check integrity */
  365. if (file == NULL)
  366. return -1;
  367. state = (gz_state *)file;
  368. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  369. return -1;
  370. /* compute and return effective offset in file */
  371. offset = LSEEK(state->fd, 0, SEEK_CUR);
  372. if (offset == -1)
  373. return -1;
  374. if (state->mode == GZ_READ) /* reading */
  375. offset -= state->strm.avail_in; /* don't count buffered input */
  376. return offset;
  377. }
  378. /* -- see zlib.h -- */
  379. #ifdef ZLIB_COMPAT
  380. z_off_t Z_EXPORT PREFIX(gzoffset)(gzFile file) {
  381. z_off64_t ret;
  382. ret = PREFIX4(gzoffset)(file);
  383. return ret == (z_off_t)ret ? (z_off_t)ret : -1;
  384. }
  385. #endif
  386. /* -- see zlib.h -- */
  387. int Z_EXPORT PREFIX(gzeof)(gzFile file) {
  388. gz_state *state;
  389. /* get internal structure and check integrity */
  390. if (file == NULL)
  391. return 0;
  392. state = (gz_state *)file;
  393. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  394. return 0;
  395. /* return end-of-file state */
  396. return state->mode == GZ_READ ? state->past : 0;
  397. }
  398. /* -- see zlib.h -- */
  399. const char * Z_EXPORT PREFIX(gzerror)(gzFile file, int *errnum) {
  400. gz_state *state;
  401. /* get internal structure and check integrity */
  402. if (file == NULL)
  403. return NULL;
  404. state = (gz_state *)file;
  405. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  406. return NULL;
  407. /* return error information */
  408. if (errnum != NULL)
  409. *errnum = state->err;
  410. return state->err == Z_MEM_ERROR ? "out of memory" : (state->msg == NULL ? "" : state->msg);
  411. }
  412. /* -- see zlib.h -- */
  413. void Z_EXPORT PREFIX(gzclearerr)(gzFile file) {
  414. gz_state *state;
  415. /* get internal structure and check integrity */
  416. if (file == NULL)
  417. return;
  418. state = (gz_state *)file;
  419. if (state->mode != GZ_READ && state->mode != GZ_WRITE)
  420. return;
  421. /* clear error and end-of-file */
  422. if (state->mode == GZ_READ) {
  423. state->eof = 0;
  424. state->past = 0;
  425. }
  426. gz_error(state, Z_OK, NULL);
  427. }
  428. /* Create an error message in allocated memory and set state->err and
  429. state->msg accordingly. Free any previous error message already there. Do
  430. not try to free or allocate space if the error is Z_MEM_ERROR (out of
  431. memory). Simply save the error message as a static string. If there is an
  432. allocation failure constructing the error message, then convert the error to
  433. out of memory. */
  434. void Z_INTERNAL gz_error(gz_state *state, int err, const char *msg) {
  435. /* free previously allocated message and clear */
  436. if (state->msg != NULL) {
  437. if (state->err != Z_MEM_ERROR)
  438. free(state->msg);
  439. state->msg = NULL;
  440. }
  441. /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
  442. if (err != Z_OK && err != Z_BUF_ERROR)
  443. state->x.have = 0;
  444. /* set error code, and if no message, then done */
  445. state->err = err;
  446. if (msg == NULL)
  447. return;
  448. /* for an out of memory error, return literal string when requested */
  449. if (err == Z_MEM_ERROR)
  450. return;
  451. /* construct error message with path */
  452. if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
  453. state->err = Z_MEM_ERROR;
  454. return;
  455. }
  456. (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, "%s%s%s", state->path, ": ", msg);
  457. }
  458. #ifdef ZLIB_COMPAT
  459. unsigned Z_INTERNAL gz_intmax(void) {
  460. return INT_MAX;
  461. }
  462. #endif