From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by dpdk.org (Postfix) with ESMTP id 8D38A8D93 for ; Sat, 26 Sep 2015 00:34:08 +0200 (CEST) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga102.jf.intel.com with ESMTP; 25 Sep 2015 15:34:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,589,1437462000"; d="scan'208";a="797527823" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by fmsmga001.fm.intel.com with ESMTP; 25 Sep 2015 15:33:17 -0700 Received: from sivswdev02.ir.intel.com (sivswdev02.ir.intel.com [10.237.217.46]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id t8PMXFqt015218; Fri, 25 Sep 2015 23:33:16 +0100 Received: from sivswdev02.ir.intel.com (localhost [127.0.0.1]) by sivswdev02.ir.intel.com with ESMTP id t8PMXFJs013697; Fri, 25 Sep 2015 23:33:15 +0100 Received: (from fanzhan2@localhost) by sivswdev02.ir.intel.com with id t8PMXFIa013693; Fri, 25 Sep 2015 23:33:15 +0100 From: roy.fan.zhang@intel.com To: dev@dpdk.org Date: Fri, 25 Sep 2015 23:33:10 +0100 Message-Id: <1443220392-13434-7-git-send-email-roy.fan.zhang@intel.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1443220392-13434-1-git-send-email-roy.fan.zhang@intel.com> References: <1443220392-13434-1-git-send-email-roy.fan.zhang@intel.com> Subject: [dpdk-dev] [PATCH 6/8] example/ip_pipeline: add parse_hex_string for internal use X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Sep 2015 22:34:09 -0000 From: Fan Zhang This patch adds parse_hex_string function to parse hex string to uint8_t array. Signed-off-by: Fan Zhang --- examples/ip_pipeline/config_parse.c | 70 +++++++++++++++++++++++++++++++++++++ examples/ip_pipeline/pipeline.h | 4 +++ 2 files changed, 74 insertions(+) diff --git a/examples/ip_pipeline/config_parse.c b/examples/ip_pipeline/config_parse.c index c9b78f9..d7ee707 100644 --- a/examples/ip_pipeline/config_parse.c +++ b/examples/ip_pipeline/config_parse.c @@ -455,6 +455,76 @@ 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.h b/examples/ip_pipeline/pipeline.h index b9a56ea..4063594 100644 --- a/examples/ip_pipeline/pipeline.h +++ b/examples/ip_pipeline/pipeline.h @@ -84,4 +84,8 @@ pipeline_type_cmds_count(struct pipeline_type *ptype) return n_cmds; } +/* Parse hex string to uint8_t array */ +int +parse_hex_string(char *src, uint8_t *dst, uint32_t *size); + #endif -- 2.1.0