1 /* 2 * Copyright 2006 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 #include "SkBuffer.h" 9 10 #include <string.h> 11 12 /////////////////////////////////////////////////////////////////////////////////////////////////// 13 read(void * buffer,size_t size)14bool SkRBuffer::read(void* buffer, size_t size) { 15 if (fValid && size <= this->available()) { 16 if (buffer) { 17 memcpy(buffer, fPos, size); 18 } 19 fPos += size; 20 return true; 21 } else { 22 fValid = false; 23 return false; 24 } 25 } 26 skipToAlign4()27bool SkRBuffer::skipToAlign4() { 28 intptr_t pos = reinterpret_cast<intptr_t>(fPos); 29 size_t n = SkAlign4(pos) - pos; 30 if (fValid && n <= this->available()) { 31 fPos += n; 32 return true; 33 } else { 34 fValid = false; 35 return false; 36 } 37 } 38 39 /////////////////////////////////////////////////////////////////////////////////////////////////// 40 skip(size_t size)41void* SkWBuffer::skip(size_t size) { 42 void* result = fPos; 43 writeNoSizeCheck(nullptr, size); 44 return fData == nullptr ? nullptr : result; 45 } 46 writeNoSizeCheck(const void * buffer,size_t size)47void SkWBuffer::writeNoSizeCheck(const void* buffer, size_t size) { 48 SkASSERT(fData == nullptr || fStop == nullptr || fPos + size <= fStop); 49 if (fData && buffer) 50 memcpy(fPos, buffer, size); 51 fPos += size; 52 } 53 padToAlign4()54size_t SkWBuffer::padToAlign4() { 55 size_t pos = this->pos(); 56 size_t n = SkAlign4(pos) - pos; 57 58 if (n && fData) 59 { 60 char* p = fPos; 61 char* stop = p + n; 62 do { 63 *p++ = 0; 64 } while (p < stop); 65 } 66 fPos += n; 67 return n; 68 } 69 70 #if 0 71 #ifdef SK_DEBUG 72 static void AssertBuffer32(const void* buffer) 73 { 74 SkASSERT(buffer); 75 SkASSERT(((size_t)buffer & 3) == 0); 76 } 77 #else 78 #define AssertBuffer32(buffer) 79 #endif 80 81 #endif 82