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