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/socket.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;
157 tmp = htonl((uint32_t) length);
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] = (uint8_t) (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((uint32_t) tv->tv_sec);
179 memcpy(t->data+t->length+1, &tmp, 4);
180 tmp = htonl((uint32_t) 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 + (size_t) 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_volume(pa_tagstruct *t, pa_volume_t vol) {
262 t->data[t->length] = PA_TAG_VOLUME;
263 u = htonl((uint32_t) vol);
264 memcpy(t->data+t->length+1, &u, 4);
268 void pa_tagstruct_put_proplist(pa_tagstruct *t, pa_proplist *p) {
275 t->data[t->length++] = PA_TAG_PROPLIST;
282 if (!(k = pa_proplist_iterate(p, &state)))
285 pa_tagstruct_puts(t, k);
286 pa_assert_se(pa_proplist_get(p, k, &d, &l) >= 0);
287 pa_tagstruct_putu32(t, (uint32_t) l);
288 pa_tagstruct_put_arbitrary(t, d, l);
291 pa_tagstruct_puts(t, NULL);
294 void pa_tagstruct_put_format_info(pa_tagstruct *t, pa_format_info *f) {
300 t->data[t->length++] = PA_TAG_FORMAT_INFO;
301 pa_tagstruct_putu8(t, (uint8_t) f->encoding);
302 pa_tagstruct_put_proplist(t, f->plist);
305 int pa_tagstruct_gets(pa_tagstruct*t, const char **s) {
313 if (t->rindex+1 > t->length)
316 if (t->data[t->rindex] == PA_TAG_STRING_NULL) {
322 if (t->rindex+2 > t->length)
325 if (t->data[t->rindex] != PA_TAG_STRING)
329 for (n = 0, c = (char*) (t->data+t->rindex+1); t->rindex+1+n < t->length; n++, c++)
338 *s = (char*) (t->data+t->rindex+1);
344 int pa_tagstruct_getu32(pa_tagstruct*t, uint32_t *i) {
348 if (t->rindex+5 > t->length)
351 if (t->data[t->rindex] != PA_TAG_U32)
354 memcpy(i, t->data+t->rindex+1, 4);
360 int pa_tagstruct_getu8(pa_tagstruct*t, uint8_t *c) {
364 if (t->rindex+2 > t->length)
367 if (t->data[t->rindex] != PA_TAG_U8)
370 *c = t->data[t->rindex+1];
375 int pa_tagstruct_get_sample_spec(pa_tagstruct *t, pa_sample_spec *ss) {
379 if (t->rindex+7 > t->length)
382 if (t->data[t->rindex] != PA_TAG_SAMPLE_SPEC)
385 ss->format = t->data[t->rindex+1];
386 ss->channels = t->data[t->rindex+2];
387 memcpy(&ss->rate, t->data+t->rindex+3, 4);
388 ss->rate = ntohl(ss->rate);
394 int pa_tagstruct_get_arbitrary(pa_tagstruct *t, const void **p, size_t length) {
400 if (t->rindex+5+length > t->length)
403 if (t->data[t->rindex] != PA_TAG_ARBITRARY)
406 memcpy(&len, t->data+t->rindex+1, 4);
407 if (ntohl(len) != length)
410 *p = t->data+t->rindex+5;
411 t->rindex += 5+length;
415 int pa_tagstruct_eof(pa_tagstruct*t) {
418 return t->rindex >= t->length;
421 const uint8_t* pa_tagstruct_data(pa_tagstruct*t, size_t *l) {
423 pa_assert(t->dynamic);
430 int pa_tagstruct_get_boolean(pa_tagstruct*t, pa_bool_t *b) {
434 if (t->rindex+1 > t->length)
437 if (t->data[t->rindex] == PA_TAG_BOOLEAN_TRUE)
439 else if (t->data[t->rindex] == PA_TAG_BOOLEAN_FALSE)
448 int pa_tagstruct_get_timeval(pa_tagstruct*t, struct timeval *tv) {
453 if (t->rindex+9 > t->length)
456 if (t->data[t->rindex] != PA_TAG_TIMEVAL)
459 memcpy(&tv->tv_sec, t->data+t->rindex+1, 4);
460 tv->tv_sec = (time_t) ntohl((uint32_t) tv->tv_sec);
461 memcpy(&tv->tv_usec, t->data+t->rindex+5, 4);
462 tv->tv_usec = (suseconds_t) ntohl((uint32_t) tv->tv_usec);
467 int pa_tagstruct_get_usec(pa_tagstruct*t, pa_usec_t *u) {
473 if (t->rindex+9 > t->length)
476 if (t->data[t->rindex] != PA_TAG_USEC)
479 memcpy(&tmp, t->data+t->rindex+1, 4);
480 *u = (pa_usec_t) ntohl(tmp) << 32;
481 memcpy(&tmp, t->data+t->rindex+5, 4);
482 *u |= (pa_usec_t) ntohl(tmp);
487 int pa_tagstruct_getu64(pa_tagstruct*t, uint64_t *u) {
493 if (t->rindex+9 > t->length)
496 if (t->data[t->rindex] != PA_TAG_U64)
499 memcpy(&tmp, t->data+t->rindex+1, 4);
500 *u = (uint64_t) ntohl(tmp) << 32;
501 memcpy(&tmp, t->data+t->rindex+5, 4);
502 *u |= (uint64_t) ntohl(tmp);
507 int pa_tagstruct_gets64(pa_tagstruct*t, int64_t *u) {
513 if (t->rindex+9 > t->length)
516 if (t->data[t->rindex] != PA_TAG_S64)
519 memcpy(&tmp, t->data+t->rindex+1, 4);
520 *u = (int64_t) ((uint64_t) ntohl(tmp) << 32);
521 memcpy(&tmp, t->data+t->rindex+5, 4);
522 *u |= (int64_t) ntohl(tmp);
527 int pa_tagstruct_get_channel_map(pa_tagstruct *t, pa_channel_map *map) {
533 if (t->rindex+2 > t->length)
536 if (t->data[t->rindex] != PA_TAG_CHANNEL_MAP)
539 if ((map->channels = t->data[t->rindex+1]) > PA_CHANNELS_MAX)
542 if (t->rindex+2+map->channels > t->length)
545 for (i = 0; i < map->channels; i ++)
546 map->map[i] = (int8_t) t->data[t->rindex + 2 + i];
548 t->rindex += 2 + (size_t) map->channels;
552 int pa_tagstruct_get_cvolume(pa_tagstruct *t, pa_cvolume *cvolume) {
559 if (t->rindex+2 > t->length)
562 if (t->data[t->rindex] != PA_TAG_CVOLUME)
565 if ((cvolume->channels = t->data[t->rindex+1]) > PA_CHANNELS_MAX)
568 if (t->rindex+2+cvolume->channels*sizeof(pa_volume_t) > t->length)
571 for (i = 0; i < cvolume->channels; i ++) {
572 memcpy(&vol, t->data + t->rindex + 2 + i * sizeof(pa_volume_t), sizeof(pa_volume_t));
573 cvolume->values[i] = (pa_volume_t) ntohl(vol);
576 t->rindex += 2 + cvolume->channels * sizeof(pa_volume_t);
580 int pa_tagstruct_get_volume(pa_tagstruct*t, pa_volume_t *vol) {
586 if (t->rindex+5 > t->length)
589 if (t->data[t->rindex] != PA_TAG_VOLUME)
592 memcpy(&u, t->data+t->rindex+1, 4);
593 *vol = (pa_volume_t) ntohl(u);
599 int pa_tagstruct_get_proplist(pa_tagstruct *t, pa_proplist *p) {
605 if (t->rindex+1 > t->length)
608 if (t->data[t->rindex] != PA_TAG_PROPLIST)
611 saved_rindex = t->rindex;
619 if (pa_tagstruct_gets(t, &k) < 0)
625 if (pa_tagstruct_getu32(t, &length) < 0)
628 if (length > MAX_TAG_SIZE)
631 if (pa_tagstruct_get_arbitrary(t, &d, length) < 0)
634 if (pa_proplist_set(p, k, d, length) < 0)
641 t->rindex = saved_rindex;
645 int pa_tagstruct_get_format_info(pa_tagstruct *t, pa_format_info *f) {
652 if (t->rindex+1 > t->length)
655 if (t->data[t->rindex] != PA_TAG_FORMAT_INFO)
658 saved_rindex = t->rindex;
661 if (pa_tagstruct_getu8(t, &encoding) < 0)
664 f->encoding = encoding;
666 if (pa_tagstruct_get_proplist(t, f->plist) < 0)
672 t->rindex = saved_rindex;
676 void pa_tagstruct_put(pa_tagstruct *t, ...) {
683 int tag = va_arg(va, int);
685 if (tag == PA_TAG_INVALID)
690 case PA_TAG_STRING_NULL:
691 pa_tagstruct_puts(t, va_arg(va, char*));
695 pa_tagstruct_putu32(t, va_arg(va, uint32_t));
699 pa_tagstruct_putu8(t, (uint8_t) va_arg(va, int));
703 pa_tagstruct_putu64(t, va_arg(va, uint64_t));
706 case PA_TAG_SAMPLE_SPEC:
707 pa_tagstruct_put_sample_spec(t, va_arg(va, pa_sample_spec*));
710 case PA_TAG_ARBITRARY: {
711 void *p = va_arg(va, void*);
712 size_t size = va_arg(va, size_t);
713 pa_tagstruct_put_arbitrary(t, p, size);
717 case PA_TAG_BOOLEAN_TRUE:
718 case PA_TAG_BOOLEAN_FALSE:
719 pa_tagstruct_put_boolean(t, va_arg(va, int));
723 pa_tagstruct_put_timeval(t, va_arg(va, struct timeval*));
727 pa_tagstruct_put_usec(t, va_arg(va, pa_usec_t));
730 case PA_TAG_CHANNEL_MAP:
731 pa_tagstruct_put_channel_map(t, va_arg(va, pa_channel_map *));
735 pa_tagstruct_put_cvolume(t, va_arg(va, pa_cvolume *));
739 pa_tagstruct_put_volume(t, va_arg(va, pa_volume_t));
742 case PA_TAG_PROPLIST:
743 pa_tagstruct_put_proplist(t, va_arg(va, pa_proplist *));
747 pa_assert_not_reached();
754 int pa_tagstruct_get(pa_tagstruct *t, ...) {
762 int tag = va_arg(va, int);
764 if (tag == PA_TAG_INVALID)
769 case PA_TAG_STRING_NULL:
770 ret = pa_tagstruct_gets(t, va_arg(va, const char**));
774 ret = pa_tagstruct_getu32(t, va_arg(va, uint32_t*));
778 ret = pa_tagstruct_getu8(t, va_arg(va, uint8_t*));
782 ret = pa_tagstruct_getu64(t, va_arg(va, uint64_t*));
785 case PA_TAG_SAMPLE_SPEC:
786 ret = pa_tagstruct_get_sample_spec(t, va_arg(va, pa_sample_spec*));
789 case PA_TAG_ARBITRARY: {
790 const void **p = va_arg(va, const void**);
791 size_t size = va_arg(va, size_t);
792 ret = pa_tagstruct_get_arbitrary(t, p, size);
796 case PA_TAG_BOOLEAN_TRUE:
797 case PA_TAG_BOOLEAN_FALSE:
798 ret = pa_tagstruct_get_boolean(t, va_arg(va, pa_bool_t*));
802 ret = pa_tagstruct_get_timeval(t, va_arg(va, struct timeval*));
806 ret = pa_tagstruct_get_usec(t, va_arg(va, pa_usec_t*));
809 case PA_TAG_CHANNEL_MAP:
810 ret = pa_tagstruct_get_channel_map(t, va_arg(va, pa_channel_map *));
814 ret = pa_tagstruct_get_cvolume(t, va_arg(va, pa_cvolume *));
818 ret = pa_tagstruct_get_volume(t, va_arg(va, pa_volume_t *));
821 case PA_TAG_PROPLIST:
822 ret = pa_tagstruct_get_proplist(t, va_arg(va, pa_proplist *));
826 pa_assert_not_reached();