public TransformBlock ( byte inputBuffer, int inputOffset, int inputCount, byte outputBuffer, int outputOffset ) : int | ||
inputBuffer | byte | |
inputOffset | int | |
inputCount | int | |
outputBuffer | byte | |
outputOffset | int | |
return | int |
/// <summary> /// Decodes a base64 encoded string into the bytes it describes /// </summary> /// <param name="base64Encoded">The string to decode</param> /// <returns>A byte array that the base64 string described</returns> public static byte[] Decode(string base64Encoded) { using (var memoryStream = new MemoryStream()) { base64Encoded = base64Encoded.Replace("\r\n", ""); base64Encoded = base64Encoded.Replace("\t", ""); base64Encoded = base64Encoded.Replace(" ", ""); var inputBytes = Encoding.ASCII.GetBytes(base64Encoded); using (var transform = new FromBase64Transform(FromBase64TransformMode.DoNotIgnoreWhiteSpaces)) { var outputBytes = new byte[transform.OutputBlockSize]; // Transform the data in chunks the size of InputBlockSize. const int inputBlockSize = 4; var currentOffset = 0; while (inputBytes.Length - currentOffset > inputBlockSize) { transform.TransformBlock(inputBytes, currentOffset, inputBlockSize, outputBytes, 0); currentOffset += inputBlockSize; memoryStream.Write(outputBytes, 0, transform.OutputBlockSize); } // Transform the final block of data. outputBytes = transform.TransformFinalBlock(inputBytes, currentOffset, inputBytes.Length - currentOffset); memoryStream.Write(outputBytes, 0, outputBytes.Length); } return memoryStream.ToArray(); } }
/// <summary> /// Decodes a Base64 Encoding, and returns the Byte Array /// </summary> /// <param name="Text">The Encoding</param> /// <returns></returns> public static byte[] Decode(String Text) { FromBase64Transform x = new FromBase64Transform(); UTF8Encoding y = new UTF8Encoding(); byte[] OutBuf; Byte[] buffer = y.GetBytes(Text); OutBuf = new byte[buffer.Length * 3]; int BytesWritten = x.TransformBlock(buffer, 0, buffer.Length, OutBuf, 0); Byte[] NewBuf = new Byte[BytesWritten]; Array.Copy(OutBuf, 0, NewBuf, 0, NewBuf.Length); return(NewBuf); }
/// <summary> /// Decodes a base64 encoded string into the bytes it describes /// </summary> /// <param name="base64Encoded">The string to decode</param> /// <returns>A byte array that the base64 string described</returns> public static byte[] Decode(string base64Encoded) { // According to http://www.tribridge.com/blog/crm/blogs/brandon-kelly/2011-04-29/Solving-OutOfMemoryException-errors-when-attempting-to-attach-large-Base64-encoded-content-into-CRM-annotations.aspx // System.Convert.ToBase64String may leak a lot of memory // An OpenPop user reported that OutOfMemoryExceptions were thrown, and supplied the following // code for the fix. This should not have memory leaks. // The code is nearly identical to the example on MSDN: // http://msdn.microsoft.com/en-us/library/system.security.cryptography.frombase64transform.aspx#exampleToggle try { using (MemoryStream memoryStream = new MemoryStream()) { base64Encoded = base64Encoded.Replace("\r\n", ""); base64Encoded = base64Encoded.Replace("\t", ""); base64Encoded = base64Encoded.Replace(" ", ""); byte[] inputBytes = Encoding.ASCII.GetBytes(base64Encoded); using (FromBase64Transform transform = new FromBase64Transform(FromBase64TransformMode.DoNotIgnoreWhiteSpaces)) { byte[] outputBytes = new byte[transform.OutputBlockSize]; // Transform the data in chunks the size of InputBlockSize. const int inputBlockSize = 4; int currentOffset = 0; while (inputBytes.Length - currentOffset > inputBlockSize) { transform.TransformBlock(inputBytes, currentOffset, inputBlockSize, outputBytes, 0); currentOffset += inputBlockSize; memoryStream.Write(outputBytes, 0, transform.OutputBlockSize); } // Transform the final block of data. outputBytes = transform.TransformFinalBlock(inputBytes, currentOffset, inputBytes.Length - currentOffset); memoryStream.Write(outputBytes, 0, outputBytes.Length); } return memoryStream.ToArray(); } } catch (FormatException e) { DefaultLogger.Log.LogError("Base64: (FormatException) " + e.Message + "\r\nOn string: " + base64Encoded); throw; } }
public void TransformBlock_OutputOffset_Overflow () { byte[] input = new byte [16]; byte[] output = new byte [16]; using (ICryptoTransform t = new FromBase64Transform ()) { t.TransformBlock (input, 0, input.Length, output, Int32.MaxValue); } }
public void TransformBlock_OutputOffset_Negative () { byte[] input = new byte [16]; byte[] output = new byte [16]; using (ICryptoTransform t = new FromBase64Transform ()) { t.TransformBlock (input, 0, input.Length, output, -1); } }
public void TransformBlock_Output_Null () { byte[] input = new byte [16]; using (ICryptoTransform t = new FromBase64Transform ()) { t.TransformBlock (input, 0, input.Length, null, 0); } }
public void TransformBlock_InputCount_Overflow () { byte[] input = new byte [16]; byte[] output = new byte [16]; using (ICryptoTransform t = new FromBase64Transform ()) { t.TransformBlock (input, 0, Int32.MaxValue, output, 0); } }
public void TransformBlock_InputCount_Negative () { byte[] input = new byte [16]; byte[] output = new byte [16]; using (ICryptoTransform t = new FromBase64Transform ()) { t.TransformBlock (input, 0, -1, output, 0); } }