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 < 255; 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 = malloc(sizeof(sdpplin_stream_t));
119 char *buf = malloc(BUFLEN);
120 char *decoded = malloc(BUFLEN);
123 if( !desc ) return NULL;
124 memset(desc, 0, sizeof(sdpplin_stream_t));
126 if( !buf ) goto error;
127 if( !decoded ) goto error;
129 if (filter(*data, "m=", &buf, BUFLEN)) {
130 desc->id = strdup(buf);
132 lprintf("sdpplin: no m= found.\n");
137 while (*data && **data && *data[0]!='m') {
140 if(filter(*data,"a=control:streamid=",&buf, BUFLEN)) {
141 /* This way negative values are mapped to unfeasibly high
142 * values, and will be discarded afterward
144 unsigned long tmp = strtoul(buf, NULL, 10);
145 if ( tmp > UINT16_MAX )
146 lprintf("stream id out of bound: %lu\n", tmp);
152 if(filter(*data,"a=MaxBitRate:integer;",&buf, BUFLEN)) {
153 desc->max_bit_rate=atoi(buf);
154 if (!desc->avg_bit_rate)
155 desc->avg_bit_rate=desc->max_bit_rate;
159 if(filter(*data,"a=MaxPacketSize:integer;",&buf, BUFLEN)) {
160 desc->max_packet_size=atoi(buf);
161 if (!desc->avg_packet_size)
162 desc->avg_packet_size=desc->max_packet_size;
166 if(filter(*data,"a=StartTime:integer;",&buf, BUFLEN)) {
167 desc->start_time=atoi(buf);
171 if(filter(*data,"a=Preroll:integer;",&buf, BUFLEN)) {
172 desc->preroll=atoi(buf);
176 if(filter(*data,"a=length:npt=",&buf, BUFLEN)) {
177 desc->duration=(uint32_t)(atof(buf)*1000);
181 if(filter(*data,"a=StreamName:string;",&buf, BUFLEN)) {
182 desc->stream_name=strdup(buf);
183 desc->stream_name_size=strlen(desc->stream_name);
187 if(filter(*data,"a=mimetype:string;",&buf, BUFLEN)) {
188 desc->mime_type=strdup(buf);
189 desc->mime_type_size=strlen(desc->mime_type);
193 if(filter(*data,"a=OpaqueData:buffer;",&buf, BUFLEN)) {
194 decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
195 if ( decoded != NULL ) {
196 desc->mlti_data = malloc(sizeof(char)*desc->mlti_data_size);
197 memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
200 lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
203 if(filter(*data,"a=ASMRuleBook:string;",&buf, BUFLEN)) {
204 desc->asm_rule_book=strdup(buf);
211 int len=strchr(*data,'\n')-(*data);
212 memcpy(buf, *data, len+1);
214 printf("libreal: sdpplin: not handled: '%s'\n", buf);
230 sdpplin_t *sdpplin_parse(char *data) {
232 sdpplin_t *desc = malloc(sizeof(sdpplin_t));
233 sdpplin_stream_t *stream;
234 char *buf=malloc(BUFLEN);
235 char *decoded=malloc(BUFLEN);
239 if( !desc ) return NULL;
252 memset(desc, 0, sizeof(sdpplin_t));
254 while (data && *data) {
257 if (filter(data, "m=", &buf, BUFLEN)) {
258 if ( !desc->stream ) {
259 fprintf(stderr, "sdpplin.c: stream identifier found before stream count, skipping.");
262 stream=sdpplin_parse_stream(&data);
263 lprintf("got data for stream id %u\n", stream->stream_id);
264 if ( stream->stream_id >= desc->stream_count )
265 lprintf("stream id %u is greater than stream count %u\n", stream->stream_id, desc->stream_count);
267 desc->stream[stream->stream_id]=stream;
270 if(filter(data,"a=Title:buffer;",&buf, BUFLEN)) {
271 decoded=b64_decode(buf, decoded, &len);
272 if ( decoded != NULL ) {
273 desc->title=strdup(decoded);
278 if(filter(data,"a=Author:buffer;",&buf, BUFLEN)) {
279 decoded=b64_decode(buf, decoded, &len);
280 if ( decoded != NULL ) {
281 desc->author=strdup(decoded);
286 if(filter(data,"a=Copyright:buffer;",&buf, BUFLEN)) {
287 decoded=b64_decode(buf, decoded, &len);
288 if ( decoded != NULL ) {
289 desc->copyright=strdup(decoded);
294 if(filter(data,"a=Abstract:buffer;",&buf, BUFLEN)) {
295 decoded=b64_decode(buf, decoded, &len);
296 if ( decoded != NULL ) {
297 desc->abstract=strdup(decoded);
302 if(filter(data,"a=StreamCount:integer;",&buf, BUFLEN)) {
303 /* This way negative values are mapped to unfeasibly high
304 * values, and will be discarded afterward
306 unsigned long tmp = strtoul(buf, NULL, 10);
307 if ( tmp > UINT16_MAX )
308 lprintf("stream count out of bound: %lu\n", tmp);
310 desc->stream_count = tmp;
311 desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
315 if(filter(data,"a=Flags:integer;",&buf, BUFLEN)) {
316 desc->flags=atoi(buf);
323 int len=strchr(data,'\n')-data;
324 memcpy(buf, data, len+1);
326 printf("libreal: sdpplin: not handled: '%s'\n", buf);
337 void sdpplin_free(sdpplin_t *description) {
341 if( !description ) return;
343 for( i=0; i<description->stream_count; i++ ) {
344 if( description->stream[i] ) {
345 free( description->stream[i]->id );
346 free( description->stream[i]->bandwidth );
347 free( description->stream[i]->range );
348 free( description->stream[i]->length );
349 free( description->stream[i]->rtpmap );
350 free( description->stream[i]->mimetype );
351 free( description->stream[i]->stream_name );
352 free( description->stream[i]->mime_type );
353 free( description->stream[i]->mlti_data );
354 free( description->stream[i]->rmff_flags );
355 free( description->stream[i]->asm_rule_book );
356 free( description->stream[i] );
359 if( description->stream_count ) free( description->stream );
361 free( description->owner );
362 free( description->session_name );
363 free( description->session_info );
364 free( description->uri );
365 free( description->email );
366 free( description->phone );
367 free( description->connection );
368 free( description->bandwidth );
369 free( description->title );
370 free( description->author );
371 free( description->copyright );
372 free( description->keywords );
373 free( description->asm_rule_book );
374 free( description->abstract );
375 free( description->range );