]> git.sesse.net Git - vlc/blob - modules/access/rtsp/real_sdpplin.c
Fix warning
[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
28 /*
29  * Decodes base64 strings (based upon b64 package)
30  */
31
32 static char *b64_decode(const char *in, char *out, int *size) {
33
34   char dtable[256];              /* Encode / decode table */
35   int i,k;
36   unsigned int j;
37
38   for (i = 0; i < 255; i++) {
39     dtable[i] = 0x80;
40   }
41   for (i = 'A'; i <= 'Z'; i++) {
42     dtable[i] = 0 + (i - 'A');
43   }
44   for (i = 'a'; i <= 'z'; i++) {
45     dtable[i] = 26 + (i - 'a');
46   }
47   for (i = '0'; i <= '9'; i++) {
48     dtable[i] = 52 + (i - '0');
49   }
50   dtable['+'] = 62;
51   dtable['/'] = 63;
52   dtable['='] = 0;
53
54   k=0;
55   /*CONSTANTCONDITION*/
56   for (j=0; j<strlen(in); j+=4) {
57     char a[4], b[4];
58
59     for (i = 0; i < 4; i++) {
60       int c = in[i+j];
61
62       if (dtable[c] & 0x80) {
63         printf("Illegal character '%c' in input.\n", c);
64         exit(1);
65       }
66       a[i] = (char) c;
67       b[i] = (char) dtable[c];
68     }
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);
74     if (i < 3) {
75       out[k]=0;
76       *size=k;
77       return out;
78     }
79   }
80   out[k]=0;
81   *size=k;
82   return out;
83 }
84
85 static char *nl(char *data) {
86
87   char *nlptr = (data) ? strchr(data,'\n') : NULL;
88   return (nlptr) ? nlptr + 1 : NULL;
89 }
90
91 static int filter(const char *in, const char *filter, char **out) {
92
93   int flen=strlen(filter);
94   int len;
95
96   if (!in) return 0;
97
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);
104     (*out)[len-flen]=0;
105     return len-flen;
106   }
107   return 0;
108 }
109
110 static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
111
112   sdpplin_stream_t *desc = malloc(sizeof(sdpplin_stream_t));
113   char      *buf = malloc(32000);
114   char      *decoded = malloc(32000);
115   int       handled;
116
117   if( !desc ) return NULL;
118   memset(desc, 0, sizeof(sdpplin_stream_t));
119
120   if( !buf ) goto error;
121   if( !decoded ) goto error;
122
123   if (filter(*data, "m=", &buf)) {
124     desc->id = strdup(buf);
125   } else {
126     lprintf("sdpplin: no m= found.\n");
127     goto error;
128   }
129   *data=nl(*data);
130
131   while (*data && **data && *data[0]!='m') {
132     handled=0;
133
134     if(filter(*data,"a=control:streamid=",&buf)) {
135       desc->stream_id=atoi(buf);
136       handled=1;
137       *data=nl(*data);
138     }
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;
143       handled=1;
144       *data=nl(*data);
145     }
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;
150       handled=1;
151       *data=nl(*data);
152     }
153     if(filter(*data,"a=StartTime:integer;",&buf)) {
154       desc->start_time=atoi(buf);
155       handled=1;
156       *data=nl(*data);
157     }
158     if(filter(*data,"a=Preroll:integer;",&buf)) {
159       desc->preroll=atoi(buf);
160       handled=1;
161       *data=nl(*data);
162     }
163     if(filter(*data,"a=length:npt=",&buf)) {
164       desc->duration=(uint32_t)(atof(buf)*1000);
165       handled=1;
166       *data=nl(*data);
167     }
168     if(filter(*data,"a=StreamName:string;",&buf)) {
169       desc->stream_name=strdup(buf);
170       desc->stream_name_size=strlen(desc->stream_name);
171       handled=1;
172       *data=nl(*data);
173     }
174     if(filter(*data,"a=mimetype:string;",&buf)) {
175       desc->mime_type=strdup(buf);
176       desc->mime_type_size=strlen(desc->mime_type);
177       handled=1;
178       *data=nl(*data);
179     }
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);
184       handled=1;
185       *data=nl(*data);
186       lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
187     }
188     if(filter(*data,"a=ASMRuleBook:string;",&buf)) {
189       desc->asm_rule_book=strdup(buf);
190       handled=1;
191       *data=nl(*data);
192     }
193
194     if(!handled) {
195 #ifdef LOG
196       int len=strchr(*data,'\n')-(*data);
197       memcpy(buf, *data, len+1);
198       buf[len]=0;
199       printf("libreal: sdpplin: not handled: '%s'\n", buf);
200 #endif
201       *data=nl(*data);
202     }
203   }
204   if( buf ) free(buf);
205   if( decoded )free(decoded);
206   return desc;
207
208 error:
209   if( decoded ) free(decoded);
210   if( desc ) free( desc );
211   if( buf ) free( buf );
212   return NULL;
213 }
214
215 sdpplin_t *sdpplin_parse(char *data) {
216
217   sdpplin_t        *desc = malloc(sizeof(sdpplin_t));
218   sdpplin_stream_t *stream;
219   char             *buf=malloc(3200);
220   char             *decoded=malloc(3200);
221   int              handled;
222   int              len;
223
224   if( !desc ) return NULL;
225   if( !buf ) {
226     free( desc );
227     return NULL;
228   }
229   if( !decoded ) {
230     free( buf );
231     free( desc );
232     return NULL;
233   }
234   memset(desc, 0, sizeof(sdpplin_t));
235
236   while (data && *data) {
237     handled=0;
238
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;
243       continue;
244     }
245     if(filter(data,"a=Title:buffer;",&buf)) {
246       decoded=b64_decode(buf, decoded, &len);
247       desc->title=strdup(decoded);
248       handled=1;
249       data=nl(data);
250     }
251     if(filter(data,"a=Author:buffer;",&buf)) {
252       decoded=b64_decode(buf, decoded, &len);
253       desc->author=strdup(decoded);
254       handled=1;
255       data=nl(data);
256     }
257     if(filter(data,"a=Copyright:buffer;",&buf)) {
258       decoded=b64_decode(buf, decoded, &len);
259       desc->copyright=strdup(decoded);
260       handled=1;
261       data=nl(data);
262     }
263     if(filter(data,"a=Abstract:buffer;",&buf)) {
264       decoded=b64_decode(buf, decoded, &len);
265       desc->abstract=strdup(decoded);
266       handled=1;
267       data=nl(data);
268     }
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);
272       handled=1;
273       data=nl(data);
274     }
275     if(filter(data,"a=Flags:integer;",&buf)) {
276       desc->flags=atoi(buf);
277       handled=1;
278       data=nl(data);
279     }
280
281     if(!handled) {
282 #ifdef LOG
283       int len=strchr(data,'\n')-data;
284       memcpy(buf, data, len+1);
285       buf[len]=0;
286       printf("libreal: sdpplin: not handled: '%s'\n", buf);
287 #endif
288       data=nl(data);
289     }
290   }
291
292   free(decoded);
293   free(buf);
294   return desc;
295 }
296
297 void sdpplin_free(sdpplin_t *description) {
298
299   int i;
300
301   if( !description ) return;
302
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] );
317     }
318   }
319   if( description->stream_count ) free( description->stream );
320
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 );
336   free(description);
337 }