static void ApplyParameters(CodecBase codec, string parameterText) { var type = codec.GetType(); var props = Utility.GetPropertiesWith <CodecParameterAttribute>(type).ToList(); parameterText = parameterText.Trim(new char[] { ':' }); var phrases = parameterText.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); foreach (var phrase in phrases) { var tokens = phrase.Split(new[] { '=' }); uint value; if (tokens.Length != 2 || !UInt32.TryParse(tokens[1], out value)) { Console.WriteLine($"Error! unknown parameter {phrase}"); continue; } var property = props.FirstOrDefault(p => { var attr = p.GetCustomAttribute(typeof(CodecParameterAttribute)) as CodecParameterAttribute; return(attr != null && attr.SymbolName == tokens[0]); } ); if (property == null) { Console.WriteLine($"Error! unknown parameter {phrase}"); continue; } property.SetValue(codec, value); Console.WriteLine($"Codec {codec.GetType().Name.Replace("Codec","")} parameter {property.Name} set to {value}"); } }
/// <summary> /// Test data round-trip. /// Gather info about it and return in result /// </summary> /// <param name="request"></param> /// <param name="codec"></param> /// <param name="data"></param> /// <returns></returns> Result TestDataRoundtrip(Request request, CodecBase codec, byte [] data) { var result = new Result(); var timer = new Stopwatch(); timer.Start(); var compressed = codec.Compress(data); timer.Stop(); result.CompressionTicks = timer.ElapsedTicks; result.CompressedLength = compressed?.Length ?? 0; result.UncompressedLength = data.Length; result.Success = true; // assume succeeded if (request.Decompress) { timer.Reset(); byte[] decompressed; if (request.UseExternalDecompressor) { byte canary = (byte)(data.Length ^ 0x5A); // check survives var decompressedCanary = new byte[data.Length + 1]; decompressedCanary[decompressedCanary.Length - 1] = canary; if (codec.GetType() == typeof(HuffmanCodec) && compressed != null) { timer.Start(); NativeMethods.DecompressHuffman(compressed, compressed.Length, decompressedCanary, decompressedCanary.Length - 1); timer.Stop(); } else if (codec.GetType() == typeof(Lz77Codec) && compressed != null) { timer.Start(); NativeMethods.DecompressLZ77(compressed, compressed.Length, decompressedCanary, decompressedCanary.Length - 1); timer.Stop(); } else if (codec.GetType() == typeof(ArithmeticCodec) && compressed != null) { timer.Start(); NativeMethods.DecompressArithmetic(compressed, compressed.Length, decompressedCanary, decompressedCanary.Length - 1); timer.Stop(); } else if (codec.GetType() == typeof(LzclCodec) && compressed != null) { timer.Start(); NativeMethods.DecompressLZCL(compressed, compressed.Length, decompressedCanary, decompressedCanary.Length - 1); timer.Stop(); } if (decompressedCanary[decompressedCanary.Length - 1] != canary) { throw new Exception("Decompression canary overwritten!"); } // must shrink decompressed to proper size for compare decompressed = new byte[data.Length]; Array.Copy(decompressedCanary, decompressed, decompressed.Length); } else { timer.Start(); decompressed = codec.Decompress(compressed); timer.Stop(); } result.DecompressionTicks = timer.ElapsedTicks; result.Success = CodecBase.CompareBytes(data, decompressed); } return(result); }