Exemple #1
0
 static void SortWriter(string Template, string Origin, int I1, UID[] UID, ref KKtIO writer)
 {
     for (byte i0 = 0; i0 < 10; i0++)
     {
         int i = int.Parse(Origin + i0.ToString());
         if (UID.Length > i)
         {
             if (UID[i].Category != "")
             {
                 writer.Write(Template + i + ".category=" + UID[i].Category + "\n");
             }
             if (UID[i].Size != -1)
             {
                 writer.Write(Template + i + ".size=" + UID[i].Size + "\n");
             }
             if (UID[i].Value != "")
             {
                 writer.Write(Template + i + ".value=" + UID[i].Value + "\n");
             }
         }
         else
         {
             break;
         }
         if (UID.Length > I1 && i != 0)
         {
             SortWriter(Template, i.ToString(), I1 * 10, UID, ref writer);
         }
     }
 }
Exemple #2
0
        void CompressStuff(int i, ref FARCFile[] Files, ref KKtIO writer)
        {
            Files[i].Offset = (int)writer.Position;
            if (Signature == Farc.FArc)
            {
                KKtIO reader = KKtIO.OpenReader(Files[i].Name);
                writer.Write(reader.ReadBytes((int)reader.Length));
                Files[i].SizeUnc = (int)reader.Length;
            }
            else
            {
                Files[i].Data = File.ReadAllBytes(Files[i].Name);
                if (Signature != Farc.FArc)
                {
                    MemoryStream stream = new MemoryStream();
                    using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
                        gZipStream.Write(Files[i].Data, 0, Files[i].Data.Length);
                    Files[i].SizeUnc  = Files[i].Data.Length;
                    Files[i].Data     = stream.ToArray();
                    Files[i].SizeComp = Files[i].Data.Length;
                }
                else if (Signature == Farc.FARC)
                {
                    int    AlignData = (int)Main.Align(Files[i].Data.Length, 0x40);
                    byte[] Data      = new byte[AlignData];
                    for (int i1 = 0; i1 < Files[i].Data.Length; i1++)
                    {
                        Data[i1] = Files[i].Data[i1];
                    }
                    for (int i1 = Files[i].Data.Length; i1 < AlignData; i1++)
                    {
                        Data[i1] = 0x78;
                    }

                    Files[i].Data = Encrypt(Data, false);
                }
                writer.Write(Files[i].Data);
                Files[i].Data = null;
            }
            if (Signature != Farc.FARC)
            {
                int Align = (int)Main.Align(writer.Position, 0x20) - (int)writer.Position;
                for (int i1 = 0; i1 < Align; i1++)
                {
                    if (Signature == Farc.FArc)
                    {
                        writer.Write((byte)0x00);
                    }
                    else
                    {
                        writer.Write((byte)0x78);
                    }
                }
            }
        }
Exemple #3
0
        public static void Encrypt(int I, string file)
        {
            KKtIO reader = KKtIO.OpenReader(file);

            if (reader.ReadInt64() == 0x454C494641564944)
            {
                reader.Close();
                Decrypt(I, file);
            }
            else
            {
                Console.Title = "PD_Tool: DIVAFILE Encryptor - File: " + Path.GetFileName(file);
                int FileLenghtOrigin = (int)reader.Length;
                int FileLenght       = (int)KKtMain.Align((long)FileLenghtOrigin, 16);
                reader.Close();
                byte[] In      = File.ReadAllBytes(file);
                byte[] Inalign = new byte[FileLenght];
                for (int i = 0; i < In.Length; i++)
                {
                    Inalign[i] = In[i];
                }
                In = null;
                byte[] encrypted = new byte[FileLenght];
                using (AesManaged crypto = new AesManaged())
                {
                    crypto.Key     = Key;
                    crypto.IV      = new byte[16];
                    crypto.Mode    = CipherMode.ECB;
                    crypto.Padding = PaddingMode.Zeros;
                    using (CryptoStream cryptoData = new CryptoStream(new MemoryStream(Inalign),
                                                                      crypto.CreateEncryptor(crypto.Key, crypto.IV), CryptoStreamMode.Read))
                        cryptoData.Read(encrypted, 0, FileLenght);
                }
                KKtIO writer = KKtIO.OpenWriter(file, Inalign.Length);
                writer.Write(0x454C494641564944);
                writer.Write(FileLenght);
                writer.Write(FileLenghtOrigin);
                writer.Write(encrypted);
                writer.Close();
            }
            Console.Title = "PD_Tool";
        }
