From ef3bfb0e3782bcd7cf6a2096ce4563020b7d1117 Mon Sep 17 00:00:00 2001 From: Alecio Furanze Date: Thu, 16 May 2024 11:42:39 +0200 Subject: [PATCH] fix string with encoding create exception --- src/Source/Reader.cs | 64 ++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/Source/Reader.cs b/src/Source/Reader.cs index ffd29ff..78e4742 100644 --- a/src/Source/Reader.cs +++ b/src/Source/Reader.cs @@ -17,7 +17,9 @@ public class Reader : IReader, IDisposable #region Builder // private for IDisposable - private Reader() { } + private Reader() + { + } public Reader(byte[] buffer, int offset = 0) { @@ -344,8 +346,8 @@ public T Read() // skip y bytes read _position += sizeof(float); - - + + // get encoded value var w = BitConverter.ToSingle(_buffer, _position); @@ -355,7 +357,10 @@ public T Read() return (T)(object)new Float4(x, y, z, w); } } - catch { } + catch + { + // + } _success = false; return default; @@ -363,34 +368,41 @@ public T Read() public T Read(Encoding encode) { - char prefix = Writer.GetPrefix(typeof(T)); - - // string - if (typeof(T) == typeof(string)) + try { - // compare (buffer prefix) to (type prefix) - if (!ValidPrefix(prefix)) return default; + char prefix = Writer.GetPrefix(typeof(T)); - // get length of bytes - int length = BitConverter.ToInt32(_buffer, _position); - _position += sizeof(int); - if (length <= 0 || length > (Length - _position)) + // string + if (typeof(T) == typeof(string)) { - _success = false; - return default; - } + // compare (buffer prefix) to (type prefix) + if (!ValidPrefix(prefix)) return default; - // get encoded value - byte[] bytes = new byte[length]; - Buffer.BlockCopy(_buffer, _position, bytes, 0, bytes.Length); + // get length of bytes + int length = BitConverter.ToInt32(_buffer, _position); + _position += sizeof(int); + if (length <= 0 || length > (Length - _position)) + { + _success = false; + return default; + } - // skip bytes read - _position += bytes.Length; + // get encoded value + byte[] bytes = new byte[length]; + Buffer.BlockCopy(_buffer, _position, bytes, 0, bytes.Length); + + // skip bytes read + _position += bytes.Length; - // convert bytes to string - string value = encode.GetString(bytes); + // convert bytes to string + string value = encode.GetString(bytes); - return (T)(object)value; + return (T)(object)value; + } + } + catch + { + // } _success = false; @@ -460,4 +472,4 @@ protected virtual void Dispose(bool disposing) #endregion } -} +} \ No newline at end of file