From 6862f27687a497aa559746b22173f2a02f1210a7 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sat, 7 Jan 2017 16:05:05 +1300 Subject: Add base64 thing --- .gitignore | 1 + base64/Makefile | 1 + base64/base64.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 base64/Makefile create mode 100644 base64/base64.c diff --git a/.gitignore b/.gitignore index 00e3b3e..d9a6211 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ words-misc/match_pool words-misc/permutations monty/monty +base64/base64 *.o diff --git a/base64/Makefile b/base64/Makefile new file mode 100644 index 0000000..59d1285 --- /dev/null +++ b/base64/Makefile @@ -0,0 +1 @@ +base64: diff --git a/base64/base64.c b/base64/base64.c new file mode 100644 index 0000000..e27217c --- /dev/null +++ b/base64/base64.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#define BYTES_PER_BLOCK 3 +#define BASE64_PER_BLOCK 4 + +const char *lookup = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +void dump_base64_block(const unsigned char *data, size_t len, FILE* stream) +{ + char out_block[BASE64_PER_BLOCK+1]; + + memset(out_block, '=', sizeof(out_block)); + out_block[4] = '\0'; + + switch(len) { + case 3: + out_block[3] = lookup[data[2] & 0x3F]; + case 2: + out_block[2] = lookup[(data[1] & 0x0F) << 2 | data[2] >> 6]; + case 1: + out_block[1] = lookup[(data[0] & 0x03) << 4 | (data[1] & 0xF0) >> 4]; + out_block[0] = lookup[data[0] >> 2]; + fputs(out_block, stream); + break; + default: + break; + } +} + +void dump_base64(FILE* in, FILE* out) +{ + size_t nread = 0; + char buffer[BYTES_PER_BLOCK+1]; + + while (!feof(in)) { + nread = fread(buffer, 1, BYTES_PER_BLOCK, in); + buffer[nread] = '\0'; + dump_base64_block(buffer, nread, out); + } +} + +int main(void) +{ + dump_base64(stdin, stdout); + fputc('\n', stdout); +} -- cgit v1.1