From cc15b60e92efaf7c7f503f822145bb13874399a2 Mon Sep 17 00:00:00 2001 From: Harry Cordewener Date: Wed, 3 Jan 2024 21:23:45 -0600 Subject: [PATCH] Allow the overriding of the Allowed Encodings. --- .../Interpreters/TelnetCharsetInterpreter.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/TelnetNegotiationCore/Interpreters/TelnetCharsetInterpreter.cs b/TelnetNegotiationCore/Interpreters/TelnetCharsetInterpreter.cs index 8e4340e..e6a025e 100644 --- a/TelnetNegotiationCore/Interpreters/TelnetCharsetInterpreter.cs +++ b/TelnetNegotiationCore/Interpreters/TelnetCharsetInterpreter.cs @@ -38,6 +38,8 @@ public partial class TelnetInterpreter private bool charsetOffered = false; + private Func> AllowedEncodings { get; set; } = Encoding.GetEncodings; + private Func, IOrderedEnumerable> _charsetOrder = (x) => x.Select(y => y.GetEncoding()).OrderBy(z => z.EncodingName); public Lazy SupportedCharacterSets { get; } @@ -205,7 +207,7 @@ private async Task CompleteCharsetAsync(StateMachine.Transition _Logger.Debug("Charsets offered to us: {@charsetResultDebug}", charsetsOffered); - var encodingDict = Encoding.GetEncodings().ToDictionary(x => x.GetEncoding().WebName); + var encodingDict = AllowedEncodings().ToDictionary(x => x.GetEncoding().WebName); var offeredEncodingInfo = charsetsOffered.Select(x => { try { return encodingDict[Encoding.GetEncoding(x).WebName]; } catch { return null; } }).Where(x => x != null); var preferredEncoding = _charsetOrder(offeredEncodingInfo); var chosenEncoding = preferredEncoding.FirstOrDefault(); @@ -273,7 +275,7 @@ private async Task WillingCharsetAsync() /// private async Task OnDoCharsetAsync(StateMachine.Transition _) { - _Logger.Debug("Charsets String: {CharsetList}", ";" + string.Join(";", _charsetOrder(Encoding.GetEncodings()).Select(x => x.WebName))); + _Logger.Debug("Charsets String: {CharsetList}", ";" + string.Join(";", _charsetOrder(AllowedEncodings()).Select(x => x.WebName))); await CallbackNegotiationAsync(SupportedCharacterSets.Value); charsetOffered = true; } @@ -286,7 +288,7 @@ private byte[] CharacterSets() { byte[] pre = [(byte)Trigger.IAC, (byte)Trigger.SB, (byte)Trigger.CHARSET, (byte)Trigger.REQUEST]; byte[] post = [(byte)Trigger.IAC, (byte)Trigger.SE]; - byte[] defaultCharsets = ascii.GetBytes($";{string.Join(";", _charsetOrder(Encoding.GetEncodings()).Select(x => x.WebName))}"); + byte[] defaultCharsets = ascii.GetBytes($";{string.Join(";", _charsetOrder(AllowedEncodings()).Select(x => x.WebName))}"); return [.. pre, .. defaultCharsets, .. post]; } }