Пример #1
0
        public static byte[] CompressionZlib(BinaryStream stream, CompressionLevel compressionLevel,
            bool oldStreamClose = false)
        {
            using (MemoryStream compressed = new MemoryStream())
            {
                compressed.WriteByte(ZLIB_HEADER);
                compressed.WriteByte(GetCompressionLevelByte(compressionLevel));

                int sum;
                using (ZlibStream zlib = new ZlibStream(compressed, compressionLevel, true))
                {
                    byte[] buffer = stream.ToArray();
                    zlib.Write(buffer, 0, buffer.Length);
                    sum = zlib.Checksum;
                }

                if (oldStreamClose)
                    stream.Close();

                byte[] sumBytes = BitConverter.GetBytes(sum);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(sumBytes);
                }

                compressed.Write(sumBytes, 0, sumBytes.Length);

                return compressed.ToArray();
            }
        }
Пример #2
0
        private void Init()
        {
            Guid         guid = System.Guid.NewGuid();
            BinaryStream bs   = new BinaryStream(guid.ToByteArray());

            Guid   = bs.ReadLong();
            PongId = bs.ReadLong();
            bs.Close();
        }
Пример #3
0
        public void TestControlCodedStream()
        {
            byte[] buffer;
            using (var memoryStream = new MemoryStream())
                using (var peekable = new PeekableStream(memoryStream))
                    using (var ccs = new ControlCodedStream(peekable))
                        using (var binary = new BinaryStream(ccs))
                        {
                            binary.WriteString("← ↔ →");
                            ccs.WriteControlCode(47);
                            binary.WriteChar((char)255);
                            ccs.WriteControlCode(48);
                            binary.WriteVarInt(0x1FF);
                            ccs.WriteControlCode(49);
                            binary.WriteFloat(float.NaN);
                            binary.WriteFloat(float.PositiveInfinity);
                            binary.WriteFloat(float.NegativeInfinity);
                            ccs.WriteControlCode(50);

                            Assert.Throws <ArgumentOutOfRangeException>(() => ccs.WriteControlCode(255));

                            binary.Close();
                            ccs.Close();
                            peekable.Close();
                            memoryStream.Close();
                            buffer = memoryStream.ToArray();
                        }

            for (int i = 1; i < buffer.Length; i++)
            {
                using (var memoryStream = new MemoryStream(buffer))
                    using (var slowStream = new SlowStream(memoryStream, i))
                        using (var peekable = new PeekableStream(slowStream))
                            using (var ccs = new ControlCodedStream(peekable))
                                using (var binary = new BinaryStream(ccs))
                                {
                                    Assert.AreEqual("← ↔ →", binary.ReadString());
                                    Assert.Throws <InvalidOperationException>(() => ccs.Read(new byte[1], 0, 1));
                                    Assert.AreEqual(47, ccs.ReadControlCode());
                                    Assert.AreEqual((char)255, binary.ReadChar());
                                    Assert.AreEqual(48, ccs.ReadControlCode());
                                    Assert.AreEqual(0x1FF, binary.ReadVarInt());
                                    Assert.AreEqual(49, ccs.ReadControlCode());
                                    Assert.IsNaN(binary.ReadFloat());
                                    Assert.AreEqual(-1, ccs.ReadControlCode());
                                    Assert.AreEqual(float.PositiveInfinity, binary.ReadFloat());
                                    Assert.AreEqual(float.NegativeInfinity, binary.ReadFloat());
                                    Assert.AreEqual(50, ccs.ReadControlCode());
                                    Assert.AreEqual(-1, ccs.ReadControlCode());
                                    Assert.AreEqual(0, ccs.Read(new byte[1], 0, 1));
                                }
            }
        }
Пример #4
0
        private static void ReceiveClient(ReceiveCustomDataPacketData data)
        {
            byte[] buf = data.CustomDataPacket.Payload;
            byte   b   = buf[0];

            if (b == 1)
            {
                BinaryStream stream = new BinaryStream(buf);
                stream.ReadByte();
                Console.WriteLine(stream.ReadStringUtf8());
                stream.Close();
            }
        }
