1
2 /*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10 #include "SkBuffer.h"
11
12 ////////////////////////////////////////////////////////////////////////////////////////
13
readNoSizeCheck(void * buffer,size_t size)14 void SkRBuffer::readNoSizeCheck(void* buffer, size_t size)
15 {
16 SkASSERT((fData != 0 && fStop == 0) || fPos + size <= fStop);
17 if (buffer)
18 memcpy(buffer, fPos, size);
19 fPos += size;
20 }
21
skip(size_t size)22 const void* SkRBuffer::skip(size_t size)
23 {
24 const void* result = fPos;
25 readNoSizeCheck(NULL, size);
26 return result;
27 }
28
skipToAlign4()29 size_t SkRBuffer::skipToAlign4()
30 {
31 size_t pos = this->pos();
32 size_t n = SkAlign4(pos) - pos;
33 fPos += n;
34 return n;
35 }
36
skip(size_t size)37 void* SkWBuffer::skip(size_t size)
38 {
39 void* result = fPos;
40 writeNoSizeCheck(NULL, size);
41 return fData == NULL ? NULL : result;
42 }
43
writeNoSizeCheck(const void * buffer,size_t size)44 void SkWBuffer::writeNoSizeCheck(const void* buffer, size_t size)
45 {
46 SkASSERT(fData == 0 || fStop == 0 || fPos + size <= fStop);
47 if (fData && buffer)
48 memcpy(fPos, buffer, size);
49 fPos += size;
50 }
51
padToAlign4()52 size_t SkWBuffer::padToAlign4()
53 {
54 size_t pos = this->pos();
55 size_t n = SkAlign4(pos) - pos;
56
57 if (n && fData)
58 {
59 char* p = fPos;
60 char* stop = p + n;
61 do {
62 *p++ = 0;
63 } while (p < stop);
64 }
65 fPos += n;
66 return n;
67 }
68
69 #if 0
70 #ifdef SK_DEBUG
71 static void AssertBuffer32(const void* buffer)
72 {
73 SkASSERT(buffer);
74 SkASSERT(((size_t)buffer & 3) == 0);
75 }
76 #else
77 #define AssertBuffer32(buffer)
78 #endif
79
80 void* sk_buffer_write_int32(void* buffer, int32_t value)
81 {
82 AssertBuffer32(buffer);
83 *(int32_t*)buffer = value;
84 return (char*)buffer + sizeof(int32_t);
85 }
86
87 void* sk_buffer_write_int32(void* buffer, const int32_t values[], int count)
88 {
89 AssertBuffer32(buffer);
90 SkASSERT(count >= 0);
91
92 memcpy((int32_t*)buffer, values, count * sizeof(int32_t));
93 return (char*)buffer + count * sizeof(int32_t);
94 }
95
96 const void* sk_buffer_read_int32(const void* buffer, int32_t* value)
97 {
98 AssertBuffer32(buffer);
99 if (value)
100 *value = *(const int32_t*)buffer;
101 return (const char*)buffer + sizeof(int32_t);
102 }
103
104 const void* sk_buffer_read_int32(const void* buffer, int32_t values[], int count)
105 {
106 AssertBuffer32(buffer);
107 SkASSERT(count >= 0);
108
109 if (values)
110 memcpy(values, (const int32_t*)buffer, count * sizeof(int32_t));
111 return (const char*)buffer + count * sizeof(int32_t);
112 }
113
114 void* sk_buffer_write_ptr(void* buffer, void* ptr)
115 {
116 AssertBuffer32(buffer);
117 *(void**)buffer = ptr;
118 return (char*)buffer + sizeof(void*);
119 }
120
121 const void* sk_buffer_read_ptr(const void* buffer, void** ptr)
122 {
123 AssertBuffer32(buffer);
124 if (ptr)
125 *ptr = *(void**)buffer;
126 return (const char*)buffer + sizeof(void*);
127 }
128
129 #endif
130