aboutsummaryrefslogtreecommitdiff
path: root/osc.lv2/osc.lv2/reader.h
blob: 2f5def9589fb6517720aa095bba281022a0ec126 (plain)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
 * Copyright (c) 2015-2016 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source 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
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#ifndef LV2_OSC_READER_H
#define LV2_OSC_READER_H

#include <stdbool.h>
#include <string.h>
#include <endian.h>

#include <osc.lv2/osc.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _LV2_OSC_Reader LV2_OSC_Reader;
typedef struct _LV2_OSC_Item LV2_OSC_Item;
typedef struct _LV2_OSC_Arg LV2_OSC_Arg;

struct _LV2_OSC_Reader {
	const uint8_t *buf;
	const uint8_t *ptr;
	const uint8_t *end;
};

struct _LV2_OSC_Item {
	int32_t size;
	const uint8_t *body;

	uint64_t timetag;
	const uint8_t *end;
};

struct _LV2_OSC_Arg {
	const char *type;
	int32_t size;
	union {
		int32_t i;
		float f;
		const char *s;
		const uint8_t *b;

		int64_t h;
		double d;
		uint64_t t;

		const uint8_t *m;
		const char *S;
		char c;
		struct {
			uint8_t R;
			uint8_t G;
			uint8_t B;
			uint8_t A;
		}; // anonymous RGBA struct
	};

	const char *path;
	const uint8_t *end;
};

static inline void
lv2_osc_reader_initialize(LV2_OSC_Reader *reader, const uint8_t *buf, size_t size)
{
	reader->buf = buf;
	reader->ptr = buf;
	reader->end = buf + size;
}

static inline bool
lv2_osc_reader_overflow(LV2_OSC_Reader *reader, size_t size)
{
	return reader->ptr + size > reader->end;
}

static inline bool
lv2_osc_reader_be32toh(LV2_OSC_Reader *reader, union swap32_t *s32)
{
	if(lv2_osc_reader_overflow(reader, 4))
		return false;

	s32->u = *(const uint32_t *)reader->ptr;
	s32->u = be32toh(s32->u);
	reader->ptr += 4;

	return true;
}

static inline bool
lv2_osc_reader_be64toh(LV2_OSC_Reader *reader, union swap64_t *s64)
{
	if(lv2_osc_reader_overflow(reader, 8))
		return false;

	s64->u = *(const uint64_t *)reader->ptr;
	s64->u = be64toh(s64->u);
	reader->ptr += 8;

	return true;
}

static inline bool
lv2_osc_reader_get_int32(LV2_OSC_Reader *reader, int32_t *i)
{
	union swap32_t s32;
	if(!lv2_osc_reader_be32toh(reader, &s32))
		return false;

	*i = s32.i;

	return true;
}

static inline bool
lv2_osc_reader_get_float(LV2_OSC_Reader *reader, float *f)
{
	union swap32_t s32;
	if(!lv2_osc_reader_be32toh(reader, &s32))
		return false;

	*f = s32.f;

	return true;
}

static inline bool
lv2_osc_reader_get_int64(LV2_OSC_Reader *reader, int64_t *h)
{
	union swap64_t s64;
	if(!lv2_osc_reader_be64toh(reader, &s64))
		return false;

	*h = s64.h;

	return true;
}

static inline bool
lv2_osc_reader_get_timetag(LV2_OSC_Reader *reader, uint64_t *t)
{
	union swap64_t s64;
	if(!lv2_osc_reader_be64toh(reader, &s64))
		return false;

	*t = s64.u;

	return true;
}

static inline bool
lv2_osc_reader_get_double(LV2_OSC_Reader *reader, double *d)
{
	union swap64_t s64;
	if(!lv2_osc_reader_be64toh(reader, &s64))
		return false;

	*d = s64.d;

	return true;
}

