| 745 | |
| 746 | |
| 747 | AString Base64Decode(const AString & a_Base64String) |
| 748 | { |
| 749 | AString res; |
| 750 | size_t i, len = a_Base64String.size(); |
| 751 | int o, c; |
| 752 | res.resize((len * 4) / 3 + 5, 0); // Approximate the upper bound on the result length |
| 753 | for (o = 0, i = 0; i < len; i++) |
| 754 | { |
| 755 | c = UnBase64(a_Base64String[i]); |
| 756 | if (c >= 0) |
| 757 | { |
| 758 | switch (o & 7) |
| 759 | { |
| 760 | case 0: res[o >> 3] |= (c << 2); break; |
| 761 | case 6: res[o >> 3] |= (c >> 4); res[(o >> 3) + 1] |= (c << 4); break; |
| 762 | case 4: res[o >> 3] |= (c >> 2); res[(o >> 3) + 1] |= (c << 6); break; |
| 763 | case 2: res[o >> 3] |= c; break; |
| 764 | } |
| 765 | o += 6; |
| 766 | } |
| 767 | if (c == -1) |
| 768 | { |
| 769 | // Error while decoding, invalid input. Return as much as we've decoded: |
| 770 | res.resize(o >> 3); |
| 771 | return res; |
| 772 | } |
| 773 | } |
| 774 | res.resize(o >> 3); |
| 775 | return res; |
| 776 | } |
| 777 | |
| 778 | |
| 779 |
no test coverage detected