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