From 5eec64fa9cebbd8b2cc5bef34e56fa8b7ac76fd2 Mon Sep 17 00:00:00 2001 From: Juri Glass Date: Fri, 8 Jul 2011 16:30:56 +0200 Subject: [PATCH] moved optional detection to parse --- sml/src/sml_octet_string.c | 54 ++++++++++++++++++-------------- test/src/sml_octet_string_test.c | 8 +++++ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/sml/src/sml_octet_string.c b/sml/src/sml_octet_string.c index 4ce8e4c..9a43787 100644 --- a/sml/src/sml_octet_string.c +++ b/sml/src/sml_octet_string.c @@ -25,6 +25,10 @@ #include #endif +uint8_t ctoi(uint8_t c); +uint8_t c2toi(uint8_t c1, uint8_t c2); +uint8_t c2ptoi(char* c); + octet_string *sml_octet_string_init(unsigned char *str, int length) { octet_string *s = (octet_string *)malloc(sizeof(octet_string)); memset(s, 0, sizeof(octet_string)); @@ -36,29 +40,6 @@ octet_string *sml_octet_string_init(unsigned char *str, int length) { return s; } -uint8_t ctoi(uint8_t c){ - uint8_t ret = 0; - - if((c >= '0') && (c <= '9')){ - ret = c - '0'; - } else if((c >= 'a') && (c <= 'f')){ - ret = c - 'a' + 10; - } else if((c >= 'A') && (c <= 'F')){ - ret = c - 'A' + 10; - } - - return ret; -} - - -inline uint8_t c2toi(uint8_t c1, uint8_t c2){ - return ctoi(c1) << 4 | ctoi(c2); -} - -inline uint8_t c2ptoi(char* c){ - return ctoi((uint8_t)c[0]) << 4 | ctoi((uint8_t)c[1]); -} - octet_string *sml_octet_string_init_from_hex(char *str) { int i, len = strlen(str); if (len % 2 != 0) { @@ -81,6 +62,10 @@ void sml_octet_string_free(octet_string *str) { } octet_string *sml_octet_string_parse(sml_buffer *buf) { + if (sml_buf_optional_is_skipped(buf)) { + return 0; + } + int l; if (sml_buf_get_next_type(buf) != SML_TYPE_OCTET_STRING) { buf->error = 1; @@ -98,7 +83,6 @@ octet_string *sml_octet_string_parse(sml_buffer *buf) { return str; } - void sml_octet_string_write(octet_string *str, sml_buffer *buf) { sml_buf_set_type_and_length(buf, SML_TYPE_OCTET_STRING, (unsigned int) str->len); memcpy(sml_buf_get_current_buf(buf), str->str, str->len); @@ -127,6 +111,28 @@ int sml_octet_string_cmp_with_hex(octet_string *str, char *hex) { return result; } +uint8_t ctoi(uint8_t c){ + uint8_t ret = 0; + + if((c >= '0') && (c <= '9')) { + ret = c - '0'; + } + else if((c >= 'a') && (c <= 'f')) { + ret = c - 'a' + 10; + } + else if((c >= 'A') && (c <= 'F')) { + ret = c - 'A' + 10; + } + return ret; +} + +uint8_t c2toi(uint8_t c1, uint8_t c2) { + return ctoi(c1) << 4 | ctoi(c2); +} + +uint8_t c2ptoi(char* c) { + return ctoi((uint8_t)c[0]) << 4 | ctoi((uint8_t)c[1]); +} diff --git a/test/src/sml_octet_string_test.c b/test/src/sml_octet_string_test.c index a1aafc6..c6b0fbd 100644 --- a/test/src/sml_octet_string_test.c +++ b/test/src/sml_octet_string_test.c @@ -57,10 +57,18 @@ TEST(sml_octet_string, parse_multiple_tl_fields) { expected_octet_string(str, "aaaaoaaaaaaaaaaa", 16); } +TEST(sml_octet_string, parse_optional) { + hex2binary("01", sml_buf_get_current_buf(buf)); + octet_string *str = sml_octet_string_parse(buf); + TEST_ASSERT_NULL(str); + TEST_ASSERT_EQUAL(1, buf->cursor); +} + TEST_GROUP_RUNNER(sml_octet_string) { RUN_TEST_CASE(sml_octet_string, init); RUN_TEST_CASE(sml_octet_string, parse); RUN_TEST_CASE(sml_octet_string, parse_multiple_tl_fields); + RUN_TEST_CASE(sml_octet_string, parse_optional); }