2016-07-28 22:25:26 +01:00
|
|
|
// /***************************************************************************
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
// Copyright © 2011-2019 Natalia Portillo
|
2016-07-28 22:25:26 +01:00
|
|
|
// ****************************************************************************/
|
2014-04-17 19:58:14 +00:00
|
|
|
|
2017-06-06 21:38:30 +01:00
|
|
|
using System;
|
2017-12-19 19:33:46 +00:00
|
|
|
using System.Text;
|
2011-03-03 18:34:33 +00:00
|
|
|
|
2014-06-15 23:39:34 +01:00
|
|
|
namespace DiscImageChef
|
2011-03-03 18:34:33 +00:00
|
|
|
{
|
2014-04-14 02:29:13 +00:00
|
|
|
public static class StringHandlers
|
|
|
|
|
{
|
2014-09-03 04:25:33 +01:00
|
|
|
/// <summary>
|
2017-12-24 02:46:53 +00:00
|
|
|
/// Converts a null-terminated (aka C string) ASCII byte array to a C# string
|
2014-09-03 04:25:33 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="CString">A null-terminated (aka C string) ASCII byte array</param>
|
2018-12-31 13:17:27 +00:00
|
|
|
public static string CToString(byte[] CString) => CToString(CString, Encoding.ASCII);
|
2015-12-04 03:34:44 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-24 02:46:53 +00:00
|
|
|
/// Converts a null-terminated (aka C string) byte array with the specified encoding to a C# string
|
2015-12-04 03:34:44 +00:00
|
|
|
/// </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>
|
2017-12-21 16:42:20 +00:00
|
|
|
/// <param name="twoBytes">Set if encoding uses 16-bit characters.</param>
|
|
|
|
|
/// <param name="start">Start decodint at this position</param>
|
2017-08-22 03:40:12 +01:00
|
|
|
public static string CToString(byte[] CString, Encoding encoding, bool twoBytes = false, int start = 0)
|
2014-04-14 02:29:13 +00:00
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(CString == null) return null;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
2017-06-06 21:38:30 +01:00
|
|
|
int len = 0;
|
2016-04-19 02:11:47 +01:00
|
|
|
|
2017-08-22 03:40:12 +01:00
|
|
|
for(int i = start; i < CString.Length; i++)
|
2014-04-14 02:29:13 +00:00
|
|
|
{
|
2016-04-19 02:11:47 +01:00
|
|
|
if(CString[i] == 0)
|
2017-07-11 02:32:40 +01:00
|
|
|
if(twoBytes)
|
|
|
|
|
{
|
2017-12-20 17:26:28 +00:00
|
|
|
if(i + 1 < CString.Length && CString[i + 1] == 0)
|
2017-07-11 02:32:40 +01:00
|
|
|
{
|
|
|
|
|
len++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-22 08:08:38 +01:00
|
|
|
else
|
|
|
|
|
break;
|
2011-03-03 18:34:33 +00:00
|
|
|
|
2017-06-06 21:38:30 +01:00
|
|
|
len++;
|
2014-04-14 02:29:13 +00:00
|
|
|
}
|
2016-04-19 02:11:47 +01:00
|
|
|
|
2019-04-14 16:36:43 +01:00
|
|
|
if(twoBytes && len % 2 > 0) len--;
|
|
|
|
|
|
2017-06-06 21:38:30 +01:00
|
|
|
byte[] dest = new byte[len];
|
2017-08-22 03:40:12 +01:00
|
|
|
Array.Copy(CString, start, dest, 0, len);
|
2017-06-06 21:38:30 +01:00
|
|
|
|
|
|
|
|
return len == 0 ? "" : encoding.GetString(dest);
|
2014-04-14 02:29:13 +00:00
|
|
|
}
|
2012-08-04 18:20:03 +00:00
|
|
|
|
2014-09-03 04:25:33 +01:00
|
|
|
/// <summary>
|
2017-12-24 02:46:53 +00:00
|
|
|
/// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string
|
2014-09-03 04:25:33 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="PascalString">A length-prefixed (aka Pascal string) ASCII byte array</param>
|
2018-12-31 13:17:27 +00:00
|
|
|
public static string PascalToString(byte[] PascalString) => PascalToString(PascalString, Encoding.ASCII);
|
2017-06-06 21:23:20 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-24 02:46:53 +00:00
|
|
|
/// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string
|
2017-06-06 21:23:20 +01:00
|
|
|
/// </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>
|
2017-12-21 16:42:20 +00:00
|
|
|
/// <param name="start">Start decodint at this position</param>
|
2017-08-22 03:40:12 +01:00
|
|
|
public static string PascalToString(byte[] PascalString, Encoding encoding, int start = 0)
|
2014-04-14 02:29:13 +00:00
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(PascalString == null) return null;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
2017-08-22 03:40:12 +01:00
|
|
|
byte length = PascalString[start];
|
2018-06-22 08:08:38 +01:00
|
|
|
int len = 0;
|
2012-08-04 18:20:03 +00:00
|
|
|
|
2017-08-22 03:40:12 +01:00
|
|
|
for(int i = start + 1; i < length + 1 && i < PascalString.Length; i++)
|
2014-04-14 02:29:13 +00:00
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(PascalString[i] == 0) break;
|
|
|
|
|
|
2017-06-06 21:38:30 +01:00
|
|
|
len++;
|
2014-04-14 02:29:13 +00:00
|
|
|
}
|
2012-08-04 18:20:03 +00:00
|
|
|
|
2017-06-06 21:38:30 +01:00
|
|
|
byte[] dest = new byte[len];
|
2017-08-22 03:40:12 +01:00
|
|
|
Array.Copy(PascalString, start + 1, dest, 0, len);
|
2017-06-06 21:38:30 +01:00
|
|
|
|
|
|
|
|
return len == 0 ? "" : encoding.GetString(dest);
|
2014-04-14 02:29:13 +00:00
|
|
|
}
|
2014-09-03 04:22:19 +01:00
|
|
|
|
2014-09-03 04:25:33 +01:00
|
|
|
/// <summary>
|
2017-12-24 02:46:53 +00:00
|
|
|
/// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string
|
2014-09-03 04:25:33 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The corresponding C# string</returns>
|
|
|
|
|
/// <param name="SpacePaddedString">A space (' ', 0x20, ASCII SPACE) padded ASCII byte array</param>
|
2018-12-31 13:17:27 +00:00
|
|
|
public static string SpacePaddedToString(byte[] SpacePaddedString) =>
|
|
|
|
|
SpacePaddedToString(SpacePaddedString, Encoding.ASCII);
|
2017-06-06 21:23:20 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-24 02:46:53 +00:00
|
|
|
/// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string
|
2017-06-06 21:23:20 +01:00
|
|
|
/// </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>
|
2017-12-21 16:42:20 +00:00
|
|
|
/// <param name="start">Start decodint at this position</param>
|
2017-08-22 03:40:12 +01:00
|
|
|
public static string SpacePaddedToString(byte[] SpacePaddedString, Encoding encoding, int start = 0)
|
2014-09-03 04:22:19 +01:00
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(SpacePaddedString == null) return null;
|
2016-04-19 02:11:47 +01:00
|
|
|
|
2017-08-22 03:40:12 +01:00
|
|
|
int len = start;
|
2014-09-03 04:22:19 +01:00
|
|
|
|
2017-08-22 03:40:12 +01:00
|
|
|
for(int i = SpacePaddedString.Length; i >= start; i--)
|
2014-09-03 04:22:19 +01:00
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(i == start) return "";
|
2014-09-03 04:22:19 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(SpacePaddedString[i - 1] == 0x20) continue;
|
|
|
|
|
|
|
|
|
|
len = i;
|
|
|
|
|
break;
|
2014-09-03 04:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
2017-08-22 03:40:12 +01:00
|
|
|
return len == 0 ? "" : encoding.GetString(SpacePaddedString, start, len);
|
2014-09-03 04:22:19 +01:00
|
|
|
}
|
2016-09-15 01:54:13 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-24 02:46:53 +00:00
|
|
|
/// Converts an OSTA compressed unicode byte array to a C# string
|
2016-09-15 01:54:13 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The C# string.</returns>
|
|
|
|
|
/// <param name="dstring">OSTA compressed unicode byte array.</param>
|
|
|
|
|
public static string DecompressUnicode(byte[] dstring)
|
|
|
|
|
{
|
|
|
|
|
ushort unicode;
|
2018-06-22 08:08:38 +01:00
|
|
|
byte compId = dstring[0];
|
|
|
|
|
string temp = "";
|
2016-09-15 01:54:13 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(compId != 8 && compId != 16) return null;
|
2016-09-15 01:54:13 +01:00
|
|
|
|
|
|
|
|
for(int byteIndex = 1; byteIndex < dstring.Length;)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(compId == 16) unicode = (ushort)(dstring[byteIndex++] << 8);
|
2018-06-22 08:08:38 +01:00
|
|
|
else unicode = 0;
|
2016-09-15 01:54:13 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(byteIndex < dstring.Length) unicode |= dstring[byteIndex++];
|
2016-09-15 01:54:13 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(unicode == 0) break;
|
2016-09-15 01:54:13 +01:00
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
temp += Encoding.Unicode.GetString(BitConverter.GetBytes(unicode));
|
2016-09-15 01:54:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2014-04-14 02:29:13 +00:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|