decdecode.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*====================================================================*
  2. *
  3. * size_t decdecode (void const * memory, size_t extent, char buffer [], size_t length);
  4. *
  5. * memory.h
  6. *
  7. * decode a memory block of given length in bytes as a string of
  8. * separated hexadecimal bytes; terminate once the string fills
  9. * or the memory ends; terminate the string and return the actual
  10. * string bytes;
  11. *
  12. * allow three string characters for each memory byte; this means
  13. * that the buffer must have at least three characters or nothing
  14. * will be decoded; the maximum number of bytes is the lesser of
  15. * chars/3 and bytes;;
  16. *
  17. * Motley Tools by Charles Maier;
  18. * Copyright (c) 2001-2006 by Charles Maier Associates;
  19. * Licensed under the Internet Software Consortium License;
  20. *
  21. *--------------------------------------------------------------------*/
  22. #ifndef DECDECODE_SOURCE
  23. #define DECDECODE_SOURCE
  24. #include "../tools/memory.h"
  25. #include "../tools/number.h"
  26. size_t decdecode (void const * memory, register size_t extent, char buffer [], register size_t length)
  27. {
  28. register char * string = (char *)(buffer);
  29. register byte * offset = (byte *)(memory);
  30. if (length)
  31. {
  32. length /= DEC_DIGITS + 1;
  33. while ((length--) && (extent--))
  34. {
  35. unsigned octet = *offset;
  36. unsigned digit = DEC_DIGITS;
  37. while (digit--)
  38. {
  39. string [digit] = '0' + octet % RADIX_DEC;
  40. octet /= RADIX_DEC;
  41. }
  42. string += DEC_DIGITS;
  43. if ((length) && (extent))
  44. {
  45. *string++ = DEC_EXTENDER;
  46. }
  47. offset++;
  48. }
  49. *string = (char) (0);
  50. }
  51. return (string - buffer);
  52. }
  53. #endif