2 // System.Security.Cryptography.FromBase64Transform.cs
5 // Sergey Chaban (serge@wildwestsoftware.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System
.Globalization
;
31 using System
.Runtime
.InteropServices
;
33 namespace System
.Security
.Cryptography
{
37 public enum FromBase64TransformMode
: int {
39 DoNotIgnoreWhiteSpaces
43 public class FromBase64Transform
: ICryptoTransform
{
45 private FromBase64TransformMode mode
;
46 private byte[] accumulator
;
48 private bool m_disposed
;
50 private const byte TerminatorByte
= ((byte) '=');
52 public FromBase64Transform ()
53 : this (FromBase64TransformMode
.IgnoreWhiteSpaces
)
57 public FromBase64Transform (FromBase64TransformMode whitespaces
)
59 this.mode
= whitespaces
;
60 accumulator
= new byte [4];
65 ~
FromBase64Transform ()
70 public bool CanTransformMultipleBlocks
{
74 public virtual bool CanReuseTransform
{
78 public int InputBlockSize
{
82 public int OutputBlockSize
{
91 void IDisposable
.Dispose ()
94 GC
.SuppressFinalize (this); // Finalization is now unnecessary
97 protected virtual void Dispose (bool disposing
)
101 if (accumulator
!= null)
102 Array
.Clear (accumulator
, 0, accumulator
.Length
);
104 // dispose unmanaged objects
106 // dispose managed objects
113 private byte[] lookupTable
;
115 private byte lookup (byte input
)
117 if (input
>= lookupTable
.Length
) {
118 throw new FormatException (
119 Locale
.GetText ("Invalid character in a Base-64 string."));
122 byte ret
= lookupTable
[input
];
123 if (ret
== Byte
.MaxValue
) {
124 throw new FormatException (
125 Locale
.GetText ("Invalid character in a Base-64 string."));
131 private int ProcessBlock (byte[] output
, int offset
)
134 if (accumulator
[3] == TerminatorByte
)
136 if (accumulator
[2] == TerminatorByte
)
139 lookupTable
= Base64Constants
.DecodeTable
;
144 b0
= lookup (accumulator
[0]);
145 b1
= lookup (accumulator
[1]);
146 b2
= lookup (accumulator
[2]);
147 b3
= lookup (accumulator
[3]);
148 output
[offset
++] = (byte) ((b0
<< 2) | (b1
>> 4));
149 output
[offset
++] = (byte) ((b1
<< 4) | (b2
>> 2));
150 output
[offset
] = (byte) ((b2
<< 6) | b3
);
153 b0
= lookup (accumulator
[0]);
154 b1
= lookup (accumulator
[1]);
155 b2
= lookup (accumulator
[2]);
156 output
[offset
++] = (byte) ((b0
<< 2) | (b1
>> 4));
157 output
[offset
] = (byte) ((b1
<< 4) | (b2
>> 2));
160 b0
= lookup (accumulator
[0]);
161 b1
= lookup (accumulator
[1]);
162 output
[offset
] = (byte) ((b0
<< 2) | (b1
>> 4));
169 private void CheckInputParameters (byte[] inputBuffer
, int inputOffset
, int inputCount
)
171 if (inputBuffer
== null)
172 throw new ArgumentNullException ("inputBuffer");
174 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
175 if (inputCount
> inputBuffer
.Length
)
176 throw new OutOfMemoryException ("inputCount " + Locale
.GetText ("Overflow"));
177 if (inputOffset
> inputBuffer
.Length
- inputCount
)
178 throw new ArgumentException ("inputOffset", Locale
.GetText ("Overflow"));
180 throw new OverflowException ("inputCount < 0");
183 public int TransformBlock (byte[] inputBuffer
, int inputOffset
, int inputCount
, byte[] outputBuffer
, int outputOffset
)
186 throw new ObjectDisposedException ("FromBase64Transform");
187 // LAMESPEC: undocumented exceptions
188 CheckInputParameters (inputBuffer
, inputOffset
, inputCount
);
189 if ((outputBuffer
== null) || (outputOffset
< 0))
190 throw new FormatException ("outputBuffer");
194 while (inputCount
> 0) {
196 byte b
= inputBuffer
[inputOffset
++];
197 if (mode
== FromBase64TransformMode
.IgnoreWhiteSpaces
) {
198 if (!Char
.IsWhiteSpace ((char) b
))
199 accumulator
[accPtr
++] = b
;
201 // don't ignore, we'll fail if bad data is provided
202 accumulator
[accPtr
++] = b
;
206 res
+= ProcessBlock (outputBuffer
, outputOffset
);
216 public byte[] TransformFinalBlock (byte[] inputBuffer
, int inputOffset
, int inputCount
)
219 throw new ObjectDisposedException ("FromBase64Transform");
220 // LAMESPEC: undocumented exceptions
221 CheckInputParameters (inputBuffer
, inputOffset
, inputCount
);
225 if (mode
== FromBase64TransformMode
.IgnoreWhiteSpaces
) {
226 // count whitespace inside string
227 for (int i
=inputOffset
, j
=0; j
< inputCount
; i
++, j
++) {
228 if (Char
.IsWhiteSpace ((char)inputBuffer
[i
]))
231 // no more (useful) data
232 if (ws
== inputCount
)
234 // there may be whitespace after the terminator
235 int k
= inputOffset
+ inputCount
- 1;
236 int n
= Math
.Min (2, inputCount
);
238 char c
= (char) inputBuffer
[k
--];
242 } else if (Char
.IsWhiteSpace (c
)) {
249 if (inputBuffer
[inputOffset
+ inputCount
- 1] == TerminatorByte
)
251 if (inputBuffer
[inputOffset
+ inputCount
- 2] == TerminatorByte
)
254 // some terminators could already be in the accumulator
255 if ((inputCount
< 4) && (terminator
< 2)) {
256 if ((accPtr
> 2) && (accumulator
[3] == TerminatorByte
))
258 if ((accPtr
> 1) && (accumulator
[2] == TerminatorByte
))
262 int count
= ((accPtr
+ inputCount
- ws
) >> 2) * 3 - terminator
;
266 // allocate the "right" ammount (to avoid multiple allocation/copy)
267 byte[] result
= new byte [count
];
268 TransformBlock (inputBuffer
, inputOffset
, inputCount
, result
, 0);