static void Check(Compression compression, string input, int offset) { if ((compression.supportedModes & CompressionOperation.Check) == CompressionOperation.None) { messagePoster("Compression type doesn't support checking."); return; } byte[] file; try { BinaryReader br = new BinaryReader(File.OpenRead(input)); file = br.ReadBytes((int)br.BaseStream.Length); br.Close(); } catch (IOException) { messagePoster("Error while opening the file."); return; } if (offset > file.Length) { messagePoster("Offset is larger than the size of the file."); return; } if (compression.CanBeDecompressed(file, offset, 0, 0xFFFFFF)) { messagePoster("Data can be decompressed."); } else { messagePoster("Data can be decompressed."); } }
static void userInterface_OnCompressionChanged(int obj) { currentCompression = compressions[obj]; UI.CheckEnabled = currentCompression.SupportsOperation(CompressionOperation.Check); UI.CompressEnabled = currentCompression.SupportsOperation(CompressionOperation.Compress); UI.DecompLenghtEnabled = currentCompression.SupportsOperation(CompressionOperation.DecompLength); UI.DecompressEnabled = currentCompression.SupportsOperation(CompressionOperation.Decompress); UI.LengthEnabled = currentCompression.SupportsOperation(CompressionOperation.CompLength); UI.ScanEnabled = currentCompression.SupportsOperation(CompressionOperation.Scan); UI.SetFileExtensions(currentCompression.fileExtensions); }
static void Test(Compression compression, string originalFile, string report) { BinaryReader br = new BinaryReader(File.OpenRead(originalFile)); byte[] file = br.ReadBytes((int)br.BaseStream.Length); br.Close(); int[] offsetsToTest = compression.Scan(file, 0, file.Length, 4, 0, file.Length); int i; List<int> differentOffsets = new List<int>(50); for (i = 0; i < offsetsToTest.Length; i++) { bool success = true; byte[] data = compression.Compress(compression.Decompress(file, offsetsToTest[i])); byte[] data2 = new byte[compression.CompressedLength(file, offsetsToTest[i])]; Array.Copy(file, offsetsToTest[i], data2, 0, data2.Length); success = data.Length <= data2.Length; /*for (int j = 0; j < data.Length && success; j++) success = data[j] == data2[j];*/ if (!success) differentOffsets.Add(offsetsToTest[i]); } if (differentOffsets.Count == 0) { messagePoster("No differences were found."); } else { messagePoster("There is a difference."); StreamWriter sw = new StreamWriter(report, false); for (int j = 0; j < differentOffsets.Count; j++) { sw.WriteLine(Convert.ToString(differentOffsets[j], 16)); } sw.Close(); } }
static void Scan(Compression compression, string input, string output, int offset, int lenght) { if ((compression.supportedModes & CompressionOperation.Scan) == CompressionOperation.None) { messagePoster("Compression type doesn't support scanning."); return; } byte[] file; try { BinaryReader br = new BinaryReader(File.OpenRead(input)); file = br.ReadBytes((int)br.BaseStream.Length); br.Close(); } catch (IOException) { messagePoster("Error while opening input file."); return; } if (lenght == 0 || lenght + offset > file.Length) lenght = file.Length - offset; int[] result = compression.Scan(file, offset, lenght, 4, 0, 0x100000); if (result.Length == 0) messagePoster("No compressed offsets were found."); else { StreamWriter sw = new StreamWriter(output); for (int i = 0; i < result.Length; i++) sw.WriteLine(Convert.ToString(result[i], 16)); sw.Close(); messagePoster("Scanning was successful."); } }
static void Decompress(Compression compression, string input, string output, int inputOffset, int outputOffset) { if ((compression.supportedModes & CompressionOperation.Decompress) == CompressionOperation.None) { messagePoster("Compression type doesn't support decompression."); return; } byte[] file; try { BinaryReader br = new BinaryReader(File.OpenRead(input)); file = br.ReadBytes((int)br.BaseStream.Length); br.Close(); } catch (IOException) { messagePoster("Error while opening input file."); return; } if (inputOffset > file.Length) { messagePoster("Offset is larger than the size of the file."); return; } byte[] decompData = compression.Decompress(file, inputOffset); if (decompData == null) { messagePoster("Data can't be decompressed."); return; } try { BinaryWriter bw = new BinaryWriter(File.Open(output, FileMode.OpenOrCreate)); bw.BaseStream.Position = outputOffset; bw.Write(decompData); bw.Close(); } catch (IOException) { messagePoster("Error while opening output file."); return; } messagePoster("Finished"); }
static void DecompLength(Compression compression, string input, int offset) { if ((compression.supportedModes & CompressionOperation.DecompLength) == CompressionOperation.None) { messagePoster("Compression type doesn't support length checking."); return; } byte[] file; try { BinaryReader br = new BinaryReader(File.OpenRead(input)); file = br.ReadBytes((int)br.BaseStream.Length); br.Close(); } catch (IOException) { messagePoster("Error while opening the file."); return; } if (offset > file.Length) messagePoster("Offset is larger than the size of the file."); else { int result = compression.DecompressedDataLenght(file, offset); if (result == -1) messagePoster("Data can't be decompressed"); else messagePoster("Length of the decompressed data is: 0x" + Convert.ToString(result, 16)); } }
static void Compress(Compression compression, string input, string output, int inputOffset, int length, int outputOffset) { if ((compression.supportedModes & CompressionOperation.Compress) == CompressionOperation.None) { messagePoster("Compression type doesn't support compressing."); return; } byte[] data; try { BinaryReader br = new BinaryReader(File.OpenRead(input)); br.BaseStream.Position = inputOffset; if (length == 0 || (br.BaseStream.Length - br.BaseStream.Position) < length) { length = (int)(br.BaseStream.Length - br.BaseStream.Position); } data = br.ReadBytes(length); br.Close(); } catch (IOException) { messagePoster("Error while opening the file."); return; } byte[] compData = compression.Compress(data); try { BinaryWriter bw = new BinaryWriter(File.Open(output, FileMode.OpenOrCreate)); bw.BaseStream.Position = outputOffset; bw.Write(compData); bw.Close(); } catch (IOException) { messagePoster("Error while opening output file."); return; } messagePoster("Finished"); }