Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling of hex encoded key to realm-trawler #6264

Merged
merged 5 commits into from
Feb 7, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions src/realm/exec/realm_trawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ void consolidate_lists(std::vector<T>& list, std::vector<T>& list2)
list.insert(list.end(), list2.begin(), list2.end());
list2.clear();
if (list.size() > 1) {
std::sort(begin(list), end(list), [](T& a, T& b) { return a.start < b.start; });
std::sort(begin(list), end(list), [](T& a, T& b) {
return a.start < b.start;
});

auto prev = list.begin();
for (auto it = list.begin() + 1; it != list.end(); ++it) {
Expand All @@ -79,7 +81,11 @@ void consolidate_lists(std::vector<T>& list, std::vector<T>& list2)
}

// Remove all of the now zero-size chunks from the free list
list.erase(std::remove_if(begin(list), end(list), [](T& chunk) { return chunk.length == 0; }), end(list));
list.erase(std::remove_if(begin(list), end(list),
[](T& chunk) {
return chunk.length == 0;
}),
end(list));
}
}

Expand Down Expand Up @@ -507,7 +513,9 @@ std::string human_readable(uint64_t val)
uint64_t get_size(const std::vector<Entry>& list)
{
uint64_t sz = 0;
std::for_each(list.begin(), list.end(), [&](const Entry& e) { sz += e.length; });
std::for_each(list.begin(), list.end(), [&](const Entry& e) {
sz += e.length;
});
return sz;
}

Expand Down Expand Up @@ -1036,6 +1044,42 @@ void RealmFile::changes() const
}
}

bool is_hex(char c)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used

{
if (c >= '0' && c <= '9')
return true;
if (c >= 'a' && c <= 'f')
return true;
if (c >= 'A' && c <= 'F')
return true;
return false;
}

unsigned int hex_char_to_bin(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
exit(-1);
}

unsigned int hex_to_bin(char first, char second)
{
return (hex_char_to_bin(first) << 4) | hex_char_to_bin(second);
}

char to_hex_char(char c)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used

{
if (c < 10)
return c + '0';
if (c < 16)
return c - 10 + 'a';
return '!';
}

int main(int argc, const char* argv[])
{
if (argc > 1) {
Expand All @@ -1049,12 +1093,20 @@ int main(int argc, const char* argv[])
const char* key_ptr = nullptr;
char key[64];
for (int curr_arg = 1; curr_arg < argc; curr_arg++) {
if (strcmp(argv[curr_arg], "--key") == 0) {
if (strcmp(argv[curr_arg], "--keyfile") == 0) {
std::ifstream key_file(argv[curr_arg + 1]);
key_file.read(key, sizeof(key));
key_ptr = key;
curr_arg++;
}
else if (strcmp(argv[curr_arg], "--hexkey") == 0) {
curr_arg++;
const char* chars = argv[curr_arg];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should have a check on the length here.

for (int idx = 0; idx < 64; ++idx) {
key[idx] = hex_to_bin(chars[idx * 2], chars[idx * 2 + 1]);
}
key_ptr = key;
}
else if (strcmp(argv[curr_arg], "--top") == 0) {
char* end;
curr_arg++;
Expand Down Expand Up @@ -1112,7 +1164,10 @@ int main(int argc, const char* argv[])
}
}
else {
std::cout << "Usage: realm-trawler [-afmsw] [--key crypt_key] [--top top_ref] <realmfile>" << std::endl;
std::cout << "Usage: realm-trawler [-afmsw] [--keyfile file-with-binary-crypt-key] [--hexkey "
"crypt-key-in-hex] [--top "
"top_ref] <realmfile>"
<< std::endl;
std::cout << " f : free list analysis" << std::endl;
std::cout << " m : memory leak check" << std::endl;
std::cout << " s : schema dump" << std::endl;
Expand Down