OLD | NEW |
1 #include "SkNativeParsedPDF.h" | 1 #include "SkNativeParsedPDF.h" |
2 #include "SkPdfNativeTokenizer.h" | 2 #include "SkPdfNativeTokenizer.h" |
3 #include "SkPdfBasics.h" | 3 #include "SkPdfBasics.h" |
4 #include "SkPdfObject.h" | 4 #include "SkPdfObject.h" |
5 | 5 |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <string.h> | 7 #include <string.h> |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 | 10 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 SkNativeParsedPDF* gDoc = NULL; | 61 SkNativeParsedPDF* gDoc = NULL; |
62 | 62 |
63 // TODO(edisonn): NYI | 63 // TODO(edisonn): NYI |
64 // TODO(edisonn): 3 constructuctors from URL, from stream, from file ... | 64 // TODO(edisonn): 3 constructuctors from URL, from stream, from file ... |
65 // TODO(edisonn): write one that accepts errors in the file and ignores/fixis th
em | 65 // TODO(edisonn): write one that accepts errors in the file and ignores/fixis th
em |
66 // TODO(edisonn): testing: | 66 // TODO(edisonn): testing: |
67 // 1) run on a lot of file | 67 // 1) run on a lot of file |
68 // 2) recoverable corupt file: remove endobj, endsteam, remove other keywords, u
se other white spaces, insert comments randomly, ... | 68 // 2) recoverable corupt file: remove endobj, endsteam, remove other keywords, u
se other white spaces, insert comments randomly, ... |
69 // 3) irrecoverable corrupt file | 69 // 3) irrecoverable corrupt file |
70 SkNativeParsedPDF::SkNativeParsedPDF(const char* path) : fAllocator(new SkPdfAll
ocator()) { | 70 SkNativeParsedPDF::SkNativeParsedPDF(const char* path) |
| 71 : fAllocator(new SkPdfAllocator()) |
| 72 , fRootCatalogRef(NULL) |
| 73 , fRootCatalog(NULL) { |
71 gDoc = this; | 74 gDoc = this; |
72 FILE* file = fopen(path, "r"); | 75 FILE* file = fopen(path, "r"); |
73 fContentLength = getFileSize(path); | 76 fContentLength = getFileSize(path); |
74 fFileContent = new unsigned char[fContentLength + 1]; | 77 fFileContent = new unsigned char[fContentLength + 1]; |
75 fread(fFileContent, fContentLength, 1, file); | 78 fread(fFileContent, fContentLength, 1, file); |
76 fFileContent[fContentLength] = '\0'; | 79 fFileContent[fContentLength] = '\0'; |
77 fclose(file); | 80 fclose(file); |
78 file = NULL; | 81 file = NULL; |
79 | 82 |
80 unsigned char* eofLine = lineHome(fFileContent, fFileContent + fContentLengt
h - 1); | 83 unsigned char* eofLine = lineHome(fFileContent, fFileContent + fContentLengt
h - 1); |
81 unsigned char* xrefByteOffsetLine = previousLineHome(fFileContent, eofLine); | 84 unsigned char* xrefByteOffsetLine = previousLineHome(fFileContent, eofLine); |
82 unsigned char* xrefstartKeywordLine = previousLineHome(fFileContent, xrefByt
eOffsetLine); | 85 unsigned char* xrefstartKeywordLine = previousLineHome(fFileContent, xrefByt
eOffsetLine); |
83 | 86 |
84 if (strcmp((char*)xrefstartKeywordLine, "startxref") != 0) { | 87 if (strcmp((char*)xrefstartKeywordLine, "startxref") != 0) { |
85 // TODO(edisonn): report/issue | 88 // TODO(edisonn): report/issue |
86 } | 89 } |
87 | 90 |
88 long xrefByteOffset = atol((const char*)xrefByteOffsetLine); | 91 long xrefByteOffset = atol((const char*)xrefByteOffsetLine); |
89 | 92 |
90 bool storeCatalog = true; | 93 bool storeCatalog = true; |
91 while (xrefByteOffset >= 0) { | 94 while (xrefByteOffset >= 0) { |
92 unsigned char* trailerStart = readCrossReferenceSection(fFileContent + x
refByteOffset, xrefstartKeywordLine); | 95 unsigned char* trailerStart = readCrossReferenceSection(fFileContent + x
refByteOffset, xrefstartKeywordLine); |
93 xrefByteOffset = readTrailer(trailerStart, xrefstartKeywordLine, storeCa
talog); | 96 xrefByteOffset = readTrailer(trailerStart, xrefstartKeywordLine, storeCa
talog); |
94 storeCatalog = false; | 97 storeCatalog = false; |
95 } | 98 } |
96 | 99 |
97 // TODO(edisonn): warn/error expect fObjects[fRefCatalogId].fGeneration == f
RefCatalogGeneration | 100 // TODO(edisonn): warn/error expect fObjects[fRefCatalogId].fGeneration == f
RefCatalogGeneration |
98 // TODO(edisonn): security, verify that SkPdfCatalogDictionary is indeed usi
ng mapper | 101 // TODO(edisonn): security, verify that SkPdfCatalogDictionary is indeed usi
ng mapper |
99 // load catalog | 102 // load catalog |
100 fRootCatalog = (SkPdfCatalogDictionary*)resolveReference(fRootCatalogRef); | |
101 SkPdfPageTreeNodeDictionary* tree = fRootCatalog->Pages(this); | |
102 | 103 |
103 fillPages(tree); | 104 if (fRootCatalogRef) { |
| 105 fRootCatalog = (SkPdfCatalogDictionary*)resolveReference(fRootCatalogRef
); |
| 106 SkPdfPageTreeNodeDictionary* tree = fRootCatalog->Pages(this); |
| 107 |
| 108 fillPages(tree); |
| 109 } else { |
| 110 // TODO(edisonn): corrupted pdf, read it from beginning and rebuild (xre
f, trailer, or just reall all objects) |
| 111 // 0 pages |
| 112 } |
104 | 113 |
105 // now actually read all objects if we want, or do it lazyly | 114 // now actually read all objects if we want, or do it lazyly |
106 // and resolve references?... or not ... | 115 // and resolve references?... or not ... |
107 } | 116 } |
108 | 117 |
109 // TODO(edisonn): NYI | 118 // TODO(edisonn): NYI |
110 SkNativeParsedPDF::~SkNativeParsedPDF() { | 119 SkNativeParsedPDF::~SkNativeParsedPDF() { |
111 delete[] fFileContent; | 120 delete[] fFileContent; |
112 delete fAllocator; | 121 delete fAllocator; |
113 } | 122 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 } | 173 } |
165 // TODO(edisonn): it should never get here? there is no trailer? | 174 // TODO(edisonn): it should never get here? there is no trailer? |
166 return current; | 175 return current; |
167 } | 176 } |
168 | 177 |
169 long SkNativeParsedPDF::readTrailer(unsigned char* trailerStart, unsigned char*
trailerEnd, bool storeCatalog) { | 178 long SkNativeParsedPDF::readTrailer(unsigned char* trailerStart, unsigned char*
trailerEnd, bool storeCatalog) { |
170 unsigned char* current = ignoreLine(trailerStart, trailerEnd); // TODO(edis
onn): verify next keyord is "trailer" use nextObject here | 179 unsigned char* current = ignoreLine(trailerStart, trailerEnd); // TODO(edis
onn): verify next keyord is "trailer" use nextObject here |
171 | 180 |
172 SkPdfObject token; | 181 SkPdfObject token; |
173 current = nextObject(current, trailerEnd, &token, fAllocator); | 182 current = nextObject(current, trailerEnd, &token, fAllocator); |
| 183 if (!token.isDictionary()) { |
| 184 return -1; |
| 185 } |
174 SkPdfFileTrailerDictionary* trailer = (SkPdfFileTrailerDictionary*)&token; | 186 SkPdfFileTrailerDictionary* trailer = (SkPdfFileTrailerDictionary*)&token; |
| 187 if (!trailer->valid()) { |
| 188 return -1; |
| 189 } |
175 | 190 |
176 if (storeCatalog) { | 191 if (storeCatalog) { |
177 const SkPdfObject* ref = trailer->Root(NULL); | 192 const SkPdfObject* ref = trailer->Root(NULL); |
178 if (ref == NULL || !ref->isReference()) { | 193 if (ref == NULL || !ref->isReference()) { |
179 // TODO(edisonn): oops, we have to fix the corrup pdf file | 194 // TODO(edisonn): oops, we have to fix the corrup pdf file |
180 return -1; | 195 return -1; |
181 } | 196 } |
182 fRootCatalogRef = ref; | 197 fRootCatalogRef = ref; |
183 } | 198 } |
184 | 199 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 } else { | 410 } else { |
396 fObjects[id].fResolvedReference = resolveReference(fObjects[id].
fObj); | 411 fObjects[id].fResolvedReference = resolveReference(fObjects[id].
fObj); |
397 } | 412 } |
398 } | 413 } |
399 | 414 |
400 return fObjects[id].fResolvedReference; | 415 return fObjects[id].fResolvedReference; |
401 } | 416 } |
402 // TODO(edisonn): fix the mess with const, probably we need to remove it pre
tty much everywhere | 417 // TODO(edisonn): fix the mess with const, probably we need to remove it pre
tty much everywhere |
403 return (SkPdfObject*)ref; | 418 return (SkPdfObject*)ref; |
404 } | 419 } |
OLD | NEW |