Skip to content

Commit

Permalink
chore(pam.hpp): factor out & fix name
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmp01 committed Nov 5, 2024
1 parent ca7ee2c commit 7c0a8c4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
34 changes: 1 addition & 33 deletions cpp/contracts/pam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,38 +64,6 @@ namespace eosio {
return true;
}

bytes _fromUTF8EncodedToBytes(const bytes &utf8_encoded) {
check(utf8_encoded.size() % 2 == 0, "invalid utf-8 encoded string");

bytes x(utf8_encoded.size() / 2, 0); // fill it with zeros

uint64_t k = 0;
uint8_t b1, b2;
for (uint64_t i = 0; i < utf8_encoded.size(); i += 2) {
b1 = utf8_encoded[i];
b2 = utf8_encoded[i + 1];

if ((b1 >= 97) && (b1 <= 102)) { // [a, b, c, ..., f]
b1 -= 87;
} else if ((b1 >= 65) && (b1 <= 70)) { // [A, B, C, ..., F]
b1 -= 55;
} else if ((b1 >= 48) && (b1 <= 57)) { // [0, 1, 2, ... ,9]
b1 -= 48;
}
if ((b2 >= 97) && (b2 <= 102)) {
b2 -= 87;
} else if ((b2 >= 65) && (b2 <= 70)) {
b2 -= 55;
} else if ((b2 >= 48) && (b2 <= 57)) {
b2 -= 48;
}

x[k++] = b1 * 16 + b2;
}

return x;
}

void check_authorization(name adapter, const operation& operation, const metadata& metadata, checksum256& event_id) {
// Metadata preimage format:
// | version | protocol | origin | blockHash | txHash | eventPayload |
Expand Down Expand Up @@ -157,7 +125,7 @@ namespace eosio {
bytes raw_data(start, end);

bytes event_data = protocol_id == 2
? _fromUTF8EncodedToBytes(raw_data)
? from_utf8_encoded_to_bytes(raw_data)
: raw_data;

offset = 0;
Expand Down
28 changes: 28 additions & 0 deletions cpp/contracts/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,32 @@ namespace eosio {
name name_value(name_str);
return name_value;
}

uint8_t from_hex_char_to_uint8(uint8_t x) {
if ((x >= 97) && (x <= 102)) { // [a, b, c, ..., f]
x -= 87;
} else if ((x >= 65) && (x <= 70)) { // [A, B, C, ..., F]
x -= 55;
} else if ((x >= 48) && (x <= 57)) { // [0, 1, 2, ... ,9]
x -= 48;
}

return x;
}

bytes from_utf8_encoded_to_bytes(const bytes &utf8_encoded) {
check(utf8_encoded.size() % 2 == 0, "invalid utf-8 encoded string");

bytes x(utf8_encoded.size() / 2, 0); // fill it with zeros

uint64_t k = 0;
uint8_t b1, b2;
for (uint64_t i = 0; i < utf8_encoded.size(); i += 2) {
b1 = from_hex_char_to_uint8(utf8_encoded[i]);
b2 = from_hex_char_to_uint8(utf8_encoded[i + 1]);
x[k++] = b1 * 16 + b2;
}

return x;
}
}

0 comments on commit 7c0a8c4

Please sign in to comment.