1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SkReader32_DEFINED 18 #define SkReader32_DEFINED 19 20 #include "SkTypes.h" 21 22 #include "SkScalar.h" 23 #include "SkPoint.h" 24 #include "SkRect.h" 25 26 class SkReader32 : SkNoncopyable { 27 public: 28 SkReader32() : fCurr(NULL), fStop(NULL), fBase(NULL) {} 29 SkReader32(const void* data, size_t size) { 30 this->setMemory(data, size); 31 } 32 33 void setMemory(const void* data, size_t size) { 34 SkASSERT(ptr_align_4(data)); 35 SkASSERT(SkAlign4(size) == size); 36 37 fBase = fCurr = (const char*)data; 38 fStop = (const char*)data + size; 39 } 40 41 uint32_t size() const { return fStop - fBase; } 42 uint32_t offset() const { return fCurr - fBase; } 43 bool eof() const { return fCurr >= fStop; } 44 const void* base() const { return fBase; } 45 const void* peek() const { return fCurr; } 46 47 uint32_t available() const { return fStop - fCurr; } 48 bool isAvailable(uint32_t size) const { return fCurr + size <= fStop; } 49 50 void rewind() { fCurr = fBase; } 51 52 void setOffset(size_t offset) { 53 SkASSERT(SkAlign4(offset) == offset); 54 SkASSERT(offset <= this->size()); 55 fCurr = fBase + offset; 56 } 57 58 bool readBool() { return this->readInt() != 0; } 59 60 int32_t readInt() { 61 SkASSERT(ptr_align_4(fCurr)); 62 int32_t value = *(const int32_t*)fCurr; 63 fCurr += sizeof(value); 64 SkASSERT(fCurr <= fStop); 65 return value; 66 } 67 68 SkScalar readScalar() { 69 SkASSERT(ptr_align_4(fCurr)); 70 SkScalar value = *(const SkScalar*)fCurr; 71 fCurr += sizeof(value); 72 SkASSERT(fCurr <= fStop); 73 return value; 74 } 75 76 const SkPoint* skipPoint() { 77 return (const SkPoint*)this->skip(sizeof(SkPoint)); 78 } 79 80 const SkRect* skipRect() { 81 return (const SkRect*)this->skip(sizeof(SkRect)); 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 void read(void* dst, size_t size) { 93 SkASSERT(0 == size || dst != NULL); 94 SkASSERT(ptr_align_4(fCurr)); 95 memcpy(dst, fCurr, size); 96 fCurr += SkAlign4(size); 97 SkASSERT(fCurr <= fStop); 98 } 99 100 uint8_t readU8() { return (uint8_t)this->readInt(); } 101 uint16_t readU16() { return (uint16_t)this->readInt(); } 102 int32_t readS32() { return this->readInt(); } 103 uint32_t readU32() { return this->readInt(); } 104 105 /** 106 * Read the length of a string written by SkWriter32::writeString() 107 * (if len is not NULL) and return the null-ternimated address of the 108 * string. 109 */ 110 const char* readString(size_t* len = NULL); 111 112 private: 113 // these are always 4-byte aligned 114 const char* fCurr; // current position within buffer 115 const char* fStop; // end of buffer 116 const char* fBase; // beginning of buffer 117 118 #ifdef SK_DEBUG 119 static bool ptr_align_4(const void* ptr) 120 { 121 return (((const char*)ptr - (const char*)NULL) & 3) == 0; 122 } 123 #endif 124 }; 125 126 #endif 127