* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / ToBase64Transform.cs
index 9186d1142a3a7f95134e4f5d6fe52f4e6f979408..4a8f992574507f70c1151ed5abcad3b283072afd 100644 (file)
@@ -7,6 +7,29 @@
 // (C) 2004 Novell (http://www.novell.com)
 //
 
+//
+// Copyright (C) 2004 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;
 using System.Globalization;
 
@@ -16,43 +39,27 @@ namespace System.Security.Cryptography {
 
                private bool m_disposed;
 
-               /// <summary>
-               ///  Default constructor.
-               /// </summary>
                public ToBase64Transform ()
                {
                }
 
-               /* Right now we have nothing to dispose to finalizer isn't required
-               ~ToBase64Transform () 
+               ~ToBase64Transform ()
                {
                        Dispose (false);
-               }*/
+               }
 
                public bool CanTransformMultipleBlocks {
                        get { return false; }
                }
 
                public virtual bool CanReuseTransform {
-                       get { return false; }
+                       get { return true; }
                }
 
-               /// <summary>
-               ///  Returns the input block size for the Base64 encoder.
-               /// </summary>
-               /// <remarks>
-               ///  The returned value is always 3.
-               /// </remarks>
                public int InputBlockSize {
                        get { return 3; }
                }
 
-               /// <summary>
-               ///  Returns the output block size for the Base64 encoder.
-               /// </summary>
-               /// <remarks>
-               ///  The value returned by this property is always 4.
-               /// </remarks>
                public int OutputBlockSize {
                        get { return 4; }
                }
@@ -79,10 +86,32 @@ namespace System.Security.Cryptography {
                        }
                }
 
+               // LAMESPEC: It's not clear from docs what should be happening 
+               // here if inputCount > InputBlockSize. It just "Converts the 
+               // specified region of the specified byte array" and that's all.
                public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
                {
-                       if (inputCount != this.InputBlockSize)
-                               throw new CryptographicException (Locale.GetText ("Invalid input length"));
+                       if (m_disposed)
+                               throw new ObjectDisposedException ("TransformBlock");
+                       if (inputBuffer == null)
+                               throw new ArgumentNullException ("inputBuffer");
+                       if (outputBuffer == null)
+                               throw new ArgumentNullException ("outputBuffer");
+                       if (inputCount < 0)
+                               throw new ArgumentException ("inputCount", "< 0");
+                       if (inputCount > inputBuffer.Length)
+                               throw new ArgumentException ("inputCount", Locale.GetText ("Overflow"));
+                       if (inputOffset < 0)
+                               throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
+                       // ordered to avoid possible integer overflow
+                       if (inputOffset > inputBuffer.Length - inputCount)
+                               throw new ArgumentException ("inputOffset", Locale.GetText ("Overflow"));
+                       // ordered to avoid possible integer overflow
+                       if ((outputOffset < 0) || (outputOffset > outputBuffer.Length - inputCount))
+                               throw new IndexOutOfRangeException ("outputOffset");
+/// To match MS implementation
+//                     if (inputCount != this.InputBlockSize)
+//                             throw new CryptographicException (Locale.GetText ("Invalid input length"));
 
                        byte[] lookup = Base64Constants.EncodeTable;
 
@@ -98,17 +127,24 @@ namespace System.Security.Cryptography {
                        return this.OutputBlockSize;
                }
 
-               // LAMESPEC: It's not clear from Beta2 docs what should be
-               // happening here if inputCount > InputBlockSize.
-               // It just "Converts the specified region of the specified
-               // byte array" and that's all.
-               // Beta2 implementation throws some strange (and undocumented)
-               // exception in such case. The exception is thrown by
-               // System.Convert and not the method itself.
-               // Anyhow, this implementation just encodes blocks of any size,
-               // like any usual Base64 encoder.
-
                public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
+               {
+                       if (m_disposed)
+                               throw new ObjectDisposedException ("TransformFinalBlock");
+                       if (inputBuffer == null)
+                               throw new ArgumentNullException ("inputBuffer");
+                       if (inputCount < 0)
+                               throw new ArgumentException ("inputCount", "< 0");
+                       if (inputOffset > inputBuffer.Length - inputCount)
+                               throw new ArgumentException ("inputCount", Locale.GetText ("Overflow"));
+                       if (inputCount > this.InputBlockSize)
+                               throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid input length"));
+                       
+                       return InternalTransformFinalBlock (inputBuffer, inputOffset, inputCount);
+               }
+               
+               // Mono System.Convert depends on the ability to process multiple blocks                
+               internal byte[] InternalTransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
                {
                        int blockLen = this.InputBlockSize;
                        int outLen = this.OutputBlockSize;