/src/qpdf/libqpdf/QPDF_json.cc
Line | Count | Source |
1 | | #include <qpdf/QPDF.hh> |
2 | | |
3 | | #include <qpdf/FileInputSource.hh> |
4 | | #include <qpdf/InputSource_private.hh> |
5 | | #include <qpdf/JSON_writer.hh> |
6 | | #include <qpdf/Pl_Base64.hh> |
7 | | #include <qpdf/Pl_StdioFile.hh> |
8 | | #include <qpdf/QIntC.hh> |
9 | | #include <qpdf/QPDFObjectHandle_private.hh> |
10 | | #include <qpdf/QPDFObject_private.hh> |
11 | | #include <qpdf/QTC.hh> |
12 | | #include <qpdf/QUtil.hh> |
13 | | #include <qpdf/Util.hh> |
14 | | |
15 | | #include <algorithm> |
16 | | #include <cstring> |
17 | | |
18 | | using namespace qpdf; |
19 | | |
20 | | // This chart shows an example of the state transitions that would occur in parsing a minimal file. |
21 | | |
22 | | // | |
23 | | // { | -> st_top |
24 | | // "qpdf": [ | -> st_qpdf |
25 | | // { | -> st_qpdf_meta |
26 | | // ... | ... |
27 | | // }, | ... |
28 | | // { | -> st_objects |
29 | | // "obj:1 0 R": { | -> st_object_top |
30 | | // "value": { | -> st_object |
31 | | // "/Pages": "2 0 R", | ... |
32 | | // "/Type": "/Catalog" | ... |
33 | | // } | <- st_object_top |
34 | | // }, | <- st_objects |
35 | | // "obj:2 0 R": { | -> st_object_top |
36 | | // "value": 12 | -> st_object |
37 | | // } | <- st_object_top |
38 | | // }, | <- st_objects |
39 | | // "obj:4 0 R": { | -> st_object_top |
40 | | // "stream": { | -> st_stream |
41 | | // "data": "cG90YXRv", | ... |
42 | | // "dict": { | -> st_object |
43 | | // "/K": true | ... |
44 | | // } | <- st_stream |
45 | | // } | <- st_object_top |
46 | | // }, | <- st_objects |
47 | | // "trailer": { | -> st_trailer |
48 | | // "value": { | -> st_object |
49 | | // "/Root": "1 0 R", | ... |
50 | | // "/Size": 7 | ... |
51 | | // } | <- st_trailer |
52 | | // } | <- st_objects |
53 | | // } | <- st_qpdf |
54 | | // ] | <- st_top |
55 | | // } | |
56 | | |
57 | | static char const* JSON_PDF = ( |
58 | | // force line break |
59 | | "%PDF-1.3\n" |
60 | | "xref\n" |
61 | | "0 1\n" |
62 | | "0000000000 65535 f \n" |
63 | | "trailer << /Size 1 >>\n" |
64 | | "startxref\n" |
65 | | "9\n" |
66 | | "%%EOF\n"); |
67 | | |
68 | | // Validator methods -- these are much more performant than std::regex. |
69 | | static bool |
70 | | is_indirect_object(std::string const& v, int& obj, int& gen) |
71 | 505k | { |
72 | 505k | char const* p = v.c_str(); |
73 | 505k | std::string o_str; |
74 | 505k | std::string g_str; |
75 | 505k | if (!util::is_digit(*p)) { |
76 | 44.6k | return false; |
77 | 44.6k | } |
78 | 1.24M | while (util::is_digit(*p)) { |
79 | 780k | o_str.append(1, *p++); |
80 | 780k | } |
81 | 460k | if (*p != ' ') { |
82 | 1.57k | return false; |
83 | 1.57k | } |
84 | 9.44M | while (*p == ' ') { |
85 | 8.98M | ++p; |
86 | 8.98M | } |
87 | 459k | if (!util::is_digit(*p)) { |
88 | 1.15k | return false; |
89 | 1.15k | } |
90 | 1.50M | while (util::is_digit(*p)) { |
91 | 1.05M | g_str.append(1, *p++); |
92 | 1.05M | } |
93 | 458k | if (*p != ' ') { |
94 | 1.09k | return false; |
95 | 1.09k | } |
96 | 15.5M | while (*p == ' ') { |
97 | 15.0M | ++p; |
98 | 15.0M | } |
99 | 457k | if (*p++ != 'R') { |
100 | 1.52k | return false; |
101 | 1.52k | } |
102 | 455k | if (*p) { |
103 | 836 | return false; |
104 | 836 | } |
105 | 454k | obj = QUtil::string_to_int(o_str.c_str()); |
106 | 454k | gen = QUtil::string_to_int(g_str.c_str()); |
107 | 454k | return obj > 0; |
108 | 455k | } |
109 | | |
110 | | static bool |
111 | | is_obj_key(std::string const& v, int& obj, int& gen) |
112 | 26.3k | { |
113 | 26.3k | if (v.substr(0, 4) != "obj:") { |
114 | 9.76k | return false; |
115 | 9.76k | } |
116 | 16.5k | return is_indirect_object(v.substr(4), obj, gen); |
117 | 26.3k | } |
118 | | |
119 | | static bool |
120 | | is_unicode_string(std::string const& v, std::string& str) |
121 | 48.9k | { |
122 | 48.9k | if (v.substr(0, 2) == "u:") { |
123 | 5.77k | str = v.substr(2); |
124 | 5.77k | return true; |
125 | 5.77k | } |
126 | 43.1k | return false; |
127 | 48.9k | } |
128 | | |
129 | | static bool |
130 | | is_binary_string(std::string const& v, std::string& str) |
131 | 43.1k | { |
132 | 43.1k | if (v.substr(0, 2) == "b:") { |
133 | 3.29k | str = v.substr(2); |
134 | 3.29k | int count = 0; |
135 | 42.9k | for (char c: str) { |
136 | 42.9k | if (!util::is_hex_digit(c)) { |
137 | 1.40k | return false; |
138 | 1.40k | } |
139 | 41.5k | ++count; |
140 | 41.5k | } |
141 | 1.89k | return (count % 2 == 0); |
142 | 3.29k | } |
143 | 39.8k | return false; |
144 | 43.1k | } |
145 | | |
146 | | static bool |
147 | | is_name(std::string const& v) |
148 | 41.9k | { |
149 | 41.9k | return ((v.length() > 1) && (v.at(0) == '/')); |
150 | 41.9k | } |
151 | | |
152 | | static bool |
153 | | is_pdf_name(std::string const& v) |
154 | 132k | { |
155 | 132k | return ((v.length() > 3) && (v.substr(0, 3) == "n:/")); |
156 | 132k | } |
157 | | |
158 | | bool |
159 | | QPDF::test_json_validators() |
160 | 0 | { |
161 | 0 | bool passed = true; |
162 | 0 | auto check_fn = [&passed](char const* msg, bool expr) { |
163 | 0 | if (!expr) { |
164 | 0 | passed = false; |
165 | 0 | std::cerr << msg << '\n'; |
166 | 0 | } |
167 | 0 | }; |
168 | 0 | #define check(expr) check_fn(#expr, expr) |
169 | |
|
170 | 0 | int obj = 0; |
171 | 0 | int gen = 0; |
172 | 0 | check(!is_indirect_object("", obj, gen)); |
173 | 0 | check(!is_indirect_object("12", obj, gen)); |
174 | 0 | check(!is_indirect_object("x12 0 R", obj, gen)); |
175 | 0 | check(!is_indirect_object("12 0 Rx", obj, gen)); |
176 | 0 | check(!is_indirect_object("12 0R", obj, gen)); |
177 | 0 | check(is_indirect_object("52 1 R", obj, gen)); |
178 | 0 | check(obj == 52); |
179 | 0 | check(gen == 1); |
180 | 0 | check(is_indirect_object("53 20 R", obj, gen)); |
181 | 0 | check(obj == 53); |
182 | 0 | check(gen == 20); |
183 | 0 | check(!is_obj_key("", obj, gen)); |
184 | 0 | check(!is_obj_key("obj:x", obj, gen)); |
185 | 0 | check(!is_obj_key("obj:x", obj, gen)); |
186 | 0 | check(is_obj_key("obj:12 13 R", obj, gen)); |
187 | 0 | check(obj == 12); |
188 | 0 | check(gen == 13); |
189 | 0 | std::string str; |
190 | 0 | check(!is_unicode_string("", str)); |
191 | 0 | check(!is_unicode_string("xyz", str)); |
192 | 0 | check(!is_unicode_string("x:", str)); |
193 | 0 | check(is_unicode_string("u:potato", str)); |
194 | 0 | check(str == "potato"); |
195 | 0 | check(is_unicode_string("u:", str)); |
196 | 0 | check(str.empty()); |
197 | 0 | check(!is_binary_string("", str)); |
198 | 0 | check(!is_binary_string("x:", str)); |
199 | 0 | check(!is_binary_string("b:1", str)); |
200 | 0 | check(!is_binary_string("b:123", str)); |
201 | 0 | check(!is_binary_string("b:gh", str)); |
202 | 0 | check(is_binary_string("b:", str)); |
203 | 0 | check(is_binary_string("b:12", str)); |
204 | 0 | check(is_binary_string("b:123aBC", str)); |
205 | 0 | check(!is_name("")); |
206 | 0 | check(!is_name("/")); |
207 | 0 | check(!is_name("xyz")); |
208 | 0 | check(is_name("/Potato")); |
209 | 0 | check(is_name("/Potato Salad")); |
210 | |
|
211 | 0 | return passed; |
212 | 0 | #undef check_arg |
213 | 0 | } |
214 | | |
215 | | static std::function<void(Pipeline*)> |
216 | | provide_data(std::shared_ptr<InputSource> is, qpdf_offset_t start, qpdf_offset_t end) |
217 | 4.35k | { |
218 | 4.35k | return [is, start, end](Pipeline* p) { |
219 | 0 | auto data = is->read(QIntC::to_size(end - start), start); |
220 | 0 | data = Pl_Base64::decode(data); |
221 | 0 | p->write(reinterpret_cast<const unsigned char*>(data.data()), data.size()); |
222 | 0 | p->finish(); |
223 | 0 | }; |
224 | 4.35k | } |
225 | | |
226 | | class QPDF::JSONReactor: public JSON::Reactor |
227 | | { |
228 | | public: |
229 | | JSONReactor(QPDF& pdf, std::shared_ptr<InputSource> is, bool must_be_complete) : |
230 | 7.87k | pdf(pdf), |
231 | 7.87k | is(is), |
232 | 7.87k | must_be_complete(must_be_complete), |
233 | | descr( |
234 | 7.87k | std::make_shared<QPDFObject::Description>( |
235 | 7.87k | QPDFObject::JSON_Descr(std::make_shared<std::string>(is->getName()), ""))) |
236 | 7.87k | { |
237 | 7.87k | } |
238 | 7.87k | ~JSONReactor() override = default; |
239 | | void dictionaryStart() override; |
240 | | void arrayStart() override; |
241 | | void containerEnd(JSON const& value) override; |
242 | | void topLevelScalar() override; |
243 | | bool dictionaryItem(std::string const& key, JSON const& value) override; |
244 | | bool arrayItem(JSON const& value) override; |
245 | | |
246 | | bool anyErrors() const; |
247 | | |
248 | | private: |
249 | | enum state_e { |
250 | | st_top, |
251 | | st_qpdf, |
252 | | st_qpdf_meta, |
253 | | st_objects, |
254 | | st_trailer, |
255 | | st_object_top, |
256 | | st_stream, |
257 | | st_object, |
258 | | st_ignore, |
259 | | }; |
260 | | |
261 | | struct StackFrame |
262 | | { |
263 | | StackFrame(state_e state) : |
264 | 38.0k | state(state) {}; |
265 | | StackFrame(state_e state, QPDFObjectHandle&& object) : |
266 | 50.2k | state(state), |
267 | 50.2k | object(object) {}; |
268 | | state_e state; |
269 | | QPDFObjectHandle object; |
270 | | }; |
271 | | |
272 | | void containerStart(); |
273 | | bool setNextStateIfDictionary(std::string const& key, JSON const& value, state_e); |
274 | | void setObjectDescription(QPDFObjectHandle& oh, JSON const& value); |
275 | | QPDFObjectHandle makeObject(JSON const& value); |
276 | | void error(qpdf_offset_t offset, std::string const& message); |
277 | | void replaceObject(QPDFObjectHandle&& replacement, JSON const& value); |
278 | | |
279 | | QPDF& pdf; |
280 | | QPDF::Doc::Objects& objects = pdf.m->objects; |
281 | | std::shared_ptr<InputSource> is; |
282 | | bool must_be_complete{true}; |
283 | | std::shared_ptr<QPDFObject::Description> descr; |
284 | | bool errors{false}; |
285 | | bool saw_qpdf{false}; |
286 | | bool saw_qpdf_meta{false}; |
287 | | bool saw_objects{false}; |
288 | | bool saw_json_version{false}; |
289 | | bool saw_pdf_version{false}; |
290 | | bool saw_trailer{false}; |
291 | | std::string cur_object; |
292 | | bool saw_value{false}; |
293 | | bool saw_stream{false}; |
294 | | bool saw_dict{false}; |
295 | | bool saw_data{false}; |
296 | | bool saw_datafile{false}; |
297 | | bool this_stream_needs_data{false}; |
298 | | std::vector<StackFrame> stack; |
299 | | QPDFObjectHandle next_obj; |
300 | | state_e next_state{st_top}; |
301 | | }; |
302 | | |
303 | | void |
304 | | QPDF::JSONReactor::error(qpdf_offset_t offset, std::string const& msg) |
305 | 83.7k | { |
306 | 83.7k | errors = true; |
307 | 83.7k | std::string object = this->cur_object; |
308 | 83.7k | if (is->getName() != pdf.getFilename()) { |
309 | 0 | object += " from " + is->getName(); |
310 | 0 | } |
311 | 83.7k | pdf.warn(qpdf_e_json, object, offset, msg); |
312 | 83.7k | } |
313 | | |
314 | | bool |
315 | | QPDF::JSONReactor::anyErrors() const |
316 | 53 | { |
317 | 53 | return errors; |
318 | 53 | } |
319 | | |
320 | | void |
321 | | QPDF::JSONReactor::containerStart() |
322 | 88.3k | { |
323 | 88.3k | if (next_obj) { |
324 | 50.2k | stack.emplace_back(next_state, std::move(next_obj)); |
325 | 50.2k | next_obj = QPDFObjectHandle(); |
326 | 50.2k | } else { |
327 | 38.0k | stack.emplace_back(next_state); |
328 | 38.0k | } |
329 | 88.3k | } |
330 | | |
331 | | void |
332 | | QPDF::JSONReactor::dictionaryStart() |
333 | 66.9k | { |
334 | 66.9k | containerStart(); |
335 | 66.9k | } |
336 | | |
337 | | void |
338 | | QPDF::JSONReactor::arrayStart() |
339 | 21.7k | { |
340 | 21.7k | if (stack.empty()) { |
341 | 386 | QTC::TC("qpdf", "QPDF_json top-level array"); |
342 | 386 | throw std::runtime_error("QPDF JSON must be a dictionary"); |
343 | 386 | } |
344 | 21.3k | containerStart(); |
345 | 21.3k | } |
346 | | |
347 | | void |
348 | | QPDF::JSONReactor::containerEnd(JSON const& value) |
349 | 35.6k | { |
350 | 35.6k | auto from_state = stack.back().state; |
351 | 35.6k | stack.pop_back(); |
352 | 35.6k | if (stack.empty()) { |
353 | 72 | if (!this->saw_qpdf) { |
354 | 23 | QTC::TC("qpdf", "QPDF_json missing qpdf"); |
355 | 23 | error(0, "\"qpdf\" object was not seen"); |
356 | 49 | } else { |
357 | 49 | if (!this->saw_json_version) { |
358 | 36 | QTC::TC("qpdf", "QPDF_json missing json version"); |
359 | 36 | error(0, "\"qpdf[0].jsonversion\" was not seen"); |
360 | 36 | } |
361 | 49 | if (must_be_complete && !this->saw_pdf_version) { |
362 | 36 | QTC::TC("qpdf", "QPDF_json missing pdf version"); |
363 | 36 | error(0, "\"qpdf[0].pdfversion\" was not seen"); |
364 | 36 | } |
365 | 49 | if (!this->saw_objects) { |
366 | 10 | QTC::TC("qpdf", "QPDF_json missing objects"); |
367 | 10 | error(0, "\"qpdf[1]\" was not seen"); |
368 | 39 | } else { |
369 | 39 | if (must_be_complete && !this->saw_trailer) { |
370 | 25 | QTC::TC("qpdf", "QPDF_json missing trailer"); |
371 | 25 | error(0, "\"qpdf[1].trailer\" was not seen"); |
372 | 25 | } |
373 | 39 | } |
374 | 49 | } |
375 | 35.5k | } else if (from_state == st_trailer) { |
376 | 1.29k | if (!saw_value) { |
377 | 984 | QTC::TC("qpdf", "QPDF_json trailer no value"); |
378 | 984 | error(value.getStart(), "\"trailer\" is missing \"value\""); |
379 | 984 | } |
380 | 34.2k | } else if (from_state == st_object_top) { |
381 | 10.4k | if (saw_value == saw_stream) { |
382 | 1.60k | QTC::TC("qpdf", "QPDF_json value stream both or neither"); |
383 | 1.60k | error(value.getStart(), "object must have exactly one of \"value\" or \"stream\""); |
384 | 1.60k | } |
385 | 10.4k | if (saw_stream) { |
386 | 5.49k | if (!saw_dict) { |
387 | 3.80k | QTC::TC("qpdf", "QPDF_json stream no dict"); |
388 | 3.80k | error(value.getStart(), "\"stream\" is missing \"dict\""); |
389 | 3.80k | } |
390 | 5.49k | if (saw_data == saw_datafile) { |
391 | 2.84k | if (this_stream_needs_data) { |
392 | 1.16k | QTC::TC("qpdf", "QPDF_json data datafile both or neither"); |
393 | 1.16k | error( |
394 | 1.16k | value.getStart(), |
395 | 1.16k | "new \"stream\" must have exactly one of \"data\" or \"datafile\""); |
396 | 1.67k | } else if (saw_datafile) { |
397 | 818 | QTC::TC("qpdf", "QPDF_json data and datafile"); |
398 | 818 | error( |
399 | 818 | value.getStart(), |
400 | 818 | "existing \"stream\" may at most one of \"data\" or \"datafile\""); |
401 | 860 | } else { |
402 | 860 | QTC::TC("qpdf", "QPDF_json no stream data in update mode"); |
403 | 860 | } |
404 | 2.84k | } |
405 | 5.49k | } |
406 | 10.4k | } |
407 | 35.6k | if (!stack.empty()) { |
408 | 35.5k | auto state = stack.back().state; |
409 | 35.5k | if (state == st_objects) { |
410 | 13.6k | this->cur_object = ""; |
411 | 13.6k | this->saw_dict = false; |
412 | 13.6k | this->saw_data = false; |
413 | 13.6k | this->saw_datafile = false; |
414 | 13.6k | this->saw_value = false; |
415 | 13.6k | this->saw_stream = false; |
416 | 13.6k | } |
417 | 35.5k | } |
418 | 35.6k | } |
419 | | |
420 | | void |
421 | | QPDF::JSONReactor::replaceObject(QPDFObjectHandle&& replacement, JSON const& value) |
422 | 18.6k | { |
423 | 18.6k | auto& tos = stack.back(); |
424 | 18.6k | auto og = tos.object.getObjGen(); |
425 | 18.6k | if (replacement.isIndirect() && !(replacement.isStream() && replacement.getObjGen() == og)) { |
426 | 621 | error( |
427 | 621 | replacement.offset(), "the value of an object may not be an indirect object reference"); |
428 | 621 | return; |
429 | 621 | } |
430 | 18.0k | pdf.replaceObject(og, replacement); |
431 | 18.0k | next_obj = pdf.getObject(og); |
432 | 18.0k | setObjectDescription(tos.object, value); |
433 | 18.0k | } |
434 | | |
435 | | void |
436 | | QPDF::JSONReactor::topLevelScalar() |
437 | 68 | { |
438 | 68 | QTC::TC("qpdf", "QPDF_json top-level scalar"); |
439 | 68 | throw std::runtime_error("QPDF JSON must be a dictionary"); |
440 | 68 | } |
441 | | |
442 | | bool |
443 | | QPDF::JSONReactor::setNextStateIfDictionary(std::string const& key, JSON const& value, state_e next) |
444 | 41.0k | { |
445 | | // Use this method when the next state is for processing a nested dictionary. |
446 | 41.0k | if (value.isDictionary()) { |
447 | 32.1k | this->next_state = next; |
448 | 32.1k | return true; |
449 | 32.1k | } |
450 | 8.81k | error(value.getStart(), "\"" + key + "\" must be a dictionary"); |
451 | 8.81k | return false; |
452 | 41.0k | } |
453 | | |
454 | | bool |
455 | | QPDF::JSONReactor::dictionaryItem(std::string const& key, JSON const& value) |
456 | 206k | { |
457 | 206k | if (stack.empty()) { |
458 | 0 | throw std::logic_error("stack is empty in dictionaryItem"); |
459 | 0 | } |
460 | 206k | next_state = st_ignore; |
461 | 206k | auto state = stack.back().state; |
462 | 206k | if (state == st_ignore) { |
463 | 6.28k | return true; // ignore |
464 | 6.28k | } |
465 | 200k | if (state == st_top) { |
466 | 14.9k | if (key == "qpdf") { |
467 | 11.1k | saw_qpdf = true; |
468 | 11.1k | if (!value.isArray()) { |
469 | 5.37k | error(value.getStart(), "\"qpdf\" must be an array"); |
470 | 5.80k | } else { |
471 | 5.80k | next_state = st_qpdf; |
472 | 5.80k | } |
473 | 11.1k | return true; |
474 | 11.1k | } |
475 | 3.76k | return true; // Ignore all other fields. |
476 | 14.9k | } |
477 | | |
478 | 185k | if (state == st_qpdf_meta) { |
479 | 12.5k | if (key == "pdfversion") { |
480 | 4.80k | saw_pdf_version = true; |
481 | 4.80k | std::string v; |
482 | 4.80k | if (value.getString(v)) { |
483 | 2.46k | std::string version; |
484 | 2.46k | char const* p = v.c_str(); |
485 | 2.46k | if (objects.validatePDFVersion(p, version) && *p == '\0') { |
486 | 345 | pdf.m->pdf_version = version; |
487 | 345 | return true; |
488 | 345 | } |
489 | 2.46k | } |
490 | 4.45k | error(value.getStart(), "invalid PDF version (must be \"x.y\")"); |
491 | 4.45k | return true; |
492 | 4.80k | } |
493 | 7.78k | if (key == "jsonversion") { |
494 | 2.23k | saw_json_version = true; |
495 | 2.23k | std::string v; |
496 | 2.23k | if (value.getNumber(v)) { |
497 | 2.03k | std::string version; |
498 | 2.03k | if (QUtil::string_to_int(v.c_str()) == 2) { |
499 | 250 | return true; |
500 | 250 | } |
501 | 2.03k | } |
502 | 1.98k | error(value.getStart(), "invalid JSON version (must be numeric value 2)"); |
503 | 1.98k | return true; |
504 | 2.23k | } |
505 | 5.55k | if (key == "pushedinheritedpageresources") { |
506 | 662 | bool v; |
507 | 662 | if (value.getBool(v)) { |
508 | 229 | if (!must_be_complete && v) { |
509 | 0 | pdf.pushInheritedAttributesToPage(); |
510 | 0 | } |
511 | 229 | return true; |
512 | 229 | } |
513 | 433 | error(value.getStart(), "pushedinheritedpageresources must be a boolean"); |
514 | 433 | return true; |
515 | 662 | } |
516 | 4.88k | if (key == "calledgetallpages") { |
517 | 1.42k | bool v; |
518 | 1.42k | if (value.getBool(v)) { |
519 | 233 | if (!must_be_complete && v) { |
520 | 0 | (void)pdf.doc().pages().all(); |
521 | 0 | } |
522 | 233 | return true; |
523 | 233 | } |
524 | 1.19k | error(value.getStart(), "calledgetallpages must be a boolean"); |
525 | 1.19k | return true; |
526 | 1.42k | } |
527 | | // ignore unknown keys for forward compatibility and to skip keys we don't care about |
528 | | // like "maxobjectid". |
529 | 3.46k | return true; |
530 | 4.88k | } |
531 | | |
532 | 173k | if (state == st_objects) { |
533 | 30.4k | if (key == "trailer") { |
534 | 4.14k | saw_trailer = true; |
535 | 4.14k | cur_object = "trailer"; |
536 | 4.14k | setNextStateIfDictionary(key, value, st_trailer); |
537 | 4.14k | return true; |
538 | 4.14k | } |
539 | | |
540 | 26.3k | int obj = 0; |
541 | 26.3k | int gen = 0; |
542 | 26.3k | if (is_obj_key(key, obj, gen)) { |
543 | 14.4k | cur_object = key; |
544 | 14.4k | if (setNextStateIfDictionary(key, value, st_object_top)) { |
545 | 13.1k | next_obj = objects.getObjectForJSON(obj, gen); |
546 | 13.1k | } |
547 | 14.4k | return true; |
548 | 14.4k | } |
549 | 11.8k | error(value.getStart(), "object key should be \"trailer\" or \"obj:n n R\""); |
550 | 11.8k | return true; |
551 | 26.3k | } |
552 | | |
553 | 142k | if (state == st_object_top) { |
554 | 24.2k | util::assertion(!stack.empty(), "QPDF_json: stack empty in st_object_top"); |
555 | 24.2k | auto& tos = stack.back(); |
556 | 24.2k | util::assertion(!!tos.object, "current object uninitialized in st_object_top"); |
557 | 24.2k | if (key == "value") { |
558 | | // Don't use setNextStateIfDictionary since this can have any type. |
559 | 15.4k | saw_value = true; |
560 | 15.4k | replaceObject(makeObject(value), value); |
561 | 15.4k | next_state = st_object; |
562 | 15.4k | return true; |
563 | 15.4k | } |
564 | 8.81k | if (key == "stream") { |
565 | 6.25k | saw_stream = true; |
566 | 6.25k | if (setNextStateIfDictionary(key, value, st_stream)) { |
567 | 5.75k | this_stream_needs_data = false; |
568 | 5.75k | if (tos.object.isStream()) { |
569 | 2.21k | QTC::TC("qpdf", "QPDF_json updating existing stream"); |
570 | 3.53k | } else { |
571 | 3.53k | this_stream_needs_data = true; |
572 | 3.53k | replaceObject( |
573 | 3.53k | qpdf::Stream( |
574 | 3.53k | pdf, tos.object.getObjGen(), QPDFObjectHandle::newDictionary(), 0, 0), |
575 | 3.53k | value); |
576 | 3.53k | } |
577 | 5.75k | next_obj = tos.object; |
578 | 5.75k | return true; |
579 | 5.75k | } |
580 | 500 | return true; // Error message already given above |
581 | 6.25k | } |
582 | 2.55k | return true; // Ignore unknown keys for forward compatibility |
583 | 8.81k | } |
584 | | |
585 | 118k | if (state == st_trailer) { |
586 | 7.44k | if (key == "value") { |
587 | 3.33k | saw_value = true; |
588 | | // The trailer must be a dictionary, so we can use setNextStateIfDictionary. |
589 | 3.33k | if (setNextStateIfDictionary("trailer.value", value, st_object)) { |
590 | 1.85k | pdf.m->trailer = makeObject(value); |
591 | 1.85k | setObjectDescription(pdf.m->trailer, value); |
592 | 1.85k | } |
593 | 3.33k | return true; |
594 | 3.33k | } |
595 | 4.10k | if (key == "stream") { |
596 | | // Don't need to set saw_stream here since there's already an error. |
597 | 1.43k | error(value.getStart(), "the trailer may not be a stream"); |
598 | 1.43k | return true; |
599 | 1.43k | } |
600 | 2.66k | return true; // Ignore unknown keys for forward compatibility |
601 | 4.10k | } |
602 | | |
603 | 110k | if (state == st_stream) { |
604 | 14.3k | util::assertion(!stack.empty(), "stack empty in st_stream"); |
605 | 14.3k | auto& tos = stack.back(); |
606 | 14.3k | util::assertion(tos.object.isStream(), "current object is not stream in st_stream"); |
607 | 14.3k | if (key == "dict") { |
608 | 2.16k | saw_dict = true; |
609 | 2.16k | if (setNextStateIfDictionary("stream.dict", value, st_object)) { |
610 | 1.58k | tos.object.replaceDict(makeObject(value)); |
611 | 1.58k | return true; |
612 | 1.58k | } |
613 | 577 | return true; // An error had already been given by setNextStateIfDictionary |
614 | 2.16k | } |
615 | 12.2k | if (key == "data") { |
616 | 8.22k | saw_data = true; |
617 | 8.22k | std::string v; |
618 | 8.22k | if (!value.getString(v)) { |
619 | 3.87k | error(value.getStart(), "\"stream.data\" must be a string"); |
620 | 3.87k | tos.object.replaceStreamData("", {}, {}); |
621 | 3.87k | return true; |
622 | 3.87k | } |
623 | | // The range includes the quotes. |
624 | 4.35k | auto start = value.getStart() + 1; |
625 | 4.35k | auto end = value.getEnd() - 1; |
626 | 4.35k | util::assertion(end >= start, "QPDF_json: JSON string length < 0"); |
627 | 4.35k | tos.object.replaceStreamData(provide_data(is, start, end), {}, {}); |
628 | 4.35k | return true; |
629 | 8.22k | } |
630 | 3.98k | if (key == "datafile") { |
631 | 2.25k | saw_datafile = true; |
632 | 2.25k | std::string filename; |
633 | 2.25k | if (!value.getString(filename)) { |
634 | 195 | error( |
635 | 195 | value.getStart(), |
636 | 195 | "\"stream.datafile\" must be a string containing a file name"); |
637 | 195 | tos.object.replaceStreamData("", {}, {}); |
638 | 195 | return true; |
639 | 195 | } |
640 | 2.05k | tos.object.replaceStreamData(QUtil::file_provider(filename), {}, {}); |
641 | 2.05k | return true; |
642 | 2.25k | } |
643 | 1.73k | return true; // Ignore unknown keys for forward compatibility. |
644 | 3.98k | } |
645 | | |
646 | 96.5k | util::assertion(state == st_object, "QPDF_json: unknown state " + std::to_string(state)); |
647 | 96.5k | util::assertion(!stack.empty(), "stack empty in st_object"); |
648 | 96.5k | auto& tos = stack.back(); |
649 | 96.5k | auto dict = tos.object; |
650 | 96.5k | if (dict.isStream()) { |
651 | 0 | dict = dict.getDict(); |
652 | 0 | } |
653 | 96.5k | util::assertion( |
654 | 96.5k | dict.isDictionary(), |
655 | 96.5k | "current object is not stream or dictionary in st_object dictionary item"); |
656 | 96.5k | dict.replaceKey( |
657 | 96.5k | is_pdf_name(key) ? QPDFObjectHandle::parse(key.substr(2)).getName() : key, |
658 | 96.5k | makeObject(value)); |
659 | 96.5k | return true; |
660 | 110k | } |
661 | | |
662 | | bool |
663 | | QPDF::JSONReactor::arrayItem(JSON const& value) |
664 | 1.01M | { |
665 | 1.01M | if (stack.empty()) { |
666 | 0 | throw std::logic_error("stack is empty in arrayItem"); |
667 | 0 | } |
668 | 1.01M | next_state = st_ignore; |
669 | 1.01M | auto state = stack.back().state; |
670 | 1.01M | if (state == st_qpdf) { |
671 | 16.2k | if (!this->saw_qpdf_meta) { |
672 | 5.60k | this->saw_qpdf_meta = true; |
673 | 5.60k | setNextStateIfDictionary("qpdf[0]", value, st_qpdf_meta); |
674 | 10.6k | } else if (!this->saw_objects) { |
675 | 5.05k | this->saw_objects = true; |
676 | 5.05k | setNextStateIfDictionary("qpdf[1]", value, st_objects); |
677 | 5.62k | } else { |
678 | 5.62k | QTC::TC("qpdf", "QPDF_json more than two qpdf elements"); |
679 | 5.62k | error(value.getStart(), "\"qpdf\" must have two elements"); |
680 | 5.62k | } |
681 | 999k | } else if (state == st_object) { |
682 | 992k | stack.back().object.appendItem(makeObject(value)); |
683 | 992k | } |
684 | 1.01M | return true; |
685 | 1.01M | } |
686 | | |
687 | | void |
688 | | QPDF::JSONReactor::setObjectDescription(QPDFObjectHandle& oh, JSON const& value) |
689 | 681k | { |
690 | 681k | auto j_descr = std::get<QPDFObject::JSON_Descr>(*descr); |
691 | 681k | if (j_descr.object != cur_object) { |
692 | 10.8k | descr = std::make_shared<QPDFObject::Description>( |
693 | 10.8k | QPDFObject::JSON_Descr(j_descr.input, cur_object)); |
694 | 10.8k | } |
695 | | |
696 | 681k | oh.obj_sp()->setDescription(&pdf, descr, value.getStart()); |
697 | 681k | } |
698 | | |
699 | | QPDFObjectHandle |
700 | | QPDF::JSONReactor::makeObject(JSON const& value) |
701 | 1.10M | { |
702 | 1.10M | QPDFObjectHandle result; |
703 | 1.10M | std::string str_v; |
704 | 1.10M | bool bool_v = false; |
705 | 1.10M | if (value.isDictionary()) { |
706 | 19.4k | result = QPDFObjectHandle::newDictionary(); |
707 | 19.4k | next_obj = result; |
708 | 19.4k | next_state = st_object; |
709 | 1.08M | } else if (value.isArray()) { |
710 | 11.4k | result = QPDFObjectHandle::newArray(); |
711 | 11.4k | next_obj = result; |
712 | 11.4k | next_state = st_object; |
713 | 1.07M | } else if (value.isNull()) { |
714 | 991 | result = QPDFObjectHandle::newNull(); |
715 | 1.07M | } else if (value.getBool(bool_v)) { |
716 | 837 | result = QPDFObjectHandle::newBool(bool_v); |
717 | 1.07M | } else if (value.getNumber(str_v)) { |
718 | 586k | if (QUtil::is_long_long(str_v.c_str())) { |
719 | 581k | result = QPDFObjectHandle::newInteger(QUtil::string_to_ll(str_v.c_str())); |
720 | 581k | } else { |
721 | | // JSON allows scientific notation, but PDF does not. |
722 | 4.96k | if (str_v.find('e') != std::string::npos || str_v.find('E') != std::string::npos) { |
723 | 3.88k | try { |
724 | 3.88k | auto v = std::stod(str_v); |
725 | 3.88k | str_v = QUtil::double_to_string(v); |
726 | 3.88k | } catch (std::exception&) { |
727 | | // Keep it as it was |
728 | 250 | } |
729 | 3.88k | } |
730 | 4.96k | result = QPDFObjectHandle::newReal(str_v); |
731 | 4.96k | } |
732 | 586k | } else if (value.getString(str_v)) { |
733 | 489k | int obj = 0; |
734 | 489k | int gen = 0; |
735 | 489k | std::string str; |
736 | 489k | if (is_indirect_object(str_v, obj, gen)) { |
737 | 440k | result = objects.getObjectForJSON(obj, gen); |
738 | 440k | } else if (is_unicode_string(str_v, str)) { |
739 | 5.77k | result = QPDFObjectHandle::newUnicodeString(str); |
740 | 43.1k | } else if (is_binary_string(str_v, str)) { |
741 | 1.25k | result = QPDFObjectHandle::newString(QUtil::hex_decode(str)); |
742 | 41.9k | } else if (is_name(str_v)) { |
743 | 5.83k | result = QPDFObjectHandle::newName(str_v); |
744 | 36.0k | } else if (is_pdf_name(str_v)) { |
745 | 6.66k | result = QPDFObjectHandle::parse(str_v.substr(2)); |
746 | 29.4k | } else { |
747 | 29.4k | QTC::TC("qpdf", "QPDF_json unrecognized string value"); |
748 | 29.4k | error(value.getStart(), "unrecognized string value"); |
749 | 29.4k | result = QPDFObjectHandle::newNull(); |
750 | 29.4k | } |
751 | 489k | } |
752 | 1.10M | if (!result) { |
753 | 0 | throw std::logic_error("JSONReactor::makeObject didn't initialize the object"); |
754 | 0 | } |
755 | | |
756 | 1.10M | if (!result.hasObjectDescription()) { |
757 | 661k | setObjectDescription(result, value); |
758 | 661k | } |
759 | 1.10M | return result; |
760 | 1.10M | } |
761 | | |
762 | | void |
763 | | QPDF::createFromJSON(std::string const& json_file) |
764 | 0 | { |
765 | 0 | createFromJSON(std::make_shared<FileInputSource>(json_file.c_str())); |
766 | 0 | } |
767 | | |
768 | | void |
769 | | QPDF::createFromJSON(std::shared_ptr<InputSource> is) |
770 | 7.87k | { |
771 | 7.87k | processMemoryFile(is->getName().c_str(), JSON_PDF, strlen(JSON_PDF)); |
772 | 7.87k | importJSON(is, true); |
773 | 7.87k | } |
774 | | |
775 | | void |
776 | | QPDF::updateFromJSON(std::string const& json_file) |
777 | 0 | { |
778 | 0 | updateFromJSON(std::make_shared<FileInputSource>(json_file.c_str())); |
779 | 0 | } |
780 | | |
781 | | void |
782 | | QPDF::updateFromJSON(std::shared_ptr<InputSource> is) |
783 | 0 | { |
784 | 0 | importJSON(is, false); |
785 | 0 | } |
786 | | |
787 | | void |
788 | | QPDF::importJSON(std::shared_ptr<InputSource> is, bool must_be_complete) |
789 | 7.87k | { |
790 | 7.87k | JSONReactor reactor(*this, is, must_be_complete); |
791 | 7.87k | try { |
792 | 7.87k | JSON::parse(*is, &reactor); |
793 | 7.87k | } catch (std::runtime_error& e) { |
794 | 7.82k | throw std::runtime_error(is->getName() + ": " + e.what()); |
795 | 7.82k | } |
796 | 53 | if (reactor.anyErrors()) { |
797 | 42 | throw std::runtime_error(is->getName() + ": errors found in JSON"); |
798 | 42 | } |
799 | 53 | } |
800 | | |
801 | | void |
802 | | writeJSONStreamFile( |
803 | | int version, |
804 | | JSON::Writer& jw, |
805 | | qpdf::Stream& stream, |
806 | | int id, |
807 | | qpdf_stream_decode_level_e decode_level, |
808 | | std::string const& file_prefix) |
809 | 0 | { |
810 | 0 | auto filename = file_prefix + "-" + std::to_string(id); |
811 | 0 | auto* f = QUtil::safe_fopen(filename.c_str(), "wb"); |
812 | 0 | Pl_StdioFile f_pl{"stream data", f}; |
813 | 0 | stream.writeStreamJSON(version, jw, qpdf_sj_file, decode_level, &f_pl, filename); |
814 | 0 | f_pl.finish(); |
815 | 0 | fclose(f); |
816 | 0 | } |
817 | | |
818 | | void |
819 | | QPDF::writeJSON( |
820 | | int version, |
821 | | Pipeline* p, |
822 | | qpdf_stream_decode_level_e decode_level, |
823 | | qpdf_json_stream_data_e json_stream_data, |
824 | | std::string const& file_prefix, |
825 | | std::set<std::string> wanted_objects) |
826 | 0 | { |
827 | 0 | bool first = true; |
828 | 0 | writeJSON(version, p, true, first, decode_level, json_stream_data, file_prefix, wanted_objects); |
829 | 0 | } |
830 | | |
831 | | void |
832 | | QPDF::writeJSON( |
833 | | int version, |
834 | | Pipeline* p, |
835 | | bool complete, |
836 | | bool& first_key, |
837 | | qpdf_stream_decode_level_e decode_level, |
838 | | qpdf_json_stream_data_e json_stream_data, |
839 | | std::string const& file_prefix, |
840 | | std::set<std::string> wanted_objects) |
841 | 0 | { |
842 | 0 | if (version != 2) { |
843 | 0 | throw std::runtime_error("QPDF::writeJSON: only version 2 is supported"); |
844 | 0 | } |
845 | 0 | JSON::Writer jw{p, 4}; |
846 | 0 | if (complete) { |
847 | 0 | jw << "{"; |
848 | 0 | } else if (!first_key) { |
849 | 0 | jw << ","; |
850 | 0 | } |
851 | 0 | first_key = false; |
852 | | |
853 | | /* clang-format off */ |
854 | 0 | jw << "\n" |
855 | 0 | " \"qpdf\": [\n" |
856 | 0 | " {\n" |
857 | 0 | " \"jsonversion\": " << std::to_string(version) << ",\n" |
858 | 0 | " \"pdfversion\": \"" << getPDFVersion() << "\",\n" |
859 | 0 | " \"pushedinheritedpageresources\": " << (everPushedInheritedAttributesToPages() ? "true" : "false") << ",\n" |
860 | 0 | " \"calledgetallpages\": " << (everCalledGetAllPages() ? "true" : "false") << ",\n" |
861 | 0 | " \"maxobjectid\": " << std::to_string(getObjectCount()) << "\n" |
862 | 0 | " },\n" |
863 | 0 | " {"; |
864 | | /* clang-format on */ |
865 | |
|
866 | 0 | bool all_objects = wanted_objects.empty(); |
867 | 0 | bool first = true; |
868 | 0 | for (auto& obj: getAllObjects()) { |
869 | 0 | auto const og = obj.getObjGen(); |
870 | 0 | std::string key = "obj:" + og.unparse(' ') + " R"; |
871 | 0 | if (all_objects || wanted_objects.contains(key)) { |
872 | 0 | if (first) { |
873 | 0 | jw << "\n \"" << key; |
874 | 0 | first = false; |
875 | 0 | } else { |
876 | 0 | jw << "\n },\n \"" << key; |
877 | 0 | } |
878 | 0 | if (Stream stream = obj) { |
879 | 0 | jw << "\": {\n \"stream\": "; |
880 | 0 | if (json_stream_data == qpdf_sj_file) { |
881 | 0 | writeJSONStreamFile( |
882 | 0 | version, jw, stream, og.getObj(), decode_level, file_prefix); |
883 | 0 | } else { |
884 | 0 | stream.writeStreamJSON( |
885 | 0 | version, jw, json_stream_data, decode_level, nullptr, ""); |
886 | 0 | } |
887 | 0 | } else { |
888 | 0 | jw << "\": {\n \"value\": "; |
889 | 0 | obj.writeJSON(version, jw, true); |
890 | 0 | } |
891 | 0 | } |
892 | 0 | } |
893 | 0 | if (all_objects || wanted_objects.contains("trailer")) { |
894 | 0 | if (!first) { |
895 | 0 | jw << "\n },"; |
896 | 0 | } |
897 | 0 | jw << "\n \"trailer\": {\n \"value\": "; |
898 | 0 | getTrailer().writeJSON(version, jw, true); |
899 | 0 | first = false; |
900 | 0 | } |
901 | 0 | if (!first) { |
902 | 0 | jw << "\n }"; |
903 | 0 | } |
904 | | /* clang-format off */ |
905 | 0 | jw << "\n" |
906 | 0 | " }\n" |
907 | 0 | " ]"; |
908 | | /* clang-format on */ |
909 | 0 | if (complete) { |
910 | 0 | jw << "\n}\n"; |
911 | 0 | p->finish(); |
912 | 0 | } |
913 | 0 | } |