zip_source_deflate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. zip_source_deflate.c -- deflate (de)compressoin routines
  3. Copyright (C) 2009-2015 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <limits.h>
  33. #include "zipint.h"
  34. struct deflate {
  35. zip_error_t error;
  36. bool eof;
  37. bool can_store;
  38. bool is_stored;
  39. int mem_level;
  40. zip_uint64_t size;
  41. zip_uint8_t buffer[BUFSIZE];
  42. z_stream zstr;
  43. };
  44. static zip_int64_t compress_read(zip_source_t *, struct deflate *, void *, zip_uint64_t);
  45. static zip_int64_t decompress_read(zip_source_t *, struct deflate *, void *, zip_uint64_t);
  46. static zip_int64_t deflate_compress(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
  47. static zip_int64_t deflate_decompress(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
  48. static void deflate_free(struct deflate *);
  49. zip_source_t *
  50. zip_source_deflate(zip_t *za, zip_source_t *src, zip_int32_t cm, int flags)
  51. {
  52. struct deflate *ctx;
  53. zip_source_t *s2;
  54. if (src == NULL || (cm != ZIP_CM_DEFLATE && !ZIP_CM_IS_DEFAULT(cm))) {
  55. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  56. return NULL;
  57. }
  58. if ((ctx=(struct deflate *)malloc(sizeof(*ctx))) == NULL) {
  59. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  60. return NULL;
  61. }
  62. zip_error_init(&ctx->error);
  63. ctx->eof = false;
  64. ctx->is_stored = false;
  65. ctx->can_store = ZIP_CM_IS_DEFAULT(cm);
  66. if (flags & ZIP_CODEC_ENCODE) {
  67. ctx->mem_level = MAX_MEM_LEVEL;
  68. }
  69. if ((s2=zip_source_layered(za, src,
  70. ((flags & ZIP_CODEC_ENCODE)
  71. ? deflate_compress : deflate_decompress),
  72. ctx)) == NULL) {
  73. deflate_free(ctx);
  74. return NULL;
  75. }
  76. return s2;
  77. }
  78. static zip_int64_t
  79. compress_read(zip_source_t *src, struct deflate *ctx, void *data, zip_uint64_t len)
  80. {
  81. int end, ret;
  82. zip_int64_t n;
  83. zip_uint64_t out_offset;
  84. uInt out_len;
  85. if (zip_error_code_zip(&ctx->error) != ZIP_ER_OK)
  86. return -1;
  87. if (len == 0 || ctx->is_stored) {
  88. return 0;
  89. }
  90. out_offset = 0;
  91. out_len = (uInt)ZIP_MIN(UINT_MAX, len);
  92. ctx->zstr.next_out = (Bytef *)data;
  93. ctx->zstr.avail_out = out_len;
  94. end = 0;
  95. while (!end) {
  96. ret = deflate(&ctx->zstr, ctx->eof ? Z_FINISH : 0);
  97. switch (ret) {
  98. case Z_STREAM_END:
  99. if (ctx->can_store && ctx->zstr.total_in <= ctx->zstr.total_out) {
  100. ctx->is_stored = true;
  101. ctx->size = ctx->zstr.total_in;
  102. memcpy(data, ctx->buffer, ctx->size);
  103. return (zip_int64_t)ctx->size;
  104. }
  105. /* fallthrough */
  106. case Z_OK:
  107. /* all ok */
  108. if (ctx->zstr.avail_out == 0) {
  109. out_offset += out_len;
  110. if (out_offset < len) {
  111. out_len = (uInt)ZIP_MIN(UINT_MAX, len-out_offset);
  112. ctx->zstr.next_out = (Bytef *)data+out_offset;
  113. ctx->zstr.avail_out = out_len;
  114. }
  115. else {
  116. ctx->can_store = false;
  117. end = 1;
  118. }
  119. }
  120. else if (ctx->eof && ctx->zstr.avail_in == 0)
  121. end = 1;
  122. break;
  123. case Z_BUF_ERROR:
  124. if (ctx->zstr.avail_in == 0) {
  125. if (ctx->eof) {
  126. end = 1;
  127. break;
  128. }
  129. if ((n=zip_source_read(src, ctx->buffer, sizeof(ctx->buffer))) < 0) {
  130. _zip_error_set_from_source(&ctx->error, src);
  131. end = 1;
  132. break;
  133. }
  134. else if (n == 0) {
  135. ctx->eof = true;
  136. /* TODO: check against stat of src? */
  137. ctx->size = ctx->zstr.total_in;
  138. }
  139. else {
  140. if (ctx->zstr.total_in > 0) {
  141. /* we overwrote a previously filled ctx->buffer */
  142. ctx->can_store = false;
  143. }
  144. ctx->zstr.next_in = (Bytef *)ctx->buffer;
  145. ctx->zstr.avail_in = (uInt)n;
  146. }
  147. continue;
  148. }
  149. /* fallthrough */
  150. case Z_NEED_DICT:
  151. case Z_DATA_ERROR:
  152. case Z_STREAM_ERROR:
  153. case Z_MEM_ERROR:
  154. zip_error_set(&ctx->error, ZIP_ER_ZLIB, ret);
  155. end = 1;
  156. break;
  157. }
  158. }
  159. if (ctx->zstr.avail_out < len) {
  160. ctx->can_store = false;
  161. return (zip_int64_t)(len - ctx->zstr.avail_out);
  162. }
  163. return (zip_error_code_zip(&ctx->error) == ZIP_ER_OK) ? 0 : -1;
  164. }
  165. static zip_int64_t
  166. decompress_read(zip_source_t *src, struct deflate *ctx, void *data, zip_uint64_t len)
  167. {
  168. int end, ret;
  169. zip_int64_t n;
  170. zip_uint64_t out_offset;
  171. uInt out_len;
  172. if (zip_error_code_zip(&ctx->error) != ZIP_ER_OK)
  173. return -1;
  174. if (len == 0)
  175. return 0;
  176. out_offset = 0;
  177. out_len = (uInt)ZIP_MIN(UINT_MAX, len);
  178. ctx->zstr.next_out = (Bytef *)data;
  179. ctx->zstr.avail_out = out_len;
  180. end = 0;
  181. while (!end) {
  182. ret = inflate(&ctx->zstr, Z_SYNC_FLUSH);
  183. switch (ret) {
  184. case Z_OK:
  185. if (ctx->zstr.avail_out == 0) {
  186. out_offset += out_len;
  187. if (out_offset < len) {
  188. out_len = (uInt)ZIP_MIN(UINT_MAX, len-out_offset);
  189. ctx->zstr.next_out = (Bytef *)data+out_offset;
  190. ctx->zstr.avail_out = out_len;
  191. }
  192. else {
  193. end = 1;
  194. }
  195. }
  196. break;
  197. case Z_STREAM_END:
  198. ctx->eof = 1;
  199. end = 1;
  200. break;
  201. case Z_BUF_ERROR:
  202. if (ctx->zstr.avail_in == 0) {
  203. if (ctx->eof) {
  204. end = 1;
  205. break;
  206. }
  207. if ((n=zip_source_read(src, ctx->buffer, sizeof(ctx->buffer))) < 0) {
  208. _zip_error_set_from_source(&ctx->error, src);
  209. end = 1;
  210. break;
  211. }
  212. else if (n == 0) {
  213. ctx->eof = 1;
  214. }
  215. else {
  216. ctx->zstr.next_in = (Bytef *)ctx->buffer;
  217. ctx->zstr.avail_in = (uInt)n;
  218. }
  219. continue;
  220. }
  221. /* fallthrough */
  222. case Z_NEED_DICT:
  223. case Z_DATA_ERROR:
  224. case Z_STREAM_ERROR:
  225. case Z_MEM_ERROR:
  226. zip_error_set(&ctx->error, ZIP_ER_ZLIB, ret);
  227. end = 1;
  228. break;
  229. }
  230. }
  231. if (ctx->zstr.avail_out < len)
  232. return (zip_int64_t)(len - ctx->zstr.avail_out);
  233. return (zip_error_code_zip(&ctx->error) == ZIP_ER_OK) ? 0 : -1;
  234. }
  235. static zip_int64_t
  236. deflate_compress(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
  237. {
  238. struct deflate *ctx;
  239. int ret;
  240. ctx = (struct deflate *)ud;
  241. switch (cmd) {
  242. case ZIP_SOURCE_OPEN:
  243. ctx->zstr.zalloc = Z_NULL;
  244. ctx->zstr.zfree = Z_NULL;
  245. ctx->zstr.opaque = NULL;
  246. ctx->zstr.avail_in = 0;
  247. ctx->zstr.next_in = NULL;
  248. ctx->zstr.avail_out = 0;
  249. ctx->zstr.next_out = NULL;
  250. /* negative value to tell zlib not to write a header */
  251. if ((ret=deflateInit2(&ctx->zstr, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, ctx->mem_level, Z_DEFAULT_STRATEGY)) != Z_OK) {
  252. zip_error_set(&ctx->error, ZIP_ER_ZLIB, ret);
  253. return -1;
  254. }
  255. return 0;
  256. case ZIP_SOURCE_READ:
  257. return compress_read(src, ctx, data, len);
  258. case ZIP_SOURCE_CLOSE:
  259. deflateEnd(&ctx->zstr);
  260. return 0;
  261. case ZIP_SOURCE_STAT:
  262. {
  263. zip_stat_t *st;
  264. st = (zip_stat_t *)data;
  265. st->comp_method = ctx->is_stored ? ZIP_CM_STORE : ZIP_CM_DEFLATE;
  266. st->valid |= ZIP_STAT_COMP_METHOD;
  267. if (ctx->eof) {
  268. st->comp_size = ctx->size;
  269. st->valid |= ZIP_STAT_COMP_SIZE;
  270. }
  271. else
  272. st->valid &= ~ZIP_STAT_COMP_SIZE;
  273. }
  274. return 0;
  275. case ZIP_SOURCE_ERROR:
  276. return zip_error_to_data(&ctx->error, data, len);
  277. case ZIP_SOURCE_FREE:
  278. deflate_free(ctx);
  279. return 0;
  280. case ZIP_SOURCE_SUPPORTS:
  281. return ZIP_SOURCE_SUPPORTS_READABLE;
  282. default:
  283. zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
  284. return -1;
  285. }
  286. }
  287. static zip_int64_t
  288. deflate_decompress(zip_source_t *src, void *ud, void *data,
  289. zip_uint64_t len, zip_source_cmd_t cmd)
  290. {
  291. struct deflate *ctx;
  292. zip_int64_t n;
  293. int ret;
  294. ctx = (struct deflate *)ud;
  295. switch (cmd) {
  296. case ZIP_SOURCE_OPEN:
  297. if ((n=zip_source_read(src, ctx->buffer, sizeof(ctx->buffer))) < 0) {
  298. _zip_error_set_from_source(&ctx->error, src);
  299. return -1;
  300. }
  301. ctx->zstr.zalloc = Z_NULL;
  302. ctx->zstr.zfree = Z_NULL;
  303. ctx->zstr.opaque = NULL;
  304. ctx->zstr.next_in = (Bytef *)ctx->buffer;
  305. ctx->zstr.avail_in = (uInt)n;
  306. /* negative value to tell zlib that there is no header */
  307. if ((ret=inflateInit2(&ctx->zstr, -MAX_WBITS)) != Z_OK) {
  308. zip_error_set(&ctx->error, ZIP_ER_ZLIB, ret);
  309. return -1;
  310. }
  311. return 0;
  312. case ZIP_SOURCE_READ:
  313. return decompress_read(src, ctx, data, len);
  314. case ZIP_SOURCE_CLOSE:
  315. inflateEnd(&ctx->zstr);
  316. return 0;
  317. case ZIP_SOURCE_STAT:
  318. {
  319. zip_stat_t *st;
  320. st = (zip_stat_t *)data;
  321. st->comp_method = ZIP_CM_STORE;
  322. if (st->comp_size > 0 && st->size > 0)
  323. st->comp_size = st->size;
  324. return 0;
  325. }
  326. case ZIP_SOURCE_ERROR:
  327. return zip_error_to_data(&ctx->error, data, len);
  328. case ZIP_SOURCE_FREE:
  329. free(ctx);
  330. return 0;
  331. case ZIP_SOURCE_SUPPORTS:
  332. return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, -1);
  333. default:
  334. zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
  335. return -1;
  336. }
  337. }
  338. static void
  339. deflate_free(struct deflate *ctx)
  340. {
  341. free(ctx);
  342. }