A hexdump function that also displays UTF-8 strings contained in the dumped buffer.
PATCH 1/1 A hexdump function that also displays UTF-8 strings contained in the dumped buffer.
This function is used by a forthcomingQemu monitor command that dumps contents of OpenFirmware Device Trees. It dumps contents of a buffer as hex in the same format as the existing function but also also appends any UTF-8 strings in human-readable format.
Like the existing hexdump function, this function may be used elsewhere in Qemu, and it shares the same prototype as the existing function.
In both functions, check for a NULL prefix parameter and omit printing the prefix if it is null.
Here is a sample of the output of both functions with no prefix string:
0000: 61 62 33 64 62 65 65 66 65 62 34 64 66 62 65 03 0010: 67 62 35 64 68 01 05 03 69 62 36 64 6a 01 06 03 0020: 6b 62 37 64 6c 01 07 03 6d 62 38 64 6e 01 08 03 0030: 6f 62 39 64 70 01 09 03 71 62 78 64
0000: 61 62 33 64 62 65 65 66 65 62 34 64 66 62 65 03 ab3dbeefeb4dfbe. 0010: 67 62 35 64 68 01 05 03 69 62 36 64 6a 01 06 03 gb5dh...ib6dj... 0020: 6b 62 37 64 6c 01 07 03 6d 62 38 64 6e 01 08 03 kb7dl...mb8dn... 0030: 6f 62 39 64 70 01 09 03 71 62 78 64 ob9dp...qbxd
--- include/qemu-common.h | 2 ++ util/hexdump.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-)
include/qemu-common.h +2 -0
@@ -435,6 +435,8 @@ int mod_utf8_codepoint(const char *s, size_t n, char **end);*/ void qemu_hexdump (const char *buf,FILE *fp,const char *prefix,size_t size); +/* include any strings alongside the hex output */ +void qemu_hexdump_str (gchar *buf,FILE *fp,const gchar *prefix,size_t len);/* vector definitions */ #ifdef __ALTIVEC__
util/hexdump.c +47 -1
57 lines not on this page · raw message
Quoted 87 lines · Mike Day
This function is used by a forthcomingQemu monitor command that dumps contents of OpenFirmware Device Trees. It dumps contents of a buffer as hex in the same format as the existing function but also also appends any UTF-8 strings in human-readable format.
Like the existing hexdump function, this function may be used elsewhere in Qemu, and it shares the same prototype as the existing function.
In both functions, check for a NULL prefix parameter and omit printing the prefix if it is null.
Here is a sample of the output of both functions with no prefix string:
0000: 61 62 33 64 62 65 65 66 65 62 34 64 66 62 65 03 0010: 67 62 35 64 68 01 05 03 69 62 36 64 6a 01 06 03 0020: 6b 62 37 64 6c 01 07 03 6d 62 38 64 6e 01 08 03 0030: 6f 62 39 64 70 01 09 03 71 62 78 640000: 61 62 33 64 62 65 65 66 65 62 34 64 66 62 65 03 ab3dbeefeb4dfbe. 0010: 67 62 35 64 68 01 05 03 69 62 36 64 6a 01 06 03 gb5dh...ib6dj... 0020: 6b 62 37 64 6c 01 07 03 6d 62 38 64 6e 01 08 03 kb7dl...mb8dn... 0030: 6f 62 39 64 70 01 09 03 71 62 78 64 ob9dp...qbxdSigned-off-by Mike Day <[email protected]>--- include/qemu-common.h | 2 ++ util/hexdump.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-)include/qemu-common.h +2
-0@@ -435,6 +435,8 @@ int mod_utf8_codepoint(const char *s, size_t n, char **end);*/ void qemu_hexdump (const char *buf,FILE *fp,const char *prefix,size_t size); +/* include any strings alongside the hex output */ +void qemu_hexdump_str (gchar *buf,FILE *fp,const gchar *prefix,size_t len);/* vector definitions */ #ifdef __ALTIVEC__util/hexdump.c +28
-1@@ -21,7 +21,11 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size)for (b =0 ; b < size; b++) {if ((b %16 ) ==0 ) { -fprintf (fp,"%s: %04x:" , prefix, b); +if (prefix) { +fprintf (fp,"%s: %04x:" , prefix, b); + }else { +fprintf (fp,"%04x:" , b); + } }if ((b %4 ) ==0 ) {fprintf (fp," " );@@ -35,3 +39,45 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size)fprintf (fp,"\n" ); } } + +/* print any strings along side the hex dump */ +void qemu_hexdump_str (gchar *buf,FILE *fp,const gchar *prefix,size_t len) +{ + +gchar *inp, *linep; +int i, offset =0 ; + inp = linep = buf; + + do { + if (prefix) { + fprintf(fp, "%s: %04x: ", prefix, offset); + } else { + fprintf(fp, "%04x: ", offset); + } + for (i = 0; i < 16 && len > 0; i++, len--, offset++, inp++) { + if (i && !(i % 4)) { + fprintf(fp, " "); + } + fprintf(fp, "%02hx ", *inp); + } + int j; + if (i < 16) {
util/hexdump.c void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size) · line 74
Quoted 10 lines
util/hexdump.c +10
-0+for (j =16 - i; j; --j) { +fprintf (fp," " ); +if (j && (!(j %4 ))) { +fprintf (fp," " ); + } + } + } +fprintf (fp," " ); +for (j =0 ; j < i; j++) { +if (*(linep + j) <0x20 || *(linep + j) >0x7e ) {
You can use qemu_isprint() for this.
util/hexdump.c void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size) · line 77
Quoted 3 lines
util/hexdump.c +3
-0+fprintf (fp,"%c" ,'.' ); + }else { +fprintf (fp,"%c" , *(linep + j));
Even though the comment says "UTF-8", this isn't actually handling UTF-8. Just ascii.
You should fold this into whatever forthcoming patch you are submitting.
Regards,
Anthony Liguori
Quoted 7 lines
util/hexdump.c +6
-0+ } + } +fprintf (fp,"\n" ); + linep = inp; + }while (len); +}