static inline bool
lv2_osc_reader_get_string(LV2_OSC_Reader *reader, const char **s)
{
	const char *str = (const char *)reader->ptr;
	const size_t padded = LV2_OSC_PADDED_SIZE(strlen(str) + 1);
	if(lv2_osc_reader_overflow(reader, padded ))
		return false;

	*s = str;
	reader->ptr += padded;

	return true;
}

static inline bool
lv2_osc_reader_get_symbol(LV2_OSC_Reader *reader, const char **S)
{
	return lv2_osc_reader_get_string(reader, S);
}

static inline bool
lv2_osc_reader_get_midi(LV2_OSC_Reader *reader, const uint8_t **m)
{
	if(lv2_osc_reader_overflow(reader, 4))
		return false;

	*m = reader->ptr;
	reader->ptr += 4;

	return true;
}

static inline bool
lv2_osc_reader_get_blob(LV2_OSC_Reader *reader, int32_t *len, const uint8_t **body)
{
	if(!lv2_osc_reader_get_int32(reader, len))
		return false;

	const size_t padded = LV2_OSC_PADDED_SIZE(*len);
	if(lv2_osc_reader_overflow(reader, padded))
		return false;

	*body = reader->ptr;
	reader->ptr += padded;

	return true;
}

static inline bool
lv2_osc_reader_get_rgba(LV2_OSC_Reader *reader, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a)
{
	if(lv2_osc_reader_overflow(reader, 4))
		return false;

	*r = reader->ptr[0];
	*g = reader->ptr[1];
	*b = reader->ptr[2];
	*a = reader->ptr[3];
	reader->ptr += 4;

	return true;
}

static inline bool
lv2_osc_reader_get_char(LV2_OSC_Reader *reader, char *c)
{
	int32_t i;
	if(!lv2_osc_reader_get_int32(reader, &i))
		return false;

	*c = i;

	return true;
}

static inline LV2_OSC_Item *
lv2_osc_reader_item_raw(LV2_OSC_Reader *reader, LV2_OSC_Item *itm)
{
	if(!lv2_osc_reader_get_int32(reader, &itm->size))
		return NULL;

	if(lv2_osc_reader_overflow(reader, itm->size))
		return NULL;

	itm->body = reader->ptr;

	return itm;
}

static inline LV2_OSC_Item *
lv2_osc_reader_item_begin(LV2_OSC_Reader *reader, LV2_OSC_Item *itm, size_t len)
{
	if(lv2_osc_reader_overflow(reader, len))
		return NULL;

	itm->end = reader->ptr + len;

	if(lv2_osc_reader_overflow(reader, 16))
		return NULL;

	if(strncmp((const char *)reader->ptr, "#bundle", 8))
		return NULL;
	reader->ptr += 8;

	if(!lv2_osc_reader_get_timetag(reader, &itm->timetag))
		return NULL;

	return lv2_osc_reader_item_raw(reader, itm);
}

static inline bool
lv2_osc_reader_item_is_end(LV2_OSC_Reader *reader, LV2_OSC_Item *itm)
{
	return reader->ptr > itm->end;
}

static inline LV2_OSC_Item *
lv2_osc_reader_item_next(LV2_OSC_Reader *reader, LV2_OSC_Item *itm)
{
	reader->ptr += itm->size;

	return lv2_osc_reader_item_raw(reader, itm);
}

#define OSC_READER_BUNDLE_BEGIN(reader, len) \
	lv2_osc_reader_item_begin( \
		(reader), \
		&(LV2_OSC_Item){ .size = 0, .body = NULL, .timetag = 1ULL, .end = NULL }, \
		len)

#define OSC_READER_BUNDLE_ITERATE(reader, itm) \
	for(itm = itm; \
		itm && !lv2_osc_reader_item_is_end((reader), (itm)); \
		itm = lv2_osc_reader_item_next((reader), (itm)))

