Ejemplo n.º 1
0
        /// <summary>
        /// Reads the specified manifest XML and returns an object representation.
        /// </summary>
        /// <param name="manifestType">Specifies the expected type of the manifest. Valid values are "AssemblyManifest", "ApplicationManifest", or "DepoyManifest".</param>
        /// <param name="input">Specifies an input stream.</param>
        /// <param name="preserveStream">Specifies whether to preserve the input stream in the InputStream property of the resulting manifest object. Used by ManifestWriter to reconstitute input which is not represented in the object representation.</param>
        /// <returns>A base object representation of the manifest. Can be cast to AssemblyManifest, ApplicationManifest, or DeployManifest to access more specific functionality.</returns>
        public static Manifest ReadManifest(string manifestType, Stream input, bool preserveStream)
        {
            int          t1       = Environment.TickCount;
            const string resource = "read2.xsl";
            Manifest     m;
            Stream       s;

            if (manifestType != null)
            {
                DictionaryEntry arg = new DictionaryEntry("manifest-type", manifestType);
                s = XmlUtil.XslTransform(resource, input, arg);
            }
            else
            {
                s = XmlUtil.XslTransform(resource, input);
            }

            try
            {
                s.Position = 0;
                m          = Deserialize(s);
                if (m.GetType() == typeof(ApplicationManifest))
                {
                    var am = (ApplicationManifest)m;
                    am.TrustInfo = new TrustInfo();
                    am.TrustInfo.ReadManifest(input);
                }
                if (preserveStream)
                {
                    input.Position = 0;
                    m.InputStream  = new MemoryStream();
                    Util.CopyStream(input, m.InputStream);
                }
                s.Position = 0;
                string n = m.AssemblyIdentity.GetFullName(AssemblyIdentity.FullNameFlags.All);
                if (String.IsNullOrEmpty(n))
                {
                    n = m.GetType().Name;
                }
                Util.WriteLogFile(n + ".read.xml", s);
            }
            finally
            {
                s.Close();
            }
            Util.WriteLog(String.Format(CultureInfo.InvariantCulture, "ManifestReader.ReadManifest t={0}", Environment.TickCount - t1));
            m.OnAfterLoad();
            return(m);
        }
