using System;
using System.Text;

namespace Decal.Filters;

internal static class Get
{
	internal static byte Byte(byte[] Data, ref int Index)
	{
		return Data[Index++];
	}

	internal static int Word(byte[] Data, ref int Index)
	{
		ushort result = BitConverter.ToUInt16(Data, Index);
		Index += 2;
		return result;
	}

	internal static int DWord(byte[] Data, ref int Index)
	{
		int result = BitConverter.ToInt32(Data, Index);
		Index += 4;
		return result;
	}

	internal static long QWord(byte[] Data, ref int Index)
	{
		long result = BitConverter.ToInt64(Data, Index);
		Index += 8;
		return result;
	}

	internal static float Float(byte[] Data, ref int Index)
	{
		float result = BitConverter.ToSingle(Data, Index);
		Index += 4;
		return result;
	}

	internal static double Double(byte[] Data, ref int Index)
	{
		double result = BitConverter.ToDouble(Data, Index);
		Index += 8;
		return result;
	}

	internal static string String(byte[] Data, ref int Index)
	{
		int num = Word(Data, ref Index);
		if (num == 65535)
		{
			num = DWord(Data, ref Index);
		}
		StringBuilder stringBuilder = new StringBuilder(num);
		Encoding encoding = Encoding.GetEncoding(1252);
		byte[] array = (byte[])Array.CreateInstance(typeof(byte), 1);
		while (num-- > 0)
		{
			array[0] = Byte(Data, ref Index);
			stringBuilder.Append(encoding.GetChars(array));
		}
		Index += 3;
		Index &= -4;
		return stringBuilder.ToString();
	}

	internal static string EncString(byte[] Data, ref int Index)
	{
		int num = Word(Data, ref Index);
		if (num == 65535)
		{
			num = DWord(Data, ref Index);
		}
		StringBuilder stringBuilder = new StringBuilder(num);
		Encoding encoding = Encoding.GetEncoding(1252);
		byte[] array = (byte[])Array.CreateInstance(typeof(byte), 1);
		while (num-- > 0)
		{
			byte b = Byte(Data, ref Index);
			array[0] = (byte)((b >> 4) | (b << 4));
			stringBuilder.Append(encoding.GetChars(array));
		}
		Index += 3;
		Index &= -4;
		return stringBuilder.ToString();
	}

	internal static string PString(byte[] Data, ref int Index)
	{
		int capacity = Byte(Data, ref Index);
		StringBuilder stringBuilder = new StringBuilder(capacity);
		Encoding encoding = Encoding.GetEncoding(1252);
		byte[] array = (byte[])Array.CreateInstance(typeof(byte), 1);
		while (capacity-- > 0)
		{
			array[0] = Byte(Data, ref Index);
			stringBuilder.Append(encoding.GetChars(array));
		}
		return stringBuilder.ToString();
	}
}
