summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFan Zhang <roy.fan.zhang@intel.com>2015-10-28 17:11:20 +0000
committerThomas Monjalon <thomas.monjalon@6wind.com>2015-11-26 00:45:50 +0100
commitb3a25920c38f9314bd4e07cd39223cf741d7c439 (patch)
treeb21b99dd0ec2238c714d499f3a00a75d83bbdea4
parent68866e2417cc0b20c67b564bbe4f6fed3e6ceac0 (diff)
downloaddpdk-b3a25920c38f9314bd4e07cd39223cf741d7c439.zip
dpdk-b3a25920c38f9314bd4e07cd39223cf741d7c439.tar.gz
dpdk-b3a25920c38f9314bd4e07cd39223cf741d7c439.tar.xz
examples/ip_pipeline: add hex parsing
This patch adds parse_hex_string function to parse hex string to uint8_t array. Signed-off-by: Fan Zhang <roy.fan.zhang@intel.com> Acked-by: Cristian Dumitrescu <cristian.dumitrescu@intel.com>
-rw-r--r--examples/ip_pipeline/config_parse.c52
-rw-r--r--examples/ip_pipeline/pipeline_be.h4
2 files changed, 56 insertions, 0 deletions
diff --git a/examples/ip_pipeline/config_parse.c b/examples/ip_pipeline/config_parse.c
index a35bd3e..d866310 100644
--- a/examples/ip_pipeline/config_parse.c
+++ b/examples/ip_pipeline/config_parse.c
@@ -465,6 +465,58 @@ parse_pipeline_core(uint32_t *socket,
return 0;
}
+static uint32_t
+get_hex_val(char c)
+{
+ switch (c) {
+ case '0': case '1': case '2': case '3': case '4': case '5':
+ case '6': case '7': case '8': case '9':
+ return c - '0';
+ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
+ return c - 'A' + 10;
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
+ return c - 'a' + 10;
+ default:
+ return 0;
+ }
+}
+
+int
+parse_hex_string(char *src, uint8_t *dst, uint32_t *size)
+{
+ char *c;
+ uint32_t len, i;
+
+ /* Check input parameters */
+ if ((src == NULL) ||
+ (dst == NULL) ||
+ (size == NULL) ||
+ (*size == 0))
+ return -1;
+
+ len = strlen(src);
+ if (((len & 3) != 0) ||
+ (len > (*size) * 2))
+ return -1;
+ *size = len / 2;
+
+ for (c = src; *c != 0; c++) {
+ if ((((*c) >= '0') && ((*c) <= '9')) ||
+ (((*c) >= 'A') && ((*c) <= 'F')) ||
+ (((*c) >= 'a') && ((*c) <= 'f')))
+ continue;
+
+ return -1;
+ }
+
+ /* Convert chars to bytes */
+ for (i = 0; i < *size; i++)
+ dst[i] = get_hex_val(src[2 * i]) * 16 +
+ get_hex_val(src[2 * i + 1]);
+
+ return 0;
+}
+
static size_t
skip_digits(const char *src)
{
diff --git a/examples/ip_pipeline/pipeline_be.h b/examples/ip_pipeline/pipeline_be.h
index f7269c0..0ba00f6 100644
--- a/examples/ip_pipeline/pipeline_be.h
+++ b/examples/ip_pipeline/pipeline_be.h
@@ -271,4 +271,8 @@ struct pipeline_be_ops {
pipeline_be_op_track f_track;
};
+/* Parse hex string to uint8_t array */
+int
+parse_hex_string(char *src, uint8_t *dst, uint32_t *size);
+
#endif