| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <ctype.h> |
| 28 |
#include <string.h> |
| 29 |
/* pow() */ |
| 30 |
#include <math.h> |
| 31 |
|
| 32 |
#include "system.h" |
| 33 |
#include "mem.h" |
| 34 |
#include "error.h" |
| 35 |
|
| 36 |
#include "numbers.h" |
| 37 |
|
| 38 |
#include "mfileio.h" |
| 39 |
|
| 40 |
#include "pdfobj.h" |
| 41 |
#include "pdfdoc.h" |
| 42 |
#include "pdfdev.h" |
| 43 |
|
| 44 |
#include "pdfparse.h" |
| 45 |
|
| 46 |
/* PDF */ |
| 47 |
#ifdef is_space |
| 48 |
#undef is_space |
| 49 |
#endif |
| 50 |
#ifdef is_delim |
| 51 |
#undef is_delim |
| 52 |
#endif |
| 53 |
|
| 54 |
#define is_space(c) ((c) == ' ' || (c) == '\t' || (c) == '\f' || \ |
| 55 |
(c) == '\r' || (c) == '\n' || (c) == '\0') |
| 56 |
#define is_delim(c) ((c) == '(' || (c) == ')' || \ |
| 57 |
(c) == '/' || \ |
| 58 |
(c) == '<' || (c) == '>' || \ |
| 59 |
(c) == '[' || (c) == ']' || \ |
| 60 |
(c) == '%') |
| 61 |
#define PDF_TOKEN_END(p,e) ((p) >= (e) || is_space(*(p)) || is_delim(*(p))) |
| 62 |
|
| 63 |
#define istokensep(c) (is_space((c)) || is_delim((c))) |
| 64 |
|
| 65 |
static struct { |
| 66 |
int tainted; |
| 67 |
} parser_state = { |
| 68 |
0 |
| 69 |
}; |
| 70 |
|
| 71 |
static int xtoi (char ch); |
| 72 |
|
| 73 |
void |
| 74 |
dump (const char *start, const char *end) |
| 75 |
{ |
| 76 |
const char *p = start; |
| 77 |
|
| 78 |
#define DUMP_LIMIT 50 |
| 79 |
MESG("\nCurrent input buffer is -->"); |
| 80 |
while (p < end && p < start + DUMP_LIMIT) |
| 81 |
MESG("%c", *(p++)); |
| 82 |
if (p == start+DUMP_LIMIT) |
| 83 |
MESG("..."); |
| 84 |
MESG("<--\n"); |
| 85 |
} |
| 86 |
|
| 87 |
void |
| 88 |
pdfparse_skip_line (const char **start, const char *end) |
| 89 |
{ |
| 90 |
while (*start < end && **start != '\n' && **start != '\r') |
| 91 |
(*start)++; |
| 92 |
/* The carriage return (CR; \r; 0x0D) and line feed (LF; \n; 0x0A) |
| 93 |
* characters, also called newline characters, are treated as |
| 94 |
* end-of-line (EOL) markers. The combination of a carriage return |
| 95 |
* followed immediately by a line feed is treated as one EOL marker. |
| 96 |
*/ |
| 97 |
if (*start < end && **start == '\r') |
| 98 |
(*start)++; |
| 99 |
if (*start < end && **start == '\n') |
| 100 |
(*start)++; |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
skip_white (const char **start, const char *end) |
| 105 |
{ |
| 106 |
/* |
| 107 |
* The null (NUL; 0x00) character is a white-space character in PDF spec |
| 108 |
* but isspace(0x00) returns FALSE; on the other hand, the vertical tab |
| 109 |
* (VT; 0x0B) character is not a white-space character in PDF spec but |
| 110 |
* isspace(0x0B) returns TRUE. |
| 111 |
*/ |
| 112 |
while (*start < end && (is_space(**start) || **start == '%')) { |
| 113 |
if (**start == '%') |
| 114 |
pdfparse_skip_line(start, end); |
| 115 |
else |
| 116 |
(*start)++; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
static char * |
| 122 |
parsed_string (const char *start, const char *end) |
| 123 |
{ |
| 124 |
char *result = NULL; |
| 125 |
int len; |
| 126 |
|
| 127 |
len = end - start; |
| 128 |
if (len > 0) { |
| 129 |
result = NEW(len + 1, char); |
| 130 |
memcpy(result, start, len); |
| 131 |
result[len] = '\0'; |
| 132 |
} |
| 133 |
|
| 134 |
return result; |
| 135 |
} |
| 136 |
|
| 137 |
char * |
| 138 |
parse_number (const char **start, const char *end) |
| 139 |
{ |
| 140 |
char *number; |
| 141 |
const char *p; |
| 142 |
|
| 143 |
skip_white(start, end); |
| 144 |
p = *start; |
| 145 |
if (p < end && (*p == '+' || *p == '-')) |
| 146 |
p++; |
| 147 |
while (p < end && isdigit((unsigned char)*p)) |
| 148 |
p++; |
| 149 |
if (p < end && *p == '.') { |
| 150 |
p++; |
| 151 |
while (p < end && isdigit((unsigned char)*p)) |
| 152 |
p++; |
| 153 |
} |
| 154 |
number = parsed_string(*start, p); |
| 155 |
|
| 156 |
*start = p; |
| 157 |
return number; |
| 158 |
} |
| 159 |
|
| 160 |
char * |
| 161 |
parse_unsigned (const char **start, const char *end) |
| 162 |
{ |
| 163 |
char *number; |
| 164 |
const char *p; |
| 165 |
|
| 166 |
skip_white(start, end); |
| 167 |
for (p = *start; p < end; p++) { |
| 168 |
if (!isdigit((unsigned char)*p)) |
| 169 |
break; |
| 170 |
} |
| 171 |
number = parsed_string(*start, p); |
| 172 |
|
| 173 |
*start = p; |
| 174 |
return number; |
| 175 |
} |
| 176 |
|
| 177 |
static char * |
| 178 |
parse_gen_ident (const char **start, const char *end, const char *valid_chars) |
| 179 |
{ |
| 180 |
char *ident; |
| 181 |
const char *p; |
| 182 |
|
| 183 |
/* No skip_white(start, end)? */ |
| 184 |
for (p = *start; p < end; p++) { |
| 185 |
if (!strchr(valid_chars, *p)) |
| 186 |
break; |
| 187 |
} |
| 188 |
ident = parsed_string(*start, p); |
| 189 |
|
| 190 |
*start = p; |
| 191 |
return ident; |
| 192 |
} |
| 193 |
|
| 194 |
char * |
| 195 |
parse_ident (const char **start, const char *end) |
| 196 |
{ |
| 197 |
static const char *valid_chars = |
| 198 |
"!\"#$&'*+,-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz|~"; |
| 199 |
|
| 200 |
return parse_gen_ident(start, end, valid_chars); |
| 201 |
} |
| 202 |
|
| 203 |
char * |
| 204 |
parse_val_ident (const char **start, const char *end) |
| 205 |
{ |
| 206 |
static const char *valid_chars = |
| 207 |
"!\"#$&'*+,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz|~"; |
| 208 |
|
| 209 |
return parse_gen_ident(start, end, valid_chars); |
| 210 |
} |
| 211 |
|
| 212 |
char * |
| 213 |
parse_opt_ident (const char **start, const char *end) |
| 214 |
{ |
| 215 |
if (*start < end && **start == '@') { |
| 216 |
(*start)++; |
| 217 |
return parse_ident(start, end); |
| 218 |
} |
| 219 |
|
| 220 |
return NULL; |
| 221 |
} |
| 222 |
|
| 223 |
pdf_obj * |
| 224 |
parse_pdf_number (const char **pp, const char *endptr) |
| 225 |
{ |
| 226 |
const char *p; |
| 227 |
double v = 0.0; |
| 228 |
int nddigits = 0, sign = 1; |
| 229 |
int has_dot = 0; |
| 230 |
|
| 231 |
p = *pp; |
| 232 |
skip_white(&p, endptr); |
| 233 |
if (p >= endptr || |
| 234 |
(!isdigit((unsigned char)p[0]) && p[0] != '.' && |
| 235 |
p[0] != '+' && p[0] != '-')) { |
| 236 |
WARN("Could not find a numeric object."); |
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
if (p[0] == '-') { |
| 241 |
if (p + 1 >= endptr) { |
| 242 |
WARN("Could not find a numeric object."); |
| 243 |
return NULL; |
| 244 |
} |
| 245 |
sign = -1; |
| 246 |
p++; |
| 247 |
} else if (p[0] == '+') { |
| 248 |
if (p + 1 >= endptr) { |
| 249 |
WARN("Could not find a numeric object."); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
sign = 1; |
| 253 |
p++; |
| 254 |
} |
| 255 |
|
| 256 |
while (p < endptr && !istokensep(p[0])) { |
| 257 |
if (p[0] == '.') { |
| 258 |
if (has_dot) { /* Two dots */ |
| 259 |
WARN("Could not find a numeric object."); |
| 260 |
return NULL; |
| 261 |
} else { |
| 262 |
has_dot = 1; |
| 263 |
} |
| 264 |
} else if (isdigit((unsigned char)p[0])) { |
| 265 |
if (has_dot) { |
| 266 |
v += (p[0] - '0') / pow(10, nddigits + 1); |
| 267 |
nddigits++; |
| 268 |
} else { |
| 269 |
v = v * 10.0 + p[0] - '0'; |
| 270 |
} |
| 271 |
} else { |
| 272 |
WARN("Could not find a numeric object."); |
| 273 |
return NULL; |
| 274 |
} |
| 275 |
p++; |
| 276 |
} |
| 277 |
|
| 278 |
*pp = p; |
| 279 |
return pdf_new_number(sign * v); |
| 280 |
} |
| 281 |
|
| 282 |
/* |
| 283 |
* PDF Name: |
| 284 |
* |
| 285 |
* PDF-1.2+: Two hexadecimal digits preceded by a number sign. |
| 286 |
*/ |
| 287 |
static int |
| 288 |
pn_getc (const char **pp, const char *endptr) |
| 289 |
{ |
| 290 |
int ch = 0; |
| 291 |
const char *p; |
| 292 |
|
| 293 |
p = *pp; |
| 294 |
if (p[0] == '#') { |
| 295 |
if (p + 2 >= endptr) { |
| 296 |
*pp = endptr; |
| 297 |
return -1; |
| 298 |
} |
| 299 |
if (!isxdigit((unsigned char)p[1]) || !isxdigit((unsigned char)p[2])) { |
| 300 |
*pp += 3; |
| 301 |
return -1; |
| 302 |
} |
| 303 |
ch = (xtoi(p[1]) << 4); |
| 304 |
ch += xtoi(p[2]); |
| 305 |
*pp += 3; |
| 306 |
} else { |
| 307 |
ch = p[0]; |
| 308 |
*pp += 1; |
| 309 |
} |
| 310 |
|
| 311 |
return ch; |
| 312 |
} |
| 313 |
|
| 314 |
#ifndef PDF_NAME_LEN_MAX |
| 315 |
#define PDF_NAME_LEN_MAX 128 |
| 316 |
#endif |
| 317 |
|
| 318 |
#ifndef PDF_STRING_LEN_MAX |
| 319 |
#define PDF_STRING_LEN_MAX 65535 |
| 320 |
#endif |
| 321 |
|
| 322 |
#define STRING_BUFFER_SIZE PDF_STRING_LEN_MAX+1 |
| 323 |
static char sbuf[PDF_STRING_LEN_MAX+1]; |
| 324 |
|
| 325 |
|
| 326 |
pdf_obj * |
| 327 |
parse_pdf_name (const char **pp, const char *endptr) |
| 328 |
{ |
| 329 |
char name[PDF_NAME_LEN_MAX+1]; |
| 330 |
int ch, len = 0; |
| 331 |
|
| 332 |
skip_white(pp, endptr); |
| 333 |
if (*pp >= endptr || **pp != '/') { |
| 334 |
WARN("Could not find a name object."); |
| 335 |
return NULL; |
| 336 |
} |
| 337 |
|
| 338 |
(*pp)++; |
| 339 |
while (*pp < endptr && !istokensep(**pp)) { |
| 340 |
ch = pn_getc(pp, endptr); |
| 341 |
if (ch < 0 || ch > 0xff) { |
| 342 |
WARN("Invalid char in PDF name object. (ignored)"); |
| 343 |
} else if (ch == 0) { |
| 344 |
WARN("Null char not allowed in PDF name object. (ignored)"); |
| 345 |
} else if (len < STRING_BUFFER_SIZE) { |
| 346 |
if (len == PDF_NAME_LEN_MAX) { |
| 347 |
WARN("PDF name length too long. (>= %d bytes)", PDF_NAME_LEN_MAX); |
| 348 |
} |
| 349 |
name[len++] = ch; |
| 350 |
} else { |
| 351 |
WARN("PDF name length too long. (>= %d bytes, truncated)", |
| 352 |
STRING_BUFFER_SIZE); |
| 353 |
} |
| 354 |
} |
| 355 |
if (len < 1) { |
| 356 |
WARN("No valid name object found."); |
| 357 |
return NULL; |
| 358 |
} |
| 359 |
name[len] = '\0'; |
| 360 |
|
| 361 |
return pdf_new_name(name); |
| 362 |
} |
| 363 |
|
| 364 |
pdf_obj * |
| 365 |
parse_pdf_boolean (const char **pp, const char *endptr) |
| 366 |
{ |
| 367 |
skip_white(pp, endptr); |
| 368 |
if (*pp + 4 <= endptr && |
| 369 |
!strncmp(*pp, "true", 4)) { |
| 370 |
if (*pp + 4 == endptr || |
| 371 |
istokensep(*(*pp + 4))) { |
| 372 |
*pp += 4; |
| 373 |
return pdf_new_boolean(1); |
| 374 |
} |
| 375 |
} else if (*pp + 5 <= endptr && |
| 376 |
!strncmp(*pp, "false", 5)) { |
| 377 |
if (*pp + 5 == endptr || |
| 378 |
istokensep(*(*pp + 5))) { |
| 379 |
*pp += 5; |
| 380 |
return pdf_new_boolean(0); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
WARN("Not a boolean object."); |
| 385 |
|
| 386 |
return NULL; |
| 387 |
} |
| 388 |
|
| 389 |
pdf_obj * |
| 390 |
parse_pdf_null (const char **pp, const char *endptr) |
| 391 |
{ |
| 392 |
skip_white(pp, endptr); |
| 393 |
if (*pp + 4 > endptr) { |
| 394 |
WARN("Not a null object."); |
| 395 |
return NULL; |
| 396 |
} else if (*pp + 4 < endptr && |
| 397 |
!istokensep(*(*pp+4))) { |
| 398 |
WARN("Not a null object."); |
| 399 |
return NULL; |
| 400 |
} else if (!strncmp(*pp, "null", 4)) { |
| 401 |
*pp += 4; |
| 402 |
return pdf_new_null(); |
| 403 |
} |
| 404 |
|
| 405 |
WARN("Not a null object."); |
| 406 |
|
| 407 |
return NULL; |
| 408 |
} |
| 409 |
|
| 410 |
/* |
| 411 |
* PDF Literal String |
| 412 |
*/ |
| 413 |
#ifndef isodigit |
| 414 |
#define isodigit(c) ((c) >= '0' && (c) <= '7') |
| 415 |
#endif |
| 416 |
static int |
| 417 |
ps_getescc (const char **pp, const char *endptr) |
| 418 |
{ |
| 419 |
int ch, i; |
| 420 |
const char *p; |
| 421 |
|
| 422 |
p = *pp + 1; /* backslash assumed. */ |
| 423 |
switch (p[0]) { |
| 424 |
case 'n': ch = '\n'; p++; break; |
| 425 |
case 'r': ch = '\r'; p++; break; |
| 426 |
case 't': ch = '\t'; p++; break; |
| 427 |
case 'b': ch = '\b'; p++; break; |
| 428 |
case 'f': ch = '\f'; p++; break; |
| 429 |
|
| 430 |
/* |
| 431 |
* An end-of-line marker preceded by a backslash must be ignored. |
| 432 |
*/ |
| 433 |
case '\n': |
| 434 |
ch = -1; |
| 435 |
p++; |
| 436 |
break; |
| 437 |
case '\r': |
| 438 |
ch = -1; |
| 439 |
p++; |
| 440 |
if (p < endptr && p[0] == '\n') |
| 441 |
p++; |
| 442 |
break; |
| 443 |
default: |
| 444 |
if (p[0] == '\\' || |
| 445 |
p[0] == '(' || p[0] == ')') { |
| 446 |
ch = p[0]; |
| 447 |
p++; |
| 448 |
} else if (isodigit(p[0])) { |
| 449 |
ch = 0; |
| 450 |
/* Don't forget isodigit() is a macro. */ |
| 451 |
for (i = 0; i < 3 && |
| 452 |
p < endptr && isodigit(p[0]); i++) { |
| 453 |
ch = (ch << 3) + (p[0] - '0'); |
| 454 |
p++; |
| 455 |
} |
| 456 |
ch = (ch & 0xff); /* Ignore overflow. */ |
| 457 |
} else { |
| 458 |
ch = ((unsigned char) p[0]); /* Ignore only backslash. */ |
| 459 |
p++; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
*pp = p; |
| 464 |
return ch; |
| 465 |
} |
| 466 |
|
| 467 |
static pdf_obj * |
| 468 |
parse_pdf_literal_string (const char **pp, const char *endptr) |
| 469 |
{ |
| 470 |
int ch, op_count = 0, len = 0; |
| 471 |
const char *p; |
| 472 |
|
| 473 |
p = *pp; |
| 474 |
|
| 475 |
skip_white(&p, endptr); |
| 476 |
|
| 477 |
if (p >= endptr || p[0] != '(') |
| 478 |
return NULL; |
| 479 |
|
| 480 |
p++; |
| 481 |
|
| 482 |
/* The carriage return (CR, 0x0d) and line feed (LF, 0x0a) characters, |
| 483 |
* also called newline characters, are treated as end-of-line (EOL) |
| 484 |
* markers. The combination of a carriage return followed immediately |
| 485 |
* by a line feed is treated as one EOL marker. |
| 486 |
* [PDF Reference, 6th ed., version 1.7, p. 50] */ |
| 487 |
|
| 488 |
/* If an end-of-line marker appears within a literal string |
| 489 |
* without a preceding backslash, the result is equivalent to |
| 490 |
* \n (regardless of whether the end-of-line marker was |
| 491 |
* a carriage return, a line feed, or both). |
| 492 |
* [PDF Reference, 6th ed., version 1.7, p. 55] */ |
| 493 |
|
| 494 |
while (p < endptr) { |
| 495 |
|
| 496 |
ch = p[0]; |
| 497 |
|
| 498 |
if (ch == ')' && op_count < 1) |
| 499 |
break; |
| 500 |
|
| 501 |
#ifndef PDF_PARSE_STRICT |
| 502 |
if (parser_state.tainted) { |
| 503 |
if (p + 1 < endptr && (ch & 0x80)) { |
| 504 |
if (len + 2 >= PDF_STRING_LEN_MAX) { |
| 505 |
WARN("PDF string length too long. (limit: %ld)", |
| 506 |
PDF_STRING_LEN_MAX); |
| 507 |
return NULL; |
| 508 |
} |
| 509 |
sbuf[len++] = p[0]; |
| 510 |
sbuf[len++] = p[1]; |
| 511 |
p += 2; |
| 512 |
continue; |
| 513 |
} |
| 514 |
} |
| 515 |
#endif /* !PDF_PARSE_STRICT */ |
| 516 |
|
| 517 |
if (len + 1 >= PDF_STRING_LEN_MAX) { |
| 518 |
WARN("PDF string length too long. (limit: %ld)", |
| 519 |
PDF_STRING_LEN_MAX); |
| 520 |
return NULL; |
| 521 |
} |
| 522 |
|
| 523 |
switch (ch) { |
| 524 |
case '\\': |
| 525 |
ch = ps_getescc(&p, endptr); |
| 526 |
if (ch >= 0) |
| 527 |
sbuf[len++] = (ch & 0xff); |
| 528 |
break; |
| 529 |
case '\r': |
| 530 |
p++; |
| 531 |
if (p < endptr && p[0] == '\n') |
| 532 |
p++; |
| 533 |
sbuf[len++] = '\n'; |
| 534 |
break; |
| 535 |
default: |
| 536 |
if (ch == '(') |
| 537 |
op_count++; |
| 538 |
else if (ch == ')') |
| 539 |
op_count--; |
| 540 |
sbuf[len++] = ch; |
| 541 |
p++; |
| 542 |
break; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
if (op_count > 0 || |
| 547 |
p >= endptr || p[0] != ')') { |
| 548 |
WARN("Unbalanced parens/truncated PDF literal string."); |
| 549 |
return NULL; |
| 550 |
} |
| 551 |
|
| 552 |
*pp = p + 1; |
| 553 |
return pdf_new_string(sbuf, len); |
| 554 |
} |
| 555 |
|
| 556 |
/* |
| 557 |
* PDF Hex String |
| 558 |
*/ |
| 559 |
static int |
| 560 |
xtoi (char ch) |
| 561 |
{ |
| 562 |
if (ch >= '0' && ch <= '9') |
| 563 |
return ch - '0'; |
| 564 |
if (ch >= 'A' && ch <= 'F') |
| 565 |
return (ch - 'A') + 10; |
| 566 |
if (ch >= 'a' && ch <= 'f') |
| 567 |
return (ch - 'a') + 10; |
| 568 |
|
| 569 |
return -1; |
| 570 |
} |
| 571 |
|
| 572 |
static pdf_obj * |
| 573 |
parse_pdf_hex_string (const char **pp, const char *endptr) |
| 574 |
{ |
| 575 |
const char *p; |
| 576 |
int len; |
| 577 |
|
| 578 |
p = *pp; |
| 579 |
|
| 580 |
skip_white(&p, endptr); |
| 581 |
if (p >= endptr || p[0] != '<') |
| 582 |
return NULL; |
| 583 |
|
| 584 |
p++; |
| 585 |
|
| 586 |
len = 0; |
| 587 |
/* |
| 588 |
* PDF Reference does not describe how to treat invalid char. |
| 589 |
* Zero is appended if final hex digit is missing. |
| 590 |
*/ |
| 591 |
while (p < endptr && p[0] != '>' && len < PDF_STRING_LEN_MAX) { |
| 592 |
int ch; |
| 593 |
|
| 594 |
skip_white(&p, endptr); |
| 595 |
if (p >= endptr || p[0] == '>') |
| 596 |
break; |
| 597 |
|
| 598 |
ch = (xtoi(p[0]) << 4); |
| 599 |
p++; |
| 600 |
|
| 601 |
skip_white(&p, endptr); |
| 602 |
if (p < endptr && p[0] != '>') { |
| 603 |
ch += xtoi(p[0]); |
| 604 |
p++; |
| 605 |
} |
| 606 |
sbuf[len++] = (ch & 0xff); |
| 607 |
} |
| 608 |
|
| 609 |
if (p >= endptr) { |
| 610 |
WARN("Premature end of input hex string."); |
| 611 |
return NULL; |
| 612 |
} else if (p[0] != '>') { |
| 613 |
WARN("PDF string length too long. (limit: %ld)", PDF_STRING_LEN_MAX); |
| 614 |
return NULL; |
| 615 |
} |
| 616 |
|
| 617 |
*pp = p + 1; |
| 618 |
return pdf_new_string(sbuf, len); |
| 619 |
} |
| 620 |
|
| 621 |
pdf_obj * |
| 622 |
parse_pdf_string (const char **pp, const char *endptr) |
| 623 |
{ |
| 624 |
skip_white(pp, endptr); |
| 625 |
if (*pp + 2 <= endptr) { |
| 626 |
if (**pp == '(') |
| 627 |
return parse_pdf_literal_string(pp, endptr); |
| 628 |
else if (**pp == '<' && |
| 629 |
(*(*pp + 1) == '>' || isxdigit((unsigned char)*(*pp + 1)))) { |
| 630 |
return parse_pdf_hex_string(pp, endptr); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
WARN("Could not find a string object."); |
| 635 |
|
| 636 |
return NULL; |
| 637 |
} |
| 638 |
|
| 639 |
static pdf_obj * |
| 640 |
parse_pdf_dict_extended (const char **pp, const char *endptr, pdf_file *pf, |
| 641 |
pdf_obj* (*unknown_handler) (const char **pp, |
| 642 |
const char *endptr, |
| 643 |
void *user_data), |
| 644 |
void *user_data) |
| 645 |
{ |
| 646 |
pdf_obj *result = NULL; |
| 647 |
const char *p; |
| 648 |
|
| 649 |
p = *pp; |
| 650 |
|
| 651 |
skip_white(&p, endptr); |
| 652 |
|
| 653 |
/* At least four letter <<>>. */ |
| 654 |
if (p + 4 > endptr || |
| 655 |
p[0] != '<' || p[1] != '<') { |
| 656 |
return NULL; |
| 657 |
} |
| 658 |
p += 2; |
| 659 |
|
| 660 |
result = pdf_new_dict(); |
| 661 |
|
| 662 |
skip_white(&p, endptr); |
| 663 |
while (p < endptr && p[0] != '>') { |
| 664 |
pdf_obj *key, *value; |
| 665 |
|
| 666 |
skip_white(&p, endptr); |
| 667 |
key = parse_pdf_name(&p, endptr); |
| 668 |
if (!key) { |
| 669 |
WARN("Could not find a key in dictionary object."); |
| 670 |
pdf_release_obj(result); |
| 671 |
return NULL; |
| 672 |
} |
| 673 |
|
| 674 |
skip_white(&p, endptr); |
| 675 |
|
| 676 |
value = parse_pdf_object_extended(&p, endptr, pf, unknown_handler, user_data); |
| 677 |
if (!value) { |
| 678 |
pdf_release_obj(key); |
| 679 |
pdf_release_obj(value); |
| 680 |
pdf_release_obj(result); |
| 681 |
WARN("Could not find a value in dictionary object."); |
| 682 |
return NULL; |
| 683 |
} |
| 684 |
pdf_add_dict(result, key, value); |
| 685 |
|
| 686 |
skip_white(&p, endptr); |
| 687 |
} |
| 688 |
|
| 689 |
if (p + 2 > endptr || |
| 690 |
p[0] != '>' || p[1] != '>') { |
| 691 |
WARN("Syntax error: Dictionary object ended prematurely."); |
| 692 |
pdf_release_obj(result); |
| 693 |
return NULL; |
| 694 |
} |
| 695 |
|
| 696 |
*pp = p + 2; /* skip >> */ |
| 697 |
return result; |
| 698 |
} |
| 699 |
|
| 700 |
static pdf_obj * |
| 701 |
parse_pdf_array_extended (const char **pp, const char *endptr, pdf_file *pf, |
| 702 |
pdf_obj* (*unknown_handler) (const char **pp, |
| 703 |
const char *endptr, |
| 704 |
void *user_data), |
| 705 |
void *user_data) |
| 706 |
{ |
| 707 |
pdf_obj *result; |
| 708 |
const char *p; |
| 709 |
|
| 710 |
p = *pp; |
| 711 |
|
| 712 |
skip_white(&p, endptr); |
| 713 |
if (p + 2 > endptr || p[0] != '[') { |
| 714 |
WARN("Could not find an array object."); |
| 715 |
return NULL; |
| 716 |
} |
| 717 |
|
| 718 |
result = pdf_new_array(); |
| 719 |
|
| 720 |
p++; |
| 721 |
skip_white(&p, endptr); |
| 722 |
|
| 723 |
while (p < endptr && p[0] != ']') { |
| 724 |
pdf_obj *elem; |
| 725 |
|
| 726 |
elem = parse_pdf_object_extended(&p, endptr, pf, unknown_handler, user_data); |
| 727 |
if (!elem) { |
| 728 |
pdf_release_obj(result); |
| 729 |
WARN("Could not find a valid object in array object."); |
| 730 |
return NULL; |
| 731 |
} |
| 732 |
pdf_add_array(result, elem); |
| 733 |
|
| 734 |
skip_white(&p, endptr); |
| 735 |
} |
| 736 |
|
| 737 |
if (p >= endptr || p[0] != ']') { |
| 738 |
WARN("Array object ended prematurely."); |
| 739 |
pdf_release_obj(result); |
| 740 |
return NULL; |
| 741 |
} |
| 742 |
|
| 743 |
*pp = p + 1; /* skip ] */ |
| 744 |
return result; |
| 745 |
} |
| 746 |
|
| 747 |
static pdf_obj * |
| 748 |
parse_pdf_stream (const char **pp, const char *endptr, pdf_obj *dict) |
| 749 |
{ |
| 750 |
pdf_obj *result = NULL; |
| 751 |
const char *p; |
| 752 |
pdf_obj *stream_dict; |
| 753 |
int stream_length; |
| 754 |
|
| 755 |
p = *pp; |
| 756 |
skip_white(&p, endptr); |
| 757 |
if (p + 6 > endptr || |
| 758 |
strncmp(p, "stream", 6)) { |
| 759 |
return NULL; |
| 760 |
} |
| 761 |
p += 6; |
| 762 |
|
| 763 |
/* The keyword stream that follows the stream dictionary |
| 764 |
* should be followed by an end-of-line marker consisting of |
| 765 |
* either a carriage return (0x0D;\r) and a line feed (0x0A;\n) |
| 766 |
* or just a line feed, and not by a carriage return alone. |
| 767 |
* [PDF Reference, 6th ed., version 1.7, pp. 60-61] */ |
| 768 |
|
| 769 |
/* Notice that TeX translates an end-of-line marker to a single space. */ |
| 770 |
if (p < endptr && p[0] == '\n') { |
| 771 |
p++; |
| 772 |
} else if (p + 1 < endptr && |
| 773 |
(p[0] == '\r' && p[1] == '\n')) { |
| 774 |
p += 2; |
| 775 |
} |
| 776 |
|
| 777 |
/* Stream length */ |
| 778 |
{ |
| 779 |
pdf_obj *tmp, *tmp2; |
| 780 |
|
| 781 |
tmp = pdf_lookup_dict(dict, "Length"); |
| 782 |
|
| 783 |
if (tmp != NULL) { |
| 784 |
tmp2 = pdf_deref_obj(tmp); |
| 785 |
if (pdf_obj_typeof(tmp2) != PDF_NUMBER) |
| 786 |
stream_length = -1; |
| 787 |
else { |
| 788 |
stream_length = (int) pdf_number_value(tmp2); |
| 789 |
} |
| 790 |
pdf_release_obj(tmp2); |
| 791 |
} |
| 792 |
else { |
| 793 |
return NULL; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
if (stream_length < 0 || |
| 799 |
p + stream_length > endptr) |
| 800 |
return NULL; |
| 801 |
|
| 802 |
/* |
| 803 |
* If Filter is not applied, set STREAM_COMPRESS flag. |
| 804 |
* Should we use filter for ASCIIHexEncode/ASCII85Encode-ed streams? |
| 805 |
*/ |
| 806 |
{ |
| 807 |
pdf_obj *filters; |
| 808 |
|
| 809 |
filters = pdf_lookup_dict(dict, "Filter"); |
| 810 |
if (!filters && stream_length > 10) { |
| 811 |
result = pdf_new_stream(STREAM_COMPRESS); |
| 812 |
} else { |
| 813 |
result = pdf_new_stream(0); |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
stream_dict = pdf_stream_dict(result); |
| 818 |
pdf_merge_dict(stream_dict, dict); |
| 819 |
|
| 820 |
pdf_add_stream(result, p, stream_length); |
| 821 |
p += stream_length; |
| 822 |
|
| 823 |
/* Check "endsteam" */ |
| 824 |
{ |
| 825 |
/* It is recommended that there be an end-of-line marker |
| 826 |
* after the data and before endstream; this marker is not included |
| 827 |
* in the stream length. |
| 828 |
* [PDF Reference, 6th ed., version 1.7, pp. 61] */ |
| 829 |
if (p < endptr && p[0] == '\r') |
| 830 |
p++; |
| 831 |
if (p < endptr && p[0] == '\n') |
| 832 |
p++; |
| 833 |
|
| 834 |
if (p + 9 > endptr || |
| 835 |
memcmp(p, "endstream", 9)) { |
| 836 |
pdf_release_obj(result); |
| 837 |
return NULL; |
| 838 |
} |
| 839 |
p += 9; |
| 840 |
} |
| 841 |
|
| 842 |
*pp = p; |
| 843 |
return result; |
| 844 |
} |
| 845 |
|
| 846 |
static pdf_obj * |
| 847 |
try_pdf_reference (const char *start, const char *end, const char **endptr, pdf_file *pf) |
| 848 |
{ |
| 849 |
unsigned id = 0; |
| 850 |
unsigned short gen = 0; |
| 851 |
|
| 852 |
ASSERT(pf); |
| 853 |
|
| 854 |
if (endptr) |
| 855 |
*endptr = start; |
| 856 |
|
| 857 |
skip_white(&start, end); |
| 858 |
if (start > end - 5 || !isdigit((unsigned char)*start)) { |
| 859 |
return NULL; |
| 860 |
} |
| 861 |
while (!is_space(*start)) { |
| 862 |
if (start >= end || !isdigit((unsigned char)*start)) { |
| 863 |
return NULL; |
| 864 |
} |
| 865 |
id = id * 10 + (*start - '0'); |
| 866 |
start++; |
| 867 |
} |
| 868 |
|
| 869 |
skip_white(&start, end); |
| 870 |
if (start >= end || !isdigit((unsigned char)*start)) |
| 871 |
return NULL; |
| 872 |
while (!is_space(*start)) { |
| 873 |
if (start >= end || !isdigit((unsigned char)*start)) |
| 874 |
return NULL; |
| 875 |
gen = gen * 10 + (*start - '0'); |
| 876 |
start++; |
| 877 |
} |
| 878 |
|
| 879 |
skip_white(&start, end); |
| 880 |
if (start >= end || *start != 'R') |
| 881 |
return NULL; |
| 882 |
start++; |
| 883 |
if (!PDF_TOKEN_END(start, end)) |
| 884 |
return NULL; |
| 885 |
|
| 886 |
if (endptr) |
| 887 |
*endptr = start; |
| 888 |
|
| 889 |
return pdf_new_indirect(pf, id, gen); |
| 890 |
} |
| 891 |
|
| 892 |
pdf_obj * |
| 893 |
parse_pdf_object_extended (const char **pp, const char *endptr, pdf_file *pf, |
| 894 |
pdf_obj* (*unknown_handler) (const char **pp, |
| 895 |
const char *endptr, |
| 896 |
void *user_data), |
| 897 |
void *user_data) |
| 898 |
/* If pf is NULL, then indirect references are not allowed */ |
| 899 |
{ |
| 900 |
pdf_obj *result = NULL; |
| 901 |
const char *nextptr; |
| 902 |
|
| 903 |
skip_white(pp, endptr); |
| 904 |
if (*pp >= endptr) { |
| 905 |
WARN("Could not find any valid object."); |
| 906 |
return NULL; |
| 907 |
} |
| 908 |
|
| 909 |
switch (**pp) { |
| 910 |
|
| 911 |
case '<': |
| 912 |
|
| 913 |
if (*(*pp + 1) != '<') { |
| 914 |
result = parse_pdf_hex_string(pp, endptr); |
| 915 |
} else { |
| 916 |
pdf_obj *dict; |
| 917 |
|
| 918 |
result = parse_pdf_dict_extended(pp, endptr, pf, unknown_handler, user_data); |
| 919 |
skip_white(pp, endptr); |
| 920 |
if ( result && |
| 921 |
*pp <= endptr - 15 && |
| 922 |
!memcmp(*pp, "stream", 6)) { |
| 923 |
dict = result; |
| 924 |
result = parse_pdf_stream(pp, endptr, dict); |
| 925 |
pdf_release_obj(dict); |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
break; |
| 930 |
case '(': |
| 931 |
result = parse_pdf_string(pp, endptr); |
| 932 |
break; |
| 933 |
case '[': |
| 934 |
result = parse_pdf_array_extended(pp, endptr, pf, unknown_handler, user_data); |
| 935 |
break; |
| 936 |
case '/': |
| 937 |
result = parse_pdf_name(pp, endptr); |
| 938 |
break; |
| 939 |
case 'n': |
| 940 |
result = parse_pdf_null(pp, endptr); |
| 941 |
break; |
| 942 |
case 't': case 'f': |
| 943 |
result = parse_pdf_boolean(pp, endptr); |
| 944 |
break; |
| 945 |
case '+': case '-': case '.': |
| 946 |
result = parse_pdf_number(pp, endptr); |
| 947 |
break; |
| 948 |
case '0': case '1': case '2': case '3': case '4': |
| 949 |
case '5': case '6': case '7': case '8': case '9': |
| 950 |
|
| 951 |
/* |
| 952 |
* If pf != NULL, then we are parsing a PDF file, |
| 953 |
* and indirect references are allowed. |
| 954 |
*/ |
| 955 |
if (pf && (result = try_pdf_reference(*pp, endptr, &nextptr, pf))) { |
| 956 |
*pp = nextptr; |
| 957 |
} else { |
| 958 |
result = parse_pdf_number(pp, endptr); |
| 959 |
} |
| 960 |
break; |
| 961 |
|
| 962 |
default: |
| 963 |
if (unknown_handler) { |
| 964 |
result = unknown_handler(pp, endptr, user_data); |
| 965 |
} else { |
| 966 |
WARN("Unknown PDF object type."); |
| 967 |
result = NULL; |
| 968 |
} |
| 969 |
break; |
| 970 |
} |
| 971 |
|
| 972 |
return result; |
| 973 |
} |
| 974 |
|
| 975 |
pdf_obj * |
| 976 |
parse_pdf_dict (const char **pp, const char *endptr, pdf_file *pf) |
| 977 |
{ |
| 978 |
return parse_pdf_dict_extended(pp, endptr, pf, NULL, NULL); |
| 979 |
} |
| 980 |
|
| 981 |
pdf_obj * |
| 982 |
parse_pdf_array (const char **pp, const char *endptr, pdf_file *pf) |
| 983 |
{ |
| 984 |
return parse_pdf_array_extended(pp, endptr, pf, NULL, NULL); |
| 985 |
} |
| 986 |
|
| 987 |
pdf_obj * |
| 988 |
parse_pdf_object (const char **pp, const char *endptr, pdf_file *pf) |
| 989 |
{ |
| 990 |
return parse_pdf_object_extended(pp, endptr, pf, NULL, NULL); |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
pdf_obj * |
| 995 |
parse_pdf_tainted_dict (const char **pp, const char *endptr, |
| 996 |
pdf_obj* (*unknown_handler) (const char **pp, |
| 997 |
const char *endptr, |
| 998 |
void *user_data), |
| 999 |
void *user_data) |
| 1000 |
{ |
| 1001 |
pdf_obj *result; |
| 1002 |
|
| 1003 |
parser_state.tainted = 1; |
| 1004 |
result = parse_pdf_dict_extended(pp, endptr, NULL, unknown_handler, user_data); |
| 1005 |
parser_state.tainted = 0; |
| 1006 |
|
| 1007 |
return result; |
| 1008 |
} |
| 1009 |
|