diff options
author | Tom Sepez <tsepez@chromium.org> | 2018-08-24 19:45:09 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-08-24 19:45:09 +0000 |
commit | 16d18d935d055b74e05f615c0325318a329a7fe1 (patch) | |
tree | bce3c35b6db03cedd537a67a28cf91eb0b502b1c /core/fxcodec/gif | |
parent | 1cb4d6b14cc9375dc04f57d3f2149eccb9e327a8 (diff) | |
download | pdfium-16d18d935d055b74e05f615c0325318a329a7fe1.tar.xz |
Use span<uint8_t> in more fxcodec code.
There are also small tweaks in tests to ensure strict order of
stack variable lifetimes.
Change-Id: Ic9d5c6a2bdd378b517be627f8e29f725bafdc2ad
Reviewed-on: https://pdfium-review.googlesource.com/41310
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcodec/gif')
-rw-r--r-- | core/fxcodec/gif/cfx_gifcontext.cpp | 4 | ||||
-rw-r--r-- | core/fxcodec/gif/cfx_gifcontext.h | 3 | ||||
-rw-r--r-- | core/fxcodec/gif/cfx_gifcontext_unittest.cpp | 174 |
3 files changed, 99 insertions, 82 deletions
diff --git a/core/fxcodec/gif/cfx_gifcontext.cpp b/core/fxcodec/gif/cfx_gifcontext.cpp index 4083072fbe..f746bc13c5 100644 --- a/core/fxcodec/gif/cfx_gifcontext.cpp +++ b/core/fxcodec/gif/cfx_gifcontext.cpp @@ -342,8 +342,8 @@ CFX_GifDecodeStatus CFX_GifContext::LoadFrame(int32_t frame_num) { return CFX_GifDecodeStatus::Error; } -void CFX_GifContext::SetInputBuffer(uint8_t* src_buf, uint32_t src_size) { - input_buffer_ = pdfium::MakeRetain<CFX_CodecMemory>(src_buf, src_size); +void CFX_GifContext::SetInputBuffer(pdfium::span<uint8_t> src_buf) { + input_buffer_ = pdfium::MakeRetain<CFX_CodecMemory>(src_buf); } uint32_t CFX_GifContext::GetAvailInput(uint8_t** avail_buf) const { diff --git a/core/fxcodec/gif/cfx_gifcontext.h b/core/fxcodec/gif/cfx_gifcontext.h index f96cf058b2..90657b05bb 100644 --- a/core/fxcodec/gif/cfx_gifcontext.h +++ b/core/fxcodec/gif/cfx_gifcontext.h @@ -16,6 +16,7 @@ #include "core/fxcodec/gif/cfx_lzwdecompressor.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/unowned_ptr.h" +#include "third_party/base/span.h" class CFX_GifContext : public CCodec_GifModule::Context { public: @@ -40,7 +41,7 @@ class CFX_GifContext : public CCodec_GifModule::Context { CFX_GifDecodeStatus ReadHeader(); CFX_GifDecodeStatus GetFrame(); CFX_GifDecodeStatus LoadFrame(int32_t frame_num); - void SetInputBuffer(uint8_t* src_buf, uint32_t src_size); + void SetInputBuffer(pdfium::span<uint8_t> src_buf); uint32_t GetAvailInput(uint8_t** avail_buf) const; size_t GetFrameNum() const { return images_.size(); } diff --git a/core/fxcodec/gif/cfx_gifcontext_unittest.cpp b/core/fxcodec/gif/cfx_gifcontext_unittest.cpp index d6cfa1fd0d..0d8941f72f 100644 --- a/core/fxcodec/gif/cfx_gifcontext_unittest.cpp +++ b/core/fxcodec/gif/cfx_gifcontext_unittest.cpp @@ -22,122 +22,134 @@ class CFX_GifContextForTest : public CFX_GifContext { }; TEST(CFX_GifContext, SetInputBuffer) { - CFX_GifContextForTest context(nullptr, nullptr); + uint8_t buffer[] = {0x00, 0x01, 0x02}; + { + // Context must not outlive its buffers. + CFX_GifContextForTest context(nullptr, nullptr); - context.SetInputBuffer(nullptr, 0); - EXPECT_EQ(nullptr, context.InputBuffer()->GetBuffer()); - EXPECT_EQ(0u, context.InputBuffer()->GetSize()); - EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({nullptr, 0}); + EXPECT_EQ(nullptr, context.InputBuffer()->GetBuffer()); + EXPECT_EQ(0u, context.InputBuffer()->GetSize()); + EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); - context.SetInputBuffer(nullptr, 100); - EXPECT_EQ(nullptr, context.InputBuffer()->GetBuffer()); - EXPECT_EQ(100u, context.InputBuffer()->GetSize()); - EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({nullptr, 100}); + EXPECT_EQ(nullptr, context.InputBuffer()->GetBuffer()); + EXPECT_EQ(100u, context.InputBuffer()->GetSize()); + EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); - uint8_t buffer[] = {0x00, 0x01, 0x02}; - context.SetInputBuffer(buffer, 0); - EXPECT_EQ(buffer, context.InputBuffer()->GetBuffer()); - EXPECT_EQ(0u, context.InputBuffer()->GetSize()); - EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); - - context.SetInputBuffer(buffer, 3); - EXPECT_EQ(buffer, context.InputBuffer()->GetBuffer()); - EXPECT_EQ(3u, context.InputBuffer()->GetSize()); - EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); - - context.SetInputBuffer(buffer, 100); - EXPECT_EQ(buffer, context.InputBuffer()->GetBuffer()); - EXPECT_EQ(100u, context.InputBuffer()->GetSize()); - EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); -} + context.SetInputBuffer({buffer, 0}); + EXPECT_EQ(buffer, context.InputBuffer()->GetBuffer()); + EXPECT_EQ(0u, context.InputBuffer()->GetSize()); + EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); -TEST(CFX_GifContext, ReadData) { - CFX_GifContextForTest context(nullptr, nullptr); + context.SetInputBuffer({buffer, 3}); + EXPECT_EQ(buffer, context.InputBuffer()->GetBuffer()); + EXPECT_EQ(3u, context.InputBuffer()->GetSize()); + EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); - context.SetInputBuffer(nullptr, 0); - EXPECT_FALSE(context.ReadData(nullptr, 0)); - EXPECT_FALSE(context.ReadData(nullptr, 10)); + context.SetInputBuffer({buffer, 100}); + EXPECT_EQ(buffer, context.InputBuffer()->GetBuffer()); + EXPECT_EQ(100u, context.InputBuffer()->GetSize()); + EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); + } +} +TEST(CFX_GifContext, ReadData) { std::vector<uint8_t> dest_buffer; - EXPECT_FALSE(context.ReadData(dest_buffer.data(), 0)); - EXPECT_FALSE(context.ReadData(dest_buffer.data(), 10)); - uint8_t src_buffer[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; - context.SetInputBuffer(src_buffer, 0); - dest_buffer.resize(sizeof(src_buffer)); - EXPECT_FALSE(context.ReadData(dest_buffer.data(), sizeof(src_buffer))); - - context.SetInputBuffer(src_buffer, 1); - EXPECT_FALSE(context.ReadData(dest_buffer.data(), sizeof(src_buffer))); - EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); - EXPECT_FALSE(context.ReadData(nullptr, sizeof(src_buffer))); - EXPECT_FALSE(context.ReadData(nullptr, 1)); - EXPECT_TRUE(context.ReadData(dest_buffer.data(), 1)); - EXPECT_EQ(src_buffer[0], dest_buffer[0]); - - context.SetInputBuffer(src_buffer, sizeof(src_buffer)); - EXPECT_FALSE(context.ReadData(nullptr, sizeof(src_buffer))); - EXPECT_TRUE(context.ReadData(dest_buffer.data(), sizeof(src_buffer))); - for (size_t i = 0; i < sizeof(src_buffer); i++) - EXPECT_EQ(src_buffer[i], dest_buffer[i]); - - context.SetInputBuffer(src_buffer, sizeof(src_buffer)); - for (size_t i = 0; i < sizeof(src_buffer); i++) { + { + // Context must not outlive its buffers. + CFX_GifContextForTest context(nullptr, nullptr); + + context.SetInputBuffer({nullptr, 0}); + EXPECT_FALSE(context.ReadData(nullptr, 0)); + EXPECT_FALSE(context.ReadData(nullptr, 10)); + + EXPECT_FALSE(context.ReadData(dest_buffer.data(), 0)); + EXPECT_FALSE(context.ReadData(dest_buffer.data(), 10)); + + context.SetInputBuffer({src_buffer, 0}); + dest_buffer.resize(sizeof(src_buffer)); + EXPECT_FALSE(context.ReadData(dest_buffer.data(), sizeof(src_buffer))); + + context.SetInputBuffer({src_buffer, 1}); + EXPECT_FALSE(context.ReadData(dest_buffer.data(), sizeof(src_buffer))); + EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); + EXPECT_FALSE(context.ReadData(nullptr, sizeof(src_buffer))); + EXPECT_FALSE(context.ReadData(nullptr, 1)); EXPECT_TRUE(context.ReadData(dest_buffer.data(), 1)); - EXPECT_EQ(src_buffer[i], dest_buffer[0]); + EXPECT_EQ(src_buffer[0], dest_buffer[0]); + + context.SetInputBuffer(src_buffer); + EXPECT_FALSE(context.ReadData(nullptr, sizeof(src_buffer))); + EXPECT_TRUE(context.ReadData(dest_buffer.data(), sizeof(src_buffer))); + for (size_t i = 0; i < sizeof(src_buffer); i++) + EXPECT_EQ(src_buffer[i], dest_buffer[i]); + + context.SetInputBuffer(src_buffer); + for (size_t i = 0; i < sizeof(src_buffer); i++) { + EXPECT_TRUE(context.ReadData(dest_buffer.data(), 1)); + EXPECT_EQ(src_buffer[i], dest_buffer[0]); + } } } TEST(CFX_GifContext, ReadGifSignature) { CFX_GifContextForTest context(nullptr, nullptr); - { uint8_t data[1]; - context.SetInputBuffer(data, 0); + context.SetInputBuffer({data, 0}); EXPECT_EQ(CFX_GifDecodeStatus::Unfinished, context.ReadGifSignature()); EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // Make sure testing the entire signature { uint8_t data[] = {'G', 'I', 'F'}; - context.SetInputBuffer(data, sizeof(data)); + context.SetInputBuffer(data); EXPECT_EQ(CFX_GifDecodeStatus::Unfinished, context.ReadGifSignature()); EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } { uint8_t data[] = {'N', 'O', 'T', 'G', 'I', 'F'}; - context.SetInputBuffer(data, sizeof(data)); + context.SetInputBuffer(data); EXPECT_EQ(CFX_GifDecodeStatus::Error, context.ReadGifSignature()); EXPECT_EQ(6u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // Make sure not matching GIF8*a { uint8_t data[] = {'G', 'I', 'F', '8', '0', 'a'}; - context.SetInputBuffer(data, sizeof(data)); + context.SetInputBuffer(data); EXPECT_EQ(CFX_GifDecodeStatus::Error, context.ReadGifSignature()); EXPECT_EQ(6u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // Make sure not matching GIF**a { uint8_t data[] = {'G', 'I', 'F', '9', '2', 'a'}; - context.SetInputBuffer(data, sizeof(data)); + context.SetInputBuffer(data); EXPECT_EQ(CFX_GifDecodeStatus::Error, context.ReadGifSignature()); EXPECT_EQ(6u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // One valid signature { uint8_t data[] = {'G', 'I', 'F', '8', '7', 'a'}; - context.SetInputBuffer(data, sizeof(data)); + context.SetInputBuffer(data); EXPECT_EQ(CFX_GifDecodeStatus::Success, context.ReadGifSignature()); EXPECT_EQ(6u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // The other valid signature { uint8_t data[] = {'G', 'I', 'F', '8', '9', 'a'}; - context.SetInputBuffer(data, sizeof(data)); + context.SetInputBuffer(data); EXPECT_EQ(CFX_GifDecodeStatus::Success, context.ReadGifSignature()); EXPECT_EQ(6u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } } @@ -145,15 +157,16 @@ TEST(CFX_GifContext, ReadLocalScreenDescriptor) { CFX_GifContextForTest context(nullptr, nullptr); { uint8_t data[1]; - context.SetInputBuffer(data, 0); + context.SetInputBuffer({data, 0}); EXPECT_EQ(CFX_GifDecodeStatus::Unfinished, context.ReadLogicalScreenDescriptor()); + context.SetInputBuffer({}); } // LSD with all the values zero'd { uint8_t lsd[sizeof(CFX_GifLocalScreenDescriptor)]; memset(&lsd, 0, sizeof(CFX_GifLocalScreenDescriptor)); - context.SetInputBuffer(lsd, sizeof(CFX_GifLocalScreenDescriptor)); + context.SetInputBuffer(lsd); EXPECT_EQ(CFX_GifDecodeStatus::Success, context.ReadLogicalScreenDescriptor()); @@ -164,12 +177,13 @@ TEST(CFX_GifContext, ReadLocalScreenDescriptor) { EXPECT_EQ(0, context.height_); EXPECT_EQ(0u, context.bc_index_); EXPECT_EQ(0u, context.pixel_aspect_); + context.SetInputBuffer({}); } // LSD with no global palette { uint8_t lsd[sizeof(CFX_GifLocalScreenDescriptor)] = {0x0A, 0x00, 0x00, 0x0F, 0x00, 0x01, 0x02}; - context.SetInputBuffer(lsd, sizeof(CFX_GifLocalScreenDescriptor)); + context.SetInputBuffer(lsd); EXPECT_EQ(CFX_GifDecodeStatus::Success, context.ReadLogicalScreenDescriptor()); @@ -180,17 +194,19 @@ TEST(CFX_GifContext, ReadLocalScreenDescriptor) { EXPECT_EQ(0x0F00, context.height_); EXPECT_EQ(0u, context.bc_index_); // bc_index_ is 0 if no global palette EXPECT_EQ(2u, context.pixel_aspect_); + context.SetInputBuffer({}); } // LSD with global palette bit set, but no global palette { uint8_t lsd[sizeof(CFX_GifLocalScreenDescriptor)] = {0x0A, 0x00, 0x00, 0x0F, 0x80, 0x01, 0x02}; - context.SetInputBuffer(lsd, sizeof(CFX_GifLocalScreenDescriptor)); + context.SetInputBuffer(lsd); EXPECT_EQ(CFX_GifDecodeStatus::Unfinished, context.ReadLogicalScreenDescriptor()); EXPECT_EQ(0u, context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // LSD with global palette { @@ -199,7 +215,7 @@ TEST(CFX_GifContext, ReadLocalScreenDescriptor) { uint8_t palette[4 * sizeof(CFX_GifPalette)]; } data = {{0x0A, 0x00, 0x00, 0x0F, 0xA9, 0x01, 0x02}, {0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1}}; - context.SetInputBuffer(reinterpret_cast<uint8_t*>(&data), sizeof(data)); + context.SetInputBuffer({reinterpret_cast<uint8_t*>(&data), sizeof(data)}); EXPECT_EQ(CFX_GifDecodeStatus::Success, context.ReadLogicalScreenDescriptor()); @@ -209,12 +225,12 @@ TEST(CFX_GifContext, ReadLocalScreenDescriptor) { EXPECT_EQ(0x0F00, context.height_); EXPECT_EQ(1u, context.bc_index_); EXPECT_EQ(2u, context.pixel_aspect_); - EXPECT_EQ(1u, context.global_pal_exp_); EXPECT_EQ(1, context.global_sort_flag_); EXPECT_EQ(2, context.global_color_resolution_); EXPECT_EQ(0, memcmp(data.palette, context.global_palette_.data(), sizeof(data.palette))); + context.SetInputBuffer({}); } } @@ -227,21 +243,21 @@ TEST(CFX_GifContext, ReadHeader) { uint8_t lsd[sizeof(CFX_GifLocalScreenDescriptor)]; } data = {{'N', 'O', 'T', 'G', 'I', 'F'}, {0x0A, 0x00, 0x00, 0x0F, 0x00, 0x01, 0x02}}; - context.SetInputBuffer(reinterpret_cast<uint8_t*>(&data), sizeof(data)); + context.SetInputBuffer({reinterpret_cast<uint8_t*>(&data), sizeof(data)}); EXPECT_EQ(CFX_GifDecodeStatus::Error, context.ReadHeader()); - EXPECT_EQ(sizeof(data.signature), context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // Short after signature { uint8_t signature[] = {'G', 'I', 'F', '8', '7', 'a'}; - context.SetInputBuffer(reinterpret_cast<uint8_t*>(&signature), - sizeof(signature)); + context.SetInputBuffer( + {reinterpret_cast<uint8_t*>(&signature), sizeof(signature)}); EXPECT_EQ(CFX_GifDecodeStatus::Unfinished, context.ReadHeader()); - EXPECT_EQ(sizeof(signature), context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // Success without global palette { @@ -250,15 +266,15 @@ TEST(CFX_GifContext, ReadHeader) { uint8_t lsd[sizeof(CFX_GifLocalScreenDescriptor)]; } data = {{'G', 'I', 'F', '8', '7', 'a'}, {0x0A, 0x00, 0x00, 0x0F, 0x00, 0x01, 0x02}}; - context.SetInputBuffer(reinterpret_cast<uint8_t*>(&data), sizeof(data)); + context.SetInputBuffer({reinterpret_cast<uint8_t*>(&data), sizeof(data)}); EXPECT_EQ(CFX_GifDecodeStatus::Success, context.ReadHeader()); - EXPECT_EQ(sizeof(data), context.InputBuffer()->GetPosition()); EXPECT_EQ(0x000A, context.width_); EXPECT_EQ(0x0F00, context.height_); EXPECT_EQ(0u, context.bc_index_); // bc_index_ is 0 if no global palette EXPECT_EQ(2u, context.pixel_aspect_); + context.SetInputBuffer({}); } // Missing Global Palette { @@ -267,11 +283,11 @@ TEST(CFX_GifContext, ReadHeader) { uint8_t lsd[sizeof(CFX_GifLocalScreenDescriptor)]; } data = {{'G', 'I', 'F', '8', '7', 'a'}, {0x0A, 0x00, 0x00, 0x0F, 0x80, 0x01, 0x02}}; - context.SetInputBuffer(reinterpret_cast<uint8_t*>(&data), sizeof(data)); + context.SetInputBuffer({reinterpret_cast<uint8_t*>(&data), sizeof(data)}); EXPECT_EQ(CFX_GifDecodeStatus::Unfinished, context.ReadHeader()); - EXPECT_EQ(sizeof(data.signature), context.InputBuffer()->GetPosition()); + context.SetInputBuffer({}); } // Success with global palette { @@ -282,10 +298,9 @@ TEST(CFX_GifContext, ReadHeader) { } data = {{'G', 'I', 'F', '8', '7', 'a'}, {0x0A, 0x00, 0x00, 0x0F, 0xA9, 0x01, 0x02}, {0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1}}; - context.SetInputBuffer(reinterpret_cast<uint8_t*>(&data), sizeof(data)); + context.SetInputBuffer({reinterpret_cast<uint8_t*>(&data), sizeof(data)}); EXPECT_EQ(CFX_GifDecodeStatus::Success, context.ReadHeader()); - EXPECT_EQ(sizeof(data), context.InputBuffer()->GetPosition()); EXPECT_EQ(0x000A, context.width_); EXPECT_EQ(0x0F00, context.height_); @@ -296,5 +311,6 @@ TEST(CFX_GifContext, ReadHeader) { EXPECT_EQ(2, context.global_color_resolution_); EXPECT_EQ(0, memcmp(data.palette, context.global_palette_.data(), sizeof(data.palette))); + context.SetInputBuffer({}); } } |