Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
basicmodule.cpp
Go to the documentation of this file.
1// Copyright 2019 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fxcodec/basic/basicmodule.h"
6
7#include <stdint.h>
8
9#include <algorithm>
10#include <utility>
11
12#include "core/fxcodec/scanlinedecoder.h"
13#include "core/fxcrt/byteorder.h"
14#include "core/fxcrt/check.h"
15#include "core/fxcrt/data_vector.h"
16#include "core/fxcrt/fx_safe_types.h"
17#include "core/fxcrt/numerics/safe_conversions.h"
18#include "core/fxcrt/raw_span.h"
19#include "core/fxcrt/stl_util.h"
20
21namespace fxcodec {
22
23namespace {
24
25class RLScanlineDecoder final : public ScanlineDecoder {
26 public:
27 RLScanlineDecoder();
28 ~RLScanlineDecoder() override;
29
30 bool Create(pdfium::span<const uint8_t> src_buf,
31 int width,
32 int height,
33 int nComps,
34 int bpc);
35
36 // ScanlineDecoder:
37 bool Rewind() override;
38 pdfium::span<uint8_t> GetNextLine() override;
39 uint32_t GetSrcOffset() override;
40
41 private:
42 bool CheckDestSize();
43 void GetNextOperator();
44 void UpdateOperator(uint8_t used_bytes);
45
46 DataVector<uint8_t> m_Scanline;
47 pdfium::raw_span<const uint8_t> m_SrcBuf;
48 size_t m_dwLineBytes = 0;
49 size_t m_SrcOffset = 0;
50 bool m_bEOD = false;
51 uint8_t m_Operator = 0;
52};
53
54RLScanlineDecoder::RLScanlineDecoder() = default;
55
56RLScanlineDecoder::~RLScanlineDecoder() {
57 // Span in superclass can't outlive our buffer.
58 m_pLastScanline = pdfium::span<uint8_t>();
59}
60
61bool RLScanlineDecoder::CheckDestSize() {
62 size_t i = 0;
63 uint32_t old_size = 0;
64 uint32_t dest_size = 0;
65 while (i < m_SrcBuf.size()) {
66 if (m_SrcBuf[i] < 128) {
67 old_size = dest_size;
68 dest_size += m_SrcBuf[i] + 1;
69 if (dest_size < old_size) {
70 return false;
71 }
72 i += m_SrcBuf[i] + 2;
73 } else if (m_SrcBuf[i] > 128) {
74 old_size = dest_size;
75 dest_size += 257 - m_SrcBuf[i];
76 if (dest_size < old_size) {
77 return false;
78 }
79 i += 2;
80 } else {
81 break;
82 }
83 }
84 if (((uint32_t)m_OrigWidth * m_nComps * m_bpc * m_OrigHeight + 7) / 8 >
85 dest_size) {
86 return false;
87 }
88 return true;
89}
90
91bool RLScanlineDecoder::Create(pdfium::span<const uint8_t> src_buf,
92 int width,
93 int height,
94 int nComps,
95 int bpc) {
96 m_SrcBuf = src_buf;
97 m_OutputWidth = m_OrigWidth = width;
99 m_nComps = nComps;
100 m_bpc = bpc;
101 // Aligning the pitch to 4 bytes requires an integer overflow check.
102 FX_SAFE_UINT32 pitch = width;
103 pitch *= nComps;
104 pitch *= bpc;
105 pitch += 31;
106 pitch /= 32;
107 pitch *= 4;
108 if (!pitch.IsValid()) {
109 return false;
110 }
111 m_Pitch = pitch.ValueOrDie();
112 // Overflow should already have been checked before this is called.
113 m_dwLineBytes = (static_cast<uint32_t>(width) * nComps * bpc + 7) / 8;
114 m_Scanline.resize(m_Pitch);
115 return CheckDestSize();
116}
117
118bool RLScanlineDecoder::Rewind() {
119 fxcrt::Fill(m_Scanline, 0);
120 m_SrcOffset = 0;
121 m_bEOD = false;
122 m_Operator = 0;
123 return true;
124}
125
126pdfium::span<uint8_t> RLScanlineDecoder::GetNextLine() {
127 if (m_SrcOffset == 0) {
128 GetNextOperator();
129 } else if (m_bEOD) {
130 return pdfium::span<uint8_t>();
131 }
132 fxcrt::Fill(m_Scanline, 0);
133 uint32_t col_pos = 0;
134 bool eol = false;
135 auto scan_span = pdfium::make_span(m_Scanline);
136 while (m_SrcOffset < m_SrcBuf.size() && !eol) {
137 if (m_Operator < 128) {
138 uint32_t copy_len = m_Operator + 1;
139 if (col_pos + copy_len >= m_dwLineBytes) {
140 copy_len = pdfium::checked_cast<uint32_t>(m_dwLineBytes - col_pos);
141 eol = true;
142 }
143 if (copy_len >= m_SrcBuf.size() - m_SrcOffset) {
144 copy_len =
145 pdfium::checked_cast<uint32_t>(m_SrcBuf.size() - m_SrcOffset);
146 m_bEOD = true;
147 }
148 fxcrt::Copy(m_SrcBuf.subspan(m_SrcOffset, copy_len),
149 scan_span.subspan(col_pos));
150 col_pos += copy_len;
151 UpdateOperator((uint8_t)copy_len);
152 } else if (m_Operator > 128) {
153 int fill = 0;
154 if (m_SrcOffset < m_SrcBuf.size()) {
155 fill = m_SrcBuf[m_SrcOffset];
156 }
157 uint32_t duplicate_len = 257 - m_Operator;
158 if (col_pos + duplicate_len >= m_dwLineBytes) {
159 duplicate_len = pdfium::checked_cast<uint32_t>(m_dwLineBytes - col_pos);
160 eol = true;
161 }
162 fxcrt::Fill(scan_span.subspan(col_pos, duplicate_len), fill);
163 col_pos += duplicate_len;
164 UpdateOperator((uint8_t)duplicate_len);
165 } else {
166 m_bEOD = true;
167 break;
168 }
169 }
170 return m_Scanline;
171}
172
173uint32_t RLScanlineDecoder::GetSrcOffset() {
174 return pdfium::checked_cast<uint32_t>(m_SrcOffset);
175}
176
177void RLScanlineDecoder::GetNextOperator() {
178 if (m_SrcOffset >= m_SrcBuf.size()) {
179 m_Operator = 128;
180 return;
181 }
182 m_Operator = m_SrcBuf[m_SrcOffset];
183 m_SrcOffset++;
184}
185void RLScanlineDecoder::UpdateOperator(uint8_t used_bytes) {
186 if (used_bytes == 0) {
187 return;
188 }
189 if (m_Operator < 128) {
190 DCHECK((uint32_t)m_Operator + 1 >= used_bytes);
191 if (used_bytes == m_Operator + 1) {
192 m_SrcOffset += used_bytes;
193 GetNextOperator();
194 return;
195 }
196 m_Operator -= used_bytes;
197 m_SrcOffset += used_bytes;
198 if (m_SrcOffset >= m_SrcBuf.size()) {
199 m_Operator = 128;
200 }
201 return;
202 }
203 uint8_t count = 257 - m_Operator;
204 DCHECK((uint32_t)count >= used_bytes);
205 if (used_bytes == count) {
206 m_SrcOffset++;
207 GetNextOperator();
208 return;
209 }
210 count -= used_bytes;
211 m_Operator = 257 - count;
212}
213
214} // namespace
215
216// static
217std::unique_ptr<ScanlineDecoder> BasicModule::CreateRunLengthDecoder(
218 pdfium::span<const uint8_t> src_buf,
219 int width,
220 int height,
221 int nComps,
222 int bpc) {
223 auto pDecoder = std::make_unique<RLScanlineDecoder>();
224 if (!pDecoder->Create(src_buf, width, height, nComps, bpc))
225 return nullptr;
226
227 return pDecoder;
228}
229
230// static
231DataVector<uint8_t> BasicModule::RunLengthEncode(
232 pdfium::span<const uint8_t> src_span) {
233 if (src_span.empty())
234 return {};
235
236 // Handle edge case.
237 if (src_span.size() == 1)
238 return {0, src_span[0], 128};
239
240 // Worst case: 1 nonmatch, 2 match, 1 nonmatch, 2 match, etc. This becomes
241 // 4 output chars for every 3 input, plus up to 4 more for the 1-2 chars
242 // rounded off plus the terminating character.
243 FX_SAFE_SIZE_T estimated_size = src_span.size();
244 estimated_size += 2;
245 estimated_size /= 3;
246 estimated_size *= 4;
247 estimated_size += 1;
248 DataVector<uint8_t> result(estimated_size.ValueOrDie());
249
250 // Set up span and counts.
251 auto result_span = pdfium::make_span(result);
252 uint32_t run_start = 0;
253 uint32_t run_end = 1;
254 uint8_t x = src_span[run_start];
255 uint8_t y = src_span[run_end];
256 while (run_end < src_span.size()) {
257 size_t max_len = std::min<size_t>(128, src_span.size() - run_start);
258 while (x == y && (run_end - run_start < max_len - 1))
259 y = src_span[++run_end];
260
261 // Reached end with matched run. Update variables to expected values.
262 if (x == y) {
263 run_end++;
264 if (run_end < src_span.size())
265 y = src_span[run_end];
266 }
267 if (run_end - run_start > 1) { // Matched run but not at end of input.
268 result_span[0] = 257 - (run_end - run_start);
269 result_span[1] = x;
270 x = y;
271 run_start = run_end;
272 run_end++;
273 if (run_end < src_span.size())
274 y = src_span[run_end];
275 result_span = result_span.subspan(2);
276 continue;
277 }
278 // Mismatched run
279 while (x != y && run_end <= run_start + max_len) {
280 result_span[run_end - run_start] = x;
281 x = y;
282 run_end++;
283 if (run_end == src_span.size()) {
284 if (run_end <= run_start + max_len) {
285 result_span[run_end - run_start] = x;
286 run_end++;
287 }
288 break;
289 }
290 y = src_span[run_end];
291 }
292 result_span[0] = run_end - run_start - 2;
293 result_span = result_span.subspan(run_end - run_start);
294 run_start = run_end - 1;
295 }
296 if (run_start < src_span.size()) { // 1 leftover character
297 result_span[0] = 0;
298 result_span[1] = x;
299 result_span = result_span.subspan(2);
300 }
301 result_span[0] = 128;
302 size_t new_size = 1 + result.size() - result_span.size();
303 CHECK_LE(new_size, result.size());
304 result.resize(new_size);
305 return result;
306}
307
308// static
309DataVector<uint8_t> BasicModule::A85Encode(
310 pdfium::span<const uint8_t> src_span) {
311 DataVector<uint8_t> result;
312 if (src_span.empty())
313 return result;
314
315 // Worst case: 5 output for each 4 input (plus up to 4 from leftover), plus
316 // 2 character new lines each 75 output chars plus 2 termination chars. May
317 // have fewer if there are special "z" chars.
318 FX_SAFE_SIZE_T estimated_size = src_span.size();
319 estimated_size /= 4;
320 estimated_size *= 5;
321 estimated_size += 4;
322 estimated_size += src_span.size() / 30;
323 estimated_size += 2;
324 result.resize(estimated_size.ValueOrDie());
325
326 // Set up span and counts.
327 auto result_span = pdfium::make_span(result);
328 uint32_t pos = 0;
329 uint32_t line_length = 0;
330 while (src_span.size() >= 4 && pos < src_span.size() - 3) {
331 auto val_span = src_span.subspan(pos, 4);
332 uint32_t val = fxcrt::GetUInt32MSBFirst(val_span);
333 pos += 4;
334 if (val == 0) { // All zero special case
335 result_span[0] = 'z';
336 result_span = result_span.subspan(1);
337 line_length++;
338 } else { // Compute base 85 characters and add 33.
339 for (int i = 4; i >= 0; i--) {
340 result_span[i] = (val % 85) + 33;
341 val /= 85;
342 }
343 result_span = result_span.subspan(5);
344 line_length += 5;
345 }
346 if (line_length >= 75) { // Add a return.
347 result_span[0] = '\r';
348 result_span[1] = '\n';
349 result_span = result_span.subspan(2);
350 line_length = 0;
351 }
352 }
353 if (pos < src_span.size()) { // Leftover bytes
354 uint32_t val = 0;
355 int count = 0;
356 while (pos < src_span.size()) {
357 val += (uint32_t)(src_span[pos]) << (8 * (3 - count));
358 count++;
359 pos++;
360 }
361 for (int i = 4; i >= 0; i--) {
362 if (i <= count)
363 result_span[i] = (val % 85) + 33;
364 val /= 85;
365 }
366 result_span = result_span.subspan(count + 1);
367 }
368
369 // Terminating characters.
370 result_span[0] = '~';
371 result_span[1] = '>';
372 size_t new_size = 2 + result.size() - result_span.size();
373 CHECK_LE(new_size, result.size());
374 result.resize(new_size);
375 return result;
376}
377
378} // namespace fxcodec
#define DCHECK
Definition check.h:33
#define CHECK_LE(x, y)
Definition check_op.h:14
pdfium::CheckedNumeric< uint32_t > FX_SAFE_UINT32