Пример #5
0
        public static byte[] DecompressGZIP(BinaryStream stream, bool oldStreamClose)
        {
            using (GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress, false))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    gzip.CopyTo(ms);

                    if (oldStreamClose)
                        stream.Close();

                    return ms.ToArray();
                }
            }
        }
Пример #6
0
        public static byte[] CompressGZIP(BinaryStream stream, CompressionLevel compressionLevel,
            bool oldStreamClose)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream gzip = new GZipStream(ms, compressionLevel, false))
                {
                    byte[] buffer = stream.ToArray();
                    gzip.Write(buffer, 0, buffer.Length);
                }

                if (oldStreamClose)
                    stream.Close();

                return ms.ToArray();
            }
        }
Пример #7
0
        public static byte[] DecompressionZlib(BinaryStream stream, bool oldStreamClose = false)
        {
            byte header = stream.ReadByte();
            if (header != ZLIB_HEADER)
            {
                throw new FormatException(nameof(header));
            }

            stream.ReadByte(); // Compression Level

            using (ZlibStream zlib =
                new ZlibStream(stream, CompressionMode.Decompress, false))
            {
                using (MemoryStream dc = new MemoryStream())
                {
                    zlib.CopyTo(dc);

                    if (oldStreamClose)
                        stream.Close();

                    return dc.ToArray();
                }
            }
        }
Пример #8
0
 public void Dispose()
 {
     stream.Close();
     binaryReader.Close();
     binaryWriter.Close();
 }
Пример #9
0
        private static void CreateFireEmblemArchive(string outdir, string newname)
        {
            Console.WriteLine("Creating archive {0}", Path.GetFileName(newname));
            FileStream newfilestream = File.Create(newname);

            //Let's get the number of files
            string[] files = Directory.GetFiles(outdir);

            uint FileCount = (uint)files.Length;

            Console.WriteLine("{0} files detected!", FileCount);

            var ShiftJIS = Encoding.GetEncoding(932);

            BinaryStream newFile = new BinaryStream(newfilestream);

            MemoryStream infos     = new MemoryStream();
            BinaryWriter FileInfos = new BinaryWriter(infos);


            Console.WriteLine("Creating dummy header...");
            newFile.Write(0);//Dummy; file size

            //MetaOffset 0x4
            newFile.Write(0);//dummy should be MetaOffset
            newFile.Write(FileCount);
            newFile.Write(FileCount + 3);

            byte nil = 0;

            for (int i = 0; i < 0x70; i++)
            {
                newFile.Write(nil);
            }
            int z = 0;

            foreach (string fileName in files)
            {
                Console.WriteLine("Adding file {0}...", Path.GetFileName(fileName));
                byte[] filetoadd = File.ReadAllBytes(fileName);
                uint   fileoff   = (uint)newFile.Tell();
                newFile.Write(filetoadd);
                while ((int)newFile.Tell() % 128 != 0)
                {
                    newFile.Write(nil);
                }
                FileInfos.Write(0);                //Name position
                FileInfos.Write(z);                //FileIndex
                FileInfos.Write(filetoadd.Length); //Length of the file
                FileInfos.Write(fileoff - 0x80);   //Data Offset - 0x80
                z++;
            }

            long countinfo = newFile.Tell();

            newFile.Write(files.Length);//Count is written there afaik
            long infopointer = newFile.Tell();

            Console.WriteLine("Adding dummy FileInfos...");

            infos.Seek(0, SeekOrigin.Begin);
            var infopos = newFile.Tell();

            newFile.Write(infos.ToArray());

            Console.WriteLine("Rewriting header...");
            long metapos = newFile.Tell();

            newFile.Seek(4, SeekOrigin.Begin);
            newFile.Write((uint)metapos - 0x20);

            newFile.Seek(metapos, SeekOrigin.Begin);

            Console.WriteLine("Adding FileInfos pointer...");
            for (int i = 0; i < files.Length; i++)
            {
                newFile.Write((uint)((infopointer + i * 16) - 0x20));
            }

            Console.WriteLine("Adding Advanced pointers...");

            newFile.Write((uint)0x60);
            newFile.Write(0);
            newFile.Write((uint)(countinfo - 0x20));
            newFile.Write((uint)5);
            newFile.Write((uint)(countinfo + 4 - 0x20));
            newFile.Write((uint)0xB);
            for (int i = 0; i < files.Length; i++)
            {
                newFile.Write((uint)((countinfo + 4) + i * 16) - 0x20);

                //Second pointer is a bit more complicated
                if (i == 0)
                {
                    newFile.Write((uint)0x10);
                }
                else
                {
                    if (i == 1)
                    {
                        newFile.Write((uint)0x1C);
                    }
                    else
                    {
                        newFile.Write((uint)(0x1C + (10 * (i - 1))));//Currently this pointer is unknown, so we assume blindly that a basic pattern is correct
                    }
                }
            }

            //This need to be reversed!
            //0, 5, 0B, 10, 1C, 26, 30, 3A, 44, 4E
            //+5, +6, +4, +12, +10, +10, +10, +10, +10

            Console.WriteLine("Adding Filenames...");
            var datcount = new byte[] { 0x44, 0x61, 0x74, 0x61, 0x00, 0x43, 0x6F, 0x75, 0x6E, 0x74, 0x00, 0x49, 0x6E, 0x66, 0x6F, 0x00 };

            newFile.Write(datcount);
            int y = 0;

            foreach (string fileName in files)
            {
                FileInfos.Seek(y * 16, SeekOrigin.Begin);
                long namepos = newFile.Tell();
                FileInfos.Write((uint)namepos - 0x20);
                newFile.Write(ShiftJIS.GetBytes(Path.GetFileName(fileName)));
                newFile.Write(nil);
                y++;
            }
            Console.WriteLine("Rewriting FileInfos...");
            newFile.Seek(infopos, SeekOrigin.Begin);

            infos.Seek(0, SeekOrigin.Begin);
            newFile.Write(infos.ToArray());

            Console.WriteLine("Finishing the job...");
            newFile.Seek(0, SeekOrigin.Begin);
            UInt32 newlength = (UInt32)newFile.BaseStream.Length;

            newFile.Write(newlength);

            Console.WriteLine("Done!");
            newFile.Close();
        }
