hexdecode.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*====================================================================*
  2. *
  3. * size_t hexdecode (void const * memory, size_t extent, char buffer [], size_t length);
  4. *
  5. * memory.h
  6. *
  7. * decode a memory region as a string of hex octets separated with
  8. * character HEX_EXTENDER;
  9. *
  10. * allow three string characters for each memory byte; this means
  11. * that the buffer must hold at least three characters or nothing
  12. * will be decoded; the maximum number of bytes is the lesser of
  13. * chars/3 and bytes;
  14. *
  15. * Motley Tools by Charles Maier;
  16. * Copyright (c) 2001-2006 by Charles Maier Associates;
  17. * Licensed under the Internet Software Consortium License;
  18. *
  19. *--------------------------------------------------------------------*/
  20. #ifndef HEXDECODE_SOURCE
  21. #define HEXDECODE_SOURCE
  22. #include "../tools/memory.h"
  23. #include "../tools/number.h"
  24. size_t hexdecode (void const * memory, register size_t extent, char buffer [], register size_t length)
  25. {
  26. register char * string = (char *)(buffer);
  27. register byte * offset = (byte *)(memory);
  28. if (length)
  29. {
  30. length /= HEX_DIGITS + 1;
  31. while ((length--) && (extent--))
  32. {
  33. *string++ = DIGITS_HEX [(*offset >> 4) & 0x0F];
  34. *string++ = DIGITS_HEX [(*offset >> 0) & 0x0F];
  35. if ((length) && (extent))
  36. {
  37. *string++ = HEX_EXTENDER;
  38. }
  39. offset++;
  40. }
  41. *string = (char) (0);
  42. }
  43. return (string - buffer);
  44. }
  45. #endif