Exemplo n.º 1
0
        static int Main(string[] args)
        {
            CRXExtractorOptions options = new CRXExtractorOptions();

            ParserResult <CRXExtractorOptions> result = Parser.Default
                                                        .ParseArguments <CRXExtractorOptions>(args)
                                                        .WithParsed(parsed => options = parsed);

            Console.WriteLine(options.FilePath);


            // Extract information from file
            CRXFile crx;

            try
            {
                crx = CRXFile.FromFile(options.FilePath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(1);
            }

            // Create output directory
            try
            {
                Directory.CreateDirectory(options.OutputPath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(1);
            }

            // Save all the files
            try
            {
                string currentPath = Path.Combine(options.OutputPath, "pubkey.key");
                File.WriteAllBytes(currentPath, crx.PubKey);
                currentPath = Path.Combine(options.OutputPath, "signature.sig");
                File.WriteAllBytes(currentPath, crx.Signature);
                currentPath = Path.Combine(options.OutputPath, "temp.zip");
                File.WriteAllBytes(currentPath, crx.ZipContents);
                string extractPath = Path.Combine(options.OutputPath, @"contents\");
                ZipFile.ExtractToDirectory(currentPath, extractPath);
                File.Delete(currentPath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(1);
            }

            return(0);
        }
Exemplo n.º 2
0
        public static CRXFile FromFile(string filePath)
        {
            CRXFile CRXData = new CRXFile();

            // Will throw exception if reading error
            using (BinaryReader br = new BinaryReader(File.Open(filePath, FileMode.Open, FileAccess.Read)))
            {
                // Read MagicNumber
                for (int i = 0; i < 4; i++)
                {
                    CRXData.MagicNumber[i] = br.ReadChar();
                }
                // Verify MagicNumber
                if (CRXData.MagicNumber[0] != 'C')
                {
                    CRXData.IsMagicNumberValid = false;
                }
                if (CRXData.MagicNumber[1] != 'r')
                {
                    CRXData.IsMagicNumberValid = false;
                }
                if (CRXData.MagicNumber[2] != '2')
                {
                    CRXData.IsMagicNumberValid = false;
                }
                if (CRXData.MagicNumber[3] != '4')
                {
                    CRXData.IsMagicNumberValid = false;
                }
                // Read version
                CRXData.Version = br.ReadUInt32();
                // Verify version
                if (CRXData.Version == 2)
                {
                    CRXData.IsVersionSupported = true;
                }
                // Read public key length
                CRXData.PubKeyLength = br.ReadUInt32();
                // Read signature length
                CRXData.SignatureLength = br.ReadUInt32();
                // Read public key
                CRXData.PubKey = new byte[CRXData.PubKeyLength];
                CRXData.PubKey = br.ReadBytes(Convert.ToInt32(CRXData.PubKeyLength));
                // Read signature
                CRXData.Signature = new byte[CRXData.PubKeyLength];
                CRXData.Signature = br.ReadBytes(Convert.ToInt32(CRXData.SignatureLength));
                // Read ZIP file
                long bytesLeft = br.BaseStream.Length - br.BaseStream.Position;
                CRXData.ZipContents = new byte[bytesLeft];
                CRXData.ZipContents = br.ReadBytes(Convert.ToInt32(bytesLeft));
            }
            return(CRXData);
        }