Index: entagged-sharp/EncodingInfo.cs =================================================================== RCS file: /cvs/gnome/banshee/entagged-sharp/EncodingInfo.cs,v retrieving revision 1.6 diff -p -u -2 -r1.6 EncodingInfo.cs --- entagged-sharp/EncodingInfo.cs 26 Nov 2005 01:52:35 -0000 1.6 +++ entagged-sharp/EncodingInfo.cs 26 Nov 2005 21:04:41 -0000 @@ -103,3 +103,74 @@ namespace Entagged.Audioformats } } + + public class UnicodeValidator { + + public static bool ValidateUTF8(byte[] str) + { + int i; + int min = 0, val = 0; + + try { + for(i = 0; i < str.Length; i++) { + if(str[i] < 128) + continue; + + if((str[i] & 0xe0) == 0xc0) { /* 110xxxxx */ + if((str[i] & 0x1e) == 0) + return false; + i++; + if((str[i] & 0xc0) != 0x80) /* 10xxxxxx */ + return false; + } else { + if((str[i] & 0xf0) == 0xe0) { /* 1110xxxx */ + min = (1 << 11); + val = str[i] & 0x0f; + goto TWO_REMAINING; + } else if((str[i] & 0xf8) == 0xf0) { /* 11110xxx */ + min = (1 << 16); + val = str[i] & 0x07; + } else { + return false; + } + i++; + if(!continuation_char(str, i, ref val)) + return false; + TWO_REMAINING: + i++; + if(!continuation_char(str, i, ref val)) + return false; + i++; + if(!continuation_char(str, i, ref val)) + return false; + + if(val < min || !unicode_valid(val)) + return false; + } + } + } catch (System.IndexOutOfRangeException e) { + return false; + } + + return true; + } + + private static bool continuation_char(byte[] str, int i, ref int val) + { + if ((str[i] & 0xc0) != 0x80) /* 10xxxxxx */ + return false; + + val <<= 6; + val |= str[i] & 0x3f; + + return true; + } + + private static bool unicode_valid(int b) + { + return (b < 0x110000 && + ((b & 0xFFFFF800) != 0xD800) && + (b < 0xFDD0 || b > 0xFDEF) && + (b & 0xFFFE) != 0xFFFE); + } + } } Index: entagged-sharp/Mp3/Util/Id3v1TagReader.cs =================================================================== RCS file: /cvs/gnome/banshee/entagged-sharp/Mp3/Util/Id3v1TagReader.cs,v retrieving revision 1.6 diff -p -u -2 -r1.6 Id3v1TagReader.cs --- entagged-sharp/Mp3/Util/Id3v1TagReader.cs 26 Nov 2005 01:52:38 -0000 1.6 +++ entagged-sharp/Mp3/Util/Id3v1TagReader.cs 26 Nov 2005 21:04:42 -0000 @@ -83,5 +83,10 @@ namespace Entagged.Audioformats.Mp3.Util byte[] b = new byte[length]; mp3Stream.Read( b, 0, b.Length ); - string ret = Encoding.GetEncoding("ISO-8859-1").GetString(b).Trim(); + string ret; + + if(Entagged.Audioformats.UnicodeValidator.ValidateUTF8(b)) + ret = Encoding.UTF8.GetString(b).Trim(); + else + ret = Encoding.GetEncoding("ISO-8859-1").GetString(b).Trim(); int pos = ret.IndexOf('\0');