Skip to content

Commit

Permalink
ut: add hex2string decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea authored and bogdan-iancu committed Apr 18, 2024
1 parent cf17530 commit d5b8122
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
25 changes: 5 additions & 20 deletions modules/aaa_diameter/dm_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2196,8 +2196,8 @@ static cJSON *dict_avp_dec_ip(struct avp_hdr * h, struct dict_avp_data *avp)

static int dict_avp_enc_hex(cJSON *obj, struct dict_avp_data *avp, int _, str *ret)
{
int len, i;
char *buf, *val;
int len;
char *buf;

if ((obj->type & cJSON_String) == 0)
return 1; /* encode it as it is */
Expand All @@ -2207,24 +2207,9 @@ static int dict_avp_enc_hex(cJSON *obj, struct dict_avp_data *avp, int _, str *r
LM_ERR("oom for hex encoding\n");
return -1;
}
val = obj->valuestring;
for (i = 0; i < len / 2; i++) {
if(val[2*i]>='0' && val[2*i]<='9')
buf[i] = (val[2*i]-'0') << 4;
else if(val[2*i]>='a' && val[2*i]<='f')
buf[i] = (val[2*i]-'a'+10) << 4;
else if(val[2*i]>='A' && val[2*i]<='F')
buf[i] = (val[2*i]-'A'+10) << 4;
else goto error;

if(val[2*i+1]>='0' && val[2*i+1]<='9')
buf[i] += val[2*i+1]-'0';
else if(val[2*i+1]>='a' && val[2*i+1]<='f')
buf[i] += val[2*i+1]-'a'+10;
else if(val[2*i+1]>='A' && val[2*i+1]<='F')
buf[i] += val[2*i+1]-'A'+10;
else goto error;
}
len = hex2string(obj->valuestring, len, buf);
if (len < 0)
goto error;
ret->s = buf;
ret->len = len/2;
return 0;
Expand Down
27 changes: 27 additions & 0 deletions ut.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,33 @@ inline static int string2hex(
return orig_len * 2;
}

inline static int hex2string(
/* input */ const char *str, int len,
/* output */ char *hex )
{
int i;
for (i = 0; i < len / 2; i++) {
if(str[2*i]>='0' && str[2*i]<='9')
hex[i] = (str[2*i]-'0') << 4;
else if(str[2*i]>='a' && str[2*i]<='f')
hex[i] = (str[2*i]-'a'+10) << 4;
else if(str[2*i]>='A' && str[2*i]<='F')
hex[i] = (str[2*i]-'A'+10) << 4;
else goto error;

if(str[2*i+1]>='0' && str[2*i+1]<='9')
hex[i] += str[2*i+1]-'0';
else if(str[2*i+1]>='a' && str[2*i+1]<='f')
hex[i] += str[2*i+1]-'a'+10;
else if(str[2*i+1]>='A' && str[2*i+1]<='F')
hex[i] += str[2*i+1]-'A'+10;
else goto error;
}
return i;
error:
return -1;
}

/* portable sleep in microseconds (no interrupt handling now) */

inline static void sleep_us( unsigned int nusecs )
Expand Down

0 comments on commit d5b8122

Please sign in to comment.