Skip to contentSkip to conversation

net: use the common ascii hex helpers

PATCH In review Harvey Harrison 3 participants 10 messages May 2, 2008 – May 3, 2008

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 12/12 net: use the common ascii hex helpers

---
 drivers/net/skfp/smt.c             |   13 ++++++-------
 net/ipv4/netfilter/ipt_CLUSTERIP.c |    5 ++---
 2 files changed, 8 insertions(+), 10 deletions(-)
drivers/net/skfp/smt.c +6 -7
index ffbfb1b..805383b 100644
@@ -19,6 +19,7 @@
 #include "h/smc.h"
 #include "h/smt_p.h"
 #include <linux/bitrev.h>
+#include <linux/kernel.h>
 
 #define KERNEL
 #include "h/smtstate.h"
@@ -1730,20 +1731,18 @@ void fddi_send_antc(struct s_smc *smc, struct fddi_addr *dest)
 #endif
 
 #ifdef	DEBUG
-#define hextoasc(x)	"0123456789abcdef"[x]
-
 char *addr_to_string(struct fddi_addr *addr)
 {
 	int	i ;
 	static char	string[6*3] = "****" ;
 
 	for (i = 0 ; i < 6 ; i++) {
-		string[i*3] = hextoasc((addr->a[i]>>4)&0xf) ;
-		string[i*3+1] = hextoasc((addr->a[i])&0xf) ;
-		string[i*3+2] = ':' ;
+		string[i * 3] = hex_asc_hi(addr->a[i]);
+		string[i * 3 + 1] = hex_asc_lo(addr->a[i]);
+		string[i * 3 + 2] = ':';
 	}
-	string[5*3+2] = 0 ;
-	return(string) ;
+	string[5 * 3 + 2] = 0;
+	return(string);
 }
 #endif
 
net/ipv4/netfilter/ipt_CLUSTERIP.c +2 -3
index 22d8e7c..30b11a7 100644
@@ -475,11 +475,10 @@ static void arp_print(struct arp_payload *payload)
 #define HBUFFERLEN 30
 	char hbuffer[HBUFFERLEN];
 	int j,k;
-	const char hexbuf[]= "0123456789abcdef";
 
 	for (k=0, j=0; k < HBUFFERLEN-3 && j < ETH_ALEN; j++) {
-		hbuffer[k++]=hexbuf[(payload->src_hw[j]>>4)&15];
-		hbuffer[k++]=hexbuf[payload->src_hw[j]&15];
+		hbuffer[k++] = hex_asc_hi(payload->src_hw[j]);
+		hbuffer[k++] = hex_asc_lo(payload->src_hw[j]);
 		hbuffer[k++]=':';
 	}
 	hbuffer[--k]='\0';
Signature · 1 line
1.5.5.1.350.gbbbf
maintainer
#2 lore raw
Quoted 9 lines · On Fri, 02 May 2008 16:25:15 -0700 (PDT) David Miller <[email protected]>…

From: Harvey Harrison <[email protected]>
Date: Fri, 02 May 2008 12:01:56 -0700

Quoted 1 line

This has to go in via who ever adds the first patch with the actual helpers, so I won't apply this one.

Yup. After a bit of testing and tyre-kicking I'll probably slip lib-add-ascii-hex-helper-functions.patch into mainline so that we can trickle all the *-use-the-common-ascii-hex-helpers.patch patches out to the various maintainers.

maintainer
#3 lore raw

From: Andrew Morton <[email protected]>
Date: Fri, 2 May 2008 16:31:31 -0700

Quoted 10 lines
Quoted 7 lines · On Fri, 02 May 2008 16:25:15 -0700 (PDT) David Miller <[email protected]>…

From: Harvey Harrison <[email protected]>
Date: Fri, 02 May 2008 12:01:56 -0700

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

This has to go in via who ever adds the first patch with the actual helpers, so I won't apply this one.

Yup. After a bit of testing and tyre-kicking I'll probably slip lib-add-ascii-hex-helper-functions.patch into mainline so that we can trickle all the *-use-the-common-ascii-hex-helpers.patch patches out to the various maintainers.

Please just slurp this networking one in with that, if you would be so kind.

maintainer
#4 lore raw
Quoted 16 lines · On Fri, 02 May 2008 16:39:44 -0700 (PDT) David Miller <[email protected]>…

From: Andrew Morton <[email protected]>
Date: Fri, 2 May 2008 16:31:31 -0700

Quoted 13 lines
Quoted 9 lines · On Fri, 02 May 2008 16:25:15 -0700 (PDT) David Miller <[email protected]>…

