5#include "core/fxcodec/basic/basicmodule.h"
12#include "core/fxcodec/scanlinedecoder.h"
13#include "core/fxcrt/data_vector.h"
14#include "core/fxcrt/fx_memory_wrappers.h"
15#include "core/fxcrt/fx_safe_types.h"
16#include "core/fxcrt/fx_system.h"
17#include "core/fxcrt/span_util.h"
18#include "third_party/base/check.h"
19#include "third_party/base/numerics/safe_conversions.h"
28 ~RLScanlineDecoder()
override;
30 bool Create(pdfium::span<
const uint8_t> src_buf,
37 bool Rewind()
override;
38 pdfium::span<uint8_t> GetNextLine()
override;
39 uint32_t GetSrcOffset()
override;
43 void GetNextOperator();
44 void UpdateOperator(uint8_t used_bytes);
46 DataVector<uint8_t> m_Scanline;
47 pdfium::span<
const uint8_t> m_SrcBuf;
48 size_t m_dwLineBytes = 0;
49 size_t m_SrcOffset = 0;
51 uint8_t m_Operator = 0;
54RLScanlineDecoder::RLScanlineDecoder() =
default;
56RLScanlineDecoder::~RLScanlineDecoder() {
58 m_pLastScanline = pdfium::span<uint8_t>();
61bool RLScanlineDecoder::CheckDestSize() {
63 uint32_t old_size = 0;
64 uint32_t dest_size = 0;
65 while (i < m_SrcBuf.size()) {
66 if (m_SrcBuf[i] < 128) {
68 dest_size += m_SrcBuf[i] + 1;
69 if (dest_size < old_size) {
73 }
else if (m_SrcBuf[i] > 128) {
75 dest_size += 257 - m_SrcBuf[i];
76 if (dest_size < old_size) {
91bool RLScanlineDecoder::Create(pdfium::span<
const uint8_t> src_buf,
102 FX_SAFE_UINT32 pitch = width;
108 if (!pitch.IsValid()) {
113 m_dwLineBytes = (
static_cast<uint32_t>(width) * nComps * bpc + 7) / 8;
114 m_Scanline.resize(m_Pitch);
115 return CheckDestSize();
118bool RLScanlineDecoder::Rewind() {
119 fxcrt::spanclr(pdfium::make_span(m_Scanline));
126pdfium::span<uint8_t> RLScanlineDecoder::GetNextLine() {
127 if (m_SrcOffset == 0) {
130 return pdfium::span<uint8_t>();
132 uint32_t col_pos = 0;
134 auto scan_span = pdfium::make_span(m_Scanline);
135 fxcrt::spanclr(scan_span);
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) {
141 pdfium::base::checked_cast<uint32_t>(m_dwLineBytes - col_pos);
144 if (copy_len >= m_SrcBuf.size() - m_SrcOffset) {
146 pdfium::base::checked_cast<uint32_t>(m_SrcBuf.size() - m_SrcOffset);
149 auto copy_span = m_SrcBuf.subspan(m_SrcOffset, copy_len);
150 fxcrt::spancpy(scan_span.subspan(col_pos), copy_span);
152 UpdateOperator((uint8_t)copy_len);
153 }
else if (m_Operator > 128) {
155 if (m_SrcOffset < m_SrcBuf.size()) {
156 fill = m_SrcBuf[m_SrcOffset];
158 uint32_t duplicate_len = 257 - m_Operator;
159 if (col_pos + duplicate_len >= m_dwLineBytes) {
161 pdfium::base::checked_cast<uint32_t>(m_dwLineBytes - col_pos);
164 fxcrt::spanset(scan_span.subspan(col_pos, duplicate_len), fill);
165 col_pos += duplicate_len;
166 UpdateOperator((uint8_t)duplicate_len);
175uint32_t RLScanlineDecoder::GetSrcOffset() {
176 return pdfium::base::checked_cast<uint32_t>(m_SrcOffset);
179void RLScanlineDecoder::GetNextOperator() {
180 if (m_SrcOffset >= m_SrcBuf.size()) {
184 m_Operator = m_SrcBuf[m_SrcOffset];
187void RLScanlineDecoder::UpdateOperator(uint8_t used_bytes) {
188 if (used_bytes == 0) {
191 if (m_Operator < 128) {
192 DCHECK((uint32_t)m_Operator + 1 >= used_bytes);
193 if (used_bytes == m_Operator + 1) {
194 m_SrcOffset += used_bytes;
198 m_Operator -= used_bytes;
199 m_SrcOffset += used_bytes;
200 if (m_SrcOffset >= m_SrcBuf.size()) {
205 uint8_t count = 257 - m_Operator;
206 DCHECK((uint32_t)count >= used_bytes);
207 if (used_bytes == count) {
213 m_Operator = 257 - count;
219std::unique_ptr<ScanlineDecoder>
BasicModule::CreateRunLengthDecoder(
220 pdfium::span<
const uint8_t> src_buf,
225 auto pDecoder =
std::make_unique<RLScanlineDecoder>();
226 if (!pDecoder->Create(src_buf, width, height, nComps, bpc))
229 return std::move(pDecoder);
234 pdfium::span<
const uint8_t> src_span) {
235 if (src_span.empty())
239 if (src_span.size() == 1)
240 return {0, src_span[0], 128};
245 FX_SAFE_SIZE_T estimated_size = src_span.size();
250 DataVector<uint8_t> result(estimated_size.ValueOrDie());
253 auto result_span = pdfium::make_span(result);
254 uint32_t run_start = 0;
255 uint32_t run_end = 1;
256 uint8_t x = src_span[run_start];
257 uint8_t y = src_span[run_end];
258 while (run_end < src_span.size()) {
259 size_t max_len = std::min<size_t>(128, src_span.size() - run_start);
260 while (x == y && (run_end - run_start < max_len - 1))
261 y = src_span[++run_end];
266 if (run_end < src_span.size())
267 y = src_span[run_end];
269 if (run_end - run_start > 1) {
270 result_span[0] = 257 - (run_end - run_start);
275 if (run_end < src_span.size())
276 y = src_span[run_end];
277 result_span = result_span.subspan(2);
281 while (x != y && run_end <= run_start + max_len) {
282 result_span[run_end - run_start] = x;
285 if (run_end == src_span.size()) {
286 if (run_end <= run_start + max_len) {
287 result_span[run_end - run_start] = x;
292 y = src_span[run_end];
294 result_span[0] = run_end - run_start - 2;
295 result_span = result_span.subspan(run_end - run_start);
296 run_start = run_end - 1;
298 if (run_start < src_span.size()) {
301 result_span = result_span.subspan(2);
303 result_span[0] = 128;
304 size_t new_size = 1 + result.size() - result_span.size();
305 CHECK_LE(new_size, result.size());
306 result.resize(new_size);
312 pdfium::span<
const uint8_t> src_span) {
313 DataVector<uint8_t> result;
314 if (src_span.empty())
320 FX_SAFE_SIZE_T estimated_size = src_span.size();
324 estimated_size += src_span.size() / 30;
326 result.resize(estimated_size.ValueOrDie());
329 auto result_span = pdfium::make_span(result);
331 uint32_t line_length = 0;
332 while (src_span.size() >= 4 && pos < src_span.size() - 3) {
333 auto val_span = src_span.subspan(pos, 4);
334 uint32_t val = FXSYS_UINT32_GET_MSBFIRST(val_span);
337 result_span[0] =
'z';
338 result_span = result_span.subspan(1);
341 for (
int i = 4; i >= 0; i--) {
342 result_span[i] = (val % 85) + 33;
345 result_span = result_span.subspan(5);
348 if (line_length >= 75) {
349 result_span[0] =
'\r';
350 result_span[1] =
'\n';
351 result_span = result_span.subspan(2);
355 if (pos < src_span.size()) {
358 while (pos < src_span.size()) {
359 val += (uint32_t)(src_span[pos]) << (8 * (3 - count));
363 for (
int i = 4; i >= 0; i--) {
365 result_span[i] = (val % 85) + 33;
368 result_span = result_span.subspan(count + 1);
372 result_span[0] =
'~';
373 result_span[1] =
'>';
374 size_t new_size = 2 + result.size() - result_span.size();
375 CHECK_LE(new_size, result.size());
376 result.resize(new_size);