]>
code.delx.au - pulseaudio/blob - src/tagstruct.c
429dd40885a61682e65b0b8c6302086839c4e5fe
3 #include <netinet/in.h>
21 size_t length
, allocated
;
27 struct tagstruct
*tagstruct_new(const uint8_t* data
, size_t length
) {
30 assert(!data
|| (data
&& length
));
32 t
= malloc(sizeof(struct tagstruct
));
34 t
->data
= (uint8_t*) data
;
35 t
->allocated
= t
->length
= data
? length
: 0;
41 void tagstruct_free(struct tagstruct
*t
) {
48 uint8_t* tagstruct_free_data(struct tagstruct
*t
, size_t *l
) {
50 assert(t
&& t
->dynamic
&& l
);
57 static void extend(struct tagstruct
*t
, size_t l
) {
58 assert(t
&& t
->dynamic
);
60 if (t
->allocated
<= l
)
63 t
->data
= realloc(t
->data
, t
->allocated
= l
+100);
67 void tagstruct_puts(struct tagstruct
*t
, const char *s
) {
72 t
->data
[t
->length
] = TAG_STRING
;
73 strcpy(t
->data
+t
->length
+1, s
);
77 void tagstruct_putu32(struct tagstruct
*t
, uint32_t i
) {
80 t
->data
[t
->length
] = TAG_U32
;
81 *((uint32_t*) (t
->data
+t
->length
+1)) = htonl(i
);
85 void tagstruct_putu8(struct tagstruct
*t
, uint8_t c
) {
88 t
->data
[t
->length
] = TAG_U8
;
89 *(t
->data
+t
->length
+1) = c
;
93 void tagstruct_put_sample_spec(struct tagstruct
*t
, struct sample_spec
*ss
) {
96 t
->data
[t
->length
] = TAG_SAMPLE_SPEC
;
97 t
->data
[t
->length
+1] = (uint8_t) ss
->format
;
98 t
->data
[t
->length
+2] = ss
->channels
;
99 *(uint32_t*) (t
->data
+t
->length
+3) = htonl(ss
->rate
);
103 int tagstruct_gets(struct tagstruct
*t
, const char **s
) {
109 if (t
->rindex
+2 > t
->length
)
112 if (t
->data
[t
->rindex
] != TAG_STRING
)
116 for (n
= 0, c
= (char*) (t
->data
+t
->rindex
+1); n
< t
->length
-t
->rindex
-1; c
++)
125 *s
= (char*) (t
->data
+t
->rindex
+1);
131 int tagstruct_getu32(struct tagstruct
*t
, uint32_t *i
) {
134 if (t
->rindex
+5 > t
->length
)
137 if (t
->data
[t
->rindex
] != TAG_U32
)
140 *i
= ntohl(*((uint32_t*) (t
->data
+t
->rindex
+1)));
145 int tagstruct_getu8(struct tagstruct
*t
, uint8_t *c
) {
148 if (t
->rindex
+2 > t
->length
)
151 if (t
->data
[t
->rindex
] != TAG_U8
)
154 *c
= t
->data
[t
->rindex
+1];
159 int tagstruct_get_sample_spec(struct tagstruct
*t
, struct sample_spec
*ss
) {
162 if (t
->rindex
+7 > t
->length
)
165 if (t
->data
[t
->rindex
] != TAG_SAMPLE_SPEC
)
168 ss
->format
= t
->data
[t
->rindex
+1];
169 ss
->channels
= t
->data
[t
->rindex
+2];
170 ss
->rate
= ntohl(*(uint32_t*) (t
->data
+t
->rindex
+3));
177 int tagstruct_eof(struct tagstruct
*t
) {
179 return t
->rindex
>= t
->length
;
182 const uint8_t* tagstruct_data(struct tagstruct
*t
, size_t *l
) {
183 assert(t
&& t
->dynamic
&& l
);