public void Test(string fname) { if (Directory.Exists(outputDir)) Directory.Delete(outputDir, true); Directory.CreateDirectory(outputDir); BinaryReader br = new BinaryReader(new FileStream(fname, FileMode.Open)); header.Read(br); Int16 i = 0; File.WriteAllBytes(outputDir + "\\header.bin", header.unkn); while (br.BaseStream.Position < br.BaseStream.Length) { UInt32 blockOffset = (UInt32)br.BaseStream.Position; TBlock block = new TBlock(); block.Read(br); blocks.Add(block); Console.Write(" " + block.contentType + " At:" + Utils.ToHex(blockOffset) + " size:" + Utils.ToHex((UInt32)block.content.Length) + " " + Utils.ToHex(block.blockChecksum8)); switch (block.contentType) { case EContentType.Code: { TCodeBlock codeBlock = block.blockHeader as TCodeBlock; if (codeBlock != null) Console.Write(" " + Utils.ToHex(codeBlock.maybe_crc) + " " + Utils.ToHex(codeBlock.location)); break; } case EContentType.Data: { i++; TDataBlock dataBlock = block.blockHeader as TDataBlock; if (dataBlock != null) Console.Write(" descr:" + dataBlock.description); break; } default: { throw new Exception("Unknown Block"); } } Console.WriteLine(""); } br.Close(); EContentType oldType = blocks[0].contentType; FileStream fs = null; BufferedStream bs = null; i = 0; bool firstCode = false; foreach (TBlock block in blocks) { if (block.contentType == EContentType.Data) { if (oldType == EContentType.Data) { fs = new FileStream(outputDir + "\\" + "rofsImage.img", FileMode.Create, FileAccess.Write); bs = new BufferedStream(fs); firstCode = true; // File.WriteAllBytes(outputDir + i + "_InitBytes.bin", block.content); } else { fs = new FileStream(outputDir + "\\" + "rofsImage.img", FileMode.Append, FileAccess.Write); bs = new BufferedStream(fs); } //bs.Write(block.content, 0, block.content.Length); bs.Write(block.content, firstCode ? 0xc00 : 0, firstCode ? block.content.Length - 0xc00 : block.content.Length); firstCode = false; bs.Close(); fs.Close(); } if (block.contentType == EContentType.Data) { i++; //TDataBlock dataBlock = block.blockHeader as TDataBlock; //string outfile = outputDir + i + "_" + Utils.CleanFileName(dataBlock.description) + ".bin"; //File.WriteAllBytes(outfile, block.content); } oldType = block.contentType; } bs.Close(); fs.Close(); }
static public void Test(string fname) { outputDir = Directory.GetCurrentDirectory() + @"\headers"; if (Directory.Exists(outputDir)) Directory.Delete(outputDir, true); Directory.CreateDirectory(outputDir); BinaryReader br = new BinaryReader(new FileStream(fname, FileMode.Open)); header.Read(br); Int16 i = 0; File.WriteAllBytes(outputDir + "\\header.bin", header.unkn); int cnt = 1; while (br.BaseStream.Position < br.BaseStream.Length) { UInt32 blockOffset = (UInt32)br.BaseStream.Position; TBlock block = new TBlock(); block.Read(br); blocks.Add(block); Console.Write(" " + block.contentType + " At:" + Utils.ToHex(blockOffset) + " size:" + Utils.ToHex((UInt32)block.content.Length) + " " + Utils.ToHex(block.blockChecksum8)); switch (block.contentType) { case EContentType.Code: { BinaryWriter bwHeader = new BinaryWriter(new FileStream(outputDir + @"\" + cnt.ToString()+ ".header.bin",FileMode.Create)); block.blockHeader.Write(bwHeader); bwHeader.Close(); cnt++; TCodeBlock codeBlock = block.blockHeader as TCodeBlock; if (codeBlock != null) Console.Write(" " + Utils.ToHex(codeBlock.maybe_crc) + " " + Utils.ToHex(codeBlock.location)); break; } case EContentType.Data: { i++; BinaryWriter bwHeader = new BinaryWriter(new FileStream(outputDir + @"\" + cnt.ToString() + ".header.bin", FileMode.Create)); block.blockHeader.Write(bwHeader); bwHeader.Close(); cnt++; TDataBlock dataBlock = block.blockHeader as TDataBlock; if (dataBlock != null) Console.Write(" descr:" + dataBlock.description); break; } default: { throw new Exception("Unknown Block"); } } Console.WriteLine(""); } br.Close(); // Merge the Code Blocks // Dump Data and Code to file... EContentType oldType = blocks[0].contentType; FileStream fs = null; BufferedStream bs = null; i = 0; foreach (TBlock block in blocks) { if (block.contentType == EContentType.Data) { if (oldType == EContentType.Data) { fs = new FileStream(outputDir + "\\" + i + "_code.bin", FileMode.Create, FileAccess.Write); bs = new BufferedStream(fs); // File.WriteAllBytes(outputDir + i + "_InitBytes.bin", block.content); } else { fs = new FileStream(outputDir + "\\" + i + "_code.bin", FileMode.Append, FileAccess.Write); bs = new BufferedStream(fs); } bs.Write(block.content, 0, block.content.Length); bs.Close(); fs.Close(); } if (block.contentType == EContentType.Data) { i++; //TDataBlock dataBlock = block.blockHeader as TDataBlock; //string outfile = outputDir + i + "_" + Utils.CleanFileName(dataBlock.description) + ".bin"; //File.WriteAllBytes(outfile, block.content); } oldType = block.contentType; } bs.Close(); fs.Close(); }