blob: 037a99465b2853f34627b379ba32323277a3f3ea [file] [log] [blame]
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +00001/*
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.org77e079a2013-10-28 15:52:02 +000014SkValidatingReadBuffer::SkValidatingReadBuffer(const void* data, size_t size) :
15 fError(false) {
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000016 this->setMemory(data, size);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000017 this->setFlags(SkReadBuffer::kValidation_Flag);
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000018}
19
20SkValidatingReadBuffer::~SkValidatingReadBuffer() {
21}
22
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +000023bool SkValidatingReadBuffer::validate(bool isValid) {
commit-bot@chromium.org02512882013-10-31 18:37:50 +000024 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.org8f457e32013-11-08 19:22:57 +000029 return !fError;
commit-bot@chromium.org02512882013-10-31 18:37:50 +000030}
31
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +000032bool SkValidatingReadBuffer::isValid() const {
33 return !fError;
34}
35
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000036void SkValidatingReadBuffer::setMemory(const void* data, size_t size) {
commit-bot@chromium.org02512882013-10-31 18:37:50 +000037 this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000038 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();
commit-bot@chromium.org02512882013-10-31 18:37:50 +000046 this->validate(IsPtrAlign4(addr) && fReader.isAvailable(inc));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000047 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() {
commit-bot@chromium.orgd594dbe2013-10-23 18:33:18 +000059 uint32_t value = this->readInt();
60 // Boolean value should be either 0 or 1
commit-bot@chromium.org02512882013-10-31 18:37:50 +000061 this->validate(!(value & ~1));
commit-bot@chromium.orgd594dbe2013-10-23 18:33:18 +000062 return value != 0;
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000063}
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);
commit-bot@chromium.org02512882013-10-31 18:37:50 +000075 this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000076 return fError ? 0 : fReader.readInt();
77}
78
79SkScalar SkValidatingReadBuffer::readScalar() {
80 const size_t inc = sizeof(SkScalar);
commit-bot@chromium.org02512882013-10-31 18:37:50 +000081 this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000082 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) {
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000094 const size_t len = this->readUInt();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000095 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.org46a9bb82013-11-22 16:43:14 +0000101 if (!fError) {
102 this->validate(cptr[len] == '\0');
103 }
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000104 if (!fError) {
105 string->set(cptr, len);
106 }
107}
108
109void* SkValidatingReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding encoding) {
commit-bot@chromium.org46a9bb82013-11-22 16:43:14 +0000110 const int32_t encodingType = this->readInt();
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000111 this->validate(encodingType == encoding);
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000112 *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) {
commit-bot@chromium.org46a9bb82013-11-22 16:43:14 +0000123 point->fX = this->readScalar();
124 point->fY = this->readScalar();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000125}
126
127void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000128 size_t size = 0;
129 if (!fError) {
130 size = matrix->readFromMemory(fReader.peek(), fReader.available());
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000131 this->validate((SkAlign4(size) == size) && (0 != size));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000132 }
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000133 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) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000153 size_t size = 0;
154 if (!fError) {
155 size = region->readFromMemory(fReader.peek(), fReader.available());
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000156 this->validate((SkAlign4(size) == size) && (0 != size));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000157 }
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000158 if (!fError) {
159 (void)this->skip(size);
160 }
161}
162
163void SkValidatingReadBuffer::readPath(SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000164 size_t size = 0;
165 if (!fError) {
166 size = path->readFromMemory(fReader.peek(), fReader.available());
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000167 this->validate((SkAlign4(size) == size) && (0 != size));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000168 }
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000169 if (!fError) {
170 (void)this->skip(size);
171 }
172}
173
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000174bool 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.orgc0b7e102013-10-23 17:06:21 +0000179 const void* ptr = this->skip(SkAlign4(byteLength));
180 if (!fError) {
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000181 memcpy(value, ptr, byteLength);
182 return true;
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000183 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000184 return false;
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000185}
186
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000187bool SkValidatingReadBuffer::readByteArray(void* value, size_t size) {
188 return readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned char));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000189}
190
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000191bool SkValidatingReadBuffer::readColorArray(SkColor* colors, size_t size) {
192 return readArray(colors, size, sizeof(SkColor));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000193}
194
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000195bool 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));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000205}
206
207uint32_t SkValidatingReadBuffer::getArrayCount() {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000208 const size_t inc = sizeof(uint32_t);
209 fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000210 return fError ? 0 : *(uint32_t*)fReader.peek();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000211}
212
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000213SkTypeface* SkValidatingReadBuffer::readTypeface() {
214 // TODO: Implement this (securely) when needed
215 return NULL;
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000216}
217
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000218bool SkValidatingReadBuffer::validateAvailable(size_t size) {
219 return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast<uint32_t>(size)));
220}
221
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000222SkFlattenable* 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.orgf1177812014-04-23 19:19:44 +0000246 size_t offset = fReader.offset();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000247 obj = (*factory)(*this);
248 // check that we read the amount we expected
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +0000249 size_t sizeRead = fReader.offset() - offset;
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000250 this->validate(sizeRecorded == sizeRead);
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000251 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.org83f23d82014-05-22 12:27:41 +0000263
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}