/src/FreeImage/Source/ZLib/gzlib.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 574 lines · 418 code · 72 blank · 84 comment · 153 complexity · 7237497e306b58e8cfce97525dee7f98 MD5 · raw file

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