Ejemplo n.º 1
0
        private static object ReadValue(BufferedBinaryReader reader, SystemTypeCode type)
        {
            switch (type)
            {
            case SystemTypeCode.Boolean:
                return(reader.ReadBoolean());

            case SystemTypeCode.Int8:
                return(reader.ReadInt8());

            case SystemTypeCode.UInt8:
                return(reader.ReadUInt8());

            case SystemTypeCode.Int16:
                return(reader.ReadInt16());

            case SystemTypeCode.UInt16:
                return(reader.ReadUInt16());

            case SystemTypeCode.Int32:
                return(reader.ReadInt32());

            case SystemTypeCode.UInt32:
                return(reader.ReadUInt32());

            case SystemTypeCode.Int64:
                return(reader.ReadInt64());

            case SystemTypeCode.UInt64:
                return(reader.ReadUInt64());

            case SystemTypeCode.Single:
                return(reader.ReadSingle());

            case SystemTypeCode.Double:
                return(reader.ReadDouble());

            case SystemTypeCode.Decimal:
                throw new NotImplementedException();

            case SystemTypeCode.DateTime:
                throw new NotImplementedException();

            case SystemTypeCode.Char:
                return(reader.ReadChar());

            case SystemTypeCode.String:
                return(reader.ReadCountedUtf8());

            case SystemTypeCode.Enum:
                throw new NotImplementedException();

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 2
0
        private static object ReadValue(ElementType type, BufferedBinaryReader reader)
        {
            switch (type)
            {
            case ElementType.Boolean:
                return(reader.ReadBoolean());

            case ElementType.Char:
                return(reader.ReadChar());

            case ElementType.Int8:
                return(reader.ReadSByte());

            case ElementType.UInt8:
                return(reader.ReadByte());

            case ElementType.Int16:
                return(reader.ReadInt16());

            case ElementType.UInt16:
                return(reader.ReadUInt16());

            case ElementType.Int32:
                return(reader.ReadInt32());

            case ElementType.UInt32:
                return(reader.ReadUInt32());

            case ElementType.Int64:
                return(reader.ReadInt64());

            case ElementType.UInt64:
                return(reader.ReadUInt64());

            case ElementType.Single:
                return(reader.ReadSingle());

            case ElementType.Double:
                return(reader.ReadDouble());

            case ElementType.String:
                return(Encoding.Unicode.GetString(reader.ToArray()));

            case ElementType.Class:
                return(null);

            default:
                return(reader.ToArray());
            }
        }
Ejemplo n.º 3
0
		private object ReadValue(BufferedBinaryReader reader, IType type)
		{
			var st = type.SystemType();
			if (st != null)
			{
				switch (st.Code)
				{
					case SystemTypeCode.Boolean:
						return reader.ReadBoolean();
					case SystemTypeCode.Int8:
						return reader.ReadInt8();
					case SystemTypeCode.UInt8:
						return reader.ReadUInt8();
					case SystemTypeCode.Int16:
						return reader.ReadInt16();
					case SystemTypeCode.UInt16:
						return reader.ReadUInt16();
					case SystemTypeCode.Int32:
						return reader.ReadInt32();
					case SystemTypeCode.UInt32:
						return reader.ReadUInt32();
					case SystemTypeCode.Int64:
						return reader.ReadInt64();
					case SystemTypeCode.UInt64:
						return reader.ReadUInt64();
					case SystemTypeCode.Single:
						return reader.ReadSingle();
					case SystemTypeCode.Double:
						return reader.ReadDouble();
					case SystemTypeCode.Char:
						return reader.ReadChar();
					case SystemTypeCode.String:
						return reader.ReadCountedUtf8();
					case SystemTypeCode.Object:
						//boxed value type
						var e = (ElementType)reader.ReadInt8();
						return ReadValue(reader, e);
					case SystemTypeCode.Type:
						return ReadType(reader);
				}
			}

			if (type.TypeKind == TypeKind.Enum)
			{
				return ReadValue(reader, type.ValueType);
			}

			if (type.IsArray)
			{
				int numElem = reader.ReadInt32();
				Array arr = null;
				for (int i = 0; i < numElem; ++i)
				{
					var val = ReadValue(reader, type.ElementType);
					if (arr == null)
						arr = Array.CreateInstance(val.GetType(), numElem);
					arr.SetValue(val, i);
				}
				return arr;
			}

			return null;
		}
Ejemplo n.º 4
0
		private object ReadValue(BufferedBinaryReader reader, ElementType e)
		{
			switch (e)
			{
				case ElementType.Boolean:
					return reader.ReadBoolean();

				case ElementType.Char:
					return reader.ReadChar();

				case ElementType.Int8:
					return reader.ReadSByte();

				case ElementType.UInt8:
					return reader.ReadByte();

				case ElementType.Int16:
					return reader.ReadInt16();

				case ElementType.UInt16:
					return reader.ReadUInt16();

				case ElementType.Int32:
					return reader.ReadInt32();

				case ElementType.UInt32:
					return reader.ReadUInt32();

				case ElementType.Int64:
					return reader.ReadInt64();

				case ElementType.UInt64:
					return reader.ReadUInt64();

				case ElementType.Single:
					return reader.ReadSingle();

				case ElementType.Double:
					return reader.ReadDouble();

				case ElementType.String:
					return reader.ReadCountedUtf8();

				case ElementType.Object:
				case ElementType.CustomArgsBoxedObject:
					{
						var elem = (ElementType)reader.ReadInt8();
						return ReadValue(reader, elem);
					}

				case ElementType.CustomArgsEnum:
					{
						string enumTypeName = reader.ReadCountedUtf8();
						var enumType = FindType(enumTypeName);
						if (enumType == null)
						{
							//TODO:
							throw new BadMetadataException();
						}
						return ReadValue(reader, enumType);
					}

				case ElementType.CustomArgsType:
					return ReadType(reader);

				case ElementType.ArraySz:
					{
						var arrElemType = (ElementType)reader.ReadInt8();
						return ReadArray(reader, arrElemType);
					}

				default:
					throw new ArgumentOutOfRangeException();
			}
		}
Ejemplo n.º 5
0
        private static Instruction ReadInstruction(IMethod method, IMethodContext context, BufferedBinaryReader reader, long startPosition)
        {
            var instr = new Instruction
            {
                Offset = (int)(reader.Position - startPosition),
                OpCode = OpCodes.Nop
            };

            byte   op = reader.ReadUInt8();
            OpCode?opCode;

            if (op != CIL.MultiBytePrefix)
            {
                opCode = CIL.GetShortOpCode(op);
            }
            else
            {
                op     = reader.ReadUInt8();
                opCode = CIL.GetLongOpCode(op);
            }

            if (!opCode.HasValue)
            {
                throw new BadImageFormatException(string.Format("The format of instruction with code {0} is invalid", op));
            }

            instr.OpCode = opCode.Value;

            //Read operand
            switch (instr.OpCode.OperandType)
            {
            case OperandType.InlineI:
                instr.Value = reader.ReadInt32();
                break;

            case OperandType.ShortInlineI:
                instr.Value = (int)reader.ReadSByte();
                break;

            case OperandType.InlineI8:
                instr.Value = reader.ReadInt64();
                break;

            case OperandType.InlineR:
                instr.Value = reader.ReadDouble();
                break;

            case OperandType.ShortInlineR:
                instr.Value = reader.ReadSingle();
                break;

            case OperandType.InlineBrTarget:
            {
                int offset = reader.ReadInt32();
                instr.Value = (int)(offset + reader.Position - startPosition);
            }
            break;

            case OperandType.ShortInlineBrTarget:
            {
                int offset = reader.ReadSByte();
                instr.Value = (int)(offset + reader.Position - startPosition);
            }
            break;

            case OperandType.InlineSwitch:
            {
                int casesCount     = reader.ReadInt32();
                var switchBranches = new int[casesCount];
                for (int k = 0; k < casesCount; k++)
                {
                    switchBranches[k] = reader.ReadInt32();
                }

                int shift = (int)(reader.Position - startPosition);
                for (int k = 0; k < casesCount; k++)
                {
                    switchBranches[k] += shift;
                }

                instr.Value = switchBranches;
            }
            break;

            case OperandType.InlineVar:
                instr.Value = (int)reader.ReadUInt16();
                break;

            case OperandType.ShortInlineVar:
                instr.Value = reader.ReadByte();
                break;

            case OperandType.InlineString:
            {
                int token = reader.ReadInt32();
                instr.Value = context.ResolveMetadataToken(method, token);
            }
            break;

            case OperandType.InlineField:
            case OperandType.InlineMethod:
            case OperandType.InlineSig:
            case OperandType.InlineTok:
            case OperandType.InlineType:
            {
                int token = reader.ReadInt32();
                instr.MetadataToken = token;

                object val = context.ResolveMetadataToken(method, token);
                if (val is ITypeMember)
                {
                }

                if (val == null)
                {
#if DEBUG
                    if (DebugHooks.BreakInvalidMetadataToken)
                    {
                        Debugger.Break();
                        val = context.ResolveMetadataToken(method, token);
                    }
#endif
                    throw new BadTokenException(token);
                }

                instr.Value = val;
            }
            break;

            case OperandType.InlineNone:
                // no operand
                break;

            case OperandType.InlinePhi:
                throw new BadImageFormatException(@"Obsolete. The InlinePhi operand is reserved and should not be used!");
            }

            return(instr);
        }