Пример #10
0
        private void AddPatch(Stream ms, string patchname = "")
        {
            using (var br = new BinaryStream(ms, Encoding.ASCII, leaveOpen: true))
            {
                if (br.ReadUInt32() != 0x504D4352)
                {
                    br.Close();
                    ms.Close();
                    throw new InvalidDataException("Invalid RCMPatch file!");
                }
                uint oaAuther    = br.ReadUInt32(),
                     obFileCount = br.ReadUInt32(),
                     num         = br.ReadUInt32();
                patchname = Path.GetFileName(patchname);
                try
                {
                    string author = br.ReadCString();
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Loading patch {0} version {1} by {2}", patchname, num, author);
                    Console.ResetColor();
                    br.Seek(oaAuther, SeekOrigin.Begin);
                    uint os1 = br.ReadUInt32(),
                         os2 = br.ReadUInt32(),
                         os3 = br.ReadUInt32();
                    br.Seek(oaAuther + os1, SeekOrigin.Begin);
                    num = br.ReadUInt32();
                    if (num > 0)
                    {
                        br.Seek(num * 4, SeekOrigin.Current);
                        Console.WriteLine("Changelog:");
                        Console.ForegroundColor = ConsoleColor.Green;
                        while (num > 0)
                        {
                            --num;
                            Console.WriteLine(" * {0}", br.ReadCString());
                        }
                    }
                    br.Seek(oaAuther + os2, SeekOrigin.Begin);
                    num = br.ReadUInt32();
                    if (num > 0)
                    {
                        br.Seek(num * 4, SeekOrigin.Current);
                        Console.ResetColor();
                        Console.WriteLine("Credits:");
                        Console.ForegroundColor = ConsoleColor.Green;
                        while (num > 0)
                        {
                            --num;
                            Console.WriteLine(" * {0}", br.ReadCString());
                        }
                        Console.ResetColor();
                    }
                    br.Seek(oaAuther + os3, SeekOrigin.Begin);
                    author = br.ReadCString();
                    if (author.Length != 0)
                    {
                        Console.WriteLine("Other information:\r\n");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("{0}", author);
                    }
                    Console.ResetColor();
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error reading rcmpatch header: {0}: {1}\r\nAttempting to continue files...",
                                      e.GetType(), e.Message);
                    Console.ResetColor();
                }
                Console.WriteLine("");
                br.Seek(obFileCount, SeekOrigin.Begin);
                num = br.ReadUInt32();
                while (num > 0)
                {
                    --num;
                    var nPatch = new Patch();
                    nPatch.NumberParent = br.ReadUInt32();
                    if (nPatch.NumberParent == 2)
                    {
                        nPatch.Parent1Size = br.ReadUInt32();
                        nPatch.Parent1     = System.Text.Encoding.UTF8.GetString(br.ReadBytes((int)nPatch.Parent1Size)).TrimEnd('\0');
                        RECOM_Toolkit.Program.Parent1LIST.Add(nPatch.Parent1);
                        nPatch.Parent2Size = br.ReadUInt32();
                        nPatch.Parent2     = System.Text.Encoding.UTF8.GetString(br.ReadBytes((int)nPatch.Parent2Size)).TrimEnd('\0');
                        RECOM_Toolkit.Program.Parent2LIST.Add(nPatch.Parent2);
                    }
                    else
                    {
                        if (nPatch.NumberParent == 1)
                        {
                            nPatch.Parent1Size = br.ReadUInt32();
                            nPatch.Parent1     = System.Text.Encoding.UTF8.GetString(br.ReadBytes((int)nPatch.Parent1Size)).TrimEnd('\0');
                            RECOM_Toolkit.Program.Parent1LIST.Add(nPatch.Parent1);
                        }
                    }
                    nPatch.nameSize   = br.ReadUInt32();
                    nPatch.name       = System.Text.Encoding.UTF8.GetString(br.ReadBytes((int)nPatch.nameSize)).TrimEnd('\0');
                    nPatch.Compressed = Convert.ToBoolean(br.ReadUInt32());
                    br.ReadUInt32();
                    br.ReadUInt32();
                    br.ReadUInt32();
                    br.ReadUInt32();
                    br.ReadUInt32();//Reserved
                    nPatch.Size  = (long)br.ReadUInt32();
                    nPatch.CSize = (long)br.ReadUInt32();
                    if (nPatch.Size != 0)
                    {
                        nPatch.Stream = new Substream(ms, br.Tell(), nPatch.Size);
                        nPatch.Data   = RECOM_Toolkit.Program.StreamToByteArray(nPatch.Stream);
#if EXTRACTPATCH
                        FileStream iso = File.Open("@out/" + nPatch.Parent2 + nPatch.name, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                        var        br2 = new BinaryStream(iso, Encoding.ASCII, leaveOpen: true);
                        br2.Write(nPatch.Data);
#endif
                        nPatch.Stream.Dispose();
                    }
                    else
                    {
                        throw new InvalidDataException("File length is 0!");
                    }
                    // Use the last file patch
                    if (patches.ContainsKey(nPatch.name))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        patches[nPatch.name].Dispose();
                        patches.Remove(nPatch.name);
                        Console.ResetColor();
                    }
                    patches.Add(nPatch.name, nPatch);
                }
            }
        }