#define OSC_READER_BUNDLE_FOREACH(reader, itm, len) \
	for(LV2_OSC_Item *(itm) = OSC_READER_BUNDLE_BEGIN((reader), (len)); \
		itm && !lv2_osc_reader_item_is_end((reader), (itm)); \
		itm = lv2_osc_reader_item_next((reader), (itm)))

static inline LV2_OSC_Arg *
lv2_osc_reader_arg_raw(LV2_OSC_Reader *reader, LV2_OSC_Arg *arg)
{
	switch( (LV2_OSC_Type)*arg->type)
	{
		case LV2_OSC_INT32:
		{
			if(!lv2_osc_reader_get_int32(reader, &arg->i))
				return NULL;
			arg->size = 4;

			break;
		}
		case LV2_OSC_FLOAT:
		{
			if(!lv2_osc_reader_get_float(reader, &arg->f))
				return NULL;
			arg->size = 4;

			break;
		}
		case LV2_OSC_STRING:
		{
			if(!lv2_osc_reader_get_string(reader, &arg->s))
				return NULL;
			arg->size = strlen(arg->s) + 1;

			break;
		}
		case LV2_OSC_BLOB:
		{
			if(!lv2_osc_reader_get_blob(reader, &arg->size, &arg->b))
				return NULL;
			//arg->size = arg->size;

			break;
		}

		case LV2_OSC_TRUE:
		case LV2_OSC_FALSE:
		case LV2_OSC_NIL:
		case LV2_OSC_IMPULSE:
			break;

		case LV2_OSC_INT64:
		{
			if(!lv2_osc_reader_get_int64(reader, &arg->h))
				return NULL;
			arg->size = 8;

			break;
		}
		case LV2_OSC_DOUBLE:
		{
			if(!lv2_osc_reader_get_double(reader, &arg->d))
				return NULL;
			arg->size = 8;

			break;
		}
		case LV2_OSC_TIMETAG:
		{
			if(!lv2_osc_reader_get_timetag(reader, &arg->t))
				return NULL;
			arg->size = 8;

			break;
		}

		case LV2_OSC_MIDI:
		{
			if(!lv2_osc_reader_get_midi(reader, &arg->m))
				return NULL;
			arg->size = 4;

			break;
		}
		case LV2_OSC_SYMBOL:
		{
			if(!lv2_osc_reader_get_symbol(reader, &arg->S))
				return NULL;
			arg->size = strlen(arg->S) + 1;

			break;
		}
		case LV2_OSC_CHAR:
		{
			if(!lv2_osc_reader_get_char(reader, &arg->c))
				return NULL;
			arg->size = 4;

			break;
		}
		case LV2_OSC_RGBA:
		{
			if(!lv2_osc_reader_get_rgba(reader, &arg->R, &arg->G, &arg->B, &arg->A))
				return NULL;
			arg->size = 4;

			break;
		}
	}

	return arg;
}

static inline LV2_OSC_Arg *
lv2_osc_reader_arg_begin(LV2_OSC_Reader *reader, LV2_OSC_Arg *arg, size_t len)
{
	if(lv2_osc_reader_overflow(reader, len))
		return NULL;

	arg->end = reader->ptr + len;

	if(!lv2_osc_reader_get_string(reader, &arg->path)) //TODO check for validity
		return NULL;

	if(!lv2_osc_reader_get_string(reader, &arg->type)) //TODO check for validity
		return NULL;

	if(*arg->type != ',')
		return NULL;

	arg->type++; // skip ','

	return lv2_osc_reader_arg_raw(reader, arg);
}

static inline bool
lv2_osc_reader_arg_is_end(LV2_OSC_Reader *reader, LV2_OSC_Arg *arg)
{
	return (*arg->type == '\0') || (reader->ptr > arg->end);
}

static inline LV2_OSC_Arg *
lv2_osc_reader_arg_next(LV2_OSC_Reader *reader, LV2_OSC_Arg *arg)
{
	arg->type++;

	return lv2_osc_reader_arg_raw(reader, arg);
}

