The mysterious case of the missing Base64 library
Many versions of base 64 encoding and decoding can be found on the
Internet, but if you would like to use a system one, here is how to
access the FreeBSD one. There isn't a documented library for the Base
64 encoding, but there is a base 64 encoder and decoder in the C
library. The library functions are
__b64_ntop
and __b64_pton
. They read and
write to user buffers with specified lengths. Here is an example
program which encodes and then decodes the command line:
#include <stdio.h> #include <string.h> #define b64_ntop __b64_ntop #define b64_pton __b64_pton /* There are no header files for these functions. */ int b64_ntop(unsigned char const *src, size_t srclength, char *target, size_t targsize); int b64_pton(char const *src, unsigned char *target, size_t targsize); /* Demonstrate the use of the functions by encoding and decoding the command line arguments. */ #define targsize 0x100 int main (int argc, char ** argv) { int i; char target[targsize]; unsigned char revert[targsize]; for (i = 0; i < argc; i++) { int end; /* Input buffer, input length, output buffer, max output length */ b64_ntop ((unsigned char*) argv[i], strlen (argv[i]), target, targsize); printf ("%s\n", target); /* Input buffer (null terminated), output buffer, max output length. */ end = b64_pton (target, revert, targsize); revert[end] = '\0'; printf ("%s\n", revert); } return 0; }
The functions aren't documented anywhere, and there doesn't seem to
be any header file you can use. There is a header
file resolv.h
which defines them, but it seems like this
include file requires other include files to also be included before
it. I don't know if there is a better way to call this function.
The output from the example program looks like this:
$ ./a.out slicey slicey oncey twicey Li9hLm91dA== ./a.out c2xpY2V5 slicey c2xpY2V5 slicey b25jZXk= oncey dHdpY2V5 twicey