internal void Read(IBinaryAccessor accessor) { // Reserved, 4 bytes accessor.ReadInt32(); _schemaMajorVersion = accessor.ReadByte(); _schemaMinorVersion = accessor.ReadByte(); byte heapFlags = accessor.ReadByte(); // Reserved, 1 bytes accessor.ReadByte(); ulong validMask = accessor.ReadUInt64(); _sortMask = accessor.ReadUInt64(); // Row counts int[] rowCounts = new int[MetadataConstants.TableCount]; for (int tableType = 0; tableType < MetadataConstants.TableCount; tableType++) { if ((validMask & (1UL << tableType)) != 0) { rowCounts[tableType] = accessor.ReadInt32(); } } var compressionInfo = TableCompressionInfo.Create(_metadata, rowCounts, heapFlags); // Tables for (int tableType = 0; tableType < MetadataConstants.TableCount; tableType++) { _tables[tableType].Read(accessor, compressionInfo, rowCounts[tableType]); } }
private ExceptionHandler LoadTinyExceptionHandler(IBinaryAccessor accessor, Module module) { var handler = new ExceptionHandler(); int flags = accessor.ReadInt16() & ILExceptionFlag.MASK; switch (flags) { case 0: handler.Type = ExceptionHandlerType.Catch; break; case 1: handler.Type = ExceptionHandlerType.Filter; break; case 2: handler.Type = ExceptionHandlerType.Finally; break; case 4: handler.Type = ExceptionHandlerType.Fault; break; } handler.TryOffset = accessor.ReadUInt16(); handler.TryLength = accessor.ReadByte(); handler.HandlerOffset = accessor.ReadUInt16(); handler.HandlerLength = accessor.ReadByte(); switch (handler.Type) { case ExceptionHandlerType.Catch: { int token = accessor.ReadInt32(); handler.CatchType = TypeSignature.Load(module, token); } break; case ExceptionHandlerType.Filter: { handler.FilterOffset = accessor.ReadInt32(); } break; default: { accessor.Position += 4; // padded } break; } return(handler); }
internal void Read(IBinaryAccessor accessor, ProjectReadState state) { _flags = accessor.ReadInt32(); _flags2 = accessor.ReadInt32(); if (NameChanged) { _name = state.GetString(accessor.Read7BitEncodedInt()); } if (NamespaceChanged) { _namespace = state.GetString(accessor.Read7BitEncodedInt()); } }
internal static BaseRelocationBlock Load(IBinaryAccessor accessor) { var block = new BaseRelocationBlock(); block.PageRVA = accessor.ReadUInt32(); // The total number of bytes in the base relocation block, including the Page RVA and Block Size fields and // the Type/Offset fields that follow. int blockSize = accessor.ReadInt32(); int count = (blockSize - 8) / 2; for (int i = 0; i < count; i++) { ushort value = accessor.ReadUInt16(); if (value == 0) // if necessary, insert 2 bytes of 0 to pad to a multiple of 4 bytes in length. { continue; } var type = (BaseRelocationType)(value >> 12); uint offset = (uint)(value & 0x0fff); var entry = new BaseRelocationEntry(type, offset); entry._parent = block; block._list.Add(entry); } return(block); }
private void LoadExceptionHandlerSection(IBinaryAccessor accessor, Module module, int flags) { if (_exceptionHandlers == null) { _exceptionHandlers = new List <ExceptionHandler>(); } if ((flags & 0x40) == 0x40) { // Fat format accessor.Position--; int length = (accessor.ReadInt32() >> 8) / 24; for (int i = 0; i < length; i++) { var handler = LoadFatExceptionHandler(accessor, module); _exceptionHandlers.Add(handler); } } else { // Tiny format int length = accessor.ReadByte() / 12; accessor.Position += 2; // padded for (int i = 0; i < length; i++) { var handler = LoadTinyExceptionHandler(accessor, module); _exceptionHandlers.Add(handler); } } }
internal static VTableFixupEntry Load(IBinaryAccessor accessor, PEImage pe) { var fixup = new VTableFixupEntry(); // In this definition, RVA points to the location of the v-table slot containing the method token(s). uint rva = accessor.ReadUInt32(); // Indicating the number of v-table slots grouped into one entry because their flags are identical. // This grouping has no effect other than saving some space�you can emit a single slot per entry, // but then you�ll have to emit as many v-table fixups as there are slots. int count = accessor.ReadUInt16(); fixup._type = (VTableFixupType)accessor.ReadUInt16(); bool is32Bits = ((fixup.Type & VTableFixupType.SlotSize32Bit) == VTableFixupType.SlotSize32Bit); // Save position long position = accessor.Position; accessor.Position = pe.ResolvePositionToSectionData(rva); for (int i = 0; i < count; i++) { fixup._list.Add(accessor.ReadInt32()); if (!is32Bits) { accessor.ReadUInt32(); } } // Restore position accessor.Position = position; return(fixup); }
internal static int ReadArrayLength(IBinaryAccessor accessor) { int length = accessor.ReadInt32(); if ((uint)length == 0xffffffff) { length = 0; } return(length); }
private static CustomAttributeTypedArgument LoadInt32(IBinaryAccessor accessor, Module module, bool isArray, int arrayLength) { var type = TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, module.Assembly); if (isArray) { var value = new int[arrayLength]; for (int i = 0; i < arrayLength; i++) { value[i] = accessor.ReadInt32(); } return(new CustomAttributeTypedArgument(value, type)); } else { int value = accessor.ReadInt32(); return(new CustomAttributeTypedArgument(value, type)); } }
private void Read(IBinaryAccessor accessor, ProjectReadState state) { _projectID = accessor.ReadGuid(); _createdDate = accessor.ReadDateTime(); _lastModifiedDate = accessor.ReadDateTime(); _flags = accessor.ReadInt32(); ReadStrings(accessor, state); ReadSignatures(accessor, state); ReadAssemblies(accessor, state); }
internal static ResourceDataEntry Load(IBinaryAccessor accessor) { ResourceDataEntry entry = new ResourceDataEntry(); entry._loadData = true; entry._rva = accessor.ReadUInt32(); entry._length = accessor.ReadInt32(); entry._codePage = accessor.ReadUInt32(); accessor.ReadUInt32(); return(entry); }
public static int ReadCell(this IBinaryAccessor accessor, bool size4) { // Cell value has unsigned type. if (size4) { return(accessor.ReadInt32()); } else { return(accessor.ReadUInt16()); } }
private void StateLoadLocalVariables(IBinaryAccessor accessor, Module module) { int count = accessor.Read7BitEncodedInt(); _localVariables = new List <TypeSignature>(count); for (int i = 0; i < count; i++) { int token = accessor.ReadInt32(); _localVariables.Add(module.GetSignature <TypeSignature>(token)); } }
private void StateLoad(IBinaryAccessor accessor, Module module) { _maxStackSize = accessor.Read7BitEncodedInt(); _initLocals = accessor.ReadBoolean(); _localVarToken = accessor.ReadInt32(); StateLoadInstructions(accessor, module); StateLoadLocalVariables(accessor, module); StateLoadExceptionHandlers(accessor, module); }
private void Load(IBinaryAccessor accessor, Module module) { int flags = accessor.ReadByte(); if ((flags & ILMethodFlags.FormatMask) == ILMethodFlags.FatFormat) { // Fat format accessor.ReadByte(); _maxStackSize = accessor.ReadUInt16(); int codeSize = accessor.ReadInt32(); _initLocals = ((flags & ILMethodFlags.InitLocals) == ILMethodFlags.InitLocals); _localVarToken = accessor.ReadInt32(); if (MetadataToken.GetType(_localVarToken) == MetadataTokenType.Signature) { LoadLocalVariables(module, MetadataToken.GetRID(_localVarToken)); } LoadInstructions(accessor, module, codeSize); if ((flags & ILMethodFlags.MoreSects) == ILMethodFlags.MoreSects) { // More sections LoadSection(accessor, module); } } else { // Tiny format // Used when the method is tiny (< 64 bytes), and there are no local vars _maxStackSize = 8; int codeSize = flags >> 2; LoadInstructions(accessor, module, codeSize); } }
internal void Read(IBinaryAccessor accessor, ProjectReadState state) { _flags = accessor.ReadInt32(); if (NameChanged) { _name = state.GetString(accessor.Read7BitEncodedInt()); } ReadNamespaces(accessor, state); ReadTypes(accessor, state); ReadMethods(accessor, state); ReadFields(accessor, state); ReadProperties(accessor, state); ReadEvents(accessor, state); }
internal static CertificateEntry Load(IBinaryAccessor accessor) { var entry = new CertificateEntry(); int length = accessor.ReadInt32(); entry._revision = (CertificateRevision)accessor.ReadUInt16(); entry._type = (CertificateType)accessor.ReadUInt16(); // Subtract header length (length, revision, type). int dataLength = length - 8; entry._data = accessor.ReadBytes(dataLength); return(entry); }
private void StateLoadExceptionHandler(IBinaryAccessor accessor, Module module) { var handler = new ExceptionHandler(); handler.Type = (ExceptionHandlerType)accessor.ReadInt32(); handler.TryOffset = accessor.ReadInt32(); handler.TryLength = accessor.ReadInt32(); handler.HandlerOffset = accessor.ReadInt32(); handler.HandlerLength = accessor.ReadInt32(); handler.FilterOffset = accessor.ReadInt32(); int catchToken = accessor.ReadInt32(); handler.CatchType = module.GetSignature <TypeSignature>(catchToken); _exceptionHandlers.Add(handler); }
protected internal override void Read(IBinaryAccessor accessor, TableCompressionInfo compressionInfo, int count) { if (count == 0) { return; } var rows = new FieldLayoutRow[count]; for (int i = 0; i < count; i++) { var row = new FieldLayoutRow(); row.OffSet = accessor.ReadInt32(); row.Field = accessor.ReadCell(compressionInfo.TableRowIndexSize4[MetadataTableType.Field]); rows[i] = row; } _count = count; _rows = rows; }
protected internal override void Read(IBinaryAccessor accessor, TableCompressionInfo compressionInfo, int count) { if (count == 0) { return; } var rows = new ClassLayoutRow[count]; for (int i = 0; i < count; i++) { var row = new ClassLayoutRow(); row.PackingSize = (int)accessor.ReadUInt16(); row.ClassSize = accessor.ReadInt32(); row.Parent = accessor.ReadCell(compressionInfo.TableRowIndexSize4[MetadataTableType.TypeDef]); rows[i] = row; } _count = count; _rows = rows; }
protected internal override void Read(IBinaryAccessor accessor, TableCompressionInfo compressionInfo, int count) { if (count == 0) { return; } var rows = new ManifestResourceRow[count]; for (int i = 0; i < count; i++) { var row = new ManifestResourceRow(); row.Offset = accessor.ReadInt32(); row.Flags = (int)accessor.ReadUInt32(); row.Name = accessor.ReadCell(compressionInfo.StringHeapOffsetSize4); row.Implementation = accessor.ReadCell(compressionInfo.CodedTokenDataSize4[9]); rows[i] = row; } _count = count; _rows = rows; }
private void ReadVersionString(IBinaryAccessor accessor) { int versionLength = accessor.ReadInt32(); if (versionLength == 0) { return; } byte[] buffer = accessor.ReadBytes(versionLength); int count = 0; for (int i = 0; i < buffer.Length; i++) { if (buffer[i] == 0) { break; } count++; } _frameworkVersionMoniker = Encoding.UTF8.GetString(buffer, 0, count); }
private void StateLoadInstructions(IBinaryAccessor accessor, Module module) { int instructionCount = accessor.Read7BitEncodedInt(); _instructions = new List <Instruction>(instructionCount); for (int i = 0; i < instructionCount; i++) { OpCode opCode; byte opByte = accessor.ReadByte(); if (opByte == 0xFE) { opByte = accessor.ReadByte(); opCode = OpCodes.OpCodeArray[256 + opByte]; } else { opCode = OpCodes.OpCodeArray[opByte]; } object value; switch (opCode.OperandType) { case OperandType.InlineBrTarget: { value = accessor.ReadInt32(); } break; case OperandType.InlineField: { int token = accessor.ReadInt32(); value = module.GetSignature <Signature>(token); } break; case OperandType.InlineI: { value = accessor.ReadInt32(); } break; case OperandType.InlineI8: { value = accessor.ReadInt64(); } break; case OperandType.InlineMethod: { int token = accessor.ReadInt32(); value = module.GetSignature <Signature>(token); } break; case OperandType.InlineR: { value = accessor.ReadDouble(); } break; case OperandType.InlineSig: { int token = accessor.ReadInt32(); value = module.GetSignature <Signature>(token); } break; case OperandType.InlineString: { // Token of a userdefined string, whose RID portion is actually an offset in the #US blob stream. value = accessor.ReadLengthPrefixedString(Encoding.Unicode); } break; case OperandType.InlineSwitch: { int count = accessor.ReadInt32(); int[] targets = new int[count]; for (int j = 0; j < count; j++) { targets[j] = accessor.ReadInt32(); } value = targets; } break; case OperandType.InlineTok: { int token = accessor.ReadInt32(); value = module.GetSignature <Signature>(token); } break; case OperandType.InlineType: { int token = accessor.ReadInt32(); value = module.GetSignature <Signature>(token); } break; case OperandType.InlineVar: { value = accessor.ReadInt16(); } break; case OperandType.ShortInlineBrTarget: { value = accessor.ReadSByte(); } break; case OperandType.ShortInlineI: { value = accessor.ReadByte(); } break; case OperandType.ShortInlineR: { value = accessor.ReadSingle(); } break; case OperandType.ShortInlineVar: { value = accessor.ReadByte(); } break; default: { value = null; } break; } _instructions.Add(new Instruction(opCode, value)); } }
internal void Read(IBinaryAccessor accessor, ProjectReadState state) { _flags = accessor.ReadInt32(); }
public static DateTime ReadDateTime(this IBinaryAccessor accessor) { return(ConvertUtils.ToDateTime(accessor.ReadInt32())); }
public static DataDirectory ReadDataDirectory(this IBinaryAccessor accessor) { return(new DataDirectory(accessor.ReadUInt32(), accessor.ReadInt32())); }
private void LoadInstructions(IBinaryAccessor accessor, Module module, int codeSize) { long startOffset = accessor.Position; var image = module.Image; _instructions = new List <Instruction>(); while (accessor.Position < startOffset + codeSize) { OpCode opCode; byte opByte = accessor.ReadByte(); if (opByte == 0xFE) { opByte = accessor.ReadByte(); opCode = OpCodes.OpCodeArray[256 + opByte]; } else { opCode = OpCodes.OpCodeArray[opByte]; } if (opCode == null) { throw new CodeModelException(string.Format(SR.AssemblyLoadError, module.Location)); } object value; switch (opCode.OperandType) { case OperandType.InlineBrTarget: { value = accessor.ReadInt32(); } break; case OperandType.InlineField: { int token = accessor.ReadInt32(); value = FieldReference.Load(module, token); } break; case OperandType.InlineI: { value = accessor.ReadInt32(); } break; case OperandType.InlineI8: { value = accessor.ReadInt64(); } break; case OperandType.InlineMethod: { int token = accessor.ReadInt32(); value = MethodReference.Load(module, token); } break; case OperandType.InlineR: { value = accessor.ReadDouble(); } break; case OperandType.InlineSig: { int token = accessor.ReadInt32(); if (MetadataToken.GetType(token) == MetadataTokenType.Signature) { int rid = MetadataToken.GetRID(token); value = CallSite.LoadStandAloneSig(module, rid); } else { throw new CodeModelException(SR.MethodBodyBlobNotValid); } } break; case OperandType.InlineString: { // Token of a userdefined string, whose RID portion is actually an offset in the #US blob stream. uint token = accessor.ReadUInt32(); int rid = (int)(token & 0x00ffffff); value = image.GetUserString(rid); } break; case OperandType.InlineSwitch: { int count = accessor.ReadInt32(); int[] targets = new int[count]; for (int i = 0; i < count; i++) { targets[i] = accessor.ReadInt32(); } value = targets; } break; case OperandType.InlineTok: { int token = accessor.ReadInt32(); int rid = MetadataToken.GetRID(token); switch (MetadataToken.GetType(token)) { case MetadataTokenType.Method: value = MethodReference.LoadMethodDef(module, rid); break; case MetadataTokenType.MethodSpec: value = GenericMethodReference.LoadMethodSpec(module, rid); break; case MetadataTokenType.MemberRef: value = MethodReference.LoadMemberRef(module, rid); break; case MetadataTokenType.Field: value = FieldReference.LoadFieldDef(module, rid); break; case MetadataTokenType.TypeDef: value = TypeReference.LoadTypeDef(module, rid); break; case MetadataTokenType.TypeRef: value = TypeReference.LoadTypeRef(module, rid); break; case MetadataTokenType.TypeSpec: value = TypeSignature.LoadTypeSpec(module, rid); break; default: throw new CodeModelException(SR.MethodBodyBlobNotValid); } } break; case OperandType.InlineType: { int token = accessor.ReadInt32(); value = TypeSignature.Load(module, token); } break; case OperandType.InlineVar: { value = accessor.ReadInt16(); } break; case OperandType.ShortInlineBrTarget: { value = accessor.ReadSByte(); } break; case OperandType.ShortInlineI: { value = accessor.ReadByte(); } break; case OperandType.ShortInlineR: { value = accessor.ReadSingle(); } break; case OperandType.ShortInlineVar: { value = accessor.ReadByte(); } break; default: { value = null; } break; } _instructions.Add(new Instruction(opCode, value)); } }
private void ReadStreams(IBinaryAccessor accessor, long metadataOffset) { int numberOfStream = accessor.ReadUInt16(); int[] offsets = new int[numberOfStream]; int[] sizes = new int[numberOfStream]; string[] names = new string[numberOfStream]; for (int i = 0; i < numberOfStream; i++) { offsets[i] = accessor.ReadInt32(); sizes[i] = accessor.ReadInt32(); // Name of the stream; a zero-terminated ASCII string no longer than 31 characters (plus zero terminator). // The name might be shorter, in which case the size of the stream header is correspondingly reduced, // padded to the 4-byte boundary. long startPos = accessor.Position; names[i] = accessor.ReadNullTerminatedString(Encoding.ASCII); accessor.Align(startPos, 4); } int tableIndex = -1; for (int i = 0; i < numberOfStream; i++) { int offset = offsets[i]; int size = sizes[i]; string name = names[i]; if (name == MetadataConstants.StreamTable) { tableIndex = i; _tables.IsOptimized = true; } else if (name == MetadataConstants.StreamTableUnoptimized) { tableIndex = i; _tables.IsOptimized = false; } else if (name == MetadataConstants.StreamStrings) { accessor.Position = offset + metadataOffset; _strings.Blob = new Blob(accessor.ReadBytes(size)); } else if (name == MetadataConstants.StreamUserStrings) { accessor.Position = offset + metadataOffset; _userStrings.Blob = new Blob(accessor.ReadBytes(size)); } else if (name == MetadataConstants.StreamGuid) { accessor.Position = offset + metadataOffset; _guids.Blob = new Blob(accessor.ReadBytes(size)); } else if (name == MetadataConstants.StreamBlob) { accessor.Position = offset + metadataOffset; _blobs.Blob = new Blob(accessor.ReadBytes(size)); } else { accessor.Position = offset + metadataOffset; var stream = new MetadataExternalStream(name, new Blob(accessor.ReadBytes(size))); ExternalStreams.Add(stream); } } if (tableIndex >= 0) { // Read table last as it relies on heaps. accessor.Position = offsets[tableIndex] + metadataOffset; _tables.Read(accessor); } }
internal void Read(IBinaryAccessor accessor, ProjectReadState state) { _filePath = ProjectHelper.MakeAbsolutePath(state.GetString(accessor.Read7BitEncodedInt()), state.BasePath); _outputPath = ProjectHelper.MakeAbsolutePath(state.GetString(accessor.Read7BitEncodedInt()), state.BasePath); _flags = accessor.ReadInt32(); _flags2 = accessor.ReadInt32(); _flags3 = accessor.ReadInt32(); if (NameChanged) { _name = state.GetString(accessor.Read7BitEncodedInt()); } if (CultureChanged) { _culture = state.GetString(accessor.Read7BitEncodedInt()); } if (VersionChanged) { _version = new Version(accessor.ReadUInt16(), accessor.ReadUInt16(), accessor.ReadUInt16(), accessor.ReadUInt16()); } if (TitleChanged) { _title = state.GetString(accessor.Read7BitEncodedInt()); } if (DescriptionChanged) { _description = state.GetString(accessor.Read7BitEncodedInt()); } if (CompanyChanged) { _company = state.GetString(accessor.Read7BitEncodedInt()); } if (ProductChanged) { _product = state.GetString(accessor.Read7BitEncodedInt()); } if (CopyrightChanged) { _copyright = state.GetString(accessor.Read7BitEncodedInt()); } if (TrademarkChanged) { _trademark = state.GetString(accessor.Read7BitEncodedInt()); } if (accessor.ReadBoolean()) { _sign = new ProjectAssemblySign(accessor, state); } ReadModules(accessor, state); ReadResources(accessor, state); }