Пример #1
0
        private static void BuildCubeFile()
        {
            CubeExtractor cbld = new CubeExtractor
            {
                ModelFileCount = cubeFiles.Length,

                ModelFileSize = 10, // fileCount, FileSize, filename Size

                ModelFilenameSize         = 0x104,
                MaxFilenameLengthPlusSize = 0x108
            };


            for (int i = 0; i < cubeFiles.Length; i++)
            {
                cbld.ModelFileSize += cbld.MaxFilenameLengthPlusSize;
                cbld.ModelFileNames.Add(cubeFiles[i]);

                using (var inFile = File.OpenRead(cubeFolder + "\\" + cubeFiles[i]))
                    using (var binaryReader = new BinaryReader(inFile))
                    {
                        var inputData = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
                        cbld.ModelFiles.Add(cubeFiles[i], inputData);
                        cbld.ModelFileSize += inputData.Length;
                        inFile.Close();
                    }
            }

            FileBackup.MakeBackup(cubeFilename, 5);

            using (var outFile = File.OpenWrite(cubeFilename))
                using (var binaryWriter = new BinaryWriter(outFile))
                {
                    binaryWriter.Write(cbld.ModelFileCount);
                    binaryWriter.Write(cbld.ModelFileSize);
                    binaryWriter.Write(cbld.MaxFilenameLengthPlusSize);
                    foreach (String fname in cbld.ModelFileNames)
                    {
                        Byte[] fnameData   = new byte[cbld.ModelFilenameSize];
                        Byte[] fnameBytes  = Encoding.ASCII.GetBytes(fname);
                        int    fnameLength = cbld.ModelFilenameSize <= fnameBytes.Length ? cbld.ModelFilenameSize : fnameBytes.Length;
                        Array.Copy(fnameBytes, fnameData, fnameLength);
                        binaryWriter.Write(cbld.ModelFiles[fname].Length);
                        binaryWriter.Write(fnameData, 0, cbld.ModelFilenameSize);
                        binaryWriter.Write(cbld.ModelFiles[fname]);
                    }
                    outFile.Close();
                }
        }
Пример #2
0
        private static void DecodeCubeFile()
        {
            Encoding encoding = Encoding.ASCII;


            if (cube3File != null || cube3File.Length > 0)
            {
                try
                {
                    using (var inFile = File.OpenRead(cube3File))
                        using (var binaryReader = new BinaryReader(inFile))
                        {
                            var inputCubeFile = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);

                            CubeExtractor ce = new CubeExtractor(inputCubeFile);

                            foreach (String filename in ce.ModelFiles.Keys)
                            {
                                String directory = Path.GetDirectoryName(filename);

                                if (directory.Length > 0)
                                {
                                    Directory.CreateDirectory(directory);
                                }

                                FileBackup.MakeBackup(filename, 5);

                                using (var outFile = File.OpenWrite(filename))
                                    using (var binaryWriter = new BinaryWriter(outFile))
                                    {
                                        binaryWriter.Write(ce.ModelFiles[filename]);
                                        outFile.Close();
                                    }
                            }
                        }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine($"Exception thrown {ex.Message}");
                }
            }
        }
Пример #3
0
        private static void generateCube3FromBFBFile(String FileName, String cube3FileName)
        {
            Encoding encoding = Encoding.ASCII;

            PaddedBufferedBlockCipher cipher;

            if (FileName != null || FileName.Length > 0)
            {
                try
                {
                    using (var inFile = File.OpenRead(FileName))
                        using (var streamReader = new StreamReader(inFile))
                        {
                            var inputBFBFile = streamReader.ReadToEnd();

                            try
                            {
                                ZeroBytePadding padding = new ZeroBytePadding();
                                cipher = new PaddedBufferedBlockCipher(engine, padding);

                                // create the key parameter
                                Byte[]       keyBytes = encoding.GetBytes(key);
                                KeyParameter param    = new KeyParameter(encoding.GetBytes(key));

                                // initalize the cipher
                                cipher.Init(true, new KeyParameter(keyBytes));

                                Byte[] newDataModel = encoding.GetBytes(inputBFBFile);

                                Byte[] encodedBytes = new Byte[cipher.GetOutputSize(newDataModel.Length)];

                                int encodedLength = cipher.ProcessBytes(newDataModel, 0, newDataModel.Length, encodedBytes, 0);
                                cipher.DoFinal(encodedBytes, encodedLength);

                                FileBackup.MakeBackup(cube3FileName, 5);

                                using (var outFile = File.OpenWrite(cube3FileName))
                                    using (var binaryWriter = new BinaryWriter(outFile))
                                    {
                                        binaryWriter.Write(encodedBytes);
                                        outFile.Close();
                                    }
                            }
                            catch (IOException)
                            {
                                Console.WriteLine($"Unable to process BFB File [{FileName}] or unable to open output file [{cube3FileName}]");
                                Environment.Exit(-1);
                            }
                        }
                }
                catch (SecurityException ex)
                {
                    Console.WriteLine($"Security error\n\nError message: {ex.Message}\n\n" +
                                      $"Details:\n\n{ex.StackTrace}");
                    Environment.Exit(ex.HResult);
                }
            }
            else
            {
                throw new SecurityException("No File specified!");
            }
        }