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
29 * Decodes base64 strings (based upon b64 package)
32 static char *b64_decode(const char *in, char *out, int *size) {
34 char dtable[256]; /* Encode / decode table */
38 for (i = 0; i < 255; i++) {
41 for (i = 'A'; i <= 'Z'; i++) {
42 dtable[i] = 0 + (i - 'A');
44 for (i = 'a'; i <= 'z'; i++) {
45 dtable[i] = 26 + (i - 'a');
47 for (i = '0'; i <= '9'; i++) {
48 dtable[i] = 52 + (i - '0');
56 for (j=0; j<strlen(in); j+=4) {
59 for (i = 0; i < 4; i++) {
62 if (dtable[c] & 0x80) {
63 printf("Illegal character '%c' in input.\n", c);
67 b[i] = (char) dtable[c];
69 //xine_buffer_ensure_size(out, k+3);
70 out[k++] = (b[0] << 2) | (b[1] >> 4);
71 out[k++] = (b[1] << 4) | (b[2] >> 2);
72 out[k++] = (b[2] << 6) | b[3];
73 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
85 static char *nl(char *data) {
87 char *nlptr = (data) ? strchr(data,'\n') : NULL;
88 return (nlptr) ? nlptr + 1 : NULL;
91 static int filter(const char *in, const char *filter, char **out) {
93 int flen=strlen(filter);
98 len = (strchr(in,'\n')) ? (size_t)(strchr(in,'\n')-in) : strlen(in);
99 if (!strncmp(in,filter,flen)) {
100 if(in[flen]=='"') flen++;
101 if(in[len-1]==13) len--;
102 if(in[len-1]=='"') len--;
103 memcpy(*out, in+flen, len-flen+1);
110 static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
112 sdpplin_stream_t *desc = malloc(sizeof(sdpplin_stream_t));
113 char *buf = malloc(32000);
114 char *decoded = malloc(32000);
117 if( !desc ) return NULL;
118 memset(desc, 0, sizeof(sdpplin_stream_t));
120 if( !buf ) goto error;
121 if( !decoded ) goto error;
123 if (filter(*data, "m=", &buf)) {
124 desc->id = strdup(buf);
126 lprintf("sdpplin: no m= found.\n");
131 while (*data && **data && *data[0]!='m') {
134 if(filter(*data,"a=control:streamid=",&buf)) {
135 desc->stream_id=atoi(buf);
139 if(filter(*data,"a=MaxBitRate:integer;",&buf)) {
140 desc->max_bit_rate=atoi(buf);
141 if (!desc->avg_bit_rate)
142 desc->avg_bit_rate=desc->max_bit_rate;
146 if(filter(*data,"a=MaxPacketSize:integer;",&buf)) {
147 desc->max_packet_size=atoi(buf);
148 if (!desc->avg_packet_size)
149 desc->avg_packet_size=desc->max_packet_size;
153 if(filter(*data,"a=StartTime:integer;",&buf)) {
154 desc->start_time=atoi(buf);
158 if(filter(*data,"a=Preroll:integer;",&buf)) {
159 desc->preroll=atoi(buf);
163 if(filter(*data,"a=length:npt=",&buf)) {
164 desc->duration=(uint32_t)(atof(buf)*1000);
168 if(filter(*data,"a=StreamName:string;",&buf)) {
169 desc->stream_name=strdup(buf);
170 desc->stream_name_size=strlen(desc->stream_name);
174 if(filter(*data,"a=mimetype:string;",&buf)) {
175 desc->mime_type=strdup(buf);
176 desc->mime_type_size=strlen(desc->mime_type);
180 if(filter(*data,"a=OpaqueData:buffer;",&buf)) {
181 decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
182 if ( decoded != NULL ) {
183 desc->mlti_data = malloc(sizeof(char)*desc->mlti_data_size);
184 memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
187 lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
190 if(filter(*data,"a=ASMRuleBook:string;",&buf)) {
191 desc->asm_rule_book=strdup(buf);
198 int len=strchr(*data,'\n')-(*data);
199 memcpy(buf, *data, len+1);
201 printf("libreal: sdpplin: not handled: '%s'\n", buf);
207 if( decoded )free(decoded);
211 if( decoded ) free(decoded);
212 if( desc ) free( desc );
213 if( buf ) free( buf );
217 sdpplin_t *sdpplin_parse(char *data) {
219 sdpplin_t *desc = malloc(sizeof(sdpplin_t));
220 sdpplin_stream_t *stream;
221 char *buf=malloc(3200);
222 char *decoded=malloc(3200);
226 if( !desc ) return NULL;
236 memset(desc, 0, sizeof(sdpplin_t));
238 while (data && *data) {
241 if (filter(data, "m=", &buf)) {
242 stream=sdpplin_parse_stream(&data);
243 lprintf("got data for stream id %u\n", stream->stream_id);
244 desc->stream[stream->stream_id]=stream;
247 if(filter(data,"a=Title:buffer;",&buf)) {
248 decoded=b64_decode(buf, decoded, &len);
249 if ( decoded != NULL ) {
250 desc->title=strdup(decoded);
255 if(filter(data,"a=Author:buffer;",&buf)) {
256 decoded=b64_decode(buf, decoded, &len);
257 if ( decoded != NULL ) {
258 desc->author=strdup(decoded);
263 if(filter(data,"a=Copyright:buffer;",&buf)) {
264 decoded=b64_decode(buf, decoded, &len);
265 if ( decoded != NULL ) {
266 desc->copyright=strdup(decoded);
271 if(filter(data,"a=Abstract:buffer;",&buf)) {
272 decoded=b64_decode(buf, decoded, &len);
273 if ( decoded != NULL ) {
274 desc->abstract=strdup(decoded);
279 if(filter(data,"a=StreamCount:integer;",&buf)) {
280 desc->stream_count=atoi(buf);
281 desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
285 if(filter(data,"a=Flags:integer;",&buf)) {
286 desc->flags=atoi(buf);
293 int len=strchr(data,'\n')-data;
294 memcpy(buf, data, len+1);
296 printf("libreal: sdpplin: not handled: '%s'\n", buf);
307 void sdpplin_free(sdpplin_t *description) {
311 if( !description ) return;
313 for( i=0; i<description->stream_count; i++ ) {
314 if( description->stream[i] ) {
315 if( description->stream[i]->id ) free( description->stream[i]->id );
316 if( description->stream[i]->bandwidth ) free( description->stream[i]->bandwidth );
317 if( description->stream[i]->range ) free( description->stream[i]->range );
318 if( description->stream[i]->length ) free( description->stream[i]->length );
319 if( description->stream[i]->rtpmap ) free( description->stream[i]->rtpmap );
320 if( description->stream[i]->mimetype ) free( description->stream[i]->mimetype );
321 if( description->stream[i]->stream_name ) free( description->stream[i]->stream_name );
322 if( description->stream[i]->mime_type ) free( description->stream[i]->mime_type );
323 if( description->stream[i]->mlti_data ) free( description->stream[i]->mlti_data );
324 if( description->stream[i]->rmff_flags ) free( description->stream[i]->rmff_flags );
325 if( description->stream[i]->asm_rule_book ) free( description->stream[i]->asm_rule_book );
326 free( description->stream[i] );
329 if( description->stream_count ) free( description->stream );
331 if( description->owner ) free( description->owner );
332 if( description->session_name ) free( description->session_name );
333 if( description->session_info ) free( description->session_info );
334 if( description->uri ) free( description->uri );
335 if( description->email ) free( description->email );
336 if( description->phone ) free( description->phone );
337 if( description->connection ) free( description->connection );
338 if( description->bandwidth ) free( description->bandwidth );
339 if( description->title ) free( description->title );
340 if( description->author ) free( description->author );
341 if( description->copyright ) free( description->copyright );
342 if( description->keywords ) free( description->keywords );
343 if( description->asm_rule_book ) free( description->asm_rule_book );
344 if( description->abstract ) free( description->abstract );
345 if( description->range ) free( description->range );