Coverage Report

Created: 2025-01-11 06:55

/src/mupdf/source/fitz/compressed-buffer.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2004-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
/* 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
57.1k
{
37
57.1k
  if (fz_drop_imp(ctx, buf, &buf->refs))
38
52.6k
  {
39
52.6k
    if (buf->params.type == FZ_IMAGE_JBIG2)
40
87
      fz_drop_jbig2_globals(ctx, buf->params.u.jbig2.globals);
41
52.6k
    fz_drop_buffer(ctx, buf->buffer);
42
52.6k
    fz_free(ctx, buf);
43
52.6k
  }
44
57.1k
}
45
46
fz_compressed_buffer *
47
fz_new_compressed_buffer(fz_context *ctx)
48
52.6k
{
49
52.6k
  fz_compressed_buffer *cbuf = fz_malloc_struct(ctx, fz_compressed_buffer);
50
51
52.6k
  cbuf->refs = 1;
52
53
52.6k
  return cbuf;
54
52.6k
}
55
56
fz_stream *
57
fz_open_image_decomp_stream_from_buffer(fz_context *ctx, fz_compressed_buffer *buffer, int *l2factor)
58
42.4k
{
59
42.4k
  fz_stream *head, *tail;
60
61
42.4k
  tail = fz_open_buffer(ctx, buffer->buffer);
62
84.9k
  fz_try(ctx)
63
84.9k
    head = fz_open_image_decomp_stream(ctx, tail, &buffer->params, l2factor);
64
84.9k
  fz_always(ctx)
65
42.4k
    fz_drop_stream(ctx, tail);
66
42.4k
  fz_catch(ctx)
67
0
    fz_rethrow(ctx);
68
42.4k
  return head;
69
42.4k
}
70
71
fz_stream *
72
fz_open_image_decomp_stream(fz_context *ctx, fz_stream *tail, fz_compression_params *params, int *l2factor)
73
159k
{
74
159k
  fz_stream *head = NULL, *body = NULL;
75
159k
  int our_l2factor = 0;
76
77
159k
  fz_var(body);
78
79
319k
  fz_try(ctx)
80
319k
  {
81
159k
    switch (params->type)
82
159k
    {
83
63.1k
    default:
84
63.1k
      head = fz_keep_stream(ctx, tail);
85
63.1k
      break;
86
87
17.8k
    case FZ_IMAGE_FAX:
88
17.8k
      head = fz_open_faxd(ctx, tail,
89
17.8k
          params->u.fax.k,
90
17.8k
          params->u.fax.end_of_line,
91
17.8k
          params->u.fax.encoded_byte_align,
92
17.8k
          params->u.fax.columns,
93
17.8k
          params->u.fax.rows,
94
17.8k
          params->u.fax.end_of_block,
95
17.8k
          params->u.fax.black_is_1);
96
17.8k
      break;
97
98
1.43k
    case FZ_IMAGE_JPEG:
99
1.43k
      if (l2factor)
100
1.42k
      {
101
1.42k
        our_l2factor = *l2factor;
102
1.42k
        if (our_l2factor > 3)
103
69
          our_l2factor = 3;
104
1.42k
        *l2factor -= our_l2factor;
105
1.42k
      }
106
1.43k
      head = fz_open_dctd(ctx, tail, params->u.jpeg.color_transform, params->u.jpeg.invert_cmyk, our_l2factor, NULL);
107
1.43k
      break;
108
109
614
    case FZ_IMAGE_JBIG2:
110
614
      head = fz_open_jbig2d(ctx, tail, params->u.jbig2.globals, params->u.jbig2.embedded);
111
614
      break;
112
113
16
    case FZ_IMAGE_RLD:
114
16
      head = fz_open_rld(ctx, tail);
115
16
      break;
116
117
76.7k
    case FZ_IMAGE_FLATE:
118
76.7k
      head = fz_open_flated(ctx, tail, 15);
119
76.7k
      if (params->u.flate.predictor > 1)
120
1.36k
      {
121
1.36k
        body = head;
122
1.36k
        head = fz_open_predict(ctx, body,
123
1.36k
            params->u.flate.predictor,
124
1.36k
            params->u.flate.columns,
125
1.36k
            params->u.flate.colors,
126
1.36k
            params->u.flate.bpc);
127
1.36k
      }
128
76.7k
      break;
129
130
6
    case FZ_IMAGE_LZW:
131
6
      head = fz_open_lzwd(ctx, tail, params->u.lzw.early_change, 9, 0, 0);
132
6
      if (params->u.flate.predictor > 1)
133
0
      {
134
0
        body = head;
135
0
        head = fz_open_predict(ctx, body,
136
0
            params->u.lzw.predictor,
137
0
            params->u.lzw.columns,
138
0
            params->u.lzw.colors,
139
0
            params->u.lzw.bpc);
140
0
      }
141
6
      break;
142
159k
    }
143
159k
  }
144
319k
  fz_always(ctx)
145
159k
    fz_drop_stream(ctx, body);
146
159k
  fz_catch(ctx)
147
6
    fz_rethrow(ctx);
148
149
159k
  return head;
150
159k
}
151
152
fz_stream *
153
fz_open_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buffer)
154
291
{
155
291
  return fz_open_image_decomp_stream_from_buffer(ctx, buffer, NULL);
156
291
}
157
158
size_t
159
fz_compressed_buffer_size(fz_compressed_buffer *buffer)
160
2.89k
{
161
2.89k
  if (!buffer)
162
2.64k
    return 0;
163
258
  if (buffer->buffer)
164
258
    return (size_t)buffer->buffer->cap + sizeof(*buffer);
165
0
  return sizeof(*buffer);
166
258
}