Match IDisposable with 4.0 changes in System.Security.Cryptography
[mono.git] / mcs / class / corlib / System.Security.Cryptography / ToBase64Transform.cs
1 //
2 // System.Security.Cryptography.ToBase64Transform
3 //
4 // Author:
5 //   Sergey Chaban (serge@wildwestsoftware.com)
6 //
7 // (C) 2004 Novell (http://www.novell.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31 using System.Runtime.InteropServices;
32
33 namespace System.Security.Cryptography {
34
35         [ComVisible (true)]
36         public class ToBase64Transform : ICryptoTransform {
37
38                 private const int inputBlockSize = 3;
39                 private const int outputBlockSize = 4;
40                 private bool m_disposed;
41
42                 public ToBase64Transform ()
43                 {
44                 }
45
46                 ~ToBase64Transform ()
47                 {
48                         Dispose (false);
49                 }
50
51                 public bool CanTransformMultipleBlocks {
52                         get { return false; }
53                 }
54
55                 public virtual bool CanReuseTransform {
56                         get { return true; }
57                 }
58
59                 public int InputBlockSize {
60                         get { return inputBlockSize; }
61                 }
62
63                 public int OutputBlockSize {
64                         get { return outputBlockSize; }
65                 }
66
67                 public void Clear() 
68                 {
69                         Dispose (true);
70                 }
71
72 #if NET_4_0
73                 public void Dispose ()
74 #else
75                 void IDisposable.Dispose () 
76 #endif
77                 {
78                         Dispose (true);
79                         GC.SuppressFinalize (this);  // Finalization is now unnecessary
80                 }
81
82                 protected virtual void Dispose (bool disposing) 
83                 {
84                         if (!m_disposed) {
85                                 // dispose unmanaged objects
86                                 if (disposing) {
87                                         // dispose managed objects
88                                 }
89                                 m_disposed = true;
90                         }
91                 }
92
93                 // LAMESPEC: It's not clear from docs what should be happening 
94                 // here if inputCount > InputBlockSize. It just "Converts the 
95                 // specified region of the specified byte array" and that's all.
96                 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
97                 {
98                         if (m_disposed)
99                                 throw new ObjectDisposedException ("TransformBlock");
100                         if (inputBuffer == null)
101                                 throw new ArgumentNullException ("inputBuffer");
102                         if (outputBuffer == null)
103                                 throw new ArgumentNullException ("outputBuffer");
104                         if (inputCount < 0)
105                                 throw new ArgumentException ("inputCount", "< 0");
106                         if (inputCount > inputBuffer.Length)
107                                 throw new ArgumentException ("inputCount", Locale.GetText ("Overflow"));
108                         if (inputOffset < 0)
109                                 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
110                         // ordered to avoid possible integer overflow
111                         if (inputOffset > inputBuffer.Length - inputCount)
112                                 throw new ArgumentException ("inputOffset", Locale.GetText ("Overflow"));
113                         // ordered to avoid possible integer overflow
114                         if (outputOffset < 0)
115                                 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
116                         if (outputOffset > outputBuffer.Length - inputCount)
117                                 throw new ArgumentException ("outputOffset", Locale.GetText ("Overflow"));
118 /// To match MS implementation
119 //                      if (inputCount != this.InputBlockSize)
120 //                              throw new CryptographicException (Locale.GetText ("Invalid input length"));
121
122                         InternalTransformBlock (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
123                         return this.OutputBlockSize;
124                 }
125
126                 internal static void InternalTransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
127                 {
128                         byte[] lookup = Base64Constants.EncodeTable;
129
130                         int b1 = inputBuffer [inputOffset];
131                         int b2 = inputBuffer [inputOffset + 1];
132                         int b3 = inputBuffer [inputOffset + 2];
133
134                         outputBuffer [outputOffset] = lookup [b1 >> 2];
135                         outputBuffer [outputOffset+1] = lookup [((b1 << 4) & 0x30) | (b2 >> 4)];
136                         outputBuffer [outputOffset+2] = lookup [((b2 << 2) & 0x3c) | (b3 >> 6)];
137                         outputBuffer [outputOffset+3] = lookup [b3 & 0x3f];
138                 }
139
140                 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
141                 {
142                         if (m_disposed)
143                                 throw new ObjectDisposedException ("TransformFinalBlock");
144                         if (inputBuffer == null)
145                                 throw new ArgumentNullException ("inputBuffer");
146                         if (inputCount < 0)
147                                 throw new ArgumentException ("inputCount", "< 0");
148                         if (inputOffset > inputBuffer.Length - inputCount)
149                                 throw new ArgumentException ("inputCount", Locale.GetText ("Overflow"));
150                         if (inputCount > this.InputBlockSize)
151                                 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid input length"));
152                         
153                         return InternalTransformFinalBlock (inputBuffer, inputOffset, inputCount);
154                 }
155                 
156                 // Mono System.Convert depends on the ability to process multiple blocks                
157                 internal static byte[] InternalTransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
158                 {
159                         int blockLen = inputBlockSize;
160                         int outLen = outputBlockSize;
161                         int fullBlocks = inputCount / blockLen;
162                         int tail = inputCount % blockLen;
163
164                         byte[] res = new byte [(inputCount != 0)
165                                                 ? ((inputCount + 2) / blockLen) * outLen
166                                                 : 0];
167
168                         int outputOffset = 0;
169
170                         for (int i = 0; i < fullBlocks; i++) {
171                                 InternalTransformBlock (inputBuffer, inputOffset, blockLen, res, outputOffset);
172                                 inputOffset += blockLen;
173                                 outputOffset += outLen;
174                         }
175
176                         byte[] lookup = Base64Constants.EncodeTable;
177                         int b1,b2;
178
179                         // When fewer than 24 input bits are available
180                         // in an input group, zero bits are added
181                         // (on the right) to form an integral number of
182                         // 6-bit groups.
183                         switch (tail) {
184                         case 0:
185                                 break;
186                         case 1:
187                                 b1 = inputBuffer [inputOffset];
188                                 res [outputOffset] = lookup [b1 >> 2];
189                                 res [outputOffset+1] = lookup [(b1 << 4) & 0x30];
190
191                                 // padding
192                                 res [outputOffset+2] = (byte)'=';
193                                 res [outputOffset+3] = (byte)'=';
194                                 break;
195
196                         case 2:
197                                 b1 = inputBuffer [inputOffset];
198                                 b2 = inputBuffer [inputOffset + 1];
199                                 res [outputOffset] = lookup [b1 >> 2];
200                                 res [outputOffset+1] = lookup [((b1 << 4) & 0x30) | (b2 >> 4)];
201                                 res [outputOffset+2] = lookup [(b2 << 2) & 0x3c];
202
203                                 // one-byte padding
204                                 res [outputOffset+3] = (byte)'=';
205                                 break;
206                         }
207
208                         return res;
209                 }
210         }
211 }