Convert GenerateMD5Base16() to use a span. Use a single parameter instead of 2. Change some callers so they just pass in a single argument. Change-Id: Iecf6ca8b5f70538f8fa003f929852eda3c2ca5cb Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/93571 Reviewed-by: Nigi <nigi@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fdrm/fx_crypt_unittest.cpp b/core/fdrm/fx_crypt_unittest.cpp index 4fce87e..807e68d 100644 --- a/core/fdrm/fx_crypt_unittest.cpp +++ b/core/fdrm/fx_crypt_unittest.cpp
@@ -14,7 +14,8 @@ namespace { std::string CRYPT_MD5String(const char* str) { - return GenerateMD5Base16(reinterpret_cast<const uint8_t*>(str), strlen(str)); + return GenerateMD5Base16( + {reinterpret_cast<const uint8_t*>(str), strlen(str)}); } void CheckArcFourContext(const CRYPT_rc4_context& context,
diff --git a/fpdfsdk/fpdf_annot_embeddertest.cpp b/fpdfsdk/fpdf_annot_embeddertest.cpp index 3d5effb..6232027 100644 --- a/fpdfsdk/fpdf_annot_embeddertest.cpp +++ b/fpdfsdk/fpdf_annot_embeddertest.cpp
@@ -1640,8 +1640,8 @@ FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, buf.data(), normal_length_bytes)); EXPECT_EQ(kMd5NormalAP, - GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), - normal_length_bytes)); + GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()), + normal_length_bytes})); // Check that the string value of an AP is returned through a buffer that is // larger than necessary. @@ -1650,8 +1650,8 @@ FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, buf.data(), normal_length_bytes + 2)); EXPECT_EQ(kMd5NormalAP, - GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), - normal_length_bytes)); + GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()), + normal_length_bytes})); // Check that getting an AP for a mode that does not have an AP returns an // empty string. @@ -1693,8 +1693,8 @@ FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, buf.data(), normal_length_bytes)); EXPECT_EQ(kMd5NormalAP, - GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), - normal_length_bytes)); + GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()), + normal_length_bytes})); } // Save the modified document, then reopen it.
diff --git a/fpdfsdk/fpdf_attachment_embeddertest.cpp b/fpdfsdk/fpdf_attachment_embeddertest.cpp index f1745b3..2646e21 100644 --- a/fpdfsdk/fpdf_attachment_embeddertest.cpp +++ b/fpdfsdk/fpdf_attachment_embeddertest.cpp
@@ -9,6 +9,7 @@ #include "public/fpdfview.h" #include "testing/embedder_test.h" #include "testing/fx_string_testhelpers.h" +#include "testing/gmock/include/gmock/gmock.h" #include "testing/utils/hash.h" static constexpr char kDateKey[] = "CreationDate"; @@ -42,12 +43,11 @@ // Check that the content of the first attachment is correct. ASSERT_TRUE(FPDFAttachment_GetFile(attachment, nullptr, 0, &length_bytes)); - std::vector<char> content_buf(length_bytes); + std::vector<uint8_t> content_buf(length_bytes); unsigned long actual_length_bytes; ASSERT_TRUE(FPDFAttachment_GetFile(attachment, content_buf.data(), length_bytes, &actual_length_bytes)); - ASSERT_EQ(4u, actual_length_bytes); - EXPECT_EQ(std::string("test"), std::string(content_buf.data(), 4)); + ASSERT_THAT(content_buf, testing::ElementsAre('t', 'e', 's', 't')); // Check that a non-existent key does not exist. EXPECT_FALSE(FPDFAttachment_HasKey(attachment, "none")); @@ -83,8 +83,7 @@ // Check that the calculated checksum of the file data matches expectation. const char kCheckSum[] = "72afcddedf554dda63c0c88e06f1ce18"; const wchar_t kCheckSumW[] = L"<72AFCDDEDF554DDA63C0C88E06F1CE18>"; - const std::string generated_checksum = GenerateMD5Base16( - reinterpret_cast<uint8_t*>(content_buf.data()), length_bytes); + const std::string generated_checksum = GenerateMD5Base16(content_buf); EXPECT_EQ(kCheckSum, generated_checksum); // Check that the stored checksum matches expectation.
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp index 2855368..8942c28 100644 --- a/fpdfsdk/fpdf_edit_embeddertest.cpp +++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -3873,18 +3873,16 @@ // Check that the raw image data has the correct length and hash value. unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0); - std::vector<char> buf(len); + std::vector<uint8_t> buf(len); EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len)); - EXPECT_EQ("f73802327d2e88e890f653961bcda81a", - GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); + EXPECT_EQ("f73802327d2e88e890f653961bcda81a", GenerateMD5Base16(buf)); // Check that the decoded image data has the correct length and hash value. len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0); buf.clear(); buf.resize(len); EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len)); - EXPECT_EQ(kEmbeddedImage33Checksum, - GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); + EXPECT_EQ(kEmbeddedImage33Checksum, GenerateMD5Base16(buf)); // Retrieve an image object with DCTDecode-encoded data stream. obj = FPDFPage_GetObject(page, 37); @@ -3895,8 +3893,7 @@ buf.clear(); buf.resize(len); EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len)); - EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", - GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); + EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", GenerateMD5Base16(buf)); // Check that the decoded image data has the correct length and hash value, // which should be the same as those of the raw data, since this image is @@ -3905,8 +3902,7 @@ buf.clear(); buf.resize(len); EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len)); - EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", - GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); + EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", GenerateMD5Base16(buf)); UnloadPage(page); }
diff --git a/fpdfsdk/fpdf_thumbnail_embeddertest.cpp b/fpdfsdk/fpdf_thumbnail_embeddertest.cpp index 184d4fd..789ba25 100644 --- a/fpdfsdk/fpdf_thumbnail_embeddertest.cpp +++ b/fpdfsdk/fpdf_thumbnail_embeddertest.cpp
@@ -36,8 +36,7 @@ EXPECT_EQ(kExpectedSize, FPDFPage_GetDecodedThumbnailData( page, thumb_buf.data(), length_bytes)); - EXPECT_EQ(kHashedDecodedData, - GenerateMD5Base16(thumb_buf.data(), kExpectedSize)); + EXPECT_EQ(kHashedDecodedData, GenerateMD5Base16(thumb_buf)); UnloadPage(page); } @@ -56,8 +55,7 @@ EXPECT_EQ(kExpectedSize, FPDFPage_GetDecodedThumbnailData( page, thumb_buf.data(), length_bytes)); - EXPECT_EQ(kHashedDecodedData, - GenerateMD5Base16(thumb_buf.data(), kExpectedSize)); + EXPECT_EQ(kHashedDecodedData, GenerateMD5Base16(thumb_buf)); UnloadPage(page); } @@ -79,8 +77,7 @@ EXPECT_EQ(kExpectedSize, FPDFPage_GetDecodedThumbnailData( page, thumb_buf.data(), length_bytes)); - EXPECT_EQ(kThumbnailWithNoFiltersChecksum, - GenerateMD5Base16(thumb_buf.data(), kExpectedSize)); + EXPECT_EQ(kThumbnailWithNoFiltersChecksum, GenerateMD5Base16(thumb_buf)); UnloadPage(page); } @@ -116,8 +113,7 @@ EXPECT_EQ(kExpectedSize, FPDFPage_GetRawThumbnailData( page, thumb_buf.data(), length_bytes)); - EXPECT_EQ(kSimpleThumbnailChecksum, - GenerateMD5Base16(thumb_buf.data(), kExpectedSize)); + EXPECT_EQ(kSimpleThumbnailChecksum, GenerateMD5Base16(thumb_buf)); UnloadPage(page); } @@ -135,8 +131,7 @@ EXPECT_EQ(kExpectedSize, FPDFPage_GetRawThumbnailData( page, thumb_buf.data(), length_bytes)); - EXPECT_EQ(kHashedRawData, - GenerateMD5Base16(thumb_buf.data(), kExpectedSize)); + EXPECT_EQ(kHashedRawData, GenerateMD5Base16(thumb_buf)); UnloadPage(page); } @@ -156,8 +151,7 @@ EXPECT_EQ(kExpectedSize, FPDFPage_GetRawThumbnailData(page, thumb_buf.data(), length_bytes)); - EXPECT_EQ(kThumbnailWithNoFiltersChecksum, - GenerateMD5Base16(thumb_buf.data(), kExpectedSize)); + EXPECT_EQ(kThumbnailWithNoFiltersChecksum, GenerateMD5Base16(thumb_buf)); UnloadPage(page); } @@ -269,8 +263,7 @@ EXPECT_EQ(kExpectedRawSize, FPDFPage_GetRawThumbnailData(page, raw_thumb_buf.data(), raw_size)); - EXPECT_EQ(kSimpleThumbnailChecksum, - GenerateMD5Base16(raw_thumb_buf.data(), kExpectedRawSize)); + EXPECT_EQ(kSimpleThumbnailChecksum, GenerateMD5Base16(raw_thumb_buf)); // Get the thumbnail ScopedFPDFBitmap thumb_bitmap(FPDFPage_GetThumbnailAsBitmap(page)); @@ -288,8 +281,7 @@ EXPECT_EQ(kExpectedRawSize, FPDFPage_GetRawThumbnailData(page, new_raw_thumb_buf.data(), new_raw_size)); - EXPECT_EQ(kSimpleThumbnailChecksum, - GenerateMD5Base16(new_raw_thumb_buf.data(), kExpectedRawSize)); + EXPECT_EQ(kSimpleThumbnailChecksum, GenerateMD5Base16(new_raw_thumb_buf)); UnloadPage(page); }
diff --git a/fpdfsdk/fpdf_view_embeddertest.cpp b/fpdfsdk/fpdf_view_embeddertest.cpp index e015466..af732df 100644 --- a/fpdfsdk/fpdf_view_embeddertest.cpp +++ b/fpdfsdk/fpdf_view_embeddertest.cpp
@@ -1123,9 +1123,8 @@ EXPECT_TRUE(FPDF_GetXFAPacketContent(document(), i, data_buffer.data(), data_buffer.size(), &buflen)); EXPECT_EQ(kExpectedResults[i].content_length, buflen); - EXPECT_STREQ( - kExpectedResults[i].content_checksum, - GenerateMD5Base16(data_buffer.data(), data_buffer.size()).c_str()); + EXPECT_STREQ(kExpectedResults[i].content_checksum, + GenerateMD5Base16(data_buffer).c_str()); } // Test bad parameters. @@ -1165,9 +1164,8 @@ EXPECT_TRUE(FPDF_GetXFAPacketContent(document(), 0, data_buffer.data(), data_buffer.size(), &buflen)); EXPECT_EQ(121u, buflen); - EXPECT_STREQ( - "8f912eaa1e66c9341cb3032ede71e147", - GenerateMD5Base16(data_buffer.data(), data_buffer.size()).c_str()); + EXPECT_STREQ("8f912eaa1e66c9341cb3032ede71e147", + GenerateMD5Base16(data_buffer).c_str()); } TEST_F(FPDFViewEmbedderTest, GetXFADataForNoForm) {
diff --git a/fxbarcode/cfx_barcode_unittest.cpp b/fxbarcode/cfx_barcode_unittest.cpp index 3e4145b..ccf5499 100644 --- a/fxbarcode/cfx_barcode_unittest.cpp +++ b/fxbarcode/cfx_barcode_unittest.cpp
@@ -49,8 +49,8 @@ bool RenderDevice() { return barcode_->RenderDevice(device_.get(), matrix_); } std::string BitmapChecksum() { - return GenerateMD5Base16(bitmap_->GetBuffer(), - bitmap_->GetPitch() * bitmap_->GetHeight()); + return GenerateMD5Base16( + {bitmap_->GetBuffer(), bitmap_->GetPitch() * bitmap_->GetHeight()}); } // Manually insert calls to this as needed for debugging.
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index 2643859..2c76808 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc
@@ -308,9 +308,9 @@ return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info); } -void OutputMD5Hash(const char* file_name, const uint8_t* buffer, int len) { +void OutputMD5Hash(const char* file_name, pdfium::span<const uint8_t> output) { // Get the MD5 hash and write it to stdout. - std::string hash = GenerateMD5Base16(buffer, len); + std::string hash = GenerateMD5Base16(output); printf("MD5:%s:%s\n", file_name, hash.c_str()); } @@ -956,7 +956,8 @@ // file. if (options.md5 && !image_file_name.empty()) { OutputMD5Hash(image_file_name.c_str(), - static_cast<const uint8_t*>(buffer), stride * height); + {static_cast<const uint8_t*>(buffer), + static_cast<size_t>(stride) * height}); } } else { fprintf(stderr, "Page was too large to be rendered.\n");
diff --git a/testing/utils/hash.cpp b/testing/utils/hash.cpp index 3170de2..71f3095 100644 --- a/testing/utils/hash.cpp +++ b/testing/utils/hash.cpp
@@ -18,8 +18,8 @@ return ret; } -std::string GenerateMD5Base16(const uint8_t* data, uint32_t size) { +std::string GenerateMD5Base16(pdfium::span<const uint8_t> data) { uint8_t digest[16]; - CRYPT_MD5Generate({data, size}, digest); + CRYPT_MD5Generate(data, digest); return CryptToBase16(digest); }
diff --git a/testing/utils/hash.h b/testing/utils/hash.h index 0e55165..e2a4f0a 100644 --- a/testing/utils/hash.h +++ b/testing/utils/hash.h
@@ -7,7 +7,9 @@ #include <string> +#include "third_party/base/span.h" + std::string CryptToBase16(const uint8_t* digest); -std::string GenerateMD5Base16(const uint8_t* data, uint32_t size); +std::string GenerateMD5Base16(pdfium::span<const uint8_t> data); #endif // TESTING_UTILS_HASH_H_