All pastes #129088 Raw Edit

Mine

public c v1 · immutable
#129088 ·published 2006-08-13 10:46 UTC
rendered paste body
static 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;}#define BUFFER_PIECE_SIZE 64int buffer_prepare_copy(buffer *b, size_t size) {        if (!b) return -1;        if ((0 == b->size) ||            (size > b->size)) {                if (b->size) free(b->ptr);                b->size = size;                /* always allocate a multiply of BUFFER_PIECE_SIZE */                b->size += BUFFER_PIECE_SIZE - (b->size % BUFFER_PIECE_SIZE);                b->ptr = malloc(b->size);                assert(b->ptr);        }        b->used = 0;        return 0;}