npp_mimetools.b64

Base64 encoding decoding - where 8 bit ascii is re-represented using just 64 ascii characters (plus optional padding '=').

This code includes options to encode to base64 in multiple ways. For example the text lines:-

If you can keep your head when all about you Are losing theirs and blaming it on you;

Using "Encode with Unix EOL" would produce a single base64 string with line breaks after each 64 characters:-

SWYgeW91IGNhbiBrZWVwIHlvdXIgaGVhZCB3aGVuIGFsbCBhYm91dCB5b3UNCkFy ZSBsb3NpbmcgdGhlaXJzIGFuZCBibGFtaW5nIGl0IG9uIHlvdTs=

That would be decoded using a single base64 decode which ignored whitespace characters (the line breaks).

Alternatively the same lines could be encoded using a "by line" option to encode each line of input as its own separate base64 string:-

SWYgeW91IGNhbiBrZWVwIHlvdXIgaGVhZCB3aGVuIGFsbCBhYm91dCB5b3U QXJlIGxvc2luZyB0aGVpcnMgYW5kIGJsYW1pbmcgaXQgb24geW91Ow

Each of these output lines could be decoded separately, or multiple lines decoded using "reset on whitespace" to cause base64 decoding to restart on each line

Members

Functions

base64Decode
int base64Decode(char[] resultString, char[] encodedString, size_t encodedStringLength, bool strictFlag, bool whitespaceReset)

base64Decode converts base64 to ascii. But there are choices about what to do with illegal characters or malformed strings. In this version there is a strict flag to indicate that the input must be a single valid base64 string with no illegal characters, no extra padding, and no short segments. Otherwise there is best effort to decode around illegal characters which ARE preserved in the output. So "TWFyeQ==.aGFk.YQ.bGl0dGxl.bGFtYg==" decodes to "Mary.had.a.little.lamb" with five seperate base64 strings decoded, each separated by the illegal character dot. In strict mode the first dot would trigger a fatal error. Some other implementations choose to ignore illegal characters which of course has it's own issues. The four whitespace characters <CR> <LF> <TAB> and <SPACE> are silently ignored unless noWhitespaceFlag is set. In this case whitespace is treated similar to illegal characters and base64 decoding operates around the white space. So "TWFyeQ== aGFk YQ bGl0dGxl bGFtYg==" would decode as "Mary had a little lamb". Decoding is done by loading four base64 characters at a time into a bitField, and then extracting them as three ascii characters. returnString is assumed to be large enough to contain the result (which could be the same size as the input), and the function return is the length of the result, or a negative value in case of an error

base64Encode
int base64Encode(char[] resultString, char[] asciiString, size_t asciiStringLength, size_t wrapLength, bool padFlag, bool byLineFlag)

base64Encode simply converts ascii to base64 with appropriate wrapping and padding. Encoding is done by loading three ascii characters at a time into a bitField, and then extracting them as four base64 values. returnString is assumed to be large enough to contain the result (which is typically 4 / 3 the input size plus line breaks), and the function return is the length of the result wrapLength sets the length at which to wrap the encoded test at (not valid with byLineFlag) padFlag controls whether the one or two '=' pad characters are included at the end of encoding byLineFlag causes each input line to be encoded as a separate base64 string

Manifest constants

base64CharSet
enum base64CharSet;
Undocumented in source.

Static variables

base64CharMap
int[] base64CharMap;
Undocumented in source.

Meta