hexcopy.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * size_t hexcopy (void * memory, size_t extent, char const * string);
  11. *
  12. * memory.h
  13. *
  14. * encode a hexadecimal string into a variable length memory region;
  15. * return the number of bytes encoded or 0 on error; an error will
  16. * occur if the entire string cannot be converted due to illegal or
  17. * excessive digits;
  18. *
  19. * permit an optional HEX_EXTENDER character between successive
  20. * octets; constant character HEX_EXTENDER is defined in number.h;
  21. *
  22. * Motley Tools by Charles Maier <cmaier@cmassoc.net>;
  23. * Copyright (c) 2001-2006 by Charles Maier Associates;
  24. * Licensed under the Internet Software Consortium License;
  25. *
  26. *--------------------------------------------------------------------*/
  27. #ifndef HEXCOPY_HEADER
  28. #define HEXCOPY_HEADER
  29. #include "../tools/number.h"
  30. #include "../tools/memory.h"
  31. size_t hexcopy (void * memory, register size_t extent, register char const * string)
  32. {
  33. register byte * origin = (byte *) (memory);
  34. register byte * offset = (byte *) (memory);
  35. unsigned radix = RADIX_HEX;
  36. unsigned digit = 0;
  37. while ((extent) && (* string))
  38. {
  39. unsigned field = HEX_DIGITS;
  40. unsigned value = 0;
  41. if ((offset > origin) && (* string == HEX_EXTENDER))
  42. {
  43. string++;
  44. }
  45. while (field--)
  46. {
  47. if ((digit = todigit (* string)) < radix)
  48. {
  49. value *= radix;
  50. value += digit;
  51. string++;
  52. continue;
  53. }
  54. return (0);
  55. }
  56. * offset = value;
  57. offset++;
  58. extent--;
  59. }
  60. return (offset - origin);
  61. }
  62. #endif