|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +namespace PartyTime |
| 7 | +{ |
| 8 | + public class Subtitles |
| 9 | + { |
| 10 | + public static string Convert(string fileName, Encoding input, Encoding output) |
| 11 | + { |
| 12 | + string tmpFile = null; |
| 13 | + try |
| 14 | + { |
| 15 | + tmpFile = Path.GetTempPath() + Guid.NewGuid().ToString() + ".srt"; |
| 16 | + StreamReader sr = new StreamReader(new FileStream(fileName, FileMode.Open), input); |
| 17 | + StreamWriter sw = new StreamWriter(new FileStream(tmpFile, FileMode.CreateNew), output); |
| 18 | + |
| 19 | + sw.Write(sr.ReadToEnd()); |
| 20 | + |
| 21 | + } catch (Exception) { return null; } |
| 22 | + |
| 23 | + return tmpFile; |
| 24 | + } |
| 25 | + public static Encoding Detect(string fileName) |
| 26 | + { |
| 27 | + Encoding ret = Encoding.Default; |
| 28 | + |
| 29 | + // Check if BOM |
| 30 | + if ((ret = CheckByBOM(fileName)) != Encoding.Default) return ret; |
| 31 | + |
| 32 | + // Check if UTF-8 |
| 33 | + using (BufferedStream fstream = new BufferedStream(File.OpenRead(fileName))) |
| 34 | + { |
| 35 | + if (IsUtf8(fstream)) ret = Encoding.UTF8; |
| 36 | + } |
| 37 | + |
| 38 | + return ret; |
| 39 | + } |
| 40 | + public static Encoding CheckByBOM(string path) |
| 41 | + { |
| 42 | + if (path == null) throw new System.ArgumentNullException("path"); |
| 43 | + |
| 44 | + var encodings = Encoding.GetEncodings() |
| 45 | + .Select(e => e.GetEncoding()) |
| 46 | + .Select(e => new { Encoding = e, Preamble = e.GetPreamble() }) |
| 47 | + .Where(e => e.Preamble.Any()) |
| 48 | + .ToArray(); |
| 49 | + |
| 50 | + var maxPrembleLength = encodings.Max(e => e.Preamble.Length); |
| 51 | + byte[] buffer = new byte[maxPrembleLength]; |
| 52 | + |
| 53 | + using (var stream = File.OpenRead(path)) |
| 54 | + { |
| 55 | + stream.Read(buffer, 0, (int)Math.Min(maxPrembleLength, stream.Length)); |
| 56 | + } |
| 57 | + |
| 58 | + return encodings |
| 59 | + .Where(enc => enc.Preamble.SequenceEqual(buffer.Take(enc.Preamble.Length))) |
| 60 | + .Select(enc => enc.Encoding) |
| 61 | + .FirstOrDefault() ?? Encoding.Default; |
| 62 | + } |
| 63 | + public static bool IsUtf8(Stream stream) |
| 64 | + { |
| 65 | + int count = 4 * 1024; |
| 66 | + byte[] buffer; |
| 67 | + int read; |
| 68 | + while (true) |
| 69 | + { |
| 70 | + buffer = new byte[count]; |
| 71 | + stream.Seek(0, SeekOrigin.Begin); |
| 72 | + read = stream.Read(buffer, 0, count); |
| 73 | + if (read < count) |
| 74 | + { |
| 75 | + break; |
| 76 | + } |
| 77 | + buffer = null; |
| 78 | + count *= 2; |
| 79 | + } |
| 80 | + return IsUtf8(buffer, read); |
| 81 | + } |
| 82 | + public static bool IsUtf8(byte[] buffer, int length) |
| 83 | + { |
| 84 | + int position = 0; |
| 85 | + int bytes = 0; |
| 86 | + while (position < length) |
| 87 | + { |
| 88 | + if (!IsValid(buffer, position, length, ref bytes)) |
| 89 | + { |
| 90 | + return false; |
| 91 | + } |
| 92 | + position += bytes; |
| 93 | + } |
| 94 | + return true; |
| 95 | + } |
| 96 | + public static bool IsValid(byte[] buffer, int position, int length, ref int bytes) |
| 97 | + { |
| 98 | + if (length > buffer.Length) |
| 99 | + { |
| 100 | + throw new ArgumentException("Invalid length"); |
| 101 | + } |
| 102 | + |
| 103 | + if (position > length - 1) |
| 104 | + { |
| 105 | + bytes = 0; |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + byte ch = buffer[position]; |
| 110 | + |
| 111 | + if (ch <= 0x7F) |
| 112 | + { |
| 113 | + bytes = 1; |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + if (ch >= 0xc2 && ch <= 0xdf) |
| 118 | + { |
| 119 | + if (position >= length - 2) |
| 120 | + { |
| 121 | + bytes = 0; |
| 122 | + return false; |
| 123 | + } |
| 124 | + if (buffer[position + 1] < 0x80 || buffer[position + 1] > 0xbf) |
| 125 | + { |
| 126 | + bytes = 0; |
| 127 | + return false; |
| 128 | + } |
| 129 | + bytes = 2; |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + if (ch == 0xe0) |
| 134 | + { |
| 135 | + if (position >= length - 3) |
| 136 | + { |
| 137 | + bytes = 0; |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + if (buffer[position + 1] < 0xa0 || buffer[position + 1] > 0xbf || |
| 142 | + buffer[position + 2] < 0x80 || buffer[position + 2] > 0xbf) |
| 143 | + { |
| 144 | + bytes = 0; |
| 145 | + return false; |
| 146 | + } |
| 147 | + bytes = 3; |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + if (ch >= 0xe1 && ch <= 0xef) |
| 153 | + { |
| 154 | + if (position >= length - 3) |
| 155 | + { |
| 156 | + bytes = 0; |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + if (buffer[position + 1] < 0x80 || buffer[position + 1] > 0xbf || |
| 161 | + buffer[position + 2] < 0x80 || buffer[position + 2] > 0xbf) |
| 162 | + { |
| 163 | + bytes = 0; |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + bytes = 3; |
| 168 | + return true; |
| 169 | + } |
| 170 | + |
| 171 | + if (ch == 0xf0) |
| 172 | + { |
| 173 | + if (position >= length - 4) |
| 174 | + { |
| 175 | + bytes = 0; |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + if (buffer[position + 1] < 0x90 || buffer[position + 1] > 0xbf || |
| 180 | + buffer[position + 2] < 0x80 || buffer[position + 2] > 0xbf || |
| 181 | + buffer[position + 3] < 0x80 || buffer[position + 3] > 0xbf) |
| 182 | + { |
| 183 | + bytes = 0; |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + bytes = 4; |
| 188 | + return true; |
| 189 | + } |
| 190 | + |
| 191 | + if (ch == 0xf4) |
| 192 | + { |
| 193 | + if (position >= length - 4) |
| 194 | + { |
| 195 | + bytes = 0; |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + if (buffer[position + 1] < 0x80 || buffer[position + 1] > 0x8f || |
| 200 | + buffer[position + 2] < 0x80 || buffer[position + 2] > 0xbf || |
| 201 | + buffer[position + 3] < 0x80 || buffer[position + 3] > 0xbf) |
| 202 | + { |
| 203 | + bytes = 0; |
| 204 | + return false; |
| 205 | + } |
| 206 | + |
| 207 | + bytes = 4; |
| 208 | + return true; |
| 209 | + } |
| 210 | + |
| 211 | + if (ch >= 0xf1 && ch <= 0xf3) |
| 212 | + { |
| 213 | + if (position >= length - 4) |
| 214 | + { |
| 215 | + bytes = 0; |
| 216 | + return false; |
| 217 | + } |
| 218 | + |
| 219 | + if (buffer[position + 1] < 0x80 || buffer[position + 1] > 0xbf || |
| 220 | + buffer[position + 2] < 0x80 || buffer[position + 2] > 0xbf || |
| 221 | + buffer[position + 3] < 0x80 || buffer[position + 3] > 0xbf) |
| 222 | + { |
| 223 | + bytes = 0; |
| 224 | + return false; |
| 225 | + } |
| 226 | + |
| 227 | + bytes = 4; |
| 228 | + return true; |
| 229 | + } |
| 230 | + |
| 231 | + return false; |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments