real_sdpplin.c 10.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 2002-2003 the xine project
 *
 * This file is part of xine, a free video player.
 *
 * xine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * xine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
20
 * $Id$
21
22
23
24
25
26
 *
 * sdp/sdpplin parser.
 *
 */
 
#include "real.h"
27
#define BUFLEN 32000
28
29
30
31
32

/*
 * Decodes base64 strings (based upon b64 package)
 */

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static char *b64_decode(const char *in, char *out, int *size) {

  char dtable[256];              /* Encode / decode table */
  int i,k;
  unsigned int j;

  for (i = 0; i < 255; i++) {
    dtable[i] = 0x80;
  }
  for (i = 'A'; i <= 'Z'; i++) {
    dtable[i] = 0 + (i - 'A');
  }
  for (i = 'a'; i <= 'z'; i++) {
    dtable[i] = 26 + (i - 'a');
  }
  for (i = '0'; i <= '9'; i++) {
    dtable[i] = 52 + (i - '0');
  }
  dtable['+'] = 62;
  dtable['/'] = 63;
  dtable['='] = 0;

  k=0;
  /*CONSTANTCONDITION*/
  for (j=0; j<strlen(in); j+=4) {
    char a[4], b[4];

    for (i = 0; i < 4; i++) {
      int c = in[i+j];

      if (dtable[c] & 0x80) {
        printf("Illegal character '%c' in input.\n", c);
        exit(1);
      }
      a[i] = (char) c;
      b[i] = (char) dtable[c];
69
    }
70
71
72
73
74
75
76
77
78
    //xine_buffer_ensure_size(out, k+3);
    out[k++] = (b[0] << 2) | (b[1] >> 4);
    out[k++] = (b[1] << 4) | (b[2] >> 2);
    out[k++] = (b[2] << 6) | b[3];
    i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
    if (i < 3) {
      out[k]=0;
      *size=k;
      return out;
79
    }
80
81
82
83
  }
  out[k]=0;
  *size=k;
  return out;
84
85
}

86
87
88
89
static char *nl(char *data) {

  char *nlptr = (data) ? strchr(data,'\n') : NULL;
  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
98

  if (!in) return 0;

Konstantin Pavlov's avatar
Konstantin Pavlov committed
99
  len = (strchr(in,'\n')) ? (size_t)(strchr(in,'\n')-in) : strlen(in);
100
101
102
103
  if (!strncmp(in,filter,flen)) {
    if(in[flen]=='"') flen++;
    if(in[len-1]==13) len--;
    if(in[len-1]=='"') len--;
104
105
106
107
108
    if( len-flen+1 > outlen )
    {
        printf("Discarding end of string to avoid overflow");
        len=outlen+flen-1;
    }
109
110
111
112
113
    memcpy(*out, in+flen, len-flen+1);
    (*out)[len-flen]=0;
    return len-flen;
  }
  return 0;
114
115
}

