private TypeRef([NotNull] string typeName, [CanBeNull] AssemblyRef assembly)
    {
      Assert.ArgumentNotNull(typeName, "typeName");

      TypeName = typeName;
      Assembly = assembly ?? AssemblyRef.Empty;
    }
Example #2
0
		public Csvm(IDeobfuscatorContext deobfuscatorContext, ModuleDefMD module, Csvm oldOne) {
			this.deobfuscatorContext = deobfuscatorContext;
			this.module = module;
			if (oldOne.resource != null)
				this.resource = (EmbeddedResource)module.Resources[oldOne.module.Resources.IndexOf(oldOne.resource)];
			if (oldOne.vmAssemblyRef != null)
				this.vmAssemblyRef = module.ResolveAssemblyRef(oldOne.vmAssemblyRef.Rid);
		}
		public AssemblyReferenceNodeImpl(ITreeNodeGroup treeNodeGroup, ModuleDef asmRefOwnerModule, AssemblyRef assemblyRef) {
			TreeNodeGroup = treeNodeGroup;
			this.asmRefOwnerModule = new WeakReference(asmRefOwnerModule);
			// Make sure we don't hold on to the original reference since it could prevent GC of the
			// owner module.
			AssemblyRef = assemblyRef.ToAssemblyRef();
			AssemblyRef.Rid = assemblyRef.Rid;
		}
 public TypeDefOrRefSig FindOrCreateTypeRef(ModuleDef module, AssemblyRef asmRef, string ns, string name, bool isValueType)
 {
     var typeRef = module.UpdateRowId(new TypeRefUser(module, ns, name, asmRef));
     if (isValueType)
         return new ValueTypeSig(typeRef);
     
     return new ClassSig(typeRef);
 }
Example #5
0
		static MModule GetModule(AssemblyRef asmRef) {
			foreach (var mm in modules.Values) {
				var asm = mm.moduleDef.Assembly;
				if (asm != null && asm.FullName == asmRef.FullName)
					return mm;
			}
			return null;
		}
Example #6
0
		static void OverwriteAssembly(AssemblyRef dst, AssemblyRef src) {
			dst.Name = src.Name;
			dst.Version = src.Version;
			dst.PublicKeyOrToken = src.PublicKeyOrToken;
			dst.Culture = src.Culture;
			dst.Attributes = src.Attributes;
			dst.Hash = src.Hash;
		}
 public AssemblyReferenceTreeNode(AssemblyRef r, AssemblyTreeNode parentAssembly)
 {
     if (parentAssembly == null)
         throw new ArgumentNullException("parentAssembly");
     if (r == null)
         throw new ArgumentNullException("r");
     this.r = r;
     this.parentAssembly = parentAssembly;
     this.LazyLoading = true;
 }
		internal AssemblyReferenceTreeNode(AssemblyRef r, AssemblyTreeNode parentAssembly, DnSpyFileListTreeNode dnSpyFileListTreeNode) {
			if (parentAssembly == null)
				throw new ArgumentNullException("parentAssembly");
			if (dnSpyFileListTreeNode == null)
				throw new ArgumentNullException("dnSpyFileListTreeNode");
			if (r == null)
				throw new ArgumentNullException("r");
			this.r = r;
			this.dnSpyFileListTreeNode = dnSpyFileListTreeNode;
			this.parentAssembly = parentAssembly;
			this.LazyLoading = true;
		}
Example #9
0
		private ISymbolWriter LoadSymbolWriter(AssemblyDefinition assembly, AssemblyRef output)
		{
			// TODO: Get symbol writing to work.
//			ISymbolWriterProvider symProv = null;
//			if (this.usingMdb) {
//				symProv = new Mono.Cecil.Mdb.MdbWriterProvider ();
//			} else if (this.usingPdb) {
//				symProv = new Mono.Cecil.Pdb.PdbWriterProvider ();
//			} else {
//				this.warnings.Add ("-writePDBFile specified, but no symbol file found, cannot write symbols.");
//			}
//			if (symProv != null) {
//				return output.IsFilename ?
//					symProv.GetSymbolWriter (assembly.MainModule, output.Filename) :
//					symProv.GetSymbolWriter (assembly.MainModule, output.Streams.Symbols);
//			}
			return null;
		}
