From 97e267a4353289d422822375485f6a51e9cfa7ca Mon Sep 17 00:00:00 2001 From: Andriy Byzhynar Date: Fri, 6 Apr 2018 19:33:55 +0300 Subject: [PATCH] Initial implementation Implemented function in PutFile which correctly calculates CRC32 checksum for std::vector Added comparison of calculated CRC checksum with received in PutFile request. Added PutFile response generation in case of CRC sum mismatch Fixed handling of unsigned integer values --- .../application_manager/smart_object_keys.h | 1 + .../src/commands/mobile/put_file_request.cc | 46 ++++++++++++++++--- .../src/smart_object_keys.cc | 1 + src/components/interfaces/MOBILE_API.xml | 7 +++ .../smart_objects/number_schema_item.h | 16 ++++--- .../smart_objects/src/number_schema_item.cc | 2 +- 6 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/components/application_manager/include/application_manager/smart_object_keys.h b/src/components/application_manager/include/application_manager/smart_object_keys.h index 32a2315f235..5a3a5fe8b6e 100644 --- a/src/components/application_manager/include/application_manager/smart_object_keys.h +++ b/src/components/application_manager/include/application_manager/smart_object_keys.h @@ -184,6 +184,7 @@ extern const char* sync_file_name; extern const char* file_name; extern const char* file_type; extern const char* file_size; +extern const char* crc32_check_sum; extern const char* request_type; extern const char* persistent_file; extern const char* file_data; diff --git a/src/components/application_manager/src/commands/mobile/put_file_request.cc b/src/components/application_manager/src/commands/mobile/put_file_request.cc index 602b420ba09..269c9c814e5 100644 --- a/src/components/application_manager/src/commands/mobile/put_file_request.cc +++ b/src/components/application_manager/src/commands/mobile/put_file_request.cc @@ -38,6 +38,22 @@ #include "application_manager/application_impl.h" #include "utils/file_system.h" +#include + +namespace { +/** +* Calculates CRC32 checksum +* @param binary_data - input data for which CRC32 should be calculated +* @return calculated CRC32 checksum +*/ +uint32_t GetCrc32CheckSum(const std::vector& binary_data) { + const std::size_t file_size = binary_data.size(); + boost::crc_32_type result; + result.process_bytes(&binary_data[0], file_size); + return result.checksum(); +} + +} // namespace namespace application_manager { @@ -137,7 +153,7 @@ void PutFileRequest::Run() { is_persistent_file_ = false; bool is_system_file = false; length_ = binary_data.size(); - bool is_download_compleate = true; + bool is_download_complete = true; bool offset_exist = (*message_)[strings::msg_params].keyExists(strings::offset); @@ -187,11 +203,29 @@ void PutFileRequest::Run() { return; } const std::string full_path = file_path + "/" + sync_file_name_; - UNUSED(full_path); + const size_t bin_data_size = binary_data.size(); + + if ((*message_)[strings::msg_params].keyExists(strings::crc32_check_sum)) { + LOG4CXX_TRACE(logger_, "Binary Data Size: " << bin_data_size); + const uint32_t crc_received = + (*message_)[strings::msg_params][strings::crc32_check_sum].asUInt(); + LOG4CXX_TRACE(logger_, "CRC32 SUM Received: " << crc_received); + const uint32_t crc_calculated = GetCrc32CheckSum(binary_data); + LOG4CXX_TRACE(logger_, "CRC32 SUM Calculated: " << crc_calculated); + if (crc_calculated != crc_received) { + SendResponse(false, + mobile_apis::Result::CORRUPTED_DATA, + "CRC Check on file failed. File upload has been cancelled, " + "please retry.", + &response_params); + return; + } + } + LOG4CXX_DEBUG(logger_, - "Wrtiting " << binary_data.size() << "bytes to " << full_path - << " (current size is" - << file_system::FileSize(full_path) << ")"); + "Writing " << bin_data_size << " bytes to " << full_path + << " (current size is" + << file_system::FileSize(full_path) << ")"); mobile_apis::Result::eType save_result = application_manager_.SaveBinary( binary_data, file_path, sync_file_name_, offset_); @@ -211,7 +245,7 @@ void PutFileRequest::Run() { if (!is_system_file) { AppFile file(sync_file_name_, is_persistent_file_, - is_download_compleate, + is_download_complete, file_type_); if (0 == offset_) { diff --git a/src/components/application_manager/src/smart_object_keys.cc b/src/components/application_manager/src/smart_object_keys.cc index c3aba90dd53..75125ec90cb 100644 --- a/src/components/application_manager/src/smart_object_keys.cc +++ b/src/components/application_manager/src/smart_object_keys.cc @@ -148,6 +148,7 @@ const char* sync_file_name = "syncFileName"; const char* file_name = "fileName"; const char* file_type = "fileType"; const char* file_size = "fileSize"; +const char* crc32_check_sum = "crc"; const char* request_type = "requestType"; const char* persistent_file = "persistentFile"; const char* file_data = "fileData"; diff --git a/src/components/interfaces/MOBILE_API.xml b/src/components/interfaces/MOBILE_API.xml index a1c64aecda3..ad9050fcf35 100644 --- a/src/components/interfaces/MOBILE_API.xml +++ b/src/components/interfaces/MOBILE_API.xml @@ -135,6 +135,9 @@ The value being set is read only + + The data sent failed to pass CRC check in receiver end + @@ -5073,6 +5076,9 @@ If offset is set to 0, then length is the total length of the file to be downloaded + + Additional CRC32 checksum to protect data integrity up to 512 Mbits . + @@ -5092,6 +5098,7 @@ + diff --git a/src/components/smart_objects/include/smart_objects/number_schema_item.h b/src/components/smart_objects/include/smart_objects/number_schema_item.h index d549b9891a1..34c5e3a8a69 100644 --- a/src/components/smart_objects/include/smart_objects/number_schema_item.h +++ b/src/components/smart_objects/include/smart_objects/number_schema_item.h @@ -39,6 +39,7 @@ #include "smart_objects/default_shema_item.h" #include "smart_objects/schema_item_parameter.h" #include "utils/convert_utils.h" +#include "utils/helpers.h" namespace NsSmartDeviceLink { namespace NsSmartObjects { @@ -123,15 +124,16 @@ bool TNumberSchemaItem::isValidNumberType(SmartType type) { NumberType value(0); if ((SmartType_Double == type) && (typeid(double) == typeid(value))) { return true; - } else if ((SmartType_Integer == type) && - (typeid(int32_t) == typeid(value) || - typeid(uint32_t) == typeid(value) || - typeid(int64_t) == typeid(value) || - typeid(double) == typeid(value))) { + } else if (((SmartType_Integer == type) || (SmartType_UInteger == type)) && + helpers::Compare( + typeid(value), + typeid(int32_t), + typeid(uint32_t), + typeid(int64_t), + typeid(double))) { return true; - } else { - return false; } + return false; } template diff --git a/src/components/smart_objects/src/number_schema_item.cc b/src/components/smart_objects/src/number_schema_item.cc index 78be9fe85db..9789434523f 100644 --- a/src/components/smart_objects/src/number_schema_item.cc +++ b/src/components/smart_objects/src/number_schema_item.cc @@ -41,7 +41,7 @@ SmartType TNumberSchemaItem::getSmartType() const { template <> SmartType TNumberSchemaItem::getSmartType() const { - return SmartType_Integer; + return SmartType_UInteger; } template <>