summaryrefslogtreecommitdiff
path: root/core/fpdfapi
diff options
context:
space:
mode:
Diffstat (limited to 'core/fpdfapi')
-rw-r--r--core/fpdfapi/edit/cpdf_flateencoder.cpp2
-rw-r--r--core/fpdfapi/page/cpdf_streamparser.cpp23
-rw-r--r--core/fpdfapi/parser/cpdf_stream_acc.cpp2
-rw-r--r--core/fpdfapi/parser/fpdf_parser_decode.cpp135
-rw-r--r--core/fpdfapi/parser/fpdf_parser_decode.h40
-rw-r--r--core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp4
-rw-r--r--core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp4
7 files changed, 98 insertions, 112 deletions
diff --git a/core/fpdfapi/edit/cpdf_flateencoder.cpp b/core/fpdfapi/edit/cpdf_flateencoder.cpp
index 573c141ff2..a42b256ed2 100644
--- a/core/fpdfapi/edit/cpdf_flateencoder.cpp
+++ b/core/fpdfapi/edit/cpdf_flateencoder.cpp
@@ -40,7 +40,7 @@ CPDF_FlateEncoder::CPDF_FlateEncoder(const CPDF_Stream* pStream,
// TODO(thestig): Move to Init() and check return value.
uint8_t* buffer = nullptr;
- ::FlateEncode(m_pAcc->GetData(), m_pAcc->GetSize(), &buffer, &m_dwSize);
+ ::FlateEncode(m_pAcc->GetSpan(), &buffer, &m_dwSize);
m_pData = std::unique_ptr<uint8_t, FxFreeDeleter>(buffer);
m_pClonedDict = ToDictionary(pStream->GetDict()->Clone());
diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp
index 3d086b37ff..0d45ed0323 100644
--- a/core/fpdfapi/page/cpdf_streamparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamparser.cpp
@@ -61,8 +61,7 @@ uint32_t DecodeAllScanlines(std::unique_ptr<CCodec_ScanlineDecoder> pDecoder,
return pDecoder->GetSrcOffset();
}
-uint32_t DecodeInlineStream(const uint8_t* src_buf,
- uint32_t limit,
+uint32_t DecodeInlineStream(pdfium::span<const uint8_t> src_span,
int width,
int height,
const ByteString& decoder,
@@ -71,30 +70,29 @@ uint32_t DecodeInlineStream(const uint8_t* src_buf,
uint32_t* dest_size) {
if (decoder == "CCITTFaxDecode" || decoder == "CCF") {
std::unique_ptr<CCodec_ScanlineDecoder> pDecoder =
- CreateFaxDecoder({src_buf, limit}, width, height, pParam);
+ CreateFaxDecoder(src_span, width, height, pParam);
return DecodeAllScanlines(std::move(pDecoder), dest_buf, dest_size);
}
if (decoder == "ASCII85Decode" || decoder == "A85")
- return A85Decode(src_buf, limit, dest_buf, dest_size);
+ return A85Decode(src_span, dest_buf, dest_size);
if (decoder == "ASCIIHexDecode" || decoder == "AHx")
- return HexDecode(src_buf, limit, dest_buf, dest_size);
+ return HexDecode(src_span, dest_buf, dest_size);
if (decoder == "FlateDecode" || decoder == "Fl") {
- return FlateOrLZWDecode(false, src_buf, limit, pParam, *dest_size, dest_buf,
+ return FlateOrLZWDecode(false, src_span, pParam, *dest_size, dest_buf,
dest_size);
}
if (decoder == "LZWDecode" || decoder == "LZW") {
- return FlateOrLZWDecode(true, src_buf, limit, pParam, 0, dest_buf,
- dest_size);
+ return FlateOrLZWDecode(true, src_span, pParam, 0, dest_buf, dest_size);
}
if (decoder == "DCTDecode" || decoder == "DCT") {
std::unique_ptr<CCodec_ScanlineDecoder> pDecoder =
CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder(
- {src_buf, limit}, width, height, 0,
+ src_span, width, height, 0,
!pParam || pParam->GetIntegerFor("ColorTransform", 1));
return DecodeAllScanlines(std::move(pDecoder), dest_buf, dest_size);
}
if (decoder == "RunLengthDecode" || decoder == "RL")
- return RunLengthDecode(src_buf, limit, dest_buf, dest_size);
+ return RunLengthDecode(src_span, dest_buf, dest_size);
*dest_size = 0;
*dest_buf = 0;
return 0xFFFFFFFF;
@@ -185,9 +183,8 @@ std::unique_ptr<CPDF_Stream> CPDF_StreamParser::ReadInlineStream(
} else {
uint8_t* pIgnore = nullptr;
uint32_t dwDestSize = OrigSize;
- dwStreamSize =
- DecodeInlineStream(&m_pBuf[m_Pos], m_pBuf.size() - m_Pos, width, height,
- Decoder, pParam, &pIgnore, &dwDestSize);
+ dwStreamSize = DecodeInlineStream(m_pBuf.subspan(m_Pos), width, height,
+ Decoder, pParam, &pIgnore, &dwDestSize);
FX_Free(pIgnore);
if (static_cast<int>(dwStreamSize) < 0)
return nullptr;
diff --git a/core/fpdfapi/parser/cpdf_stream_acc.cpp b/core/fpdfapi/parser/cpdf_stream_acc.cpp
index 18d0b35107..1083e3177e 100644
--- a/core/fpdfapi/parser/cpdf_stream_acc.cpp
+++ b/core/fpdfapi/parser/cpdf_stream_acc.cpp
@@ -47,7 +47,7 @@ void CPDF_StreamAcc::LoadAllData(bool bRawAccess,
if (bProcessRawData) {
m_pData = pSrcData;
m_dwSize = dwSrcSize;
- } else if (!PDF_DataDecode(pSrcData, dwSrcSize, m_pStream->GetDict(),
+ } else if (!PDF_DataDecode({pSrcData, dwSrcSize}, m_pStream->GetDict(),
estimated_size, bImageAcc, &m_pData, &m_dwSize,
&m_ImageDecoder, &m_pImageParam)) {
m_pData = pSrcData;
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp
index 8143517029..de93acf938 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -83,20 +83,19 @@ const uint16_t PDFDocEncoding[256] = {
0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb,
0x00fc, 0x00fd, 0x00fe, 0x00ff};
-uint32_t A85Decode(const uint8_t* src_buf,
- uint32_t src_size,
+uint32_t A85Decode(pdfium::span<const uint8_t> src_span,
uint8_t** dest_buf,
uint32_t* dest_size) {
*dest_size = 0;
*dest_buf = nullptr;
- if (src_size == 0)
+ if (src_span.empty())
return 0;
// Count legal characters and zeros.
uint32_t zcount = 0;
uint32_t pos = 0;
- while (pos < src_size) {
- uint8_t ch = src_buf[pos];
+ while (pos < src_span.size()) {
+ uint8_t ch = src_span[pos];
if (ch == 'z') {
zcount++;
} else if ((ch < '!' || ch > 'u') && !PDFCharIsLineEnding(ch) &&
@@ -119,8 +118,8 @@ uint32_t A85Decode(const uint8_t* src_buf,
size_t state = 0;
uint32_t res = 0;
pos = 0;
- while (pos < src_size) {
- uint8_t ch = src_buf[pos++];
+ while (pos < src_span.size()) {
+ uint8_t ch = src_span[pos++];
if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
continue;
@@ -155,30 +154,29 @@ uint32_t A85Decode(const uint8_t* src_buf,
for (size_t i = 0; i < state - 1; ++i)
(*dest_buf)[(*dest_size)++] = static_cast<uint8_t>(res >> (3 - i) * 8);
}
- if (pos < src_size && src_buf[pos] == '>')
+ if (pos < src_span.size() && src_span[pos] == '>')
++pos;
return pos;
}
-uint32_t HexDecode(const uint8_t* src_buf,
- uint32_t src_size,
+uint32_t HexDecode(pdfium::span<const uint8_t> src_span,
uint8_t** dest_buf,
uint32_t* dest_size) {
*dest_size = 0;
- if (src_size == 0) {
+ if (src_span.empty()) {
*dest_buf = nullptr;
return 0;
}
uint32_t i = 0;
// Find the end of data.
- while (i < src_size && src_buf[i] != '>')
+ while (i < src_span.size() && src_span[i] != '>')
++i;
*dest_buf = FX_Alloc(uint8_t, i / 2 + 1);
bool bFirst = true;
- for (i = 0; i < src_size; ++i) {
- uint8_t ch = src_buf[i];
+ for (i = 0; i < src_span.size(); ++i) {
+ uint8_t ch = src_span[i];
if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
continue;
@@ -201,24 +199,23 @@ uint32_t HexDecode(const uint8_t* src_buf,
return i;
}
-uint32_t RunLengthDecode(const uint8_t* src_buf,
- uint32_t src_size,
+uint32_t RunLengthDecode(pdfium::span<const uint8_t> src_span,
uint8_t** dest_buf,
uint32_t* dest_size) {
- uint32_t i = 0;
+ size_t i = 0;
*dest_size = 0;
- while (i < src_size) {
- if (src_buf[i] == 128)
+ while (i < src_span.size()) {
+ if (src_span[i] == 128)
break;
uint32_t old = *dest_size;
- if (src_buf[i] < 128) {
- *dest_size += src_buf[i] + 1;
+ if (src_span[i] < 128) {
+ *dest_size += src_span[i] + 1;
if (*dest_size < old)
return FX_INVALID_OFFSET;
- i += src_buf[i] + 2;
+ i += src_span[i] + 2;
} else {
- *dest_size += 257 - src_buf[i];
+ *dest_size += 257 - src_span[i];
if (*dest_size < old)
return FX_INVALID_OFFSET;
i += 2;
@@ -230,31 +227,31 @@ uint32_t RunLengthDecode(const uint8_t* src_buf,
*dest_buf = FX_Alloc(uint8_t, *dest_size);
i = 0;
int dest_count = 0;
- while (i < src_size) {
- if (src_buf[i] == 128)
+ while (i < src_span.size()) {
+ if (src_span[i] == 128)
break;
- if (src_buf[i] < 128) {
- uint32_t copy_len = src_buf[i] + 1;
- uint32_t buf_left = src_size - i - 1;
+ if (src_span[i] < 128) {
+ uint32_t copy_len = src_span[i] + 1;
+ uint32_t buf_left = src_span.size() - i - 1;
if (buf_left < copy_len) {
uint32_t delta = copy_len - buf_left;
copy_len = buf_left;
memset(*dest_buf + dest_count + copy_len, '\0', delta);
}
- memcpy(*dest_buf + dest_count, src_buf + i + 1, copy_len);
- dest_count += src_buf[i] + 1;
- i += src_buf[i] + 2;
+ memcpy(*dest_buf + dest_count, &src_span[i + 1], copy_len);
+ dest_count += src_span[i] + 1;
+ i += src_span[i] + 2;
} else {
int fill = 0;
- if (i < src_size - 1)
- fill = src_buf[i + 1];
- memset(*dest_buf + dest_count, fill, 257 - src_buf[i]);
- dest_count += 257 - src_buf[i];
+ if (i < src_span.size() - 1)
+ fill = src_span[i + 1];
+ memset(*dest_buf + dest_count, fill, 257 - src_span[i]);
+ dest_count += 257 - src_span[i];
i += 2;
}
}
- return std::min(i + 1, src_size);
+ return std::min(i + 1, src_span.size());
}
std::unique_ptr<CCodec_ScanlineDecoder> CreateFaxDecoder(
@@ -308,8 +305,7 @@ std::unique_ptr<CCodec_ScanlineDecoder> CreateFlateDecoder(
}
uint32_t FlateOrLZWDecode(bool bLZW,
- const uint8_t* src_buf,
- uint32_t src_size,
+ pdfium::span<const uint8_t> src_span,
const CPDF_Dictionary* pParams,
uint32_t estimated_size,
uint8_t** dest_buf,
@@ -329,12 +325,11 @@ uint32_t FlateOrLZWDecode(bool bLZW,
return FX_INVALID_OFFSET;
}
return CPDF_ModuleMgr::Get()->GetFlateModule()->FlateOrLZWDecode(
- bLZW, src_buf, src_size, bEarlyChange, predictor, Colors,
- BitsPerComponent, Columns, estimated_size, dest_buf, dest_size);
+ bLZW, src_span, bEarlyChange, predictor, Colors, BitsPerComponent,
+ Columns, estimated_size, dest_buf, dest_size);
}
-bool PDF_DataDecode(const uint8_t* src_buf,
- uint32_t src_size,
+bool PDF_DataDecode(pdfium::span<const uint8_t> src_span,
const CPDF_Dictionary* pDict,
uint32_t last_estimated_size,
bool bImageAcc,
@@ -362,8 +357,8 @@ bool PDF_DataDecode(const uint8_t* src_buf,
DecoderArray.push_back(
{pDecoder->GetString(), pParams ? pParams->GetDict() : nullptr});
}
- uint8_t* last_buf = const_cast<uint8_t*>(src_buf);
- uint32_t last_size = src_size;
+ pdfium::span<uint8_t> last_span(const_cast<uint8_t*>(src_span.data()),
+ src_span.size());
size_t nSize = DecoderArray.size();
for (size_t i = 0; i < nSize; ++i) {
int estimated_size = i == nSize - 1 ? last_estimated_size : 0;
@@ -377,29 +372,29 @@ bool PDF_DataDecode(const uint8_t* src_buf,
if (decoder == "FlateDecode" || decoder == "Fl") {
if (bImageAcc && i == nSize - 1) {
*ImageEncoding = "FlateDecode";
- *dest_buf = last_buf;
- *dest_size = last_size;
+ *dest_buf = last_span.data();
+ *dest_size = last_span.size();
*pImageParams = pParam;
return true;
}
- offset = FlateOrLZWDecode(false, last_buf, last_size, pParam,
- estimated_size, &new_buf, &new_size);
+ offset = FlateOrLZWDecode(false, last_span, pParam, estimated_size,
+ &new_buf, &new_size);
} else if (decoder == "LZWDecode" || decoder == "LZW") {
- offset = FlateOrLZWDecode(true, last_buf, last_size, pParam,
- estimated_size, &new_buf, &new_size);
+ offset = FlateOrLZWDecode(true, last_span, pParam, estimated_size,
+ &new_buf, &new_size);
} else if (decoder == "ASCII85Decode" || decoder == "A85") {
- offset = A85Decode(last_buf, last_size, &new_buf, &new_size);
+ offset = A85Decode(last_span, &new_buf, &new_size);
} else if (decoder == "ASCIIHexDecode" || decoder == "AHx") {
- offset = HexDecode(last_buf, last_size, &new_buf, &new_size);
+ offset = HexDecode(last_span, &new_buf, &new_size);
} else if (decoder == "RunLengthDecode" || decoder == "RL") {
if (bImageAcc && i == nSize - 1) {
*ImageEncoding = "RunLengthDecode";
- *dest_buf = last_buf;
- *dest_size = last_size;
+ *dest_buf = last_span.data();
+ *dest_size = last_span.size();
*pImageParams = pParam;
return true;
}
- offset = RunLengthDecode(last_buf, last_size, &new_buf, &new_size);
+ offset = RunLengthDecode(last_span, &new_buf, &new_size);
} else {
// If we get here, assume it's an image decoder.
if (decoder == "DCT")
@@ -408,23 +403,25 @@ bool PDF_DataDecode(const uint8_t* src_buf,
decoder = "CCITTFaxDecode";
*ImageEncoding = std::move(decoder);
*pImageParams = pParam;
- *dest_buf = last_buf;
- *dest_size = last_size;
+ *dest_buf = last_span.data();
+ *dest_size = last_span.size();
return true;
}
- if (last_buf != src_buf)
- FX_Free(last_buf);
+ if (last_span.data() != src_span.data()) {
+ auto* temp = last_span.data();
+ last_span = {}; // Spans can't outlive their data.
+ FX_Free(temp);
+ }
if (offset == FX_INVALID_OFFSET) {
FX_Free(new_buf);
return false;
}
- last_buf = new_buf;
- last_size = new_size;
+ last_span = {new_buf, new_size};
}
ImageEncoding->clear();
*pImageParams = nullptr;
- *dest_buf = last_buf;
- *dest_size = last_size;
+ *dest_buf = last_span.data();
+ *dest_size = last_span.size();
return true;
}
@@ -554,20 +551,18 @@ ByteString PDF_EncodeString(const ByteString& src, bool bHex) {
return ByteString(result);
}
-bool FlateEncode(const uint8_t* src_buf,
- uint32_t src_size,
+bool FlateEncode(pdfium::span<const uint8_t> src_span,
uint8_t** dest_buf,
uint32_t* dest_size) {
CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
- return pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf,
- dest_size);
+ return pEncoders->GetFlateModule()->Encode(src_span.data(), src_span.size(),
+ dest_buf, dest_size);
}
-uint32_t FlateDecode(const uint8_t* src_buf,
- uint32_t src_size,
+uint32_t FlateDecode(pdfium::span<const uint8_t> src_span,
uint8_t** dest_buf,
uint32_t* dest_size) {
CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule();
return pEncoders->GetFlateModule()->FlateOrLZWDecode(
- false, src_buf, src_size, false, 0, 0, 0, 0, 0, dest_buf, dest_size);
+ false, src_span, false, 0, 0, 0, 0, 0, dest_buf, dest_size);
}
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.h b/core/fpdfapi/parser/fpdf_parser_decode.h
index da228d223d..1484dd7cf0 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.h
+++ b/core/fpdfapi/parser/fpdf_parser_decode.h
@@ -11,6 +11,7 @@
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/unowned_ptr.h"
+#include "third_party/base/span.h"
class CCodec_ScanlineDecoder;
class CPDF_Dictionary;
@@ -23,21 +24,6 @@ WideString PDF_DecodeText(const uint8_t* pData, uint32_t size);
WideString PDF_DecodeText(const ByteString& bstr);
ByteString PDF_EncodeText(const WideString& str);
-bool FlateEncode(const uint8_t* src_buf,
- uint32_t src_size,
- uint8_t** dest_buf,
- uint32_t* dest_size);
-
-uint32_t FlateDecode(const uint8_t* src_buf,
- uint32_t src_size,
- uint8_t** dest_buf,
- uint32_t* dest_size);
-
-uint32_t RunLengthDecode(const uint8_t* src_buf,
- uint32_t src_size,
- uint8_t** dest_buf,
- uint32_t* dest_size);
-
std::unique_ptr<CCodec_ScanlineDecoder> CreateFaxDecoder(
pdfium::span<const uint8_t> src_span,
int width,
@@ -52,26 +38,34 @@ std::unique_ptr<CCodec_ScanlineDecoder> CreateFlateDecoder(
int bpc,
const CPDF_Dictionary* pParams);
-uint32_t A85Decode(const uint8_t* src_buf,
- uint32_t src_size,
+bool FlateEncode(pdfium::span<const uint8_t> src_span,
+ uint8_t** dest_buf,
+ uint32_t* dest_size);
+
+uint32_t FlateDecode(pdfium::span<const uint8_t> src_span,
+ uint8_t** dest_buf,
+ uint32_t* dest_size);
+
+uint32_t RunLengthDecode(pdfium::span<const uint8_t> src_span,
+ uint8_t** dest_buf,
+ uint32_t* dest_size);
+
+uint32_t A85Decode(pdfium::span<const uint8_t> src_span,
uint8_t** dest_buf,
uint32_t* dest_size);
-uint32_t HexDecode(const uint8_t* src_buf,
- uint32_t src_size,
+uint32_t HexDecode(pdfium::span<const uint8_t> src_span,
uint8_t** dest_buf,
uint32_t* dest_size);
uint32_t FlateOrLZWDecode(bool bLZW,
- const uint8_t* src_buf,
- uint32_t src_size,
+ pdfium::span<const uint8_t> src_span,
const CPDF_Dictionary* pParams,
uint32_t estimated_size,
uint8_t** dest_buf,
uint32_t* dest_size);
-bool PDF_DataDecode(const uint8_t* src_buf,
- uint32_t src_size,
+bool PDF_DataDecode(pdfium::span<const uint8_t> src_span,
const CPDF_Dictionary* pDict,
uint32_t estimated_size,
bool bImageAcc,
diff --git a/core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp b/core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp
index 3a48e80de8..d7256f0da5 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode_embeddertest.cpp
@@ -39,7 +39,7 @@ TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) {
const pdfium::StrFuncTestData& data = flate_encode_cases[i];
unsigned char* buf = nullptr;
uint32_t buf_size;
- EXPECT_TRUE(FlateEncode(data.input, data.input_size, &buf, &buf_size));
+ EXPECT_TRUE(FlateEncode({data.input, data.input_size}, &buf, &buf_size));
ASSERT_TRUE(buf);
EXPECT_EQ(data.expected_size, buf_size) << " for case " << i;
if (data.expected_size != buf_size)
@@ -77,7 +77,7 @@ TEST_F(FPDFParserDecodeEmbeddertest, FlateDecode) {
unsigned char* buf = nullptr;
uint32_t buf_size;
EXPECT_EQ(data.processed_size,
- FlateDecode(data.input, data.input_size, &buf, &buf_size))
+ FlateDecode({data.input, data.input_size}, &buf, &buf_size))
<< " for case " << i;
ASSERT_TRUE(buf);
EXPECT_EQ(data.expected_size, buf_size) << " for case " << i;
diff --git a/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp b/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp
index 9ab4958825..aa37b796e2 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp
@@ -31,7 +31,7 @@ TEST(fpdf_parser_decode, A85Decode) {
uint8_t* result = nullptr;
uint32_t result_size = 0;
EXPECT_EQ(ptr->processed_size,
- A85Decode(ptr->input, ptr->input_size, &result, &result_size))
+ A85Decode({ptr->input, ptr->input_size}, &result, &result_size))
<< "for case " << i;
ASSERT_EQ(ptr->expected_size, result_size);
for (size_t j = 0; j < result_size; ++j) {
@@ -66,7 +66,7 @@ TEST(fpdf_parser_decode, HexDecode) {
uint8_t* result = nullptr;
uint32_t result_size = 0;
EXPECT_EQ(ptr->processed_size,
- HexDecode(ptr->input, ptr->input_size, &result, &result_size))
+ HexDecode({ptr->input, ptr->input_size}, &result, &result_size))
<< "for case " << i;
ASSERT_EQ(ptr->expected_size, result_size);
for (size_t j = 0; j < result_size; ++j) {