Example #1
0
        ResourceElement Convert(ResourceInfo info)
        {
            var reader = info.dataReader;
            reader.Position = info.offset;

            IResourceData resourceData;
            int type = info.flags & 0x7F;
            switch (type) {
            case 1:		// bool
                resourceData = dataCreator.Create(reader.ReadBoolean());
                break;

            case 2:		// byte
                resourceData = dataCreator.Create(reader.ReadByte());
                break;

            case 3:		// byte[]
                resourceData = dataCreator.Create(reader.ReadBytes(info.length));
                break;

            case 4:		// char[]
                resourceData = new CharArrayResourceData(dataCreator.CreateUserResourceType(CharArrayResourceData.ReflectionTypeName), reader.ReadChars(info.length));
                break;

            case 5:		// sbyte
                resourceData = dataCreator.Create(reader.ReadSByte());
                break;

            case 6:		// char
                resourceData = dataCreator.Create(reader.ReadChar());
                break;

            case 7:		// decimal
                resourceData = dataCreator.Create(reader.ReadDecimal());
                break;

            case 8:		// double
                resourceData = dataCreator.Create(reader.ReadDouble());
                break;

            case 9:		// short
                resourceData = dataCreator.Create(reader.ReadInt16());
                break;

            case 10:	// int
                resourceData = dataCreator.Create(reader.ReadInt32());
                break;

            case 11:	// long
                resourceData = dataCreator.Create(reader.ReadInt64());
                break;

            case 12:	// float
                resourceData = dataCreator.Create(reader.ReadSingle());
                break;

            case 13:	// string
                resourceData = dataCreator.Create(reader.ReadString());
                break;

            case 14:	// ushort
                resourceData = dataCreator.Create(reader.ReadUInt16());
                break;

            case 15:	// uint
                resourceData = dataCreator.Create(reader.ReadUInt32());
                break;

            case 16:	// ulong
                resourceData = dataCreator.Create(reader.ReadUInt64());
                break;

            case 17:	// DateTime
                resourceData = dataCreator.Create(DateTime.FromBinary(reader.ReadInt64()));
                break;

            case 18:	// TimeSpan
                resourceData = dataCreator.Create(TimeSpan.FromTicks(reader.ReadInt64()));
                break;

            case 19:	// Icon
                resourceData = new IconResourceData(dataCreator.CreateUserResourceType(IconResourceData.ReflectionTypeName), reader.ReadBytes(info.length));
                break;

            case 20:	// Image
                resourceData = new ImageResourceData(dataCreator.CreateUserResourceType(ImageResourceData.ReflectionTypeName), reader.ReadBytes(info.length));
                break;

            case 31:	// binary
                resourceData = dataCreator.CreateSerialized(reader.ReadBytes(info.length));
                break;

            case 21:	// Point (CV doesn't restore this type)
            default:
                throw new Exception("Unknown type");
            }

            return new ResourceElement() {
                Name = info.name,
                ResourceData = resourceData,
            };
        }
Example #2
0
        public ResourceInfo[] read()
        {
            if (reader.ReadUInt32() != 0xBEEFCACE)
                throw new InvalidDataException("Invalid magic");
            if (reader.ReadUInt32() <= 0)
                throw new InvalidDataException("Invalid number");
            reader.ReadUInt32();
            resourceReader = reader.ReadString();
            if (Utils.StartsWith(resourceReader, "System.Resources.ResourceReader", StringComparison.Ordinal))
                throw new InvalidDataException("Resource isn't encrypted");
            resourceSet = reader.ReadString();
            if (reader.ReadByte() != 1)
                throw new ApplicationException("Invalid version");

            int flags = reader.ReadByte();
            if ((flags & 0xFC) != 0)
                throw new ApplicationException("Invalid flags");
            bool inflateData = (flags & 1) != 0;
            bool encrypted = (flags & 2) != 0;

            int numResources = reader.ReadInt32();
            if (numResources < 0)
                throw new ApplicationException("Invalid number of resources");

            var infos = new ResourceInfo[numResources];
            for (int i = 0; i < numResources; i++) {
                var resourceName = readResourceName(reader, encrypted);
                int offset = reader.ReadInt32();
                byte resourceFlags = reader.ReadByte();
                int resourceLength = (resourceFlags & 0x80) == 0 ? -1 : reader.ReadInt32();
                infos[i] = new ResourceInfo(resourceName, resourceFlags, offset, resourceLength);
            }

            var dataReader = reader;
            if (encrypted) {
                var key = new uint[4];
                key[0] = dataReader.ReadUInt32();
                key[1] = dataReader.ReadUInt32();
                int numDwords = dataReader.ReadInt32();
                if (numDwords < 0 || numDwords >= 0x40000000)
                    throw new ApplicationException("Invalid number of encrypted dwords");
                var encryptedData = new uint[numDwords];
                for (int i = 0; i < numDwords; i++)
                    encryptedData[i] = dataReader.ReadUInt32();
                key[2] = dataReader.ReadUInt32();
                key[3] = dataReader.ReadUInt32();
                DeobUtils.xxteaDecrypt(encryptedData, key);
                byte[] decryptedData = new byte[encryptedData.Length * 4];
                Buffer.BlockCopy(encryptedData, 0, decryptedData, 0, decryptedData.Length);
                dataReader = new BinaryReader(new MemoryStream(decryptedData));
            }

            if (inflateData) {
                var data = dataReader.ReadBytes((int)(dataReader.BaseStream.Length - dataReader.BaseStream.Position));
                data = DeobUtils.inflate(data, true);
                dataReader = new BinaryReader(new MemoryStream(data));
            }

            foreach (var info in infos)
                info.dataReader = dataReader;

            return infos;
        }
