Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/fitz/gz-doc.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 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 | |
| 23 #include "mupdf/fitz.h" | |
| 24 | |
| 25 #ifndef MAX_WBITS | |
| 26 # define MAX_WBITS 15 /* 32K LZ77 window */ | |
| 27 #endif | |
| 28 | |
| 29 static fz_document * | |
| 30 gz_open_document(fz_context *ctx, const fz_document_handler *handler, fz_stream *ostm, fz_stream *accel, fz_archive *dir, void *state) | |
| 31 { | |
| 32 fz_stream *stm = fz_open_flated(ctx, ostm, 16 + MAX_WBITS); | |
| 33 fz_buffer *buf = NULL; | |
| 34 fz_document *doc = NULL; | |
| 35 | |
| 36 fz_var(buf); | |
| 37 fz_var(doc); | |
| 38 | |
| 39 fz_try(ctx) | |
| 40 { | |
| 41 buf = fz_read_all(ctx, stm, 1024); | |
| 42 /* No way to pass the magic onwards :( */ | |
| 43 doc = fz_open_document_with_buffer(ctx, "application/octet-stream", buf); | |
| 44 } | |
| 45 fz_always(ctx) | |
| 46 { | |
| 47 fz_drop_stream(ctx, stm); | |
| 48 fz_drop_buffer(ctx, buf); | |
| 49 } | |
| 50 fz_catch(ctx) | |
| 51 fz_rethrow(ctx); | |
| 52 | |
| 53 return doc; | |
| 54 } | |
| 55 | |
| 56 static const char *gz_extensions[] = | |
| 57 { | |
| 58 "gz", | |
| 59 NULL | |
| 60 }; | |
| 61 | |
| 62 static const char *gz_mimetypes[] = | |
| 63 { | |
| 64 "application/x-gzip-compressed", | |
| 65 NULL | |
| 66 }; | |
| 67 | |
| 68 static int | |
| 69 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) | |
| 70 { | |
| 71 int ret = 0; | |
| 72 uint8_t data[10]; | |
| 73 | |
| 74 if (state) | |
| 75 *state = NULL; | |
| 76 if (free_state) | |
| 77 *free_state = NULL; | |
| 78 | |
| 79 if (stream == NULL) | |
| 80 return 0; | |
| 81 | |
| 82 fz_try(ctx) | |
| 83 { | |
| 84 fz_seek(ctx, stream, 0, SEEK_SET); | |
| 85 /* 10 byte header */ | |
| 86 if (fz_read(ctx, stream, data, 10) != 10) | |
| 87 break; | |
| 88 /* We only actually check the first 3 bytes though. */ | |
| 89 if (data[0] == 0x1f && data[1] == 0x8b && data[2] == 0x08) | |
| 90 ret = 100; | |
| 91 } | |
| 92 fz_catch(ctx) | |
| 93 fz_rethrow(ctx); | |
| 94 | |
| 95 return ret; | |
| 96 } | |
| 97 | |
| 98 fz_document_handler gz_document_handler = | |
| 99 { | |
| 100 NULL, | |
| 101 gz_open_document, | |
| 102 gz_extensions, | |
| 103 gz_mimetypes, | |
| 104 gz_recognize_doc_content | |
| 105 }; |
