MCPcopy Create free account
hub / github.com/cuberite/cuberite / Base64Encode

Function Base64Encode

src/StringUtils.cpp:782–824  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

780
781
782AString Base64Encode(const AString & a_Input)
783{
784 static const char BASE64[64] = {
785 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
786 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
787 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
788 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
789 };
790
791 AString output;
792 output.resize(((a_Input.size() + 2) / 3) * 4);
793
794 size_t output_index = 0;
795 size_t size_full24 = (a_Input.size() / 3) * 3;
796
797 for (size_t i = 0; i < size_full24; i += 3)
798 {
799 output[output_index++] = BASE64[(unsigned char)a_Input[i] >> 2];
800 output[output_index++] = BASE64[((unsigned char)a_Input[i] << 4 | (unsigned char)a_Input[i + 1] >> 4) & 63];
801 output[output_index++] = BASE64[((unsigned char)a_Input[i + 1] << 2 | (unsigned char)a_Input[i + 2] >> 6) & 63];
802 output[output_index++] = BASE64[(unsigned char)a_Input[i + 2] & 63];
803 }
804
805 if (size_full24 < a_Input.size())
806 {
807 output[output_index++] = BASE64[(unsigned char)a_Input[size_full24] >> 2];
808 if (size_full24 + 1 == a_Input.size())
809 {
810 output[output_index++] = BASE64[((unsigned char)a_Input[size_full24] << 4) & 63];
811 output[output_index++] = '=';
812 }
813 else
814 {
815 output[output_index++] = BASE64[((unsigned char)a_Input[size_full24] << 4 | (unsigned char)a_Input[size_full24 + 1] >> 4) & 63];
816 output[output_index++] = BASE64[((unsigned char)a_Input[size_full24 + 1] << 2) & 63];
817 }
818
819 output[output_index++] = '=';
820 }
821 ASSERT(output_index == output.size());
822
823 return output;
824}
825
826
827

Callers 2

InitServerMethod · 0.85
tolua_Base64EncodeFunction · 0.85

Calls 2

resizeMethod · 0.80
sizeMethod · 0.80

Tested by

no test coverage detected