Beispiel #1
0
 internal MethodIL(
     bool localVariablesInited,
     ushort maxStack,
     uint localSignatureToken,
     MemoryBlock encodedILMemoryBlock,
     SEHTableEntry[]/*?*/ sehTable
     )
 {
     this.LocalVariablesInited = localVariablesInited;
       this.MaxStack = maxStack;
       this.LocalSignatureToken = localSignatureToken;
       this.EncodedILMemoryBlock = encodedILMemoryBlock;
       this.SEHTable = sehTable;
 }
 bool ProcessAndCacheMetadataTableBlocks(
   MemoryBlock metadataTablesMemoryBlock
 )
   //^ requires this.ReaderState >= ReaderState.CORModule;
 {
   uint[] rowCountArray = new uint[(int)TableIndices.Count];
   int[] rowRefSizeArray = new int[(int)TableIndices.Count];
   ulong validTables = (ulong)this.MetadataTableHeader.ValidTables;
   uint[] rowCountCompressedArray = this.MetadataTableRowCount;
   //  Fill in the row count and table reference sizes...
   for (int tableIndex = 0, arrayIndex = 0; tableIndex < (int)TableIndices.Count; tableIndex++) {
     if ((validTables & 0x0000000000000001UL) != 0) {
       uint rowCount = rowCountCompressedArray[arrayIndex++];
       rowCountArray[tableIndex] = rowCount;
       rowRefSizeArray[tableIndex] = rowCount < MetadataStreamConstants.LargeTableRowCount ? 2 : 4;
     } else {
       rowRefSizeArray[tableIndex] = 2;
     }
     validTables >>= 1;
   }
   //  Compute ref sizes for tables that can have pointer tables for it
   int fieldRefSize = rowRefSizeArray[(int)TableIndices.FieldPtr] > 2 ? 4 : rowRefSizeArray[(int)TableIndices.Field];
   int methodRefSize = rowRefSizeArray[(int)TableIndices.MethodPtr] > 2 ? 4 : rowRefSizeArray[(int)TableIndices.Method];
   int paramRefSize = rowRefSizeArray[(int)TableIndices.ParamPtr] > 2 ? 4 : rowRefSizeArray[(int)TableIndices.Param];
   int eventRefSize = rowRefSizeArray[(int)TableIndices.EventPtr] > 2 ? 4 : rowRefSizeArray[(int)TableIndices.Event];
   int propertyRefSize = rowRefSizeArray[(int)TableIndices.PropertyPtr] > 2 ? 4 : rowRefSizeArray[(int)TableIndices.Property];
   //  Compute the coded token ref sizes
   int typeDefOrRefRefSize = PEFileReader.ComputeCodedTokenSize(TypeDefOrRefTag.LargeRowSize, rowCountArray, TypeDefOrRefTag.TablesReferenced);
   int hasConstantRefSize = PEFileReader.ComputeCodedTokenSize(HasConstantTag.LargeRowSize, rowCountArray, HasConstantTag.TablesReferenced);
   int hasCustomAttributeRefSize = PEFileReader.ComputeCodedTokenSize(HasCustomAttributeTag.LargeRowSize, rowCountArray, HasCustomAttributeTag.TablesReferenced);
   int hasFieldMarshalRefSize = PEFileReader.ComputeCodedTokenSize(HasFieldMarshalTag.LargeRowSize, rowCountArray, HasFieldMarshalTag.TablesReferenced);
   int hasDeclSecurityRefSize = PEFileReader.ComputeCodedTokenSize(HasDeclSecurityTag.LargeRowSize, rowCountArray, HasDeclSecurityTag.TablesReferenced);
   int memberRefParentRefSize = PEFileReader.ComputeCodedTokenSize(MemberRefParentTag.LargeRowSize, rowCountArray, MemberRefParentTag.TablesReferenced);
   int hasSemanticsRefSize = PEFileReader.ComputeCodedTokenSize(HasSemanticsTag.LargeRowSize, rowCountArray, HasSemanticsTag.TablesReferenced);
   int methodDefOrRefRefSize = PEFileReader.ComputeCodedTokenSize(MethodDefOrRefTag.LargeRowSize, rowCountArray, MethodDefOrRefTag.TablesReferenced);
   int memberForwardedRefSize = PEFileReader.ComputeCodedTokenSize(MemberForwardedTag.LargeRowSize, rowCountArray, MemberForwardedTag.TablesReferenced);
   int implementationRefSize = PEFileReader.ComputeCodedTokenSize(ImplementationTag.LargeRowSize, rowCountArray, ImplementationTag.TablesReferenced);
   int customAttributeTypeRefSize = PEFileReader.ComputeCodedTokenSize(CustomAttributeTypeTag.LargeRowSize, rowCountArray, CustomAttributeTypeTag.TablesReferenced);
   int resolutionScopeRefSize = PEFileReader.ComputeCodedTokenSize(ResolutionScopeTag.LargeRowSize, rowCountArray, ResolutionScopeTag.TablesReferenced);
   int typeOrMethodDefRefSize = PEFileReader.ComputeCodedTokenSize(TypeOrMethodDefTag.LargeRowSize, rowCountArray, TypeOrMethodDefTag.TablesReferenced);
   //  Compute HeapRef Sizes
   int stringHeapRefSize = (this.MetadataTableHeader.HeapSizeFlags & HeapSizeFlag.StringHeapLarge) == HeapSizeFlag.StringHeapLarge ? 4 : 2;
   int guidHeapRefSize = (this.MetadataTableHeader.HeapSizeFlags & HeapSizeFlag.GUIDHeapLarge) == HeapSizeFlag.GUIDHeapLarge ? 4 : 2;
   int blobHeapRefSize = (this.MetadataTableHeader.HeapSizeFlags & HeapSizeFlag.BlobHeapLarge) == HeapSizeFlag.BlobHeapLarge ? 4 : 2;
   //  Populate the Table blocks
   int totalRequiredSize = 0;
   int currentTableSize = 0;
   byte* currentPointer = metadataTablesMemoryBlock.Buffer;
   this.ModuleTable = new ModuleTableReader(rowCountArray[(int)TableIndices.Module], stringHeapRefSize, guidHeapRefSize, currentPointer);
   currentTableSize = this.ModuleTable.ModuleTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.TypeRefTable = new TypeRefTableReader(rowCountArray[(int)TableIndices.TypeRef], resolutionScopeRefSize, stringHeapRefSize, currentPointer);
   currentTableSize = this.TypeRefTable.TypeRefTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.TypeDefTable = new TypeDefTableReader(rowCountArray[(int)TableIndices.TypeDef], fieldRefSize, methodRefSize, typeDefOrRefRefSize, stringHeapRefSize, currentPointer);
   currentTableSize = this.TypeDefTable.TypeDefTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.FieldPtrTable = new FieldPtrTableReader(rowCountArray[(int)TableIndices.FieldPtr], rowRefSizeArray[(int)TableIndices.Field], currentPointer);
   currentTableSize = this.FieldPtrTable.FieldPtrTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.FieldTable = new FieldTableReader(rowCountArray[(int)TableIndices.Field], stringHeapRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.FieldTable.FieldTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.MethodPtrTable = new MethodPtrTableReader(rowCountArray[(int)TableIndices.MethodPtr], rowRefSizeArray[(int)TableIndices.Method], currentPointer);
   currentTableSize = this.MethodPtrTable.MethodPtrTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.MethodTable = new MethodTableReader(rowCountArray[(int)TableIndices.Method], paramRefSize, stringHeapRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.MethodTable.MethodTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.ParamPtrTable = new ParamPtrTableReader(rowCountArray[(int)TableIndices.ParamPtr], rowRefSizeArray[(int)TableIndices.Param], currentPointer);
   currentTableSize = this.ParamPtrTable.ParamPtrTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.ParamTable = new ParamTableReader(rowCountArray[(int)TableIndices.Param], stringHeapRefSize, currentPointer);
   currentTableSize = this.ParamTable.ParamTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.InterfaceImplTable = new InterfaceImplTableReader(rowCountArray[(int)TableIndices.InterfaceImpl], rowRefSizeArray[(int)TableIndices.TypeDef], typeDefOrRefRefSize, currentPointer);
   currentTableSize = this.InterfaceImplTable.InterfaceImplTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.MemberRefTable = new MemberRefTableReader(rowCountArray[(int)TableIndices.MemberRef], memberRefParentRefSize, stringHeapRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.MemberRefTable.MemberRefTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.ConstantTable = new ConstantTableReader(rowCountArray[(int)TableIndices.Constant], hasConstantRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.ConstantTable.ConstantTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.CustomAttributeTable = new CustomAttributeTableReader(rowCountArray[(int)TableIndices.CustomAttribute], hasCustomAttributeRefSize, customAttributeTypeRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.CustomAttributeTable.CustomAttributeTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.FieldMarshalTable = new FieldMarshalTableReader(rowCountArray[(int)TableIndices.FieldMarshal], hasFieldMarshalRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.FieldMarshalTable.FieldMarshalTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.DeclSecurityTable = new DeclSecurityTableReader(rowCountArray[(int)TableIndices.DeclSecurity], hasDeclSecurityRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.DeclSecurityTable.DeclSecurityTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.ClassLayoutTable = new ClassLayoutTableReader(rowCountArray[(int)TableIndices.ClassLayout], rowRefSizeArray[(int)TableIndices.TypeDef], currentPointer);
   currentTableSize = this.ClassLayoutTable.ClassLayoutTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.FieldLayoutTable = new FieldLayoutTableReader(rowCountArray[(int)TableIndices.FieldLayout], rowRefSizeArray[(int)TableIndices.Field], currentPointer);
   currentTableSize = this.FieldLayoutTable.FieldLayoutTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.StandAloneSigTable = new StandAloneSigTableReader(rowCountArray[(int)TableIndices.StandAloneSig], blobHeapRefSize, currentPointer);
   currentTableSize = this.StandAloneSigTable.StandAloneSigTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.EventMapTable = new EventMapTableReader(rowCountArray[(int)TableIndices.EventMap], rowRefSizeArray[(int)TableIndices.TypeDef], eventRefSize, currentPointer);
   currentTableSize = this.EventMapTable.EventMapTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.EventPtrTable = new EventPtrTableReader(rowCountArray[(int)TableIndices.EventPtr], rowRefSizeArray[(int)TableIndices.Event], currentPointer);
   currentTableSize = this.EventPtrTable.EventPtrTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.EventTable = new EventTableReader(rowCountArray[(int)TableIndices.Event], typeDefOrRefRefSize, stringHeapRefSize, currentPointer);
   currentTableSize = this.EventTable.EventTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.PropertyMapTable = new PropertyMapTableReader(rowCountArray[(int)TableIndices.PropertyMap], rowRefSizeArray[(int)TableIndices.TypeDef], propertyRefSize, currentPointer);
   currentTableSize = this.PropertyMapTable.PropertyMapTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.PropertyPtrTable = new PropertyPtrTableReader(rowCountArray[(int)TableIndices.PropertyPtr], rowRefSizeArray[(int)TableIndices.Property], currentPointer);
   currentTableSize = this.PropertyPtrTable.PropertyPtrTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.PropertyTable = new PropertyTableReader(rowCountArray[(int)TableIndices.Property], stringHeapRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.PropertyTable.PropertyTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.MethodSemanticsTable = new MethodSemanticsTableReader(rowCountArray[(int)TableIndices.MethodSemantics], rowRefSizeArray[(int)TableIndices.Method], hasSemanticsRefSize, currentPointer);
   currentTableSize = this.MethodSemanticsTable.MethodSemanticsTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.MethodImplTable = new MethodImplTableReader(rowCountArray[(int)TableIndices.MethodImpl], rowRefSizeArray[(int)TableIndices.TypeDef], methodDefOrRefRefSize, currentPointer);
   currentTableSize = this.MethodImplTable.MethodImplTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.ModuleRefTable = new ModuleRefTableReader(rowCountArray[(int)TableIndices.ModuleRef], stringHeapRefSize, currentPointer);
   currentTableSize = this.ModuleRefTable.ModuleRefTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.TypeSpecTable = new TypeSpecTableReader(rowCountArray[(int)TableIndices.TypeSpec], blobHeapRefSize, currentPointer);
   currentTableSize = this.TypeSpecTable.TypeSpecTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.ImplMapTable = new ImplMapTableReader(rowCountArray[(int)TableIndices.ImplMap], rowRefSizeArray[(int)TableIndices.ModuleRef], memberForwardedRefSize, stringHeapRefSize, currentPointer);
   currentTableSize = this.ImplMapTable.ImplMapTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.FieldRVATable = new FieldRVATableReader(rowCountArray[(int)TableIndices.FieldRva], rowRefSizeArray[(int)TableIndices.Field], currentPointer);
   currentTableSize = this.FieldRVATable.FieldRVATableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.EnCLogTable = new EnCLogTableReader(rowCountArray[(int)TableIndices.EnCLog], currentPointer);
   currentTableSize = this.EnCLogTable.EnCLogTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.EnCMapTable = new EnCMapTableReader(rowCountArray[(int)TableIndices.EnCMap], currentPointer);
   currentTableSize = this.EnCMapTable.EnCMapTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.AssemblyTable = new AssemblyTableReader(rowCountArray[(int)TableIndices.Assembly], stringHeapRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.AssemblyTable.AssemblyTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.AssemblyProcessorTable = new AssemblyProcessorTableReader(rowCountArray[(int)TableIndices.AssemblyProcessor], currentPointer);
   currentTableSize = this.AssemblyProcessorTable.AssemblyProcessorTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.AssemblyOSTable = new AssemblyOSTableReader(rowCountArray[(int)TableIndices.AssemblyOS], currentPointer);
   currentTableSize = this.AssemblyOSTable.AssemblyOSTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.AssemblyRefTable = new AssemblyRefTableReader(rowCountArray[(int)TableIndices.AssemblyRef], stringHeapRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.AssemblyRefTable.AssemblyRefTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.AssemblyRefProcessorTable = new AssemblyRefProcessorTableReader(rowCountArray[(int)TableIndices.AssemblyRefProcessor], rowRefSizeArray[(int)TableIndices.AssemblyRef], currentPointer);
   currentTableSize = this.AssemblyRefProcessorTable.AssemblyRefProcessorTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.AssemblyRefOSTable = new AssemblyRefOSTableReader(rowCountArray[(int)TableIndices.AssemblyRefOS], rowRefSizeArray[(int)TableIndices.AssemblyRef], currentPointer);
   currentTableSize = this.AssemblyRefOSTable.AssemblyRefOSTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.FileTable = new FileTableReader(rowCountArray[(int)TableIndices.File], stringHeapRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.FileTable.FileTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.ExportedTypeTable = new ExportedTypeTableReader(rowCountArray[(int)TableIndices.ExportedType], implementationRefSize, stringHeapRefSize, currentPointer);
   currentTableSize = this.ExportedTypeTable.ExportedTypeTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.ManifestResourceTable = new ManifestResourceTableReader(rowCountArray[(int)TableIndices.ManifestResource], implementationRefSize, stringHeapRefSize, currentPointer);
   currentTableSize = this.ManifestResourceTable.ManifestResourceTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.NestedClassTable = new NestedClassTableReader(rowCountArray[(int)TableIndices.NestedClass], rowRefSizeArray[(int)TableIndices.TypeDef], currentPointer);
   currentTableSize = this.NestedClassTable.NestedClassTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.GenericParamTable = new GenericParamTableReader(rowCountArray[(int)TableIndices.GenericParam], typeOrMethodDefRefSize, stringHeapRefSize, currentPointer);
   currentTableSize = this.GenericParamTable.GenericParamTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.MethodSpecTable = new MethodSpecTableReader(rowCountArray[(int)TableIndices.MethodSpec], methodDefOrRefRefSize, blobHeapRefSize, currentPointer);
   currentTableSize = this.MethodSpecTable.MethodSpecTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   this.GenericParamConstraintTable = new GenericParamConstraintTableReader(rowCountArray[(int)TableIndices.GenericParamConstraint], rowRefSizeArray[(int)TableIndices.GenericParam], typeDefOrRefRefSize, currentPointer);
   currentTableSize = this.GenericParamConstraintTable.GenericParamConstraintTableMemoryReader.Length;
   totalRequiredSize += currentTableSize;
   currentPointer += currentTableSize;
   if (totalRequiredSize > metadataTablesMemoryBlock.Length) {
     this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, 0, MetadataReaderErrorKind.MetadataTablesTooSmall);
     return false;
   }
   return true;
 }
Beispiel #3
0
 internal SubSection(
     string sectionName,
     int offset,
     MemoryBlock memoryBlock
     )
 {
     this.SectionName = sectionName;
       this.Offset = (uint)offset;
       this.MemoryBlock = memoryBlock;
 }
 bool ProcessAndCacheStreams(
   ref MemoryBlock metadataRoot
 )
   //^ requires this.ReaderState >= ReaderState.PEFile;
 {
   foreach (StreamHeader streamHeader in this.StreamHeaders) {
     switch (streamHeader.Name) {
       case COR20Constants.StringStreamName:
         if (metadataRoot.Length < streamHeader.Offset + streamHeader.Size) {
           this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, streamHeader.Offset, MetadataReaderErrorKind.NotEnoughSpaceForStringStream);
           return false;
         }
         this.StringStream.MemoryReader =
           new MemoryReader(
             metadataRoot.Buffer + streamHeader.Offset,
             streamHeader.Size
           );
         break;
       case COR20Constants.BlobStreamName:
         if (metadataRoot.Length < streamHeader.Offset + streamHeader.Size) {
           this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, streamHeader.Offset, MetadataReaderErrorKind.NotEnoughSpaceForBlobStream);
           return false;
         }
         this.BlobStream.MemoryReader =
           new MemoryReader(
             metadataRoot.Buffer + streamHeader.Offset,
             streamHeader.Size
           );
         break;
       case COR20Constants.GUIDStreamName:
         if (metadataRoot.Length < streamHeader.Offset + streamHeader.Size) {
           this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, streamHeader.Offset, MetadataReaderErrorKind.NotEnoughSpaceForGUIDStream);
           return false;
         }
         this.GUIDStream.MemoryReader =
           new MemoryReader(
             metadataRoot.Buffer + streamHeader.Offset,
             streamHeader.Size
           );
         break;
       case COR20Constants.UserStringStreamName:
         if (metadataRoot.Length < streamHeader.Offset + streamHeader.Size) {
           this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, streamHeader.Offset, MetadataReaderErrorKind.NotEnoughSpaceForBlobStream);
           return false;
         }
         this.UserStringStream.MemoryReader =
           new MemoryReader(
             metadataRoot.Buffer + streamHeader.Offset,
             streamHeader.Size
           );
         break;
       case COR20Constants.CompressedMetadataTableStreamName:
         if (metadataRoot.Length < streamHeader.Offset + streamHeader.Size) {
           this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, streamHeader.Offset, MetadataReaderErrorKind.NotEnoughSpaceForMetadataStream);
           return false;
         }
         this.MetadataStreamKind = MetadataStreamKind.Compressed;
         this.MetadataTableStream =
           new MemoryBlock(
             metadataRoot.Buffer + streamHeader.Offset,
             streamHeader.Size
           );
         break;
       case COR20Constants.UncompressedMetadataTableStreamName:
         if (metadataRoot.Length < streamHeader.Offset + streamHeader.Size) {
           this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, streamHeader.Offset, MetadataReaderErrorKind.NotEnoughSpaceForMetadataStream);
           return false;
         }
         this.MetadataStreamKind = MetadataStreamKind.UnCompressed;
         this.MetadataTableStream =
           new MemoryBlock(
             metadataRoot.Buffer + streamHeader.Offset,
             streamHeader.Size
           );
         break;
       default:
         this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, streamHeader.Offset, MetadataReaderErrorKind.UnknownMetadataStream);
         break;
     }
   }
   return true;
 }
 bool ReadCORModuleLevelData()
   //^ requires this.ReaderState >= ReaderState.PEFile;
 {
   if (!this.ReadCOR20Header()) {
     return false;
   }
   MemoryBlock metadataRoot = this.DirectoryToMemoryBlock(this.COR20Header.MetaDataDirectory);
   if (metadataRoot.Length < this.COR20Header.MetaDataDirectory.Size) {
     this.ErrorContainer.AddDirectoryError(Directories.Cor20HeaderMetaData, 0, MetadataReaderErrorKind.NotEnoughSpaceForMetadataDirectory);
     return false;
   }
   MemoryReader memReader = new MemoryReader(metadataRoot);
   if (
     !this.ReadMetadataHeader(ref memReader)
     || !this.ReadStorageHeader(ref memReader)
     || !this.ReadStreamHeaders(ref memReader)
     || !this.ProcessAndCacheStreams(ref metadataRoot)
   ) {
     return false;
   }
   this.ReaderState = ReaderState.CORModule;
   this.ResourceMemoryReader = new MemoryReader(this.DirectoryToMemoryBlock(this.COR20Header.ResourcesDirectory));
   this.StrongNameSignature = this.DirectoryToMemoryBlock(this.COR20Header.StrongNameSignatureDirectory);
   return true;
 }
 internal MemoryBlock RVAToMemoryBlock(
   int RVA
 )
   //^ requires this.ReaderState >= ReaderState.PEFile;
 {
   foreach (SectionHeader sectionHeaderIter in this.SectionHeaders) {
     if (sectionHeaderIter.VirtualAddress <= RVA && RVA < sectionHeaderIter.VirtualAddress + sectionHeaderIter.VirtualSize) {
       int relativeOffset = RVA - sectionHeaderIter.VirtualAddress;
       MemoryBlock retMemBlock =
         new MemoryBlock(
           this.BinaryDocumentMemoryBlock.Pointer + sectionHeaderIter.OffsetToRawData + relativeOffset,
           sectionHeaderIter.VirtualSize - relativeOffset
         );
       return retMemBlock;
     }
   }
   //  Error section not found...
   return new MemoryBlock();
 }
 internal SubSection RVAToSubSection(
   int RVA,
   int size
 ) {
   foreach (SectionHeader sectionHeaderIter in this.SectionHeaders) {
     if (sectionHeaderIter.VirtualAddress <= RVA && RVA + size <= sectionHeaderIter.VirtualAddress + sectionHeaderIter.VirtualSize) {
       int relativeOffset = RVA - sectionHeaderIter.VirtualAddress;
       MemoryBlock memBlock =
         new MemoryBlock(
           this.BinaryDocumentMemoryBlock.Pointer + sectionHeaderIter.OffsetToRawData + relativeOffset,
           size
         );
       SubSection retSubSection = new SubSection(sectionHeaderIter.Name, relativeOffset, memBlock);
       return retSubSection;
     }
   }
   //  Error section not found...
   return new SubSection();
 }
 private string ReadDebugInformationLocationFromDebugTableDirectoryData() {
   if (this.OptionalHeaderDirectoryEntries.DebugTableDirectory.Size != 0x1c) return string.Empty;
   var debugDirectoryReader = new MemoryReader(this.DirectoryToMemoryBlock(this.OptionalHeaderDirectoryEntries.DebugTableDirectory));
   PeDebugDirectory debugDir = new PeDebugDirectory();
   debugDir.Characteristics = debugDirectoryReader.ReadUInt32();
   debugDir.TimeDateStamp = debugDirectoryReader.ReadUInt32();
   debugDir.MajorVersion = debugDirectoryReader.ReadUInt16();
   debugDir.MinorVersion = debugDirectoryReader.ReadUInt16();
   debugDir.Type = debugDirectoryReader.ReadUInt32();
   debugDir.SizeOfData = debugDirectoryReader.ReadUInt32();
   debugDir.AddressOfRawData = debugDirectoryReader.ReadUInt32();
   debugDir.PointerToRawData = debugDirectoryReader.ReadUInt32();
   if (debugDir.SizeOfData == 0 ) return string.Empty;
   var dataBlock = new MemoryBlock(this.BinaryDocumentMemoryBlock.Pointer + debugDir.PointerToRawData, debugDir.SizeOfData);
   var ptrToDebugInfo = this.BinaryDocumentMemoryBlock.Pointer + debugDir.PointerToRawData;
   var ptrToDebugInfoEnd = this.BinaryDocumentMemoryBlock.Pointer + debugDir.PointerToRawData + debugDir.SizeOfData;
   if (ptrToDebugInfo >= this.BinaryDocumentMemoryBlock.Pointer + this.BinaryDocumentMemoryBlock.Length - 28) {
     //TODO: error
     return string.Empty;
   }
   if (ptrToDebugInfoEnd > this.BinaryDocumentMemoryBlock.Pointer + this.BinaryDocumentMemoryBlock.Length) {
     //TODO: error
     return string.Empty;
   }
   var debugDataReader = new MemoryReader(dataBlock);
   var magic = debugDataReader.ReadUInt32();
   if (magic != 0x53445352) { //RSDS in little endian format
     //TODO: error
     return string.Empty;
   }
   var unknown1 = debugDataReader.ReadUInt32();
   var unknown2 = debugDataReader.ReadUInt32();
   var unknown3 = debugDataReader.ReadUInt32();
   var unknown4 = debugDataReader.ReadUInt32();
   var unknown5 = debugDataReader.ReadUInt32();
   return debugDataReader.ReadASCIINullTerminated();
 }
 internal MemoryBlock DirectoryToMemoryBlock(
   DirectoryEntry directory
 )
   //^ requires this.ReaderState >= ReaderState.PEFile;
 {
   foreach (SectionHeader sectionHeaderIter in this.SectionHeaders) {
     if (sectionHeaderIter.VirtualAddress <= directory.RelativeVirtualAddress && directory.RelativeVirtualAddress < sectionHeaderIter.VirtualAddress + sectionHeaderIter.VirtualSize) {
       int relativeOffset = directory.RelativeVirtualAddress - sectionHeaderIter.VirtualAddress;
       if (directory.Size > sectionHeaderIter.VirtualSize - relativeOffset) {
         //  PEFile Error.
       }
       MemoryBlock retMemBlock =
         new MemoryBlock(
           this.BinaryDocumentMemoryBlock.Pointer + sectionHeaderIter.OffsetToRawData + relativeOffset,
           directory.Size
         );
       return retMemBlock;
     }
   }
   //  Error section not found...
   return new MemoryBlock();
 }
Beispiel #10
0
 bool ReadMetadataRoot() {
   var metadataRoot = new MemoryBlock(this.BinaryDocumentMemoryBlock.Pointer, (int)this.BinaryDocumentMemoryBlock.Length);
   MemoryReader memReader = new MemoryReader(metadataRoot);
   if (
     !this.ReadMetadataHeader(ref memReader)
     || !this.ReadStorageHeader(ref memReader)
     || !this.ReadStreamHeaders(ref memReader)
     || !this.ProcessAndCacheStreams(ref metadataRoot)
   ) {
     return false;
   }
   this.ReaderState = ReaderState.CORModule;
   return true;
 }
Beispiel #11
0
    private PEFileDebugInformation ReadDebugInformationLocationFromDebugTableDirectoryData() {
      if (this.OptionalHeaderDirectoryEntries.DebugTableDirectory.Size != 0x1c) return new PEFileDebugInformation();
      var debugDirectoryReader = new MemoryReader(this.DirectoryToMemoryBlock(this.OptionalHeaderDirectoryEntries.DebugTableDirectory));
      PeDebugDirectory debugDir = new PeDebugDirectory();
      debugDir.Characteristics = debugDirectoryReader.ReadUInt32();
      debugDir.TimeDateStamp = debugDirectoryReader.ReadUInt32();
      debugDir.MajorVersion = debugDirectoryReader.ReadUInt16();
      debugDir.MinorVersion = debugDirectoryReader.ReadUInt16();
      debugDir.Type = debugDirectoryReader.ReadUInt32();
      debugDir.SizeOfData = debugDirectoryReader.ReadUInt32();
      debugDir.AddressOfRawData = debugDirectoryReader.ReadUInt32();
      debugDir.PointerToRawData = debugDirectoryReader.ReadUInt32();
      if (debugDir.SizeOfData == 0) return new PEFileDebugInformation();
      var dataBlock = new MemoryBlock(this.BinaryDocumentMemoryBlock.Pointer + debugDir.PointerToRawData, debugDir.SizeOfData);
      var ptrToDebugInfo = this.BinaryDocumentMemoryBlock.Pointer + debugDir.PointerToRawData;
      var ptrToDebugInfoEnd = this.BinaryDocumentMemoryBlock.Pointer + debugDir.PointerToRawData + debugDir.SizeOfData;
      if (ptrToDebugInfo >= this.BinaryDocumentMemoryBlock.Pointer + this.BinaryDocumentMemoryBlock.Length - 28) {
        //TODO: error
        return new PEFileDebugInformation();
      }
      if (ptrToDebugInfoEnd > this.BinaryDocumentMemoryBlock.Pointer + this.BinaryDocumentMemoryBlock.Length) {
        //TODO: error
        return new PEFileDebugInformation();
      }
      var debugDataReader = new MemoryReader(dataBlock);
      var magic = debugDataReader.ReadUInt32();
      if (magic != 0x53445352) { //RSDS in little endian format
        //TODO: error
        return new PEFileDebugInformation();
      }

      var guid = debugDataReader.PeekGuid(0);
      debugDataReader.SkipBytes(16);
      UInt32 age = debugDataReader.ReadUInt32();
      string fileName = debugDataReader.ReadASCIINullTerminated();

      var guidHex = guid.ToString("N");
      string ageHex = age.ToString("X");
      string version = guidHex + ageHex;

      PEFileDebugInformation information = new PEFileDebugInformation();
      information.Signature = guid;
      information.Age = age;
      information.PdbVersion = version;
      information.PdbFileName = fileName;
      return information;
    }
Beispiel #12
0
     //^ requires this.ReaderState >= ReaderState.PEFile;
     internal MemoryBlock RVAToMemoryBlockWithSize(
   int RVA,
   int size
 )
     {
         foreach (SectionHeader sectionHeaderIter in this.SectionHeaders) {
         if (sectionHeaderIter.VirtualAddress <= RVA && RVA < sectionHeaderIter.VirtualAddress + sectionHeaderIter.VirtualSize) {
           int relativeOffset = RVA - sectionHeaderIter.VirtualAddress;
           if (size > sectionHeaderIter.VirtualSize - relativeOffset) {
         //  PEFile Error.
           }
           MemoryBlock retMemBlock =
         new MemoryBlock(
           this.BinaryDocumentMemoryBlock.Pointer + sectionHeaderIter.OffsetToRawData + relativeOffset,
           (int)size
         );
           return retMemBlock;
         }
           }
           //  Error section not found...
           return new MemoryBlock();
     }
 internal MemoryReader(
   MemoryBlock memBlock
 )
   : this(memBlock.Buffer, memBlock.Length, 0)
   //^ requires memBlock.IsValid;
 {
 }
Beispiel #14
0
 internal EnumberableMemoryBlockWrapper(
     MemoryBlock memBlock
     )
 {
     this.MemBlock = memBlock;
 }