Coverage Report

Created: 2026-05-16 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mupdf/source/fitz/compressed-buffer.c
Line
Count
Source
1
// Copyright (C) 2004-2025 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
/* This code needs to be kept out of stm_buffer.c to avoid it being
26
 * pulled into cmapdump.c */
27
28
fz_compressed_buffer *
29
fz_keep_compressed_buffer(fz_context *ctx, fz_compressed_buffer *cbuf)
30
0
{
31
0
  return (fz_compressed_buffer *)fz_keep_imp(ctx, cbuf, &cbuf->refs);
32
0
}
33
34
void
35
fz_drop_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buf)
36
4.89k
{
37
4.89k
  if (fz_drop_imp(ctx, buf, &buf->refs))
38
3.03k
  {
39
3.03k
    if (buf->params.type == FZ_IMAGE_JBIG2)
40
0
      fz_drop_jbig2_globals(ctx, buf->params.u.jbig2.globals);
41
3.03k
    fz_drop_buffer(ctx, buf->buffer);
42
3.03k
    fz_free(ctx, buf);
43
3.03k
  }
44
4.89k
}
45
46
fz_compressed_buffer *
47
fz_new_compressed_buffer(fz_context *ctx)
48
3.03k
{
49
3.03k
  fz_compressed_buffer *cbuf = fz_malloc_struct(ctx, fz_compressed_buffer);
50
51
3.03k
  cbuf->refs = 1;
52
53
3.03k
  return cbuf;
54
3.03k
}
55
56
fz_stream *
57
fz_open_image_decomp_stream_from_buffer(fz_context *ctx, fz_compressed_buffer *buffer, int *l2factor)
58
3.00k
{
59
3.00k
  fz_stream *head, *tail;
60
61
3.00k
  tail = fz_open_buffer(ctx, buffer->buffer);
62
6.01k
  fz_try(ctx)
63
6.01k
    head = fz_open_image_decomp_stream(ctx, tail, &buffer->params, l2factor);
64
6.01k
  fz_always(ctx)
65
3.00k
    fz_drop_stream(ctx, tail);
66
3.00k
  fz_catch(ctx)
67
0
    fz_rethrow(ctx);
68
3.00k
  return head;
69
3.00k
}
70
71
fz_stream *
72
fz_open_image_decomp_stream(fz_context *ctx, fz_stream *tail, fz_compression_params *params, int *l2factor)
73
13.8k
{
74
13.8k
  fz_stream *head = NULL, *body = NULL;
75
13.8k
  int our_l2factor = 0;
76
77
13.8k
  fz_var(body);
78
79
27.6k
  fz_try(ctx)
80
27.6k
  {
81
13.8k
    switch (params->type)
82
13.8k
    {
83
106
    default:
84
106
      head = fz_keep_stream(ctx, tail);
85
106
      break;
86
87
0
    case FZ_IMAGE_FAX:
88
0
      head = fz_open_faxd(ctx, tail,
89
0
          params->u.fax.k,
90
0
          params->u.fax.end_of_line,
91
0
          params->u.fax.encoded_byte_align,
92
0
          params->u.fax.columns,
93
0
          params->u.fax.rows,
94
0
          params->u.fax.end_of_block,
95
0
          params->u.fax.black_is_1);
96
0
      break;
97
98
16
    case FZ_IMAGE_JPEG:
99
16
      if (l2factor)
100
16
      {
101
16
        our_l2factor = *l2factor;
102
16
        if (our_l2factor > 3)
103
0
          our_l2factor = 3;
104
16
        *l2factor -= our_l2factor;
105
16
      }
106
16
      head = fz_open_dctd(ctx, tail, params->u.jpeg.color_transform, params->u.jpeg.invert_cmyk, our_l2factor, NULL);
107
16
      break;
108
109
0
    case FZ_IMAGE_JBIG2:
110
0
      head = fz_open_jbig2d(ctx, tail, params->u.jbig2.globals, params->u.jbig2.embedded);
111
0
      break;
112
113
0
    case FZ_IMAGE_RLD:
114
0
      head = fz_open_rld(ctx, tail);
115
0
      break;
116
117
13.7k
    case FZ_IMAGE_FLATE:
118
13.7k
      head = fz_open_flated(ctx, tail, 15);
119
13.7k
      if (params->u.flate.predictor > 1)
120
25
      {
121
25
        body = head;
122
25
        head = fz_open_predict(ctx, body,
123
25
            params->u.flate.predictor,
124
25
            params->u.flate.columns,
125
25
            params->u.flate.colors,
126
25
            params->u.flate.bpc);
127
25
      }
128
13.7k
      break;
129
130
0
    case FZ_IMAGE_BROTLI:
131
0
      head = fz_open_brotlid(ctx, tail);
132
0
      if (params->u.brotli.predictor > 1)
133
0
      {
134
0
        body = head;
135
0
        head = fz_open_predict(ctx, body,
136
0
            params->u.brotli.predictor,
137
0
            params->u.brotli.columns,
138
0
            params->u.brotli.colors,
139
0
            params->u.brotli.bpc);
140
0
      }
141
0
      break;
142
143
0
    case FZ_IMAGE_LZW:
144
0
      head = fz_open_lzwd(ctx, tail, params->u.lzw.early_change, 9, 0, 0);
145
0
      if (params->u.flate.predictor > 1)
146
0
      {
147
0
        body = head;
148
0
        head = fz_open_predict(ctx, body,
149
0
            params->u.lzw.predictor,
150
0
            params->u.lzw.columns,
151
0
            params->u.lzw.colors,
152
0
            params->u.lzw.bpc);
153
0
      }
154
0
      break;
155
13.8k
    }
156
13.8k
  }
157
27.6k
  fz_always(ctx)
158
13.8k
    fz_drop_stream(ctx, body);
159
13.8k
  fz_catch(ctx)
160
0
    fz_rethrow(ctx);
161
162
13.8k
  return head;
163
13.8k
}
164
165
fz_stream *
166
fz_open_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buffer)
167
0
{
168
0
  return fz_open_image_decomp_stream_from_buffer(ctx, buffer, NULL);
169
0
}
170
171
size_t
172
fz_compressed_buffer_size(fz_compressed_buffer *buffer)
173
1.56k
{
174
1.56k
  if (!buffer)
175
1.56k
    return 0;
176
0
  if (buffer->buffer)
177
0
    return (size_t)buffer->buffer->cap + sizeof(*buffer);
178
0
  return sizeof(*buffer);
179
0
}