Ejemplo n.º 2
0
        public static Manifest ReadManifest(string manifestType, Stream input, bool preserveStream)
        {
            Stream   stream;
            int      tickCount = Environment.TickCount;
            string   resource  = "read2.xsl";
            Manifest manifest  = null;

            if (manifestType != null)
            {
                DictionaryEntry   entry   = new DictionaryEntry("manifest-type", manifestType);
                DictionaryEntry[] entries = new DictionaryEntry[] { entry };
                stream = XmlUtil.XslTransform(resource, input, entries);
            }
            else
            {
                stream = XmlUtil.XslTransform(resource, input, new DictionaryEntry[0]);
            }
            try
            {
                stream.Position = 0L;
                manifest        = Deserialize(stream);
                if (manifest.GetType() == typeof(ApplicationManifest))
                {
                    ApplicationManifest manifest2 = (ApplicationManifest)manifest;
                    manifest2.TrustInfo = new TrustInfo();
                    manifest2.TrustInfo.ReadManifest(input);
                }
                if (preserveStream)
                {
                    input.Position       = 0L;
                    manifest.InputStream = new MemoryStream();
                    Util.CopyStream(input, manifest.InputStream);
                }
                stream.Position = 0L;
                string fullName = manifest.AssemblyIdentity.GetFullName(AssemblyIdentity.FullNameFlags.All);
                if (string.IsNullOrEmpty(fullName))
                {
                    fullName = manifest.GetType().Name;
                }
                Util.WriteLogFile(fullName + ".read.xml", stream);
            }
            finally
            {
                stream.Close();
            }
            Util.WriteLog(string.Format(CultureInfo.InvariantCulture, "ManifestReader.ReadManifest t={0}", new object[] { Environment.TickCount - tickCount }));
            manifest.OnAfterLoad();
            return(manifest);
        }
        public static Stream XslTransform(string resource, Stream input, params DictionaryEntry[] entries)
        {
            int           tickCount = Environment.TickCount;
            Stream        embeddedResourceStream = Util.GetEmbeddedResourceStream(resource);
            int           num2       = Environment.TickCount;
            XPathDocument stylesheet = new XPathDocument(embeddedResourceStream);

            Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "new XPathDocument(1) t={1}", new object[] { resource, Environment.TickCount - num2 }));
            int num3 = Environment.TickCount;

            System.Xml.Xsl.XslTransform transform = new System.Xml.Xsl.XslTransform();
            transform.Load(stylesheet, resolver, evidence);
            Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "XslTransform.Load t={1}", new object[] { resource, Environment.TickCount - num3 }));
            MemoryStream output = new MemoryStream();

            Util.CopyStream(input, output);
            int           num4      = Environment.TickCount;
            XPathDocument document2 = new XPathDocument(output);

            Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "new XPathDocument(2) t={1}", new object[] { resource, Environment.TickCount - num4 }));
            XsltArgumentList args = null;

            if (entries.Length > 0)
            {
                args = new XsltArgumentList();
                foreach (DictionaryEntry entry in entries)
                {
                    string name      = entry.Key.ToString();
                    object parameter = entry.Value.ToString();
                    args.AddParam(name, "", parameter);
                    Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "arg: key='{0}' value='{1}'", new object[] { name, parameter.ToString() }));
                }
            }
            MemoryStream  w      = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(w, Encoding.UTF8);

            writer.WriteStartDocument();
            int num1 = Environment.TickCount;

            transform.Transform((IXPathNavigable)document2, args, (XmlWriter)writer, resolver);
            Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "XslTransform.Transform t={1}", new object[] { resource, Environment.TickCount - num4 }));
            writer.WriteEndDocument();
            writer.Flush();
            w.Position = 0L;
            Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "XslTransform(\"{0}\") t={1}", new object[] { resource, Environment.TickCount - tickCount }));
            return(w);
        }
