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: SkReader32()28 SkReader32() : fCurr(NULL), fStop(NULL), fBase(NULL) {} SkReader32(const void * data,size_t size)29 SkReader32(const void* data, size_t size) { 30 this->setMemory(data, size); 31 } 32 setMemory(const void * data,size_t size)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 size()41 uint32_t size() const { return fStop - fBase; } offset()42 uint32_t offset() const { return fCurr - fBase; } eof()43 bool eof() const { return fCurr >= fStop; } base()44 const void* base() const { return fBase; } peek()45 const void* peek() const { return fCurr; } 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 readScalar()64 SkScalar readScalar() { 65 SkASSERT(ptr_align_4(fCurr)); 66 SkScalar value = *(const SkScalar*)fCurr; 67 fCurr += sizeof(value); 68 SkASSERT(fCurr <= fStop); 69 return value; 70 } 71 skipPoint()72 const SkPoint* skipPoint() { 73 return (const SkPoint*)this->skip(sizeof(SkPoint)); 74 } 75 skipRect()76 const SkRect* skipRect() { 77 return (const SkRect*)this->skip(sizeof(SkRect)); 78 } 79 skip(size_t size)80 const void* skip(size_t size) { 81 SkASSERT(ptr_align_4(fCurr)); 82 const void* addr = fCurr; 83 fCurr += SkAlign4(size); 84 SkASSERT(fCurr <= fStop); 85 return addr; 86 } 87 read(void * dst,size_t size)88 void read(void* dst, size_t size) { 89 SkASSERT(dst != NULL); 90 SkASSERT(ptr_align_4(fCurr)); 91 memcpy(dst, fCurr, size); 92 fCurr += SkAlign4(size); 93 SkASSERT(fCurr <= fStop); 94 } 95 readU8()96 uint8_t readU8() { return (uint8_t)this->readInt(); } readU16()97 uint16_t readU16() { return (uint16_t)this->readInt(); } readS32()98 int32_t readS32() { return this->readInt(); } readU32()99 uint32_t readU32() { return this->readInt(); } 100 101 private: 102 // these are always 4-byte aligned 103 const char* fCurr; // current position within buffer 104 const char* fStop; // end of buffer 105 const char* fBase; // beginning of buffer 106 107 #ifdef SK_DEBUG ptr_align_4(const void * ptr)108 static bool ptr_align_4(const void* ptr) 109 { 110 return (((const char*)ptr - (const char*)NULL) & 3) == 0; 111 } 112 #endif 113 }; 114 115 #endif 116