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 desc->stream_id=atoi(buf);
145 if(filter(*data,"a=MaxBitRate:integer;",&buf, BUFLEN)) {
146 desc->max_bit_rate=atoi(buf);
147 if (!desc->avg_bit_rate)
148 desc->avg_bit_rate=desc->max_bit_rate;
152 if(filter(*data,"a=MaxPacketSize:integer;",&buf, BUFLEN)) {
153 desc->max_packet_size=atoi(buf);
154 if (!desc->avg_packet_size)
155 desc->avg_packet_size=desc->max_packet_size;
159 if(filter(*data,"a=StartTime:integer;",&buf, BUFLEN)) {
160 desc->start_time=atoi(buf);
164 if(filter(*data,"a=Preroll:integer;",&buf, BUFLEN)) {
165 desc->preroll=atoi(buf);
169 if(filter(*data,"a=length:npt=",&buf, BUFLEN)) {
170 desc->duration=(uint32_t)(atof(buf)*1000);
174 if(filter(*data,"a=StreamName:string;",&buf, BUFLEN)) {
175 desc->stream_name=strdup(buf);
176 desc->stream_name_size=strlen(desc->stream_name);
180 if(filter(*data,"a=mimetype:string;",&buf, BUFLEN)) {
181 desc->mime_type=strdup(buf);
182 desc->mime_type_size=strlen(desc->mime_type);
186 if(filter(*data,"a=OpaqueData:buffer;",&buf, BUFLEN)) {
187 decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
188 if ( decoded != NULL ) {
189 desc->mlti_data = malloc(sizeof(char)*desc->mlti_data_size);
190 memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
193 lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
196 if(filter(*data,"a=ASMRuleBook:string;",&buf, BUFLEN)) {
197 desc->asm_rule_book=strdup(buf);
204 int len=strchr(*data,'\n')-(*data);
205 memcpy(buf, *data, len+1);
207 printf("libreal: sdpplin: not handled: '%s'\n", buf);
223 sdpplin_t *sdpplin_parse(char *data) {
225 sdpplin_t *desc = malloc(sizeof(sdpplin_t));
226 sdpplin_stream_t *stream;
227 char *buf=malloc(BUFLEN);
228 char *decoded=malloc(BUFLEN);
232 if( !desc ) return NULL;
245 memset(desc, 0, sizeof(sdpplin_t));
247 while (data && *data) {
250 if (filter(data, "m=", &buf, BUFLEN)) {
251 if ( !desc->stream ) {
252 fprintf(stderr, "sdpplin.c: stream identifier found before stream count, skipping.");
255 stream=sdpplin_parse_stream(&data);
256 lprintf("got data for stream id %u\n", stream->stream_id);
257 desc->stream[stream->stream_id]=stream;
260 if(filter(data,"a=Title:buffer;",&buf, BUFLEN)) {
261 decoded=b64_decode(buf, decoded, &len);
262 if ( decoded != NULL ) {
263 desc->title=strdup(decoded);
268 if(filter(data,"a=Author:buffer;",&buf, BUFLEN)) {
269 decoded=b64_decode(buf, decoded, &len);
270 if ( decoded != NULL ) {
271 desc->author=strdup(decoded);
276 if(filter(data,"a=Copyright:buffer;",&buf, BUFLEN)) {
277 decoded=b64_decode(buf, decoded, &len);
278 if ( decoded != NULL ) {
279 desc->copyright=strdup(decoded);
284 if(filter(data,"a=Abstract:buffer;",&buf, BUFLEN)) {
285 decoded=b64_decode(buf, decoded, &len);
286 if ( decoded != NULL ) {
287 desc->abstract=strdup(decoded);
292 if(filter(data,"a=StreamCount:integer;",&buf, BUFLEN)) {
293 desc->stream_count=atoi(buf);
294 desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
298 if(filter(data,"a=Flags:integer;",&buf, BUFLEN)) {
299 desc->flags=atoi(buf);
306 int len=strchr(data,'\n')-data;
307 memcpy(buf, data, len+1);
309 printf("libreal: sdpplin: not handled: '%s'\n", buf);
320 void sdpplin_free(sdpplin_t *description) {
324 if( !description ) return;
326 for( i=0; i<description->stream_count; i++ ) {
327 if( description->stream[i] ) {
328 free( description->stream[i]->id );
329 free( description->stream[i]->bandwidth );
330 free( description->stream[i]->range );
331 free( description->stream[i]->length );
332 free( description->stream[i]->rtpmap );
333 free( description->stream[i]->mimetype );
334 free( description->stream[i]->stream_name );
335 free( description->stream[i]->mime_type );
336 free( description->stream[i]->mlti_data );
337 free( description->stream[i]->rmff_flags );
338 free( description->stream[i]->asm_rule_book );
339 free( description->stream[i] );
342 if( description->stream_count ) free( description->stream );
344 free( description->owner );
345 free( description->session_name );
346 free( description->session_info );
347 free( description->uri );
348 free( description->email );
349 free( description->phone );
350 free( description->connection );
351 free( description->bandwidth );
352 free( description->title );
353 free( description->author );
354 free( description->copyright );
355 free( description->keywords );
356 free( description->asm_rule_book );
357 free( description->abstract );
358 free( description->range );