zip_algorithm_deflate.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. zip_algorithm_deflate.c -- deflate (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 "zipint.h"
  31. #include <limits.h>
  32. #include <stdlib.h>
  33. #include <zlib.h>
  34. struct ctx {
  35. zip_error_t *error;
  36. bool compress;
  37. int compression_flags;
  38. bool end_of_input;
  39. z_stream zstr;
  40. };
  41. static zip_uint64_t
  42. maximum_compressed_size(zip_uint64_t uncompressed_size) {
  43. /* max deflate size increase: size + ceil(size/16k)*5+6 */
  44. zip_uint64_t compressed_size = uncompressed_size + (uncompressed_size + 16383) / 16384 * 5 + 6;
  45. if (compressed_size < uncompressed_size) {
  46. return ZIP_UINT64_MAX;
  47. }
  48. return compressed_size;
  49. }
  50. static void *
  51. allocate(bool compress, int compression_flags, zip_error_t *error) {
  52. struct ctx *ctx;
  53. if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
  54. zip_error_set(error, ZIP_ET_SYS, errno);
  55. return NULL;
  56. }
  57. ctx->error = error;
  58. ctx->compress = compress;
  59. ctx->compression_flags = compression_flags;
  60. if (ctx->compression_flags < 1 || ctx->compression_flags > 9) {
  61. ctx->compression_flags = Z_BEST_COMPRESSION;
  62. }
  63. ctx->end_of_input = false;
  64. ctx->zstr.zalloc = Z_NULL;
  65. ctx->zstr.zfree = Z_NULL;
  66. ctx->zstr.opaque = NULL;
  67. return ctx;
  68. }
  69. static void *
  70. compress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error) {
  71. return allocate(true, compression_flags, error);
  72. }
  73. static void *
  74. decompress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error) {
  75. return allocate(false, compression_flags, error);
  76. }
  77. static void
  78. deallocate(void *ud) {
  79. struct ctx *ctx = (struct ctx *)ud;
  80. free(ctx);
  81. }
  82. static zip_uint16_t
  83. general_purpose_bit_flags(void *ud) {
  84. struct ctx *ctx = (struct ctx *)ud;
  85. if (!ctx->compress) {
  86. return 0;
  87. }
  88. if (ctx->compression_flags < 3) {
  89. return 2 << 1;
  90. }
  91. else if (ctx->compression_flags > 7) {
  92. return 1 << 1;
  93. }
  94. return 0;
  95. }
  96. static bool
  97. start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
  98. struct ctx *ctx = (struct ctx *)ud;
  99. int ret;
  100. ctx->zstr.avail_in = 0;
  101. ctx->zstr.next_in = NULL;
  102. ctx->zstr.avail_out = 0;
  103. ctx->zstr.next_out = NULL;
  104. if (ctx->compress) {
  105. /* negative value to tell zlib not to write a header */
  106. ret = deflateInit2(&ctx->zstr, ctx->compression_flags, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  107. }
  108. else {
  109. ret = inflateInit2(&ctx->zstr, -MAX_WBITS);
  110. }
  111. if (ret != Z_OK) {
  112. zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
  113. return false;
  114. }
  115. return true;
  116. }
  117. static bool
  118. end(void *ud) {
  119. struct ctx *ctx = (struct ctx *)ud;
  120. int err;
  121. if (ctx->compress) {
  122. err = deflateEnd(&ctx->zstr);
  123. }
  124. else {
  125. err = inflateEnd(&ctx->zstr);
  126. }
  127. if (err != Z_OK) {
  128. zip_error_set(ctx->error, ZIP_ER_ZLIB, err);
  129. return false;
  130. }
  131. return true;
  132. }
  133. static bool
  134. input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
  135. struct ctx *ctx = (struct ctx *)ud;
  136. if (length > UINT_MAX || ctx->zstr.avail_in > 0) {
  137. zip_error_set(ctx->error, ZIP_ER_INVAL, 0);
  138. return false;
  139. }
  140. ctx->zstr.avail_in = (uInt)length;
  141. ctx->zstr.next_in = (Bytef *)data;
  142. return true;
  143. }
  144. static void
  145. end_of_input(void *ud) {
  146. struct ctx *ctx = (struct ctx *)ud;
  147. ctx->end_of_input = true;
  148. }
  149. static zip_compression_status_t
  150. process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
  151. struct ctx *ctx = (struct ctx *)ud;
  152. int ret;
  153. ctx->zstr.avail_out = (uInt)ZIP_MIN(UINT_MAX, *length);
  154. ctx->zstr.next_out = (Bytef *)data;
  155. if (ctx->compress) {
  156. ret = deflate(&ctx->zstr, ctx->end_of_input ? Z_FINISH : 0);
  157. }
  158. else {
  159. ret = inflate(&ctx->zstr, Z_SYNC_FLUSH);
  160. }
  161. *length = *length - ctx->zstr.avail_out;
  162. switch (ret) {
  163. case Z_OK:
  164. return ZIP_COMPRESSION_OK;
  165. case Z_STREAM_END:
  166. return ZIP_COMPRESSION_END;
  167. case Z_BUF_ERROR:
  168. if (ctx->zstr.avail_in == 0) {
  169. return ZIP_COMPRESSION_NEED_DATA;
  170. }
  171. /* fallthrough */
  172. default:
  173. zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
  174. return ZIP_COMPRESSION_ERROR;
  175. }
  176. }
  177. /* clang-format off */
  178. zip_compression_algorithm_t zip_algorithm_deflate_compress = {
  179. maximum_compressed_size,
  180. compress_allocate,
  181. deallocate,
  182. general_purpose_bit_flags,
  183. 20,
  184. start,
  185. end,
  186. input,
  187. end_of_input,
  188. process
  189. };
  190. zip_compression_algorithm_t zip_algorithm_deflate_decompress = {
  191. maximum_compressed_size,
  192. decompress_allocate,
  193. deallocate,
  194. general_purpose_bit_flags,
  195. 20,
  196. start,
  197. end,
  198. input,
  199. end_of_input,
  200. process
  201. };
  202. /* clang-format on */