1 /* 2 * Copyright 2012 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 "SkDeduper.h" 10 #include "SkImage.h" 11 #include "SkImageDeserializer.h" 12 #include "SkImageGenerator.h" 13 #include "SkMakeUnique.h" 14 #include "SkReadBuffer.h" 15 #include "SkStream.h" 16 #include "SkTypeface.h" 17 18 namespace { 19 20 // This generator intentionally should always fail on all attempts to get its pixels, 21 // simulating a bad or empty codec stream. 22 class EmptyImageGenerator final : public SkImageGenerator { 23 public: 24 EmptyImageGenerator(const SkImageInfo& info) : INHERITED(info) { } 25 26 private: 27 typedef SkImageGenerator INHERITED; 28 }; 29 30 static sk_sp<SkImage> MakeEmptyImage(int width, int height) { 31 return SkImage::MakeFromGenerator( 32 skstd::make_unique<EmptyImageGenerator>(SkImageInfo::MakeN32Premul(width, height))); 33 } 34 35 } // anonymous namespace 36 37 38 static uint32_t default_flags() { 39 uint32_t flags = 0; 40 flags |= SkReadBuffer::kScalarIsFloat_Flag; 41 if (8 == sizeof(void*)) { 42 flags |= SkReadBuffer::kPtrIs64Bit_Flag; 43 } 44 return flags; 45 } 46 47 // This has an empty constructor and destructor, and is thread-safe, so we can use a singleton. 48 static SkImageDeserializer gDefaultImageDeserializer; 49 50 SkReadBuffer::SkReadBuffer() { 51 fFlags = default_flags(); 52 fVersion = 0; 53 fMemoryPtr = nullptr; 54 55 fTFArray = nullptr; 56 fTFCount = 0; 57 58 fFactoryArray = nullptr; 59 fFactoryCount = 0; 60 fImageDeserializer = &gDefaultImageDeserializer; 61 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT 62 fDecodedBitmapIndex = -1; 63 #endif // DEBUG_NON_DETERMINISTIC_ASSERT 64 } 65 66 SkReadBuffer::SkReadBuffer(const void* data, size_t size) { 67 fFlags = default_flags(); 68 fVersion = 0; 69 fReader.setMemory(data, size); 70 fMemoryPtr = nullptr; 71 72 fTFArray = nullptr; 73 fTFCount = 0; 74 75 fFactoryArray = nullptr; 76 fFactoryCount = 0; 77 fImageDeserializer = &gDefaultImageDeserializer; 78 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT 79 fDecodedBitmapIndex = -1; 80 #endif // DEBUG_NON_DETERMINISTIC_ASSERT 81 } 82 83 SkReadBuffer::SkReadBuffer(SkStream* stream) { 84 fFlags = default_flags(); 85 fVersion = 0; 86 const size_t length = stream->getLength(); 87 fMemoryPtr = sk_malloc_throw(length); 88 stream->read(fMemoryPtr, length); 89 fReader.setMemory(fMemoryPtr, length); 90 91 fTFArray = nullptr; 92 fTFCount = 0; 93 94 fFactoryArray = nullptr; 95 fFactoryCount = 0; 96 fImageDeserializer = &gDefaultImageDeserializer; 97 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT 98 fDecodedBitmapIndex = -1; 99 #endif // DEBUG_NON_DETERMINISTIC_ASSERT 100 } 101 102 SkReadBuffer::~SkReadBuffer() { 103 sk_free(fMemoryPtr); 104 } 105 106 void SkReadBuffer::setImageDeserializer(SkImageDeserializer* deserializer) { 107 fImageDeserializer = deserializer ? deserializer : &gDefaultImageDeserializer; 108 } 109 110 bool SkReadBuffer::readBool() { 111 return fReader.readBool(); 112 } 113 114 SkColor SkReadBuffer::readColor() { 115 return fReader.readInt(); 116 } 117 118 int32_t SkReadBuffer::readInt() { 119 return fReader.readInt(); 120 } 121 122 SkScalar SkReadBuffer::readScalar() { 123 return fReader.readScalar(); 124 } 125 126 uint32_t SkReadBuffer::readUInt() { 127 return fReader.readU32(); 128 } 129 130 int32_t SkReadBuffer::read32() { 131 return fReader.readInt(); 132 } 133 134 uint8_t SkReadBuffer::peekByte() { 135 SkASSERT(fReader.available() > 0); 136 return *((uint8_t*) fReader.peek()); 137 } 138 139 void SkReadBuffer::readString(SkString* string) { 140 size_t len; 141 const char* strContents = fReader.readString(&len); 142 string->set(strContents, len); 143 } 144 145 void SkReadBuffer::readColor4f(SkColor4f* color) { 146 memcpy(color, fReader.skip(sizeof(SkColor4f)), sizeof(SkColor4f)); 147 } 148 149 void SkReadBuffer::readPoint(SkPoint* point) { 150 point->fX = fReader.readScalar(); 151 point->fY = fReader.readScalar(); 152 } 153 154 void SkReadBuffer::readPoint3(SkPoint3* point) { 155 point->fX = fReader.readScalar(); 156 point->fY = fReader.readScalar(); 157 point->fZ = fReader.readScalar(); 158 } 159 160 void SkReadBuffer::readMatrix(SkMatrix* matrix) { 161 fReader.readMatrix(matrix); 162 } 163 164 void SkReadBuffer::readIRect(SkIRect* rect) { 165 memcpy(rect, fReader.skip(sizeof(SkIRect)), sizeof(SkIRect)); 166 } 167 168 void SkReadBuffer::readRect(SkRect* rect) { 169 memcpy(rect, fReader.skip(sizeof(SkRect)), sizeof(SkRect)); 170 } 171 172 void SkReadBuffer::readRRect(SkRRect* rrect) { 173 fReader.readRRect(rrect); 174 } 175 176 void SkReadBuffer::readRegion(SkRegion* region) { 177 fReader.readRegion(region); 178 } 179 180 void SkReadBuffer::readPath(SkPath* path) { 181 fReader.readPath(path); 182 } 183 184 bool SkReadBuffer::readArray(void* value, size_t size, size_t elementSize) { 185 const size_t count = this->getArrayCount(); 186 if (count == size) { 187 (void)fReader.skip(sizeof(uint32_t)); // Skip array count 188 const size_t byteLength = count * elementSize; 189 memcpy(value, fReader.skip(SkAlign4(byteLength)), byteLength); 190 return true; 191 } 192 SkASSERT(false); 193 fReader.skip(fReader.available()); 194 return false; 195 } 196 197 bool SkReadBuffer::readByteArray(void* value, size_t size) { 198 return readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned char)); 199 } 200 201 bool SkReadBuffer::readColorArray(SkColor* colors, size_t size) { 202 return readArray(colors, size, sizeof(SkColor)); 203 } 204 205 bool SkReadBuffer::readColor4fArray(SkColor4f* colors, size_t size) { 206 return readArray(colors, size, sizeof(SkColor4f)); 207 } 208 209 bool SkReadBuffer::readIntArray(int32_t* values, size_t size) { 210 return readArray(values, size, sizeof(int32_t)); 211 } 212 213 bool SkReadBuffer::readPointArray(SkPoint* points, size_t size) { 214 return readArray(points, size, sizeof(SkPoint)); 215 } 216 217 bool SkReadBuffer::readScalarArray(SkScalar* values, size_t size) { 218 return readArray(values, size, sizeof(SkScalar)); 219 } 220 221 uint32_t SkReadBuffer::getArrayCount() { 222 return *(uint32_t*)fReader.peek(); 223 } 224 225 sk_sp<SkImage> SkReadBuffer::readBitmapAsImage() { 226 const int width = this->readInt(); 227 const int height = this->readInt(); 228 229 // The writer stored a boolean value to determine whether an SkBitmapHeap was used during 230 // writing. That feature is deprecated. 231 if (this->readBool()) { 232 this->readUInt(); // Bitmap index 233 this->readUInt(); // Bitmap generation ID 234 // Old unsupported SkBitmapHeap format. No longer supported. 235 } else { 236 // The writer stored false, meaning the SkBitmap was not stored in an SkBitmapHeap. 237 const size_t length = this->readUInt(); 238 if (length > 0) { 239 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT 240 fDecodedBitmapIndex++; 241 #endif // DEBUG_NON_DETERMINISTIC_ASSERT 242 // A non-zero size means the SkBitmap was encoded. Read the data and pixel 243 // offset. 244 const void* data = this->skip(length); 245 const int32_t xOffset = this->readInt(); 246 const int32_t yOffset = this->readInt(); 247 SkIRect subset = SkIRect::MakeXYWH(xOffset, yOffset, width, height); 248 sk_sp<SkImage> image = fImageDeserializer->makeFromMemory(data, length, &subset); 249 if (image) { 250 return image; 251 } 252 253 // This bitmap was encoded when written, but we are unable to 254 // decode, possibly due to not having a decoder. Even though we 255 // weren't able to decode the pixels, the readbuffer should still 256 // be intact, so we return true with an empty bitmap, so we don't 257 // force an abort of the larger deserialize. 258 return MakeEmptyImage(width, height); 259 } else { 260 SkBitmap bitmap; 261 if (SkBitmap::ReadRawPixels(this, &bitmap)) { 262 bitmap.setImmutable(); 263 return SkImage::MakeFromBitmap(bitmap); 264 } 265 } 266 } 267 // Could not read the SkBitmap. Use a placeholder bitmap. 268 return nullptr; 269 } 270 271 sk_sp<SkImage> SkReadBuffer::readImage() { 272 if (fInflator) { 273 SkImage* img = fInflator->getImage(this->read32()); 274 return img ? sk_ref_sp(img) : nullptr; 275 } 276 277 int width = this->read32(); 278 int height = this->read32(); 279 if (width <= 0 || height <= 0) { // SkImage never has a zero dimension 280 this->validate(false); 281 return nullptr; 282 } 283 284 uint32_t encoded_size = this->getArrayCount(); 285 if (encoded_size == 0) { 286 // The image could not be encoded at serialization time - return an empty placeholder. 287 (void)this->readUInt(); // Swallow that encoded_size == 0 sentinel. 288 return MakeEmptyImage(width, height); 289 } 290 if (encoded_size == 1) { 291 // We had to encode the image as raw pixels via SkBitmap. 292 (void)this->readUInt(); // Swallow that encoded_size == 1 sentinel. 293 SkBitmap bm; 294 if (SkBitmap::ReadRawPixels(this, &bm)) { 295 return SkImage::MakeFromBitmap(bm); 296 } 297 return MakeEmptyImage(width, height); 298 } 299 300 // The SkImage encoded itself. 301 sk_sp<SkData> encoded(this->readByteArrayAsData()); 302 303 int originX = this->read32(); 304 int originY = this->read32(); 305 if (originX < 0 || originY < 0) { 306 this->validate(false); 307 return nullptr; 308 } 309 310 const SkIRect subset = SkIRect::MakeXYWH(originX, originY, width, height); 311 312 sk_sp<SkImage> image = fImageDeserializer->makeFromData(encoded.get(), &subset); 313 return image ? image : MakeEmptyImage(width, height); 314 } 315 316 sk_sp<SkTypeface> SkReadBuffer::readTypeface() { 317 if (fInflator) { 318 return sk_ref_sp(fInflator->getTypeface(this->read32())); 319 } 320 321 uint32_t index = this->readUInt(); 322 if (0 == index || index > (unsigned)fTFCount) { 323 return nullptr; 324 } else { 325 SkASSERT(fTFArray); 326 return sk_ref_sp(fTFArray[index - 1]); 327 } 328 } 329 330 SkFlattenable* SkReadBuffer::readFlattenable(SkFlattenable::Type ft) { 331 // 332 // TODO: confirm that ft matches the factory we decide to use 333 // 334 335 SkFlattenable::Factory factory = nullptr; 336 337 if (fInflator) { 338 factory = fInflator->getFactory(this->read32()); 339 if (!factory) { 340 return nullptr; 341 } 342 } else if (fFactoryCount > 0) { 343 int32_t index = fReader.readU32(); 344 if (0 == index) { 345 return nullptr; // writer failed to give us the flattenable 346 } 347 index -= 1; // we stored the index-base-1 348 if ((unsigned)index >= (unsigned)fFactoryCount) { 349 this->validate(false); 350 return nullptr; 351 } 352 factory = fFactoryArray[index]; 353 } else { 354 SkString name; 355 if (this->peekByte()) { 356 // If the first byte is non-zero, the flattenable is specified by a string. 357 this->readString(&name); 358 359 // Add the string to the dictionary. 360 fFlattenableDict.set(fFlattenableDict.count() + 1, name); 361 } else { 362 // Read the index. We are guaranteed that the first byte 363 // is zeroed, so we must shift down a byte. 364 uint32_t index = fReader.readU32() >> 8; 365 if (0 == index) { 366 return nullptr; // writer failed to give us the flattenable 367 } 368 369 SkString* namePtr = fFlattenableDict.find(index); 370 SkASSERT(namePtr); 371 name = *namePtr; 372 } 373 374 // Check if a custom Factory has been specified for this flattenable. 375 if (!(factory = this->getCustomFactory(name))) { 376 // If there is no custom Factory, check for a default. 377 if (!(factory = SkFlattenable::NameToFactory(name.c_str()))) { 378 return nullptr; // writer failed to give us the flattenable 379 } 380 } 381 } 382 383 // if we get here, factory may still be null, but if that is the case, the 384 // failure was ours, not the writer. 385 sk_sp<SkFlattenable> obj; 386 uint32_t sizeRecorded = fReader.readU32(); 387 if (factory) { 388 size_t offset = fReader.offset(); 389 obj = (*factory)(*this); 390 // check that we read the amount we expected 391 size_t sizeRead = fReader.offset() - offset; 392 if (sizeRecorded != sizeRead) { 393 this->validate(false); 394 return nullptr; 395 } 396 } else { 397 // we must skip the remaining data 398 fReader.skip(sizeRecorded); 399 } 400 return obj.release(); 401 } 402