[Qemu-devel] [PATCH] A hexdump function that also displays UTF-8 strings contained in the dumped buffer.

2013-11-11 Thread 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:

:  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

:  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 64ob9dp...qbxd

Signed-off-by: Mike Day ncm...@ncultra.org
---
 include/qemu-common.h |  2 ++
 util/hexdump.c| 48 +++-
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/include/qemu-common.h b/include/qemu-common.h
index 5054836..7b8e2b9 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -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__
diff --git a/util/hexdump.c b/util/hexdump.c
index 969b340..a920c81 100644
--- a/util/hexdump.c
+++ b/util/hexdump.c
@@ -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) {
+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) {
+fprintf(fp, %c, '.');
+} else {
+fprintf(fp, %c, *(linep + j));
+}
+}
+fprintf(fp, \n);
+linep = inp;
+} while (len);
+}



Re: [Qemu-devel] [PATCH] A hexdump function that also displays UTF-8 strings contained in the dumped buffer.

2013-11-11 Thread Anthony Liguori
On Mon, Nov 11, 2013 at 10:29 AM, Mike Day ncm...@ncultra.org wrote:
 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:

 :  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

 :  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 64ob9dp...qbxd

 Signed-off-by: Mike Day ncm...@ncultra.org
 ---
  include/qemu-common.h |  2 ++
  util/hexdump.c| 48 +++-
  2 files changed, 49 insertions(+), 1 deletion(-)

 diff --git a/include/qemu-common.h b/include/qemu-common.h
 index 5054836..7b8e2b9 100644
 --- a/include/qemu-common.h
 +++ b/include/qemu-common.h
 @@ -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__
 diff --git a/util/hexdump.c b/util/hexdump.c
 index 969b340..a920c81 100644
 --- a/util/hexdump.c
 +++ b/util/hexdump.c
 @@ -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) {
 +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.

 +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

 +}
 +}
 +fprintf(fp, \n);
 +linep = inp;
 +} while (len);
 +}