1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
/* public domain */
/*
* arbitrary data on stdin -> BASE64 data on stdout
*
* UNIX's newline convention is used, i.e. one ASCII control-j (10 decimal).
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#ifndef WIN32
#define WIN32
#endif
#endif
#ifdef WIN32
#include <io.h>
#include <fcntl.h>
#endif
unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int
main(void)
{
int cols, bits, c, char_count;
#ifdef WIN32
_setmode( _fileno(stdin), _O_BINARY);
#endif
char_count = 0;
bits = 0;
cols = 0;
while ((c = getchar()) != EOF) {
if (c > 255) {
fprintf(stderr, "encountered char > 255 (decimal %d)", c);
exit(1);
}
bits += c;
char_count++;
if (char_count == 3) {
putchar(alphabet[bits >> 18]);
putchar(alphabet[(bits >> 12) & 0x3f]);
putchar(alphabet[(bits >> 6) & 0x3f]);
putchar(alphabet[bits & 0x3f]);
cols += 4;
if (cols == 72) {
putchar('\n');
cols = 0;
}
bits = 0;
char_count = 0;
} else {
bits <<= 8;
}
}
if (char_count != 0) {
bits <<= 16 - (8 * char_count);
putchar(alphabet[bits >> 18]);
putchar(alphabet[(bits >> 12) & 0x3f]);
if (char_count == 1) {
putchar('=');
putchar('=');
} else {
putchar(alphabet[(bits >> 6) & 0x3f]);
putchar('=');
}
if (cols > 0)
putchar('\n');
}
exit(0);
}
|