Example #3
0
 public ResourceConverter(ModuleDefMD module, ResourceInfo[] infos)
 {
     this.module = module;
     this.dataCreator = new MyResourceDataCreator(module);
     this.infos = infos;
 }
Example #4
0
        ResourceElement Convert(ResourceInfo info)
        {
            var reader = info.dataReader;

            reader.Position = info.offset;

            IResourceData resourceData;
            int           type = info.flags & 0x7F;

            switch (type)
            {
            case 1:                     // bool
                resourceData = dataCreator.Create(reader.ReadBoolean());
                break;

            case 2:                     // byte
                resourceData = dataCreator.Create(reader.ReadByte());
                break;

            case 3:                     // byte[]
                resourceData = dataCreator.Create(reader.ReadBytes(info.length));
                break;

            case 4:                     // char[]
                resourceData = new CharArrayResourceData(dataCreator.CreateUserResourceType(CharArrayResourceData.ReflectionTypeName), reader.ReadChars(info.length));
                break;

            case 5:                     // sbyte
                resourceData = dataCreator.Create(reader.ReadSByte());
                break;

            case 6:                     // char
                resourceData = dataCreator.Create(reader.ReadChar());
                break;

            case 7:                     // decimal
                resourceData = dataCreator.Create(reader.ReadDecimal());
                break;

            case 8:                     // double
                resourceData = dataCreator.Create(reader.ReadDouble());
                break;

            case 9:                     // short
                resourceData = dataCreator.Create(reader.ReadInt16());
                break;

            case 10:                    // int
                resourceData = dataCreator.Create(reader.ReadInt32());
                break;

            case 11:                    // long
                resourceData = dataCreator.Create(reader.ReadInt64());
                break;

            case 12:                    // float
                resourceData = dataCreator.Create(reader.ReadSingle());
                break;

            case 13:                    // string
                resourceData = dataCreator.Create(reader.ReadString());
                break;

            case 14:                    // ushort
                resourceData = dataCreator.Create(reader.ReadUInt16());
                break;

            case 15:                    // uint
                resourceData = dataCreator.Create(reader.ReadUInt32());
                break;

            case 16:                    // ulong
                resourceData = dataCreator.Create(reader.ReadUInt64());
                break;

            case 17:                    // DateTime
                resourceData = dataCreator.Create(DateTime.FromBinary(reader.ReadInt64()));
                break;

            case 18:                    // TimeSpan
                resourceData = dataCreator.Create(TimeSpan.FromTicks(reader.ReadInt64()));
                break;

            case 19:                    // Icon
                resourceData = new IconResourceData(dataCreator.CreateUserResourceType(IconResourceData.ReflectionTypeName), reader.ReadBytes(info.length));
                break;

            case 20:                    // Image
                resourceData = new ImageResourceData(dataCreator.CreateUserResourceType(ImageResourceData.ReflectionTypeName), reader.ReadBytes(info.length));
                break;

            case 31:                    // binary
                resourceData = dataCreator.CreateSerialized(reader.ReadBytes(info.length));
                break;

            case 21:                    // Point (CV doesn't restore this type)
            default:
                throw new Exception("Unknown type");
            }

            return(new ResourceElement()
            {
                Name = info.name,
                ResourceData = resourceData,
            });
        }