120 lines
3.1 KiB
C++
120 lines
3.1 KiB
C++
/*
|
|
* ProtectedDataImageF150221.hpp
|
|
*
|
|
* Created on:
|
|
* Author: titov
|
|
*/
|
|
|
|
#ifndef UMLIBRARY_DRIVER_PROTECTEDDATAIMAGEF150221_HPP_
|
|
#define UMLIBRARY_DRIVER_PROTECTEDDATAIMAGEF150221_HPP_
|
|
|
|
#include "DataImageF150221.hh"
|
|
#include "../common/Crc.hpp"
|
|
|
|
namespace driver { namespace bf {
|
|
|
|
template<class Crc16Type = common::crc::Crc16_CCITT, class Crc32Type = common::crc::Crc32_C>
|
|
struct ProtectedDataImageF150221 : public DataImageF150221 {
|
|
typedef Crc16Type DataCrc;
|
|
typedef Crc32Type InfoCrc;
|
|
|
|
void update_info_crc();
|
|
void update_data_crc( Id id );
|
|
void update_crc();
|
|
|
|
bool check_info_crc() const;
|
|
bool check_data_crc( Id id ) const;
|
|
bool check_crc() const;
|
|
};
|
|
|
|
}
|
|
}
|
|
|
|
template< class Crc16Type,
|
|
class Crc32Type>
|
|
inline void driver::bf::ProtectedDataImageF150221<Crc16Type, Crc32Type>::update_info_crc() {
|
|
|
|
typename InfoCrc::crc_t crc;
|
|
|
|
for( std::size_t i = 0; i < head->setting_count; ++i ) {
|
|
crc = common::crc::crcCalc<InfoCrc>( reinterpret_cast<const char *>( &info[i] ), sizeof(info[i].id) + sizeof(info[i].size), crc );
|
|
}
|
|
|
|
head->info_crc = crc.value();
|
|
|
|
}
|
|
|
|
template< class Crc16Type,
|
|
class Crc32Type>
|
|
inline void driver::bf::ProtectedDataImageF150221<Crc16Type, Crc32Type>::update_data_crc(
|
|
Id id ) {
|
|
|
|
Setting setting = extract_setting(id);
|
|
|
|
if( setting.buffer ) {
|
|
setting.info->crc = common::crc::crcCalc<DataCrc>( setting.buffer, setting.info->size ).value();
|
|
}
|
|
|
|
}
|
|
|
|
template< class Crc16Type,
|
|
class Crc32Type>
|
|
inline void driver::bf::ProtectedDataImageF150221<Crc16Type, Crc32Type>::update_crc() {
|
|
|
|
update_info_crc();
|
|
|
|
for( std::size_t i = 0; i < head->setting_count; ++i )
|
|
update_data_crc( info[i].id );
|
|
|
|
}
|
|
|
|
template< class Crc16Type,
|
|
class Crc32Type>
|
|
inline bool driver::bf::ProtectedDataImageF150221<Crc16Type, Crc32Type>::check_info_crc() const {
|
|
|
|
typename InfoCrc::crc_t crc;
|
|
|
|
for( std::size_t i = 0; i < head->setting_count; ++i ) {
|
|
crc = common::crc::crcCalc<InfoCrc>( reinterpret_cast<const char *>(&info[i] ), sizeof(info[i].id) + sizeof(info[i].size), crc );
|
|
}
|
|
|
|
return head->info_crc == crc.value();
|
|
|
|
}
|
|
|
|
template< class Crc16Type,
|
|
class Crc32Type>
|
|
inline bool driver::bf::ProtectedDataImageF150221<Crc16Type, Crc32Type>::check_data_crc(
|
|
Id id ) const {
|
|
|
|
Setting setting = extract_setting(id);
|
|
|
|
if( setting.buffer )
|
|
return setting.info->crc == common::crc::crcCalc<DataCrc>( setting.buffer, setting.info->size ).value();
|
|
|
|
return false;
|
|
}
|
|
|
|
template< class Crc16Type,
|
|
class Crc32Type>
|
|
inline bool driver::bf::ProtectedDataImageF150221<Crc16Type, Crc32Type>::check_crc() const {
|
|
|
|
if( not check_info_crc() )
|
|
return false;
|
|
|
|
for( std::size_t i = 0; i < head->setting_count; ++i ) {
|
|
|
|
Setting setting = extract_setting(info[i].id);
|
|
|
|
if( not setting.buffer
|
|
or setting.info->crc != common::crc::crcCalc<DataCrc>( setting.buffer, setting.info->size ).value() )
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif /* UMLIBRARY_DRIVER_PROTECTEDDATAIMAGEF150221_HPP_ */
|