Teach uc_hex_decode to skip spaces
This commit is contained in:
@@ -20,8 +20,8 @@ extern "C" {
|
||||
*/
|
||||
char* uc_hex_encode(const uint8_t *in, size_t len, char *out);
|
||||
|
||||
/** Decode hex. Will stop at the first non-hex digit
|
||||
* (anything not 0-9,a-f or A-F)
|
||||
/** Decode hex. Will stop at the first non-convertible hex-digit
|
||||
* spaces(' ','\t,'\r','\n') in input are skipped
|
||||
* Subtract the return value from the passed in @out to find
|
||||
* the length of the converted data
|
||||
*
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include "ucore/string.h"
|
||||
@@ -79,14 +80,29 @@ uc_hex_decode(const char *in, size_t maxlen,uint8_t *out)
|
||||
{
|
||||
size_t i, t;
|
||||
|
||||
for (t = 0,i = 0; i + 1 < maxlen && in[i] && in[i + 1]; i+=2 , ++t) {
|
||||
for (t = 0,i = 0; i + 1 < maxlen ;) {
|
||||
int hn, ln;
|
||||
if (!(in[i] && in[i + 1])) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (in[i] == ' ' ||
|
||||
in[i] == '\t' ||
|
||||
in[i] == '\r' ||
|
||||
in[i] == '\n') {
|
||||
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
hn = char_to_bin(in[i]);
|
||||
ln = char_to_bin(in[i + 1]);
|
||||
if (hn == -1 || ln == -1)
|
||||
return NULL;
|
||||
|
||||
out[t] = (hn << 4 ) | ln;
|
||||
i += 2;
|
||||
t++;
|
||||
}
|
||||
|
||||
return &out[t];
|
||||
|
||||
+45
-2
@@ -87,7 +87,7 @@ END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_empty_string)
|
||||
char hex[] = "";
|
||||
uint8_t binary[4] = {0x11, 0x11, 0x11};
|
||||
uint8_t binary[] = {0x11, 0x11, 0x11};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, 2, binary);
|
||||
@@ -95,7 +95,47 @@ START_TEST (test_uc_hex_decode_empty_string)
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x11);
|
||||
fail_if(binary[2] != 0x11);
|
||||
fail_if(res != binary);
|
||||
fail_if(res != binary, "length %zu\n", res - binary);
|
||||
|
||||
END_TEST
|
||||
START_TEST (test_uc_hex_decode_spaces)
|
||||
char hex[] = " 11 1213";
|
||||
uint8_t binary[3];
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, strlen(hex), binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x12);
|
||||
fail_if(binary[2] != 0x13);
|
||||
fail_if(res != binary+3, "length %zu\n", res - binary);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_spaces2)
|
||||
char hex[] = "1112 13\r\n";
|
||||
uint8_t binary[3];
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, strlen(hex), binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x12);
|
||||
fail_if(binary[2] != 0x13);
|
||||
fail_if(res != binary+3, "length %zu\n", res - binary);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_spaces_empty)
|
||||
char hex[] = " \r\n ";
|
||||
uint8_t binary[] = {0x11,0x12};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, strlen(hex), binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x12);
|
||||
fail_if(res != binary, "length %zu\n", res - binary);
|
||||
|
||||
END_TEST
|
||||
|
||||
@@ -194,6 +234,9 @@ Suite *hex_suite(void)
|
||||
tcase_add_test(tc, test_uc_hex_decode_1);
|
||||
tcase_add_test(tc, test_uc_hex_decode_2);
|
||||
tcase_add_test(tc, test_uc_hex_decode_zero_length);
|
||||
tcase_add_test(tc, test_uc_hex_decode_spaces);
|
||||
tcase_add_test(tc, test_uc_hex_decode_spaces2);
|
||||
tcase_add_test(tc, test_uc_hex_decode_spaces_empty);
|
||||
tcase_add_test(tc, test_uc_hex_decode_odd_length);
|
||||
tcase_add_test(tc, test_uc_hex_decode_invalid);
|
||||
tcase_add_test(tc, test_uc_hex_decode_empty_string);
|
||||
|
||||
Reference in New Issue
Block a user