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')) ? 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 desc->mlti_data = malloc(sizeof(char)*desc->mlti_data_size);
183 memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
186 lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
188 if(filter(*data,"a=ASMRuleBook:string;",&buf)) {
189 desc->asm_rule_book=strdup(buf);
196 int len=strchr(*data,'\n')-(*data);
197 memcpy(buf, *data, len+1);
199 printf("libreal: sdpplin: not handled: '%s'\n", buf);
205 if( decoded )free(decoded);
209 if( decoded ) free(decoded);
210 if( desc ) free( desc );
211 if( buf ) free( buf );
215 sdpplin_t *sdpplin_parse(char *data) {
217 sdpplin_t *desc = malloc(sizeof(sdpplin_t));
218 sdpplin_stream_t *stream;
219 char *buf=malloc(3200);
220 char *decoded=malloc(3200);
224 if( !desc ) return NULL;
234 memset(desc, 0, sizeof(sdpplin_t));
236 while (data && *data) {
239 if (filter(data, "m=", &buf)) {
240 stream=sdpplin_parse_stream(&data);
241 lprintf("got data for stream id %u\n", stream->stream_id);
242 desc->stream[stream->stream_id]=stream;
245 if(filter(data,"a=Title:buffer;",&buf)) {
246 decoded=b64_decode(buf, decoded, &len);
247 desc->title=strdup(decoded);
251 if(filter(data,"a=Author:buffer;",&buf)) {
252 decoded=b64_decode(buf, decoded, &len);
253 desc->author=strdup(decoded);
257 if(filter(data,"a=Copyright:buffer;",&buf)) {
258 decoded=b64_decode(buf, decoded, &len);
259 desc->copyright=strdup(decoded);
263 if(filter(data,"a=Abstract:buffer;",&buf)) {
264 decoded=b64_decode(buf, decoded, &len);
265 desc->abstract=strdup(decoded);
269 if(filter(data,"a=StreamCount:integer;",&buf)) {
270 desc->stream_count=atoi(buf);
271 desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
275 if(filter(data,"a=Flags:integer;",&buf)) {
276 desc->flags=atoi(buf);
283 int len=strchr(data,'\n')-data;
284 memcpy(buf, data, len+1);
286 printf("libreal: sdpplin: not handled: '%s'\n", buf);
297 void sdpplin_free(sdpplin_t *description) {
301 if( !description ) return;
303 for( i=0; i<description->stream_count; i++ ) {
304 if( description->stream[i] ) {
305 if( description->stream[i]->id ) free( description->stream[i]->id );
306 if( description->stream[i]->bandwidth ) free( description->stream[i]->bandwidth );
307 if( description->stream[i]->range ) free( description->stream[i]->range );
308 if( description->stream[i]->length ) free( description->stream[i]->length );
309 if( description->stream[i]->rtpmap ) free( description->stream[i]->rtpmap );
310 if( description->stream[i]->mimetype ) free( description->stream[i]->mimetype );
311 if( description->stream[i]->stream_name ) free( description->stream[i]->stream_name );
312 if( description->stream[i]->mime_type ) free( description->stream[i]->mime_type );
313 if( description->stream[i]->mlti_data ) free( description->stream[i]->mlti_data );
314 if( description->stream[i]->rmff_flags ) free( description->stream[i]->rmff_flags );
315 if( description->stream[i]->asm_rule_book ) free( description->stream[i]->asm_rule_book );
316 free( description->stream[i] );
319 if( description->stream_count ) free( description->stream );
321 if( description->owner ) free( description->owner );
322 if( description->session_name ) free( description->session_name );
323 if( description->session_info ) free( description->session_info );
324 if( description->uri ) free( description->uri );
325 if( description->email ) free( description->email );
326 if( description->phone ) free( description->phone );
327 if( description->connection ) free( description->connection );
328 if( description->bandwidth ) free( description->bandwidth );
329 if( description->title ) free( description->title );
330 if( description->author ) free( description->author );
331 if( description->copyright ) free( description->copyright );
332 if( description->keywords ) free( description->keywords );
333 if( description->asm_rule_book ) free( description->asm_rule_book );
334 if( description->abstract ) free( description->abstract );
335 if( description->range ) free( description->range );