Exemple #4
0
        public static void BINWriter(ref AUTH Data, string file)
        {
            DateTime date = DateTime.Now;

            KKtIO writer = KKtIO.OpenWriter(file + ".bin", true);

            writer.Write("#A3DA__________\n");
            writer.Write("#" + KKtMain.ToTitleCase(date.ToString("ddd")) + " " + KKtMain.
                         ToTitleCase(date.ToString("MMM")) + " " + date.ToString("dd HH:mm:ss yyyy") + "\n");

            if (Data.Category != null)
            {
                SortWriter("category.", "", 10, Data.Category, ref writer);
                writer.Write("category.length=" + Data.Category.Length.ToString() + "\n");
            }

            if (Data.UID != null)
            {
                SortWriter("uid.", "", 10, Data.UID, ref writer);
                writer.Write("uid.length=" + Data.UID.Length.ToString() + "\n");
            }

            writer.Close();
        }
Exemple #5
0
 static void SortWriter(string Template, string Origin, int I1, string[] Data, ref KKtIO writer)
 {
     for (byte i0 = 0; i0 < 10; i0++)
     {
         int i = int.Parse(Origin + i0.ToString());
         if (Data.Length > i)
         {
             writer.Write(Template + i + ".value=" + Data + "\n");
         }
         else
         {
             break;
         }
         if (Data.Length > I1 && i != 0)
         {
             SortWriter(Template, i.ToString(), I1 * 10, Data, ref writer);
         }
     }
 }
Exemple #6
0
        public static void Decrypt(int I, string file)
        {
            KKtIO reader = KKtIO.OpenReader(file);

            if (reader.ReadInt64() != 0x454C494641564944)
            {
                reader.Close();
                Encrypt(I, file);
            }
            else
            {
                Console.Title = "PD_Tool: DIVAFILE Decryptor - File: " + Path.GetFileName(file);
                reader.ReadUInt32();
                int    StreamLenght = (int)reader.Length;
                int    FileLenght   = reader.ReadInt32();
                byte[] decrypted    = new byte[StreamLenght];
                reader.Seek(0, 0);
                using (AesManaged crypto = new AesManaged())
                {
                    crypto.Key     = Key;
                    crypto.IV      = new byte[16];
                    crypto.Mode    = CipherMode.ECB;
                    crypto.Padding = PaddingMode.Zeros;
                    using (CryptoStream cryptoData = new CryptoStream(reader.BaseStream,
                                                                      crypto.CreateDecryptor(crypto.Key, crypto.IV), CryptoStreamMode.Read))
                        cryptoData.Read(decrypted, 0, StreamLenght);
                }
                KKtIO writer = KKtIO.OpenWriter(file, FileLenght);
                for (int i = 0x10; i < StreamLenght && i < FileLenght + 0x10; i++)
                {
                    writer.Write(decrypted[i]);
                }
                writer.Close();
            }
            Console.Title = "PD_Tool";
        }
