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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (!fError) { | 104 if (!fError) { |
105 string->set(cptr, len); | 105 string->set(cptr, len); |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 void* SkValidatingReadBuffer::readEncodedString(size_t* length, SkPaint::TextEnc
oding encoding) { | 109 void* SkValidatingReadBuffer::readEncodedString(size_t* length, SkPaint::TextEnc
oding encoding) { |
110 const int32_t encodingType = this->readInt(); | 110 const int32_t encodingType = this->readInt(); |
111 this->validate(encodingType == encoding); | 111 this->validate(encodingType == encoding); |
112 *length = this->readInt(); | 112 *length = this->readInt(); |
113 const void* ptr = this->skip(SkAlign4(*length)); | 113 const void* ptr = this->skip(SkAlign4(*length)); |
114 void* data = NULL; | 114 void* data = nullptr; |
115 if (!fError) { | 115 if (!fError) { |
116 data = sk_malloc_throw(*length); | 116 data = sk_malloc_throw(*length); |
117 memcpy(data, ptr, *length); | 117 memcpy(data, ptr, *length); |
118 } | 118 } |
119 return data; | 119 return data; |
120 } | 120 } |
121 | 121 |
122 void SkValidatingReadBuffer::readPoint(SkPoint* point) { | 122 void SkValidatingReadBuffer::readPoint(SkPoint* point) { |
123 point->fX = this->readScalar(); | 123 point->fX = this->readScalar(); |
124 point->fY = this->readScalar(); | 124 point->fY = this->readScalar(); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } | 207 } |
208 | 208 |
209 uint32_t SkValidatingReadBuffer::getArrayCount() { | 209 uint32_t SkValidatingReadBuffer::getArrayCount() { |
210 const size_t inc = sizeof(uint32_t); | 210 const size_t inc = sizeof(uint32_t); |
211 fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc)
; | 211 fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc)
; |
212 return fError ? 0 : *(uint32_t*)fReader.peek(); | 212 return fError ? 0 : *(uint32_t*)fReader.peek(); |
213 } | 213 } |
214 | 214 |
215 SkTypeface* SkValidatingReadBuffer::readTypeface() { | 215 SkTypeface* SkValidatingReadBuffer::readTypeface() { |
216 // TODO: Implement this (securely) when needed | 216 // TODO: Implement this (securely) when needed |
217 return NULL; | 217 return nullptr; |
218 } | 218 } |
219 | 219 |
220 bool SkValidatingReadBuffer::validateAvailable(size_t size) { | 220 bool SkValidatingReadBuffer::validateAvailable(size_t size) { |
221 return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast
<uint32_t>(size))); | 221 return this->validate((size <= SK_MaxU32) && fReader.isAvailable(static_cast
<uint32_t>(size))); |
222 } | 222 } |
223 | 223 |
224 SkFlattenable* SkValidatingReadBuffer::readFlattenable(SkFlattenable::Type type)
{ | 224 SkFlattenable* SkValidatingReadBuffer::readFlattenable(SkFlattenable::Type type)
{ |
225 SkString name; | 225 SkString name; |
226 this->readString(&name); | 226 this->readString(&name); |
227 if (fError) { | 227 if (fError) { |
228 return NULL; | 228 return nullptr; |
229 } | 229 } |
230 | 230 |
231 // Is this the type we wanted ? | 231 // Is this the type we wanted ? |
232 const char* cname = name.c_str(); | 232 const char* cname = name.c_str(); |
233 SkFlattenable::Type baseType; | 233 SkFlattenable::Type baseType; |
234 if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) { | 234 if (!SkFlattenable::NameToType(cname, &baseType) || (baseType != type)) { |
235 return NULL; | 235 return nullptr; |
236 } | 236 } |
237 | 237 |
238 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(cname); | 238 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(cname); |
239 if (NULL == factory) { | 239 if (nullptr == factory) { |
240 return NULL; // writer failed to give us the flattenable | 240 return nullptr; // writer failed to give us the flattenable |
241 } | 241 } |
242 | 242 |
243 // if we get here, factory may still be null, but if that is the case, the | 243 // if we get here, factory may still be null, but if that is the case, the |
244 // failure was ours, not the writer. | 244 // failure was ours, not the writer. |
245 SkFlattenable* obj = NULL; | 245 SkFlattenable* obj = nullptr; |
246 uint32_t sizeRecorded = this->readUInt(); | 246 uint32_t sizeRecorded = this->readUInt(); |
247 if (factory) { | 247 if (factory) { |
248 size_t offset = fReader.offset(); | 248 size_t offset = fReader.offset(); |
249 obj = (*factory)(*this); | 249 obj = (*factory)(*this); |
250 // check that we read the amount we expected | 250 // check that we read the amount we expected |
251 size_t sizeRead = fReader.offset() - offset; | 251 size_t sizeRead = fReader.offset() - offset; |
252 this->validate(sizeRecorded == sizeRead); | 252 this->validate(sizeRecorded == sizeRead); |
253 if (fError) { | 253 if (fError) { |
254 // we could try to fix up the offset... | 254 // we could try to fix up the offset... |
255 SkSafeUnref(obj); | 255 SkSafeUnref(obj); |
256 obj = NULL; | 256 obj = nullptr; |
257 } | 257 } |
258 } else { | 258 } else { |
259 // we must skip the remaining data | 259 // we must skip the remaining data |
260 this->skip(sizeRecorded); | 260 this->skip(sizeRecorded); |
261 SkASSERT(false); | 261 SkASSERT(false); |
262 } | 262 } |
263 return obj; | 263 return obj; |
264 } | 264 } |
265 | 265 |
266 void SkValidatingReadBuffer::skipFlattenable() { | 266 void SkValidatingReadBuffer::skipFlattenable() { |
267 SkString name; | 267 SkString name; |
268 this->readString(&name); | 268 this->readString(&name); |
269 if (fError) { | 269 if (fError) { |
270 return; | 270 return; |
271 } | 271 } |
272 uint32_t sizeRecorded = this->readUInt(); | 272 uint32_t sizeRecorded = this->readUInt(); |
273 this->skip(sizeRecorded); | 273 this->skip(sizeRecorded); |
274 } | 274 } |
OLD | NEW |