Commit 8ad1ea34feb3f867b18ab13e1ac521431c0f9dd0
1 parent
6670c685
Add private methods QPDFParser::warn
Part of #729
Showing
2 changed files
with
44 additions
and
108 deletions
libqpdf/QPDFParser.cc
| @@ -55,28 +55,14 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -55,28 +55,14 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 55 | std::string const& token_error_message = token.getErrorMessage(); | 55 | std::string const& token_error_message = token.getErrorMessage(); |
| 56 | if (!token_error_message.empty()) { | 56 | if (!token_error_message.empty()) { |
| 57 | // Tokens other than tt_bad can still generate warnings. | 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 | switch (token.getType()) { | 61 | switch (token.getType()) { |
| 69 | case QPDFTokenizer::tt_eof: | 62 | case QPDFTokenizer::tt_eof: |
| 70 | if (!content_stream) { | 63 | if (!content_stream) { |
| 71 | QTC::TC("qpdf", "QPDFParser eof in parse"); | 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 | bad = true; | 67 | bad = true; |
| 82 | state = st_eof; | 68 | state = st_eof; |
| @@ -91,14 +77,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -91,14 +77,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 91 | case QPDFTokenizer::tt_brace_open: | 77 | case QPDFTokenizer::tt_brace_open: |
| 92 | case QPDFTokenizer::tt_brace_close: | 78 | case QPDFTokenizer::tt_brace_close: |
| 93 | QTC::TC("qpdf", "QPDFParser bad brace"); | 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 | bad = true; | 81 | bad = true; |
| 103 | object = QPDFObjectHandle::newNull(); | 82 | object = QPDFObjectHandle::newNull(); |
| 104 | break; | 83 | break; |
| @@ -108,14 +87,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -108,14 +87,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 108 | state = st_stop; | 87 | state = st_stop; |
| 109 | } else { | 88 | } else { |
| 110 | QTC::TC("qpdf", "QPDFParser bad array close"); | 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 | bad = true; | 91 | bad = true; |
| 120 | object = QPDFObjectHandle::newNull(); | 92 | object = QPDFObjectHandle::newNull(); |
| 121 | } | 93 | } |
| @@ -126,14 +98,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -126,14 +98,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 126 | state = st_stop; | 98 | state = st_stop; |
| 127 | } else { | 99 | } else { |
| 128 | QTC::TC("qpdf", "QPDFParser bad dictionary close"); | 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 | bad = true; | 102 | bad = true; |
| 138 | object = QPDFObjectHandle::newNull(); | 103 | object = QPDFObjectHandle::newNull(); |
| 139 | } | 104 | } |
| @@ -143,14 +108,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -143,14 +108,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 143 | case QPDFTokenizer::tt_dict_open: | 108 | case QPDFTokenizer::tt_dict_open: |
| 144 | if (olist_stack.size() > 500) { | 109 | if (olist_stack.size() > 500) { |
| 145 | QTC::TC("qpdf", "QPDFParser too deep"); | 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 | bad = true; | 112 | bad = true; |
| 155 | object = QPDFObjectHandle::newNull(); | 113 | object = QPDFObjectHandle::newNull(); |
| 156 | state = st_top; | 114 | state = st_top; |
| @@ -233,15 +191,8 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -233,15 +191,8 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 233 | empty = true; | 191 | empty = true; |
| 234 | } else { | 192 | } else { |
| 235 | QTC::TC("qpdf", "QPDFParser treat word as string"); | 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 | bad = true; | 196 | bad = true; |
| 246 | object = QPDFObjectHandle::newString(value); | 197 | object = QPDFObjectHandle::newString(value); |
| 247 | } | 198 | } |
| @@ -265,15 +216,8 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -265,15 +216,8 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 265 | break; | 216 | break; |
| 266 | 217 | ||
| 267 | default: | 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 | bad = true; | 221 | bad = true; |
| 278 | object = QPDFObjectHandle::newNull(); | 222 | object = QPDFObjectHandle::newNull(); |
| 279 | break; | 223 | break; |
| @@ -299,14 +243,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -299,14 +243,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 299 | if (bad_count > 5) { | 243 | if (bad_count > 5) { |
| 300 | // We had too many consecutive errors without enough | 244 | // We had too many consecutive errors without enough |
| 301 | // intervening successful objects. Give up. | 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 | state = st_top; | 247 | state = st_top; |
| 311 | object = QPDFObjectHandle::newNull(); | 248 | object = QPDFObjectHandle::newNull(); |
| 312 | } | 249 | } |
| @@ -314,14 +251,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -314,14 +251,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 314 | switch (state) { | 251 | switch (state) { |
| 315 | case st_eof: | 252 | case st_eof: |
| 316 | if (state_stack.size() > 1) { | 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 | done = true; | 256 | done = true; |
| 327 | // In content stream mode, leave object uninitialized to | 257 | // In content stream mode, leave object uninitialized to |
| @@ -404,28 +334,18 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -404,28 +334,18 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 404 | (found_fake ? 0 : 1)); | 334 | (found_fake ? 0 : 1)); |
| 405 | } | 335 | } |
| 406 | warn( | 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 | val = key_obj; | 341 | val = key_obj; |
| 417 | key_obj = QPDFObjectHandle::newName(candidate); | 342 | key_obj = QPDFObjectHandle::newName(candidate); |
| 418 | } else if (i + 1 >= olist.size()) { | 343 | } else if (i + 1 >= olist.size()) { |
| 419 | QTC::TC("qpdf", "QPDFParser no val for last key"); | 344 | QTC::TC("qpdf", "QPDFParser no val for last key"); |
| 420 | warn( | 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 | val = QPDFObjectHandle::newNull(); | 349 | val = QPDFObjectHandle::newNull(); |
| 430 | QPDFObjectHandle::setObjectDescriptionFromInput( | 350 | QPDFObjectHandle::setObjectDescriptionFromInput( |
| 431 | val, context, object_description, input, offset); | 351 | val, context, object_description, input, offset); |
| @@ -436,15 +356,10 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -436,15 +356,10 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 436 | if (dict.count(key) > 0) { | 356 | if (dict.count(key) > 0) { |
| 437 | QTC::TC("qpdf", "QPDFParser duplicate dict key"); | 357 | QTC::TC("qpdf", "QPDFParser duplicate dict key"); |
| 438 | warn( | 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 | dict[key] = val; | 364 | dict[key] = val; |
| 450 | } | 365 | } |
| @@ -501,3 +416,22 @@ QPDFParser::warn(QPDF* qpdf, QPDFExc const& e) | @@ -501,3 +416,22 @@ QPDFParser::warn(QPDF* qpdf, QPDFExc const& e) | ||
| 501 | throw e; | 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,6 +37,8 @@ class QPDFParser | ||
| 37 | st_array | 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 | static void warn(QPDF*, QPDFExc const&); | 42 | static void warn(QPDF*, QPDFExc const&); |
| 41 | void setParsedOffset(qpdf_offset_t offset); | 43 | void setParsedOffset(qpdf_offset_t offset); |
| 42 | 44 |