Created
April 9, 2015 03:36
-
-
Save yohhoy/a3335ad92b4d78733603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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('='); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CC0 license.
based on http://tools.ietf.org/html/rfc3548