OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkReader32_DEFINED | 10 #ifndef SkReader32_DEFINED |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 memcpy(dst, fCurr, size); | 99 memcpy(dst, fCurr, size); |
100 fCurr += SkAlign4(size); | 100 fCurr += SkAlign4(size); |
101 SkASSERT(fCurr <= fStop); | 101 SkASSERT(fCurr <= fStop); |
102 } | 102 } |
103 | 103 |
104 uint8_t readU8() { return (uint8_t)this->readInt(); } | 104 uint8_t readU8() { return (uint8_t)this->readInt(); } |
105 uint16_t readU16() { return (uint16_t)this->readInt(); } | 105 uint16_t readU16() { return (uint16_t)this->readInt(); } |
106 int32_t readS32() { return this->readInt(); } | 106 int32_t readS32() { return this->readInt(); } |
107 uint32_t readU32() { return this->readInt(); } | 107 uint32_t readU32() { return this->readInt(); } |
108 | 108 |
109 void readPath(SkPath* path) { | 109 bool readPath(SkPath* path) { |
110 size_t size = path->readFromMemory(this->peek()); | 110 return readObjectFromMemory(path); |
111 SkASSERT(SkAlign4(size) == size); | |
112 (void)this->skip(size); | |
113 } | 111 } |
114 | 112 |
115 void readMatrix(SkMatrix* matrix) { | 113 bool readMatrix(SkMatrix* matrix) { |
116 size_t size = matrix->readFromMemory(this->peek()); | 114 return readObjectFromMemory(matrix); |
117 SkASSERT(SkAlign4(size) == size); | |
118 (void)this->skip(size); | |
119 } | 115 } |
120 | 116 |
121 SkRRect* readRRect(SkRRect* rrect) { | 117 bool readRRect(SkRRect* rrect) { |
122 rrect->readFromMemory(this->skip(SkRRect::kSizeInMemory)); | 118 return readObjectFromMemory(rrect); |
123 return rrect; | |
124 } | 119 } |
125 | 120 |
126 void readRegion(SkRegion* rgn) { | 121 bool readRegion(SkRegion* rgn) { |
127 size_t size = rgn->readFromMemory(this->peek()); | 122 return readObjectFromMemory(rgn); |
128 SkASSERT(SkAlign4(size) == size); | |
129 (void)this->skip(size); | |
130 } | 123 } |
131 | 124 |
132 /** | 125 /** |
133 * Read the length of a string (written by SkWriter32::writeString) into | 126 * Read the length of a string (written by SkWriter32::writeString) into |
134 * len (if len is not NULL) and return the null-ternimated address of the | 127 * len (if len is not NULL) and return the null-ternimated address of the |
135 * string within the reader's buffer. | 128 * string within the reader's buffer. |
136 */ | 129 */ |
137 const char* readString(size_t* len = NULL); | 130 const char* readString(size_t* len = NULL); |
138 | 131 |
139 /** | 132 /** |
140 * Read the string (written by SkWriter32::writeString) and return it in | 133 * Read the string (written by SkWriter32::writeString) and return it in |
141 * copy (if copy is not null). Return the length of the string. | 134 * copy (if copy is not null). Return the length of the string. |
142 */ | 135 */ |
143 size_t readIntoString(SkString* copy); | 136 size_t readIntoString(SkString* copy); |
144 | 137 |
145 private: | 138 private: |
| 139 template <typename T> bool readObjectFromMemory(T* obj) { |
| 140 size_t size = obj->readFromMemory(this->peek(), this->available()); |
| 141 // If readFromMemory() fails (which means that available() was too small
), it returns 0 |
| 142 bool success = (size > 0) && (size <= this->available()) && (SkAlign4(si
ze) == size); |
| 143 // In case of failure, we want to skip to the end |
| 144 (void)this->skip(success ? size : this->available()); |
| 145 return success; |
| 146 } |
| 147 |
146 // these are always 4-byte aligned | 148 // these are always 4-byte aligned |
147 const char* fCurr; // current position within buffer | 149 const char* fCurr; // current position within buffer |
148 const char* fStop; // end of buffer | 150 const char* fStop; // end of buffer |
149 const char* fBase; // beginning of buffer | 151 const char* fBase; // beginning of buffer |
150 | 152 |
151 #ifdef SK_DEBUG | 153 #ifdef SK_DEBUG |
152 static bool ptr_align_4(const void* ptr) { | 154 static bool ptr_align_4(const void* ptr) { |
153 return (((const char*)ptr - (const char*)NULL) & 3) == 0; | 155 return (((const char*)ptr - (const char*)NULL) & 3) == 0; |
154 } | 156 } |
155 #endif | 157 #endif |
156 }; | 158 }; |
157 | 159 |
158 #endif | 160 #endif |
OLD | NEW |