Sat, 03 Jan 2015 20:18:00 +0100
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 2012 Google Inc. |
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 | #include "SkBitmap.h" |
michael@0 | 10 | #include "SkErrorInternals.h" |
michael@0 | 11 | #include "SkReadBuffer.h" |
michael@0 | 12 | #include "SkStream.h" |
michael@0 | 13 | #include "SkTypeface.h" |
michael@0 | 14 | |
michael@0 | 15 | static uint32_t default_flags() { |
michael@0 | 16 | uint32_t flags = 0; |
michael@0 | 17 | #ifdef SK_SCALAR_IS_FLOAT |
michael@0 | 18 | flags |= SkReadBuffer::kScalarIsFloat_Flag; |
michael@0 | 19 | #endif |
michael@0 | 20 | if (8 == sizeof(void*)) { |
michael@0 | 21 | flags |= SkReadBuffer::kPtrIs64Bit_Flag; |
michael@0 | 22 | } |
michael@0 | 23 | return flags; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | SkReadBuffer::SkReadBuffer() { |
michael@0 | 27 | fFlags = default_flags(); |
michael@0 | 28 | fMemoryPtr = NULL; |
michael@0 | 29 | |
michael@0 | 30 | fBitmapStorage = NULL; |
michael@0 | 31 | fTFArray = NULL; |
michael@0 | 32 | fTFCount = 0; |
michael@0 | 33 | |
michael@0 | 34 | fFactoryTDArray = NULL; |
michael@0 | 35 | fFactoryArray = NULL; |
michael@0 | 36 | fFactoryCount = 0; |
michael@0 | 37 | fBitmapDecoder = NULL; |
michael@0 | 38 | #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 39 | fDecodedBitmapIndex = -1; |
michael@0 | 40 | #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | SkReadBuffer::SkReadBuffer(const void* data, size_t size) { |
michael@0 | 44 | fFlags = default_flags(); |
michael@0 | 45 | fReader.setMemory(data, size); |
michael@0 | 46 | fMemoryPtr = NULL; |
michael@0 | 47 | |
michael@0 | 48 | fBitmapStorage = NULL; |
michael@0 | 49 | fTFArray = NULL; |
michael@0 | 50 | fTFCount = 0; |
michael@0 | 51 | |
michael@0 | 52 | fFactoryTDArray = NULL; |
michael@0 | 53 | fFactoryArray = NULL; |
michael@0 | 54 | fFactoryCount = 0; |
michael@0 | 55 | fBitmapDecoder = NULL; |
michael@0 | 56 | #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 57 | fDecodedBitmapIndex = -1; |
michael@0 | 58 | #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | SkReadBuffer::SkReadBuffer(SkStream* stream) { |
michael@0 | 62 | fFlags = default_flags(); |
michael@0 | 63 | const size_t length = stream->getLength(); |
michael@0 | 64 | fMemoryPtr = sk_malloc_throw(length); |
michael@0 | 65 | stream->read(fMemoryPtr, length); |
michael@0 | 66 | fReader.setMemory(fMemoryPtr, length); |
michael@0 | 67 | |
michael@0 | 68 | fBitmapStorage = NULL; |
michael@0 | 69 | fTFArray = NULL; |
michael@0 | 70 | fTFCount = 0; |
michael@0 | 71 | |
michael@0 | 72 | fFactoryTDArray = NULL; |
michael@0 | 73 | fFactoryArray = NULL; |
michael@0 | 74 | fFactoryCount = 0; |
michael@0 | 75 | fBitmapDecoder = NULL; |
michael@0 | 76 | #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 77 | fDecodedBitmapIndex = -1; |
michael@0 | 78 | #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | SkReadBuffer::~SkReadBuffer() { |
michael@0 | 82 | sk_free(fMemoryPtr); |
michael@0 | 83 | SkSafeUnref(fBitmapStorage); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | bool SkReadBuffer::readBool() { |
michael@0 | 87 | return fReader.readBool(); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | SkColor SkReadBuffer::readColor() { |
michael@0 | 91 | return fReader.readInt(); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | SkFixed SkReadBuffer::readFixed() { |
michael@0 | 95 | return fReader.readS32(); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | int32_t SkReadBuffer::readInt() { |
michael@0 | 99 | return fReader.readInt(); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | SkScalar SkReadBuffer::readScalar() { |
michael@0 | 103 | return fReader.readScalar(); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | uint32_t SkReadBuffer::readUInt() { |
michael@0 | 107 | return fReader.readU32(); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | int32_t SkReadBuffer::read32() { |
michael@0 | 111 | return fReader.readInt(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | void SkReadBuffer::readString(SkString* string) { |
michael@0 | 115 | size_t len; |
michael@0 | 116 | const char* strContents = fReader.readString(&len); |
michael@0 | 117 | string->set(strContents, len); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | void* SkReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding encoding) { |
michael@0 | 121 | SkDEBUGCODE(int32_t encodingType = ) fReader.readInt(); |
michael@0 | 122 | SkASSERT(encodingType == encoding); |
michael@0 | 123 | *length = fReader.readInt(); |
michael@0 | 124 | void* data = sk_malloc_throw(*length); |
michael@0 | 125 | memcpy(data, fReader.skip(SkAlign4(*length)), *length); |
michael@0 | 126 | return data; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | void SkReadBuffer::readPoint(SkPoint* point) { |
michael@0 | 130 | point->fX = fReader.readScalar(); |
michael@0 | 131 | point->fY = fReader.readScalar(); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | void SkReadBuffer::readMatrix(SkMatrix* matrix) { |
michael@0 | 135 | fReader.readMatrix(matrix); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | void SkReadBuffer::readIRect(SkIRect* rect) { |
michael@0 | 139 | memcpy(rect, fReader.skip(sizeof(SkIRect)), sizeof(SkIRect)); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | void SkReadBuffer::readRect(SkRect* rect) { |
michael@0 | 143 | memcpy(rect, fReader.skip(sizeof(SkRect)), sizeof(SkRect)); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | void SkReadBuffer::readRegion(SkRegion* region) { |
michael@0 | 147 | fReader.readRegion(region); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | void SkReadBuffer::readPath(SkPath* path) { |
michael@0 | 151 | fReader.readPath(path); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | bool SkReadBuffer::readArray(void* value, size_t size, size_t elementSize) { |
michael@0 | 155 | const size_t count = this->getArrayCount(); |
michael@0 | 156 | if (count == size) { |
michael@0 | 157 | (void)fReader.skip(sizeof(uint32_t)); // Skip array count |
michael@0 | 158 | const size_t byteLength = count * elementSize; |
michael@0 | 159 | memcpy(value, fReader.skip(SkAlign4(byteLength)), byteLength); |
michael@0 | 160 | return true; |
michael@0 | 161 | } |
michael@0 | 162 | SkASSERT(false); |
michael@0 | 163 | fReader.skip(fReader.available()); |
michael@0 | 164 | return false; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | bool SkReadBuffer::readByteArray(void* value, size_t size) { |
michael@0 | 168 | return readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned char)); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | bool SkReadBuffer::readColorArray(SkColor* colors, size_t size) { |
michael@0 | 172 | return readArray(colors, size, sizeof(SkColor)); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | bool SkReadBuffer::readIntArray(int32_t* values, size_t size) { |
michael@0 | 176 | return readArray(values, size, sizeof(int32_t)); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | bool SkReadBuffer::readPointArray(SkPoint* points, size_t size) { |
michael@0 | 180 | return readArray(points, size, sizeof(SkPoint)); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | bool SkReadBuffer::readScalarArray(SkScalar* values, size_t size) { |
michael@0 | 184 | return readArray(values, size, sizeof(SkScalar)); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | uint32_t SkReadBuffer::getArrayCount() { |
michael@0 | 188 | return *(uint32_t*)fReader.peek(); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | void SkReadBuffer::readBitmap(SkBitmap* bitmap) { |
michael@0 | 192 | const int width = this->readInt(); |
michael@0 | 193 | const int height = this->readInt(); |
michael@0 | 194 | // The writer stored a boolean value to determine whether an SkBitmapHeap was used during |
michael@0 | 195 | // writing. |
michael@0 | 196 | if (this->readBool()) { |
michael@0 | 197 | // An SkBitmapHeap was used for writing. Read the index from the stream and find the |
michael@0 | 198 | // corresponding SkBitmap in fBitmapStorage. |
michael@0 | 199 | const uint32_t index = fReader.readU32(); |
michael@0 | 200 | fReader.readU32(); // bitmap generation ID (see SkWriteBuffer::writeBitmap) |
michael@0 | 201 | if (fBitmapStorage) { |
michael@0 | 202 | *bitmap = *fBitmapStorage->getBitmap(index); |
michael@0 | 203 | fBitmapStorage->releaseRef(index); |
michael@0 | 204 | return; |
michael@0 | 205 | } else { |
michael@0 | 206 | // The bitmap was stored in a heap, but there is no way to access it. Set an error and |
michael@0 | 207 | // fall through to use a place holder bitmap. |
michael@0 | 208 | SkErrorInternals::SetError(kParseError_SkError, "SkWriteBuffer::writeBitmap " |
michael@0 | 209 | "stored the SkBitmap in an SkBitmapHeap, but " |
michael@0 | 210 | "SkReadBuffer has no SkBitmapHeapReader to " |
michael@0 | 211 | "retrieve the SkBitmap."); |
michael@0 | 212 | } |
michael@0 | 213 | } else { |
michael@0 | 214 | // The writer stored false, meaning the SkBitmap was not stored in an SkBitmapHeap. |
michael@0 | 215 | const size_t length = this->readUInt(); |
michael@0 | 216 | if (length > 0) { |
michael@0 | 217 | #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 218 | fDecodedBitmapIndex++; |
michael@0 | 219 | #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 220 | // A non-zero size means the SkBitmap was encoded. Read the data and pixel |
michael@0 | 221 | // offset. |
michael@0 | 222 | const void* data = this->skip(length); |
michael@0 | 223 | const int32_t xOffset = fReader.readS32(); |
michael@0 | 224 | const int32_t yOffset = fReader.readS32(); |
michael@0 | 225 | if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap)) { |
michael@0 | 226 | if (bitmap->width() == width && bitmap->height() == height) { |
michael@0 | 227 | #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 228 | if (0 != xOffset || 0 != yOffset) { |
michael@0 | 229 | SkDebugf("SkReadBuffer::readBitmap: heights match," |
michael@0 | 230 | " but offset is not zero. \nInfo about the bitmap:" |
michael@0 | 231 | "\n\tIndex: %d\n\tDimensions: [%d %d]\n\tEncoded" |
michael@0 | 232 | " data size: %d\n\tOffset: (%d, %d)\n", |
michael@0 | 233 | fDecodedBitmapIndex, width, height, length, xOffset, |
michael@0 | 234 | yOffset); |
michael@0 | 235 | } |
michael@0 | 236 | #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
michael@0 | 237 | // If the width and height match, there should be no offset. |
michael@0 | 238 | SkASSERT(0 == xOffset && 0 == yOffset); |
michael@0 | 239 | return; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | // This case can only be reached if extractSubset was called, so |
michael@0 | 243 | // the recorded width and height must be smaller than or equal to |
michael@0 | 244 | // the encoded width and height. |
michael@0 | 245 | // FIXME (scroggo): This assert assumes that our decoder and the |
michael@0 | 246 | // sources encoder agree on the width and height which may not |
michael@0 | 247 | // always be the case. Removing until it can be investigated |
michael@0 | 248 | // further. |
michael@0 | 249 | //SkASSERT(width <= bitmap->width() && height <= bitmap->height()); |
michael@0 | 250 | |
michael@0 | 251 | SkBitmap subsetBm; |
michael@0 | 252 | SkIRect subset = SkIRect::MakeXYWH(xOffset, yOffset, width, height); |
michael@0 | 253 | if (bitmap->extractSubset(&subsetBm, subset)) { |
michael@0 | 254 | bitmap->swap(subsetBm); |
michael@0 | 255 | return; |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | // This bitmap was encoded when written, but we are unable to decode, possibly due to |
michael@0 | 259 | // not having a decoder. |
michael@0 | 260 | SkErrorInternals::SetError(kParseError_SkError, |
michael@0 | 261 | "Could not decode bitmap. Resulting bitmap will be red."); |
michael@0 | 262 | } else { |
michael@0 | 263 | // A size of zero means the SkBitmap was simply flattened. |
michael@0 | 264 | bitmap->unflatten(*this); |
michael@0 | 265 | return; |
michael@0 | 266 | } |
michael@0 | 267 | } |
michael@0 | 268 | // Could not read the SkBitmap. Use a placeholder bitmap. |
michael@0 | 269 | bitmap->allocPixels(SkImageInfo::MakeN32Premul(width, height)); |
michael@0 | 270 | bitmap->eraseColor(SK_ColorRED); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | SkTypeface* SkReadBuffer::readTypeface() { |
michael@0 | 274 | |
michael@0 | 275 | uint32_t index = fReader.readU32(); |
michael@0 | 276 | if (0 == index || index > (unsigned)fTFCount) { |
michael@0 | 277 | if (index) { |
michael@0 | 278 | SkDebugf("====== typeface index %d\n", index); |
michael@0 | 279 | } |
michael@0 | 280 | return NULL; |
michael@0 | 281 | } else { |
michael@0 | 282 | SkASSERT(fTFArray); |
michael@0 | 283 | return fTFArray[index - 1]; |
michael@0 | 284 | } |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | SkFlattenable* SkReadBuffer::readFlattenable(SkFlattenable::Type ft) { |
michael@0 | 288 | // |
michael@0 | 289 | // TODO: confirm that ft matches the factory we decide to use |
michael@0 | 290 | // |
michael@0 | 291 | |
michael@0 | 292 | SkFlattenable::Factory factory = NULL; |
michael@0 | 293 | |
michael@0 | 294 | if (fFactoryCount > 0) { |
michael@0 | 295 | int32_t index = fReader.readU32(); |
michael@0 | 296 | if (0 == index) { |
michael@0 | 297 | return NULL; // writer failed to give us the flattenable |
michael@0 | 298 | } |
michael@0 | 299 | index -= 1; // we stored the index-base-1 |
michael@0 | 300 | SkASSERT(index < fFactoryCount); |
michael@0 | 301 | factory = fFactoryArray[index]; |
michael@0 | 302 | } else if (fFactoryTDArray) { |
michael@0 | 303 | int32_t index = fReader.readU32(); |
michael@0 | 304 | if (0 == index) { |
michael@0 | 305 | return NULL; // writer failed to give us the flattenable |
michael@0 | 306 | } |
michael@0 | 307 | index -= 1; // we stored the index-base-1 |
michael@0 | 308 | factory = (*fFactoryTDArray)[index]; |
michael@0 | 309 | } else { |
michael@0 | 310 | factory = (SkFlattenable::Factory)readFunctionPtr(); |
michael@0 | 311 | if (NULL == factory) { |
michael@0 | 312 | return NULL; // writer failed to give us the flattenable |
michael@0 | 313 | } |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | // if we get here, factory may still be null, but if that is the case, the |
michael@0 | 317 | // failure was ours, not the writer. |
michael@0 | 318 | SkFlattenable* obj = NULL; |
michael@0 | 319 | uint32_t sizeRecorded = fReader.readU32(); |
michael@0 | 320 | if (factory) { |
michael@0 | 321 | uint32_t offset = fReader.offset(); |
michael@0 | 322 | obj = (*factory)(*this); |
michael@0 | 323 | // check that we read the amount we expected |
michael@0 | 324 | uint32_t sizeRead = fReader.offset() - offset; |
michael@0 | 325 | if (sizeRecorded != sizeRead) { |
michael@0 | 326 | // we could try to fix up the offset... |
michael@0 | 327 | sk_throw(); |
michael@0 | 328 | } |
michael@0 | 329 | } else { |
michael@0 | 330 | // we must skip the remaining data |
michael@0 | 331 | fReader.skip(sizeRecorded); |
michael@0 | 332 | } |
michael@0 | 333 | return obj; |
michael@0 | 334 | } |