/src/qpdf/libqpdf/Pl_RunLength.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <qpdf/Pl_RunLength.hh> |
2 | | |
3 | | #include <qpdf/QTC.hh> |
4 | | #include <qpdf/QUtil.hh> |
5 | | |
6 | | Pl_RunLength::Members::Members(action_e action) : |
7 | | action(action), |
8 | | state(st_top), |
9 | | length(0) |
10 | 404 | { |
11 | 404 | } |
12 | | |
13 | | Pl_RunLength::Pl_RunLength(char const* identifier, Pipeline* next, action_e action) : |
14 | | Pipeline(identifier, next), |
15 | | m(new Members(action)) |
16 | 404 | { |
17 | 404 | } |
18 | | |
19 | | Pl_RunLength::~Pl_RunLength() // NOLINT (modernize-use-equals-default) |
20 | 404 | { |
21 | | // Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer |
22 | 404 | } |
23 | | |
24 | | void |
25 | | Pl_RunLength::write(unsigned char const* data, size_t len) |
26 | 29.8k | { |
27 | 29.8k | if (m->action == a_encode) { |
28 | 0 | encode(data, len); |
29 | 29.8k | } else { |
30 | 29.8k | decode(data, len); |
31 | 29.8k | } |
32 | 29.8k | } |
33 | | |
34 | | void |
35 | | Pl_RunLength::encode(unsigned char const* data, size_t len) |
36 | 0 | { |
37 | 0 | for (size_t i = 0; i < len; ++i) { |
38 | 0 | if ((m->state == st_top) != (m->length <= 1)) { |
39 | 0 | throw std::logic_error("Pl_RunLength::encode: state/length inconsistency"); |
40 | 0 | } |
41 | 0 | unsigned char ch = data[i]; |
42 | 0 | if ((m->length > 0) && ((m->state == st_copying) || (m->length < 128)) && |
43 | 0 | (ch == m->buf[m->length - 1])) { |
44 | 0 | QTC::TC("libtests", "Pl_RunLength: switch to run", (m->length == 128) ? 0 : 1); |
45 | 0 | if (m->state == st_copying) { |
46 | 0 | --m->length; |
47 | 0 | flush_encode(); |
48 | 0 | m->buf[0] = ch; |
49 | 0 | m->length = 1; |
50 | 0 | } |
51 | 0 | m->state = st_run; |
52 | 0 | m->buf[m->length] = ch; |
53 | 0 | ++m->length; |
54 | 0 | } else { |
55 | 0 | if ((m->length == 128) || (m->state == st_run)) { |
56 | 0 | flush_encode(); |
57 | 0 | } else if (m->length > 0) { |
58 | 0 | m->state = st_copying; |
59 | 0 | } |
60 | 0 | m->buf[m->length] = ch; |
61 | 0 | ++m->length; |
62 | 0 | } |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | void |
67 | | Pl_RunLength::decode(unsigned char const* data, size_t len) |
68 | 29.8k | { |
69 | 29.8k | m->out.reserve(len); |
70 | 29.2M | for (size_t i = 0; i < len; ++i) { |
71 | 29.2M | unsigned char const& ch = data[i]; |
72 | 29.2M | switch (m->state) { |
73 | 11.5M | case st_top: |
74 | 11.5M | if (ch < 128) { |
75 | | // length represents remaining number of bytes to copy |
76 | 6.13M | m->length = 1U + ch; |
77 | 6.13M | m->state = st_copying; |
78 | 6.13M | } else if (ch > 128) { |
79 | | // length represents number of copies of next byte |
80 | 5.36M | m->length = 257U - ch; |
81 | 5.36M | m->state = st_run; |
82 | 5.36M | } else // ch == 128 |
83 | 895 | { |
84 | | // EOD; stay in this state |
85 | 895 | } |
86 | 11.5M | break; |
87 | | |
88 | 12.3M | case st_copying: |
89 | 12.3M | m->out.append(1, static_cast<char>(ch)); |
90 | 12.3M | if (--m->length == 0) { |
91 | 6.13M | m->state = st_top; |
92 | 6.13M | } |
93 | 12.3M | break; |
94 | | |
95 | 5.36M | case st_run: |
96 | 5.36M | m->out.append(m->length, static_cast<char>(ch)); |
97 | 5.36M | m->state = st_top; |
98 | 5.36M | break; |
99 | 29.2M | } |
100 | 29.2M | } |
101 | 29.8k | } |
102 | | |
103 | | void |
104 | | Pl_RunLength::flush_encode() |
105 | 0 | { |
106 | 0 | if (m->length == 128) { |
107 | 0 | QTC::TC( |
108 | 0 | "libtests", |
109 | 0 | "Pl_RunLength flush full buffer", |
110 | 0 | (m->state == st_copying ? 0 |
111 | 0 | : m->state == st_run ? 1 |
112 | 0 | : -1)); |
113 | 0 | } |
114 | 0 | if (m->length == 0) { |
115 | 0 | QTC::TC("libtests", "Pl_RunLength flush empty buffer"); |
116 | 0 | } |
117 | 0 | if (m->state == st_run) { |
118 | 0 | if ((m->length < 2) || (m->length > 128)) { |
119 | 0 | throw std::logic_error("Pl_RunLength: invalid length in flush_encode for run"); |
120 | 0 | } |
121 | 0 | auto ch = static_cast<unsigned char>(257 - m->length); |
122 | 0 | this->getNext()->write(&ch, 1); |
123 | 0 | this->getNext()->write(&m->buf[0], 1); |
124 | 0 | } else if (m->length > 0) { |
125 | 0 | auto ch = static_cast<unsigned char>(m->length - 1); |
126 | 0 | this->getNext()->write(&ch, 1); |
127 | 0 | this->getNext()->write(m->buf, m->length); |
128 | 0 | } |
129 | 0 | m->state = st_top; |
130 | 0 | m->length = 0; |
131 | 0 | } |
132 | | |
133 | | void |
134 | | Pl_RunLength::finish() |
135 | 404 | { |
136 | | // When decoding, we might have read a length byte not followed by data, which means the stream |
137 | | // was terminated early, but we will just ignore this case since this is the only sensible thing |
138 | | // to do. |
139 | 404 | auto next = getNext(); |
140 | 404 | if (m->action == a_encode) { |
141 | 0 | flush_encode(); |
142 | 0 | unsigned char ch = 128; |
143 | 0 | next->write(&ch, 1); |
144 | 404 | } else { |
145 | 404 | next->writeString(m->out); |
146 | 404 | } |
147 | 404 | next->finish(); |
148 | 404 | } |