/// <summary> /// Encode a RIF byte array to zRIF string. /// </summary> /// <param name="zrif">The zRIF string.</param> /// <returns>The zRIF string or null on error.</returns> public static string Encode(byte[] rif) { byte[] output = new byte[128 + 2]; if ((rif.Length != 512) && (rif.Length != 1024)) { Console.Error.WriteLine("[ERROR] invalid RIF length"); return(null); } var deflater = new Deflater(); deflater.SetDictionary(zrif_dict); deflater.SetInput(rif); deflater.SetLevel(Deflater.BEST_COMPRESSION); deflater.SetStrategy(DeflateStrategy.Default); deflater.Finish(); int size = deflater.Deflate(output, 0, output.Length - 2); if (!deflater.IsFinished) { Console.Error.WriteLine("[ERROR] Deflate error"); return(null); } // Don't have that much control over Window size so our header needs to be adjusted. if ((output[0] == 0x78) && (output[1] == 0xF9)) { output[0] = 0x28; output[1] = 0xEE; } // Adjust the size to be a multiple of 3 so that we don't get padding return(Convert.ToBase64String(output, 0, ((size + 2) / 3) * 3)); }