| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "baseutils.h" |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
const char* base64Table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 24 |
|
| 25 |
std::string taEncodeBase64(const std::string& data) { |
| 26 |
std::string ret; |
| 27 |
|
| 28 |
ret.reserve((2+data.size()*4)/3); |
| 29 |
|
| 30 |
unsigned int groupOfThree=0; |
| 31 |
unsigned char pos=0; |
| 32 |
for (const char& i: data) { |
| 33 |
groupOfThree|=((unsigned char)i)<<((2-pos)<<3); |
| 34 |
if (++pos>=3) { |
| 35 |
pos=0; |
| 36 |
ret+=base64Table[(groupOfThree>>18)&63]; |
| 37 |
ret+=base64Table[(groupOfThree>>12)&63]; |
| 38 |
ret+=base64Table[(groupOfThree>>6)&63]; |
| 39 |
ret+=base64Table[groupOfThree&63]; |
| 40 |
groupOfThree=0; |
| 41 |
} |
| 42 |
} |
| 43 |
if (pos==2) { |
| 44 |
ret+=base64Table[(groupOfThree>>18)&63]; |
| 45 |
ret+=base64Table[(groupOfThree>>12)&63]; |
| 46 |
ret+=base64Table[(groupOfThree>>6)&63]; |
| 47 |
ret+='='; |
| 48 |
} else if (pos==1) { |
| 49 |
ret+=base64Table[(groupOfThree>>18)&63]; |
| 50 |
ret+=base64Table[(groupOfThree>>12)&63]; |
| 51 |
ret+="=="; |
| 52 |
} |
| 53 |
|
| 54 |
return ret; |
| 55 |
} |
| 56 |
|
| 57 |
std::string taDecodeBase64(const char* buf) { |
| 58 |
std::string data; |
| 59 |
|
| 60 |
unsigned int groupOfThree=0; |
| 61 |
signed char pos=18; |
| 62 |
for (const char* i=buf; *i; i++) { |
| 63 |
unsigned char nextVal=0; |
| 64 |
if ((*i)=='/') { |
| 65 |
nextVal=63; |
| 66 |
} else if ((*i)=='+') { |
| 67 |
nextVal=62; |
| 68 |
} else if ((*i)>='0' && (*i)<='9') { |
| 69 |
nextVal=52+((*i)-'0'); |
| 70 |
} else if ((*i)>='a' && (*i)<='z') { |
| 71 |
nextVal=26+((*i)-'a'); |
| 72 |
} else if ((*i)>='A' && (*i)<='Z') { |
| 73 |
nextVal=((*i)-'A'); |
| 74 |
} else { |
| 75 |
nextVal=0; |
| 76 |
} |
| 77 |
groupOfThree|=nextVal<<pos; |
| 78 |
pos-=6; |
| 79 |
if (pos<0) { |
| 80 |
pos=18; |
| 81 |
if ((groupOfThree>>16)&0xff) data+=(groupOfThree>>16)&0xff; |
| 82 |
if ((groupOfThree>>8)&0xff) data+=(groupOfThree>>8)&0xff; |
| 83 |
if (groupOfThree&0xff) data+=groupOfThree&0xff; |
| 84 |
groupOfThree=0; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return data; |
| 89 |
} |