2 // System.Security.Cryptography.ToBase64Transform
5 // Sergey Chaban (serge@wildwestsoftware.com)
7 // (C) 2004 Novell (http://www.novell.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2013 Xamarin Inc. (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.Runtime.InteropServices;
34 namespace System.Security.Cryptography {
36 static class Base64Helper {
38 private const int inputBlockSize = 3;
39 private const int outputBlockSize = 4;
41 internal static void TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
43 byte[] lookup = Base64Constants.EncodeTable;
45 int b1 = inputBuffer [inputOffset];
46 int b2 = inputBuffer [inputOffset + 1];
47 int b3 = inputBuffer [inputOffset + 2];
49 outputBuffer [outputOffset] = lookup [b1 >> 2];
50 outputBuffer [outputOffset+1] = lookup [((b1 << 4) & 0x30) | (b2 >> 4)];
51 outputBuffer [outputOffset+2] = lookup [((b2 << 2) & 0x3c) | (b3 >> 6)];
52 outputBuffer [outputOffset+3] = lookup [b3 & 0x3f];
55 // Mono System.Convert depends on the ability to process multiple blocks
56 internal static byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
58 int blockLen = inputBlockSize;
59 int outLen = outputBlockSize;
60 int fullBlocks = inputCount / blockLen;
61 int tail = inputCount % blockLen;
63 byte[] res = new byte [(inputCount != 0)
64 ? ((inputCount + 2) / blockLen) * outLen
69 for (int i = 0; i < fullBlocks; i++) {
70 TransformBlock (inputBuffer, inputOffset, blockLen, res, outputOffset);
71 inputOffset += blockLen;
72 outputOffset += outLen;
75 byte[] lookup = Base64Constants.EncodeTable;
78 // When fewer than 24 input bits are available
79 // in an input group, zero bits are added
80 // (on the right) to form an integral number of
86 b1 = inputBuffer [inputOffset];
87 res [outputOffset] = lookup [b1 >> 2];
88 res [outputOffset+1] = lookup [(b1 << 4) & 0x30];
91 res [outputOffset+2] = (byte)'=';
92 res [outputOffset+3] = (byte)'=';
96 b1 = inputBuffer [inputOffset];
97 b2 = inputBuffer [inputOffset + 1];
98 res [outputOffset] = lookup [b1 >> 2];
99 res [outputOffset+1] = lookup [((b1 << 4) & 0x30) | (b2 >> 4)];
100 res [outputOffset+2] = lookup [(b2 << 2) & 0x3c];
103 res [outputOffset+3] = (byte)'=';
112 public class ToBase64Transform : ICryptoTransform {
114 private const int inputBlockSize = 3;
115 private const int outputBlockSize = 4;
116 private bool m_disposed;
118 public ToBase64Transform ()
122 ~ToBase64Transform ()
127 public bool CanTransformMultipleBlocks {
128 get { return false; }
131 public virtual bool CanReuseTransform {
135 public int InputBlockSize {
136 get { return inputBlockSize; }
139 public int OutputBlockSize {
140 get { return outputBlockSize; }
149 public void Dispose ()
151 void IDisposable.Dispose ()
155 GC.SuppressFinalize (this); // Finalization is now unnecessary
158 protected virtual void Dispose (bool disposing)
161 // dispose unmanaged objects
163 // dispose managed objects
169 // LAMESPEC: It's not clear from docs what should be happening
170 // here if inputCount > InputBlockSize. It just "Converts the
171 // specified region of the specified byte array" and that's all.
172 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
175 throw new ObjectDisposedException ("TransformBlock");
176 if (inputBuffer == null)
177 throw new ArgumentNullException ("inputBuffer");
178 if (outputBuffer == null)
179 throw new ArgumentNullException ("outputBuffer");
181 throw new ArgumentException ("inputCount", "< 0");
182 if (inputCount > inputBuffer.Length)
183 throw new ArgumentException ("inputCount", Locale.GetText ("Overflow"));
185 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
186 // ordered to avoid possible integer overflow
187 if (inputOffset > inputBuffer.Length - inputCount)
188 throw new ArgumentException ("inputOffset", Locale.GetText ("Overflow"));
189 // ordered to avoid possible integer overflow
190 if (outputOffset < 0)
191 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
192 if (outputOffset > outputBuffer.Length - inputCount)
193 throw new ArgumentException ("outputOffset", Locale.GetText ("Overflow"));
194 /// To match MS implementation
195 // if (inputCount != this.InputBlockSize)
196 // throw new CryptographicException (Locale.GetText ("Invalid input length"));
198 Base64Helper.TransformBlock (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
199 return this.OutputBlockSize;
202 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
205 throw new ObjectDisposedException ("TransformFinalBlock");
206 if (inputBuffer == null)
207 throw new ArgumentNullException ("inputBuffer");
209 throw new ArgumentException ("inputCount", "< 0");
210 if (inputOffset > inputBuffer.Length - inputCount)
211 throw new ArgumentException ("inputCount", Locale.GetText ("Overflow"));
212 if (inputCount > this.InputBlockSize)
213 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid input length"));
215 return Base64Helper.TransformFinalBlock (inputBuffer, inputOffset, inputCount);