## Summary - Adds proper Latin1 encoding support, replacing CP1252 usage throughout the codebase - Adds specialized, optimized string decoding methods with safe string filtering for each encoding type - Filters invalid Unicode characters (C0/C1 control codes, non-characters) by removal rather than replacement since the UO client renders nothing for these characters - Fixes UTF-16 null terminator position handling to correctly advance by 2 bytes ## Changes TextEncoding.cs - Added SearchValues-based invalid byte/char detection for efficient filtering - Added encoding-specific GetString methods: GetStringAscii, GetStringLatin1, GetStringUtf8, GetStringBigUni, GetStringLittleUni - Each method supports a safeString parameter for filtering invalid characters - Little-endian UTF-16 uses direct memory cast for zero-copy decoding on LE systems - Invalid characters are removed (not replaced with U+FFFD) since the client renders nothing for them SpanReader.cs - Added ReadLatin1() and ReadLatin1Safe() methods - Rewrote encoding-specific read methods to use optimized TextEncoding.GetString* methods - Fixed UTF-16 null terminator handling: position now correctly advances by byteLength (2) instead of 1 SpanWriter.cs - Added WriteLatin1 and WriteLatin1Null methods ## Packet Updates - Updated all packet code to use Latin1 encoding instead of CP1252 - Affected: account packets, equipment packets, menu packets, message packets, mobile packets, player packets, secure trade packets, vendor packets, gump packets, book packets, mahjong packets ## Filtering Behavior Invalid characters filtered in safe mode: ``` ┌───────────────┬────────────────────────┐ │ Range │ Description │ ├───────────────┼────────────────────────┤ │ 0x00-0x1F │ C0 control codes │ ├───────────────┼────────────────────────┤ │ 0x7F │ DEL │ ├───────────────┼────────────────────────┤ │ 0x80-0x9F │ C1 control codes │ ├───────────────┼────────────────────────┤ │ 0xFFFE-0xFFFF │ Unicode non-characters │ └───────────────┴────────────────────────┘ ``` Note: Surrogate pairs (0xD800-0xDFFF) are not filtered because proper validation requires context checking for paired vs unpaired surrogates. The UO client renders nothing for these anyway. ## Test Plan - All 631 Server.Tests pass - Verified client rendering behavior using TestUnicodeGump command (pages 1-5) - Confirmed U+FFFD, unpaired surrogates, and non-characters all render as blank in client - Verified Latin1 characters (0xA0-0xFF) display correctly - Verified C1 control codes (0x80-0x9F) are filtered and don't display
141 lines
3.9 KiB
C#
141 lines
3.9 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2023 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: IncomingMessagePackets.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
*************************************************************************/
|
|
|
|
using System;
|
|
using System.Buffers;
|
|
|
|
namespace Server.Network;
|
|
|
|
public static class IncomingMessagePackets
|
|
{
|
|
private static readonly KeywordList m_KeywordList = new();
|
|
|
|
public static unsafe void Configure()
|
|
{
|
|
IncomingPackets.Register(0x03, 0, true, &AsciiSpeech);
|
|
IncomingPackets.Register(0xAD, 0, true, &UnicodeSpeech);
|
|
}
|
|
|
|
public static void AsciiSpeech(NetState state, SpanReader reader)
|
|
{
|
|
var from = state.Mobile;
|
|
|
|
if (from == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var type = (MessageType)reader.ReadByte();
|
|
int hue = reader.ReadInt16();
|
|
reader.ReadInt16(); // font
|
|
var text = reader.ReadLatin1Safe().Trim();
|
|
|
|
if (text.Length is <= 0 or > 128)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!Enum.IsDefined(typeof(MessageType), type))
|
|
{
|
|
type = MessageType.Regular;
|
|
}
|
|
|
|
from.DoSpeech(text, Array.Empty<int>(), type, Utility.ClipDyedHue(hue));
|
|
}
|
|
|
|
public static void UnicodeSpeech(NetState state, SpanReader reader)
|
|
{
|
|
var from = state.Mobile;
|
|
|
|
if (from == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var type = (MessageType)reader.ReadByte();
|
|
int hue = reader.ReadInt16();
|
|
reader.ReadInt16(); // font
|
|
var lang = reader.ReadAscii(4);
|
|
string text;
|
|
|
|
var isEncoded = (type & MessageType.Encoded) != 0;
|
|
int[] keywords;
|
|
|
|
if (isEncoded)
|
|
{
|
|
int value = reader.ReadInt16();
|
|
var count = (value & 0xFFF0) >> 4;
|
|
var hold = value & 0xF;
|
|
|
|
if (count is < 0 or > 50)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var keyList = m_KeywordList;
|
|
|
|
for (var i = 0; i < count; ++i)
|
|
{
|
|
int speechID;
|
|
|
|
if ((i & 1) == 0)
|
|
{
|
|
hold <<= 8;
|
|
hold |= reader.ReadByte();
|
|
speechID = hold;
|
|
hold = 0;
|
|
}
|
|
else
|
|
{
|
|
value = reader.ReadInt16();
|
|
speechID = (value & 0xFFF0) >> 4;
|
|
hold = value & 0xF;
|
|
}
|
|
|
|
if (!keyList.Contains(speechID))
|
|
{
|
|
keyList.Add(speechID);
|
|
}
|
|
}
|
|
|
|
text = reader.ReadUTF8Safe();
|
|
|
|
keywords = keyList.ToArray();
|
|
}
|
|
else
|
|
{
|
|
text = reader.ReadBigUniSafe();
|
|
|
|
keywords = Array.Empty<int>();
|
|
}
|
|
|
|
text = text.Trim();
|
|
|
|
if (text.Length is <= 0 or > 128)
|
|
{
|
|
return;
|
|
}
|
|
|
|
type &= ~MessageType.Encoded;
|
|
|
|
if (!Enum.IsDefined(typeof(MessageType), type))
|
|
{
|
|
type = MessageType.Regular;
|
|
}
|
|
|
|
from.Language = lang;
|
|
from.DoSpeech(text, keywords, type, Utility.ClipDyedHue(hue));
|
|
}
|
|
}
|