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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
** v_pack.c
**
** These functions are used to pack and unpack various quantities to/from network
** packet buffers. They do not care about alignment, operating at byte level internally.
** The external byte-ordering used is big-endian (aka "network byte order") for all
** quantities larger than a single byte.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "v_pack.h"
size_t vnp_raw_pack_uint8(void *buffer, uint8 data)
{
*(uint8 *) buffer = data;
return sizeof data;
}
size_t vnp_raw_unpack_uint8(const void *buffer, uint8 *data)
{
*data = *(uint8 *) buffer;
return sizeof *data;
}
size_t vnp_raw_pack_uint8_vector(void *buffer, const uint8 *data, unsigned int length)
{
memcpy(buffer, data, length);
return length;
}
size_t vnp_raw_unpack_uint8_vector(const void *buffer, uint8 *data, unsigned int length)
{
memcpy(data, buffer, length);
return length;
}
size_t vnp_raw_pack_uint16(void *buffer, uint16 data)
{
*(uint8 *) buffer = (data & 0xFF00) >> 8;
*((uint8 *) buffer + 1) = data & 0xFF;
return sizeof data;
}
size_t vnp_raw_unpack_uint16(const void *buffer, uint16 *data)
{
const register uint8 *b = buffer;
register uint16 tmp;
tmp = ((uint16) *b++) << 8;
tmp |= (uint16) *b;
*data = tmp;
return sizeof *data;
}
size_t vnp_raw_pack_uint16_vector(void *buffer, const uint16 *data, unsigned int length)
{
register uint8 *b = buffer;
unsigned int i;
for(i = 0; i < length; i++)
{
*b++ = (*data & 0xFF00) >> 8;
*b++ = *data & 0xFF;
data++;
}
return length * 2;
}
size_t vnp_raw_unpack_uint16_vector(const void *buffer, uint16 *data, unsigned int length)
{
register const uint8 *b = buffer;
uint16 *end;
for(end = data + length; end != data; data++)
{
*data = ((uint16) *b++) << 8;
*data |= (uint16) *b++;
}
return length * 2;
}
size_t vnp_raw_pack_uint24(void *buffer, uint32 data)
{
register uint8 *p = buffer;
data >>= 8;
*(p++) = (data >> 24) & 0xFF;
*(p++) = (data >> 16) & 0xFF;
*(p++) = (data >> 8) & 0xFF;
return 3;
}
size_t vnp_raw_unpack_uint24(const void *buffer, uint32 *data)
{
register const uint8 *p = buffer;
register uint32 tmp = 0;
tmp |= ((uint32) *p++) << 24;
tmp |= ((uint32) *p++) << 16;
tmp |= ((uint32) *p++) << 8;
tmp |= tmp >> 24;
return 3;
}
size_t vnp_raw_pack_uint24_vector(void *buffer, const uint32 *data, unsigned int length)
{
register uint8 *b = buffer;
unsigned int i;
for(i = 0; i < length; i++)
{
*b++ = (*data >> 24) & 0xFF;
*b++ = (*data >> 16) & 0xFF;
*b++ = (*data >> 8) & 0xFF;
data++;
}
return length * 3;
}
size_t vnp_raw_unpack_uint24_vector(const void *buffer, uint32 *data, unsigned int length)
{
register const uint8 *b = buffer;
register uint32 tmp;
uint32 *end;
for(end = data + length; end != data; data++)
{
tmp = ((uint32) *b++) << 24;
tmp |= ((uint32) *b++) << 16;
tmp |= ((uint32) *b++) << 8;
tmp |= tmp >> 24;
*data = tmp;
}
return length * 3;
}
size_t vnp_raw_pack_uint32(void *buffer, uint32 data)
{
register uint8 *b = buffer;
*b++ = (data >> 24) & 0xFF;
*b++ = (data >> 16) & 0xFF;
*b++ = (data >> 8) & 0xFF;
*b++ = data & 0xFF;
return sizeof data;
}
size_t vnp_raw_unpack_uint32(const void *buffer, uint32 *data)
{
register const uint8 *b = buffer;
*data = ((uint32) *b++) << 24;
*data |= ((uint32) *b++) << 16;
*data |= ((uint32) *b++) << 8;
*data |= *b;
return sizeof *data;
}
size_t vnp_raw_pack_uint32_vector(void *buffer, const uint32 *data, unsigned int length)
{
register uint8 *b = buffer;
unsigned int i;
for(i = 0; i < length; i++)
{
*b++ = (*data >> 24) & 0xFF;
*b++ = (*data >> 16) & 0xFF;
*b++ = (*data >> 8) & 0xFF;
*b++ = *data & 0xFF;
data++;
}
return length * 4;
}
size_t vnp_raw_unpack_uint32_vector(const void *buffer, uint32 *data, unsigned int length)
{
register const uint8 *b = buffer;
uint32 *end;
for(end = data + length; end != data; data++)
{
*data = ((uint32) *b++) << 24;
*data |= ((uint32) *b++) << 16;
*data |= ((uint32) *b++) << 8;
*data |= ((uint32) *b++);
}
return length * 4;
}
size_t vnp_raw_pack_real32(void *buffer, real32 data)
{
union { uint32 uint; real32 real; } punt;
punt.real = data;
return vnp_raw_pack_uint32(buffer, punt.uint);
}
size_t vnp_raw_unpack_real32(const void *buffer, real32 *data)
{
return vnp_raw_unpack_uint32(buffer, (uint32 *) data);
}
size_t vnp_raw_pack_real32_vector(void *buffer, const real32 *data, unsigned int length)
{
uint32 i;
for(i = 0; i < length; i++)
vnp_raw_pack_real32(&((uint8 *)buffer)[i * 4], data[i]);
return length * 4;
}
size_t vnp_raw_unpack_real32_vector(const void *buffer, real32 *data, unsigned int length)
{
uint32 i;
for(i = 0; i < length; i++)
vnp_raw_unpack_real32(&((uint8 *)buffer)[i * 4], &data[i]);
return length * 4;
}
size_t vnp_raw_pack_real64(void *buffer, real64 data)
{
union { uint32 uint[2]; real64 real; } punt;
uint32 size;
punt.real = data;
size = vnp_raw_pack_uint32(buffer, punt.uint[0]);
buffer = (uint8 *) buffer + size;
size += vnp_raw_pack_uint32(buffer, punt.uint[1]);
return size;
}
size_t vnp_raw_unpack_real64(const void *buffer, real64 *data)
{
union { uint32 uint[2]; real64 real; } punt;
uint32 size;
size = vnp_raw_unpack_uint32(buffer, &punt.uint[0]);
size += vnp_raw_unpack_uint32(((uint8 *)buffer) + size, &punt.uint[1]);
*data = punt.real;
return size;
}
size_t vnp_raw_pack_real64_vector(void *buffer, const real64 *data, unsigned int length)
{
uint32 i;
for(i = 0; i < length; i++)
vnp_raw_pack_real64(&((uint8 *)buffer)[i * 8], data[i]);
return length * 8;
}
size_t vnp_raw_unpack_real64_vector(const void *buffer, real64 *data, unsigned int length)
{
uint32 i;
for(i = 0; i < length; i++)
vnp_raw_unpack_real64(&((uint8 *)buffer)[i * 8], &data[i]);
return length * 8;
}
size_t vnp_raw_pack_string(void *buffer, const char *string, size_t max_size)
{
unsigned int i = 0;
char *p = buffer;
if(string != 0)
for(; i < max_size && string[i] != 0; i++)
p[i] = string[i];
p[i] = 0;
return ++i;
}
size_t vnp_raw_unpack_string(const void *buffer, char *string, size_t max_size, size_t max_size2)
{
unsigned int i;
const char *p = buffer;
max_size--;
max_size2--;
for(i = 0; i < max_size && i < max_size2 && p[i] != 0; i++)
string[i] = p[i];
string[i] = 0;
return ++i;
}
/* --------------------------------------------------------------------------------------------------- */
size_t vnp_pack_quat32(void *buffer, const VNQuat32 *data)
{
uint8 *out = buffer;
if(data == NULL)
return 0;
out += vnp_raw_pack_real32(out, data->x);
out += vnp_raw_pack_real32(out, data->y);
out += vnp_raw_pack_real32(out, data->z);
out += vnp_raw_pack_real32(out, data->w);
return out - (uint8 *) buffer;
}
size_t vnp_unpack_quat32(const void *buffer, VNQuat32 *data)
{
const uint8 *in = buffer;
if(data == NULL)
return 0;
in += vnp_raw_unpack_real32(in, &data->x);
in += vnp_raw_unpack_real32(in, &data->y);
in += vnp_raw_unpack_real32(in, &data->z);
in += vnp_raw_unpack_real32(in, &data->w);
return in - (uint8 *) buffer;
}
size_t vnp_pack_quat64(void *buffer, const VNQuat64 *data)
{
uint8 *out = buffer;
if(data == NULL)
return 0;
out += vnp_raw_pack_real64(out, data->x);
out += vnp_raw_pack_real64(out, data->y);
out += vnp_raw_pack_real64(out, data->z);
out += vnp_raw_pack_real64(out, data->w);
return out - (uint8 *) buffer;
}
size_t vnp_unpack_quat64(const void *buffer, VNQuat64 *data)
{
const uint8 *in = buffer;
if(data == NULL)
return 0;
in += vnp_raw_unpack_real64(in, &data->x);
in += vnp_raw_unpack_real64(in, &data->y);
in += vnp_raw_unpack_real64(in, &data->z);
in += vnp_raw_unpack_real64(in, &data->w);
return in - (uint8 *) buffer;
}
size_t vnp_pack_audio_block(void *buffer, VNABlockType type, const VNABlock *block)
{
if(block == NULL)
return 0;
switch(type)
{
case VN_A_BLOCK_INT8:
return vnp_raw_pack_uint8_vector(buffer, block->vint8, sizeof block->vint8 / sizeof *block->vint8);
case VN_A_BLOCK_INT16:
return vnp_raw_pack_uint16_vector(buffer, block->vint16, sizeof block->vint16 / sizeof *block->vint16);
case VN_A_BLOCK_INT24:
return vnp_raw_pack_uint24_vector(buffer, block->vint24, sizeof block->vint24 / sizeof *block->vint24);
case VN_A_BLOCK_INT32:
return vnp_raw_pack_uint32_vector(buffer, block->vint32, sizeof block->vint32 / sizeof *block->vint32);
case VN_A_BLOCK_REAL32:
return vnp_raw_pack_real32_vector(buffer, block->vreal32, sizeof block->vreal32 / sizeof *block->vreal32);
case VN_A_BLOCK_REAL64:
return vnp_raw_pack_real64_vector(buffer, block->vreal64, sizeof block->vreal64 / sizeof *block->vreal64);
}
return 0;
}
size_t vnp_unpack_audio_block(const void *buffer, VNABlockType type, VNABlock *block)
{
if(block == NULL)
return 0;
switch(type)
{
case VN_A_BLOCK_INT8:
return vnp_raw_unpack_uint8_vector(buffer, block->vint8, sizeof block->vint8 / sizeof *block->vint8);
case VN_A_BLOCK_INT16:
return vnp_raw_unpack_uint16_vector(buffer, block->vint16, sizeof block->vint16 / sizeof *block->vint16);
case VN_A_BLOCK_INT24:
return vnp_raw_unpack_uint24_vector(buffer, block->vint24, sizeof block->vint24 / sizeof *block->vint24);
case VN_A_BLOCK_INT32:
return vnp_raw_unpack_uint32_vector(buffer, block->vint32, sizeof block->vint32 / sizeof *block->vint32);
case VN_A_BLOCK_REAL32:
return vnp_raw_unpack_real32_vector(buffer, block->vreal32, sizeof block->vreal32 / sizeof *block->vreal32);
case VN_A_BLOCK_REAL64:
return vnp_raw_unpack_real64_vector(buffer, block->vreal64, sizeof block->vreal64 / sizeof *block->vreal64);
}
return 0;
}
|