mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-08 01:07:06 +01:00
fix hexDecode result & input checks
- for some reason we were never checking it's output no need to increase +1 since we write the exact number - allow hexEncode output be decoded, enforce even-ness - raw msg must have at least 3 bytes - start+code+end
This commit is contained in:
@@ -812,10 +812,9 @@ size_t hexEncode(const uint8_t * in, size_t in_size, char * out, size_t out_size
|
||||
|
||||
// From an hexa char array ("A220EE...") to a byte array (half the size)
|
||||
size_t hexDecode(const char* in, size_t in_size, uint8_t* out, size_t out_size) {
|
||||
if (out_size < (in_size / 2)) return 0;
|
||||
|
||||
size_t index = 0;
|
||||
size_t out_index = 0;
|
||||
if ((in_size & 1) || (out_size < (in_size / 2))) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto char2byte = [](char ch) -> uint8_t {
|
||||
if ((ch >= '0') && (ch <= '9')) {
|
||||
@@ -829,13 +828,22 @@ size_t hexDecode(const char* in, size_t in_size, uint8_t* out, size_t out_size)
|
||||
}
|
||||
};
|
||||
|
||||
while (index < in_size) {
|
||||
out[out_index] = char2byte(in[index]) << 4;
|
||||
out[out_index] += char2byte(in[index + 1]);
|
||||
size_t index = 0;
|
||||
size_t out_index = 0;
|
||||
|
||||
index += 2;
|
||||
out_index += 1;
|
||||
uint8_t lhs, rhs;
|
||||
|
||||
while (index < in_size) {
|
||||
lhs = char2byte(in[index]) << 4;
|
||||
rhs = char2byte(in[index + 1]);
|
||||
if (lhs || rhs) {
|
||||
out[out_index++] = lhs | rhs;
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
out_index = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return out_index ? (1 + out_index) : 0;
|
||||
return out_index;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user