2014-09-22 05:00:27 +00:00
|
|
|
/*
|
|
|
|
base64.c - by Joe DF (joedf@ahkscript.org)
|
|
|
|
Released under the MIT License
|
|
|
|
|
2014-09-22 22:30:28 +00:00
|
|
|
See "base64.h", for more information.
|
2014-09-22 05:00:27 +00:00
|
|
|
|
|
|
|
Thank you for inspiration:
|
|
|
|
http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode
|
|
|
|
*/
|
2014-09-22 22:30:28 +00:00
|
|
|
#include "base64.h"
|
2014-09-22 05:00:27 +00:00
|
|
|
|
|
|
|
//Base64 char table - used internally for encoding
|
|
|
|
unsigned char b64_chr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
|
|
|
unsigned int b64_int(unsigned int ch) {
|
|
|
|
|
|
|
|
// ASCII to base64_int
|
|
|
|
// 65-90 Upper Case >> 0-25
|
|
|
|
// 97-122 Lower Case >> 26-51
|
|
|
|
// 48-57 Numbers >> 52-61
|
|
|
|
// 43 Plus (+) >> 62
|
|
|
|
// 47 Slash (/) >> 63
|
|
|
|
// 61 Equal (=) >> 64~
|
|
|
|
if (ch==43)
|
2014-09-22 22:30:28 +00:00
|
|
|
return 62;
|
2014-09-22 05:00:27 +00:00
|
|
|
if (ch==47)
|
2014-09-22 22:30:28 +00:00
|
|
|
return 63;
|
2014-09-22 05:00:27 +00:00
|
|
|
if (ch==61)
|
2014-09-22 22:30:28 +00:00
|
|
|
return 64;
|
2014-09-22 05:00:27 +00:00
|
|
|
if ((ch>47) && (ch<58))
|
2014-09-22 22:30:28 +00:00
|
|
|
return ch + 4;
|
2014-09-22 05:00:27 +00:00
|
|
|
if ((ch>64) && (ch<91))
|
2014-09-22 22:30:28 +00:00
|
|
|
return ch - 'A';
|
2014-09-22 05:00:27 +00:00
|
|
|
if ((ch>96) && (ch<123))
|
2014-09-22 22:30:28 +00:00
|
|
|
return (ch - 'a') + 26;
|
2014-09-22 05:00:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int b64e_size(unsigned int in_size) {
|
|
|
|
|
|
|
|
// size equals 4*floor((1/3)*(in_size+2));
|
|
|
|
int i, j = 0;
|
|
|
|
for (i=0;i<in_size;i++) {
|
|
|
|
if (i % 3 == 0)
|
2014-09-22 22:30:28 +00:00
|
|
|
j += 1;
|
2014-09-22 05:00:27 +00:00
|
|
|
}
|
|
|
|
return (4*j);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int b64d_size(unsigned int in_size) {
|
|
|
|
|
|
|
|
return ((3*in_size)/4);
|
|
|
|
}
|
|
|
|
|
2021-05-15 21:16:20 +00:00
|
|
|
unsigned int
|
|
|
|
b64_encode(char* in, unsigned int in_len, char* out){
|
2014-09-22 05:00:27 +00:00
|
|
|
|
|
|
|
unsigned int i=0, j=0, k=0, s[3];
|
|
|
|
|
2021-05-15 21:16:20 +00:00
|
|
|
for (i=0;i<in_len;i++){
|
2014-09-22 05:00:27 +00:00
|
|
|
s[j++]=*(in+i);
|
2021-05-15 21:16:20 +00:00
|
|
|
if (j==3){
|
2018-02-08 14:38:50 +00:00
|
|
|
out[k+0] = b64_chr[ (s[0]&255)>>2 ];
|
2014-09-22 05:00:27 +00:00
|
|
|
out[k+1] = b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ];
|
|
|
|
out[k+2] = b64_chr[ ((s[1]&0x0F)<<2)+((s[2]&0xC0)>>6) ];
|
|
|
|
out[k+3] = b64_chr[ s[2]&0x3F ];
|
|
|
|
j=0; k+=4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j) {
|
|
|
|
if (j==1)
|
|
|
|
s[1] = 0;
|
2018-02-08 14:38:50 +00:00
|
|
|
out[k+0] = b64_chr[ (s[0]&255)>>2 ];
|
2014-09-22 05:00:27 +00:00
|
|
|
out[k+1] = b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ];
|
|
|
|
if (j==2)
|
|
|
|
out[k+2] = b64_chr[ ((s[1]&0x0F)<<2) ];
|
|
|
|
else
|
|
|
|
out[k+2] = '=';
|
|
|
|
out[k+3] = '=';
|
|
|
|
k+=4;
|
|
|
|
}
|
|
|
|
|
|
|
|
out[k] = '\0';
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:21:23 +00:00
|
|
|
int
|
2021-05-15 21:16:20 +00:00
|
|
|
b64_decode(char* in, unsigned int in_len, char* out){
|
2014-09-22 05:00:27 +00:00
|
|
|
|
|
|
|
unsigned int i=0, j=0, k=0, s[4];
|
|
|
|
|
|
|
|
for (i=0;i<in_len;i++) {
|
|
|
|
s[j++]=b64_int(*(in+i));
|
|
|
|
if (j==4) {
|
2018-02-08 14:38:50 +00:00
|
|
|
out[k+0] = ((s[0]&255)<<2)+((s[1]&0x30)>>4);
|
2014-09-22 05:00:27 +00:00
|
|
|
if (s[2]!=64) {
|
|
|
|
out[k+1] = ((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2);
|
|
|
|
if ((s[3]!=64)) {
|
|
|
|
out[k+2] = ((s[2]&0x03)<<6)+(s[3]); k+=3;
|
|
|
|
} else {
|
|
|
|
k+=2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
k+=1;
|
|
|
|
}
|
|
|
|
j=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|