gfx/skia/trunk/src/core/SkValidatingReadBuffer.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 * Copyright 2013 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkBitmap.h"
michael@0 9 #include "SkErrorInternals.h"
michael@0 10 #include "SkValidatingReadBuffer.h"
michael@0 11 #include "SkStream.h"
michael@0 12 #include "SkTypeface.h"
michael@0 13
michael@0 14 SkValidatingReadBuffer::SkValidatingReadBuffer(const void* data, size_t size) :
michael@0 15 fError(false) {
michael@0 16 this->setMemory(data, size);
michael@0 17 this->setFlags(SkReadBuffer::kValidation_Flag);
michael@0 18 }
michael@0 19
michael@0 20 SkValidatingReadBuffer::~SkValidatingReadBuffer() {
michael@0 21 }
michael@0 22
michael@0 23 bool SkValidatingReadBuffer::validate(bool isValid) {
michael@0 24 if (!fError && !isValid) {
michael@0 25 // When an error is found, send the read cursor to the end of the stream
michael@0 26 fReader.skip(fReader.available());
michael@0 27 fError = true;
michael@0 28 }
michael@0 29 return !fError;
michael@0 30 }
michael@0 31
michael@0 32 bool SkValidatingReadBuffer::isValid() const {
michael@0 33 return !fError;
michael@0 34 }
michael@0 35
michael@0 36 void SkValidatingReadBuffer::setMemory(const void* data, size_t size) {
michael@0 37 this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size));
michael@0 38 if (!fError) {
michael@0 39 fReader.setMemory(data, size);
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 const void* SkValidatingReadBuffer::skip(size_t size) {
michael@0 44 size_t inc = SkAlign4(size);
michael@0 45 const void* addr = fReader.peek();
michael@0 46 this->validate(IsPtrAlign4(addr) && fReader.isAvailable(inc));
michael@0 47 if (!fError) {
michael@0 48 fReader.skip(size);
michael@0 49 }
michael@0 50 return addr;
michael@0 51 }
michael@0 52
michael@0 53 // All the methods in this file funnel down into either readInt(), readScalar() or skip(),
michael@0 54 // followed by a memcpy. So we've got all our validation in readInt(), readScalar() and skip();
michael@0 55 // if they fail they'll return a zero value or skip nothing, respectively, and set fError to
michael@0 56 // true, which the caller should check to see if an error occurred during the read operation.
michael@0 57
michael@0 58 bool SkValidatingReadBuffer::readBool() {
michael@0 59 uint32_t value = this->readInt();
michael@0 60 // Boolean value should be either 0 or 1
michael@0 61 this->validate(!(value & ~1));
michael@0 62 return value != 0;
michael@0 63 }
michael@0 64
michael@0 65 SkColor SkValidatingReadBuffer::readColor() {
michael@0 66 return this->readInt();
michael@0 67 }
michael@0 68
michael@0 69 SkFixed SkValidatingReadBuffer::readFixed() {
michael@0 70 return this->readInt();
michael@0 71 }
michael@0 72
michael@0 73 int32_t SkValidatingReadBuffer::readInt() {
michael@0 74 const size_t inc = sizeof(int32_t);
michael@0 75 this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
michael@0 76 return fError ? 0 : fReader.readInt();
michael@0 77 }
michael@0 78
michael@0 79 SkScalar SkValidatingReadBuffer::readScalar() {
michael@0 80 const size_t inc = sizeof(SkScalar);
michael@0 81 this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
michael@0 82 return fError ? 0 : fReader.readScalar();
michael@0 83 }
michael@0 84
michael@0 85 uint32_t SkValidatingReadBuffer::readUInt() {
michael@0 86 return this->readInt();
michael@0 87 }
michael@0 88
michael@0 89 int32_t SkValidatingReadBuffer::read32() {
michael@0 90 return this->readInt();
michael@0 91 }
michael@0 92
michael@0 93 void SkValidatingReadBuffer::readString(SkString* string) {
michael@0 94 const size_t len = this->readInt();
michael@0 95 const void* ptr = fReader.peek();
michael@0 96 const char* cptr = (const char*)ptr;
michael@0 97
michael@0 98 // skip over the string + '\0' and then pad to a multiple of 4
michael@0 99 const size_t alignedSize = SkAlign4(len + 1);
michael@0 100 this->skip(alignedSize);
michael@0 101 if (!fError) {
michael@0 102 this->validate(cptr[len] == '\0');
michael@0 103 }
michael@0 104 if (!fError) {
michael@0 105 string->set(cptr, len);
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 void* SkValidatingReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding encoding) {
michael@0 110 const int32_t encodingType = this->readInt();
michael@0 111 this->validate(encodingType == encoding);
michael@0 112 *length = this->readInt();
michael@0 113 const void* ptr = this->skip(SkAlign4(*length));
michael@0 114 void* data = NULL;
michael@0 115 if (!fError) {
michael@0 116 data = sk_malloc_throw(*length);
michael@0 117 memcpy(data, ptr, *length);
michael@0 118 }
michael@0 119 return data;
michael@0 120 }
michael@0 121
michael@0 122 void SkValidatingReadBuffer::readPoint(SkPoint* point) {
michael@0 123 point->fX = this->readScalar();
michael@0 124 point->fY = this->readScalar();
michael@0 125 }
michael@0 126
michael@0 127 void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) {
michael@0 128 size_t size = 0;
michael@0 129 if (!fError) {
michael@0 130 size = matrix->readFromMemory(fReader.peek(), fReader.available());
michael@0 131 this->validate((SkAlign4(size) == size) && (0 != size));
michael@0 132 }
michael@0 133 if (!fError) {
michael@0 134 (void)this->skip(size);
michael@0 135 }
michael@0 136 }
michael@0 137
michael@0 138 void SkValidatingReadBuffer::readIRect(SkIRect* rect) {
michael@0 139 const void* ptr = this->skip(sizeof(SkIRect));
michael@0 140 if (!fError) {
michael@0 141 memcpy(rect, ptr, sizeof(SkIRect));
michael@0 142 }
michael@0 143 }
michael@0 144
michael@0 145 void SkValidatingReadBuffer::readRect(SkRect* rect) {
michael@0 146 const void* ptr = this->skip(sizeof(SkRect));
michael@0 147 if (!fError) {
michael@0 148 memcpy(rect, ptr, sizeof(SkRect));
michael@0 149 }
michael@0 150 }
michael@0 151
michael@0 152 void SkValidatingReadBuffer::readRegion(SkRegion* region) {
michael@0 153 size_t size = 0;
michael@0 154 if (!fError) {
michael@0 155 size = region->readFromMemory(fReader.peek(), fReader.available());
michael@0 156 this->validate((SkAlign4(size) == size) && (0 != size));
michael@0 157 }
michael@0 158 if (!fError) {
michael@0 159 (void)this->skip(size);
michael@0 160 }
michael@0 161 }
michael@0 162
michael@0 163 void SkValidatingReadBuffer::readPath(SkPath* path) {
michael@0 164 size_t size = 0;
michael@0 165 if (!fError) {
michael@0 166 size = path->readFromMemory(fReader.peek(), fReader.available());
michael@0 167 this->validate((SkAlign4(size) == size) && (0 != size));
michael@0 168 }
michael@0 169 if (!fError) {
michael@0 170 (void)this->skip(size);
michael@0 171 }
michael@0 172 }
michael@0 173
michael@0 174 bool SkValidatingReadBuffer::readArray(void* value, size_t size, size_t elementSize) {
michael@0 175 const uint32_t count = this->getArrayCount();
michael@0 176 this->validate(size == count);
michael@0 177 (void)this->skip(sizeof(uint32_t)); // Skip array count
michael@0 178 const size_t byteLength = count * elementSize;
michael@0 179 const void* ptr = this->skip(SkAlign4(byteLength));
michael@0 180 if (!fError) {
michael@0 181 memcpy(value, ptr, byteLength);
michael@0 182 return true;
michael@0 183 }
michael@0 184 return false;
michael@0 185 }
michael@0 186
michael@0 187 bool SkValidatingReadBuffer::readByteArray(void* value, size_t size) {
michael@0 188 return readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned char));
michael@0 189 }
michael@0 190
michael@0 191 bool SkValidatingReadBuffer::readColorArray(SkColor* colors, size_t size) {
michael@0 192 return readArray(colors, size, sizeof(SkColor));
michael@0 193 }
michael@0 194
michael@0 195 bool SkValidatingReadBuffer::readIntArray(int32_t* values, size_t size) {
michael@0 196 return readArray(values, size, sizeof(int32_t));
michael@0 197 }
michael@0 198
michael@0 199 bool SkValidatingReadBuffer::readPointArray(SkPoint* points, size_t size) {
michael@0 200 return readArray(points, size, sizeof(SkPoint));
michael@0 201 }
michael@0 202
michael@0 203 bool SkValidatingReadBuffer::readScalarArray(SkScalar* values, size_t size) {
michael@0 204 return readArray(values, size, sizeof(SkScalar));
michael@0 205 }
michael@0 206
michael@0 207 uint32_t SkValidatingReadBuffer::getArrayCount() {
michael@0 208 const size_t inc = sizeof(uint32_t);
michael@0 209 fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc);
michael@0 210 return fError ? 0 : *(uint32_t*)fReader.peek();
michael@0 211 }
michael@0 212
michael@0 213 void SkValidatingReadBuffer::readBitmap(SkBitmap* bitmap) {
michael@0 214 const int width = this->readInt();
michael@0 215 const int height = this->readInt();
michael@0 216 const bool useBitmapHeap = this->readBool();
michael@0 217 const size_t length = this->readUInt();
michael@0 218 // A size of zero means the SkBitmap was simply flattened.
michael@0 219 if (!this->validate(!useBitmapHeap && (0 == length))) {
michael@0 220 return;
michael@0 221 }
michael@0 222 bitmap->unflatten(*this);
michael@0 223 this->validate((bitmap->width() == width) && (bitmap->height() == height));
michael@0 224 }
michael@0 225
michael@0 226 SkTypeface* SkValidatingReadBuffer::readTypeface() {
michael@0 227 // TODO: Implement this (securely) when needed
michael@0 228 return NULL;
michael@0 229 }
michael@0 230
michael@0 231 bool SkValidatingReadBuffer::validateAvailable(size_t size) {
michael@0 232 return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast<uint32_t>(size)));
michael@0 233 }
michael@0 234
michael@0 235 SkFlattenable* SkValidatingReadBuffer::readFlattenable(SkFlattenable::Type type) {
michael@0 236 SkString name;
michael@0 237 this->readString(&name);
michael@0 238 if (fError) {
michael@0 239 return NULL;
michael@0 240 }
michael@0 241
michael@0 242 // Is this the type we wanted ?
michael@0 243 const char* cname = name.c_str();
michael@0 244 SkFlattenable::Type baseType;
michael@0 245 if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) {
michael@0 246 return NULL;
michael@0 247 }
michael@0 248
michael@0 249 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(cname);
michael@0 250 if (NULL == factory) {
michael@0 251 return NULL; // writer failed to give us the flattenable
michael@0 252 }
michael@0 253
michael@0 254 // if we get here, factory may still be null, but if that is the case, the
michael@0 255 // failure was ours, not the writer.
michael@0 256 SkFlattenable* obj = NULL;
michael@0 257 uint32_t sizeRecorded = this->readUInt();
michael@0 258 if (factory) {
michael@0 259 uint32_t offset = fReader.offset();
michael@0 260 obj = (*factory)(*this);
michael@0 261 // check that we read the amount we expected
michael@0 262 uint32_t sizeRead = fReader.offset() - offset;
michael@0 263 this->validate(sizeRecorded == sizeRead);
michael@0 264 if (fError) {
michael@0 265 // we could try to fix up the offset...
michael@0 266 delete obj;
michael@0 267 obj = NULL;
michael@0 268 }
michael@0 269 } else {
michael@0 270 // we must skip the remaining data
michael@0 271 this->skip(sizeRecorded);
michael@0 272 SkASSERT(false);
michael@0 273 }
michael@0 274 return obj;
michael@0 275 }

mercurial