Exemple #7
0
        public void DEXWriter()
        {
            KKtIO writer = KKtIO.OpenWriter(filepath + (Header.Format > KKtMain.Format.F ? ".dex" : ".bin"), true);

            writer.Format = Header.Format;

            if (writer.Format > KKtMain.Format.F)
            {
                Header.Lenght      = 0x20;
                Header.DataSize    = 0x00;
                Header.Signature   = 0x43505845;
                Header.SectionSize = 0x00;
                writer.Write(Header);
            }

            writer.Write(0x64);
            writer.Write(DEX.Length);
            if (Header.Format == KKtMain.Format.X)
            {
                writer.Write(0x28);
                writer.Write(0x00);
                writer.Write(0x18 + (int)KKtMain.Align(DEX.Length * 3 * 8, 0x10));
                writer.Write(0x00);
                writer.Seek(KKtMain.Align(DEX.Length * 3 * 8, 0x10) + 0x20 + Header.Lenght, 0);
            }
            else
            {
                writer.Write(0x20);
                writer.Write(0x10 + (int)KKtMain.Align(DEX.Length * 3 * 4, 0x10));
                writer.Seek(KKtMain.Align(DEX.Length * 3 * 4, 0x10) + 0x30 + Header.Lenght, 0);
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0].MainOffset = writer.Position - Header.Lenght;
                for (int i1 = 0; i1 < DEX[i0].Main.Count; i1++)
                {
                    writer.Write((float)DEX[i0].Main[i1].Frame);
                    writer.Write(DEX[i0].Main[i1].Both);
                    writer.Write(DEX[i0].Main[i1].ID);
                    writer.Write((float)DEX[i0].Main[i1].Value);
                    writer.Write((float)DEX[i0].Main[i1].Trans);
                }
                writer.Align(0x20, true);

                DEX[i0].EyesOffset = writer.Position - Header.Lenght;
                for (int i1 = 0; i1 < DEX[i0].Eyes.Count; i1++)
                {
                    writer.Write((float)DEX[i0].Eyes[i1].Frame);
                    writer.Write(DEX[i0].Eyes[i1].Both);
                    writer.Write(DEX[i0].Eyes[i1].ID);
                    writer.Write((float)DEX[i0].Eyes[i1].Value);
                    writer.Write((float)DEX[i0].Eyes[i1].Trans);
                }
                writer.Align(0x20, true);
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0].NameOffset = writer.Position - Header.Lenght;
                writer.Write(DEX[i0].Name + "\0");
            }
            writer.Align(0x10, true);

            if (Header.Format == KKtMain.Format.X)
            {
                writer.Seek(Header.Lenght + 0x28, 0);
            }
            else
            {
                writer.Seek(Header.Lenght + 0x20, 0);
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                writer.Write(DEX[i0].MainOffset);
                if (Header.Format == KKtMain.Format.X)
                {
                    writer.Write(0x00);
                }
                writer.Write(DEX[i0].EyesOffset);
                if (Header.Format == KKtMain.Format.X)
                {
                    writer.Write(0x00);
                }
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                writer.Write(DEX[i0].NameOffset);
                if (Header.Format == KKtMain.Format.X)
                {
                    writer.Write(0x00);
                }
            }

            if (writer.Format > KKtMain.Format.F)
            {
                Offset = writer.Length - Header.Lenght;
                writer.Seek(writer.Length, 0);
                writer.WriteEOFC(0);
                writer.Seek(0, 0);
                Header.DataSize    = Offset;
                Header.SectionSize = Offset;
                writer.Write(Header);
            }
            writer.Close();
        }
