Esempio n. 1
0
        void LoadAndCompress()
        {
            compressedmem = new System.IO.MemoryStream();

            System.IO.FileInfo   fi         = new System.IO.FileInfo(filename);
            System.IO.FileStream filestream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            ComponentAce.Compression.Libs.zlib.ZOutputStream zstream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(compressedmem, ComponentAce.Compression.Libs.zlib.zlibConst.Z_DEFAULT_COMPRESSION);

            int totallen = (int)Size;
            int len      = buffer.Length;

            while (totallen > 0)
            {
                if (len > totallen)
                {
                    len = totallen;
                }
                filestream.Read(buffer, 0, len);
                zstream.Write(buffer, 0, len);
                totallen -= len;
            }
            zstream.finish();
            filestream.Close();

            compressedsize = (uint)compressedmem.Length;
            adler32        = AdlerCheckSum.GetAdler32(compressedmem, 0, (int)compressedmem.Length);
        }
Esempio n. 2
0
        public override void Convert(BlowfishNET.BlowfishECB ECB, AlgorithmType newalgorithm)
        {
            ecb          = ECB;
            oldalgorithm = algorithm;
            algorithm    = newalgorithm;

            if (compressedmem == null)
            {
                LoadAndCompress();
            }

            if (oldalgorithm == algorithm || compressedsize < BlowfishNET.BlowfishECB.BLOCK_SIZE)
            {
                return;
            }

            int encryptlen = (int)compressedsize;

            if (encryptlen % BlowfishNET.BlowfishECB.BLOCK_SIZE != 0)
            {
                encryptlen -= encryptlen % BlowfishNET.BlowfishECB.BLOCK_SIZE;
            }

            byte [] tmp = (byte [])compressedmem.GetBuffer().Clone();

            if (algorithm == AlgorithmType.infilateandblowfish)
            {
                ecb.Encrypt(tmp, 0, compressedmem.GetBuffer(), 0, encryptlen);
            }
            else
            {
                ecb.Decrypt(tmp, 0, compressedmem.GetBuffer(), 0, encryptlen);
            }
            adler32 = AdlerCheckSum.GetAdler32(compressedmem, 0, (int)compressedmem.Length);
        }
Esempio n. 3
0
        public static Kom2SubFileFromKom ReadSubFileFromOldKom(string komfilename, System.IO.BinaryReader reader, uint headersize)
        {
            Kom2SubFileFromKom subfile = new Kom2SubFileFromKom();

            subfile.size           = reader.ReadUInt32();
            subfile.compressedsize = reader.ReadUInt32();
            subfile.offset         = headersize + reader.ReadUInt32();
            subfile.adler32        = AdlerCheckSum.GetAdler32(komfilename, (int)subfile.offset, (int)subfile.compressedsize);

            subfile.filename = komfilename;
            return(subfile);
        }
Esempio n. 4
0
        public override void Convert(BlowfishNET.BlowfishECB ECB, AlgorithmType newalgorithm)
        {
            ecb       = ECB;
            algorithm = newalgorithm;

            if (oldalgorithm == algorithm || compressedsize < BlowfishNET.BlowfishECB.BLOCK_SIZE)
            {
                compressedmem = null;
                return;
            }

            int encryptlen = (int)compressedsize;

            compressedmem = new System.IO.MemoryStream();
            System.IO.FileStream filestream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            filestream.Position = offset;
            byte[] data = new byte[encryptlen];
            filestream.Read(data, 0, encryptlen);
            byte[] tmp = (byte[])data.Clone();

            if (encryptlen % BlowfishNET.BlowfishECB.BLOCK_SIZE != 0)
            {
                encryptlen -= encryptlen % BlowfishNET.BlowfishECB.BLOCK_SIZE;
            }

            if (algorithm == AlgorithmType.infilateandblowfish)
            {
                ecb.Encrypt(tmp, 0, data, 0, encryptlen);
            }
            else
            {
                ecb.Decrypt(tmp, 0, data, 0, encryptlen);
            }
            compressedmem.Write(data, 0, (int)compressedsize);
            adler32 = AdlerCheckSum.GetAdler32(compressedmem, 0, (int)compressedmem.Length);
            filestream.Close();
        }
Esempio n. 5
0
        public void Save(string filename)
        {
            string tmpfilename = filename + ".tmp";

            System.IO.FileStream   stream = new System.IO.FileStream(tmpfilename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);

            // 형식 정보 쓰자
            byte[] magicbyte   = new byte[52];
            string magicstring = string.Format("KOG GC TEAM MASSFILE V.{0}.{1}.", version / 10, version % 10);


            System.Text.ASCIIEncoding.ASCII.GetBytes(magicstring, 0, magicstring.Length, magicbyte, 0);
            writer.Write(magicbyte);

            filetime = 0;
            foreach (System.Collections.Generic.KeyValuePair <string, Kom2SubFile> KeyValue in subfiles)
            {
                filetime += KeyValue.Value.FileTime;
            }

            // 헤더는 xml 형식

            System.Xml.XmlDocument headerxml = new System.Xml.XmlDocument();
            headerxml.AppendChild(headerxml.CreateNode(System.Xml.XmlNodeType.XmlDeclaration, "", ""));

            System.Xml.XmlElement files = headerxml.CreateElement("Files");
            headerxml.AppendChild(files);

            foreach (System.Collections.Generic.KeyValuePair <string, Kom2SubFile> KeyValue in subfiles)
            {
                System.Xml.XmlElement file = headerxml.CreateElement("File");

                file.SetAttribute("Name", KeyValue.Key);
                KeyValue.Value.WriteHeader(file);
                files.AppendChild(file);
            }

            int header_raw_size = headerxml.InnerXml.Length;

            if (header_raw_size % BlowfishNET.BlowfishECB.BLOCK_SIZE != 0)
            {
                header_raw_size += BlowfishNET.BlowfishECB.BLOCK_SIZE - (header_raw_size % BlowfishNET.BlowfishECB.BLOCK_SIZE);
            }


            byte[] header_raw = new byte[header_raw_size];

            System.Text.ASCIIEncoding.ASCII.GetBytes(headerxml.InnerXml, 0, headerxml.InnerXml.Length, header_raw, 0);

            if (ecb != null)
            {
                byte[] header_encrypt = (byte [])header_raw.Clone();
                int    a = ecb.Encrypt(header_encrypt, 0, header_raw, 0, header_raw.Length);
            }

            writer.Write((UInt32)subfiles.Count);
            writer.Write((UInt32)1);
            writer.Write(FileTime);                                                         // 파일타임
            writer.Write(AdlerCheckSum.GetAdler32(header_raw, 0, (uint)header_raw.Length)); // 체크섬
            writer.Write((UInt32)header_raw.Length);                                        // 압축 안한 사이즈
            writer.Write(header_raw, 0, (int)header_raw.Length);                            // 헤더


            foreach (System.Collections.Generic.KeyValuePair <string, Kom2SubFile> KeyValue in subfiles)
            {
                KeyValue.Value.WriteCompressed(writer.BaseStream);
            }

            stream.Close();
            System.IO.File.Delete(filename);
            System.IO.File.Move(tmpfilename, filename);
        }