On FreeBSD (a Unix variant operating system), there doesn't seem to be
any library for encoding and decoding the Base 64 encoding (a system
of encoding which turns binary data into ASCII). Scrabbling around in
the source code, I found that there is in fact a base 64 encoder and
decoder in the C library. The library functions are
called __b64_ntop and __b64_pton. They're
based around the idea of reading and writing to user buffers with
specified lengths. Here is a simple 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 more sensible way to call this
function.
$ ./a.out slicey slicey oncey twicey Li9hLm91dA== ./a.out c2xpY2V5 slicey c2xpY2V5 slicey b25jZXk= oncey dHdpY2V5 twicey
Looking around on the internet, it seems like everyone has their own precious and clever version of the base 64 encoding and decoding, but if you would like to use a system one rather than downloading someone else's or making your own, here is how to access the FreeBSD one.