2 * Copyright (C) 2002-2003 the xine project
4 * This file is part of xine, a free video player.
6 * xine is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * xine is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
30 * Decodes base64 strings (based upon b64 package)
33 static char *b64_decode(const char *in, char *out, int *size) {
35 char dtable[256]; /* Encode / decode table */
39 for( i = 0; i < 256; i++ )
42 for( i = 'A'; i <= 'Z'; i++ )
43 dtable[i] = 0 + (i - 'A');
45 for( i = 'a'; i <= 'z'; i++ )
46 dtable[i] = 26 + (i - 'a');
48 for( i = '0'; i <= '9'; i++ )
49 dtable[i] = 52 + (i - '0');
57 int in_len = strlen(in);
58 for (j=0; j < in_len; j+=4) {
61 for (i = 0; i < 4 && j + i < in_len; i++) {
64 if (dtable[c] & 0x80) {
65 printf("Illegal character '%c' in input.\n", c);
69 b[i] = (char) dtable[c];
71 //xine_buffer_ensure_size(out, k+3);
72 out[k++] = (b[0] << 2) | (b[1] >> 4);
73 out[k++] = (b[1] << 4) | (b[2] >> 2);
74 out[k++] = (b[2] << 6) | b[3];
75 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
87 static char *nl(char *data) {
89 char *nlptr = (data) ? strchr(data,'\n') : NULL;
90 return (nlptr) ? nlptr + 1 : NULL;
93 static int filter(const char *in, const char *filter, char **out, size_t outlen) {
95 int flen=strlen(filter);
100 len = (strchr(in,'\n')) ? (size_t)(strchr(in,'\n')-in) : strlen(in);
101 if (!strncmp(in,filter,flen)) {
102 if(in[flen]=='"') flen++;
103 if(in[len-1]==13) len--;
104 if(in[len-1]=='"') len--;
105 if( len-flen+1 > outlen )
107 printf("Discarding end of string to avoid overflow");
110 memcpy(*out, in+flen, len-flen+1);
117 static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
119 sdpplin_stream_t *desc;
121 char* decoded = NULL;
124 desc = calloc( 1, sizeof(sdpplin_stream_t) );
128 buf = malloc( BUFLEN );
132 decoded = malloc( BUFLEN );
136 if (filter(*data, "m=", &buf, BUFLEN)) {
137 desc->id = strdup(buf);
139 lprintf("sdpplin: no m= found.\n");
144 while (*data && **data && *data[0]!='m') {
147 if(filter(*data,"a=control:streamid=",&buf, BUFLEN)) {
148 /* This way negative values are mapped to unfeasibly high
149 * values, and will be discarded afterward
151 unsigned long tmp = strtoul(buf, NULL, 10);
152 if ( tmp > UINT16_MAX )
153 lprintf("stream id out of bound: %lu\n", tmp);
159 if(filter(*data,"a=MaxBitRate:integer;",&buf, BUFLEN)) {
160 desc->max_bit_rate=atoi(buf);
161 if (!desc->avg_bit_rate)
162 desc->avg_bit_rate=desc->max_bit_rate;
166 if(filter(*data,"a=MaxPacketSize:integer;",&buf, BUFLEN)) {
167 desc->max_packet_size=atoi(buf);
168 if (!desc->avg_packet_size)
169 desc->avg_packet_size=desc->max_packet_size;
173 if(filter(*data,"a=StartTime:integer;",&buf, BUFLEN)) {
174 desc->start_time=atoi(buf);
178 if(filter(*data,"a=Preroll:integer;",&buf, BUFLEN)) {
179 desc->preroll=atoi(buf);
183 if(filter(*data,"a=length:npt=",&buf, BUFLEN)) {
184 desc->duration=(uint32_t)(atof(buf)*1000);
188 if(filter(*data,"a=StreamName:string;",&buf, BUFLEN)) {
189 desc->stream_name=strdup(buf);
190 desc->stream_name_size=strlen(desc->stream_name);
194 if(filter(*data,"a=mimetype:string;",&buf, BUFLEN)) {
195 desc->mime_type=strdup(buf);
196 desc->mime_type_size=strlen(desc->mime_type);
200 if(filter(*data,"a=OpaqueData:buffer;",&buf, BUFLEN)) {
201 decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
202 if ( decoded != NULL ) {
203 desc->mlti_data = malloc(desc->mlti_data_size);
204 memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
207 lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
210 if(filter(*data,"a=ASMRuleBook:string;",&buf, BUFLEN)) {
211 desc->asm_rule_book=strdup(buf);
218 int len=strchr(*data,'\n')-(*data);
219 memcpy(buf, *data, len+1);
221 printf("libreal: sdpplin: not handled: '%s'\n", buf);
238 sdpplin_t *sdpplin_parse(char *data)
241 sdpplin_stream_t* stream;
247 desc = calloc( 1, sizeof(sdpplin_t) );
251 buf = malloc( BUFLEN );
258 decoded = malloc( BUFLEN );
267 while (data && *data) {
270 if (filter(data, "m=", &buf, BUFLEN)) {
271 if ( !desc->stream ) {
272 fprintf(stderr, "sdpplin.c: stream identifier found before stream count, skipping.");
275 stream=sdpplin_parse_stream(&data);
276 lprintf("got data for stream id %u\n", stream->stream_id);
277 if ( stream->stream_id >= desc->stream_count )
278 lprintf("stream id %u is greater than stream count %u\n", stream->stream_id, desc->stream_count);
280 desc->stream[stream->stream_id]=stream;
283 if(filter(data,"a=Title:buffer;",&buf, BUFLEN)) {
284 decoded=b64_decode(buf, decoded, &len);
285 if ( decoded != NULL ) {
286 desc->title=strdup(decoded);
291 if(filter(data,"a=Author:buffer;",&buf, BUFLEN)) {
292 decoded=b64_decode(buf, decoded, &len);
293 if ( decoded != NULL ) {
294 desc->author=strdup(decoded);
299 if(filter(data,"a=Copyright:buffer;",&buf, BUFLEN)) {
300 decoded=b64_decode(buf, decoded, &len);
301 if ( decoded != NULL ) {
302 desc->copyright=strdup(decoded);
307 if(filter(data,"a=Abstract:buffer;",&buf, BUFLEN)) {
308 decoded=b64_decode(buf, decoded, &len);
309 if ( decoded != NULL ) {
310 desc->abstract=strdup(decoded);
315 if(filter(data,"a=StreamCount:integer;",&buf, BUFLEN)) {
316 /* This way negative values are mapped to unfeasibly high
317 * values, and will be discarded afterward
319 unsigned long tmp = strtoul(buf, NULL, 10);
320 if ( tmp > UINT16_MAX )
321 lprintf("stream count out of bound: %lu\n", tmp);
323 desc->stream_count = tmp;
324 desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
328 if(filter(data,"a=Flags:integer;",&buf, BUFLEN)) {
329 desc->flags=atoi(buf);
336 int len=strchr(data,'\n')-data;
337 memcpy(buf, data, len+1);
339 printf("libreal: sdpplin: not handled: '%s'\n", buf);
350 void sdpplin_free(sdpplin_t *description) {
354 if( !description ) return;
356 for( i=0; i<description->stream_count; i++ ) {
357 if( description->stream[i] ) {
358 free( description->stream[i]->id );
359 free( description->stream[i]->bandwidth );
360 free( description->stream[i]->range );
361 free( description->stream[i]->length );
362 free( description->stream[i]->rtpmap );
363 free( description->stream[i]->mimetype );
364 free( description->stream[i]->stream_name );
365 free( description->stream[i]->mime_type );
366 free( description->stream[i]->mlti_data );
367 free( description->stream[i]->rmff_flags );
368 free( description->stream[i]->asm_rule_book );
369 free( description->stream[i] );
372 if( description->stream_count )
373 free( description->stream );
375 free( description->owner );
376 free( description->session_name );
377 free( description->session_info );
378 free( description->uri );
379 free( description->email );
380 free( description->phone );
381 free( description->connection );
382 free( description->bandwidth );
383 free( description->title );
384 free( description->author );
385 free( description->copyright );
386 free( description->keywords );
387 free( description->asm_rule_book );
388 free( description->abstract );
389 free( description->range );