From: Harvey Harrison <[email protected]>
Date: Fri, 02 May 2008 12:01:56 -0700

Quoted 1 line

This has to go in via who ever adds the first patch with the actual helpers, so I won't apply this one.

Quoted 7 lines
Quoted 4 lines

Yup. After a bit of testing and tyre-kicking I'll probably slip lib-add-ascii-hex-helper-functions.patch into mainline so that we can trickle all the *-use-the-common-ascii-hex-helpers.patch patches out to the various maintainers.

Please just slurp this networking one in with that, if you would be so kind.

No probs. Always happy to do that - more people should tell me "stop being an ass and just merge it" ;)

#5 lore raw
In reply to Andrew Morton
Quoted 10 lines · Andrew Morton
Quoted 7 lines · On Fri, 02 May 2008 16:25:15 -0700 (PDT) David Miller <[email protected]>…

From: Harvey Harrison <[email protected]>
Date: Fri, 02 May 2008 12:01:56 -0700

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

This has to go in via who ever adds the first patch with the actual helpers, so I won't apply this one.

Yup. After a bit of testing and tyre-kicking I'll probably slip lib-add-ascii-hex-helper-functions.patch into mainline so that we can trickle all the *-use-the-common-ascii-hex-helpers.patch patches out to the various maintainers.

I suppose the hex_asc array and hex_to_int could also use EXPORT_SYMBOL().

Also, As I've looked further around the tree, there seem to be a lot of hex_to_int/long variants around the tree, so what I'm thinking is the current hex_to_int becomes hex_to_u8 and I'll add a:

/**
* hex_to_u32 - read a u32 from a char buffer
* @buf: buffer to read ascii chars from
* @val: where to put the u32
*
* Read hex-ascii chars into a u32. Return the number of chars read.
* A maximum of 8 chars will be read, if a char not in [a-f][A-F][0-9]
* is encountered, the function returns.
*/
int hex_to_u32(const char *buf, u32 *val)

Similar to the above, but a max of 16 chars will be read. int hex_to_u64(const char *buf, u64 *val)

If this api is better, I suppose:

int hex_to_u8 - max of two chars
int hex_to_u16 - max of 4
int hex_to_u32 - max of 8
int hex_to_u64 - max of 16

Could be added around a common function that takes the max length as an additional arg.

Thoughts?

Harvey

maintainer
#6 lore raw
Quoted 14 lines · On Fri, 02 May 2008 16:52:43 -0700 Harvey Harrison <[email protected]>…
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Fri, 02 May 2008 16:25:15 -0700 (PDT) David Miller <[email protected]>…

From: Harvey Harrison <[email protected]>
Date: Fri, 02 May 2008 12:01:56 -0700

Quoted 1 line

This has to go in via who ever adds the first patch with the actual helpers, so I won't apply this one.

Quoted 8 lines
Quoted 5 lines

Yup. After a bit of testing and tyre-kicking I'll probably slip lib-add-ascii-hex-helper-functions.patch into mainline so that we can trickle all the *-use-the-common-ascii-hex-helpers.patch patches out to the various maintainers.

I suppose the hex_asc array and hex_to_int could also use EXPORT_SYMBOL().

gack. How could you do all those patches without discovering this? Please go give your QA staff a wedgie.

lib/hexdump.c renamed from lib/hexdump.c~lib-add-ascii-hex-helper-functions-fix +2 -0
@@ -13,6 +13,7 @@
 #include <linux/module.h>
 
 const char hex_asc[] = "0123456789abcdef";
+EXPORT_SYMBOL(hex_asc);
 
 /**
  * hex_to_int - convert a single hex ASCII char to an integer value
@@ -32,6 +33,7 @@ int hex_to_int(char ch)
 		return ch - '0';
 	return -1;
 }
+EXPORT_SYMBOL(hex_to_int);
 
 /**
  * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory

_

Quoted 20 lines

Also, As I've looked further around the tree, there seem to be a lot of hex_to_int/long variants around the tree, so what I'm thinking is the current hex_to_int becomes hex_to_u8 and I'll add a:

/**
* hex_to_u32 - read a u32 from a char buffer
* @buf: buffer to read ascii chars from
* @val: where to put the u32
*
* Read hex-ascii chars into a u32. Return the number of chars read.
* A maximum of 8 chars will be read, if a char not in [a-f][A-F][0-9]
* is encountered, the function returns.
*/
int hex_to_u32(const char *buf, u32 *val)

Similar to the above, but a max of 16 chars will be read. int hex_to_u64(const char *buf, u64 *val)

If this api is better, I suppose:

