Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #define MAX_BUF 1024
- uint8_t base64_tbl[] = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
- 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
- 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
- 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
- void base64_gen_table() {
- uint8_t tbl[256];
- int i, s;
- memset(tbl, 0xff, 256);
- for (i = 0, s = 'A'; s <= 'Z'; ++i, ++s) {
- tbl[s] = i;
- }
- for (s = 'a'; s <= 'z'; ++i, ++s) {
- tbl[s] = i;
- }
- for (s = '0'; s <= '9'; ++i, ++s) {
- tbl[s] = i;
- }
- tbl['+'] = i++;
- tbl['/'] = i;
- for (i = 0; i < 256; ++i) {
- if (i % 0x10 == 0) {
- printf("\n");
- }
- printf("0x%02x,", tbl[i]);
- }
- }
- int base64_decode(const uint8_t *src, size_t nsrc, uint8_t *dst, int *err) {
- const uint8_t *p;
- int nsrc_rem, ndst, e;
- uint8_t i0, i1, i2, i3;
- p = src;
- nsrc_rem = nsrc % 4;
- nsrc = nsrc & -4;
- ndst = 0;
- e = 0;
- while (nsrc) {
- i0 = base64_tbl[*p++];
- i1 = base64_tbl[*p++];
- i2 = base64_tbl[*p++];
- i3 = base64_tbl[*p++];
- if ((i0 | i1 | i3 | i3) & 0x80) {
- /* encoded text corruption */
- ++e;
- goto next;
- }
- dst[ndst] = i0 << 2 | i1 >> 4;
- dst[ndst + 1] = i1 << 4 | i2 >> 2;
- dst[ndst + 2] = i2 << 6 | i3;
- ndst += 3;
- next:
- nsrc -= 4;
- }
- if (nsrc_rem == 3) {
- i0 = base64_tbl[*p++];
- i1 = base64_tbl[*p++];
- i2 = base64_tbl[*p++];
- if (!(i0 & i1 & i2 & 0x80)) {
- dst[ndst] = i0 << 2 | i1 >> 4;
- dst[ndst + 1] = i1 << 4 | i2 >> 2;
- dst[ndst + 2] = i2 << 6;
- ndst += 3;
- } else {
- ++e;
- }
- } else if (nsrc_rem == 2) {
- i0 = base64_tbl[*p++];
- i1 = base64_tbl[*p++];
- if (!(i0 & i1 & 0x80)) {
- dst[ndst] = i0 << 2 | i1 >> 4;
- dst[ndst + 1] = i1 << 4;
- ndst += 2;
- } else {
- ++e;
- }
- } else if (nsrc_rem == 1) {
- i0 = base64_tbl[*p++];
- if (!(i0 & 0x80)) {
- dst[ndst] = i0 << 2;
- ndst += 2;
- } else {
- ++e;
- }
- }
- *err = e;
- return ndst;
- }
- int main(int argc, char **argv) {
- uint8_t buf[MAX_BUF];
- int len, err, i, hex;
- char *in;
- if (argc < 2) {
- return 1;
- }
- if (!strcmp(argv[1], "-g")) {
- base64_gen_table();
- return 0;
- }
- hex = 0;
- in = argv[1];
- if (argc > 2 && !strcmp(argv[1], "-hex")) {
- hex = 1;
- in = argv[2];
- }
- len = base64_decode((uint8_t *)in, strlen(in), buf, &err);
- printf("len: %d, err: %d\n", len, err);
- for (i = 0; i < len; ++i) {
- if (hex) {
- printf("%02x ", buf[i]);
- } else {
- printf("%c", (char)buf[i]);
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment