Created
December 28, 2022 22:10
-
-
Save sayotte/16d4a1c807e3913b62690a6c90ad7204 to your computer and use it in GitHub Desktop.
data -> ServUO compression -> ClassicUO decompression round-trip is broken?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
// THIS IS TAKEN FROM SERVUO: https://github.com/ServUO/ServUO/blob/05cdc8780ecd81a6abca0f5f14097c8e6bc4860f/Server/Network/Compression.cs | |
internal static class ServUOCompressor { | |
private static readonly int[] _huffmanTable = new int[514] | |
{ | |
0x2, 0x000, 0x5, 0x01F, 0x6, 0x022, 0x7, 0x034, 0x7, 0x075, 0x6, 0x028, 0x6, 0x03B, 0x7, 0x032, 0x8, 0x0E0, 0x8, | |
0x062, 0x7, 0x056, 0x8, 0x079, 0x9, 0x19D, 0x8, 0x097, 0x6, 0x02A, 0x7, 0x057, 0x8, 0x071, 0x8, 0x05B, 0x9, 0x1CC, | |
0x8, 0x0A7, 0x7, 0x025, 0x7, 0x04F, 0x8, 0x066, 0x8, 0x07D, 0x9, 0x191, 0x9, 0x1CE, 0x7, 0x03F, 0x9, 0x090, 0x8, | |
0x059, 0x8, 0x07B, 0x8, 0x091, 0x8, 0x0C6, 0x6, 0x02D, 0x9, 0x186, 0x8, 0x06F, 0x9, 0x093, 0xA, 0x1CC, 0x8, 0x05A, | |
0xA, 0x1AE, 0xA, 0x1C0, 0x9, 0x148, 0x9, 0x14A, 0x9, 0x082, 0xA, 0x19F, 0x9, 0x171, 0x9, 0x120, 0x9, 0x0E7, 0xA, | |
0x1F3, 0x9, 0x14B, 0x9, 0x100, 0x9, 0x190, 0x6, 0x013, 0x9, 0x161, 0x9, 0x125, 0x9, 0x133, 0x9, 0x195, 0x9, 0x173, | |
0x9, 0x1CA, 0x9, 0x086, 0x9, 0x1E9, 0x9, 0x0DB, 0x9, 0x1EC, 0x9, 0x08B, 0x9, 0x085, 0x5, 0x00A, 0x8, 0x096, 0x8, | |
0x09C, 0x9, 0x1C3, 0x9, 0x19C, 0x9, 0x08F, 0x9, 0x18F, 0x9, 0x091, 0x9, 0x087, 0x9, 0x0C6, 0x9, 0x177, 0x9, 0x089, | |
0x9, 0x0D6, 0x9, 0x08C, 0x9, 0x1EE, 0x9, 0x1EB, 0x9, 0x084, 0x9, 0x164, 0x9, 0x175, 0x9, 0x1CD, 0x8, 0x05E, 0x9, | |
0x088, 0x9, 0x12B, 0x9, 0x172, 0x9, 0x10A, 0x9, 0x08D, 0x9, 0x13A, 0x9, 0x11C, 0xA, 0x1E1, 0xA, 0x1E0, 0x9, 0x187, | |
0xA, 0x1DC, 0xA, 0x1DF, 0x7, 0x074, 0x9, 0x19F, 0x8, 0x08D, 0x8, 0x0E4, 0x7, 0x079, 0x9, 0x0EA, 0x9, 0x0E1, 0x8, | |
0x040, 0x7, 0x041, 0x9, 0x10B, 0x9, 0x0B0, 0x8, 0x06A, 0x8, 0x0C1, 0x7, 0x071, 0x7, 0x078, 0x8, 0x0B1, 0x9, 0x14C, | |
0x7, 0x043, 0x8, 0x076, 0x7, 0x066, 0x7, 0x04D, 0x9, 0x08A, 0x6, 0x02F, 0x8, 0x0C9, 0x9, 0x0CE, 0x9, 0x149, 0x9, | |
0x160, 0xA, 0x1BA, 0xA, 0x19E, 0xA, 0x39F, 0x9, 0x0E5, 0x9, 0x194, 0x9, 0x184, 0x9, 0x126, 0x7, 0x030, 0x8, 0x06C, | |
0x9, 0x121, 0x9, 0x1E8, 0xA, 0x1C1, 0xA, 0x11D, 0xA, 0x163, 0xA, 0x385, 0xA, 0x3DB, 0xA, 0x17D, 0xA, 0x106, 0xA, | |
0x397, 0xA, 0x24E, 0x7, 0x02E, 0x8, 0x098, 0xA, 0x33C, 0xA, 0x32E, 0xA, 0x1E9, 0x9, 0x0BF, 0xA, 0x3DF, 0xA, 0x1DD, | |
0xA, 0x32D, 0xA, 0x2ED, 0xA, 0x30B, 0xA, 0x107, 0xA, 0x2E8, 0xA, 0x3DE, 0xA, 0x125, 0xA, 0x1E8, 0x9, 0x0E9, 0xA, | |
0x1CD, 0xA, 0x1B5, 0x9, 0x165, 0xA, 0x232, 0xA, 0x2E1, 0xB, 0x3AE, 0xB, 0x3C6, 0xB, 0x3E2, 0xA, 0x205, 0xA, 0x29A, | |
0xA, 0x248, 0xA, 0x2CD, 0xA, 0x23B, 0xB, 0x3C5, 0xA, 0x251, 0xA, 0x2E9, 0xA, 0x252, 0x9, 0x1EA, 0xB, 0x3A0, 0xB, | |
0x391, 0xA, 0x23C, 0xB, 0x392, 0xB, 0x3D5, 0xA, 0x233, 0xA, 0x2CC, 0xB, 0x390, 0xA, 0x1BB, 0xB, 0x3A1, 0xB, 0x3C4, | |
0xA, 0x211, 0xA, 0x203, 0x9, 0x12A, 0xA, 0x231, 0xB, 0x3E0, 0xA, 0x29B, 0xB, 0x3D7, 0xA, 0x202, 0xB, 0x3AD, 0xA, | |
0x213, 0xA, 0x253, 0xA, 0x32C, 0xA, 0x23D, 0xA, 0x23F, 0xA, 0x32F, 0xA, 0x11C, 0xA, 0x384, 0xA, 0x31C, 0xA, 0x17C, | |
0xA, 0x30A, 0xA, 0x2E0, 0xA, 0x276, 0xA, 0x250, 0xB, 0x3E3, 0xA, 0x396, 0xA, 0x18F, 0xA, 0x204, 0xA, 0x206, 0xA, | |
0x230, 0xA, 0x265, 0xA, 0x212, 0xA, 0x23E, 0xB, 0x3AC, 0xB, 0x393, 0xB, 0x3E1, 0xA, 0x1DE, 0xB, 0x3D6, 0xA, 0x31D, | |
0xB, 0x3E5, 0xB, 0x3E4, 0xA, 0x207, 0xB, 0x3C7, 0xA, 0x277, 0xB, 0x3D4, 0x8, 0x0C0, 0xA, 0x162, 0xA, 0x3DA, 0xA, | |
0x124, 0xA, 0x1B4, 0xA, 0x264, 0xA, 0x33D, 0xA, 0x1D1, 0xA, 0x1AF, 0xA, 0x39E, 0xA, 0x24F, 0xB, 0x373, 0xA, 0x249, | |
0xB, 0x372, 0x9, 0x167, 0xA, 0x210, 0xA, 0x23A, 0xA, 0x1B8, 0xB, 0x3AF, 0xA, 0x18E, 0xA, 0x2EC, 0x7, 0x062, 0x4, | |
0x00D | |
}; | |
private const int CountIndex = 0; | |
private const int ValueIndex = 1; | |
// UO packets may not exceed 64kb in length | |
private const int BufferSize = 0x10000; | |
// Optimal compression ratio is 2 / 8; worst compression ratio is 11 / 8 | |
private const int MinimalCodeLength = 2; | |
private const int MaximalCodeLength = 11; | |
// Fixed overhead, in bits, per compression call | |
private const int TerminalCodeLength = 4; | |
// If our input exceeds this length, we cannot possibly compress it within the buffer | |
private const int DefiniteOverflow = ((BufferSize * 8) - TerminalCodeLength) / MinimalCodeLength; | |
// If our input exceeds this length, we may potentially overflow the buffer | |
private const int PossibleOverflow = ((BufferSize * 8) - TerminalCodeLength) / MaximalCodeLength; | |
public static unsafe void Compress(byte[] input, int offset, int count, byte[] output, ref int length) | |
{ | |
if (input == null){ | |
throw new ArgumentNullException("input"); | |
} else if (offset < 0 || offset >= input.Length) { | |
throw new ArgumentOutOfRangeException("offset"); | |
} else if (count < 0 || count > input.Length) { | |
throw new ArgumentOutOfRangeException("count"); | |
} else if ((input.Length - offset) < count) { | |
throw new ArgumentException(); | |
} | |
length = 0; | |
if (count > DefiniteOverflow) { | |
return; | |
} | |
var bitCount = 0; | |
var bitValue = 0; | |
fixed (int* pTable = _huffmanTable) | |
{ | |
int* pEntry; | |
fixed (byte* pInputBuffer = input) | |
{ | |
byte* pInput = pInputBuffer + offset, pInputEnd = pInput + count; | |
fixed (byte* pOutputBuffer = output) | |
{ | |
byte* pOutput = pOutputBuffer, pOutputEnd = pOutput + BufferSize; | |
while (pInput < pInputEnd) | |
{ | |
pEntry = &pTable[*pInput++ << 1]; | |
bitCount += pEntry[CountIndex]; | |
bitValue <<= pEntry[CountIndex]; | |
bitValue |= pEntry[ValueIndex]; | |
while (bitCount >= 8) | |
{ | |
bitCount -= 8; | |
if (pOutput < pOutputEnd) | |
{ | |
*pOutput++ = (byte)(bitValue >> bitCount); | |
} | |
else | |
{ | |
length = 0; | |
return; | |
} | |
} | |
} | |
// terminal code | |
pEntry = &pTable[0x200]; | |
bitCount += pEntry[CountIndex]; | |
bitValue <<= pEntry[CountIndex]; | |
bitValue |= pEntry[ValueIndex]; | |
// align on byte boundary | |
if ((bitCount & 7) != 0) | |
{ | |
bitValue <<= 8 - (bitCount & 7); | |
bitCount += 8 - (bitCount & 7); | |
} | |
while (bitCount >= 8) | |
{ | |
bitCount -= 8; | |
if (pOutput < pOutputEnd) | |
{ | |
*pOutput++ = (byte)(bitValue >> bitCount); | |
} | |
else | |
{ | |
length = 0; | |
return; | |
} | |
} | |
length = (int)(pOutput - pOutputBuffer); | |
return; | |
} | |
} | |
} | |
} | |
} | |
// THIS IS TAKEN FROM CLASSICUO: https://github.com/ClassicUO/ClassicUO/blob/c13b6e96fcb029cc9a0fb794936844ce919ea0b1/src/Network/Huffman.cs | |
internal static class ClassicUODecompressor | |
{ | |
#region Decompression Tree | |
private static readonly int[,] dec_tree = | |
{ | |
/*node*/ /*leaf0 leaf1*/ | |
/* 0*/ | |
{ | |
2, 1 | |
}, | |
/* 1*/ | |
{ | |
4, 3 | |
}, | |
/* 2*/ | |
{ | |
0, 5 | |
}, | |
/* 3*/ | |
{ | |
7, 6 | |
}, | |
/* 4*/ | |
{ | |
9, 8 | |
}, | |
/* 5*/ | |
{ | |
11, 10 | |
}, | |
/* 6*/ | |
{ | |
13, 12 | |
}, | |
/* 7*/ | |
{ | |
14, -256 | |
}, | |
/* 8*/ | |
{ | |
16, 15 | |
}, | |
/* 9*/ | |
{ | |
18, 17 | |
}, | |
/* 10*/ | |
{ | |
20, 19 | |
}, | |
/* 11*/ | |
{ | |
22, 21 | |
}, | |
/* 12*/ | |
{ | |
23, -1 | |
}, | |
/* 13*/ | |
{ | |
25, 24 | |
}, | |
/* 14*/ | |
{ | |
27, 26 | |
}, | |
/* 15*/ | |
{ | |
29, 28 | |
}, | |
/* 16*/ | |
{ | |
31, 30 | |
}, | |
/* 17*/ | |
{ | |
33, 32 | |
}, | |
/* 18*/ | |
{ | |
35, 34 | |
}, | |
/* 19*/ | |
{ | |
37, 36 | |
}, | |
/* 20*/ | |
{ | |
39, 38 | |
}, | |
/* 21*/ | |
{ | |
-64, 40 | |
}, | |
/* 22*/ | |
{ | |
42, 41 | |
}, | |
/* 23*/ | |
{ | |
44, 43 | |
}, | |
/* 24*/ | |
{ | |
45, -6 | |
}, | |
/* 25*/ | |
{ | |
47, 46 | |
}, | |
/* 26*/ | |
{ | |
49, 48 | |
}, | |
/* 27*/ | |
{ | |
51, 50 | |
}, | |
/* 28*/ | |
{ | |
52, -119 | |
}, | |
/* 29*/ | |
{ | |
53, -32 | |
}, | |
/* 30*/ | |
{ | |
-14, 54 | |
}, | |
/* 31*/ | |
{ | |
-5, 55 | |
}, | |
/* 32*/ | |
{ | |
57, 56 | |
}, | |
/* 33*/ | |
{ | |
59, 58 | |
}, | |
/* 34*/ | |
{ | |
-2, 60 | |
}, | |
/* 35*/ | |
{ | |
62, 61 | |
}, | |
/* 36*/ | |
{ | |
64, 63 | |
}, | |
/* 37*/ | |
{ | |
66, 65 | |
}, | |
/* 38*/ | |
{ | |
68, 67 | |
}, | |
/* 39*/ | |
{ | |
70, 69 | |
}, | |
/* 40*/ | |
{ | |
72, 71 | |
}, | |
/* 41*/ | |
{ | |
73, -51 | |
}, | |
/* 42*/ | |
{ | |
75, 74 | |
}, | |
/* 43*/ | |
{ | |
77, 76 | |
}, | |
/* 44*/ | |
{ | |
-111, -101 | |
}, | |
/* 45*/ | |
{ | |
-97, -4 | |
}, | |
/* 46*/ | |
{ | |
79, 78 | |
}, | |
/* 47*/ | |
{ | |
80, -110 | |
}, | |
/* 48*/ | |
{ | |
-116, 81 | |
}, | |
/* 49*/ | |
{ | |
83, 82 | |
}, | |
/* 50*/ | |
{ | |
-255, 84 | |
}, | |
/* 51*/ | |
{ | |
86, 85 | |
}, | |
/* 52*/ | |
{ | |
88, 87 | |
}, | |
/* 53*/ | |
{ | |
90, 89 | |
}, | |
/* 54*/ | |
{ | |
-10, -15 | |
}, | |
/* 55*/ | |
{ | |
92, 91 | |
}, | |
/* 56*/ | |
{ | |
93, -21 | |
}, | |
/* 57*/ | |
{ | |
94, -117 | |
}, | |
/* 58*/ | |
{ | |
96, 95 | |
}, | |
/* 59*/ | |
{ | |
98, 97 | |
}, | |
/* 60*/ | |
{ | |
100, 99 | |
}, | |
/* 61*/ | |
{ | |
101, -114 | |
}, | |
/* 62*/ | |
{ | |
102, -105 | |
}, | |
/* 63*/ | |
{ | |
103, -26 | |
}, | |
/* 64*/ | |
{ | |
105, 104 | |
}, | |
/* 65*/ | |
{ | |
107, 106 | |
}, | |
/* 66*/ | |
{ | |
109, 108 | |
}, | |
/* 67*/ | |
{ | |
111, 110 | |
}, | |
/* 68*/ | |
{ | |
-3, 112 | |
}, | |
/* 69*/ | |
{ | |
-7, 113 | |
}, | |
/* 70*/ | |
{ | |
-131, 114 | |
}, | |
/* 71*/ | |
{ | |
-144, 115 | |
}, | |
/* 72*/ | |
{ | |
117, 116 | |
}, | |
/* 73*/ | |
{ | |
118, -20 | |
}, | |
/* 74*/ | |
{ | |
120, 119 | |
}, | |
/* 75*/ | |
{ | |
122, 121 | |
}, | |
/* 76*/ | |
{ | |
124, 123 | |
}, | |
/* 77*/ | |
{ | |
126, 125 | |
}, | |
/* 78*/ | |
{ | |
128, 127 | |
}, | |
/* 79*/ | |
{ | |
-100, 129 | |
}, | |
/* 80*/ | |
{ | |
-8, 130 | |
}, | |
/* 81*/ | |
{ | |
132, 131 | |
}, | |
/* 82*/ | |
{ | |
134, 133 | |
}, | |
/* 83*/ | |
{ | |
135, -120 | |
}, | |
/* 84*/ | |
{ | |
-31, 136 | |
}, | |
/* 85*/ | |
{ | |
138, 137 | |
}, | |
/* 86*/ | |
{ | |
-234, -109 | |
}, | |
/* 87*/ | |
{ | |
140, 139 | |
}, | |
/* 88*/ | |
{ | |
142, 141 | |
}, | |
/* 89*/ | |
{ | |
144, 143 | |
}, | |
/* 90*/ | |
{ | |
145, -112 | |
}, | |
/* 91*/ | |
{ | |
146, -19 | |
}, | |
/* 92*/ | |
{ | |
148, 147 | |
}, | |
/* 93*/ | |
{ | |
-66, 149 | |
}, | |
/* 94*/ | |
{ | |
-145, 150 | |
}, | |
/* 95*/ | |
{ | |
-65, -13 | |
}, | |
/* 96*/ | |
{ | |
152, 151 | |
}, | |
/* 97*/ | |
{ | |
154, 153 | |
}, | |
/* 98*/ | |
{ | |
155, -30 | |
}, | |
/* 99*/ | |
{ | |
157, 156 | |
}, | |
/* 100*/ | |
{ | |
158, -99 | |
}, | |
/* 101*/ | |
{ | |
160, 159 | |
}, | |
/* 102*/ | |
{ | |
162, 161 | |
}, | |
/* 103*/ | |
{ | |
163, -23 | |
}, | |
/* 104*/ | |
{ | |
164, -29 | |
}, | |
/* 105*/ | |
{ | |
165, -11 | |
}, | |
/* 106*/ | |
{ | |
-115, 166 | |
}, | |
/* 107*/ | |
{ | |
168, 167 | |
}, | |
/* 108*/ | |
{ | |
170, 169 | |
}, | |
/* 109*/ | |
{ | |
171, -16 | |
}, | |
/* 110*/ | |
{ | |
172, -34 | |
}, | |
/* 111*/ | |
{ | |
-132, 173 | |
}, | |
/* 112*/ | |
{ | |
-108, 174 | |
}, | |
/* 113*/ | |
{ | |
-22, 175 | |
}, | |
/* 114*/ | |
{ | |
-9, 176 | |
}, | |
/* 115*/ | |
{ | |
-84, 177 | |
}, | |
/* 116*/ | |
{ | |
-37, -17 | |
}, | |
/* 117*/ | |
{ | |
178, -28 | |
}, | |
/* 118*/ | |
{ | |
180, 179 | |
}, | |
/* 119*/ | |
{ | |
182, 181 | |
}, | |
/* 120*/ | |
{ | |
184, 183 | |
}, | |
/* 121*/ | |
{ | |
186, 185 | |
}, | |
/* 122*/ | |
{ | |
-104, 187 | |
}, | |
/* 123*/ | |
{ | |
-78, 188 | |
}, | |
/* 124*/ | |
{ | |
-61, 189 | |
}, | |
/* 125*/ | |
{ | |
-178, -79 | |
}, | |
/* 126*/ | |
{ | |
-134, -59 | |
}, | |
/* 127*/ | |
{ | |
-25, 190 | |
}, | |
/* 128*/ | |
{ | |
-18, -83 | |
}, | |
/* 129*/ | |
{ | |
-57, 191 | |
}, | |
/* 130*/ | |
{ | |
192, -67 | |
}, | |
/* 131*/ | |
{ | |
193, -98 | |
}, | |
/* 132*/ | |
{ | |
-68, -12 | |
}, | |
/* 133*/ | |
{ | |
195, 194 | |
}, | |
/* 134*/ | |
{ | |
-128, -55 | |
}, | |
/* 135*/ | |
{ | |
-50, -24 | |
}, | |
/* 136*/ | |
{ | |
196, -70 | |
}, | |
/* 137*/ | |
{ | |
-33, -94 | |
}, | |
/* 138*/ | |
{ | |
-129, 197 | |
}, | |
/* 139*/ | |
{ | |
198, -74 | |
}, | |
/* 140*/ | |
{ | |
199, -82 | |
}, | |
/* 141*/ | |
{ | |
-87, -56 | |
}, | |
/* 142*/ | |
{ | |
200, -44 | |
}, | |
/* 143*/ | |
{ | |
201, -248 | |
}, | |
/* 144*/ | |
{ | |
-81, -163 | |
}, | |
/* 145*/ | |
{ | |
-123, -52 | |
}, | |
/* 146*/ | |
{ | |
-113, 202 | |
}, | |
/* 147*/ | |
{ | |
-41, -48 | |
}, | |
/* 148*/ | |
{ | |
-40, -122 | |
}, | |
/* 149*/ | |
{ | |
-90, 203 | |
}, | |
/* 150*/ | |
{ | |
204, -54 | |
}, | |
/* 151*/ | |
{ | |
-192, -86 | |
}, | |
/* 152*/ | |
{ | |
206, 205 | |
}, | |
/* 153*/ | |
{ | |
-130, 207 | |
}, | |
/* 154*/ | |
{ | |
208, -53 | |
}, | |
/* 155*/ | |
{ | |
-45, -133 | |
}, | |
/* 156*/ | |
{ | |
210, 209 | |
}, | |
/* 157*/ | |
{ | |
-91, 211 | |
}, | |
/* 158*/ | |
{ | |
213, 212 | |
}, | |
/* 159*/ | |
{ | |
-88, -106 | |
}, | |
/* 160*/ | |
{ | |
215, 214 | |
}, | |
/* 161*/ | |
{ | |
217, 216 | |
}, | |
/* 162*/ | |
{ | |
-49, 218 | |
}, | |
/* 163*/ | |
{ | |
220, 219 | |
}, | |
/* 164*/ | |
{ | |
222, 221 | |
}, | |
/* 165*/ | |
{ | |
224, 223 | |
}, | |
/* 166*/ | |
{ | |
226, 225 | |
}, | |
/* 167*/ | |
{ | |
-102, 227 | |
}, | |
/* 168*/ | |
{ | |
228, -160 | |
}, | |
/* 169*/ | |
{ | |
229, -46 | |
}, | |
/* 170*/ | |
{ | |
230, -127 | |
}, | |
/* 171*/ | |
{ | |
231, -103 | |
}, | |
/* 172*/ | |
{ | |
233, 232 | |
}, | |
/* 173*/ | |
{ | |
234, -60 | |
}, | |
/* 174*/ | |
{ | |
-76, 235 | |
}, | |
/* 175*/ | |
{ | |
-121, 236 | |
}, | |
/* 176*/ | |
{ | |
-73, 237 | |
}, | |
/* 177*/ | |
{ | |
238, -149 | |
}, | |
/* 178*/ | |
{ | |
-107, 239 | |
}, | |
/* 179*/ | |
{ | |
240, -35 | |
}, | |
/* 180*/ | |
{ | |
-27, -71 | |
}, | |
/* 181*/ | |
{ | |
241, -69 | |
}, | |
/* 182*/ | |
{ | |
-77, -89 | |
}, | |
/* 183*/ | |
{ | |
-118, -62 | |
}, | |
/* 184*/ | |
{ | |
-85, -75 | |
}, | |
/* 185*/ | |
{ | |
-58, -72 | |
}, | |
/* 186*/ | |
{ | |
-80, -63 | |
}, | |
/* 187*/ | |
{ | |
-42, 242 | |
}, | |
/* 188*/ | |
{ | |
-157, -150 | |
}, | |
/* 189*/ | |
{ | |
-236, -139 | |
}, | |
/* 190*/ | |
{ | |
-243, -126 | |
}, | |
/* 191*/ | |
{ | |
-214, -142 | |
}, | |
/* 192*/ | |
{ | |
-206, -138 | |
}, | |
/* 193*/ | |
{ | |
-146, -240 | |
}, | |
/* 194*/ | |
{ | |
-147, -204 | |
}, | |
/* 195*/ | |
{ | |
-201, -152 | |
}, | |
/* 196*/ | |
{ | |
-207, -227 | |
}, | |
/* 197*/ | |
{ | |
-209, -154 | |
}, | |
/* 198*/ | |
{ | |
-254, -153 | |
}, | |
/* 199*/ | |
{ | |
-156, -176 | |
}, | |
/* 200*/ | |
{ | |
-210, -165 | |
}, | |
/* 201*/ | |
{ | |
-185, -172 | |
}, | |
/* 202*/ | |
{ | |
-170, -195 | |
}, | |
/* 203*/ | |
{ | |
-211, -232 | |
}, | |
/* 204*/ | |
{ | |
-239, -219 | |
}, | |
/* 205*/ | |
{ | |
-177, -200 | |
}, | |
/* 206*/ | |
{ | |
-212, -175 | |
}, | |
/* 207*/ | |
{ | |
-143, -244 | |
}, | |
/* 208*/ | |
{ | |
-171, -246 | |
}, | |
/* 209*/ | |
{ | |
-221, -203 | |
}, | |
/* 210*/ | |
{ | |
-181, -202 | |
}, | |
/* 211*/ | |
{ | |
-250, -173 | |
}, | |
/* 212*/ | |
{ | |
-164, -184 | |
}, | |
/* 213*/ | |
{ | |
-218, -193 | |
}, | |
/* 214*/ | |
{ | |
-220, -199 | |
}, | |
/* 215*/ | |
{ | |
-249, -190 | |
}, | |
/* 216*/ | |
{ | |
-217, -230 | |
}, | |
/* 217*/ | |
{ | |
-216, -169 | |
}, | |
/* 218*/ | |
{ | |
-197, -191 | |
}, | |
/* 219*/ | |
{ | |
243, -47 | |
}, | |
/* 220*/ | |
{ | |
245, 244 | |
}, | |
/* 221*/ | |
{ | |
247, 246 | |
}, | |
/* 222*/ | |
{ | |
-159, -148 | |
}, | |
/* 223*/ | |
{ | |
249, 248 | |
}, | |
/* 224*/ | |
{ | |
-93, -92 | |
}, | |
/* 225*/ | |
{ | |
-225, -96 | |
}, | |
/* 226*/ | |
{ | |
-95, -151 | |
}, | |
/* 227*/ | |
{ | |
251, 250 | |
}, | |
/* 228*/ | |
{ | |
252, -241 | |
}, | |
/* 229*/ | |
{ | |
-36, -161 | |
}, | |
/* 230*/ | |
{ | |
254, 253 | |
}, | |
/* 231*/ | |
{ | |
-39, -135 | |
}, | |
/* 232*/ | |
{ | |
-124, -187 | |
}, | |
/* 233*/ | |
{ | |
-251, 255 | |
}, | |
/* 234*/ | |
{ | |
-238, -162 | |
}, | |
/* 235*/ | |
{ | |
-38, -242 | |
}, | |
/* 236*/ | |
{ | |
-125, -43 | |
}, | |
/* 237*/ | |
{ | |
-253, -215 | |
}, | |
/* 238*/ | |
{ | |
-208, -140 | |
}, | |
/* 239*/ | |
{ | |
-235, -137 | |
}, | |
/* 240*/ | |
{ | |
-237, -158 | |
}, | |
/* 241*/ | |
{ | |
-205, -136 | |
}, | |
/* 242*/ | |
{ | |
-141, -155 | |
}, | |
/* 243*/ | |
{ | |
-229, -228 | |
}, | |
/* 244*/ | |
{ | |
-168, -213 | |
}, | |
/* 245*/ | |
{ | |
-194, -224 | |
}, | |
/* 246*/ | |
{ | |
-226, -196 | |
}, | |
/* 247*/ | |
{ | |
-233, -183 | |
}, | |
/* 248*/ | |
{ | |
-167, -231 | |
}, | |
/* 249*/ | |
{ | |
-189, -174 | |
}, | |
/* 250*/ | |
{ | |
-166, -252 | |
}, | |
/* 251*/ | |
{ | |
-222, -198 | |
}, | |
/* 252*/ | |
{ | |
-179, -188 | |
}, | |
/* 253*/ | |
{ | |
-182, -223 | |
}, | |
/* 254*/ | |
{ | |
-186, -180 | |
}, | |
/* 255*/ | |
{ | |
-247, -245 | |
} | |
}; | |
#endregion | |
public static bool DecompressChunk | |
( | |
byte[] src, | |
ref int srcOffset, | |
int srcLength, | |
byte[] dest, | |
int destOffset, | |
out int destLength | |
) | |
{ | |
//Array.Clear(dest, destOffset, dest.Length - destOffset); | |
destLength = 0; | |
int end = srcOffset + srcLength; | |
int node = 0; | |
int destPos = destOffset; | |
int bitNum = 8; | |
while (srcOffset < end) | |
{ | |
int leaf = (src[srcOffset] >> (bitNum - 1)) & 1; | |
int leafValue = dec_tree[node, leaf]; | |
// all numbers below 1 (0..-256) are codewords | |
// if the halt codeword has been found, skip this byte | |
if (leafValue == -256) | |
{ | |
++srcOffset; | |
destLength = destPos - destOffset; | |
return true; | |
} | |
if (leafValue < 1) | |
{ | |
dest[destPos] = (byte) -leafValue; | |
leafValue = 0; | |
++destPos; | |
} | |
--bitNum; | |
node = leafValue; | |
/* if its the end of the byte, go to the next byte */ | |
if (bitNum < 1) | |
{ | |
bitNum = 8; | |
++srcOffset; | |
} | |
// check to see if the current codeword has no end | |
// if not, make it an incomplete byte | |
if (srcOffset == srcLength) | |
{ | |
return false; | |
} | |
} | |
destLength = destPos - destOffset; | |
return false; | |
} | |
} | |
public static void Main() | |
{ | |
// DATA TAKEN FROM A RAZOR PACKET LOG, THIS IS THE 0xA9 "CHARACTERS | |
// AND STARTING LOCATIONS" PACKET | |
byte[] preCompressed = new byte[939]{0xA9, 0x03, 0xAB, 0x05, 0x61, 0x20, 0x6D, 0x75, 0x6C, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x61, 0x20, 0x67, 0x72, 0x61, 0x76, 0x79, 0x20, 0x74, 0x72, 0x61, 0x69, 0x6E, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x20, 0x73, 0x69, | |
0x67, 0x69, 0x6C, 0x20, 0x74, 0x68, 0x69, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x20, 0x73, 0x63, 0x6F, 0x75, 0x74, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x4C, 0x69, 0x7A, 0x7A, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x0A, 0x00, 0x59, 0x65, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x54, 0x68, 0x65, 0x20, 0x45, 0x6D, 0x70, 0x61, 0x74, 0x68, 0x20, 0x41, 0x62, 0x62, 0x65, | |
0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x01, 0x4D, 0x69, 0x6E, 0x6F, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x54, 0x68, 0x65, 0x20, 0x42, 0x61, 0x72, 0x6E, 0x61, 0x63, 0x6C, 0x65, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, | |
0x42, 0x72, 0x69, 0x74, 0x61, 0x69, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, | |
0x77, 0x65, 0x65, 0x74, 0x20, 0x44, 0x72, 0x65, 0x61, 0x6D, 0x73, 0x20, 0x49, 0x6E, 0x6E, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4D, | |
0x6F, 0x6F, 0x6E, 0x67, 0x6C, 0x6F, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, | |
0x65, 0x20, 0x53, 0x63, 0x68, 0x6F, 0x6C, 0x61, 0x72, 0x73, 0x20, 0x49, 0x6E, 0x6E, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x54, 0x72, | |
0x69, 0x6E, 0x73, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, | |
0x20, 0x54, 0x72, 0x61, 0x76, 0x65, 0x6C, 0x65, 0x72, 0x27, 0x73, 0x20, 0x49, 0x6E, 0x6E, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x4D, 0x61, 0x67, | |
0x69, 0x6E, 0x63, 0x69, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, | |
0x47, 0x72, 0x65, 0x61, 0x74, 0x20, 0x48, 0x6F, 0x72, 0x6E, 0x73, 0x20, 0x54, 0x61, 0x76, 0x65, | |
0x72, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x4A, 0x68, 0x65, 0x6C, | |
0x6F, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, 0x4D, | |
0x65, 0x72, 0x63, 0x65, 0x6E, 0x61, 0x72, 0x79, 0x20, 0x49, 0x6E, 0x6E, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x53, 0x6B, 0x61, 0x72, 0x61, | |
0x20, 0x42, 0x72, 0x61, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, 0x46, 0x61, | |
0x6C, 0x63, 0x6F, 0x6E, 0x65, 0x72, 0x27, 0x73, 0x20, 0x49, 0x6E, 0x6E, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x56, 0x65, 0x73, 0x70, 0x65, 0x72, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, 0x49, 0x72, 0x6F, | |
0x6E, 0x77, 0x6F, 0x6F, 0x64, 0x20, 0x49, 0x6E, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x4F, 0x63, 0x63, 0x6C, 0x6F, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, 0x42, 0x6F, 0x75, 0x6E, | |
0x74, 0x69, 0x66, 0x75, 0x6C, 0x20, 0x48, 0x61, 0x72, 0x76, 0x65, 0x73, 0x74, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08}; | |
byte[] compressed = new byte[16384]; | |
int compressedLength = 0; | |
ServUOCompressor.Compress(preCompressed, 0, 1, compressed, ref compressedLength); | |
for (int i = 0; i < compressedLength; i++) { | |
Console.Write(compressed[i].ToString("X2") + " "); | |
} | |
Console.Write("\ncompressedLength: "+ compressedLength + "\n"); | |
byte[] decompressed = new byte[16384]; | |
int compressedOffset = 0; | |
bool something; | |
int decompressedLength = 0; | |
something = ClassicUODecompressor.DecompressChunk(compressed, ref compressedOffset, compressedLength, decompressed, 0, out decompressedLength); | |
Console.Write("something?: "+something+"\n"); | |
for (int i=0; i < decompressedLength; i++) { | |
Console.Write(compressed[i].ToString("X2")+" "); | |
} | |
Console.Write("\ndecompressedLength: "+decompressedLength+"\n"); | |
} | |
} |
Line 1331: Console.Write(compressed[i].ToString("X2")+" ");
Also, why would compressed data match with decompressed data?
If you trying to match input -> output; loop at 1319 should print preCompressed?
Line 1318: ServUOCompressor.Compress(preCompressed, 0, 1, compressed, ref compressedLength);
Did you do this on purpose to just test one byte?
Else you probably want preCompressed.Length
here.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: