에서
많은 감사가 당신이 모든 데이터를 읽을 필요없이 경량 검사를 수행 할 가정 (가 얻을 것이다으로하는 경우에 예외를 처리하는 것은 효율적이다).
I 간단한 기능 단지 테스트 성공 : 여기
bool can_deserialize(std::istream& is)
{
bool ok = false;
is.seekg(0, std::ios_base::beg);
try
{
boost::archive::binary_iarchive ia(is);
unsigned test = ia.get_library_version();
ok = true;
} catch (...) { }
is.seekg(0, std::ios_base::beg);
is.clear();
return ok;
}
제가 사용되는 간단한 테스트 하네스 ('나쁜 스트림'확인하기 위해 직렬화 전에 data.bin에서 데이터를 조작)
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <fstream>
#include <iostream>
bool can_deserialize(std::istream& is)
{
bool ok = false;
is.seekg(0, std::ios_base::beg);
try
{
boost::archive::binary_iarchive ia(is);
unsigned test = ia.get_library_version();
ok = true;
} catch (...) { }
is.seekg(0, std::ios_base::beg);
is.clear();
return ok;
}
int main()
{
std::vector<int> data = { 19415, -2611, 12092, -3942, -2535, 12105, 21079, 4660, 3,
27131, 13647, 24428, 15159, 9029, 24827, -979, 17194, 25102, -3631,
20914, -3223, 25801, 6652, 26208, -77, 15606, 8764, 1896, 7430, 24323,
-152, 23805, -4259, 11243, 13367, 23559, 19293, 18581, 1639, 15671,
7929, 18386, 5168, 13816, 465, 15801, 16750, -3340, -202, 10412, 11068,
13458, 24304, 14814, 6530, 1178, -974, 12882, 757, 583, 4897, 24541,
12490, -119, 2240, -4833, 569, 24700, 24522, 8708, 9760, 26837, 26060,
20914, -3223, 25801, 6652, 26208, -77, 15606, 8764, 1896, 7430, 24323,
3377, 6972, 25689, 2334, 1567, 21670, 23233, 14711, 4650, -4703, 25057,
16057, 19488, 14575, 18936, 13346, 2779, 5644, 17165, 4526, 4390,
9616, 2413, 14459, -1070, -4079, 22126, 9063, 4362, 8182, 24439, 23625,
7929, 18386, 5168, 13816, 465, 15801, 16750, -3340, -202, 10412, 11068,
4184, 25930, 24767, 2785, 17361, 18033, 12366, 20548, -3831, -4101,
16841, -193, 23217, 6351, 19077, 23565, 10482, 4100, 27488, 15956,
-2577, 7161, 20943, 25708, -2877, 7900, -4564, -3647, 12008, 1648,
10533 };
{
std::ofstream ofs("data.bin", std::ios::out | std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
oa & data;
ofs.flush();
ofs.close();
}
{
std::ifstream ifs("data.bin", std::ios::in | std::ios::binary);
if (can_deserialize(ifs))
{
std::cout << "OK! going to read..." << std::endl;
boost::archive::binary_iarchive ia(ifs);
std::vector<int> cloned;
ia & cloned;
std::cout << "Read " << cloned.size() << " records" << std::endl;
}
else
std::cout << "not OK! -- skipping data read" << std::endl;
}
}
[오류 감지 및 수정] (http://en.wikipedia.org/wiki/Error_detection_and_correction)에 관심이 있거나 [메시지 인증 코드] (http : // ko)와 같은 것을 찾고 계십니까? wikipedia.org/wiki/Message_authentication_codes)? – user786653
나는 '공격자'가 잘못된 데이터를 보내고 데이터가 어떻게 든 서버에 손상되었는지 여부를 검사하지 않고 내 소프트웨어를 파괴하지 못하도록하고 싶기 때문에 오류 탐지 문제를 찾고 있다고 생각합니다. 메시지 인증 코드는 양측 (이 경우 : 서버 및 클라이언트)이 올바른 패킷을 보내고 받기를 원하는 경우에만 작동하는 것처럼 보입니다. (내 말은, atacker가 유효하지 않은 데이터에 대한 유효한 인증 코드를 보내지 못하도록하는 것). 의견을 주셔서 감사합니다, 나는 모든 것을 정확하게 이해하기를 바랍니다. –