2016-07-28 22:25:26 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:23 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-07-28 22:25:26 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : StringHandlers.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Helpers.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Convert byte arrays to C# strings.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2016-07-28 22:25:26 +01:00
|
|
|
// ****************************************************************************/
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2017-06-06 21:38:30 +01:00
|
|
|
using System;
|
2023-10-05 01:05:21 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2017-12-19 19:33:46 +00:00
|
|
|
using System.Text;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
namespace Aaru.Helpers;
|
|
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Helper operations to work with strings</summary>
|
2023-10-05 01:05:21 +01:00
|
|
|
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
2022-03-06 13:29:37 +00:00
|
|
|
public static class StringHandlers
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Converts a null-terminated (aka C string) ASCII byte array to a C# string</summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="cString">A null-terminated (aka C string) ASCII byte array</param>
|
|
|
|
|
public static string CToString(byte[] cString) => CToString(cString, Encoding.ASCII);
|
|
|
|
|
|
|
|
|
|
/// <summary>Converts a null-terminated (aka C string) byte array with the specified encoding to a C# string</summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="cString">A null-terminated (aka C string) byte array in the specified encoding</param>
|
|
|
|
|
/// <param name="encoding">Encoding.</param>
|
|
|
|
|
/// <param name="twoBytes">Set if encoding uses 16-bit characters.</param>
|
|
|
|
|
/// <param name="start">Start decoding at this position</param>
|
|
|
|
|
public static string CToString(byte[] cString, Encoding encoding, bool twoBytes = false, int start = 0)
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(cString == null) return null;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
2023-10-03 23:25:24 +01:00
|
|
|
var len = 0;
|
2016-04-19 02:11:47 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
for(int i = start; i < cString.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if(cString[i] == 0)
|
2023-10-03 23:25:24 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
if(twoBytes)
|
|
|
|
|
{
|
2023-10-04 17:34:37 +01:00
|
|
|
if(i + 1 < cString.Length && cString[i + 1] == 0)
|
2017-07-11 02:32:40 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
len++;
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
break;
|
2022-03-06 13:29:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break;
|
2023-10-03 23:25:24 +01:00
|
|
|
}
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
len++;
|
|
|
|
|
}
|
2016-04-19 02:11:47 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(twoBytes && len % 2 > 0) len--;
|
2019-04-14 16:36:43 +01:00
|
|
|
|
2023-10-03 23:25:24 +01:00
|
|
|
var dest = new byte[len];
|
2022-03-06 13:29:37 +00:00
|
|
|
Array.Copy(cString, start, dest, 0, len);
|
2017-06-06 21:38:30 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return len == 0 ? "" : encoding.GetString(dest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string</summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="pascalString">A length-prefixed (aka Pascal string) ASCII byte array</param>
|
|
|
|
|
public static string PascalToString(byte[] pascalString) => PascalToString(pascalString, Encoding.ASCII);
|
|
|
|
|
|
|
|
|
|
/// <summary>Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string</summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="pascalString">A length-prefixed (aka Pascal string) ASCII byte array</param>
|
|
|
|
|
/// <param name="encoding">Encoding.</param>
|
|
|
|
|
/// <param name="start">Start decoding at this position</param>
|
|
|
|
|
public static string PascalToString(byte[] pascalString, Encoding encoding, int start = 0)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(pascalString == null) return null;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
byte length = pascalString[start];
|
2023-10-03 23:25:24 +01:00
|
|
|
var len = 0;
|
2022-03-06 13:29:37 +00:00
|
|
|
|
|
|
|
|
for(int i = start + 1; i < length + 1 && i < pascalString.Length; i++)
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(pascalString[i] == 0) break;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
len++;
|
|
|
|
|
}
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2023-10-03 23:25:24 +01:00
|
|
|
var dest = new byte[len];
|
2022-03-06 13:29:37 +00:00
|
|
|
Array.Copy(pascalString, start + 1, dest, 0, len);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return len == 0 ? "" : encoding.GetString(dest);
|
|
|
|
|
}
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string</summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="spacePaddedString">A space (' ', 0x20, ASCII SPACE) padded ASCII byte array</param>
|
|
|
|
|
public static string SpacePaddedToString(byte[] spacePaddedString) =>
|
|
|
|
|
SpacePaddedToString(spacePaddedString, Encoding.ASCII);
|
|
|
|
|
|
|
|
|
|
/// <summary>Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string</summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="spacePaddedString">A space (' ', 0x20, ASCII SPACE) padded ASCII byte array</param>
|
|
|
|
|
/// <param name="encoding">Encoding.</param>
|
|
|
|
|
/// <param name="start">Start decoding at this position</param>
|
|
|
|
|
public static string SpacePaddedToString(byte[] spacePaddedString, Encoding encoding, int start = 0)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(spacePaddedString == null) return null;
|
2017-06-06 21:38:30 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
int len = start;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
for(int i = spacePaddedString.Length; i >= start; i--)
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(i == start) return "";
|
2016-04-19 02:11:47 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(spacePaddedString[i - 1] == 0x20) continue;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
len = i;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return len == 0 ? "" : encoding.GetString(spacePaddedString, start, len);
|
|
|
|
|
}
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Converts an OSTA compressed unicode byte array to a C# string</summary>
|
|
|
|
|
/// <returns>The C# string.</returns>
|
|
|
|
|
/// <param name="dstring">OSTA compressed unicode byte array.</param>
|
|
|
|
|
public static string DecompressUnicode(byte[] dstring)
|
|
|
|
|
{
|
2023-10-03 23:25:24 +01:00
|
|
|
byte compId = dstring[0];
|
|
|
|
|
var temp = "";
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(compId != 8 && compId != 16) return null;
|
2016-09-15 01:54:13 +01:00
|
|
|
|
2023-10-03 23:25:24 +01:00
|
|
|
for(var byteIndex = 1; byteIndex < dstring.Length;)
|
2016-09-15 01:54:13 +01:00
|
|
|
{
|
2022-11-13 19:16:13 +00:00
|
|
|
ushort unicode;
|
|
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
if(compId == 16)
|
|
|
|
|
unicode = (ushort)(dstring[byteIndex++] << 8);
|
|
|
|
|
else
|
|
|
|
|
unicode = 0;
|
2016-09-15 01:54:13 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(byteIndex < dstring.Length) unicode |= dstring[byteIndex++];
|
2016-09-15 01:54:13 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(unicode == 0) break;
|
2016-09-15 01:54:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
temp += Encoding.Unicode.GetString(BitConverter.GetBytes(unicode));
|
2016-09-15 01:54:13 +01:00
|
|
|
}
|
2022-03-06 13:29:37 +00:00
|
|
|
|
|
|
|
|
return temp;
|
2015-10-05 18:58:13 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|