rendered paste bodystatic unsigned char * base64_decode(buffer *out, const char *in) { unsigned char *result; int ch, j = 0, k; size_t i; size_t in_len = strlen(in); buffer_prepare_copy(out, in_len); result = (unsigned char *)out->ptr; ch = in[0]; /* run through the whole string, converting as we go */ for (i = 0; i < in_len; i++) { ch = in[i]; if (ch == '\0') break; if (ch == base64_pad) break; ch = base64_reverse_table[ch]; if (ch < 0) continue; switch(i % 4) { case 0: result[j] = ch << 2; break; case 1: result[j++] |= ch >> 4; result[j] = (ch & 0x0f) << 4; break; case 2: result[j++] |= ch >>2; result[j] = (ch & 0x03) << 6; break; case 3: result[j++] |= ch; break; } } k = j; /* mop things up if we ended on a boundary */ if (ch == base64_pad) { switch(i % 4) { case 0: case 1: return NULL; case 2: k++; case 3: result[k++] = 0; } } result[k] = '\0'; out->used = k; return result;}