gfx/skia/trunk/src/pdf/SkPDFCatalog.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2010 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #include "SkPDFCatalog.h"
michael@0 11 #include "SkPDFTypes.h"
michael@0 12 #include "SkStream.h"
michael@0 13 #include "SkTypes.h"
michael@0 14
michael@0 15 SkPDFCatalog::SkPDFCatalog(SkPDFDocument::Flags flags)
michael@0 16 : fFirstPageCount(0),
michael@0 17 fNextObjNum(1),
michael@0 18 fNextFirstPageObjNum(0),
michael@0 19 fDocumentFlags(flags) {
michael@0 20 }
michael@0 21
michael@0 22 SkPDFCatalog::~SkPDFCatalog() {
michael@0 23 fSubstituteResourcesRemaining.safeUnrefAll();
michael@0 24 fSubstituteResourcesFirstPage.safeUnrefAll();
michael@0 25 }
michael@0 26
michael@0 27 SkPDFObject* SkPDFCatalog::addObject(SkPDFObject* obj, bool onFirstPage) {
michael@0 28 if (findObjectIndex(obj) != -1) { // object already added
michael@0 29 return obj;
michael@0 30 }
michael@0 31 SkASSERT(fNextFirstPageObjNum == 0);
michael@0 32 if (onFirstPage) {
michael@0 33 fFirstPageCount++;
michael@0 34 }
michael@0 35
michael@0 36 struct Rec newEntry(obj, onFirstPage);
michael@0 37 fCatalog.append(1, &newEntry);
michael@0 38 return obj;
michael@0 39 }
michael@0 40
michael@0 41 size_t SkPDFCatalog::setFileOffset(SkPDFObject* obj, off_t offset) {
michael@0 42 int objIndex = assignObjNum(obj) - 1;
michael@0 43 SkASSERT(fCatalog[objIndex].fObjNumAssigned);
michael@0 44 SkASSERT(fCatalog[objIndex].fFileOffset == 0);
michael@0 45 fCatalog[objIndex].fFileOffset = offset;
michael@0 46
michael@0 47 return getSubstituteObject(obj)->getOutputSize(this, true);
michael@0 48 }
michael@0 49
michael@0 50 void SkPDFCatalog::emitObjectNumber(SkWStream* stream, SkPDFObject* obj) {
michael@0 51 stream->writeDecAsText(assignObjNum(obj));
michael@0 52 stream->writeText(" 0"); // Generation number is always 0.
michael@0 53 }
michael@0 54
michael@0 55 size_t SkPDFCatalog::getObjectNumberSize(SkPDFObject* obj) {
michael@0 56 SkDynamicMemoryWStream buffer;
michael@0 57 emitObjectNumber(&buffer, obj);
michael@0 58 return buffer.getOffset();
michael@0 59 }
michael@0 60
michael@0 61 int SkPDFCatalog::findObjectIndex(SkPDFObject* obj) const {
michael@0 62 for (int i = 0; i < fCatalog.count(); i++) {
michael@0 63 if (fCatalog[i].fObject == obj) {
michael@0 64 return i;
michael@0 65 }
michael@0 66 }
michael@0 67 // If it's not in the main array, check if it's a substitute object.
michael@0 68 for (int i = 0; i < fSubstituteMap.count(); ++i) {
michael@0 69 if (fSubstituteMap[i].fSubstitute == obj) {
michael@0 70 return findObjectIndex(fSubstituteMap[i].fOriginal);
michael@0 71 }
michael@0 72 }
michael@0 73 return -1;
michael@0 74 }
michael@0 75
michael@0 76 int SkPDFCatalog::assignObjNum(SkPDFObject* obj) {
michael@0 77 int pos = findObjectIndex(obj);
michael@0 78 // If this assert fails, it means you probably forgot to add an object
michael@0 79 // to the resource list.
michael@0 80 SkASSERT(pos >= 0);
michael@0 81 uint32_t currentIndex = pos;
michael@0 82 if (fCatalog[currentIndex].fObjNumAssigned) {
michael@0 83 return currentIndex + 1;
michael@0 84 }
michael@0 85
michael@0 86 // First assignment.
michael@0 87 if (fNextFirstPageObjNum == 0) {
michael@0 88 fNextFirstPageObjNum = fCatalog.count() - fFirstPageCount + 1;
michael@0 89 }
michael@0 90
michael@0 91 uint32_t objNum;
michael@0 92 if (fCatalog[currentIndex].fOnFirstPage) {
michael@0 93 objNum = fNextFirstPageObjNum;
michael@0 94 fNextFirstPageObjNum++;
michael@0 95 } else {
michael@0 96 objNum = fNextObjNum;
michael@0 97 fNextObjNum++;
michael@0 98 }
michael@0 99
michael@0 100 // When we assign an object an object number, we put it in that array
michael@0 101 // offset (minus 1 because object number 0 is reserved).
michael@0 102 SkASSERT(!fCatalog[objNum - 1].fObjNumAssigned);
michael@0 103 if (objNum - 1 != currentIndex) {
michael@0 104 SkTSwap(fCatalog[objNum - 1], fCatalog[currentIndex]);
michael@0 105 }
michael@0 106 fCatalog[objNum - 1].fObjNumAssigned = true;
michael@0 107 return objNum;
michael@0 108 }
michael@0 109
michael@0 110 int32_t SkPDFCatalog::emitXrefTable(SkWStream* stream, bool firstPage) {
michael@0 111 int first = -1;
michael@0 112 int last = fCatalog.count() - 1;
michael@0 113 // TODO(vandebo): Support linearized format.
michael@0 114 // int last = fCatalog.count() - fFirstPageCount - 1;
michael@0 115 // if (firstPage) {
michael@0 116 // first = fCatalog.count() - fFirstPageCount;
michael@0 117 // last = fCatalog.count() - 1;
michael@0 118 // }
michael@0 119
michael@0 120 stream->writeText("xref\n");
michael@0 121 stream->writeDecAsText(first + 1);
michael@0 122 stream->writeText(" ");
michael@0 123 stream->writeDecAsText(last - first + 1);
michael@0 124 stream->writeText("\n");
michael@0 125
michael@0 126 if (first == -1) {
michael@0 127 stream->writeText("0000000000 65535 f \n");
michael@0 128 first++;
michael@0 129 }
michael@0 130 for (int i = first; i <= last; i++) {
michael@0 131 // For 32 bits platforms, the maximum offset has to fit within off_t
michael@0 132 // which is a 32 bits signed integer on these platforms.
michael@0 133 SkDEBUGCODE(static const off_t kMaxOff = SK_MaxS32;)
michael@0 134 SkASSERT(fCatalog[i].fFileOffset > 0);
michael@0 135 SkASSERT(fCatalog[i].fFileOffset < kMaxOff);
michael@0 136 stream->writeBigDecAsText(fCatalog[i].fFileOffset, 10);
michael@0 137 stream->writeText(" 00000 n \n");
michael@0 138 }
michael@0 139
michael@0 140 return fCatalog.count() + 1;
michael@0 141 }
michael@0 142
michael@0 143 void SkPDFCatalog::setSubstitute(SkPDFObject* original,
michael@0 144 SkPDFObject* substitute) {
michael@0 145 #if defined(SK_DEBUG)
michael@0 146 // Sanity check: is the original already in substitute list?
michael@0 147 for (int i = 0; i < fSubstituteMap.count(); ++i) {
michael@0 148 if (original == fSubstituteMap[i].fSubstitute ||
michael@0 149 original == fSubstituteMap[i].fOriginal) {
michael@0 150 SkASSERT(false);
michael@0 151 return;
michael@0 152 }
michael@0 153 }
michael@0 154 #endif
michael@0 155 // Check if the original is on first page.
michael@0 156 bool onFirstPage = false;
michael@0 157 for (int i = 0; i < fCatalog.count(); ++i) {
michael@0 158 if (fCatalog[i].fObject == original) {
michael@0 159 onFirstPage = fCatalog[i].fOnFirstPage;
michael@0 160 break;
michael@0 161 }
michael@0 162 #if defined(SK_DEBUG)
michael@0 163 if (i == fCatalog.count() - 1) {
michael@0 164 SkASSERT(false); // original not in catalog
michael@0 165 return;
michael@0 166 }
michael@0 167 #endif
michael@0 168 }
michael@0 169
michael@0 170 SubstituteMapping newMapping(original, substitute);
michael@0 171 fSubstituteMap.append(1, &newMapping);
michael@0 172
michael@0 173 // Add resource objects of substitute object to catalog.
michael@0 174 SkTSet<SkPDFObject*>* targetSet = getSubstituteList(onFirstPage);
michael@0 175 SkTSet<SkPDFObject*> newResourceObjects;
michael@0 176 newMapping.fSubstitute->getResources(*targetSet, &newResourceObjects);
michael@0 177 for (int i = 0; i < newResourceObjects.count(); ++i) {
michael@0 178 addObject(newResourceObjects[i], onFirstPage);
michael@0 179 }
michael@0 180 // mergeInto returns the number of duplicates.
michael@0 181 // If there are duplicates, there is a bug and we mess ref counting.
michael@0 182 SkDEBUGCODE(int duplicates =) targetSet->mergeInto(newResourceObjects);
michael@0 183 SkASSERT(duplicates == 0);
michael@0 184 }
michael@0 185
michael@0 186 SkPDFObject* SkPDFCatalog::getSubstituteObject(SkPDFObject* object) {
michael@0 187 for (int i = 0; i < fSubstituteMap.count(); ++i) {
michael@0 188 if (object == fSubstituteMap[i].fOriginal) {
michael@0 189 return fSubstituteMap[i].fSubstitute;
michael@0 190 }
michael@0 191 }
michael@0 192 return object;
michael@0 193 }
michael@0 194
michael@0 195 off_t SkPDFCatalog::setSubstituteResourcesOffsets(off_t fileOffset,
michael@0 196 bool firstPage) {
michael@0 197 SkTSet<SkPDFObject*>* targetSet = getSubstituteList(firstPage);
michael@0 198 off_t offsetSum = fileOffset;
michael@0 199 for (int i = 0; i < targetSet->count(); ++i) {
michael@0 200 offsetSum += setFileOffset((*targetSet)[i], offsetSum);
michael@0 201 }
michael@0 202 return offsetSum - fileOffset;
michael@0 203 }
michael@0 204
michael@0 205 void SkPDFCatalog::emitSubstituteResources(SkWStream *stream, bool firstPage) {
michael@0 206 SkTSet<SkPDFObject*>* targetSet = getSubstituteList(firstPage);
michael@0 207 for (int i = 0; i < targetSet->count(); ++i) {
michael@0 208 (*targetSet)[i]->emit(stream, this, true);
michael@0 209 }
michael@0 210 }
michael@0 211
michael@0 212 SkTSet<SkPDFObject*>* SkPDFCatalog::getSubstituteList(bool firstPage) {
michael@0 213 return firstPage ? &fSubstituteResourcesFirstPage :
michael@0 214 &fSubstituteResourcesRemaining;
michael@0 215 }

mercurial