hexdump.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* hexdump.c - hexdump function */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/types.h>
  20. #include <grub/misc.h>
  21. #include <grub/lib/hexdump.h>
  22. void
  23. hexdump (unsigned long bse, char *buf, int len)
  24. {
  25. int pos;
  26. char line[80];
  27. while (len > 0)
  28. {
  29. int cnt, i;
  30. pos = grub_snprintf (line, sizeof (line), "%08lx ", bse);
  31. cnt = 16;
  32. if (cnt > len)
  33. cnt = len;
  34. for (i = 0; i < cnt; i++)
  35. {
  36. pos += grub_snprintf (&line[pos], sizeof (line) - pos,
  37. "%02x ", (unsigned char) buf[i]);
  38. if ((i & 7) == 7)
  39. line[pos++] = ' ';
  40. }
  41. for (; i < 16; i++)
  42. {
  43. pos += grub_snprintf (&line[pos], sizeof (line) - pos, " ");
  44. if ((i & 7) == 7)
  45. line[pos++] = ' ';
  46. }
  47. line[pos++] = '|';
  48. for (i = 0; i < cnt; i++)
  49. line[pos++] = ((buf[i] >= 32) && (buf[i] < 127)) ? buf[i] : '.';
  50. line[pos++] = '|';
  51. line[pos] = 0;
  52. grub_printf ("%s\n", line);
  53. /* Print only first and last line if more than 3 lines are identical. */
  54. if (len >= 4 * 16
  55. && ! grub_memcmp (buf, buf + 1 * 16, 16)
  56. && ! grub_memcmp (buf, buf + 2 * 16, 16)
  57. && ! grub_memcmp (buf, buf + 3 * 16, 16))
  58. {
  59. grub_printf ("*\n");
  60. do
  61. {
  62. bse += 16;
  63. buf += 16;
  64. len -= 16;
  65. }
  66. while (len >= 3 * 16 && ! grub_memcmp (buf, buf + 2 * 16, 16));
  67. }
  68. bse += 16;
  69. buf += 16;
  70. len -= cnt;
  71. }
  72. }