Example #10
0
		public ResourceOptions(Resource resource) {
			this.ResourceType = resource.ResourceType;
			this.Name = resource.Name ?? UTF8String.Empty;
			this.Attributes = resource.Attributes;
			switch (resource.ResourceType) {
			case ResourceType.Embedded:
				break;

			case ResourceType.AssemblyLinked:
				this.Assembly = ((AssemblyLinkedResource)resource).Assembly;
				break;

			case ResourceType.Linked:
				this.File = ((LinkedResource)resource).File;
				break;

			default:
				throw new InvalidOperationException();
			}
		}
Example #11
0
 static bool IsAssemblyRef(AssemblyRef asmRef, UTF8String name, PublicKeyToken token)
 {
     if (asmRef.Name != name)
         return false;
     var pkot = asmRef.PublicKeyOrToken;
     if (pkot == null)
         return false;
     return token.Equals(pkot.Token);
 }
Example #12
0
 /// <summary>
 /// Add an external assembly to this PEFile (.assembly extern)
 /// </summary>
 /// <param name="assemName">the external assembly name</param>
 /// <returns>a descriptor for this external assembly</returns>
 public AssemblyRef AddExternAssembly(string assemName) {
   if (assemName.CompareTo(mscorlibName) == 0) return metaData.mscorlib;
   AssemblyRef anAssem = new AssemblyRef(metaData,assemName);
   metaData.AddToTable(MDTable.AssemblyRef,anAssem);
   // Console.WriteLine("Adding assembly " + assemName);
   return anAssem;
 }
Example #13
0
 /// <summary>
 /// Add a managed resource from another assembly.
 /// </summary>
 /// <param name="resName">The name of the resource</param>
 /// <param name="assem">The assembly where the resource is</param>
 /// <param name="isPublic">Access for the resource</param>
 public void AddExternalManagedResource (string resName, AssemblyRef assem, uint flags) {
   resources.Add (new ManifestResource (resName, flags, assem));
 }
		public ClassSig Type(string ns, string name, AssemblyRef asmRef) {
			return (ClassSig)Type(false, ns, name, asmRef);
		}
Example #15
0
 public void Write(ISyntaxHighlightOutput output, ILanguage language, AssemblyRef asmRef, bool showToken)
 {
     output.Write(NameUtils.CleanIdentifier(asmRef.Name), TextTokenKind.Text);
     WriteToken(output, asmRef, showToken);
 }
Example #16
0
		public void Find() {
			resource = FindCsvmResource();
			vmAssemblyRef = FindVmAssemblyRef();
		}
Example #17
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="name">Name of resource</param>
		/// <param name="asmRef">Assembly reference</param>
		/// <param name="flags">Resource flags</param>
		public AssemblyLinkedResource(UTF8String name, AssemblyRef asmRef, ManifestResourceAttributes flags)
			: base(name, flags) {
			if (asmRef == null)
				throw new ArgumentNullException("asmRef");
			this.asmRef = asmRef;
		}
Example #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeRef"/> class.
 /// </summary>
 /// <param name="assembly">The belonging assembly of this type reference, or <c>null</c> if it is mscorlib.</param>
 /// <param name="name">The value of <see cref="Type.FullName"/>, or <c>null</c> if referring type is <see cref="Object"/>.</param>
 public TypeRef(AssemblyRef assembly, String name)
     : this()
 {
     this.Assembly = assembly;
     this.Name = name;
 }
		public ManifestResource(string name, uint flags, AssemblyRef assemRef) 
		{
			InitResource (name, flags);
			rRef = assemRef;
		}