Exemple #8
0
        public void UnPack(string file, bool SaveToDisk)
        {
            Files         = null;
            Signature     = Farc.FArC;
            FT            = false;
            Console.Title = "PD_Tool: FARC Extractor - Archive: " + Path.GetFileName(file);
            if (File.Exists(file))
            {
                KKtIO  reader    = KKtIO.OpenReader(file);
                string directory = Path.GetFullPath(file).Replace(Path.GetExtension(file), "");
                Signature = (Farc)reader.ReadInt32Endian(true);
                if (Signature == Farc.FARC)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLenght = reader.ReadInt32Endian(true);
                    int Mode         = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();
                    bool GZip = (Mode & 2) == 2;
                    bool ECB  = (Mode & 4) == 4;

                    int FARCType = reader.ReadInt32Endian(true);
                    FT = FARCType == 0x10;
                    bool CBC = !FT && FARCType != 0x40;
                    if (ECB && CBC)
                    {
                        byte[] Header = new byte[HeaderLenght - 0x08];
                        FT = true;
                        reader.Close();
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(0x10, 0);

                        using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                            GetAes(true, null).CreateDecryptor(), CryptoStreamMode.Read))
                            cryptoStream.Read(Header, 0x00, HeaderLenght - 0x08);
                        Header = SkipData(Header, 0x10);
                        KKtIO CBCreader = new KKtIO(new MemoryStream(Header));
                        CBCreader.BaseStream.Seek(0, 0);

                        FARCType = CBCreader.ReadInt32Endian(true);
                        FT       = FARCType == 0x10;
                        if (CBCreader.ReadInt32Endian(true) == 1)
                        {
                            Files = new FARCFile[CBCreader.ReadInt32Endian(true)];
                        }
                        CBCreader.ReadUInt32();
                        HeaderReader(HeaderLenght, ref Files, ref CBCreader);
                        CBCreader.Close();
                    }
                    else
                    {
                        if (reader.ReadInt32Endian(true) == 1)
                        {
                            Files = new FARCFile[reader.ReadInt32Endian(true)];
                        }
                        reader.ReadUInt32();
                        HeaderReader(HeaderLenght, ref Files, ref reader);
                        reader.Close();
                    }

                    for (int i = 0; i < Files.Length; i++)
                    {
                        int FileSize = ECB || Files[i].ECB ? (int)Main.
                                       Align(Files[i].SizeComp, 0x10) : Files[i].SizeComp;
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[FileSize];

                        bool Encrypted = false;
                        if (ECB)
                        {
                            if ((FT && Files[i].ECB) || CBC)
                            {
                                using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                                    GetAes(true, null).CreateDecryptor(), CryptoStreamMode.Read))
                                    cryptoStream.Read(Files[i].Data, 0, FileSize);
                                Files[i].Data = SkipData(Files[i].Data, 0x10);
                            }
                            else
                            {
                                using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                                    GetAes(false, null).CreateDecryptor(), CryptoStreamMode.Read))
                                    cryptoStream.Read(Files[i].Data, 0, FileSize);
                            }
                            Encrypted = true;
                        }

                        bool Compressed = false;
                        bool LocalGZip  = (FT && Files[i].GZip) || GZip && Files[i].SizeUnc != 0;
                        if (LocalGZip)
                        {
                            GZipStream gZipStream;
                            if (Encrypted)
                            {
                                gZipStream = new GZipStream(new MemoryStream(
                                                                Files[i].Data), CompressionMode.Decompress);
                                stream.Close();
                            }
                            else
                            {
                                gZipStream = new GZipStream(stream, CompressionMode.Decompress);
                            }
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            gZipStream.Read(Files[i].Data, 0, Files[i].SizeUnc);

                            Compressed = true;
                        }

                        if (!Encrypted && !Compressed)
                        {
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            stream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                            stream.Close();
                        }

                        if (SaveToDisk)
                        {
                            KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                            writer.Write(Files[i].Data);
                            writer.Close();
                            Files[i].Data = null;
                        }
                    }
                }
                else if (Signature == Farc.FArC)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLength = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();
                    HeaderReader(HeaderLength, ref Files, ref reader);
                    reader.Close();

                    for (int i = 0; i < Files.Length; i++)
                    {
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[Files[i].SizeComp];
                        stream.Read(Files[i].Data, 0, Files[i].SizeComp);
                        stream.Close();

                        using (MemoryStream memorystream = new MemoryStream(Files[i].Data))
                        {
                            GZipStream gZipStream = new GZipStream(memorystream, CompressionMode.Decompress);
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            gZipStream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                        }

                        KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                        writer.Write(Files[i].Data);
                        writer.Close();
                        Files[i].Data = null;
                    }
                }
                else if (Signature == Farc.FArc)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLength = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();

                    HeaderReader(HeaderLength, ref Files, ref reader);
                    for (int i = 0; i < Files.Length; i++)
                    {
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[Files[i].SizeUnc];
                        stream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                        stream.Close();

                        KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                        writer.Write(Files[i].Data);
                        writer.Close();
                        Files[i].Data = null;
                    }
                }
                else
                {
                    Console.WriteLine("Unknown signature");
                    reader.Close();
                }
            }
            else
            {
                Console.WriteLine("File {0} doesn't exist.", Path.GetFileName(file));
            }
            Console.Clear();
            Console.Title = "PD_Tool: FARC Extractor";
        }
Exemple #9
0
        public void Pack(string file)
        {
            Files = null;
            FT    = false;
            string[] files = Directory.GetFiles(file);
            Files = new FARCFile[files.Length];

            for (int i = 0; i < files.Length; i++)
            {
                Files[i] = new FARCFile {
                    Name = files[i]
                };
                string ext = Path.GetExtension(files[i]).ToLower();
                if (ext == ".a3da" || ext == ".mot" || ext == ".vag")
                {
                    Signature = Farc.FArc;
                }
            }
            files = null;

            KKtIO writer = KKtIO.OpenWriter(file + ".farc", true);

            if (Signature == Farc.FArc)
            {
                writer.Write(Text.ToASCII("FArc"));
            }
            else if (Signature == Farc.FArC)
            {
                writer.Write(Text.ToASCII("FArC"));
            }
            else
            {
                writer.Write(Text.ToASCII("FARC"));
            }

            KKtIO HeaderWriter = new KKtIO(new MemoryStream());

            for (int i = 0; i < 3; i++)
            {
                HeaderWriter.Write((byte)0x00);
            }
            if (Signature == Farc.FArc)
            {
                HeaderWriter.Write((byte)0x20);
            }
            else if (Signature == Farc.FArC)
            {
                HeaderWriter.Write((byte)0x10);
            }
            else if (Signature == Farc.FARC)
            {
                HeaderWriter.Write((byte)0x06);
                for (int i = 0; i < 7; i++)
                {
                    HeaderWriter.Write((byte)0x00);
                }
                HeaderWriter.Write((byte)0x40);
                for (int i = 0; i < 8; i++)
                {
                    HeaderWriter.Write((byte)0x00);
                }
            }
            for (int i = 0; i < Files.Length; i++)
            {
                for (int i1 = 0; i1 < Path.GetFileName(Files[i].Name).Length +
                     (Signature == Farc.FArc ? 0x09 : 0x0D); i1++)
                {
                    HeaderWriter.Write((byte)0x00);
                }
            }
            writer.WriteEndian((uint)HeaderWriter.Length, true);
            writer.Write(HeaderWriter.ToArray());
            HeaderWriter = null;

            int Align = (int)Main.Align(writer.Position, 0x10) - (int)writer.Position;

            for (int i1 = 0; i1 < Align; i1++)
            {
                if (Signature == Farc.FArc)
                {
                    writer.Write((byte)0x00);
                }
                else
                {
                    writer.Write((byte)0x78);
                }
            }

            for (int i = 0; i < Files.Length; i++)
            {
                CompressStuff(i, ref Files, ref writer);
            }

            if (Signature == Farc.FARC)
            {
                writer.Seek(0x1C, 0);
            }
            else
            {
                writer.Seek(0x0C, 0);
            }
            for (int i = 0; i < Files.Length; i++)
            {
                writer.Write(Text.ToUTF8(Path.GetFileName(Files[i].Name) + "\0"));
                writer.WriteEndian(Files[i].Offset, true);
                if (Signature != Farc.FArc)
                {
                    writer.WriteEndian(Files[i].SizeComp, true);
                }
                writer.WriteEndian(Files[i].SizeUnc, true);
            }
            writer.Close();
        }
