filter-brotli.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/decode.h"
  25. #include <string.h>
  26. typedef struct
  27. {
  28. fz_stream *chain;
  29. BrotliDecoderState *dec_state;
  30. unsigned char buffer[4096];
  31. } fz_brotli_state;
  32. void *my_brotlid_alloc(void *ctx, size_t size)
  33. {
  34. return Memento_label(fz_malloc_no_throw(ctx, size), "brotlid_alloc");
  35. }
  36. void my_brotlid_free(void *ctx, void *ptr)
  37. {
  38. fz_free(ctx, ptr);
  39. }
  40. static int
  41. next_brotlid(fz_context *ctx, fz_stream *stm, size_t required)
  42. {
  43. fz_brotli_state *state = stm->state;
  44. fz_stream *chain = state->chain;
  45. unsigned char *outbuf = state->buffer;
  46. size_t outlen = sizeof(state->buffer);
  47. BrotliDecoderResult res;
  48. if (stm->eof)
  49. return EOF;
  50. while (outlen > 0)
  51. {
  52. size_t avail_in = fz_available(ctx, chain, 1);
  53. res = BrotliDecoderDecompressStream(state->dec_state,
  54. &avail_in,
  55. (const uint8_t **)&chain->rp,
  56. &outlen,
  57. &outbuf,
  58. NULL);
  59. chain->rp = chain->wp - avail_in;
  60. if (res == BROTLI_DECODER_RESULT_SUCCESS)
  61. {
  62. break;
  63. }
  64. else if (res == BROTLI_DECODER_RESULT_ERROR)
  65. {
  66. fz_throw(ctx, FZ_ERROR_LIBRARY, "brotli decompression error");
  67. }
  68. }
  69. stm->rp = state->buffer;
  70. stm->wp = state->buffer + sizeof(state->buffer) - outlen;
  71. stm->pos += sizeof(state->buffer) - outlen;
  72. if (stm->rp == stm->wp)
  73. {
  74. stm->eof = 1;
  75. return EOF;
  76. }
  77. return *stm->rp++;
  78. }
  79. static void
  80. close_brotlid(fz_context *ctx, void *state_)
  81. {
  82. fz_brotli_state *state = (fz_brotli_state *)state_;
  83. BrotliDecoderDestroyInstance(state->dec_state);
  84. fz_drop_stream(ctx, state->chain);
  85. fz_free(ctx, state);
  86. }
  87. fz_stream *
  88. fz_open_brotlid(fz_context *ctx, fz_stream *chain)
  89. {
  90. fz_brotli_state *state;
  91. state = fz_malloc_struct(ctx, fz_brotli_state);
  92. state->dec_state = BrotliDecoderCreateInstance(
  93. my_brotlid_alloc,
  94. my_brotlid_free,
  95. ctx);
  96. if (state->dec_state == NULL)
  97. {
  98. fz_free(ctx, state);
  99. fz_throw(ctx, FZ_ERROR_LIBRARY, "brotli error: decoder creation failed");
  100. }
  101. state->chain = fz_keep_stream(ctx, chain);
  102. return fz_new_stream(ctx, state, next_brotlid, close_brotlid);
  103. }
  104. #else
  105. fz_stream *
  106. fz_open_brotlid(fz_context *ctx, fz_stream *chain)
  107. {
  108. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "brotli compression not enabled");
  109. return NULL;
  110. }
  111. #endif