-
Notifications
You must be signed in to change notification settings - Fork 8
/
buffer.oc
302 lines (262 loc) · 8.84 KB
/
buffer.oc
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! A growable buffer / string-builder type
//!
//! The buffer is a growable array of bytes, with a null terminator at the end.
//! It can be used as a string builder, or as a string itself. It is also useful
//! to hold binary data.
import std::libc::{ memcpy, memset, exit }
import std::mem
import std::sv::{ SV }
import std::{ Endian }
//* A growable buffer / string-builder type holding bytes.
[formatting "%.*s" "$.size, $.data"]
struct Buffer {
data: &u8
size: u32
capacity: u32
}
def Buffer::make(capacity: u32 = 16): Buffer {
return Buffer(
data: mem::alloc<u8>(capacity),
size: 0,
capacity
)
}
def Buffer::from_str(s: str): Buffer {
return Buffer(
data: s as &u8,
size: s.len() as u32,
capacity: s.len() as u32,
)
}
def Buffer::from_sv(sv: SV): Buffer {
let data = mem::alloc<u8>(sv.len + 1)
memcpy(data, sv.data, sv.len)
data[sv.len] = '\0' as u8
return Buffer(
data: data,
size: sv.len,
capacity: sv.len,
)
}
def Buffer::from_sized_str(s: str, size: u32): Buffer {
return Buffer(
data: s as &u8,
size: size as u32,
capacity: size as u32,
)
}
def Buffer::resize_if_necessary(&this, new_size: u32) {
// NOTE: To make a buffer trivially usable as a C-style string,
// we always allocate one extra byte for the null terminator, and
// ensure that any unused bytes are zeroed out.
if new_size + 1 >= .capacity {
let new_capacity = u32::max(.capacity * 3 / 2, new_size + 1)
.data = mem::realloc<u8>(.data, .capacity, new_capacity)
// Zero out the new capacity
memset(.data + .capacity, 0, new_capacity - .capacity)
.capacity = new_capacity as u32
assert .data?, "Out of memory!"
}
}
def hex_dump(data: &u8, size: u32) {
print("(%d bytes): ", size)
for let i = 0; i < size; i += 1 {
if (i % 4 == 0 and i > 0) print("_")
print("%02x", data[i])
}
println("")
}
def bit_dump(data: &u8, size: u32) {
print("(%d bytes): ", size)
for let i = 0; i < size; i += 1 {
if (i > 0) print("_")
for let j = 0u8; j < 8; j += 1 {
print("%d", (data[i] >> (7u8 - j)) & 1)
}
}
println("")
}
def Buffer::hex_dump(this) => hex_dump(.data, .size)
def Buffer::bit_dump(this) => bit_dump(.data, .size)
[operator "[]"]
def Buffer::get_byte_at(this, index: u32): u8 {
assert index < .size, "Index out of bounds"
return .data[index]
}
[operator "+="]
//* Write the buffer contents to this buffer
def Buffer::write_buf(&this, buf: &Buffer) {
.resize_if_necessary(new_size: .size + buf.size)
memcpy(.data + .size, buf.data, buf.size)
.size += buf.size
}
[operator "<<="]
//* Write the buffer contents to this buffer and free it
def Buffer::write_buf_f(&this, buf: &Buffer) {
.write_buf(buf)
buf.free()
}
[operator "+="]
//* Write the string to the buffer
def Buffer::write_str(&this, s: str) {
let len = s.len() as u32
.resize_if_necessary(new_size: .size + len) // +1 for null terminator
memcpy(.data + .size, s, len)
.size += len
}
[operator "<<="]
//* Write the string to the buffer and free it
def Buffer::write_str_f(&this, s: str) {
.write_str(s)
mem::free(s)
}
[operator "+="]
def Buffer::write_char(&this, c: char) => .write_u8(c as u8)
def Buffer::write_bytes(&this, bytes: untyped_ptr, size: u32) {
.resize_if_necessary(new_size: .size + size)
memcpy(.data + .size, bytes, size)
.size += size
}
[operator "+="]
def Buffer::write_sv(&this, sv: SV) => .write_bytes(sv.data as &u8, sv.len)
//* Write a signed 64-bit integer with the given endianness
def Buffer::write_i64(&this, value: i64, endian: Endian = Big) { .write_u64(value as u64, endian) }
//* Write an unsigned 64-bit integer with the given endianness
def Buffer::write_u64(&this, value: u64, endian: Endian = Big) {
.resize_if_necessary(.size + 8)
for let i = 0; i < 8; i += 1 {
let shift = if endian == Big then i else 7 - i
.data[.size + i] = ((value >> (56u64 - shift as u64 * 8)) & 0xff) as u8
}
.size += 8
}
//* Write a signed 32-bit integer with the given endianness
def Buffer::write_i32(&this, value: i32, endian: Endian = Big) { .write_u32(value as u32, endian) }
//* Write an unsigned 32-bit integer with the given endianness
def Buffer::write_u32(&this, value: u32, endian: Endian = Big) {
.resize_if_necessary(.size + 4)
for let i = 0; i < 4; i += 1 {
let shift = if endian == Big then i else 3 - i
.data[.size + i] = ((value >> (24u32 - shift * 8)) & 0xff) as u8
}
.size += 4
}
//* Write a signed 16-bit integer with the given endianness
def Buffer::write_i16(&this, value: i16, endian: Endian = Big) { .write_u16(value as u16, endian) }
//* Write an unsigned 16-bit integer with the given endianness
def Buffer::write_u16(&this, value: u16, endian: Endian = Big) {
.resize_if_necessary(.size + 2)
for let i = 0; i < 2; i += 1 {
let shift = if endian == Big then i else 1 - i
.data[.size + i] = ((value >> (8u16 - shift as u16 * 8)) & 0xff) as u8
}
.size += 2
}
[operator "+="]
//* Write a signed 8-bit integer with the given endianness
def Buffer::write_i8(&this, value: i8) { .write_u8(value as u8) }
[operator "+="]
//* Write an unsigned 8-bit integer with the given endianness
def Buffer::write_u8(&this, value: u8) {
.resize_if_necessary(.size + 1)
.data[.size] = value
.size += 1
}
//* Get a BytesReader object for reading Binary data
def Buffer::reader(this): BytesReader => BytesReader::make(.data, .size)
//* Allocate a new string with the contents of the buffer and return it
def Buffer::new_str(this): str => (.data as str).copy()
//* Return a reference to the buffer's contents as a string
def Buffer::str(this): str => .data as str
//* Return a reference to the buffer's contents as an SV
def Buffer::sv(this): SV => SV(.data as str, .size)
def Buffer::copy(&this): Buffer {
let new_data = mem::alloc<u8>(.capacity)
memcpy(new_data, .data, .size)
return Buffer(
data: new_data,
size: .size,
capacity: .capacity,
)
}
def Buffer::clear(&this) {
.size = 0
memset(.data, 0, .capacity)
}
def Buffer::free(&this) {
mem::free(.data)
}
//! Wrapper for reading binary data from a a series of bytes.
//!
//! This can be used along with the {{Buffer}} and {{SV}} types
//! to read binary data from a series of bytes.
struct BytesReader {
data: &u8
size: u32
index: u32
}
def BytesReader::make(data: &u8, size: u32): BytesReader {
return BytesReader(data, size, 0)
}
//* Read a signed 64-bit integer with the given endianness
def BytesReader::read_i64(&this, endian: Endian = Big): i64 { return .read_u64(endian) as i64 }
//* Read an unsigned 64-bit integer with the given endianness
def BytesReader::read_u64(&this, endian: Endian = Big): u64 {
let value = 0u64
for let i = 0; i < 8; i += 1 {
let shift = if endian == Big then i else 7 - i
value = value | .data[.index + i] as u64 << (56u64 - shift as u64 * 8)
}
.index += 8
return value as u64
}
//* Read a signed 32-bit integer with the given endianness
def BytesReader::read_i32(&this, endian: Endian = Big): i32 { return .read_u32(endian) as i32 }
//* Read an unsigned 32-bit integer with the given endianness
def BytesReader::read_u32(&this, endian: Endian = Big): u32 {
let value = 0
for let i = 0; i < 4; i += 1 {
let shift = if endian == Big then i else 3 - i
value = value | .data[.index + i] as u32 << (24u32 - shift * 8)
}
.index += 4
return value
}
//* Read a signed 16-bit integer with the given endianness
def BytesReader::read_i16(&this, endian: Endian = Big): i16 { return .read_u16(endian) as i16 }
//* Read an unsigned 16-bit integer with the given endianness
def BytesReader::read_u16(&this, endian: Endian = Big): u16 {
let value = 0u16
for let i = 0; i < 2; i += 1 {
let shift = if endian == Big then i else 1 - i
value = value | .data[.index + i] as u16 << (8u16 - shift as u16 * 8)
}
.index += 2
return value as u16
}
//* Read a signed 8-bit integer
def BytesReader::read_i8(&this): i8 { return .read_u8() as i8 }
//* Read an unsigned 8-bit integer
def BytesReader::read_u8(&this): u8 {
let value = .data[.index]
.index += 1
return value
}
//* Read `count` bytes from the buffer.
def BytesReader::read_bytes(&this, _dst: untyped_ptr, count: u32) {
let dst = _dst as &u8
if dst? {
for let i = 0; i < count; i += 1 {
dst[i] = .data[.index as u32 + i]
}
}
.index += count
}
//* Read `count` bytes from the buffer and return them as a view
def BytesReader::read_bytes_sv(&this, count: u32): SV {
let sv = SV((.data + .index) as str, count)
.index += count
return sv
}
//* Returns if the buffer is empty.
def BytesReader::is_empty(&this): bool => .index >= .size