1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBitmap.h"
#include "SkValidatingReadBuffer.h"
#include "SkStream.h"
#include "SkTypeface.h"
SkValidatingReadBuffer::SkValidatingReadBuffer(const void* data, size_t size) :
fError(false) {
this->setMemory(data, size);
this->setFlags(SkReadBuffer::kValidation_Flag);
}
SkValidatingReadBuffer::~SkValidatingReadBuffer() {
}
bool SkValidatingReadBuffer::validate(bool isValid) {
if (!fError && !isValid) {
// When an error is found, send the read cursor to the end of the stream
fReader.skip(fReader.available());
fError = true;
}
return !fError;
}
bool SkValidatingReadBuffer::isValid() const {
return !fError;
}
void SkValidatingReadBuffer::setMemory(const void* data, size_t size) {
this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size));
if (!fError) {
fReader.setMemory(data, size);
}
}
const void* SkValidatingReadBuffer::skip(size_t size) {
size_t inc = SkAlign4(size);
this->validate(inc >= size);
const void* addr = fReader.peek();
this->validate(IsPtrAlign4(addr) && fReader.isAvailable(inc));
if (fError) {
return nullptr;
}
fReader.skip(size);
return addr;
}
// All the methods in this file funnel down into either readInt(), readScalar() or skip(),
// followed by a memcpy. So we've got all our validation in readInt(), readScalar() and skip();
// if they fail they'll return a zero value or skip nothing, respectively, and set fError to
// true, which the caller should check to see if an error occurred during the read operation.
bool SkValidatingReadBuffer::readBool() {
uint32_t value = this->readInt();
// Boolean value should be either 0 or 1
this->validate(!(value & ~1));
return value != 0;
}
SkColor SkValidatingReadBuffer::readColor() {
return this->readInt();
}
int32_t SkValidatingReadBuffer::readInt() {
const size_t inc = sizeof(int32_t);
this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
return fError ? 0 : fReader.readInt();
}
SkScalar SkValidatingReadBuffer::readScalar() {
const size_t inc = sizeof(SkScalar);
this->validate(IsPtrAlign4(fReader.peek()) && fReader.isAvailable(inc));
return fError ? 0 : fReader.readScalar();
}
uint32_t SkValidatingReadBuffer::readUInt() {
return this->readInt();
}
int32_t SkValidatingReadBuffer::read32() {
return this->readInt();
}
uint8_t SkValidatingReadBuffer::peekByte() {
if (fReader.available() <= 0) {
fError = true;
return 0;
}
return *((uint8_t*) fReader.peek());
}
void SkValidatingReadBuffer::readString(SkString* string) {
const size_t len = this->readUInt();
const void* ptr = fReader.peek();
const char* cptr = (const char*)ptr;
// skip over the string + '\0' and then pad to a multiple of 4
const size_t alignedSize = SkAlign4(len + 1);
this->skip(alignedSize);
if (!fError) {
this->validate(cptr[len] == '\0');
}
if (!fError) {
string->set(cptr, len);
}
}
void SkValidatingReadBuffer::readColor4f(SkColor4f* color) {
const void* ptr = this->skip(sizeof(SkColor4f));
if (!fError) {
memcpy(color, ptr, sizeof(SkColor4f));
}
}
void SkValidatingReadBuffer::readPoint(SkPoint* point) {
point->fX = this->readScalar();
point->fY = this->readScalar();
}
void SkValidatingReadBuffer::readPoint3(SkPoint3* point) {
point->fX = this->readScalar();
point->fY = this->readScalar();
point->fZ = this->readScalar();
}
void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) {
size_t size = 0;
if (!fError) {
size = matrix->readFromMemory(fReader.peek(), fReader.available());
this->validate((SkAlign4(size) == size) && (0 != size));
}
if (!fError) {
(void)this->skip(size);
}
}
void SkValidatingReadBuffer::readIRect(SkIRect* rect) {
const void* ptr = this->skip(sizeof(SkIRect));
if (!fError) {
memcpy(rect, ptr, sizeof(SkIRect));
}
}
void SkValidatingReadBuffer::readRect(SkRect* rect) {
const void* ptr = this->skip(sizeof(SkRect));
if (!fError) {
memcpy(rect, ptr, sizeof(SkRect));
}
}
void SkValidatingReadBuffer::readRRect(SkRRect* rrect) {
const void* ptr = this->skip(sizeof(SkRRect));
if (!fError) {
memcpy(rrect, ptr, sizeof(SkRRect));
this->validate(rrect->isValid());
}
if (fError) {
rrect->setEmpty();
}
}
void SkValidatingReadBuffer::readRegion(SkRegion* region) {
size_t size = 0;
if (!fError) {
size = region->readFromMemory(fReader.peek(), fReader.available());
this->validate((SkAlign4(size) == size) && (0 != size));
}
if (!fError) {
(void)this->skip(size);
}
}
void SkValidatingReadBuffer::readPath(SkPath* path) {
size_t size = 0;
if (!fError) {
size = path->readFromMemory(fReader.peek(), fReader.available());
this->validate((SkAlign4(size) == size) && (0 != size));
}
if (!fError) {
(void)this->skip(size);
}
}
bool SkValidatingReadBuffer::readArray(void* value, size_t size, size_t elementSize) {
const uint32_t count = this->getArrayCount();
this->validate(size == count);
(void)this->skip(sizeof(uint32_t)); // Skip array count
const uint64_t byteLength64 = sk_64_mul(count, elementSize);
const size_t byteLength = count * elementSize;
this->validate(byteLength == byteLength64);
const void* ptr = this->skip(SkAlign4(byteLength));
if (!fError) {
memcpy(value, ptr, byteLength);
return true;
}
return false;
}
bool SkValidatingReadBuffer::readByteArray(void* value, size_t size) {
return this->readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned char));
}
bool SkValidatingReadBuffer::readColorArray(SkColor* colors, size_t size) {
return this->readArray(colors, size, sizeof(SkColor));
}
bool SkValidatingReadBuffer::readColor4fArray(SkColor4f* colors, size_t size) {
return this->readArray(colors, size, sizeof(SkColor4f));
}
bool SkValidatingReadBuffer::readIntArray(int32_t* values, size_t size) {
return this->readArray(values, size, sizeof(int32_t));
}
bool SkValidatingReadBuffer::readPointArray(SkPoint* points, size_t size) {
return this->readArray(points, size, sizeof(SkPoint));
}
bool SkValidatingReadBuffer::readScalarArray(SkScalar* values, size_t size) {
return this->readArray(values, size, sizeof(SkScalar));
}
uint32_t SkValidatingReadBuffer::getArrayCount() {
const size_t inc = sizeof(uint32_t);
fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc);
return fError ? 0 : *(uint32_t*)fReader.peek();
}
bool SkValidatingReadBuffer::validateAvailable(size_t size) {
return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast<uint32_t>(size)));
}
SkFlattenable* SkValidatingReadBuffer::readFlattenable(SkFlattenable::Type type) {
// The validating read buffer always uses strings and string-indices for unflattening.
SkASSERT(0 == this->factoryCount());
uint8_t firstByte = this->peekByte();
if (fError) {
return nullptr;
}
SkString name;
if (firstByte) {
// If the first byte is non-zero, the flattenable is specified by a string.
this->readString(&name);
if (fError) {
return nullptr;
}
// Add the string to the dictionary.
fFlattenableDict.set(fFlattenableDict.count() + 1, name);
} else {
// Read the index. We are guaranteed that the first byte
// is zeroed, so we must shift down a byte.
uint32_t index = this->readUInt() >> 8;
if (0 == index) {
return nullptr; // writer failed to give us the flattenable
}
SkString* namePtr = fFlattenableDict.find(index);
if (!namePtr) {
return nullptr;
}
name = *namePtr;
}
// Is this the type we wanted ?
const char* cname = name.c_str();
SkFlattenable::Type baseType;
if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) {
return nullptr;
}
// Get the factory for this flattenable.
SkFlattenable::Factory factory = this->getCustomFactory(name);
if (!factory) {
factory = SkFlattenable::NameToFactory(cname);
if (!factory) {
return nullptr; // writer failed to give us the flattenable
}
}
// If we get here, the factory is non-null.
sk_sp<SkFlattenable> obj;
uint32_t sizeRecorded = this->readUInt();
size_t offset = fReader.offset();
obj = (*factory)(*this);
// check that we read the amount we expected
size_t sizeRead = fReader.offset() - offset;
this->validate(sizeRecorded == sizeRead);
if (fError) {
obj = nullptr;
}
return obj.release();
}
|