-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathByteBuffer.c
More file actions
219 lines (181 loc) · 4.72 KB
/
ByteBuffer.c
File metadata and controls
219 lines (181 loc) · 4.72 KB
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
/**
* This is a pure C implementation of a ByteBuffer with the functionality similar
* to that of java.nio.ByteBuffer class.
*
* Created 2014-05-31
* Author Stanley Hawkeye
* Version 0.1
*/
#include <stdlib.h>
#include <string.h>
#include "ByteBuffer.h"
/**
* Allocates a new buffer
* @param size The size of the new buffer
*/
ByteBuffer* buffer_allocate(uint64_t size)
{
ByteBuffer* buffer = malloc(sizeof(ByteBuffer));
if(buffer == NULL)
return NULL;
buffer->size = size;
buffer->data = malloc(sizeof(uint8_t) * size);
if(buffer->data == NULL)
return NULL;
buffer_clear(buffer);
return buffer;
}
/**
* Frees the buffer from memory
*/
void buffer_free(ByteBuffer* buffer)
{
free(buffer->data);
free(buffer);
}
/**
* Clears the buffer -- erases all data in the buffer,
* and sets the position to zero and limit to the buffer's size.
*/
void buffer_clear(ByteBuffer* buffer)
{
memset(buffer->data, 0, buffer->size);
buffer->position = 0;
buffer->limit = buffer->size;
}
/**
* Compacts the buffer -- moves all the data from the current position to the beginning of the buffer
*/
void buffer_compact(ByteBuffer* buffer)
{
uint64_t numberOfBytes = buffer->limit - buffer->position;
memmove(buffer->data, &(buffer->data[buffer->position]), numberOfBytes);
buffer->limit = buffer->size;
buffer->position = numberOfBytes;
}
/**
* Flips the buffer -- sets the limit to position and position to zero
*/
void buffer_flip(ByteBuffer* buffer)
{
buffer->limit = buffer->position;
buffer->position = 0;
}
// put operations
uint8_t buffer_put(ByteBuffer* buffer, uint8_t data)
{
if(buffer == NULL)
return 0;
if(buffer->position == buffer->size)
return 0;
buffer->data[buffer->position++] = data;
return 1;
}
uint8_t buffer_putShort(ByteBuffer* buffer, int16_t data)
{
if(buffer == NULL)
return 0;
if(buffer->position + 1 == buffer->size)
return 0;
buffer->data[buffer->position++] = data & 255;
buffer->data[buffer->position++] = (data << 8) & 255;
return 2;
}
uint8_t buffer_putInt(ByteBuffer* buffer, int32_t data)
{
if(buffer == NULL)
return 0;
if(buffer->position + 3 == buffer->size)
return 0;
buffer->data[buffer->position++] = data & 255;
buffer->data[buffer->position++] = (data << 8) & 255;
buffer->data[buffer->position++] = (data << 16) & 255;
buffer->data[buffer->position++] = (data << 24) & 255;
return 4;
}
uint8_t buffer_putLong(ByteBuffer* buffer, int64_t data)
{
if(buffer == NULL)
return 0;
if(buffer->position + 7 == buffer->size)
return 0;
buffer->data[buffer->position++] = data & 255;
buffer->data[buffer->position++] = (data << 8) & 255;
buffer->data[buffer->position++] = (data << 16) & 255;
buffer->data[buffer->position++] = (data << 24) & 255;
buffer->data[buffer->position++] = (data << 32) & 255;
buffer->data[buffer->position++] = (data << 40) & 255;
buffer->data[buffer->position++] = (data << 48) & 255;
buffer->data[buffer->position++] = (data << 56) & 255;
return 8;
}
/**
* Gets one byte from the buffer.
* @returns 1 on success 0 on buffer underflow
*/
uint8_t buffer_get(ByteBuffer* buffer, int8_t* output)
{
if(buffer == NULL)
return 0;
if(buffer->position >= buffer->limit)
return 0;
*output = buffer->data[buffer->position++];
return 1;
}
/**
* Gets a 16 bit integer from the buffer.
* @returns 2 on success 0 on buffer underflow
*/
uint8_t buffer_getShort(ByteBuffer* buffer, int16_t* output)
{
if(buffer == NULL)
return 0;
if(buffer->position + 1 >= buffer->limit)
return 0;
*output = *((int16_t*)(buffer->data + buffer->position));
buffer->position += 2;
return 2;
}
/**
* Gets a 32 bit integer from the buffer.
* @returns 4 on success 0 on buffer underflow
*/
uint8_t buffer_getInt(ByteBuffer* buffer, int32_t* output)
{
if(buffer == NULL)
return 0;
if(buffer->position + 3 >= buffer->limit)
return 0;
*output = *((int32_t*)(buffer->data + buffer->position));
buffer->position += 4;
return 4;
}
/**
* Gets a 64 bit integer from the buffer.
* @returns 8 on success 0 on buffer underflow
*/
uint8_t buffer_getLong(ByteBuffer* buffer, int64_t* output)
{
if(buffer == NULL)
return 0;
if(buffer->position + 7 >= buffer->limit)
return 0;
*output = *((int64_t*)(buffer->data + buffer->position));
buffer->position += 8;
return 8;
}
/**
* Gets bytes from the buffer and copies them into the output.
* @param output The pointer to the output (the output must be already allocated)
* @param size The number of bytes to be copied.
* @returns 1 on success 0 on buffer underflow
*/
uint64_t buffer_getBytes(ByteBuffer* buffer, void* output, uint64_t size)
{
if(buffer == NULL)
return 0;
if(buffer->position + size == buffer->limit)
return 0;
memmove(output, buffer->data + buffer->position, size);
return size;
}