1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// 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 <limits>
#include <string>
#include "core/fpdfapi/parser/cpdf_object.h"
#include "core/fpdfapi/parser/cpdf_parser.h"
#include "core/fpdfapi/parser/cpdf_syntax_parser.h"
#include "core/fxcrt/cfx_memorystream.h"
#include "core/fxcrt/fx_extension.h"
#include "core/fxcrt/fx_stream.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/utils/path_service.h"
TEST(cpdf_syntax_parser, ReadHexString) {
{
// Empty string.
uint8_t data[] = "";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 0, false), 0);
EXPECT_EQ("", parser.ReadHexString());
EXPECT_EQ(0, parser.GetPos());
}
{
// Blank string.
uint8_t data[] = " ";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 2, false), 0);
EXPECT_EQ("", parser.ReadHexString());
EXPECT_EQ(2, parser.GetPos());
}
{
// Skips unknown characters.
uint8_t data[] = "z12b";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 4, false), 0);
EXPECT_EQ("\x12\xb0", parser.ReadHexString());
EXPECT_EQ(4, parser.GetPos());
}
{
// Skips unknown characters.
uint8_t data[] = "*<&*#$^&@1";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 10, false), 0);
EXPECT_EQ("\x10", parser.ReadHexString());
EXPECT_EQ(10, parser.GetPos());
}
{
// Skips unknown characters.
uint8_t data[] = "\x80zab";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 4, false), 0);
EXPECT_EQ("\xab", parser.ReadHexString());
EXPECT_EQ(4, parser.GetPos());
}
{
// Skips unknown characters.
uint8_t data[] = "\xffzab";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 4, false), 0);
EXPECT_EQ("\xab", parser.ReadHexString());
EXPECT_EQ(4, parser.GetPos());
}
{
// Regular conversion.
uint8_t data[] = "1A2b>abcd";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 9, false), 0);
EXPECT_EQ("\x1a\x2b", parser.ReadHexString());
EXPECT_EQ(5, parser.GetPos());
}
{
// Position out of bounds.
uint8_t data[] = "12ab>";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 5, false), 0);
parser.SetPos(5);
EXPECT_EQ("", parser.ReadHexString());
parser.SetPos(6);
EXPECT_EQ("", parser.ReadHexString());
parser.SetPos(-1);
EXPECT_EQ("", parser.ReadHexString());
parser.SetPos(std::numeric_limits<FX_FILESIZE>::max());
EXPECT_EQ("", parser.ReadHexString());
// Check string still parses when set to 0.
parser.SetPos(0);
EXPECT_EQ("\x12\xab", parser.ReadHexString());
}
{
// Missing ending >.
uint8_t data[] = "1A2b";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 4, false), 0);
EXPECT_EQ("\x1a\x2b", parser.ReadHexString());
EXPECT_EQ(4, parser.GetPos());
}
{
// Missing ending >.
uint8_t data[] = "12abz";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 5, false), 0);
EXPECT_EQ("\x12\xab", parser.ReadHexString());
EXPECT_EQ(5, parser.GetPos());
}
{
// Uneven number of bytes.
uint8_t data[] = "1A2>asdf";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 8, false), 0);
EXPECT_EQ("\x1a\x20", parser.ReadHexString());
EXPECT_EQ(4, parser.GetPos());
}
{
// Uneven number of bytes.
uint8_t data[] = "1A2zasdf";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 8, false), 0);
EXPECT_EQ("\x1a\x2a\xdf", parser.ReadHexString());
EXPECT_EQ(8, parser.GetPos());
}
{
// Just ending character.
uint8_t data[] = ">";
CPDF_SyntaxParser parser;
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 1, false), 0);
EXPECT_EQ("", parser.ReadHexString());
EXPECT_EQ(1, parser.GetPos());
}
}
TEST(cpdf_syntax_parser, GetInvalidReference) {
CPDF_SyntaxParser parser;
// Data with a reference with number CPDF_Object::kInvalidObjNum
uint8_t data[] = "4294967295 0 R";
parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 14, false), 0);
std::unique_ptr<CPDF_Object> ref =
parser.GetObject(nullptr, CPDF_Object::kInvalidObjNum, 0, false);
EXPECT_FALSE(ref);
}
|