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/QTC.hh>
|
2012-07-21 09:48:13 +00:00
|
|
|
#include <qpdf/QPDFExc.hh>
|
2017-07-22 23:23:52 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2018-02-02 23:21:34 +00:00
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-05-04 16:02:53 +00:00
|
|
|
#include <string.h>
|
2017-07-30 07:15:50 +00:00
|
|
|
#include <cstdlib>
|
2008-05-04 16:02:53 +00:00
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
QPDFTokenizer::Members::Members() :
|
2013-01-20 20:26:45 +00:00
|
|
|
pound_special_in_name(true),
|
2018-01-28 23:28:45 +00:00
|
|
|
allow_eof(false),
|
|
|
|
include_ignorable(false)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::Members::reset()
|
|
|
|
{
|
|
|
|
state = st_top;
|
|
|
|
type = tt_bad;
|
|
|
|
val = "";
|
|
|
|
raw_val = "";
|
|
|
|
error_message = "";
|
|
|
|
unread_char = false;
|
|
|
|
char_to_unread = '\0';
|
|
|
|
string_depth = 0;
|
|
|
|
string_ignoring_newline = false;
|
|
|
|
last_char_was_bs = false;
|
2018-08-05 22:59:41 +00:00
|
|
|
last_char_was_cr = false;
|
2018-01-30 01:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFTokenizer::Members::~Members()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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() :
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::allowPoundAnywhereInName()
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer allow pound anywhere in name");
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->pound_special_in_name = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 20:26:45 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::allowEOF()
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->allow_eof = true;
|
2013-01-20 20:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 23:28:45 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::includeIgnorable()
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->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)
|
|
|
|
{
|
|
|
|
return (strchr(" \t\n\v\f\r()<>[]{}/%", ch) != 0);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2012-08-11 13:22:59 +00:00
|
|
|
QPDFTokenizer::resolveLiteral()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
if ((this->m->val.length() > 0) && (this->m->val.at(0) == '/'))
|
2012-08-11 13:22:59 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_name;
|
2012-08-11 13:22:59 +00:00
|
|
|
// Deal with # in name token. Note: '/' by itself is a
|
|
|
|
// valid name, so don't strip leading /. That way we
|
|
|
|
// don't have to deal with the empty string as a name.
|
|
|
|
std::string nval = "/";
|
2018-01-30 01:00:06 +00:00
|
|
|
char const* valstr = this->m->val.c_str() + 1;
|
2012-08-11 13:22:59 +00:00
|
|
|
for (char const* p = valstr; *p; ++p)
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
if ((*p == '#') && this->m->pound_special_in_name)
|
2012-08-11 13:22:59 +00:00
|
|
|
{
|
|
|
|
if (p[1] && p[2] &&
|
2017-07-22 23:23:52 +00:00
|
|
|
QUtil::is_hex_digit(p[1]) && QUtil::is_hex_digit(p[2]))
|
2012-08-11 13:22:59 +00:00
|
|
|
{
|
|
|
|
char num[3];
|
|
|
|
num[0] = p[1];
|
|
|
|
num[1] = p[2];
|
|
|
|
num[2] = '\0';
|
2013-02-24 02:46:21 +00:00
|
|
|
char ch = static_cast<char>(strtol(num, 0, 16));
|
2012-08-11 13:22:59 +00:00
|
|
|
if (ch == '\0')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_bad;
|
2018-01-30 01:57:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer null in name");
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->error_message =
|
2012-08-11 13:22:59 +00:00
|
|
|
"null character not allowed in name token";
|
|
|
|
nval += "#00";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nval += ch;
|
|
|
|
}
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:57:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad name");
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_bad;
|
|
|
|
this->m->error_message = "invalid name token";
|
2012-08-11 13:22:59 +00:00
|
|
|
nval += *p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nval += *p;
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val = nval;
|
2012-08-11 13:22:59 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (QUtil::is_number(this->m->val.c_str()))
|
2012-08-11 13:22:59 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
if (this->m->val.find('.') != std::string::npos)
|
2012-08-11 13:22:59 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_real;
|
2012-08-11 13:22:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_integer;
|
2012-08-11 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if ((this->m->val == "true") || (this->m->val == "false"))
|
2012-08-11 13:22:59 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_bool;
|
2012-08-11 13:22:59 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->val == "null")
|
2012-08-11 13:22:59 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_null;
|
2012-08-11 13:22:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// I don't really know what it is, so leave it as tt_word.
|
|
|
|
// Lots of cases ($, #, etc.) other than actual words fall
|
|
|
|
// into this category, but that's okay at least for now.
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_word;
|
2012-08-11 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::presentCharacter(char ch)
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
if (this->m->state == st_token_ready)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: QPDF tokenizer presented character "
|
|
|
|
"while token is waiting");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char orig_ch = ch;
|
|
|
|
|
|
|
|
// State machine is implemented such that some characters may be
|
|
|
|
// handled more than once. This happens whenever you have to use
|
|
|
|
// the character that caused a state change in the new state.
|
|
|
|
|
|
|
|
bool handled = true;
|
2018-01-30 01:00:06 +00:00
|
|
|
if (this->m->state == st_top)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// Note: we specifically do not use ctype here. It is
|
|
|
|
// locale-dependent.
|
2018-01-28 23:28:45 +00:00
|
|
|
if (isSpace(ch))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
if (this->m->include_ignorable)
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_in_space;
|
|
|
|
this->m->val += ch;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (ch == '%')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_in_comment;
|
|
|
|
if (this->m->include_ignorable)
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (ch == '(')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->string_depth = 1;
|
|
|
|
this->m->string_ignoring_newline = false;
|
|
|
|
memset(this->m->bs_num_register, '\0',
|
|
|
|
sizeof(this->m->bs_num_register));
|
|
|
|
this->m->last_char_was_bs = false;
|
2018-08-05 22:59:41 +00:00
|
|
|
this->m->last_char_was_cr = false;
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_in_string;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (ch == '<')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_lt;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (ch == '>')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_gt;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
2008-04-29 12:55:25 +00:00
|
|
|
if (ch == ')')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_bad;
|
2018-01-30 01:57:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad )");
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->error_message = "unexpected )";
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (ch == '[')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_array_open;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (ch == ']')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_array_close;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (ch == '{')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_brace_open;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (ch == '}')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_brace_close;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_literal;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->state == st_in_space)
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
|
|
|
// We only enter this state if include_ignorable is true.
|
|
|
|
if (! isSpace(ch))
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_space;
|
|
|
|
this->m->unread_char = true;
|
|
|
|
this->m->char_to_unread = ch;
|
|
|
|
this->m->state = st_token_ready;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->state == st_in_comment)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
if ((ch == '\r') || (ch == '\n'))
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
if (this->m->include_ignorable)
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_comment;
|
|
|
|
this->m->unread_char = true;
|
|
|
|
this->m->char_to_unread = ch;
|
|
|
|
this->m->state = st_token_ready;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_top;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->include_ignorable)
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->state == st_lt)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
if (ch == '<')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val = "<<";
|
|
|
|
this->m->type = tt_dict_open;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handled = false;
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_in_hexstring;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->state == st_gt)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
if (ch == '>')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val = ">>";
|
|
|
|
this->m->type = tt_dict_close;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val = ">";
|
|
|
|
this->m->type = tt_bad;
|
2018-01-30 01:57:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad >");
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->error_message = "unexpected >";
|
|
|
|
this->m->unread_char = true;
|
|
|
|
this->m->char_to_unread = ch;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->state == st_in_string)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-08-05 22:59:41 +00:00
|
|
|
if (this->m->string_ignoring_newline && (ch != '\n'))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->string_ignoring_newline = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
size_t bs_num_count = strlen(this->m->bs_num_register);
|
2008-04-29 12:55:25 +00:00
|
|
|
bool ch_is_octal = ((ch >= '0') && (ch <= '7'));
|
|
|
|
if ((bs_num_count == 3) || ((bs_num_count > 0) && (! ch_is_octal)))
|
|
|
|
{
|
|
|
|
// We've accumulated \ddd. PDF Spec says to ignore
|
|
|
|
// high-order overflow.
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += static_cast<char>(
|
|
|
|
strtol(this->m->bs_num_register, 0, 8));
|
|
|
|
memset(this->m->bs_num_register, '\0',
|
|
|
|
sizeof(this->m->bs_num_register));
|
2008-04-29 12:55:25 +00:00
|
|
|
bs_num_count = 0;
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:59:41 +00:00
|
|
|
if (this->m->string_ignoring_newline && (ch == '\n'))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// ignore
|
2018-08-05 22:59:41 +00:00
|
|
|
this->m->string_ignoring_newline = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (ch_is_octal &&
|
|
|
|
(this->m->last_char_was_bs || (bs_num_count > 0)))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->bs_num_register[bs_num_count++] = ch;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->last_char_was_bs)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 'n':
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += '\n';
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += '\r';
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += '\t';
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += '\b';
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += '\f';
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\n':
|
2018-08-05 22:59:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->string_ignoring_newline = true;
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// PDF spec says backslash is ignored before anything else
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
2008-04-29 12:55:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == '\\')
|
|
|
|
{
|
|
|
|
// last_char_was_bs is set/cleared below as appropriate
|
|
|
|
if (bs_num_count)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: QPDFTokenizer: bs_num_count != 0 "
|
|
|
|
"when ch == '\\'");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == '(')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
|
|
|
++this->m->string_depth;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if ((ch == ')') && (--this->m->string_depth == 0))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_string;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-08-05 22:59:41 +00:00
|
|
|
else if (ch == '\r')
|
|
|
|
{
|
|
|
|
// CR by itself is converted to LF
|
|
|
|
this->m->val += '\n';
|
|
|
|
}
|
|
|
|
else if (ch == '\n')
|
|
|
|
{
|
|
|
|
// CR LF is converted to LF
|
|
|
|
if (! this->m->last_char_was_cr)
|
|
|
|
{
|
|
|
|
this->m->val += ch;
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 22:59:41 +00:00
|
|
|
this->m->last_char_was_cr =
|
|
|
|
((! this->m->string_ignoring_newline) && (ch == '\r'));
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->last_char_was_bs =
|
|
|
|
((! this->m->last_char_was_bs) && (ch == '\\'));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->state == st_literal)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:57:04 +00:00
|
|
|
if (isDelimiter(ch))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-02-21 02:54:31 +00:00
|
|
|
// A C-locale whitespace character or delimiter terminates
|
2008-04-29 12:55:25 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_word;
|
|
|
|
this->m->unread_char = true;
|
|
|
|
this->m->char_to_unread = ch;
|
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:57:04 +00:00
|
|
|
else if (this->m->state == st_inline_image)
|
|
|
|
{
|
|
|
|
size_t len = this->m->val.length();
|
|
|
|
if ((len >= 4) &&
|
|
|
|
isDelimiter(this->m->val.at(len-4)) &&
|
|
|
|
(this->m->val.at(len-3) == 'E') &&
|
|
|
|
(this->m->val.at(len-2) == 'I') &&
|
|
|
|
isDelimiter(this->m->val.at(len-1)))
|
|
|
|
{
|
|
|
|
this->m->type = tt_inline_image;
|
|
|
|
this->m->unread_char = true;
|
|
|
|
this->m->char_to_unread = ch;
|
|
|
|
this->m->state = st_token_ready;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->m->val += ch;
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
handled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (handled)
|
|
|
|
{
|
|
|
|
// okay
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->state == st_in_hexstring)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
if (ch == '>')
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_string;
|
|
|
|
this->m->state = st_token_ready;
|
|
|
|
if (this->m->val.length() % 2)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// PDF spec says odd hexstrings have implicit
|
|
|
|
// trailing 0.
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += '0';
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
char num[3];
|
|
|
|
num[2] = '\0';
|
|
|
|
std::string nval;
|
2018-01-30 01:00:06 +00:00
|
|
|
for (unsigned int i = 0; i < this->m->val.length(); i += 2)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
num[0] = this->m->val.at(i);
|
|
|
|
num[1] = this->m->val.at(i+1);
|
2013-02-24 02:46:21 +00:00
|
|
|
char nch = static_cast<char>(strtol(num, 0, 16));
|
2008-04-29 12:55:25 +00:00
|
|
|
nval += nch;
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val = nval;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-07-22 23:23:52 +00:00
|
|
|
else if (QUtil::is_hex_digit(ch))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val += ch;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-01-28 23:28:45 +00:00
|
|
|
else if (isSpace(ch))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_bad;
|
2018-01-30 01:57:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer bad hexstring character");
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->error_message = std::string("invalid character (") +
|
2008-04-29 12:55:25 +00:00
|
|
|
ch + ") in hexstring";
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: invalid state while reading token");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
if ((this->m->state == st_token_ready) && (this->m->type == tt_word))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-08-11 13:22:59 +00:00
|
|
|
resolveLiteral();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
if (! (betweenTokens() ||
|
|
|
|
((this->m->state == st_token_ready) && this->m->unread_char)))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->raw_val += orig_ch;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFTokenizer::presentEOF()
|
|
|
|
{
|
2018-01-30 01:57:04 +00:00
|
|
|
if (this->m->state == st_inline_image)
|
|
|
|
{
|
|
|
|
size_t len = this->m->val.length();
|
|
|
|
if ((len >= 3) &&
|
|
|
|
isDelimiter(this->m->val.at(len-3)) &&
|
|
|
|
(this->m->val.at(len-2) == 'E') &&
|
|
|
|
(this->m->val.at(len-1) == 'I'))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer inline image at EOF");
|
|
|
|
this->m->type = tt_inline_image;
|
|
|
|
this->m->state = st_token_ready;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
if (this->m->state == st_literal)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-01-30 01:57:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer EOF reading appendable token");
|
2012-08-11 13:27:30 +00:00
|
|
|
resolveLiteral();
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if ((this->m->include_ignorable) && (this->m->state == st_in_space))
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_space;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if ((this->m->include_ignorable) && (this->m->state == st_in_comment))
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_comment;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
|
|
|
else if (betweenTokens())
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_eof;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
else if (this->m->state != st_token_ready)
|
2012-08-11 13:27:30 +00:00
|
|
|
{
|
2018-01-30 01:57:04 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer EOF reading token");
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_bad;
|
|
|
|
this->m->error_message = "EOF while reading token";
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-08-11 13:27:30 +00:00
|
|
|
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->state = st_token_ready;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:57:04 +00:00
|
|
|
void
|
|
|
|
QPDFTokenizer::expectInlineImage()
|
|
|
|
{
|
|
|
|
if (this->m->state != st_top)
|
|
|
|
{
|
|
|
|
throw std::logic_error("QPDFTokenizer::expectInlineImage called"
|
|
|
|
" when tokenizer is in improper state");
|
|
|
|
}
|
|
|
|
this->m->state = st_inline_image;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
bool
|
|
|
|
QPDFTokenizer::getToken(Token& token, bool& unread_char, char& ch)
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
bool ready = (this->m->state == st_token_ready);
|
|
|
|
unread_char = this->m->unread_char;
|
|
|
|
ch = this->m->char_to_unread;
|
2008-04-29 12:55:25 +00:00
|
|
|
if (ready)
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
if (this->m->type == tt_bad)
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->val = this->m->raw_val;
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
token = Token(this->m->type, this->m->val,
|
|
|
|
this->m->raw_val, this->m->error_message);
|
|
|
|
this->m->reset();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFTokenizer::betweenTokens()
|
|
|
|
{
|
2018-01-30 01:00:06 +00:00
|
|
|
return ((this->m->state == st_top) ||
|
|
|
|
((! this->m->include_ignorable) &&
|
|
|
|
((this->m->state == st_in_comment) ||
|
|
|
|
(this->m->state == st_in_space))));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-07-21 09:48:13 +00:00
|
|
|
|
|
|
|
QPDFTokenizer::Token
|
|
|
|
QPDFTokenizer::readToken(PointerHolder<InputSource> input,
|
2017-08-09 01:44:37 +00:00
|
|
|
std::string const& context,
|
2017-08-22 14:24:19 +00:00
|
|
|
bool allow_bad,
|
|
|
|
size_t max_len)
|
2012-07-21 09:48:13 +00:00
|
|
|
{
|
|
|
|
qpdf_offset_t offset = input->tell();
|
|
|
|
Token token;
|
|
|
|
bool unread_char;
|
|
|
|
char char_to_unread;
|
2012-08-11 13:27:30 +00:00
|
|
|
bool presented_eof = false;
|
2012-07-21 09:48:13 +00:00
|
|
|
while (! getToken(token, unread_char, char_to_unread))
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
if (input->read(&ch, 1) == 0)
|
|
|
|
{
|
2012-08-11 13:27:30 +00:00
|
|
|
if (! presented_eof)
|
|
|
|
{
|
|
|
|
presentEOF();
|
|
|
|
presented_eof = true;
|
2018-01-30 01:00:06 +00:00
|
|
|
if ((this->m->type == tt_eof) && (! this->m->allow_eof))
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
2018-02-16 22:25:27 +00:00
|
|
|
// Nothing in the qpdf library calls readToken
|
|
|
|
// without allowEOF anymore, so this case is not
|
|
|
|
// exercised.
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_bad;
|
|
|
|
this->m->error_message = "unexpected EOF";
|
2018-01-28 23:28:45 +00:00
|
|
|
offset = input->getLastOffset();
|
|
|
|
}
|
2012-08-11 13:27:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"getToken returned false after presenting EOF");
|
|
|
|
}
|
2012-07-21 09:48:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-28 23:28:45 +00:00
|
|
|
presentCharacter(ch);
|
|
|
|
if (betweenTokens() && (input->getLastOffset() == offset))
|
2012-07-21 09:48:13 +00:00
|
|
|
{
|
|
|
|
++offset;
|
|
|
|
}
|
2018-01-30 01:00:06 +00:00
|
|
|
if (max_len && (this->m->raw_val.length() >= max_len) &&
|
|
|
|
(this->m->state != st_token_ready))
|
2017-08-22 14:24:19 +00:00
|
|
|
{
|
|
|
|
// terminate this token now
|
|
|
|
QTC::TC("qpdf", "QPDFTokenizer block long token");
|
2018-01-30 01:00:06 +00:00
|
|
|
this->m->type = tt_bad;
|
|
|
|
this->m->state = st_token_ready;
|
|
|
|
this->m->error_message =
|
2018-01-28 23:28:45 +00:00
|
|
|
"exceeded allowable length while reading token";
|
2017-08-22 14:24:19 +00:00
|
|
|
}
|
2012-07-21 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unread_char)
|
|
|
|
{
|
|
|
|
input->unreadCh(char_to_unread);
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:25:27 +00:00
|
|
|
if (token.getType() != tt_eof)
|
|
|
|
{
|
|
|
|
input->setLastOffset(offset);
|
|
|
|
}
|
2017-08-09 01:44:37 +00:00
|
|
|
|
2012-07-21 09:48:13 +00:00
|
|
|
if (token.getType() == tt_bad)
|
|
|
|
{
|
2017-08-09 01:44:37 +00:00
|
|
|
if (allow_bad)
|
|
|
|
{
|
2017-08-05 18:54:07 +00:00
|
|
|
QTC::TC("qpdf", "QPDFTokenizer allowing bad token");
|
2017-08-09 01:44:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, input->getName(),
|
|
|
|
context, offset, token.getErrorMessage());
|
|
|
|
}
|
2012-07-21 09:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|