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