Example #20
0
 internal static AssemblyRef ReadAssemblyRef(PEReader buff)
 {
     buff.SetElementPosition(MDTable.Assembly, 1);
     HashAlgorithmType hAlg = (HashAlgorithmType)buff.ReadUInt32();
     ushort majVer = buff.ReadUInt16();
     ushort minVer = buff.ReadUInt16();
     ushort bldNo = buff.ReadUInt16();
     ushort revNo = buff.ReadUInt16();
     uint flags = buff.ReadUInt32();
     byte[] pKey = buff.GetBlob();
     string name = buff.GetString();
     string cult = buff.GetString();
     AssemblyRef assemRef;
     if (name.ToLower() == "mscorlib")
     {
         assemRef = MSCorLib.mscorlib;
         assemRef.AddVersionInfo(majVer, minVer, bldNo, revNo);
         if (pKey.Length > 8) assemRef.AddKey(pKey);
         else assemRef.AddKeyToken(pKey);
         assemRef.AddCulture(cult);
         assemRef.SetFlags(flags);
     }
     else
     {
         assemRef = new AssemblyRef(name, majVer, minVer, bldNo, revNo, flags, pKey, cult, null);
     }
     //AssemblyRef assemRef = new AssemblyRef(name,majVer,minVer,bldNo,revNo,flags,pKey,cult,null);
     assemRef.ReadAsDef();
     return assemRef;
 }
Example #21
0
 /// <summary>
 /// Make an AssemblyRef descriptor for this Assembly
 /// </summary>
 /// <returns>AssemblyRef descriptor for this Assembly</returns>
 public AssemblyRef MakeRefOf()
 {
     if (refOf == null)
     {
         refOf = new AssemblyRef(name, majorVer, minorVer, buildNo, revisionNo,
             flags, publicKey, culture, null);
     }
     return refOf;
 }
		public ValueTypeSig ValueType(string ns, string name, AssemblyRef asmRef) {
			return (ValueTypeSig)Type(true, ns, name, asmRef);
		}
		TypeDef Resolve(AssemblyRef asmRef, TypeRef typeRef) {
			var asm = ownerModule.Assembly;
			if (asm == null)
				return null;
			if (asmRef.FullName != asm.GetFullNameWithPublicKey() && asmRef.FullName != asm.GetFullNameWithPublicKeyToken())
				return null;
			var td = typeRef.Resolve();
			return td != null && td.Module == ownerModule ? td : null;
		}
