1 /*********************************************************************
3 * Author: Brad Conte (brad AT bradconte.com)
5 * Disclaimer: This code is presented "as is" without any guarantees.
6 * Details: Implementation of the Base64 encoding algorithm.
7 *********************************************************************/
9 /*************************** HEADER FILES ***************************/
13 /****************************** MACROS ******************************/
14 #define NEWLINE_INVL 76
16 /**************************** VARIABLES *****************************/
17 // Note: To change the charset to a URL encoding, replace the '+' and '/' with '*' and '-'
18 static const BYTE charset[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
20 /*********************** FUNCTION DEFINITIONS ***********************/
23 if (ch >= 'A' && ch <= 'Z')
25 else if (ch >= 'a' && ch <='z')
27 else if (ch >= '0' && ch <='9')
37 size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag)
39 size_t idx, idx2, blks, blk_ceiling, left_over, newline_count = 0;
49 idx2 += len / 57; // (NEWLINE_INVL / 4) * 3 = 57. One newline per 57 input bytes.
52 // Since 3 input bytes = 4 output bytes, determine out how many even sets of
53 // 3 bytes the input has.
54 blk_ceiling = blks * 3;
55 for (idx = 0, idx2 = 0; idx < blk_ceiling; idx += 3, idx2 += 4) {
56 out[idx2] = charset[in[idx] >> 2];
57 out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
58 out[idx2 + 2] = charset[((in[idx + 1] & 0x0f) << 2) | (in[idx + 2] >> 6)];
59 out[idx2 + 3] = charset[in[idx + 2] & 0x3F];
60 // The offical standard requires a newline every 76 characters.
61 // (Eg, first newline is character 77 of the output.)
62 if (((idx2 - newline_count + 4) % NEWLINE_INVL == 0) && newline_flag) {
70 out[idx2] = charset[in[idx] >> 2];
71 out[idx2 + 1] = charset[(in[idx] & 0x03) << 4];
76 else if (left_over == 2) {
77 out[idx2] = charset[in[idx] >> 2];
78 out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
79 out[idx2 + 2] = charset[(in[idx + 1] & 0x0F) << 2];
88 size_t base64_decode(const BYTE in[], BYTE out[], size_t len)
91 size_t idx, idx2, blks, blk_ceiling, left_over;
93 if (in[len - 1] == '=')
95 if (in[len - 1] == '=')
102 if (len >= 77 && in[NEWLINE_INVL] == '\n') // Verify that newlines where used.
103 len -= len / (NEWLINE_INVL + 1);
110 else if (left_over == 3)
114 blk_ceiling = blks * 4;
115 for (idx = 0, idx2 = 0; idx2 < blk_ceiling; idx += 3, idx2 += 4) {
116 if (in[idx2] == '\n')
118 out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
119 out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
120 out[idx + 2] = (revchar(in[idx2 + 2]) << 6) | revchar(in[idx2 + 3]);
123 if (left_over == 2) {
124 out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
127 else if (left_over == 3) {
128 out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
129 out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);