Skip to content

Commit

Permalink
Fieldset: fix a few to/from string conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Jan 22, 2023
1 parent e38ad0f commit 6d6c5e9
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ int field_to_string(char *buf, size_t len, ProtoViewField *f) {
case FieldTypeBytes:
{
uint64_t idx = 0;
uint32_t nibble_num = 0;
while(idx < len-1 && nibble_num < f->len) {
uint32_t nibble_num = f->len;
while(idx < len-1 && nibble_num) {
const char *charset = "0123456789ABCDEF";
uint32_t nibble = nibble_num & 1 ?
(f->bytes[nibble_num/2] >> 4) :
(f->bytes[nibble_num/2] & 0xf);
(f->bytes[idx/2] >> 4) :
(f->bytes[idx/2] & 0xf);
buf[idx++] = charset[nibble];
nibble_num++;
nibble_num--;
}
buf[idx] = 0;
return idx;
Expand All @@ -93,9 +93,11 @@ int field_to_string(char *buf, size_t len, ProtoViewField *f) {
* new value, otherwise if the specified value is invalid for the
* field type, false is returned. */
bool field_set_from_string(ProtoViewField *f, char *buf, size_t len) {
long long val;
unsigned long long uval;
float fval;
// Initialize values to zero since the Flipper sscanf() implementation
// is fuzzy... may populate only part of the value.
long long val = 0;
unsigned long long uval = 0;
float fval = 0;

switch(f->type) {
case FieldTypeStr:
Expand Down Expand Up @@ -143,15 +145,15 @@ bool field_set_from_string(ProtoViewField *f, char *buf, size_t len) {
uint8_t nibble = 0;
char c = toupper(buf[idx]);
if (c >= '0' && c <= '9') nibble = c-'0';
else if (c >= 'A' && c <= 'F') nibble = c-'A';
else if (c >= 'A' && c <= 'F') nibble = 10+(c-'A');
else return false;

if (nibble_idx & 1) {
f->bytes[idx/2] =
(f->bytes[idx/2] & 0x0F) | (nibble<<4);
(f->bytes[idx/2] & 0xF0) | nibble;
} else {
f->bytes[idx/2] =
(f->bytes[idx/2] & 0xF0) | nibble;
(f->bytes[idx/2] & 0x0F) | (nibble<<4);
}
nibble_idx--;
idx++;
Expand Down

0 comments on commit 6d6c5e9

Please sign in to comment.