]>
code.delx.au - pulseaudio/blob - polyp/tagstruct.c
9578a9eb64db57f5d8d2cc958ac0795c6b23b2e4
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
28 #include <netinet/in.h>
31 #include "tagstruct.h"
42 TAG_SAMPLE_SPEC
= 'a',
48 size_t length
, allocated
;
54 struct pa_tagstruct
*pa_tagstruct_new(const uint8_t* data
, size_t length
) {
55 struct pa_tagstruct
*t
;
57 assert(!data
|| (data
&& length
));
59 t
= pa_xmalloc(sizeof(struct pa_tagstruct
));
60 t
->data
= (uint8_t*) data
;
61 t
->allocated
= t
->length
= data
? length
: 0;
67 void pa_tagstruct_free(struct pa_tagstruct
*t
) {
74 uint8_t* pa_tagstruct_free_data(struct pa_tagstruct
*t
, size_t *l
) {
76 assert(t
&& t
->dynamic
&& l
);
83 static void extend(struct pa_tagstruct
*t
, size_t l
) {
84 assert(t
&& t
->dynamic
);
86 if (t
->length
+l
<= t
->allocated
)
89 t
->data
= pa_xrealloc(t
->data
, t
->allocated
= t
->length
+l
+100);
92 void pa_tagstruct_puts(struct pa_tagstruct
*t
, const char *s
) {
97 t
->data
[t
->length
] = TAG_STRING
;
98 strcpy(t
->data
+t
->length
+1, s
);
102 void pa_tagstruct_putu32(struct pa_tagstruct
*t
, uint32_t i
) {
105 t
->data
[t
->length
] = TAG_U32
;
106 *((uint32_t*) (t
->data
+t
->length
+1)) = htonl(i
);
110 void pa_tagstruct_putu8(struct pa_tagstruct
*t
, uint8_t c
) {
113 t
->data
[t
->length
] = TAG_U8
;
114 *(t
->data
+t
->length
+1) = c
;
118 void pa_tagstruct_put_sample_spec(struct pa_tagstruct
*t
, const struct pa_sample_spec
*ss
) {
121 t
->data
[t
->length
] = TAG_SAMPLE_SPEC
;
122 t
->data
[t
->length
+1] = (uint8_t) ss
->format
;
123 t
->data
[t
->length
+2] = ss
->channels
;
124 *(uint32_t*) (t
->data
+t
->length
+3) = htonl(ss
->rate
);
129 void pa_tagstruct_put_arbitrary(struct pa_tagstruct
*t
, const void *p
, size_t length
) {
133 t
->data
[t
->length
] = TAG_ARBITRARY
;
134 *((uint32_t*) (t
->data
+t
->length
+1)) = htonl(length
);
136 memcpy(t
->data
+t
->length
+5, p
, length
);
137 t
->length
+= 5+length
;
140 int pa_tagstruct_gets(struct pa_tagstruct
*t
, const char **s
) {
146 if (t
->rindex
+2 > t
->length
)
149 if (t
->data
[t
->rindex
] != TAG_STRING
)
153 for (n
= 0, c
= (char*) (t
->data
+t
->rindex
+1); t
->rindex
+1+n
< t
->length
; n
++, c
++)
162 *s
= (char*) (t
->data
+t
->rindex
+1);
168 int pa_tagstruct_getu32(struct pa_tagstruct
*t
, uint32_t *i
) {
171 if (t
->rindex
+5 > t
->length
)
174 if (t
->data
[t
->rindex
] != TAG_U32
)
177 *i
= ntohl(*((uint32_t*) (t
->data
+t
->rindex
+1)));
182 int pa_tagstruct_getu8(struct pa_tagstruct
*t
, uint8_t *c
) {
185 if (t
->rindex
+2 > t
->length
)
188 if (t
->data
[t
->rindex
] != TAG_U8
)
191 *c
= t
->data
[t
->rindex
+1];
196 int pa_tagstruct_get_sample_spec(struct pa_tagstruct
*t
, struct pa_sample_spec
*ss
) {
199 if (t
->rindex
+7 > t
->length
)
202 if (t
->data
[t
->rindex
] != TAG_SAMPLE_SPEC
)
205 ss
->format
= t
->data
[t
->rindex
+1];
206 ss
->channels
= t
->data
[t
->rindex
+2];
207 ss
->rate
= ntohl(*(uint32_t*) (t
->data
+t
->rindex
+3));
213 int pa_tagstruct_get_arbitrary(struct pa_tagstruct
*t
, const void **p
, size_t length
) {
216 if (t
->rindex
+5+length
> t
->length
)
219 if (t
->data
[t
->rindex
] != TAG_ARBITRARY
)
222 if (ntohl(*((uint32_t*) (t
->data
+t
->rindex
+1))) != length
)
225 *p
= t
->data
+t
->rindex
+5;
226 t
->rindex
+= 5+length
;
230 int pa_tagstruct_eof(struct pa_tagstruct
*t
) {
232 return t
->rindex
>= t
->length
;
235 const uint8_t* pa_tagstruct_data(struct pa_tagstruct
*t
, size_t *l
) {
236 assert(t
&& t
->dynamic
&& l
);