PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/zlib/zlib/utility.c

https://github.com/XULRunner42/ferite-modules
C | 427 lines | 309 code | 67 blank | 51 comment | 95 complexity | ea85f5d86a1f8b4ae63e636a210a7d9b MD5 | raw file
  1. #include "zlib_header.h"
  2. #include "utility.h"
  3. #include <zlib.h>
  4. #include <stdlib.h>
  5. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  6. # include <fcntl.h>
  7. # include <io.h>
  8. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  9. #else
  10. # define SET_BINARY_MODE(file)
  11. #endif
  12. #define CHUNK 16384
  13. int
  14. ferite_zlib_priv_compres(int level,
  15. char *inData, long inLength,
  16. char **outData, long *outLength)
  17. {
  18. int retVal;
  19. *outLength = inLength + inLength / 1024 + 16;
  20. *outData = malloc(*outLength);
  21. if (*outData == 0) {
  22. /* out of memory */
  23. retVal = Z_MEM_ERROR;
  24. } else {
  25. retVal = compress2((unsigned char *)(*outData), (unsigned long *)outLength,
  26. (unsigned char *)inData, (unsigned long)inLength,
  27. (level >= 0 && level <= 9)
  28. ? (unsigned long)level : Z_DEFAULT_COMPRESSION);
  29. }
  30. /* if we have an error, free resources */
  31. if (retVal != Z_OK) {
  32. free(*outData);
  33. }
  34. return retVal;
  35. }
  36. int
  37. ferite_zlib_priv_deflate(int level,
  38. char *inData, long inLength,
  39. char **outData, long *outLength)
  40. {
  41. int retVal;
  42. z_stream z;
  43. /* allocate deflate state */
  44. z.data_type = Z_ASCII;
  45. z.zalloc = Z_NULL;
  46. z.zfree = Z_NULL;
  47. z.opaque = Z_NULL;
  48. /* point to the input string */
  49. z.next_in = (unsigned char *)inData;
  50. z.avail_in = inLength;
  51. /* get space for the output string */
  52. z.avail_out = z.avail_in + (z.avail_in / 1024) + 16; /* guarentee memory */
  53. *outData = malloc(z.avail_out);
  54. if (*outData == 0) {
  55. /* out of memory */
  56. return Z_MEM_ERROR;
  57. }
  58. z.next_out = (unsigned char *)(*outData);
  59. /* php/ext/zlib/zlib.c says that the following will compress without
  60. * the zlib internal headers and that is good enough for me
  61. */
  62. retVal = deflateInit2(&z,
  63. (level >= 0 && level <= 9) ? level : Z_DEFAULT_COMPRESSION,
  64. Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, 0);
  65. if (retVal == Z_OK) {
  66. /* deflate succeeded, so clean up */
  67. retVal = deflate(&z, Z_FINISH);
  68. if (retVal == Z_STREAM_END) {
  69. retVal = deflateEnd(&z);
  70. } else {
  71. deflateEnd(&z);
  72. if (retVal == Z_OK) {
  73. retVal = Z_BUF_ERROR;
  74. }
  75. }
  76. }
  77. *outLength = z.total_out;
  78. /* if we have an error, free resources */
  79. if (retVal != Z_OK) {
  80. free(*outData);
  81. }
  82. return retVal;
  83. }
  84. int
  85. ferite_zlib_priv_inflate(char *inData, long inLength, char **outData, long *outLength)
  86. {
  87. int multiple;
  88. int retVal;
  89. z_stream z;
  90. *outData = 0;
  91. /* from PHP, use multiple to expand our buffer until it is large enough*/
  92. for (multiple = 2; multiple < 16; ++multiple) {
  93. *outLength = inLength * (1 << multiple);
  94. /* get a better, bigger, buffer */
  95. char *newBuffer = realloc(*outData, *outLength);
  96. if (newBuffer == 0) {
  97. /* out of memory */
  98. retVal = Z_MEM_ERROR;
  99. break;
  100. }
  101. *outData = newBuffer;
  102. z.zalloc = Z_NULL;
  103. z.zfree = Z_NULL;
  104. z.next_in = (unsigned char *)inData;
  105. z.avail_in = inLength;
  106. z.next_out = (unsigned char *)(*outData);
  107. z.avail_out = *outLength;
  108. retVal = inflateInit2(&z, -MAX_WBITS);
  109. if (retVal == Z_OK) {
  110. retVal = inflate(&z, Z_FINISH);
  111. if (retVal == Z_STREAM_END) {
  112. retVal = inflateEnd(&z);
  113. if (retVal != Z_BUF_ERROR) {
  114. break;
  115. }
  116. } else {
  117. inflateEnd(&z);
  118. if (retVal == Z_OK) {
  119. retVal = Z_BUF_ERROR;
  120. }
  121. }
  122. }
  123. }
  124. *outLength = z.total_out;
  125. /* if we have an error, free resources */
  126. if (retVal != Z_OK && *outData) {
  127. free(*outData);
  128. }
  129. return retVal;
  130. }
  131. int
  132. ferite_zlib_priv_uncompr(char *inData, long inLength, char **outData, long *outLength)
  133. {
  134. int multiple;
  135. int retVal;
  136. *outData = 0;
  137. /* from PHP, use multiple to expand our buffer until it is large enough*/
  138. for (multiple = 2; multiple < 16; ++multiple) {
  139. *outLength = inLength * (1 << multiple);
  140. /* get a better, bigger, buffer */
  141. char *newBuffer = realloc(*outData, *outLength);
  142. if (newBuffer == 0) {
  143. /* out of memory */
  144. retVal = Z_MEM_ERROR;
  145. break;
  146. }
  147. *outData = newBuffer;
  148. retVal = uncompress((unsigned char *)(*outData),
  149. (unsigned long)outLength,
  150. (unsigned char *)inData,
  151. (unsigned long)inLength);
  152. if (retVal != Z_BUF_ERROR) {
  153. break;
  154. }
  155. }
  156. /* if we have an error, free resources */
  157. if (retVal != Z_OK && *outData) {
  158. free(*outData);
  159. }
  160. return retVal;
  161. }
  162. #if 0
  163. /* Compress from file source to file dest until EOF on source.
  164. * def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  165. * allocated for processing, Z_STREAM_ERROR if an invalid compression
  166. * level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  167. * version of the library linked do not match, or Z_ERRNO if there is
  168. * an error reading or writing the files.
  169. */
  170. int
  171. def(FILE *source, FILE *dest, int level)
  172. {
  173. int ret, flush;
  174. unsigned have;
  175. z_stream strm;
  176. unsigned char in[CHUNK];
  177. unsigned char out[CHUNK];
  178. /* allocate deflate state */
  179. strm.zalloc = Z_NULL;
  180. strm.zfree = Z_NULL;
  181. strm.opaque = Z_NULL;
  182. ret = deflateInit(&strm, level);
  183. if (ret != Z_OK) {
  184. return ret;
  185. }
  186. /* compress until end of file */
  187. do {
  188. strm.avail_in = fread(in, 1, CHUNK, source);
  189. if (ferror(source)) {
  190. (void)deflateEnd(&strm);
  191. return Z_ERRNO;
  192. }
  193. flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
  194. strm.next_in = in;
  195. /* run deflate() on input until output buffer not full, finish
  196. * compression if all of source has been read in
  197. */
  198. do {
  199. strm.avail_out = CHUNK;
  200. strm.next_out = out;
  201. ret = deflate(&strm, flush); /* no bad return value */
  202. #if 0
  203. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  204. #else
  205. if (ret == Z_STREAM_ERROR) {
  206. fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
  207. }
  208. #endif
  209. have = CHUNK - strm.avail_out;
  210. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  211. (void)deflateEnd(&strm);
  212. return Z_ERRNO;
  213. }
  214. } while (strm.avail_out == 0);
  215. #if 0
  216. assert(strm.avail_in == 0); /* all input will be used */
  217. #else
  218. if (strm.avail_in != 0) {
  219. fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
  220. }
  221. #endif
  222. /* done when last data in file processed */
  223. } while (flush != Z_FINISH);
  224. #if 0
  225. assert(ret == Z_STREAM_END); /* stream will be complete */
  226. #else
  227. if (ret != Z_STREAM_END) {
  228. fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
  229. }
  230. #endif
  231. /* clean up and return */
  232. (void)deflateEnd(&strm);
  233. return Z_OK;
  234. }
  235. /* Decompress from file source to file dest until stream ends or EOF.
  236. * inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  237. * allocated for processing, Z_DATA_ERROR if the deflate data is
  238. * invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
  239. * the version of the library linked do not match, or Z_ERRNO if there
  240. * is an error reading or writing the files.
  241. */
  242. int
  243. inf(FILE *source, FILE *dest)
  244. {
  245. int ret;
  246. unsigned have;
  247. z_stream strm;
  248. unsigned char in[CHUNK];
  249. unsigned char out[CHUNK];
  250. /* allocate inflate state */
  251. strm.zalloc = Z_NULL;
  252. strm.zfree = Z_NULL;
  253. strm.opaque = Z_NULL;
  254. strm.avail_in = 0;
  255. strm.next_in = Z_NULL;
  256. ret = inflateInit(&strm);
  257. if (ret != Z_OK) {
  258. return ret;
  259. }
  260. /* decompress until deflate stream ends or end of file */
  261. do {
  262. strm.avail_in = fread(in, 1, CHUNK, source);
  263. if (ferror(source)) {
  264. (void)inflateEnd(&strm);
  265. return Z_ERRNO;
  266. }
  267. if (strm.avail_in == 0) {
  268. break;
  269. }
  270. strm.next_in = in;
  271. /* run inflate() on input until output buffer not full */
  272. do {
  273. strm.avail_out = CHUNK;
  274. strm.next_out = out;
  275. ret = inflate(&strm, Z_NO_FLUSH);
  276. #if 0
  277. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  278. #else
  279. if (ret != Z_STREAM_ERROR) {
  280. fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
  281. }
  282. #endif
  283. switch (ret) {
  284. case Z_NEED_DICT:
  285. ret = Z_DATA_ERROR; /* and fall through */
  286. case Z_DATA_ERROR:
  287. case Z_MEM_ERROR:
  288. (void)inflateEnd(&strm);
  289. return ret;
  290. }
  291. have = CHUNK - strm.avail_out;
  292. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  293. (void)inflateEnd(&strm);
  294. return Z_ERRNO;
  295. }
  296. } while (strm.avail_out == 0);
  297. /* done when inflate() says it's done */
  298. } while (ret != Z_STREAM_END);
  299. /* clean up and return */
  300. (void)inflateEnd(&strm);
  301. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  302. }
  303. /* report a zlib or i/o error */
  304. void
  305. zerr(int ret)
  306. {
  307. fputs("zpipe: ", stderr);
  308. switch (ret) {
  309. case Z_ERRNO:
  310. if (ferror(stdin)) {
  311. fputs("error reading stdin\n", stderr);
  312. }
  313. if (ferror(stdout)) {
  314. fputs("error writing stdout\n", stderr);
  315. }
  316. break;
  317. case Z_STREAM_ERROR:
  318. fputs("invalid compression level\n", stderr);
  319. break;
  320. case Z_DATA_ERROR:
  321. fputs("invalid or incomplete deflate data\n", stderr);
  322. break;
  323. case Z_MEM_ERROR:
  324. fputs("out of memory\n", stderr);
  325. break;
  326. case Z_VERSION_ERROR:
  327. fputs("zlib version mismatch!\n", stderr);
  328. }
  329. }
  330. /* compress or decompress from stdin to stdout */
  331. int
  332. testMain(int argc, char **argv)
  333. {
  334. int ret;
  335. /* avoid end-of-line conversions */
  336. SET_BINARY_MODE(stdin);
  337. SET_BINARY_MODE(stdout);
  338. /* do compression if no arguments */
  339. if (argc == 1) {
  340. ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
  341. if (ret != Z_OK) {
  342. zerr(ret);
  343. }
  344. return ret;
  345. }
  346. /* do decompression if -d specified */
  347. else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
  348. ret = inf(stdin, stdout);
  349. if (ret != Z_OK) {
  350. zerr(ret);
  351. }
  352. return ret;
  353. }
  354. /* otherwise, report usage */
  355. else {
  356. fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
  357. return 1;
  358. }
  359. }
  360. #endif