/// <summary> /// Base64-encode the given data and return a newly allocated /// byte[] with the result. /// </summary> /// <remarks> /// Base64-encode the given data and return a newly allocated /// byte[] with the result. /// </remarks> /// <param name="input">the data to encode</param> /// <param name="offset"> /// the position within the input array at which to /// start /// </param> /// <param name="len">the number of bytes of input to encode</param> /// <param name="flags"> /// controls certain features of the encoded output. /// Passing /// <code>DEFAULT</code> /// results in output that /// adheres to RFC 2045. /// </param> public static byte[] Encode(byte[] input, int offset, int len, int flags) { Base64.Encoder encoder = new Base64.Encoder(flags, null); // Compute the exact length of the array we will produce. int output_len = len / 3 * 4; // Account for the tail of the data and the padding bytes, if any. if (encoder.do_padding) { if (len % 3 > 0) { output_len += 4; } } else { switch (len % 3) { case 0: { break; } case 1: { output_len += 2; break; } case 2: { output_len += 3; break; } } } // Account for the newlines, if any. if (encoder.do_newline && len > 0) { output_len += (((len - 1) / (3 * Base64.Encoder.LineGroups)) + 1) * (encoder.do_cr ? 2 : 1); } encoder.output = new byte[output_len]; encoder.Process(input, offset, len, true); System.Diagnostics.Debug.Assert(encoder.op == output_len); return(encoder.output); }
/// <summary> /// Base64-encode the given data and return a newly allocated /// byte[] with the result. /// </summary> /// <remarks> /// Base64-encode the given data and return a newly allocated /// byte[] with the result. /// </remarks> /// <param name="input">the data to encode</param> /// <param name="offset"> /// the position within the input array at which to /// start /// </param> /// <param name="len">the number of bytes of input to encode</param> /// <param name="flags"> /// controls certain features of the encoded output. /// Passing /// <code>DEFAULT</code> /// results in output that /// adheres to RFC 2045. /// </param> public static byte[] Encode(byte[] input, int offset, int len, int flags) { Base64.Encoder encoder = new Base64.Encoder(flags, null); // Compute the exact length of the array we will produce. int output_len = len / 3 * 4; // Account for the tail of the data and the padding bytes, if any. if (encoder.do_padding) { if (len % 3 > 0) { output_len += 4; } } else { switch (len % 3) { case 0: { break; } case 1: { output_len += 2; break; } case 2: { output_len += 3; break; } } } // Account for the newlines, if any. if (encoder.do_newline && len > 0) { output_len += (((len - 1) / (3 * Base64.Encoder.LineGroups)) + 1) * (encoder.do_cr ? 2 : 1); } encoder.output = new byte[output_len]; encoder.Process(input, offset, len, true); System.Diagnostics.Debug.Assert(encoder.op == output_len); return encoder.output; }