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