hexdump.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Helper to hexdump a buffer
  3. *
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * Copyright (c) 2013 Gerd Hoffmann <kraxel@redhat.com>
  6. * Copyright (c) 2013 Peter Crosthwaite <peter.crosthwaite@xilinx.com>
  7. * Copyright (c) 2013 Xilinx, Inc
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qemu-common.h"
  17. void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size)
  18. {
  19. unsigned int b, len, i, c;
  20. for (b = 0; b < size; b += 16) {
  21. len = size - b;
  22. if (len > 16) {
  23. len = 16;
  24. }
  25. fprintf(fp, "%s: %04x:", prefix, b);
  26. for (i = 0; i < 16; i++) {
  27. if ((i % 4) == 0) {
  28. fprintf(fp, " ");
  29. }
  30. if (i < len) {
  31. fprintf(fp, " %02x", (unsigned char)buf[b + i]);
  32. } else {
  33. fprintf(fp, " ");
  34. }
  35. }
  36. fprintf(fp, " ");
  37. for (i = 0; i < len; i++) {
  38. c = buf[b + i];
  39. if (c < ' ' || c > '~') {
  40. c = '.';
  41. }
  42. fprintf(fp, "%c", c);
  43. }
  44. fprintf(fp, "\n");
  45. }
  46. }