hexdump.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.h>
  22. GRUB_EXPORT(hexdump);
  23. void
  24. hexdump (unsigned long bse, char *buf, int len)
  25. {
  26. int pos;
  27. char line[80];
  28. while (len > 0)
  29. {
  30. int cnt, i;
  31. pos = grub_snprintf (line, sizeof (line), "%08lx ", bse);
  32. cnt = 16;
  33. if (cnt > len)
  34. cnt = len;
  35. for (i = 0; i < cnt; i++)
  36. {
  37. pos += grub_snprintf (&line[pos], sizeof (line) - pos,
  38. "%02x ", (unsigned char) buf[i]);
  39. if ((i & 7) == 7)
  40. line[pos++] = ' ';
  41. }
  42. for (; i < 16; i++)
  43. {
  44. pos += grub_snprintf (&line[pos], sizeof (line) - pos, " ");
  45. if ((i & 7) == 7)
  46. line[pos++] = ' ';
  47. }
  48. line[pos++] = '|';
  49. for (i = 0; i < cnt; i++)
  50. line[pos++] = ((buf[i] >= 32) && (buf[i] < 127)) ? buf[i] : '.';
  51. line[pos++] = '|';
  52. line[pos] = 0;
  53. grub_printf ("%s\n", line);
  54. /* Print only first and last line if more than 3 lines are identical. */
  55. if (len >= 4 * 16
  56. && ! grub_memcmp (buf, buf + 1 * 16, 16)
  57. && ! grub_memcmp (buf, buf + 2 * 16, 16)
  58. && ! grub_memcmp (buf, buf + 3 * 16, 16))
  59. {
  60. grub_printf ("*\n");
  61. do
  62. {
  63. bse += 16;
  64. buf += 16;
  65. len -= 16;
  66. }
  67. while (len >= 3 * 16 && ! grub_memcmp (buf, buf + 2 * 16, 16));
  68. }
  69. bse += 16;
  70. buf += 16;
  71. len -= cnt;
  72. }
  73. }