public byte[] UnpackStream(long data_offset, int data_packed, int data_unpacked, int ctl_packed, int ctl_unpacked) { var ctl_offset = data_offset + data_packed; var data = new byte[data_unpacked]; using (var z = new StreamRegion(InputStream, data_offset, data_packed, true)) using (var data_in = new ZLibStream(z, CompressionMode.Decompress)) if (data.Length != data_in.Read(data, 0, data.Length)) { throw new EndOfStreamException(); } using (var z = new StreamRegion(InputStream, ctl_offset, ctl_packed, true)) using (var ctl_in = new ZLibStream(z, CompressionMode.Decompress)) using (var bits = new LsbBitStream(ctl_in)) { bool copy = bits.GetNextBit() != 0; int output_size = GetBitCount(bits); var output = new byte[output_size]; int src = 0; int dst = 0; while (dst < output_size) { int count = GetBitCount(bits); if (copy) { Buffer.BlockCopy(data, src, output, dst, count); src += count; } dst += count; copy = !copy; } return(ApplyDelta(output)); } }
public void Unpack() { m_input.Position = 0x11; int ctl_length = (m_input.ReadInt32() + 7) / 8; var ctl_bytes = m_input.ReadBytes(ctl_length); m_input.ReadInt32(); using (var ctl_mem = new MemoryStream(ctl_bytes)) using (var bits = new LsbBitStream(ctl_mem)) { int dst = 0; while (dst < m_output.Length) { int bit = bits.GetNextBit(); if (-1 == bit) { break; } if (0 == bit) { m_output[dst++] = m_input.ReadUInt8(); } else { int offset = m_input.ReadUInt16(); int count = (offset & 7) + 1; offset = (offset >> 3) + 1; Binary.CopyOverlapped(m_output, dst - offset, dst, count); dst += count; } } } }
byte[] UnpackHuffman(Stream input) { var tree = CreateHuffmanTree(input); var unpacked = new byte[m_huffman_unpacked]; using (var bits = new LsbBitStream(input, true)) { int dst = 0; while (dst < m_huffman_unpacked) { int node = RootNodeIndex; while (node > 0xFF) { if (0 != bits.GetNextBit()) { node = tree[node].Right; } else { node = tree[node].Left; } } unpacked[dst++] = (byte)node; } } return(unpacked); }
static int GetBitCount(LsbBitStream bits) { int n = 0; while (0 == bits.GetNextBit()) { ++n; if (n >= 0x20) { throw new InvalidFormatException("Overflow at HgReader.GetBitCount"); } } int value = 1; while (n-- > 0) { value = (value << 1) | bits.GetNextBit(); } return(value); }
protected override IEnumerator <int> Unpack() { byte[] frame = new byte[0x1000]; int frame_pos = 1; for (;;) { int ctl = m_input.GetNextBit(); if (-1 == ctl) { yield break; } if (ctl != 0) { byte v = (byte)m_input.GetBits(8); m_buffer[m_pos++] = frame[frame_pos++ & 0xFFF] = v; if (0 == --m_length) { yield return(m_pos); } } else { int offset = m_input.GetBits(12); if (-1 == offset) { yield break; } int count = m_input.GetBits(4) + 2; while (count-- > 0) { byte v = frame[offset++ & 0xFFF]; m_buffer[m_pos++] = frame[frame_pos++ & 0xFFF] = v; if (0 == --m_length) { yield return(m_pos); } } } } }
public void Unpack() { m_input.BaseStream.Position = 0x11; int ctl_length = (m_input.ReadInt32() + 7) / 8; var ctl_bytes = m_input.ReadBytes (ctl_length); m_input.ReadInt32(); using (var ctl_mem = new MemoryStream (ctl_bytes)) using (var bits = new LsbBitStream (ctl_mem)) { int dst = 0; while (dst < m_output.Length) { int bit = bits.GetNextBit(); if (-1 == bit) break; if (0 == bit) { m_output[dst++] = m_input.ReadByte(); } else { int offset = m_input.ReadUInt16(); int count = (offset & 7) + 1; offset = (offset >> 3) + 1; Binary.CopyOverlapped (m_output, dst - offset, dst, count); dst += count; } } } }
void DecodeBlock(int block_size) { int channel_count = m_info.Format.Channels; for (int c = 0; c < channel_count; ++c) { if (8 == m_info.Format.BitsPerSample) { m_sample[c] = m_input.ReadUInt8(); } else { m_sample[c] = m_input.ReadInt16(); } } m_bits.Reset(); int channel = 0; int repeat_count = 0; for (int i = 0; i < block_size; ++i) { if (0 == repeat_count) { int ctl = m_bits.GetBits(3); if (7 == ctl) { if (1 == m_bits.GetNextBit()) { m_sample[channel] = 0; } else { int bits = 8; int shift = 9; if (m_info.Compression < 3) { bits -= m_info.Compression; shift += m_info.Compression; } int sign_bit = 1 << (bits - 1); int mask = sign_bit - 1; int val = m_bits.GetBits(bits); if (0 != (val & sign_bit)) { m_sample[channel] -= (short)((val & mask) << shift); } else { m_sample[channel] += (short)((val & mask) << shift); } } } else if (ctl != 0) { int bits, shift; if (m_info.Compression < 3) { bits = 5 - m_info.Compression; shift = 2 + ctl + m_info.Compression; } else { bits = 3 + m_info.Compression; shift = 1 + ctl; } int sign_bit = 1 << (bits - 1); int mask = sign_bit - 1; int val = m_bits.GetBits(bits); if (0 != (val & sign_bit)) { m_sample[channel] -= (short)((val & mask) << shift); } else { m_sample[channel] += (short)((val & mask) << shift); } } else if (m_info.RunLengthEncoded) { repeat_count = m_bits.GetNextBit(); if (1 == repeat_count) { repeat_count = m_bits.GetBits(2); if (3 == repeat_count) { repeat_count = m_bits.GetBits(8); } } } } else { --repeat_count; } if (8 == m_info.Format.BitsPerSample) { m_output[m_dst++] = (byte)m_sample[channel]; } else { LittleEndian.Pack(m_sample[channel], m_output, m_dst); m_dst += 2; } if (2 == channel_count) { channel ^= 1; } } }
static string DecodeClassName(byte[] enc, int length) { using (var output = new MemoryStream()) using (var input = new MemoryStream(enc, 0, length)) using (var bits = new LsbBitStream(input)) { if (0 == bits.GetNextBit()) { output.WriteByte((byte)'C'); } for (;;) { int b = bits.GetNextBit(); if (-1 == b) { break; } int c; if (0 == b) { int idx = bits.GetBits(4); if (-1 == idx) { break; } c = CharMap1[idx]; } else if (bits.GetNextBit() != 0) { int idx = bits.GetBits(5); if (-1 == idx) { break; } c = CharMap3[idx]; } else { int idx = bits.GetBits(4); if (-1 == idx) { break; } if (idx != 0) { c = CharMap2[idx]; } else { c = bits.GetBits(8); if (-1 == c) { break; } } } output.WriteByte((byte)c); } var buf = output.GetBuffer(); return(Encodings.cp932.GetString(buf, 0, (int)output.Length)); } }