+ /* BOM detection */
+ i_pos = stream_Tell( s );
+ if( i_pos == 0 && i_data > 4 )
+ {
+ int i_bom_size = 0;
+ char *psz_encoding = NULL;
+
+ if( p_data[0] == 0xEF && p_data[1] == 0xBB && p_data[2] == 0xBF )
+ {
+ psz_encoding = strdup( "UTF-8" );
+ i_bom_size = 3;
+ }
+ else if( p_data[0] == 0x00 && p_data[1] == 0x00 )
+ {
+ if( p_data[2] == 0xFE && p_data[3] == 0xFF )
+ {
+ psz_encoding = strdup( "UTF-32BE" );
+ s->i_char_width = 4;
+ i_bom_size = 4;
+ }
+ }
+ else if( p_data[0] == 0xFF && p_data[1] == 0xFE )
+ {
+ if( p_data[2] == 0x00 && p_data[3] == 0x00 )
+ {
+ psz_encoding = strdup( "UTF-32LE" );
+ s->i_char_width = 4;
+ s->b_little_endian = VLC_TRUE;
+ i_bom_size = 4;
+ }
+ else
+ {
+ psz_encoding = strdup( "UTF-16LE" );
+ s->b_little_endian = VLC_TRUE;
+ s->i_char_width = 2;
+ i_bom_size = 2;
+ }
+ }
+ else if( p_data[0] == 0xFE && p_data[1] == 0xFF )
+ {
+ psz_encoding = strdup( "UTF-16BE" );
+ s->i_char_width = 2;
+ i_bom_size = 2;
+ }
+ /* Seek past the offset */
+ stream_Seek( s, i_bom_size );
+ p_data += i_bom_size;
+ i_data -= i_bom_size;
+
+ /* Open the converter if we need it */
+ if( psz_encoding != NULL )
+ {
+ msg_Dbg( s, "%s BOM detected", psz_encoding );
+ s->conv = vlc_iconv_open( "UTF-8", psz_encoding );
+ if( s->conv == (vlc_iconv_t)-1 )
+ {
+ msg_Err( s, "iconv_open failed" );
+ }
+ if( psz_encoding ) free( psz_encoding );
+ }
+ }
+
+ if( i_data % s->i_char_width )
+ {
+ msg_Warn( s, "the read is not i_char_width compatible");
+ }
+