Beispiel #1
0
        private static void DecodeAndExtractFirm(string filename, int decodeOffset = 0)
        {
            //SendEvent("TryFile", openFileDialog1.File.Name);

            var ext = Path.GetExtension(filename).ToLower();

            if (ext == ".dmg")
            {
                MessageBox.Show("Please open the DMG file and select the .BIN file inside");
                return;
            }
            if (ext == ".exe")
            {
                MessageBox.Show("Please open the .EXE file and select the .BIN file inside");
                return;
            }
            if (ext != ".bin")
            {
                MessageBox.Show("The files need to be a Nikon Firmware .BIN file");
                return;
            }



            // Open the selected file to read.
            System.IO.Stream fileStream = File.OpenRead(filename);

            //SendEvent("OpenFile", openFileDialog1.File.Name);

            if (fileStream.Length > (48 * 1024 * 1024))
            {
                fileStream.Close();
                return;
            }

            byte[] data = new byte[fileStream.Length];

            if (data == null)
            {
                fileStream.Close();
                return;
            }

            fileStream.Read(data, 0, (int)fileStream.Length);
            fileStream.Close();

            var p = new Package();

            if (p.LoadData(data))
            {
                BinaryWriter bw = new BinaryWriter(File.Open(filename + ".out.bin", FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
                bw.Write(p.raw);
                bw.Close();
            }



            //DecodePackageFile(filename, decodeOffset);
            //ExactFirmware(filename);
        }
Beispiel #2
0
        private static void DecodeAndExtractFirm(string filename, int decodeOffset = 0)
        {
            //SendEvent("TryFile", openFileDialog1.File.Name);

            var ext = Path.GetExtension(filename).ToLower();

            if (ext == ".dmg")
            {
                MessageBox.Show("Please open the DMG file and select the .BIN file inside");
                return;
            }
            if (ext == ".exe")
            {
                MessageBox.Show("Please open the .EXE file and select the .BIN file inside");
                return;
            }
            if (ext != ".bin")
            {
                MessageBox.Show("The files need to be a Nikon Firmware .BIN file");
                return;
            }



            // Open the selected file to read.
            System.IO.Stream fileStream = File.OpenRead(filename);

            //SendEvent("OpenFile", openFileDialog1.File.Name);

            if (fileStream.Length > (48 * 1024 * 1024))
            {
                fileStream.Close();
                return;
            }

            byte[] data = new byte[fileStream.Length];

            if (data == null)
            {
                fileStream.Close();
                return;
            }

            fileStream.Read(data, 0, (int)fileStream.Length);
            fileStream.Close();

            var p = new Package();

            if (p.LoadData(data))
            {
                BinaryWriter bw = new BinaryWriter(File.Open(filename + ".out.bin", FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
                bw.Write(p.raw);
                bw.Close();
            }

            if (p.TryOpen())
            {
                for (int i = 0; i < p.header.Count; i++)
                {
                    var h = p.header[i];
                    var b = p.blocks[i];

                    BinaryWriter bw = null;

                    try
                    {
                        var new_filename = Path.Combine(Path.GetDirectoryName(filename), h.Item1);
                        bw = new BinaryWriter(File.Open(new_filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
                        bw.Write(b);
                    }
                    finally
                    {
                        if (bw != null)
                        {
                            bw.Close();
                        }
                    }
                }
            }
        }
Beispiel #3
0
        private static void DecodeAndExtractFirm(string filename)
        {
            //SendEvent("TryFile", openFileDialog1.File.Name);

            var ext = Path.GetExtension(filename).ToLower();

            if (ext == ".dmg")
            {
                MessageBox.Show("Please open the DMG file and select the .BIN file inside");
                return;
            }
            if (ext == ".exe")
            {
                MessageBox.Show("Please open the .EXE file and select the .BIN file inside");
                return;
            }
            if (ext != ".bin")
            {
                MessageBox.Show("The files need to be a Nikon Firmware .BIN file");
                return;
            }



            // Open the selected file to read.
            System.IO.Stream fileStream     = File.OpenRead(filename);
            const int        headerCheckLen = 0x20;

            byte[] headerCheck = new byte[headerCheckLen];

            fileStream.Read(headerCheck, 0, headerCheckLen);

            bool v1Header = headerCheck.All(b => b == 0x20);
            bool v2Header = false;
            long readLen  = fileStream.Length;
            int  readOff  = 0;

            if (v1Header == false)
            {
                fileStream.Seek(headerCheckLen, SeekOrigin.Begin);
                fileStream.Read(headerCheck, 0, headerCheckLen);
                v2Header = headerCheck.All(b => b == 0x20);
                if (v2Header)
                {
                    readLen -= headerCheckLen;
                    readOff  = headerCheckLen;
                }
            }
            fileStream.Seek(readOff, SeekOrigin.Begin);


            //SendEvent("OpenFile", openFileDialog1.File.Name);

            if (readLen > (48 * 1024 * 1024))
            {
                fileStream.Close();
                return;
            }

            byte[] data = new byte[readLen];

            if (data == null)
            {
                fileStream.Close();
                return;
            }

            fileStream.Read(data, 0, (int)readLen);
            fileStream.Close();

            var p = new Package();

            if (p.LoadData(data))
            {
                BinaryWriter bw = new BinaryWriter(File.Open(filename + ".out.bin", FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
                bw.Write(p.raw);
                bw.Close();
            }

            if (p.TryOpen())
            {
                for (int i = 0; i < p.header.Count; i++)
                {
                    var h = p.header[i];
                    var b = p.blocks[i];

                    BinaryWriter bw = null;

                    try
                    {
                        var new_filename = Path.Combine(Path.GetDirectoryName(filename), h.Item1);
                        bw = new BinaryWriter(File.Open(new_filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
                        bw.Write(b);
                    }
                    finally
                    {
                        if (bw != null)
                        {
                            bw.Close();
                        }
                    }
                }
            }
        }