Ejemplo n.º 4
0
        public static Stream XslTransform(string resource, Stream input, params DictionaryEntry[] entries)
        {
            int t1 = Environment.TickCount;

            Stream s = Util.GetEmbeddedResourceStream(resource);

            int           t2 = Environment.TickCount;
            XPathDocument d  = new XPathDocument(s);

            Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "new XPathDocument(1) t={0}", Environment.TickCount - t2));

            int t3 = Environment.TickCount;
            XslCompiledTransform xslc = new XslCompiledTransform();

            // Using the Trusted Xslt is fine as the style sheet comes from our own assemblies.
            // This is similar to the prior this.GetType().Assembly/Evidence method that was used in the now depricated XslTransform.
            xslc.Load(d, XsltSettings.TrustedXslt, s_resolver);
            Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "XslCompiledTransform.Load t={0}", Environment.TickCount - t3));

            // Need to copy input stream because XmlReader will close it,
            // causing errors for later callers that access the same stream
            MemoryStream clonedInput = new MemoryStream();

            Util.CopyStream(input, clonedInput);

            int       t4  = Environment.TickCount;
            XmlReader xml = XmlReader.Create(clonedInput);

            Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "new XmlReader(2) t={0}", Environment.TickCount - t4));

            XsltArgumentList args = null;

            if (entries.Length > 0)
            {
                args = new XsltArgumentList();
                foreach (DictionaryEntry entry in entries)
                {
                    string key = entry.Key.ToString();
                    object val = entry.Value.ToString();
                    args.AddParam(key, "", val);
                    Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "arg: key='{0}' value='{1}'", key, val.ToString()));
                }
            }

            MemoryStream  m = new MemoryStream();
            XmlTextWriter w = new XmlTextWriter(m, Encoding.UTF8);

            w.WriteStartDocument();

            int t5 = Environment.TickCount;

            xslc.Transform(xml, args, w, s_resolver);
            Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "XslCompiledTransform.Transform t={0}", Environment.TickCount - t4));

            w.WriteEndDocument();
            w.Flush();
            m.Position = 0;

            Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "XslCompiledTransform(\"{0}\") t={1}", resource, Environment.TickCount - t1));
            return(m);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Writes the application trust to an XML file.
        /// </summary>
        /// <param name="output"></param>
        public void Write(Stream output)
        {
            XmlDocument outputDocument            = new XmlDocument();
            XmlElement  inputPermissionSetElement = GetInputPermissionSetElement();

            //NOTE: XmlDocument.ImportNode munges "xmlns:asmv2" to "xmlns:d1p1" for some reason, use XmlUtil.CloneElementToDocument instead
            XmlElement outputPermissionSetElement = XmlUtil.CloneElementToDocument(inputPermissionSetElement, outputDocument, XmlNamespaces.asmv2);

            outputDocument.AppendChild(outputPermissionSetElement);

            string tempPrivilegeDocument = null;

            XmlDocument privilegeDocument = new XmlDocument();
            XmlElement  inputRequestedPrivilegeElement = GetInputRequestedPrivilegeElement();
            XmlElement  requestedPrivilegeElement      = null;

            requestedPrivilegeElement = GetRequestedPrivilegeElement(inputRequestedPrivilegeElement, privilegeDocument);

            if (requestedPrivilegeElement != null)
            {
                privilegeDocument.AppendChild(requestedPrivilegeElement);

                MemoryStream p = new MemoryStream();
                privilegeDocument.Save(p);
                p.Position            = 0;
                tempPrivilegeDocument = Util.WriteTempFile(p);
            }

            try
            {
                string trustInfoResource2 = "trustinfo2.xsl";

                // If permission set was not altered, just write out what was read in...
                MemoryStream m = new MemoryStream();
                if (_outputPermissionSet == null && !_sameSiteChanged)
                {
                    XmlElement permissionSetElement = outputDocument.DocumentElement;
                    FixupPermissionSetElement(permissionSetElement);

                    outputDocument.Save(m);
                    m.Position = 0;
                }
                else
                {
                    XmlDocument permissionSetDocument = GetOutputPermissionSetDocument();
                    XmlElement  permissionSetElement  = permissionSetDocument.DocumentElement;
                    FixupPermissionSetElement(permissionSetElement);

                    if (outputDocument.DocumentElement == null)
                    {
                        permissionSetDocument.Save(m);
                        m.Position = 0;
                    }
                    else
                    {
                        XmlElement oldPermissionSetElement = outputDocument.DocumentElement;
                        XmlElement newPermissionSetElement = (XmlElement)outputDocument.ImportNode(permissionSetElement, true);
                        oldPermissionSetElement.ParentNode.ReplaceChild(newPermissionSetElement, oldPermissionSetElement);

                        outputDocument.Save(m);
                        m.Position = 0;
                    }
                }

                // Wrap <PermissionSet> in a <TrustInfo> section
                Stream s = null;
                if (tempPrivilegeDocument != null)
                {
                    s = XmlUtil.XslTransform(trustInfoResource2, m, new DictionaryEntry("defaultRequestedPrivileges", tempPrivilegeDocument));
                }
                else
                {
                    s = XmlUtil.XslTransform(trustInfoResource2, m);
                }
                Util.CopyStream(s, output);
            }
            finally
            {
                if (tempPrivilegeDocument != null)
                {
                    File.Delete(tempPrivilegeDocument);
                }
            }
        }
        public static void WriteManifest(Manifest manifest, Stream output)
        {
            int    tickCount = Environment.TickCount;
            Stream s         = Serialize(manifest);
            string fullName  = manifest.AssemblyIdentity.GetFullName(AssemblyIdentity.FullNameFlags.All);

            if (string.IsNullOrEmpty(fullName))
            {
                fullName = manifest.GetType().Name;
            }
            Util.WriteLogFile(fullName + ".write.0-serialized.xml", s);
            string resource = "write2.xsl";
            Stream stream2  = null;

            if (manifest.GetType() == typeof(ApplicationManifest))
            {
                ApplicationManifest manifest2 = (ApplicationManifest)manifest;
                if (manifest2.TrustInfo == null)
                {
                    stream2 = XmlUtil.XslTransform(resource, s, new DictionaryEntry[0]);
                }
                else
                {
                    string temporaryFile = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
                    manifest2.TrustInfo.Write(temporaryFile);
                    if (Util.logging)
                    {
                        try
                        {
                            File.Copy(temporaryFile, Path.Combine(Util.logPath, fullName + ".trust-file.xml"), true);
                        }
                        catch (IOException)
                        {
                        }
                        catch (ArgumentException)
                        {
                        }
                        catch (UnauthorizedAccessException)
                        {
                        }
                        catch (NotSupportedException)
                        {
                        }
                    }
                    DictionaryEntry entry = new DictionaryEntry("trust-file", temporaryFile);
                    try
                    {
                        DictionaryEntry[] entries = new DictionaryEntry[] { entry };
                        stream2 = XmlUtil.XslTransform(resource, s, entries);
                    }
                    finally
                    {
                        File.Delete(temporaryFile);
                    }
                }
            }
            else
            {
                stream2 = XmlUtil.XslTransform(resource, s, new DictionaryEntry[0]);
            }
            Util.WriteLogFile(fullName + ".write.1-transformed.xml", stream2);
            Stream stream3 = null;

            if (manifest.InputStream == null)
            {
                stream3 = stream2;
            }
            else
            {
                string          str4   = Util.WriteTempFile(manifest.InputStream);
                DictionaryEntry entry2 = new DictionaryEntry("base-file", str4);
                try
                {
                    DictionaryEntry[] entryArray2 = new DictionaryEntry[] { entry2 };
                    stream3 = XmlUtil.XslTransform("merge.xsl", stream2, entryArray2);
                }
                finally
                {
                    File.Delete(str4);
                }
                Util.WriteLogFile(fullName + ".write.2-merged.xml", stream3);
            }
            Stream stream4 = ManifestFormatter.Format(stream3);

            Util.WriteLogFile(fullName + ".write.3-formatted.xml", stream4);
            Util.CopyStream(stream4, output);
            Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "ManifestWriter.WriteManifest t={0}", new object[] { Environment.TickCount - tickCount }));
        }
