C-Code
/*****************************
* base64.c
*****************************/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define LF "\n"
char base64_enctab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/=";
int8_t base64_dectab[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 127, -1, -1, 127, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 64, -1, -1, -1, 0, 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, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
void base64_encode(FILE *in, FILE *out) {
uint8_t b1, b2, b3, b4;
uint8_t c1, c2, c3;
int bcnt=0, c, pad;
do {
pad = 3;
if ((c = fgetc(in)) != EOF) {
c1 = (uint8_t)c;
pad--;
if ((c = fgetc(in)) != EOF) {
c2 = (uint8_t)c;
pad--;
if ((c = fgetc(in)) != EOF) {
c3 = (uint8_t)c;
pad--;
}
}
}
else
break;
b1 = (c1 & 0xFC)>>2;
b2 = ((c1 & 0x03)<<4)|((c2 & 0xF0)>>4);
b3 = (pad<2)?(((c2 & 0x0F)<<2)|((c3 & 0xC0)>>6)):64;
b4 = (pad<1)?(c3 & 0x3F):64;
bcnt += 4;
fprintf(out, "%c%c%c%c", base64_enctab[b1],
base64_enctab[b2],
base64_enctab[b3],
base64_enctab[b4]);
/* encoded line with 76 characters */
if (bcnt%76==0)
fprintf(out, LF);
} while (c != EOF);
}
int base64_decode(FILE *in, FILE *out) {
uint8_t c1, c2, c3;
uint8_t c, ac[4];
int b=0, cntb=0, pad=0, nc;
do {
nc = 0;
while (nc < 4) {
if ((b = fgetc(in)) != EOF) {
cntb++;
if ((c = base64_dectab[b]) < 0)
/* illegal character */
return -1;
else if (c == 127) {
/* skip line feed */
continue;
}
else if (b == '=') {
/* padding byte */
pad++;
if (pad > 2)
break;
nc++;
}
else {
if (pad)
return -1;
else
ac[nc++] = c;
}
}
else
break;
}
if (nc == 4) {
c1 = (ac[0]<<2)|((ac[1] & 0x30)>>4);
c2 = ((ac[1] & 0x0F)<<4)|((ac[2] & 0x3C)>>2);
c3 = ((ac[2] & 0x03)<<6)|(ac[3]);
fprintf(out, "%c%c%c", c1, c2, c3);
}
} while (b != EOF);
if (nc > 0)
return -1;
return 0;
}
int main(int argc, char **argv) {
if (argc > 1) {
if ((strcmp(argv[1], "-encode") == 0)) {
base64_encode(stdin, stdout);
} else
if ((strcmp(argv[1], "-decode")==0)) {
if (base64_decode(stdin, stdout) < 0)
fprintf(stderr, "Keine gültige Base64-Kodierung.\n");
} else {
printf("Usage: base64 -encode\n");
printf(" base64 -decode\n");
printf(" Eingabe=stdin, Ausgabe=stdout\n");
}
}
return 0;
}