Example #24
0
        private void ParseTables()
        {
            byte[] buffer = new byte[256];
            file.Seek(metadata.StreamHeaders[meta_table_index].Offset, SeekOrigin.Begin);

            file.Read(buffer, 0, 4); // Reserved DWORD = 0
            file.Read(buffer, 0, 1); // Major version
            file.Read(buffer, 0, 1); // Minor version
            file.Read(buffer, 0, 1); // Heap offset sizes
            char heap_offset_sizes = BitConverter.ToChar(buffer, 0);
            file.Read(buffer, 0, 1); // Reserved byte = 0

            string_indices_are_dword = ((heap_offset_sizes & 1) != 0);
            guid_indices_are_dword = ((heap_offset_sizes & 2) != 0);
            blob_indices_are_dword = ((heap_offset_sizes & 4) != 0);

            UInt64 table_mask = 0;
            file.Read(buffer, 0, 8);
            table_mask = BitConverter.ToUInt64(buffer, 0);

            UInt64 sorted_mask = 0;
            file.Read(buffer, 0, 8);
            sorted_mask = BitConverter.ToUInt64(buffer, 0);

            // Count the tables
            int num_tables = 0;
            for (int i = 0; i < 64; ++i)
            {
                if ((table_mask & ((ulong)1 << i)) != 0) ++num_tables;
            }

            // Get the sizes
            UInt32[] table_sizes = new UInt32[64];
            int highest_used_table = 0;
            for (int i = 0; i < 64; ++i)
            {
                uint size = 0;
                if ((table_mask & ((ulong)1 << i)) != 0)
                {
                    file.Read(buffer, 0, 4);
                    size = BitConverter.ToUInt32(buffer, 0);
                    highest_used_table = i;
                }
                table_sizes[i] = size;
            }

            ComputeIndexSizes(table_sizes);

            // Process the tables
            for (int i = 0; i <= highest_used_table; ++i)
            {
                if ((table_mask & ((ulong)1 << i)) != 0)
                {
                    uint num_entries = table_sizes[i];
                    for (int j = 0; j < num_entries; ++j)
                    {
                        switch (i)
                        {
                            case 0: // Modules
                                file.Read(buffer, 0, 2); // Generation = 0
                                uint index = ReadStringIndex(); // Name
                                Name = ReadStringByIndex(index);
                                ReadGUIDIndex(); // MVID
                                ReadGUIDIndex(); // EncID
                                ReadGUIDIndex(); // EncBaseID
                                break;
                            case 1: // TypeRef
                                file.Read(buffer, 0, size_resolution_scope);
                                ReadStringIndex(); // TypeName
                                ReadStringIndex(); // TypeNamespace
                                break;
                            case 2: // TypeDef
                                file.Read(buffer, 0, 4); // Flags
                                ReadStringIndex(); // TypeName
                                ReadStringIndex(); // TypeNamespace
                                file.Read(buffer, 0, size_type_def_of_ref); // Extends
                                file.Read(buffer, 0, size_field_index);// FieldList
                                file.Read(buffer, 0, size_method_index);// MethodList
                                break;
                            case 4: // Field
                                file.Read(buffer, 0, 2); // Flags
                                ReadStringIndex(); // Name
                                ReadBlobIndex(); // Signature
                                break;
                            case 6: // MethodDef
                                file.Read(buffer, 0, 4); // RVA
                                file.Read(buffer, 0, 2); // ImplFlags
                                file.Read(buffer, 0, 2); // Flags
                                ReadStringIndex(); // Name
                                ReadBlobIndex(); // Signature
                                file.Read(buffer, 0, size_param_index); // ParamList
                                break;
                            case 8: // Param
                                file.Read(buffer, 0, 2); // Flags
                                file.Read(buffer, 0, 2); // Sequence
                                ReadStringIndex(); // Name
                                break;
                            case 9: // InterfaceImpl
                                file.Read(buffer, 0, size_type_def_index); // Class
                                file.Read(buffer, 0, size_type_def_of_ref); // Interface
                                break;
                            case 10: // MemberRef
                                file.Read(buffer, 0, size_member_ref_parent); // Class
                                ReadStringIndex(); // Name
                                ReadBlobIndex(); // Index
                                break;
                            case 11: // Constant
                                file.Read(buffer, 0, 2); // Type
                                file.Read(buffer, 0, size_has_constant); // Parent
                                ReadBlobIndex(); // Value
                                break;
                            case 12: // CustomAttribute
                                file.Read(buffer, 0, size_has_custom_attribute); // Parent
                                file.Read(buffer, 0, size_custom_attribute_type); // Type
                                ReadBlobIndex(); // Value
                                break;
                            case 13: // FieldMarshall
                                file.Read(buffer, 0, size_has_field_marshall); // Parent
                                ReadBlobIndex(); // NativeType
                                break;
                            case 14: // DeclSecurity
                                file.Read(buffer, 0, 2); // Action
                                file.Read(buffer, 0, size_has_decl_security); // Parent
                                ReadBlobIndex(); // PermissionSet
                                break;
                            case 15: // ClassLayout
                                file.Read(buffer, 0, 2); // PackingSize
                                file.Read(buffer, 0, 4); // ClassSize
                                file.Read(buffer, 0, size_type_def_index); // Parent
                                break;
                            case 16: // FieldLayout
                                file.Read(buffer, 0, 4); // Offset
                                file.Read(buffer, 0, size_field_index); // Field
                                break;
                            case 17: // StandaloneSig
                                ReadBlobIndex(); // Signature
                                break;
                            case 18: // EventMapTable
                                file.Read(buffer, 0, size_type_def_index); // Parent
                                file.Read(buffer, 0, size_event_index); // EventList
                                break;
                            case 20: // Event
                                file.Read(buffer, 0, 2); // EventFlag
                                ReadStringIndex(); // Name
                                file.Read(buffer, 0, size_type_def_of_ref); // EventType
                                break;
                            case 21: // PropertyMap
                                file.Read(buffer, 0, size_type_def_index); // Parent
                                file.Read(buffer, 0, size_property_index); // Parent
                                break;
                            case 23: // PropertyTable
                                file.Read(buffer, 0, 2); // Flags
                                ReadStringIndex(); // Name
                                ReadBlobIndex(); // Type
                                break;
                            case 24: // MethodSemantics
                                file.Read(buffer, 0, 2); // Semantics
                                file.Read(buffer, 0, size_method_index); // Method
                                file.Read(buffer, 0, size_has_semantic); // Association
                                break;
                            case 25: // MethodImplTable
                                file.Read(buffer, 0, size_type_def_index); // Class
                                file.Read(buffer, 0, size_method_def_or_ref); // MethodBody
                                file.Read(buffer, 0, size_method_def_or_ref); // MethodDeclaration
                                break;
                            case 26: // ModuleRef
                                ReadStringIndex(); // Name
                                break;
                            case 27: // TypeSpec
                                ReadBlobIndex(); // Signature
                                break;
                            case 28: // ImplMap
                                file.Read(buffer, 0, 2); // MappingFlags
                                file.Read(buffer, 0, size_member_forwarded); // MemberForwarded
                                ReadStringIndex(); // ImportName
                                file.Read(buffer, 0, size_module_ref_index); // ImportScope
                                break;
                            case 29: // FieldRVA
                                file.Read(buffer, 0, 4); // RVA
                                file.Read(buffer, 0, size_field_index); // Field
                                break;
                            case 32: // Assembly
                                file.Read(buffer, 0, 4); // HashAlgId
                                file.Read(buffer, 0, 2); // MajVersion
                                file.Read(buffer, 0, 2); // MinVersion
                                file.Read(buffer, 0, 2); // BuildNumber
                                file.Read(buffer, 0, 2); // RevisionNumber
                                file.Read(buffer, 0, 4); // Flags
                                AssemblyFlags = BitConverter.ToUInt32(buffer, 0);
                                AssemblyFlagsOffset = file.Position - 4;
                                AssemblyPublicKeyIndexOffset = file.Position;
                                AssemblyPublicKeyIndex = ReadBlobIndex(); // Public Key
                                ReadStringIndex(); // Name
                                ReadStringIndex(); // Culture

                                // Get Public Key
                                long save_pos = file.Position;
                                file.Seek(metadata.StreamHeaders[blob_table_index].Offset + AssemblyPublicKeyIndex, SeekOrigin.Begin);
                                PublicKey = ReadPublicKeyToken((AssemblyFlags & 1) != 0);
                                file.Seek(save_pos, SeekOrigin.Begin);
                                break;
                            case 33: // AssemblyProcessor
                                file.Read(buffer, 0, 4); // Processor
                                break;
                            case 34: // AssemblyOS
                                file.Read(buffer, 0, 4); // OSPlatformID
                                file.Read(buffer, 0, 4); // OSMajorVersion
                                file.Read(buffer, 0, 4); // OSMinorVersion
                                break;
                            case 35: // AssemblyRef
                                long base_offset = file.Position;
                                file.Read(buffer, 0, 12);

                                AssemblyRef assembly_ref = new AssemblyRef();
                                assembly_ref.VersionOffset = base_offset;
                                assembly_ref.MajVersion = BitConverter.ToUInt16(buffer, 0);
                                assembly_ref.MinVersion = BitConverter.ToUInt16(buffer, 2);
                                assembly_ref.BuildNumber = BitConverter.ToUInt16(buffer, 4);
                                assembly_ref.RevisionNumber = BitConverter.ToUInt16(buffer, 6);
                                assembly_ref.FlagsOffset = base_offset + 8;
                                assembly_ref.Flags = BitConverter.ToUInt32(buffer, 8);
                                assembly_ref.PublicKeyOrTokenOffset = base_offset + 12;
                                assembly_ref.PublicKeyOrToken = ReadBlobIndex();
                                assembly_ref.NameStringIndexOffset = base_offset + 12 + (blob_indices_are_dword ? 4 : 2);
                                assembly_ref.NameStringIndex = ReadStringIndex();
                                assembly_ref.CultureStringIndex = ReadStringIndex();
                                assembly_ref.HashValueIndex = ReadBlobIndex();

                                if (assembly_ref.NameStringIndex != 0)
                                {
                                    assembly_ref.Name = ReadStringByIndex(assembly_ref.NameStringIndex);
                                }
                                else
                                {
                                    assembly_ref.Name = "";
                                }
                                if (assembly_ref.CultureStringIndex != 0)
                                {
                                    assembly_ref.Culture = ReadStringByIndex(assembly_ref.CultureStringIndex);
                                }
                                else
                                {
                                    assembly_ref.Culture = "";
                                }

                                // Get Public Key
                                save_pos = file.Position;
                                assembly_ref.PublicKeyOffset = metadata.StreamHeaders[blob_table_index].Offset + assembly_ref.PublicKeyOrToken;
                                file.Seek(assembly_ref.PublicKeyOffset, SeekOrigin.Begin);
                                assembly_ref.PublicKey = ReadPublicKeyToken((assembly_ref.Flags & 1) != 0);
                                file.Seek(save_pos, SeekOrigin.Begin);

                                references.Add(assembly_ref);
                                break;
                            case 36: // AssemblyRefProcessor
                                file.Read(buffer, 0, 4); // Processor
                                file.Read(buffer, 0, size_assembly_ref_index); // AssemblyRef
                                break;
                            case 37: // AssemblyRefOS
                                file.Read(buffer, 0, 4); // OSPlatformId
                                file.Read(buffer, 0, 4); // OSMajorVersion
                                file.Read(buffer, 0, 4); // OSMinorVersion
                                file.Read(buffer, 0, size_assembly_ref_index); // AssemblyRef
                                break;
                            case 38: // File
                                file.Read(buffer, 0, 4); // Flags
                                ReadStringIndex(); // Name
                                ReadBlobIndex(); // HashValue
                                break;
                            case 39: // Exported Type
                                file.Read(buffer, 0, 4); // Flags
                                file.Read(buffer, 0, size_type_def_index); // TypeDefId
                                ReadStringIndex(); // TypeName
                                ReadStringIndex(); // TypeNamespace
                                file.Read(buffer, 0, size_implementation); // Implementation
                                break;
                            case 40: // ManifestResource
                                file.Read(buffer, 0, 4); // Offset
                                file.Read(buffer, 0, 4); // Flags
                                ReadStringIndex(); // Name
                                file.Read(buffer, 0, size_implementation); // Implementation
                                break;
                            case 41:
                                file.Read(buffer, 0, size_type_def_index); // NestedClass
                                file.Read(buffer, 0, size_type_def_index); // EncodingClass
                                break;
                        }
                    }
                }
            }
        }
