moved optional detection to parse

This commit is contained in:
Juri Glass 2011-07-08 16:30:56 +02:00
parent 32c7b5d016
commit 5eec64fa9c
2 changed files with 38 additions and 24 deletions

View file

@ -25,6 +25,10 @@
#include <uuid/uuid.h>
#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]);
}

View file

@ -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);
}