+int base64::decode(const unsigned char *src, int srcLen, char unsigned *dst, long dstLen) {
+ unsigned char inv_table[INVERSE_TABLE_SIZE];
+ memset(inv_table, 0x80, INVERSE_TABLE_SIZE);
+ for (ulong i = 0; i < sizeof(base64_table) - 1; i++) {
+ inv_table[base64_table[i]] = (unsigned char) i;
+ }
+ inv_table['='] = 0;
+
+
+ if (dstLen == 0 || dstLen < (int)(srcLen / 4 * 3)) {
+ mdclog_write(MDCLOG_ERR, "Destination size %ld can be up to 40 smaller then source size %d",
+ dstLen, srcLen);
+ return -1;
+ }
+ if (dst == nullptr) {
+ mdclog_write(MDCLOG_ERR, "Destination must be allocated and freed by caller the function not allocate the memory");
+ return -1;
+ }
+
+ unsigned char *pos, block[4], tmp;
+ long i;
+ int pad = 0;
+
+ size_t count = 0;
+
+ for (i = 0; i < srcLen; i++) {
+ if (inv_table[src[i]] != 0x80) {
+ count++;
+ }
+ }
+
+ if (count == 0 || count % 4)
+ return -1;
+
+ pos = dst;
+ count = 0;
+ for (i = 0; i < srcLen; i++) {
+ tmp = inv_table[src[i]];
+ if (tmp == 0x80) {
+ continue;
+ }
+ block[count] = tmp;
+
+ if (src[i] == '=') {
+ pad++;
+ }
+
+ count++;
+ if (count == 4) {
+ *pos++ = (block[0] << 2) | ((unsigned char)block[1] >> (unsigned int)4);
+ *pos++ = (block[1] << 4) | ((unsigned char)block[2] >> (unsigned int)2);
+ *pos++ = (block[2] << 6) | block[3];
+ count = 0;
+ if (pad) {
+ if (pad == 1) {
+ pos--;
+ }
+ else if (pad == 2) {
+ pos -= 2;
+ }
+ else {
+ return -1;
+ }
+ break;
+ }
+ }
+ }
+
+ dstLen = pos - dst;
+ return 0;
+}