OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkErrorInternals.h" | 9 #include "SkErrorInternals.h" |
10 #include "SkValidatingReadBuffer.h" | 10 #include "SkValidatingReadBuffer.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 79 } |
80 | 80 |
81 uint32_t SkValidatingReadBuffer::readUInt() { | 81 uint32_t SkValidatingReadBuffer::readUInt() { |
82 return this->readInt(); | 82 return this->readInt(); |
83 } | 83 } |
84 | 84 |
85 int32_t SkValidatingReadBuffer::read32() { | 85 int32_t SkValidatingReadBuffer::read32() { |
86 return this->readInt(); | 86 return this->readInt(); |
87 } | 87 } |
88 | 88 |
| 89 uint8_t SkValidatingReadBuffer::peekByte() { |
| 90 if (fReader.available() <= 0) { |
| 91 fError = true; |
| 92 return 0; |
| 93 } |
| 94 return *((uint8_t*) fReader.peek()); |
| 95 } |
| 96 |
89 void SkValidatingReadBuffer::readString(SkString* string) { | 97 void SkValidatingReadBuffer::readString(SkString* string) { |
90 const size_t len = this->readUInt(); | 98 const size_t len = this->readUInt(); |
91 const void* ptr = fReader.peek(); | 99 const void* ptr = fReader.peek(); |
92 const char* cptr = (const char*)ptr; | 100 const char* cptr = (const char*)ptr; |
93 | 101 |
94 // skip over the string + '\0' and then pad to a multiple of 4 | 102 // skip over the string + '\0' and then pad to a multiple of 4 |
95 const size_t alignedSize = SkAlign4(len + 1); | 103 const size_t alignedSize = SkAlign4(len + 1); |
96 this->skip(alignedSize); | 104 this->skip(alignedSize); |
97 if (!fError) { | 105 if (!fError) { |
98 this->validate(cptr[len] == '\0'); | 106 this->validate(cptr[len] == '\0'); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 SkASSERT(false); | 220 SkASSERT(false); |
213 // TODO: Implement this (securely) when needed | 221 // TODO: Implement this (securely) when needed |
214 return nullptr; | 222 return nullptr; |
215 } | 223 } |
216 | 224 |
217 bool SkValidatingReadBuffer::validateAvailable(size_t size) { | 225 bool SkValidatingReadBuffer::validateAvailable(size_t size) { |
218 return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast
<uint32_t>(size))); | 226 return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast
<uint32_t>(size))); |
219 } | 227 } |
220 | 228 |
221 SkFlattenable* SkValidatingReadBuffer::readFlattenable(SkFlattenable::Type type)
{ | 229 SkFlattenable* SkValidatingReadBuffer::readFlattenable(SkFlattenable::Type type)
{ |
222 SkString name; | 230 // The validating read buffer always uses strings and string-indices for unf
lattening. |
223 this->readString(&name); | 231 SkASSERT(0 == this->factoryCount()); |
| 232 |
| 233 uint8_t firstByte = this->peekByte(); |
224 if (fError) { | 234 if (fError) { |
225 return nullptr; | 235 return nullptr; |
226 } | 236 } |
227 | 237 |
| 238 SkString name; |
| 239 if (firstByte) { |
| 240 // If the first byte is non-zero, the flattenable is specified by a stri
ng. |
| 241 this->readString(&name); |
| 242 if (fError) { |
| 243 return nullptr; |
| 244 } |
| 245 |
| 246 // Add the string to the dictionary. |
| 247 fFlattenableDict.set(fFlattenableDict.count() + 1, name); |
| 248 } else { |
| 249 // Read the index. We are guaranteed that the first byte |
| 250 // is zeroed, so we must shift down a byte. |
| 251 uint32_t index = fReader.readU32() >> 8; |
| 252 if (0 == index) { |
| 253 return nullptr; // writer failed to give us the flattenable |
| 254 } |
| 255 |
| 256 SkString* namePtr = fFlattenableDict.find(index); |
| 257 SkASSERT(namePtr); |
| 258 name = *namePtr; |
| 259 } |
| 260 |
228 // Is this the type we wanted ? | 261 // Is this the type we wanted ? |
229 const char* cname = name.c_str(); | 262 const char* cname = name.c_str(); |
230 SkFlattenable::Type baseType; | 263 SkFlattenable::Type baseType; |
231 if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) { | 264 if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) { |
232 return nullptr; | 265 return nullptr; |
233 } | 266 } |
234 | 267 |
235 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(cname); | 268 // Get the factory for this flattenable. |
236 if (nullptr == factory) { | 269 SkFlattenable::Factory factory = this->getCustomFactory(name); |
237 return nullptr; // writer failed to give us the flattenable | 270 if (!factory) { |
| 271 factory = SkFlattenable::NameToFactory(cname); |
| 272 if (!factory) { |
| 273 return nullptr; // writer failed to give us the flattenable |
| 274 } |
238 } | 275 } |
239 | 276 |
240 // if we get here, factory may still be null, but if that is the case, the | 277 // If we get here, the factory is non-null. |
241 // failure was ours, not the writer. | |
242 sk_sp<SkFlattenable> obj; | 278 sk_sp<SkFlattenable> obj; |
243 uint32_t sizeRecorded = this->readUInt(); | 279 uint32_t sizeRecorded = this->readUInt(); |
244 if (factory) { | 280 size_t offset = fReader.offset(); |
245 size_t offset = fReader.offset(); | 281 obj = (*factory)(*this); |
246 obj = (*factory)(*this); | 282 // check that we read the amount we expected |
247 // check that we read the amount we expected | 283 size_t sizeRead = fReader.offset() - offset; |
248 size_t sizeRead = fReader.offset() - offset; | 284 this->validate(sizeRecorded == sizeRead); |
249 this->validate(sizeRecorded == sizeRead); | 285 if (fError) { |
250 if (fError) { | 286 obj = nullptr; |
251 obj = nullptr; | |
252 } | |
253 } else { | |
254 // we must skip the remaining data | |
255 this->skip(sizeRecorded); | |
256 SkASSERT(false); | |
257 } | 287 } |
258 return obj.release(); | 288 return obj.release(); |
259 } | 289 } |
260 | 290 |
261 void SkValidatingReadBuffer::skipFlattenable() { | 291 void SkValidatingReadBuffer::skipFlattenable() { |
262 SkString name; | 292 SkString name; |
263 this->readString(&name); | 293 this->readString(&name); |
264 if (fError) { | 294 if (fError) { |
265 return; | 295 return; |
266 } | 296 } |
267 uint32_t sizeRecorded = this->readUInt(); | 297 uint32_t sizeRecorded = this->readUInt(); |
268 this->skip(sizeRecorded); | 298 this->skip(sizeRecorded); |
269 } | 299 } |
OLD | NEW |