TransformFinalBlock() public method

public TransformFinalBlock ( byte inputBuffer, int inputOffset, int inputCount ) : byte[]
inputBuffer byte
inputOffset int
inputCount int
return byte[]
コード例 #1
0
ファイル: Base64.cs プロジェクト: iraychen/MSGReader
	    /// <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();
	        }
	    }
コード例 #2
0
ファイル: Base64.cs プロジェクト: JoshKeegan/hpop
		/// <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;
			}
		}
コード例 #3
0
        /// <summary>
        /// 原始base64解码
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static byte[] Base64Decode(byte[] source)
        {
            if ((source == null) || (source.Length == 0))
                throw new ArgumentException("source is not valid");

            FromBase64Transform fb64 = new FromBase64Transform();
            MemoryStream stm = new MemoryStream();
            int pos = 0;
            byte[] buff;

            while (pos + 4 < source.Length)
            {
                buff = fb64.TransformFinalBlock(source, pos, 4);
                stm.Write(buff, 0, buff.Length);
                pos += 4;
            }

            buff = fb64.TransformFinalBlock(source, pos, source.Length - pos);
            stm.Write(buff, 0, buff.Length);
            return stm.ToArray();
        }
コード例 #4
0
ファイル: LLSD.cs プロジェクト: ChrisD/opensim
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static object LLSDParseOne(XmlTextReader reader)
        {
            SkipWS(reader);
            if (reader.NodeType != XmlNodeType.Element)
                throw new LLSDParseException("Expected an element");

            string dtype = reader.LocalName;
            object ret = null;

            switch (dtype)
            {
                case "undef":
                    {
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return null;
                        }

                        reader.Read();
                        SkipWS(reader);
                        ret = null;
                        break;
                    }
                case "boolean":
                    {
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return false;
                        }

                        reader.Read();
                        string s = reader.ReadString().Trim();

                        if (s == String.Empty || s == "false" || s == "0")
                            ret = false;
                        else if (s == "true" || s == "1")
                            ret = true;
                        else
                            throw new LLSDParseException("Bad boolean value " + s);

                        break;
                    }
                case "integer":
                    {
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return 0;
                        }

                        reader.Read();
                        ret = Convert.ToInt32(reader.ReadString().Trim());
                        break;
                    }
                case "real":
                    {
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return 0.0f;
                        }

                        reader.Read();
                        ret = Convert.ToDouble(reader.ReadString().Trim());
                        break;
                    }
                case "uuid":
                    {
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return UUID.Zero;
                        }

                        reader.Read();
                        ret = new UUID(reader.ReadString().Trim());
                        break;
                    }
                case "string":
                    {
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return String.Empty;
                        }

                        reader.Read();
                        ret = reader.ReadString();
                        break;
                    }
                case "binary":
                    {
                        if (reader.IsEmptyElement)
                        {
                            reader.Read();
                            return new byte[0];
                        }

                        if (reader.GetAttribute("encoding") != null &&
                            reader.GetAttribute("encoding") != "base64")
                        {
                            throw new LLSDParseException("Unknown encoding: " + reader.GetAttribute("encoding"));
                        }

                        reader.Read();
                        FromBase64Transform b64 = new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces);
                        byte[] inp = Encoding.UTF8.GetBytes(reader.ReadString());
                        ret = b64.TransformFinalBlock(inp, 0, inp.Length);
                        break;
                    }
                case "date":
                    {
                        reader.Read();
                        throw new Exception("LLSD TODO: date");
                    }
                case "map":
                    {
                        return LLSDParseMap(reader);
                    }
                case "array":
                    {
                        return LLSDParseArray(reader);
                    }
                default:
                    throw new LLSDParseException("Unknown element <" + dtype + ">");
            }

            if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != dtype)
            {
                throw new LLSDParseException("Expected </" + dtype + ">");
            }

            reader.Read();
            return ret;
        }
コード例 #5
0
		public void TransformFinalBlock_InputCount_Overflow () 
		{
			byte[] input = new byte [16];
			using (ICryptoTransform t = new FromBase64Transform ()) {
				t.TransformFinalBlock (input, 0, Int32.MaxValue);
			}
		}
コード例 #6
0
		public void TransformFinalBlock_InputCount_Negative () 
		{
			byte[] input = new byte [16];
			using (ICryptoTransform t = new FromBase64Transform ()) {
				t.TransformFinalBlock (input, 0, -1);
			}
		}
コード例 #7
0
		public void TransformFinalBlock_InputOffset_Overflow () 
		{
			byte[] input = new byte [16];
			using (ICryptoTransform t = new FromBase64Transform ()) {
				t.TransformFinalBlock (input, Int32.MaxValue, input.Length);
			}
		}
コード例 #8
0
		public void TransformFinalBlock_InputOffset_Negative () 
		{
			byte[] input = new byte [16];
			using (ICryptoTransform t = new FromBase64Transform ()) {
				t.TransformFinalBlock (input, -1, input.Length);
			}
		}
コード例 #9
0
		public void TransformFinalBlock_Input_Null () 
		{
			using (ICryptoTransform t = new FromBase64Transform ()) {
				t.TransformFinalBlock (null, 0, 16);
			}
		}
コード例 #10
0
		public void Dispose () 
		{
			byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
			byte[] expected = { 174, 90, 123, 66 };
			byte[] output = null;

			using (ICryptoTransform t = new FromBase64Transform ()) {
				output = t.TransformFinalBlock (input, 0, input.Length);
			}

			AssertEquals ("IDisposable", expected.Length, output.Length);
			for (int i = 0; i < expected.Length; i++)
				AssertEquals ("IDisposable(" + i + ")", expected [i], output [i]);
		}
コード例 #11
0
ファイル: BuiltInDatatype.cs プロジェクト: nobled/mono
		public override object ParseValue (string s,
			XmlNameTable nameTable, NSResolver nsmgr)
		{
		        // If it isnt ASCII it isnt valid base64 data
			byte[] inArr = new System.Text.ASCIIEncoding().GetBytes(s);
			FromBase64Transform t = new FromBase64Transform();
			return t.TransformFinalBlock(inArr, 0, inArr.Length);
		}