Example #25
0
        public TreeNode NewAssemblyRef(AssemblyRef assemblyRef)
        {
            TreeNode node = NewNode(assemblyRef.FullName, false);
            node.Tag = assemblyRef;
            node.ImageIndex = node.SelectedImageIndex = 0;

            return node;
        }
Example #26
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="assembly">Target assembly</param>
		/// <param name="namespace">Namespace</param>
		public NamespaceReference(IAssembly assembly, string @namespace) {
			if (assembly == null)
				throw new ArgumentNullException(nameof(assembly));
			Assembly = assembly.ToAssemblyRef();
			Namespace = @namespace;
		}
Example #27
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="module">The owner module</param>
		/// <param name="corLibAssemblyRef">Corlib assembly reference or <c>null</c> if a default
		/// assembly reference should be created</param>
		public CorLibTypes(ModuleDef module, AssemblyRef corLibAssemblyRef) {
			this.module = module;
			this.corLibAssemblyRef = corLibAssemblyRef ?? CreateCorLibAssemblyRef();
			Initialize();
		}
		static bool IsGreaterAssemblyRefVersion(AssemblyRef found, AssemblyRef newOne) {
			if (found == null)
				return true;
			var foundVer = found.Version;
			var newVer = newOne.Version;
			return foundVer == null || (newVer != null && newVer >= foundVer);
		}
		internal SystemClass(PrimitiveType eType, AssemblyRef paren, MetaData md)
			: base("System",eType.GetName(),md) {
				elemType = eType;
				parent = paren;
			}
Example #30
0
			public AssemblyRefInfo(AssemblyRef asmRef) {
				this.AssemblyRef = asmRef;
				this.OrigName = asmRef.Name;
				this.OrigPublicKeyOrToken = asmRef.PublicKeyOrToken;
			}