Skip to content

Commit 1e80047

Browse files
LucasCholletawesomekling
authored andcommitted
LibPDF: Don't consider the End of Data code as normal ASCII85 input
Data encoded with ASCII85 is terminated with the EOD code 0x7E3E. This should not be considered as normal input but rather discarded.
1 parent 59a6d4b commit 1e80047

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Userland/Libraries/LibPDF/Filter.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ PDFErrorOr<ByteBuffer> Filter::decode_ascii_hex(ReadonlyBytes bytes)
9494

9595
PDFErrorOr<ByteBuffer> Filter::decode_ascii85(ReadonlyBytes bytes)
9696
{
97+
// 3.3.2 ASCII85Decode Filter
98+
9799
ByteBuffer buffer;
98100
TRY(buffer.try_ensure_capacity(bytes.size()));
99101

@@ -114,10 +116,21 @@ PDFErrorOr<ByteBuffer> Filter::decode_ascii85(ReadonlyBytes bytes)
114116

115117
u32 number = 0;
116118

117-
auto const to_write = byte_index + 5 >= bytes.size() ? bytes.size() - byte_index : 5;
119+
auto to_write = byte_index + 5 >= bytes.size() ? bytes.size() - byte_index : 5;
120+
121+
Optional<u32> end_of_data_index {};
118122

119123
for (int i = 0; i < 5; i++) {
120-
auto byte = byte_index >= bytes.size() ? 'u' : bytes[byte_index++];
124+
// We check for the EOD sequence '~>', but as '~' can only appear in
125+
// this sequence, there is no need to check for '>'.
126+
if (!end_of_data_index.has_value() && bytes[byte_index] == '~') {
127+
end_of_data_index = i;
128+
to_write = i + 1;
129+
}
130+
131+
bool const should_fake_end = byte_index >= bytes.size() || end_of_data_index.has_value();
132+
auto const byte = should_fake_end ? 'u' : bytes[byte_index++];
133+
121134
if (Reader::is_whitespace(byte)) {
122135
i--;
123136
continue;
@@ -127,6 +140,9 @@ PDFErrorOr<ByteBuffer> Filter::decode_ascii85(ReadonlyBytes bytes)
127140

128141
for (size_t i = 0; i < to_write - 1; i++)
129142
buffer.append(reinterpret_cast<u8*>(&number)[3 - i]);
143+
144+
if (end_of_data_index.has_value())
145+
break;
130146
}
131147

132148
return buffer;

0 commit comments

Comments
 (0)