filter-flate.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <zlib.h>
  24. #include <string.h>
  25. typedef struct
  26. {
  27. fz_stream *chain;
  28. z_stream z;
  29. unsigned char buffer[4096];
  30. } fz_inflate_state;
  31. void *fz_zlib_alloc(void *ctx, unsigned int items, unsigned int size)
  32. {
  33. return Memento_label(fz_malloc_no_throw(ctx, (size_t)items * size), "zlib_alloc");
  34. }
  35. void fz_zlib_free(void *ctx, void *ptr)
  36. {
  37. fz_free(ctx, ptr);
  38. }
  39. static int
  40. next_flated(fz_context *ctx, fz_stream *stm, size_t required)
  41. {
  42. fz_inflate_state *state = stm->state;
  43. fz_stream *chain = state->chain;
  44. z_streamp zp = &state->z;
  45. int code;
  46. unsigned char *outbuf = state->buffer;
  47. int outlen = sizeof(state->buffer);
  48. if (stm->eof)
  49. return EOF;
  50. zp->next_out = outbuf;
  51. zp->avail_out = outlen;
  52. while (zp->avail_out > 0)
  53. {
  54. zp->avail_in = (uInt)fz_available(ctx, chain, 1);
  55. zp->next_in = chain->rp;
  56. code = inflate(zp, Z_SYNC_FLUSH);
  57. chain->rp = chain->wp - zp->avail_in;
  58. if (code == Z_STREAM_END)
  59. {
  60. break;
  61. }
  62. else if (code == Z_BUF_ERROR)
  63. {
  64. fz_warn(ctx, "premature end of data in flate filter");
  65. break;
  66. }
  67. else if (code == Z_DATA_ERROR && zp->avail_in == 0)
  68. {
  69. fz_warn(ctx, "ignoring zlib error: %s", zp->msg);
  70. break;
  71. }
  72. else if (code == Z_DATA_ERROR && !strcmp(zp->msg, "incorrect data check"))
  73. {
  74. fz_warn(ctx, "ignoring zlib error: %s", zp->msg);
  75. chain->rp = chain->wp;
  76. break;
  77. }
  78. else if (code != Z_OK)
  79. {
  80. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib error: %s", zp->msg);
  81. }
  82. }
  83. stm->rp = state->buffer;
  84. stm->wp = state->buffer + outlen - zp->avail_out;
  85. stm->pos += outlen - zp->avail_out;
  86. if (stm->rp == stm->wp)
  87. {
  88. stm->eof = 1;
  89. return EOF;
  90. }
  91. return *stm->rp++;
  92. }
  93. static void
  94. close_flated(fz_context *ctx, void *state_)
  95. {
  96. fz_inflate_state *state = (fz_inflate_state *)state_;
  97. int code;
  98. code = inflateEnd(&state->z);
  99. if (code != Z_OK)
  100. fz_warn(ctx, "zlib error: inflateEnd: %s", state->z.msg);
  101. fz_drop_stream(ctx, state->chain);
  102. fz_free(ctx, state);
  103. }
  104. fz_stream *
  105. fz_open_flated(fz_context *ctx, fz_stream *chain, int window_bits)
  106. {
  107. fz_inflate_state *state;
  108. int code;
  109. state = fz_malloc_struct(ctx, fz_inflate_state);
  110. state->z.zalloc = fz_zlib_alloc;
  111. state->z.zfree = fz_zlib_free;
  112. state->z.opaque = ctx;
  113. state->z.next_in = NULL;
  114. state->z.avail_in = 0;
  115. code = inflateInit2(&state->z, window_bits);
  116. if (code != Z_OK)
  117. {
  118. fz_free(ctx, state);
  119. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib error: inflateInit2 failed");
  120. }
  121. state->chain = fz_keep_stream(ctx, chain);
  122. return fz_new_stream(ctx, state, next_flated, close_flated);
  123. }