Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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