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