Skip to content

Commit

Permalink
Add a 'gz' document handler.
Browse files Browse the repository at this point in the history
We can now open pdf.gz or txt.gz etc.
  • Loading branch information
robinwatts committed Oct 30, 2023
1 parent 7d6ef47 commit 8334fbe
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions platform/win32/libmupdf.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@
<ClCompile Include="..\..\source\fitz\getopt.c" />
<ClCompile Include="..\..\source\fitz\glyph.c" />
<ClCompile Include="..\..\source\fitz\glyphbox.c" />
<ClCompile Include="..\..\source\fitz\gz-doc.c" />
<ClCompile Include="..\..\source\fitz\halftone.c" />
<ClCompile Include="..\..\source\fitz\harfbuzz.c" />
<ClCompile Include="..\..\source\fitz\hash.c" />
Expand Down
3 changes: 3 additions & 0 deletions platform/win32/libmupdf.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@
<ClCompile Include="..\..\source\fitz\encode-jpx.c">
<Filter>fitz</Filter>
</ClCompile>
<ClCompile Include="..\..\source\fitz\gz-doc.c">
<Filter>fitz</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\source\fitz\draw-imp.h">
Expand Down
2 changes: 2 additions & 0 deletions source/fitz/document-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
100 changes: 100 additions & 0 deletions source/fitz/gz-doc.c
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> 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
};

0 comments on commit 8334fbe

Please sign in to comment.