Пример #1
0
        public void TwoEquialsElementsTest()
        {
            var element    = "el2";
            var newElement = "new";
            var ht         = new HashTable.Hash(3);

            ht.PutPair(2, element);
            ht.PutPair(2, newElement);
            Assert.AreEqual(newElement, ht.GetValueByKey(2));
        }
Пример #2
0
        public void ThousandsElementsTest()
        {
            var size = 10000;
            var ht   = new HashTable.Hash(size);

            for (int i = 0; i < size; i++)
            {
                ht.PutPair(i, i + 1);
            }
            Assert.AreEqual(ht.GetValueByKey(15), 16);
        }
Пример #3
0
        public void AddingThreeElementsTest()
        {
            var key1 = "int";
            var key2 = 4;
            var key3 = 3;
            var ht   = new HashTable.Hash(3);

            ht.PutPair(key1, 2);
            ht.PutPair(key2, 34);
            ht.PutPair(key3, new[] { 0, 1, 2 });
            Assert.AreEqual(ht.GetValueByKey(key1), 2);
            Assert.AreEqual(ht.GetValueByKey(key2), 34);
            Assert.AreEqual(ht.GetValueByKey(key3), new[] { 0, 1, 2 });
        }
Пример #4
0
        public void FindingAlienKeys()
        {
            var size = 10000;
            var ht   = new HashTable.Hash(size);

            for (int i = 1; i < size; i++)
            {
                ht.PutPair(i, i + 1);
            }

            for (int i = size; i < 11000; i++)
            {
                Assert.AreEqual(ht.GetValueByKey(i), null);
            }
        }
