1 | /* |
2 | * Copyright 2008 The Android Open Source Project |
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 | |
9 | #ifndef SkReader32_DEFINED |
10 | #define SkReader32_DEFINED |
11 | |
12 | #include "include/core/SkData.h" |
13 | #include "include/core/SkMatrix.h" |
14 | #include "include/core/SkPath.h" |
15 | #include "include/core/SkRRect.h" |
16 | #include "include/core/SkRegion.h" |
17 | #include "include/core/SkScalar.h" |
18 | #include "include/private/SkNoncopyable.h" |
19 | |
20 | class SkString; |
21 | |
22 | class SkReader32 : SkNoncopyable { |
23 | public: |
24 | SkReader32() : fCurr(nullptr), fStop(nullptr), fBase(nullptr) {} |
25 | SkReader32(const void* data, size_t size) { |
26 | this->setMemory(data, size); |
27 | } |
28 | |
29 | void setMemory(const void* data, size_t size) { |
30 | SkASSERT(ptr_align_4(data)); |
31 | SkASSERT(SkAlign4(size) == size); |
32 | |
33 | fBase = fCurr = (const char*)data; |
34 | fStop = (const char*)data + size; |
35 | } |
36 | |
37 | size_t size() const { return fStop - fBase; } |
38 | size_t offset() const { return fCurr - fBase; } |
39 | bool eof() const { return fCurr >= fStop; } |
40 | const void* base() const { return fBase; } |
41 | const void* peek() const { return fCurr; } |
42 | |
43 | size_t available() const { return fStop - fCurr; } |
44 | bool isAvailable(size_t size) const { return size <= this->available(); } |
45 | |
46 | void rewind() { fCurr = fBase; } |
47 | |
48 | void setOffset(size_t offset) { |
49 | SkASSERT(SkAlign4(offset) == offset); |
50 | SkASSERT(offset <= this->size()); |
51 | fCurr = fBase + offset; |
52 | } |
53 | |
54 | bool readBool() { return this->readInt() != 0; } |
55 | |
56 | int32_t readInt() { |
57 | SkASSERT(ptr_align_4(fCurr)); |
58 | int32_t value = *(const int32_t*)fCurr; |
59 | fCurr += sizeof(value); |
60 | SkASSERT(fCurr <= fStop); |
61 | return value; |
62 | } |
63 | |
64 | void* readPtr() { |
65 | void* ptr; |
66 | // we presume this "if" is resolved at compile-time |
67 | if (4 == sizeof(void*)) { |
68 | ptr = *(void**)fCurr; |
69 | } else { |
70 | memcpy(&ptr, fCurr, sizeof(void*)); |
71 | } |
72 | fCurr += sizeof(void*); |
73 | return ptr; |
74 | } |
75 | |
76 | SkScalar readScalar() { |
77 | SkASSERT(ptr_align_4(fCurr)); |
78 | SkScalar value = *(const SkScalar*)fCurr; |
79 | fCurr += sizeof(value); |
80 | SkASSERT(fCurr <= fStop); |
81 | return value; |
82 | } |
83 | |
84 | const void* skip(size_t size) { |
85 | SkASSERT(ptr_align_4(fCurr)); |
86 | const void* addr = fCurr; |
87 | fCurr += SkAlign4(size); |
88 | SkASSERT(fCurr <= fStop); |
89 | return addr; |
90 | } |
91 | |
92 | template <typename T> const T& skipT() { |
93 | SkASSERT(SkAlign4(sizeof(T)) == sizeof(T)); |
94 | return *(const T*)this->skip(sizeof(T)); |
95 | } |
96 | |
97 | void read(void* dst, size_t size) { |
98 | SkASSERT(0 == size || dst != nullptr); |
99 | SkASSERT(ptr_align_4(fCurr)); |
100 | sk_careful_memcpy(dst, fCurr, size); |
101 | fCurr += SkAlign4(size); |
102 | SkASSERT(fCurr <= fStop); |
103 | } |
104 | |
105 | uint8_t readU8() { return (uint8_t)this->readInt(); } |
106 | uint16_t readU16() { return (uint16_t)this->readInt(); } |
107 | int32_t readS32() { return this->readInt(); } |
108 | uint32_t readU32() { return this->readInt(); } |
109 | |
110 | bool readPath(SkPath* path) { |
111 | return this->readObjectFromMemory(path); |
112 | } |
113 | |
114 | bool readMatrix(SkMatrix* matrix) { |
115 | return this->readObjectFromMemory(matrix); |
116 | } |
117 | |
118 | bool readRRect(SkRRect* rrect) { |
119 | return this->readObjectFromMemory(rrect); |
120 | } |
121 | |
122 | bool readRegion(SkRegion* rgn) { |
123 | return this->readObjectFromMemory(rgn); |
124 | } |
125 | |
126 | /** |
127 | * Read the length of a string (written by SkWriter32::writeString) into |
128 | * len (if len is not nullptr) and return the null-ternimated address of the |
129 | * string within the reader's buffer. |
130 | */ |
131 | const char* readString(size_t* len = nullptr); |
132 | |
133 | /** |
134 | * Read the string (written by SkWriter32::writeString) and return it in |
135 | * copy (if copy is not null). Return the length of the string. |
136 | */ |
137 | size_t readIntoString(SkString* copy); |
138 | |
139 | sk_sp<SkData> readData() { |
140 | uint32_t byteLength = this->readU32(); |
141 | if (0 == byteLength) { |
142 | return SkData::MakeEmpty(); |
143 | } |
144 | return SkData::MakeWithCopy(this->skip(byteLength), byteLength); |
145 | } |
146 | |
147 | private: |
148 | template <typename T> bool readObjectFromMemory(T* obj) { |
149 | size_t size = obj->readFromMemory(this->peek(), this->available()); |
150 | // If readFromMemory() fails (which means that available() was too small), it returns 0 |
151 | bool success = (size > 0) && (size <= this->available()) && (SkAlign4(size) == size); |
152 | // In case of failure, we want to skip to the end |
153 | (void)this->skip(success ? size : this->available()); |
154 | return success; |
155 | } |
156 | |
157 | // these are always 4-byte aligned |
158 | const char* fCurr; // current position within buffer |
159 | const char* fStop; // end of buffer |
160 | const char* fBase; // beginning of buffer |
161 | |
162 | #ifdef SK_DEBUG |
163 | static bool ptr_align_4(const void* ptr) { |
164 | return (((const char*)ptr - (const char*)nullptr) & 3) == 0; |
165 | } |
166 | #endif |
167 | }; |
168 | |
169 | #endif |
170 | |