116
117
118
static sdpplin_stream_t *sdpplin_parse_stream(char **data) {

  sdpplin_stream_t *desc = malloc(sizeof(sdpplin_stream_t));
119
120
  char      *buf = malloc(BUFLEN);
  char      *decoded = malloc(BUFLEN);
121
122
123
124
125
126
127
  int       handled;

  if( !desc ) return NULL;
  memset(desc, 0, sizeof(sdpplin_stream_t));

  if( !buf ) goto error;
  if( !decoded ) goto error;
128

129
  if (filter(*data, "m=", &buf, BUFLEN)) {
130
131
132
133
134
135
    desc->id = strdup(buf);
  } else {
    lprintf("sdpplin: no m= found.\n");
    goto error;
  }
  *data=nl(*data);
136

137
138
  while (*data && **data && *data[0]!='m') {
    handled=0;
139

140
    if(filter(*data,"a=control:streamid=",&buf, BUFLEN)) {
141
142
143
144
      desc->stream_id=atoi(buf);
      handled=1;
      *data=nl(*data);
    }
145
    if(filter(*data,"a=MaxBitRate:integer;",&buf, BUFLEN)) {
146
147
148
149
150
151
      desc->max_bit_rate=atoi(buf);
      if (!desc->avg_bit_rate)
        desc->avg_bit_rate=desc->max_bit_rate;
      handled=1;
      *data=nl(*data);
    }
152
    if(filter(*data,"a=MaxPacketSize:integer;",&buf, BUFLEN)) {
153
154
155
156
157
158
      desc->max_packet_size=atoi(buf);
      if (!desc->avg_packet_size)
        desc->avg_packet_size=desc->max_packet_size;
      handled=1;
      *data=nl(*data);
    }
159
    if(filter(*data,"a=StartTime:integer;",&buf, BUFLEN)) {
160
161
162
163
      desc->start_time=atoi(buf);
      handled=1;
      *data=nl(*data);
    }
164
    if(filter(*data,"a=Preroll:integer;",&buf, BUFLEN)) {
165
166
167
168
      desc->preroll=atoi(buf);
      handled=1;
      *data=nl(*data);
    }
169
    if(filter(*data,"a=length:npt=",&buf, BUFLEN)) {
170
171
172
      desc->duration=(uint32_t)(atof(buf)*1000);
      handled=1;
      *data=nl(*data);
173
    }
174
    if(filter(*data,"a=StreamName:string;",&buf, BUFLEN)) {
175
176
177
178
      desc->stream_name=strdup(buf);
      desc->stream_name_size=strlen(desc->stream_name);
      handled=1;
      *data=nl(*data);
179
    }
180
    if(filter(*data,"a=mimetype:string;",&buf, BUFLEN)) {
181
182
183
184
185
      desc->mime_type=strdup(buf);
      desc->mime_type_size=strlen(desc->mime_type);
      handled=1;
      *data=nl(*data);
    }
186
    if(filter(*data,"a=OpaqueData:buffer;",&buf, BUFLEN)) {
187
      decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
188
189
190
191
192
193
194
      if ( decoded != NULL ) {
          desc->mlti_data = malloc(sizeof(char)*desc->mlti_data_size);
          memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
          handled=1;
          *data=nl(*data);
          lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
      }
195
    }
196
    if(filter(*data,"a=ASMRuleBook:string;",&buf, BUFLEN)) {
197
198
199
200
201
202
      desc->asm_rule_book=strdup(buf);
      handled=1;
      *data=nl(*data);
    }

    if(!handled) {
203
#ifdef LOG
204
205
206
207
      int len=strchr(*data,'\n')-(*data);
      memcpy(buf, *data, len+1);
      buf[len]=0;
      printf("libreal: sdpplin: not handled: '%s'\n", buf);
208
#endif
209
      *data=nl(*data);
210
    }
211
212
213
214
  }
  if( buf ) free(buf);
  if( decoded )free(decoded);
  return desc;
215
216

error:
217
218
219
220
  if( decoded ) free(decoded);
  if( desc ) free( desc );
  if( buf ) free( buf );
  return NULL;
221
222
}

