+static int encode_base64(
+ const uint8_t *data,
+ size_t datalen,
+ char **encoded,
+ size_t *encodedlen,
+ int width,
+ int pad,
+ int url)
+{
+ uint16_t u16 = 0;
+ uint8_t u8 = 0;
+ size_t in, out, rlen, n3, r3, iout, nout;
+ int iw;
+ char *result, c;
+
+ /* compute unformatted output length */
+ n3 = datalen / 3;
+ r3 = datalen % 3;
+ nout = 4 * n3 + r3 + !!r3;
+
+ /* deduce formatted output length */
+ rlen = nout;
+ if (pad)
+ rlen += ((~rlen) + 1) & 3;
+ if (width)
+ rlen += rlen / width;
+
+ /* allocate the output */
+ result = malloc(rlen + 1);
+ if (result == NULL)
+ return wrap_json_error_out_of_memory;
+
+ /* compute the formatted output */
+ iw = width;
+ for (in = out = iout = 0 ; iout < nout ; iout++) {
+ /* get in 'u8' the 6 bits value to add */
+ switch (iout & 3) {
+ case 0:
+ u16 = (uint16_t)data[in++];
+ u8 = (uint8_t)(u16 >> 2);
+ break;
+ case 1:
+ u16 = (uint16_t)(u16 << 8);
+ if (in < datalen)
+ u16 = (uint16_t)(u16 | data[in++]);
+ u8 = (uint8_t)(u16 >> 4);
+ break;
+ case 2:
+ u16 = (uint16_t)(u16 << 8);
+ if (in < datalen)
+ u16 = (uint16_t)(u16 | data[in++]);
+ u8 = (uint8_t)(u16 >> 6);
+ break;
+ case 3:
+ u8 = (uint8_t)u16;
+ break;
+ }
+ u8 &= 63;
+
+ /* encode 'u8' to the char 'c' */
+ if (u8 < 52) {
+ if (u8 < 26)
+ c = (char)('A' + u8);
+ else
+ c = (char)('a' + u8 - 26);
+ } else {
+ if (u8 < 62)
+ c = (char)('0' + u8 - 52);
+ else if (u8 == 62)
+ c = url ? '-' : '+';
+ else
+ c = url ? '_' : '/';
+ }
+
+ /* put to output with format */
+ result[out++] = c;
+ if (iw && !--iw) {
+ result[out++] = '\n';
+ iw = width;
+ }
+ }
+
+ /* pad the output */
+ while (out < rlen) {
+ result[out++] = '=';
+ if (iw && !--iw) {
+ result[out++] = '\n';
+ iw = width;
+ }
+ }
+
+ /* terminate */
+ result[out] = 0;
+ *encoded = result;
+ *encodedlen = rlen;
+ return 0;
+}
+
+static int decode_base64(
+ const char *data,
+ size_t datalen,
+ uint8_t **decoded,
+ size_t *decodedlen,
+ int url)
+{
+ uint16_t u16;
+ uint8_t u8, *result;
+ size_t in, out, iin;
+ char c;
+
+ /* allocate enougth output */
+ result = malloc(datalen);
+ if (result == NULL)
+ return wrap_json_error_out_of_memory;
+
+ /* decode the input */
+ for (iin = in = out = 0 ; in < datalen ; in++) {
+ c = data[in];
+ if (c != '\n' && c != '\r' && c != '=') {
+ if ('A' <= c && c <= 'Z')
+ u8 = (uint8_t)(c - 'A');
+ else if ('a' <= c && c <= 'z')
+ u8 = (uint8_t)(c - 'a' + 26);
+ else if ('0' <= c && c <= '9')
+ u8 = (uint8_t)(c - '0' + 52);
+ else if (c == '+' || c == '-')
+ u8 = (uint8_t)62;
+ else if (c == '/' || c == '_')
+ u8 = (uint8_t)63;
+ else {
+ free(result);
+ return wrap_json_error_bad_base64;
+ }
+ if (!iin) {
+ u16 = (uint16_t)u8;
+ iin = 6;
+ } else {
+ u16 = (uint16_t)((u16 << 6) | u8);
+ iin -= 2;
+ u8 = (uint8_t)(u16 >> iin);
+ result[out++] = u8;
+ }
+ }
+ }