[2/2] hexdump: align output of string

Message ID 20180816153106.7608-3-stephen@networkplumber.org
State New
Delegated to: Thomas Monjalon
Headers show
Series
  • eal: hexcump trivial changes
Related show

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/checkpatch warning coding style issues

Commit Message

Stephen Hemminger Aug. 16, 2018, 3:31 p.m.
This fixes the issue where if the length of the output is not
a multiple of 16 the formatting was off.

Before:
00000000: 45 00 00 1C 12 34 2C E0 40 06 B8 2E C0 A8 01 12 | E....4,.@.......
00000010: C0 A8 01 37 |  |  |  |  |  |  |  |  |  |  |  |  | ...7

After:
00000000: 45 00 00 1C 12 34 2C E0 40 06 B8 2E C0 A8 01 12 | E....4,.@.......
00000010: C0 A8 01 37                                     | ...7

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/librte_eal/common/eal_common_hexdump.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Comments

Stephen Hemminger Aug. 16, 2018, 3:50 p.m. | #1
On Thu, 16 Aug 2018 08:31:06 -0700
Stephen Hemminger <stephen@networkplumber.org> wrote:

> This fixes the issue where if the length of the output is not
> a multiple of 16 the formatting was off.
> 
> Before:
> 00000000: 45 00 00 1C 12 34 2C E0 40 06 B8 2E C0 A8 01 12 | E....4,.@.......
> 00000010: C0 A8 01 37 |  |  |  |  |  |  |  |  |  |  |  |  | ...7
> 
> After:
> 00000000: 45 00 00 1C 12 34 2C E0 40 06 B8 2E C0 A8 01 12 | E....4,.@.......
> 00000010: C0 A8 01 37                                     | ...7
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/librte_eal/common/eal_common_hexdump.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/librte_eal/common/eal_common_hexdump.c b/lib/librte_eal/common/eal_common_hexdump.c
> index 980cf73ac337..19c2d86c693e 100644
> --- a/lib/librte_eal/common/eal_common_hexdump.c
> +++ b/lib/librte_eal/common/eal_common_hexdump.c
> @@ -22,9 +22,16 @@ void rte_hexdump(FILE *f, const char *title, const void *buf, unsigned int len)
>  	while (ofs < len) {
>  		/* format the line in the buffer */
>  		out = snprintf(line, LINE_LEN, "%08X:", ofs);
> -		for (i = 0; i < 16 && ofs + i < len; i++)
> -			out += snprintf(line + out, LINE_LEN - out,
> +		for (i = 0; i < 16; i++) {
> +			if (ofs + i < len)
> +				snprintf(line + out, LINE_LEN - out,
>  					 " %02X", (data[ofs + i] & 0xff));
> +			else
> +				strcpy(line + out, "   ");
> +			out += 3;
> +		}
> +
> +
>  		for (; i <= 16; i++)
>  			out += snprintf(line + out, LINE_LEN - out, " | ");
>  

Nevermind, this version.
The last two lines are dead code after tha patch and could be dropped.

Patch

diff --git a/lib/librte_eal/common/eal_common_hexdump.c b/lib/librte_eal/common/eal_common_hexdump.c
index 980cf73ac337..19c2d86c693e 100644
--- a/lib/librte_eal/common/eal_common_hexdump.c
+++ b/lib/librte_eal/common/eal_common_hexdump.c
@@ -22,9 +22,16 @@  void rte_hexdump(FILE *f, const char *title, const void *buf, unsigned int len)
 	while (ofs < len) {
 		/* format the line in the buffer */
 		out = snprintf(line, LINE_LEN, "%08X:", ofs);
-		for (i = 0; i < 16 && ofs + i < len; i++)
-			out += snprintf(line + out, LINE_LEN - out,
+		for (i = 0; i < 16; i++) {
+			if (ofs + i < len)
+				snprintf(line + out, LINE_LEN - out,
 					 " %02X", (data[ofs + i] & 0xff));
+			else
+				strcpy(line + out, "   ");
+			out += 3;
+		}
+
+
 		for (; i <= 16; i++)
 			out += snprintf(line + out, LINE_LEN - out, " | ");