This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
forked from aosp-mirror/platform_external_qemu
-
Notifications
You must be signed in to change notification settings - Fork 10
/
cbuffer.c
231 lines (176 loc) · 4.91 KB
/
cbuffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
#include "cbuffer.h"
#include "android/utils/stralloc.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#define DEBUG 0
#if DEBUG
# define ASSERT(cond,fmt,...) ({ if (!(cond)) { fprintf(stderr, fmt, __VA_ARGS__); assert(cond); } })
#else
# define ASSERT(cond,fmt,...) ((void)0)
#endif
#if DEBUG
void
cbuffer_assert( CBuffer* cb, const char* file, long lineno )
{
const char* reason = NULL;
if (cb->rpos < 0 || cb->rpos >= cb->size) {
reason = "rpos is out of bounds";
}
else if (cb->count < 0 || cb->count > cb->size) {
reason = "count is incorrect";
}
if (!reason)
return;
fprintf(stderr, "assert:%s:%ld: assertion failed: %s (pos=%d count=%d size=%d)\n",
file, lineno, reason, cb->rpos, cb->count, cb->size);
assert(0);
}
# define CBUFFER_ASSERT(cb) cbuffer_assert(cb,__FUNCTION__,__LINE__)
#else
# define CBUFFER_ASSERT(cb) ((void)0)
#endif
int
cbuffer_write_peek( CBuffer* cb, uint8_t* *pbase )
{
int wpos = cb->rpos + cb->count;
int avail = cb->size - cb->count;
CBUFFER_ASSERT(cb);
if (wpos >= cb->size)
wpos -= cb->size;
if (wpos + avail > cb->size)
avail = cb->size - wpos;
*pbase = cb->buff + wpos;
return avail;
}
void
cbuffer_write_step( CBuffer* cb, int len )
{
CBUFFER_ASSERT(cb);
cb->count += len;
if (cb->count > cb->size)
cb->count = cb->size;
}
int
cbuffer_write( CBuffer* cb, const void* from, int len )
{
int len2 = len;
CBUFFER_ASSERT(cb);
while (len2 > 0) {
int avail = cb->size - cb->count;
int wpos = cb->rpos + cb->count;
ASSERT(avail >= 0, "avail is negative: %d", avail);
if (avail == 0)
break;
if (wpos >= cb->size)
wpos -= cb->size;
ASSERT( wpos >= 0 && wpos < cb->size, "wpos is out-of-bounds: %d (rpos=%d)", wpos, cb->rpos);
if (wpos + avail > cb->size)
avail = cb->size - wpos;
if (avail > len2)
avail = len2;
memcpy( cb->buff + wpos, (const char*)from, avail );
from = (char*)from + avail;
len2 -= avail;
cb->count += avail;
}
return len - len2;
}
int
cbuffer_read( CBuffer* cb, void* to, int len )
{
int len2 = len;
CBUFFER_ASSERT(cb);
while (len2 > 0) {
int avail = cb->count;
int rpos = cb->rpos;
ASSERT(avail >= 0, "avail is negative: %d", avail);
if (avail == 0)
break;
ASSERT((rpos >= 0 && rpos < cb->size), "rpos is out-of-bounds: %d", rpos);
if (rpos+avail > cb->size)
avail = cb->size - rpos;
if (avail > len2)
avail = len2;
memcpy( (char*)to, (const char*)cb->buff + rpos, avail );
to = (char*)to + avail;
len2 -= avail;
cb->count -= avail;
cb->rpos += avail;
if (cb->rpos >= cb->size)
cb->rpos -= cb->size;
}
return len - len2;
}
int
cbuffer_read_peek( CBuffer* cb, uint8_t* *pbase )
{
int rpos = cb->rpos;
int avail = cb->count;
CBUFFER_ASSERT(cb);
if (rpos + avail > cb->size)
avail = cb->size - rpos;
*pbase = cb->buff + rpos;
return avail;
}
void
cbuffer_read_step( CBuffer* cb, int len )
{
CBUFFER_ASSERT(cb);
if (len > cb->count)
len = cb->count;
cb->rpos += len;
if (cb->rpos >= cb->size)
cb->rpos -= cb->size;
cb->count -= len;
}
const char*
cbuffer_quote( CBuffer* cb )
{
STRALLOC_DEFINE(s);
char* q;
stralloc_format( s, "cbuffer %p (pos=%d count=%d size=%d)",
cb, cb->rpos, cb->count, cb->size );
q = stralloc_to_tempstr( s );
stralloc_reset(s);
return q;
}
const char*
cbuffer_quote_data( CBuffer* cb )
{
STRALLOC_DEFINE(s);
int len = cb->count;
int rpos = cb->rpos;
char* result;
while (len > 0) {
int avail = len;
if (rpos >= cb->size)
rpos -= cb->size;
if (rpos + avail > cb->size)
avail = cb->size - rpos;
stralloc_add_quote_bytes( s, cb->buff + rpos, avail );
rpos += avail;
len -= avail;
}
result = stralloc_to_tempstr(s);
stralloc_reset(s);
return result;
}
void
cbuffer_print( CBuffer* cb )
{
/* print the content of a cbuffer */
printf( "%s: %s", cbuffer_quote(cb), cbuffer_quote_data(cb) );
}