gzlib.c 14 KB

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