#define OSC_READER_MESSAGE_BEGIN(reader, len) \
	lv2_osc_reader_arg_begin( \
		(reader), \
		&(LV2_OSC_Arg){ .type = NULL, .size = 0, .path = NULL, .end = NULL }, \
		len)

#define OSC_READER_MESSAGE_ITERATE(reader, arg) \
	for(arg = arg; \
		arg && !lv2_osc_reader_arg_is_end((reader), (arg)); \
		arg = lv2_osc_reader_arg_next((reader), (arg)))

#define OSC_READER_MESSAGE_FOREACH(reader, arg, len) \
	for(LV2_OSC_Arg *(arg) = OSC_READER_MESSAGE_BEGIN((reader), (len)); \
		arg && !lv2_osc_reader_arg_is_end((reader), (arg)); \
		arg = lv2_osc_reader_arg_next((reader), (arg)))

static inline bool
lv2_osc_reader_arg_varlist(LV2_OSC_Reader *reader, const char *fmt, va_list args)
{
	for(const char *type = fmt; *type; type++)
	{
		switch( (LV2_OSC_Type)*type)
		{
			case LV2_OSC_INT32:
				if(!lv2_osc_reader_get_int32(reader, va_arg(args, int32_t *)))
					return false;
				break;
			case LV2_OSC_FLOAT:
				if(!lv2_osc_reader_get_float(reader, va_arg(args, float *)))
					return false;
				break;
			case LV2_OSC_STRING:
				if(!lv2_osc_reader_get_string(reader, va_arg(args, const char **)))
					return false;
				break;
			case LV2_OSC_BLOB:
				if(!lv2_osc_reader_get_blob(reader, va_arg(args, int32_t *), va_arg(args, const uint8_t **)))
					return false;
				break;

			case LV2_OSC_TRUE:
			case LV2_OSC_FALSE:
			case LV2_OSC_NIL:
			case LV2_OSC_IMPULSE:
				break;

			case LV2_OSC_INT64:
				if(!lv2_osc_reader_get_int64(reader, va_arg(args, int64_t *)))
					return false;
				break;
			case LV2_OSC_DOUBLE:
				if(!lv2_osc_reader_get_double(reader, va_arg(args, double *)))
					return false;
				break;
			case LV2_OSC_TIMETAG:
				if(!lv2_osc_reader_get_timetag(reader, va_arg(args, uint64_t *)))
					return false;
				break;

			case LV2_OSC_MIDI:
				if(!lv2_osc_reader_get_midi(reader, va_arg(args, const uint8_t **)))
					return false;
				break;
			case LV2_OSC_SYMBOL:
				if(!lv2_osc_reader_get_symbol(reader, va_arg(args, const char **)))
					return false;
				break;
			case LV2_OSC_CHAR:
				if(!lv2_osc_reader_get_char(reader, va_arg(args, char *)))
					return false;
				break;
			case LV2_OSC_RGBA:
				if(!lv2_osc_reader_get_rgba(reader, va_arg(args, uint8_t *), va_arg(args, uint8_t *),
						va_arg(args, uint8_t *), va_arg(args, uint8_t *)))
					return false;
				break;
		}
	}

	return true;
}

static inline bool
lv2_osc_reader_arg_vararg(LV2_OSC_Reader *reader, const char *fmt, ...)
{
  va_list args;
  va_start(args, fmt);

	const bool res = lv2_osc_reader_arg_varlist(reader, fmt, args);

	va_end(args);

	return res;
}

static inline bool
lv2_osc_reader_is_bundle(LV2_OSC_Reader *reader)
{
	return strncmp((const char *)reader->ptr, "#bundle", 8) == 0;
}

static inline bool
lv2_osc_reader_is_message(LV2_OSC_Reader *reader)
{
	return reader->ptr[0] == '/'; //FIXME check path
}

#ifdef __cplusplus
} // extern "C"
#endif

#endif // LV2_OSC_READER_H