boost :: archive를 사용하는 응용 프로그램에서 표준이 아닌 표준 문자열을 조사하고 있습니다. 비표준 문자열은 아래 예제와 같이 비 관입 스타일로 정의 된 (비) 직렬화를가집니다. 직렬화 및 직렬화 해제는 예상대로 작동하지만 이식 된 응용 프로그램이 이전 메시지를 받으면 잘못된 할당으로 인해 충돌합니다. 이것은 문자열의 크기 앞에 5 바이트 (모두 0)가 삽입되어 발생합니다.비 침입 직렬화가 5 바이트 0 프리픽스를 추가하는 이유는 무엇입니까?
이러한 5 개의 추가 바이트가 삽입되는 이유는 무엇입니까? 어떤 종류의 마술 마커인가요?
예 :
#include <iostream>
#include <string>
#include <sstream>
#include <boost/serialization/split_free.hpp>
#include <boost/archive/binary_oarchive.hpp>
struct own_string { // simplified custom string class
std::string content;
};
namespace boost
{
namespace serialization
{
template<class Archive>
inline void save(
Archive & ar,
const own_string & t,
const unsigned int /* file_version */)
{
size_t size = t.content.size();
ar << size;
ar.save_binary(&t.content[0], size);
}
template<class Archive>
inline void load(
Archive & ar,
own_string & t,
const unsigned int /* file_version */)
{
size_t size;
ar >> size;
t.content.resize(size);
ar.load_binary(&t.content[0], size);
}
// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<class Archive>
inline void serialize(
Archive & ar,
own_string & t,
const unsigned int file_version)
{
boost::serialization::split_free(ar, t, file_version);
}
} // namespace serialization
} // namespace boost
std::string string_to_hex(const std::string& input)
{
static const char* const lut = "ABCDEF";
size_t len = input.length();
std::string output;
output.reserve(2 * len);
for (size_t i = 0; i < len; ++i)
{
const unsigned char c = input[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
void test_normal_string()
{
std::stringstream ss;
boost::archive::binary_oarchive ar{ss};
std::string test = "";
std::cout << string_to_hex(ss.str()) << std::endl;
ar << test;
//adds 00 00 00 00 00 00 00 00
std::cout << string_to_hex(ss.str()) << std::endl;
}
void test_own_string()
{
std::stringstream ss;
boost::archive::binary_oarchive ar{ss};
std::string test = "";
own_string otest{test};
std::cout << string_to_hex(ss.str()) << std::endl;
ar << otest;
//adds 00 00 00 00 00 00 00 00 00 00 00 00 00
std::cout << string_to_hex(ss.str()) << std::endl;
}
int main()
{
test_normal_string();
test_own_string();
}
'오래된 메시지'가 무엇인가요? 여분의 바이트에 관해서는, 나는 그것이 단지 버전 데이터라고 생각할 것이다 ... –
오래된 메시지는 커스텀 문자열이 사용 된 메시지이다. – choeger
@ choeger 혼란을 없애기 위해 질문 문구를 수정하십시오! +1 자체 우수 샘플 포함 – sehe