Decode table construction for base64

base64, c

Solution

It's a shifted and limited ASCII translating table. The keys of the table are ASCII values, the values are base64 decoded values. The table is shifted such that the index `0` actually maps to the ASCII character `+` and any further indices map the ASCII values after `+`. The first entry in the table, the ASCII character `+`, is mapped to the base64 value `62`. Then three characters are ignored (ASCII `,-.`) and the next character is mapped to the base64 value `63`. That next character is ASCII `/`.

The rest will become obvious if you look at that table and the ASCII table.

It's usage is something like this:

int decode_base64(char ch) {
    if (ch < `+` or ch > `z`) {
        return SOME_INVALID_CH_ERROR;
    }

    /* shift range into decoding table range */
    ch -= `+`;

    int base64_val = decoding[ch];

    if (base64_val < 0) {
        return SOME_INVALID_CH_ERROR;
    }

    return base64_val;
}

Problem

I am reading this libb64 source code for encoding and decoding base64 data. I know the encoding procedure but i can't figure out how the following decoding table is constructed for fast lookup to perform decoding of encoded base64 characters. This is the table they are using: ``` static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; ``` Can some one explain me how the values in this table are used for decoding purpose.

Original source