Пример #5
0
        public void Save(Stream stream)
        {
            EndianReader writer = new EndianReader(stream, Endianness.BigEndian);

            Header.Save(writer);

            writer.Write(ContentID);
            writer.Write(EntryID);
            writer.Write((uint)ContentType);
            writer.Write(MetadataVersion);
            writer.Write(ContentSize);
            writer.Write(MediaID);
            writer.Write(Version);
            writer.Write(BaseVersion);
            writer.Write(TitleID);
            writer.Write(Platform);
            writer.Write(ExecutableType);
            writer.Write(DiscNumber);
            writer.Write(DiscInSet);
            writer.Write(SaveGameID);
            writer.Write(ConsoleID);
            writer.Write(ProfileID);
            Descriptor.Save(writer);
            writer.Write(DataFiles);
            writer.Write(DataFileSize);
            writer.Write(Reserved);
            writer.Write(Padding);
            writer.Write(DeviceID);
            for (int i = 0; i < DisplayName.Length; i++)
            {
                WriteString(writer, DisplayName[i]);
            }
            for (int i = 0; i < DisplayDescription.Length; i++)
            {
                WriteString(writer, DisplayDescription[i]);
            }
            WriteString(writer, Publisher);
            WriteString(writer, TitleName);
            writer.Write(TransferFlags);

            MemoryStream thumbnail = new MemoryStream();

            new Bitmap(Thumbnail, 64, 64).Save(thumbnail, ImageFormat.Png);
            MemoryStream titlethumbnail = new MemoryStream();

            new Bitmap(TitleThumbnail, 64, 64).Save(titlethumbnail, ImageFormat.Png);

            if (thumbnail.Length > 0x4000 || titlethumbnail.Length > 0x4000)
            {
                throw new FormatException();
            }

            writer.Write((uint)thumbnail.Length);
            writer.Write((uint)titlethumbnail.Length);

            writer.Position    = 0x171A;
            thumbnail.Position = 0;
            Util.StreamCopy(writer, thumbnail);
            thumbnail.Close();

            writer.Position         = 0x571A;
            titlethumbnail.Position = 0;
            Util.StreamCopy(writer, titlethumbnail);
            titlethumbnail.Close();

            BlockStream block = new BlockStream(this, stream);

            block.SeekBlock(Descriptor.FileTableBlock);
            EndianReader blockwriter = new EndianReader(block, Endianness.BigEndian);

            foreach (FileDescriptor file in Files)
            {
                file.Save(blockwriter);
            }
            FileDescriptor.Null.Save(blockwriter);

            foreach (FileDescriptor file in Files)
            {
                if ((file.Flags & 0x80) == 0)
                {
                    block.SeekBlock(file.Block);
                    file.Data.Position = 0;
                    Util.StreamCopy(block, file.Data, file.Size);
                }
            }

            block.Position = block.Length;
            blockwriter.PadToMultiple(BlockSize);

            uint      current      = 0;
            uint      blocks       = (uint)(block.Length / BlockSize);
            uint      subhashblock = 0;
            HashTable master       = new HashTable();

            while (current < blocks)
            {
                HashTable hashes = new HashTable();
                block.SeekBlock(current);
                hashes.HashBlocks(blockwriter, current, blocks - current);
                for (uint fileblock = 0; fileblock < 0xAA; fileblock++)
                {
                    HashTable.Hash hash = hashes.Hashes[fileblock];
                    if (hash == null)
                    {
                        continue;
                    }
                    foreach (FileDescriptor file in Files)
                    {
                        if ((file.Flags & 0x80) == 0)
                        {
                            if (fileblock + current == file.Block + file.BlockSize - 1)
                            {
                                hash.ID = 0x80FFFFFF;
                            }
                        }
                    }
                    if (fileblock + current == 0)
                    {
                        hash.ID = 0x80FFFFFF;                         // File table counts as a file
                    }
                }
                current += 0xAA;
                long position = BlockSize * 0xB;
                if (subhashblock > 0 && blocks > 0xAA)
                {
                    position = BlockSize * (0x0C + subhashblock * (0xB7 - 0x0C));
                }
                stream.Position = position;
                hashes.Save(writer);
                stream.Position = position;
                master.HashBlock(stream, (int)(subhashblock % 0xAA), 0);
                subhashblock++;
            }
            stream.Position = BlockSize * 0xB6;
            master.Save(writer);
            if (blocks > 0xAA)
            {
                stream.Position = 0xB6FF0;
                writer.Write(blocks);
            }
            stream.Position = 0x395;
            writer.Write(blocks);

            writer.Position = 0x34C;
            writer.Write((ulong)(stream.Length - 0xB * BlockSize));

            stream.Position = blocks > 0xAA ? (BlockSize * 0xB6) : (BlockSize * 0x0B);
            byte[] sha1 = Util.SHA1Hash(stream, BlockSize);
            writer.Position = 0x381;
            writer.Write(sha1);

            stream.Position = 0x344;
            sha1            = Util.SHA1Hash(stream, 0xACBC);
            writer.Position = 0x32C;
            writer.Write(sha1);

            stream.Position = 0x22C;
            sha1            = Util.SHA1Hash(stream, 0x118);
            writer.Position = 4;
            RSACryptoServiceProvider   rsa       = new RSACryptoServiceProvider();
            RSAPKCS1SignatureFormatter rsaformat = new RSAPKCS1SignatureFormatter();
            RSAParameters rsaparams = new RSAParameters();
            EndianReader  reader    = new EndianReader(new MemoryStream(Properties.Resources.XK4), Endianness.LittleEndian);

            rsaparams.Exponent = new byte[] { 0, 0, 0, 3 };
            rsaparams.D        = Properties.Resources.XK3;
            reader.Position    = 0;
            rsaparams.Modulus  = reader.ReadBytes(0x100);
            rsaparams.P        = reader.ReadBytes(0x80);
            rsaparams.Q        = reader.ReadBytes(0x80);
            rsaparams.DP       = reader.ReadBytes(0x80);
            rsaparams.DQ       = reader.ReadBytes(0x80);
            rsaparams.InverseQ = reader.ReadBytes(0x80);
            rsa.ImportParameters(rsaparams);
            rsaformat.SetHashAlgorithm("SHA1");
            rsaformat.SetKey(rsa);
            byte[] signature = rsaformat.CreateSignature(sha1);
            reader.Base.Close();
            Array.Reverse(signature);
            writer.Write(signature);
            writer.Pad(0x128);
        }