bytespec.c.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. bytespec.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='__bswap.c.html' title=' __bswap.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='ChangeIdent.c.html' title=' ChangeIdent.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * size_t bytespec (char const *string, void * memory, size_t extent);
  24. *
  25. * memory.h
  26. *
  27. * encode a memory region with a fixed-length hexadecimal string;
  28. * return the number of bytes encoded or terminate the program on
  29. * error;
  30. *
  31. * the number of octets in string must equal the memory extent or
  32. * an error will occur; octets may be seperated by colons; empty
  33. * octets are illegal;
  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 BYTESPEC_SOURCE
  41. #define BYTESPEC_SOURCE
  42. #include &lt;ctype.h&gt;
  43. #include &lt;errno.h&gt;
  44. #include &quot;../tools/memory.h&quot;
  45. #include &quot;../tools/number.h&quot;
  46. #include &quot;../tools/error.h&quot;
  47. size_t bytespec (char const * string, void * memory, size_t extent)
  48. {
  49. char const * number = string;
  50. byte * origin = (byte *)(memory);
  51. byte * offset = (byte *)(memory);
  52. if (!number)
  53. {
  54. error (1, EINVAL, &quot;bytespec&quot;);
  55. }
  56. while (isspace (*number))
  57. {
  58. number++;
  59. }
  60. while ((*number) &amp;&amp; (extent))
  61. {
  62. unsigned digit;
  63. if ((offset &gt; origin) &amp;&amp; (*number == HEX_EXTENDER))
  64. {
  65. number++;
  66. }
  67. if ((digit = todigit (*number++)) &gt;= RADIX_HEX)
  68. {
  69. error (1, EINVAL, &quot;You said '%s' but I want a hex digit&quot;, string);
  70. }
  71. *offset = digit &lt;&lt; 4;
  72. if ((digit = todigit (*number++)) &gt;= RADIX_HEX)
  73. {
  74. error (1, EINVAL, &quot;You said '%s' but I want a hex digit&quot;, string);
  75. }
  76. *offset |= digit;
  77. offset++;
  78. extent--;
  79. }
  80. while (isspace (*number))
  81. {
  82. number++;
  83. }
  84. if ((*number) || (extent))
  85. {
  86. error (1, EINVAL, &quot;%s is not %d bytes&quot;, string, (unsigned)(offset - origin + extent));
  87. }
  88. return (offset - origin);
  89. }
  90. #endif
  91. </pre>
  92. <div class='footerlink'>
  93. [<a href='__bswap.c.html' title=' __bswap.c '>PREV</a>]
  94. [<a href='toolkit.html' title=' Index '>HOME</a>]
  95. [<a href='ChangeIdent.c.html' title=' ChangeIdent.c '>NEXT</a>]
  96. </div>
  97. </body>
  98. </html>