1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <cstring>
6 #include <string>
7
8 #include "core/include/fpdfapi/fpdf_parser.h"
9 #include "core/include/fxcrt/fx_basic.h"
10 #include "testing/embedder_test.h"
11 #include "testing/fx_string_testhelpers.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 class FPDFParserDecodeEmbeddertest : public EmbedderTest {};
15
16 // NOTE: python's zlib.compress() and zlib.decompress() may be useful for
17 // external validation of the FlateEncode/FlateDecode test cases.
18
19 #define TEST_CASE(input_literal, expected_literal) \
20 { \
21 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \
22 (const unsigned char*)expected_literal, sizeof(expected_literal) - 1 \
23 }
24
TEST_F(FPDFParserDecodeEmbeddertest,FlateEncode)25 TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) {
26 struct FlateEncodeCase {
27 const unsigned char* input;
28 unsigned int input_size;
29 const unsigned char* expected;
30 unsigned int expected_size;
31 } flate_encode_cases[] = {
32 TEST_CASE("", "\x78\x9c\x03\x00\x00\x00\x00\x01"),
33 TEST_CASE(" ", "\x78\x9c\x53\x00\x00\x00\x21\x00\x21"),
34 TEST_CASE("123", "\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97"),
35 TEST_CASE("\x00\xff", "\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00"),
36 TEST_CASE(
37 "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n"
38 "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\n"
39 "0 0 693 917 re\nf\nQ\nQ\n",
40 "\x78\x9c\x33\x54\x30\x00\x42\x5d\x43\x05\x23\x4b\x05\x73\x33\x63"
41 "\x85\xe4\x5c\x2e\x90\x80\xa9\xa9\xa9\x82\xb9\xb1\xa9\x42\x51\x2a"
42 "\x57\xb8\x42\x1e\x57\x21\x92\xa0\x89\x9e\xb1\xa5\x09\x92\x84\x9e"
43 "\x85\x81\x81\x25\xd8\x14\x24\x26\xd0\x18\x43\x05\x10\x0c\x72\x57"
44 "\x80\x30\x8a\xd2\xb9\xf4\xdd\x0d\x14\xd2\x8b\xc1\x46\x99\x59\x1a"
45 "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42"),
46 };
47
48 for (size_t i = 0; i < FX_ArraySize(flate_encode_cases); ++i) {
49 FlateEncodeCase* ptr = &flate_encode_cases[i];
50 unsigned char* result;
51 unsigned int result_size;
52 FlateEncode(ptr->input, ptr->input_size, result, result_size);
53 ASSERT_TRUE(result);
54 EXPECT_EQ(std::string((const char*)ptr->expected, ptr->expected_size),
55 std::string((const char*)result, result_size))
56 << " for case " << i;
57 FX_Free(result);
58 }
59 }
60
TEST_F(FPDFParserDecodeEmbeddertest,FlateDecode)61 TEST_F(FPDFParserDecodeEmbeddertest, FlateDecode) {
62 struct FlateDecodeCase {
63 const unsigned char* input;
64 unsigned int input_size;
65 const unsigned char* expected;
66 unsigned int expected_size;
67 } flate_decode_cases[] = {
68 TEST_CASE("", ""), TEST_CASE("preposterous nonsense", ""),
69 TEST_CASE("\x78\x9c\x03\x00\x00\x00\x00\x01", ""),
70 TEST_CASE("\x78\x9c\x53\x00\x00\x00\x21\x00\x21", " "),
71 TEST_CASE("\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97", "123"),
72 TEST_CASE("\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00", "\x00\xff"),
73 TEST_CASE(
74 "\x78\x9c\x33\x54\x30\x00\x42\x5d\x43\x05\x23\x4b\x05\x73\x33\x63"
75 "\x85\xe4\x5c\x2e\x90\x80\xa9\xa9\xa9\x82\xb9\xb1\xa9\x42\x51\x2a"
76 "\x57\xb8\x42\x1e\x57\x21\x92\xa0\x89\x9e\xb1\xa5\x09\x92\x84\x9e"
77 "\x85\x81\x81\x25\xd8\x14\x24\x26\xd0\x18\x43\x05\x10\x0c\x72\x57"
78 "\x80\x30\x8a\xd2\xb9\xf4\xdd\x0d\x14\xd2\x8b\xc1\x46\x99\x59\x1a"
79 "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42",
80 "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n"
81 "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\n"
82 "0 0 693 917 re\nf\nQ\nQ\n"),
83 };
84
85 for (size_t i = 0; i < FX_ArraySize(flate_decode_cases); ++i) {
86 FlateDecodeCase* ptr = &flate_decode_cases[i];
87 unsigned char* result;
88 unsigned int result_size;
89 FlateDecode(ptr->input, ptr->input_size, result, result_size);
90 ASSERT_TRUE(result);
91 EXPECT_EQ(std::string((const char*)ptr->expected, ptr->expected_size),
92 std::string((const char*)result, result_size))
93 << " for case " << i;
94 FX_Free(result);
95 }
96 }
97
TEST_F(FPDFParserDecodeEmbeddertest,Bug_552046)98 TEST_F(FPDFParserDecodeEmbeddertest, Bug_552046) {
99 // Tests specifying multiple image filters for a stream. Should not cause a
100 // crash when rendered.
101 EXPECT_TRUE(OpenDocument("bug_552046.pdf"));
102 FPDF_PAGE page = LoadPage(0);
103 FPDF_BITMAP bitmap = RenderPage(page);
104 FPDFBitmap_Destroy(bitmap);
105 UnloadPage(page);
106 }
107
TEST_F(FPDFParserDecodeEmbeddertest,Bug_555784)108 TEST_F(FPDFParserDecodeEmbeddertest, Bug_555784) {
109 // Tests bad input to the run length decoder that caused a heap overflow.
110 // Should not cause a crash when rendered.
111 EXPECT_TRUE(OpenDocument("bug_555784.pdf"));
112 FPDF_PAGE page = LoadPage(0);
113 FPDF_BITMAP bitmap = RenderPage(page);
114 FPDFBitmap_Destroy(bitmap);
115 UnloadPage(page);
116 }
117
118 #undef TEST_CASE
119