Skip to content

Commit

Permalink
Add cs_hex_decode()
Browse files Browse the repository at this point in the history
CL: Add cs_hex_decode()
  • Loading branch information
rojer9-fb committed Jan 8, 2020
1 parent b5147e3 commit 4d80044
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 7 deletions.
35 changes: 35 additions & 0 deletions include/common/cs_hex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2014-2018 Cesanta Software Limited
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

/*
* Decodes a hex-encoded string `s` length `len` into `dst`.
* `dst` must have enough space to hold the result.
* `*dec_len` will contain the resulting length of the string in `dst`
* while return value will return number of processed bytes in `src`.
* Return value == len indicates successful processing of all the data.
*/
int cs_hex_decode(const char *s, int len, unsigned char *dst, int *dst_len);

#ifdef __cplusplus
}
#endif
1 change: 1 addition & 0 deletions include/mgos.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "common/cs_dbg.h"
#include "common/cs_file.h"
#include "common/cs_hex.h"
#include "common/json_utils.h"
#include "common/mbuf.h"
#include "common/str_util.h"
Expand Down
2 changes: 1 addition & 1 deletion platforms/cc3200/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ MGOS_SRCS += mgos_event.c \
mgos_config_util.c mgos_sys_config.c \
mgos_dlsym.c mgos_system.c \
$(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \
cs_crc32.c cs_file.c \
cs_crc32.c cs_file.c cs_hex.c \
cs_frbuf.c mgos_file_utils.c mgos_utils.c \
cs_rbuf.c mgos_core_dump.c mgos_uart.c \
boot.c frozen.c json_utils.c
Expand Down
2 changes: 1 addition & 1 deletion platforms/cc3220/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ VPATH += $(SDK_PATH)/source/ti/devices/cc32xx/driverlib \
SDK_CFLAGS = -DTARGET_IS_CC3220 -DUSE_CC3220_ROM_DRV_API -DUSE_FREERTOS

MGOS_SRCS += $(notdir $(wildcard $(MGOS_CC3220_PATH)/src/*.c)) \
cs_crc32.c cs_file.c cs_rbuf.c \
cs_crc32.c cs_file.c cs_hex.c cs_rbuf.c \
frozen.c json_utils.c \
mgos_config_util.c mgos_core_dump.c mgos_debug.c mgos_dlsym.c mgos_event.c mgos_gpio.c \
mgos_file_utils.c mgos_init.c \
Expand Down
2 changes: 1 addition & 1 deletion platforms/esp32/src/esp32_src.mk
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ MGOS_SRCS += mgos_config_util.c mgos_core_dump.c mgos_dlsym.c mgos_event.c \
VPATH += $(MGOS_ESP_SRC_PATH) $(MGOS_PATH)/common \
$(MGOS_PATH)/common/platforms/esp/src

MGOS_SRCS += cs_crc32.c cs_file.c cs_rbuf.c json_utils.c
MGOS_SRCS += cs_crc32.c cs_file.c cs_hex.c cs_rbuf.c json_utils.c

VPATH += $(MGOS_VPATH)

Expand Down
2 changes: 1 addition & 1 deletion platforms/esp8266/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ include $(MGOS_ESP8266_PATH)/common.mk

MGOS_ESP_SRC_PATH = $(MGOS_ESP8266_PATH)/src

MGOS_SRCS += cs_file.c cs_rbuf.c \
MGOS_SRCS += cs_file.c cs_hex.c cs_rbuf.c \
mgos_config_util.c \
mgos_core_dump.c \
mgos_dlsym.c \
Expand Down
2 changes: 1 addition & 1 deletion platforms/rs14100/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ FFI_EXPORTS_C = $(GEN_DIR)/ffi_exports.c
MGOS_SRCS += $(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \
mgos_config_util.c mgos_core_dump.c mgos_event.c mgos_gpio.c \
mgos_hw_timers.c mgos_sys_config.c \
mgos_time.c mgos_timers.c cs_crc32.c cs_file.c \
mgos_time.c mgos_timers.c cs_crc32.c cs_file.c cs_hex.c \
json_utils.c frozen.c mgos_uart.c cs_rbuf.c mgos_init.c \
mgos_dlsym.c mgos_file_utils.c mgos_system.c mgos_utils.c \
arm_exc_top.S arm_exc.c arm_nsleep100.c arm_nsleep100_m4.S \
Expand Down
2 changes: 1 addition & 1 deletion platforms/stm32/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ FFI_EXPORTS_C = $(GEN_DIR)/ffi_exports.c
MGOS_SRCS += $(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \
mgos_config_util.c mgos_core_dump.c mgos_event.c mgos_gpio.c \
mgos_hw_timers.c mgos_sys_config.c \
mgos_time.c mgos_timers.c cs_crc32.c cs_file.c \
mgos_time.c mgos_timers.c cs_crc32.c cs_file.c cs_hex.c \
json_utils.c frozen.c mgos_uart.c cs_rbuf.c mgos_init.c \
mgos_dlsym.c mgos_file_utils.c mgos_system.c mgos_utils.c \
arm_exc_top.S arm_exc.c arm_nsleep100.c \
Expand Down
2 changes: 1 addition & 1 deletion platforms/ubuntu/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ MGOS_SRCS = $(notdir $(wildcard *.c)) mgos_init.c \
mgos_system.c mgos_time.c mgos_timers.c \
mgos_config_util.c mgos_sys_config.c \
json_utils.c cs_rbuf.c mgos_uart.c \
mgos_utils.c cs_file.c cs_crc32.c
mgos_utils.c cs_file.c cs_hex.c cs_crc32.c

PLATFORM_SRCS = $(wildcard $(PLATFORM_VPATH)/*.c)

Expand Down
45 changes: 45 additions & 0 deletions src/common/cs_hex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2014-2018 Cesanta Software Limited
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "common/cs_hex.h"

#include <ctype.h>

static int hextoi(int x) {
return (x >= '0' && x <= '9' ? x - '0' : x - 'W');
}

int cs_hex_decode(const char *s, int len, unsigned char *dst, int *dst_len) {
int i = 0;
unsigned char *p = dst;
while (i < len) {
int c1, c2;
c1 = hextoi(tolower((int) s[i++]));
if (c1 < 0 || c1 > 15 || i == len) {
i--;
break;
}
c2 = hextoi(tolower((int) s[i++]));
if (c2 < 0 || c2 > 15) {
i -= 2;
break;
}
*p++ = (unsigned char) ((c1 << 4) | c2);
}
*dst_len = (int) (p - dst);
return i;
}
1 change: 1 addition & 0 deletions src/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SOURCES = unit_test.c \
$(REPO_ROOT)/src/mgos_event.c \
$(REPO_ROOT)/src/common/json_utils.c \
$(REPO_ROOT)/src/common/cs_file.c \
$(REPO_ROOT)/src/common/cs_hex.c \
$(MONGOOSE_PATH)/mongoose.c \
test_main.c \
test_util.c
Expand Down
35 changes: 35 additions & 0 deletions src/test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "common/cs_dbg.h"
#include "common/cs_file.h"
#include "common/cs_hex.h"

#include "frozen.h"

Expand Down Expand Up @@ -202,13 +203,47 @@ static const char *test_events(void) {
return NULL;
}

static const char *test_cs_hex(void) {
unsigned char dst[32];
int dst_len = 0;
ASSERT_EQ(cs_hex_decode(NULL, 0, NULL, &dst_len), 0);
{
const char *s = "11";
ASSERT_EQ(cs_hex_decode(s, strlen(s), dst, &dst_len), strlen(s));
ASSERT_EQ(dst_len, 1);
ASSERT_EQ(dst[0], 0x11);
}
{
const char *s = "A1b200";
ASSERT_EQ(cs_hex_decode(s, strlen(s), dst, &dst_len), strlen(s));
ASSERT_EQ(dst_len, 3);
ASSERT_EQ(dst[0], 0xa1);
ASSERT_EQ(dst[1], 0xb2);
ASSERT_EQ(dst[2], 0x00);
}
{
const char *s = "A1b";
ASSERT_EQ(cs_hex_decode(s, strlen(s), dst, &dst_len), 2);
ASSERT_EQ(dst_len, 1);
ASSERT_EQ(dst[0], 0xa1);
}
{
const char *s = "A1x200";
ASSERT_EQ(cs_hex_decode(s, strlen(s), dst, &dst_len), 2);
ASSERT_EQ(dst_len, 1);
ASSERT_EQ(dst[0], 0xa1);
}
return NULL;
}

void tests_setup(void) {
}

const char *tests_run(const char *filter) {
RUN_TEST(test_config);
RUN_TEST(test_json_scanf);
RUN_TEST(test_events);
RUN_TEST(test_cs_hex);
return NULL;
}

Expand Down

0 comments on commit 4d80044

Please sign in to comment.