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
14SkValidatingReadBuffer::SkValidatingReadBuffer(const void* data, size_t size) :
15    fError(false) {
16    this->setMemory(data, size);
17    this->setFlags(SkReadBuffer::kValidation_Flag);
18}
19
20SkValidatingReadBuffer::~SkValidatingReadBuffer() {
21}
22
23bool 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());
27        fError = true;
28    }
29    return !fError;
30}
31
32bool SkValidatingReadBuffer::isValid() const {
33    return !fError;
34}
35
36void SkValidatingReadBuffer::setMemory(const void* data, size_t size) {
37    this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size));
38    if (!fError) {
39        fReader.setMemory(data, size);
40    }
41}
42
43const 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));
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
58bool SkValidatingReadBuffer::readBool() {
59    uint32_t value = this->readInt();
60    // Boolean value should be either 0 or 1
61    this->validate(!(value & ~1));
62    return value != 0;
63}
64
65SkColor SkValidatingReadBuffer::readColor() {
66    return this->readInt();
67}
68
69SkFixed SkValidatingReadBuffer::readFixed() {
70    return this->readInt();
71}
72
73int32_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();
77}
78
79SkScalar SkValidatingReadBuffer::readScalar() {
80    const size_t inc = sizeof(SkScalar);
81    this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
82    return fError ? 0 : fReader.readScalar();
83}
84
85uint32_t SkValidatingReadBuffer::readUInt() {
86    return this->readInt();
87}
88
89int32_t SkValidatingReadBuffer::read32() {
90    return this->readInt();
91}
92
93void SkValidatingReadBuffer::readString(SkString* string) {
94    const size_t len = this->readUInt();
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);
101    if (!fError) {
102        this->validate(cptr[len] == '\0');
103    }
104    if (!fError) {
105        string->set(cptr, len);
106    }
107}
108
109void* 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));
114    void* data = NULL;
115    if (!fError) {
116        data = sk_malloc_throw(*length);
117        memcpy(data, ptr, *length);
118    }
119    return data;
120}
121
122void SkValidatingReadBuffer::readPoint(SkPoint* point) {
123    point->fX = this->readScalar();
124    point->fY = this->readScalar();
125}
126
127void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) {
128    size_t size = 0;
129    if (!fError) {
130        size = matrix->readFromMemory(fReader.peek(), fReader.available());
131        this->validate((SkAlign4(size) == size) && (0 != size));
132    }
133    if (!fError) {
134        (void)this->skip(size);
135    }
136}
137
138void SkValidatingReadBuffer::readIRect(SkIRect* rect) {
139    const void* ptr = this->skip(sizeof(SkIRect));
140    if (!fError) {
141        memcpy(rect, ptr, sizeof(SkIRect));
142    }
143}
144
145void SkValidatingReadBuffer::readRect(SkRect* rect) {
146    const void* ptr = this->skip(sizeof(SkRect));
147    if (!fError) {
148        memcpy(rect, ptr, sizeof(SkRect));
149    }
150}
151
152void SkValidatingReadBuffer::readRegion(SkRegion* region) {
153    size_t size = 0;
154    if (!fError) {
155        size = region->readFromMemory(fReader.peek(), fReader.available());
156        this->validate((SkAlign4(size) == size) && (0 != size));
157    }
158    if (!fError) {
159        (void)this->skip(size);
160    }
161}
162
163void SkValidatingReadBuffer::readPath(SkPath* path) {
164    size_t size = 0;
165    if (!fError) {
166        size = path->readFromMemory(fReader.peek(), fReader.available());
167        this->validate((SkAlign4(size) == size) && (0 != size));
168    }
169    if (!fError) {
170        (void)this->skip(size);
171    }
172}
173
174bool 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));
180    if (!fError) {
181        memcpy(value, ptr, byteLength);
182        return true;
183    }
184    return false;
185}
186
187bool SkValidatingReadBuffer::readByteArray(void* value, size_t size) {
188    return readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned char));
189}
190
191bool SkValidatingReadBuffer::readColorArray(SkColor* colors, size_t size) {
192    return readArray(colors, size, sizeof(SkColor));
193}
194
195bool SkValidatingReadBuffer::readIntArray(int32_t* values, size_t size) {
196    return readArray(values, size, sizeof(int32_t));
197}
198
199bool SkValidatingReadBuffer::readPointArray(SkPoint* points, size_t size) {
200    return readArray(points, size, sizeof(SkPoint));
201}
202
203bool SkValidatingReadBuffer::readScalarArray(SkScalar* values, size_t size) {
204    return readArray(values, size, sizeof(SkScalar));
205}
206
207uint32_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();
211}
212
213SkTypeface* SkValidatingReadBuffer::readTypeface() {
214    // TODO: Implement this (securely) when needed
215    return NULL;
216}
217
218bool SkValidatingReadBuffer::validateAvailable(size_t size) {
219    return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast<uint32_t>(size)));
220}
221
222SkFlattenable* 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) {
246        size_t offset = fReader.offset();
247        obj = (*factory)(*this);
248        // check that we read the amount we expected
249        size_t sizeRead = fReader.offset() - offset;
250        this->validate(sizeRecorded == sizeRead);
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}
263
264void 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}
273