X
X
xtensp2020-07-02 12:12:13
C++ / C#
xtensp, 2020-07-02 12:12:13

Base64 encoding logic?

Hi all!
I found the code in which encoding in base64 takes place. But I could not understand what exactly is happening in the body of the pos_of_char function. Please explain line by line. It is especially unclear what return returns there.
To understand how everything works, I want to play around with encode - decode. I take this opportunity to ask those who understand to show an example of how you can literally write something like this in a few lines of code (if possible): Display the word on the screen - produce encode - display the result - produce decode - display result. Thanks!!!

const char* base64_chars[2] = {
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
             "abcdefghijklmnopqrstuvwxyz"
             "0123456789"
             "+/",

             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
             "abcdefghijklmnopqrstuvwxyz"
             "0123456789"
             "-_"};

static unsigned int pos_of_char(const unsigned char chr) {

    if      (chr >= 'A' && chr <= 'Z') return chr - 'A';
    else if (chr >= 'a' && chr <= 'z') return chr - 'a' + ('Z' - 'A')               + 1;
    else if (chr >= '0' && chr <= '9') return chr - '0' + ('Z' - 'A') + ('z' - 'a') + 2;
    else if (chr == '+' || chr == '-') return 62; 
    else if (chr == '/' || chr == '_') return 63; 

    throw "If input is correct, this line should never be reached.";
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Karpion, 2020-07-02
@xtensp

Base64 encodes every three bytes (24 bits) of the source file into four characters from among those that are known to be correct in text processing; the number of such characters is obviously 64 (six bits).
The author of the function takes advantage of the fact that some characters go continuously. If there were more continuity intervals, then it would be easier to make a tabular transformation.

D
Developer, 2020-07-02
@samodum

return returns a number from 0 to 63.
Depending on the input character, it is converted.
If from A to Z, then returns 0 if "A", and 90-65=25 if "Z".
If from a to z, then returns 90-65+1 = 26 if "a" and so on.
Then everything should be clear according to the logic above. The code is very easy to read.
The main point is to push the character set "A-Za-z0-9+/" into the range of numbers from 0 to 63.
This is called mapping.
https://en.wikipedia.org/wiki/Base64

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question