OLD | NEW |
1 | 1 |
2 #include "SkPdfNativeTokenizer.h" | 2 #include "SkPdfNativeTokenizer.h" |
3 #include "SkPdfObject.h" | 3 #include "SkPdfNativeObject.h" |
4 #include "SkPdfConfig.h" | 4 #include "SkPdfConfig.h" |
5 | 5 |
6 #include "SkPdfStreamCommonDictionary_autogen.h" | 6 #include "SkPdfStreamCommonDictionary_autogen.h" |
7 #include "SkPdfImageDictionary_autogen.h" | 7 #include "SkPdfImageDictionary_autogen.h" |
8 | 8 |
9 // TODO(edisonn): perf!!! | 9 // TODO(edisonn): perf!!! |
10 // there could be 0s between start and end! but not in the needle. | 10 // there could be 0s between start and end! but not in the needle. |
11 static char* strrstrk(char* hayStart, char* hayEnd, const char* needle) { | 11 static char* strrstrk(char* hayStart, char* hayEnd, const char* needle) { |
12 int needleLen = strlen(needle); | 12 int needleLen = strlen(needle); |
13 if ((isPdfWhiteSpaceOrPdfDelimiter(*(hayStart+needleLen)) || (hayStart+needl
eLen == hayEnd)) && | 13 if ((isPdfWhiteSpaceOrPdfDelimiter(*(hayStart+needleLen)) || (hayStart+needl
eLen == hayEnd)) && |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } | 119 } |
120 | 120 |
121 while (start < end && !isPdfWhiteSpaceOrPdfDelimiter(*start)) { | 121 while (start < end && !isPdfWhiteSpaceOrPdfDelimiter(*start)) { |
122 TRACE_TK(*start); | 122 TRACE_TK(*start); |
123 start++; | 123 start++; |
124 } | 124 } |
125 return start; | 125 return start; |
126 } | 126 } |
127 | 127 |
128 // last elem has to be ] | 128 // last elem has to be ] |
129 static const unsigned char* readArray(int level, const unsigned char* start, con
st unsigned char* end, SkPdfObject* array, SkPdfAllocator* allocator, SkNativePa
rsedPDF* doc) { | 129 static const unsigned char* readArray(int level, const unsigned char* start, con
st unsigned char* end, SkPdfNativeObject* array, SkPdfAllocator* allocator, SkPd
fNativeDoc* doc) { |
130 if (allocator == NULL) { | 130 if (allocator == NULL) { |
131 // TODO(edisonn): report/warning error | 131 // TODO(edisonn): report/warning error |
132 return end; | 132 return end; |
133 } | 133 } |
134 | 134 |
135 TRACE_INDENT(level, "Array"); | 135 TRACE_INDENT(level, "Array"); |
136 while (start < end) { | 136 while (start < end) { |
137 // skip white spaces | 137 // skip white spaces |
138 start = skipPdfWhiteSpaces(level + 1, start, end); | 138 start = skipPdfWhiteSpaces(level + 1, start, end); |
139 | 139 |
140 const unsigned char* endOfToken = endOfPdfToken(level + 1, start, end); | 140 const unsigned char* endOfToken = endOfPdfToken(level + 1, start, end); |
141 | 141 |
142 if (endOfToken == start) { | 142 if (endOfToken == start) { |
143 // TODO(edisonn): report error in pdf file (end of stream with ] for
end of aray | 143 // TODO(edisonn): report error in pdf file (end of stream with ] for
end of aray |
144 return start; | 144 return start; |
145 } | 145 } |
146 | 146 |
147 if (endOfToken == start + 1 && *start == kClosedSquareBracket_PdfDelimit
er) { | 147 if (endOfToken == start + 1 && *start == kClosedSquareBracket_PdfDelimit
er) { |
148 return endOfToken; | 148 return endOfToken; |
149 } | 149 } |
150 | 150 |
151 SkPdfObject* newObj = allocator->allocObject(); | 151 SkPdfNativeObject* newObj = allocator->allocObject(); |
152 start = nextObject(level + 1, start, end, newObj, allocator, doc); | 152 start = nextObject(level + 1, start, end, newObj, allocator, doc); |
153 // TODO(edisonn): perf/memory: put the variables on the stack, and flush
them on the array only when | 153 // TODO(edisonn): perf/memory: put the variables on the stack, and flush
them on the array only when |
154 // we are sure they are not references! | 154 // we are sure they are not references! |
155 if (newObj->isKeywordReference() && array->size() >= 2 && array->objAtAI
ndex(array->size() - 1)->isInteger() && array->objAtAIndex(array->size() - 2)->i
sInteger()) { | 155 if (newObj->isKeywordReference() && array->size() >= 2 && array->objAtAI
ndex(array->size() - 1)->isInteger() && array->objAtAIndex(array->size() - 2)->i
sInteger()) { |
156 SkPdfObject* gen = array->removeLastInArray(); | 156 SkPdfNativeObject* gen = array->removeLastInArray(); |
157 SkPdfObject* id = array->removeLastInArray(); | 157 SkPdfNativeObject* id = array->removeLastInArray(); |
158 newObj->reset(); | 158 newObj->reset(); |
159 SkPdfObject::makeReference((unsigned int)id->intValue(), (unsigned i
nt)gen->intValue(), newObj); | 159 SkPdfNativeObject::makeReference((unsigned int)id->intValue(), (unsi
gned int)gen->intValue(), newObj); |
160 } | 160 } |
161 array->appendInArray(newObj); | 161 array->appendInArray(newObj); |
162 } | 162 } |
163 // TODO(edisonn): report not reached, we should never get here | 163 // TODO(edisonn): report not reached, we should never get here |
164 // TODO(edisonn): there might be a bug here, enable an assert and run it on
files | 164 // TODO(edisonn): there might be a bug here, enable an assert and run it on
files |
165 // or it might be that the files were actually corrupted | 165 // or it might be that the files were actually corrupted |
166 return start; | 166 return start; |
167 } | 167 } |
168 | 168 |
169 // When we read strings we will rewrite the string so we will reuse the memory | 169 // When we read strings we will rewrite the string so we will reuse the memory |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 return in; // consumed already ) at the end of the string | 289 return in; // consumed already ) at the end of the string |
290 } else { | 290 } else { |
291 return start + (out - (const unsigned char*)NULL); // return where the s
tring would end if we reuse the string | 291 return start + (out - (const unsigned char*)NULL); // return where the s
tring would end if we reuse the string |
292 } | 292 } |
293 } | 293 } |
294 | 294 |
295 static int readStringLength(int level, const unsigned char* start, const unsigne
d char* end) { | 295 static int readStringLength(int level, const unsigned char* start, const unsigne
d char* end) { |
296 return readString(level, start, end, NULL) - start; | 296 return readString(level, start, end, NULL) - start; |
297 } | 297 } |
298 | 298 |
299 static const unsigned char* readString(int level, const unsigned char* start, co
nst unsigned char* end, SkPdfObject* str, SkPdfAllocator* allocator) { | 299 static const unsigned char* readString(int level, const unsigned char* start, co
nst unsigned char* end, SkPdfNativeObject* str, SkPdfAllocator* allocator) { |
300 if (!allocator) { | 300 if (!allocator) { |
301 return end; | 301 return end; |
302 } | 302 } |
303 int outLength = readStringLength(level, start, end); | 303 int outLength = readStringLength(level, start, end); |
304 // TODO(edisonn): optimize the allocation, don't allocate new string, but pu
t it in a preallocated buffer | 304 // TODO(edisonn): optimize the allocation, don't allocate new string, but pu
t it in a preallocated buffer |
305 unsigned char* out = (unsigned char*)allocator->alloc(outLength); | 305 unsigned char* out = (unsigned char*)allocator->alloc(outLength); |
306 start = readString(level, start, end, out); | 306 start = readString(level, start, end, out); |
307 SkPdfObject::makeString(out, out + outLength, str); | 307 SkPdfNativeObject::makeString(out, out + outLength, str); |
308 TRACE_STRING(out, out + outLength); | 308 TRACE_STRING(out, out + outLength); |
309 return start; // consumed already ) at the end of the string | 309 return start; // consumed already ) at the end of the string |
310 } | 310 } |
311 | 311 |
312 static const unsigned char* readHexString(int level, const unsigned char* start,
const unsigned char* end, unsigned char* out) { | 312 static const unsigned char* readHexString(int level, const unsigned char* start,
const unsigned char* end, unsigned char* out) { |
313 TRACE_INDENT(level, "HexString"); | 313 TRACE_INDENT(level, "HexString"); |
314 bool hasOut = (out != NULL); | 314 bool hasOut = (out != NULL); |
315 const unsigned char* in = start; | 315 const unsigned char* in = start; |
316 | 316 |
317 unsigned char code = 0; | 317 unsigned char code = 0; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 return in; // consumed already > at the end of the string | 436 return in; // consumed already > at the end of the string |
437 } else { | 437 } else { |
438 return start + (out - (const unsigned char*)NULL); // return where the s
tring would end if we reuse the string | 438 return start + (out - (const unsigned char*)NULL); // return where the s
tring would end if we reuse the string |
439 } | 439 } |
440 } | 440 } |
441 | 441 |
442 static int readHexStringLength(int level, const unsigned char* start, const unsi
gned char* end) { | 442 static int readHexStringLength(int level, const unsigned char* start, const unsi
gned char* end) { |
443 return readHexString(level, start, end, NULL) - start; | 443 return readHexString(level, start, end, NULL) - start; |
444 } | 444 } |
445 | 445 |
446 static const unsigned char* readHexString(int level, const unsigned char* start,
const unsigned char* end, SkPdfObject* str, SkPdfAllocator* allocator) { | 446 static const unsigned char* readHexString(int level, const unsigned char* start,
const unsigned char* end, SkPdfNativeObject* str, SkPdfAllocator* allocator) { |
447 if (!allocator) { | 447 if (!allocator) { |
448 return end; | 448 return end; |
449 } | 449 } |
450 int outLength = readHexStringLength(level, start, end); | 450 int outLength = readHexStringLength(level, start, end); |
451 // TODO(edisonn): optimize the allocation, don't allocate new string, but pu
t it in a preallocated buffer | 451 // TODO(edisonn): optimize the allocation, don't allocate new string, but pu
t it in a preallocated buffer |
452 unsigned char* out = (unsigned char*)allocator->alloc(outLength); | 452 unsigned char* out = (unsigned char*)allocator->alloc(outLength); |
453 start = readHexString(level, start, end, out); | 453 start = readHexString(level, start, end, out); |
454 SkPdfObject::makeHexString(out, out + outLength, str); | 454 SkPdfNativeObject::makeHexString(out, out + outLength, str); |
455 TRACE_HEXSTRING(out, out + outLength); | 455 TRACE_HEXSTRING(out, out + outLength); |
456 return start; // consumed already > at the end of the string | 456 return start; // consumed already > at the end of the string |
457 } | 457 } |
458 | 458 |
459 // TODO(edisonn): before PDF 1.2 name could not have special characters, add ver
sion parameter | 459 // TODO(edisonn): before PDF 1.2 name could not have special characters, add ver
sion parameter |
460 static const unsigned char* readName(int level, const unsigned char* start, cons
t unsigned char* end, unsigned char* out) { | 460 static const unsigned char* readName(int level, const unsigned char* start, cons
t unsigned char* end, unsigned char* out) { |
461 TRACE_INDENT(level, "Name"); | 461 TRACE_INDENT(level, "Name"); |
462 bool hasOut = (out != NULL); | 462 bool hasOut = (out != NULL); |
463 const unsigned char* in = start; | 463 const unsigned char* in = start; |
464 | 464 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 return in; | 561 return in; |
562 } else { | 562 } else { |
563 return start + (out - (const unsigned char*)NULL); // return where the s
tring would end if we reuse the string | 563 return start + (out - (const unsigned char*)NULL); // return where the s
tring would end if we reuse the string |
564 } | 564 } |
565 } | 565 } |
566 | 566 |
567 static int readNameLength(int level, const unsigned char* start, const unsigned
char* end) { | 567 static int readNameLength(int level, const unsigned char* start, const unsigned
char* end) { |
568 return readName(level, start, end, NULL) - start; | 568 return readName(level, start, end, NULL) - start; |
569 } | 569 } |
570 | 570 |
571 static const unsigned char* readName(int level, const unsigned char* start, cons
t unsigned char* end, SkPdfObject* name, SkPdfAllocator* allocator) { | 571 static const unsigned char* readName(int level, const unsigned char* start, cons
t unsigned char* end, SkPdfNativeObject* name, SkPdfAllocator* allocator) { |
572 if (!allocator) { | 572 if (!allocator) { |
573 return end; | 573 return end; |
574 } | 574 } |
575 int outLength = readNameLength(level, start, end); | 575 int outLength = readNameLength(level, start, end); |
576 // TODO(edisonn): optimize the allocation, don't allocate new string, but pu
t it in a preallocated buffer | 576 // TODO(edisonn): optimize the allocation, don't allocate new string, but pu
t it in a preallocated buffer |
577 unsigned char* out = (unsigned char*)allocator->alloc(outLength); | 577 unsigned char* out = (unsigned char*)allocator->alloc(outLength); |
578 start = readName(level, start, end, out); | 578 start = readName(level, start, end, out); |
579 SkPdfObject::makeName(out, out + outLength, name); | 579 SkPdfNativeObject::makeName(out, out + outLength, name); |
580 TRACE_NAME(out, out + outLength); | 580 TRACE_NAME(out, out + outLength); |
581 return start; | 581 return start; |
582 } | 582 } |
583 | 583 |
584 // TODO(edisonn): pdf spec let Length to be an indirect object define after the
stream | 584 // TODO(edisonn): pdf spec let Length to be an indirect object define after the
stream |
585 // that makes for an interesting scenario, where the stream itself contains ends
tream, together | 585 // that makes for an interesting scenario, where the stream itself contains ends
tream, together |
586 // with a reference object with the length, but the real length object would be
somewhere else | 586 // with a reference object with the length, but the real length object would be
somewhere else |
587 // it could confuse the parser | 587 // it could confuse the parser |
588 /*example: | 588 /*example: |
589 | 589 |
590 7 0 obj | 590 7 0 obj |
591 << /length 8 0 R>> | 591 << /length 8 0 R>> |
592 stream | 592 stream |
593 ............... | 593 ............... |
594 endstream | 594 endstream |
595 8 0 obj #we are in stream actually, not a real object | 595 8 0 obj #we are in stream actually, not a real object |
596 << 10 >> #we are in stream actually, not a real object | 596 << 10 >> #we are in stream actually, not a real object |
597 endobj | 597 endobj |
598 endstream | 598 endstream |
599 8 0 obj #real obj | 599 8 0 obj #real obj |
600 << 100 >> #real obj | 600 << 100 >> #real obj |
601 endobj | 601 endobj |
602 and it could get worse, with multiple object like this | 602 and it could get worse, with multiple object like this |
603 */ | 603 */ |
604 | 604 |
605 // right now implement the silly algorithm that assumes endstream is finishing t
he stream | 605 // right now implement the silly algorithm that assumes endstream is finishing t
he stream |
606 | 606 |
607 | 607 |
608 static const unsigned char* readStream(int level, const unsigned char* start, co
nst unsigned char* end, SkPdfObject* dict, SkNativeParsedPDF* doc) { | 608 static const unsigned char* readStream(int level, const unsigned char* start, co
nst unsigned char* end, SkPdfNativeObject* dict, SkPdfNativeDoc* doc) { |
609 TRACE_INDENT(level, "Stream"); | 609 TRACE_INDENT(level, "Stream"); |
610 start = skipPdfWhiteSpaces(level, start, end); | 610 start = skipPdfWhiteSpaces(level, start, end); |
611 if (!(start[0] == 's' && start[1] == 't' && start[2] == 'r' && start[3] == '
e' && start[4] == 'a' && start[5] == 'm')) { | 611 if (!(start[0] == 's' && start[1] == 't' && start[2] == 'r' && start[3] == '
e' && start[4] == 'a' && start[5] == 'm')) { |
612 // no stream. return. | 612 // no stream. return. |
613 return start; | 613 return start; |
614 } | 614 } |
615 | 615 |
616 start += 6; // strlen("stream") | 616 start += 6; // strlen("stream") |
617 if (start[0] == kCR_PdfWhiteSpace && start[1] == kLF_PdfWhiteSpace) { | 617 if (start[0] == kCR_PdfWhiteSpace && start[1] == kLF_PdfWhiteSpace) { |
618 start += 2; | 618 start += 2; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 // TODO(edisonn): verify the next bytes are "endstream" | 675 // TODO(edisonn): verify the next bytes are "endstream" |
676 | 676 |
677 endstream += strlen("endstream"); | 677 endstream += strlen("endstream"); |
678 // TODO(edisonn): Assert? report error/warning? | 678 // TODO(edisonn): Assert? report error/warning? |
679 dict->addStream(start, (size_t)length); | 679 dict->addStream(start, (size_t)length); |
680 return endstream; | 680 return endstream; |
681 } | 681 } |
682 return start; | 682 return start; |
683 } | 683 } |
684 | 684 |
685 static const unsigned char* readInlineImageStream(int level, const unsigned char
* start, const unsigned char* end, SkPdfImageDictionary* inlineImage, SkNativePa
rsedPDF* doc) { | 685 static const unsigned char* readInlineImageStream(int level, const unsigned char
* start, const unsigned char* end, SkPdfImageDictionary* inlineImage, SkPdfNativ
eDoc* doc) { |
686 TRACE_INDENT(level, "Inline Image"); | 686 TRACE_INDENT(level, "Inline Image"); |
687 // We already processed ID keyword, and we should be positioned immediately
after it | 687 // We already processed ID keyword, and we should be positioned immediately
after it |
688 | 688 |
689 // TODO(edisonn): security: read after end check, or make buffers with extra
2 bytes | 689 // TODO(edisonn): security: read after end check, or make buffers with extra
2 bytes |
690 if (start[0] == kCR_PdfWhiteSpace && start[1] == kLF_PdfWhiteSpace) { | 690 if (start[0] == kCR_PdfWhiteSpace && start[1] == kLF_PdfWhiteSpace) { |
691 start += 2; | 691 start += 2; |
692 } else if (start[0] == kLF_PdfWhiteSpace) { | 692 } else if (start[0] == kLF_PdfWhiteSpace) { |
693 start += 1; | 693 start += 1; |
694 } else if (isPdfWhiteSpace(start[0])) { | 694 } else if (isPdfWhiteSpace(start[0])) { |
695 start += 1; | 695 start += 1; |
(...skipping 11 matching lines...) Expand all Loading... |
707 if (*(endstream-2) == kCR_PdfWhiteSpace) length--; | 707 if (*(endstream-2) == kCR_PdfWhiteSpace) length--; |
708 inlineImage->addStream(start, (size_t)length); | 708 inlineImage->addStream(start, (size_t)length); |
709 } else { | 709 } else { |
710 // TODO(edisonn): report error in inline image stream (ID-EI) section | 710 // TODO(edisonn): report error in inline image stream (ID-EI) section |
711 // TODO(edisonn): based on filter, try to ignore a missing EI, and read
data properly | 711 // TODO(edisonn): based on filter, try to ignore a missing EI, and read
data properly |
712 return end; | 712 return end; |
713 } | 713 } |
714 return endEI; | 714 return endEI; |
715 } | 715 } |
716 | 716 |
717 static const unsigned char* readDictionary(int level, const unsigned char* start
, const unsigned char* end, SkPdfObject* dict, SkPdfAllocator* allocator, SkNati
veParsedPDF* doc) { | 717 static const unsigned char* readDictionary(int level, const unsigned char* start
, const unsigned char* end, SkPdfNativeObject* dict, SkPdfAllocator* allocator,
SkPdfNativeDoc* doc) { |
718 if (allocator == NULL) { | 718 if (allocator == NULL) { |
719 // TODO(edisonn): report/warning error | 719 // TODO(edisonn): report/warning error |
720 return end; | 720 return end; |
721 } | 721 } |
722 TRACE_INDENT(level, "Dictionary"); | 722 TRACE_INDENT(level, "Dictionary"); |
723 SkPdfObject::makeEmptyDictionary(dict); | 723 SkPdfNativeObject::makeEmptyDictionary(dict); |
724 | 724 |
725 start = skipPdfWhiteSpaces(level, start, end); | 725 start = skipPdfWhiteSpaces(level, start, end); |
726 SkPdfAllocator tmpStorage; // keys will be stored in dict, we can free them
immediately after set. | 726 SkPdfAllocator tmpStorage; // keys will be stored in dict, we can free them
immediately after set. |
727 | 727 |
728 while (start < end && *start == kNamed_PdfDelimiter) { | 728 while (start < end && *start == kNamed_PdfDelimiter) { |
729 SkPdfObject key; | 729 SkPdfNativeObject key; |
730 //*start = '\0'; | 730 //*start = '\0'; |
731 start++; | 731 start++; |
732 start = readName(level + 1, start, end, &key, &tmpStorage); | 732 start = readName(level + 1, start, end, &key, &tmpStorage); |
733 start = skipPdfWhiteSpaces(level + 1, start, end); | 733 start = skipPdfWhiteSpaces(level + 1, start, end); |
734 | 734 |
735 if (start < end) { | 735 if (start < end) { |
736 SkPdfObject* value = allocator->allocObject(); | 736 SkPdfNativeObject* value = allocator->allocObject(); |
737 start = nextObject(level + 1, start, end, value, allocator, doc); | 737 start = nextObject(level + 1, start, end, value, allocator, doc); |
738 | 738 |
739 start = skipPdfWhiteSpaces(level + 1, start, end); | 739 start = skipPdfWhiteSpaces(level + 1, start, end); |
740 | 740 |
741 if (start < end) { | 741 if (start < end) { |
742 // seems we have an indirect reference | 742 // seems we have an indirect reference |
743 if (isPdfDigit(*start)) { | 743 if (isPdfDigit(*start)) { |
744 SkPdfObject generation; | 744 SkPdfNativeObject generation; |
745 start = nextObject(level + 1, start, end, &generation, alloc
ator, doc); | 745 start = nextObject(level + 1, start, end, &generation, alloc
ator, doc); |
746 | 746 |
747 SkPdfObject keywordR; | 747 SkPdfNativeObject keywordR; |
748 start = nextObject(level + 1, start, end, &keywordR, allocat
or, doc); | 748 start = nextObject(level + 1, start, end, &keywordR, allocat
or, doc); |
749 | 749 |
750 if (value->isInteger() && generation.isInteger() && keywordR
.isKeywordReference()) { | 750 if (value->isInteger() && generation.isInteger() && keywordR
.isKeywordReference()) { |
751 int64_t id = value->intValue(); | 751 int64_t id = value->intValue(); |
752 value->reset(); | 752 value->reset(); |
753 SkPdfObject::makeReference((unsigned int)id, (unsigned i
nt)generation.intValue(), value); | 753 SkPdfNativeObject::makeReference((unsigned int)id, (unsi
gned int)generation.intValue(), value); |
754 dict->set(&key, value); | 754 dict->set(&key, value); |
755 } else { | 755 } else { |
756 // error, ignore | 756 // error, ignore |
757 dict->set(&key, value); | 757 dict->set(&key, value); |
758 } | 758 } |
759 } else { | 759 } else { |
760 // next elem is not a digit, but it might not be / either! | 760 // next elem is not a digit, but it might not be / either! |
761 dict->set(&key, value); | 761 dict->set(&key, value); |
762 } | 762 } |
763 } else { | 763 } else { |
764 // /key >> | 764 // /key >> |
765 dict->set(&key, value); | 765 dict->set(&key, value); |
766 return end; | 766 return end; |
767 } | 767 } |
768 start = skipPdfWhiteSpaces(level + 1, start, end); | 768 start = skipPdfWhiteSpaces(level + 1, start, end); |
769 } else { | 769 } else { |
770 dict->set(&key, &SkPdfObject::kNull); | 770 dict->set(&key, &SkPdfNativeObject::kNull); |
771 return end; | 771 return end; |
772 } | 772 } |
773 } | 773 } |
774 | 774 |
775 // TODO(edisonn): options to ignore these errors | 775 // TODO(edisonn): options to ignore these errors |
776 | 776 |
777 // now we should expect >> | 777 // now we should expect >> |
778 start = skipPdfWhiteSpaces(level, start, end); | 778 start = skipPdfWhiteSpaces(level, start, end); |
779 if (*start != kClosedInequityBracket_PdfDelimiter) { | 779 if (*start != kClosedInequityBracket_PdfDelimiter) { |
780 // TODO(edisonn): report/warning | 780 // TODO(edisonn): report/warning |
781 } | 781 } |
782 //*start = '\0'; | 782 //*start = '\0'; |
783 start++; // skip > | 783 start++; // skip > |
784 if (*start != kClosedInequityBracket_PdfDelimiter) { | 784 if (*start != kClosedInequityBracket_PdfDelimiter) { |
785 // TODO(edisonn): report/warning | 785 // TODO(edisonn): report/warning |
786 } | 786 } |
787 //*start = '\0'; | 787 //*start = '\0'; |
788 start++; // skip > | 788 start++; // skip > |
789 | 789 |
790 start = readStream(level, start, end, dict, doc); | 790 start = readStream(level, start, end, dict, doc); |
791 | 791 |
792 return start; | 792 return start; |
793 } | 793 } |
794 | 794 |
795 const unsigned char* nextObject(int level, const unsigned char* start, const uns
igned char* end, SkPdfObject* token, SkPdfAllocator* allocator, SkNativeParsedPD
F* doc) { | 795 const unsigned char* nextObject(int level, const unsigned char* start, const uns
igned char* end, SkPdfNativeObject* token, SkPdfAllocator* allocator, SkPdfNativ
eDoc* doc) { |
796 const unsigned char* current; | 796 const unsigned char* current; |
797 | 797 |
798 // skip white spaces | 798 // skip white spaces |
799 start = skipPdfWhiteSpaces(level, start, end); | 799 start = skipPdfWhiteSpaces(level, start, end); |
800 | 800 |
801 current = endOfPdfToken(level, start, end); | 801 current = endOfPdfToken(level, start, end); |
802 | 802 |
803 // no token, len would be 0 | 803 // no token, len would be 0 |
804 if (current == start) { | 804 if (current == start) { |
805 return NULL; | 805 return NULL; |
806 } | 806 } |
807 | 807 |
808 int tokenLen = current - start; | 808 int tokenLen = current - start; |
809 | 809 |
810 if (tokenLen == 1) { | 810 if (tokenLen == 1) { |
811 // start array | 811 // start array |
812 switch (*start) { | 812 switch (*start) { |
813 case kOpenedSquareBracket_PdfDelimiter: | 813 case kOpenedSquareBracket_PdfDelimiter: |
814 //*start = '\0'; | 814 //*start = '\0'; |
815 SkPdfObject::makeEmptyArray(token); | 815 SkPdfNativeObject::makeEmptyArray(token); |
816 return readArray(level + 1, current, end, token, allocator, doc)
; | 816 return readArray(level + 1, current, end, token, allocator, doc)
; |
817 | 817 |
818 case kOpenedRoundBracket_PdfDelimiter: | 818 case kOpenedRoundBracket_PdfDelimiter: |
819 //*start = '\0'; | 819 //*start = '\0'; |
820 return readString(level, start + 1, end, token, allocator); | 820 return readString(level, start + 1, end, token, allocator); |
821 | 821 |
822 case kOpenedInequityBracket_PdfDelimiter: | 822 case kOpenedInequityBracket_PdfDelimiter: |
823 //*start = '\0'; | 823 //*start = '\0'; |
824 if (end > start + 1 && start[1] == kOpenedInequityBracket_PdfDel
imiter) { | 824 if (end > start + 1 && start[1] == kOpenedInequityBracket_PdfDel
imiter) { |
825 //start[1] = '\0'; // optional | 825 //start[1] = '\0'; // optional |
(...skipping 14 matching lines...) Expand all Loading... |
840 } | 840 } |
841 | 841 |
842 SkASSERT(!isPdfWhiteSpace(*start)); | 842 SkASSERT(!isPdfWhiteSpace(*start)); |
843 if (isPdfDelimiter(*start)) { | 843 if (isPdfDelimiter(*start)) { |
844 // TODO(edisonn): how stream ] } > ) will be handled? | 844 // TODO(edisonn): how stream ] } > ) will be handled? |
845 // for now ignore, and it will become a keyword to be ignored | 845 // for now ignore, and it will become a keyword to be ignored |
846 } | 846 } |
847 } | 847 } |
848 | 848 |
849 if (tokenLen == 4 && start[0] == 'n' && start[1] == 'u' && start[2] == 'l' &
& start[3] == 'l') { | 849 if (tokenLen == 4 && start[0] == 'n' && start[1] == 'u' && start[2] == 'l' &
& start[3] == 'l') { |
850 SkPdfObject::makeNull(token); | 850 SkPdfNativeObject::makeNull(token); |
851 return current; | 851 return current; |
852 } | 852 } |
853 | 853 |
854 if (tokenLen == 4 && start[0] == 't' && start[1] == 'r' && start[2] == 'u' &
& start[3] == 'e') { | 854 if (tokenLen == 4 && start[0] == 't' && start[1] == 'r' && start[2] == 'u' &
& start[3] == 'e') { |
855 SkPdfObject::makeBoolean(true, token); | 855 SkPdfNativeObject::makeBoolean(true, token); |
856 return current; | 856 return current; |
857 } | 857 } |
858 | 858 |
859 if (tokenLen == 5 && start[0] == 'f' && start[1] == 'a' && start[2] == 'l' &
& start[3] == 's' && start[4] == 'e') { | 859 if (tokenLen == 5 && start[0] == 'f' && start[1] == 'a' && start[2] == 'l' &
& start[3] == 's' && start[4] == 'e') { |
860 SkPdfObject::makeBoolean(false, token); | 860 SkPdfNativeObject::makeBoolean(false, token); |
861 return current; | 861 return current; |
862 } | 862 } |
863 | 863 |
864 if (isPdfNumeric(*start)) { | 864 if (isPdfNumeric(*start)) { |
865 SkPdfObject::makeNumeric(start, current, token); | 865 SkPdfNativeObject::makeNumeric(start, current, token); |
866 } else { | 866 } else { |
867 SkPdfObject::makeKeyword(start, current, token); | 867 SkPdfNativeObject::makeKeyword(start, current, token); |
868 } | 868 } |
869 return current; | 869 return current; |
870 } | 870 } |
871 | 871 |
872 SkPdfObject* SkPdfAllocator::allocBlock() { | 872 SkPdfNativeObject* SkPdfAllocator::allocBlock() { |
873 fSizeInBytes += BUFFER_SIZE * sizeof(SkPdfObject); | 873 fSizeInBytes += BUFFER_SIZE * sizeof(SkPdfNativeObject); |
874 return new SkPdfObject[BUFFER_SIZE]; | 874 return new SkPdfNativeObject[BUFFER_SIZE]; |
875 } | 875 } |
876 | 876 |
877 SkPdfAllocator::~SkPdfAllocator() { | 877 SkPdfAllocator::~SkPdfAllocator() { |
878 for (int i = 0 ; i < fHandles.count(); i++) { | 878 for (int i = 0 ; i < fHandles.count(); i++) { |
879 free(fHandles[i]); | 879 free(fHandles[i]); |
880 } | 880 } |
881 for (int i = 0 ; i < fHistory.count(); i++) { | 881 for (int i = 0 ; i < fHistory.count(); i++) { |
882 for (int j = 0 ; j < BUFFER_SIZE; j++) { | 882 for (int j = 0 ; j < BUFFER_SIZE; j++) { |
883 fHistory[i][j].reset(); | 883 fHistory[i][j].reset(); |
884 } | 884 } |
885 delete[] fHistory[i]; | 885 delete[] fHistory[i]; |
886 } | 886 } |
887 for (int j = 0 ; j < BUFFER_SIZE; j++) { | 887 for (int j = 0 ; j < BUFFER_SIZE; j++) { |
888 fCurrent[j].reset(); | 888 fCurrent[j].reset(); |
889 } | 889 } |
890 delete[] fCurrent; | 890 delete[] fCurrent; |
891 } | 891 } |
892 | 892 |
893 SkPdfObject* SkPdfAllocator::allocObject() { | 893 SkPdfNativeObject* SkPdfAllocator::allocObject() { |
894 if (fCurrentUsed >= BUFFER_SIZE) { | 894 if (fCurrentUsed >= BUFFER_SIZE) { |
895 fHistory.push(fCurrent); | 895 fHistory.push(fCurrent); |
896 fCurrent = allocBlock(); | 896 fCurrent = allocBlock(); |
897 fCurrentUsed = 0; | 897 fCurrentUsed = 0; |
898 fSizeInBytes += sizeof(SkPdfObject*); | 898 fSizeInBytes += sizeof(SkPdfNativeObject*); |
899 } | 899 } |
900 fCurrentUsed++; | 900 fCurrentUsed++; |
901 return &fCurrent[fCurrentUsed - 1]; | 901 return &fCurrent[fCurrentUsed - 1]; |
902 } | 902 } |
903 | 903 |
904 // TODO(edisonn): perf: do no copy the buffers, but use them, and mark cache the
result, so there is no need of a second pass | 904 // TODO(edisonn): perf: do no copy the buffers, but use them, and mark cache the
result, so there is no need of a second pass |
905 SkPdfNativeTokenizer::SkPdfNativeTokenizer(SkPdfObject* objWithStream, const SkP
dfMapper* mapper, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) : fDoc(doc)
, fMapper(mapper), fAllocator(allocator), fUncompressedStream(NULL), fUncompress
edStreamEnd(NULL), fEmpty(false), fHasPutBack(false) { | 905 SkPdfNativeTokenizer::SkPdfNativeTokenizer(SkPdfNativeObject* objWithStream, con
st SkPdfMapper* mapper, SkPdfAllocator* allocator, SkPdfNativeDoc* doc) : fDoc(d
oc), fMapper(mapper), fAllocator(allocator), fUncompressedStream(NULL), fUncompr
essedStreamEnd(NULL), fEmpty(false), fHasPutBack(false) { |
906 const unsigned char* buffer = NULL; | 906 const unsigned char* buffer = NULL; |
907 size_t len = 0; | 907 size_t len = 0; |
908 objWithStream->GetFilteredStreamRef(&buffer, &len); | 908 objWithStream->GetFilteredStreamRef(&buffer, &len); |
909 // TODO(edisonn): hack, find end of object | 909 // TODO(edisonn): hack, find end of object |
910 char* endobj = strrstrk((char*)buffer, (char*)buffer + len, "endobj"); | 910 char* endobj = strrstrk((char*)buffer, (char*)buffer + len, "endobj"); |
911 if (endobj) { | 911 if (endobj) { |
912 len = endobj - (char*)buffer + strlen("endobj"); | 912 len = endobj - (char*)buffer + strlen("endobj"); |
913 } | 913 } |
914 fUncompressedStreamStart = fUncompressedStream = buffer; | 914 fUncompressedStreamStart = fUncompressedStream = buffer; |
915 fUncompressedStreamEnd = fUncompressedStream + len; | 915 fUncompressedStreamEnd = fUncompressedStream + len; |
916 } | 916 } |
917 | 917 |
918 SkPdfNativeTokenizer::SkPdfNativeTokenizer(const unsigned char* buffer, int len,
const SkPdfMapper* mapper, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) :
fDoc(doc), fMapper(mapper), fAllocator(allocator), fEmpty(false), fHasPutBack(f
alse) { | 918 SkPdfNativeTokenizer::SkPdfNativeTokenizer(const unsigned char* buffer, int len,
const SkPdfMapper* mapper, SkPdfAllocator* allocator, SkPdfNativeDoc* doc) : fD
oc(doc), fMapper(mapper), fAllocator(allocator), fEmpty(false), fHasPutBack(fals
e) { |
919 // TODO(edisonn): hack, find end of object | 919 // TODO(edisonn): hack, find end of object |
920 char* endobj = strrstrk((char*)buffer, (char*)buffer + len, "endobj"); | 920 char* endobj = strrstrk((char*)buffer, (char*)buffer + len, "endobj"); |
921 if (endobj) { | 921 if (endobj) { |
922 len = endobj - (char*)buffer + strlen("endobj"); | 922 len = endobj - (char*)buffer + strlen("endobj"); |
923 } | 923 } |
924 fUncompressedStreamStart = fUncompressedStream = buffer; | 924 fUncompressedStreamStart = fUncompressedStream = buffer; |
925 fUncompressedStreamEnd = fUncompressedStream + len; | 925 fUncompressedStreamEnd = fUncompressedStream + len; |
926 } | 926 } |
927 | 927 |
928 SkPdfNativeTokenizer::~SkPdfNativeTokenizer() { | 928 SkPdfNativeTokenizer::~SkPdfNativeTokenizer() { |
929 } | 929 } |
930 | 930 |
931 bool SkPdfNativeTokenizer::readTokenCore(PdfToken* token) { | 931 bool SkPdfNativeTokenizer::readTokenCore(PdfToken* token) { |
932 SkPdfObject obj; | 932 SkPdfNativeObject obj; |
933 #ifdef PDF_TRACE_READ_TOKEN | 933 #ifdef PDF_TRACE_READ_TOKEN |
934 static int read_op = 0; | 934 static int read_op = 0; |
935 #endif | 935 #endif |
936 token->fKeyword = NULL; | 936 token->fKeyword = NULL; |
937 token->fObject = NULL; | 937 token->fObject = NULL; |
938 | 938 |
939 fUncompressedStream = skipPdfWhiteSpaces(0, fUncompressedStream, fUncompress
edStreamEnd); | 939 fUncompressedStream = skipPdfWhiteSpaces(0, fUncompressedStream, fUncompress
edStreamEnd); |
940 if (fUncompressedStream >= fUncompressedStreamEnd) { | 940 if (fUncompressedStream >= fUncompressedStreamEnd) { |
941 return false; | 941 return false; |
942 } | 942 } |
943 | 943 |
944 fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedStream
End, &obj, fAllocator, fDoc); | 944 fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedStream
End, &obj, fAllocator, fDoc); |
945 | 945 |
946 // If it is a keyword, we will only get the pointer of the string | 946 // If it is a keyword, we will only get the pointer of the string |
947 if (obj.type() == SkPdfObject::kKeyword_PdfObjectType) { | 947 if (obj.type() == SkPdfNativeObject::kKeyword_PdfObjectType) { |
948 token->fKeyword = obj.c_str(); | 948 token->fKeyword = obj.c_str(); |
949 token->fKeywordLength = obj.lenstr(); | 949 token->fKeywordLength = obj.lenstr(); |
950 token->fType = kKeyword_TokenType; | 950 token->fType = kKeyword_TokenType; |
951 } else { | 951 } else { |
952 SkPdfObject* pobj = fAllocator->allocObject(); | 952 SkPdfNativeObject* pobj = fAllocator->allocObject(); |
953 *pobj = obj; | 953 *pobj = obj; |
954 token->fObject = pobj; | 954 token->fObject = pobj; |
955 token->fType = kObject_TokenType; | 955 token->fType = kObject_TokenType; |
956 } | 956 } |
957 | 957 |
958 #ifdef PDF_TRACE_READ_TOKEN | 958 #ifdef PDF_TRACE_READ_TOKEN |
959 read_op++; | 959 read_op++; |
960 #if 0 | 960 #if 0 |
961 if (548 == read_op) { | 961 if (548 == read_op) { |
962 printf("break;\n"); | 962 printf("break;\n"); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1020 DECLARE_PDF_NAME(ASCII85Decode); | 1020 DECLARE_PDF_NAME(ASCII85Decode); |
1021 DECLARE_PDF_NAME(LZWDecode); | 1021 DECLARE_PDF_NAME(LZWDecode); |
1022 DECLARE_PDF_NAME(FlateDecode); // PDF 1.2 | 1022 DECLARE_PDF_NAME(FlateDecode); // PDF 1.2 |
1023 DECLARE_PDF_NAME(RunLengthDecode); | 1023 DECLARE_PDF_NAME(RunLengthDecode); |
1024 DECLARE_PDF_NAME(CCITTFaxDecode); | 1024 DECLARE_PDF_NAME(CCITTFaxDecode); |
1025 DECLARE_PDF_NAME(DCTDecode); | 1025 DECLARE_PDF_NAME(DCTDecode); |
1026 | 1026 |
1027 #define HANDLE_NAME_ABBR(obj,longName,shortName) if (obj->isName(#shortName)) re
turn &longName; | 1027 #define HANDLE_NAME_ABBR(obj,longName,shortName) if (obj->isName(#shortName)) re
turn &longName; |
1028 | 1028 |
1029 | 1029 |
1030 static SkPdfObject* inlineImageKeyAbbreviationExpand(SkPdfObject* key) { | 1030 static SkPdfNativeObject* inlineImageKeyAbbreviationExpand(SkPdfNativeObject* ke
y) { |
1031 if (!key || !key->isName()) { | 1031 if (!key || !key->isName()) { |
1032 return key; | 1032 return key; |
1033 } | 1033 } |
1034 | 1034 |
1035 // TODO(edisonn): use autogenerated code! | 1035 // TODO(edisonn): use autogenerated code! |
1036 HANDLE_NAME_ABBR(key, BitsPerComponent, BPC); | 1036 HANDLE_NAME_ABBR(key, BitsPerComponent, BPC); |
1037 HANDLE_NAME_ABBR(key, ColorSpace, CS); | 1037 HANDLE_NAME_ABBR(key, ColorSpace, CS); |
1038 HANDLE_NAME_ABBR(key, Decode, D); | 1038 HANDLE_NAME_ABBR(key, Decode, D); |
1039 HANDLE_NAME_ABBR(key, DecodeParms, DP); | 1039 HANDLE_NAME_ABBR(key, DecodeParms, DP); |
1040 HANDLE_NAME_ABBR(key, Filter, F); | 1040 HANDLE_NAME_ABBR(key, Filter, F); |
1041 HANDLE_NAME_ABBR(key, Height, H); | 1041 HANDLE_NAME_ABBR(key, Height, H); |
1042 HANDLE_NAME_ABBR(key, ImageMask, IM); | 1042 HANDLE_NAME_ABBR(key, ImageMask, IM); |
1043 // HANDLE_NAME_ABBR(key, Intent, ); | 1043 // HANDLE_NAME_ABBR(key, Intent, ); |
1044 HANDLE_NAME_ABBR(key, Interpolate, I); | 1044 HANDLE_NAME_ABBR(key, Interpolate, I); |
1045 HANDLE_NAME_ABBR(key, Width, W); | 1045 HANDLE_NAME_ABBR(key, Width, W); |
1046 | 1046 |
1047 return key; | 1047 return key; |
1048 } | 1048 } |
1049 | 1049 |
1050 static SkPdfObject* inlineImageValueAbbreviationExpand(SkPdfObject* value) { | 1050 static SkPdfNativeObject* inlineImageValueAbbreviationExpand(SkPdfNativeObject*
value) { |
1051 if (!value || !value->isName()) { | 1051 if (!value || !value->isName()) { |
1052 return value; | 1052 return value; |
1053 } | 1053 } |
1054 | 1054 |
1055 // TODO(edisonn): use autogenerated code! | 1055 // TODO(edisonn): use autogenerated code! |
1056 HANDLE_NAME_ABBR(value, DeviceGray, G); | 1056 HANDLE_NAME_ABBR(value, DeviceGray, G); |
1057 HANDLE_NAME_ABBR(value, DeviceRGB, RGB); | 1057 HANDLE_NAME_ABBR(value, DeviceRGB, RGB); |
1058 HANDLE_NAME_ABBR(value, DeviceCMYK, CMYK); | 1058 HANDLE_NAME_ABBR(value, DeviceCMYK, CMYK); |
1059 HANDLE_NAME_ABBR(value, Indexed, I); | 1059 HANDLE_NAME_ABBR(value, Indexed, I); |
1060 HANDLE_NAME_ABBR(value, ASCIIHexDecode, AHx); | 1060 HANDLE_NAME_ABBR(value, ASCIIHexDecode, AHx); |
1061 HANDLE_NAME_ABBR(value, ASCII85Decode, A85); | 1061 HANDLE_NAME_ABBR(value, ASCII85Decode, A85); |
1062 HANDLE_NAME_ABBR(value, LZWDecode, LZW); | 1062 HANDLE_NAME_ABBR(value, LZWDecode, LZW); |
1063 HANDLE_NAME_ABBR(value, FlateDecode, Fl); // (PDF 1.2) | 1063 HANDLE_NAME_ABBR(value, FlateDecode, Fl); // (PDF 1.2) |
1064 HANDLE_NAME_ABBR(value, RunLengthDecode, RL); | 1064 HANDLE_NAME_ABBR(value, RunLengthDecode, RL); |
1065 HANDLE_NAME_ABBR(value, CCITTFaxDecode, CCF); | 1065 HANDLE_NAME_ABBR(value, CCITTFaxDecode, CCF); |
1066 HANDLE_NAME_ABBR(value, DCTDecode, DCT); | 1066 HANDLE_NAME_ABBR(value, DCTDecode, DCT); |
1067 | 1067 |
1068 return value; | 1068 return value; |
1069 } | 1069 } |
1070 | 1070 |
1071 SkPdfImageDictionary* SkPdfNativeTokenizer::readInlineImage() { | 1071 SkPdfImageDictionary* SkPdfNativeTokenizer::readInlineImage() { |
1072 // BI already processed | 1072 // BI already processed |
1073 fUncompressedStream = skipPdfWhiteSpaces(0, fUncompressedStream, fUncompress
edStreamEnd); | 1073 fUncompressedStream = skipPdfWhiteSpaces(0, fUncompressedStream, fUncompress
edStreamEnd); |
1074 if (fUncompressedStream >= fUncompressedStreamEnd) { | 1074 if (fUncompressedStream >= fUncompressedStreamEnd) { |
1075 return NULL; | 1075 return NULL; |
1076 } | 1076 } |
1077 | 1077 |
1078 SkPdfImageDictionary* inlineImage = (SkPdfImageDictionary*)fAllocator->alloc
Object(); | 1078 SkPdfImageDictionary* inlineImage = (SkPdfImageDictionary*)fAllocator->alloc
Object(); |
1079 SkPdfObject::makeEmptyDictionary(inlineImage); | 1079 SkPdfNativeObject::makeEmptyDictionary(inlineImage); |
1080 | 1080 |
1081 while (fUncompressedStream < fUncompressedStreamEnd) { | 1081 while (fUncompressedStream < fUncompressedStreamEnd) { |
1082 SkPdfObject* key = fAllocator->allocObject(); | 1082 SkPdfNativeObject* key = fAllocator->allocObject(); |
1083 fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedSt
reamEnd, key, fAllocator, fDoc); | 1083 fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedSt
reamEnd, key, fAllocator, fDoc); |
1084 | 1084 |
1085 if (key->isKeyword() && key->lenstr() == 2 && key->c_str()[0] == 'I' &&
key->c_str()[1] == 'D') { // ID | 1085 if (key->isKeyword() && key->lenstr() == 2 && key->c_str()[0] == 'I' &&
key->c_str()[1] == 'D') { // ID |
1086 fUncompressedStream = readInlineImageStream(0, fUncompressedStream,
fUncompressedStreamEnd, inlineImage, fDoc); | 1086 fUncompressedStream = readInlineImageStream(0, fUncompressedStream,
fUncompressedStreamEnd, inlineImage, fDoc); |
1087 return inlineImage; | 1087 return inlineImage; |
1088 } else { | 1088 } else { |
1089 SkPdfObject* obj = fAllocator->allocObject(); | 1089 SkPdfNativeObject* obj = fAllocator->allocObject(); |
1090 fUncompressedStream = nextObject(0, fUncompressedStream, fUncompress
edStreamEnd, obj, fAllocator, fDoc); | 1090 fUncompressedStream = nextObject(0, fUncompressedStream, fUncompress
edStreamEnd, obj, fAllocator, fDoc); |
1091 // TODO(edisonn): perf maybe we should not expand abreviation like t
his | 1091 // TODO(edisonn): perf maybe we should not expand abreviation like t
his |
1092 inlineImage->set(inlineImageKeyAbbreviationExpand(key), | 1092 inlineImage->set(inlineImageKeyAbbreviationExpand(key), |
1093 inlineImageValueAbbreviationExpand(obj)); | 1093 inlineImageValueAbbreviationExpand(obj)); |
1094 } | 1094 } |
1095 } | 1095 } |
1096 // TODO(edisonn): report end of data with inline image without an EI | 1096 // TODO(edisonn): report end of data with inline image without an EI |
1097 return inlineImage; | 1097 return inlineImage; |
1098 } | 1098 } |
OLD | NEW |