All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 12/12] net: use the common ascii hex helpers
@ 2008-05-02 19:01 Harvey Harrison
  2008-05-02 23:25 ` David Miller
  0 siblings, 1 reply; 10+ messages in thread
From: Harvey Harrison @ 2008-05-02 19:01 UTC (permalink / raw)
  To: Andrew Morton; +Cc: LKML

Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
---
 drivers/net/skfp/smt.c             |   13 ++++++-------
 net/ipv4/netfilter/ipt_CLUSTERIP.c |    5 ++---
 2 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/drivers/net/skfp/smt.c b/drivers/net/skfp/smt.c
index ffbfb1b..805383b 100644
--- a/drivers/net/skfp/smt.c
+++ b/drivers/net/skfp/smt.c
@@ -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
 
diff --git a/net/ipv4/netfilter/ipt_CLUSTERIP.c b/net/ipv4/netfilter/ipt_CLUSTERIP.c
index 22d8e7c..30b11a7 100644
--- a/net/ipv4/netfilter/ipt_CLUSTERIP.c
+++ b/net/ipv4/netfilter/ipt_CLUSTERIP.c
@@ -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';
-- 
1.5.5.1.350.gbbbf


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-02 19:01 [PATCH 12/12] net: use the common ascii hex helpers Harvey Harrison
@ 2008-05-02 23:25 ` David Miller
  2008-05-02 23:31   ` Andrew Morton
  0 siblings, 1 reply; 10+ messages in thread
From: David Miller @ 2008-05-02 23:25 UTC (permalink / raw)
  To: harvey.harrison; +Cc: akpm, linux-kernel

From: Harvey Harrison <harvey.harrison@gmail.com>
Date: Fri, 02 May 2008 12:01:56 -0700

> Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>

Acked-by: David S. Miller <davem@davemloft.net>

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-02 23:25 ` David Miller
@ 2008-05-02 23:31   ` Andrew Morton
  2008-05-02 23:39     ` David Miller
  2008-05-02 23:52     ` Harvey Harrison
  0 siblings, 2 replies; 10+ messages in thread
From: Andrew Morton @ 2008-05-02 23:31 UTC (permalink / raw)
  To: David Miller; +Cc: harvey.harrison, linux-kernel

On Fri, 02 May 2008 16:25:15 -0700 (PDT)
David Miller <davem@davemloft.net> wrote:

> From: Harvey Harrison <harvey.harrison@gmail.com>
> Date: Fri, 02 May 2008 12:01:56 -0700
> 
> > Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
> 
> Acked-by: David S. Miller <davem@davemloft.net>
> 
> 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.




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-02 23:31   ` Andrew Morton
@ 2008-05-02 23:39     ` David Miller
  2008-05-02 23:43       ` Andrew Morton
  2008-05-02 23:52     ` Harvey Harrison
  1 sibling, 1 reply; 10+ messages in thread
From: David Miller @ 2008-05-02 23:39 UTC (permalink / raw)
  To: akpm; +Cc: harvey.harrison, linux-kernel

From: Andrew Morton <akpm@linux-foundation.org>
Date: Fri, 2 May 2008 16:31:31 -0700

> On Fri, 02 May 2008 16:25:15 -0700 (PDT)
> David Miller <davem@davemloft.net> wrote:
> 
> > From: Harvey Harrison <harvey.harrison@gmail.com>
> > Date: Fri, 02 May 2008 12:01:56 -0700
> > 
> > > Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
> > 
> > Acked-by: David S. Miller <davem@davemloft.net>
> > 
> > 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.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-02 23:39     ` David Miller
@ 2008-05-02 23:43       ` Andrew Morton
  0 siblings, 0 replies; 10+ messages in thread
From: Andrew Morton @ 2008-05-02 23:43 UTC (permalink / raw)
  To: David Miller; +Cc: harvey.harrison, linux-kernel

On Fri, 02 May 2008 16:39:44 -0700 (PDT)
David Miller <davem@davemloft.net> wrote:

> From: Andrew Morton <akpm@linux-foundation.org>
> Date: Fri, 2 May 2008 16:31:31 -0700
> 
> > On Fri, 02 May 2008 16:25:15 -0700 (PDT)
> > David Miller <davem@davemloft.net> wrote:
> > 
> > > From: Harvey Harrison <harvey.harrison@gmail.com>
> > > Date: Fri, 02 May 2008 12:01:56 -0700
> > > 
> > > > Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
> > > 
> > > Acked-by: David S. Miller <davem@davemloft.net>
> > > 
> > > 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.

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-02 23:31   ` Andrew Morton
  2008-05-02 23:39     ` David Miller
@ 2008-05-02 23:52     ` Harvey Harrison
  2008-05-03  0:08       ` Andrew Morton
  1 sibling, 1 reply; 10+ messages in thread
From: Harvey Harrison @ 2008-05-02 23:52 UTC (permalink / raw)
  To: Andrew Morton; +Cc: David Miller, linux-kernel

On Fri, 2008-05-02 at 16:31 -0700, Andrew Morton wrote:
> On Fri, 02 May 2008 16:25:15 -0700 (PDT)
> David Miller <davem@davemloft.net> wrote:
> 
> > From: Harvey Harrison <harvey.harrison@gmail.com>
> > Date: Fri, 02 May 2008 12:01:56 -0700
> > 
> > > Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
> > 
> > Acked-by: David S. Miller <davem@davemloft.net>
> > 
> > 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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-02 23:52     ` Harvey Harrison
@ 2008-05-03  0:08       ` Andrew Morton
  2008-05-03  0:11         ` Harvey Harrison
  2008-05-03  0:15         ` Harvey Harrison
  0 siblings, 2 replies; 10+ messages in thread
From: Andrew Morton @ 2008-05-03  0:08 UTC (permalink / raw)
  To: Harvey Harrison; +Cc: davem, linux-kernel

On Fri, 02 May 2008 16:52:43 -0700
Harvey Harrison <harvey.harrison@gmail.com> wrote:

> On Fri, 2008-05-02 at 16:31 -0700, Andrew Morton wrote:
> > On Fri, 02 May 2008 16:25:15 -0700 (PDT)
> > David Miller <davem@davemloft.net> wrote:
> > 
> > > From: Harvey Harrison <harvey.harrison@gmail.com>
> > > Date: Fri, 02 May 2008 12:01:56 -0700
> > > 
> > > > Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
> > > 
> > > Acked-by: David S. Miller <davem@davemloft.net>
> > > 
> > > 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().

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

--- a/lib/hexdump.c~lib-add-ascii-hex-helper-functions-fix
+++ a/lib/hexdump.c
@@ -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
_


> 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.
> 

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.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-03  0:08       ` Andrew Morton
@ 2008-05-03  0:11         ` Harvey Harrison
  2008-05-03  0:15         ` Harvey Harrison
  1 sibling, 0 replies; 10+ messages in thread
