SkValidatingReadBuffer.h revision 57b46159740020ef904aaf41e9e5ae07106591cc
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
19class SkBitmap;
20
21class SkValidatingReadBuffer : public SkReadBuffer {
22public:
23    SkValidatingReadBuffer(const void* data, size_t size);
24    ~SkValidatingReadBuffer() override;
25
26    SkReadBuffer* clone(const void* data, size_t size) const override {
27        return new SkValidatingReadBuffer(data, size);
28    }
29
30    const void* skip(size_t size) override;
31
32    // primitives
33    bool readBool() override;
34    SkColor readColor() override;
35    int32_t readInt() override;
36    SkScalar readScalar() override;
37    uint32_t readUInt() override;
38    int32_t read32() override;
39
40    // peek
41    uint8_t peekByte() override;
42
43    // strings -- the caller is responsible for freeing the string contents
44    void readString(SkString* string) override;
45
46    // common data structures
47    SkFlattenable* readFlattenable(SkFlattenable::Type type) override;
48    void skipFlattenable() override;
49    void readPoint(SkPoint* point) override;
50    void readMatrix(SkMatrix* matrix) override;
51    void readIRect(SkIRect* rect) override;
52    void readRect(SkRect* rect) override;
53    void readRRect(SkRRect* rrect) override;
54    void readRegion(SkRegion* region) override;
55    void readPath(SkPath* path) override;
56
57    // binary data and arrays
58    bool readByteArray(void* value, size_t size) override;
59    bool readColorArray(SkColor* 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    // TODO: Implement this (securely) when needed
68    SkTypeface* readTypeface() override;
69
70    bool validate(bool isValid) override;
71    bool isValid() const override;
72
73    bool validateAvailable(size_t size) override;
74
75private:
76    bool readArray(void* value, size_t size, size_t elementSize);
77
78    void setMemory(const void* data, size_t size);
79
80    static bool IsPtrAlign4(const void* ptr) {
81        return SkIsAlign4((uintptr_t)ptr);
82    }
83
84    bool fError;
85
86    typedef SkReadBuffer INHERITED;
87};
88
89#endif // SkValidatingReadBuffer_DEFINED
90