From c40d83eb8df53fd61c1f4cdaf46f6b548db3d0dc Mon Sep 17 00:00:00 2001 From: Meiam <91270@qq.com> Date: Thu, 17 Sep 2026 10:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E8=BF=85=E9=9B=B7=E5=AD=97=E5=B9=95=E7=BC=96=E7=A0=81=E4=B8=BA?= =?UTF-8?q?=20UTF-8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Emby.MeiamSub.Thunder.csproj | 1 + Emby.MeiamSub.Thunder/ThunderProvider.cs | 4 +- .../Jellyfin.MeiamSub.Thunder.csproj | 1 + Jellyfin.MeiamSub.Thunder/ThunderProvider.cs | 7 ++- Shared/SubtitleContentValidator.cs | 59 ++++++++++++++---- .../MeiamSubtitles.Tests.csproj | 1 + .../SubtitleContentValidatorTests.cs | 61 +++++++++++++++++++ 7 files changed, 119 insertions(+), 15 deletions(-) diff --git a/Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj b/Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj index a01b4da..ada29ea 100644 --- a/Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj +++ b/Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj @@ -27,6 +27,7 @@ + diff --git a/Emby.MeiamSub.Thunder/ThunderProvider.cs b/Emby.MeiamSub.Thunder/ThunderProvider.cs index eccc758..1f06657 100644 --- a/Emby.MeiamSub.Thunder/ThunderProvider.cs +++ b/Emby.MeiamSub.Thunder/ThunderProvider.cs @@ -286,14 +286,14 @@ namespace Emby.MeiamSub.Thunder await response.Content.CopyToAsync(stream, 81920, cancellationToken); var data = stream.ToArray(); stream.Dispose(); - SubtitleContentValidator.Validate(data, format, response.ContentType); + var utf8Data = SubtitleContentValidator.ValidateAndConvertToUtf8(data, format, response.ContentType); return new SubtitleResponse() { Language = downloadSub.Language, IsForced = downloadSub.IsForced ?? false, Format = format, - Stream = new MemoryStream(data, writable: false), + Stream = new MemoryStream(utf8Data, writable: false), }; } } diff --git a/Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj b/Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj index 4b39a97..477821b 100644 --- a/Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj +++ b/Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj @@ -14,6 +14,7 @@ + diff --git a/Jellyfin.MeiamSub.Thunder/ThunderProvider.cs b/Jellyfin.MeiamSub.Thunder/ThunderProvider.cs index a2976c7..146d857 100644 --- a/Jellyfin.MeiamSub.Thunder/ThunderProvider.cs +++ b/Jellyfin.MeiamSub.Thunder/ThunderProvider.cs @@ -283,8 +283,11 @@ namespace Jellyfin.MeiamSub.Thunder var format = NormalizeFormat(downloadSub.Format) ?? throw new InvalidDataException($"Unsupported subtitle format: {downloadSub.Format}"); var data = await response.Content.ReadAsByteArrayAsync(cancellationToken); - SubtitleContentValidator.Validate(data, format, response.Content.Headers.ContentType?.MediaType); - var stream = new MemoryStream(data, writable: false); + var utf8Data = SubtitleContentValidator.ValidateAndConvertToUtf8( + data, + format, + response.Content.Headers.ContentType?.MediaType); + var stream = new MemoryStream(utf8Data, writable: false); return new SubtitleResponse() { diff --git a/Shared/SubtitleContentValidator.cs b/Shared/SubtitleContentValidator.cs index 4871972..f10010d 100644 --- a/Shared/SubtitleContentValidator.cs +++ b/Shared/SubtitleContentValidator.cs @@ -7,7 +7,27 @@ namespace MeiamSubtitles.Shared { internal static class SubtitleContentValidator { + private static readonly Encoding StrictUtf8 = new UTF8Encoding(false, true); + private static readonly Encoding StrictUtf16Le = new UnicodeEncoding(false, true, true); + private static readonly Encoding StrictUtf16Be = new UnicodeEncoding(true, true, true); + + static SubtitleContentValidator() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + } + public static void Validate(byte[] data, string format, string mediaType) + { + DecodeAndValidate(data, format, mediaType); + } + + public static byte[] ValidateAndConvertToUtf8(byte[] data, string format, string mediaType) + { + var text = DecodeAndValidate(data, format, mediaType); + return Encoding.UTF8.GetBytes(text); + } + + private static string DecodeAndValidate(byte[] data, string format, string mediaType) { if (data == null || data.Length < 8) { @@ -38,7 +58,7 @@ namespace MeiamSubtitles.Shared if (IsValidSubtitle(sample, format)) { - return; + return sample; } if (sample.StartsWith("[", StringComparison.Ordinal)) @@ -65,35 +85,52 @@ namespace MeiamSubtitles.Shared private static IEnumerable DecodeSamples(byte[] data) { - var length = Math.Min(data.Length, 4096); + // Decode the complete subtitle because the selected text is returned to + // the host after validation and must not be truncated to a probe window. + var length = data.Length; if (data.Length >= 2) { if (data[0] == 0xFF && data[1] == 0xFE) { - yield return DecodeSample(data, length, Encoding.Unicode); + yield return DecodeSample(data, length, StrictUtf16Le); yield break; } if (data[0] == 0xFE && data[1] == 0xFF) { - yield return DecodeSample(data, length, Encoding.BigEndianUnicode); + yield return DecodeSample(data, length, StrictUtf16Be); yield break; } } if (data.Length >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF) { - yield return DecodeSample(data, length, Encoding.UTF8); + yield return DecodeSample(data, length, StrictUtf8); yield break; } - // Thunder may return UTF-16 subtitles without a BOM. Try the common - // single-byte/UTF-8 representation and both UTF-16 byte orders, then - // accept only a candidate that contains the expected subtitle syntax. - yield return DecodeSample(data, length, new UTF8Encoding(false, false)); - yield return DecodeSample(data, length, Encoding.Unicode); - yield return DecodeSample(data, length, Encoding.BigEndianUnicode); + // Thunder may return UTF-16 or GB18030 subtitles without a BOM. Decode + // strictly so invalid UTF-8 cannot hide the actual legacy encoding. + foreach (var encoding in new[] { StrictUtf8, StrictUtf16Le, StrictUtf16Be, GetGb18030Encoding() }) + { + string sample; + try + { + sample = DecodeSample(data, length, encoding); + } + catch (DecoderFallbackException) + { + continue; + } + + yield return sample; + } + } + + private static Encoding GetGb18030Encoding() + { + return Encoding.GetEncoding(54936, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback); } private static string DecodeSample(byte[] data, int length, Encoding encoding) diff --git a/tests/MeiamSubtitles.Tests/MeiamSubtitles.Tests.csproj b/tests/MeiamSubtitles.Tests/MeiamSubtitles.Tests.csproj index f8ba95c..cabb0bd 100644 --- a/tests/MeiamSubtitles.Tests/MeiamSubtitles.Tests.csproj +++ b/tests/MeiamSubtitles.Tests/MeiamSubtitles.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/tests/MeiamSubtitles.Tests/SubtitleContentValidatorTests.cs b/tests/MeiamSubtitles.Tests/SubtitleContentValidatorTests.cs index 8da86fe..e7e81d7 100644 --- a/tests/MeiamSubtitles.Tests/SubtitleContentValidatorTests.cs +++ b/tests/MeiamSubtitles.Tests/SubtitleContentValidatorTests.cs @@ -32,6 +32,67 @@ public class SubtitleContentValidatorTests SubtitleContentValidator.Validate(data, "srt", "application/octet-stream"); } + [Fact] + public void ConvertsGb18030SrtToUtf8ForPreview() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + var content = "1\r\n00:00:01,000 --> 00:00:02,000\r\n中文字幕\r\n"; + var data = Encoding.GetEncoding(54936).GetBytes(content); + + var normalized = SubtitleContentValidator.ValidateAndConvertToUtf8( + data, + "srt", + "application/octet-stream"); + + Assert.Equal(content, Encoding.UTF8.GetString(normalized)); + } + + [Fact] + public void PreservesCompleteUtf8SubtitleWhenNormalizing() + { + var content = "1\r\n00:00:01,000 --> 00:00:20,000\r\n" + new string('中', 3000) + "\r\n"; + + var normalized = SubtitleContentValidator.ValidateAndConvertToUtf8( + Encoding.UTF8.GetBytes(content), + "srt", + "application/octet-stream"); + + Assert.Equal(content, Encoding.UTF8.GetString(normalized)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void ConvertsUtf16SrtToUtf8ForPreview(bool bigEndian) + { + var encoding = new UnicodeEncoding(bigEndian, true); + var content = "1\r\n00:00:01,000 --> 00:00:02,000\r\n中文字幕\r\n"; + var data = encoding.GetPreamble().Concat(encoding.GetBytes(content)).ToArray(); + + var normalized = SubtitleContentValidator.ValidateAndConvertToUtf8( + data, + "srt", + "application/octet-stream"); + + Assert.Equal(content, Encoding.UTF8.GetString(normalized)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void ConvertsUtf16WithoutBomToUtf8ForPreview(bool bigEndian) + { + var encoding = new UnicodeEncoding(bigEndian, false); + var content = "1\r\n00:00:01,000 --> 00:00:02,000\r\n中文字幕\r\n"; + + var normalized = SubtitleContentValidator.ValidateAndConvertToUtf8( + encoding.GetBytes(content), + "srt", + "application/octet-stream"); + + Assert.Equal(content, Encoding.UTF8.GetString(normalized)); + } + [Theory] [InlineData(false, false)] [InlineData(false, true)]