]> git.sesse.net Git - vlc/blobdiff - modules/demux/asf/libasf.c
demux: ts: Better missing PCR fixes
[vlc] / modules / demux / asf / libasf.c
index bf896df10f06040784548e782be30fef4eb75aa9..7171549d2d1b7bb6143f7aeec44836f09c287768 100644 (file)
 /*****************************************************************************
  * libasf.c : asf stream demux module for vlc
  *****************************************************************************
- * Copyright (C) 2001-2003 the VideoLAN team
- * $Id$
+ * Copyright © 2001-2004, 2006-2008 VLC authors and VideoLAN
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Gildas Bazin <gbazin@videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
-#include <vlc_common.h>
-
-
 #include <vlc_demux.h>
+#include <vlc_charset.h>          /* FromCharset */
 
-#include <vlc_codecs.h>                   /* BITMAPINFOHEADER, WAVEFORMATEX */
 #include "libasf.h"
 
-#define ASF_DEBUG 1
-
-#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"
-#define GUID_PRINT( guid )  \
-    (guid).v1,              \
-    (guid).v2,              \
-    (guid).v3,              \
-    (guid).v4[0],(guid).v4[1],(guid).v4[2],(guid).v4[3],    \
-    (guid).v4[4],(guid).v4[5],(guid).v4[6],(guid).v4[7]
-
-/****************************************************************************
- *
- ****************************************************************************/
-static int ASF_ReadObject( stream_t *, asf_object_t *,  asf_object_t * );
+#ifndef NDEBUG
+# define ASF_DEBUG 1
+#endif
 
-/****************************************************************************
- * GUID functions
- ****************************************************************************/
-void ASF_GetGUID( guid_t *p_guid, const uint8_t *p_data )
+/* Helpers:
+ * They ensure that invalid reads will not create problems.
+ * They are expansion safe
+ * They make the following assumptions:
+ *  const uint8_t *p_peek exists and points to the start of a buffer
+ *  int i_peek gives the size of the buffer pointed by p_peek
+ *  const uint8_t *p_data exits and points to the data inside p_peek to be read.
+ */
+/* ASF_HAVE(n):
+ *  Check that n bytes can be read */
+static inline bool AsfObjectHelperHave( const uint8_t *p_peek, int i_peek, const uint8_t *p_current, int i_wanted )
 {
-    p_guid->v1 = GetDWLE( p_data );
-    p_guid->v2 = GetWLE( p_data + 4);
-    p_guid->v3 = GetWLE( p_data + 6);
-    memcpy( p_guid->v4, p_data + 8, 8 );
+    if( i_wanted < 0 || i_wanted > i_peek )
+        return false;
+    return &p_current[i_wanted] <= &p_peek[i_peek];
 }
+#define ASF_HAVE(n) AsfObjectHelperHave( p_peek, i_peek, p_data, n )
 
-int ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
+/* ASF_SKIP(n)
+ *  Skip n bytes if possible */
+static inline void AsfObjectHelperSkip( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_wanted )
 {
-    if( (p_guid1->v1 != p_guid2->v1 )||
-        (p_guid1->v2 != p_guid2->v2 )||
-        (p_guid1->v3 != p_guid2->v3 )||
-        ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
+    if( AsfObjectHelperHave( p_peek, i_peek, *pp_data, i_wanted ) )
+        *pp_data += i_wanted;
+    else
+        *pp_data = (uint8_t*)&p_peek[i_peek];
+}
+#define ASF_SKIP(n) AsfObjectHelperSkip( p_peek, i_peek, (uint8_t**)&p_data, n )
+
+/* ASF_READX()
+ *  Read X byte if possible, else return 0 */
+#define ASF_FUNCTION_READ_X(type, x, cmd ) \
+static inline type AsfObjectHelperRead##x( const uint8_t *p_peek, int i_peek, uint8_t **pp_data ) { \
+    uint8_t *p_data = *pp_data; \
+    type i_ret = 0;  \
+    if( ASF_HAVE(x) )   \
+        i_ret = cmd;    \
+    ASF_SKIP(x);        \
+    *pp_data = p_data;  \
+    return i_ret;   }
+ASF_FUNCTION_READ_X( uint8_t,  1, *p_data )
+ASF_FUNCTION_READ_X( uint16_t, 2, GetWLE(p_data) )
+ASF_FUNCTION_READ_X( uint32_t, 4, GetDWLE(p_data) )
+ASF_FUNCTION_READ_X( uint64_t, 8, GetQWLE(p_data) )
+#define ASF_READ1() AsfObjectHelperRead1( p_peek, i_peek, (uint8_t**)&p_data )
+#define ASF_READ2() AsfObjectHelperRead2( p_peek, i_peek, (uint8_t**)&p_data )
+#define ASF_READ4() AsfObjectHelperRead4( p_peek, i_peek, (uint8_t**)&p_data )
+#define ASF_READ8() AsfObjectHelperRead8( p_peek, i_peek, (uint8_t**)&p_data )
+
+/* ASF_READS(n)
+ *  Read a string of n/2 wchar long ie n bytes. Do a stupid conversion (suppose latin1)
+ *  Return allocated "" if not possible */
+static char *AsfObjectHelperReadString( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_size )
+{
+    uint8_t *p_data = *pp_data;
+    char *psz_string;
+    if( ASF_HAVE(i_size) )
+    {
+        psz_string = calloc( i_size/2 + 1, sizeof( char ) );
+        if( psz_string )
+        {
+            for( int i = 0; i < i_size/2; i++ )
+                psz_string[i] = GetWLE( &p_data[2*i] );
+            psz_string[i_size/2] = '\0';
+        }
+    }
+    else
     {
-        return( 0 );
+        psz_string = strdup("");
     }
-    return( 1 ); /* match */
+    ASF_SKIP(i_size);
+    *pp_data = p_data;
+    return psz_string;
 }
+#define ASF_READS(n) AsfObjectHelperReadString( p_peek, i_peek, (uint8_t**)&p_data, n )
+
+/****************************************************************************
+ *
+ ****************************************************************************/
+static int ASF_ReadObject( stream_t *, asf_object_t *,  asf_object_t * );
 
 /****************************************************************************
  *
  ****************************************************************************/
 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
+    asf_object_common_t *p_common = &p_obj->common;
     const uint8_t *p_peek;
 
     if( stream_Peek( s, &p_peek, 24 ) < 24 )
-    {
-        return( VLC_EGENERIC );
-    }
+        return VLC_EGENERIC;
+
     ASF_GetGUID( &p_common->i_object_id, p_peek );
     p_common->i_object_size = GetQWLE( p_peek + 16 );
     p_common->i_object_pos  = stream_Tell( s );
@@ -91,30 +133,35 @@ static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
 
 #ifdef ASF_DEBUG
     msg_Dbg( s,
-             "found object guid: " GUID_FMT " size:%"PRId64,
+             "found object guid: " GUID_FMT " size:%"PRId64" at %"PRId64,
              GUID_PRINT( p_common->i_object_id ),
-             p_common->i_object_size );
+             p_common->i_object_size, p_common->i_object_pos );
 #endif
 
-    return( VLC_SUCCESS );
+    return VLC_SUCCESS;
 }
 
-static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
+static int ASF_NextObject( stream_t *s, asf_object_t *p_obj, uint64_t i_boundary )
 {
     asf_object_t obj;
+
+    int64_t i_pos = stream_Tell( s );
+    if ( i_boundary && i_pos >= 0 && (uint64_t) i_pos >= i_boundary )
+    {
+        return VLC_EGENERIC;
+    }
+
     if( p_obj == NULL )
     {
         if( ASF_ReadObjectCommon( s, &obj ) )
-        {
-            return( VLC_EGENERIC );
-        }
+            return VLC_EGENERIC;
+
         p_obj = &obj;
     }
 
     if( p_obj->common.i_object_size <= 0 )
-    {
         return VLC_EGENERIC;
-    }
+
     if( p_obj->common.p_father &&
         p_obj->common.p_father->common.i_object_size != 0 )
     {
@@ -135,20 +182,16 @@ static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
 static void ASF_FreeObject_Null( asf_object_t *pp_obj )
 {
     VLC_UNUSED(pp_obj);
-    return;
 }
 
 static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj;
+    asf_object_header_t *p_hdr = &p_obj->header;
     asf_object_t        *p_subobj;
-    int                 i_peek;
     const uint8_t       *p_peek;
 
-    if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
-    {
-       return( VLC_EGENERIC );
-    }
+    if( stream_Peek( s, &p_peek, 30 ) < 30 )
+       return VLC_EGENERIC;
 
     p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
     p_hdr->i_reserved1 = p_peek[28];
@@ -172,29 +215,25 @@ static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
     {
         p_subobj = malloc( sizeof( asf_object_t ) );
 
-        if( ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
+        if( !p_subobj || ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
         {
             free( p_subobj );
             break;
         }
-        if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
-        {
+        if( ASF_NextObject( s, p_subobj, 0 ) ) /* Go to the next object */
             break;
-        }
     }
     return VLC_SUCCESS;
 }
 
 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_data_t *p_data = (asf_object_data_t*)p_obj;
-    int               i_peek;
+    asf_object_data_t *p_data = &p_obj->data;
     const uint8_t     *p_peek;
 
-    if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
-    {
+    if( stream_Peek( s, &p_peek, 50 ) < 50 )
        return VLC_EGENERIC;
-    }
+
     ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
     p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
     p_data->i_reserved = GetWLE( p_peek + 48 );
@@ -213,16 +252,14 @@ static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
 
 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
+    asf_object_index_t *p_index = &p_obj->index;
     const uint8_t      *p_peek;
-    int                 i;
+    unsigned int       i;
 
+    /* We just ignore error on the index */
     if( stream_Peek( s, &p_peek, p_index->i_object_size ) <
-        (int)p_index->i_object_size )
-    {
-        /* Just ignore */
+        __MAX( (int64_t)p_index->i_object_size, 56 ) )
         return VLC_SUCCESS;
-    }
 
     ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
     p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
@@ -245,14 +282,15 @@ static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
     if( p_index->i_index_entry_count > (p_index->i_object_size - 56) / 6 )
         p_index->i_index_entry_count = (p_index->i_object_size - 56) / 6;
 
-    p_index->index_entry = malloc( sizeof(asf_index_entry_t) *
-                                   p_index->i_index_entry_count );
+    p_index->index_entry = calloc( p_index->i_index_entry_count,
+                                   sizeof(asf_index_entry_t) );
+    if( !p_index->index_entry )
+        return VLC_ENOMEM;
 
-    for( i = 0, p_peek += 56; i < (int)p_index->i_index_entry_count;
-         i++, p_peek += 6 )
+    for( i = 0, p_peek += 56; i < p_index->i_index_entry_count; i++, p_peek += 6 )
     {
         p_index->index_entry[i].i_packet_number = GetDWLE( p_peek );
-        p_index->index_entry[i].i_packet_count = GetDWLE( p_peek + 4 );
+        p_index->index_entry[i].i_packet_count = GetWLE( p_peek + 4 );
     }
 
     return VLC_SUCCESS;
@@ -260,21 +298,19 @@ static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_Index( asf_object_t *p_obj )
 {
-    asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
+    asf_object_index_t *p_index = &p_obj->index;
 
     FREENULL( p_index->index_entry );
 }
 
 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
-    int           i_peek;
+    asf_object_file_properties_t *p_fp = &p_obj->file_properties;
     const uint8_t *p_peek;
 
-    if( ( i_peek = stream_Peek( s, &p_peek,  104 ) ) < 104 )
-    {
+    if( stream_Peek( s, &p_peek,  104 ) < 104 )
        return VLC_EGENERIC;
-    }
+
     ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
     p_fp->i_file_size = GetQWLE( p_peek + 40 );
     p_fp->i_creation_date = GetQWLE( p_peek + 48 );
@@ -283,8 +319,8 @@ static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
     p_fp->i_send_duration = GetQWLE( p_peek + 72 );
     p_fp->i_preroll = GetQWLE( p_peek + 80 );
     p_fp->i_flags = GetDWLE( p_peek + 88 );
-    p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
-    p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
+    p_fp->i_min_data_packet_size = __MAX( GetDWLE( p_peek + 92 ), (uint32_t) 1 );
+    p_fp->i_max_data_packet_size = __MAX( GetDWLE( p_peek + 96 ), (uint32_t) 1 );
     p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
 
 #ifdef ASF_DEBUG
@@ -307,11 +343,9 @@ static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_metadata( asf_object_t *p_obj )
 {
-    asf_object_metadata_t *p_meta =
-        (asf_object_metadata_t *)p_obj;
-    unsigned int i;
+    asf_object_metadata_t *p_meta = &p_obj->metadata;
 
-    for( i = 0; i < p_meta->i_record_entries_count; i++ )
+    for( uint32_t i = 0; i < p_meta->i_record_entries_count; i++ )
     {
         free( p_meta->record[i].psz_name );
         free( p_meta->record[i].p_data );
@@ -321,109 +355,92 @@ static void ASF_FreeObject_metadata( asf_object_t *p_obj )
 
 static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_metadata_t *p_meta =
-        (asf_object_metadata_t *)p_obj;
+    asf_object_metadata_t *p_meta = &p_obj->metadata;
 
-    int i_peek, i_entries, i;
-    const uint8_t *p_peek;
-#ifdef ASF_DEBUG
-    unsigned int j;
-#endif
-
-    p_meta->i_record_entries_count = 0;
-    p_meta->record = 0;
+    int i_peek;
+    uint32_t i;
+    const uint8_t *p_peek, *p_data;
 
-    if( stream_Peek( s, &p_peek, p_meta->i_object_size ) <
-        (int)p_meta->i_object_size )
-    {
+    if( ( i_peek = stream_Peek( s, &p_peek, p_meta->i_object_size ) ) <
+        __MAX( (int64_t)p_meta->i_object_size, 26 ) )
        return VLC_EGENERIC;
-    }
 
-    i_peek = 24;
-    i_entries = GetWLE( p_peek + i_peek ); i_peek += 2;
-    for( i = 0; i < i_entries && i_peek < (int)p_meta->i_object_size -12; i++ )
+    p_meta->i_record_entries_count = GetWLE( p_peek + 24 );
+
+    p_data = p_peek + 26;
+
+    p_meta->record = calloc( p_meta->i_record_entries_count,
+                             sizeof(asf_metadata_record_t) );
+    if( !p_meta->record )
+        return VLC_ENOMEM;
+
+    for( i = 0; i < p_meta->i_record_entries_count; i++ )
     {
-        asf_metadata_record_t record;
-        int i_name, i_data, j;
+        asf_metadata_record_t *p_record = &p_meta->record[i];
+        uint16_t i_name;
+        uint32_t i_data;
 
-        if( GetWLE( p_peek + i_peek ) != 0 )
-        {
-            ASF_FreeObject_metadata( p_obj );
-            return VLC_EGENERIC;
-        }
+        if( !ASF_HAVE( 2+2+2+2+4 ) )
+            break;
 
-        i_peek += 2;
-        record.i_stream = GetWLE( p_peek + i_peek ); i_peek += 2;
-        i_name = GetWLE( p_peek + i_peek ); i_peek += 2;
-        record.i_type = GetWLE( p_peek + i_peek ); i_peek += 2;
-        i_data = GetDWLE( p_peek + i_peek ); i_peek += 4;
+        if( ASF_READ2() != 0 )
+            break;
 
-        if( record.i_type > ASF_METADATA_TYPE_WORD ||
-            i_peek + i_data + i_name > (int)p_meta->i_object_size )
-        {
-            ASF_FreeObject_metadata( p_obj );
-            return VLC_EGENERIC;
-        }
+        p_record->i_stream = ASF_READ2();
+        i_name = ASF_READ2();
+        p_record->i_type = ASF_READ2();
+        i_data = ASF_READ4();
+
+        if( !ASF_HAVE( i_name + i_data ) )
+            break;
 
-        record.i_val = 0;
-        record.i_data = 0;
-        record.p_data = 0;
+        /* Read name */
+        p_record->psz_name = ASF_READS( i_name );
 
-        /* get name */
-        record.psz_name = malloc( i_name/2 + 1 );
-        for( j = 0; j < i_name/2; j++ )
+        /* Read data */
+        if( p_record->i_type == ASF_METADATA_TYPE_STRING )
         {
-            record.psz_name[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
+            p_record->p_data = (uint8_t *)ASF_READS( i_data );
+            p_record->i_data = i_data/2; /* FIXME Is that needed ? */
         }
-        record.psz_name[j] = 0; /* just to make sure */
+        else if( p_record->i_type == ASF_METADATA_TYPE_BYTE )
+        {
+            p_record->p_data = malloc( i_data );
+            p_record->i_data = i_data;
+            if( p_record->p_data && i_data > 0 )
+                memcpy( p_record->p_data, p_data, i_data );
 
-        /* get data */
-        if( record.i_type == ASF_METADATA_TYPE_STRING )
+            p_data += i_data;
+        }
+        else if( p_record->i_type == ASF_METADATA_TYPE_QWORD )
         {
-            record.p_data = malloc( i_data/2 + 1 );
-            record.i_data = i_data/2;
-            for( j = 0; j < i_data/2; j++ )
-            {
-                record.p_data[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
-            }
-            record.p_data[j] = 0; /* just to make sure */
+            p_record->i_val = ASF_READ8();
         }
-        else if( record.i_type == ASF_METADATA_TYPE_BYTE )
+        else if( p_record->i_type == ASF_METADATA_TYPE_DWORD )
         {
-            record.p_data = malloc( i_data );
-            record.i_data = i_data;
-            memcpy( record.p_data, p_peek + i_peek, i_data );
-            p_peek += i_data;
+            p_record->i_val = ASF_READ4();
+        }
+        else if( p_record->i_type == ASF_METADATA_TYPE_WORD )
+        {
+            p_record->i_val = ASF_READ2();
+        }
+        else if( p_record->i_type == ASF_METADATA_TYPE_BOOL )
+        {
+            p_record->i_val = ASF_READ2();
         }
         else
         {
-            if( record.i_type == ASF_METADATA_TYPE_QWORD )
-            {
-                record.i_val = GetQWLE( p_peek + i_peek ); i_peek += 8;
-            }
-            else if( record.i_type == ASF_METADATA_TYPE_DWORD )
-            {
-                record.i_val = GetDWLE( p_peek + i_peek ); i_peek += 4;
-            }
-            else
-            {
-                record.i_val = GetWLE( p_peek + i_peek ); i_peek += 2;
-            }
+            /* Unknown */
+            p_data += i_data;
         }
-
-        p_meta->i_record_entries_count++;
-        p_meta->record =
-            realloc( p_meta->record, p_meta->i_record_entries_count *
-                     sizeof(asf_metadata_record_t) );
-        memcpy( &p_meta->record[p_meta->i_record_entries_count-1],
-                &record, sizeof(asf_metadata_record_t) );
     }
+    p_meta->i_record_entries_count = i;
 
 #ifdef ASF_DEBUG
     msg_Dbg( s,
-            "read \"metadata object\" %d entries",
+             "read \"metadata object\" %"PRIu32" entries",
             p_meta->i_record_entries_count );
-    for( j = 0; j < p_meta->i_record_entries_count; j++ )
+    for( uint32_t j = 0; j < p_meta->i_record_entries_count; j++ )
     {
         asf_metadata_record_t *p_rec = &p_meta->record[j];
 
@@ -431,10 +448,10 @@ static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
             msg_Dbg( s, "  - %s=%s",
                      p_rec->psz_name, p_rec->p_data );
         else if( p_rec->i_type == ASF_METADATA_TYPE_BYTE )
-            msg_Dbg( s, "  - %s (%i bytes)",
+            msg_Dbg( s, "  - %s (%u bytes)",
                      p_rec->psz_name, p_rec->i_data );
         else
-            msg_Dbg( s, "  - %s=%"PRId64,
+            msg_Dbg( s, "  - %s=%"PRIu64,
                      p_rec->psz_name, p_rec->i_val );
     }
 #endif
@@ -444,8 +461,7 @@ static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
 
 static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_header_extension_t *p_he =
-        (asf_object_header_extension_t *)p_obj;
+    asf_object_header_extension_t *p_he = &p_obj->header_extension;
     int     i_peek;
     const uint8_t *p_peek;
 
@@ -458,8 +474,14 @@ static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
     p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
     if( p_he->i_header_extension_size )
     {
+        if( (unsigned int)(i_peek-46) < p_he->i_header_extension_size )
+            return VLC_EGENERIC;
+
         p_he->p_header_extension_data =
             malloc( p_he->i_header_extension_size );
+        if( !p_he->p_header_extension_data )
+            return VLC_ENOMEM;
+
         memcpy( p_he->p_header_extension_data, p_peek + 46,
                 p_he->i_header_extension_size );
     }
@@ -471,7 +493,7 @@ static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
 #ifdef ASF_DEBUG
     msg_Dbg( s,
             "read \"header extension object\" reserved1:" GUID_FMT
-            " reserved2:%d header_extension_size:%d",
+            " reserved2:%u header_extension_size:%"PRIu32,
             GUID_PRINT( p_he->i_reserved1 ), p_he->i_reserved2,
             p_he->i_header_extension_size );
 #endif
@@ -484,13 +506,13 @@ static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
     {
         asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );
 
-        if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
+        if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
         {
             free( p_obj );
             break;
         }
 
-        if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
+        if( ASF_NextObject( s, p_obj, 0 ) ) /* Go to the next object */
         {
             break;
         }
@@ -501,23 +523,19 @@ static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
 {
-    asf_object_header_extension_t *p_he =
-        (asf_object_header_extension_t *)p_obj;
+    asf_object_header_extension_t *p_he = &p_obj->header_extension;
 
     FREENULL( p_he->p_header_extension_data );
 }
 
 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_stream_properties_t *p_sp =
-                    (asf_object_stream_properties_t*)p_obj;
+    asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
     size_t        i_peek;
     const uint8_t *p_peek;
 
     if( ( i_peek = stream_Peek( s, &p_peek,  p_sp->i_object_size ) ) < 78 )
-    {
        return VLC_EGENERIC;
-    }
 
     ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
     ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
@@ -525,7 +543,9 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
     p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
     p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
     p_sp->i_flags = GetWLE( p_peek + 72 );
-        p_sp->i_stream_number = p_sp->i_flags&0x07f;
+    p_sp->i_stream_number = p_sp->i_flags&0x07f;
+    if ( p_sp->i_stream_number > ASF_MAX_STREAMNUMBER )
+        return VLC_EGENERIC;
     p_sp->i_reserved = GetDWLE( p_peek + 74 );
     i_peek -= 78;
 
@@ -536,17 +556,13 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
 
         p_sp->p_type_specific_data =
             malloc( p_sp->i_type_specific_data_length );
-        if( p_sp->p_type_specific_data == NULL )
+        if( !p_sp->p_type_specific_data )
             return VLC_ENOMEM;
 
         memcpy( p_sp->p_type_specific_data, p_peek + 78,
                 p_sp->i_type_specific_data_length );
         i_peek -= p_sp->i_type_specific_data_length;
     }
-    else
-    {
-        p_sp->p_type_specific_data = NULL;
-    }
 
     if( p_sp->i_error_correction_data_length )
     {
@@ -558,7 +574,7 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
 
         p_sp->p_error_correction_data =
             malloc( p_sp->i_error_correction_data_length );
-        if( p_sp->p_error_correction_data == NULL )
+        if( !p_sp->p_error_correction_data )
         {
             free( p_sp->p_type_specific_data );
             return VLC_ENOMEM;
@@ -567,16 +583,12 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
                 p_peek + 78 + p_sp->i_type_specific_data_length,
                 p_sp->i_error_correction_data_length );
     }
-    else
-    {
-        p_sp->p_error_correction_data = NULL;
-    }
 
 #ifdef ASF_DEBUG
     msg_Dbg( s,
             "read \"stream Properties object\" stream_type:" GUID_FMT
-            " error_correction_type:" GUID_FMT " time_offset:%"PRId64
-            " type_specific_data_length:%d error_correction_data_length:%d"
+            " error_correction_type:" GUID_FMT " time_offset:%"PRIu64
+            " type_specific_data_length:%"PRIu32" error_correction_data_length:%"PRIu32
             " flags:0x%x stream_number:%d",
             GUID_PRINT( p_sp->i_stream_type ),
             GUID_PRINT( p_sp->i_error_correction_type ),
@@ -592,8 +604,7 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
 {
-    asf_object_stream_properties_t *p_sp =
-                (asf_object_stream_properties_t*)p_obj;
+    asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
 
     FREENULL( p_sp->p_type_specific_data );
     FREENULL( p_sp->p_error_correction_data );
@@ -602,71 +613,59 @@ static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
 
 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
 {
-    asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
+    asf_object_codec_list_t *p_cl = &p_obj->codec_list;
     int     i_peek;
     const uint8_t *p_peek, *p_data;
 
-    unsigned int i_codec;
+    uint32_t i_codec;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
-    {
        return VLC_EGENERIC;
-    }
 
     ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
-    p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
+    p_cl->i_codec_entries_count = GetDWLE( p_peek + 40 );
+
+    p_data = p_peek + 44;
+
     if( p_cl->i_codec_entries_count > 0 )
     {
         p_cl->codec = calloc( p_cl->i_codec_entries_count,
                               sizeof( asf_codec_entry_t ) );
-        memset( p_cl->codec, 0,
-                p_cl->i_codec_entries_count * sizeof( asf_codec_entry_t ) );
+        if( !p_cl->codec )
+            return VLC_ENOMEM;
 
-        p_data = p_peek + 44;
         for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
         {
-#define codec p_cl->codec[i_codec]
-            int i_len, i;
+            asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
+
+            if( !ASF_HAVE( 2+2+2 ) )
+                break;
+
+            /* */
+            p_codec->i_type = ASF_READ2();
+
+            /* XXX the length here are the number of *unicode* characters and
+             * not of bytes like nearly every elsewhere */
 
-            codec.i_type = GetWLE( p_data ); p_data += 2;
             /* codec name */
-            i_len = GetWLE( p_data ); p_data += 2;
-            codec.psz_name = calloc( sizeof( char ), i_len + 1);
-            for( i = 0; i < i_len; i++ )
-            {
-                codec.psz_name[i] = GetWLE( p_data + 2*i );
-            }
-            codec.psz_name[i_len] = '\0';
-            p_data += 2 * i_len;
+            p_codec->psz_name = ASF_READS( 2*ASF_READ2() );
 
             /* description */
-            i_len = GetWLE( p_data ); p_data += 2;
-            codec.psz_description = calloc( sizeof( char ), i_len + 1);
-            for( i = 0; i < i_len; i++ )
-            {
-                codec.psz_description[i] = GetWLE( p_data + 2*i );
-            }
-            codec.psz_description[i_len] = '\0';
-            p_data += 2 * i_len;
+            p_codec->psz_description = ASF_READS( 2*ASF_READ2() );
 
             /* opaque information */
-            codec.i_information_length = GetWLE( p_data ); p_data += 2;
-            if( codec.i_information_length > 0 )
-            {
-                codec.p_information = malloc( codec.i_information_length );
-                memcpy( codec.p_information, p_data, codec.i_information_length );
-                p_data += codec.i_information_length;
-            }
-            else
+            p_codec->i_information_length = ASF_READ2();
+            if( p_codec->i_information_length > 0 && ASF_HAVE( p_codec->i_information_length ) )
             {
-                codec.p_information = NULL;
+                p_codec->p_information = malloc( p_codec->i_information_length );
+                if( p_codec->p_information )
+                    memcpy( p_codec->p_information, p_data, p_codec->i_information_length );
+                else
+                    p_codec->i_information_length = 0;
+                p_data += p_codec->i_information_length;
             }
-#undef  codec
         }
-    }
-    else
-    {
-        p_cl->codec = NULL;
+        p_cl->i_codec_entries_count = i_codec;
     }
 
 #ifdef ASF_DEBUG
@@ -676,15 +675,15 @@ static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
 
     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
     {
-#define codec p_cl->codec[i_codec]
-        msg_Dbg( s, "  - codec[%d] %s name:\"%s\" "
-                 "description:\"%s\" information_length:%d",
-                 i_codec, ( codec.i_type == ASF_CODEC_TYPE_VIDEO ) ?
-                 "video" : ( ( codec.i_type == ASF_CODEC_TYPE_AUDIO ) ?
+        const asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
+
+        msg_Dbg( s, "  - codec[%"PRIu32"] %s name:\"%s\" "
+                 "description:\"%s\" information_length:%u",
+                 i_codec, ( p_codec->i_type == ASF_CODEC_TYPE_VIDEO ) ?
+                 "video" : ( ( p_codec->i_type == ASF_CODEC_TYPE_AUDIO ) ?
                  "audio" : "unknown" ),
-                 codec.psz_name, codec.psz_description,
-                 codec.i_information_length );
-#undef  codec
+                 p_codec->psz_name, p_codec->psz_description,
+                 p_codec->i_information_length );
     }
 #endif
 
@@ -693,68 +692,55 @@ static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
 
 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
 {
-    asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
-    unsigned int i_codec;
+    asf_object_codec_list_t *p_cl = &p_obj->codec_list;
 
-    for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
+    for( uint32_t i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
     {
-#define codec p_cl->codec[i_codec]
-        FREENULL( codec.psz_name );
-        FREENULL( codec.psz_description );
-        FREENULL( codec.p_information );
-#undef  codec
+        asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
+
+        FREENULL( p_codec->psz_name );
+        FREENULL( p_codec->psz_description );
+        FREENULL( p_codec->p_information );
     }
     FREENULL( p_cl->codec );
 }
 
+static inline char *get_wstring( const uint8_t *p_data, size_t i_size )
+{
+    char *psz_str = FromCharset( "UTF-16LE", p_data, i_size );
+    if( psz_str )
+        p_data += i_size;
+    return psz_str;
+}
+
 /* Microsoft should go to hell. This time the length give number of bytes
  * and for the some others object, length give char16 count ... */
 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
 {
-    asf_object_content_description_t *p_cd =
-        (asf_object_content_description_t *)p_obj;
+    asf_object_content_description_t *p_cd = &p_obj->content_description;
     const uint8_t *p_peek, *p_data;
-    int i_peek, i_title, i_artist, i_copyright, i_description, i_rating;
-    vlc_iconv_t cd = (vlc_iconv_t)-1;
-    const char *ib = NULL;
-    char *ob = NULL;
-    size_t i_ibl, i_obl, i_len;
-
-    cd = vlc_iconv_open("UTF-8", "UTF-16LE");
-    if ( cd == (vlc_iconv_t)-1 ) {
-        msg_Err( s, "vlc_iconv_open failed" );
-        return VLC_EGENERIC;
-    }
-
-/* FIXME i_size*3 is the worst case. */
-#define GETSTRINGW( psz_str, i_size ) \
-    psz_str = (char *)calloc( i_size*3+1, sizeof( char ) ); \
-    ib = (const char *)p_data; \
-    ob = psz_str; \
-    i_ibl = i_size; \
-    i_obl = i_size*3; \
-    i_len = vlc_iconv(cd, &ib, &i_ibl, &ob, &i_obl); \
-    p_data += i_size;
+    int i_peek;
+    uint16_t i_title, i_artist, i_copyright, i_description, i_rating;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
-    {
        return VLC_EGENERIC;
-    }
+
     p_data = p_peek + 24;
 
-    i_title = GetWLE( p_data ); p_data += 2;
-    i_artist= GetWLE( p_data ); p_data += 2;
-    i_copyright     = GetWLE( p_data ); p_data += 2;
-    i_description   = GetWLE( p_data ); p_data += 2;
-    i_rating        = GetWLE( p_data ); p_data += 2;
+    i_title         = ASF_READ2();
+    i_artist        = ASF_READ2();
+    i_copyright     = ASF_READ2();
+    i_description   = ASF_READ2();
+    i_rating        = ASF_READ2();
 
-    GETSTRINGW( p_cd->psz_title, i_title );
-    GETSTRINGW( p_cd->psz_artist, i_artist );
-    GETSTRINGW( p_cd->psz_copyright, i_copyright );
-    GETSTRINGW( p_cd->psz_description, i_description );
-    GETSTRINGW( p_cd->psz_rating, i_rating );
+    if( !ASF_HAVE( i_title+i_artist+i_copyright+i_description+i_rating ) )
+        return VLC_EGENERIC;
 
-#undef  GETSTRINGW
+    p_cd->psz_title = get_wstring( p_data, i_title );
+    p_cd->psz_artist = get_wstring( p_data, i_artist );
+    p_cd->psz_copyright = get_wstring( p_data, i_copyright );
+    p_cd->psz_description = get_wstring( p_data, i_description );
+    p_cd->psz_rating = get_wstring( p_data, i_rating );
 
 #ifdef ASF_DEBUG
     msg_Dbg( s,
@@ -766,14 +752,12 @@ static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
              p_cd->psz_rating );
 #endif
 
-    vlc_iconv_close(cd);
     return VLC_SUCCESS;
 }
 
 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
 {
-    asf_object_content_description_t *p_cd =
-        (asf_object_content_description_t *)p_obj;
+    asf_object_content_description_t *p_cd = &p_obj->content_description;
 
     FREENULL( p_cd->psz_title );
     FREENULL( p_cd->psz_artist );
@@ -785,42 +769,34 @@ static void ASF_FreeObject_content_description( asf_object_t *p_obj)
 /* Language list: */
 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
 {
-    asf_object_language_list_t *p_ll =
-        (asf_object_language_list_t*)p_obj;
+    asf_object_language_list_t *p_ll = &p_obj->language_list;
     const uint8_t *p_peek, *p_data;
     int i_peek;
-    int i;
+    uint16_t i;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
        return VLC_EGENERIC;
 
     p_data = &p_peek[24];
 
-    p_ll->i_language = GetWLE( &p_data[0] ); p_data += 2;
+    p_ll->i_language = ASF_READ2();
     if( p_ll->i_language > 0 )
     {
         p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
+        if( !p_ll->ppsz_language )
+            return VLC_ENOMEM;
 
         for( i = 0; i < p_ll->i_language; i++ )
         {
-            char *psz;
-            int i_size = *p_data++;
-            int i_len;
-
-            psz = calloc( i_size/2 + 1, sizeof( char ) );
-            for( i_len = 0; i_len < i_size/2; i_len++ )
-            {
-                psz[i_len] = GetWLE( p_data + 2*i_len );
-            }
-            psz[i_size/2] = '\0'; \
-            p_data += i_size;
-
-            p_ll->ppsz_language[i] = psz;
+            if( !ASF_HAVE(1) )
+                break;
+            p_ll->ppsz_language[i] = ASF_READS( ASF_READ1() );
         }
+        p_ll->i_language = i;
     }
 
 #ifdef ASF_DEBUG
-    msg_Dbg( s, "read \"language list object\" %d entries",
+    msg_Dbg( s, "read \"language list object\" %u entries",
              p_ll->i_language );
     for( i = 0; i < p_ll->i_language; i++ )
         msg_Dbg( s, "  - '%s'",
@@ -831,9 +807,8 @@ static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
 
 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
 {
-    asf_object_language_list_t *p_ll =
-        (asf_object_language_list_t *)p_obj;
-    int i;
+    asf_object_language_list_t *p_ll = &p_obj->language_list;
+    uint16_t i;
 
     for( i = 0; i < p_ll->i_language; i++ )
         FREENULL( p_ll->ppsz_language[i] );
@@ -844,32 +819,35 @@ static void ASF_FreeObject_language_list( asf_object_t *p_obj)
 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
                                                      asf_object_t *p_obj)
 {
-    asf_object_stream_bitrate_properties_t *p_sb =
-        (asf_object_stream_bitrate_properties_t *)p_obj;
+    asf_object_stream_bitrate_properties_t *p_sb = &p_obj->stream_bitrate;
     const uint8_t *p_peek, *p_data;
     int i_peek;
-    int i;
+    uint16_t i;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
        return VLC_EGENERIC;
 
     p_data = &p_peek[24];
 
-    p_sb->i_bitrate = GetWLE( &p_data[0] ); p_data += 2;
-    if( p_sb->i_bitrate > 127 ) p_sb->i_bitrate = 127;  /* Buggy ? */
+    p_sb->i_bitrate = ASF_READ2();
+    if( p_sb->i_bitrate > ASF_MAX_STREAMNUMBER )
+        p_sb->i_bitrate = ASF_MAX_STREAMNUMBER;  /* Buggy ? */
     for( i = 0; i < p_sb->i_bitrate; i++ )
     {
-        p_sb->bitrate[i].i_stream_number = GetWLE( &p_data[0] )& 0x7f;
-        p_sb->bitrate[i].i_avg_bitrate = GetDWLE( &p_data[2] );
-
-        p_data += 2+4;
+        if( !ASF_HAVE(2 + 4) )
+            break;
+        p_sb->bitrate[i].i_stream_number = (uint8_t) ASF_READ2()& 0x7f;
+        if ( p_sb->bitrate[i].i_stream_number > ASF_MAX_STREAMNUMBER )
+            return VLC_EGENERIC;
+        p_sb->bitrate[i].i_avg_bitrate = ASF_READ4();
     }
+    p_sb->i_bitrate = i;
 
 #ifdef ASF_DEBUG
     msg_Dbg( s,"read \"stream bitrate properties object\"" );
     for( i = 0; i < p_sb->i_bitrate; i++ )
     {
-        msg_Dbg( s,"  - stream=%d bitrate=%d",
+        msg_Dbg( s,"  - stream=%u bitrate=%"PRIu32,
                  p_sb->bitrate[i].i_stream_number,
                  p_sb->bitrate[i].i_avg_bitrate );
     }
@@ -878,15 +856,16 @@ static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
 }
 static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
 {
+    VLC_UNUSED(p_obj);
 }
 
 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
                                                       asf_object_t *p_obj)
 {
-    asf_object_extended_stream_properties_t *p_esp =
-        (asf_object_extended_stream_properties_t*)p_obj;
+    asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
     const uint8_t *p_peek, *p_data;
-    int i_peek, i;
+    int i_peek;
+    uint16_t i;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
        return VLC_EGENERIC;
@@ -904,6 +883,8 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
     p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
     p_esp->i_flags = GetDWLE( &p_data[44] );
     p_esp->i_stream_number = GetWLE( &p_data[48] );
+    if ( p_esp->i_stream_number > ASF_MAX_STREAMNUMBER )
+        return VLC_EGENERIC;
     p_esp->i_language_index = GetWLE( &p_data[50] );
     p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
     p_esp->i_stream_name_count = GetWLE( &p_data[60] );
@@ -911,38 +892,48 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
 
     p_data += 64;
 
-    p_esp->pi_stream_name_language = calloc( sizeof(int),
-                                             p_esp->i_stream_name_count );
-    p_esp->ppsz_stream_name = calloc( sizeof(char*),
-                                      p_esp->i_stream_name_count );
+    p_esp->pi_stream_name_language = calloc( p_esp->i_stream_name_count,
+                                             sizeof(uint16_t) );
+    p_esp->ppsz_stream_name = calloc( p_esp->i_stream_name_count,
+                                      sizeof(char*) );
+    if( !p_esp->pi_stream_name_language ||
+        !p_esp->ppsz_stream_name )
+    {
+        free( p_esp->pi_stream_name_language );
+        free( p_esp->ppsz_stream_name );
+        return VLC_ENOMEM;
+    }
     for( i = 0; i < p_esp->i_stream_name_count; i++ )
     {
-        int i_size;
-        char *psz;
-        int i_len;
-
-        p_esp->pi_stream_name_language[i] = GetWLE( &p_data[0] );
-        i_size = GetWLE( &p_data[2] );
-        p_data += 2;
-        psz = calloc( i_size/2 + 1, sizeof( char ) );
-        for( i_len = 0; i_len < i_size/2; i_len++ )
-        {
-            psz[i_len] = GetWLE( p_data + 2*i_len );
-        }
-        psz[i_size/2] = '\0'; \
-        p_data += i_size;
-
-        p_esp->ppsz_stream_name[i] = psz;
+        if( !ASF_HAVE( 2+2 ) )
+            break;
+        p_esp->pi_stream_name_language[i] = ASF_READ2();
+        p_esp->ppsz_stream_name[i] = ASF_READS( ASF_READ2() );
     }
+    p_esp->i_stream_name_count = i;
 
-    for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
+    p_esp->p_ext = calloc( p_esp->i_payload_extension_system_count,
+                           sizeof( asf_payload_extension_system_t ) );
+    if ( p_esp->p_ext )
     {
-        /* Skip them */
-        int i_size = GetDWLE( &p_data[16 + 2] );
-
-        p_data += 16+2+4+i_size;
-    }
+        for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
+        {
+            asf_payload_extension_system_t *p_ext = & p_esp->p_ext[i];
+            if( !ASF_HAVE( 16+2+4 ) ) break;
+            ASF_GetGUID( &p_ext->i_extension_id, p_data );
+            ASF_SKIP( 16 );   // GUID
+            p_ext->i_data_size = ASF_READ2();
+            p_ext->i_info_length = ASF_READ4();
+            if ( p_ext->i_info_length )
+            {
+                if( !ASF_HAVE( p_ext->i_info_length ) ) break;
+                p_ext->pi_info = malloc( p_ext->i_info_length );
+                if ( p_ext->pi_info )
+                    memcpy( p_ext->pi_info, p_data, p_ext->i_info_length );
+                ASF_SKIP( p_ext->i_info_length );
+            }
+        }
+    } else p_esp->i_payload_extension_system_count = 0;
 
     p_esp->p_sp = NULL;
     if( p_data < &p_peek[i_peek] )
@@ -950,10 +941,10 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
         asf_object_t *p_sp;
         /* Cannot fail as peek succeed */
         stream_Read( s, NULL, p_data - p_peek );
+
         p_sp = malloc( sizeof( asf_object_t ) );
 
-        if( ASF_ReadObject( s, p_sp, NULL ) )
+        if( !p_sp || ASF_ReadObject( s, p_sp, NULL ) )
         {
             free( p_sp );
         }
@@ -966,39 +957,46 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
 
 #ifdef ASF_DEBUG
     msg_Dbg( s, "read \"extended stream properties object\":" );
-    msg_Dbg( s, "  - start=%"PRId64" end=%"PRId64,
+    msg_Dbg( s, "  - start=%"PRIu64" end=%"PRIu64,
              p_esp->i_start_time, p_esp->i_end_time );
-    msg_Dbg( s, "  - data bitrate=%d buffer=%d initial fullness=%d",
+    msg_Dbg( s, "  - data bitrate=%"PRId32" buffer=%"PRId32" initial fullness=%"PRId32,
              p_esp->i_data_bitrate,
              p_esp->i_buffer_size,
              p_esp->i_initial_buffer_fullness );
-    msg_Dbg( s, "  - alternate data bitrate=%d buffer=%d initial fullness=%d",
+    msg_Dbg( s, "  - alternate data bitrate=%"PRId32" buffer=%"PRId32" initial fullness=%"PRId32,
              p_esp->i_alternate_data_bitrate,
              p_esp->i_alternate_buffer_size,
              p_esp->i_alternate_initial_buffer_fullness );
-    msg_Dbg( s, "  - maximum object size=%d", p_esp->i_maximum_object_size );
+    msg_Dbg( s, "  - maximum object size=%"PRId32, p_esp->i_maximum_object_size );
     msg_Dbg( s, "  - flags=0x%x", p_esp->i_flags );
-    msg_Dbg( s, "  - stream number=%d language=%d",
+    msg_Dbg( s, "  - stream number=%u language=%u",
              p_esp->i_stream_number, p_esp->i_language_index );
-    msg_Dbg( s, "  - average time per frame=%"PRId64,
+    msg_Dbg( s, "  - average time per frame=%"PRIu64,
              p_esp->i_average_time_per_frame );
-    msg_Dbg( s, "  - stream name count=%d", p_esp->i_stream_name_count );
+    msg_Dbg( s, "  - stream name count=%u", p_esp->i_stream_name_count );
     for( i = 0; i < p_esp->i_stream_name_count; i++ )
-        msg_Dbg( s, "     - lang id=%d name=%s",
+        msg_Dbg( s, "     - lang id=%u name=%s",
                  p_esp->pi_stream_name_language[i],
                  p_esp->ppsz_stream_name[i] );
-    msg_Dbg( s, "  - payload extension system count=%d",
+    msg_Dbg( s, "  - payload extension system count=%u",
              p_esp->i_payload_extension_system_count );
+    for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
+        msg_Dbg( s, "  - %u  - payload extension: " GUID_FMT, i,
+                 GUID_PRINT( p_esp->p_ext[i].i_extension_id ) );
 #endif
     return VLC_SUCCESS;
 }
 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
 {
-    asf_object_extended_stream_properties_t *p_esp =
-        (asf_object_extended_stream_properties_t *)p_obj;
-    int i;
+    asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
 
-    for( i = 0; i < p_esp->i_stream_name_count; i++ )
+    if ( p_esp->p_ext )
+    {
+        for( uint16_t i = 0; i < p_esp->i_payload_extension_system_count; i++ )
+            free( p_esp->p_ext[i].pi_info );
+        FREENULL( p_esp->p_ext );
+    }
+    for( uint16_t i = 0; i < p_esp->i_stream_name_count; i++ )
         FREENULL( p_esp->ppsz_stream_name[i] );
     FREENULL( p_esp->pi_stream_name_language );
     FREENULL( p_esp->ppsz_stream_name );
@@ -1008,31 +1006,48 @@ static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
                                                      asf_object_t *p_obj)
 {
-    asf_object_advanced_mutual_exclusion_t *p_ae =
-        (asf_object_advanced_mutual_exclusion_t *)p_obj;
+    asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
     const uint8_t *p_peek, *p_data;
     int i_peek;
-    int i;
+    uint16_t i;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
        return VLC_EGENERIC;
 
     p_data = &p_peek[24];
 
-    ASF_GetGUID( &p_ae->type, &p_data[0] );
-    p_ae->i_stream_number_count = GetWLE( &p_data[16] );
+    if( !ASF_HAVE( 16 + 2 * sizeof(uint16_t) ) ) /* at least one entry */
+        return VLC_EGENERIC;
+
+    if ( guidcmp( (const guid_t *) p_data, &asf_guid_mutex_language ) )
+        p_ae->exclusion_type = LANGUAGE;
+    else if ( guidcmp( (const guid_t *) p_data, &asf_guid_mutex_bitrate ) )
+        p_ae->exclusion_type = BITRATE;
+    ASF_SKIP( 16 );
+
+    p_ae->i_stream_number_count = ASF_READ2();
+    p_ae->pi_stream_number = calloc( p_ae->i_stream_number_count, sizeof(uint16_t) );
+    if ( !p_ae->pi_stream_number )
+        return VLC_ENOMEM;
 
-    p_data += 16 + 2;
-    p_ae->pi_stream_number = calloc( sizeof(int),
-                                     p_ae->i_stream_number_count );
     for( i = 0; i < p_ae->i_stream_number_count; i++ )
     {
-        p_ae->pi_stream_number[i] = GetWLE( p_data );
-        p_data += 2;
+        if( !ASF_HAVE(2) )
+            break;
+        p_ae->pi_stream_number[i] = ASF_READ2();
+        if ( p_ae->pi_stream_number[i] > ASF_MAX_STREAMNUMBER )
+        {
+            free( p_ae->pi_stream_number );
+            return VLC_EGENERIC;
+        }
     }
+    p_ae->i_stream_number_count = i;
+
 #ifdef ASF_DEBUG
-    msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
+    msg_Dbg( s, "read \"advanced mutual exclusion object\" type %s",
+             p_ae->exclusion_type == LANGUAGE ? "Language" :
+             ( p_ae->exclusion_type == BITRATE ) ? "Bitrate" : "Unknown"
+    );
     for( i = 0; i < p_ae->i_stream_number_count; i++ )
         msg_Dbg( s, "  - stream=%d", p_ae->pi_stream_number[i] );
 #endif
@@ -1040,8 +1055,7 @@ static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
 }
 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
 {
-    asf_object_advanced_mutual_exclusion_t *p_ae =
-        (asf_object_advanced_mutual_exclusion_t *)p_obj;
+    asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
 
     FREENULL( p_ae->pi_stream_number );
 }
@@ -1050,33 +1064,42 @@ static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
 static int ASF_ReadObject_stream_prioritization( stream_t *s,
                                                  asf_object_t *p_obj)
 {
-    asf_object_stream_prioritization_t *p_sp =
-        (asf_object_stream_prioritization_t *)p_obj;
+    asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
     const uint8_t *p_peek, *p_data;
     int i_peek;
-    int i;
+    uint16_t i;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
        return VLC_EGENERIC;
 
     p_data = &p_peek[24];
 
-    p_sp->i_priority_count = GetWLE( &p_data[0] );
-    p_data += 2;
+    p_sp->i_priority_count = ASF_READ2();
 
-    p_sp->pi_priority_flag = calloc( sizeof(int), p_sp->i_priority_count );
+    p_sp->pi_priority_flag = calloc( p_sp->i_priority_count, sizeof(uint16_t) );
     p_sp->pi_priority_stream_number =
-                             calloc( sizeof(int), p_sp->i_priority_count );
+                             calloc( p_sp->i_priority_count, sizeof(uint16_t) );
+
+    if( !p_sp->pi_priority_flag || !p_sp->pi_priority_stream_number )
+    {
+        free( p_sp->pi_priority_flag );
+        free( p_sp->pi_priority_stream_number );
+        return VLC_ENOMEM;
+    }
 
     for( i = 0; i < p_sp->i_priority_count; i++ )
     {
-        p_sp->pi_priority_stream_number[i] = GetWLE( p_data ); p_data += 2;
-        p_sp->pi_priority_flag[i] = GetWLE( p_data ); p_data += 2;
+        if( !ASF_HAVE(2+2) )
+            break;
+        p_sp->pi_priority_stream_number[i] = ASF_READ2();
+        p_sp->pi_priority_flag[i] = ASF_READ2();
     }
+    p_sp->i_priority_count = i;
+
 #ifdef ASF_DEBUG
     msg_Dbg( s, "read \"stream prioritization object\"" );
     for( i = 0; i < p_sp->i_priority_count; i++ )
-        msg_Dbg( s, "  - Stream:%d flags=0x%x",
+        msg_Dbg( s, "  - Stream:%u flags=0x%x",
                  p_sp->pi_priority_stream_number[i],
                  p_sp->pi_priority_flag[i] );
 #endif
@@ -1084,98 +1107,162 @@ static int ASF_ReadObject_stream_prioritization( stream_t *s,
 }
 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
 {
-    asf_object_stream_prioritization_t *p_sp =
-        (asf_object_stream_prioritization_t *)p_obj;
+    asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
 
     FREENULL( p_sp->pi_priority_stream_number );
     FREENULL( p_sp->pi_priority_flag );
 }
 
+static int ASF_ReadObject_bitrate_mutual_exclusion( stream_t *s, asf_object_t *p_obj )
+{
+    asf_object_bitrate_mutual_exclusion_t *p_ex = &p_obj->bitrate_mutual_exclusion;
+    const uint8_t *p_peek, *p_data;
+    int i_peek;
+
+    if( ( i_peek = stream_Peek( s, &p_peek, p_ex->i_object_size ) ) < 42 )
+       return VLC_EGENERIC;
+
+    p_data = &p_peek[24];
+
+    if( !ASF_HAVE( 16 + 2 * sizeof(uint16_t) ) ) /* at least one entry */
+        return VLC_EGENERIC;
+
+    if ( guidcmp( (const guid_t *) p_data, &asf_guid_mutex_language ) )
+        p_ex->exclusion_type = LANGUAGE;
+    else if ( guidcmp( (const guid_t *) p_data, &asf_guid_mutex_bitrate ) )
+        p_ex->exclusion_type = BITRATE;
+    ASF_SKIP( 16 );
+
+    p_ex->i_stream_number_count = ASF_READ2();
+    p_ex->pi_stream_numbers = calloc( p_ex->i_stream_number_count, sizeof(uint16_t) );
+    if ( ! p_ex->pi_stream_numbers )
+    {
+        p_ex->i_stream_number_count = 0;
+        return VLC_ENOMEM;
+    }
+
+    for( uint16_t i = 0; i < p_ex->i_stream_number_count; i++ )
+    {
+        if( !ASF_HAVE(2) )
+            break;
+        p_ex->pi_stream_numbers[i] = ASF_READ2();
+        if ( p_ex->pi_stream_numbers[i] > ASF_MAX_STREAMNUMBER )
+        {
+            free( p_ex->pi_stream_numbers );
+            return VLC_EGENERIC;
+        }
+    }
+
+#ifdef ASF_DEBUG
+    msg_Dbg( s, "read \"bitrate exclusion object\" type %s",
+             p_ex->exclusion_type == LANGUAGE ? "Language" :
+             ( p_ex->exclusion_type == BITRATE ) ? "Bitrate" : "Unknown"
+    );
+    for( uint16_t i = 0; i < p_ex->i_stream_number_count; i++ )
+        msg_Dbg( s, "  - stream=%i", p_ex->pi_stream_numbers[i] );
+#endif
+
+    return VLC_SUCCESS;
+}
+static void ASF_FreeObject_bitrate_mutual_exclusion( asf_object_t *p_obj)
+{
+    asf_object_bitrate_mutual_exclusion_t *p_ex = &p_obj->bitrate_mutual_exclusion;
+
+    FREENULL( p_ex->pi_stream_numbers );
+    p_ex->i_stream_number_count = 0;
+}
 
 static int ASF_ReadObject_extended_content_description( stream_t *s,
                                                         asf_object_t *p_obj)
 {
     asf_object_extended_content_description_t *p_ec =
-        (asf_object_extended_content_description_t *)p_obj;
+                                        &p_obj->extended_content_description;
     const uint8_t *p_peek, *p_data;
     int i_peek;
-    int i;
+    uint16_t i;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
        return VLC_EGENERIC;
 
     p_data = &p_peek[24];
 
-    p_ec->i_count = GetWLE( p_data ); p_data += 2;
-    p_ec->ppsz_name = calloc( sizeof(char*), p_ec->i_count );
-    p_ec->ppsz_value = calloc( sizeof(char*), p_ec->i_count );
+    p_ec->i_count = ASF_READ2();
+    p_ec->ppsz_name  = calloc( p_ec->i_count, sizeof(char*) );
+    p_ec->ppsz_value = calloc( p_ec->i_count, sizeof(char*) );
+    if( !p_ec->ppsz_name || !p_ec->ppsz_value )
+    {
+        free( p_ec->ppsz_name );
+        free( p_ec->ppsz_value );
+        return VLC_ENOMEM;
+    }
     for( i = 0; i < p_ec->i_count; i++ )
     {
-        int i_size;
-        int i_type;
-        int i_len;
-#define GETSTRINGW( psz_str, i_size ) \
-       psz_str = calloc( i_size/2 + 1, sizeof( char ) ); \
-       for( i_len = 0; i_len < i_size/2; i_len++ ) \
-       { \
-           psz_str[i_len] = GetWLE( p_data + 2*i_len ); \
-       } \
-       psz_str[i_size/2] = '\0';
-
-        i_size = GetWLE( p_data ); p_data += 2;
-        GETSTRINGW( p_ec->ppsz_name[i], i_size );
-        p_data += i_size;
+        uint16_t i_size;
+        uint16_t i_type;
+
+        if( !ASF_HAVE(2 + 2+2) )
+            break;
+
+        p_ec->ppsz_name[i] = ASF_READS( ASF_READ2() );
 
         /* Grrr */
-        i_type = GetWLE( p_data ); p_data += 2;
-        i_size = GetWLE( p_data ); p_data += 2;
+        i_type = ASF_READ2();
+        i_size = ASF_READ2();
+
         if( i_type == 0 )
         {
-            GETSTRINGW( p_ec->ppsz_value[i], i_size );
+            /* Unicode string */
+            p_ec->ppsz_value[i] = ASF_READS( i_size );
         }
         else if( i_type == 1 )
         {
-            int j;
             /* Byte array */
+            static const char hex[16] = "0123456789ABCDEF";
+
             p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
-            for( j = 0; j < i_size; j++ )
+            if( p_ec->ppsz_value[i] )
             {
-                static const char hex[16] = "0123456789ABCDEF";
-                p_ec->ppsz_value[i][2*j+0] = hex[p_data[0]>>4];
-                p_ec->ppsz_value[i][2*j+1] = hex[p_data[0]&0xf];
+                char *psz_value = p_ec->ppsz_value[i];
+                for( int j = 0; j < i_size; j++ )
+                {
+                    const uint8_t v = ASF_READ1();
+                    psz_value[2*j+0] = hex[v>>4];
+                    psz_value[2*j+1] = hex[v&0xf];
+                }
+                psz_value[2*i_size] = '\0';
             }
-            p_ec->ppsz_value[i][2*i_size] = '\0';
         }
         else if( i_type == 2 )
         {
             /* Bool */
-            p_ec->ppsz_value[i] = strdup( *p_data ? "true" : "false" );
+            p_ec->ppsz_value[i] = strdup( ASF_READ1() ? "true" : "false" );
+            ASF_SKIP(i_size-1);
         }
         else if( i_type == 3 )
         {
             /* DWord */
-            asprintf( &p_ec->ppsz_value[i], "%d", GetDWLE(p_data));
+            if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ4() ) == -1 )
+                p_ec->ppsz_value[i] = NULL;
         }
         else if( i_type == 4 )
         {
             /* QWord */
-            asprintf( &p_ec->ppsz_value[i], "%"PRId64, GetQWLE(p_data));
+            if( asprintf( &p_ec->ppsz_value[i], "%"PRId64, ASF_READ8() ) == -1 )
+                p_ec->ppsz_value[i] = NULL;
         }
         else if( i_type == 5 )
         {
             /* Word */
-            asprintf( &p_ec->ppsz_value[i], "%d", GetWLE(p_data));
+            if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ2() ) == -1 )
+                p_ec->ppsz_value[i] = NULL;
         }
         else
+        {
             p_ec->ppsz_value[i] = NULL;
-
-        p_data += i_size;
-
-
-#undef GETSTRINGW
-
+            ASF_SKIP(i_size);
+        }
     }
+    p_ec->i_count = i;
 
 #ifdef ASF_DEBUG
     msg_Dbg( s, "read \"extended content description object\"" );
@@ -1189,10 +1276,9 @@ static int ASF_ReadObject_extended_content_description( stream_t *s,
 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
 {
     asf_object_extended_content_description_t *p_ec =
-        (asf_object_extended_content_description_t *)p_obj;
-    int i;
+                                        &p_obj->extended_content_description;
 
-    for( i = 0; i < p_ec->i_count; i++ )
+    for( uint16_t i = 0; i < p_ec->i_count; i++ )
     {
         FREENULL( p_ec->ppsz_name[i] );
         FREENULL( p_ec->ppsz_value[i] );
@@ -1201,6 +1287,72 @@ static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
     FREENULL( p_ec->ppsz_value );
 }
 
+static int ASF_ReadObject_marker(stream_t *s, asf_object_t *p_obj)
+{
+    asf_object_marker_t *p_mk = (asf_object_marker_t *)p_obj;
+    const uint8_t *p_peek, *p_data;
+    int i_peek;
+
+    if( ( i_peek = stream_Peek( s, &p_peek, p_mk->i_object_size ) ) < 24 )
+       return VLC_EGENERIC;
+
+    p_data = &p_peek[24];
+
+    ASF_GetGUID( &p_mk->i_reserved1, p_data );
+    ASF_SKIP( 16 );
+    p_mk->i_count = ASF_READ4();
+    p_mk->i_reserved2 = ASF_READ2();
+    p_mk->name = ASF_READS( ASF_READ2() );
+
+    if( p_mk->i_count > 0 )
+    {
+        p_mk->marker = calloc( p_mk->i_count,
+                              sizeof( asf_marker_t ) );
+        if( !p_mk->marker )
+            return VLC_ENOMEM;
+
+        for( uint32_t i = 0; i < p_mk->i_count; i++ )
+        {
+            asf_marker_t *p_marker = &p_mk->marker[i];
+
+            if( !ASF_HAVE(8+8+2+4+4+4) )
+                break;
+
+            p_marker->i_offset = ASF_READ8();
+            p_marker->i_presentation_time = ASF_READ8();
+            p_marker->i_entry_length = ASF_READ2();
+            p_marker->i_send_time = ASF_READ4();
+            p_marker->i_flags = ASF_READ4();
+            p_marker->i_marker_description_length = ASF_READ4();
+            p_marker->p_marker_description = ASF_READS( p_marker->i_marker_description_length * 2 );
+        }
+    }
+
+#ifdef ASF_DEBUG
+    msg_Dbg( s, "Read \"marker object\": %"PRIu32" chapters: %s", p_mk->i_count, p_mk->name );
+
+    for( unsigned i = 0; i < p_mk->i_count; i++ )
+        msg_Dbg( s, "New chapter named: %s", p_mk->marker[i].p_marker_description );
+#endif
+    return VLC_SUCCESS;
+}
+static void ASF_FreeObject_marker( asf_object_t *p_obj)
+{
+    asf_object_marker_t *p_mk = (asf_object_marker_t *)p_obj;
+
+    for( uint32_t i = 0; i < p_mk->i_count; i++ )
+    {
+        FREENULL( p_mk->marker[i].p_marker_description  );
+    }
+    FREENULL( p_mk->name );
+}
+
+static int ASF_ReadObject_Raw(stream_t *s, asf_object_t *p_obj)
+{
+    VLC_UNUSED(s);
+    VLC_UNUSED(p_obj);
+    return VLC_SUCCESS;
+}
 
 #if 0
 static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
@@ -1231,7 +1383,7 @@ static void ASF_FreeObject_XXX( asf_object_t *p_obj)
 
 
 /* */
-static struct
+static const struct
 {
     const guid_t  *p_id;
     int     i_type;
@@ -1244,7 +1396,7 @@ static struct
       ASF_ReadObject_Header, ASF_FreeObject_Null },
     { &asf_object_data_guid, ASF_OBJECT_DATA,
       ASF_ReadObject_Data, ASF_FreeObject_Null },
-    { &asf_object_index_guid, ASF_OBJECT_INDEX,
+    { &asf_object_simple_index_guid, ASF_OBJECT_INDEX,
       ASF_ReadObject_Index, ASF_FreeObject_Index },
     { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
       ASF_ReadObject_file_properties, ASF_FreeObject_Null },
@@ -1256,8 +1408,10 @@ static struct
       ASF_ReadObject_metadata, ASF_FreeObject_metadata},
     { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
       ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
-    { &asf_object_marker_guid, ASF_OBJECT_MARKER, NULL, NULL },
+    { &asf_object_marker_guid, ASF_OBJECT_MARKER, 
+      ASF_ReadObject_marker, ASF_FreeObject_marker },
     { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
+    { &asf_object_compatibility_guid, ASF_OBJECT_OTHER, NULL, NULL },
     { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
       ASF_ReadObject_content_description, ASF_FreeObject_content_description },
     { &asf_object_language_list, ASF_OBJECT_OTHER,
@@ -1265,7 +1419,7 @@ static struct
     { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
       ASF_ReadObject_stream_bitrate_properties,
       ASF_FreeObject_stream_bitrate_properties },
-    { &asf_object_extended_stream_properties, ASF_OBJECT_OTHER,
+    { &asf_object_extended_stream_properties_guid, ASF_OBJECT_OTHER,
       ASF_ReadObject_extended_stream_properties,
       ASF_FreeObject_extended_stream_properties },
     { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
@@ -1274,9 +1428,18 @@ static struct
     { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
       ASF_ReadObject_stream_prioritization,
       ASF_FreeObject_stream_prioritization },
+    { &asf_object_bitrate_mutual_exclusion_guid, ASF_OBJECT_OTHER,
+      ASF_ReadObject_bitrate_mutual_exclusion,
+      ASF_FreeObject_bitrate_mutual_exclusion },
     { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
       ASF_ReadObject_extended_content_description,
       ASF_FreeObject_extended_content_description },
+    { &asf_object_content_encryption_guid, ASF_OBJECT_OTHER,
+      ASF_ReadObject_Raw, ASF_FreeObject_Null },
+    { &asf_object_advanced_content_encryption_guid, ASF_OBJECT_OTHER,
+      ASF_ReadObject_Raw, ASF_FreeObject_Null },
+    { &asf_object_extended_content_encryption_guid, ASF_OBJECT_OTHER,
+      ASF_ReadObject_Raw, ASF_FreeObject_Null },
 
     { &asf_object_null_guid, 0, NULL, NULL }
 };
@@ -1288,9 +1451,9 @@ static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
     int i_index;
 
     if( !p_obj )
-        return( 0 );
+        return 0;
 
-    memset( p_obj, 0, sizeof( p_obj ) );
+    memset( p_obj, 0, sizeof( *p_obj ) );
 
     if( ASF_ReadObjectCommon( s, p_obj ) )
     {
@@ -1311,9 +1474,9 @@ static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
     /* find this object */
     for( i_index = 0; ; i_index++ )
     {
-        if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
+        if( guidcmp( ASF_Object_Function[i_index].p_id,
                          &p_obj->common.i_object_id ) ||
-            ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
+            guidcmp( ASF_Object_Function[i_index].p_id,
                          &asf_object_null_guid ) )
         {
             break;
@@ -1321,10 +1484,13 @@ static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
     }
     p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
 
+    if( i_index == sizeof(ASF_Object_Function)/sizeof(ASF_Object_Function[0]) - 1 )
+        msg_Warn( s, "unknown asf object (not loaded): " GUID_FMT,
+                GUID_PRINT( p_obj->common.i_object_id ) );
+
     /* Now load this object */
     if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
     {
-        msg_Warn( s, "unknown asf object (not loaded)" );
         i_result = VLC_SUCCESS;
     }
     else
@@ -1348,7 +1514,7 @@ static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
         p_father->common.p_last = p_obj;
     }
 
-    return( i_result );
+    return i_result;
 }
 
 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
@@ -1356,7 +1522,8 @@ static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
     int i_index;
     asf_object_t *p_child;
 
-    if( !p_obj ) return;
+    if( !p_obj )
+        return;
 
     /* Free all child object */
     p_child = p_obj->common.p_first;
@@ -1371,9 +1538,9 @@ static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
     /* find this object */
     for( i_index = 0; ; i_index++ )
     {
-        if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
+        if( guidcmp( ASF_Object_Function[i_index].p_id,
                      &p_obj->common.i_object_id )||
-            ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
+            guidcmp( ASF_Object_Function[i_index].p_id,
                      &asf_object_null_guid ) )
         {
             break;
@@ -1381,13 +1548,7 @@ static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
     }
 
     /* Now free this object */
-    if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
-    {
-        msg_Warn( s,
-                  "unknown asf object " GUID_FMT,
-                  GUID_PRINT( p_obj->common.i_object_id ) );
-    }
-    else
+    if( ASF_Object_Function[i_index].ASF_FreeObject_function != NULL )
     {
 #ifdef ASF_DEBUG
         msg_Dbg( s,
@@ -1397,7 +1558,6 @@ static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
         (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
     }
     free( p_obj );
-    return;
 }
 
 /*****************************************************************************
@@ -1409,9 +1569,11 @@ static const struct
     const char *psz_name;
 } ASF_ObjectDumpDebugInfo[] =
 {
+    { &vlc_object_root_guid, "Root" },
     { &asf_object_header_guid, "Header" },
     { &asf_object_data_guid, "Data" },
     { &asf_object_index_guid, "Index" },
+    { &asf_object_simple_index_guid, "Simple Index" },
     { &asf_object_file_properties_guid, "File Properties" },
     { &asf_object_stream_properties_guid, "Stream Properties" },
     { &asf_object_content_description_guid, "Content Description" },
@@ -1423,44 +1585,62 @@ static const struct
     { &asf_object_stream_type_video, "Stream Type Video" },
     { &asf_object_stream_type_command, "Stream Type Command" },
     { &asf_object_language_list, "Language List" },
-    { &asf_object_stream_bitrate_properties, "Stream Bitrate Propoerties" },
+    { &asf_object_stream_bitrate_properties, "Stream Bitrate Properties" },
     { &asf_object_padding, "Padding" },
-    { &asf_object_extended_stream_properties, "Extended Stream Properties" },
+    { &asf_object_extended_stream_properties_guid, "Extended Stream Properties" },
     { &asf_object_advanced_mutual_exclusion, "Advanced Mutual Exclusion" },
     { &asf_object_stream_prioritization, "Stream Prioritization" },
+    { &asf_object_bitrate_mutual_exclusion_guid, "Bitrate Mutual Exclusion" },
     { &asf_object_extended_content_description, "Extended content description"},
+    { &asf_object_content_encryption_guid, "Content Encryption"},
+    { &asf_object_advanced_content_encryption_guid, "Advanced Content Encryption"},
+    { &asf_object_extended_content_encryption_guid, "Entended Content Encryption"},
+    /* Non Readable from this point */
+    { &nonasf_object_index_placeholder_guid, "Index Placeholder"},
+    { &nonasf_object_compatibility, "Object Compatibility"},
 
     { NULL, "Unknown" },
 };
 
 
 static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
-                                 asf_object_common_t *p_node, int i_level )
+                                 asf_object_common_t *p_node, unsigned i_level )
 {
-    char str[1024];
-    int i;
+    unsigned i;
     union asf_object_u *p_child;
     const char *psz_name;
 
     /* Find the name */
     for( i = 0; ASF_ObjectDumpDebugInfo[i].p_id != NULL; i++ )
     {
-        if( ASF_CmpGUID( ASF_ObjectDumpDebugInfo[i].p_id,
+        if( guidcmp( ASF_ObjectDumpDebugInfo[i].p_id,
                           &p_node->i_object_id ) )
             break;
     }
     psz_name = ASF_ObjectDumpDebugInfo[i].psz_name;
 
+    char str[512];
+    if( i_level >= (sizeof(str) - 1)/5 )
+        return;
+
     memset( str, ' ', sizeof( str ) );
-    for( i = 1; i < i_level; i++ )
+    for( i = 0; i < i_level; i++ )
     {
-        str[i * 5] = '|';
+        str[i * 4] = '|';
     }
-    snprintf( str + 5*i_level, 1024,
-             "+ '%s' GUID "GUID_FMT" size:%"PRIu64"pos:%"PRIu64,
-             psz_name,
-             GUID_PRINT( p_node->i_object_id ),
-             p_node->i_object_size, p_node->i_object_pos );
+    snprintf( &str[4*i_level], sizeof(str) - 5*i_level,
+             "+ '%s'"
+#ifdef ASF_DEBUG
+             "GUID "GUID_FMT" size:%"PRIu64" pos:%"PRIu64
+#endif
+             , psz_name
+
+#ifdef ASF_DEBUG
+             , GUID_PRINT( p_node->i_object_id ),
+             p_node->i_object_size, p_node->i_object_pos
+#endif
+             );
+
 
     msg_Dbg( p_obj, "%s", str );
 
@@ -1478,9 +1658,13 @@ asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
 {
     asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
     asf_object_t *p_obj;
+    uint64_t i_boundary = 0;
+
+    if( !p_root )
+        return NULL;
 
     p_root->i_type = ASF_OBJECT_ROOT;
-    memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
+    memcpy( &p_root->i_object_id, &vlc_object_root_guid, sizeof( guid_t ) );
     p_root->i_object_pos = stream_Tell( s );
     p_root->i_object_size = 0;
     p_root->p_first = NULL;
@@ -1496,7 +1680,7 @@ asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
     {
         p_obj = malloc( sizeof( asf_object_t ) );
 
-        if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
+        if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
         {
             free( p_obj );
             break;
@@ -1504,18 +1688,29 @@ asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
         switch( p_obj->common.i_type )
         {
             case( ASF_OBJECT_HEADER ):
+                if ( p_root->p_index || p_root->p_data || p_root->p_hdr ) break;
                 p_root->p_hdr = (asf_object_header_t*)p_obj;
                 break;
             case( ASF_OBJECT_DATA ):
+                if ( p_root->p_index || p_root->p_data ) break;
                 p_root->p_data = (asf_object_data_t*)p_obj;
-                break;
+            break;
             case( ASF_OBJECT_INDEX ):
+                if ( p_root->p_index ) break;
                 p_root->p_index = (asf_object_index_t*)p_obj;
                 break;
             default:
-                msg_Warn( s, "unknow object found" );
+                msg_Warn( s, "unknown top-level object found: " GUID_FMT,
+                      GUID_PRINT( p_obj->common.i_object_id ) );
                 break;
         }
+
+        /* Set a limit to avoid junk when possible */
+        if ( guidcmp( &p_obj->common.i_object_id, &asf_object_file_properties_guid ) )
+        {
+            i_boundary = p_obj->file_properties.i_file_size;
+        }
+
         if( p_obj->common.i_type == ASF_OBJECT_DATA &&
             p_obj->common.i_object_size <= 50 )
         {
@@ -1528,10 +1723,8 @@ asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
             break;
         }
 
-        if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
-        {
+        if( ASF_NextObject( s, p_obj, i_boundary ) ) /* Go to the next object */
             break;
-        }
     }
 
     if( p_root->p_hdr != NULL && p_root->p_data != NULL )
@@ -1547,19 +1740,18 @@ asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
             if( p_hdr_ext )
             {
                 int i_ext_stream;
-                int i;
 
                 p_root->p_metadata =
                     ASF_FindObject( p_hdr_ext,
                                     &asf_object_metadata_guid, 0 );
                 /* Special case for broken designed file format :( */
                 i_ext_stream = ASF_CountObject( p_hdr_ext,
-                                    &asf_object_extended_stream_properties );
-                for( i = 0; i < i_ext_stream; i++ )
+                                    &asf_object_extended_stream_properties_guid );
+                for( int i = 0; i < i_ext_stream; i++ )
                 {
                     asf_object_t *p_esp =
                         ASF_FindObject( p_hdr_ext,
-                                   &asf_object_extended_stream_properties, i );
+                                   &asf_object_extended_stream_properties_guid, i );
                     if( p_esp->ext_stream.p_sp )
                     {
                         asf_object_t *p_sp =
@@ -1601,46 +1793,45 @@ void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
     free( p_root );
 }
 
-int  __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
+int ASF_CountObject( void *_p_obj, const guid_t *p_guid )
 {
     int i_count;
-    asf_object_t *p_child;
+    asf_object_t *p_child, *p_obj;
 
-    if( !p_obj ) return( 0 );
+    p_obj = (asf_object_t *)_p_obj;
+    if( !p_obj )
+        return 0;
 
     i_count = 0;
     p_child = p_obj->common.p_first;
     while( p_child )
     {
-        if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
-        {
+        if( guidcmp( &p_child->common.i_object_id, p_guid ) )
             i_count++;
-        }
+
         p_child = p_child->common.p_next;
     }
-    return( i_count );
+    return i_count;
 }
 
-void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid,
+void *ASF_FindObject( void *_p_obj, const guid_t *p_guid,
                         int i_number )
 {
-    asf_object_t *p_child;
+    asf_object_t *p_child, *p_obj;
 
+    p_obj = (asf_object_t *)_p_obj;
     p_child = p_obj->common.p_first;
 
     while( p_child )
     {
-        if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
+        if( guidcmp( &p_child->common.i_object_id, p_guid ) )
         {
             if( i_number == 0 )
-            {
-                /* We found it */
-                return( p_child );
-            }
+                return p_child;
 
             i_number--;
         }
         p_child = p_child->common.p_next;
     }
-    return( NULL );
+    return NULL;
 }