From: Harvey Harrison @ 2008-05-03  0:11 UTC (permalink / raw)
  To: Andrew Morton; +Cc: davem, linux-kernel

On Fri, 2008-05-02 at 17:08 -0700, Andrew Morton wrote:
> On Fri, 02 May 2008 16:52:43 -0700
> Harvey Harrison <harvey.harrison@gmail.com> wrote:
> 
> > On Fri, 2008-05-02 at 16:31 -0700, Andrew Morton wrote:
> > > On Fri, 02 May 2008 16:25:15 -0700 (PDT)
> > > David Miller <davem@davemloft.net> wrote:
> > > 
> > > > From: Harvey Harrison <harvey.harrison@gmail.com>
> > > > Date: Fri, 02 May 2008 12:01:56 -0700
> > > > 
> > > > > Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
> > > > 
> > > > Acked-by: David S. Miller <davem@davemloft.net>
> > > > 
> > > > 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().
> 
> gack.  How could you do all those patches without discovering this?  Please
> go give your QA staff a wedgie.

allyesconfig :(

> > 
> 
> 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);





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-03  0:08       ` Andrew Morton
  2008-05-03  0:11         ` Harvey Harrison
@ 2008-05-03  0:15         ` Harvey Harrison
  2008-05-03  0:30           ` Andrew Morton
  1 sibling, 1 reply; 10+ messages in thread
From: Harvey Harrison @ 2008-05-03  0:15 UTC (permalink / raw)
  To: Andrew Morton; +Cc: davem, linux-kernel

On Fri, 2008-05-02 at 17:08 -0700, Andrew Morton wrote:
> > > 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 :(

> 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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 12/12] net: use the common ascii hex helpers
  2008-05-03  0:15         ` Harvey Harrison
@ 2008-05-03  0:30           ` Andrew Morton
  0 siblings, 0 replies; 10+ messages in thread
From: Andrew Morton @ 2008-05-03  0:30 UTC (permalink / raw)
  To: Harvey Harrison; +Cc: davem, linux-kernel

On Fri, 02 May 2008 17:15:55 -0700
Harvey Harrison <harvey.harrison@gmail.com> wrote:

> 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?

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?

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2008-05-03  0:30 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-02 19:01 [PATCH 12/12] net: use the common ascii hex helpers Harvey Harrison
2008-05-02 23:25 ` David Miller
2008-05-02 23:31   ` Andrew Morton
2008-05-02 23:39     ` David Miller
2008-05-02 23:43       ` Andrew Morton
2008-05-02 23:52     ` Harvey Harrison
2008-05-03  0:08       ` Andrew Morton
2008-05-03  0:11         ` Harvey Harrison
2008-05-03  0:15         ` Harvey Harrison
2008-05-03  0:30           ` Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.