Example #1
0
        public CngKey Build()
        {
            if(_theReader.First().Header == "PUBLIC KEY")
            {
                using(var ms = new MemoryStream(_theReader.First().Body))
                using(var br = new BinaryReader(ms))
                {
                    // should have a sequence
                    br.Require(Asn1Token.Sequence);
                    // ignore the length
                    br.ReadLengthField();

                    // should have an inner sequence
                    br.Require(Asn1Token.Sequence);
                    // ignore the length
                    br.ReadLengthField();

                    //should have an OID
                    br.Require(Asn1Token.Oid);
                    // get its length
                    int oidLength = br.ReadByte();
                    var oid = Asn1Token.GetOid(br.ReadBytes(oidLength));
                    var builder = GetBuilderFor(oid);
                    return builder.Build(br);
                }
            }
            throw new NotImplementedException();
        }
Example #2
0
        public Script(AssetManager manager, BinaryReader reader, string name)
            : base(manager, name)
        {
            long end = reader.BaseStream.Length;

            ExceptionEnd = "";

            reader.Require(0x01011388);
            CodeAddressA = reader.ReadUInt16();
            reader.Require((ushort)0x0101);
            CodeAddressB = reader.ReadUInt16();
            reader.Require((ushort)0x0101);
            CodeAddressC = reader.ReadUInt16();
            reader.Require((ushort)0x0101);
            CodeAddressD = reader.ReadUInt16();
            reader.Require((ushort)0x0101);
            CodeAddressE = reader.ReadUInt16();

            while (reader.BaseStream.Position < end) {
                try {
                    var instruction = ScriptInstruction.Read(this, reader);
                    InstructionsMutable.Add(instruction);
                } catch (Exception exception) {
                    ExceptionEnd = string.Format("\r\n{0:X}: Exception: {1}\r\nStopping\r\n", reader.BaseStream.Position, exception);
                    break;
                }
            }

            Link();
        }
        internal StringArchive(AssetManager manager, BinaryReader reader, string name, long length)
            : base(manager, name)
        {
            ByteOrder = ByteOrder.LittleEndian;

            Encoding = Encoding.Unicode;
            if (reader is BigEndianBinaryReader)
                Encoding = Encoding.BigEndianUnicode;

            using (reader) {
                reader.Require(Magic1);

                int totalFileLength = reader.ReadInt32();
                if (totalFileLength != length) {
                    if (totalFileLength.ReverseBytes() == length) {
                        ByteOrder = ByteOrder.BigEndian;
                        Encoding = Encoding.BigEndianUnicode;
                    } else
                        throw new InvalidDataException();
                }

                int magic2 = reader.ReadInt32();
                if (magic2 != Magic2 && magic2 != Magic2BE)
                    throw new InvalidDataException();

                int groupCount = reader.ReadInt32(ByteOrder);
                int stringCount = reader.ReadInt32(ByteOrder);
                int stringOffset = reader.ReadInt32(ByteOrder);
                reader.RequireZeroes(4 * 1);

                for (int index = 0; index < groupCount; index++)
                    groups.Add(new StringGroup(reader, ByteOrder));

                int[] stringOffsets = reader.ReadArrayInt32(stringCount, ByteOrder);

                for (int index = 0; index < stringCount; index++) {
                    int offset = stringOffsets[index];

                    if (offset == 0)
                        strings.Add(null);
                    else {
                        reader.BaseStream.Position = offset;
                        string value = reader.ReadStringz(Encoding);
                        strings.Add(value);
                    }
                }

                foreach (StringGroup group in groups) {
                    for (int index = 0; index < group.StringCount; index++) {
                        int stringIndex = group.StringsIndex + index;
                        string stringValue = strings[stringIndex];
                        int realIndex = group.IndexStart + index;

                        if (stringValue != null)
                            stringsById[realIndex] = stringValue;
                    }
                }
            }
        }
Example #4
0
        public FbxValue(BinaryReader reader)
            : this()
        {
            Type = (FbxValueType)reader.ReadByte();
            switch (Type) {
                case FbxValueType.Double:
                    ValueCombo.Double = reader.ReadDouble();
                    break;

                case FbxValueType.DoubleArray:
                    int doubleCount = reader.ReadInt32();
                    reader.RequireZeroes(4);
                    reader.Require(doubleCount * 8);
                    ValueObject = reader.ReadArrayDouble(doubleCount);
                    break;

                case FbxValueType.Int32:
                    ValueCombo.Int32 = reader.ReadInt32();
                    break;

                case FbxValueType.Int32Array:
                    int int32Count = reader.ReadInt32();
                    reader.RequireZeroes(4);
                    reader.Require(int32Count * 4);
                    ValueObject = reader.ReadArrayInt32(int32Count);
                    break;

                case FbxValueType.String:
                    int stringLength = reader.ReadInt32();
                    ValueObject = reader.ReadString(stringLength, Encoding.UTF8);
                    break;

                case FbxValueType.Data:
                    int dataLength = reader.ReadInt32();
                    ValueObject = reader.ReadBytes(dataLength);
                    break;

                default:
                    throw new NotImplementedException(string.Format("{0} {1:X2}h/'{2}' is not implemented.", typeof(FbxValueType).Name, (int)Type, (char)Type));
            }
        }
        internal DolphinCompressedDisc(BinaryReader reader)
        {
            Reader = reader;
            reader.Require(Magic);
            System = (DolphinCompressedDiscSystem)reader.ReadUInt32();
            CompressedDataSize = reader.ReadInt64();
            UncompressedDataSize = reader.ReadInt64();

            BlockSize = reader.ReadInt32();

            int blockCount = reader.ReadInt32();
            Blocks = new Block[blockCount];
            for (int index = 0; index < blockCount; index++)
                Blocks[index].OffsetCode = reader.ReadInt64();
            for (int index = 0; index < blockCount; index++)
                Blocks[index].Adler32HashOfCompressedData = reader.ReadUInt32();
            BlockDataOffset = reader.BaseStream.Position;
        }
