简体   繁体   English

C ++:如何在不丢失NUL字符的情况下将std :: string编码为base64

[英]C++: How to encode a std::string into base64 without losing NUL character

I am working on getting someone else's code up and running. 我正在努力启动并运行其他人的代码 The code is written in C++. 该代码用C ++编写。 The part that is failing is when it converts a std::string to base64: 失败的部分是将std :: string转换为base64时:

std::string tmp = "\0";
tmp.append(strUserName);
tmp.append("\0");
tmp.append(strPassword);
tmp = base64_encode(tmp.c_str(), tmp.length());

where base64 is: 其中base64是:

std::string base64_encode(char const* bytes_to_encode, unsigned int in_len) {
    std::string ret;
    int i = 0;
    int j = 0;
    unsigned char char_array_3[3];
    unsigned char char_array_4[4];

    while (in_len--) {
        char_array_3[i++] = *(bytes_to_encode++);
        if (i == 3) {
            char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
            char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
            char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
            char_array_4[3] = char_array_3[2] & 0x3f;

            for(i = 0; (i <4) ; i++)
                ret += base64_chars[char_array_4[i]];
            i = 0;
        }
    }

    if (i)
    {
        for(j = i; j < 3; j++)
            char_array_3[j] = '\0';

        char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
        char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
        char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
        char_array_4[3] = char_array_3[2] & 0x3f;

        for (j = 0; (j < i + 1); j++)
            ret += base64_chars[char_array_4[j]];

        while((i++ < 3))
            ret += '=';

    }

    return ret;

}

It uses the 'tmp' string to make a call to a server and it's imperative that the base64 string has the two NUL characters embedded within it (before strUserName and before strPassword). 它使用“ tmp”字符串来调用服务器,并且必须在base64字符串中嵌入两个NUL字符(在strUserName之前和strPassword之前)。 However, it seems that since the code is passing tmp as a c_str(), the NUL characters are being stripped. 但是,由于代码正在将tmp作为c_str()传递,因此NUL字符已被剥离。 Is there a good solution for this? 有一个好的解决方案吗? Thanks. 谢谢。

Update I guess I should add that the code includes " #include <asm/errno.h> " which I googled for and didn't find compatibility for macOS so I just commented it out.. Not sure if that is making things not work but I doubt it. 更新,我想我应该补充一点,代码中包括“ #include <asm/errno.h> ”,我用它搜索了一下,但没有发现与macOS的兼容性,所以我只是将其注释掉了。但我对此表示怀疑。 Full disclosure. 全面披露。

std::string tmp = "\\0"; and tmp.append("\\0"); tmp.append("\\0"); don't add any '\\0' characters to tmp . 不要在tmp添加任何'\\0'字符。 The versions of std::string::string and std::string::append that take a const char* take a NUL-terminated C-style string, so they stop as soon as they see a NUL character. 带有const char*std::string::stringstd::string::append的版本采用NUL终止的C样式字符串,因此一旦看到NUL字符,它们就会停止。

To actually add a NUL character to your string, you'll need to use the constructor and append methods that take a length along with a const char* , or the versions that take a count and a char : 要将NUL字符实际添加到字符串中,您需要使用构造函数并append带有const char*长度的方法或带有count和char的版本的方法:

std::string tmp("\0", 1);
tmp.append(strUserName);
tmp.append("\0", 1);
tmp.append(strPassword);
tmp = base64_encode(tmp.c_str(), tmp.length());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM