]> git.sesse.net Git - vlc/blobdiff - modules/demux/asf/libasf.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / demux / asf / libasf.c
index 1a3819c76aa325c65c999eff589a2aad2e3ce273..80a5413d236394ce084f23dc5dc6130bd9cf179d 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <stdlib.h>                                      /* malloc(), free() */
-
 #include <vlc/vlc.h>
-#include <vlc/input.h>
 
-#include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
+
+#include <vlc_demux.h>
+
+#include <vlc_codecs.h>                   /* BITMAPINFOHEADER, WAVEFORMATEX */
 #include "libasf.h"
 
 #define ASF_DEBUG 1
 
-#define FREE( p ) \
-    if( p ) {free( p ); p = NULL; }
-
 #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,              \
@@ -51,7 +48,7 @@ static int ASF_ReadObject( stream_t *, asf_object_t *,  asf_object_t * );
 /****************************************************************************
  * GUID functions
  ****************************************************************************/
-void ASF_GetGUID( guid_t *p_guid, uint8_t *p_data )
+void ASF_GetGUID( guid_t *p_guid, const uint8_t *p_data )
 {
     p_guid->v1 = GetDWLE( p_data );
     p_guid->v2 = GetWLE( p_data + 4);
@@ -77,7 +74,7 @@ int ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
 {
     asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
-    uint8_t             *p_peek;
+    const uint8_t *p_peek;
 
     if( stream_Peek( s, &p_peek, 24 ) < 24 )
     {
@@ -141,7 +138,7 @@ 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_t        *p_subobj;
     int                 i_peek;
-    uint8_t             *p_peek;
+    const uint8_t       *p_peek;
 
     if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
     {
@@ -187,7 +184,7 @@ 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;
-    uint8_t           *p_peek;
+    const uint8_t     *p_peek;
 
     if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
     {
@@ -212,18 +209,21 @@ 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;
-    int                i_peek;
-    uint8_t            *p_peek;
+    const uint8_t      *p_peek;
+    int                 i;
 
-    if( ( i_peek = stream_Peek( s, &p_peek, 56 ) ) < 56 )
+    if( stream_Peek( s, &p_peek, p_index->i_object_size ) <
+        (int)p_index->i_object_size )
     {
-       return VLC_EGENERIC;
+        /* Just ignore */
+        return VLC_SUCCESS;
     }
+
     ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
     p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
     p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
     p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
-    p_index->index_entry = NULL; /* FIXME */
+    p_index->index_entry = NULL;
 
 #ifdef ASF_DEBUG
     msg_Dbg( s,
@@ -236,6 +236,20 @@ static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
             (long int)p_index->i_index_entry_count );
 #endif
 
+    /* Sanity checking */
+    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 );
+
+    for( i = 0, p_peek += 56; i < (int)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 );
+    }
+
     return VLC_SUCCESS;
 }
 
@@ -243,16 +257,16 @@ static void ASF_FreeObject_Index( asf_object_t *p_obj )
 {
     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
 
-    FREE( p_index->index_entry );
+    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;
-    uint8_t  *p_peek;
+    int           i_peek;
+    const uint8_t *p_peek;
 
-    if( ( i_peek = stream_Peek( s, &p_peek,  92) ) < 92 )
+    if( ( i_peek = stream_Peek( s, &p_peek,  104) ) < 104 )
     {
        return VLC_EGENERIC;
     }
@@ -306,7 +320,7 @@ static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
         (asf_object_metadata_t *)p_obj;
 
     int i_peek, i_entries, i;
-    uint8_t *p_peek;
+    const uint8_t *p_peek;
 #ifdef ASF_DEBUG
     unsigned int j;
 #endif
@@ -428,7 +442,7 @@ 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;
     int     i_peek;
-    uint8_t *p_peek;
+    const uint8_t *p_peek;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) <  46)
     {
@@ -485,20 +499,21 @@ 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;
 
-    FREE( p_he->p_header_extension_data );
+    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;
-    int     i_peek;
-    uint8_t *p_peek;
+    size_t        i_peek;
+    const uint8_t *p_peek;
 
-    if( ( i_peek = stream_Peek( s, &p_peek,  p_sp->i_object_size ) ) < 74 )
+    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 );
     p_sp->i_time_offset = GetQWLE( p_peek + 56 );
@@ -507,21 +522,42 @@ static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
     p_sp->i_flags = GetWLE( p_peek + 72 );
         p_sp->i_stream_number = p_sp->i_flags&0x07f;
     p_sp->i_reserved = GetDWLE( p_peek + 74 );
+    i_peek -= 78;
+
     if( p_sp->i_type_specific_data_length )
     {
+        if( i_peek < p_sp->i_type_specific_data_length )
+            return VLC_EGENERIC;
+
         p_sp->p_type_specific_data =
             malloc( p_sp->i_type_specific_data_length );
+        if( p_sp->p_type_specific_data == NULL )
+            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 )
     {
+        if( i_peek < p_sp->i_error_correction_data_length )
+        {
+            free( p_sp->p_type_specific_data );
+            return VLC_EGENERIC;
+        }
+
         p_sp->p_error_correction_data =
             malloc( p_sp->i_error_correction_data_length );
+        if( p_sp->p_error_correction_data == NULL )
+        {
+            free( p_sp->p_type_specific_data );
+            return VLC_ENOMEM;
+        }
         memcpy( p_sp->p_error_correction_data,
                 p_peek + 78 + p_sp->i_type_specific_data_length,
                 p_sp->i_error_correction_data_length );
@@ -554,8 +590,8 @@ 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;
 
-    FREE( p_sp->p_type_specific_data );
-    FREE( p_sp->p_error_correction_data );
+    FREENULL( p_sp->p_type_specific_data );
+    FREENULL( p_sp->p_error_correction_data );
 }
 
 
@@ -563,7 +599,7 @@ 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;
     int     i_peek;
-    uint8_t *p_peek, *p_data;
+    const uint8_t *p_peek, *p_data;
 
     unsigned int i_codec;
 
@@ -658,12 +694,12 @@ static void ASF_FreeObject_codec_list( 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]
-        FREE( codec.psz_name );
-        FREE( codec.psz_description );
-        FREE( codec.p_information );
+        FREENULL( codec.psz_name );
+        FREENULL( codec.psz_description );
+        FREENULL( codec.p_information );
 #undef  codec
     }
-    FREE( p_cl->codec );
+    FREENULL( p_cl->codec );
 }
 
 /* Microsoft should go to hell. This time the length give number of bytes
@@ -672,10 +708,10 @@ 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;
-    uint8_t *p_peek, *p_data;
-    int i_peek, i_title, i_author, i_copyright, i_description, i_rating;
+    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;
-    char *ib = NULL;
+    const char *ib = NULL;
     char *ob = NULL;
     size_t i_ibl, i_obl, i_len;
 
@@ -688,7 +724,7 @@ static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
 /* 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 = (char *)p_data; \
+    ib = (const char *)p_data; \
     ob = psz_str; \
     i_ibl = i_size; \
     i_obl = i_size*3; \
@@ -702,13 +738,13 @@ static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
     p_data = p_peek + 24;
 
     i_title = GetWLE( p_data ); p_data += 2;
-    i_author= 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;
 
     GETSTRINGW( p_cd->psz_title, i_title );
-    GETSTRINGW( p_cd->psz_author, i_author );
+    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 );
@@ -717,9 +753,9 @@ static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
 
 #ifdef ASF_DEBUG
     msg_Dbg( s,
-             "read \"content description object\" title:\"%s\" author:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
+             "read \"content description object\" title:\"%s\" artist:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
              p_cd->psz_title,
-             p_cd->psz_author,
+             p_cd->psz_artist,
              p_cd->psz_copyright,
              p_cd->psz_description,
              p_cd->psz_rating );
@@ -734,11 +770,11 @@ 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;
 
-    FREE( p_cd->psz_title );
-    FREE( p_cd->psz_author );
-    FREE( p_cd->psz_copyright );
-    FREE( p_cd->psz_description );
-    FREE( p_cd->psz_rating );
+    FREENULL( p_cd->psz_title );
+    FREENULL( p_cd->psz_artist );
+    FREENULL( p_cd->psz_copyright );
+    FREENULL( p_cd->psz_description );
+    FREENULL( p_cd->psz_rating );
 }
 
 /* Language list: */
@@ -746,7 +782,7 @@ 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;
-    uint8_t *p_peek, *p_data;
+    const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
 
@@ -779,10 +815,10 @@ static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
     }
 
 #ifdef ASF_DEBUG
-    msg_Dbg( s, "read \"language list object\" %d entries", 
+    msg_Dbg( s, "read \"language list object\" %d entries",
              p_ll->i_language );
     for( i = 0; i < p_ll->i_language; i++ )
-        msg_Dbg( s, "  - '%s'", 
+        msg_Dbg( s, "  - '%s'",
                  p_ll->ppsz_language[i] );
 #endif
     return VLC_SUCCESS;
@@ -795,8 +831,8 @@ static void ASF_FreeObject_language_list( asf_object_t *p_obj)
     int i;
 
     for( i = 0; i < p_ll->i_language; i++ )
-        FREE( p_ll->ppsz_language[i] );
-    FREE( p_ll->ppsz_language );
+        FREENULL( p_ll->ppsz_language[i] );
+    FREENULL( p_ll->ppsz_language );
 }
 
 /* Stream bitrate properties */
@@ -805,7 +841,7 @@ static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
 {
     asf_object_stream_bitrate_properties_t *p_sb =
         (asf_object_stream_bitrate_properties_t *)p_obj;
-    uint8_t *p_peek, *p_data;
+    const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
 
@@ -830,7 +866,7 @@ static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
     {
         msg_Dbg( s,"  - stream=%d bitrate=%d",
                  p_sb->bitrate[i].i_stream_number,
-                 p_sb->bitrate[i].i_avg_bitrate ); 
+                 p_sb->bitrate[i].i_avg_bitrate );
     }
 #endif
     return VLC_SUCCESS;
@@ -844,7 +880,7 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
 {
     asf_object_extended_stream_properties_t *p_esp =
         (asf_object_extended_stream_properties_t*)p_obj;
-    uint8_t *p_peek, *p_data;
+    const uint8_t *p_peek, *p_data;
     int i_peek, i;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
@@ -866,7 +902,7 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
     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] );
-    p_esp->i_payload_extention_system_count = GetWLE( &p_data[62] );
+    p_esp->i_payload_extension_system_count = GetWLE( &p_data[62] );
 
     p_data += 64;
 
@@ -895,7 +931,7 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
         p_esp->ppsz_stream_name[i] = psz;
     }
 
-    for( i = 0; i < p_esp->i_payload_extention_system_count; i++ )
+    for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
     {
         /* Skip them */
         int i_size = GetDWLE( &p_data[16 + 2] );
@@ -909,7 +945,7 @@ 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 ) )
@@ -946,8 +982,8 @@ static int ASF_ReadObject_extended_stream_properties( stream_t *s,
         msg_Dbg( s, "     - lang id=%d name=%s",
                  p_esp->pi_stream_name_language[i],
                  p_esp->ppsz_stream_name[i] );
-    msg_Dbg( s, "  - payload extention system count=%d",
-             p_esp->i_payload_extention_system_count );
+    msg_Dbg( s, "  - payload extension system count=%d",
+             p_esp->i_payload_extension_system_count );
 #endif
     return VLC_SUCCESS;
 }
@@ -958,9 +994,9 @@ static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
     int i;
 
     for( i = 0; i < p_esp->i_stream_name_count; i++ )
-        FREE( p_esp->ppsz_stream_name[i] );
-    FREE( p_esp->pi_stream_name_language );
-    FREE( p_esp->ppsz_stream_name );
+        FREENULL( p_esp->ppsz_stream_name[i] );
+    FREENULL( p_esp->pi_stream_name_language );
+    FREENULL( p_esp->ppsz_stream_name );
 }
 
 
@@ -969,7 +1005,7 @@ static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
 {
     asf_object_advanced_mutual_exclusion_t *p_ae =
         (asf_object_advanced_mutual_exclusion_t *)p_obj;
-    uint8_t *p_peek, *p_data;
+    const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
 
@@ -989,7 +1025,7 @@ static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
         p_ae->pi_stream_number[i] = GetWLE( p_data );
         p_data += 2;
     }
-        
 #ifdef ASF_DEBUG
     msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
     for( i = 0; i < p_ae->i_stream_number_count; i++ )
@@ -1002,7 +1038,7 @@ 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;
 
-    FREE( p_ae->pi_stream_number );
+    FREENULL( p_ae->pi_stream_number );
 }
 
 
@@ -1011,7 +1047,7 @@ static int ASF_ReadObject_stream_prioritization( stream_t *s,
 {
     asf_object_stream_prioritization_t *p_sp =
         (asf_object_stream_prioritization_t *)p_obj;
-    uint8_t *p_peek, *p_data;
+    const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
 
@@ -1046,8 +1082,8 @@ 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;
 
-    FREE( p_sp->pi_priority_stream_number );
-    FREE( p_sp->pi_priority_flag );
+    FREENULL( p_sp->pi_priority_stream_number );
+    FREENULL( p_sp->pi_priority_flag );
 }
 
 
@@ -1056,7 +1092,7 @@ static int ASF_ReadObject_extended_content_description( stream_t *s,
 {
     asf_object_extended_content_description_t *p_ec =
         (asf_object_extended_content_description_t *)p_obj;
-    uint8_t *p_peek, *p_data;
+    const uint8_t *p_peek, *p_data;
     int i_peek;
     int i;
 
@@ -1129,7 +1165,7 @@ static int ASF_ReadObject_extended_content_description( stream_t *s,
             p_ec->ppsz_value[i] = NULL;
 
         p_data += i_size;
-        
 
 
 #undef GETSTRINGW
@@ -1153,9 +1189,11 @@ static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
 
     for( i = 0; i < p_ec->i_count; i++ )
     {
-        FREE( p_ec->ppsz_name[i] );
-        FREE( p_ec->ppsz_value[i] );
+        FREENULL( p_ec->ppsz_name[i] );
+        FREENULL( p_ec->ppsz_value[i] );
     }
+    FREENULL( p_ec->ppsz_name );
+    FREENULL( p_ec->ppsz_value );
 }
 
 
@@ -1164,7 +1202,8 @@ static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
 {
     asf_object_XXX_t *p_XX =
         (asf_object_XXX_t *)p_obj;
-    uint8_t *p_peek, *p_data;
+    const uint8_t *p_peek;
+    uint8_t *p_data;
     int i_peek;
 
     if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
@@ -1291,7 +1330,7 @@ static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
     }
 
     /* link this object with father */
-    if( p_father )
+    if( p_father && ! i_result )
     {
         if( p_father->common.p_first )
         {
@@ -1362,7 +1401,7 @@ static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
 static const struct
 {
     const guid_t *p_id;
-    char *psz_name;
+    const char *psz_name;
 } ASF_ObjectDumpDebugInfo[] =
 {
     { &asf_object_header_guid, "Header" },
@@ -1371,7 +1410,7 @@ static const struct
     { &asf_object_file_properties_guid, "File Properties" },
     { &asf_object_stream_properties_guid, "Stream Properties" },
     { &asf_object_content_description_guid, "Content Description" },
-    { &asf_object_header_extension_guid, "Header Extention" },
+    { &asf_object_header_extension_guid, "Header Extension" },
     { &asf_object_metadata_guid, "Metadata" },
     { &asf_object_codec_list_guid, "Codec List" },
     { &asf_object_marker_guid, "Marker" },
@@ -1396,7 +1435,7 @@ static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
     char str[1024];
     int i;
     union asf_object_u *p_child;
-    char *psz_name;
+    const char *psz_name;
 
     /* Find the name */
     for( i = 0; ASF_ObjectDumpDebugInfo[i].p_id != NULL; i++ )