1 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 using System
.Collections
.Generic
;
26 using Moonlight
.SecurityModel
;
30 static List
<string> additions
= new List
<string> ();
31 static List
<string> removals
= new List
<string> ();
32 static List
<string> always
= new List
<string> ();
34 private static List
<string> Merge ()
36 List
<string> lines
= new List
<string> ();
38 foreach (string add in additions
) {
39 // check if this addition was 'manually' overidden
40 if (removals
.Contains (add))
43 if (!lines
.Contains (add))
47 foreach (string add in always
) {
48 // add '!' into the list (if they were not already there)
49 if (!lines
.Contains (add))
55 for (int i
= lines
.Count
- 1; i
>= 0; i
--) {
56 string line
= lines
[i
];
57 // if this is a method...
58 bool critical_method
= line
.StartsWith ("SC-M: ");
59 bool safe_method
= line
.StartsWith ("SSC-M: ");
60 if (critical_method
|| safe_method
) {
61 // then check if the type is already marked
62 int start
= line
.IndexOf (' ', 7) + 1;
63 int length
= line
.IndexOf ("::", start
) - start
;
64 string type
= line
.Substring (start
, length
);
67 // if it's a safe method then it's not worth having it inside a SSC type
68 if (lines
.Contains ("SSC-T: " + type
))
72 // if it's a critical (or safe) method then it's not worth having inside a SC type
73 if (lines
.Contains ("SC-T: " + type
))
83 // 1. read all files for an assembly
84 // 2. copy all lines that start with a '+'
85 // 3. remove (from copy) all lines that start with a '-'
86 // 4. add (to copy) all lines that start with a '!'
90 static void ProcessLine (string line
)
92 if (String
.IsNullOrEmpty (line
))
97 // don't copy comments into the merged files
100 always
.Add (line
.Substring (1));
103 additions
.Add (line
.Substring (1));
106 removals
.Add (line
.Substring (1));
109 // if the line is not empty (spaces, tabs) then display an error
110 if (line
.Trim ().Length
> 0)
111 Console
.WriteLine ("\t\tUnknown command '{0}'.", line
[0]);
116 static void ProcessFile (string file
)
118 Console
.Write ("\tfile '{0}' ", file
);
119 if (!File
.Exists (file
)) {
120 Console
.WriteLine ("not found");
124 using (StreamReader sr
= new StreamReader (file
)) {
125 while (!sr
.EndOfStream
)
126 ProcessLine (sr
.ReadLine ());
128 Console
.WriteLine ("processed");
131 static void Process (string assembly
, string input
, string output
)
133 // read (add) all autogenerated [SecurityCritical] (from detect-sc)
134 string filename
= Path
.Combine (Path
.Combine (input
, "automatic"), assembly
+ ".auto.sc");
135 ProcessFile (filename
);
137 // read (add) all autogenerated [SecuritySafeCritical] (from detect-ssc)
138 filename
= Path
.Combine (Path
.Combine (input
, "automatic"), assembly
+ ".auto.ssc");
139 ProcessFile (filename
);
141 // read (add / remove) all manual overrides
142 filename
= Path
.Combine (Path
.Combine (input
, "overrides"), assembly
+ ".manual");
143 ProcessFile (filename
);
145 // read (add) all "required for compatibility" [SecurityCritical] (from find-sc)
146 filename
= Path
.Combine (Path
.Combine (input
, "compatibility"), assembly
+ ".compat.sc");
147 ProcessFile (filename
);
149 // make it easier for humans
150 List
<string> lines
= Merge ();
152 filename
= Path
.Combine (output
, assembly
+ ".secattr");
153 using (StreamWriter sw
= new StreamWriter (filename
)) {
154 foreach (string line
in lines
)
163 static int Main (string [] args
)
165 if (args
.Length
< 3) {
166 Console
.WriteLine ("Usage: merge input-dir output-dir assembly.secattr");
170 string input
= args
[0];
171 string output
= args
[1];
172 string secattr
= args
[2];
174 if (!secattr
.EndsWith (".secattr")) {
175 Console
.WriteLine ("Usage: merge input-dir output-dir assembly.secattr");
179 string assembly
= secattr
.Substring (0, secattr
.Length
- ".secattr".Length
);
181 if (Array
.IndexOf (PlatformCode
.Assemblies
, assembly
) == -1) {
182 Console
.WriteLine ("asssembly {0} is not a platform code assembly", assembly
);
186 // {input}/compatibility/"assembly".compat.sc find-sc only !
187 // {input}/automatic/"assembly".auto.sc detect-sc only +
188 // {input}/automatic/"assembly".auto.ssc detect-ssc only +
189 // {input}/overrides/"assembly".manual (manual) + or -
191 // {output}/"assembly".secattr
193 Process (assembly
, input
, output
);