Use DataVector<T> in core/fpdfapi/parser/.
Easier to write than std::vector<T, FxAllocAllocator<T>>.
Change-Id: I7c2a7a931c793ade8e9945622143b49f3f938c79
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/96612
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_encryptor.cpp b/core/fpdfapi/parser/cpdf_encryptor.cpp
index 1faff25..ae2a6e2 100644
--- a/core/fpdfapi/parser/cpdf_encryptor.cpp
+++ b/core/fpdfapi/parser/cpdf_encryptor.cpp
@@ -6,7 +6,10 @@
#include "core/fpdfapi/parser/cpdf_encryptor.h"
+#include <stdint.h>
+
#include "core/fpdfapi/parser/cpdf_crypto_handler.h"
+#include "core/fxcrt/data_vector.h"
#include "third_party/base/check.h"
CPDF_Encryptor::CPDF_Encryptor(const CPDF_CryptoHandler* pHandler, int objnum)
@@ -14,12 +17,12 @@
DCHECK(m_pHandler);
}
-std::vector<uint8_t, FxAllocAllocator<uint8_t>> CPDF_Encryptor::Encrypt(
+DataVector<uint8_t> CPDF_Encryptor::Encrypt(
pdfium::span<const uint8_t> src_data) const {
if (src_data.empty())
- return std::vector<uint8_t, FxAllocAllocator<uint8_t>>();
+ return DataVector<uint8_t>();
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> result;
+ DataVector<uint8_t> result;
size_t buf_size = m_pHandler->EncryptGetSize(src_data);
result.resize(buf_size);
m_pHandler->EncryptContent(m_ObjNum, 0, src_data, result.data(),
diff --git a/core/fpdfapi/parser/cpdf_encryptor.h b/core/fpdfapi/parser/cpdf_encryptor.h
index bdb6245..41c4401 100644
--- a/core/fpdfapi/parser/cpdf_encryptor.h
+++ b/core/fpdfapi/parser/cpdf_encryptor.h
@@ -9,9 +9,7 @@
#include <stdint.h>
-#include <vector>
-
-#include "core/fxcrt/fx_memory_wrappers.h"
+#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/unowned_ptr.h"
#include "third_party/base/span.h"
@@ -22,8 +20,7 @@
CPDF_Encryptor(const CPDF_CryptoHandler* pHandler, int objnum);
~CPDF_Encryptor();
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> Encrypt(
- pdfium::span<const uint8_t> src_data) const;
+ DataVector<uint8_t> Encrypt(pdfium::span<const uint8_t> src_data) const;
private:
UnownedPtr<const CPDF_CryptoHandler> const m_pHandler;
diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp
index 6a935c8..899e06f 100644
--- a/core/fpdfapi/parser/cpdf_object_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp
@@ -4,6 +4,8 @@
#include "core/fpdfapi/parser/cpdf_object.h"
+#include <stdint.h>
+
#include <memory>
#include <string>
#include <utility>
@@ -21,6 +23,7 @@
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "core/fpdfapi/parser/cpdf_stream_acc.h"
#include "core/fpdfapi/parser/cpdf_string.h"
+#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_memory_wrappers.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -839,7 +842,7 @@
}
TEST(PDFStreamTest, SetData) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> data(100);
+ DataVector<uint8_t> data(100);
auto stream = pdfium::MakeRetain<CPDF_Stream>(
data, pdfium::MakeRetain<CPDF_Dictionary>());
EXPECT_EQ(static_cast<int>(data.size()),
@@ -850,7 +853,7 @@
stream->GetMutableDict()->SetNewFor<CPDF_String>(pdfium::stream::kDecodeParms,
L"SomeParams");
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> new_data(data.size() * 2);
+ DataVector<uint8_t> new_data(data.size() * 2);
stream->SetData(new_data);
// The "Length" field should be updated for new data size.
@@ -865,7 +868,7 @@
}
TEST(PDFStreamTest, SetDataAndRemoveFilter) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> data(100);
+ DataVector<uint8_t> data(100);
auto stream = pdfium::MakeRetain<CPDF_Stream>(
data, pdfium::MakeRetain<CPDF_Dictionary>());
EXPECT_EQ(static_cast<int>(data.size()),
@@ -876,7 +879,7 @@
stream->GetMutableDict()->SetNewFor<CPDF_String>(pdfium::stream::kDecodeParms,
L"SomeParams");
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> new_data(data.size() * 2);
+ DataVector<uint8_t> new_data(data.size() * 2);
stream->SetDataAndRemoveFilter(new_data);
// The "Length" field should be updated for new data size.
EXPECT_EQ(static_cast<int>(new_data.size()),
diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp
index 649b474..b204ae0 100644
--- a/core/fpdfapi/parser/cpdf_parser.cpp
+++ b/core/fpdfapi/parser/cpdf_parser.cpp
@@ -7,6 +7,7 @@
#include "core/fpdfapi/parser/cpdf_parser.h"
#include <ctype.h>
+#include <stdint.h>
#include <algorithm>
#include <utility>
@@ -27,8 +28,8 @@
#include "core/fpdfapi/parser/cpdf_syntax_parser.h"
#include "core/fpdfapi/parser/fpdf_parser_utility.h"
#include "core/fxcrt/autorestorer.h"
+#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_extension.h"
-#include "core/fxcrt/fx_memory_wrappers.h"
#include "core/fxcrt/fx_safe_types.h"
#include "core/fxcrt/scoped_set_insertion.h"
#include "third_party/base/check.h"
@@ -508,7 +509,7 @@
out_objects->resize(new_size.ValueOrDie());
- std::vector<char, FxAllocAllocator<char>> buf(1024 * kEntryConstSize + 1);
+ DataVector<char> buf(1024 * kEntryConstSize + 1);
buf.back() = '\0';
uint32_t nBytesToRead = count;
diff --git a/core/fpdfapi/parser/cpdf_read_validator_unittest.cpp b/core/fpdfapi/parser/cpdf_read_validator_unittest.cpp
index d5409eb..7b75cfc 100644
--- a/core/fpdfapi/parser/cpdf_read_validator_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_read_validator_unittest.cpp
@@ -4,11 +4,13 @@
#include "core/fpdfapi/parser/cpdf_read_validator.h"
+#include <stdint.h>
+
#include <limits>
#include <utility>
-#include <vector>
#include "core/fxcrt/cfx_readonlymemorystream.h"
+#include "core/fxcrt/data_vector.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/invalid_seekable_read_stream.h"
@@ -65,12 +67,12 @@
} // namespace
TEST(ReadValidatorTest, UnavailableData) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> test_data(kTestDataSize);
+ DataVector<uint8_t> test_data(kTestDataSize);
auto file = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(test_data);
MockFileAvail file_avail;
auto validator = pdfium::MakeRetain<CPDF_ReadValidator>(file, &file_avail);
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> read_buffer(100);
+ DataVector<uint8_t> read_buffer(100);
EXPECT_FALSE(validator->ReadBlockAtOffset(read_buffer.data(), 5000,
read_buffer.size()));
@@ -88,7 +90,7 @@
}
TEST(ReadValidatorTest, UnavailableDataWithHints) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> test_data(kTestDataSize);
+ DataVector<uint8_t> test_data(kTestDataSize);
auto file = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(test_data);
MockFileAvail file_avail;
auto validator = pdfium::MakeRetain<CPDF_ReadValidator>(file, &file_avail);
@@ -96,7 +98,7 @@
MockDownloadHints hints;
validator->SetDownloadHints(&hints);
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> read_buffer(100);
+ DataVector<uint8_t> read_buffer(100);
EXPECT_FALSE(validator->ReadBlockAtOffset(read_buffer.data(), 5000,
read_buffer.size()));
@@ -135,7 +137,7 @@
auto validator = pdfium::MakeRetain<CPDF_ReadValidator>(file, nullptr);
static const uint32_t kBufferSize = 3 * 1000;
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> buffer(kBufferSize);
+ DataVector<uint8_t> buffer(kBufferSize);
EXPECT_FALSE(validator->ReadBlockAtOffset(buffer.data(), 5000, 100));
EXPECT_TRUE(validator->read_error());
@@ -143,12 +145,12 @@
}
TEST(ReadValidatorTest, IntOverflow) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> test_data(kTestDataSize);
+ DataVector<uint8_t> test_data(kTestDataSize);
auto file = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(test_data);
MockFileAvail file_avail;
auto validator = pdfium::MakeRetain<CPDF_ReadValidator>(file, &file_avail);
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> read_buffer(100);
+ DataVector<uint8_t> read_buffer(100);
// If we have int overflow, this is equal reading after file end. This is not
// read_error, and in this case we have not unavailable data. It is just error
@@ -161,7 +163,7 @@
}
TEST(ReadValidatorTest, Session) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> test_data(kTestDataSize);
+ DataVector<uint8_t> test_data(kTestDataSize);
auto file = pdfium::MakeRetain<InvalidSeekableReadStream>(kTestDataSize);
MockFileAvail file_avail;
@@ -199,7 +201,7 @@
}
TEST(ReadValidatorTest, SessionReset) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> test_data(kTestDataSize);
+ DataVector<uint8_t> test_data(kTestDataSize);
auto file = pdfium::MakeRetain<InvalidSeekableReadStream>(kTestDataSize);
MockFileAvail file_avail;
@@ -241,7 +243,7 @@
}
TEST(ReadValidatorTest, CheckDataRangeAndRequestIfUnavailable) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> test_data(kTestDataSize);
+ DataVector<uint8_t> test_data(kTestDataSize);
auto file = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(test_data);
MockFileAvail file_avail;
auto validator = pdfium::MakeRetain<CPDF_ReadValidator>(file, &file_avail);
@@ -266,7 +268,7 @@
EXPECT_FALSE(validator->read_error());
EXPECT_FALSE(validator->has_unavailable_data());
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> read_buffer(100);
+ DataVector<uint8_t> read_buffer(100);
EXPECT_TRUE(validator->ReadBlockAtOffset(read_buffer.data(), 5000,
read_buffer.size()));
// No new request on already available data.
diff --git a/core/fpdfapi/parser/cpdf_security_handler.cpp b/core/fpdfapi/parser/cpdf_security_handler.cpp
index 31c000b..fadc00a 100644
--- a/core/fpdfapi/parser/cpdf_security_handler.cpp
+++ b/core/fpdfapi/parser/cpdf_security_handler.cpp
@@ -6,11 +6,11 @@
#include "core/fpdfapi/parser/cpdf_security_handler.h"
+#include <stdint.h>
#include <time.h>
#include <algorithm>
#include <utility>
-#include <vector>
#include "core/fdrm/fx_crypt.h"
#include "core/fpdfapi/parser/cpdf_array.h"
@@ -18,6 +18,7 @@
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_object.h"
#include "core/fpdfapi/parser/cpdf_string.h"
+#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_random.h"
#include "third_party/base/check.h"
#include "third_party/base/check_op.h"
@@ -118,13 +119,13 @@
uint8_t digest[32];
CRYPT_SHA256Finish(&sha, digest);
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> buf;
+ DataVector<uint8_t> buf;
uint8_t* input = digest;
uint8_t* key = input;
uint8_t* iv = input + 16;
uint8_t* E = nullptr;
int iBufLen = 0;
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> interDigest;
+ DataVector<uint8_t> interDigest;
int i = 0;
int iBlockSize = 32;
CRYPT_aes_context aes = {};
@@ -136,7 +137,7 @@
iBufLen = iRoundSize * 64;
buf.resize(iBufLen);
E = buf.data();
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> content;
+ DataVector<uint8_t> content;
for (int j = 0; j < 64; ++j) {
content.insert(std::end(content), password.raw_str(),
password.raw_str() + password.GetLength());
diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp
index bf82ea7..ae6eb7a 100644
--- a/core/fpdfapi/parser/cpdf_stream.cpp
+++ b/core/fpdfapi/parser/cpdf_stream.cpp
@@ -6,9 +6,10 @@
#include "core/fpdfapi/parser/cpdf_stream.h"
+#include <stdint.h>
+
#include <sstream>
#include <utility>
-#include <vector>
#include "constants/stream_dict_common.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
@@ -18,6 +19,7 @@
#include "core/fpdfapi/parser/cpdf_stream_acc.h"
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
#include "core/fpdfapi/parser/fpdf_parser_utility.h"
+#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_stream.h"
#include "core/fxcrt/span_util.h"
#include "third_party/base/containers/contains.h"
@@ -41,7 +43,7 @@
SetData(pData);
}
-CPDF_Stream::CPDF_Stream(std::vector<uint8_t, FxAllocAllocator<uint8_t>> pData,
+CPDF_Stream::CPDF_Stream(DataVector<uint8_t> pData,
RetainPtr<CPDF_Dictionary> pDict)
: m_pDict(std::move(pDict)) {
// TODO(crbug.com/pdfium/1872): Avoid copying.
@@ -192,7 +194,7 @@
const bool is_metadata = IsMetaDataStreamDictionary(GetDict());
CPDF_FlateEncoder encoder(this, !is_metadata);
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> encrypted_data;
+ DataVector<uint8_t> encrypted_data;
pdfium::span<const uint8_t> data = encoder.GetSpan();
if (encryptor && !is_metadata) {
diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h
index d38288d..b477f9a 100644
--- a/core/fpdfapi/parser/cpdf_stream.h
+++ b/core/fpdfapi/parser/cpdf_stream.h
@@ -7,11 +7,13 @@
#ifndef CORE_FPDFAPI_PARSER_CPDF_STREAM_H_
#define CORE_FPDFAPI_PARSER_CPDF_STREAM_H_
+#include <stdint.h>
+
#include <memory>
#include <set>
-#include <vector>
#include "core/fpdfapi/parser/cpdf_object.h"
+#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_memory_wrappers.h"
#include "core/fxcrt/fx_stream.h"
#include "core/fxcrt/fx_string_wrappers.h"
@@ -66,8 +68,7 @@
CPDF_Stream();
CPDF_Stream(pdfium::span<const uint8_t> pData,
RetainPtr<CPDF_Dictionary> pDict);
- CPDF_Stream(std::vector<uint8_t, FxAllocAllocator<uint8_t>> pData,
- RetainPtr<CPDF_Dictionary> pDict);
+ CPDF_Stream(DataVector<uint8_t> pData, RetainPtr<CPDF_Dictionary> pDict);
// TODO(crbug.com/pdfium/1872): Replace with vector version above.
CPDF_Stream(std::unique_ptr<uint8_t, FxFreeDeleter> pData,
size_t size,
diff --git a/core/fpdfapi/parser/cpdf_string.cpp b/core/fpdfapi/parser/cpdf_string.cpp
index fe667d7..ba15ed4 100644
--- a/core/fpdfapi/parser/cpdf_string.cpp
+++ b/core/fpdfapi/parser/cpdf_string.cpp
@@ -6,11 +6,13 @@
#include "core/fpdfapi/parser/cpdf_string.h"
+#include <stdint.h>
+
#include <utility>
-#include <vector>
#include "core/fpdfapi/parser/cpdf_encryptor.h"
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
+#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_stream.h"
CPDF_String::CPDF_String() = default;
@@ -68,7 +70,7 @@
bool CPDF_String::WriteTo(IFX_ArchiveStream* archive,
const CPDF_Encryptor* encryptor) const {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> encrypted_data;
+ DataVector<uint8_t> encrypted_data;
pdfium::span<const uint8_t> data = m_String.raw_span();
if (encryptor) {
encrypted_data = encryptor->Encrypt(data);
diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.h b/core/fpdfapi/parser/cpdf_syntax_parser.h
index 46e1e9a..43af7e2 100644
--- a/core/fpdfapi/parser/cpdf_syntax_parser.h
+++ b/core/fpdfapi/parser/cpdf_syntax_parser.h
@@ -13,7 +13,7 @@
#include <vector>
#include "core/fpdfapi/parser/cpdf_stream.h"
-#include "core/fxcrt/fx_memory_wrappers.h"
+#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_types.h"
#include "core/fxcrt/retain_ptr.h"
#include "core/fxcrt/string_pool_template.h"
@@ -124,7 +124,7 @@
const FX_FILESIZE m_FileLen;
FX_FILESIZE m_Pos = 0;
WeakPtr<ByteStringPool> m_pPool;
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> m_pFileBuf;
+ DataVector<uint8_t> m_pFileBuf;
FX_FILESIZE m_BufOffset = 0;
uint32_t m_WordSize = 0;
uint8_t m_WordBuffer[257];