MCPcopy Create free account
hub / github.com/SIPp/sipp / base64Encode

Method base64Encode

src/jlsrtp.cpp:907–963  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

905}
906
907std::string JLSRTP::base64Encode(std::vector<unsigned char> const& s)
908{
909 int i = 0;
910 int j = 0;
911 unsigned char char_array_3[3];
912 unsigned char char_array_4[4];
913 unsigned char const* bytes_to_encode = &s.front();
914 unsigned int in_len = s.size();
915 std::string ret;
916 // Reserve enough space in 'ret' to avoid multiple allocations.
917 // In Base64, every 3 bytes of binary data become 4 bytes of ASCII text.
918 // Round up for potential padding.
919 ret.reserve(((in_len + 2) / 3) * 4);
920
921 while (in_len--)
922 {
923 char_array_3[i++] = *(bytes_to_encode++);
924 if (i == 3)
925 {
926 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
927 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
928 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
929 char_array_4[3] = char_array_3[2] & 0x3f;
930
931 for(i = 0; (i <4) ; i++)
932 {
933 ret += base64Chars[char_array_4[i]];
934 }
935 i = 0;
936 }
937 }
938
939 if (i)
940 {
941 for(j = i; j < 3; j++)
942 {
943 char_array_3[j] = '\0';
944 }
945
946 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
947 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
948 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
949 char_array_4[3] = char_array_3[2] & 0x3f;
950
951 for (j = 0; (j < i + 1); j++)
952 {
953 ret += base64Chars[char_array_4[j]];
954 }
955
956 while((i++ < 3))
957 {
958 ret += '=';
959 }
960 }
961
962 return ret;
963}
964

Callers

nothing calls this directly

Calls 1

sizeMethod · 0.80

Tested by

no test coverage detected