From 784dc1cecb4c9343ce6215c41b331df5d2f729f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Fri, 6 Dec 2013 23:10:32 +0100 Subject: [PATCH] Teach uc_hex_decode to skip spaces --- include/ucore/string.h | 4 ++-- src/hex.c | 18 +++++++++++++++- test/test_hex.c | 47 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/include/ucore/string.h b/include/ucore/string.h index 5bcc0cb..3fb3b1c 100644 --- a/include/ucore/string.h +++ b/include/ucore/string.h @@ -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 * diff --git a/src/hex.c b/src/hex.c index bf9a04d..e361856 100644 --- a/src/hex.c +++ b/src/hex.c @@ -1,4 +1,5 @@ #include +#include #include #include #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]; diff --git a/test/test_hex.c b/test/test_hex.c index 212e0c8..1c6fa2a 100644 --- a/test/test_hex.c +++ b/test/test_hex.c @@ -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);