Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDsigBase64Transform.cs
1 //
2 // XmlDsigBase64Transform.cs - Base64 Transform implementation for XML Signature
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.IO;
33 using System.Security.Cryptography;
34 using System.Text;
35 using System.Xml;
36
37 namespace System.Security.Cryptography.Xml { 
38
39         // http://www.w3.org/2000/09/xmldsig#base64
40         public class XmlDsigBase64Transform : Transform {
41
42                 private CryptoStream cs;
43                 private Type[] input;
44                 private Type[] output;
45
46                 public XmlDsigBase64Transform () 
47                 {
48                         Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform;
49                 }
50
51                 public override Type[] InputTypes {
52                         get {
53                                 if (input == null) {
54                                         input = new Type [3];
55                                         input[0] = typeof (System.IO.Stream);
56                                         input[1] = typeof (System.Xml.XmlDocument);
57                                         input[2] = typeof (System.Xml.XmlNodeList);
58                                 }
59                                 return input;
60                         }
61                 }
62
63                 public override Type[] OutputTypes {
64                         get {
65                                 if (output == null) {
66                                         output = new Type [1];
67                                         output[0] = typeof (System.IO.Stream);
68                                 }
69                                 return output;
70                         }
71                 }
72
73                 protected override XmlNodeList GetInnerXml () 
74                 {
75                         return null; // THIS IS DOCUMENTED AS SUCH
76                 }
77
78                 public override object GetOutput () 
79                 {
80                         return (object) cs;
81                 }
82
83                 public override object GetOutput (Type type) 
84                 {
85                         if (type != typeof (System.IO.Stream))
86                                 throw new ArgumentException ("type");
87                         return GetOutput ();
88                 }
89
90                 public override void LoadInnerXml (XmlNodeList nodeList) 
91                 {
92                         // documented as not changing the state of the transform
93                 }
94
95                 public override void LoadInput (object obj) 
96                 {
97                         XmlNodeList xnl = null;
98                         Stream stream = null;
99
100                         if (obj is Stream) 
101                                 stream = (obj as Stream);
102                         else if (obj is XmlDocument)
103                                 xnl = (obj as XmlDocument).SelectNodes ("//.");
104                         else if (obj is XmlNodeList)
105                                 xnl = (XmlNodeList) obj;
106
107                         if (xnl != null) {
108                                 stream = new MemoryStream ();
109                                 StreamWriter sw = new StreamWriter (stream);
110                                 foreach (XmlNode xn in xnl) {
111                                         switch (xn.NodeType) {
112                                         case XmlNodeType.Attribute:
113                                         case XmlNodeType.Text:
114                                         case XmlNodeType.CDATA:
115                                         case XmlNodeType.SignificantWhitespace:
116                                         case XmlNodeType.Whitespace:
117                                                 sw.Write (xn.Value);
118                                                 break;
119                                         }
120                                 }
121                                 sw.Flush ();
122                                 // ready to be re-used
123                                 stream.Position = 0;
124                         }
125
126                         if (stream != null)
127                                 cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
128                         // note: there is no default are other types won't throw an exception
129                 }
130         }
131 }