| 1 |
// SPDX-FileCopyrightText: 2007 Dominik Seichter <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]> |
| 3 |
// SPDX-License-Identifier: MIT-0 |
| 4 |
|
| 5 |
#include <PdfTest.h> |
| 6 |
#include <podofo/private/PdfFilterFactory.h> |
| 7 |
|
| 8 |
using namespace std; |
| 9 |
using namespace PoDoFo; |
| 10 |
|
| 11 |
static void testFilter(PdfFilterType filterType, const bufferview& buffer); |
| 12 |
|
| 13 |
static string_view s_testBuffer1 = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."; |
| 14 |
|
| 15 |
const char s_testBuffer2[] = { |
| 16 |
0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x01, |
| 17 |
0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x03, |
| 18 |
0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x02, |
| 19 |
0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x00, |
| 20 |
0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x00, |
| 21 |
0x00, 0x00, 0x00, 0x00, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 22 |
}; |
| 23 |
|
| 24 |
TEST_CASE("TestFilters") |
| 25 |
{ |
| 26 |
for (unsigned i = 0; i <= (unsigned)PdfFilterType::Crypt; i++) |
| 27 |
{ |
| 28 |
testFilter(static_cast<PdfFilterType>(i), { s_testBuffer1.data(), s_testBuffer1.length() }); |
| 29 |
testFilter(static_cast<PdfFilterType>(i), { s_testBuffer2, std::size(s_testBuffer2) }); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// RunLengthDecode can't encode, so it's never exercised by TestFilters above: |
| 34 |
// decode a stream combining a literal run, a repeat run and a single-byte |
| 35 |
// literal run (control byte 0) directly instead |
| 36 |
TEST_CASE("TestRunLengthDecodeFilter") |
| 37 |
{ |
| 38 |
const char input[] = { |
| 39 |
0x02, 'A', 'B', 'C', |
| 40 |
static_cast<char>(0xFE), 'Z', |
| 41 |
0x00, 'Q', |
| 42 |
static_cast<char>(0x80), |
| 43 |
}; |
| 44 |
|
| 45 |
unique_ptr<PdfFilter> filter; |
| 46 |
REQUIRE(PdfFilterFactory::TryCreate(PdfFilterType::RunLengthDecode, filter)); |
| 47 |
|
| 48 |
charbuff decoded; |
| 49 |
filter->DecodeTo(decoded, bufferview(input, std::size(input))); |
| 50 |
REQUIRE(decoded == "ABCZZZQ"); |
| 51 |
} |
| 52 |
|
| 53 |
void testFilter(PdfFilterType filterType, const bufferview& view) |
| 54 |
{ |
| 55 |
charbuff encoded; |
| 56 |
charbuff decoded; |
| 57 |
|
| 58 |
unique_ptr<PdfFilter> filter; |
| 59 |
if (!PdfFilterFactory::TryCreate(filterType, filter)) |
| 60 |
{ |
| 61 |
INFO(utls::Format("!!! Filter {} not implemented.\n", (int)filterType)); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
INFO(utls::Format("Testing Algorithm {}:", (int)filterType)); |
| 66 |
INFO("\t-> Testing Encoding"); |
| 67 |
try |
| 68 |
{ |
| 69 |
filter->EncodeTo(encoded, view); |
| 70 |
} |
| 71 |
catch (PdfError& e) |
| 72 |
{ |
| 73 |
if (e == PdfErrorCode::UnsupportedFilter) |
| 74 |
{ |
| 75 |
INFO(utls::Format("\t-> Encoding not supported for filter {}.", (unsigned)filterType)); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
throw; |
| 80 |
} |
| 81 |
|
| 82 |
INFO("\t-> Testing Decoding"); |
| 83 |
try |
| 84 |
{ |
| 85 |
filter->DecodeTo(decoded, encoded); |
| 86 |
} |
| 87 |
catch (PdfError& e) |
| 88 |
{ |
| 89 |
if (e == PdfErrorCode::UnsupportedFilter) |
| 90 |
{ |
| 91 |
INFO(utls::Format("\t-> Decoding not supported for filter {}", (int)filterType)); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
throw; |
| 96 |
} |
| 97 |
|
| 98 |
INFO(utls::Format("\t-> Original Data Length: {}", view.size())); |
| 99 |
INFO(utls::Format("\t-> Encoded Data Length: {}", encoded.size())); |
| 100 |
INFO(utls::Format("\t-> Decoded Data Length: {}", decoded.size())); |
| 101 |
|
| 102 |
REQUIRE(decoded == view); |
| 103 |
|
| 104 |
INFO("\t-> Test succeeded!"); |
| 105 |
} |