diff options
author | jbevain <jbevain@gmail.com> | 2011-03-27 16:56:08 +0400 |
---|---|---|
committer | jbevain <jbevain@gmail.com> | 2011-03-27 16:56:08 +0400 |
commit | 7bb6a70e0595fb38a4ff0b1539d4c06bd8200387 (patch) | |
tree | a2eac04857d21214f6993cac4a0703a31ab3940a /Mono.Cecil | |
parent | 0604bc70fb95fe5cb410a8f62a14fcbc700cd0c0 (diff) |
add support for writing xml security declarations back to net_1_* modules
Diffstat (limited to 'Mono.Cecil')
-rw-r--r-- | Mono.Cecil/AssemblyWriter.cs | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index 8dd9145..dbc3094 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -1932,21 +1932,13 @@ namespace Mono.Cecil { SignatureWriter GetSecurityDeclarationSignature (SecurityDeclaration declaration) { var signature = CreateSignatureWriter (); - if (!declaration.resolved) { - signature.WriteBytes (declaration.GetBlob ()); - return signature; - } - - signature.WriteByte ((byte) '.'); - - var attributes = declaration.security_attributes; - if (attributes == null) - throw new NotSupportedException (); - signature.WriteCompressedUInt32 ((uint) attributes.Count); - - for (int i = 0; i < attributes.Count; i++) - signature.WriteSecurityAttribute (attributes [i]); + if (!declaration.resolved) + signature.WriteBytes (declaration.GetBlob ()); + else if (module.Runtime < TargetRuntime.Net_2_0) + signature.WriteXmlSecurityDeclaration (declaration); + else + signature.WriteSecurityDeclaration (declaration); return signature; } @@ -2449,7 +2441,7 @@ namespace Mono.Cecil { WriteCustomAttributeFixedArgument (argument.Type, argument); } - public void WriteSecurityAttribute (SecurityAttribute attribute) + void WriteSecurityAttribute (SecurityAttribute attribute) { WriteTypeReference (attribute.AttributeType); @@ -2469,6 +2461,49 @@ namespace Mono.Cecil { WriteBytes (buffer); } + public void WriteSecurityDeclaration (SecurityDeclaration declaration) + { + WriteByte ((byte) '.'); + + var attributes = declaration.security_attributes; + if (attributes == null) + throw new NotSupportedException (); + + WriteCompressedUInt32 ((uint) attributes.Count); + + for (int i = 0; i < attributes.Count; i++) + WriteSecurityAttribute (attributes [i]); + } + + public void WriteXmlSecurityDeclaration (SecurityDeclaration declaration) + { + var xml = GetXmlSecurityDeclaration (declaration); + if (xml == null) + throw new NotSupportedException (); + + WriteBytes (Encoding.Unicode.GetBytes (xml)); + } + + static string GetXmlSecurityDeclaration (SecurityDeclaration declaration) + { + if (declaration.security_attributes == null || declaration.security_attributes.Count != 1) + return null; + + var attribute = declaration.security_attributes [0]; + + if (!attribute.AttributeType.IsTypeOf ("System.Security.Permissions", "PermissionSetAttribute")) + return null; + + if (attribute.properties == null || attribute.properties.Count != 1) + return null; + + var property = attribute.properties [0]; + if (property.Name != "XML") + return null; + + return (string) property.Argument.Value; + } + void WriteTypeReference (TypeReference type) { WriteUTF8String (TypeParser.ToParseable (type)); |