diff --git a/platform/win32/libmupdf.vcxproj b/platform/win32/libmupdf.vcxproj index 9a7f7ca5bb..3ec62f7bab 100644 --- a/platform/win32/libmupdf.vcxproj +++ b/platform/win32/libmupdf.vcxproj @@ -512,6 +512,7 @@ + diff --git a/platform/win32/libmupdf.vcxproj.filters b/platform/win32/libmupdf.vcxproj.filters index 69ab8c5c85..35c74195fb 100644 --- a/platform/win32/libmupdf.vcxproj.filters +++ b/platform/win32/libmupdf.vcxproj.filters @@ -642,6 +642,9 @@ fitz + + fitz + diff --git a/source/fitz/document-all.c b/source/fitz/document-all.c index ce4bec6ae3..5ae52d977f 100644 --- a/source/fitz/document-all.c +++ b/source/fitz/document-all.c @@ -34,6 +34,7 @@ extern fz_document_handler mobi_document_handler; extern fz_document_handler epub_document_handler; extern fz_document_handler txt_document_handler; extern fz_document_handler office_document_handler; +extern fz_document_handler gz_document_handler; void fz_register_document_handlers(fz_context *ctx) { @@ -63,4 +64,5 @@ void fz_register_document_handlers(fz_context *ctx) #if FZ_ENABLE_EPUB fz_register_document_handler(ctx, &epub_document_handler); #endif /* FZ_ENABLE_EPUB */ + fz_register_document_handler(ctx, &gz_document_handler); } diff --git a/source/fitz/gz-doc.c b/source/fitz/gz-doc.c new file mode 100644 index 0000000000..2f00cbe100 --- /dev/null +++ b/source/fitz/gz-doc.c @@ -0,0 +1,100 @@ +// Copyright (C) 2023 Artifex Software, Inc. +// +// This file is part of MuPDF. +// +// MuPDF is free software: you can redistribute it and/or modify it under the +// terms of the GNU Affero General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. +// +// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +// details. +// +// You should have received a copy of the GNU Affero General Public License +// along with MuPDF. If not, see +// +// Alternative licensing terms are available from the licensor. +// For commercial licensing, see or contact +// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, +// CA 94129, USA, for further information. + +#include "mupdf/fitz.h" + +#ifndef MAX_WBITS +# define MAX_WBITS 15 /* 32K LZ77 window */ +#endif + +static fz_document * +gz_open_document_with_stream(fz_context *ctx, fz_stream *ostm) +{ + fz_stream *stm = fz_open_flated(ctx, ostm, 16 + MAX_WBITS); + fz_buffer *buf = NULL; + fz_document *doc = NULL; + + fz_var(buf); + fz_var(doc); + + fz_try(ctx) + { + buf = fz_read_all(ctx, stm, 1024); + /* No way to pass the magic onwards :( */ + doc = fz_open_document_with_buffer(ctx, "application/octet-stream", buf); + } + fz_always(ctx) + { + fz_drop_stream(ctx, stm); + fz_drop_buffer(ctx, buf); + } + fz_catch(ctx) + fz_rethrow(ctx); + + return doc; +} + +static const char *gz_extensions[] = +{ + "gz", + NULL +}; + +static const char *gz_mimetypes[] = +{ + "application/x-gzip-compressed", + NULL +}; + +static int +gz_recognize_doc_content(fz_context *ctx, fz_stream *stream) +{ + int ret = 0; + uint8_t data[10]; + + fz_try(ctx) + { + fz_seek(ctx, stream, 0, SEEK_SET); + /* 10 byte header */ + if (fz_read(ctx, stream, data, 10) != 10) + break; + /* We only actually check the first 3 bytes though. */ + if (data[0] == 0x1f && data[1] == 0x8b && data[2] == 0x08) + ret = 100; + } + fz_catch(ctx) + fz_rethrow(ctx); + + return ret; +} + +fz_document_handler gz_document_handler = +{ + NULL, + NULL, + gz_open_document_with_stream, + gz_extensions, + gz_mimetypes, + NULL, /* open_accel */ + NULL, /* open_accel with stream */ + gz_recognize_doc_content +};