1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
//
// System.Security.Cryptography.FromBase64Transform.cs
//
// Authors:
// Sergey Chaban (serge@wildwestsoftware.com)
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Globalization;
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[Serializable]
#if NET_2_0
[ComVisible (true)]
#endif
public enum FromBase64TransformMode : int {
IgnoreWhiteSpaces,
DoNotIgnoreWhiteSpaces
}
#if NET_2_0
[ComVisible (true)]
#endif
public class FromBase64Transform : ICryptoTransform {
private FromBase64TransformMode mode;
private byte[] accumulator;
private int accPtr;
private bool m_disposed;
private const byte TerminatorByte = ((byte) '=');
public FromBase64Transform ()
: this (FromBase64TransformMode.IgnoreWhiteSpaces)
{
}
public FromBase64Transform (FromBase64TransformMode whitespaces)
{
this.mode = whitespaces;
accumulator = new byte [4];
accPtr = 0;
m_disposed = false;
}
~FromBase64Transform ()
{
Dispose (false);
}
public bool CanTransformMultipleBlocks {
get { return false; }
}
public virtual bool CanReuseTransform {
get { return true; }
}
public int InputBlockSize {
get { return 1; }
}
public int OutputBlockSize {
get { return 3; }
}
public void Clear()
{
Dispose (true);
}
void IDisposable.Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this); // Finalization is now unnecessary
}
protected virtual void Dispose (bool disposing)
{
if (!m_disposed) {
// zeroize data
if (accumulator != null)
Array.Clear (accumulator, 0, accumulator.Length);
// dispose unmanaged objects
if (disposing) {
// dispose managed objects
accumulator = null;
}
m_disposed = true;
}
}
private byte[] lookupTable;
private byte lookup (byte input)
{
if (input >= lookupTable.Length) {
throw new FormatException (
Locale.GetText ("Invalid character in a Base-64 string."));
}
byte ret = lookupTable [input];
if (ret == Byte.MaxValue) {
throw new FormatException (
Locale.GetText ("Invalid character in a Base-64 string."));
}
return ret;
}
private int ProcessBlock (byte[] output, int offset)
{
int rem = 0;
if (accumulator [3] == TerminatorByte)
rem++;
if (accumulator [2] == TerminatorByte)
rem++;
lookupTable = Base64Constants.DecodeTable;
int b0,b1,b2,b3;
switch (rem) {
case 0:
b0 = lookup (accumulator [0]);
b1 = lookup (accumulator [1]);
b2 = lookup (accumulator [2]);
b3 = lookup (accumulator [3]);
output [offset++] = (byte) ((b0 << 2) | (b1 >> 4));
output [offset++] = (byte) ((b1 << 4) | (b2 >> 2));
output [offset] = (byte) ((b2 << 6) | b3);
break;
case 1:
b0 = lookup (accumulator [0]);
b1 = lookup (accumulator [1]);
b2 = lookup (accumulator [2]);
output [offset++] = (byte) ((b0 << 2) | (b1 >> 4));
output [offset] = (byte) ((b1 << 4) | (b2 >> 2));
break;
case 2:
b0 = lookup (accumulator [0]);
b1 = lookup (accumulator [1]);
output [offset] = (byte) ((b0 << 2) | (b1 >> 4));
break;
}
return (3 - rem);
}
private void CheckInputParameters (byte[] inputBuffer, int inputOffset, int inputCount)
{
if (inputBuffer == null)
throw new ArgumentNullException ("inputBuffer");
if (inputOffset < 0)
throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
if (inputCount > inputBuffer.Length)
throw new OutOfMemoryException ("inputCount " + Locale.GetText ("Overflow"));
if (inputOffset > inputBuffer.Length - inputCount)
throw new ArgumentException ("inputOffset", Locale.GetText ("Overflow"));
if (inputCount < 0)
throw new OverflowException ("inputCount < 0");
}
public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
if (m_disposed)
throw new ObjectDisposedException ("FromBase64Transform");
// LAMESPEC: undocumented exceptions
CheckInputParameters (inputBuffer, inputOffset, inputCount);
if ((outputBuffer == null) || (outputOffset < 0))
throw new FormatException ("outputBuffer");
int res = 0;
while (inputCount > 0) {
if (accPtr < 4) {
byte b = inputBuffer [inputOffset++];
if (mode == FromBase64TransformMode.IgnoreWhiteSpaces) {
if (!Char.IsWhiteSpace ((char) b))
accumulator [accPtr++] = b;
} else {
// don't ignore, we'll fail if bad data is provided
accumulator [accPtr++] = b;
}
}
if (accPtr == 4) {
res += ProcessBlock (outputBuffer, outputOffset);
outputOffset += 3;
accPtr = 0;
}
inputCount--;
}
return res;
}
public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
{
if (m_disposed)
throw new ObjectDisposedException ("FromBase64Transform");
// LAMESPEC: undocumented exceptions
CheckInputParameters (inputBuffer, inputOffset, inputCount);
int ws = 0;
int terminator = 0;
if (mode == FromBase64TransformMode.IgnoreWhiteSpaces) {
// count whitespace inside string
for (int i=inputOffset, j=0; j < inputCount; i++, j++) {
if (Char.IsWhiteSpace ((char)inputBuffer [i]))
ws++;
}
// no more (useful) data
if (ws == inputCount)
return new byte [0];
// there may be whitespace after the terminator
int k = inputOffset + inputCount - 1;
int n = Math.Min (2, inputCount);
while (n > 0) {
char c = (char) inputBuffer [k--];
if (c == '=') {
terminator++;
n--;
} else if (Char.IsWhiteSpace (c)) {
continue;
} else {
break;
}
}
} else {
if (inputBuffer [inputOffset + inputCount - 1] == TerminatorByte)
terminator++;
if (inputBuffer [inputOffset + inputCount - 2] == TerminatorByte)
terminator++;
}
// some terminators could already be in the accumulator
if ((inputCount < 4) && (terminator < 2)) {
if ((accPtr > 2) && (accumulator [3] == TerminatorByte))
terminator++;
if ((accPtr > 1) && (accumulator [2] == TerminatorByte))
terminator++;
}
int count = ((accPtr + inputCount - ws) >> 2) * 3 - terminator;
if (count <= 0)
return new byte [0];
// allocate the "right" ammount (to avoid multiple allocation/copy)
byte[] result = new byte [count];
TransformBlock (inputBuffer, inputOffset, inputCount, result, 0);
return result;
}
}
}
|