]> git.sesse.net Git - vlc/blob - modules/demux/asf/libasf.c
Fix a bunch of potential segmentation faults:
[vlc] / modules / demux / asf / libasf.c
1 /*****************************************************************************
2  * libasf.c : asf stream demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc_demux.h>
31
32 #include <vlc_codecs.h>                   /* BITMAPINFOHEADER, WAVEFORMATEX */
33 #include "libasf.h"
34
35 #define ASF_DEBUG 1
36
37 #define GUID_FMT "0x%x-0x%x-0x%x-0x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x"
38 #define GUID_PRINT( guid )  \
39     (guid).v1,              \
40     (guid).v2,              \
41     (guid).v3,              \
42     (guid).v4[0],(guid).v4[1],(guid).v4[2],(guid).v4[3],    \
43     (guid).v4[4],(guid).v4[5],(guid).v4[6],(guid).v4[7]
44
45 /****************************************************************************
46  *
47  ****************************************************************************/
48 static int ASF_ReadObject( stream_t *, asf_object_t *,  asf_object_t * );
49
50 /****************************************************************************
51  * GUID functions
52  ****************************************************************************/
53 void ASF_GetGUID( guid_t *p_guid, uint8_t *p_data )
54 {
55     p_guid->v1 = GetDWLE( p_data );
56     p_guid->v2 = GetWLE( p_data + 4);
57     p_guid->v3 = GetWLE( p_data + 6);
58     memcpy( p_guid->v4, p_data + 8, 8 );
59 }
60
61 int ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
62 {
63     if( (p_guid1->v1 != p_guid2->v1 )||
64         (p_guid1->v2 != p_guid2->v2 )||
65         (p_guid1->v3 != p_guid2->v3 )||
66         ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
67     {
68         return( 0 );
69     }
70     return( 1 ); /* match */
71 }
72
73 /****************************************************************************
74  *
75  ****************************************************************************/
76 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
77 {
78     asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
79     uint8_t             *p_peek;
80
81     if( stream_Peek( s, &p_peek, 24 ) < 24 )
82     {
83         return( VLC_EGENERIC );
84     }
85     ASF_GetGUID( &p_common->i_object_id, p_peek );
86     p_common->i_object_size = GetQWLE( p_peek + 16 );
87     p_common->i_object_pos  = stream_Tell( s );
88     p_common->p_next = NULL;
89
90 #ifdef ASF_DEBUG
91     msg_Dbg( s,
92              "found object guid: " GUID_FMT " size:"I64Fd,
93              GUID_PRINT( p_common->i_object_id ),
94              p_common->i_object_size );
95 #endif
96
97     return( VLC_SUCCESS );
98 }
99
100 static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
101 {
102     asf_object_t obj;
103     if( p_obj == NULL )
104     {
105         if( ASF_ReadObjectCommon( s, &obj ) )
106         {
107             return( VLC_EGENERIC );
108         }
109         p_obj = &obj;
110     }
111
112     if( p_obj->common.i_object_size <= 0 )
113     {
114         return VLC_EGENERIC;
115     }
116     if( p_obj->common.p_father &&
117         p_obj->common.p_father->common.i_object_size != 0 )
118     {
119         if( p_obj->common.p_father->common.i_object_pos +
120             p_obj->common.p_father->common.i_object_size <
121                 p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
122                                 /* 24 is min size of an object */
123         {
124             return VLC_EGENERIC;
125         }
126
127     }
128
129     return stream_Seek( s, p_obj->common.i_object_pos +
130                         p_obj->common.i_object_size );
131 }
132
133 static void ASF_FreeObject_Null( asf_object_t *pp_obj )
134 {
135     return;
136 }
137
138 static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
139 {
140     asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj;
141     asf_object_t        *p_subobj;
142     int                 i_peek;
143     uint8_t             *p_peek;
144
145     if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
146     {
147        return( VLC_EGENERIC );
148     }
149
150     p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
151     p_hdr->i_reserved1 = p_peek[28];
152     p_hdr->i_reserved2 = p_peek[29];
153     p_hdr->p_first = NULL;
154     p_hdr->p_last  = NULL;
155
156 #ifdef ASF_DEBUG
157     msg_Dbg( s,
158              "read \"header object\" subobj:%d, reserved1:%d, reserved2:%d",
159              p_hdr->i_sub_object_count,
160              p_hdr->i_reserved1,
161              p_hdr->i_reserved2 );
162 #endif
163
164     /* Cannot fail as peek succeed */
165     stream_Read( s, NULL, 30 );
166
167     /* Now load sub object */
168     for( ; ; )
169     {
170         p_subobj = malloc( sizeof( asf_object_t ) );
171
172         if( ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
173         {
174             free( p_subobj );
175             break;
176         }
177         if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
178         {
179             break;
180         }
181     }
182     return VLC_SUCCESS;
183 }
184
185 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
186 {
187     asf_object_data_t *p_data = (asf_object_data_t*)p_obj;
188     int               i_peek;
189     uint8_t           *p_peek;
190
191     if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
192     {
193        return VLC_EGENERIC;
194     }
195     ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
196     p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
197     p_data->i_reserved = GetWLE( p_peek + 48 );
198
199 #ifdef ASF_DEBUG
200     msg_Dbg( s,
201              "read \"data object\" file_id:" GUID_FMT " total data packet:"
202              I64Fd" reserved:%d",
203              GUID_PRINT( p_data->i_file_id ),
204              p_data->i_total_data_packets,
205              p_data->i_reserved );
206 #endif
207
208     return VLC_SUCCESS;
209 }
210
211 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
212 {
213     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
214     int                i_peek;
215     uint8_t            *p_peek;
216
217     if( ( i_peek = stream_Peek( s, &p_peek, 56 ) ) < 56 )
218     {
219        return VLC_EGENERIC;
220     }
221     ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
222     p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
223     p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
224     p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
225     p_index->index_entry = NULL; /* FIXME */
226
227 #ifdef ASF_DEBUG
228     msg_Dbg( s,
229             "read \"index object\" file_id:" GUID_FMT
230             " index_entry_time_interval:"I64Fd" max_packet_count:%d "
231             "index_entry_count:%ld",
232             GUID_PRINT( p_index->i_file_id ),
233             p_index->i_index_entry_time_interval,
234             p_index->i_max_packet_count,
235             (long int)p_index->i_index_entry_count );
236 #endif
237
238     return VLC_SUCCESS;
239 }
240
241 static void ASF_FreeObject_Index( asf_object_t *p_obj )
242 {
243     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
244
245     FREENULL( p_index->index_entry );
246 }
247
248 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
249 {
250     asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
251     int      i_peek;
252     uint8_t  *p_peek;
253
254     if( ( i_peek = stream_Peek( s, &p_peek,  92) ) < 92 )
255     {
256        return VLC_EGENERIC;
257     }
258     ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
259     p_fp->i_file_size = GetQWLE( p_peek + 40 );
260     p_fp->i_creation_date = GetQWLE( p_peek + 48 );
261     p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
262     p_fp->i_play_duration = GetQWLE( p_peek + 64 );
263     p_fp->i_send_duration = GetQWLE( p_peek + 72 );
264     p_fp->i_preroll = GetQWLE( p_peek + 80 );
265     p_fp->i_flags = GetDWLE( p_peek + 88 );
266     p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
267     p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
268     p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
269
270 #ifdef ASF_DEBUG
271     msg_Dbg( s,
272             "read \"file properties object\" file_id:" GUID_FMT
273             " file_size:"I64Fd" creation_date:"I64Fd" data_packets_count:"
274             I64Fd" play_duration:"I64Fd" send_duration:"I64Fd" preroll:"
275             I64Fd" flags:%d min_data_packet_size:%d max_data_packet_size:%d "
276             "max_bitrate:%d",
277             GUID_PRINT( p_fp->i_file_id ), p_fp->i_file_size,
278             p_fp->i_creation_date, p_fp->i_data_packets_count,
279             p_fp->i_play_duration, p_fp->i_send_duration,
280             p_fp->i_preroll, p_fp->i_flags,
281             p_fp->i_min_data_packet_size, p_fp->i_max_data_packet_size,
282             p_fp->i_max_bitrate );
283 #endif
284
285     return VLC_SUCCESS;
286 }
287
288 static void ASF_FreeObject_metadata( asf_object_t *p_obj )
289 {
290     asf_object_metadata_t *p_meta =
291         (asf_object_metadata_t *)p_obj;
292     unsigned int i;
293
294     for( i = 0; i < p_meta->i_record_entries_count; i++ )
295     {
296         if( p_meta->record[i].psz_name ) free( p_meta->record[i].psz_name );
297         if( p_meta->record[i].p_data ) free( p_meta->record[i].p_data );
298     }
299     if( p_meta->record ) free( p_meta->record );
300 }
301
302 static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
303 {
304     asf_object_metadata_t *p_meta =
305         (asf_object_metadata_t *)p_obj;
306
307     int i_peek, i_entries, i;
308     uint8_t *p_peek;
309 #ifdef ASF_DEBUG
310     unsigned int j;
311 #endif
312
313     p_meta->i_record_entries_count = 0;
314     p_meta->record = 0;
315
316     if( stream_Peek( s, &p_peek, p_meta->i_object_size ) <
317         (int)p_meta->i_object_size )
318     {
319        return VLC_EGENERIC;
320     }
321
322     i_peek = 24;
323     i_entries = GetWLE( p_peek + i_peek ); i_peek += 2;
324     for( i = 0; i < i_entries && i_peek < (int)p_meta->i_object_size -12; i++ )
325     {
326         asf_metadata_record_t record;
327         int i_name, i_data, j;
328
329         if( GetWLE( p_peek + i_peek ) != 0 )
330         {
331             ASF_FreeObject_metadata( p_obj );
332             return VLC_EGENERIC;
333         }
334
335         i_peek += 2;
336         record.i_stream = GetWLE( p_peek + i_peek ); i_peek += 2;
337         i_name = GetWLE( p_peek + i_peek ); i_peek += 2;
338         record.i_type = GetWLE( p_peek + i_peek ); i_peek += 2;
339         i_data = GetDWLE( p_peek + i_peek ); i_peek += 4;
340
341         if( record.i_type > ASF_METADATA_TYPE_WORD ||
342             i_peek + i_data + i_name > (int)p_meta->i_object_size )
343         {
344             ASF_FreeObject_metadata( p_obj );
345             return VLC_EGENERIC;
346         }
347
348         record.i_val = 0;
349         record.i_data = 0;
350         record.p_data = 0;
351
352         /* get name */
353         record.psz_name = malloc( i_name/2 + 1 );
354         for( j = 0; j < i_name/2; j++ )
355         {
356             record.psz_name[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
357         }
358         record.psz_name[j] = 0; /* just to make sure */
359
360         /* get data */
361         if( record.i_type == ASF_METADATA_TYPE_STRING )
362         {
363             record.p_data = malloc( i_data/2 + 1 );
364             record.i_data = i_data/2;
365             for( j = 0; j < i_data/2; j++ )
366             {
367                 record.p_data[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
368             }
369             record.p_data[j] = 0; /* just to make sure */
370         }
371         else if( record.i_type == ASF_METADATA_TYPE_BYTE )
372         {
373             record.p_data = malloc( i_data );
374             record.i_data = i_data;
375             memcpy( record.p_data, p_peek + i_peek, i_data );
376             p_peek += i_data;
377         }
378         else
379         {
380             if( record.i_type == ASF_METADATA_TYPE_QWORD )
381             {
382                 record.i_val = GetQWLE( p_peek + i_peek ); i_peek += 8;
383             }
384             else if( record.i_type == ASF_METADATA_TYPE_DWORD )
385             {
386                 record.i_val = GetDWLE( p_peek + i_peek ); i_peek += 4;
387             }
388             else
389             {
390                 record.i_val = GetWLE( p_peek + i_peek ); i_peek += 2;
391             }
392         }
393
394         p_meta->i_record_entries_count++;
395         p_meta->record =
396             realloc( p_meta->record, p_meta->i_record_entries_count *
397                      sizeof(asf_metadata_record_t) );
398         memcpy( &p_meta->record[p_meta->i_record_entries_count-1],
399                 &record, sizeof(asf_metadata_record_t) );
400     }
401
402 #ifdef ASF_DEBUG
403     msg_Dbg( s,
404             "read \"metadata object\" %d entries",
405             p_meta->i_record_entries_count );
406     for( j = 0; j < p_meta->i_record_entries_count; j++ )
407     {
408         asf_metadata_record_t *p_rec = &p_meta->record[j];
409
410         if( p_rec->i_type == ASF_METADATA_TYPE_STRING )
411             msg_Dbg( s, "  - %s=%s",
412                      p_rec->psz_name, p_rec->p_data );
413         else if( p_rec->i_type == ASF_METADATA_TYPE_BYTE )
414             msg_Dbg( s, "  - %s (%i bytes)",
415                      p_rec->psz_name, p_rec->i_data );
416         else
417             msg_Dbg( s, "  - %s="I64Fd,
418                      p_rec->psz_name, p_rec->i_val );
419     }
420 #endif
421
422     return VLC_SUCCESS;
423 }
424
425 static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
426 {
427     asf_object_header_extension_t *p_he =
428         (asf_object_header_extension_t *)p_obj;
429     int     i_peek;
430     uint8_t *p_peek;
431
432     if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) <  46)
433     {
434        return VLC_EGENERIC;
435     }
436     ASF_GetGUID( &p_he->i_reserved1, p_peek + 24 );
437     p_he->i_reserved2 = GetWLE( p_peek + 40 );
438     p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
439     if( p_he->i_header_extension_size )
440     {
441         p_he->p_header_extension_data =
442             malloc( p_he->i_header_extension_size );
443         memcpy( p_he->p_header_extension_data, p_peek + 46,
444                 p_he->i_header_extension_size );
445     }
446     else
447     {
448         p_he->p_header_extension_data = NULL;
449     }
450
451 #ifdef ASF_DEBUG
452     msg_Dbg( s,
453             "read \"header extension object\" reserved1:" GUID_FMT
454             " reserved2:%d header_extension_size:%d",
455             GUID_PRINT( p_he->i_reserved1 ), p_he->i_reserved2,
456             p_he->i_header_extension_size );
457 #endif
458
459     if( !p_he->i_header_extension_size ) return VLC_SUCCESS;
460
461     /* Read the extension objects */
462     stream_Read( s, NULL, 46 );
463     for( ; ; )
464     {
465         asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );
466
467         if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
468         {
469             free( p_obj );
470             break;
471         }
472
473         if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
474         {
475             break;
476         }
477     }
478
479     return VLC_SUCCESS;
480 }
481
482 static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
483 {
484     asf_object_header_extension_t *p_he =
485         (asf_object_header_extension_t *)p_obj;
486
487     FREENULL( p_he->p_header_extension_data );
488 }
489
490 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
491 {
492     asf_object_stream_properties_t *p_sp =
493                     (asf_object_stream_properties_t*)p_obj;
494     size_t   i_peek;
495     uint8_t *p_peek;
496
497     if( ( i_peek = stream_Peek( s, &p_peek,  p_sp->i_object_size ) ) < 78 )
498     {
499        return VLC_EGENERIC;
500     }
501
502     ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
503     ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
504     p_sp->i_time_offset = GetQWLE( p_peek + 56 );
505     p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
506     p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
507     p_sp->i_flags = GetWLE( p_peek + 72 );
508         p_sp->i_stream_number = p_sp->i_flags&0x07f;
509     p_sp->i_reserved = GetDWLE( p_peek + 74 );
510     i_peek -= 78;
511
512     if( p_sp->i_type_specific_data_length )
513     {
514         if( i_peek < p_sp->i_type_specific_data_length )
515             return VLC_EGENERIC;
516
517         p_sp->p_type_specific_data =
518             malloc( p_sp->i_type_specific_data_length );
519         if( p_sp->p_type_specific_data == NULL )
520             return VLC_ENOMEM;
521
522         memcpy( p_sp->p_type_specific_data, p_peek + 78,
523                 p_sp->i_type_specific_data_length );
524         i_peek -= p_sp->i_type_specific_data_length;
525     }
526     else
527     {
528         p_sp->p_type_specific_data = NULL;
529     }
530
531     if( p_sp->i_error_correction_data_length )
532     {
533         if( i_peek < p_sp->i_error_correction_data_length )
534         {
535             free( p_sp->p_type_specific_data );
536             return VLC_EGENERIC;
537         }
538
539         p_sp->p_error_correction_data =
540             malloc( p_sp->i_error_correction_data_length );
541         if( p_sp->p_error_correction_data == NULL )
542         {
543             free( p_sp->p_type_specific_data );
544             return VLC_ENOMEM;
545         }
546         memcpy( p_sp->p_error_correction_data,
547                 p_peek + 78 + p_sp->i_type_specific_data_length,
548                 p_sp->i_error_correction_data_length );
549     }
550     else
551     {
552         p_sp->p_error_correction_data = NULL;
553     }
554
555 #ifdef ASF_DEBUG
556     msg_Dbg( s,
557             "read \"stream Properties object\" stream_type:" GUID_FMT
558             " error_correction_type:" GUID_FMT " time_offset:"I64Fd
559             " type_specific_data_length:%d error_correction_data_length:%d"
560             " flags:0x%x stream_number:%d",
561             GUID_PRINT( p_sp->i_stream_type ),
562             GUID_PRINT( p_sp->i_error_correction_type ),
563             p_sp->i_time_offset,
564             p_sp->i_type_specific_data_length,
565             p_sp->i_error_correction_data_length,
566             p_sp->i_flags,
567             p_sp->i_stream_number );
568
569 #endif
570     return VLC_SUCCESS;
571 }
572
573 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
574 {
575     asf_object_stream_properties_t *p_sp =
576                 (asf_object_stream_properties_t*)p_obj;
577
578     FREENULL( p_sp->p_type_specific_data );
579     FREENULL( p_sp->p_error_correction_data );
580 }
581
582
583 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
584 {
585     asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
586     int     i_peek;
587     uint8_t *p_peek, *p_data;
588
589     unsigned int i_codec;
590
591     if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
592     {
593        return VLC_EGENERIC;
594     }
595
596     ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
597     p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
598     if( p_cl->i_codec_entries_count > 0 )
599     {
600         p_cl->codec = calloc( p_cl->i_codec_entries_count,
601                               sizeof( asf_codec_entry_t ) );
602         memset( p_cl->codec, 0,
603                 p_cl->i_codec_entries_count * sizeof( asf_codec_entry_t ) );
604
605         p_data = p_peek + 44;
606         for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
607         {
608 #define codec p_cl->codec[i_codec]
609             int i_len, i;
610
611             codec.i_type = GetWLE( p_data ); p_data += 2;
612             /* codec name */
613             i_len = GetWLE( p_data ); p_data += 2;
614             codec.psz_name = calloc( sizeof( char ), i_len + 1);
615             for( i = 0; i < i_len; i++ )
616             {
617                 codec.psz_name[i] = GetWLE( p_data + 2*i );
618             }
619             codec.psz_name[i_len] = '\0';
620             p_data += 2 * i_len;
621
622             /* description */
623             i_len = GetWLE( p_data ); p_data += 2;
624             codec.psz_description = calloc( sizeof( char ), i_len + 1);
625             for( i = 0; i < i_len; i++ )
626             {
627                 codec.psz_description[i] = GetWLE( p_data + 2*i );
628             }
629             codec.psz_description[i_len] = '\0';
630             p_data += 2 * i_len;
631
632             /* opaque information */
633             codec.i_information_length = GetWLE( p_data ); p_data += 2;
634             if( codec.i_information_length > 0 )
635             {
636                 codec.p_information = malloc( codec.i_information_length );
637                 memcpy( codec.p_information, p_data, codec.i_information_length );
638                 p_data += codec.i_information_length;
639             }
640             else
641             {
642                 codec.p_information = NULL;
643             }
644 #undef  codec
645         }
646     }
647     else
648     {
649         p_cl->codec = NULL;
650     }
651
652 #ifdef ASF_DEBUG
653     msg_Dbg( s, "read \"codec list object\" reserved_guid:" GUID_FMT
654              " codec_entries_count:%d",
655             GUID_PRINT( p_cl->i_reserved ), p_cl->i_codec_entries_count );
656
657     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
658     {
659 #define codec p_cl->codec[i_codec]
660         msg_Dbg( s, "  - codec[%d] %s name:\"%s\" "
661                  "description:\"%s\" information_length:%d",
662                  i_codec, ( codec.i_type == ASF_CODEC_TYPE_VIDEO ) ?
663                  "video" : ( ( codec.i_type == ASF_CODEC_TYPE_AUDIO ) ?
664                  "audio" : "unknown" ),
665                  codec.psz_name, codec.psz_description,
666                  codec.i_information_length );
667 #undef  codec
668     }
669 #endif
670
671     return VLC_SUCCESS;
672 }
673
674 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
675 {
676     asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
677     unsigned int i_codec;
678
679     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
680     {
681 #define codec p_cl->codec[i_codec]
682         FREENULL( codec.psz_name );
683         FREENULL( codec.psz_description );
684         FREENULL( codec.p_information );
685 #undef  codec
686     }
687     FREENULL( p_cl->codec );
688 }
689
690 /* Microsoft should go to hell. This time the length give number of bytes
691  * and for the some others object, length give char16 count ... */
692 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
693 {
694     asf_object_content_description_t *p_cd =
695         (asf_object_content_description_t *)p_obj;
696     uint8_t *p_peek, *p_data;
697     int i_peek, i_title, i_artist, i_copyright, i_description, i_rating;
698     vlc_iconv_t cd = (vlc_iconv_t)-1;
699     const char *ib = NULL;
700     char *ob = NULL;
701     size_t i_ibl, i_obl, i_len;
702
703     cd = vlc_iconv_open("UTF-8", "UTF-16LE");
704     if ( cd == (vlc_iconv_t)-1 ) {
705         msg_Err( s, "vlc_iconv_open failed" );
706         return VLC_EGENERIC;
707     }
708
709 /* FIXME i_size*3 is the worst case. */
710 #define GETSTRINGW( psz_str, i_size ) \
711     psz_str = (char *)calloc( i_size*3+1, sizeof( char ) ); \
712     ib = (const char *)p_data; \
713     ob = psz_str; \
714     i_ibl = i_size; \
715     i_obl = i_size*3; \
716     i_len = vlc_iconv(cd, &ib, &i_ibl, &ob, &i_obl); \
717     p_data += i_size;
718
719     if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
720     {
721        return VLC_EGENERIC;
722     }
723     p_data = p_peek + 24;
724
725     i_title = GetWLE( p_data ); p_data += 2;
726     i_artist= GetWLE( p_data ); p_data += 2;
727     i_copyright     = GetWLE( p_data ); p_data += 2;
728     i_description   = GetWLE( p_data ); p_data += 2;
729     i_rating        = GetWLE( p_data ); p_data += 2;
730
731     GETSTRINGW( p_cd->psz_title, i_title );
732     GETSTRINGW( p_cd->psz_artist, i_artist );
733     GETSTRINGW( p_cd->psz_copyright, i_copyright );
734     GETSTRINGW( p_cd->psz_description, i_description );
735     GETSTRINGW( p_cd->psz_rating, i_rating );
736
737 #undef  GETSTRINGW
738
739 #ifdef ASF_DEBUG
740     msg_Dbg( s,
741              "read \"content description object\" title:\"%s\" artist:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
742              p_cd->psz_title,
743              p_cd->psz_artist,
744              p_cd->psz_copyright,
745              p_cd->psz_description,
746              p_cd->psz_rating );
747 #endif
748
749     vlc_iconv_close(cd);
750     return VLC_SUCCESS;
751 }
752
753 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
754 {
755     asf_object_content_description_t *p_cd =
756         (asf_object_content_description_t *)p_obj;
757
758     FREENULL( p_cd->psz_title );
759     FREENULL( p_cd->psz_artist );
760     FREENULL( p_cd->psz_copyright );
761     FREENULL( p_cd->psz_description );
762     FREENULL( p_cd->psz_rating );
763 }
764
765 /* Language list: */
766 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
767 {
768     asf_object_language_list_t *p_ll =
769         (asf_object_language_list_t*)p_obj;
770     uint8_t *p_peek, *p_data;
771     int i_peek;
772     int i;
773
774     if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
775        return VLC_EGENERIC;
776
777     p_data = &p_peek[24];
778
779     p_ll->i_language = GetWLE( &p_data[0] ); p_data += 2;
780     if( p_ll->i_language > 0 )
781     {
782         p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
783
784         for( i = 0; i < p_ll->i_language; i++ )
785         {
786             char *psz;
787             int i_size = *p_data++;
788             int i_len;
789
790             psz = calloc( i_size/2 + 1, sizeof( char ) );
791             for( i_len = 0; i_len < i_size/2; i_len++ )
792             {
793                 psz[i_len] = GetWLE( p_data + 2*i_len );
794             }
795             psz[i_size/2] = '\0'; \
796             p_data += i_size;
797
798             p_ll->ppsz_language[i] = psz;
799         }
800     }
801
802 #ifdef ASF_DEBUG
803     msg_Dbg( s, "read \"language list object\" %d entries", 
804              p_ll->i_language );
805     for( i = 0; i < p_ll->i_language; i++ )
806         msg_Dbg( s, "  - '%s'", 
807                  p_ll->ppsz_language[i] );
808 #endif
809     return VLC_SUCCESS;
810 }
811
812 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
813 {
814     asf_object_language_list_t *p_ll =
815         (asf_object_language_list_t *)p_obj;
816     int i;
817
818     for( i = 0; i < p_ll->i_language; i++ )
819         FREENULL( p_ll->ppsz_language[i] );
820     FREENULL( p_ll->ppsz_language );
821 }
822
823 /* Stream bitrate properties */
824 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
825                                                      asf_object_t *p_obj)
826 {
827     asf_object_stream_bitrate_properties_t *p_sb =
828         (asf_object_stream_bitrate_properties_t *)p_obj;
829     uint8_t *p_peek, *p_data;
830     int i_peek;
831     int i;
832
833     if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
834        return VLC_EGENERIC;
835
836     p_data = &p_peek[24];
837
838     p_sb->i_bitrate = GetWLE( &p_data[0] ); p_data += 2;
839     if( p_sb->i_bitrate > 127 ) p_sb->i_bitrate = 127;  /* Buggy ? */
840     for( i = 0; i < p_sb->i_bitrate; i++ )
841     {
842         p_sb->bitrate[i].i_stream_number = GetWLE( &p_data[0] )& 0x7f;
843         p_sb->bitrate[i].i_avg_bitrate = GetDWLE( &p_data[2] );
844
845         p_data += 2+4;
846     }
847
848 #ifdef ASF_DEBUG
849     msg_Dbg( s,"read \"stream bitrate properties object\"" );
850     for( i = 0; i < p_sb->i_bitrate; i++ )
851     {
852         msg_Dbg( s,"  - stream=%d bitrate=%d",
853                  p_sb->bitrate[i].i_stream_number,
854                  p_sb->bitrate[i].i_avg_bitrate ); 
855     }
856 #endif
857     return VLC_SUCCESS;
858 }
859 static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
860 {
861 }
862
863 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
864                                                       asf_object_t *p_obj)
865 {
866     asf_object_extended_stream_properties_t *p_esp =
867         (asf_object_extended_stream_properties_t*)p_obj;
868     uint8_t *p_peek, *p_data;
869     int i_peek, i;
870
871     if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
872        return VLC_EGENERIC;
873
874     p_data = &p_peek[24];
875
876     p_esp->i_start_time = GetQWLE( &p_data[0] );
877     p_esp->i_end_time = GetQWLE( &p_data[8] );
878     p_esp->i_data_bitrate = GetDWLE( &p_data[16] );
879     p_esp->i_buffer_size = GetDWLE( &p_data[20] );
880     p_esp->i_initial_buffer_fullness = GetDWLE( &p_data[24] );
881     p_esp->i_alternate_data_bitrate = GetDWLE( &p_data[28] );
882     p_esp->i_alternate_buffer_size = GetDWLE( &p_data[32] );
883     p_esp->i_alternate_initial_buffer_fullness = GetDWLE( &p_data[36] );
884     p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
885     p_esp->i_flags = GetDWLE( &p_data[44] );
886     p_esp->i_stream_number = GetWLE( &p_data[48] );
887     p_esp->i_language_index = GetWLE( &p_data[50] );
888     p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
889     p_esp->i_stream_name_count = GetWLE( &p_data[60] );
890     p_esp->i_payload_extension_system_count = GetWLE( &p_data[62] );
891
892     p_data += 64;
893
894     p_esp->pi_stream_name_language = calloc( sizeof(int),
895                                              p_esp->i_stream_name_count );
896     p_esp->ppsz_stream_name = calloc( sizeof(char*),
897                                       p_esp->i_stream_name_count );
898     for( i = 0; i < p_esp->i_stream_name_count; i++ )
899     {
900         int i_size;
901         char *psz;
902         int i_len;
903
904         p_esp->pi_stream_name_language[i] = GetWLE( &p_data[0] );
905         i_size = GetWLE( &p_data[2] );
906         p_data += 2;
907  
908         psz = calloc( i_size/2 + 1, sizeof( char ) );
909         for( i_len = 0; i_len < i_size/2; i_len++ )
910         {
911             psz[i_len] = GetWLE( p_data + 2*i_len );
912         }
913         psz[i_size/2] = '\0'; \
914         p_data += i_size;
915
916         p_esp->ppsz_stream_name[i] = psz;
917     }
918
919     for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
920     {
921         /* Skip them */
922         int i_size = GetDWLE( &p_data[16 + 2] );
923
924         p_data += 16+2+4+i_size;
925     }
926
927     p_esp->p_sp = NULL;
928     if( p_data < &p_peek[i_peek] )
929     {
930         asf_object_t *p_sp;
931         /* Cannot fail as peek succeed */
932         stream_Read( s, NULL, p_data - p_peek );
933         
934         p_sp = malloc( sizeof( asf_object_t ) );
935
936         if( ASF_ReadObject( s, p_sp, NULL ) )
937         {
938             free( p_sp );
939         }
940         else
941         {
942             /* This p_sp will be inserted by ReadRoot later */
943             p_esp->p_sp = (asf_object_stream_properties_t*)p_sp;
944         }
945     }
946
947 #ifdef ASF_DEBUG
948     msg_Dbg( s, "read \"extended stream properties object\":" );
949     msg_Dbg( s, "  - start="I64Fd" end="I64Fd,
950              p_esp->i_start_time, p_esp->i_end_time );
951     msg_Dbg( s, "  - data bitrate=%d buffer=%d initial fullness=%d",
952              p_esp->i_data_bitrate,
953              p_esp->i_buffer_size,
954              p_esp->i_initial_buffer_fullness );
955     msg_Dbg( s, "  - alternate data bitrate=%d buffer=%d initial fullness=%d",
956              p_esp->i_alternate_data_bitrate,
957              p_esp->i_alternate_buffer_size,
958              p_esp->i_alternate_initial_buffer_fullness );
959     msg_Dbg( s, "  - maximum object size=%d", p_esp->i_maximum_object_size );
960     msg_Dbg( s, "  - flags=0x%x", p_esp->i_flags );
961     msg_Dbg( s, "  - stream number=%d language=%d",
962              p_esp->i_stream_number, p_esp->i_language_index );
963     msg_Dbg( s, "  - average time per frame="I64Fd,
964              p_esp->i_average_time_per_frame );
965     msg_Dbg( s, "  - stream name count=%d", p_esp->i_stream_name_count );
966     for( i = 0; i < p_esp->i_stream_name_count; i++ )
967         msg_Dbg( s, "     - lang id=%d name=%s",
968                  p_esp->pi_stream_name_language[i],
969                  p_esp->ppsz_stream_name[i] );
970     msg_Dbg( s, "  - payload extension system count=%d",
971              p_esp->i_payload_extension_system_count );
972 #endif
973     return VLC_SUCCESS;
974 }
975 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
976 {
977     asf_object_extended_stream_properties_t *p_esp =
978         (asf_object_extended_stream_properties_t *)p_obj;
979     int i;
980
981     for( i = 0; i < p_esp->i_stream_name_count; i++ )
982         FREENULL( p_esp->ppsz_stream_name[i] );
983     FREENULL( p_esp->pi_stream_name_language );
984     FREENULL( p_esp->ppsz_stream_name );
985 }
986
987
988 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
989                                                      asf_object_t *p_obj)
990 {
991     asf_object_advanced_mutual_exclusion_t *p_ae =
992         (asf_object_advanced_mutual_exclusion_t *)p_obj;
993     uint8_t *p_peek, *p_data;
994     int i_peek;
995     int i;
996
997     if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
998        return VLC_EGENERIC;
999
1000     p_data = &p_peek[24];
1001
1002     ASF_GetGUID( &p_ae->type, &p_data[0] );
1003     p_ae->i_stream_number_count = GetWLE( &p_data[16] );
1004
1005     p_data += 16 + 2;
1006     p_ae->pi_stream_number = calloc( sizeof(int),
1007                                      p_ae->i_stream_number_count );
1008     for( i = 0; i < p_ae->i_stream_number_count; i++ )
1009     {
1010         p_ae->pi_stream_number[i] = GetWLE( p_data );
1011         p_data += 2;
1012     }
1013         
1014 #ifdef ASF_DEBUG
1015     msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
1016     for( i = 0; i < p_ae->i_stream_number_count; i++ )
1017         msg_Dbg( s, "  - stream=%d", p_ae->pi_stream_number[i] );
1018 #endif
1019     return VLC_SUCCESS;
1020 }
1021 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
1022 {
1023     asf_object_advanced_mutual_exclusion_t *p_ae =
1024         (asf_object_advanced_mutual_exclusion_t *)p_obj;
1025
1026     FREENULL( p_ae->pi_stream_number );
1027 }
1028
1029
1030 static int ASF_ReadObject_stream_prioritization( stream_t *s,
1031                                                  asf_object_t *p_obj)
1032 {
1033     asf_object_stream_prioritization_t *p_sp =
1034         (asf_object_stream_prioritization_t *)p_obj;
1035     uint8_t *p_peek, *p_data;
1036     int i_peek;
1037     int i;
1038
1039     if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
1040        return VLC_EGENERIC;
1041
1042     p_data = &p_peek[24];
1043
1044     p_sp->i_priority_count = GetWLE( &p_data[0] );
1045     p_data += 2;
1046
1047     p_sp->pi_priority_flag = calloc( sizeof(int), p_sp->i_priority_count );
1048     p_sp->pi_priority_stream_number =
1049                              calloc( sizeof(int), p_sp->i_priority_count );
1050
1051     for( i = 0; i < p_sp->i_priority_count; i++ )
1052     {
1053         p_sp->pi_priority_stream_number[i] = GetWLE( p_data ); p_data += 2;
1054         p_sp->pi_priority_flag[i] = GetWLE( p_data ); p_data += 2;
1055     }
1056 #ifdef ASF_DEBUG
1057     msg_Dbg( s, "read \"stream prioritization object\"" );
1058     for( i = 0; i < p_sp->i_priority_count; i++ )
1059         msg_Dbg( s, "  - Stream:%d flags=0x%x",
1060                  p_sp->pi_priority_stream_number[i],
1061                  p_sp->pi_priority_flag[i] );
1062 #endif
1063     return VLC_SUCCESS;
1064 }
1065 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
1066 {
1067     asf_object_stream_prioritization_t *p_sp =
1068         (asf_object_stream_prioritization_t *)p_obj;
1069
1070     FREENULL( p_sp->pi_priority_stream_number );
1071     FREENULL( p_sp->pi_priority_flag );
1072 }
1073
1074
1075 static int ASF_ReadObject_extended_content_description( stream_t *s,
1076                                                         asf_object_t *p_obj)
1077 {
1078     asf_object_extended_content_description_t *p_ec =
1079         (asf_object_extended_content_description_t *)p_obj;
1080     uint8_t *p_peek, *p_data;
1081     int i_peek;
1082     int i;
1083
1084     if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
1085        return VLC_EGENERIC;
1086
1087     p_data = &p_peek[24];
1088
1089     p_ec->i_count = GetWLE( p_data ); p_data += 2;
1090     p_ec->ppsz_name = calloc( sizeof(char*), p_ec->i_count );
1091     p_ec->ppsz_value = calloc( sizeof(char*), p_ec->i_count );
1092     for( i = 0; i < p_ec->i_count; i++ )
1093     {
1094         int i_size;
1095         int i_type;
1096         int i_len;
1097 #define GETSTRINGW( psz_str, i_size ) \
1098        psz_str = calloc( i_size/2 + 1, sizeof( char ) ); \
1099        for( i_len = 0; i_len < i_size/2; i_len++ ) \
1100        { \
1101            psz_str[i_len] = GetWLE( p_data + 2*i_len ); \
1102        } \
1103        psz_str[i_size/2] = '\0';
1104
1105         i_size = GetWLE( p_data ); p_data += 2;
1106         GETSTRINGW( p_ec->ppsz_name[i], i_size );
1107         p_data += i_size;
1108
1109         /* Grrr */
1110         i_type = GetWLE( p_data ); p_data += 2;
1111         i_size = GetWLE( p_data ); p_data += 2;
1112         if( i_type == 0 )
1113         {
1114             GETSTRINGW( p_ec->ppsz_value[i], i_size );
1115         }
1116         else if( i_type == 1 )
1117         {
1118             int j;
1119             /* Byte array */
1120             p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
1121             for( j = 0; j < i_size; j++ )
1122             {
1123                 static const char hex[16] = "0123456789ABCDEF";
1124                 p_ec->ppsz_value[i][2*j+0] = hex[p_data[0]>>4];
1125                 p_ec->ppsz_value[i][2*j+1] = hex[p_data[0]&0xf];
1126             }
1127             p_ec->ppsz_value[i][2*i_size] = '\0';
1128         }
1129         else if( i_type == 2 )
1130         {
1131             /* Bool */
1132             p_ec->ppsz_value[i] = strdup( *p_data ? "true" : "false" );
1133         }
1134         else if( i_type == 3 )
1135         {
1136             /* DWord */
1137             asprintf( &p_ec->ppsz_value[i], "%d", GetDWLE(p_data));
1138         }
1139         else if( i_type == 4 )
1140         {
1141             /* QWord */
1142             asprintf( &p_ec->ppsz_value[i], I64Fd, GetQWLE(p_data));
1143         }
1144         else if( i_type == 5 )
1145         {
1146             /* Word */
1147             asprintf( &p_ec->ppsz_value[i], "%d", GetWLE(p_data));
1148         }
1149         else
1150             p_ec->ppsz_value[i] = NULL;
1151
1152         p_data += i_size;
1153         
1154
1155
1156 #undef GETSTRINGW
1157
1158     }
1159
1160 #ifdef ASF_DEBUG
1161     msg_Dbg( s, "read \"extended content description object\"" );
1162     for( i = 0; i < p_ec->i_count; i++ )
1163         msg_Dbg( s, "  - '%s' = '%s'",
1164                  p_ec->ppsz_name[i],
1165                  p_ec->ppsz_value[i] );
1166 #endif
1167     return VLC_SUCCESS;
1168 }
1169 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
1170 {
1171     asf_object_extended_content_description_t *p_ec =
1172         (asf_object_extended_content_description_t *)p_obj;
1173     int i;
1174
1175     for( i = 0; i < p_ec->i_count; i++ )
1176     {
1177         FREENULL( p_ec->ppsz_name[i] );
1178         FREENULL( p_ec->ppsz_value[i] );
1179     }
1180     FREENULL( p_ec->ppsz_name );
1181     FREENULL( p_ec->ppsz_value );
1182 }
1183
1184
1185 #if 0
1186 static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
1187 {
1188     asf_object_XXX_t *p_XX =
1189         (asf_object_XXX_t *)p_obj;
1190     uint8_t *p_peek, *p_data;
1191     int i_peek;
1192
1193     if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
1194        return VLC_EGENERIC;
1195
1196     p_data = &p_peek[24];
1197
1198 #ifdef ASF_DEBUG
1199     msg_Dbg( s,
1200              "Read \"XXX object\"" );
1201 #endif
1202     return VLC_SUCCESS;
1203 }
1204 static void ASF_FreeObject_XXX( asf_object_t *p_obj)
1205 {
1206     asf_object_XXX_t *p_XX =
1207         (asf_object_XXX_t *)p_obj;
1208 }
1209 #endif
1210
1211
1212 /* */
1213 static struct
1214 {
1215     const guid_t  *p_id;
1216     int     i_type;
1217     int     (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
1218     void    (*ASF_FreeObject_function)( asf_object_t *p_obj );
1219
1220 } ASF_Object_Function [] =
1221 {
1222     { &asf_object_header_guid, ASF_OBJECT_HEADER,
1223       ASF_ReadObject_Header, ASF_FreeObject_Null },
1224     { &asf_object_data_guid, ASF_OBJECT_DATA,
1225       ASF_ReadObject_Data, ASF_FreeObject_Null },
1226     { &asf_object_index_guid, ASF_OBJECT_INDEX,
1227       ASF_ReadObject_Index, ASF_FreeObject_Index },
1228     { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
1229       ASF_ReadObject_file_properties, ASF_FreeObject_Null },
1230     { &asf_object_stream_properties_guid, ASF_OBJECT_STREAM_PROPERTIES,
1231       ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
1232     { &asf_object_header_extension_guid, ASF_OBJECT_HEADER_EXTENSION,
1233       ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
1234     { &asf_object_metadata_guid, ASF_OBJECT_METADATA,
1235       ASF_ReadObject_metadata, ASF_FreeObject_metadata},
1236     { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
1237       ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
1238     { &asf_object_marker_guid, ASF_OBJECT_MARKER, NULL, NULL },
1239     { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
1240     { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
1241       ASF_ReadObject_content_description, ASF_FreeObject_content_description },
1242     { &asf_object_language_list, ASF_OBJECT_OTHER,
1243       ASF_ReadObject_language_list, ASF_FreeObject_language_list },
1244     { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
1245       ASF_ReadObject_stream_bitrate_properties,
1246       ASF_FreeObject_stream_bitrate_properties },
1247     { &asf_object_extended_stream_properties, ASF_OBJECT_OTHER,
1248       ASF_ReadObject_extended_stream_properties,
1249       ASF_FreeObject_extended_stream_properties },
1250     { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
1251       ASF_ReadObject_advanced_mutual_exclusion,
1252       ASF_FreeObject_advanced_mutual_exclusion },
1253     { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
1254       ASF_ReadObject_stream_prioritization,
1255       ASF_FreeObject_stream_prioritization },
1256     { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
1257       ASF_ReadObject_extended_content_description,
1258       ASF_FreeObject_extended_content_description },
1259
1260     { &asf_object_null_guid, 0, NULL, NULL }
1261 };
1262
1263 static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
1264                            asf_object_t *p_father )
1265 {
1266     int i_result;
1267     int i_index;
1268
1269     if( !p_obj )
1270         return( 0 );
1271
1272     memset( p_obj, 0, sizeof( p_obj ) );
1273
1274     if( ASF_ReadObjectCommon( s, p_obj ) )
1275     {
1276         msg_Warn( s, "cannot read one asf object" );
1277         return VLC_EGENERIC;
1278     }
1279     p_obj->common.p_father = p_father;
1280     p_obj->common.p_first = NULL;
1281     p_obj->common.p_next = NULL;
1282     p_obj->common.p_last = NULL;
1283
1284     if( p_obj->common.i_object_size < 24 )
1285     {
1286         msg_Warn( s, "found a corrupted asf object (size<24)" );
1287         return VLC_EGENERIC;
1288     }
1289
1290     /* find this object */
1291     for( i_index = 0; ; i_index++ )
1292     {
1293         if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1294                          &p_obj->common.i_object_id ) ||
1295             ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1296                          &asf_object_null_guid ) )
1297         {
1298             break;
1299         }
1300     }
1301     p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
1302
1303     /* Now load this object */
1304     if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
1305     {
1306         msg_Warn( s, "unknown asf object (not loaded)" );
1307         i_result = VLC_SUCCESS;
1308     }
1309     else
1310     {
1311         /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
1312         i_result =
1313           (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
1314     }
1315
1316     /* link this object with father */
1317     if( p_father && ! i_result )
1318     {
1319         if( p_father->common.p_first )
1320         {
1321             p_father->common.p_last->common.p_next = p_obj;
1322         }
1323         else
1324         {
1325             p_father->common.p_first = p_obj;
1326         }
1327         p_father->common.p_last = p_obj;
1328     }
1329
1330     return( i_result );
1331 }
1332
1333 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
1334 {
1335     int i_index;
1336     asf_object_t *p_child;
1337
1338     if( !p_obj ) return;
1339
1340     /* Free all child object */
1341     p_child = p_obj->common.p_first;
1342     while( p_child )
1343     {
1344         asf_object_t *p_next;
1345         p_next = p_child->common.p_next;
1346         ASF_FreeObject( s, p_child );
1347         p_child = p_next;
1348     }
1349
1350     /* find this object */
1351     for( i_index = 0; ; i_index++ )
1352     {
1353         if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1354                      &p_obj->common.i_object_id )||
1355             ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1356                      &asf_object_null_guid ) )
1357         {
1358             break;
1359         }
1360     }
1361
1362     /* Now free this object */
1363     if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
1364     {
1365         msg_Warn( s,
1366                   "unknown asf object " GUID_FMT,
1367                   GUID_PRINT( p_obj->common.i_object_id ) );
1368     }
1369     else
1370     {
1371 #ifdef ASF_DEBUG
1372         msg_Dbg( s,
1373                   "free asf object " GUID_FMT,
1374                   GUID_PRINT( p_obj->common.i_object_id ) );
1375 #endif
1376         (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
1377     }
1378     free( p_obj );
1379     return;
1380 }
1381
1382 /*****************************************************************************
1383  * ASF_ObjectDumpDebug:
1384  *****************************************************************************/
1385 static const struct
1386 {
1387     const guid_t *p_id;
1388     const char *psz_name;
1389 } ASF_ObjectDumpDebugInfo[] =
1390 {
1391     { &asf_object_header_guid, "Header" },
1392     { &asf_object_data_guid, "Data" },
1393     { &asf_object_index_guid, "Index" },
1394     { &asf_object_file_properties_guid, "File Properties" },
1395     { &asf_object_stream_properties_guid, "Stream Properties" },
1396     { &asf_object_content_description_guid, "Content Description" },
1397     { &asf_object_header_extension_guid, "Header Extension" },
1398     { &asf_object_metadata_guid, "Metadata" },
1399     { &asf_object_codec_list_guid, "Codec List" },
1400     { &asf_object_marker_guid, "Marker" },
1401     { &asf_object_stream_type_audio, "Stream Type Audio" },
1402     { &asf_object_stream_type_video, "Stream Type Video" },
1403     { &asf_object_stream_type_command, "Stream Type Command" },
1404     { &asf_object_language_list, "Language List" },
1405     { &asf_object_stream_bitrate_properties, "Stream Bitrate Propoerties" },
1406     { &asf_object_padding, "Padding" },
1407     { &asf_object_extended_stream_properties, "Extended Stream Properties" },
1408     { &asf_object_advanced_mutual_exclusion, "Advanced Mutual Exclusion" },
1409     { &asf_object_stream_prioritization, "Stream Prioritization" },
1410     { &asf_object_extended_content_description, "Extended content description"},
1411
1412     { NULL, "Unknown" },
1413 };
1414
1415
1416 static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
1417                                  asf_object_common_t *p_node, int i_level )
1418 {
1419     char str[1024];
1420     int i;
1421     union asf_object_u *p_child;
1422     char *psz_name;
1423
1424     /* Find the name */
1425     for( i = 0; ASF_ObjectDumpDebugInfo[i].p_id != NULL; i++ )
1426     {
1427         if( ASF_CmpGUID( ASF_ObjectDumpDebugInfo[i].p_id,
1428                           &p_node->i_object_id ) )
1429             break;
1430     }
1431     psz_name = ASF_ObjectDumpDebugInfo[i].psz_name;
1432
1433     memset( str, ' ', sizeof( str ) );
1434     for( i = 1; i < i_level; i++ )
1435     {
1436         str[i * 5] = '|';
1437     }
1438     snprintf( str + 5*i_level, 1024,
1439              "+ '%s' GUID "GUID_FMT" size:"I64Fu"pos:"I64Fu,
1440              psz_name,
1441              GUID_PRINT( p_node->i_object_id ),
1442              p_node->i_object_size, p_node->i_object_pos );
1443
1444     msg_Dbg( p_obj, "%s", str );
1445
1446     for( p_child = p_node->p_first; p_child != NULL;
1447                                              p_child = p_child->common.p_next )
1448     {
1449         ASF_ObjectDumpDebug( p_obj, &p_child->common, i_level + 1 );
1450     }
1451 }
1452
1453 /*****************************************************************************
1454  * ASF_ReadObjetRoot : parse the entire stream/file
1455  *****************************************************************************/
1456 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
1457 {
1458     asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
1459     asf_object_t *p_obj;
1460
1461     p_root->i_type = ASF_OBJECT_ROOT;
1462     memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
1463     p_root->i_object_pos = stream_Tell( s );
1464     p_root->i_object_size = 0;
1465     p_root->p_first = NULL;
1466     p_root->p_last  = NULL;
1467     p_root->p_next  = NULL;
1468     p_root->p_hdr   = NULL;
1469     p_root->p_data  = NULL;
1470     p_root->p_fp    = NULL;
1471     p_root->p_index = NULL;
1472     p_root->p_metadata = NULL;
1473
1474     for( ; ; )
1475     {
1476         p_obj = malloc( sizeof( asf_object_t ) );
1477
1478         if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
1479         {
1480             free( p_obj );
1481             break;
1482         }
1483         switch( p_obj->common.i_type )
1484         {
1485             case( ASF_OBJECT_HEADER ):
1486                 p_root->p_hdr = (asf_object_header_t*)p_obj;
1487                 break;
1488             case( ASF_OBJECT_DATA ):
1489                 p_root->p_data = (asf_object_data_t*)p_obj;
1490                 break;
1491             case( ASF_OBJECT_INDEX ):
1492                 p_root->p_index = (asf_object_index_t*)p_obj;
1493                 break;
1494             default:
1495                 msg_Warn( s, "unknow object found" );
1496                 break;
1497         }
1498         if( p_obj->common.i_type == ASF_OBJECT_DATA &&
1499             p_obj->common.i_object_size <= 50 )
1500         {
1501             /* probably a dump of broadcasted asf */
1502             break;
1503         }
1504         if( !b_seekable && p_root->p_hdr && p_root->p_data )
1505         {
1506             /* For unseekable stream it's enough to play */
1507             break;
1508         }
1509
1510         if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
1511         {
1512             break;
1513         }
1514     }
1515
1516     if( p_root->p_hdr != NULL && p_root->p_data != NULL )
1517     {
1518         p_root->p_fp = ASF_FindObject( p_root->p_hdr,
1519                                        &asf_object_file_properties_guid, 0 );
1520
1521         if( p_root->p_fp )
1522         {
1523             asf_object_t *p_hdr_ext =
1524                 ASF_FindObject( p_root->p_hdr,
1525                                 &asf_object_header_extension_guid, 0 );
1526             if( p_hdr_ext )
1527             {
1528                 int i_ext_stream;
1529                 int i;
1530
1531                 p_root->p_metadata =
1532                     ASF_FindObject( p_hdr_ext,
1533                                     &asf_object_metadata_guid, 0 );
1534                 /* Special case for broken designed file format :( */
1535                 i_ext_stream = ASF_CountObject( p_hdr_ext,
1536                                     &asf_object_extended_stream_properties );
1537                 for( i = 0; i < i_ext_stream; i++ )
1538                 {
1539                     asf_object_t *p_esp =
1540                         ASF_FindObject( p_hdr_ext,
1541                                    &asf_object_extended_stream_properties, i );
1542                     if( p_esp->ext_stream.p_sp )
1543                     {
1544                         asf_object_t *p_sp =
1545                                          (asf_object_t*)p_esp->ext_stream.p_sp;
1546
1547                         /* Insert this p_sp */
1548                         p_root->p_hdr->p_last->common.p_next = p_sp;
1549                         p_root->p_hdr->p_last = p_sp;
1550
1551                         p_sp->common.p_father = (asf_object_t*)p_root->p_hdr;
1552                     }
1553                 }
1554             }
1555
1556             ASF_ObjectDumpDebug( VLC_OBJECT(s),
1557                                  (asf_object_common_t*)p_root, 0 );
1558             return p_root;
1559         }
1560         msg_Warn( s, "cannot find file properties object" );
1561     }
1562
1563     /* Invalid file */
1564     ASF_FreeObjectRoot( s, p_root );
1565     return NULL;
1566 }
1567
1568 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
1569 {
1570     asf_object_t *p_obj;
1571
1572     p_obj = p_root->p_first;
1573     while( p_obj )
1574     {
1575         asf_object_t *p_next;
1576         p_next = p_obj->common.p_next;
1577         ASF_FreeObject( s, p_obj );
1578         p_obj = p_next;
1579     }
1580     free( p_root );
1581 }
1582
1583 int  __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
1584 {
1585     int i_count;
1586     asf_object_t *p_child;
1587
1588     if( !p_obj ) return( 0 );
1589
1590     i_count = 0;
1591     p_child = p_obj->common.p_first;
1592     while( p_child )
1593     {
1594         if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1595         {
1596             i_count++;
1597         }
1598         p_child = p_child->common.p_next;
1599     }
1600     return( i_count );
1601 }
1602
1603 void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid,
1604                         int i_number )
1605 {
1606     asf_object_t *p_child;
1607
1608     p_child = p_obj->common.p_first;
1609
1610     while( p_child )
1611     {
1612         if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1613         {
1614             if( i_number == 0 )
1615             {
1616                 /* We found it */
1617                 return( p_child );
1618             }
1619
1620             i_number--;
1621         }
1622         p_child = p_child->common.p_next;
1623     }
1624     return( NULL );
1625 }