summaryrefslogtreecommitdiff
path: root/core/fpdfapi
diff options
context:
space:
mode:
Diffstat (limited to 'core/fpdfapi')
-rw-r--r--core/fpdfapi/fpdf_page/fpdf_page_parser.cpp72
-rw-r--r--core/fpdfapi/fpdf_page/fpdf_page_parser_unittest.cpp34
-rw-r--r--core/fpdfapi/fpdf_page/pageint.h4
3 files changed, 69 insertions, 41 deletions
diff --git a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp
index bf691f41d2..daddb9f05b 100644
--- a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp
+++ b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp
@@ -41,42 +41,24 @@ const char kPathOperatorCubicBezier3 = 'y';
const char kPathOperatorClosePath = 'h';
const char kPathOperatorRectangle[] = "re";
-struct _FX_BSTR {
- const FX_CHAR* m_Ptr;
- int m_Size;
+struct PDF_AbbrPair {
+ const FX_CHAR* abbr;
+ const FX_CHAR* full_name;
};
-#define _FX_BSTRC(str) \
- { str, sizeof(str) - 1 }
-struct PDF_AbbrPairs {
- _FX_BSTR full_name;
- _FX_BSTR abbr;
+const PDF_AbbrPair PDF_InlineKeyAbbr[] = {
+ {"BPC", "BitsPerComponent"}, {"CS", "ColorSpace"}, {"D", "Decode"},
+ {"DP", "DecodeParms"}, {"F", "Filter"}, {"H", "Height"},
+ {"IM", "ImageMask"}, {"I", "Interpolate"}, {"W", "Width"},
};
-const PDF_AbbrPairs PDF_InlineKeyAbbr[] = {
- {_FX_BSTRC("BitsPerComponent"), _FX_BSTRC("BPC")},
- {_FX_BSTRC("ColorSpace"), _FX_BSTRC("CS")},
- {_FX_BSTRC("Decode"), _FX_BSTRC("D")},
- {_FX_BSTRC("DecodeParms"), _FX_BSTRC("DP")},
- {_FX_BSTRC("Filter"), _FX_BSTRC("F")},
- {_FX_BSTRC("Height"), _FX_BSTRC("H")},
- {_FX_BSTRC("ImageMask"), _FX_BSTRC("IM")},
- {_FX_BSTRC("Interpolate"), _FX_BSTRC("I")},
- {_FX_BSTRC("Width"), _FX_BSTRC("W")},
-};
-
-const PDF_AbbrPairs PDF_InlineValueAbbr[] = {
- {_FX_BSTRC("DeviceGray"), _FX_BSTRC("G")},
- {_FX_BSTRC("DeviceRGB"), _FX_BSTRC("RGB")},
- {_FX_BSTRC("DeviceCMYK"), _FX_BSTRC("CMYK")},
- {_FX_BSTRC("Indexed"), _FX_BSTRC("I")},
- {_FX_BSTRC("ASCIIHexDecode"), _FX_BSTRC("AHx")},
- {_FX_BSTRC("ASCII85Decode"), _FX_BSTRC("A85")},
- {_FX_BSTRC("LZWDecode"), _FX_BSTRC("LZW")},
- {_FX_BSTRC("FlateDecode"), _FX_BSTRC("Fl")},
- {_FX_BSTRC("RunLengthDecode"), _FX_BSTRC("RL")},
- {_FX_BSTRC("CCITTFaxDecode"), _FX_BSTRC("CCF")},
- {_FX_BSTRC("DCTDecode"), _FX_BSTRC("DCT")},
+const PDF_AbbrPair PDF_InlineValueAbbr[] = {
+ {"G", "DeviceGray"}, {"RGB", "DeviceRGB"},
+ {"CMYK", "DeviceCMYK"}, {"I", "Indexed"},
+ {"AHx", "ASCIIHexDecode"}, {"A85", "ASCII85Decode"},
+ {"LZW", "LZWDecode"}, {"Fl", "FlateDecode"},
+ {"RL", "RunLengthDecode"}, {"CCF", "CCITTFaxDecode"},
+ {"DCT", "DCTDecode"},
};
struct AbbrReplacementOp {
@@ -98,21 +80,29 @@ class CPDF_StreamParserAutoClearer {
CPDF_StreamParser** scoped_variable_;
};
-CFX_ByteStringC PDF_FindFullName(const PDF_AbbrPairs* table,
+CFX_ByteStringC PDF_FindFullName(const PDF_AbbrPair* table,
size_t count,
const CFX_ByteStringC& abbr) {
- for (size_t i = 0; i < count; ++i) {
- if (abbr.GetLength() != table[i].abbr.m_Size)
- continue;
- if (memcmp(abbr.GetPtr(), table[i].abbr.m_Ptr, abbr.GetLength()))
- continue;
- return CFX_ByteStringC(table[i].full_name.m_Ptr, table[i].full_name.m_Size);
- }
- return CFX_ByteStringC();
+ auto it = std::find_if(
+ table, table + count,
+ [abbr](const PDF_AbbrPair& pair) { return pair.abbr == abbr; });
+ return it != table + count ? CFX_ByteStringC(it->full_name)
+ : CFX_ByteStringC();
}
} // namespace
+CFX_ByteStringC PDF_FindKeyAbbreviationForTesting(const CFX_ByteStringC& abbr) {
+ return PDF_FindFullName(PDF_InlineKeyAbbr, FX_ArraySize(PDF_InlineKeyAbbr),
+ abbr);
+}
+
+CFX_ByteStringC PDF_FindValueAbbreviationForTesting(
+ const CFX_ByteStringC& abbr) {
+ return PDF_FindFullName(PDF_InlineValueAbbr,
+ FX_ArraySize(PDF_InlineValueAbbr), abbr);
+}
+
bool IsPathOperator(const uint8_t* buf, size_t len) {
if (len == 1) {
uint8_t op = buf[0];
diff --git a/core/fpdfapi/fpdf_page/fpdf_page_parser_unittest.cpp b/core/fpdfapi/fpdf_page/fpdf_page_parser_unittest.cpp
new file mode 100644
index 0000000000..3da36a8f8c
--- /dev/null
+++ b/core/fpdfapi/fpdf_page/fpdf_page_parser_unittest.cpp
@@ -0,0 +1,34 @@
+// Copyright 2016 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/fpdfapi/fpdf_page/pageint.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(fpdf_page_parser, PDF_FindKeyAbbreviation) {
+ EXPECT_EQ(CFX_ByteStringC("BitsPerComponent"),
+ PDF_FindKeyAbbreviationForTesting(CFX_ByteStringC("BPC")));
+ EXPECT_EQ(CFX_ByteStringC("Width"),
+ PDF_FindKeyAbbreviationForTesting(CFX_ByteStringC("W")));
+ EXPECT_EQ(CFX_ByteStringC(""),
+ PDF_FindKeyAbbreviationForTesting(CFX_ByteStringC("")));
+ EXPECT_EQ(CFX_ByteStringC(""),
+ PDF_FindKeyAbbreviationForTesting(CFX_ByteStringC("NoInList")));
+ // Prefix should not match.
+ EXPECT_EQ(CFX_ByteStringC(""),
+ PDF_FindKeyAbbreviationForTesting(CFX_ByteStringC("WW")));
+}
+
+TEST(fpdf_page_parser, PDF_FindValueAbbreviation) {
+ EXPECT_EQ(CFX_ByteStringC("DeviceGray"),
+ PDF_FindValueAbbreviationForTesting(CFX_ByteStringC("G")));
+ EXPECT_EQ(CFX_ByteStringC("DCTDecode"),
+ PDF_FindValueAbbreviationForTesting(CFX_ByteStringC("DCT")));
+ EXPECT_EQ(CFX_ByteStringC(""),
+ PDF_FindValueAbbreviationForTesting(CFX_ByteStringC("")));
+ EXPECT_EQ(CFX_ByteStringC(""),
+ PDF_FindValueAbbreviationForTesting(CFX_ByteStringC("NoInList")));
+ // Prefix should not match.
+ EXPECT_EQ(CFX_ByteStringC(""),
+ PDF_FindValueAbbreviationForTesting(CFX_ByteStringC("II")));
+}
diff --git a/core/fpdfapi/fpdf_page/pageint.h b/core/fpdfapi/fpdf_page/pageint.h
index 4fa62c03cb..0909125ab0 100644
--- a/core/fpdfapi/fpdf_page/pageint.h
+++ b/core/fpdfapi/fpdf_page/pageint.h
@@ -505,6 +505,10 @@ struct PatternValue {
FX_FLOAT m_Comps[MAX_PATTERN_COLORCOMPS];
};
+CFX_ByteStringC PDF_FindKeyAbbreviationForTesting(const CFX_ByteStringC& abbr);
+CFX_ByteStringC PDF_FindValueAbbreviationForTesting(
+ const CFX_ByteStringC& abbr);
+
void PDF_ReplaceAbbr(CPDF_Object* pObj);
bool IsPathOperator(const uint8_t* buf, size_t len);