Пример #11
0
        private void AddPatch(Stream ms, string patchname = "")
        {
            using (var br = new BinaryStream(ms, Encoding.ASCII, leaveOpen: true))
            {
                if (br.ReadUInt32() != 0x5032484b)
                {
                    br.Seek(0, SeekOrigin.Begin);
                    if (br.ReadUInt32() != 0x5132484b)
                    {
                        br.Seek(0, SeekOrigin.Begin);
                        if (br.ReadUInt32() != 0x4632484b)
                        {
                            br.Close();
                            ms.Close();
                            throw new InvalidDataException("Invalid KH2Patch file!");
                        }
                        else
                        {
                            fast_patch = true;
                            Console.WriteLine("Fast patch and dev flags detected! You might get some issues with those!");
                        }
                    }
                }
                patchms.Add(ms);
                uint oaAuther    = br.ReadUInt32(),
                     obFileCount = br.ReadUInt32(),
                     num         = br.ReadUInt32();
                patchname = Path.GetFileName(patchname);
                try
                {
                    string author = br.ReadCString();
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Loading patch {0} version {1} by {2}", patchname, num, author);
                    Console.ResetColor();
                    br.Seek(oaAuther, SeekOrigin.Begin);
                    uint os1 = br.ReadUInt32(),
                         os2 = br.ReadUInt32(),
                         os3 = br.ReadUInt32();
                    br.Seek(oaAuther + os1, SeekOrigin.Begin);
                    num = br.ReadUInt32();
                    if (num > 0)
                    {
                        br.Seek(num * 4, SeekOrigin.Current);
                        Console.WriteLine("Changelog:");
                        Console.ForegroundColor = ConsoleColor.Green;
                        while (num > 0)
                        {
                            --num;
                            Console.WriteLine(" * {0}", br.ReadCString());
                        }
                    }
                    br.Seek(oaAuther + os2, SeekOrigin.Begin);
                    num = br.ReadUInt32();
                    if (num > 0)
                    {
                        br.Seek(num * 4, SeekOrigin.Current);
                        Console.ResetColor();
                        Console.WriteLine("Credits:");
                        Console.ForegroundColor = ConsoleColor.Green;
                        while (num > 0)
                        {
                            --num;
                            Console.WriteLine(" * {0}", br.ReadCString());
                        }
                        Console.ResetColor();
                    }
                    br.Seek(oaAuther + os3, SeekOrigin.Begin);
                    author = br.ReadCString();
                    if (author.Length != 0)
                    {
                        Console.WriteLine("Other information:\r\n");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("{0}", author);
                    }
                    Console.ResetColor();
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error reading kh2patch header: {0}: {1}\r\nAttempting to continue files...",
                                      e.GetType(), e.Message);
                    Console.ResetColor();
                }
                Console.WriteLine("");
                br.Seek(obFileCount, SeekOrigin.Begin);
                num = br.ReadUInt32();
                while (num > 0)
                {
                    --num;
                    var nPatch = new Patch();
                    nPatch.Hash             = br.ReadUInt32();
                    oaAuther                = br.ReadUInt32();
                    nPatch.CompressedSize   = br.ReadUInt32();
                    nPatch.UncompressedSize = br.ReadUInt32();
                    nPatch.Parent           = br.ReadUInt32();
                    nPatch.Relink           = br.ReadUInt32();
                    nPatch.Compressed       = br.ReadUInt32() != 0;
                    nPatch.IsNew            = br.ReadUInt32() == 1; //Custom
                    if (!nPatch.IsRelink)
                    {
                        if (nPatch.CompressedSize != 0)
                        {
                            nPatch.Stream = new Substream(ms, oaAuther, nPatch.CompressedSize);
                        }
                        else
                        {
                            throw new InvalidDataException("File length is 0, but not relinking.");
                        }
                    }
                    // Use the last file patch
                    if (patches.ContainsKey(nPatch.Hash))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
#if DEBUG
                        Console.WriteLine("The file {0} has been included multiple times. Using the one from {1}.",
                                          HashList.HashList.NameFromHash(nPatch.Hash), patchname);
#endif
                        patches[nPatch.Hash].Dispose();
                        patches.Remove(nPatch.Hash);
                        Console.ResetColor();
                    }
                    patches.Add(nPatch.Hash, nPatch);
                    //Global checks
                    if (!KH2Changed && nPatch.IsInKH2 || nPatch.IsInKH2Sub)
                    {
                        KH2Changed = true;
                    }
                    else if (!OVLChanged && nPatch.IsInOVL)
                    {
                        OVLChanged = true;
                    }
                    else if (!ISOChanged && nPatch.IsinISO)
                    {
                        ISOChanged = true;
                    }
                    if (nPatch.IsNew)
                    {
                        AddToNewFiles(nPatch);
                    }
                    br.Seek(60, SeekOrigin.Current);
                }
            }
        }