]>
code.delx.au - pulseaudio/blob - polyp/tagstruct.c
b9ab55f0cefbce360517783c371afb61445d3ca4
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio 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 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <netinet/in.h>
33 #include "tagstruct.h"
38 TAG_NULL_STRING
= 'N',
47 TAG_SAMPLE_SPEC
= 'a',
49 TAG_BOOLEAN_TRUE
= '1',
50 TAG_BOOLEAN_FALSE
= '0',
52 TAG_USEC
= 'U' /* 64bit unsigned */
57 size_t length
, allocated
;
63 struct pa_tagstruct
*pa_tagstruct_new(const uint8_t* data
, size_t length
) {
64 struct pa_tagstruct
*t
;
66 assert(!data
|| (data
&& length
));
68 t
= pa_xmalloc(sizeof(struct pa_tagstruct
));
69 t
->data
= (uint8_t*) data
;
70 t
->allocated
= t
->length
= data
? length
: 0;
76 void pa_tagstruct_free(struct pa_tagstruct
*t
) {
83 uint8_t* pa_tagstruct_free_data(struct pa_tagstruct
*t
, size_t *l
) {
85 assert(t
&& t
->dynamic
&& l
);
92 static void extend(struct pa_tagstruct
*t
, size_t l
) {
93 assert(t
&& t
->dynamic
);
95 if (t
->length
+l
<= t
->allocated
)
98 t
->data
= pa_xrealloc(t
->data
, t
->allocated
= t
->length
+l
+100);
101 void pa_tagstruct_puts(struct pa_tagstruct
*t
, const char *s
) {
107 t
->data
[t
->length
] = TAG_STRING
;
108 strcpy((char*) (t
->data
+t
->length
+1), s
);
112 t
->data
[t
->length
] = TAG_NULL_STRING
;
117 void pa_tagstruct_putu32(struct pa_tagstruct
*t
, uint32_t i
) {
120 t
->data
[t
->length
] = TAG_U32
;
121 *((uint32_t*) (t
->data
+t
->length
+1)) = htonl(i
);
125 void pa_tagstruct_putu8(struct pa_tagstruct
*t
, uint8_t c
) {
128 t
->data
[t
->length
] = TAG_U8
;
129 *(t
->data
+t
->length
+1) = c
;
133 void pa_tagstruct_put_sample_spec(struct pa_tagstruct
*t
, const struct pa_sample_spec
*ss
) {
136 t
->data
[t
->length
] = TAG_SAMPLE_SPEC
;
137 t
->data
[t
->length
+1] = (uint8_t) ss
->format
;
138 t
->data
[t
->length
+2] = ss
->channels
;
139 *(uint32_t*) (t
->data
+t
->length
+3) = htonl(ss
->rate
);
143 void pa_tagstruct_put_arbitrary(struct pa_tagstruct
*t
, const void *p
, size_t length
) {
147 t
->data
[t
->length
] = TAG_ARBITRARY
;
148 *((uint32_t*) (t
->data
+t
->length
+1)) = htonl(length
);
150 memcpy(t
->data
+t
->length
+5, p
, length
);
151 t
->length
+= 5+length
;
154 void pa_tagstruct_put_boolean(struct pa_tagstruct
*t
, int b
) {
157 t
->data
[t
->length
] = b
? TAG_BOOLEAN_TRUE
: TAG_BOOLEAN_FALSE
;
161 void pa_tagstruct_put_timeval(struct pa_tagstruct
*t
, const struct timeval
*tv
) {
164 t
->data
[t
->length
] = TAG_TIMEVAL
;
165 *((uint32_t*) (t
->data
+t
->length
+1)) = htonl(tv
->tv_sec
);
166 *((uint32_t*) (t
->data
+t
->length
+5)) = htonl(tv
->tv_usec
);
170 void pa_tagstruct_put_usec(struct pa_tagstruct
*t
, pa_usec_t u
) {
173 t
->data
[t
->length
] = TAG_USEC
;
174 *((uint32_t*) (t
->data
+t
->length
+1)) = htonl((uint32_t) (u
>> 32));
175 *((uint32_t*) (t
->data
+t
->length
+5)) = htonl((uint32_t) u
);
179 void pa_tagstruct_putu64(struct pa_tagstruct
*t
, uint64_t u
) {
182 t
->data
[t
->length
] = TAG_U64
;
183 *((uint32_t*) (t
->data
+t
->length
+1)) = htonl((uint32_t) (u
>> 32));
184 *((uint32_t*) (t
->data
+t
->length
+5)) = htonl((uint32_t) u
);
188 int pa_tagstruct_gets(struct pa_tagstruct
*t
, const char **s
) {
194 if (t
->rindex
+1 > t
->length
)
197 if (t
->data
[t
->rindex
] == TAG_NULL_STRING
) {
203 if (t
->rindex
+2 > t
->length
)
206 if (t
->data
[t
->rindex
] != TAG_STRING
)
210 for (n
= 0, c
= (char*) (t
->data
+t
->rindex
+1); t
->rindex
+1+n
< t
->length
; n
++, c
++)
219 *s
= (char*) (t
->data
+t
->rindex
+1);
225 int pa_tagstruct_getu32(struct pa_tagstruct
*t
, uint32_t *i
) {
228 if (t
->rindex
+5 > t
->length
)
231 if (t
->data
[t
->rindex
] != TAG_U32
)
234 *i
= ntohl(*((uint32_t*) (t
->data
+t
->rindex
+1)));
239 int pa_tagstruct_getu8(struct pa_tagstruct
*t
, uint8_t *c
) {
242 if (t
->rindex
+2 > t
->length
)
245 if (t
->data
[t
->rindex
] != TAG_U8
)
248 *c
= t
->data
[t
->rindex
+1];
253 int pa_tagstruct_get_sample_spec(struct pa_tagstruct
*t
, struct pa_sample_spec
*ss
) {
256 if (t
->rindex
+7 > t
->length
)
259 if (t
->data
[t
->rindex
] != TAG_SAMPLE_SPEC
)
262 ss
->format
= t
->data
[t
->rindex
+1];
263 ss
->channels
= t
->data
[t
->rindex
+2];
264 ss
->rate
= ntohl(*(uint32_t*) (t
->data
+t
->rindex
+3));
270 int pa_tagstruct_get_arbitrary(struct pa_tagstruct
*t
, const void **p
, size_t length
) {
273 if (t
->rindex
+5+length
> t
->length
)
276 if (t
->data
[t
->rindex
] != TAG_ARBITRARY
)
279 if (ntohl(*((uint32_t*) (t
->data
+t
->rindex
+1))) != length
)
282 *p
= t
->data
+t
->rindex
+5;
283 t
->rindex
+= 5+length
;
287 int pa_tagstruct_eof(struct pa_tagstruct
*t
) {
289 return t
->rindex
>= t
->length
;
292 const uint8_t* pa_tagstruct_data(struct pa_tagstruct
*t
, size_t *l
) {
293 assert(t
&& t
->dynamic
&& l
);
298 int pa_tagstruct_get_boolean(struct pa_tagstruct
*t
, int *b
) {
301 if (t
->rindex
+1 > t
->length
)
304 if (t
->data
[t
->rindex
] == TAG_BOOLEAN_TRUE
)
306 else if (t
->data
[t
->rindex
] == TAG_BOOLEAN_FALSE
)
315 int pa_tagstruct_get_timeval(struct pa_tagstruct
*t
, struct timeval
*tv
) {
317 if (t
->rindex
+9 > t
->length
)
320 if (t
->data
[t
->rindex
] != TAG_TIMEVAL
)
323 tv
->tv_sec
= ntohl(*((uint32_t*) (t
->data
+t
->rindex
+1)));
324 tv
->tv_usec
= ntohl(*((uint32_t*) (t
->data
+t
->rindex
+5)));
330 int pa_tagstruct_get_usec(struct pa_tagstruct
*t
, pa_usec_t
*u
) {
333 if (t
->rindex
+9 > t
->length
)
336 if (t
->data
[t
->rindex
] != TAG_USEC
)
339 *u
= (pa_usec_t
) ntohl(*((uint32_t*) (t
->data
+t
->rindex
+1))) << 32;
340 *u
|= (pa_usec_t
) ntohl(*((uint32_t*) (t
->data
+t
->rindex
+5)));
345 int pa_tagstruct_getu64(struct pa_tagstruct
*t
, uint64_t *u
) {
348 if (t
->rindex
+9 > t
->length
)
351 if (t
->data
[t
->rindex
] != TAG_U64
)
354 *u
= (uint64_t) ntohl(*((uint32_t*) (t
->data
+t
->rindex
+1))) << 32;
355 *u
|= (uint64_t) ntohl(*((uint32_t*) (t
->data
+t
->rindex
+5)));