Example #6
0
            public override CngKey Build(BinaryReader reader)
            {
                reader.Require(Asn1Token.Oid);
                var length = reader.ReadLengthField();
                var curve = Asn1Token.GetOid(reader.ReadBytes(length));
                if (!isSupportedCurve(curve))
                {
                throw new UnsupportedCurveException("Unsupported curve oid");
                }

                // We need to build a key blob structured as follows:
                //     BCRYPT_ECCKEY_BLOB   header
                //     byte[cbPublicExp]    publicExponent      - Exponent
                //     byte[cbModulus]      modulus             - Modulus
                //     -- Private only --
                //     byte[cbPrime1]       prime1              - P
                //     byte[cbPrime2]       prime2              - Q
                //

                // Where
                // typedef struct _BCRYPT_ECCKEY_BLOB {
                //  ULONG Magic; //BCRYPT_ECDSA_PUBLIC_P256_MAGIC =  0x31534345
                //  ULONG cbKey; // Key length in bytes
                //} B

                reader.Require(Asn1Token.BitString);
                var keyLength = reader.ReadLengthField() - 2;
                //ignore the zero byte
                reader.Require(0x00);

                // if this isn't an uncompressed curve, then panic
                reader.Require(0x04);

                var x = reader.ReadBytes(keyLength/2);
                var y = reader.ReadBytes(keyLength/2);

                return BuildEcKey(x, y, curve);
            }
Example #7
0
        public override CngKey Build(BinaryReader reader)
        {
            // skip NULL
            reader.Require(Asn1Token.Null);
            reader.ReadLengthField();

            reader.Require(Asn1Token.BitString);
            // length
            reader.ReadLengthField();
            // unused buts
            reader.Skip();

            reader.Require(Asn1Token.Sequence);
            reader.ReadLengthField();

            // Modulus
            reader.Require(Asn1Token.Integer);
            var modLength = reader.ReadLengthField();
            // if the first byte is zero, skip it.
            if (reader.Peek() == 0x00)
            {
                modLength--;
                reader.Skip();
            }
            var modulus = reader.ReadBytes(modLength);

            // Exponent
            reader.Require(Asn1Token.Integer);
            var expLength = reader.ReadLengthField();
            var exponent = reader.ReadBytes(expLength);

            var parameters = new RSAParameters()
            {
                Exponent = exponent,
                Modulus = modulus
            };
            var cng = new RSACng();
            cng.ImportParameters(parameters);

            return cng.Key;
        }
Example #8
0
        internal ArchiveRecord(Archive archive, int index, BinaryReader reader, int recordHeaderSize)
            : base(archive, "")
        {
            archive.records.Add(this);
            Index = index;

            switch (archive.Variant) {
                case ArchiveVariant.V3:
                    reader.Require(0x40);
                    Size = reader.ReadInt32();
                    Offset = reader.ReadInt32();
                    Id = reader.ReadInt32();
                    Name = reader.ReadStringzAtUInt32(Archive.ShiftJis);
                    int size2 = reader.ReadInt32();
                    if (Size != size2)
                        throw new Exception();
                    fixedUncompressedSize = Size;
                    break;

                case ArchiveVariant.V4:
                    Compression = reader.ReadInt32(); // 2 or 3 - compression?
                    reader.ReadMatch(-1);
                    Size = reader.ReadInt64();
                    if (recordHeaderSize == 0x1C) {
                        Offset = reader.ReadInt64();
                        Name = reader.ReadStringzAtUInt32(Archive.ShiftJis);
                    } else {
                        fixedUncompressedSize = reader.ReadInt64();
                        Offset = reader.ReadUInt32();
                        Unknowns.ReadInt32s(reader, 1);
                        // 'Compression' = 2, Unknown = 0, Encoding = UTF8
                        Name = reader.ReadStringzAtUInt32(Archive.ShiftJis);
                    }
                    Id = DarkSouls.Archive.HashFilename(Name);
                    break;

                case ArchiveVariant.V5:
                    Id = reader.ReadInt32();
                    Size = reader.ReadInt32();
                    Offset = reader.ReadInt32();
                    reader.RequireZeroes(4); // NameOffset, but zero?

                    string foundName;
                    if (!Archive.KnownFiles.TryGetValue(Id, out foundName))
                        Name = string.Format("#{0:X}", Id);
                    else
                        Name = foundName;
                    break;

                default:
                    throw new NotImplementedException();
            }

            MoveIntoPath(Name);
        }