gz-doc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (C) 2023-2024 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. #ifndef MAX_WBITS
  24. # define MAX_WBITS 15 /* 32K LZ77 window */
  25. #endif
  26. static fz_document *
  27. gz_open_document(fz_context *ctx, const fz_document_handler *handler, fz_stream *ostm, fz_stream *accel, fz_archive *dir, void *state)
  28. {
  29. fz_stream *stm = fz_open_flated(ctx, ostm, 16 + MAX_WBITS);
  30. fz_buffer *buf = NULL;
  31. fz_document *doc = NULL;
  32. fz_var(buf);
  33. fz_var(doc);
  34. fz_try(ctx)
  35. {
  36. buf = fz_read_all(ctx, stm, 1024);
  37. /* No way to pass the magic onwards :( */
  38. doc = fz_open_document_with_buffer(ctx, "application/octet-stream", buf);
  39. }
  40. fz_always(ctx)
  41. {
  42. fz_drop_stream(ctx, stm);
  43. fz_drop_buffer(ctx, buf);
  44. }
  45. fz_catch(ctx)
  46. fz_rethrow(ctx);
  47. return doc;
  48. }
  49. static const char *gz_extensions[] =
  50. {
  51. "gz",
  52. NULL
  53. };
  54. static const char *gz_mimetypes[] =
  55. {
  56. "application/x-gzip-compressed",
  57. NULL
  58. };
  59. static int
  60. gz_recognize_doc_content(fz_context *ctx, const fz_document_handler *handler, fz_stream *stream, fz_archive *dir, void **state, fz_document_recognize_state_free_fn **free_state)
  61. {
  62. int ret = 0;
  63. uint8_t data[10];
  64. if (state)
  65. *state = NULL;
  66. if (free_state)
  67. *free_state = NULL;
  68. if (stream == NULL)
  69. return 0;
  70. fz_try(ctx)
  71. {
  72. fz_seek(ctx, stream, 0, SEEK_SET);
  73. /* 10 byte header */
  74. if (fz_read(ctx, stream, data, 10) != 10)
  75. break;
  76. /* We only actually check the first 3 bytes though. */
  77. if (data[0] == 0x1f && data[1] == 0x8b && data[2] == 0x08)
  78. ret = 100;
  79. }
  80. fz_catch(ctx)
  81. fz_rethrow(ctx);
  82. return ret;
  83. }
  84. fz_document_handler gz_document_handler =
  85. {
  86. NULL,
  87. gz_open_document,
  88. gz_extensions,
  89. gz_mimetypes,
  90. gz_recognize_doc_content
  91. };