edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1 | #include "SkNativeParsedPDF.h" |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2 | #include "SkPdfNativeTokenizer.h" |
| 3 | #include "SkPdfBasics.h" |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 4 | #include "SkPdfObject.h" |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 5 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 10 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 11 | #include "SkPdfFileTrailerDictionary_autogen.h" |
| 12 | #include "SkPdfCatalogDictionary_autogen.h" |
| 13 | #include "SkPdfPageObjectDictionary_autogen.h" |
| 14 | #include "SkPdfPageTreeNodeDictionary_autogen.h" |
| 15 | #include "SkPdfMapper_autogen.h" |
| 16 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 18 | |
| 19 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 20 | static long getFileSize(const char* filename) |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 21 | { |
| 22 | struct stat stat_buf; |
| 23 | int rc = stat(filename, &stat_buf); |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 24 | return rc == 0 ? (long)stat_buf.st_size : -1; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 25 | } |
| 26 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 27 | static const unsigned char* lineHome(const unsigned char* start, const unsigned char* current) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 28 | while (current > start && !isPdfEOL(*(current - 1))) { |
| 29 | current--; |
| 30 | } |
| 31 | return current; |
| 32 | } |
| 33 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 34 | static const unsigned char* previousLineHome(const unsigned char* start, const unsigned char* current) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 35 | if (current > start && isPdfEOL(*(current - 1))) { |
| 36 | current--; |
| 37 | } |
| 38 | |
| 39 | // allows CR+LF, LF+CR but not two CR+CR or LF+LF |
| 40 | if (current > start && isPdfEOL(*(current - 1)) && *current != *(current - 1)) { |
| 41 | current--; |
| 42 | } |
| 43 | |
| 44 | while (current > start && !isPdfEOL(*(current - 1))) { |
| 45 | current--; |
| 46 | } |
| 47 | |
| 48 | return current; |
| 49 | } |
| 50 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 51 | static const unsigned char* ignoreLine(const unsigned char* current, const unsigned char* end) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 52 | while (current < end && !isPdfEOL(*current)) { |
| 53 | current++; |
| 54 | } |
| 55 | current++; |
| 56 | if (current < end && isPdfEOL(*current) && *current != *(current - 1)) { |
| 57 | current++; |
| 58 | } |
| 59 | return current; |
| 60 | } |
| 61 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 62 | SkNativeParsedPDF* gDoc = NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 63 | |
| 64 | // TODO(edisonn): NYI |
| 65 | // TODO(edisonn): 3 constructuctors from URL, from stream, from file ... |
| 66 | // TODO(edisonn): write one that accepts errors in the file and ignores/fixis them |
| 67 | // TODO(edisonn): testing: |
| 68 | // 1) run on a lot of file |
| 69 | // 2) recoverable corupt file: remove endobj, endsteam, remove other keywords, use other white spaces, insert comments randomly, ... |
| 70 | // 3) irrecoverable corrupt file |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 71 | |
| 72 | SkNativeParsedPDF::SkNativeParsedPDF(SkStream* stream) |
| 73 | : fAllocator(new SkPdfAllocator()) |
| 74 | , fFileContent(NULL) |
| 75 | , fContentLength(0) |
| 76 | , fRootCatalogRef(NULL) |
| 77 | , fRootCatalog(NULL) { |
| 78 | size_t size = stream->getLength(); |
| 79 | void* ptr = sk_malloc_throw(size); |
| 80 | stream->read(ptr, size); |
| 81 | |
| 82 | init(ptr, size); |
| 83 | } |
| 84 | |
edisonn@google.com | 432640a | 2013-07-10 22:53:40 +0000 | [diff] [blame] | 85 | SkNativeParsedPDF::SkNativeParsedPDF(const char* path) |
| 86 | : fAllocator(new SkPdfAllocator()) |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 87 | , fFileContent(NULL) |
| 88 | , fContentLength(0) |
edisonn@google.com | 432640a | 2013-07-10 22:53:40 +0000 | [diff] [blame] | 89 | , fRootCatalogRef(NULL) |
| 90 | , fRootCatalog(NULL) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 91 | gDoc = this; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 92 | FILE* file = fopen(path, "r"); |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 93 | size_t size = getFileSize(path); |
| 94 | void* content = sk_malloc_throw(size); |
| 95 | bool ok = (0 != fread(content, size, 1, file)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 96 | fclose(file); |
| 97 | file = NULL; |
| 98 | |
edisonn@google.com | 620edc5 | 2013-07-18 13:03:03 +0000 | [diff] [blame] | 99 | if (!ok) { |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 100 | sk_free(content); |
edisonn@google.com | 620edc5 | 2013-07-18 13:03:03 +0000 | [diff] [blame] | 101 | // TODO(edisonn): report read error |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 102 | // TODO(edisonn): not nice to return like this from constructor, create a static |
| 103 | // function that can report NULL for failures. |
edisonn@google.com | 620edc5 | 2013-07-18 13:03:03 +0000 | [diff] [blame] | 104 | return; // Doc will have 0 pages |
| 105 | } |
| 106 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 107 | init(content, size); |
| 108 | } |
| 109 | |
| 110 | void SkNativeParsedPDF::init(const void* bytes, size_t length) { |
| 111 | fFileContent = (const unsigned char*)bytes; |
| 112 | fContentLength = length; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 113 | const unsigned char* eofLine = lineHome(fFileContent, fFileContent + fContentLength - 1); |
| 114 | const unsigned char* xrefByteOffsetLine = previousLineHome(fFileContent, eofLine); |
| 115 | const unsigned char* xrefstartKeywordLine = previousLineHome(fFileContent, xrefByteOffsetLine); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 116 | |
| 117 | if (strcmp((char*)xrefstartKeywordLine, "startxref") != 0) { |
| 118 | // TODO(edisonn): report/issue |
| 119 | } |
| 120 | |
| 121 | long xrefByteOffset = atol((const char*)xrefByteOffsetLine); |
| 122 | |
| 123 | bool storeCatalog = true; |
| 124 | while (xrefByteOffset >= 0) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 125 | const unsigned char* trailerStart = readCrossReferenceSection(fFileContent + xrefByteOffset, xrefstartKeywordLine); |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame^] | 126 | xrefByteOffset = -1; |
| 127 | if (trailerStart < xrefstartKeywordLine) { |
| 128 | readTrailer(trailerStart, xrefstartKeywordLine, storeCatalog, &xrefByteOffset, false); |
| 129 | storeCatalog = false; |
| 130 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // TODO(edisonn): warn/error expect fObjects[fRefCatalogId].fGeneration == fRefCatalogGeneration |
| 134 | // TODO(edisonn): security, verify that SkPdfCatalogDictionary is indeed using mapper |
| 135 | // load catalog |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 136 | |
edisonn@google.com | 432640a | 2013-07-10 22:53:40 +0000 | [diff] [blame] | 137 | if (fRootCatalogRef) { |
| 138 | fRootCatalog = (SkPdfCatalogDictionary*)resolveReference(fRootCatalogRef); |
edisonn@google.com | 8bad737 | 2013-07-10 23:36:56 +0000 | [diff] [blame] | 139 | if (fRootCatalog->isDictionary() && fRootCatalog->valid()) { |
| 140 | SkPdfPageTreeNodeDictionary* tree = fRootCatalog->Pages(this); |
| 141 | if (tree && tree->isDictionary() && tree->valid()) { |
| 142 | fillPages(tree); |
| 143 | } |
| 144 | } |
edisonn@google.com | 432640a | 2013-07-10 22:53:40 +0000 | [diff] [blame] | 145 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 146 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 147 | // TODO(edisonn): clean up this doc, or better, let the caller call again and build a new doc |
| 148 | // caller should be a static function. |
| 149 | if (pages() == 0) { |
| 150 | loadWithoutXRef(); |
| 151 | } |
| 152 | |
edisonn@google.com | 8bad737 | 2013-07-10 23:36:56 +0000 | [diff] [blame] | 153 | // TODO(edisonn): corrupted pdf, read it from beginning and rebuild (xref, trailer, or just reall all objects) |
| 154 | // 0 pages |
| 155 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 156 | // now actually read all objects if we want, or do it lazyly |
| 157 | // and resolve references?... or not ... |
| 158 | } |
| 159 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 160 | void SkNativeParsedPDF::loadWithoutXRef() { |
| 161 | const unsigned char* current = fFileContent; |
| 162 | const unsigned char* end = fFileContent + fContentLength; |
| 163 | |
| 164 | // TODO(edisonn): read pdf version |
| 165 | current = ignoreLine(current, end); |
| 166 | |
| 167 | current = skipPdfWhiteSpaces(0, current, end); |
| 168 | while (current < end) { |
| 169 | SkPdfObject token; |
| 170 | current = nextObject(0, current, end, &token, NULL, NULL); |
| 171 | if (token.isInteger()) { |
| 172 | int id = (int)token.intValue(); |
| 173 | |
| 174 | token.reset(); |
| 175 | current = nextObject(0, current, end, &token, NULL, NULL); |
| 176 | // int generation = (int)token.intValue(); // TODO(edisonn): ignored for now |
| 177 | |
| 178 | token.reset(); |
| 179 | current = nextObject(0, current, end, &token, NULL, NULL); |
| 180 | // TODO(edisonn): must be obj, return error if not? ignore ? |
| 181 | if (!token.isKeyword("obj")) { |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | while (fObjects.count() < id + 1) { |
| 186 | reset(fObjects.append()); |
| 187 | } |
| 188 | |
| 189 | fObjects[id].fOffset = current - fFileContent; |
| 190 | |
| 191 | SkPdfObject* obj = fAllocator->allocObject(); |
| 192 | current = nextObject(0, current, end, obj, fAllocator, this); |
| 193 | |
| 194 | fObjects[id].fResolvedReference = obj; |
| 195 | fObjects[id].fObj = obj; |
| 196 | |
| 197 | // set objects |
| 198 | } else if (token.isKeyword("trailer")) { |
| 199 | long dummy; |
| 200 | current = readTrailer(current, end, true, &dummy, true); |
| 201 | } else if (token.isKeyword("startxref")) { |
| 202 | token.reset(); |
| 203 | current = nextObject(0, current, end, &token, NULL, NULL); // ignore |
| 204 | } |
| 205 | |
| 206 | current = skipPdfWhiteSpaces(0, current, end); |
| 207 | } |
| 208 | |
| 209 | if (fRootCatalogRef) { |
| 210 | fRootCatalog = (SkPdfCatalogDictionary*)resolveReference(fRootCatalogRef); |
| 211 | if (fRootCatalog->isDictionary() && fRootCatalog->valid()) { |
| 212 | SkPdfPageTreeNodeDictionary* tree = fRootCatalog->Pages(this); |
| 213 | if (tree && tree->isDictionary() && tree->valid()) { |
| 214 | fillPages(tree); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 221 | // TODO(edisonn): NYI |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 222 | SkNativeParsedPDF::~SkNativeParsedPDF() { |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 223 | sk_free((void*)fFileContent); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 224 | delete fAllocator; |
| 225 | } |
| 226 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 227 | const unsigned char* SkNativeParsedPDF::readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd) { |
| 228 | const unsigned char* current = ignoreLine(xrefStart, trailerEnd); // TODO(edisonn): verify next keyord is "xref", use nextObject here |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 229 | |
| 230 | SkPdfObject token; |
| 231 | while (current < trailerEnd) { |
| 232 | token.reset(); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 233 | const unsigned char* previous = current; |
| 234 | current = nextObject(0, current, trailerEnd, &token, NULL, NULL); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 235 | if (!token.isInteger()) { |
| 236 | return previous; |
| 237 | } |
| 238 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 239 | int startId = (int)token.intValue(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 240 | token.reset(); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 241 | current = nextObject(0, current, trailerEnd, &token, NULL, NULL); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 242 | |
| 243 | if (!token.isInteger()) { |
| 244 | // TODO(edisonn): report/warning |
| 245 | return current; |
| 246 | } |
| 247 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 248 | int entries = (int)token.intValue(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 249 | |
| 250 | for (int i = 0; i < entries; i++) { |
| 251 | token.reset(); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 252 | current = nextObject(0, current, trailerEnd, &token, NULL, NULL); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 253 | if (!token.isInteger()) { |
| 254 | // TODO(edisonn): report/warning |
| 255 | return current; |
| 256 | } |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 257 | int offset = (int)token.intValue(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 258 | |
| 259 | token.reset(); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 260 | current = nextObject(0, current, trailerEnd, &token, NULL, NULL); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 261 | if (!token.isInteger()) { |
| 262 | // TODO(edisonn): report/warning |
| 263 | return current; |
| 264 | } |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 265 | int generation = (int)token.intValue(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 266 | |
| 267 | token.reset(); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 268 | current = nextObject(0, current, trailerEnd, &token, NULL, NULL); |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 269 | if (!token.isKeyword() || token.lenstr() != 1 || (*token.c_str() != 'f' && *token.c_str() != 'n')) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 270 | // TODO(edisonn): report/warning |
| 271 | return current; |
| 272 | } |
| 273 | |
| 274 | addCrossSectionInfo(startId + i, generation, offset, *token.c_str() == 'f'); |
| 275 | } |
| 276 | } |
| 277 | // TODO(edisonn): it should never get here? there is no trailer? |
| 278 | return current; |
| 279 | } |
| 280 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 281 | const unsigned char* SkNativeParsedPDF::readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKeyword) { |
| 282 | *prev = -1; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 283 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 284 | const unsigned char* current = trailerStart; |
| 285 | if (!skipKeyword) { |
| 286 | SkPdfObject trailerKeyword; |
| 287 | // TODO(edisonn): use null allocator, and let it just fail if memory |
| 288 | // needs allocated (but no crash)! |
| 289 | current = nextObject(0, current, trailerEnd, &trailerKeyword, NULL, NULL); |
| 290 | |
| 291 | if (!trailerKeyword.isKeyword() || strlen("trailer") != trailerKeyword.lenstr() || |
| 292 | strncmp(trailerKeyword.c_str(), "trailer", strlen("trailer")) != 0) { |
| 293 | // TODO(edisonn): report warning, rebuild trailer from objects. |
| 294 | return current; |
| 295 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 296 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 297 | |
| 298 | SkPdfObject token; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 299 | current = nextObject(0, current, trailerEnd, &token, fAllocator, NULL); |
edisonn@google.com | 432640a | 2013-07-10 22:53:40 +0000 | [diff] [blame] | 300 | if (!token.isDictionary()) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 301 | return current; |
edisonn@google.com | 432640a | 2013-07-10 22:53:40 +0000 | [diff] [blame] | 302 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 303 | SkPdfFileTrailerDictionary* trailer = (SkPdfFileTrailerDictionary*)&token; |
edisonn@google.com | 432640a | 2013-07-10 22:53:40 +0000 | [diff] [blame] | 304 | if (!trailer->valid()) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 305 | return current; |
edisonn@google.com | 432640a | 2013-07-10 22:53:40 +0000 | [diff] [blame] | 306 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 307 | |
| 308 | if (storeCatalog) { |
| 309 | const SkPdfObject* ref = trailer->Root(NULL); |
| 310 | if (ref == NULL || !ref->isReference()) { |
| 311 | // TODO(edisonn): oops, we have to fix the corrup pdf file |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 312 | return current; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 313 | } |
| 314 | fRootCatalogRef = ref; |
| 315 | } |
| 316 | |
| 317 | if (trailer->has_Prev()) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 318 | *prev = (long)trailer->Prev(NULL); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 319 | } |
| 320 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 321 | return current; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void SkNativeParsedPDF::addCrossSectionInfo(int id, int generation, int offset, bool isFreed) { |
| 325 | // TODO(edisonn): security here |
| 326 | while (fObjects.count() < id + 1) { |
| 327 | reset(fObjects.append()); |
| 328 | } |
| 329 | |
| 330 | fObjects[id].fOffset = offset; |
| 331 | fObjects[id].fObj = NULL; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 332 | fObjects[id].fResolvedReference = NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 333 | } |
| 334 | |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 335 | SkPdfObject* SkNativeParsedPDF::readObject(int id/*, int expectedGeneration*/) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 336 | long startOffset = fObjects[id].fOffset; |
| 337 | //long endOffset = fObjects[id].fOffsetEnd; |
| 338 | // TODO(edisonn): use hinted endOffset |
| 339 | // TODO(edisonn): current implementation will result in a lot of memory usage |
| 340 | // to decrease memory usage, we wither need to be smart and know where objects end, and we will |
| 341 | // alocate only the chancks needed, or the tokenizer will not make copies, but then it needs to |
| 342 | // cache the results so it does not go twice on the same buffer |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 343 | const unsigned char* current = fFileContent + startOffset; |
| 344 | const unsigned char* end = fFileContent + fContentLength; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 345 | |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 346 | SkPdfNativeTokenizer tokenizer(current, end - current, fMapper, fAllocator, this); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 347 | |
| 348 | SkPdfObject idObj; |
| 349 | SkPdfObject generationObj; |
| 350 | SkPdfObject objKeyword; |
| 351 | SkPdfObject* dict = fAllocator->allocObject(); |
| 352 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 353 | current = nextObject(0, current, end, &idObj, NULL, NULL); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 354 | if (current >= end) { |
| 355 | // TODO(edisonn): report warning/error |
| 356 | return NULL; |
| 357 | } |
| 358 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 359 | current = nextObject(0, current, end, &generationObj, NULL, NULL); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 360 | if (current >= end) { |
| 361 | // TODO(edisonn): report warning/error |
| 362 | return NULL; |
| 363 | } |
| 364 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 365 | current = nextObject(0, current, end, &objKeyword, NULL, NULL); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 366 | if (current >= end) { |
| 367 | // TODO(edisonn): report warning/error |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | if (!idObj.isInteger() || !generationObj.isInteger() || id != idObj.intValue()/* || generation != generationObj.intValue()*/) { |
| 372 | // TODO(edisonn): report warning/error |
| 373 | } |
| 374 | |
| 375 | if (!objKeyword.isKeyword() || strcmp(objKeyword.c_str(), "obj") != 0) { |
| 376 | // TODO(edisonn): report warning/error |
| 377 | } |
| 378 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 379 | current = nextObject(1, current, end, dict, fAllocator, this); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 380 | |
| 381 | // TODO(edisonn): report warning/error - verify last token is endobj |
| 382 | |
| 383 | return dict; |
| 384 | } |
| 385 | |
| 386 | void SkNativeParsedPDF::fillPages(SkPdfPageTreeNodeDictionary* tree) { |
| 387 | const SkPdfArray* kids = tree->Kids(this); |
| 388 | if (kids == NULL) { |
| 389 | *fPages.append() = (SkPdfPageObjectDictionary*)tree; |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | int cnt = kids->size(); |
| 394 | for (int i = 0; i < cnt; i++) { |
| 395 | const SkPdfObject* obj = resolveReference(kids->objAtAIndex(i)); |
| 396 | if (fMapper->mapPageObjectDictionary(obj) != kPageObjectDictionary_SkPdfObjectType) { |
| 397 | *fPages.append() = (SkPdfPageObjectDictionary*)obj; |
| 398 | } else { |
| 399 | // TODO(edisonn): verify that it is a page tree indeed |
| 400 | fillPages((SkPdfPageTreeNodeDictionary*)obj); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | int SkNativeParsedPDF::pages() const { |
| 406 | return fPages.count(); |
| 407 | } |
| 408 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 409 | SkPdfPageObjectDictionary* SkNativeParsedPDF::page(int page) { |
| 410 | SkASSERT(page >= 0 && page < fPages.count()); |
| 411 | return fPages[page]; |
| 412 | } |
| 413 | |
| 414 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 415 | SkPdfResourceDictionary* SkNativeParsedPDF::pageResources(int page) { |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 416 | SkASSERT(page >= 0 && page < fPages.count()); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 417 | return fPages[page]->Resources(this); |
| 418 | } |
| 419 | |
| 420 | // TODO(edisonn): Partial implemented. Move the logics directly in the code generator for inheritable and default value? |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 421 | SkRect SkNativeParsedPDF::MediaBox(int page) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 422 | SkPdfPageObjectDictionary* current = fPages[page]; |
| 423 | while (!current->has_MediaBox() && current->has_Parent()) { |
| 424 | current = (SkPdfPageObjectDictionary*)current->Parent(this); |
| 425 | } |
| 426 | if (current) { |
| 427 | return current->MediaBox(this); |
| 428 | } |
| 429 | return SkRect::MakeEmpty(); |
| 430 | } |
| 431 | |
| 432 | // TODO(edisonn): stream or array ... ? for now only array |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 433 | SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfPage(int page, |
| 434 | SkPdfAllocator* allocator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 435 | if (fPages[page]->isContentsAStream(this)) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 436 | return tokenizerOfStream(fPages[page]->getContentsAsStream(this), allocator); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 437 | } else { |
| 438 | // TODO(edisonn): NYI, we need to concatenate all streams in the array or make the tokenizer smart |
| 439 | // so we don't allocate new memory |
| 440 | return NULL; |
| 441 | } |
| 442 | } |
| 443 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 444 | SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfStream(SkPdfObject* stream, |
| 445 | SkPdfAllocator* allocator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 446 | if (stream == NULL) { |
| 447 | return NULL; |
| 448 | } |
| 449 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 450 | return new SkPdfNativeTokenizer(stream, fMapper, allocator, this); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // TODO(edisonn): NYI |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 454 | SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfBuffer(const unsigned char* buffer, size_t len, |
| 455 | SkPdfAllocator* allocator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 456 | // warning does not track two calls in the same buffer! the buffer is updated! |
| 457 | // make a clean copy if needed! |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 458 | return new SkPdfNativeTokenizer(buffer, len, fMapper, allocator, this); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | size_t SkNativeParsedPDF::objects() const { |
| 462 | return fObjects.count(); |
| 463 | } |
| 464 | |
| 465 | SkPdfObject* SkNativeParsedPDF::object(int i) { |
| 466 | SkASSERT(!(i < 0 || i > fObjects.count())); |
| 467 | |
| 468 | if (i < 0 || i > fObjects.count()) { |
| 469 | return NULL; |
| 470 | } |
| 471 | |
| 472 | if (fObjects[i].fObj == NULL) { |
| 473 | // TODO(edisonn): when we read the cross reference sections, store the start of the next object |
| 474 | // and fill fOffsetEnd |
| 475 | fObjects[i].fObj = readObject(i); |
| 476 | } |
| 477 | |
| 478 | return fObjects[i].fObj; |
| 479 | } |
| 480 | |
| 481 | const SkPdfMapper* SkNativeParsedPDF::mapper() const { |
| 482 | return fMapper; |
| 483 | } |
| 484 | |
| 485 | SkPdfReal* SkNativeParsedPDF::createReal(double value) const { |
| 486 | SkPdfObject* obj = fAllocator->allocObject(); |
| 487 | SkPdfObject::makeReal(value, obj); |
| 488 | return (SkPdfReal*)obj; |
| 489 | } |
| 490 | |
| 491 | SkPdfInteger* SkNativeParsedPDF::createInteger(int value) const { |
| 492 | SkPdfObject* obj = fAllocator->allocObject(); |
| 493 | SkPdfObject::makeInteger(value, obj); |
| 494 | return (SkPdfInteger*)obj; |
| 495 | } |
| 496 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 497 | SkPdfString* SkNativeParsedPDF::createString(const unsigned char* sz, size_t len) const { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 498 | SkPdfObject* obj = fAllocator->allocObject(); |
| 499 | SkPdfObject::makeString(sz, len, obj); |
| 500 | return (SkPdfString*)obj; |
| 501 | } |
| 502 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 503 | SkPdfAllocator* SkNativeParsedPDF::allocator() const { |
| 504 | return fAllocator; |
| 505 | } |
| 506 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 507 | // TODO(edisonn): fix infinite loop if ref to itself! |
| 508 | // TODO(edisonn): perf, fix refs at load, and resolve will simply return fResolvedReference? |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 509 | SkPdfObject* SkNativeParsedPDF::resolveReference(const SkPdfObject* ref) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 510 | if (ref && ref->isReference()) { |
| 511 | int id = ref->referenceId(); |
| 512 | // TODO(edisonn): generation/updates not supported now |
| 513 | //int gen = ref->referenceGeneration(); |
| 514 | |
edisonn@google.com | 641cce9 | 2013-07-30 12:09:14 +0000 | [diff] [blame] | 515 | // TODO(edisonn): verify id and gen expected |
| 516 | if (id < 0 || id >= fObjects.count()) { |
| 517 | // TODO(edisonn): report error/warning |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 518 | return NULL; |
| 519 | } |
| 520 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 521 | if (fObjects[id].fResolvedReference != NULL) { |
| 522 | return fObjects[id].fResolvedReference; |
| 523 | } |
| 524 | |
| 525 | if (fObjects[id].fObj == NULL) { |
| 526 | fObjects[id].fObj = readObject(id); |
| 527 | } |
| 528 | |
| 529 | if (fObjects[id].fResolvedReference == NULL) { |
| 530 | if (!fObjects[id].fObj->isReference()) { |
| 531 | fObjects[id].fResolvedReference = fObjects[id].fObj; |
| 532 | } else { |
| 533 | fObjects[id].fResolvedReference = resolveReference(fObjects[id].fObj); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return fObjects[id].fResolvedReference; |
| 538 | } |
| 539 | // TODO(edisonn): fix the mess with const, probably we need to remove it pretty much everywhere |
| 540 | return (SkPdfObject*)ref; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 541 | } |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 542 | |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 543 | size_t SkNativeParsedPDF::bytesUsed() const { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 544 | return fAllocator->bytesUsed() + |
| 545 | fContentLength + |
| 546 | fObjects.count() * sizeof(PublicObjectEntry) + |
| 547 | fPages.count() * sizeof(SkPdfPageObjectDictionary*) + |
| 548 | sizeof(*this); |
| 549 | } |