compress.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "z-imp.h"
  24. #include <limits.h>
  25. void fz_deflate(fz_context *ctx, unsigned char *dest, size_t *destLen, const unsigned char *source, size_t sourceLen, fz_deflate_level level)
  26. {
  27. z_stream stream;
  28. int err;
  29. size_t left;
  30. left = *destLen;
  31. *destLen = 0;
  32. stream.zalloc = fz_zlib_alloc;
  33. stream.zfree = fz_zlib_free;
  34. stream.opaque = ctx;
  35. err = deflateInit(&stream, (int)level);
  36. if (err != Z_OK)
  37. fz_throw(ctx, FZ_ERROR_LIBRARY, "deflateInit failed: %d", err);
  38. stream.next_out = dest;
  39. stream.avail_out = 0;
  40. stream.next_in = (z_const Bytef *)source;
  41. stream.avail_in = 0;
  42. do {
  43. if (stream.avail_out == 0) {
  44. stream.avail_out = left > UINT_MAX ? UINT_MAX : (uInt)left;
  45. left -= stream.avail_out;
  46. }
  47. if (stream.avail_in == 0) {
  48. stream.avail_in = sourceLen > UINT_MAX ? UINT_MAX : (uInt)sourceLen;
  49. sourceLen -= stream.avail_in;
  50. }
  51. err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
  52. } while (err == Z_OK);
  53. /* We might have problems if the compressed length > uLong sized. Tough, for now. */
  54. *destLen = stream.total_out;
  55. deflateEnd(&stream);
  56. if (err != Z_STREAM_END)
  57. fz_throw(ctx, FZ_ERROR_LIBRARY, "deflate error: %d", err);
  58. }
  59. unsigned char *fz_new_deflated_data(fz_context *ctx, size_t *compressed_length, const unsigned char *source, size_t source_length, fz_deflate_level level)
  60. {
  61. size_t bound = fz_deflate_bound(ctx, source_length);
  62. unsigned char *cdata = Memento_label(fz_malloc(ctx, bound), "deflated_data");
  63. *compressed_length = 0;
  64. fz_try(ctx)
  65. fz_deflate(ctx, cdata, &bound, source, source_length, level);
  66. fz_catch(ctx)
  67. {
  68. fz_free(ctx, cdata);
  69. fz_rethrow(ctx);
  70. }
  71. *compressed_length = bound;
  72. return cdata;
  73. }
  74. unsigned char *fz_new_deflated_data_from_buffer(fz_context *ctx, size_t *compressed_length, fz_buffer *buffer, fz_deflate_level level)
  75. {
  76. unsigned char *data;
  77. size_t size = fz_buffer_storage(ctx, buffer, &data);
  78. if (size == 0 || data == NULL)
  79. {
  80. *compressed_length = 0;
  81. return NULL;
  82. }
  83. return fz_new_deflated_data(ctx, compressed_length, data, size, level);
  84. }
  85. size_t fz_deflate_bound(fz_context *ctx, size_t size)
  86. {
  87. /* Copied from zlib to account for size_t vs uLong */
  88. return size + (size >> 12) + (size >> 14) + (size >> 25) + 13;
  89. }