-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathosc_mem.c
310 lines (297 loc) · 6.4 KB
/
osc_mem.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
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
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2009-ll, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "osc.h"
#include "osc_mem.h"
#include "osc_util.h"
#include "osc_byteorder.h"
#include "osc_timetag.h"
static void *(*osc_mem_alloc_fp)(size_t size) = malloc;
static void (*osc_mem_free_fp)(void *ptr) = free;
static void *(*osc_mem_resize_fp)(void *ptr, size_t size) = realloc;
void *osc_mem_alloc(size_t size)
{
void *p = osc_mem_alloc_fp(size);
return p;
}
void *osc_mem_resize(void *ptr, size_t size)
{
if(!ptr){
void *p = osc_mem_alloc_fp(size);
return p;
}
void *p = osc_mem_resize_fp(ptr, size);
return p;
}
void osc_mem_free(void *ptr)
{
OSC_MEM_INVALIDATE(ptr);
osc_mem_free_fp(ptr);
}
void osc_set_mem(void *(*malloc_func)(size_t),
void (*free_func)(void*),
void *(*resize_func)(void*, size_t))
{
if(malloc_func){
osc_mem_alloc_fp = malloc_func;
}
if(free_func){
osc_mem_free_fp = free_func;
}
if(resize_func){
osc_mem_resize_fp = resize_func;
}
}
// we don't define the size for the bundle or timetag typetags here since they're switchable with
// #defines (in osc.h and osc_timetag.h respectively). It's not necessary to define their sizes
// here because they're special cases in osc_sizeof() below.
char osc_data_lengths[128] = {
/* 0 */ -1 ,
/* 1 */ -1 ,
/* 2 */ -1 ,
/* 3 */ -1 ,
/* 4 */ -1 ,
/* 5 */ -1 ,
/* 6 */ -1 ,
/* 7 */ -1 ,
/* 8 */ -1 ,
/* 9 */ -1 ,
/* 10 */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 87 W */ -1 ,
/* 88 X */ -1 ,
/* 89 Y */ -1 ,
/* 90 Z */ -1 ,
/* 91 */ -1 ,
/* 92 */ -1 ,
/* 93 */ -1 ,
/* 94 */ -1 ,
/* 95 */ -1 ,
/* 96 */ -1 ,
/* 97 a */ -1 ,
/* 98 b */ sizeof(char *) ,
/* 99 c */ 4 ,
/* 100 d */ 8 ,
/* 101 e */ -1 ,
/* 102 f */ 4 ,
/* 103 g */ -1 ,
/* 104 h */ 8 ,
/* 105 i */ 4 ,
/* 106 j */ 16 ,
/* 107 k */ -1 ,
/* 108 l */ -1 ,
/* 109 m */ 4 ,
/* 110 n */ -1 ,
/* 111 o */ -1 ,
/* 112 p */ -1 ,
/* 113 q */ -1 ,
/* 114 r */ 4 ,
/* 115 s */ sizeof(char *) ,
/* 116 t */ -1 ,
/* 117 u */ 4 ,
/* 118 v */ -1 ,
/* 119 w */ -1 ,
/* 120 x */ -1 ,
/* 121 y */ -1 ,
/* 122 z */ -1 ,
/* 123 */ -1 ,
/* 124 */ -1 ,
/* 125 */ -1 ,
/* 126 */ -1 ,
/* 127 */ -1
};
size_t osc_sizeof(unsigned char typetag, char *data){
if(typetag > 127){
return -1;
}
if(!data){
return -1;
}
switch(typetag){
case 'b':
return osc_util_getBlobLength(ntoh32(*((int32_t *)data)));
case 's':
case 'S':
return osc_util_getPaddedStringLen(data);
case OSC_BUNDLE_TYPETAG:
return ntoh32(*((uint32_t *)data)) + 4;
case OSC_TIMETAG_TYPETAG:
return OSC_TIMETAG_SIZEOF;
default:
return osc_data_lengths[typetag];
}
}
int osc_mem_shouldByteswap(unsigned char typetag){
switch(typetag){
// signed ints
case 'c':
case 'u':
case 'a':
case 'i':
case 'h':
case 'j':
// unsigned ints
case 'C':
case 'U':
case 'A':
case 'I':
case 'H':
case 'J':
// floats
case 'e':
case 'f':
case 'd':
case 'q':
return 1;
default:
return 0;
}
}
t_osc_err osc_mem_encodeByteorder(unsigned char typetag, char *data, char **out)
{
size_t size = osc_sizeof(typetag, data);
if(!osc_mem_shouldByteswap(typetag)){
memcpy(*out, data, size);
return OSC_ERR_NONE;
}
char tmp[size];
switch(size){
case 1:
break;
case 2:
*((uint16_t *)tmp) = hton16(*((uint16_t *)data));
break;
case 4:
*((uint32_t *)tmp) = hton32(*((uint32_t *)data));
break;
case 8:
*((uint64_t *)tmp) = hton64(*((uint64_t *)data));
break;
case 16:
*((uint128_t *)tmp) = hton128(*((uint128_t *)data));
break;
}
memcpy(*out, tmp, size);
return OSC_ERR_NONE;
}
t_osc_err osc_mem_decodeByteorder(unsigned char typetag, char *data, char **out)
{
size_t size = osc_sizeof(typetag, data);
if(!osc_mem_shouldByteswap(typetag)){
memcpy(*out, data, size);
return OSC_ERR_NONE;
}
char tmp[size];
switch(size){
case 1:
break;
case 2:
*((uint16_t *)tmp) = ntoh16(*((uint16_t *)data));
break;
case 4:
*((uint32_t *)tmp) = ntoh32(*((uint32_t *)data));
break;
case 8:
*((uint64_t *)tmp) = ntoh64(*((uint64_t *)data));
break;
case 16:
*((uint128_t *)tmp) = ntoh128(*((uint128_t *)data));
break;
}
memcpy(*out, tmp, size);
return OSC_ERR_NONE;
}