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