Quoted 8 lines

int hex_to_u8 - max of two chars
int hex_to_u16 - max of 4
int hex_to_u32 - max of 8
int hex_to_u64 - max of 16

Could be added around a common function that takes the max length as an additional arg.

yes, hex_to_u8() is more specific and better defined than hex_to_int().

Although anally it should perhaps be two_digit_hex_to_ulong, four_digit_hex_to_ulong, etc.

ho hum, I'll drop them all.

#7 lore raw
Quoted 23 lines · Andrew Morton
Quoted 20 lines · On Fri, 02 May 2008 16:52:43 -0700 Harvey Harrison <[email protected]>…
Quoted 18 lines · Andrew Morton
Quoted 9 lines · On Fri, 02 May 2008 16:25:15 -0700 (PDT) David Miller <[email protected]>…

From: Harvey Harrison <[email protected]>
Date: Fri, 02 May 2008 12:01:56 -0700

Quoted 1 line

This has to go in via who ever adds the first patch with the actual helpers, so I won't apply this one.

Yup. After a bit of testing and tyre-kicking I'll probably slip lib-add-ascii-hex-helper-functions.patch into mainline so that we can trickle all the *-use-the-common-ascii-hex-helpers.patch patches out to the various maintainers.

Quoted 5 lines
Quoted 2 lines

I suppose the hex_asc array and hex_to_int could also use EXPORT_SYMBOL().

gack. How could you do all those patches without discovering this? Please go give your QA staff a wedgie.

allyesconfig :(

Quoted 8 lines
Quoted 1 line

yes, hex_to_u8() is more specific and better defined than hex_to_int().

Although anally it should perhaps be two_digit_hex_to_ulong, four_digit_hex_to_ulong, etc.

ho hum, I'll drop them all.

My apologies, how about:

int hex_char_to_int(char ch)
int hex_char_to_int(char ch)
int hex_char_to_int(char ch)

EXPORT_SYMBOL(hex_dump_to_buffer);

#8 lore raw
In reply to Andrew Morton
Quoted 11 lines · Andrew Morton
Quoted 8 lines
Quoted 5 lines

Yup. After a bit of testing and tyre-kicking I'll probably slip lib-add-ascii-hex-helper-functions.patch into mainline so that we can trickle all the *-use-the-common-ascii-hex-helpers.patch patches out to the various maintainers.

I suppose the hex_asc array and hex_to_int could also use EXPORT_SYMBOL().

gack. How could you do all those patches without discovering this? Please go give your QA staff a wedgie.

Sorry about the fat-fingered send, as before:

allyesconfig :(

Quoted 6 lines

yes, hex_to_u8() is more specific and better defined than hex_to_int().

Although anally it should perhaps be two_digit_hex_to_ulong, four_digit_hex_to_ulong, etc.

ho hum, I'll drop them all.

int hex_char_to_int(char ch)

{
	return -1 if not [a-fA-F0-9]
}

int hex_to_u8(const char *buf, u8 *val)
int hex_to_u16(const char *buf, u16 *val)
int hex_to_u32(const char *buf, u32 *val)
int hex_to_u64(const char *buf, u64 *val)

Which return the number of chars read and reads up to a maximum of 2,4,8,16 chars respectively and returns early if a non-hex char is encountered.

Thoughts?

Harvey

maintainer
#9 lore raw
Quoted 10 lines · On Fri, 02 May 2008 17:15:55 -0700 Harvey Harrison <[email protected]>…

int hex_char_to_int(char ch)

{
	return -1 if not [a-fA-F0-9]
}

int hex_to_u8(const char *buf, u8 *val)
int hex_to_u16(const char *buf, u16 *val)
int hex_to_u32(const char *buf, u32 *val)
int hex_to_u64(const char *buf, u64 *val)

Quoted 5 lines

Which return the number of chars read and reads up to a maximum of 2,4,8,16 chars respectively and returns early if a non-hex char is encountered.

Thoughts?

I dunno. Return-by-reference is a bit nasty and forces the compiler to generate extra code to put local storage into a stack slot rather than keeping it in a register.

Does anyone actually test whether hex_to_foo returns -1? Probably not many.

In which case it might be better to do

u64 hex_to_u64(const char *buf);

and, if we encounter a non-hex char, do a WARN_ON(1).

One could legitimately do

	int c = hex_to_int(some_user_input);
	if (c < 0) {
		printk("user: you're an idiot\n");
	}

to perform validation and conversion in a single operation but I expect that such code is in the minority, and could be covered by running a separate

bool is_hex_string(const char *bug, size_t nchars);

beforehand?