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)
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 {
36 public class ToBase64Transform : ICryptoTransform {
38 private const int inputBlockSize = 3;
39 private const int outputBlockSize = 4;
40 private bool m_disposed;
42 public ToBase64Transform ()
51 public bool CanTransformMultipleBlocks {
55 public virtual bool CanReuseTransform {
59 public int InputBlockSize {
60 get { return inputBlockSize; }
63 public int OutputBlockSize {
64 get { return outputBlockSize; }
72 void IDisposable.Dispose ()
75 GC.SuppressFinalize (this); // Finalization is now unnecessary
78 protected virtual void Dispose (bool disposing)
81 // dispose unmanaged objects
83 // dispose managed objects
89 // LAMESPEC: It's not clear from docs what should be happening
90 // here if inputCount > InputBlockSize. It just "Converts the
91 // specified region of the specified byte array" and that's all.
92 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
95 throw new ObjectDisposedException ("TransformBlock");
96 if (inputBuffer == null)
97 throw new ArgumentNullException ("inputBuffer");
98 if (outputBuffer == null)
99 throw new ArgumentNullException ("outputBuffer");
101 throw new ArgumentException ("inputCount", "< 0");
102 if (inputCount > inputBuffer.Length)
103 throw new ArgumentException ("inputCount", Locale.GetText ("Overflow"));
105 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
106 // ordered to avoid possible integer overflow
107 if (inputOffset > inputBuffer.Length - inputCount)
108 throw new ArgumentException ("inputOffset", Locale.GetText ("Overflow"));
109 // ordered to avoid possible integer overflow
110 if (outputOffset < 0)
111 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
112 if (outputOffset > outputBuffer.Length - inputCount)
113 throw new ArgumentException ("outputOffset", Locale.GetText ("Overflow"));
114 /// To match MS implementation
115 // if (inputCount != this.InputBlockSize)
116 // throw new CryptographicException (Locale.GetText ("Invalid input length"));
118 InternalTransformBlock (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
119 return this.OutputBlockSize;
122 internal static void InternalTransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
124 byte[] lookup = Base64Constants.EncodeTable;
126 int b1 = inputBuffer [inputOffset];
127 int b2 = inputBuffer [inputOffset + 1];
128 int b3 = inputBuffer [inputOffset + 2];
130 outputBuffer [outputOffset] = lookup [b1 >> 2];
131 outputBuffer [outputOffset+1] = lookup [((b1 << 4) & 0x30) | (b2 >> 4)];
132 outputBuffer [outputOffset+2] = lookup [((b2 << 2) & 0x3c) | (b3 >> 6)];
133 outputBuffer [outputOffset+3] = lookup [b3 & 0x3f];
136 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
139 throw new ObjectDisposedException ("TransformFinalBlock");
140 if (inputBuffer == null)
141 throw new ArgumentNullException ("inputBuffer");
143 throw new ArgumentException ("inputCount", "< 0");
144 if (inputOffset > inputBuffer.Length - inputCount)
145 throw new ArgumentException ("inputCount", Locale.GetText ("Overflow"));
146 if (inputCount > this.InputBlockSize)
147 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid input length"));
149 return InternalTransformFinalBlock (inputBuffer, inputOffset, inputCount);
152 // Mono System.Convert depends on the ability to process multiple blocks
153 internal static byte[] InternalTransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
155 int blockLen = inputBlockSize;
156 int outLen = outputBlockSize;
157 int fullBlocks = inputCount / blockLen;
158 int tail = inputCount % blockLen;
160 byte[] res = new byte [(inputCount != 0)
161 ? ((inputCount + 2) / blockLen) * outLen
164 int outputOffset = 0;
166 for (int i = 0; i < fullBlocks; i++) {
167 InternalTransformBlock (inputBuffer, inputOffset, blockLen, res, outputOffset);
168 inputOffset += blockLen;
169 outputOffset += outLen;
172 byte[] lookup = Base64Constants.EncodeTable;
175 // When fewer than 24 input bits are available
176 // in an input group, zero bits are added
177 // (on the right) to form an integral number of
183 b1 = inputBuffer [inputOffset];
184 res [outputOffset] = lookup [b1 >> 2];
185 res [outputOffset+1] = lookup [(b1 << 4) & 0x30];
188 res [outputOffset+2] = (byte)'=';
189 res [outputOffset+3] = (byte)'=';
193 b1 = inputBuffer [inputOffset];
194 b2 = inputBuffer [inputOffset + 1];
195 res [outputOffset] = lookup [b1 >> 2];
196 res [outputOffset+1] = lookup [((b1 << 4) & 0x30) | (b2 >> 4)];
197 res [outputOffset+2] = lookup [(b2 << 2) & 0x3c];
200 res [outputOffset+3] = (byte)'=';