private static void DecompressLzo(Entry entry, Stream input, Stream output) { input.Seek(entry.Offset, SeekOrigin.Begin); var compressedData = new byte[entry.CompressedSize]; if (input.Read(compressedData, 0, compressedData.Length) != compressedData.Length) { throw new EndOfStreamException(); } var uncompressedData = new byte[entry.UncompressedSize]; int actualUncompressedLength = uncompressedData.Length; var result = LZO.Decompress(compressedData, 0, compressedData.Length, uncompressedData, 0, ref actualUncompressedLength); if (result != LZO.ErrorCode.Success) { throw new FormatException(string.Format("LZO decompression failure ({0})", result)); } if (actualUncompressedLength != uncompressedData.Length) { throw new FormatException("LZO decompression failure (uncompressed size mismatch)"); } output.Write(uncompressedData, 0, uncompressedData.Length); }
private byte[] UnpackSectionLZO(String Section) { var Compressed = ReadSection(Section); var Un64 = Convert.FromBase64String(Compressed); return(LZO.Slurp(Un64)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldCompareModifierProtocolsByListOrder() public virtual void ShouldCompareModifierProtocolsByListOrder() { IList <ModifierSupportedProtocols> supportedProtocols = asList(new ModifierSupportedProtocols(COMPRESSION, new IList <string> { LZO.implementation(), SNAPPY.implementation(), LZ4.implementation() })); IComparer <Org.Neo4j.causalclustering.protocol.Protocol_ModifierProtocol> comparator = ModifierProtocolRepository.GetModifierProtocolComparator(supportedProtocols).apply(COMPRESSION.canonicalName()); assertThat(comparator.Compare(LZO, TestProtocols_TestModifierProtocols.Snappy), Matchers.greaterThan(0)); assertThat(comparator.Compare(TestProtocols_TestModifierProtocols.Snappy, TestProtocols_TestModifierProtocols.Lz4), Matchers.greaterThan(0)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldNotCloseConnectionForGivenModifierProtocol() public void shouldNotCloseConnectionForGivenModifierProtocol() { // given ISet <string> versions = asSet(SNAPPY.implementation(), LZO.implementation(), LZ4.implementation()); _server.handle(InitialMagicMessage.Instance()); // when _server.handle(new ModifierProtocolRequest(COMPRESSION.canonicalName(), versions)); // then AssertUnfinished(); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldReturnModifierProtocolOfSingleConfiguredVersionIfOthersRequested() public virtual void ShouldReturnModifierProtocolOfSingleConfiguredVersionIfOthersRequested() { // given IList <ModifierSupportedProtocols> supportedProtocols = asList(new ModifierSupportedProtocols(COMPRESSION, new IList <string> { LZO.implementation() })); ModifierProtocolRepository modifierProtocolRepository = new ModifierProtocolRepository(TestProtocols_TestModifierProtocols.values(), supportedProtocols); // when Optional <Org.Neo4j.causalclustering.protocol.Protocol_ModifierProtocol> modifierProtocol = modifierProtocolRepository.Select(COMPRESSION.canonicalName(), asSet(TestProtocols_TestModifierProtocols.allVersionsOf(COMPRESSION))); // then //JAVA TO C# CONVERTER TODO TASK: Method reference arbitrary object instance method syntax is not converted by Java to C# Converter: assertThat(modifierProtocol.map(Protocol::implementation), OptionalMatchers.contains(LZO.implementation())); }
// Token: 0x06000025 RID: 37 RVA: 0x00002484 File Offset: 0x00000684 public byte[] ReadLZO(uint expectedSize) { bool flag = expectedSize >= 1024u; if (this.UseCompressionFlag) { flag = this.ReadBoolean(); } if (!flag) { return(this.ReadBytes((int)expectedSize)); } return(LZO.readLZO(this.BaseStream, expectedSize)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldSendFailModifierProtocolResponseForUnknownProtocol() public void shouldSendFailModifierProtocolResponseForUnknownProtocol() { // given ISet <string> versions = asSet(SNAPPY.implementation(), LZO.implementation(), LZ4.implementation()); _server.handle(InitialMagicMessage.Instance()); // when string protocolName = "let's just randomly reorder all the bytes"; _server.handle(new ModifierProtocolRequest(protocolName, versions)); // then verify(_channel).writeAndFlush(new ModifierProtocolResponse(FAILURE, protocolName, "")); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldNotCloseConnectionIfUnknownModifierProtocol() public void shouldNotCloseConnectionIfUnknownModifierProtocol() { // given ISet <string> versions = asSet(SNAPPY.implementation(), LZO.implementation(), LZ4.implementation()); _server.handle(InitialMagicMessage.Instance()); // when string protocolName = "let's just randomly reorder all the bytes"; _server.handle(new ModifierProtocolRequest(protocolName, versions)); // then AssertUnfinished(); }
public byte[] ReadLZO(uint expectedSize, bool forceCompressed = false) { bool isCompressed = (expectedSize >= 1024) || forceCompressed; if (UseCompressionFlag) { isCompressed = ReadBoolean(); } if (!isCompressed) { return(ReadBytes((int)expectedSize)); } return(LZO.ReadLZO(BaseStream, expectedSize)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldReturnModifierProtocolOfFirstConfiguredVersionRequestedAndSupported() public virtual void ShouldReturnModifierProtocolOfFirstConfiguredVersionRequestedAndSupported() { // given IList <ModifierSupportedProtocols> supportedProtocols = asList(new ModifierSupportedProtocols(COMPRESSION, new IList <string> { LZO.implementation(), SNAPPY.implementation(), LZ4.implementation() }), new ModifierSupportedProtocols(GRATUITOUS_OBFUSCATION, new IList <string> { NAME_CLASH.implementation() })); ModifierProtocolRepository modifierProtocolRepository = new ModifierProtocolRepository(TestProtocols_TestModifierProtocols.values(), supportedProtocols); // when Optional <Org.Neo4j.causalclustering.protocol.Protocol_ModifierProtocol> modifierProtocol = modifierProtocolRepository.Select(COMPRESSION.canonicalName(), asSet("bzip2", SNAPPY.implementation(), LZ4.implementation(), LZO.implementation(), "fast_lz")); // then //JAVA TO C# CONVERTER TODO TASK: Method reference arbitrary object instance method syntax is not converted by Java to C# Converter: assertThat(modifierProtocol.map(Protocol::implementation), OptionalMatchers.contains(LZO.implementation())); }
private static void CompressLzo(ref Entry entry, Stream input, Stream output) { var uncompressedData = input.ReadBytes((uint)input.Length); var uncompressedSize = (uint)uncompressedData.Length; var compressedData = new byte[uncompressedData.Length + (uncompressedData.Length / 16) + 64 + 3]; var actualCompressedSize = compressedData.Length; var result = LZO.Compress(uncompressedData, 0, uncompressedData.Length, compressedData, 0, ref actualCompressedSize); if (result != LZO.ErrorCode.Success) { throw new InvalidOperationException("compression error " + result.ToString()); } if (actualCompressedSize < uncompressedSize) { entry.CompressionScheme = CompressionScheme.LZO1x; entry.UncompressedSize = uncompressedSize; entry.CompressedSize = (uint)actualCompressedSize; output.Write(compressedData, 0, actualCompressedSize); } else { input.Seek(0, SeekOrigin.Begin); entry.CompressionScheme = CompressionScheme.None; entry.UncompressedSize = 0; entry.CompressedSize = (uint)input.Length; output.WriteFromStream(input, input.Length); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Parameterized.Parameters public static java.util.Collection<Parameters> data() public static ICollection <Parameters> Data() { // Application protocols ApplicationSupportedProtocols allRaft = new ApplicationSupportedProtocols(RAFT, TestProtocols_TestApplicationProtocols.listVersionsOf(RAFT)); ApplicationSupportedProtocols raft1 = new ApplicationSupportedProtocols(RAFT, singletonList(RAFT_1.implementation())); ApplicationSupportedProtocols allRaftByDefault = new ApplicationSupportedProtocols(RAFT, emptyList()); // Modifier protocols ICollection <ModifierSupportedProtocols> allModifiers = asList(new ModifierSupportedProtocols(COMPRESSION, TestProtocols_TestModifierProtocols.listVersionsOf(COMPRESSION)), new ModifierSupportedProtocols(GRATUITOUS_OBFUSCATION, TestProtocols_TestModifierProtocols.listVersionsOf(GRATUITOUS_OBFUSCATION)) ); ICollection <ModifierSupportedProtocols> allCompressionModifiers = singletonList(new ModifierSupportedProtocols(COMPRESSION, TestProtocols_TestModifierProtocols.listVersionsOf(COMPRESSION))); ICollection <ModifierSupportedProtocols> allObfuscationModifiers = singletonList(new ModifierSupportedProtocols(GRATUITOUS_OBFUSCATION, TestProtocols_TestModifierProtocols.listVersionsOf(GRATUITOUS_OBFUSCATION))); ICollection <ModifierSupportedProtocols> allCompressionModifiersByDefault = singletonList(new ModifierSupportedProtocols(COMPRESSION, emptyList())); IList <ModifierSupportedProtocols> onlyLzoCompressionModifiers = singletonList(new ModifierSupportedProtocols(COMPRESSION, singletonList(LZO.implementation()))); IList <ModifierSupportedProtocols> onlySnappyCompressionModifiers = singletonList(new ModifierSupportedProtocols(COMPRESSION, singletonList(SNAPPY.implementation()))); ICollection <ModifierSupportedProtocols> noModifiers = emptyList(); // Ordered modifier protocols ModifierProtocolRepository modifierProtocolRepository = new ModifierProtocolRepository(TestProtocols_TestModifierProtocols.values(), allModifiers); string[] lzoFirstVersions = new string[] { LZO.implementation(), LZ4.implementation(), SNAPPY.implementation() }; IList <ModifierSupportedProtocols> lzoFirstCompressionModifiers = singletonList(new ModifierSupportedProtocols(COMPRESSION, new IList <string> { lzoFirstVersions })); Protocol_ModifierProtocol preferredLzoFirstCompressionModifier = modifierProtocolRepository.Select(COMPRESSION.canonicalName(), asSet(lzoFirstVersions)).get(); string[] snappyFirstVersions = new string[] { SNAPPY.implementation(), LZ4.implementation(), LZO.implementation() }; IList <ModifierSupportedProtocols> snappyFirstCompressionModifiers = singletonList(new ModifierSupportedProtocols(COMPRESSION, new IList <string> { snappyFirstVersions })); Protocol_ModifierProtocol preferredSnappyFirstCompressionModifier = modifierProtocolRepository.Select(COMPRESSION.canonicalName(), asSet(snappyFirstVersions)).get(); return(asList(new Parameters(allRaft, allRaft, allModifiers, allModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION), TestProtocols_TestModifierProtocols.latest(GRATUITOUS_OBFUSCATION) }), new Parameters(allRaft, allRaftByDefault, allModifiers, allModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION), TestProtocols_TestModifierProtocols.latest(GRATUITOUS_OBFUSCATION) }), new Parameters(allRaftByDefault, allRaft, allModifiers, allModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION), TestProtocols_TestModifierProtocols.latest(GRATUITOUS_OBFUSCATION) }), new Parameters(allRaft, raft1, allModifiers, allModifiers, RAFT_1, new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION), TestProtocols_TestModifierProtocols.latest(GRATUITOUS_OBFUSCATION) }), new Parameters(raft1, allRaft, allModifiers, allModifiers, RAFT_1, new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION), TestProtocols_TestModifierProtocols.latest(GRATUITOUS_OBFUSCATION) }), new Parameters(allRaft, allRaft, allModifiers, allCompressionModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION) }), new Parameters(allRaft, allRaft, allCompressionModifiers, allModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION) }), new Parameters(allRaft, allRaft, allModifiers, allCompressionModifiersByDefault, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION) }), new Parameters(allRaft, allRaft, allCompressionModifiersByDefault, allModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(COMPRESSION) }), new Parameters(allRaft, allRaft, allModifiers, allObfuscationModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(GRATUITOUS_OBFUSCATION) }), new Parameters(allRaft, allRaft, allObfuscationModifiers, allModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.latest(GRATUITOUS_OBFUSCATION) }), new Parameters(allRaft, allRaft, allModifiers, lzoFirstCompressionModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { LZO }), new Parameters(allRaft, allRaft, lzoFirstCompressionModifiers, allCompressionModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { preferredLzoFirstCompressionModifier }), new Parameters(allRaft, allRaft, allModifiers, snappyFirstCompressionModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { SNAPPY }), new Parameters(allRaft, allRaft, snappyFirstCompressionModifiers, allCompressionModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { preferredSnappyFirstCompressionModifier }), new Parameters(allRaft, allRaft, allModifiers, onlyLzoCompressionModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.LZO }), new Parameters(allRaft, allRaft, onlyLzoCompressionModifiers, allModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] { TestProtocols_TestModifierProtocols.LZO }), new Parameters(allRaft, allRaft, onlySnappyCompressionModifiers, onlyLzoCompressionModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] {}), new Parameters(allRaft, allRaft, onlyLzoCompressionModifiers, onlySnappyCompressionModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] {}), new Parameters(allRaft, allRaft, allModifiers, noModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] {}), new Parameters(allRaft, allRaft, noModifiers, allModifiers, TestProtocols_TestApplicationProtocols.latest(RAFT), new Protocol_ModifierProtocol[] {}) )); }
private FastBinaryReader GetDataStream(ZFSFileInfo fileInfo) { ZFSFileInfo originalFile = null; if (fileInfo.ContainingPakFilename != null) { originalFile = fileInfo; fileInfo = _files[fileInfo.ContainingPakFilename]; } if (fileInfo.Compression == 0) { FastBinaryReader br = new FastBinaryReader(_zfsMemory) { Position = fileInfo.Offset, Length = (int)(fileInfo.Offset + fileInfo.Length) }; if (originalFile != null) { br.Position += originalFile.Offset; br.Length = (int)(br.Position + originalFile.Length); } return(br); } CompressionAlgorithm compressionAlgorithm; if (fileInfo.Compression == 2) { compressionAlgorithm = CompressionAlgorithm.LZO1X; } else if (fileInfo.Compression == 4) { compressionAlgorithm = CompressionAlgorithm.LZO1Y; } else { throw new Exception("Unknown compression " + fileInfo.Compression); } byte[] fileData = new byte[fileInfo.Length]; Buffer.BlockCopy(_zfsMemory, (int)fileInfo.Offset, fileData, 0, fileData.Length); byte[] decompressedData = new byte[fileInfo.DecompressedLength]; uint length = LZO.Decompress(fileData, decompressedData, fileInfo.DecompressedLength, compressionAlgorithm); if (length != fileInfo.DecompressedLength) { throw new Exception("Decompressed length does not match expected decompressed length"); } FastBinaryReader reader = new FastBinaryReader(decompressedData) { Position = 0, Length = decompressedData.Length }; if (originalFile != null) { reader.Position = originalFile.Offset; reader.Length = (int)(originalFile.Offset + originalFile.Length); } return(reader); }
static void Main(string[] args) { uint HPNT = (5 * 4); uint HDMY = (14 * 4); using (BinaryReader buffer = new BinaryReader(File.Open(args[0], FileMode.Open))) { LZO lzo = new LZO(); //Grab string header var stringbytes = new List <char>(); while (buffer.PeekChar() != 0x00) { stringbytes.Add(buffer.ReadChar()); } buffer.ReadChar(); //null string termination not handled by ReadString() //Skeleton and Origin Byte[] header = buffer.ReadBytes(41); UInt16 NumberHPNTs = buffer.ReadUInt16(); UInt16 NumberHDMYs = buffer.ReadUInt16(); UInt32 HLPR_Size = buffer.ReadUInt32(); Byte[] pad1 = buffer.ReadBytes(2); Byte[] helper_points = new Byte[HPNT * NumberHPNTs]; Byte[] helper_dummies = new Byte[HDMY * NumberHDMYs]; Byte[] helpers = new Byte[HLPR_Size]; UInt16 compressed_point_size = buffer.ReadUInt16(); if (compressed_point_size > 0) { Byte[] compressed = buffer.ReadBytes(compressed_point_size); Byte[] runoff = buffer.ReadBytes(3); helper_points = lzo.Decompress(compressed, compressed_point_size, helper_points, (int)(HPNT * NumberHPNTs)); System.Buffer.BlockCopy(runoff, 0, helper_points, helper_points.Length - 3, 3); } else { helper_points = buffer.ReadBytes((int)(HPNT * NumberHPNTs)); } UInt16 compressed_dummy_size = buffer.ReadUInt16(); if (compressed_dummy_size > 0) { Byte[] compressed = buffer.ReadBytes(compressed_dummy_size); Byte[] runoff = buffer.ReadBytes(3); helper_dummies = lzo.Decompress(compressed, compressed_dummy_size, helper_dummies, (int)(HDMY * NumberHDMYs)); System.Buffer.BlockCopy(runoff, 0, helper_dummies, helper_dummies.Length - 3, 3); } else { helper_dummies = buffer.ReadBytes((int)(HDMY * NumberHDMYs)); } UInt16 compressed_helper_size = buffer.ReadUInt16(); if (compressed_helper_size > 0) { Byte[] compressed = buffer.ReadBytes(compressed_helper_size); Byte[] runoff = buffer.ReadBytes(3); helpers = lzo.Decompress(compressed, compressed_helper_size, new byte[HLPR_Size], (int)HLPR_Size); System.Buffer.BlockCopy(runoff, 0, helpers, helpers.Length - 3, 3); } else { helpers = buffer.ReadBytes((int)HLPR_Size); } UInt32 NumberMaterials = buffer.ReadUInt32(); UInt32 NumberSubMeshes = buffer.ReadUInt32(); UInt32 NumberBones = buffer.ReadUInt32(); UInt32 SizeOfBoneIndex = buffer.ReadUInt32(); byte[] pad2 = buffer.ReadBytes(1); UInt16 unk1 = buffer.ReadUInt16(); UInt16 unk2 = buffer.ReadUInt16(); Byte[] identitymatrix = buffer.ReadBytes(12 * 4); //matrix, 12 floats for (var i = 1; i < NumberBones; i++) { } for (var i = 1; i < NumberMaterials; i++) { } for (var i = 1; i < NumberSubMeshes; i++) { } string newfile = Path.ChangeExtension(args[0], null) + ".unpacked.bbm"; (new FileInfo(newfile)).Directory.Create(); using (BinaryWriter b = new BinaryWriter(File.Open(newfile, FileMode.Create))) { //Leave it uncompressed compressed_point_size = 0; compressed_dummy_size = 0; compressed_helper_size = 0; //None of this is needed NumberMaterials = 0; NumberSubMeshes = 0; NumberBones = 0; unk1 = 0; unk2 = 0; //Write a new file... b.Write(stringbytes.ToArray()); b.Write(new Byte[] { 0x00 }); b.Write(header); b.Write(NumberHPNTs); b.Write(NumberHDMYs); b.Write(HLPR_Size); b.Write(pad1); b.Write(compressed_point_size); b.Write(helper_points); b.Write(compressed_dummy_size); b.Write(helper_dummies); b.Write(compressed_helper_size); b.Write(helpers); b.Write(NumberMaterials); b.Write(NumberSubMeshes); b.Write(NumberBones); b.Write(SizeOfBoneIndex); b.Write(pad2); b.Write(unk1); b.Write(unk2); b.Write(identitymatrix); //Nothing after this is needed or read by the game... } } }
private static void Main(string[] args) { bool showHelp = false; bool overwriteFiles = false; bool verbose = false; var options = new OptionSet() { { "o|overwrite", "overwrite existing files", v => overwriteFiles = v != null }, { "v|verbose", "be verbose", v => verbose = v != null }, { "h|help", "show this message and exit", v => showHelp = v != null }, }; List <string> extras; try { extras = options.Parse(args); } catch (OptionException e) { Console.Write("{0}: ", GetExecutableName()); Console.WriteLine(e.Message); Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName()); return; } if (extras.Count < 1 || extras.Count > 2 || showHelp == true) { Console.WriteLine("Usage: {0} [OPTIONS]+ input_tms [output_dir]", GetExecutableName()); Console.WriteLine(); Console.WriteLine("Options:"); options.WriteOptionDescriptions(Console.Out); return; } string inputPath = extras[0]; string outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, null) + "_unpack"; var endian = Endian.Little; using (var input = File.OpenRead(inputPath)) { var uncompressedSize3 = input.ReadValueU32(endian); var fileCount = input.ReadValueU32(endian); var magic = input.ReadValueU32(endian); var version = input.ReadValueU32(endian); if (magic != 0x9E2A83C1 || version != 0x00020000) { throw new FormatException(); } var compressedSize1 = input.ReadValueU32(endian); var uncompressedSize1 = input.ReadValueU32(endian); var compressedSize2 = input.ReadValueU32(endian); var uncompressedSize2 = input.ReadValueU32(endian); if (compressedSize1 != compressedSize2 || uncompressedSize1 != uncompressedSize2 || uncompressedSize1 != uncompressedSize3) { throw new FormatException(); } var compressedBytes = input.ReadBytes(compressedSize1); var uncompressedBytes = new byte[uncompressedSize1]; var actualUncompressedSize = (int)uncompressedSize1; var result = LZO.Decompress(compressedBytes, 0, (int)compressedSize1, uncompressedBytes, 0, ref actualUncompressedSize); if (result != LZO.ErrorCode.Success) { throw new FormatException(); } if (actualUncompressedSize != uncompressedSize1) { throw new FormatException(); } using (var data = new MemoryStream(uncompressedBytes)) { for (uint i = 0; i < fileCount; i++) { var entryNameLength = data.ReadValueS32(endian); if (entryNameLength < 0) { throw new NotImplementedException(); } var entryName = data.ReadString(entryNameLength, true, Encoding.ASCII); var entryTextLength = data.ReadValueS32(endian); Encoding entryTextEncoding; if (entryTextLength >= 0) { entryTextEncoding = Encoding.ASCII; } else { entryTextEncoding = Encoding.Unicode; entryTextLength = (-entryTextLength) * 2; } var entryText = data.ReadString(entryTextLength, true, entryTextEncoding); var entryPath = Path.Combine(outputPath, entryName); if (overwriteFiles == false && File.Exists(entryPath) == true) { continue; } if (verbose == true) { Console.WriteLine(entryName); } var entryParentPath = Path.GetDirectoryName(entryPath); if (string.IsNullOrEmpty(entryParentPath) == false) { Directory.CreateDirectory(entryParentPath); } using (var output = new StreamWriter(entryPath, false, entryTextEncoding)) { output.Write(entryText); } } } } }
private static void Main(string[] args) { var paths = Directory.GetFiles("saves", "*.sav"); var successes = 0; var failures = 0; foreach (var path in paths) { var name = Path.GetFileNameWithoutExtension(path); using (var input = File.OpenRead(path)) { var readHash = input.ReadBytes(20); using (var data = input.ReadToMemoryStream(input.Length - 20)) { byte[] computedHash; using (var sha1 = new System.Security.Cryptography.SHA1Managed()) { computedHash = sha1.ComputeHash(data); } if (readHash.SequenceEqual(computedHash) == false) { Console.WriteLine("{0}: failed (SHA1 mismatch)", name); failures++; continue; } data.Position = 0; var uncompressedSize = data.ReadValueU32(Endian.Big); var actualUncompressedSize = (int)uncompressedSize; var uncompressedBytes = new byte[uncompressedSize]; var compressedSize = (int)(data.Length - 4); var compressedBytes = data.ReadBytes(compressedSize); var result = LZO.Decompress(compressedBytes, 0, compressedSize, uncompressedBytes, 0, ref actualUncompressedSize); if (result != LZO.ErrorCode.Success) { Console.WriteLine("{0}: failed (LZO error {1})", name, result); failures++; continue; } using (var outerData = new MemoryStream(uncompressedBytes)) { var innerSize = outerData.ReadValueU32(Endian.Big); var magic = outerData.ReadString(3); if (magic != "WSG") { Console.WriteLine("{0}: failed (bad magic)", name); failures++; continue; } var version = outerData.ReadValueU32(Endian.Little); if (version != 2 && version.Swap() != 2) { Console.WriteLine("{0}: failed (bad version)", name); failures++; continue; } var endian = version == 2 ? Endian.Little : Endian.Big; var hash = outerData.ReadValueU32(endian); var innerUncompressedSize = outerData.ReadValueS32(endian); var innerCompressedBytes = outerData.ReadBytes(innerSize - 3 - 4 - 4 - 4); var innerUncompressedBytes = Huffman.Decoder.Decode(innerCompressedBytes, innerUncompressedSize); using (var innerUncompressedData = new MemoryStream(innerUncompressedBytes)) { using (var output = File.Create("temp.bin")) { output.WriteBytes(innerUncompressedBytes); } var saveGame = Serializer.Deserialize <WillowTwoSave.WillowTwoPlayerSaveGame>(innerUncompressedData); SaveExpansion.ExtractExpansionSavedataFromUnloadableItemData(saveGame); using (var testData = new MemoryStream()) { SaveExpansion.AddExpansionSavedataToUnloadableItemData(saveGame); Serializer.Serialize(testData, saveGame); testData.Position = 0; var testBytes = testData.ReadBytes((uint)testData.Length); if (innerUncompressedBytes.SequenceEqual(testBytes) == false) { Console.WriteLine("{0}: failed (reencode mismatch)", name); using (var output = File.Create(Path.Combine("failures", name + "_before.bin"))) { output.WriteBytes(innerUncompressedBytes); } using (var output = File.Create(Path.Combine("failures", name + "_after.bin"))) { output.WriteBytes(testBytes); } failures++; continue; } successes++; } } } } } } Console.WriteLine("{0} processed ({1} failed, {2} succeeded).", paths.Length, failures, successes); }
private static void Main(string[] args) { bool showHelp = false; bool verbose = false; var options = new OptionSet() { { "v|verbose", "be verbose", v => verbose = v != null }, { "h|help", "show this message and exit", v => showHelp = v != null }, }; List <string> extras; try { extras = options.Parse(args); } catch (OptionException e) { Console.Write("{0}: ", GetExecutableName()); Console.WriteLine(e.Message); Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName()); return; } if (extras.Count < 1 || extras.Count > 2 || showHelp == true) { Console.WriteLine("Usage: {0} [OPTIONS]+ input_dir [output_tms]", GetExecutableName()); Console.WriteLine(); Console.WriteLine("Options:"); options.WriteOptionDescriptions(Console.Out); return; } string baseInputPath = extras[0]; string outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(baseInputPath + "_packed", ".tms"); var endian = Endian.Little; var inputPaths = Directory.GetFiles(baseInputPath); byte[] uncompressedBytes; using (var data = new MemoryStream()) { foreach (var inputPath in inputPaths) { { var inputName = Path.GetFileName(inputPath) ?? ""; var inputNameBytes = Encoding.ASCII.GetBytes(inputName); Array.Resize(ref inputNameBytes, inputNameBytes.Length + 1); data.WriteValueS32(inputNameBytes.Length, endian); data.WriteBytes(inputNameBytes); } { var inputText = File.ReadAllText(inputPath); var inputTextIsUnicode = inputText.Any(c => c > 127); if (inputTextIsUnicode == false) { var inputTextBytes = Encoding.ASCII.GetBytes(inputText); Array.Resize(ref inputTextBytes, inputTextBytes.Length + 1); data.WriteValueS32(inputTextBytes.Length, endian); data.WriteBytes(inputTextBytes); } else { var inputTextBytes = Encoding.Unicode.GetBytes(inputText); Array.Resize(ref inputTextBytes, inputTextBytes.Length + 2); data.WriteValueS32(-(inputTextBytes.Length / 2), endian); data.WriteBytes(inputTextBytes); } } } data.Flush(); uncompressedBytes = (byte[])data.GetBuffer().Clone(); Array.Resize(ref uncompressedBytes, (int)data.Length); } var compressedBytes = new byte[uncompressedBytes.Length + (uncompressedBytes.Length / 16) + 64 + 3]; var actualCompressedSize = compressedBytes.Length; var result = LZO.Compress(uncompressedBytes, 0, uncompressedBytes.Length, compressedBytes, 0, ref actualCompressedSize); if (result != LZO.ErrorCode.Success) { throw new SaveCorruptionException(string.Format("LZO compression failure ({0})", result)); } Array.Resize(ref compressedBytes, actualCompressedSize); using (var output = File.Create(outputPath)) { output.WriteValueS32(uncompressedBytes.Length, endian); output.WriteValueS32(inputPaths.Length, endian); output.WriteValueU32(0x9E2A83C1, endian); output.WriteValueU32(0x00020000, endian); output.WriteValueS32(compressedBytes.Length, endian); output.WriteValueS32(uncompressedBytes.Length, endian); output.WriteValueS32(compressedBytes.Length, endian); output.WriteValueS32(uncompressedBytes.Length, endian); output.WriteBytes(compressedBytes); } }