Function Base64Enc
|
Encodes data with MIME Base64.
Base64 encoding is designed to make binary data survive transport through
transport layers that are not 8-bit clean, such as mail bodies.
Base64 processes data as 24-bit groups, mapping this data to four encoded characters. Base64
encoding is sometimes referred to as 3-to-4 encoding. Each 6 bits of the 24-bit
group is used as an index into a mapping table (the base64 alphabet) to obtain a character for the encoded data. Padding at the end of the data is performed using the additional "=" character.
The encoded data are consistently only about 33 percent larger than the unencoded data.
The encoding scheme is first defined in RFC 1341, obsoleted later by RFC 1521 and RFC 2045. The section 6.8 of RFC 2045, 'Base64 Content-Transfer-Encoding', is reproduced here.
|
Declaration
|
The preferred format of the input and output data stream may vary depending on the context. Here are three possibilities:
Function Base64Enc(Expression As String) As String
Function Base64Enc(abSrc() As Byte) As String
Sub Base64Dec(abSrc() As Byte, abDst() As Byte)
The input string resp. byte array can contain arbitrary binary data, and the function returns a string containing one or more lines (see below) of base64 encoded data.
|
Line breaks:
RFC 2045 demands: "The encoded output stream must be represented in lines of no more
than 76 characters each." Note that VBspeed's Base64Enc definition does not conform to this.
|
Unicode:
Care has to be taken when a source string contains UNICODE characters that cannot be losslessly mapped to the ANSI charset (and only very few can).
For example, take the Cyrillic character Я (dec 1071, hex 04 2F): in Base64Enc01, which can handle ANSI only, this character is mapped to the default character "?", and consequently the encoding is wrong.
Base64Enc01("Я") --> "Pw=="; Base64Dec01("Pw==") --> "?" (WRONG)
Base64Enc02("Я") --> "LwQ="; Base64Dec02("LwQ=") --> "Я" (correct)
Functions that take byte arrays as source data (as e.g. Base64Enc02 - shown above as if taking a string for demonstration purposes), do not have any unicode problems, resp. they export those problems to the place where unicode strings are converted to byte arrays.
|
Examples
|
ANSI, 1 byte/char:
Base64Enc("VBspee") --> "VkJzcGVl" 'in: 6, out: 8, padding: 0
Base64Enc("VBspeed") --> "VkJzcGVlZA==" 'in: 7, out: 12, padding: 2
Base64Enc("VBspeedo") --> "VkJzcGVlZG8=" 'in: 8, out: 12, padding: 1
Base64Enc("VBspeedos")--> "VkJzcGVlZG9z" 'in: 9, out: 12, padding: 0
UNICODE, 2 bytes/char:
Base64Enc("VBspee") --> "VgBCAHMAcABlAGUA" 'in: 12, out: 16, padding: 0
Base64Enc("VBspeed") --> "VgBCAHMAcABlAGUAZAA=" 'in: 14, out: 20, padding: 1
Base64Enc("VBspeedo") --> "VgBCAHMAcABlAGUAZABvAA==" 'in: 16, out: 24, padding: 2
Base64Enc("VBspeedos")--> "VgBCAHMAcABlAGUAZABvAHMA" 'in: 18, out: 24, padding: 0
|
Roll your own
|
If you want to have a go at Base64Enc yourself, use this function (VB5/6-compatible) to verify the correctness of your code.
|
|