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