1 /*
2  * Copyright 2013 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 #ifndef SkValidatingReadBuffer_DEFINED
9 #define SkValidatingReadBuffer_DEFINED
10 
11 #include "SkRefCnt.h"
12 #include "SkBitmapHeap.h"
13 #include "SkReadBuffer.h"
14 #include "SkWriteBuffer.h"
15 #include "SkPath.h"
16 #include "SkPicture.h"
17 #include "SkReader32.h"
18 
19 class SkBitmap;
20 
21 class SkValidatingReadBuffer : public SkReadBuffer {
22 public:
23     SkValidatingReadBuffer(const void* data, size_t size);
24     virtual ~SkValidatingReadBuffer();
25 
26     const void* skip(size_t size) override;
27 
28     // primitives
29     bool readBool() override;
30     SkColor readColor() override;
31     SkFixed readFixed() override;
32     int32_t readInt() override;
33     SkScalar readScalar() override;
34     uint32_t readUInt() override;
35     int32_t read32() override;
36 
37     // strings -- the caller is responsible for freeing the string contents
38     void readString(SkString* string) override;
39     void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding) override;
40 
41     // common data structures
42     SkFlattenable* readFlattenable(SkFlattenable::Type type) override;
43     void skipFlattenable() override;
44     void readPoint(SkPoint* point) override;
45     void readMatrix(SkMatrix* matrix) override;
46     void readIRect(SkIRect* rect) override;
47     void readRect(SkRect* rect) override;
48     void readRegion(SkRegion* region) override;
49     void readPath(SkPath* path) override;
50 
51     // binary data and arrays
52     bool readByteArray(void* value, size_t size) override;
53     bool readColorArray(SkColor* colors, size_t size) override;
54     bool readIntArray(int32_t* values, size_t size) override;
55     bool readPointArray(SkPoint* points, size_t size) override;
56     bool readScalarArray(SkScalar* values, size_t size) override;
57 
58     // helpers to get info about arrays and binary data
59     uint32_t getArrayCount() override;
60 
61     // TODO: Implement this (securely) when needed
62     SkTypeface* readTypeface() override;
63 
64     bool validate(bool isValid) override;
65     bool isValid() const override;
66 
67     bool validateAvailable(size_t size) override;
68 
69 private:
70     bool readArray(void* value, size_t size, size_t elementSize);
71 
72     void setMemory(const void* data, size_t size);
73 
IsPtrAlign4(const void * ptr)74     static bool IsPtrAlign4(const void* ptr) {
75         return SkIsAlign4((uintptr_t)ptr);
76     }
77 
78     bool fError;
79 
80     typedef SkReadBuffer INHERITED;
81 };
82 
83 #endif // SkValidatingReadBuffer_DEFINED
84