Exemple #10
0
        public void STRWriter()
        {
            uint  Offset        = 0;
            uint  CurrentOffset = 0;
            KKtIO writer        = KKtIO.OpenWriter(filepath + ((int)Header.Format > 5 ? ".str" : ".bin"), true);

            writer.Format = Header.Format;
            POF           = new KKtMain.POF {
                Offsets = new List <int>(), POFOffsets = new List <long>()
            };
            writer.IsBE = writer.Format == KKtMain.Format.F2BE;

            if ((int)writer.Format > 1)
            {
                writer.Seek(0x40, 0);
                writer.WriteEndian(Count);
                writer.GetOffset(ref POF).WriteEndian(0x80);
                writer.Seek(0x80, 0);
                for (int i = 0; i < STR.Count; i++)
                {
                    writer.GetOffset(ref POF).Write(0x00);
                    writer.WriteEndian(STR[i].ID);
                }
                writer.Align(16);
            }
            else
            {
                for (int i = 0; i < Count; i++)
                {
                    writer.Write(0x00);
                }
                writer.Align(32);
            }
            List <string> UsedSTR    = new List <string>();
            List <int>    UsedSTRPos = new List <int>();

            int[] STRPos = new int[Count];
            for (int i1 = 0; i1 < Count; i1++)
            {
                if (UsedSTR.Contains(STR[i1].Str))
                {
                    for (int i2 = 0; i2 < Count; i2++)
                    {
                        if (UsedSTR[i2] == STR[i1].Str)
                        {
                            STRPos[i1] = UsedSTRPos[i2];
                            break;
                        }
                    }
                }
                else
                {
                    STRPos[i1] = (int)writer.Position;
                    UsedSTRPos.Add(STRPos[i1]);
                    UsedSTR.Add(STR[i1].Str);
                    writer.Write(STR[i1].Str + "\0");
                }
            }
            if (writer.Format > KKtMain.Format.F)
            {
                writer.Align(16);
                Offset = (uint)writer.Position;
                writer.Seek(0x80, 0);
            }
            else
            {
                writer.Seek(0, 0);
            }
            for (int i1 = 0; i1 < Count; i1++)
            {
                writer.WriteEndian(STRPos[i1]);
                if (writer.Format > KKtMain.Format.F)
                {
                    writer.Seek(4, (SeekOrigin)1);
                }
            }

            if (writer.Format > KKtMain.Format.F)
            {
                writer.Seek(Offset, 0);
                writer.Write(ref POF, 1);
                CurrentOffset = (uint)writer.Length;
                writer.WriteEOFC(0);
                Header.IsBE        = writer.IsBE;
                Header.Lenght      = 0x40;
                Header.DataSize    = (int)(CurrentOffset - Header.Lenght);
                Header.Signature   = 0x41525453;
                Header.SectionSize = (int)(Offset - Header.Lenght);
                writer.Seek(0, 0);
                writer.Write(Header);
            }
            writer.Close();
        }