Ejemplo n.º 7
0
        private static void WriteManifest(Manifest manifest, Stream output, string targetframeWorkVersion)
        {
            int    t1 = Environment.TickCount;
            Stream s1 = Serialize(manifest);
            string n  = manifest.AssemblyIdentity.GetFullName(AssemblyIdentity.FullNameFlags.All);

            if (String.IsNullOrEmpty(n))
            {
                n = manifest.GetType().Name;
            }
            Util.WriteLogFile(n + ".write.0-serialized.xml", s1);

            string resource;

            if (string.IsNullOrEmpty(targetframeWorkVersion) || Util.CompareFrameworkVersions(targetframeWorkVersion, Constants.TargetFrameworkVersion40) <= 0)
            {
                resource = "write2.xsl";
            }
            else
            {
                resource = "write3.xsl";
            }

            Stream s2;

            if (manifest.GetType() == typeof(ApplicationManifest))
            {
                var am = (ApplicationManifest)manifest;
                if (am.TrustInfo == null)
                {
                    s2 = XmlUtil.XslTransform(resource, s1);
                }
                else
                {
                    // May throw IO-related exceptions
                    string temp = FileUtilities.GetTemporaryFile();

                    am.TrustInfo.Write(temp);
                    if (Util.logging)
                    {
                        try
                        {
                            File.Copy(temp, Path.Combine(Util.logPath, n + ".trust-file.xml"), true);
                        }
                        catch (IOException)
                        {
                        }
                        catch (ArgumentException)
                        {
                        }
                        catch (UnauthorizedAccessException)
                        {
                        }
                        catch (NotSupportedException)
                        {
                        }
                    }

                    var arg = new DictionaryEntry("trust-file", temp);
                    try
                    {
                        s2 = XmlUtil.XslTransform(resource, s1, arg);
                    }
                    finally
                    {
                        File.Delete(temp);
                    }
                }
            }
            else
            {
                s2 = XmlUtil.XslTransform(resource, s1);
            }
            Util.WriteLogFile(n + ".write.1-transformed.xml", s2);

            Stream s3;

            if (manifest.InputStream == null)
            {
                s3 = s2;
            }
            else
            {
                string temp = Util.WriteTempFile(manifest.InputStream);
                var    arg  = new DictionaryEntry("base-file", temp);
                try
                {
                    s3 = XmlUtil.XslTransform("merge.xsl", s2, arg);
                }
                finally
                {
                    File.Delete(temp);
                }
                Util.WriteLogFile(n + ".write.2-merged.xml", s3);
            }

            Stream s4 = ManifestFormatter.Format(s3);

            Util.WriteLogFile(n + ".write.3-formatted.xml", s4);

            Util.CopyStream(s4, output);
            Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "ManifestWriter.WriteManifest t={0}", Environment.TickCount - t1));
        }
