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