Coverage Report

Created: 2026-03-31 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mupdf/source/fitz/gz-doc.c
Line
Count
Source
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
0
#  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
0
{
32
0
  fz_stream *stm = fz_open_flated(ctx, ostm, 16 + MAX_WBITS);
33
0
  fz_buffer *buf = NULL;
34
0
  fz_document *doc = NULL;
35
36
0
  fz_var(buf);
37
0
  fz_var(doc);
38
39
0
  fz_try(ctx)
40
0
  {
41
0
    buf = fz_read_all(ctx, stm, 1024);
42
    /* No way to pass the magic onwards :( */
43
0
    doc = fz_open_document_with_buffer(ctx, "application/octet-stream", buf);
44
0
  }
45
0
  fz_always(ctx)
46
0
  {
47
0
    fz_drop_stream(ctx, stm);
48
0
    fz_drop_buffer(ctx, buf);
49
0
  }
50
0
  fz_catch(ctx)
51
0
    fz_rethrow(ctx);
52
53
0
  return doc;
54
0
}
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
12
{
71
12
  int ret = 0;
72
12
  uint8_t data[10];
73
74
12
  if (state)
75
12
    *state = NULL;
76
12
  if (free_state)
77
12
    *free_state = NULL;
78
79
12
  if (stream == NULL)
80
0
    return 0;
81
82
24
  fz_try(ctx)
83
24
  {
84
12
    fz_seek(ctx, stream, 0, SEEK_SET);
85
    /* 10 byte header */
86
12
    if (fz_read(ctx, stream, data, 10) != 10)
87
0
      break;
88
    /* We only actually check the first 3 bytes though. */
89
12
    if (data[0] == 0x1f && data[1] == 0x8b && data[2] == 0x08)
90
0
      ret = 100;
91
12
  }
92
24
  fz_catch(ctx)
93
0
    fz_rethrow(ctx);
94
95
12
  return ret;
96
12
}
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
};