summaryrefslogtreecommitdiffstats
path: root/third_party/pipewire/spa/pod/parser.h
blob: c0a3099da64fd5f6a3f1afc4d2b36252187e3501 (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
571
572
573
574
575
576
577
578
579
580
581
582
583
/* Spa
 *
 * Copyright © 2018 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef SPA_POD_PARSER_H
#define SPA_POD_PARSER_H

#ifdef __cplusplus
extern "C" {
#endif

#include <errno.h>
#include <stdarg.h>

#include <spa/pod/iter.h>
#include <spa/pod/vararg.h>

/**
 * \addtogroup spa_pod
 * \{
 */

struct spa_pod_parser_state {
	uint32_t offset;
	uint32_t flags;
	struct spa_pod_frame *frame;
};

struct spa_pod_parser {
	const void *data;
	uint32_t size;
	uint32_t _padding;
	struct spa_pod_parser_state state;
};

#define SPA_POD_PARSER_INIT(buffer,size)  (struct spa_pod_parser){ buffer, size, 0, {} }

static inline void spa_pod_parser_init(struct spa_pod_parser *parser,
				       const void *data, uint32_t size)
{
	*parser = SPA_POD_PARSER_INIT(data, size);
}

static inline void spa_pod_parser_pod(struct spa_pod_parser *parser,
				      const struct spa_pod *pod)
{
	spa_pod_parser_init(parser, pod, SPA_POD_SIZE(pod));
}

static inline void
spa_pod_parser_get_state(struct spa_pod_parser *parser, struct spa_pod_parser_state *state)
{
	*state = parser->state;
}

static inline void
spa_pod_parser_reset(struct spa_pod_parser *parser, struct spa_pod_parser_state *state)
{
	parser->state = *state;
}

static inline struct spa_pod *
spa_pod_parser_deref(struct spa_pod_parser *parser, uint32_t offset, uint32_t size)
{
	if (offset + 8 <= size) {
		struct spa_pod *pod = SPA_PTROFF(parser->data, offset, struct spa_pod);
		if (offset + SPA_POD_SIZE(pod) <= size)
			return pod;
	}
        return NULL;
}

static inline struct spa_pod *spa_pod_parser_frame(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
{
	return SPA_PTROFF(parser->data, frame->offset, struct spa_pod);
}

static inline void spa_pod_parser_push(struct spa_pod_parser *parser,
		      struct spa_pod_frame *frame, const struct spa_pod *pod, uint32_t offset)
{
	frame->pod = *pod;
	frame->offset = offset;
	frame->parent = parser->state.frame;
	frame->flags = parser->state.flags;
	parser->state.frame = frame;
}

static inline struct spa_pod *spa_pod_parser_current(struct spa_pod_parser *parser)
{
	struct spa_pod_frame *f = parser->state.frame;
	uint32_t size = f ? f->offset + SPA_POD_SIZE(&f->pod) : parser->size;
	return spa_pod_parser_deref(parser, parser->state.offset, size);
}

static inline void spa_pod_parser_advance(struct spa_pod_parser *parser, const struct spa_pod *pod)
{
	parser->state.offset += SPA_ROUND_UP_N(SPA_POD_SIZE(pod), 8);
}

static inline struct spa_pod *spa_pod_parser_next(struct spa_pod_parser *parser)
{
	struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod)
		spa_pod_parser_advance(parser, pod);
	return pod;
}

static inline int spa_pod_parser_pop(struct spa_pod_parser *parser,
		      struct spa_pod_frame *frame)
{
	parser->state.frame = frame->parent;
	parser->state.offset = frame->offset + SPA_ROUND_UP_N(SPA_POD_SIZE(&frame->pod), 8);
	return 0;
}

static inline int spa_pod_parser_get_bool(struct spa_pod_parser *parser, bool *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_bool(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_id(struct spa_pod_parser *parser, uint32_t *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_id(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_int(struct spa_pod_parser *parser, int32_t *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_int(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_long(struct spa_pod_parser *parser, int64_t *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_long(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_float(struct spa_pod_parser *parser, float *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_float(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_double(struct spa_pod_parser *parser, double *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_double(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_string(struct spa_pod_parser *parser, const char **value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_string(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_bytes(struct spa_pod_parser *parser, const void **value, uint32_t *len)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_bytes(pod, value, len)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_pointer(struct spa_pod_parser *parser, uint32_t *type, const void **value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_pointer(pod, type, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_fd(struct spa_pod_parser *parser, int64_t *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_fd(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_rectangle(struct spa_pod_parser *parser, struct spa_rectangle *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_rectangle(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_fraction(struct spa_pod_parser *parser, struct spa_fraction *value)
{
	int res = -EPIPE;
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod != NULL && (res = spa_pod_get_fraction(pod, value)) >= 0)
		spa_pod_parser_advance(parser, pod);
	return res;
}

static inline int spa_pod_parser_get_pod(struct spa_pod_parser *parser, struct spa_pod **value)
{
	struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod == NULL)
		return -EPIPE;
	*value = pod;
	spa_pod_parser_advance(parser, pod);
	return 0;
}
static inline int spa_pod_parser_push_struct(struct spa_pod_parser *parser,
		struct spa_pod_frame *frame)
{
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod == NULL)
		return -EPIPE;
	if (!spa_pod_is_struct(pod))
		return -EINVAL;
	spa_pod_parser_push(parser, frame, pod, parser->state.offset);
	parser->state.offset += sizeof(struct spa_pod_struct);
	return 0;
}

static inline int spa_pod_parser_push_object(struct spa_pod_parser *parser,
		struct spa_pod_frame *frame, uint32_t type, uint32_t *id)
{
	const struct spa_pod *pod = spa_pod_parser_current(parser);
	if (pod == NULL)
		return -EPIPE;
	if (!spa_pod_is_object(pod))
		return -EINVAL;
	if (type != SPA_POD_OBJECT_TYPE(pod))
		return -EPROTO;
	if (id != NULL)
		*id = SPA_POD_OBJECT_ID(pod);
	spa_pod_parser_push(parser, frame, pod, parser->state.offset);
	parser->state.offset = parser->size;
	return 0;
}

static inline bool spa_pod_parser_can_collect(const struct spa_pod *pod, char type)
{
	if (pod == NULL)
		return false;

	if (spa_pod_is_choice(pod) &&
	    SPA_POD_CHOICE_TYPE(pod) == SPA_CHOICE_None &&
	    spa_pod_parser_can_collect(SPA_POD_CHOICE_CHILD(pod), type))
		return true;

	switch (type) {
	case 'P':
		return true;
	case 'b':
		return spa_pod_is_bool(pod);
	case 'I':
		return spa_pod_is_id(pod);
	case 'i':
		return spa_pod_is_int(pod);
	case 'l':
		return spa_pod_is_long(pod);
	case 'f':
		return spa_pod_is_float(pod);
	case 'd':
		return spa_pod_is_double(pod);
	case 's':
		return spa_pod_is_string(pod) || spa_pod_is_none(pod);
	case 'S':
		return spa_pod_is_string(pod);
	case 'y':
		return spa_pod_is_bytes(pod);
	case 'R':
		return spa_pod_is_rectangle(pod);
	case 'F':
		return spa_pod_is_fraction(pod);
	case 'B':
		return spa_pod_is_bitmap(pod);
	case 'a':
		return spa_pod_is_array(pod);
	case 'p':
		return spa_pod_is_pointer(pod);
	case 'h':
		return spa_pod_is_fd(pod);
	case 'T':
		return spa_pod_is_struct(pod) || spa_pod_is_none(pod);
	case 'O':
		return spa_pod_is_object(pod) || spa_pod_is_none(pod);
	case 'V':
		return spa_pod_is_choice(pod);
	default:
		return false;
	}
}

#define SPA_POD_PARSER_COLLECT(pod,_type,args)						\
do {											\
	switch (_type) {								\
	case 'b':									\
		*va_arg(args, bool*) = SPA_POD_VALUE(struct spa_pod_bool, pod);		\
		break;									\
	case 'I':									\
	case 'i':									\
		*va_arg(args, int32_t*) = SPA_POD_VALUE(struct spa_pod_int, pod);	\
		break;									\
	case 'l':									\
		*va_arg(args, int64_t*) = SPA_POD_VALUE(struct spa_pod_long, pod);	\
		break;									\
	case 'f':									\
		*va_arg(args, float*) = SPA_POD_VALUE(struct spa_pod_float, pod);	\
		break;									\
	case 'd':									\
		*va_arg(args, double*) = SPA_POD_VALUE(struct spa_pod_double, pod);	\
		break;									\
	case 's':									\
		*va_arg(args, char**) =							\
			(pod == NULL || (SPA_POD_TYPE(pod) == SPA_TYPE_None)		\
				? NULL							\
				: (char *)SPA_POD_CONTENTS(struct spa_pod_string, pod));	\
		break;									\
	case 'S':									\
	{										\
		char *dest = va_arg(args, char*);					\
		uint32_t maxlen = va_arg(args, uint32_t);				\
		strncpy(dest, (char *)SPA_POD_CONTENTS(struct spa_pod_string, pod), maxlen-1);	\
		dest[maxlen-1] = '\0';							\
		break;									\
	}										\
	case 'y':									\
		*(va_arg(args, void **)) = SPA_POD_CONTENTS(struct spa_pod_bytes, pod);	\
		*(va_arg(args, uint32_t *)) = SPA_POD_BODY_SIZE(pod);			\
		break;									\
	case 'R':									\
		*va_arg(args, struct spa_rectangle*) =					\
				SPA_POD_VALUE(struct spa_pod_rectangle, pod);		\
		break;									\
	case 'F':									\
		*va_arg(args, struct spa_fraction*) =					\
				SPA_POD_VALUE(struct spa_pod_fraction, pod);		\
		break;									\
	case 'B':									\
		*va_arg(args, uint32_t **) =						\
			(uint32_t *) SPA_POD_CONTENTS(struct spa_pod_bitmap, pod);	\
		break;									\
	case 'a':									\
		*va_arg(args, uint32_t*) = SPA_POD_ARRAY_VALUE_SIZE(pod);		\
		*va_arg(args, uint32_t*) = SPA_POD_ARRAY_VALUE_TYPE(pod);		\
		*va_arg(args, uint32_t*) = SPA_POD_ARRAY_N_VALUES(pod);			\
		*va_arg(args, void**) = SPA_POD_ARRAY_VALUES(pod);			\
		break;									\
	case 'p':									\
	{										\
		struct spa_pod_pointer_body *b =					\
				(struct spa_pod_pointer_body *) SPA_POD_BODY(pod);	\
		*(va_arg(args, uint32_t *)) = b->type;					\
		*(va_arg(args, const void **)) = b->value;				\
		break;									\
	}										\
	case 'h':									\
		*va_arg(args, int64_t*) = SPA_POD_VALUE(struct spa_pod_fd, pod);	\
		break;									\
	case 'P':									\
	case 'T':									\
	case 'O':									\
	case 'V':									\
	{										\
		const struct spa_pod **d = va_arg(args, const struct spa_pod**);	\
		if (d)									\
			*d = (pod == NULL || (SPA_POD_TYPE(pod) == SPA_TYPE_None)	\
				? NULL : pod);						\
		break;									\
	}										\
	default:									\
		break;									\
	}										\
} while(false)

#define SPA_POD_PARSER_SKIP(_type,args)							\
do {											\
	switch (_type) {								\
	case 'S':									\
		va_arg(args, char*);							\
		va_arg(args, uint32_t);							\
		break;									\
	case 'a':									\
		va_arg(args, void*);							\
		va_arg(args, void*);							\
		SPA_FALLTHROUGH 							\
	case 'p':									\
	case 'y':									\
		va_arg(args, void*);							\
		SPA_FALLTHROUGH 							\
	case 'b':									\
	case 'I':									\
	case 'i':									\
	case 'l':									\
	case 'f':									\
	case 'd':									\
	case 's':									\
	case 'R':									\
	case 'F':									\
	case 'B':									\
	case 'h':									\
	case 'V':									\
	case 'P':									\
	case 'T':									\
	case 'O':									\
		va_arg(args, void*);							\
		break;									\
	}										\
} while(false)

static inline int spa_pod_parser_getv(struct spa_pod_parser *parser, va_list args)
{
	struct spa_pod_frame *f = parser->state.frame;
        uint32_t ftype = f ? f->pod.type : (uint32_t)SPA_TYPE_Struct;
	const struct spa_pod_prop *prop = NULL;
	int count = 0;

	do {
		bool optional;
		const struct spa_pod *pod = NULL;
		const char *format;

		if (ftype == SPA_TYPE_Object) {
			uint32_t key = va_arg(args, uint32_t);
			const struct spa_pod_object *object;

			if (key == 0)
				break;

			object = (const struct spa_pod_object *)spa_pod_parser_frame(parser, f);
			prop = spa_pod_object_find_prop(object, prop, key);
			pod = prop ? &prop->value : NULL;
		}

		if ((format = va_arg(args, char *)) == NULL)
			break;

		if (ftype == SPA_TYPE_Struct)
			pod = spa_pod_parser_next(parser);

		if ((optional = (*format == '?')))
			format++;

		if (!spa_pod_parser_can_collect(pod, *format)) {
			if (!optional) {
				if (pod == NULL)
					return -ESRCH;
				else
					return -EPROTO;
			}
			SPA_POD_PARSER_SKIP(*format, args);
		} else {
			if (pod->type == SPA_TYPE_Choice && *format != 'V' &&
			    SPA_POD_CHOICE_TYPE(pod) == SPA_CHOICE_None)
				pod = SPA_POD_CHOICE_CHILD(pod);

			SPA_POD_PARSER_COLLECT(pod, *format, args);
			count++;
		}
	} while (true);

	return count;
}

static inline int spa_pod_parser_get(struct spa_pod_parser *parser, ...)
{
	int res;
	va_list args;

	va_start(args, parser);
	res = spa_pod_parser_getv(parser, args);
	va_end(args);

	return res;
}

#define SPA_POD_OPT_Bool(val)				"?" SPA_POD_Bool(val)
#define SPA_POD_OPT_Id(val)				"?" SPA_POD_Id(val)
#define SPA_POD_OPT_Int(val)				"?" SPA_POD_Int(val)
#define SPA_POD_OPT_Long(val)				"?" SPA_POD_Long(val)
#define SPA_POD_OPT_Float(val)				"?" SPA_POD_Float(val)
#define SPA_POD_OPT_Double(val)				"?" SPA_POD_Double(val)
#define SPA_POD_OPT_String(val)				"?" SPA_POD_String(val)
#define SPA_POD_OPT_Stringn(val,len)			"?" SPA_POD_Stringn(val,len)
#define SPA_POD_OPT_Bytes(val,len)			"?" SPA_POD_Bytes(val,len)
#define SPA_POD_OPT_Rectangle(val)			"?" SPA_POD_Rectangle(val)
#define SPA_POD_OPT_Fraction(val)			"?" SPA_POD_Fraction(val)
#define SPA_POD_OPT_Array(csize,ctype,n_vals,vals)	"?" SPA_POD_Array(csize,ctype,n_vals,vals)
#define SPA_POD_OPT_Pointer(type,val)			"?" SPA_POD_Pointer(type,val)
#define SPA_POD_OPT_Fd(val)				"?" SPA_POD_Fd(val)
#define SPA_POD_OPT_Pod(val)				"?" SPA_POD_Pod(val)
#define SPA_POD_OPT_PodObject(val)			"?" SPA_POD_PodObject(val)
#define SPA_POD_OPT_PodStruct(val)			"?" SPA_POD_PodStruct(val)
#define SPA_POD_OPT_PodChoice(val)			"?" SPA_POD_PodChoice(val)

#define spa_pod_parser_get_object(p,type,id,...)				\
({										\
	struct spa_pod_frame _f;						\
	int _res;								\
	if ((_res = spa_pod_parser_push_object(p, &_f, type, id)) == 0) {	\
		_res = spa_pod_parser_get(p,##__VA_ARGS__, 0);			\
		spa_pod_parser_pop(p, &_f);					\
	}									\
	_res;									\
})

#define spa_pod_parser_get_struct(p,...)				\
({									\
	struct spa_pod_frame _f;					\
	int _res;							\
	if ((_res = spa_pod_parser_push_struct(p, &_f)) == 0) {		\
		_res = spa_pod_parser_get(p,##__VA_ARGS__, NULL);	\
		spa_pod_parser_pop(p, &_f);				\
	}								\
	_res;							\
})

#define spa_pod_parse_object(pod,type,id,...)			\
({								\
	struct spa_pod_parser _p;				\
	spa_pod_parser_pod(&_p, pod);				\
	spa_pod_parser_get_object(&_p,type,id,##__VA_ARGS__);	\
})

#define spa_pod_parse_struct(pod,...)				\
({								\
	struct spa_pod_parser _p;				\
	spa_pod_parser_pod(&_p, pod);				\
	spa_pod_parser_get_struct(&_p,##__VA_ARGS__);		\
})

/**
 * \}
 */

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

#endif /* SPA_POD_PARSER_H */