Debian Codegraph @

src/core/SkReadBuffer.cpp

raw
1 /*
2 * Copyright 2012 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 "src/core/SkReadBuffer.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageGenerator.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkM44.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPath.h"
18 #include "include/core/SkPixmap.h"
19 #include "include/core/SkPoint3.h"
20 #include "include/core/SkRRect.h"
21 #include "include/core/SkRegion.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/private/base/SkMalloc.h"
26 #include "src/base/SkAutoMalloc.h"
27 #include "src/base/SkMathPriv.h"
28 #include "src/base/SkSafeMath.h"
29 #include "src/core/SkMatrixPriv.h"
30 #include "src/core/SkMipmapBuilder.h"
31 #include "src/core/SkWriteBuffer.h"
32
33 #include <memory>
34 #include <optional>
35 #include <utility>
36
37 namespace {
38 // This generator intentionally should always fail on all attempts to get its pixels,
39 // simulating a bad or empty codec stream.
40 class EmptyImageGenerator final : public SkImageGenerator {
41 public:
42 EmptyImageGenerator(const SkImageInfo& info) : SkImageGenerator(info) { }
43
44 };
45
46 static sk_sp<SkImage> MakeEmptyImage(int width, int height) {
47 return SkImages::DeferredFromGenerator(
48 std::make_unique<EmptyImageGenerator>(SkImageInfo::MakeN32Premul(width, height)));
49 }
50
51 } // anonymous namespace
52
53 void SkReadBuffer::setMemory(const void* data, size_t size) {
54 this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size));
55 if (!fError) {
56 fBase = fCurr = (const char*)data;
57 fStop = fBase + size;
58 }
59 }
60
61 void SkReadBuffer::setInvalid() {
62 if (!fError) {
63 // When an error is found, send the read cursor to the end of the stream
64 fCurr = fStop;
65 fError = true;
66 }
67 }
68
69 const void* SkReadBuffer::skip(size_t size) {
70 size_t inc = SkAlign4(size);
71 this->validate(inc >= size);
72 const void* addr = fCurr;
73 this->validate(IsPtrAlign4(addr) && this->isAvailable(inc));
74 if (fError) {
75 return nullptr;
76 }
77
78 fCurr += inc;
79 return addr;
80 }
81
82 const void* SkReadBuffer::skip(size_t count, size_t size) {
83 return this->skip(SkSafeMath::Mul(count, size));
84 }
85
86 void SkReadBuffer::setDeserialProcs(const SkDeserialProcs& procs) {
87 fProcs = procs;
88 this->setAllowSkSL(procs.fAllowSkSL);
89 }
90
91 bool SkReadBuffer::readBool() {
92 uint32_t value = this->readUInt();
93 // Boolean value should be either 0 or 1
94 this->validate(!(value & ~1));
95 return value != 0;
96 }
97
98 SkColor SkReadBuffer::readColor() {
99 return this->readUInt();
100 }
101
102 int32_t SkReadBuffer::readInt() {
103 const size_t inc = sizeof(int32_t);
104 if (!this->validate(IsPtrAlign4(fCurr) && this->isAvailable(inc))) {
105 return 0;
106 }
107 int32_t value = *((const int32_t*)fCurr);
108 fCurr += inc;
109 return value;
110 }
111
112 SkScalar SkReadBuffer::readScalar() {
113 const size_t inc = sizeof(SkScalar);
114 if (!this->validate(IsPtrAlign4(fCurr) && this->isAvailable(inc))) {
115 return 0;
116 }
117 SkScalar value = *((const SkScalar*)fCurr);
118 fCurr += inc;
119 return value;
120 }
121
122 uint32_t SkReadBuffer::readUInt() {
123 return this->readInt();
124 }
125
126 int32_t SkReadBuffer::read32() {
127 return this->readInt();
128 }
129
130 uint8_t SkReadBuffer::peekByte() {
131 if (this->available() <= 0) {
132 fError = true;
133 return 0;
134 }
135 return *((const uint8_t*)fCurr);
136 }
137
138 bool SkReadBuffer::readPad32(void* buffer, size_t bytes) {
139 if (const void* src = this->skip(bytes)) {
140 // buffer might be null if bytes is zero (see SkAutoMalloc), hence we call
141 // the careful version of memcpy.
142 sk_careful_memcpy(buffer, src, bytes);
143 return true;
144 }
145 return false;
146 }
147
148 const char* SkReadBuffer::readString(size_t* len) {
149 *len = this->readUInt();
150
151 // The string is len characters and a terminating \0.
152 const char* c_str = this->skipT<char>(*len+1);
153
154 if (this->validate(c_str && c_str[*len] == '\0')) {
155 return c_str;
156 }
157 return nullptr;
158 }
159
160 void SkReadBuffer::readString(SkString* string) {
161 size_t len;
162 if (const char* c_str = this->readString(&len)) {
163 string->set(c_str, len);
164 return;
165 }
166 string->reset();
167 }
168
169 void SkReadBuffer::readColor4f(SkColor4f* color) {
170 if (!this->readPad32(color, sizeof(SkColor4f))) {
171 *color = {0, 0, 0, 0};
172 }
173 }
174
175 void SkReadBuffer::readPoint(SkPoint* point) {
176 point->fX = this->readScalar();
177 point->fY = this->readScalar();
178 }
179
180 void SkReadBuffer::readPoint3(SkPoint3* point) {
181 this->readPad32(point, sizeof(SkPoint3));
182 }
183
184 void SkReadBuffer::read(SkM44* matrix) {
185 if (this->isValid()) {
186 if (const float* m = (const float*)this->skip(sizeof(float) * 16)) {
187 *matrix = SkM44::ColMajor(m);
188 }
189 }
190 if (!this->isValid()) {
191 *matrix = SkM44();
192 }
193 }
194
195 void SkReadBuffer::readMatrix(SkMatrix* matrix) {
196 size_t size = 0;
197 if (this->isValid()) {
198 size = SkMatrixPriv::ReadFromMemory(matrix, fCurr, this->available());
199 (void)this->validate((SkAlign4(size) == size) && (0 != size));
200 }
201 if (!this->isValid()) {
202 matrix->reset();
203 }
204 (void)this->skip(size);
205 }
206
207 void SkReadBuffer::readIRect(SkIRect* rect) {
208 if (!this->readPad32(rect, sizeof(SkIRect))) {
209 rect->setEmpty();
210 }
211 }
212
213 void SkReadBuffer::readRect(SkRect* rect) {
214 if (!this->readPad32(rect, sizeof(SkRect))) {
215 rect->setEmpty();
216 }
217 }
218
219 SkRect SkReadBuffer::readRect() {
220 SkRect r;
221 if (!this->readPad32(&r, sizeof(SkRect))) {
222 r.setEmpty();
223 }
224 return r;
225 }
226
227 SkSamplingOptions SkReadBuffer::readSampling() {
228 if (!this->isVersionLT(SkPicturePriv::kAnisotropicFilter)) {
229 int maxAniso = this->readInt();
230 if (maxAniso != 0) {
231 return SkSamplingOptions::Aniso(maxAniso);
232 }
233 }
234 if (this->readBool()) {
235 float B = this->readScalar();
236 float C = this->readScalar();
237 return SkSamplingOptions({B, C});
238 } else {
239 SkFilterMode filter = this->read32LE(SkFilterMode::kLinear);
240 SkMipmapMode mipmap = this->read32LE(SkMipmapMode::kLinear);
241 return SkSamplingOptions(filter, mipmap);
242 }
243 }
244
245 void SkReadBuffer::readRRect(SkRRect* rrect) {
246 size_t size = 0;
247 if (!fError) {
248 size = rrect->readFromMemory(fCurr, this->available());
249 if (!this->validate((SkAlign4(size) == size) && (0 != size))) {
250 rrect->setEmpty();
251 }
252 }
253 (void)this->skip(size);
254 }
255
256 void SkReadBuffer::readRegion(SkRegion* region) {
257 size_t size = 0;
258 if (!fError) {
259 size = region->readFromMemory(fCurr, this->available());
260 if (!this->validate((SkAlign4(size) == size) && (0 != size))) {
261 region->setEmpty();
262 }
263 }
264 (void)this->skip(size);
265 }
266
267 std::optional<SkPath> SkReadBuffer::readPath() {
268 if (fError) {
269 return {};
270 }
271
272 size_t size = 0;
273 auto path = SkPath::ReadFromMemory(fCurr, this->available(), &size);
274
275 // todo: consider moving this 4-byte-alignment check elsewhere
276 // i.e. why is that a burden on SkPath?
277 // why don't we just skipAlign4() or something?
278 (void)this->validate(SkAlign4(size) == size && path.has_value());
279
280 // we move forward, regardless of if the path succeeded
281 (void)this->skip(size);
282
283 return path;
284 }
285
286 bool SkReadBuffer::readArray(void* value, size_t size, size_t elementSize) {
287 const uint32_t count = this->readUInt();
288 return this->validate(size == count) &&
289 this->readPad32(value, SkSafeMath::Mul(size, elementSize));
290 }
291
292 bool SkReadBuffer::readByteArray(void* value, size_t size) {
293 return this->readArray(value, size, sizeof(uint8_t));
294 }
295
296 bool SkReadBuffer::readColorArray(SkSpan<SkColor> colors) {
297 return this->readArray(colors.data(), colors.size(), sizeof(SkColor));
298 }
299
300 bool SkReadBuffer::readColor4fArray(SkSpan<SkColor4f> colors) {
301 return this->readArray(colors.data(), colors.size(), sizeof(SkColor4f));
302 }
303
304 bool SkReadBuffer::readIntArray(SkSpan<int32_t> values) {
305 return this->readArray(values.data(), values.size(), sizeof(int32_t));
306 }
307
308 bool SkReadBuffer::readPointArray(SkSpan<SkPoint> points) {
309 return this->readArray(points.data(), points.size(), sizeof(SkPoint));
310 }
311
312 bool SkReadBuffer::readScalarArray(SkSpan<SkScalar> values) {
313 return this->readArray(values.data(), values.size(), sizeof(SkScalar));
314 }
315
316 const void* SkReadBuffer::skipByteArray(size_t* size) {
317 const uint32_t count = this->readUInt();
318 const void* buf = this->skip(count);
319 if (size) {
320 *size = this->isValid() ? count : 0;
321 }
322 return buf;
323 }
324
325 sk_sp<SkData> SkReadBuffer::readByteArrayAsData() {
326 size_t numBytes = this->getArrayCount();
327 if (!this->validate(this->isAvailable(numBytes))) {
328 return nullptr;
329 }
330
331 SkAutoMalloc buffer(numBytes);
332 if (!this->readByteArray(buffer.get(), numBytes)) {
333 return nullptr;
334 }
335 return SkData::MakeFromMalloc(buffer.release(), numBytes);
336 }
337
338 uint32_t SkReadBuffer::getArrayCount() {
339 const size_t inc = sizeof(uint32_t);
340 if (!this->validate(IsPtrAlign4(fCurr) && this->isAvailable(inc))) {
341 return 0;
342 }
343 return *((const uint32_t*)fCurr);
344 }
345
346 static sk_sp<SkImage> deserialize_image(sk_sp<SkData> data, SkDeserialProcs dProcs,
347 std::optional<SkAlphaType> alphaType) {
348 sk_sp<SkImage> image;
349 if (dProcs.fImageDataProc) {
350 image = dProcs.fImageDataProc(data, alphaType, dProcs.fImageCtx);
351 } else if (dProcs.fImageProc) {
352 #if !defined(SK_LEGACY_DESERIAL_IMAGE_PROC)
353 image = dProcs.fImageProc(data->data(), data->size(), dProcs.fImageCtx);
354 #else
355 image = dProcs.fImageProc(data->data(), data->size(), alphaType, dProcs.fImageCtx);
356 #endif
357 }
358 return image;
359 }
360
361 static sk_sp<SkImage> add_mipmaps(sk_sp<SkImage> img, sk_sp<SkData> data,
362 SkDeserialProcs dProcs, std::optional<SkAlphaType> alphaType) {
363 SkMipmapBuilder builder(img->imageInfo());
364
365 SkReadBuffer buffer(data->data(), data->size());
366 int count = buffer.read32();
367 if (builder.countLevels() != count) {
368 return img;
369 }
370 for (int i = 0; i < count; ++i) {
371 size_t size = buffer.read32();
372 const void* ptr = buffer.skip(size);
373 if (!ptr) {
374 return img;
375 }
376 // This use of SkData::MakeWithoutCopy is safe because the image goes
377 // out of scope after we read the pixels from it, so we are sure the
378 // data (from buffer) outlives the image.
379 sk_sp<SkImage> mip = deserialize_image(SkData::MakeWithoutCopy(ptr, size), dProcs,
380 alphaType);
381 if (!mip) {
382 return img;
383 }
384
385 SkPixmap pm = builder.level(i);
386 if (mip->dimensions() != pm.dimensions()) {
387 return img;
388 }
389 if (!mip->readPixels(nullptr, pm, 0, 0)) {
390 return img;
391 }
392 }
393 if (!buffer.isValid()) {
394 return img;
395 }
396 sk_sp<SkImage> raster = img->makeRasterImage(nullptr);
397 if (!raster) {
398 return img;
399 }
400 sk_sp<SkImage> rasterWithMips = builder.attachTo(raster);
401 SkASSERT(rasterWithMips); // attachTo should never return null
402 return rasterWithMips;
403 }
404
405
406 // If we see a corrupt stream, we return null (fail). If we just fail trying to decode
407 // the image, we don't fail, but return a 1x1 empty image.
408 sk_sp<SkImage> SkReadBuffer::readImage() {
409 uint32_t flags = this->read32();
410
411 std::optional<SkAlphaType> alphaType = std::nullopt;
412 if (flags & SkWriteBufferImageFlags::kUnpremul) {
413 alphaType = kUnpremul_SkAlphaType;
414 }
415 sk_sp<SkImage> image;
416 {
417 sk_sp<SkData> data = this->readByteArrayAsData();
418 if (!data) {
419 this->validate(false);
420 return nullptr;
421 }
422 image = deserialize_image(data, fProcs, alphaType);
423 }
424
425 // This flag is not written by new SKPs anymore.
426 if (flags & SkWriteBufferImageFlags::kHasSubsetRect) {
427 SkIRect subset;
428 this->readIRect(&subset);
429 if (image) {
430 image = image->makeSubset(nullptr, subset, {});
431 }
432 }
433
434 if (flags & SkWriteBufferImageFlags::kHasMipmap) {
435 sk_sp<SkData> data = this->readByteArrayAsData();
436 if (!data) {
437 this->validate(false);
438 return nullptr;
439 }
440 if (image) {
441 image = add_mipmaps(image, std::move(data), fProcs, alphaType);
442 }
443 }
444 return image ? image : MakeEmptyImage(1, 1);
445 }
446
447 sk_sp<SkTypeface> SkReadBuffer::readTypeface() {
448 // Read 32 bits (signed)
449 // 0 -- return null (empty font)
450 // >0 -- index
451 // <0 -- custom (serial procs) : negative size in bytes
452
453 int32_t index = this->read32();
454 if (index == 0) {
455 return nullptr;
456 } else if (index > 0) {
457 if (!this->validate(index <= fTFCount)) {
458 return nullptr;
459 }
460 return fTFArray[index - 1];
461 } else { // custom
462 size_t size = sk_negate_to_size_t(index);
463 const void* data = this->skip(size);
464 if (!this->validate(data && fProcs.fTypefaceStreamProc)) {
465 return nullptr;
466 }
467 SkMemoryStream s(data, size, false);
468 return fProcs.fTypefaceStreamProc(s, fProcs.fTypefaceCtx);
469 }
470 }
471
472 SkFlattenable* SkReadBuffer::readRawFlattenable() {
473 SkFlattenable::Factory factory = nullptr;
474
475 if (fFactoryCount > 0) {
476 int32_t index = this->read32();
477 if (0 == index || !this->isValid()) {
478 return nullptr; // writer failed to give us the flattenable
479 }
480 if (index < 0) {
481 this->validate(false);
482 return nullptr;
483 }
484 index -= 1; // we stored the index-base-1
485 if ((unsigned)index >= (unsigned)fFactoryCount) {
486 this->validate(false);
487 return nullptr;
488 }
489 factory = fFactoryArray[index];
490 } else {
491 if (this->peekByte() != 0) {
492 // If the first byte is non-zero, the flattenable is specified by a string.
493 size_t ignored_length;
494 if (const char* name = this->readString(&ignored_length)) {
495 factory = SkFlattenable::NameToFactory(name);
496 fFlattenableDict.set(fFlattenableDict.count() + 1, factory);
497 }
498 } else {
499 // Read the index. We are guaranteed that the first byte
500 // is zeroed, so we must shift down a byte.
501 uint32_t index = this->readUInt() >> 8;
502 if (index == 0) {
503 return nullptr; // writer failed to give us the flattenable
504 }
505
506 if (SkFlattenable::Factory* found = fFlattenableDict.find(index)) {
507 factory = *found;
508 }
509 }
510
511 if (!this->validate(factory != nullptr)) {
512 return nullptr;
513 }
514 }
515
516 // if we get here, factory may still be null, but if that is the case, the
517 // failure was ours, not the writer.
518 sk_sp<SkFlattenable> obj;
519 uint32_t sizeRecorded = this->read32();
520 if (factory) {
521 size_t offset = this->offset();
522 obj = (*factory)(*this);
523 // check that we read the amount we expected
524 size_t sizeRead = this->offset() - offset;
525 if (sizeRecorded != sizeRead) {
526 this->validate(false);
527 return nullptr;
528 }
529 } else {
530 // we must skip the remaining data
531 this->skip(sizeRecorded);
532 }
533 if (!this->isValid()) {
534 return nullptr;
535 }
536 return obj.release();
537 }
538
539 SkFlattenable* SkReadBuffer::readFlattenable(SkFlattenable::Type ft) {
540 SkFlattenable* obj = this->readRawFlattenable();
541 if (obj && obj->getFlattenableType() != ft) {
542 this->validate(false);
543 obj->unref();
544 return nullptr;
545 }
546 return obj;
547 }
548
549 ///////////////////////////////////////////////////////////////////////////////////////////////////
550
551 int32_t SkReadBuffer::checkInt(int32_t min, int32_t max) {
552 SkASSERT(min <= max);
553 int32_t value = this->read32();
554 if (value < min || value > max) {
555 this->validate(false);
556 value = min;
557 }
558 return value;
559 }
560
561 SkLegacyFQ SkReadBuffer::checkFilterQuality() {
562 return this->checkRange<SkLegacyFQ>(kNone_SkLegacyFQ, kLast_SkLegacyFQ);
563 }