zip_source_compress.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. zip_source_compress.c -- (de)compression routines
  3. Copyright (C) 2017-2021 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 "zipint.h"
  33. struct context {
  34. zip_error_t error;
  35. bool end_of_input;
  36. bool end_of_stream;
  37. bool can_store;
  38. bool is_stored; /* only valid if end_of_stream is true */
  39. bool compress;
  40. zip_int32_t method;
  41. zip_uint64_t size;
  42. zip_int64_t first_read;
  43. zip_uint8_t buffer[BUFSIZE];
  44. zip_compression_algorithm_t *algorithm;
  45. void *ud;
  46. };
  47. struct implementation {
  48. zip_uint16_t method;
  49. zip_compression_algorithm_t *compress;
  50. zip_compression_algorithm_t *decompress;
  51. };
  52. static struct implementation implementations[] = {
  53. {ZIP_CM_DEFLATE, &zip_algorithm_deflate_compress, &zip_algorithm_deflate_decompress},
  54. #if defined(HAVE_LIBBZ2)
  55. {ZIP_CM_BZIP2, &zip_algorithm_bzip2_compress, &zip_algorithm_bzip2_decompress},
  56. #endif
  57. #if defined(HAVE_LIBLZMA)
  58. {ZIP_CM_LZMA, &zip_algorithm_xz_compress, &zip_algorithm_xz_decompress},
  59. /* Disabled - because 7z isn't able to unpack ZIP+LZMA2
  60. archives made this way - and vice versa.
  61. {ZIP_CM_LZMA2, &zip_algorithm_xz_compress, &zip_algorithm_xz_decompress},
  62. */
  63. {ZIP_CM_XZ, &zip_algorithm_xz_compress, &zip_algorithm_xz_decompress},
  64. #endif
  65. #if defined(HAVE_LIBZSTD)
  66. {ZIP_CM_ZSTD, &zip_algorithm_zstd_compress, &zip_algorithm_zstd_decompress},
  67. #endif
  68. };
  69. static size_t implementations_size = sizeof(implementations) / sizeof(implementations[0]);
  70. static zip_source_t *compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool compress, int compression_flags);
  71. static zip_int64_t compress_callback(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
  72. static void context_free(struct context *ctx);
  73. static struct context *context_new(zip_int32_t method, bool compress, int compression_flags, zip_compression_algorithm_t *algorithm);
  74. static zip_int64_t compress_read(zip_source_t *, struct context *, void *, zip_uint64_t);
  75. zip_compression_algorithm_t *
  76. _zip_get_compression_algorithm(zip_int32_t method, bool compress) {
  77. size_t i;
  78. zip_uint16_t real_method = ZIP_CM_ACTUAL(method);
  79. for (i = 0; i < implementations_size; i++) {
  80. if (implementations[i].method == real_method) {
  81. if (compress) {
  82. return implementations[i].compress;
  83. }
  84. else {
  85. return implementations[i].decompress;
  86. }
  87. }
  88. }
  89. return NULL;
  90. }
  91. ZIP_EXTERN int
  92. zip_compression_method_supported(zip_int32_t method, int compress) {
  93. if (method == ZIP_CM_STORE) {
  94. return 1;
  95. }
  96. return _zip_get_compression_algorithm(method, compress) != NULL;
  97. }
  98. zip_source_t *
  99. zip_source_compress(zip_t *za, zip_source_t *src, zip_int32_t method, int compression_flags) {
  100. return compression_source_new(za, src, method, true, compression_flags);
  101. }
  102. zip_source_t *
  103. zip_source_decompress(zip_t *za, zip_source_t *src, zip_int32_t method) {
  104. return compression_source_new(za, src, method, false, 0);
  105. }
  106. static zip_source_t *
  107. compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool compress, int compression_flags) {
  108. struct context *ctx;
  109. zip_source_t *s2;
  110. zip_compression_algorithm_t *algorithm = NULL;
  111. if (src == NULL) {
  112. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  113. return NULL;
  114. }
  115. if ((algorithm = _zip_get_compression_algorithm(method, compress)) == NULL) {
  116. zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
  117. return NULL;
  118. }
  119. if ((ctx = context_new(method, compress, compression_flags, algorithm)) == NULL) {
  120. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  121. return NULL;
  122. }
  123. if ((s2 = zip_source_layered(za, src, compress_callback, ctx)) == NULL) {
  124. context_free(ctx);
  125. return NULL;
  126. }
  127. return s2;
  128. }
  129. static struct context *
  130. context_new(zip_int32_t method, bool compress, int compression_flags, zip_compression_algorithm_t *algorithm) {
  131. struct context *ctx;
  132. if ((ctx = (struct context *)malloc(sizeof(*ctx))) == NULL) {
  133. return NULL;
  134. }
  135. zip_error_init(&ctx->error);
  136. ctx->can_store = compress ? ZIP_CM_IS_DEFAULT(method) : false;
  137. ctx->algorithm = algorithm;
  138. ctx->method = method;
  139. ctx->compress = compress;
  140. ctx->end_of_input = false;
  141. ctx->end_of_stream = false;
  142. ctx->is_stored = false;
  143. if ((ctx->ud = ctx->algorithm->allocate(ZIP_CM_ACTUAL(method), compression_flags, &ctx->error)) == NULL) {
  144. zip_error_fini(&ctx->error);
  145. free(ctx);
  146. return NULL;
  147. }
  148. return ctx;
  149. }
  150. static void
  151. context_free(struct context *ctx) {
  152. if (ctx == NULL) {
  153. return;
  154. }
  155. ctx->algorithm->deallocate(ctx->ud);
  156. zip_error_fini(&ctx->error);
  157. free(ctx);
  158. }
  159. static zip_int64_t
  160. compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t len) {
  161. zip_compression_status_t ret;
  162. bool end;
  163. zip_int64_t n;
  164. zip_uint64_t out_offset;
  165. zip_uint64_t out_len;
  166. if (zip_error_code_zip(&ctx->error) != ZIP_ER_OK) {
  167. return -1;
  168. }
  169. if (len == 0 || ctx->end_of_stream) {
  170. return 0;
  171. }
  172. out_offset = 0;
  173. end = false;
  174. while (!end && out_offset < len) {
  175. out_len = len - out_offset;
  176. ret = ctx->algorithm->process(ctx->ud, (zip_uint8_t *)data + out_offset, &out_len);
  177. if (ret != ZIP_COMPRESSION_ERROR) {
  178. out_offset += out_len;
  179. }
  180. switch (ret) {
  181. case ZIP_COMPRESSION_END:
  182. ctx->end_of_stream = true;
  183. if (!ctx->end_of_input) {
  184. /* TODO: garbage after stream, or compression ended before all data read */
  185. }
  186. if (ctx->first_read < 0) {
  187. /* we got end of processed stream before reading any input data */
  188. zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
  189. end = true;
  190. break;
  191. }
  192. if (ctx->can_store && (zip_uint64_t)ctx->first_read <= out_offset) {
  193. ctx->is_stored = true;
  194. ctx->size = (zip_uint64_t)ctx->first_read;
  195. memcpy(data, ctx->buffer, ctx->size);
  196. return (zip_int64_t)ctx->size;
  197. }
  198. end = true;
  199. break;
  200. case ZIP_COMPRESSION_OK:
  201. break;
  202. case ZIP_COMPRESSION_NEED_DATA:
  203. if (ctx->end_of_input) {
  204. /* TODO: error: stream not ended, but no more input */
  205. end = true;
  206. break;
  207. }
  208. if ((n = zip_source_read(src, ctx->buffer, sizeof(ctx->buffer))) < 0) {
  209. _zip_error_set_from_source(&ctx->error, src);
  210. end = true;
  211. break;
  212. }
  213. else if (n == 0) {
  214. ctx->end_of_input = true;
  215. ctx->algorithm->end_of_input(ctx->ud);
  216. if (ctx->first_read < 0) {
  217. ctx->first_read = 0;
  218. }
  219. }
  220. else {
  221. if (ctx->first_read >= 0) {
  222. /* we overwrote a previously filled ctx->buffer */
  223. ctx->can_store = false;
  224. }
  225. else {
  226. ctx->first_read = n;
  227. }
  228. ctx->algorithm->input(ctx->ud, ctx->buffer, (zip_uint64_t)n);
  229. }
  230. break;
  231. case ZIP_COMPRESSION_ERROR:
  232. /* error set by algorithm */
  233. if (zip_error_code_zip(&ctx->error) == ZIP_ER_OK) {
  234. zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
  235. }
  236. end = true;
  237. break;
  238. }
  239. }
  240. if (out_offset > 0) {
  241. ctx->can_store = false;
  242. ctx->size += out_offset;
  243. return (zip_int64_t)out_offset;
  244. }
  245. return (zip_error_code_zip(&ctx->error) == ZIP_ER_OK) ? 0 : -1;
  246. }
  247. static zip_int64_t
  248. compress_callback(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
  249. struct context *ctx;
  250. ctx = (struct context *)ud;
  251. switch (cmd) {
  252. case ZIP_SOURCE_OPEN: {
  253. zip_stat_t st;
  254. zip_file_attributes_t attributes;
  255. ctx->size = 0;
  256. ctx->end_of_input = false;
  257. ctx->end_of_stream = false;
  258. ctx->is_stored = false;
  259. ctx->first_read = -1;
  260. if (zip_source_stat(src, &st) < 0 || zip_source_get_file_attributes(src, &attributes) < 0) {
  261. _zip_error_set_from_source(&ctx->error, src);
  262. return -1;
  263. }
  264. if (!ctx->algorithm->start(ctx->ud, &st, &attributes)) {
  265. return -1;
  266. }
  267. return 0;
  268. }
  269. case ZIP_SOURCE_READ:
  270. return compress_read(src, ctx, data, len);
  271. case ZIP_SOURCE_CLOSE:
  272. if (!ctx->algorithm->end(ctx->ud)) {
  273. return -1;
  274. }
  275. return 0;
  276. case ZIP_SOURCE_STAT: {
  277. zip_stat_t *st;
  278. st = (zip_stat_t *)data;
  279. if (ctx->compress) {
  280. if (ctx->end_of_stream) {
  281. st->comp_method = ctx->is_stored ? ZIP_CM_STORE : ZIP_CM_ACTUAL(ctx->method);
  282. st->comp_size = ctx->size;
  283. st->valid |= ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD;
  284. }
  285. else {
  286. st->valid &= ~(ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD);
  287. }
  288. }
  289. else {
  290. st->comp_method = ZIP_CM_STORE;
  291. st->valid |= ZIP_STAT_COMP_METHOD;
  292. if (ctx->end_of_stream) {
  293. st->size = ctx->size;
  294. st->valid |= ZIP_STAT_SIZE;
  295. }
  296. }
  297. }
  298. return 0;
  299. case ZIP_SOURCE_ERROR:
  300. return zip_error_to_data(&ctx->error, data, len);
  301. case ZIP_SOURCE_FREE:
  302. context_free(ctx);
  303. return 0;
  304. case ZIP_SOURCE_GET_FILE_ATTRIBUTES: {
  305. zip_file_attributes_t *attributes = (zip_file_attributes_t *)data;
  306. if (len < sizeof(*attributes)) {
  307. zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
  308. return -1;
  309. }
  310. attributes->valid |= ZIP_FILE_ATTRIBUTES_VERSION_NEEDED | ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS;
  311. attributes->version_needed = ctx->algorithm->version_needed;
  312. attributes->general_purpose_bit_mask = ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK;
  313. attributes->general_purpose_bit_flags = (ctx->is_stored ? 0 : ctx->algorithm->general_purpose_bit_flags(ctx->ud));
  314. return sizeof(*attributes);
  315. }
  316. case ZIP_SOURCE_SUPPORTS:
  317. return ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_GET_FILE_ATTRIBUTES, -1);
  318. default:
  319. zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
  320. return -1;
  321. }
  322. }