private void ReadStream(Stream stream) { using (var reader = new ImprovedBinaryReader(stream)) { // header header = reader.ReadStruct <Header>(); // table 0 reader.BaseStream.Position = header.T0 << 2; using (var table0 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table0.BaseStream.Position < table0.BaseStream.Length) { t0_list.Add(table0.ReadStruct <T0Entry>()); } } // table 1 reader.BaseStream.Position = header.T1 << 2; using (var table1 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table1.BaseStream.Position < table1.BaseStream.Length) { t1_list.Add(table1.ReadStruct <T1Entry>()); } } // table 2 reader.BaseStream.Position = header.T2 << 2; using (var table2 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table2.BaseStream.Position < table2.BaseStream.Length) { t2_list.Add(table2.ReadStruct <T2Entry>()); } } // table 3 reader.BaseStream.Position = header.T3 << 2; using (var table3 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table3.BaseStream.Position < table3.BaseStream.Length) { t3_list.Add(table3.ReadStruct <T3Entry>()); } } // table 4 reader.BaseStream.Position = header.T4 << 2; using (var table4 = new ImprovedBinaryReader(new MemoryStream(Level5.Decompress(reader.BaseStream)))) { while (table4.BaseStream.Position < table4.BaseStream.Length) { t4_data.Add(table4.ReadByte()); } } } }
//Huffman 4bit/8bit public static byte[] Decompress(Stream input, int num_bits, long decompressedLength) { using (var br = new ImprovedBinaryReader(input, true)) { var result = new List <byte>(); var tree_size = br.ReadByte(); var tree_root = br.ReadByte(); var tree_buffer = br.ReadBytes(tree_size * 2); for (int i = 0, code = 0, next = 0, pos = tree_root;; i++) { if (i % 32 == 0) { code = br.ReadInt32(); } next += (pos & 0x3F) * 2 + 2; int direction = (code >> (31 - i)) % 2 == 0 ? 2 : 1; var leaf = (pos >> 5 >> direction) % 2 != 0; pos = tree_buffer[next - direction]; if (leaf) { result.Add((byte)pos); pos = tree_root; next = 0; } if (result.Count == decompressedLength * 8 / num_bits) { if (num_bits == 8) { return(result.ToArray()); } else { return(Enumerable.Range(0, (int)decompressedLength) .Select(j => (byte)(result[2 * j + 1] * 16 + result[2 * j])).ToArray()); } } } } }
public static byte[] Decompress(Stream instream, long decompressedLength) { using (var br = new ImprovedBinaryReader(instream, true)) { var result = new List <byte>(); while (true) { var flag = br.ReadByte(); result.AddRange(flag >= 128 ? Enumerable.Repeat(br.ReadByte(), flag - 128 + 3) : br.ReadBytes(flag + 1)); if (result.Count == decompressedLength) { return(result.ToArray()); } else if (result.Count > decompressedLength) { throw new InvalidDataException("Went past the end of the stream"); } } } }