hexencode.c.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. hexencode.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='hexdump.c.html' title=' hexdump.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='hexload.c.html' title=' hexload.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * size_t hexencode (void * memory, size_t extent, char const * string);
  24. *
  25. * memory.h
  26. *
  27. * encode a hexadecimal string into a fixed length memory region;
  28. * return the number of bytes encoded or 0 on error; an error will
  29. * occur of the entire region cannot be encoded or the entire
  30. * string cannot be converted due to illegal or excessive digits;
  31. *
  32. * permit an optional HEX_EXTENDER character between successive
  33. * octets; constant character HEX_EXTENDER is defined in number.h;
  34. *
  35. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  36. * Copyright (c) 2001-2006 by Charles Maier Associates;
  37. * Licensed under the Internet Software Consortium License;
  38. *
  39. *--------------------------------------------------------------------*/
  40. #ifndef HEXENCODE_SOURCE
  41. #define HEXENCODE_SOURCE
  42. #include &lt;errno.h&gt;
  43. #include &lt;ctype.h&gt;
  44. #include &quot;../tools/memory.h&quot;
  45. #include &quot;../tools/number.h&quot;
  46. size_t hexencode (void * memory, register size_t extent, register char const * string)
  47. {
  48. register byte * origin = (byte *)(memory);
  49. register byte * offset = (byte *)(memory);
  50. unsigned radix = RADIX_HEX;
  51. unsigned digit = 0;
  52. while ((extent) &amp;&amp; (*string))
  53. {
  54. unsigned field = HEX_DIGITS;
  55. unsigned value = 0;
  56. if ((offset &gt; origin) &amp;&amp; (*string == HEX_EXTENDER))
  57. {
  58. string++;
  59. }
  60. while (field--)
  61. {
  62. if ((digit = todigit (*string)) &lt; radix)
  63. {
  64. value *= radix;
  65. value += digit;
  66. string++;
  67. continue;
  68. }
  69. errno = EINVAL;
  70. return (0);
  71. }
  72. *offset = value;
  73. offset++;
  74. extent--;
  75. }
  76. #if defined (WIN32)
  77. while (isspace (*string))
  78. {
  79. string++;
  80. }
  81. #endif
  82. if ((extent) || (*string))
  83. {
  84. errno = EINVAL;
  85. return (0);
  86. }
  87. return (offset - origin);
  88. }
  89. #endif
  90. </pre>
  91. <div class='footerlink'>
  92. [<a href='hexdump.c.html' title=' hexdump.c '>PREV</a>]
  93. [<a href='toolkit.html' title=' Index '>HOME</a>]
  94. [<a href='hexload.c.html' title=' hexload.c '>NEXT</a>]
  95. </div>
  96. </body>
  97. </html>