2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDFTokenizer.hh>
|
|
|
|
|
|
|
|
// DO NOT USE ctype -- it is locale dependent for some things, and it's not worth the risk of
|
|
|
|
// including it in case it may accidentally be used.
|
|
|
|
|
|
|
|
#include <qpdf/QIntC.hh>
|
2012-07-21 09:48:13 +00:00
|
|
|
#include <qpdf/QPDFExc.hh>
|
2018-02-02 23:21:34 +00:00
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-05-04 16:02:53 +00:00
|
|
|
|
2023-01-30 14:45:04 +00:00
|
|
|
static inline bool
|
2019-01-30 19:20:56 +00:00
|
|
|
is_delimiter(char ch)
|
|
|
|
{
|
2023-01-30 14:45:04 +00:00
|
|
|
return (
|
|
|
|
ch == ' ' || ch == '\n' || ch == '/' || ch == '(' || ch == ')' || ch == '{' || ch == '}' ||
|
|
|
|
ch == '<' || ch == '>' || ch == '[' || ch == ']' || ch == '%' || ch == '\t' || ch == '\r' ||
|
|
|
|
ch == '\v' || ch == '\f' || ch == 0);
|
2019-01-30 19:20:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 17:21:57 +00:00
|
|
|
namespace
|
2019-01-30 19:20:56 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
class QPDFWordTokenFinder: public InputSource::Finder
|
2019-01-30 19:20:56 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
public:
|
2024-07-25 15:42:29 +00:00
|
|
|
QPDFWordTokenFinder(InputSource& is, std::string const& str) :
|
2022-04-16 17:21:57 +00:00
|
|
|
is(is),
|
|
|
|
str(str)
|
|
|
|
{
|
|
|
|
}
|
2023-05-20 13:25:46 +00:00
|
|
|
~QPDFWordTokenFinder() override = default;
|
|
|
|
bool check() override;
|
2019-01-30 19:20:56 +00:00
|
|
|
|
2022-04-16 17:21:57 +00:00
|
|
|
private:
|
2024-07-25 15:42:29 +00:00
|
|
|
InputSource& is;
|
2022-04-16 17:21:57 +00:00
|
|
|
std::string str;
|
|
|
|
};
|
|
|
|
} // namespace
|
2019-01-30 19:20:56 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFWordTokenFinder::check()
|
|
|
|
{
|
|
|
|
// Find a word token matching the given string, preceded by a delimiter, and followed by a
|
|
|
|
// delimiter or EOF.
|
|
|
|
QPDFTokenizer tokenizer;
|
|
|
|
QPDFTokenizer::Token t = tokenizer.readToken(is, "finder", true);
|
2024-07-25 15:42:29 +00:00
|
|
|
qpdf_offset_t pos = is.tell();
|
2019-01-30 19:20:56 +00:00
|
|
|
if (!(t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, str))) {
|
2019-01-30 19:26:08 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer finder found wrong word");
|
2019-01-30 19:20:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-07-25 15:42:29 +00:00
|
|
|
qpdf_offset_t token_start = is.getLastOffset();
|
2019-01-30 19:20:56 +00:00
|
|
|
char next;
|
|
|
|
bool next_okay = false;
|
2024-07-25 15:42:29 +00:00
|
|
|
if (is.read(&next, 1) == 0) {
|
2019-01-30 19:20:56 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer inline image at EOF");
|
|
|
|
next_okay = true;
|
|
|
|
} else {
|
|
|
|
next_okay = is_delimiter(next);
|
|
|
|
}
|
2024-07-25 15:42:29 +00:00
|
|
|
is.seek(pos, SEEK_SET);
|
2019-01-30 19:20:56 +00:00
|
|
|
if (!next_okay) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (token_start == 0) {
|
|
|
|
// Can't actually happen...we never start the search at the beginning of the input.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
void
|
2022-08-18 12:13:19 +00:00
|
|
|
QPDFTokenizer::reset()
|
2018-01-30 01:00:06 +00:00
|
|
|
{
|
2022-08-24 16:29:50 +00:00
|
|
|
state = st_before_token;
|
2018-01-30 01:00:06 +00:00
|
|
|
type = tt_bad;
|
2022-08-20 18:43:00 +00:00
|
|
|
val.clear();
|
|
|
|
raw_val.clear();
|
2018-01-30 01:00:06 +00:00
|
|
|
error_message = "";
|
2022-08-24 16:29:50 +00:00
|
|
|
before_token = true;
|
|
|
|
in_token = false;
|
2018-01-30 01:00:06 +00:00
|
|
|
char_to_unread = '\0';
|
2019-01-30 19:20:56 +00:00
|
|
|
inline_image_bytes = 0;
|
2018-01-30 01:00:06 +00:00
|
|
|
string_depth = 0;
|
2022-08-22 23:14:43 +00:00
|
|
|
bad = false;
|
2018-01-30 01:00:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 23:21:34 +00:00
|
|
|
QPDFTokenizer::Token::Token(token_type_e type, std::string const& value) :
|
|
|
|
type(type),
|
|
|
|
value(value),
|
|
|
|
raw_value(value)
|
|
|
|
{
|
|
|
|
if (type == tt_string) {
|
|
|
|
raw_value = QPDFObjectHandle::newString(value).unparse();
|
2018-05-05 21:47:22 +00:00
|
|
|
} else if (type == tt_name) {
|
2018-02-02 23:21:34 +00:00
|
|
|
raw_value = QPDFObjectHandle::newName(value).unparse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
QPDFTokenizer::QPDFTokenizer() :
|
2022-08-18 12:13:19 +00:00
|
|
|
allow_eof(false),
|
|
|
|
include_ignorable(false)
|
2018-01-30 01:00:06 +00:00
|
|
|
{
|
2022-08-18 12:13:19 +00:00
|
|
|
reset();
|
2018-01-30 01:00:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 20:26:45 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::allowEOF()
|
|
|
|
{
|
2022-08-18 12:13:19 +00:00
|
|
|
this->allow_eof = true;
|
2013-01-20 20:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 23:28:45 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::includeIgnorable()
|
|
|
|
{
|
2022-08-18 12:13:19 +00:00
|
|
|
this->include_ignorable = true;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFTokenizer::isSpace(char ch)
|
|
|
|
{
|
|
|
|
return ((ch == '\0') || QUtil::is_space(ch));
|
|
|
|
}
|
|
|
|
|
2018-01-30 01:57:04 +00:00
|
|
|
bool
|
|
|
|
QPDFTokenizer::isDelimiter(char ch)
|
|
|
|
{
|
2019-01-30 19:20:56 +00:00
|
|
|
return is_delimiter(ch);
|
2018-01-30 01:57:04 +00:00
|
|
|
}
|
|
|
|
|
2012-08-11 13:22:59 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::presentCharacter(char ch)
|
|
|
|
{
|
2022-08-18 16:54:51 +00:00
|
|
|
handleCharacter(ch);
|
|
|
|
|
2022-08-27 11:43:49 +00:00
|
|
|
if (this->in_token) {
|
2022-08-23 11:49:01 +00:00
|
|
|
this->raw_val += ch;
|
2022-08-18 16:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::handleCharacter(char ch)
|
|
|
|
{
|
2023-10-14 21:04:58 +00:00
|
|
|
// In some cases, functions called below may call a second handler. This happens whenever you
|
|
|
|
// have to use a character from the next token to detect the end of the current token.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-08-18 15:53:16 +00:00
|
|
|
switch (this->state) {
|
2022-08-20 14:14:21 +00:00
|
|
|
case st_top:
|
2022-08-21 19:08:58 +00:00
|
|
|
inTop(ch);
|
|
|
|
return;
|
2022-08-18 15:53:16 +00:00
|
|
|
|
|
|
|
case st_in_space:
|
2022-08-21 19:08:58 +00:00
|
|
|
inSpace(ch);
|
|
|
|
return;
|
2022-08-18 15:53:16 +00:00
|
|
|
|
|
|
|
case st_in_comment:
|
2022-08-21 19:08:58 +00:00
|
|
|
inComment(ch);
|
2022-08-18 16:54:51 +00:00
|
|
|
return;
|
2022-08-18 15:53:16 +00:00
|
|
|
|
|
|
|
case st_lt:
|
2022-08-21 19:08:58 +00:00
|
|
|
inLt(ch);
|
2022-08-18 17:43:50 +00:00
|
|
|
return;
|
2022-08-18 15:53:16 +00:00
|
|
|
|
|
|
|
case st_gt:
|
2022-08-21 19:08:58 +00:00
|
|
|
inGt(ch);
|
2022-08-18 16:54:51 +00:00
|
|
|
return;
|
2022-02-08 14:18:08 +00:00
|
|
|
|
2022-08-20 14:44:44 +00:00
|
|
|
case st_in_string:
|
2022-08-20 11:01:49 +00:00
|
|
|
inString(ch);
|
2022-08-18 16:54:51 +00:00
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-08-22 23:14:43 +00:00
|
|
|
case st_name:
|
|
|
|
inName(ch);
|
|
|
|
return;
|
|
|
|
|
2022-08-23 01:00:55 +00:00
|
|
|
case st_number:
|
|
|
|
inNumber(ch);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case st_real:
|
|
|
|
inReal(ch);
|
|
|
|
return;
|
|
|
|
|
2022-08-20 11:01:49 +00:00
|
|
|
case st_string_after_cr:
|
2022-08-21 19:08:58 +00:00
|
|
|
inStringAfterCR(ch);
|
2022-08-19 18:10:38 +00:00
|
|
|
return;
|
|
|
|
|
2022-08-20 11:01:49 +00:00
|
|
|
case st_string_escape:
|
2022-08-21 19:08:58 +00:00
|
|
|
inStringEscape(ch);
|
|
|
|
return;
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case st_char_code:
|
|
|
|
inCharCode(ch);
|
|
|
|
return;
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case st_literal:
|
|
|
|
inLiteral(ch);
|
|
|
|
return;
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case st_inline_image:
|
|
|
|
inInlineImage(ch);
|
|
|
|
return;
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case st_in_hexstring:
|
|
|
|
inHexstring(ch);
|
|
|
|
return;
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case st_in_hexstring_2nd:
|
|
|
|
inHexstring2nd(ch);
|
|
|
|
return;
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-22 23:14:43 +00:00
|
|
|
case st_name_hex1:
|
|
|
|
inNameHex1(ch);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case st_name_hex2:
|
|
|
|
inNameHex2(ch);
|
|
|
|
return;
|
|
|
|
|
2022-08-23 01:00:55 +00:00
|
|
|
case st_sign:
|
|
|
|
inSign(ch);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case st_decimal:
|
|
|
|
inDecimal(ch);
|
|
|
|
return;
|
|
|
|
|
2022-08-24 16:29:50 +00:00
|
|
|
case (st_before_token):
|
|
|
|
inBeforeToken(ch);
|
|
|
|
return;
|
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case (st_token_ready):
|
|
|
|
inTokenReady(ch);
|
|
|
|
return;
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
default:
|
|
|
|
throw std::logic_error("INTERNAL ERROR: invalid state while reading token");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inTokenReady(char ch)
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: QPDF tokenizer presented character while token is waiting");
|
|
|
|
}
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
void
|
2022-08-24 16:29:50 +00:00
|
|
|
QPDFTokenizer::inBeforeToken(char ch)
|
2022-08-21 19:08:58 +00:00
|
|
|
{
|
|
|
|
// Note: we specifically do not use ctype here. It is locale-dependent.
|
|
|
|
if (isSpace(ch)) {
|
2022-08-24 16:29:50 +00:00
|
|
|
this->before_token = !this->include_ignorable;
|
|
|
|
this->in_token = this->include_ignorable;
|
2022-08-21 19:08:58 +00:00
|
|
|
if (this->include_ignorable) {
|
|
|
|
this->state = st_in_space;
|
2022-08-20 11:01:49 +00:00
|
|
|
}
|
2022-08-24 16:29:50 +00:00
|
|
|
} else if (ch == '%') {
|
|
|
|
this->before_token = !this->include_ignorable;
|
|
|
|
this->in_token = this->include_ignorable;
|
2022-08-21 19:08:58 +00:00
|
|
|
this->state = st_in_comment;
|
2022-08-24 16:29:50 +00:00
|
|
|
} else {
|
|
|
|
this->before_token = false;
|
|
|
|
this->in_token = true;
|
|
|
|
inTop(ch);
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 11:01:49 +00:00
|
|
|
|
2022-08-24 16:29:50 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::inTop(char ch)
|
|
|
|
{
|
|
|
|
switch (ch) {
|
2022-08-21 19:08:58 +00:00
|
|
|
case '(':
|
|
|
|
this->string_depth = 1;
|
|
|
|
this->state = st_in_string;
|
2022-08-19 12:04:11 +00:00
|
|
|
return;
|
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case '<':
|
|
|
|
this->state = st_lt;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
this->state = st_gt;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case (')'):
|
|
|
|
this->type = tt_bad;
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad )");
|
|
|
|
this->error_message = "unexpected )";
|
|
|
|
this->state = st_token_ready;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
this->type = tt_array_open;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ']':
|
|
|
|
this->type = tt_array_close;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
this->type = tt_brace_open;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '}':
|
|
|
|
this->type = tt_brace_close;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
return;
|
|
|
|
|
2022-08-22 23:14:43 +00:00
|
|
|
case '/':
|
|
|
|
this->state = st_name;
|
|
|
|
this->val += ch;
|
|
|
|
return;
|
|
|
|
|
2022-08-23 01:00:55 +00:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
this->state = st_number;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
this->state = st_sign;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
this->state = st_decimal;
|
|
|
|
return;
|
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
default:
|
|
|
|
this->state = st_literal;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inSpace(char ch)
|
|
|
|
{
|
|
|
|
// We only enter this state if include_ignorable is true.
|
|
|
|
if (!isSpace(ch)) {
|
|
|
|
this->type = tt_space;
|
2022-08-24 17:53:34 +00:00
|
|
|
this->in_token = false;
|
2022-08-21 19:08:58 +00:00
|
|
|
this->char_to_unread = ch;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inComment(char ch)
|
|
|
|
{
|
|
|
|
if ((ch == '\r') || (ch == '\n')) {
|
|
|
|
if (this->include_ignorable) {
|
|
|
|
this->type = tt_comment;
|
2022-08-24 17:53:34 +00:00
|
|
|
this->in_token = false;
|
2022-08-18 12:13:19 +00:00
|
|
|
this->char_to_unread = ch;
|
|
|
|
this->state = st_token_ready;
|
2022-02-08 14:18:08 +00:00
|
|
|
} else {
|
2022-08-24 16:29:50 +00:00
|
|
|
this->state = st_before_token;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2022-08-21 19:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inString(char ch)
|
|
|
|
{
|
|
|
|
switch (ch) {
|
|
|
|
case '\\':
|
|
|
|
this->state = st_string_escape;
|
2022-08-18 16:54:51 +00:00
|
|
|
return;
|
2022-08-18 15:53:16 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case '(':
|
2022-08-18 12:13:19 +00:00
|
|
|
this->val += ch;
|
2022-08-21 19:08:58 +00:00
|
|
|
++this->string_depth;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
if (--this->string_depth == 0) {
|
|
|
|
this->type = tt_string;
|
2022-08-18 12:13:19 +00:00
|
|
|
this->state = st_token_ready;
|
2022-08-21 19:08:58 +00:00
|
|
|
return;
|
2019-01-30 19:20:56 +00:00
|
|
|
}
|
2022-08-21 19:08:58 +00:00
|
|
|
|
|
|
|
this->val += ch;
|
2022-08-18 16:54:51 +00:00
|
|
|
return;
|
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case '\r':
|
|
|
|
// CR by itself is converted to LF
|
|
|
|
this->val += '\n';
|
|
|
|
this->state = st_string_after_cr;
|
2022-08-18 17:43:50 +00:00
|
|
|
return;
|
2022-08-18 15:53:16 +00:00
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
case '\n':
|
|
|
|
this->val += ch;
|
2022-08-20 12:44:27 +00:00
|
|
|
return;
|
|
|
|
|
2022-08-18 15:53:16 +00:00
|
|
|
default:
|
2022-08-21 19:08:58 +00:00
|
|
|
this->val += ch;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 23:14:43 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::inName(char ch)
|
|
|
|
{
|
|
|
|
if (isDelimiter(ch)) {
|
|
|
|
// A C-locale whitespace character or delimiter terminates token. It is important to unread
|
|
|
|
// the whitespace character even though it is ignored since it may be the newline after a
|
|
|
|
// stream keyword. Removing it here could make the stream-reading code break on some files,
|
|
|
|
// though not on any files in the test suite as of this
|
|
|
|
// writing.
|
|
|
|
|
|
|
|
this->type = this->bad ? tt_bad : tt_name;
|
2022-08-24 17:53:34 +00:00
|
|
|
this->in_token = false;
|
2022-08-22 23:14:43 +00:00
|
|
|
this->char_to_unread = ch;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
} else if (ch == '#') {
|
|
|
|
this->char_code = 0;
|
|
|
|
this->state = st_name_hex1;
|
|
|
|
} else {
|
|
|
|
this->val += ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inNameHex1(char ch)
|
|
|
|
{
|
|
|
|
this->hex_char = ch;
|
|
|
|
|
2023-03-22 12:10:24 +00:00
|
|
|
if (char hval = QUtil::hex_decode_char(ch); hval < '\20') {
|
|
|
|
this->char_code = int(hval) << 4;
|
2022-08-22 23:14:43 +00:00
|
|
|
this->state = st_name_hex2;
|
|
|
|
} else {
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad name 1");
|
|
|
|
this->error_message = "name with stray # will not work with PDF >= 1.2";
|
|
|
|
// Use null to encode a bad # -- this is reversed in QPDF_Name::normalizeName.
|
|
|
|
this->val += '\0';
|
|
|
|
this->state = st_name;
|
|
|
|
inName(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inNameHex2(char ch)
|
|
|
|
{
|
2023-03-22 12:10:24 +00:00
|
|
|
if (char hval = QUtil::hex_decode_char(ch); hval < '\20') {
|
|
|
|
this->char_code |= int(hval);
|
2022-08-22 23:14:43 +00:00
|
|
|
} else {
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad name 2");
|
|
|
|
this->error_message = "name with stray # will not work with PDF >= 1.2";
|
|
|
|
// Use null to encode a bad # -- this is reversed in QPDF_Name::normalizeName.
|
|
|
|
this->val += '\0';
|
|
|
|
this->val += this->hex_char;
|
|
|
|
this->state = st_name;
|
|
|
|
inName(ch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this->char_code == 0) {
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer null in name");
|
|
|
|
this->error_message = "null character not allowed in name token";
|
|
|
|
this->val += "#00";
|
|
|
|
this->state = st_name;
|
|
|
|
this->bad = true;
|
|
|
|
} else {
|
|
|
|
this->val += char(this->char_code);
|
|
|
|
this->state = st_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 01:00:55 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::inSign(char ch)
|
|
|
|
{
|
|
|
|
if (QUtil::is_digit(ch)) {
|
|
|
|
this->state = st_number;
|
|
|
|
} else if (ch == '.') {
|
|
|
|
this->state = st_decimal;
|
|
|
|
} else {
|
|
|
|
this->state = st_literal;
|
|
|
|
inLiteral(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inDecimal(char ch)
|
|
|
|
{
|
|
|
|
if (QUtil::is_digit(ch)) {
|
|
|
|
this->state = st_real;
|
|
|
|
} else {
|
|
|
|
this->state = st_literal;
|
|
|
|
inLiteral(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inNumber(char ch)
|
|
|
|
{
|
|
|
|
if (QUtil::is_digit(ch)) {
|
|
|
|
} else if (ch == '.') {
|
|
|
|
this->state = st_real;
|
|
|
|
} else if (isDelimiter(ch)) {
|
|
|
|
this->type = tt_integer;
|
|
|
|
this->state = st_token_ready;
|
2022-08-24 17:53:34 +00:00
|
|
|
this->in_token = false;
|
2022-08-23 01:00:55 +00:00
|
|
|
this->char_to_unread = ch;
|
|
|
|
} else {
|
|
|
|
this->state = st_literal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inReal(char ch)
|
|
|
|
{
|
|
|
|
if (QUtil::is_digit(ch)) {
|
|
|
|
} else if (isDelimiter(ch)) {
|
|
|
|
this->type = tt_real;
|
|
|
|
this->state = st_token_ready;
|
2022-08-24 17:53:34 +00:00
|
|
|
this->in_token = false;
|
2022-08-23 01:00:55 +00:00
|
|
|
this->char_to_unread = ch;
|
|
|
|
} else {
|
|
|
|
this->state = st_literal;
|
|
|
|
}
|
|
|
|
}
|
2022-08-21 19:08:58 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::inStringEscape(char ch)
|
|
|
|
{
|
|
|
|
this->state = st_in_string;
|
|
|
|
switch (ch) {
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
this->state = st_char_code;
|
|
|
|
this->char_code = 0;
|
|
|
|
this->digit_count = 0;
|
|
|
|
inCharCode(ch);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
this->val += '\n';
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
this->val += '\r';
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
this->val += '\t';
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
this->val += '\b';
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
this->val += '\f';
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
this->state = st_string_after_cr;
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// PDF spec says backslash is ignored before anything else
|
|
|
|
this->val += ch;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inStringAfterCR(char ch)
|
|
|
|
{
|
|
|
|
this->state = st_in_string;
|
|
|
|
if (ch != '\n') {
|
|
|
|
inString(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inLt(char ch)
|
|
|
|
{
|
|
|
|
if (ch == '<') {
|
|
|
|
this->type = tt_dict_open;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->state = st_in_hexstring;
|
|
|
|
inHexstring(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inGt(char ch)
|
|
|
|
{
|
|
|
|
if (ch == '>') {
|
|
|
|
this->type = tt_dict_close;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
} else {
|
|
|
|
this->type = tt_bad;
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad >");
|
|
|
|
this->error_message = "unexpected >";
|
2022-08-24 17:53:34 +00:00
|
|
|
this->in_token = false;
|
2022-08-21 19:08:58 +00:00
|
|
|
this->char_to_unread = ch;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::inLiteral(char ch)
|
|
|
|
{
|
|
|
|
if (isDelimiter(ch)) {
|
|
|
|
// A C-locale whitespace character or delimiter terminates token. It is important to unread
|
|
|
|
// the whitespace character even though it is ignored since it may be the newline after a
|
|
|
|
// stream keyword. Removing it here could make the stream-reading code break on some files,
|
|
|
|
// though not on any files in the test suite as of this writing.
|
|
|
|
|
2022-08-24 17:53:34 +00:00
|
|
|
this->in_token = false;
|
2022-08-21 19:08:58 +00:00
|
|
|
this->char_to_unread = ch;
|
|
|
|
this->state = st_token_ready;
|
2022-10-04 21:37:23 +00:00
|
|
|
this->type = (this->raw_val == "true") || (this->raw_val == "false")
|
2022-08-23 11:49:01 +00:00
|
|
|
? tt_bool
|
2022-10-04 21:37:23 +00:00
|
|
|
: (this->raw_val == "null" ? tt_null : tt_word);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-08-18 17:43:50 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-08-18 17:43:50 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::inHexstring(char ch)
|
|
|
|
{
|
2023-03-22 12:10:24 +00:00
|
|
|
if (char hval = QUtil::hex_decode_char(ch); hval < '\20') {
|
|
|
|
this->char_code = int(hval) << 4;
|
2022-08-20 12:44:27 +00:00
|
|
|
this->state = st_in_hexstring_2nd;
|
|
|
|
|
|
|
|
} else if (ch == '>') {
|
2022-08-18 16:54:51 +00:00
|
|
|
this->type = tt_string;
|
|
|
|
this->state = st_token_ready;
|
2022-08-20 12:44:27 +00:00
|
|
|
|
2022-08-18 16:54:51 +00:00
|
|
|
} else if (isSpace(ch)) {
|
|
|
|
// ignore
|
2022-08-20 12:44:27 +00:00
|
|
|
|
2022-08-18 16:54:51 +00:00
|
|
|
} else {
|
|
|
|
this->type = tt_bad;
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad hexstring character");
|
|
|
|
this->error_message = std::string("invalid character (") + ch + ") in hexstring";
|
|
|
|
this->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 12:44:27 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::inHexstring2nd(char ch)
|
|
|
|
{
|
2023-03-22 12:10:24 +00:00
|
|
|
if (char hval = QUtil::hex_decode_char(ch); hval < '\20') {
|
|
|
|
this->val += char(this->char_code) | hval;
|
2022-08-20 12:44:27 +00:00
|
|
|
this->state = st_in_hexstring;
|
|
|
|
|
|
|
|
} else if (ch == '>') {
|
|
|
|
// PDF spec says odd hexstrings have implicit trailing 0.
|
|
|
|
this->val += char(this->char_code);
|
|
|
|
this->type = tt_string;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
|
|
|
|
} else if (isSpace(ch)) {
|
|
|
|
// ignore
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this->type = tt_bad;
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad hexstring 2nd character");
|
|
|
|
this->error_message = std::string("invalid character (") + ch + ") in hexstring";
|
|
|
|
this->state = st_token_ready;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:04:11 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::inCharCode(char ch)
|
|
|
|
{
|
2023-10-14 21:04:58 +00:00
|
|
|
bool handled = false;
|
2022-08-20 14:44:44 +00:00
|
|
|
if (('0' <= ch) && (ch <= '7')) {
|
|
|
|
this->char_code = 8 * this->char_code + (int(ch) - int('0'));
|
|
|
|
if (++(this->digit_count) < 3) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-14 21:04:58 +00:00
|
|
|
handled = true;
|
2022-08-19 12:04:11 +00:00
|
|
|
}
|
2023-10-14 21:04:58 +00:00
|
|
|
// We've accumulated \ddd or we have \d or \dd followed by other than an octal digit. The PDF
|
|
|
|
// Spec says to ignore high-order overflow.
|
2022-08-20 14:44:44 +00:00
|
|
|
this->val += char(this->char_code % 256);
|
|
|
|
this->state = st_in_string;
|
2023-10-14 21:04:58 +00:00
|
|
|
if (!handled) {
|
|
|
|
inString(ch);
|
|
|
|
}
|
2022-08-19 12:04:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 19:08:58 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::inInlineImage(char ch)
|
|
|
|
{
|
2022-10-04 21:37:23 +00:00
|
|
|
if ((this->raw_val.length() + 1) == this->inline_image_bytes) {
|
2022-08-21 19:08:58 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer found EI by byte count");
|
|
|
|
this->type = tt_inline_image;
|
|
|
|
this->inline_image_bytes = 0;
|
|
|
|
this->state = st_token_ready;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::presentEOF()
|
|
|
|
{
|
2022-08-23 13:47:10 +00:00
|
|
|
switch (this->state) {
|
|
|
|
case st_name:
|
|
|
|
case st_name_hex1:
|
|
|
|
case st_name_hex2:
|
|
|
|
case st_number:
|
|
|
|
case st_real:
|
|
|
|
case st_sign:
|
|
|
|
case st_decimal:
|
|
|
|
case st_literal:
|
2022-08-23 11:49:01 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer EOF reading appendable token");
|
2022-08-22 23:14:43 +00:00
|
|
|
// Push any delimiter to the state machine to finish off the final token.
|
|
|
|
presentCharacter('\f');
|
2022-08-24 17:53:34 +00:00
|
|
|
this->in_token = true;
|
2022-08-23 13:47:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case st_top:
|
2022-08-24 16:29:50 +00:00
|
|
|
case st_before_token:
|
2022-08-18 12:13:19 +00:00
|
|
|
this->type = tt_eof;
|
2022-08-23 13:47:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case st_in_space:
|
|
|
|
this->type = this->include_ignorable ? tt_space : tt_eof;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case st_in_comment:
|
|
|
|
this->type = this->include_ignorable ? tt_comment : tt_bad;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case st_token_ready:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-01-30 01:57:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer EOF reading token");
|
2022-08-18 12:13:19 +00:00
|
|
|
this->type = tt_bad;
|
|
|
|
this->error_message = "EOF while reading token";
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-08-18 12:13:19 +00:00
|
|
|
this->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:20:56 +00:00
|
|
|
void
|
2022-04-09 18:35:56 +00:00
|
|
|
QPDFTokenizer::expectInlineImage(std::shared_ptr<InputSource> input)
|
2024-07-25 15:42:29 +00:00
|
|
|
{
|
|
|
|
expectInlineImage(*input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::expectInlineImage(InputSource& input)
|
2019-01-30 19:20:56 +00:00
|
|
|
{
|
2022-10-04 19:50:36 +00:00
|
|
|
if (this->state == st_token_ready) {
|
|
|
|
reset();
|
|
|
|
} else if (this->state != st_before_token) {
|
2024-07-25 15:42:29 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"QPDFTokenizer::expectInlineImage called when tokenizer is in improper state");
|
2019-01-30 19:26:08 +00:00
|
|
|
}
|
|
|
|
findEI(input);
|
2022-08-24 16:29:50 +00:00
|
|
|
this->before_token = false;
|
|
|
|
this->in_token = true;
|
2022-08-18 12:13:19 +00:00
|
|
|
this->state = st_inline_image;
|
2019-01-30 19:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-07-25 15:42:29 +00:00
|
|
|
QPDFTokenizer::findEI(InputSource& input)
|
2019-01-30 19:26:08 +00:00
|
|
|
{
|
2024-07-25 15:42:29 +00:00
|
|
|
qpdf_offset_t last_offset = input.getLastOffset();
|
|
|
|
qpdf_offset_t pos = input.tell();
|
2019-01-30 19:20:56 +00:00
|
|
|
|
2019-01-30 19:26:08 +00:00
|
|
|
// Use QPDFWordTokenFinder to find EI surrounded by delimiters. Then read the next several
|
|
|
|
// tokens or up to EOF. If we find any suspicious-looking or tokens, this is probably still part
|
|
|
|
// of the image data, so keep looking for EI. Stop at the first EI that passes. If we get to the
|
|
|
|
// end without finding one, return the last EI we found. Store the number of bytes expected in
|
|
|
|
// the inline image including the EI and use that to break out of inline image, falling back to
|
|
|
|
// the old method if needed.
|
|
|
|
|
|
|
|
bool okay = false;
|
|
|
|
bool first_try = true;
|
|
|
|
while (!okay) {
|
2019-01-30 19:20:56 +00:00
|
|
|
QPDFWordTokenFinder f(input, "EI");
|
2024-07-25 15:42:29 +00:00
|
|
|
if (!input.findFirst("EI", input.tell(), 0, f)) {
|
2019-01-30 19:26:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-07-25 15:42:29 +00:00
|
|
|
inline_image_bytes = QIntC::to_size(input.tell() - pos - 2);
|
2019-01-30 19:26:08 +00:00
|
|
|
|
|
|
|
QPDFTokenizer check;
|
|
|
|
bool found_bad = false;
|
|
|
|
// Look at the next 10 tokens or up to EOF. The next inline image's image data would look
|
|
|
|
// like bad tokens, but there will always be at least 10 tokens between one inline image's
|
|
|
|
// EI and the next valid one's ID since width, height, bits per pixel, and color space are
|
|
|
|
// all required as well as a BI and ID. If we get 10 good tokens in a row or hit EOF, we can
|
|
|
|
// be pretty sure we've found the actual EI.
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
QPDFTokenizer::Token t = check.readToken(input, "checker", true);
|
|
|
|
token_type_e type = t.getType();
|
|
|
|
if (type == tt_eof) {
|
|
|
|
okay = true;
|
|
|
|
} else if (type == tt_bad) {
|
|
|
|
found_bad = true;
|
2022-09-29 13:33:11 +00:00
|
|
|
} else if (t.isWord()) {
|
2019-01-30 19:26:08 +00:00
|
|
|
// The qpdf tokenizer lumps alphabetic and otherwise uncategorized characters into
|
|
|
|
// "words". We recognize strings of alphabetic characters as potential valid
|
|
|
|
// operators for purposes of telling whether we're in valid content or not. It's not
|
|
|
|
// perfect, but it should work more reliably than what we used to do, which was
|
|
|
|
// already good enough for the vast majority of files.
|
|
|
|
bool found_alpha = false;
|
|
|
|
bool found_non_printable = false;
|
|
|
|
bool found_other = false;
|
2022-05-21 14:18:15 +00:00
|
|
|
for (char ch: t.getValue()) {
|
2019-01-30 19:26:08 +00:00
|
|
|
if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||
|
|
|
|
(ch == '*')) {
|
|
|
|
// Treat '*' as alpha since there are valid PDF operators that contain *
|
|
|
|
// along with alphabetic characters.
|
|
|
|
found_alpha = true;
|
2022-01-10 16:52:07 +00:00
|
|
|
} else if ((static_cast<signed char>(ch) < 32) && (!isSpace(ch))) {
|
|
|
|
// Compare ch as a signed char so characters outside of 7-bit will be < 0.
|
2019-01-30 19:26:08 +00:00
|
|
|
found_non_printable = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
found_other = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found_non_printable || (found_alpha && found_other)) {
|
|
|
|
found_bad = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (okay || found_bad) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found_bad) {
|
|
|
|
okay = true;
|
|
|
|
}
|
|
|
|
if (!okay) {
|
|
|
|
first_try = false;
|
2019-01-30 19:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-30 19:26:08 +00:00
|
|
|
if (okay && (!first_try)) {
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer found EI after more than one try");
|
2018-01-30 01:57:04 +00:00
|
|
|
}
|
2019-01-30 19:26:08 +00:00
|
|
|
|
2024-07-25 15:42:29 +00:00
|
|
|
input.seek(pos, SEEK_SET);
|
|
|
|
input.setLastOffset(last_offset);
|
2018-01-30 01:57:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
bool
|
|
|
|
QPDFTokenizer::getToken(Token& token, bool& unread_char, char& ch)
|
|
|
|
{
|
2022-08-18 12:13:19 +00:00
|
|
|
bool ready = (this->state == st_token_ready);
|
2022-08-24 17:53:34 +00:00
|
|
|
unread_char = !this->in_token && !this->before_token;
|
2022-08-18 12:13:19 +00:00
|
|
|
ch = this->char_to_unread;
|
2008-04-29 12:55:25 +00:00
|
|
|
if (ready) {
|
2022-10-04 21:37:23 +00:00
|
|
|
token = (!(this->type == tt_name || this->type == tt_string))
|
2022-08-23 18:37:31 +00:00
|
|
|
? Token(this->type, this->raw_val, this->raw_val, this->error_message)
|
|
|
|
: Token(this->type, this->val, this->raw_val, this->error_message);
|
|
|
|
|
2022-08-18 12:13:19 +00:00
|
|
|
this->reset();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFTokenizer::betweenTokens()
|
|
|
|
{
|
2022-08-24 16:29:50 +00:00
|
|
|
return this->before_token;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-07-21 09:48:13 +00:00
|
|
|
|
|
|
|
QPDFTokenizer::Token
|
|
|
|
QPDFTokenizer::readToken(
|
2022-04-09 18:35:56 +00:00
|
|
|
InputSource& input, std::string const& context, bool allow_bad, size_t max_len)
|
2012-07-21 09:48:13 +00:00
|
|
|
{
|
2022-10-04 19:50:36 +00:00
|
|
|
nextToken(input, context, max_len);
|
|
|
|
|
|
|
|
Token token;
|
|
|
|
bool unread_char;
|
|
|
|
char char_to_unread;
|
|
|
|
getToken(token, unread_char, char_to_unread);
|
|
|
|
|
|
|
|
if (token.getType() == tt_bad) {
|
|
|
|
if (allow_bad) {
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer allowing bad token");
|
|
|
|
} else {
|
|
|
|
throw QPDFExc(
|
|
|
|
qpdf_e_damaged_pdf,
|
|
|
|
input.getName(),
|
2024-07-27 17:55:43 +00:00
|
|
|
context.empty() ? "offset " + std::to_string(input.getLastOffset()) : context,
|
2022-10-04 19:50:36 +00:00
|
|
|
input.getLastOffset(),
|
|
|
|
token.getErrorMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2012-07-21 09:48:13 +00:00
|
|
|
QPDFTokenizer::Token
|
|
|
|
QPDFTokenizer::readToken(
|
2022-10-04 19:50:36 +00:00
|
|
|
std::shared_ptr<InputSource> input, std::string const& context, bool allow_bad, size_t max_len)
|
2023-05-24 15:28:17 +00:00
|
|
|
{
|
2022-10-04 19:50:36 +00:00
|
|
|
return readToken(*input, context, allow_bad, max_len);
|
2023-05-24 15:28:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 19:50:36 +00:00
|
|
|
bool
|
|
|
|
QPDFTokenizer::nextToken(InputSource& input, std::string const& context, size_t max_len)
|
|
|
|
{
|
|
|
|
if (this->state != st_inline_image) {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
qpdf_offset_t offset = input.fastTell();
|
2022-08-23 23:36:24 +00:00
|
|
|
|
|
|
|
while (this->state != st_token_ready) {
|
2022-02-08 14:18:08 +00:00
|
|
|
char ch;
|
2022-10-04 19:50:36 +00:00
|
|
|
if (!input.fastRead(ch)) {
|
2022-08-23 23:36:24 +00:00
|
|
|
presentEOF();
|
|
|
|
|
|
|
|
if ((this->type == tt_eof) && (!this->allow_eof)) {
|
|
|
|
// Nothing in the qpdf library calls readToken without allowEOF anymore, so this
|
|
|
|
// case is not exercised.
|
|
|
|
this->type = tt_bad;
|
|
|
|
this->error_message = "unexpected EOF";
|
2022-10-04 19:50:36 +00:00
|
|
|
offset = input.getLastOffset();
|
2022-08-23 23:36:24 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
} else {
|
2022-08-24 18:29:39 +00:00
|
|
|
handleCharacter(ch);
|
|
|
|
if (this->before_token) {
|
2022-02-08 14:18:08 +00:00
|
|
|
++offset;
|
|
|
|
}
|
2022-08-24 18:29:39 +00:00
|
|
|
if (this->in_token) {
|
|
|
|
this->raw_val += ch;
|
|
|
|
}
|
2022-08-18 12:13:19 +00:00
|
|
|
if (max_len && (this->raw_val.length() >= max_len) && (this->state != st_token_ready)) {
|
2017-08-22 14:24:19 +00:00
|
|
|
// terminate this token now
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer block long token");
|
2022-08-18 12:13:19 +00:00
|
|
|
this->type = tt_bad;
|
|
|
|
this->state = st_token_ready;
|
2018-01-28 23:28:45 +00:00
|
|
|
this->error_message = "exceeded allowable length while reading token";
|
2017-08-22 14:24:19 +00:00
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2012-07-21 09:48:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 19:50:36 +00:00
|
|
|
input.fastUnread(!this->in_token && !this->before_token);
|
2017-08-09 01:44:37 +00:00
|
|
|
|
2022-10-04 19:50:36 +00:00
|
|
|
if (this->type != tt_eof) {
|
|
|
|
input.setLastOffset(offset);
|
2012-07-21 09:48:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 19:50:36 +00:00
|
|
|
return this->error_message.empty();
|
2012-07-21 09:48:13 +00:00
|
|
|
}
|