Fix wrong length calcuation in *_to_buffer utilities

This commit is contained in:
Doridian 2022-06-14 15:56:04 -07:00
commit 824d2129a0

View file

@ -164,7 +164,7 @@ void ascii_to_buffer(uint8_t *buf, const uint8_t *hex_data, const size_t hex_len
size_t i = 0; size_t i = 0;
for (i = 0; i < max_len; ++i, tmp++) { for (i = 0; i < max_len; ++i, tmp++) {
char c = hex_data[i]; char c = hex_data[i];
snprintf(tmp, hex_max_len - (tmp - tmp_base), "%c", ((c < 32) || (c == 127)) ? '.' : c); *tmp = ((c < 32) || (c == 127)) ? '.' : c;
} }
size_t m = (min_str_len > i) ? min_str_len : 0; size_t m = (min_str_len > i) ? min_str_len : 0;
@ -172,7 +172,7 @@ void ascii_to_buffer(uint8_t *buf, const uint8_t *hex_data, const size_t hex_len
m = hex_max_len; m = hex_max_len;
for (; i < m; i++, tmp++) for (; i < m; i++, tmp++)
snprintf(tmp, hex_max_len - (tmp - tmp_base), " "); *tmp = ' ';
// remove last space // remove last space
*tmp = '\0'; *tmp = '\0';
@ -187,13 +187,14 @@ void hex_to_buffer(uint8_t *buf, const uint8_t *hex_data, const size_t hex_len,
char *tmp = tmp_base; char *tmp = tmp_base;
size_t max_len = (hex_len > hex_max_len) ? hex_max_len : hex_len; size_t max_len = (hex_len > hex_max_len) ? hex_max_len : hex_len;
size_t str_max_len = hex_max_len * (2 + spaces_between);
size_t i; size_t i;
for (i = 0; i < max_len; ++i, tmp += 2 + spaces_between) { for (i = 0; i < max_len; ++i, tmp += 2 + spaces_between) {
snprintf(tmp, hex_max_len - (tmp - tmp_base), (uppercase) ? "%02X" : "%02x", (unsigned int) hex_data[i]); snprintf(tmp, str_max_len - (tmp - tmp_base), (uppercase) ? "%02X" : "%02x", (unsigned int) hex_data[i]);
for (size_t j = 0; j < spaces_between; j++) for (size_t j = 0; j < spaces_between; j++)
snprintf(tmp + 2 + j, hex_max_len - ((tmp + 2 + j) - tmp_base), " "); *(tmp + 2 + j) = ' ';
} }
i *= (2 + spaces_between); i *= (2 + spaces_between);
@ -203,7 +204,7 @@ void hex_to_buffer(uint8_t *buf, const uint8_t *hex_data, const size_t hex_len,
m = hex_max_len; m = hex_max_len;
for (; i < m; i++, tmp++) for (; i < m; i++, tmp++)
snprintf(tmp, hex_max_len - (tmp - tmp_base), " "); *tmp = ' ';
// remove last space // remove last space
*tmp = '\0'; *tmp = '\0';