]> git.sesse.net Git - vlc/blob - modules/access/rtsp/real_sdpplin.c
Another time "Remove useless test before a free".
[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 < 255; 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   for (j=0; j<strlen(in); j+=4) {
58     char a[4], b[4];
59
60     for (i = 0; i < 4; i++) {
61       int c = in[i+j];
62
63       if (dtable[c] & 0x80) {
64         printf("Illegal character '%c' in input.\n", c);
65         exit(1);
66       }
67       a[i] = (char) c;
68       b[i] = (char) dtable[c];
69     }
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);
75     if (i < 3) {
76       out[k]=0;
77       *size=k;
78       return out;
79     }
80   }
81   out[k]=0;
82   *size=k;
83   return out;
84 }
85
86 static char *nl(char *data) {
87
88   char *nlptr = (data) ? strchr(data,'\n') : NULL;
89   return (nlptr) ? nlptr + 1 : NULL;
90 }
91
92 static int filter(const char *in, const char *filter, char **out, size_t outlen) {
93
94   int flen=strlen(filter);
95   size_t len;
96
97   if (!in) return 0;
98
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 )
105     {
106         printf("Discarding end of string to avoid overflow");
107         len=outlen+flen-1;
108     }
109     memcpy(*out, in+flen, len-flen+1);
110     (*out)[len-flen]=0;
111     return len-flen;
112   }
113   return 0;
114 }
115
116 static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
117
118   sdpplin_stream_t *desc = malloc(sizeof(sdpplin_stream_t));
119   char      *buf = malloc(BUFLEN);
120   char      *decoded = malloc(BUFLEN);
121   int       handled;
122
123   if( !desc ) return NULL;
124   memset(desc, 0, sizeof(sdpplin_stream_t));
125
126   if( !buf ) goto error;
127   if( !decoded ) goto error;
128
129   if (filter(*data, "m=", &buf, BUFLEN)) {
130     desc->id = strdup(buf);
131   } else {
132     lprintf("sdpplin: no m= found.\n");
133     goto error;
134   }
135   *data=nl(*data);
136
137   while (*data && **data && *data[0]!='m') {
138     handled=0;
139
140     if(filter(*data,"a=control:streamid=",&buf, BUFLEN)) {
141       desc->stream_id=atoi(buf);
142       handled=1;
143       *data=nl(*data);
144     }
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;
149       handled=1;
150       *data=nl(*data);
151     }
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;
156       handled=1;
157       *data=nl(*data);
158     }
159     if(filter(*data,"a=StartTime:integer;",&buf, BUFLEN)) {
160       desc->start_time=atoi(buf);
161       handled=1;
162       *data=nl(*data);
163     }
164     if(filter(*data,"a=Preroll:integer;",&buf, BUFLEN)) {
165       desc->preroll=atoi(buf);
166       handled=1;
167       *data=nl(*data);
168     }
169     if(filter(*data,"a=length:npt=",&buf, BUFLEN)) {
170       desc->duration=(uint32_t)(atof(buf)*1000);
171       handled=1;
172       *data=nl(*data);
173     }
174     if(filter(*data,"a=StreamName:string;",&buf, BUFLEN)) {
175       desc->stream_name=strdup(buf);
176       desc->stream_name_size=strlen(desc->stream_name);
177       handled=1;
178       *data=nl(*data);
179     }
180     if(filter(*data,"a=mimetype:string;",&buf, BUFLEN)) {
181       desc->mime_type=strdup(buf);
182       desc->mime_type_size=strlen(desc->mime_type);
183       handled=1;
184       *data=nl(*data);
185     }
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);
191           handled=1;
192           *data=nl(*data);
193           lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
194       }
195     }
196     if(filter(*data,"a=ASMRuleBook:string;",&buf, BUFLEN)) {
197       desc->asm_rule_book=strdup(buf);
198       handled=1;
199       *data=nl(*data);
200     }
201
202     if(!handled) {
203 #ifdef LOG
204       int len=strchr(*data,'\n')-(*data);
205       memcpy(buf, *data, len+1);
206       buf[len]=0;
207       printf("libreal: sdpplin: not handled: '%s'\n", buf);
208 #endif
209       *data=nl(*data);
210     }
211   }
212   free( buf );
213   free( decoded) ;
214   return desc;
215
216 error:
217   free( decoded );
218   free( desc );
219   free( buf );
220   return NULL;
221 }
222
223 sdpplin_t *sdpplin_parse(char *data) {
224
225   sdpplin_t        *desc = malloc(sizeof(sdpplin_t));
226   sdpplin_stream_t *stream;
227   char             *buf=malloc(BUFLEN);
228   char             *decoded=malloc(BUFLEN);
229   int              handled;
230   int              len;
231
232   if( !desc ) return NULL;
233   if( !buf ) {
234     free( desc );
235     return NULL;
236   }
237   if( !decoded ) {
238     free( buf );
239     free( desc );
240     return NULL;
241   }
242
243   desc->stream = NULL;
244
245   memset(desc, 0, sizeof(sdpplin_t));
246
247   while (data && *data) {
248     handled=0;
249
250     if (filter(data, "m=", &buf, BUFLEN)) {
251         if ( !desc->stream ) {
252             fprintf(stderr, "sdpplin.c: stream identifier found before stream count, skipping.");
253             continue;
254         }
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;
258         continue;
259     }
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);
264           handled=1;
265           data=nl(data);
266       }
267     }
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);
272           handled=1;
273           data=nl(data);
274       }
275     }
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);
280           handled=1;
281           data=nl(data);
282       }
283     }
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);
288            handled=1;
289            data=nl(data);
290       }
291     }
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);
295       handled=1;
296       data=nl(data);
297     }
298     if(filter(data,"a=Flags:integer;",&buf, BUFLEN)) {
299       desc->flags=atoi(buf);
300       handled=1;
301       data=nl(data);
302     }
303
304     if(!handled) {
305 #ifdef LOG
306       int len=strchr(data,'\n')-data;
307       memcpy(buf, data, len+1);
308       buf[len]=0;
309       printf("libreal: sdpplin: not handled: '%s'\n", buf);
310 #endif
311       data=nl(data);
312     }
313   }
314
315   free( decoded );
316   free( buf );
317   return desc;
318 }
319
320 void sdpplin_free(sdpplin_t *description) {
321
322   int i;
323
324   if( !description ) return;
325
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] );
340     }
341   }
342   if( description->stream_count ) free( description->stream );
343
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 );
359   free(description);
360 }