Commit 8ad1ea34feb3f867b18ab13e1ac521431c0f9dd0

Authored by m-holger
1 parent 6670c685

Add private methods QPDFParser::warn

Part of #729
libqpdf/QPDFParser.cc
... ... @@ -55,28 +55,14 @@ QPDFParser::parse(bool& empty, bool content_stream)
55 55 std::string const& token_error_message = token.getErrorMessage();
56 56 if (!token_error_message.empty()) {
57 57 // Tokens other than tt_bad can still generate warnings.
58   - warn(
59   - context,
60   - QPDFExc(
61   - qpdf_e_damaged_pdf,
62   - input->getName(),
63   - object_description,
64   - input->getLastOffset(),
65   - token_error_message));
  58 + warn(token_error_message);
66 59 }
67 60  
68 61 switch (token.getType()) {
69 62 case QPDFTokenizer::tt_eof:
70 63 if (!content_stream) {
71 64 QTC::TC("qpdf", "QPDFParser eof in parse");
72   - warn(
73   - context,
74   - QPDFExc(
75   - qpdf_e_damaged_pdf,
76   - input->getName(),
77   - object_description,
78   - input->getLastOffset(),
79   - "unexpected EOF"));
  65 + warn("unexpected EOF");
80 66 }
81 67 bad = true;
82 68 state = st_eof;
... ... @@ -91,14 +77,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
91 77 case QPDFTokenizer::tt_brace_open:
92 78 case QPDFTokenizer::tt_brace_close:
93 79 QTC::TC("qpdf", "QPDFParser bad brace");
94   - warn(
95   - context,
96   - QPDFExc(
97   - qpdf_e_damaged_pdf,
98   - input->getName(),
99   - object_description,
100   - input->getLastOffset(),
101   - "treating unexpected brace token as null"));
  80 + warn("treating unexpected brace token as null");
102 81 bad = true;
103 82 object = QPDFObjectHandle::newNull();
104 83 break;
... ... @@ -108,14 +87,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
108 87 state = st_stop;
109 88 } else {
110 89 QTC::TC("qpdf", "QPDFParser bad array close");
111   - warn(
112   - context,
113   - QPDFExc(
114   - qpdf_e_damaged_pdf,
115   - input->getName(),
116   - object_description,
117   - input->getLastOffset(),
118   - "treating unexpected array close token as null"));
  90 + warn("treating unexpected array close token as null");
119 91 bad = true;
120 92 object = QPDFObjectHandle::newNull();
121 93 }
... ... @@ -126,14 +98,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
126 98 state = st_stop;
127 99 } else {
128 100 QTC::TC("qpdf", "QPDFParser bad dictionary close");
129   - warn(
130   - context,
131   - QPDFExc(
132   - qpdf_e_damaged_pdf,
133   - input->getName(),
134   - object_description,
135   - input->getLastOffset(),
136   - "unexpected dictionary close token"));
  101 + warn("unexpected dictionary close token");
137 102 bad = true;
138 103 object = QPDFObjectHandle::newNull();
139 104 }
... ... @@ -143,14 +108,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
143 108 case QPDFTokenizer::tt_dict_open:
144 109 if (olist_stack.size() > 500) {
145 110 QTC::TC("qpdf", "QPDFParser too deep");
146   - warn(
147   - context,
148   - QPDFExc(
149   - qpdf_e_damaged_pdf,
150   - input->getName(),
151   - object_description,
152   - input->getLastOffset(),
153   - "ignoring excessively deeply nested data structure"));
  111 + warn("ignoring excessively deeply nested data structure");
154 112 bad = true;
155 113 object = QPDFObjectHandle::newNull();
156 114 state = st_top;
... ... @@ -233,15 +191,8 @@ QPDFParser::parse(bool& empty, bool content_stream)
233 191 empty = true;
234 192 } else {
235 193 QTC::TC("qpdf", "QPDFParser treat word as string");
236   - warn(
237   - context,
238   - QPDFExc(
239   - qpdf_e_damaged_pdf,
240   - input->getName(),
241   - object_description,
242   - input->getLastOffset(),
243   - "unknown token while reading object;"
244   - " treating as string"));
  194 + warn("unknown token while reading object;"
  195 + " treating as string");
245 196 bad = true;
246 197 object = QPDFObjectHandle::newString(value);
247 198 }
... ... @@ -265,15 +216,8 @@ QPDFParser::parse(bool& empty, bool content_stream)
265 216 break;
266 217  
267 218 default:
268   - warn(
269   - context,
270   - QPDFExc(
271   - qpdf_e_damaged_pdf,
272   - input->getName(),
273   - object_description,
274   - input->getLastOffset(),
275   - "treating unknown token type as null while "
276   - "reading object"));
  219 + warn("treating unknown token type as null while "
  220 + "reading object");
