/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 | 350k | { |
72 | 350k | char const* p = v.c_str(); |
73 | 350k | std::string o_str; |
74 | 350k | std::string g_str; |
75 | 350k | if (!util::is_digit(*p)) { |
76 | 46.4k | return false; |
77 | 46.4k | } |
78 | 668k | while (util::is_digit(*p)) { |
79 | 364k | o_str.append(1, *p++); |
80 | 364k | } |
81 | 303k | if (*p != ' ') { |
82 | 1.91k | return false; |
83 | 1.91k | } |
84 | 605k | while (*p == ' ') { |
85 | 303k | ++p; |
86 | 303k | } |
87 | 301k | if (!util::is_digit(*p)) { |
88 | 898 | return false; |
89 | 898 | } |
90 | 12.2M | while (util::is_digit(*p)) { |
91 | 11.9M | g_str.append(1, *p++); |
92 | 11.9M | } |
93 | 300k | if (*p != ' ') { |
94 | 929 | return false; |
95 | 929 | } |
96 | 610k | while (*p == ' ') { |
97 | 310k | ++p; |
98 | 310k | } |
99 | 299k | if (*p++ != 'R') { |
100 | 635 | return false; |
101 | 635 | } |
102 | 299k | if (*p) { |
103 | 670 | return false; |
104 | 670 | } |
105 | 298k | obj = QUtil::string_to_int(o_str.c_str()); |
106 | 298k | gen = QUtil::string_to_int(g_str.c_str()); |
107 | 298k | return obj > 0; |
108 | 299k | } |
109 | | |
110 | | static bool |
111 | | is_obj_key(std::string const& v, int& obj, int& gen) |
112 | 22.2k | { |
113 | 22.2k | if (v.substr(0, 4) != "obj:") { |
114 | 4.12k | return false; |
115 | 4.12k | } |
116 | 18.0k | return is_indirect_object(v.substr(4), obj, gen); |
117 | 22.2k | } |
118 | | |
119 | | static bool |
120 | | is_unicode_string(std::string const& v, std::string& str) |
121 | 49.4k | { |
122 | 49.4k | if (v.substr(0, 2) == "u:") { |
123 | 5.55k | str = v.substr(2); |
124 | 5.55k | return true; |
125 | 5.55k | } |
126 | 43.9k | return false; |
127 | 49.4k | } |
128 | | |
129 | | static bool |
130 | | is_binary_string(std::string const& v, std::string& str) |
131 | 43.9k | { |
132 | 43.9k | if (v.substr(0, 2) == "b:") { |
133 | 2.53k | str = v.substr(2); |
134 | 2.53k | int count = 0; |
135 | 29.3k | for (char c: str) { |
136 | 29.3k | if (!util::is_hex_digit(c)) { |
137 | 795 | return false; |
138 | 795 | } |
139 | 28.5k | ++count; |
140 | 28.5k | } |
141 | 1.73k | return (count % 2 == 0); |
142 | 2.53k | } |
143 | 41.4k | return false; |
144 | 43.9k | } |
145 | | |
146 | | static bool |
147 | | is_name(std::string const& v) |
148 | 42.9k | { |
149 | 42.9k | return v.starts_with('/'); |
150 | 42.9k | } |
151 | | |
152 | | static bool |
153 | | is_pdf_name(std::string const& v) |
154 | 131k | { |
155 | 131k | return v.starts_with("n:/"); |
156 | 131k | } |
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 | 0 | check(!is_pdf_name("n:")); |
211 | 0 | check(is_pdf_name("n:/")); |
212 | 0 | check(!is_pdf_name("n:xyz")); |
213 | 0 | check(is_pdf_name("n:/Potato")); |
214 | 0 | check(is_pdf_name("n:/Potato Salad")); |
215 | |
|
216 | 0 | return passed; |
217 | 0 | #undef check_arg |
218 | 0 | } |
219 | | |
220 | | static std::function<void(Pipeline*)> |
221 | | provide_data(std::shared_ptr<InputSource> is, qpdf_offset_t start, qpdf_offset_t end) |
222 | 6.20k | { |
223 | 6.20k | return [is, start, end](Pipeline* p) { |
224 | 0 | auto data = is->read(QIntC::to_size(end - start), start); |
225 | 0 | data = Pl_Base64::decode(data); |
226 | 0 | p->write(reinterpret_cast<const unsigned char*>(data.data()), data.size()); |
227 | 0 | p->finish(); |
228 | 0 | }; |
229 | 6.20k | } |
230 | | |
231 | | class QPDF::JSONReactor: public JSON::Reactor |
232 | | { |
233 | | public: |
234 | | JSONReactor(QPDF& pdf, std::shared_ptr<InputSource> is, bool must_be_complete) : |
235 | 7.63k | pdf(pdf), |
236 | 7.63k | is(is), |
237 | 7.63k | must_be_complete(must_be_complete), |
238 | | descr( |
239 | 7.63k | std::make_shared<QPDFObject::Description>( |
240 | 7.63k | QPDFObject::JSON_Descr(std::make_shared<std::string>(is->getName()), ""))) |
241 | 7.63k | { |
242 | 7.63k | } |
243 | 7.63k | ~JSONReactor() override = default; |
244 | | void dictionaryStart() override; |
245 | | void arrayStart() override; |
246 | | void containerEnd(JSON const& value) override; |
247 | | void topLevelScalar() override; |
248 | | bool dictionaryItem(std::string const& key, JSON const& value) override; |
249 | | bool arrayItem(JSON const& value) override; |
250 | | |
251 | | bool anyErrors() const; |
252 | | |
253 | | private: |
254 | | enum state_e { |
255 | | st_top, |
256 | | st_qpdf, |
257 | | st_qpdf_meta, |
258 | | st_objects, |
259 | | st_trailer, |
260 | | st_object_top, |
261 | | st_stream, |
262 | | st_object, |
263 | | st_ignore, |
264 | | }; |
265 | | |
266 | | struct StackFrame |
267 | | { |
268 | | StackFrame(state_e state) : |
269 | 30.6k | state(state) {}; |
270 | | StackFrame(state_e state, QPDFObjectHandle&& object) : |
271 | 53.4k | state(state), |
272 | 53.4k | object(object) {}; |
273 | | state_e state; |
274 | | QPDFObjectHandle object; |
275 | | }; |
276 | | |
277 | | void containerStart(); |
278 | | bool setNextStateIfDictionary(std::string const& key, JSON const& value, state_e); |
279 | | void setObjectDescription(QPDFObjectHandle& oh, JSON const& value); |
280 | | QPDFObjectHandle makeObject(JSON const& value); |
281 | | void error(qpdf_offset_t offset, std::string const& message); |
282 | | void replaceObject(QPDFObjectHandle&& replacement, JSON const& value); |
283 | | |
284 | | QPDF& pdf; |
285 | | QPDF::Doc::Objects& objects = pdf.m->objects; |
286 | | std::shared_ptr<InputSource> is; |
287 | | bool must_be_complete{true}; |
288 | | std::shared_ptr<QPDFObject::Description> descr; |
289 | | bool errors{false}; |
290 | | bool saw_qpdf{false}; |
291 | | bool saw_qpdf_meta{false}; |
292 | | bool saw_objects{false}; |
293 | | bool saw_json_version{false}; |
294 | | bool saw_pdf_version{false}; |
295 | | bool saw_trailer{false}; |
296 | | std::string cur_object; |
297 | | bool saw_value{false}; |
298 | | bool saw_stream{false}; |
299 | | bool saw_dict{false}; |
300 | | bool saw_data{false}; |
301 | | bool saw_datafile{false}; |
302 | | bool this_stream_needs_data{false}; |
303 | | std::vector<StackFrame> stack; |
304 | | QPDFObjectHandle next_obj; |
305 | | state_e next_state{st_top}; |
306 | | }; |
307 | | |
308 | | void |
309 | | QPDF::JSONReactor::error(qpdf_offset_t offset, std::string const& msg) |
310 | 47.7k | { |
311 | 47.7k | errors = true; |
312 | 47.7k | std::string object = this->cur_object; |
313 | 47.7k | if (is->getName() != pdf.getFilename()) { |
314 | 0 | object += " from " + is->getName(); |
315 | 0 | } |
316 | 47.7k | pdf.warn(qpdf_e_json, object, offset, msg); |
317 | 47.7k | } |
318 | | |
319 | | bool |
320 | | QPDF::JSONReactor::anyErrors() const |
321 | 68 | { |
322 | 68 | return errors; |
323 | 68 | } |
324 | | |
325 | | void |
326 | | QPDF::JSONReactor::containerStart() |
327 | 84.0k | { |
328 | 84.0k | if (next_obj) { |
329 | 53.4k | stack.emplace_back(next_state, std::move(next_obj)); |
330 | 53.4k | next_obj = QPDFObjectHandle(); |
331 | 53.4k | } else { |
332 | 30.6k | stack.emplace_back(next_state); |
333 | 30.6k | } |
334 | 84.0k | } |
335 | | |
336 | | void |
337 | | QPDF::JSONReactor::dictionaryStart() |
338 | 62.5k | { |
339 | 62.5k | containerStart(); |
340 | 62.5k | } |
341 | | |
342 | | void |
343 | | QPDF::JSONReactor::arrayStart() |
344 | 21.9k | { |
345 | 21.9k | if (stack.empty()) { |
346 | 350 | QTC::TC("qpdf", "QPDF_json top-level array"); |
347 | 350 | throw std::runtime_error("QPDF JSON must be a dictionary"); |
348 | 350 | } |
349 | 21.5k | containerStart(); |
350 | 21.5k | } |
351 | | |
352 | | void |
353 | | QPDF::JSONReactor::containerEnd(JSON const& value) |
354 | 36.8k | { |
355 | 36.8k | auto from_state = stack.back().state; |
356 | 36.8k | stack.pop_back(); |
357 | 36.8k | if (stack.empty()) { |
358 | 80 | if (!this->saw_qpdf) { |
359 | 18 | QTC::TC("qpdf", "QPDF_json missing qpdf"); |
360 | 18 | error(0, "\"qpdf\" object was not seen"); |
361 | 62 | } else { |
362 | 62 | if (!this->saw_json_version) { |
363 | 44 | QTC::TC("qpdf", "QPDF_json missing json version"); |
364 | 44 | error(0, "\"qpdf[0].jsonversion\" was not seen"); |
365 | 44 | } |
366 | 62 | if (must_be_complete && !this->saw_pdf_version) { |
367 | 43 | QTC::TC("qpdf", "QPDF_json missing pdf version"); |
368 | 43 | error(0, "\"qpdf[0].pdfversion\" was not seen"); |
369 | 43 | } |
370 | 62 | if (!this->saw_objects) { |
371 | 8 | QTC::TC("qpdf", "QPDF_json missing objects"); |
372 | 8 | error(0, "\"qpdf[1]\" was not seen"); |
373 | 54 | } else { |
374 | 54 | if (must_be_complete && !this->saw_trailer) { |
375 | 34 | QTC::TC("qpdf", "QPDF_json missing trailer"); |
376 | 34 | error(0, "\"qpdf[1].trailer\" was not seen"); |
377 | 34 | } |
378 | 54 | } |
379 | 62 | } |
380 | 36.7k | } else if (from_state == st_trailer) { |
381 | 574 | if (!saw_value) { |
382 | 362 | QTC::TC("qpdf", "QPDF_json trailer no value"); |
383 | 362 | error(value.getStart(), "\"trailer\" is missing \"value\""); |
384 | 362 | } |
385 | 36.1k | } else if (from_state == st_object_top) { |
386 | 10.9k | if (saw_value == saw_stream) { |
387 | 775 | QTC::TC("qpdf", "QPDF_json value stream both or neither"); |
388 | 775 | error(value.getStart(), "object must have exactly one of \"value\" or \"stream\""); |
389 | 775 | } |
390 | 10.9k | if (saw_stream) { |
391 | 4.97k | if (!saw_dict) { |
392 | 1.39k | QTC::TC("qpdf", "QPDF_json stream no dict"); |
393 | 1.39k | error(value.getStart(), "\"stream\" is missing \"dict\""); |
394 | 1.39k | } |
395 | 4.97k | if (saw_data == saw_datafile) { |
396 | 1.38k | if (this_stream_needs_data) { |
397 | 629 | QTC::TC("qpdf", "QPDF_json data datafile both or neither"); |
398 | 629 | error( |
399 | 629 | value.getStart(), |
400 | 629 | "new \"stream\" must have exactly one of \"data\" or \"datafile\""); |
401 | 759 | } else if (saw_datafile) { |
402 | 137 | QTC::TC("qpdf", "QPDF_json data and datafile"); |
403 | 137 | error( |
404 | 137 | value.getStart(), |
405 | 137 | "existing \"stream\" may at most one of \"data\" or \"datafile\""); |
406 | 622 | } else { |
407 | 622 | QTC::TC("qpdf", "QPDF_json no stream data in update mode"); |
408 | 622 | } |
409 | 1.38k | } |
410 | 4.97k | } |
411 | 10.9k | } |
412 | 36.8k | if (!stack.empty()) { |
413 | 36.7k | auto state = stack.back().state; |
414 | 36.7k | if (state == st_objects) { |
415 | 13.1k | this->cur_object = ""; |
416 | 13.1k | this->saw_dict = false; |
417 | 13.1k | this->saw_data = false; |
418 | 13.1k | this->saw_datafile = false; |
419 | 13.1k | this->saw_value = false; |
420 | 13.1k | this->saw_stream = false; |
421 | 13.1k | } |
422 | 36.7k | } |
423 | 36.8k | } |
424 | | |
425 | | void |
426 | | QPDF::JSONReactor::replaceObject(QPDFObjectHandle&& replacement, JSON const& value) |
427 | 20.1k | { |
428 | 20.1k | auto& tos = stack.back(); |
429 | 20.1k | auto og = tos.object.getObjGen(); |
430 | 20.1k | if (replacement.isIndirect() && !(replacement.isStream() && replacement.getObjGen() == og)) { |
431 | 724 | error( |
432 | 724 | replacement.offset(), "the value of an object may not be an indirect object reference"); |
433 | 724 | return; |
434 | 724 | } |
435 | 19.4k | pdf.replaceObject(og, replacement); |
436 | 19.4k | next_obj = pdf.getObject(og); |
437 | 19.4k | setObjectDescription(tos.object, value); |
438 | 19.4k | } |
439 | | |
440 | | void |
441 | | QPDF::JSONReactor::topLevelScalar() |
442 | 60 | { |
443 | 60 | QTC::TC("qpdf", "QPDF_json top-level scalar"); |
444 | 60 | throw std::runtime_error("QPDF JSON must be a dictionary"); |
445 | 60 | } |
446 | | |
447 | | bool |
448 | | QPDF::JSONReactor::setNextStateIfDictionary(std::string const& key, JSON const& value, state_e next) |
449 | 39.2k | { |
450 | | // Use this method when the next state is for processing a nested dictionary. |
451 | 39.2k | if (value.isDictionary()) { |
452 | 32.6k | this->next_state = next; |
453 | 32.6k | return true; |
454 | 32.6k | } |
455 | 6.50k | error(value.getStart(), "\"" + key + "\" must be a dictionary"); |
456 | 6.50k | return false; |
457 | 39.2k | } |
458 | | |
459 | | bool |
460 | | QPDF::JSONReactor::dictionaryItem(std::string const& key, JSON const& value) |
461 | 194k | { |
462 | 194k | if (stack.empty()) { |
463 | 0 | throw std::logic_error("stack is empty in dictionaryItem"); |
464 | 0 | } |
465 | 194k | next_state = st_ignore; |
466 | 194k | auto state = stack.back().state; |
467 | 194k | if (state == st_ignore) { |
468 | 5.72k | return true; // ignore |
469 | 5.72k | } |
470 | 189k | if (state == st_top) { |
471 | 9.53k | if (key == "qpdf") { |
472 | 6.30k | saw_qpdf = true; |
473 | 6.30k | if (!value.isArray()) { |
474 | 653 | error(value.getStart(), "\"qpdf\" must be an array"); |
475 | 5.64k | } else { |
476 | 5.64k | next_state = st_qpdf; |
477 | 5.64k | } |
478 | 6.30k | return true; |
479 | 6.30k | } |
480 | 3.23k | return true; // Ignore all other fields. |
481 | 9.53k | } |
482 | | |
483 | 179k | if (state == st_qpdf_meta) { |
484 | 7.66k | if (key == "pdfversion") { |
485 | 2.76k | saw_pdf_version = true; |
486 | 2.76k | std::string v; |
487 | 2.76k | if (value.getString(v)) { |
488 | 1.87k | std::string version; |
489 | 1.87k | char const* p = v.c_str(); |
490 | 1.87k | if (objects.validatePDFVersion(p, version) && *p == '\0') { |
491 | 346 | pdf.m->pdf_version = version; |
492 | 346 | return true; |
493 | 346 | } |
494 | 1.87k | } |
495 | 2.41k | error(value.getStart(), "invalid PDF version (must be \"x.y\")"); |
496 | 2.41k | return true; |
497 | 2.76k | } |
498 | 4.90k | if (key == "jsonversion") { |
499 | 1.36k | saw_json_version = true; |
500 | 1.36k | std::string v; |
501 | 1.36k | if (value.getNumber(v)) { |
502 | 1.17k | std::string version; |
503 | 1.17k | if (QUtil::string_to_int(v.c_str()) == 2) { |
504 | 375 | return true; |
505 | 375 | } |
506 | 1.17k | } |
507 | 994 | error(value.getStart(), "invalid JSON version (must be numeric value 2)"); |
508 | 994 | return true; |
509 | 1.36k | } |
510 | 3.53k | if (key == "pushedinheritedpageresources") { |
511 | 445 | bool v; |
512 | 445 | if (value.getBool(v)) { |
513 | 233 | if (!must_be_complete && v) { |
514 | 0 | pdf.pushInheritedAttributesToPage(); |
515 | 0 | } |
516 | 233 | return true; |
517 | 233 | } |
518 | 212 | error(value.getStart(), "pushedinheritedpageresources must be a boolean"); |
519 | 212 | return true; |
520 | 445 | } |
521 | 3.09k | if (key == "calledgetallpages") { |
522 | 705 | bool v; |
523 | 705 | if (value.getBool(v)) { |
524 | 240 | if (!must_be_complete && v) { |
525 | 0 | (void)pdf.doc().pages().all(); |
526 | 0 | } |
527 | 240 | return true; |
528 | 240 | } |
529 | 465 | error(value.getStart(), "calledgetallpages must be a boolean"); |
530 | 465 | return true; |
531 | 705 | } |
532 | | // ignore unknown keys for forward compatibility and to skip keys we don't care about |
533 | | // like "maxobjectid". |
534 | 2.38k | return true; |
535 | 3.09k | } |
536 | | |
537 | 172k | if (state == st_objects) { |
538 | 23.7k | if (key == "trailer") { |
539 | 1.53k | saw_trailer = true; |
540 | 1.53k | cur_object = "trailer"; |
541 | 1.53k | setNextStateIfDictionary(key, value, st_trailer); |
542 | 1.53k | return true; |
543 | 1.53k | } |
544 | | |
545 | 22.2k | int obj = 0; |
546 | 22.2k | int gen = 0; |
547 | 22.2k | if (is_obj_key(key, obj, gen)) { |
548 | 15.9k | cur_object = key; |
549 | 15.9k | if (setNextStateIfDictionary(key, value, st_object_top)) { |
550 | 14.6k | next_obj = objects.getObjectForJSON(obj, gen); |
551 | 14.6k | } |
552 | 15.9k | return true; |
553 | 15.9k | } |
554 | 6.26k | error(value.getStart(), "object key should be \"trailer\" or \"obj:n n R\""); |
555 | 6.26k | return true; |
556 | 22.2k | } |
557 | | |
558 | 148k | if (state == st_object_top) { |
559 | 25.3k | util::assertion(!stack.empty(), "QPDF_json: stack empty in st_object_top"); |
560 | 25.3k | auto& tos = stack.back(); |
561 | 25.3k | util::assertion(!!tos.object, "current object uninitialized in st_object_top"); |
562 | 25.3k | if (key == "value") { |
563 | | // Don't use setNextStateIfDictionary since this can have any type. |
564 | 16.8k | saw_value = true; |
565 | 16.8k | replaceObject(makeObject(value), value); |
566 | 16.8k | next_state = st_object; |
567 | 16.8k | return true; |
568 | 16.8k | } |
569 | 8.44k | if (key == "stream") { |
570 | 5.97k | saw_stream = true; |
571 | 5.97k | if (setNextStateIfDictionary(key, value, st_stream)) { |
572 | 5.50k | this_stream_needs_data = false; |
573 | 5.50k | if (tos.object.isStream()) { |
574 | 1.89k | QTC::TC("qpdf", "QPDF_json updating existing stream"); |
575 | 3.60k | } else { |
576 | 3.60k | this_stream_needs_data = true; |
577 | 3.60k | replaceObject( |
578 | 3.60k | qpdf::Stream( |
579 | 3.60k | pdf, tos.object.getObjGen(), QPDFObjectHandle::newDictionary(), 0, 0), |
580 | 3.60k | value); |
581 | 3.60k | } |
582 | 5.50k | next_obj = tos.object; |
583 | 5.50k | return true; |
584 | 5.50k | } |
585 | 469 | return true; // Error message already given above |
586 | 5.97k | } |
587 | 2.47k | return true; // Ignore unknown keys for forward compatibility |
588 | 8.44k | } |
589 | | |
590 | 122k | if (state == st_trailer) { |
591 | 2.06k | if (key == "value") { |
592 | 1.36k | saw_value = true; |
593 | | // The trailer must be a dictionary, so we can use setNextStateIfDictionary. |
594 | 1.36k | if (setNextStateIfDictionary("trailer.value", value, st_object)) { |
595 | 559 | pdf.m->trailer = makeObject(value); |
596 | 559 | setObjectDescription(pdf.m->trailer, value); |
597 | 559 | } |
598 | 1.36k | return true; |
599 | 1.36k | } |
600 | 700 | if (key == "stream") { |
601 | | // Don't need to set saw_stream here since there's already an error. |
602 | 396 | error(value.getStart(), "the trailer may not be a stream"); |
603 | 396 | return true; |
604 | 396 | } |
605 | 304 | return true; // Ignore unknown keys for forward compatibility |
606 | 700 | } |
607 | | |
608 | 120k | if (state == st_stream) { |
609 | 15.6k | util::assertion(!stack.empty(), "stack empty in st_stream"); |
610 | 15.6k | auto& tos = stack.back(); |
611 | 15.6k | util::assertion(tos.object.isStream(), "current object is not stream in st_stream"); |
612 | 15.6k | if (key == "dict") { |
613 | 4.06k | saw_dict = true; |
614 | 4.06k | if (setNextStateIfDictionary("stream.dict", value, st_object)) { |
615 | 3.57k | tos.object.replaceDict(makeObject(value)); |
616 | 3.57k | return true; |
617 | 3.57k | } |
618 | 491 | return true; // An error had already been given by setNextStateIfDictionary |
619 | 4.06k | } |
620 | 11.5k | if (key == "data") { |
621 | 8.78k | saw_data = true; |
622 | 8.78k | std::string v; |
623 | 8.78k | if (!value.getString(v)) { |
624 | 2.57k | error(value.getStart(), "\"stream.data\" must be a string"); |
625 | 2.57k | tos.object.replaceStreamData("", {}, {}); |
626 | 2.57k | return true; |
627 | 2.57k | } |
628 | | // The range includes the quotes. |
629 | 6.20k | auto start = value.getStart() + 1; |
630 | 6.20k | auto end = value.getEnd() - 1; |
631 | 6.20k | util::assertion(end >= start, "QPDF_json: JSON string length < 0"); |
632 | 6.20k | tos.object.replaceStreamData(provide_data(is, start, end), {}, {}); |
633 | 6.20k | return true; |
634 | 8.78k | } |
635 | 2.79k | if (key == "datafile") { |
636 | 1.53k | saw_datafile = true; |
637 | 1.53k | std::string filename; |
638 | 1.53k | if (!value.getString(filename)) { |
639 | 366 | error( |
640 | 366 | value.getStart(), |
641 | 366 | "\"stream.datafile\" must be a string containing a file name"); |
642 | 366 | tos.object.replaceStreamData("", {}, {}); |
643 | 366 | return true; |
644 | 366 | } |
645 | 1.16k | tos.object.replaceStreamData(QUtil::file_provider(filename), {}, {}); |
646 | 1.16k | return true; |
647 | 1.53k | } |
648 | 1.25k | return true; // Ignore unknown keys for forward compatibility. |
649 | 2.79k | } |
650 | | |
651 | 105k | util::assertion(state == st_object, "QPDF_json: unknown state " + std::to_string(state)); |
652 | 105k | util::assertion(!stack.empty(), "stack empty in st_object"); |
653 | 105k | auto& tos = stack.back(); |
654 | 105k | auto dict = tos.object; |
655 | 105k | if (dict.isStream()) { |
656 | 0 | dict = dict.getDict(); |
657 | 0 | } |
658 | 105k | util::assertion( |
659 | 105k | dict.isDictionary(), |
660 | 105k | "current object is not stream or dictionary in st_object dictionary item"); |
661 | 105k | dict.replaceKey( |
662 | 105k | is_pdf_name(key) ? QPDFObjectHandle::parse(key.substr(2)).getName() : key, |
663 | 105k | makeObject(value)); |
664 | 105k | return true; |
665 | 120k | } |
666 | | |
667 | | bool |
668 | | QPDF::JSONReactor::arrayItem(JSON const& value) |
669 | 1.41M | { |
670 | 1.41M | if (stack.empty()) { |
671 | 0 | throw std::logic_error("stack is empty in arrayItem"); |
672 | 0 | } |
673 | 1.41M | next_state = st_ignore; |
674 | 1.41M | auto state = stack.back().state; |
675 | 1.41M | if (state == st_qpdf) { |
676 | 15.0k | if (!this->saw_qpdf_meta) { |
677 | 5.45k | this->saw_qpdf_meta = true; |
678 | 5.45k | setNextStateIfDictionary("qpdf[0]", value, st_qpdf_meta); |
679 | 9.62k | } else if (!this->saw_objects) { |
680 | 4.88k | this->saw_objects = true; |
681 | 4.88k | setNextStateIfDictionary("qpdf[1]", value, st_objects); |
682 | 4.88k | } else { |
683 | 4.74k | QTC::TC("qpdf", "QPDF_json more than two qpdf elements"); |
684 | 4.74k | error(value.getStart(), "\"qpdf\" must have two elements"); |
685 | 4.74k | } |
686 | 1.40M | } else if (state == st_object) { |
687 | 1.39M | stack.back().object.appendItem(makeObject(value)); |
688 | 1.39M | } |
689 | 1.41M | return true; |
690 | 1.41M | } |
691 | | |
692 | | void |
693 | | QPDF::JSONReactor::setObjectDescription(QPDFObjectHandle& oh, JSON const& value) |
694 | 1.25M | { |
695 | 1.25M | auto j_descr = std::get<QPDFObject::JSON_Descr>(*descr); |
696 | 1.25M | if (j_descr.object != cur_object) { |
697 | 11.3k | descr = std::make_shared<QPDFObject::Description>( |
698 | 11.3k | QPDFObject::JSON_Descr(j_descr.input, cur_object)); |
699 | 11.3k | } |
700 | | |
701 | 1.25M | oh.obj_sp()->setDescription(&pdf, descr, value.getStart()); |
702 | 1.25M | } |
703 | | |
704 | | QPDFObjectHandle |
705 | | QPDF::JSONReactor::makeObject(JSON const& value) |
706 | 1.52M | { |
707 | 1.52M | QPDFObjectHandle result; |
708 | 1.52M | std::string str_v; |
709 | 1.52M | bool bool_v = false; |
710 | 1.52M | if (value.isDictionary()) { |
711 | 21.0k | result = QPDFObjectHandle::newDictionary(); |
712 | 21.0k | next_obj = result; |
713 | 21.0k | next_state = st_object; |
714 | 1.50M | } else if (value.isArray()) { |
715 | 11.8k | result = QPDFObjectHandle::newArray(); |
716 | 11.8k | next_obj = result; |
717 | 11.8k | next_state = st_object; |
718 | 1.48M | } else if (value.isNull()) { |
719 | 108 | result = QPDFObjectHandle::newNull(); |
720 | 1.48M | } else if (value.getBool(bool_v)) { |
721 | 669 | result = QPDFObjectHandle::newBool(bool_v); |
722 | 1.48M | } else if (value.getNumber(str_v)) { |
723 | 1.15M | if (QUtil::is_long_long(str_v.c_str())) { |
724 | 1.14M | result = QPDFObjectHandle::newInteger(QUtil::string_to_ll(str_v.c_str())); |
725 | 1.14M | } else { |
726 | | // JSON allows scientific notation, but PDF does not. |
727 | 6.03k | if (str_v.find('e') != std::string::npos || str_v.find('E') != std::string::npos) { |
728 | 2.96k | try { |
729 | 2.96k | auto v = std::stod(str_v); |
730 | 2.96k | str_v = QUtil::double_to_string(v); |
731 | 2.96k | } catch (std::exception&) { |
732 | | // Keep it as it was |
733 | 212 | } |
734 | 2.96k | } |
735 | 6.03k | result = QPDFObjectHandle::newReal(str_v); |
736 | 6.03k | } |
737 | 1.15M | } else if (value.getString(str_v)) { |
738 | 332k | int obj = 0; |
739 | 332k | int gen = 0; |
740 | 332k | std::string str; |
741 | 332k | if (is_indirect_object(str_v, obj, gen)) { |
742 | 282k | result = objects.getObjectForJSON(obj, gen); |
743 | 282k | } else if (is_unicode_string(str_v, str)) { |
744 | 5.55k | result = QPDFObjectHandle::newUnicodeString(str); |
745 | 43.9k | } else if (is_binary_string(str_v, str)) { |
746 | 1.04k | result = QPDFObjectHandle::newString(QUtil::hex_decode(str)); |
747 | 42.9k | } else if (is_name(str_v)) { |
748 | 16.4k | result = QPDFObjectHandle::newName(str_v); |
749 | 26.4k | } else if (is_pdf_name(str_v)) { |
750 | 8.38k | result = QPDFObjectHandle::parse(str_v.substr(2)); |
751 | 18.0k | } else { |
752 | 18.0k | QTC::TC("qpdf", "QPDF_json unrecognized string value"); |
753 | 18.0k | error(value.getStart(), "unrecognized string value"); |
754 | 18.0k | result = QPDFObjectHandle::newNull(); |
755 | 18.0k | } |
756 | 332k | } |
757 | 1.52M | if (!result) { |
758 | 0 | throw std::logic_error("JSONReactor::makeObject didn't initialize the object"); |
759 | 0 | } |
760 | | |
761 | 1.52M | if (!result.hasObjectDescription()) { |
762 | 1.23M | setObjectDescription(result, value); |
763 | 1.23M | } |
764 | 1.52M | return result; |
765 | 1.52M | } |
766 | | |
767 | | void |
768 | | QPDF::createFromJSON(std::string const& json_file) |
769 | 0 | { |
770 | 0 | createFromJSON(std::make_shared<FileInputSource>(json_file.c_str())); |
771 | 0 | } |
772 | | |
773 | | void |
774 | | QPDF::createFromJSON(std::shared_ptr<InputSource> is) |
775 | 7.63k | { |
776 | 7.63k | auto mw = m->cf.max_warnings(); |
777 | 7.63k | (void)m->cf.max_warnings(0); |
778 | 7.63k | processMemoryFile(is->getName().c_str(), JSON_PDF, strlen(JSON_PDF)); |
779 | 7.63k | (void)m->cf.max_warnings(mw); |
780 | 7.63k | importJSON(is, true); |
781 | 7.63k | (void)m->cf.max_warnings(0); |
782 | 7.63k | } |
783 | | |
784 | | void |
785 | | QPDF::updateFromJSON(std::string const& json_file) |
786 | 0 | { |
787 | 0 | updateFromJSON(std::make_shared<FileInputSource>(json_file.c_str())); |
788 | 0 | } |
789 | | |
790 | | void |
791 | | QPDF::updateFromJSON(std::shared_ptr<InputSource> is) |
792 | 0 | { |
793 | 0 | importJSON(is, false); |
794 | 0 | } |
795 | | |
796 | | void |
797 | | QPDF::importJSON(std::shared_ptr<InputSource> is, bool must_be_complete) |
798 | 7.63k | { |
799 | 7.63k | JSONReactor reactor(*this, is, must_be_complete); |
800 | 7.63k | try { |
801 | 7.63k | JSON::parse(*is, &reactor); |
802 | 7.63k | } catch (std::runtime_error& e) { |
803 | 7.57k | throw std::runtime_error(is->getName() + ": " + e.what()); |
804 | 7.57k | } |
805 | 68 | if (reactor.anyErrors()) { |
806 | 54 | throw std::runtime_error(is->getName() + ": errors found in JSON"); |
807 | 54 | } |
808 | 68 | } |
809 | | |
810 | | void |
811 | | writeJSONStreamFile( |
812 | | int version, |
813 | | JSON::Writer& jw, |
814 | | qpdf::Stream& stream, |
815 | | int id, |
816 | | qpdf_stream_decode_level_e decode_level, |
817 | | std::string const& file_prefix) |
818 | 0 | { |
819 | 0 | auto filename = file_prefix + "-" + std::to_string(id); |
820 | 0 | auto* f = QUtil::safe_fopen(filename.c_str(), "wb"); |
821 | 0 | Pl_StdioFile f_pl{"stream data", f}; |
822 | 0 | stream.writeStreamJSON(version, jw, qpdf_sj_file, decode_level, &f_pl, filename); |
823 | 0 | f_pl.finish(); |
824 | 0 | fclose(f); |
825 | 0 | } |
826 | | |
827 | | void |
828 | | QPDF::writeJSON( |
829 | | int version, |
830 | | Pipeline* p, |
831 | | qpdf_stream_decode_level_e decode_level, |
832 | | qpdf_json_stream_data_e json_stream_data, |
833 | | std::string const& file_prefix, |
834 | | std::set<std::string> wanted_objects) |
835 | 0 | { |
836 | 0 | bool first = true; |
837 | 0 | writeJSON(version, p, true, first, decode_level, json_stream_data, file_prefix, wanted_objects); |
838 | 0 | } |
839 | | |
840 | | void |
841 | | QPDF::writeJSON( |
842 | | int version, |
843 | | Pipeline* p, |
844 | | bool complete, |
845 | | bool& first_key, |
846 | | qpdf_stream_decode_level_e decode_level, |
847 | | qpdf_json_stream_data_e json_stream_data, |
848 | | std::string const& file_prefix, |
849 | | std::set<std::string> wanted_objects) |
850 | 0 | { |
851 | 0 | if (version != 2) { |
852 | 0 | throw std::runtime_error("QPDF::writeJSON: only version 2 is supported"); |
853 | 0 | } |
854 | 0 | JSON::Writer jw{p, 4}; |
855 | 0 | if (complete) { |
856 | 0 | jw << "{"; |
857 | 0 | } else if (!first_key) { |
858 | 0 | jw << ","; |
859 | 0 | } |
860 | 0 | first_key = false; |
861 | | |
862 | | /* clang-format off */ |
863 | 0 | jw << "\n" |
864 | 0 | " \"qpdf\": [\n" |
865 | 0 | " {\n" |
866 | 0 | " \"jsonversion\": " << std::to_string(version) << ",\n" |
867 | 0 | " \"pdfversion\": \"" << getPDFVersion() << "\",\n" |
868 | 0 | " \"pushedinheritedpageresources\": " << (everPushedInheritedAttributesToPages() ? "true" : "false") << ",\n" |
869 | 0 | " \"calledgetallpages\": " << (everCalledGetAllPages() ? "true" : "false") << ",\n" |
870 | 0 | " \"maxobjectid\": " << std::to_string(getObjectCount()) << "\n" |
871 | 0 | " },\n" |
872 | 0 | " {"; |
873 | | /* clang-format on */ |
874 | |
|
875 | 0 | bool all_objects = wanted_objects.empty(); |
876 | 0 | bool first = true; |
877 | 0 | for (auto& obj: getAllObjects()) { |
878 | 0 | auto const og = obj.getObjGen(); |
879 | 0 | std::string key = "obj:" + og.unparse(' ') + " R"; |
880 | 0 | if (all_objects || wanted_objects.contains(key)) { |
881 | 0 | if (first) { |
882 | 0 | jw << "\n \"" << key; |
883 | 0 | first = false; |
884 | 0 | } else { |
885 | 0 | jw << "\n },\n \"" << key; |
886 | 0 | } |
887 | 0 | if (Stream stream = obj) { |
888 | 0 | jw << "\": {\n \"stream\": "; |
889 | 0 | if (json_stream_data == qpdf_sj_file) { |
890 | 0 | writeJSONStreamFile( |
891 | 0 | version, jw, stream, og.getObj(), decode_level, file_prefix); |
892 | 0 | } else { |
893 | 0 | stream.writeStreamJSON( |
894 | 0 | version, jw, json_stream_data, decode_level, nullptr, ""); |
895 | 0 | } |
896 | 0 | } else { |
897 | 0 | jw << "\": {\n \"value\": "; |
898 | 0 | obj.writeJSON(version, jw, true); |
899 | 0 | } |
900 | 0 | } |
901 | 0 | } |
902 | 0 | if (all_objects || wanted_objects.contains("trailer")) { |
903 | 0 | if (!first) { |
904 | 0 | jw << "\n },"; |
905 | 0 | } |
906 | 0 | jw << "\n \"trailer\": {\n \"value\": "; |
907 | 0 | getTrailer().writeJSON(version, jw, true); |
908 | 0 | first = false; |
909 | 0 | } |
910 | 0 | if (!first) { |
911 | 0 | jw << "\n }"; |
912 | 0 | } |
913 | | /* clang-format off */ |
914 | 0 | jw << "\n" |
915 | 0 | " }\n" |
916 | 0 | " ]"; |
917 | | /* clang-format on */ |
918 | 0 | if (complete) { |
919 | 0 | jw << "\n}\n"; |
920 | 0 | p->finish(); |
921 | 0 | } |
922 | 0 | } |