223
224
225
226
sdpplin_t *sdpplin_parse(char *data) {

  sdpplin_t        *desc = malloc(sizeof(sdpplin_t));
  sdpplin_stream_t *stream;
227
228
  char             *buf=malloc(BUFLEN);
  char             *decoded=malloc(BUFLEN);
229
230
231
232
233
234
235
236
237
238
239
240
241
  int              handled;
  int              len;

  if( !desc ) return NULL;
  if( !buf ) {
    free( desc );
    return NULL;
  }
  if( !decoded ) {
    free( buf );
    free( desc );
    return NULL;
  }
242
243
244

  desc->stream = NULL;

245
246
247
248
249
  memset(desc, 0, sizeof(sdpplin_t));

  while (data && *data) {
    handled=0;

250
    if (filter(data, "m=", &buf, BUFLEN)) {
251
252
253
254
255
256
257
258
        if ( !desc->stream ) {
            fprintf(stderr, "sdpplin.c: stream identifier found before stream count, skipping.");
            continue;
        }
        stream=sdpplin_parse_stream(&data);
        lprintf("got data for stream id %u\n", stream->stream_id);
        desc->stream[stream->stream_id]=stream;
        continue;
259
    }
260
    if(filter(data,"a=Title:buffer;",&buf, BUFLEN)) {
261
      decoded=b64_decode(buf, decoded, &len);
262
263
264
265
266
	  if ( decoded != NULL ) {
          desc->title=strdup(decoded);
          handled=1;
          data=nl(data);
      }
267
    }
268
    if(filter(data,"a=Author:buffer;",&buf, BUFLEN)) {
269
      decoded=b64_decode(buf, decoded, &len);
270
271
272
273
274
	  if ( decoded != NULL ) {
          desc->author=strdup(decoded);
          handled=1;
          data=nl(data);
      }
275
    }
276
    if(filter(data,"a=Copyright:buffer;",&buf, BUFLEN)) {
277
      decoded=b64_decode(buf, decoded, &len);
278
279
280
281
282
	  if ( decoded != NULL ) {
          desc->copyright=strdup(decoded);
          handled=1;
          data=nl(data);
      }
283
    }
284
    if(filter(data,"a=Abstract:buffer;",&buf, BUFLEN)) {
285
      decoded=b64_decode(buf, decoded, &len);
286
287
288
289
290
      if ( decoded != NULL ) {
           desc->abstract=strdup(decoded);
           handled=1;
           data=nl(data);
      }
291
    }
292
    if(filter(data,"a=StreamCount:integer;",&buf, BUFLEN)) {
293
294
295
296
297
      desc->stream_count=atoi(buf);
      desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
      handled=1;
      data=nl(data);
    }
298
    if(filter(data,"a=Flags:integer;",&buf, BUFLEN)) {
299
300
301
302
303
304
      desc->flags=atoi(buf);
      handled=1;
      data=nl(data);
    }

    if(!handled) {
305
#ifdef LOG
306
307
308
309
      int len=strchr(data,'\n')-data;
      memcpy(buf, data, len+1);
      buf[len]=0;
      printf("libreal: sdpplin: not handled: '%s'\n", buf);
310
#endif
311
      data=nl(data);
312
    }
313
  }
314

315
316
317
  free(decoded);
  free(buf);
  return desc;
318
319
}

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
void sdpplin_free(sdpplin_t *description) {

  int i;

  if( !description ) return;

  for( i=0; i<description->stream_count; i++ ) {
    if( description->stream[i] ) {
      if( description->stream[i]->id ) free( description->stream[i]->id );
      if( description->stream[i]->bandwidth ) free( description->stream[i]->bandwidth );
      if( description->stream[i]->range ) free( description->stream[i]->range );
      if( description->stream[i]->length ) free( description->stream[i]->length );
      if( description->stream[i]->rtpmap ) free( description->stream[i]->rtpmap );
      if( description->stream[i]->mimetype ) free( description->stream[i]->mimetype );
      if( description->stream[i]->stream_name ) free( description->stream[i]->stream_name );
      if( description->stream[i]->mime_type ) free( description->stream[i]->mime_type );
      if( description->stream[i]->mlti_data ) free( description->stream[i]->mlti_data );
      if( description->stream[i]->rmff_flags ) free( description->stream[i]->rmff_flags );
      if( description->stream[i]->asm_rule_book ) free( description->stream[i]->asm_rule_book );
339
      free( description->stream[i] );
340
    }
341
  }
342
  if( description->stream_count ) free( description->stream );
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

  if( description->owner ) free( description->owner );
  if( description->session_name ) free( description->session_name );
  if( description->session_info ) free( description->session_info );
  if( description->uri ) free( description->uri );
  if( description->email ) free( description->email );
  if( description->phone ) free( description->phone );
  if( description->connection ) free( description->connection );
  if( description->bandwidth ) free( description->bandwidth );
  if( description->title ) free( description->title );
  if( description->author ) free( description->author );
  if( description->copyright ) free( description->copyright );
  if( description->keywords ) free( description->keywords );
  if( description->asm_rule_book ) free( description->asm_rule_book );
  if( description->abstract ) free( description->abstract );
  if( description->range ) free( description->range );
  free(description);
360
}