Skip to content
Snippets Groups Projects
Commit 68a52a4b authored by Michael Davis's avatar Michael Davis
Browse files

[checksum] Implements ChecksumBlob methods

parent 3e2e0886
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iomanip>
#include "ChecksumBlob.hpp"
#include "ChecksumBlobSerDeser.hpp"
......@@ -86,18 +88,33 @@ size_t ChecksumBlob::length() const {
}
void ChecksumBlob::deserialize(const std::string &bytearray) {
//ProtobufToChecksumBlob(const common::ChecksumBlob &p_csb, checksum::ChecksumBlob &csb)
common::ChecksumBlob p_csb;
p_csb.ParseFromString(bytearray);
ProtobufToChecksumBlob(p_csb, *this);
}
static std::string ChecksumValueHex(const std::string &bytearray) {
if(bytearray.empty()) return "0";
std::stringstream value;
value << std::hex << std::setfill('0') << std::setw(2);
for(auto c = bytearray.end(); c != bytearray.begin(); ) {
--c;
value << static_cast<uint8_t>(*c);
}
return value.str();
}
std::ostream &operator<<(std::ostream &os, const ChecksumBlob &csb) {
throw exception::Exception("not implemented");
#if 0
os << "(";
os << "[ ";
auto num_els = csb.m_cs.size();
for(auto &cs : csb.m_cs) {
os << " " << cs << " ";
bool is_last_el = --num_els > 0;
os << "{ \"" << ChecksumTypeName.at(cs.first) << "\",0x" << ChecksumValueHex(cs.second)
<< (is_last_el ? " }," : " }");
}
os << ")";
#endif
os << " ]";
return os;
}
......
......@@ -79,10 +79,37 @@ public:
*/
void insert(ChecksumType type, uint32_t value);
/*!
* Deserialize from a byte array
*/
void deserialize(const std::string &bytearray);
/*!
* Serialise to a byte array
*/
std::string serialize() const;
/*!
* Length of the serialized byte array
*/
size_t length() const;
/*!
* True if there are no checksums in the blob
*/
bool empty() const { return m_cs.empty(); }
/*!
* Get a const reference to the implementation (for conversion to protobuf)
*/
const std::map<ChecksumType,std::string> &getMap() const {
return m_cs;
}
/*!
* Return the checksum for the specified key
*/
std::string at(ChecksumType type) {
std::string at(ChecksumType type) const {
return m_cs.at(type);
}
......@@ -91,6 +118,11 @@ public:
*/
void validate(ChecksumType type, const std::string &value) const;
/*!
* Check all the checksums in the blob match, throw an exception if they don't
*/
void validate(const ChecksumBlob &blob) const;
/*!
* Returns true if the checksum is in the blob and that it has the value expected
*/
......@@ -105,11 +137,6 @@ public:
return true;
}
/*!
* Check all the checksums in the blob match, throw an exception if they don't
*/
void validate(const ChecksumBlob &blob) const;
/*!
* Returns true if all the checksums in the blob match
*/
......@@ -127,27 +154,13 @@ public:
}
/*!
* Serialise to a byte array
*/
std::string serialize() const;
/*!
* Length of the serialized byte array
* Convert little-endian byte array to hexadecimal string
*/
size_t length() const;
/*!
* True if there are no checksums in the blob
*/
bool empty() const { return m_cs.empty(); }
/*!
* Deserialize from a byte array
*/
void deserialize(const std::string &bytearray);
static std::string ChecksumValueHex(const std::string &bytearray);
private:
friend std::ostream &operator<<(std::ostream &os, const ChecksumBlob &csb);
std::map<ChecksumType,std::string> m_cs;
};
......
......@@ -30,19 +30,33 @@ void ProtobufToChecksumBlob(const common::ChecksumBlob &p_csb, checksum::Checksu
checksum::ChecksumType type;
switch(cs.type()) {
case common::ChecksumBlob::Checksum::ADLER32: type = ADLER32; break;
case common::ChecksumBlob::Checksum::CRC32: type = CRC32; break;
case common::ChecksumBlob::Checksum::CRC32C: type = CRC32C; break;
case common::ChecksumBlob::Checksum::MD5: type = MD5; break;
case common::ChecksumBlob::Checksum::SHA1: type = SHA1; break;
case common::ChecksumBlob::Checksum::CRC32: type = CRC32; break;
case common::ChecksumBlob::Checksum::CRC32C: type = CRC32C; break;
case common::ChecksumBlob::Checksum::MD5: type = MD5; break;
case common::ChecksumBlob::Checksum::SHA1: type = SHA1; break;
case common::ChecksumBlob::Checksum::NONE:
default: type = NONE; break;
default: type = NONE; break;
}
csb.insert(type, cs.value());
}
}
void ChecksumBlobToProtobuf(const checksum::ChecksumBlob &csb, common::ChecksumBlob &p_csb) {
throw exception::Exception("not implemented");
for(auto &cs : csb.getMap()) {
common::ChecksumBlob::Checksum::Type type;
switch(cs.first) {
case ADLER32: type = common::ChecksumBlob::Checksum::ADLER32; break;
case CRC32: type = common::ChecksumBlob::Checksum::CRC32; break;
case CRC32C: type = common::ChecksumBlob::Checksum::CRC32C; break;
case MD5: type = common::ChecksumBlob::Checksum::MD5; break;
case SHA1: type = common::ChecksumBlob::Checksum::SHA1; break;
case NONE:
default: type = common::ChecksumBlob::Checksum::NONE; break;
}
auto cs_ptr = p_csb.add_cs();
cs_ptr->set_type(type);
cs_ptr->set_value(cs.second);
}
}
}} // namespace cta::checksum
......@@ -70,6 +70,17 @@ TEST_F(cta_ChecksumBlobTest, invalid_checksums) {
checksumBlob.insert(MD5, 0x0A141E28);
}
TEST_F(cta_ChecksumBlobTest, bytearray_to_hex) {
using namespace cta::checksum;
ChecksumBlob checksumBlob;
checksumBlob.insert(ADLER32, 0x00120abc);
std::stringstream ss;
ss << checksumBlob;
ASSERT_EQ(ss.str(), "[ {\"ADLER32\",0x00120abc} ]");
}
TEST_F(cta_ChecksumBlobTest, serialize_deserialize) {
using namespace cta::checksum;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment