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