2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/winsock.h>
39 #include <pulsecore/macro.h>
41 #include "tagstruct.h"
43 #define MAX_TAG_SIZE (64*1024)
47 size_t length, allocated;
53 pa_tagstruct *pa_tagstruct_new(const uint8_t* data, size_t length) {
56 pa_assert(!data || (data && length));
58 t = pa_xnew(pa_tagstruct, 1);
59 t->data = (uint8_t*) data;
60 t->allocated = t->length = data ? length : 0;
67 void pa_tagstruct_free(pa_tagstruct*t) {
75 uint8_t* pa_tagstruct_free_data(pa_tagstruct*t, size_t *l) {
79 pa_assert(t->dynamic);
88 static void extend(pa_tagstruct*t, size_t l) {
90 pa_assert(t->dynamic);
92 if (t->length+l <= t->allocated)
95 t->data = pa_xrealloc(t->data, t->allocated = t->length+l+100);
98 void pa_tagstruct_puts(pa_tagstruct*t, const char *s) {
105 t->data[t->length] = PA_TAG_STRING;
106 strcpy((char*) (t->data+t->length+1), s);
110 t->data[t->length] = PA_TAG_STRING_NULL;
115 void pa_tagstruct_putu32(pa_tagstruct*t, uint32_t i) {
119 t->data[t->length] = PA_TAG_U32;
121 memcpy(t->data+t->length+1, &i, 4);
125 void pa_tagstruct_putu8(pa_tagstruct*t, uint8_t c) {
129 t->data[t->length] = PA_TAG_U8;
130 *(t->data+t->length+1) = c;
134 void pa_tagstruct_put_sample_spec(pa_tagstruct *t, const pa_sample_spec *ss) {
141 t->data[t->length] = PA_TAG_SAMPLE_SPEC;
142 t->data[t->length+1] = (uint8_t) ss->format;
143 t->data[t->length+2] = ss->channels;
144 rate = htonl(ss->rate);
145 memcpy(t->data+t->length+3, &rate, 4);
149 void pa_tagstruct_put_arbitrary(pa_tagstruct *t, const void *p, size_t length) {
156 t->data[t->length] = PA_TAG_ARBITRARY;
158 memcpy(t->data+t->length+1, &tmp, 4);
160 memcpy(t->data+t->length+5, p, length);
161 t->length += 5+length;
164 void pa_tagstruct_put_boolean(pa_tagstruct*t, pa_bool_t b) {
168 t->data[t->length] = b ? PA_TAG_BOOLEAN_TRUE : PA_TAG_BOOLEAN_FALSE;
172 void pa_tagstruct_put_timeval(pa_tagstruct*t, const struct timeval *tv) {
177 t->data[t->length] = PA_TAG_TIMEVAL;
178 tmp = htonl(tv->tv_sec);
179 memcpy(t->data+t->length+1, &tmp, 4);
180 tmp = htonl(tv->tv_usec);
181 memcpy(t->data+t->length+5, &tmp, 4);
185 void pa_tagstruct_put_usec(pa_tagstruct*t, pa_usec_t u) {
191 t->data[t->length] = PA_TAG_USEC;
192 tmp = htonl((uint32_t) (u >> 32));
193 memcpy(t->data+t->length+1, &tmp, 4);
194 tmp = htonl((uint32_t) u);
195 memcpy(t->data+t->length+5, &tmp, 4);
199 void pa_tagstruct_putu64(pa_tagstruct*t, uint64_t u) {
205 t->data[t->length] = PA_TAG_U64;
206 tmp = htonl((uint32_t) (u >> 32));
207 memcpy(t->data+t->length+1, &tmp, 4);
208 tmp = htonl((uint32_t) u);
209 memcpy(t->data+t->length+5, &tmp, 4);
213 void pa_tagstruct_puts64(pa_tagstruct*t, int64_t u) {
219 t->data[t->length] = PA_TAG_S64;
220 tmp = htonl((uint32_t) ((uint64_t) u >> 32));
221 memcpy(t->data+t->length+1, &tmp, 4);
222 tmp = htonl((uint32_t) ((uint64_t) u));
223 memcpy(t->data+t->length+5, &tmp, 4);
227 void pa_tagstruct_put_channel_map(pa_tagstruct *t, const pa_channel_map *map) {
231 extend(t, 2 + map->channels);
233 t->data[t->length++] = PA_TAG_CHANNEL_MAP;
234 t->data[t->length++] = map->channels;
236 for (i = 0; i < map->channels; i ++)
237 t->data[t->length++] = (uint8_t) map->map[i];
240 void pa_tagstruct_put_cvolume(pa_tagstruct *t, const pa_cvolume *cvolume) {
245 extend(t, 2 + cvolume->channels * sizeof(pa_volume_t));
247 t->data[t->length++] = PA_TAG_CVOLUME;
248 t->data[t->length++] = cvolume->channels;
250 for (i = 0; i < cvolume->channels; i ++) {
251 vol = htonl(cvolume->values[i]);
252 memcpy(t->data + t->length, &vol, sizeof(pa_volume_t));
253 t->length += sizeof(pa_volume_t);
257 void pa_tagstruct_put_proplist(pa_tagstruct *t, pa_proplist *p) {
264 t->data[t->length++] = PA_TAG_PROPLIST;
271 if (!(k = pa_proplist_iterate(p, &state)))
274 pa_tagstruct_puts(t, k);
275 pa_assert_se(pa_proplist_get(p, k, &d, &l) >= 0);
276 pa_tagstruct_putu32(t, (uint32_t) l);
277 pa_tagstruct_put_arbitrary(t, d, l);
280 pa_tagstruct_puts(t, NULL);
283 int pa_tagstruct_gets(pa_tagstruct*t, const char **s) {
291 if (t->rindex+1 > t->length)
294 if (t->data[t->rindex] == PA_TAG_STRING_NULL) {
300 if (t->rindex+2 > t->length)
303 if (t->data[t->rindex] != PA_TAG_STRING)
307 for (n = 0, c = (char*) (t->data+t->rindex+1); t->rindex+1+n < t->length; n++, c++)
316 *s = (char*) (t->data+t->rindex+1);
322 int pa_tagstruct_getu32(pa_tagstruct*t, uint32_t *i) {
326 if (t->rindex+5 > t->length)
329 if (t->data[t->rindex] != PA_TAG_U32)
332 memcpy(i, t->data+t->rindex+1, 4);
338 int pa_tagstruct_getu8(pa_tagstruct*t, uint8_t *c) {
342 if (t->rindex+2 > t->length)
345 if (t->data[t->rindex] != PA_TAG_U8)
348 *c = t->data[t->rindex+1];
353 int pa_tagstruct_get_sample_spec(pa_tagstruct *t, pa_sample_spec *ss) {
357 if (t->rindex+7 > t->length)
360 if (t->data[t->rindex] != PA_TAG_SAMPLE_SPEC)
363 ss->format = t->data[t->rindex+1];
364 ss->channels = t->data[t->rindex+2];
365 memcpy(&ss->rate, t->data+t->rindex+3, 4);
366 ss->rate = ntohl(ss->rate);
372 int pa_tagstruct_get_arbitrary(pa_tagstruct *t, const void **p, size_t length) {
378 if (t->rindex+5+length > t->length)
381 if (t->data[t->rindex] != PA_TAG_ARBITRARY)
384 memcpy(&len, t->data+t->rindex+1, 4);
385 if (ntohl(len) != length)
388 *p = t->data+t->rindex+5;
389 t->rindex += 5+length;
393 int pa_tagstruct_eof(pa_tagstruct*t) {
396 return t->rindex >= t->length;
399 const uint8_t* pa_tagstruct_data(pa_tagstruct*t, size_t *l) {
401 pa_assert(t->dynamic);
408 int pa_tagstruct_get_boolean(pa_tagstruct*t, pa_bool_t *b) {
412 if (t->rindex+1 > t->length)
415 if (t->data[t->rindex] == PA_TAG_BOOLEAN_TRUE)
417 else if (t->data[t->rindex] == PA_TAG_BOOLEAN_FALSE)
426 int pa_tagstruct_get_timeval(pa_tagstruct*t, struct timeval *tv) {
431 if (t->rindex+9 > t->length)
434 if (t->data[t->rindex] != PA_TAG_TIMEVAL)
437 memcpy(&tv->tv_sec, t->data+t->rindex+1, 4);
438 tv->tv_sec = ntohl(tv->tv_sec);
439 memcpy(&tv->tv_usec, t->data+t->rindex+5, 4);
440 tv->tv_usec = ntohl(tv->tv_usec);
445 int pa_tagstruct_get_usec(pa_tagstruct*t, pa_usec_t *u) {
451 if (t->rindex+9 > t->length)
454 if (t->data[t->rindex] != PA_TAG_USEC)
457 memcpy(&tmp, t->data+t->rindex+1, 4);
458 *u = (pa_usec_t) ntohl(tmp) << 32;
459 memcpy(&tmp, t->data+t->rindex+5, 4);
460 *u |= (pa_usec_t) ntohl(tmp);
465 int pa_tagstruct_getu64(pa_tagstruct*t, uint64_t *u) {
471 if (t->rindex+9 > t->length)
474 if (t->data[t->rindex] != PA_TAG_U64)
477 memcpy(&tmp, t->data+t->rindex+1, 4);
478 *u = (uint64_t) ntohl(tmp) << 32;
479 memcpy(&tmp, t->data+t->rindex+5, 4);
480 *u |= (uint64_t) ntohl(tmp);
485 int pa_tagstruct_gets64(pa_tagstruct*t, int64_t *u) {
491 if (t->rindex+9 > t->length)
494 if (t->data[t->rindex] != PA_TAG_S64)
497 memcpy(&tmp, t->data+t->rindex+1, 4);
498 *u = (int64_t) ((uint64_t) ntohl(tmp) << 32);
499 memcpy(&tmp, t->data+t->rindex+5, 4);
500 *u |= (int64_t) ntohl(tmp);
505 int pa_tagstruct_get_channel_map(pa_tagstruct *t, pa_channel_map *map) {
511 if (t->rindex+2 > t->length)
514 if (t->data[t->rindex] != PA_TAG_CHANNEL_MAP)
517 if ((map->channels = t->data[t->rindex+1]) > PA_CHANNELS_MAX)
520 if (t->rindex+2+map->channels > t->length)
523 for (i = 0; i < map->channels; i ++)
524 map->map[i] = (int8_t) t->data[t->rindex + 2 + i];
526 t->rindex += 2 + map->channels;
530 int pa_tagstruct_get_cvolume(pa_tagstruct *t, pa_cvolume *cvolume) {
537 if (t->rindex+2 > t->length)
540 if (t->data[t->rindex] != PA_TAG_CVOLUME)
543 if ((cvolume->channels = t->data[t->rindex+1]) > PA_CHANNELS_MAX)
546 if (t->rindex+2+cvolume->channels*sizeof(pa_volume_t) > t->length)
549 for (i = 0; i < cvolume->channels; i ++) {
550 memcpy(&vol, t->data + t->rindex + 2 + i * sizeof(pa_volume_t), sizeof(pa_volume_t));
551 cvolume->values[i] = (pa_volume_t) ntohl(vol);
554 t->rindex += 2 + cvolume->channels * sizeof(pa_volume_t);
558 int pa_tagstruct_get_proplist(pa_tagstruct *t, pa_proplist *p) {
564 if (t->rindex+1 > t->length)
567 if (t->data[t->rindex] != PA_TAG_PROPLIST)
570 saved_rindex = t->rindex;
578 if (pa_tagstruct_gets(t, &k) < 0)
584 if (pa_tagstruct_getu32(t, &length) < 0)
587 if (length > MAX_TAG_SIZE)
590 if (pa_tagstruct_get_arbitrary(t, &d, length) < 0)
593 if (pa_proplist_set(p, k, d, length) < 0)
600 t->rindex = saved_rindex;
604 void pa_tagstruct_put(pa_tagstruct *t, ...) {
611 int tag = va_arg(va, int);
613 if (tag == PA_TAG_INVALID)
618 case PA_TAG_STRING_NULL:
619 pa_tagstruct_puts(t, va_arg(va, char*));
623 pa_tagstruct_putu32(t, va_arg(va, uint32_t));
627 pa_tagstruct_putu8(t, (uint8_t) va_arg(va, int));
631 pa_tagstruct_putu64(t, va_arg(va, uint64_t));
634 case PA_TAG_SAMPLE_SPEC:
635 pa_tagstruct_put_sample_spec(t, va_arg(va, pa_sample_spec*));
638 case PA_TAG_ARBITRARY: {
639 void *p = va_arg(va, void*);
640 size_t size = va_arg(va, size_t);
641 pa_tagstruct_put_arbitrary(t, p, size);
645 case PA_TAG_BOOLEAN_TRUE:
646 case PA_TAG_BOOLEAN_FALSE:
647 pa_tagstruct_put_boolean(t, va_arg(va, int));
651 pa_tagstruct_put_timeval(t, va_arg(va, struct timeval*));
655 pa_tagstruct_put_usec(t, va_arg(va, pa_usec_t));
658 case PA_TAG_CHANNEL_MAP:
659 pa_tagstruct_put_channel_map(t, va_arg(va, pa_channel_map *));
663 pa_tagstruct_put_cvolume(t, va_arg(va, pa_cvolume *));
666 case PA_TAG_PROPLIST:
667 pa_tagstruct_put_proplist(t, va_arg(va, pa_proplist *));
671 pa_assert_not_reached();
678 int pa_tagstruct_get(pa_tagstruct *t, ...) {
686 int tag = va_arg(va, int);
688 if (tag == PA_TAG_INVALID)
693 case PA_TAG_STRING_NULL:
694 ret = pa_tagstruct_gets(t, va_arg(va, const char**));
698 ret = pa_tagstruct_getu32(t, va_arg(va, uint32_t*));
702 ret = pa_tagstruct_getu8(t, va_arg(va, uint8_t*));
706 ret = pa_tagstruct_getu64(t, va_arg(va, uint64_t*));
709 case PA_TAG_SAMPLE_SPEC:
710 ret = pa_tagstruct_get_sample_spec(t, va_arg(va, pa_sample_spec*));
713 case PA_TAG_ARBITRARY: {
714 const void **p = va_arg(va, const void**);
715 size_t size = va_arg(va, size_t);
716 ret = pa_tagstruct_get_arbitrary(t, p, size);
720 case PA_TAG_BOOLEAN_TRUE:
721 case PA_TAG_BOOLEAN_FALSE:
722 ret = pa_tagstruct_get_boolean(t, va_arg(va, pa_bool_t*));
726 ret = pa_tagstruct_get_timeval(t, va_arg(va, struct timeval*));
730 ret = pa_tagstruct_get_usec(t, va_arg(va, pa_usec_t*));
733 case PA_TAG_CHANNEL_MAP:
734 ret = pa_tagstruct_get_channel_map(t, va_arg(va, pa_channel_map *));
738 ret = pa_tagstruct_get_cvolume(t, va_arg(va, pa_cvolume *));
741 case PA_TAG_PROPLIST:
742 ret = pa_tagstruct_get_proplist(t, va_arg(va, pa_proplist *));
746 pa_assert_not_reached();