Ejemplo n.º 8
0
        public void Write(Stream output)
        {
            XmlDocument document = new XmlDocument();
            XmlElement  newChild = XmlUtil.CloneElementToDocument(this.GetInputPermissionSetElement(), document, "urn:schemas-microsoft-com:asm.v2");

            document.AppendChild(newChild);
            string      str       = null;
            XmlDocument document2 = new XmlDocument();
            XmlElement  inputRequestedPrivilegeElement = this.GetInputRequestedPrivilegeElement();
            XmlElement  requestedPrivilegeElement      = null;

            requestedPrivilegeElement = this.GetRequestedPrivilegeElement(inputRequestedPrivilegeElement, document2);
            if (requestedPrivilegeElement != null)
            {
                document2.AppendChild(requestedPrivilegeElement);
                MemoryStream outStream = new MemoryStream();
                document2.Save(outStream);
                outStream.Position = 0L;
                str = Util.WriteTempFile(outStream);
            }
            try
            {
                string       resource = "trustinfo2.xsl";
                MemoryStream stream2  = new MemoryStream();
                if ((this._outputPermissionSet == null) && !this.sameSiteChanged)
                {
                    XmlElement documentElement = document.DocumentElement;
                    this.FixupPermissionSetElement(documentElement);
                    document.Save(stream2);
                    stream2.Position = 0L;
                }
                else
                {
                    XmlDocument outputPermissionSetDocument = this.GetOutputPermissionSetDocument();
                    XmlElement  permissionSetElement        = outputPermissionSetDocument.DocumentElement;
                    this.FixupPermissionSetElement(permissionSetElement);
                    if (document.DocumentElement == null)
                    {
                        outputPermissionSetDocument.Save(stream2);
                        stream2.Position = 0L;
                    }
                    else
                    {
                        XmlElement oldChild = document.DocumentElement;
                        XmlElement element8 = (XmlElement)document.ImportNode(permissionSetElement, true);
                        oldChild.ParentNode.ReplaceChild(element8, oldChild);
                        document.Save(stream2);
                        stream2.Position = 0L;
                    }
                }
                Stream input = null;
                if (str != null)
                {
                    DictionaryEntry[] entries = new DictionaryEntry[] { new DictionaryEntry("defaultRequestedPrivileges", str) };
                    input = XmlUtil.XslTransform(resource, stream2, entries);
                }
                else
                {
                    input = XmlUtil.XslTransform(resource, stream2, new DictionaryEntry[0]);
                }
                Util.CopyStream(input, output);
            }
            finally
            {
                if (str != null)
                {
                    File.Delete(str);
                }
            }
        }