Skip to content

Commit 4d80044

Browse files
committed
Add cs_hex_decode()
CL: Add cs_hex_decode()
1 parent b5147e3 commit 4d80044

File tree

12 files changed

+124
-7
lines changed

12 files changed

+124
-7
lines changed

include/common/cs_hex.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2014-2018 Cesanta Software Limited
3+
* All rights reserved
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the ""License"");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an ""AS IS"" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/*
25+
* Decodes a hex-encoded string `s` length `len` into `dst`.
26+
* `dst` must have enough space to hold the result.
27+
* `*dec_len` will contain the resulting length of the string in `dst`
28+
* while return value will return number of processed bytes in `src`.
29+
* Return value == len indicates successful processing of all the data.
30+
*/
31+
int cs_hex_decode(const char *s, int len, unsigned char *dst, int *dst_len);
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif

include/mgos.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "common/cs_dbg.h"
2626
#include "common/cs_file.h"
27+
#include "common/cs_hex.h"
2728
#include "common/json_utils.h"
2829
#include "common/mbuf.h"
2930
#include "common/str_util.h"

platforms/cc3200/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ MGOS_SRCS += mgos_event.c \
141141
mgos_config_util.c mgos_sys_config.c \
142142
mgos_dlsym.c mgos_system.c \
143143
$(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \
144-
cs_crc32.c cs_file.c \
144+
cs_crc32.c cs_file.c cs_hex.c \
145145
cs_frbuf.c mgos_file_utils.c mgos_utils.c \
146146
cs_rbuf.c mgos_core_dump.c mgos_uart.c \
147147
boot.c frozen.c json_utils.c

platforms/cc3220/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ VPATH += $(SDK_PATH)/source/ti/devices/cc32xx/driverlib \
174174
SDK_CFLAGS = -DTARGET_IS_CC3220 -DUSE_CC3220_ROM_DRV_API -DUSE_FREERTOS
175175

176176
MGOS_SRCS += $(notdir $(wildcard $(MGOS_CC3220_PATH)/src/*.c)) \
177-
cs_crc32.c cs_file.c cs_rbuf.c \
177+
cs_crc32.c cs_file.c cs_hex.c cs_rbuf.c \
178178
frozen.c json_utils.c \
179179
mgos_config_util.c mgos_core_dump.c mgos_debug.c mgos_dlsym.c mgos_event.c mgos_gpio.c \
180180
mgos_file_utils.c mgos_init.c \

platforms/esp32/src/esp32_src.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ MGOS_SRCS += mgos_config_util.c mgos_core_dump.c mgos_dlsym.c mgos_event.c \
4545
VPATH += $(MGOS_ESP_SRC_PATH) $(MGOS_PATH)/common \
4646
$(MGOS_PATH)/common/platforms/esp/src
4747

48-
MGOS_SRCS += cs_crc32.c cs_file.c cs_rbuf.c json_utils.c
48+
MGOS_SRCS += cs_crc32.c cs_file.c cs_hex.c cs_rbuf.c json_utils.c
4949

5050
VPATH += $(MGOS_VPATH)
5151

platforms/esp8266/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ include $(MGOS_ESP8266_PATH)/common.mk
7777

7878
MGOS_ESP_SRC_PATH = $(MGOS_ESP8266_PATH)/src
7979

80-
MGOS_SRCS += cs_file.c cs_rbuf.c \
80+
MGOS_SRCS += cs_file.c cs_hex.c cs_rbuf.c \
8181
mgos_config_util.c \
8282
mgos_core_dump.c \
8383
mgos_dlsym.c \

platforms/rs14100/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ FFI_EXPORTS_C = $(GEN_DIR)/ffi_exports.c
136136
MGOS_SRCS += $(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \
137137
mgos_config_util.c mgos_core_dump.c mgos_event.c mgos_gpio.c \
138138
mgos_hw_timers.c mgos_sys_config.c \
139-
mgos_time.c mgos_timers.c cs_crc32.c cs_file.c \
139+
mgos_time.c mgos_timers.c cs_crc32.c cs_file.c cs_hex.c \
140140
json_utils.c frozen.c mgos_uart.c cs_rbuf.c mgos_init.c \
141141
mgos_dlsym.c mgos_file_utils.c mgos_system.c mgos_utils.c \
142142
arm_exc_top.S arm_exc.c arm_nsleep100.c arm_nsleep100_m4.S \

platforms/stm32/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ FFI_EXPORTS_C = $(GEN_DIR)/ffi_exports.c
153153
MGOS_SRCS += $(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \
154154
mgos_config_util.c mgos_core_dump.c mgos_event.c mgos_gpio.c \
155155
mgos_hw_timers.c mgos_sys_config.c \
156-
mgos_time.c mgos_timers.c cs_crc32.c cs_file.c \
156+
mgos_time.c mgos_timers.c cs_crc32.c cs_file.c cs_hex.c \
157157
json_utils.c frozen.c mgos_uart.c cs_rbuf.c mgos_init.c \
158158
mgos_dlsym.c mgos_file_utils.c mgos_system.c mgos_utils.c \
159159
arm_exc_top.S arm_exc.c arm_nsleep100.c \

platforms/ubuntu/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ MGOS_SRCS = $(notdir $(wildcard *.c)) mgos_init.c \
4848
mgos_system.c mgos_time.c mgos_timers.c \
4949
mgos_config_util.c mgos_sys_config.c \
5050
json_utils.c cs_rbuf.c mgos_uart.c \
51-
mgos_utils.c cs_file.c cs_crc32.c
51+
mgos_utils.c cs_file.c cs_hex.c cs_crc32.c
5252

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

src/common/cs_hex.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2014-2018 Cesanta Software Limited
3+
* All rights reserved
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the ""License"");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an ""AS IS"" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "common/cs_hex.h"
19+
20+
#include <ctype.h>
21+
22+
static int hextoi(int x) {
23+
return (x >= '0' && x <= '9' ? x - '0' : x - 'W');
24+
}
25+
26+
int cs_hex_decode(const char *s, int len, unsigned char *dst, int *dst_len) {
27+
int i = 0;
28+
unsigned char *p = dst;
29+
while (i < len) {
30+
int c1, c2;
31+
c1 = hextoi(tolower((int) s[i++]));
32+
if (c1 < 0 || c1 > 15 || i == len) {
33+
i--;
34+
break;
35+
}
36+
c2 = hextoi(tolower((int) s[i++]));
37+
if (c2 < 0 || c2 > 15) {
38+
i -= 2;
39+
break;
40+
}
41+
*p++ = (unsigned char) ((c1 << 4) | c2);
42+
}
43+
*dst_len = (int) (p - dst);
44+
return i;
45+
}

0 commit comments

Comments
 (0)