|
| 1 | +// https://raw.githubusercontent.com/zhicheng/base64/master/base64.c |
| 2 | +/* This is a public domain base64 implementation written by WEI Zhicheng. */ |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | + |
| 6 | +#include "base64url.h" |
| 7 | + |
| 8 | +/* BASE 64 encode table */ |
| 9 | +static const char base64en[] = { |
| 10 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 11 | + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 12 | + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 13 | + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 14 | + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
| 15 | + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 16 | + 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
| 17 | + '4', '5', '6', '7', '8', '9', '-', '_', |
| 18 | +}; |
| 19 | + |
| 20 | +#define BASE64_PAD '=' |
| 21 | + |
| 22 | + |
| 23 | +#define BASE64DE_FIRST '+' |
| 24 | +#define BASE64DE_LAST 'z' |
| 25 | +/* ASCII order for BASE 64 decode, -1 in unused character */ |
| 26 | +static const signed char base64de[] = { |
| 27 | + /* '+', ',', '-', '.', '/', '0', '1', '2', */ |
| 28 | + 62, -1, -1, -1, 63, 52, 53, 54, |
| 29 | + |
| 30 | + /* '3', '4', '5', '6', '7', '8', '9', ':', */ |
| 31 | + 55, 56, 57, 58, 59, 60, 61, -1, |
| 32 | + |
| 33 | + /* ';', '<', '=', '>', '?', '@', 'A', 'B', */ |
| 34 | + -1, -1, -1, -1, -1, -1, 0, 1, |
| 35 | + |
| 36 | + /* 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', */ |
| 37 | + 2, 3, 4, 5, 6, 7, 8, 9, |
| 38 | + |
| 39 | + /* 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', */ |
| 40 | + 10, 11, 12, 13, 14, 15, 16, 17, |
| 41 | + |
| 42 | + /* 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */ |
| 43 | + 18, 19, 20, 21, 22, 23, 24, 25, |
| 44 | + |
| 45 | + /* '[', '\', ']', '^', '_', '`', 'a', 'b', */ |
| 46 | + -1, -1, -1, -1, -1, -1, 26, 27, |
| 47 | + |
| 48 | + /* 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', */ |
| 49 | + 28, 29, 30, 31, 32, 33, 34, 35, |
| 50 | + |
| 51 | + /* 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', */ |
| 52 | + 36, 37, 38, 39, 40, 41, 42, 43, |
| 53 | + |
| 54 | + /* 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */ |
| 55 | + 44, 45, 46, 47, 48, 49, 50, 51, |
| 56 | +}; |
| 57 | + |
| 58 | +int base64url_encode(const unsigned char *in, unsigned int inlen, char *out) |
| 59 | +{ |
| 60 | + unsigned int i, j; |
| 61 | + |
| 62 | + for (i = j = 0; i < inlen; i++) { |
| 63 | + int s = i % 3; /* from 6/gcd(6, 8) */ |
| 64 | + |
| 65 | + switch (s) { |
| 66 | + case 0: |
| 67 | + out[j++] = base64en[(in[i] >> 2) & 0x3F]; |
| 68 | + continue; |
| 69 | + case 1: |
| 70 | + out[j++] = base64en[((in[i-1] & 0x3) << 4) + ((in[i] >> 4) & 0xF)]; |
| 71 | + continue; |
| 72 | + case 2: |
| 73 | + out[j++] = base64en[((in[i-1] & 0xF) << 2) + ((in[i] >> 6) & 0x3)]; |
| 74 | + out[j++] = base64en[in[i] & 0x3F]; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /* move back */ |
| 79 | + i -= 1; |
| 80 | + |
| 81 | + /* check the last and add padding */ |
| 82 | + |
| 83 | + if ((i % 3) == 0) { |
| 84 | + out[j++] = base64en[(in[i] & 0x3) << 4]; |
| 85 | + //out[j++] = BASE64_PAD; |
| 86 | + //out[j++] = BASE64_PAD; |
| 87 | + } else if ((i % 3) == 1) { |
| 88 | + out[j++] = base64en[(in[i] & 0xF) << 2]; |
| 89 | + //out[j++] = BASE64_PAD; |
| 90 | + } |
| 91 | + |
| 92 | + out[j++] = 0; |
| 93 | + |
| 94 | + return BASE64_OK; |
| 95 | +} |
| 96 | + |
| 97 | +int base64url_decode(const char *in, unsigned int inlen, unsigned char *out) |
| 98 | +{ |
| 99 | + unsigned int i, j; |
| 100 | + |
| 101 | + for (i = j = 0; i < inlen; i++) { |
| 102 | + int c; |
| 103 | + int s = i % 4; /* from 8/gcd(6, 8) */ |
| 104 | + |
| 105 | + if (in[i] == '=') |
| 106 | + return BASE64_OK; |
| 107 | + |
| 108 | + if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST || |
| 109 | + (c = base64de[in[i] - BASE64DE_FIRST]) == -1) |
| 110 | + return BASE64_INVALID; |
| 111 | + |
| 112 | + switch (s) { |
| 113 | + case 0: |
| 114 | + out[j] = ((unsigned int)c << 2) & 0xFF; |
| 115 | + continue; |
| 116 | + case 1: |
| 117 | + out[j++] += ((unsigned int)c >> 4) & 0x3; |
| 118 | + |
| 119 | + /* if not last char with padding */ |
| 120 | + if (i < (inlen - 3) || in[inlen - 2] != '=') |
| 121 | + out[j] = ((unsigned int)c & 0xF) << 4; |
| 122 | + continue; |
| 123 | + case 2: |
| 124 | + out[j++] += ((unsigned int)c >> 2) & 0xF; |
| 125 | + |
| 126 | + /* if not last char with padding */ |
| 127 | + if (i < (inlen - 2) || in[inlen - 1] != '=') |
| 128 | + out[j] = ((unsigned int)c & 0x3) << 6; |
| 129 | + continue; |
| 130 | + case 3: |
| 131 | + out[j++] += (unsigned char)c; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return BASE64_OK; |
| 136 | +} |
0 commit comments