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 for (j=0; j<strlen(in); j+=4) {
60 for (i = 0; i < 4; i++) {
63 if (dtable[c] & 0x80) {
64 printf("Illegal character '%c' in input.\n", c);
68 b[i] = (char) dtable[c];
70 //xine_buffer_ensure_size(out, k+3);
71 out[k++] = (b[0] << 2) | (b[1] >> 4);
72 out[k++] = (b[1] << 4) | (b[2] >> 2);
73 out[k++] = (b[2] << 6) | b[3];
74 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
86 static char *nl(char *data) {
88 char *nlptr = (data) ? strchr(data,'\n') : NULL;
89 return (nlptr) ? nlptr + 1 : NULL;
92 static int filter(const char *in, const char *filter, char **out, size_t outlen) {
94 int flen=strlen(filter);
99 len = (strchr(in,'\n')) ? (size_t)(strchr(in,'\n')-in) : strlen(in);
100 if (!strncmp(in,filter,flen)) {
101 if(in[flen]=='"') flen++;
102 if(in[len-1]==13) len--;
103 if(in[len-1]=='"') len--;
104 if( len-flen+1 > outlen )
106 printf("Discarding end of string to avoid overflow");
109 memcpy(*out, in+flen, len-flen+1);
116 static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
118 sdpplin_stream_t *desc;
120 char* decoded = NULL;
123 desc = calloc( 1, sizeof(sdpplin_stream_t) );
127 buf = malloc( BUFLEN );
131 decoded = malloc( BUFLEN );
135 if (filter(*data, "m=", &buf, BUFLEN)) {
136 desc->id = strdup(buf);
138 lprintf("sdpplin: no m= found.\n");
143 while (*data && **data && *data[0]!='m') {
146 if(filter(*data,"a=control:streamid=",&buf, BUFLEN)) {
147 /* This way negative values are mapped to unfeasibly high
148 * values, and will be discarded afterward
150 unsigned long tmp = strtoul(buf, NULL, 10);
151 if ( tmp > UINT16_MAX )
152 lprintf("stream id out of bound: %lu\n", tmp);
158 if(filter(*data,"a=MaxBitRate:integer;",&buf, BUFLEN)) {
159 desc->max_bit_rate=atoi(buf);
160 if (!desc->avg_bit_rate)
161 desc->avg_bit_rate=desc->max_bit_rate;
165 if(filter(*data,"a=MaxPacketSize:integer;",&buf, BUFLEN)) {
166 desc->max_packet_size=atoi(buf);
167 if (!desc->avg_packet_size)
168 desc->avg_packet_size=desc->max_packet_size;
172 if(filter(*data,"a=StartTime:integer;",&buf, BUFLEN)) {
173 desc->start_time=atoi(buf);
177 if(filter(*data,"a=Preroll:integer;",&buf, BUFLEN)) {
178 desc->preroll=atoi(buf);
182 if(filter(*data,"a=length:npt=",&buf, BUFLEN)) {
183 desc->duration=(uint32_t)(atof(buf)*1000);
187 if(filter(*data,"a=StreamName:string;",&buf, BUFLEN)) {
188 desc->stream_name=strdup(buf);
189 desc->stream_name_size=strlen(desc->stream_name);
193 if(filter(*data,"a=mimetype:string;",&buf, BUFLEN)) {
194 desc->mime_type=strdup(buf);
195 desc->mime_type_size=strlen(desc->mime_type);
199 if(filter(*data,"a=OpaqueData:buffer;",&buf, BUFLEN)) {
200 decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
201 if ( decoded != NULL ) {
202 desc->mlti_data = malloc(desc->mlti_data_size);
203 memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
206 lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
209 if(filter(*data,"a=ASMRuleBook:string;",&buf, BUFLEN)) {
210 desc->asm_rule_book=strdup(buf);
217 int len=strchr(*data,'\n')-(*data);
218 memcpy(buf, *data, len+1);
220 printf("libreal: sdpplin: not handled: '%s'\n", buf);
237 sdpplin_t *sdpplin_parse(char *data)
240 sdpplin_stream_t* stream;
246 desc = calloc( 1, sizeof(sdpplin_t) );
250 buf = malloc( BUFLEN );
257 decoded = malloc( BUFLEN );
266 while (data && *data) {
269 if (filter(data, "m=", &buf, BUFLEN)) {
270 if ( !desc->stream ) {
271 fprintf(stderr, "sdpplin.c: stream identifier found before stream count, skipping.");
274 stream=sdpplin_parse_stream(&data);
275 lprintf("got data for stream id %u\n", stream->stream_id);
276 if ( stream->stream_id >= desc->stream_count )
277 lprintf("stream id %u is greater than stream count %u\n", stream->stream_id, desc->stream_count);
279 desc->stream[stream->stream_id]=stream;
282 if(filter(data,"a=Title:buffer;",&buf, BUFLEN)) {
283 decoded=b64_decode(buf, decoded, &len);
284 if ( decoded != NULL ) {
285 desc->title=strdup(decoded);
290 if(filter(data,"a=Author:buffer;",&buf, BUFLEN)) {
291 decoded=b64_decode(buf, decoded, &len);
292 if ( decoded != NULL ) {
293 desc->author=strdup(decoded);
298 if(filter(data,"a=Copyright:buffer;",&buf, BUFLEN)) {
299 decoded=b64_decode(buf, decoded, &len);
300 if ( decoded != NULL ) {
301 desc->copyright=strdup(decoded);
306 if(filter(data,"a=Abstract:buffer;",&buf, BUFLEN)) {
307 decoded=b64_decode(buf, decoded, &len);
308 if ( decoded != NULL ) {
309 desc->abstract=strdup(decoded);
314 if(filter(data,"a=StreamCount:integer;",&buf, BUFLEN)) {
315 /* This way negative values are mapped to unfeasibly high
316 * values, and will be discarded afterward
318 unsigned long tmp = strtoul(buf, NULL, 10);
319 if ( tmp > UINT16_MAX )
320 lprintf("stream count out of bound: %lu\n", tmp);
322 desc->stream_count = tmp;
323 desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
327 if(filter(data,"a=Flags:integer;",&buf, BUFLEN)) {
328 desc->flags=atoi(buf);
335 int len=strchr(data,'\n')-data;
336 memcpy(buf, data, len+1);
338 printf("libreal: sdpplin: not handled: '%s'\n", buf);
349 void sdpplin_free(sdpplin_t *description) {
353 if( !description ) return;
355 for( i=0; i<description->stream_count; i++ ) {
356 if( description->stream[i] ) {
357 free( description->stream[i]->id );
358 free( description->stream[i]->bandwidth );
359 free( description->stream[i]->range );
360 free( description->stream[i]->length );
361 free( description->stream[i]->rtpmap );
362 free( description->stream[i]->mimetype );
363 free( description->stream[i]->stream_name );
364 free( description->stream[i]->mime_type );
365 free( description->stream[i]->mlti_data );
366 free( description->stream[i]->rmff_flags );
367 free( description->stream[i]->asm_rule_book );
368 free( description->stream[i] );
371 if( description->stream_count )
372 free( description->stream );
374 free( description->owner );
375 free( description->session_name );
376 free( description->session_info );
377 free( description->uri );
378 free( description->email );
379 free( description->phone );
380 free( description->connection );
381 free( description->bandwidth );
382 free( description->title );
383 free( description->author );
384 free( description->copyright );
385 free( description->keywords );
386 free( description->asm_rule_book );
387 free( description->abstract );
388 free( description->range );