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 "SkData.h" 13 #include "SkMatrix.h" 14 #include "SkNoncopyable.h" 15 #include "SkPath.h" 16 #include "SkRegion.h" 17 #include "SkRRect.h" 18 #include "SkScalar.h" 19 20 class SkString; 21 22 class SkReader32 : SkNoncopyable { 23 public: SkReader32()24 SkReader32() : fCurr(nullptr), fStop(nullptr), fBase(nullptr) {} SkReader32(const void * data,size_t size)25 SkReader32(const void* data, size_t size) { 26 this->setMemory(data, size); 27 } 28 setMemory(const void * data,size_t size)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 size()37 size_t size() const { return fStop - fBase; } offset()38 size_t offset() const { return fCurr - fBase; } eof()39 bool eof() const { return fCurr >= fStop; } base()40 const void* base() const { return fBase; } peek()41 const void* peek() const { return fCurr; } 42 available()43 size_t available() const { return fStop - fCurr; } isAvailable(size_t size)44 bool isAvailable(size_t size) const { return size <= this->available(); } 45 rewind()46 void rewind() { fCurr = fBase; } 47 setOffset(size_t offset)48 void setOffset(size_t offset) { 49 SkASSERT(SkAlign4(offset) == offset); 50 SkASSERT(offset <= this->size()); 51 fCurr = fBase + offset; 52 } 53 readBool()54 bool readBool() { return this->readInt() != 0; } 55 readInt()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 readPtr()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 readScalar()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 skip(size_t size)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 skipT()92 template <typename T> const T& skipT() { 93 SkASSERT(SkAlign4(sizeof(T)) == sizeof(T)); 94 return *(const T*)this->skip(sizeof(T)); 95 } 96 read(void * dst,size_t size)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 readU8()105 uint8_t readU8() { return (uint8_t)this->readInt(); } readU16()106 uint16_t readU16() { return (uint16_t)this->readInt(); } readS32()107 int32_t readS32() { return this->readInt(); } readU32()108 uint32_t readU32() { return this->readInt(); } 109 readPath(SkPath * path)110 bool readPath(SkPath* path) { 111 return this->readObjectFromMemory(path); 112 } 113 readMatrix(SkMatrix * matrix)114 bool readMatrix(SkMatrix* matrix) { 115 return this->readObjectFromMemory(matrix); 116 } 117 readRRect(SkRRect * rrect)118 bool readRRect(SkRRect* rrect) { 119 return this->readObjectFromMemory(rrect); 120 } 121 readRegion(SkRegion * rgn)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 readData()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: readObjectFromMemory(T * obj)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 ptr_align_4(const void * ptr)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