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