DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Dumitrescu, Cristian" <cristian.dumitrescu@intel.com>
To: Thomas Monjalon <thomas.monjalon@6wind.com>,
	"Singh, Jasvinder" <jasvinder.singh@intel.com>,
	"Zhang, Roy Fan" <roy.fan.zhang@intel.com>
Cc: "dev@dpdk.org" <dev@dpdk.org>
Subject: Re: [dpdk-dev] [PATCH v2 6/8] example/ip_pipeline: add	parse_hex_string for internal use
Date: Tue, 20 Oct 2015 14:31:12 +0000	[thread overview]
Message-ID: <3EB4FA525960D640B5BDFFD6A3D89126478F22F0@IRSMSX108.ger.corp.intel.com> (raw)
In-Reply-To: <2304122.ekIElQmNCJ@xps13>



> -----Original Message-----
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> Sent: Monday, October 19, 2015 4:50 PM
> To: Singh, Jasvinder <jasvinder.singh@intel.com>
> Cc: dev@dpdk.org
> Subject: Re: [dpdk-dev] [PATCH v2 6/8] example/ip_pipeline: add
> parse_hex_string for internal use
> 
> 2015-10-13 14:57, Jasvinder Singh:
> > From: Fan Zhang <roy.fan.zhang@intel.com>
> > +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;
> > +}
> 
> Why not use strtol()?

Strtol() was considered, but the code would have been more complicated (more corner case errors to handle) and less readable (more difficult to maintain), so our vote went for this simpler solution.

This function needs to convert an array of characters of up to 128 hex digits to the associated array of bytes, with output byte on position I representing the value of the i-th pair of input characters. For example, "010203" gets converted to array [1, 2, 3]. It only needs very simple math, which leads to the above code, which looks very readable.

Strtol() was not really intended for this. Strtol() is primarily intended to convert up to 8 characters to the associated value. Using strtol() would be possible to use (to convert each 2 characters to the byte) but we end up with a lot of error cases to handle, which leads to more complex code in our opinion.

Thanks,
Cristian

  reply	other threads:[~2015-10-20 14:33 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-25 22:33 [dpdk-dev] [PATCH 0/8] librte_table: add key_mask parameter to 8-byte key roy.fan.zhang
2015-09-25 22:33 ` [dpdk-dev] [PATCH 1/8] librte_table: add key_mask parameter to 8-byte key hash parameters roy.fan.zhang
2015-09-25 22:33 ` [dpdk-dev] [PATCH 2/8] librte_table: add key_mask parameter to 16-byte " roy.fan.zhang
2015-09-25 22:33 ` [dpdk-dev] [PATCH 3/8] librte_table: add 16 byte hash table operations with computed lookup roy.fan.zhang
2015-09-25 22:33 ` [dpdk-dev] [PATCH 4/8] app/test: modify app/test_table_combined and app/test_table_tables roy.fan.zhang
2015-09-25 22:33 ` [dpdk-dev] [PATCH 5/8] app/test-pipeline: modify pipeline test roy.fan.zhang
2015-09-25 22:33 ` [dpdk-dev] [PATCH 6/8] example/ip_pipeline: add parse_hex_string for internal use roy.fan.zhang
2015-09-25 22:33 ` [dpdk-dev] [PATCH 7/8] example/ip_pipeline/pipeline: update flow_classification pipeline roy.fan.zhang
2015-09-25 22:33 ` [dpdk-dev] [PATCH 8/8] librte_table: modify release notes and deprecation notice roy.fan.zhang
2015-10-12 14:24   ` Thomas Monjalon
2015-10-12 16:30     ` Mcnamara, John
2015-10-13 13:57       ` [dpdk-dev] [PATCH v2 0/8] librte_table: add key_mask parameter to 8-byte key Jasvinder Singh
2015-10-13 13:57         ` [dpdk-dev] [PATCH v2 1/8] librte_table: add key_mask parameter to 8-byte key hash parameters Jasvinder Singh
2015-10-13 13:57         ` [dpdk-dev] [PATCH v2 2/8] librte_table: add key_mask parameter to 16-byte " Jasvinder Singh
2015-10-13 13:57         ` [dpdk-dev] [PATCH v2 3/8] librte_table: add 16 byte hash table operations with computed lookup Jasvinder Singh
2015-10-13 13:57         ` [dpdk-dev] [PATCH v2 4/8] app/test: modify app/test_table_combined and app/test_table_tables Jasvinder Singh
2015-10-13 13:57         ` [dpdk-dev] [PATCH v2 5/8] app/test-pipeline: modify pipeline test Jasvinder Singh
2015-10-13 13:57         ` [dpdk-dev] [PATCH v2 6/8] example/ip_pipeline: add parse_hex_string for internal use Jasvinder Singh
2015-10-19 13:49           ` Thomas Monjalon
2015-10-20 14:31             ` Dumitrescu, Cristian [this message]
2015-10-13 13:57         ` [dpdk-dev] [PATCH v2 7/8] example/ip_pipeline/pipeline: update flow_classification pipeline Jasvinder Singh
2015-10-19 13:57           ` Thomas Monjalon
2015-10-13 13:57         ` [dpdk-dev] [PATCH v2 8/8] librte_table: modify release notes and deprecation notice Jasvinder Singh
2015-10-21 12:18         ` [dpdk-dev] [PATCH v3 0/6] librte_table: add key_mask parameter to hash table parameter structure Jasvinder Singh
2015-10-21 12:18           ` [dpdk-dev] [PATCH v3 1/6] librte_table: add key_mask parameter to 8- and 16-bytes key hash parameters Jasvinder Singh
2015-10-21 12:18           ` [dpdk-dev] [PATCH v3 2/6] librte_table: add 16 byte hash table operations with computed lookup Jasvinder Singh
2015-10-21 12:18           ` [dpdk-dev] [PATCH v3 3/6] app/test: modify app/test_table_combined and app/test_table_tables Jasvinder Singh
2015-10-21 12:18           ` [dpdk-dev] [PATCH v3 4/6] app/test-pipeline: modify pipeline test Jasvinder Singh
2015-10-21 12:18           ` [dpdk-dev] [PATCH v3 5/6] example/ip_pipeline: add parse_hex_string for internal use Jasvinder Singh
2015-10-21 12:18           ` [dpdk-dev] [PATCH v3 6/6] example/ip_pipeline/pipeline: update flow_classification pipeline Jasvinder Singh
2015-10-19 13:31       ` [dpdk-dev] [PATCH 8/8] librte_table: modify release notes and deprecation notice Thomas Monjalon
2015-09-28 20:07 ` [dpdk-dev] [PATCH 0/8] librte_table: add key_mask parameter to 8-byte key Dumitrescu, Cristian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3EB4FA525960D640B5BDFFD6A3D89126478F22F0@IRSMSX108.ger.corp.intel.com \
    --to=cristian.dumitrescu@intel.com \
    --cc=dev@dpdk.org \
    --cc=jasvinder.singh@intel.com \
    --cc=roy.fan.zhang@intel.com \
    --cc=thomas.monjalon@6wind.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).