2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkErrorInternals.h"
10 #include "SkValidatingReadBuffer.h"
12 #include "SkTypeface.h"
14 SkValidatingReadBuffer::SkValidatingReadBuffer(const void* data, size_t size) :
16 this->setMemory(data, size);
17 this->setFlags(SkReadBuffer::kValidation_Flag);
20 SkValidatingReadBuffer::~SkValidatingReadBuffer() {
23 bool SkValidatingReadBuffer::validate(bool isValid) {
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());
32 bool SkValidatingReadBuffer::isValid() const {
36 void SkValidatingReadBuffer::setMemory(const void* data, size_t size) {
37 this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size));
39 fReader.setMemory(data, size);
43 const void* SkValidatingReadBuffer::skip(size_t size) {
44 size_t inc = SkAlign4(size);
45 const void* addr = fReader.peek();
46 this->validate(IsPtrAlign4(addr) && fReader.isAvailable(inc));
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.
58 bool SkValidatingReadBuffer::readBool() {
59 uint32_t value = this->readInt();
60 // Boolean value should be either 0 or 1
61 this->validate(!(value & ~1));
65 SkColor SkValidatingReadBuffer::readColor() {
66 return this->readInt();
69 SkFixed SkValidatingReadBuffer::readFixed() {
70 return this->readInt();
73 int32_t SkValidatingReadBuffer::readInt() {
74 const size_t inc = sizeof(int32_t);
75 this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
76 return fError ? 0 : fReader.readInt();
79 SkScalar SkValidatingReadBuffer::readScalar() {
80 const size_t inc = sizeof(SkScalar);
81 this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
82 return fError ? 0 : fReader.readScalar();
85 uint32_t SkValidatingReadBuffer::readUInt() {
86 return this->readInt();
89 int32_t SkValidatingReadBuffer::read32() {
90 return this->readInt();
93 void SkValidatingReadBuffer::readString(SkString* string) {
94 const size_t len = this->readInt();
95 const void* ptr = fReader.peek();
96 const char* cptr = (const char*)ptr;
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);
102 this->validate(cptr[len] == '\0');
105 string->set(cptr, len);
109 void* SkValidatingReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding encoding) {
110 const int32_t encodingType = this->readInt();
111 this->validate(encodingType == encoding);
112 *length = this->readInt();
113 const void* ptr = this->skip(SkAlign4(*length));
116 data = sk_malloc_throw(*length);
117 memcpy(data, ptr, *length);
122 void SkValidatingReadBuffer::readPoint(SkPoint* point) {
123 point->fX = this->readScalar();
124 point->fY = this->readScalar();
127 void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) {
130 size = matrix->readFromMemory(fReader.peek(), fReader.available());
131 this->validate((SkAlign4(size) == size) && (0 != size));
134 (void)this->skip(size);
138 void SkValidatingReadBuffer::readIRect(SkIRect* rect) {
139 const void* ptr = this->skip(sizeof(SkIRect));
141 memcpy(rect, ptr, sizeof(SkIRect));
145 void SkValidatingReadBuffer::readRect(SkRect* rect) {
146 const void* ptr = this->skip(sizeof(SkRect));
148 memcpy(rect, ptr, sizeof(SkRect));
152 void SkValidatingReadBuffer::readRegion(SkRegion* region) {
155 size = region->readFromMemory(fReader.peek(), fReader.available());
156 this->validate((SkAlign4(size) == size) && (0 != size));
159 (void)this->skip(size);
163 void SkValidatingReadBuffer::readPath(SkPath* path) {
166 size = path->readFromMemory(fReader.peek(), fReader.available());
167 this->validate((SkAlign4(size) == size) && (0 != size));
170 (void)this->skip(size);
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;
179 const void* ptr = this->skip(SkAlign4(byteLength));
181 memcpy(value, ptr, byteLength);
187 bool SkValidatingReadBuffer::readByteArray(void* value, size_t size) {
188 return readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned char));
191 bool SkValidatingReadBuffer::readColorArray(SkColor* colors, size_t size) {
192 return readArray(colors, size, sizeof(SkColor));
195 bool SkValidatingReadBuffer::readIntArray(int32_t* values, size_t size) {
196 return readArray(values, size, sizeof(int32_t));
199 bool SkValidatingReadBuffer::readPointArray(SkPoint* points, size_t size) {
200 return readArray(points, size, sizeof(SkPoint));
203 bool SkValidatingReadBuffer::readScalarArray(SkScalar* values, size_t size) {
204 return readArray(values, size, sizeof(SkScalar));
207 uint32_t SkValidatingReadBuffer::getArrayCount() {
208 const size_t inc = sizeof(uint32_t);
209 fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc);
210 return fError ? 0 : *(uint32_t*)fReader.peek();
213 void SkValidatingReadBuffer::readBitmap(SkBitmap* bitmap) {
214 const int width = this->readInt();
215 const int height = this->readInt();
216 const size_t length = this->readUInt();
217 // A size of zero means the SkBitmap was simply flattened.
218 this->validate(length == 0);
222 bitmap->unflatten(*this);
223 this->validate((bitmap->width() == width) && (bitmap->height() == height));
226 SkTypeface* SkValidatingReadBuffer::readTypeface() {
227 // TODO: Implement this (securely) when needed
231 bool SkValidatingReadBuffer::validateAvailable(size_t size) {
232 return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast<uint32_t>(size)));
235 SkFlattenable* SkValidatingReadBuffer::readFlattenable(SkFlattenable::Type type) {
237 this->readString(&name);
242 // Is this the type we wanted ?
243 const char* cname = name.c_str();
244 SkFlattenable::Type baseType;
245 if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) {
249 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(cname);
250 if (NULL == factory) {
251 return NULL; // writer failed to give us the flattenable
254 // if we get here, factory may still be null, but if that is the case, the
255 // failure was ours, not the writer.
256 SkFlattenable* obj = NULL;
257 uint32_t sizeRecorded = this->readUInt();
259 uint32_t offset = fReader.offset();
260 obj = (*factory)(*this);
261 // check that we read the amount we expected
262 uint32_t sizeRead = fReader.offset() - offset;
263 this->validate(sizeRecorded == sizeRead);
265 // we could try to fix up the offset...
270 // we must skip the remaining data
271 this->skip(sizeRecorded);