]> git.sesse.net Git - vlc/blob - modules/access/rtsp/real_sdpplin.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / access / rtsp / real_sdpplin.c
1 /*
2  * Copyright (C) 2002-2003 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
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.
10  *
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.
15  *
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
19  *
20  * $Id$
21  *
22  * sdp/sdpplin parser.
23  *
24  */
25  
26 #include "real.h"
27 #define BUFLEN 32000
28
29 /*
30  * Decodes base64 strings (based upon b64 package)
31  */
32
33 static char *b64_decode(const char *in, char *out, int *size) {
34
35   char dtable[256];              /* Encode / decode table */
36   int i,k;
37   unsigned int j;
38
39   for( i = 0; i < 256; i++ )
40     dtable[i] = 0x80;
41
42   for( i = 'A'; i <= 'Z'; i++ )
43     dtable[i] = 0 + (i - 'A');
44
45   for( i = 'a'; i <= 'z'; i++ )
46     dtable[i] = 26 + (i - 'a');
47
48   for( i = '0'; i <= '9'; i++ )
49     dtable[i] = 52 + (i - '0');
50
51   dtable['+'] = 62;
52   dtable['/'] = 63;
53   dtable['='] = 0;
54
55   k=0;
56   /*CONSTANTCONDITION*/
57   int in_len = strlen(in);
58   for (j=0; j < in_len; j+=4) {
59     char a[4], b[4];
60
61     for (i = 0; i < 4 && j + i < in_len; i++) {
62       int c = in[i+j];
63
64       if (dtable[c] & 0x80) {
65         printf("Illegal character '%c' in input.\n", c);
66         exit(1);
67       }
68       a[i] = (char) c;
69       b[i] = (char) dtable[c];
70     }
71     //xine_buffer_ensure_size(out, k+3);
72     out[k++] = (b[0] << 2) | (b[1] >> 4);
73     out[k++] = (b[1] << 4) | (b[2] >> 2);
74     out[k++] = (b[2] << 6) | b[3];
75     i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
76     if (i < 3) {
77       out[k]=0;
78       *size=k;
79       return out;
80     }
81   }
82   out[k]=0;
83   *size=k;
84   return out;
85 }
86
87 static char *nl(char *data) {
88
89   char *nlptr = (data) ? strchr(data,'\n') : NULL;
90   return (nlptr) ? nlptr + 1 : NULL;
91 }
92
93 static int filter(const char *in, const char *filter, char **out, size_t outlen) {
94
95   int flen=strlen(filter);
96   size_t len;
97
98   if (!in) return 0;
99
100   len = (strchr(in,'\n')) ? (size_t)(strchr(in,'\n')-in) : strlen(in);
101   if (!strncmp(in,filter,flen)) {
102     if(in[flen]=='"') flen++;
103     if(in[len-1]==13) len--;
104     if(in[len-1]=='"') len--;
105     if( len-flen+1 > outlen )
106     {
107         printf("Discarding end of string to avoid overflow");
108         len=outlen+flen-1;
109     }
110     memcpy(*out, in+flen, len-flen+1);
111     (*out)[len-flen]=0;
112     return len-flen;
113   }
114   return 0;
115 }
116
117 static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
118
119   sdpplin_stream_t *desc;
120   char* buf = NULL;
121   char* decoded = NULL;
122   int handled;
123
124   desc = calloc( 1, sizeof(sdpplin_stream_t) );
125   if( !desc )
126     return NULL;
127
128   buf = malloc( BUFLEN );
129   if( !buf )
130     goto error;
131
132   decoded = malloc( BUFLEN );
133   if( !decoded )
134     goto error;
135
136   if (filter(*data, "m=", &buf, BUFLEN)) {
137     desc->id = strdup(buf);
138   } else {
139     lprintf("sdpplin: no m= found.\n");
140     goto error;
141   }
142   *data=nl(*data);
143
144   while (*data && **data && *data[0]!='m') {
145     handled=0;
146
147     if(filter(*data,"a=control:streamid=",&buf, BUFLEN)) {
148         /* This way negative values are mapped to unfeasibly high
149          * values, and will be discarded afterward
150          */
151         unsigned long tmp = strtoul(buf, NULL, 10);
152         if ( tmp > UINT16_MAX )
153             lprintf("stream id out of bound: %lu\n", tmp);
154         else
155             desc->stream_id=tmp;
156         handled=1;
157         *data=nl(*data);
158     }
159     if(filter(*data,"a=MaxBitRate:integer;",&buf, BUFLEN)) {
160       desc->max_bit_rate=atoi(buf);
161       if (!desc->avg_bit_rate)
162         desc->avg_bit_rate=desc->max_bit_rate;
163       handled=1;
164       *data=nl(*data);
165     }
166     if(filter(*data,"a=MaxPacketSize:integer;",&buf, BUFLEN)) {
167       desc->max_packet_size=atoi(buf);
168       if (!desc->avg_packet_size)
169         desc->avg_packet_size=desc->max_packet_size;
170       handled=1;
171       *data=nl(*data);
172     }
173     if(filter(*data,"a=StartTime:integer;",&buf, BUFLEN)) {
174       desc->start_time=atoi(buf);
175       handled=1;
176       *data=nl(*data);
177     }
178     if(filter(*data,"a=Preroll:integer;",&buf, BUFLEN)) {
179       desc->preroll=atoi(buf);
180       handled=1;
181       *data=nl(*data);
182     }
183     if(filter(*data,"a=length:npt=",&buf, BUFLEN)) {
184       desc->duration=(uint32_t)(atof(buf)*1000);
185       handled=1;
186       *data=nl(*data);
187     }
188     if(filter(*data,"a=StreamName:string;",&buf, BUFLEN)) {
189       desc->stream_name=strdup(buf);
190       desc->stream_name_size=strlen(desc->stream_name);
191       handled=1;
192       *data=nl(*data);
193     }
194     if(filter(*data,"a=mimetype:string;",&buf, BUFLEN)) {
195       desc->mime_type=strdup(buf);
196       desc->mime_type_size=strlen(desc->mime_type);
197       handled=1;
198       *data=nl(*data);
199     }
200     if(filter(*data,"a=OpaqueData:buffer;",&buf, BUFLEN)) {
201       decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
202       if ( decoded != NULL ) {
203           desc->mlti_data = malloc(desc->mlti_data_size);
204           memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
205           handled=1;
206           *data=nl(*data);
207           lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
208       }
209     }
210     if(filter(*data,"a=ASMRuleBook:string;",&buf, BUFLEN)) {
211       desc->asm_rule_book=strdup(buf);
212       handled=1;
213       *data=nl(*data);
214     }
215
216     if(!handled) {
217 #ifdef LOG
218       int len=strchr(*data,'\n')-(*data);
219       memcpy(buf, *data, len+1);
220       buf[len]=0;
221       printf("libreal: sdpplin: not handled: '%s'\n", buf);
222 #endif
223       *data=nl(*data);
224     }
225   }
226   free( buf );
227   free( decoded) ;
228   return desc;
229
230 error:
231   free( decoded );
232   free( desc );
233   free( buf );
234   return NULL;
235 }
236
237
238 sdpplin_t *sdpplin_parse(char *data)
239 {
240   sdpplin_t*        desc;
241   sdpplin_stream_t* stream;
242   char*             buf;
243   char*             decoded;
244   int               handled;
245   int               len;
246
247   desc = calloc( 1, sizeof(sdpplin_t) );
248   if( !desc )
249     return NULL;
250
251   buf = malloc( BUFLEN );
252   if( !buf )
253   {
254     free( desc );
255     return NULL;
256   }
257
258   decoded = malloc( BUFLEN );
259   if( !decoded )
260   {
261     free( buf );
262     free( desc );
263     return NULL;
264   }
265   desc->stream = NULL;
266
267   while (data && *data) {
268     handled=0;
269
270     if (filter(data, "m=", &buf, BUFLEN)) {
271         if ( !desc->stream ) {
272             fprintf(stderr, "sdpplin.c: stream identifier found before stream count, skipping.");
273             continue;
274         }
275         stream=sdpplin_parse_stream(&data);
276         lprintf("got data for stream id %u\n", stream->stream_id);
277         if ( stream->stream_id >= desc->stream_count )
278             lprintf("stream id %u is greater than stream count %u\n", stream->stream_id, desc->stream_count);
279         else
280             desc->stream[stream->stream_id]=stream;
281         continue;
282     }
283     if(filter(data,"a=Title:buffer;",&buf, BUFLEN)) {
284       decoded=b64_decode(buf, decoded, &len);
285           if ( decoded != NULL ) {
286           desc->title=strdup(decoded);
287           handled=1;
288           data=nl(data);
289       }
290     }
291     if(filter(data,"a=Author:buffer;",&buf, BUFLEN)) {
292       decoded=b64_decode(buf, decoded, &len);
293           if ( decoded != NULL ) {
294           desc->author=strdup(decoded);
295           handled=1;
296           data=nl(data);
297       }
298     }
299     if(filter(data,"a=Copyright:buffer;",&buf, BUFLEN)) {
300       decoded=b64_decode(buf, decoded, &len);
301           if ( decoded != NULL ) {
302           desc->copyright=strdup(decoded);
303           handled=1;
304           data=nl(data);
305       }
306     }
307     if(filter(data,"a=Abstract:buffer;",&buf, BUFLEN)) {
308       decoded=b64_decode(buf, decoded, &len);
309       if ( decoded != NULL ) {
310            desc->abstract=strdup(decoded);
311            handled=1;
312            data=nl(data);
313       }
314     }
315     if(filter(data,"a=StreamCount:integer;",&buf, BUFLEN)) {
316         /* This way negative values are mapped to unfeasibly high
317          * values, and will be discarded afterward
318          */
319         unsigned long tmp = strtoul(buf, NULL, 10);
320         if ( tmp > UINT16_MAX )
321             lprintf("stream count out of bound: %lu\n", tmp);
322         else
323             desc->stream_count = tmp;
324         desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
325         handled=1;
326         data=nl(data);
327     }
328     if(filter(data,"a=Flags:integer;",&buf, BUFLEN)) {
329       desc->flags=atoi(buf);
330       handled=1;
331       data=nl(data);
332     }
333
334     if(!handled) {
335 #ifdef LOG
336       int len=strchr(data,'\n')-data;
337       memcpy(buf, data, len+1);
338       buf[len]=0;
339       printf("libreal: sdpplin: not handled: '%s'\n", buf);
340 #endif
341       data=nl(data);
342     }
343   }
344
345   free( decoded );
346   free( buf );
347   return desc;
348 }
349
350 void sdpplin_free(sdpplin_t *description) {
351
352   int i;
353
354   if( !description ) return;
355
356   for( i=0; i<description->stream_count; i++ ) {
357     if( description->stream[i] ) {
358       free( description->stream[i]->id );
359       free( description->stream[i]->bandwidth );
360       free( description->stream[i]->range );
361       free( description->stream[i]->length );
362       free( description->stream[i]->rtpmap );
363       free( description->stream[i]->mimetype );
364       free( description->stream[i]->stream_name );
365       free( description->stream[i]->mime_type );
366       free( description->stream[i]->mlti_data );
367       free( description->stream[i]->rmff_flags );
368       free( description->stream[i]->asm_rule_book );
369       free( description->stream[i] );
370     }
371   }
372   if( description->stream_count )
373     free( description->stream );
374
375   free( description->owner );
376   free( description->session_name );
377   free( description->session_info );
378   free( description->uri );
379   free( description->email );
380   free( description->phone );
381   free( description->connection );
382   free( description->bandwidth );
383   free( description->title );
384   free( description->author );
385   free( description->copyright );
386   free( description->keywords );
387   free( description->asm_rule_book );
388   free( description->abstract );
389   free( description->range );
390   free( description );
391 }
392