277 221 bad = true;
278 222 object = QPDFObjectHandle::newNull();
279 223 break;
... ... @@ -299,14 +243,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
299 243 if (bad_count > 5) {
300 244 // We had too many consecutive errors without enough
301 245 // intervening successful objects. Give up.
302   - warn(
303   - context,
304   - QPDFExc(
305   - qpdf_e_damaged_pdf,
306   - input->getName(),
307   - object_description,
308   - input->getLastOffset(),
309   - "too many errors; giving up on reading object"));
  246 + warn("too many errors; giving up on reading object");
310 247 state = st_top;
311 248 object = QPDFObjectHandle::newNull();
312 249 }
... ... @@ -314,14 +251,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
314 251 switch (state) {
315 252 case st_eof:
316 253 if (state_stack.size() > 1) {
317   - warn(
318   - context,
319   - QPDFExc(
320   - qpdf_e_damaged_pdf,
321   - input->getName(),
322   - object_description,
323   - input->getLastOffset(),
324   - "parse error while reading object"));
  254 + warn("parse error while reading object");
325 255 }
326 256 done = true;
327 257 // In content stream mode, leave object uninitialized to
... ... @@ -404,28 +334,18 @@ QPDFParser::parse(bool& empty, bool content_stream)
404 334 (found_fake ? 0 : 1));
405 335 }
406 336 warn(
407   - context,
408   - QPDFExc(
409   - qpdf_e_damaged_pdf,
410   - input->getName(),
411   - object_description,
412   - offset,
413   - "expected dictionary key but found"
414   - " non-name object; inserting key " +
415   - candidate));
  337 + offset,
  338 + "expected dictionary key but found"
  339 + " non-name object; inserting key " +
  340 + candidate);
416 341 val = key_obj;
417 342 key_obj = QPDFObjectHandle::newName(candidate);
418 343 } else if (i + 1 >= olist.size()) {
419 344 QTC::TC("qpdf", "QPDFParser no val for last key");
420 345 warn(
421   - context,
422   - QPDFExc(
423   - qpdf_e_damaged_pdf,
424   - input->getName(),
425   - object_description,
426   - offset,
427   - "dictionary ended prematurely; "
428   - "using null as value for last key"));
  346 + offset,
  347 + "dictionary ended prematurely; "
  348 + "using null as value for last key");
429 349 val = QPDFObjectHandle::newNull();
430 350 QPDFObjectHandle::setObjectDescriptionFromInput(
431 351 val, context, object_description, input, offset);
... ... @@ -436,15 +356,10 @@ QPDFParser::parse(bool& empty, bool content_stream)
436 356 if (dict.count(key) > 0) {
437 357 QTC::TC("qpdf", "QPDFParser duplicate dict key");
438 358 warn(
439   - context,
440   - QPDFExc(
441   - qpdf_e_damaged_pdf,
442   - input->getName(),
443   - object_description,
444   - offset,
445   - "dictionary has duplicated key " + key +
446   - "; last occurrence overrides earlier "
447   - "ones"));
  359 + offset,
  360 + "dictionary has duplicated key " + key +
  361 + "; last occurrence overrides earlier "
  362 + "ones");
448 363 }
449 364 dict[key] = val;
450 365 }
... ... @@ -501,3 +416,22 @@ QPDFParser::warn(QPDF* qpdf, QPDFExc const& e)
501 416 throw e;
502 417 }
503 418 }
  419 +
  420 +void
  421 +QPDFParser::warn(qpdf_offset_t offset, std::string const& msg) const
  422 +{
  423 + warn(
  424 + context,
  425 + QPDFExc(
  426 + qpdf_e_damaged_pdf,
  427 + input->getName(),
  428 + object_description,
  429 + offset,
  430 + msg));
  431 +}
  432 +
  433 +void
  434 +QPDFParser::warn(std::string const& msg) const
  435 +{
  436 + warn(input->getLastOffset(), msg);
  437 +}
... ...
libqpdf/qpdf/QPDFParser.hh
... ... @@ -37,6 +37,8 @@ class QPDFParser
37 37 st_array
38 38 };
39 39  
  40 + void warn(qpdf_offset_t offset, std::string const& msg) const;
  41 + void warn(std::string const& msg) const;
40 42 static void warn(QPDF*, QPDFExc const&);
41 43 void setParsedOffset(qpdf_offset_t offset);
42 44  
... ...