A short while ago I needed to implement a little web safe base64 en/decoder and couldn't find any good small example in the width of the internet, so I decided to do my own dirty one. I hope I help somebody with this little demonstration code...
I used Pelles C Compiler to build this program, but I am optimistic that it works on every common C Compiler, since it's quite close to the C11 standard.
#include
#include
#include
#include
#define MAX_B64_PADDING 0x2
#define B64_PAD_CHAR "="
char * Base64Encode(char *input, unsigned int inputLen);
char * Base64Decode(char *input, unsigned int inputLen);
static unsigned char GetIndexByChar(unsigned char c);
static char *b64alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s StringToEncode\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("String \"%s\" to: " ,argv[1]);
printf("%s\n", Base64Encode(argv[1], strlen(argv[1])));
exit(EXIT_SUCCESS);
}
/* Caller has to free the returned base64 encoded string ! */
char *
Base64Encode(char *input, unsigned int inputLen)
{
char *encodedBuf;
int fillBytes, i, k, base64StrLen;
unsigned char a0, a1, a2, a3;
/* Make sure there is no overflow. RAM is cheap :) */
base64StrLen = inputLen + (int)(inputLen * 0.45);
encodedBuf = calloc(base64StrLen, sizeof(char));
if (encodedBuf == NULL) {
printf("calloc() failed …
Continue reading