Created
July 12, 2025 22:35
-
-
Save rbmm/f9b5faf6e4f843ef1ba408e5fc96551e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| long BinToBase64(const unsigned char* data, unsigned cb, char* encoded_string, unsigned * plen, unsigned line = 76) | |
| { | |
| static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
| if (!cb) | |
| { | |
| return STATUS_INVALID_PARAMETER; | |
| } | |
| int z = 0, len = *plen; | |
| unsigned n = 0, m = 0; | |
| union { | |
| unsigned tmp = 0; | |
| unsigned char temp[3]; | |
| }; | |
| do | |
| { | |
| if (cb < 3) | |
| { | |
| z = 3 - cb; | |
| switch (cb) | |
| { | |
| case 2: | |
| temp[1] = data[1]; | |
| [[fallthrough]]; | |
| case 1: | |
| temp[0] = data[0]; | |
| break; | |
| default:__assume(false); | |
| } | |
| data = temp; | |
| cb = 3; | |
| } | |
| union | |
| { | |
| unsigned char bb[3]; | |
| struct | |
| { | |
| unsigned int f1 : 6; | |
| unsigned int f2 : 6; | |
| unsigned int f3 : 6; | |
| unsigned int f4 : 6; | |
| }; | |
| }; | |
| bb[2] = *data++; | |
| bb[1] = *data++; | |
| bb[0] = *data++; | |
| if (0 <= (len -= 4)) | |
| { | |
| *encoded_string++ = base64_chars[f4]; | |
| *encoded_string++ = base64_chars[f3]; | |
| *encoded_string++ = base64_chars[f2]; | |
| *encoded_string++ = base64_chars[f1]; | |
| } | |
| if ((m += 4) == line) | |
| { | |
| if (0 <= (len -= 2)) | |
| { | |
| *encoded_string++ = '\r'; | |
| *encoded_string++ = '\n'; | |
| } | |
| n += 2, m = 0; | |
| } | |
| } while (n += 4, cb -= 3); | |
| if (0 < len--) | |
| { | |
| *encoded_string = 0; | |
| while(z--) *--encoded_string = '='; | |
| } | |
| *plen = n + 1; | |
| return 0 > len ? STATUS_BUFFER_OVERFLOW : 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment