Merge pull request #2046 from rolfbjarne/profile-simplification
[mono.git] / mcs / class / System.Core / tools / hashwrap.cs
1 //
2 // Code generator for hash wrappers
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.IO;
31
32 class Program {
33
34         static string Template = @"//
35 // NOTE: DO NOT EDIT - This file was automatically generated using
36 //      /mcs/class/System.Core/tools/hashwrap.cs
37 //
38 // System.Security.Cryptography.<className>
39 //
40 // Authors:
41 //      Sebastien Pouliot  <sebastien@ximian.com>
42 //
43 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
44 //
45 // Permission is hereby granted, free of charge, to any person obtaining
46 // a copy of this software and associated documentation files (the
47 // 'Software'), to deal in the Software without restriction, including
48 // without limitation the rights to use, copy, modify, merge, publish,
49 // distribute, sublicense, and/or sell copies of the Software, and to
50 // permit persons to whom the Software is furnished to do so, subject to
51 // the following conditions:
52 // 
53 // The above copyright notice and this permission notice shall be
54 // included in all copies or substantial portions of the Software.
55 // 
56 // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
57 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
59 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
60 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
61 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
62 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63 //
64
65 #if !NET_2_1
66
67 namespace System.Security.Cryptography {
68
69         // this is a wrapper around <wrapAround>
70         // see README.CNG and README.CSP for more details
71
72         public sealed class <className> : <baseName> {
73
74                 static byte[] Empty = new byte [0];
75
76                 private <baseName> hash;
77
78                 [SecurityCritical]
79                 public <className> ()
80                 {
81                         // note: we don't use <baseName>.Create since CryptoConfig could, 
82                         // if set to use this class, result in a endless recursion
83                         hash = new <wrapAround> ();
84                 }
85
86                 [SecurityCritical]
87                 public override void Initialize ()
88                 {
89                         hash.Initialize ();
90                 }
91
92                 [SecurityCritical]
93                 protected override void HashCore (byte[] array, int ibStart, int cbSize)
94                 {
95                         hash.TransformBlock (array, ibStart, cbSize, null, 0);
96                 }
97
98                 [SecurityCritical]
99                 protected override byte[] HashFinal ()
100                 {
101                         hash.TransformFinalBlock (Empty, 0, 0);
102                         HashValue = hash.Hash;
103                         return HashValue;
104                 }
105
106                 [SecurityCritical]
107                 protected override void Dispose (bool disposing)
108                 {
109                         (hash as IDisposable).Dispose ();
110                         base.Dispose (disposing);
111                 }
112         }
113 }
114
115 #endif
116 ";
117
118         static int Main (string [] args)
119         {
120                 int n = (args.Length / 3);
121                 if ((args.Length > 0) && ((args.Length % 3) != 0)) {
122                         Console.WriteLine ("usage: mono hashwrap.exe className baseName wrapAround [...]");
123                         return 1;
124                 }
125
126                 int p = 0;
127                 for (int i = 0; i < n; i++) {
128                         string className = args [p++];  // e.g. MD5Cng
129                         string baseName = args [p++];   // e.g. MD5
130                         string wrapAround = args [p++]; // e.g. MD5CryptoServiceProvider
131
132                         string filename = className + ".cs";
133                         using (StreamWriter writer = new StreamWriter (filename)) {
134                                 string code = Template.Replace ("<className>", className);
135                                 code = code.Replace ("<baseName>", baseName);
136                                 code = code.Replace ("<wrapAround>", wrapAround);
137                                 writer.Write (code);
138                         }
139                 }
140
141                 return 0;
142         }
143 }