Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Created April 9, 2015 03:36
Show Gist options
  • Save yohhoy/a3335ad92b4d78733603 to your computer and use it in GitHub Desktop.
Save yohhoy/a3335ad92b4d78733603 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
void base64_dump(const uint8_t *data, size_t len)
{
static const char lut[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t pos;
for (pos = 0; pos + 3 <= len; pos += 3) {
putchar(lut[ data[pos ] >> 2]);
putchar(lut[((data[pos ] & 0x03) << 4) | (data[pos+1] >> 4)]);
putchar(lut[((data[pos+1] & 0x0f) << 2) | (data[pos+2] >> 6)]);
putchar(lut[ data[pos+2] & 0x3f]);
}
if (pos < len) {
putchar(lut[ data[pos ] >> 2]);
if (pos + 1 < len) {
putchar(lut[((data[pos ] & 0x03) << 4) | (data[pos+1] >> 4)]);
putchar(lut[ (data[pos+1] & 0x0f) << 2]);
} else {
putchar(lut[ (data[pos ] & 0x03) << 4]);
putchar('=');
}
putchar('=');
}
}
@yohhoy
Copy link
Author

yohhoy commented Apr 10, 2015

CC0 license.
based on http://tools.ietf.org/html/rfc3548

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment