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