epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed | fb8c1fc | 2015-08-04 18:44:56 -0700 | [diff] [blame] | 8 | #include "SkBuffer.h" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame] | 9 | |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 12 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 14 | bool 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; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 27 | bool 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 | |
| 41 | void* SkWBuffer::skip(size_t size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | void* result = fPos; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | writeNoSizeCheck(nullptr, size); |
| 44 | return fData == nullptr ? nullptr : result; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 47 | void SkWBuffer::writeNoSizeCheck(const void* buffer, size_t size) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 48 | SkASSERT(fData == nullptr || fStop == nullptr || fPos + size <= fStop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | if (fData && buffer) |
| 50 | memcpy(fPos, buffer, size); |
| 51 | fPos += size; |
| 52 | } |
| 53 | |
Mike Reed | 1026ccf | 2017-01-08 14:35:29 -0500 | [diff] [blame] | 54 | size_t SkWBuffer::padToAlign4() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | #endif |