public override byte[] Decompress(byte[] buf) { var tableID = BitList.From(buf).SetMinimumSize(16).ToUInt16(); if (FixedTableCompressions.TryGetValue(tableID, out var compType)) { var comp = Activator.CreateInstance(compType) as CompressionBase; return(comp.Decompress(buf)); } else { return((new CompressionHuffmanCoding()).Decompress(buf)); } }
public BitList GetBitResult(INode leaf) { var ret = new List <bool>(); InternalNode parent = null; for (var node = leaf; node != null; node = parent) { parent = GetParent(node); if (parent != null) { ret.Add(ReferenceEquals(node, parent.Right)); } } return(BitList.From(((IEnumerable <bool>)ret).Reverse())); }
public override byte[] Decompress(byte[] buf) { var bits = new BitList(buf); if (bits.Count < 24) { throw new Exception("Table Format Error: Size is not enough"); } var tableID = bits.SetMinimumSize(16).ToUInt16(); if (tableID != 0xffff) { throw new Exception("Table Format Error"); } return(bits.ToBytes().Skip(2).ToArray()); }
public override byte[] Compress(byte[] buf) { var ret = MakeTableBinary(TableID, Table, out var table); // ADD DATA FIELDS ret.Add(BitList.MakeVariableBits((UInt64)buf.Length)); // [DA] Data Bytes foreach (var c in buf) { ret.Add(table[c]); // [DB] Huffman code if (ret.ByteCount >= LimitSize) { return(null); } } ret.AddPad(); // [DC] padding return(ret.ToByteArray()); }
public static Dictionary <BitList, byte> ReadTableBinary(BitList bits, out int nextBitIndex) { // === READ TABLE === var table = new Dictionary <BitList, byte>(); var tableCount = BitList.From(bits.Subbit(0, 16)).SetMinimumSize(16).ToUInt16(); // [TA] Debug.Assert(tableCount < 32768); nextBitIndex = 16; for (var i = 0; i < tableCount; i++) { var value = BitList.From(bits.Subbit(nextBitIndex, 8)).ToByte(); // [TB] nextBitIndex += 8; var bitLen = (int)BitList.GetNumberFromVariableBits(bits.Subbit(nextBitIndex), out var numbits); nextBitIndex += numbits; table[BitList.From(bits.Subbit(nextBitIndex, bitLen))] = value; // [TE] nextBitIndex += bitLen; } if (nextBitIndex % 8 != 0) { nextBitIndex += 8 - nextBitIndex % 8; // [TF] } return(table); }
/// <summary> /// copy constructor /// </summary> /// <param name="from"></param> public BitList(BitList from) { Bytes.AddRange(from.Bytes); Count = from.Count; }