brotli.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2004-2025 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. #if FZ_ENABLE_BROTLI
  24. #include "brotli/encode.h"
  25. #include <limits.h>
  26. static void *
  27. my_brotli_alloc(void *opaque, size_t size)
  28. {
  29. return fz_malloc_no_throw((fz_context *)opaque, size);
  30. }
  31. static void
  32. my_brotli_free(void *opaque, void *ptr)
  33. {
  34. fz_free((fz_context *)opaque, ptr);
  35. }
  36. void fz_compress_brotli(fz_context *ctx, unsigned char *dest, size_t *dest_len, const unsigned char *source, size_t source_len, fz_brotli_level level)
  37. {
  38. int ok;
  39. BrotliEncoderState *enc_state;
  40. unsigned char *outp = dest;
  41. size_t avail_out = *dest_len;
  42. enc_state = BrotliEncoderCreateInstance(my_brotli_alloc, my_brotli_free, ctx);
  43. if (!BrotliEncoderSetParameter(enc_state, BROTLI_PARAM_QUALITY, level))
  44. {
  45. BrotliEncoderDestroyInstance(enc_state);
  46. fz_throw(ctx, FZ_ERROR_LIBRARY, "Brotli compression failed");
  47. }
  48. do {
  49. ok = BrotliEncoderCompressStream(enc_state,
  50. (source_len > 0 ? BROTLI_OPERATION_PROCESS : BROTLI_OPERATION_FINISH),
  51. &source_len,
  52. &source,
  53. &avail_out,
  54. &outp,
  55. NULL);
  56. } while (ok && !BrotliEncoderIsFinished(enc_state));
  57. if (!ok)
  58. {
  59. BrotliEncoderDestroyInstance(enc_state);
  60. fz_throw(ctx, FZ_ERROR_LIBRARY, "Brotli compression failed");
  61. }
  62. *dest_len = outp - dest;
  63. BrotliEncoderDestroyInstance(enc_state);
  64. }
  65. unsigned char *fz_new_brotli_data(fz_context *ctx, size_t *compressed_length, const unsigned char *source, size_t source_length, fz_brotli_level level)
  66. {
  67. size_t bound = fz_brotli_bound(ctx, source_length);
  68. unsigned char *cdata = Memento_label(fz_malloc(ctx, bound), "brotli_data");
  69. *compressed_length = 0;
  70. fz_try(ctx)
  71. fz_compress_brotli(ctx, cdata, &bound, source, source_length, level);
  72. fz_catch(ctx)
  73. {
  74. fz_free(ctx, cdata);
  75. fz_rethrow(ctx);
  76. }
  77. *compressed_length = bound;
  78. return cdata;
  79. }
  80. unsigned char *fz_new_brotli_data_from_buffer(fz_context *ctx, size_t *compressed_length, fz_buffer *buffer, fz_brotli_level level)
  81. {
  82. unsigned char *data;
  83. size_t size = fz_buffer_storage(ctx, buffer, &data);
  84. if (size == 0 || data == NULL)
  85. {
  86. *compressed_length = 0;
  87. return NULL;
  88. }
  89. return fz_new_brotli_data(ctx, compressed_length, data, size, level);
  90. }
  91. size_t fz_brotli_bound(fz_context *ctx, size_t size)
  92. {
  93. return BrotliEncoderMaxCompressedSize(size);
  94. }
  95. #else
  96. size_t fz_brotli_bound(fz_context *ctx, size_t size)
  97. {
  98. return size;
  99. }
  100. void fz_compress_brotli(fz_context *ctx, unsigned char *dest, size_t *dest_len, const unsigned char *source, size_t source_len, fz_brotli_level level)
  101. {
  102. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "brotli compression not enabled");
  103. }
  104. unsigned char *fz_new_brotli_data_from_buffer(fz_context *ctx, size_t *compressed_length, fz_buffer *buffer, fz_brotli_level level)
  105. {
  106. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "brotli compression not enabled");
  107. return NULL;
  108. }
  109. unsigned char *fz_new_brotli_data(fz_context *ctx, size_t *compressed_length, const unsigned char *source, size_t source_length, fz_brotli_level level)
  110. {
  111. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "brotli compression not enabled");
  112. return NULL;
  113. }
  114. #endif