memout.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*====================================================================*
  2. *
  3. * void memout (void const * memory, size_t extent, char const * format, unsigned group, char c, char e, FILE * fp)
  4. *
  5. * memory.h
  6. *
  7. * print memory as a series of octets formatted by format string fmt,
  8. * seperated by character c every mod prints;
  9. *
  10. * for example, memout (memory, IPv4_LEN, "%d", 1, '.', stdout) would print:
  11. *
  12. * 192.168.1.1
  13. *
  14. * another example, memout (memory, IPv6_LEN, "%02x", 2, ':', stdout) would print:
  15. *
  16. * 0032:0045:0000:0000:0000:0000:1123:4456
  17. *
  18. * Motley Tools by Charles Maier;
  19. * Copyright (c) 2001-2006 by Charles Maier Associates;
  20. * Licensed under the Internet Software Consortium License;
  21. *
  22. * Contributor(s):
  23. * Nathaniel Houghton
  24. *
  25. *--------------------------------------------------------------------*/
  26. #ifndef MEMOUT_SOURCE
  27. #define MEMOUT_SOURCE
  28. #include <stdio.h>
  29. #include <stddef.h>
  30. #include "../tools/memory.h"
  31. void memout (void const * memory, size_t extent, char const * format, unsigned group, char c, char e, FILE * fp)
  32. {
  33. byte * origin = (byte *) (memory);
  34. byte * offset = (byte *) (memory);
  35. while (extent--)
  36. {
  37. ptrdiff_t count = (offset - origin) + 1;
  38. fprintf (fp, format, * offset);
  39. if ((count % group) == 0 && extent)
  40. {
  41. putc (c, fp);
  42. }
  43. offset++;
  44. }
  45. if (e)
  46. {
  47. putc (c, fp);
  48. }
  49. return;
  50. }
  51. #endif