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 uint8_t firstByte = this->peekByte(); |
reed1
2016/04/22 15:06:08
Can we assert that there is no "factory" here (sin
msarett
2016/04/22 18:54:09
I've made a protected getter for the factoryCount
| |
223 this->readString(&name); | |
224 if (fError) { | 231 if (fError) { |
225 return nullptr; | 232 return nullptr; |
226 } | 233 } |
227 | 234 |
235 SkString name; | |
reed1
2016/04/22 15:06:08
Is it easy to share this section in a helper funct
msarett
2016/04/22 18:54:09
I refactored this, but it ultimately didn't work (
| |
236 if (firstByte) { | |
237 // If the first byte is non-zero, the flattenable is specified by a stri ng. | |
238 this->readString(&name); | |
239 if (fError) { | |
240 return nullptr; | |
241 } | |
242 | |
243 // Add the string to the dictionary. | |
244 fFlattenableDict.set(fFlattenableDict.count() + 1, name); | |
245 } else { | |
246 // Read the index. We are guaranteed that the first byte | |
247 // is zeroed, so we must shift down a byte. | |
248 uint32_t index = fReader.readU32() >> 8; | |
249 if (0 == index) { | |
250 return nullptr; // writer failed to give us the flattenable | |
251 } | |
252 | |
253 SkString* namePtr = fFlattenableDict.find(index); | |
254 SkASSERT(namePtr); | |
255 name = *namePtr; | |
256 } | |
257 | |
228 // Is this the type we wanted ? | 258 // Is this the type we wanted ? |
229 const char* cname = name.c_str(); | 259 const char* cname = name.c_str(); |
230 SkFlattenable::Type baseType; | 260 SkFlattenable::Type baseType; |
231 if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) { | 261 if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) { |
232 return nullptr; | 262 return nullptr; |
233 } | 263 } |
234 | 264 |
235 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(cname); | 265 // Get the factory for this flattenable. |
236 if (nullptr == factory) { | 266 SkFlattenable::Factory factory = this->getCustomFactory(name); |
237 return nullptr; // writer failed to give us the flattenable | 267 if (!factory) { |
268 factory = SkFlattenable::NameToFactory(cname); | |
269 if (!factory) { | |
270 return nullptr; // writer failed to give us the flattenable | |
271 } | |
238 } | 272 } |
239 | 273 |
240 // if we get here, factory may still be null, but if that is the case, the | 274 // If we get here, the factory is non-null. |
241 // failure was ours, not the writer. | |
242 sk_sp<SkFlattenable> obj; | 275 sk_sp<SkFlattenable> obj; |
243 uint32_t sizeRecorded = this->readUInt(); | 276 uint32_t sizeRecorded = this->readUInt(); |
244 if (factory) { | 277 size_t offset = fReader.offset(); |
245 size_t offset = fReader.offset(); | 278 obj = (*factory)(*this); |
246 obj = (*factory)(*this); | 279 // check that we read the amount we expected |
247 // check that we read the amount we expected | 280 size_t sizeRead = fReader.offset() - offset; |
248 size_t sizeRead = fReader.offset() - offset; | 281 this->validate(sizeRecorded == sizeRead); |
249 this->validate(sizeRecorded == sizeRead); | 282 if (fError) { |
250 if (fError) { | 283 obj = nullptr; |
251 obj = nullptr; | |
252 } | |
253 } else { | |
254 // we must skip the remaining data | |
255 this->skip(sizeRecorded); | |
256 SkASSERT(false); | |
257 } | 284 } |
258 return obj.release(); | 285 return obj.release(); |
259 } | 286 } |
260 | 287 |
261 void SkValidatingReadBuffer::skipFlattenable() { | 288 void SkValidatingReadBuffer::skipFlattenable() { |
262 SkString name; | 289 SkString name; |
263 this->readString(&name); | 290 this->readString(&name); |
264 if (fError) { | 291 if (fError) { |
265 return; | 292 return; |
266 } | 293 } |
267 uint32_t sizeRecorded = this->readUInt(); | 294 uint32_t sizeRecorded = this->readUInt(); |
268 this->skip(sizeRecorded); | 295 this->skip(sizeRecorded); |
269 } | 296 } |
OLD | NEW |