Skip to content

Commit

Permalink
Save raw md5sum value in note.xrt.UID and not its ascii hex represent…
Browse files Browse the repository at this point in the history
…ation

Signed-off-by: Sonal Santan <sonal.santan@amd.com>
  • Loading branch information
sonals committed Feb 27, 2025
1 parent 3173399 commit fb4da76
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
50 changes: 36 additions & 14 deletions src/cpp/aiebu/src/common/uid_md5.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,65 @@
#ifndef _AIEBU_COMMON_UID_MD5_H_
#define _AIEBU_COMMON_UID_MD5_H_

#include <vector>
#include <iostream>
#include <boost/uuid/detail/md5.hpp>
#include <boost/algorithm/hex.hpp>

namespace aiebu {

unsigned constexpr md5_size = 16;

class uid_md5 {
boost::uuids::detail::md5 hasher;
boost::uuids::detail::md5 hasher;
std::vector<char> sig = std::vector<char>(md5_size, 0);

public:
uid_md5()
{
}
uid_md5() = default;

void update(const std::vector<uint8_t>& data)
{
hasher.process_bytes(data.data(), data.size());
hasher.process_bytes(data.data(), data.size());
}

std::string calculate()
const std::vector<char>& calculate()
{
// Creating local copy of context, so calculate() return same md5sum on every call.
boost::uuids::detail::md5 hasher_copy = hasher;
boost::uuids::detail::md5::digest_type digest;

hasher_copy.get_digest(digest);
std::memcpy(sig.data(), digest, md5_size);
return sig;

/*
std::stringstream md5;
// Different boost versions model digest_type differently:
// 1. typedef unsigned int(digest_type)[4];
// 2. typedef unsigned char digest_type[16];
// The code sets the print width to number of chars needed to print the
element type
// used by digest_type. This results in the same string representation
for both cases
// which matches with that reported by command line md5sum utility
md5 << std::hex << std::setfill('0');
for (auto ele : digest) {
md5 << std::setw(sizeof(ele) * 2) << (unsigned int)ele;
}
return md5.str();
*/
}

[[nodiscard]] std::string str() const {
std::stringstream md5;
// Different boost versions model digest_type differently:
// 1. typedef unsigned int(digest_type)[4];
// 2. typedef unsigned char digest_type[16];
// The code sets the print width to number of chars needed to print the element type
// used by digest_type. This results in the same string representation for both cases
// which matches with that reported by command line md5sum utility

std::cout << std::hex << std::setfill('0');
md5 << std::hex << std::setfill('0');
for (auto ele : digest) {
md5 << std::setw(sizeof(ele) * 2) << (unsigned int)ele;
for (auto ele : sig) {
auto c = (unsigned char)ele;
md5 << std::setw(sizeof(ele) * 2) << (unsigned int)c;
std::cout << std::setw(sizeof(ele) * 2) << (unsigned int)c << "\n";
}
return md5.str();
}
Expand Down
6 changes: 3 additions & 3 deletions src/cpp/aiebu/src/elf/elfwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,22 @@ add_dynamic_section_segment()

void
elf_writer::
add_note(ELFIO::Elf_Word type, const std::string& name, const std::string& dec)
add_note(ELFIO::Elf_Word type, const std::string& name, const std::vector<char>& dec)
{
ELFIO::section* note_sec = m_elfio.sections.add( name.c_str() );
note_sec->set_type( ELFIO::SHT_NOTE );
note_sec->set_addr_align( 1 );

ELFIO::note_section_accessor note_writer( m_elfio, note_sec );
note_writer.add_note( type, "XRT", dec.c_str(), dec.size() );
note_writer.add_note( type, "XRT", dec.data(), dec.size() );
}

std::vector<char>
elf_writer::
finalize()
{
std::cout << "UID:" << m_uid.calculate() << "\n";
add_note(NT_XRT_UID, ".note.xrt.UID", m_uid.calculate());
std::cout << "UID:" << m_uid.str() << "\n";
std::stringstream stream;
stream << std::noskipws;
//m_elfio.save( "hello_32" );
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/aiebu/src/elf/elfwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class elf_writer
void add_dynamic_section_segment();
std::vector<char> finalize();
void add_text_data_section(std::vector<writer>& mwriter, std::vector<symbol>& syms);
void add_note(ELFIO::Elf_Word type, const std::string& name, const std::string& dec);
void add_note(ELFIO::Elf_Word type, const std::string& name, const std::vector<char>& dec);

public:

Expand Down

0 comments on commit fb4da76

Please sign in to comment.