hexview.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*====================================================================*
  2. *
  3. * void hexview (void const * memory, size_t offset, size_t extent, FILE *fp);
  4. *
  5. * memory.h
  6. *
  7. * print memory as a hexadecimal dump showing absolute offsets and
  8. * an ASCII character display; this function is similar to but not
  9. * the same as function hexdump;
  10. *
  11. * Motley Tools by Charles Maier <cmaier@cmassoc.net>;
  12. * Copyright (c) 2001-2006 by Charles Maier Associates;
  13. * Licensed under the Internet Software Consortium License;
  14. *
  15. *--------------------------------------------------------------------*/
  16. #ifndef HEXVIEW_SOURCE
  17. #define HEXVIEW_SOURCE
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <stdint.h>
  21. #include "../tools/memory.h"
  22. #include "../tools/number.h"
  23. void hexview (void const * memory, size_t offset, size_t extent, FILE * fp)
  24. {
  25. byte * origin = (byte *) (memory);
  26. unsigned block = 0x10;
  27. size_t lower = block * (offset / block);
  28. size_t upper = block + lower;
  29. size_t index = 0;
  30. char buffer [ADDRSIZE + 72];
  31. char * output;
  32. while (lower < offset + extent)
  33. {
  34. output = buffer + ADDRSIZE;
  35. for (index = lower; output-- > buffer; index >>= 4)
  36. {
  37. * output = DIGITS_HEX [index & 0x0F];
  38. }
  39. output = buffer + ADDRSIZE;
  40. for (index = lower; index < upper; index++)
  41. {
  42. * output++ = ' ';
  43. if (index < offset)
  44. {
  45. * output++ = ' ';
  46. * output++ = ' ';
  47. }
  48. else if (index < offset + extent)
  49. {
  50. * output++ = DIGITS_HEX [(origin [index - offset] >> 4) & 0x0F];
  51. * output++ = DIGITS_HEX [(origin [index - offset] >> 0) & 0x0F];
  52. }
  53. else
  54. {
  55. * output++ = ' ';
  56. * output++ = ' ';
  57. }
  58. }
  59. * output++ = ' ';
  60. for (index = lower; index < upper; index++)
  61. {
  62. if (index < offset)
  63. {
  64. * output++ = ' ';
  65. }
  66. else if (index < offset + extent)
  67. {
  68. unsigned c = origin [index - offset];
  69. * output++ = isprint (c)? c: '.';
  70. }
  71. else
  72. {
  73. * output++ = ' ';
  74. }
  75. }
  76. * output++ = '\n';
  77. * output++ = '\0';
  78. fputs (buffer, fp);
  79. lower += block;
  80. upper += block;
  81. }
  82. fflush (fp);
  83. return;
  84. }
  85. #endif