示例#1
0
 public static bool IsNativeAssembly(string path)
 {
     if (String.IsNullOrEmpty(path))
     {
         throw new ArgumentNullException(nameof(path));
     }
     if (String.Equals(Path.GetExtension(path), ".manifest", StringComparison.Ordinal))
     {
         return(true);
     }
     return(EmbeddedManifestReader.Read(path) != null);
 }
示例#2
0
        private static Manifest ReadEmbeddedManifest(string path)
        {
            Stream m = EmbeddedManifestReader.Read(path);

            if (m == null)
            {
                return(null);
            }

            Util.WriteLogFile(Path.GetFileNameWithoutExtension(path) + ".embedded.xml", m);
            Manifest manifest = ReadManifest(m, false);

            manifest.SourcePath = path;
            return(manifest);
        }
示例#3
0
        /// <summary>
        /// Obtains identity of the specified native assembly.
        /// File must be either a PE with an embedded xml manifest, or a stand-alone xml manifest file.
        /// Returns null if identity could not be obtained.
        /// </summary>
        /// <param name="path">The name of the file from which the identity is to be obtained.</param>
        /// <returns>The assembly identity of the specified file.</returns>
        public static AssemblyIdentity FromNativeAssembly(string path)
        {
            if (!FileSystems.Default.FileExists(path))
            {
                return(null);
            }

            if (PathUtil.IsPEFile(path))
            {
                Stream m = EmbeddedManifestReader.Read(path);
                if (m == null)
                {
                    return(null);
                }
                return(FromManifest(m));
            }
            return(FromManifest(path));
        }
示例#4
0
        public static Stream Read(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (!path.EndsWith(".manifest", StringComparison.Ordinal) && !path.EndsWith(".dll", StringComparison.Ordinal))
            {
                // Everything that does not end with .dll or .manifest is not a valid native assembly (this includes
                //    EXEs with ID1 manifest)
                return(null);
            }

            int t1 = Environment.TickCount;
            EmbeddedManifestReader r = new EmbeddedManifestReader(path);

            Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "EmbeddedManifestReader.Read t={0}", Environment.TickCount - t1));
            return(r._manifest);
        }
示例#5
0
        private static XmlDocument GetXmlDocument(string path)
        {
            using (Stream s = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] buffer = new byte[2];
                s.Read(buffer, 0, 2);
                s.Position = 0;
                var document   = new XmlDocument();
                var xrSettings = new XmlReaderSettings {
                    DtdProcessing = DtdProcessing.Ignore
                };
                // if first two bytes are "MZ" then we're looking at an .exe or a .dll not a .manifest
                if ((buffer[0] == 0x4D) && (buffer[1] == 0x5A))
                {
                    Stream m = EmbeddedManifestReader.Read(path);
                    if (m == null)
                    {
                        throw new BadImageFormatException(null, path);
                    }

                    using (XmlReader xr = XmlReader.Create(m, xrSettings))
                    {
                        document.Load(xr);
                    }
                }
                else
                {
                    using (XmlReader xr = XmlReader.Create(s, xrSettings))
                    {
                        document.Load(xr);
                    }
                }

                return(document);
            }
        }