internal static string Decompress(string data) { var coder = new Coder(new Coder.Tree()); coder.Decode( Encoding.UTF8.GetBytes(data), out var result); return(Encoding.UTF8.GetString(result)); }
internal static string Compress(string data) { var coder = new Coder(new Coder.Tree()); return(Encoding.UTF8.GetString(coder.Encode(data))); }
private void Init(Dictionary <byte, Dictionary <uint, byte> > lookup) { if (lookup == null) { throw new InvalidOperationException("No lookup table has been created. Either provide one or generate it"); } MinBitLen = 32; MaxBitLen = 0; foreach (byte v in lookup.Keys) { if (v < MinBitLen) { MinBitLen = v; } if (v > MaxBitLen) { MaxBitLen = v; } } ForwardLookup = new ForwardLookupEntry[256]; ReverseLookup = new Dictionary <byte, Dictionary <uint, ReverseLookupEntry> >(); foreach (var kv in lookup) { var entry = new Dictionary <uint, ReverseLookupEntry>(); foreach (var kkv in kv.Value) { ForwardLookup[kkv.Value] = new ForwardLookupEntry { Value = kkv.Key, BitLength = kv.Key, CurrentBits = (byte)(kv.Key % 8), TotalBytes = (byte)(kv.Key >> 3 << 3) }; entry.Add(kkv.Key, new ReverseLookupEntry { Value = kkv.Value, Collides = false }); } ReverseLookup.Add(kv.Key, entry); } var test = new Coder(this); var clist = new HashSet <byte>(); for (byte i = 0; i < 255; i++) { test.Decode(test.Encode(new byte[1] { i }), out var arr); if (arr.Length > 0) { byte t = arr[0]; if (t != i) { if (!clist.Contains(t)) { clist.Add(t); } if (!clist.Contains(i)) { clist.Add(i); } } } } foreach (var b in clist) { var entry = ForwardLookup[b]; ReverseLookup[entry.BitLength][entry.Value].Collides = true; } }