[英]How can I execute a jar file included in c++ with a Base64 string without write it on the HDD?
[英]C++: How to encode a std::string into base64 without losing NUL character
我正在努力启动并运行其他人的代码 。 该代码用C ++编写。 失败的部分是将std :: string转换为base64时:
std::string tmp = "\0";
tmp.append(strUserName);
tmp.append("\0");
tmp.append(strPassword);
tmp = base64_encode(tmp.c_str(), tmp.length());
其中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;
}
它使用“ tmp”字符串来调用服务器,并且必须在base64字符串中嵌入两个NUL字符(在strUserName之前和strPassword之前)。 但是,由于代码正在将tmp作为c_str()传递,因此NUL字符已被剥离。 有一个好的解决方案吗? 谢谢。
更新,我想我应该补充一点,代码中包括“ #include <asm/errno.h>
”,我用它搜索了一下,但没有发现与macOS的兼容性,所以我只是将其注释掉了。但我对此表示怀疑。 全面披露。
std::string tmp = "\\0";
和tmp.append("\\0");
不要在tmp
添加任何'\\0'
字符。 带有const char*
的std::string::string
和std::string::append
的版本采用NUL终止的C样式字符串,因此一旦看到NUL字符,它们就会停止。
要将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.