/// <summary> /// Expands compressed patch data into the given array of int values /// /// If the value starts with bits 0: The value is zero /// If the value starts with bits 00: The rest of the patch is zeroes /// If the value starts with bits 000: The value is a positive integer of wordBits bits /// If the value starts with bits 001: The value is a negative integer of wordBits bits /// /// </summary> /// <param name="bitPack">The bitPack to read data from</param> /// <param name="patchSize">The number of values in the patch is patchSize * patchSize</param> /// <param name="wordBits">The number of bits in each value</param> /// <param name="patchData">Array to place the decoded values in</param> public static void Decode(BitPack bitPack, int patchSize, int wordBits, int[] patchData) { //Logger.LogDebug($"Patch.Decode: patchSize={patchSize}, wordBits={wordBits}"); // TODO: Different for Big Endian? int i; int j; for (i = 0; i < patchSize * patchSize; i++) { if (bitPack.GetBool() == false) { patchData[i] = 0; continue; } if (bitPack.GetBool() == false) { for (j = i; j < patchSize * patchSize; j++) { patchData[j] = 0; } return; } bool isNegative = bitPack.GetBool(); patchData[i] = (int)bitPack.GetUInt32_Le(wordBits) * (isNegative ? -1 : 1); } }
public void GetUInt32_Le() { UInt32[] values = { 0x00000055, 0x000000aa, 0x000055aa, 0x0000aa55, 0x0055aa55, 0x00aa55aa, 0x55aa55aa, 0xaa55aa55, 0x01234567, 0x89abcdef, 0xfedcba98, 0x76543210 }; byte[] buffer = new byte[values.Length * 4]; int destIndex = 0; for (int i = 0; i < values.Length; i++) { buffer[destIndex++] = (byte)((values[i] >> 0) & 0xff); buffer[destIndex++] = (byte)((values[i] >> 8) & 0xff); buffer[destIndex++] = (byte)((values[i] >> 16) & 0xff); buffer[destIndex++] = (byte)((values[i] >> 24) & 0xff); } BitPack bitPack = new BitPack(buffer); for (int i = 0; i < values.Length; i++) { UInt32 v = bitPack.GetUInt32_Le(); Assert.AreEqual(values[i], v); } }