Beispiel #1
0
        public bool DoPack(string SourceRootDirPath, string DestFileName, ulong RndSeed, string Psw)
        {
            // File construction:
            // 1- random-seed (64 bit ulong)     bytes 0,1,2,3,4,5,6,7
            // 2- crc (32 bit uint)              bytes 8,9,10,11
            // 3- CIPHEROK (64 bit string)       bytes 12,13,14,15
            // 4- header-length (64 bit ulong)   bytes 16,17,18,19,20,21,22,23
            // 5- header-data (string)           bytes 24,25, ...
            // 6- body


            if (SourceRootDirPath.Trim() == "")
            {
                return(false);
            }
            uint crc = 0;

            Prompt(string.Format("Paking..."));
            Prompt("--------------------------------");

            List <string> Log = new List <string>();
            BinaryWriter  bw  = null;
            BinaryReader  br  = null;

            byte[] buff_read = new byte[1024 * 1024];

            string source_file = null;

            if (File.Exists(SourceRootDirPath))
            {
                source_file = SourceRootDirPath;
            }

            try
            {
                MyCypher mc = new MyCypher(Psw, RndSeed);


                KeyValPair kvp          = ProvideHeader(SourceRootDirPath);
                byte[]     bytes_header = ASCIIEncoding.ASCII.GetBytes(kvp.GetStr());


                bw = new BinaryWriter(new FileStream(DestFileName, FileMode.Create));
                byte[] bytes_rnd_seed = BitConverter.GetBytes(RndSeed);

                byte[] bytes_cipher_test   = ASCIIEncoding.ASCII.GetBytes("CIPHEROK");
                ulong  header_length       = (ulong)bytes_header.Length;
                byte[] bytes_header_length = BitConverter.GetBytes(header_length);


                // Random seed is written in plain-text (it is not ciphered)
                bw.Write(bytes_rnd_seed);
                crc ^= GetCrc(ref bytes_rnd_seed);
                //--------------------------------------------------


                // CRC is written in plain-text (it is not ciphered)
                bw.Write(crc);
                //--------------------------------------------------

                mc.Cipher(ref bytes_cipher_test);
                bw.Write(bytes_cipher_test);
                crc ^= GetCrc(ref bytes_cipher_test);
                //--------------------------------------------------


                mc.Cipher(ref bytes_header_length);
                bw.Write(bytes_header_length);
                crc ^= GetCrc(ref bytes_header_length);
                //--------------------------------------------------


                mc.Cipher(ref bytes_header);
                bw.Write(bytes_header);
                crc ^= GetCrc(ref bytes_header);
                //--------------------------------------------------

                int    file_count     = Int16.Parse(kvp.GetVal("F.CNT"));
                string file_full_name = "";

                for (int i = 0; i < file_count; i++)
                {
                    if (source_file != null)
                    {
                        file_full_name = source_file;
                    }
                    else
                    {
                        file_full_name = SourceRootDirPath + "\\" + kvp.GetVal(string.Format("F.{0}", i));
                    }

                    Prompt(string.Format("Packing File: {0}", file_full_name));

                    FileInfo fi = new FileInfo(file_full_name);
                    if (File.Exists(file_full_name) == false)
                    {
                        Log.Add(string.Format("'{0}' not found", file_full_name));
                        continue;
                    }
                    br = new BinaryReader(new FileStream(file_full_name, FileMode.Open));
                    int cnt = 0;
                    while (true)
                    {
                        cnt = br.Read(buff_read, 0, buff_read.Length);
                        if (cnt == 0)
                        {
                            break;
                        }
                        mc.Cipher(ref buff_read, cnt);
                        bw.Write(buff_read, 0, cnt);
                        crc ^= GetCrc(ref buff_read, cnt);
                    }
                    br.Close();
                }
                bw.Seek(8, SeekOrigin.Begin);
                bw.Write(crc);
                bw.Close();
                Prompt("--------------------------------");
                return(true);
            }
            catch (Exception ex)
            {
                if (bw != null)
                {
                    try
                    {
                        bw.Close();
                    }
                    catch { }
                }
                throw new Exception(string.Format("Unable to pack folder '{0}'.\n{1}", ex.Message));
            }
        }