summaryrefslogtreecommitdiffstats
path: root/polyp/tagstruct.c
blob: cb93a9c4df3c78071a07b1d9c929d008d1390027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  polypaudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <assert.h>

#include "tagstruct.h"
#include "xmalloc.h"

enum tags {
    TAG_STRING = 't',
    TAG_U32 = 'L',
    TAG_S32 = 'l',
    TAG_U16 = 'S',
    TAG_S16 = 's',
    TAG_U8 = 'B',
    TAG_S8 = 'b',
    TAG_SAMPLE_SPEC = 'a',
    TAG_ARBITRARY = 'x'
};

struct pa_tagstruct {
    uint8_t *data;
    size_t length, allocated;
    size_t rindex;

    int dynamic;
};

struct pa_tagstruct *pa_tagstruct_new(const uint8_t* data, size_t length) {
    struct pa_tagstruct*t;

    assert(!data || (data && length));
    
    t = pa_xmalloc(sizeof(struct pa_tagstruct));
    t->data = (uint8_t*) data;
    t->allocated = t->length = data ? length : 0;
    t->rindex = 0;
    t->dynamic = !data;
    return t;
}
    
void pa_tagstruct_free(struct pa_tagstruct*t) {
    assert(t);
    if (t->dynamic)
        pa_xfree(t->data);
    pa_xfree(t);
}

uint8_t* pa_tagstruct_free_data(struct pa_tagstruct*t, size_t *l) {
    uint8_t *p;
    assert(t && t->dynamic && l);
    p = t->data;
    *l = t->length;
    pa_xfree(t);
    return p;
}

static void extend(struct pa_tagstruct*t, size_t l) {
    assert(t && t->dynamic);

    if (l <= t->allocated)
        return;

    t->data = pa_xrealloc(t->data, t->allocated = l+100);
}

void pa_tagstruct_puts(struct pa_tagstruct*t, const char *s) {
    size_t l;
    assert(t && s);
    l = strlen(s)+2;
    extend(t, l);
    t->data[t->length] = TAG_STRING;
    strcpy(t->data+t->length+1, s);
    t->length += l;
}

void pa_tagstruct_putu32(struct pa_tagstruct*t, uint32_t i) {
    assert(t);
    extend(t, 5);
    t->data[t->length] = TAG_U32;
    *((uint32_t*) (t->data+t->length+1)) = htonl(i);
    t->length += 5;
}

void pa_tagstruct_putu8(struct pa_tagstruct*t, uint8_t c) {
    assert(t);
    extend(t, 2);
    t->data[t->length] = TAG_U8;
    *(t->data+t->length+1) = c;
    t->length += 2;
}

void pa_tagstruct_put_sample_spec(struct pa_tagstruct *t, const struct pa_sample_spec *ss) {
    assert(t && ss);
    extend(t, 7);
    t->data[t->length] = TAG_SAMPLE_SPEC;
    t->data[t->length+1] = (uint8_t) ss->format;
    t->data[t->length+2] = ss->channels;
    *(uint32_t*) (t->data+t->length+3) = htonl(ss->rate);
    t->length += 7;
}


void pa_tagstruct_put_arbitrary(struct pa_tagstruct *t, const void *p, size_t length) {
    assert(t && p);

    extend(t, 5+length);
    t->data[t->length] = TAG_ARBITRARY;
    *((uint32_t*) (t->data+t->length+1)) = htonl(length);
    if (length)
        memcpy(t->data+t->length+5, p, length);
    t->length += 5+length;
}

int pa_tagstruct_gets(struct pa_tagstruct*t, const char **s) {
    int error = 0;
    size_t n;
    char *c;
    assert(t && s);

    if (t->rindex+2 > t->length)
        return -1;
    
    if (t->data[t->rindex] != TAG_STRING)
        return -1;

    error = 1;
    for (n = 0, c = (char*) (t->data+t->rindex+1); t->rindex+1+n < t->length; n++, c++)
        if (!*c) {
            error = 0;
            break;
        }

    if (error)
        return -1;

    *s = (char*) (t->data+t->rindex+1);

    t->rindex += n+2;
    return 0;
}

int pa_tagstruct_getu32(struct pa_tagstruct*t, uint32_t *i) {
    assert(t && i);

    if (t->rindex+5 > t->length)
        return -1;

    if (t->data[t->rindex] != TAG_U32)
        return -1;
    
    *i = ntohl(*((uint32_t*) (t->data+t->rindex+1)));
    t->rindex += 5;
    return 0;
}

int pa_tagstruct_getu8(struct pa_tagstruct*t, uint8_t *c) {
    assert(t && c);

    if (t->rindex+2 > t->length)
        return -1;

    if (t->data[t->rindex] != TAG_U8)
        return -1;

    *c = t->data[t->rindex+1];
    t->rindex +=2;
    return 0;
}

int pa_tagstruct_get_sample_spec(struct pa_tagstruct *t, struct pa_sample_spec *ss) {
    assert(t && ss);

    if (t->rindex+7 > t->length)
        return -1;

    if (t->data[t->rindex] != TAG_SAMPLE_SPEC)
        return -1;
    
    ss->format = t->data[t->rindex+1];
    ss->channels = t->data[t->rindex+2];
    ss->rate = ntohl(*(uint32_t*) (t->data+t->rindex+3));
    
    t->rindex += 7;
    return 0;
}

int pa_tagstruct_get_arbitrary(struct pa_tagstruct *t, const void **p, size_t length) {
    assert(t && p);
    
    if (t->rindex+5+length > t->length)
        return -1;

    if (t->data[t->rindex] != TAG_ARBITRARY)
        return -1;

    if (ntohl(*((uint32_t*) (t->data+t->rindex+1))) != length)
        return -1;

    *p = t->data+t->rindex+5;
    t->rindex += 5+length;
    return 0;
}

int pa_tagstruct_eof(struct pa_tagstruct*t) {
    assert(t);
    return t->rindex >= t->length;
}

const uint8_t* pa_tagstruct_data(struct pa_tagstruct*t, size_t *l) {
    assert(t && t->dynamic && l);
    *l = t->length;
    return t->data;
}