hexload.c.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. hexload.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='hexencode.c.html' title=' hexencode.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='hexoffset.c.html' title=' hexoffset.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * size_t hexload (void * memory, size_t extent, FILE * fp);
  24. *
  25. * memory.h
  26. *
  27. * read a file and convert hexadecimal octets to binary bytes then
  28. * store them in consecutive memory locations up to a given length;
  29. * return the actual number of bytes stored;
  30. *
  31. * digits may be consecutive or separated by white space or comment
  32. * text; a colon terminates a frame, to allow multiple frames in a
  33. * on one file;
  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 HEXLOAD_SOURCE
  41. #define HEXLOAD_SOURCE
  42. #include &lt;stdio.h&gt;
  43. #include &lt;ctype.h&gt;
  44. #include &lt;stdint.h&gt;
  45. #include &lt;errno.h&gt;
  46. #include &quot;../tools/memory.h&quot;
  47. #include &quot;../tools/error.h&quot;
  48. #include &quot;../tools/chars.h&quot;
  49. /*====================================================================*
  50. * private variables;
  51. *--------------------------------------------------------------------*/
  52. static unsigned row = 1;
  53. static unsigned col = 1;
  54. /*====================================================================*
  55. *
  56. * signed fpgetc (FILE * fp)
  57. *
  58. * return the next input character after updating the file cursor
  59. * position;
  60. *
  61. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  62. * Copyright (c) 2001-2006 by Charles Maier Associates;
  63. * Licensed under the Internet Software Consortium License;
  64. *
  65. *--------------------------------------------------------------------*/
  66. static signed fpgetc (FILE * fp)
  67. {
  68. extern unsigned row;
  69. extern unsigned col;
  70. signed c = getc (fp);
  71. if (c == '\n')
  72. {
  73. row++;
  74. col = 0;
  75. }
  76. else
  77. {
  78. col++;
  79. }
  80. return (c);
  81. }
  82. /*====================================================================*
  83. *
  84. * size_t hexload (void * memory, size_t extent, FILE * fp);
  85. *
  86. * memory.h
  87. *
  88. * read a file and convert hexadecimal octets to binary bytes then
  89. * store them in consecutive memory locations up to a given length;
  90. * return the actual number of bytes stored;
  91. *
  92. * digits may be consecutive or separated by white space or comment
  93. * text; a colon terminates a frame, to allow multiple frames in a
  94. * on one file;
  95. *
  96. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  97. * Copyright (c) 2001-2006 by Charles Maier Associates;
  98. * Licensed under the Internet Software Consortium License;
  99. *
  100. *--------------------------------------------------------------------*/
  101. size_t hexload (void * memory, size_t extent, FILE * fp)
  102. {
  103. extern unsigned row;
  104. extern unsigned col;
  105. byte * origin = (uint8_t *)(memory);
  106. byte * offset = (uint8_t *)(memory);
  107. unsigned digits = sizeof (* offset) &lt;&lt; 1;
  108. unsigned digit = 0;
  109. signed c = EOF;
  110. while ((extent) &amp;&amp; ((c = fpgetc (fp)) != EOF) &amp;&amp; (c != ';'))
  111. {
  112. if (isspace (c))
  113. {
  114. continue;
  115. }
  116. if (c == '#')
  117. {
  118. do
  119. {
  120. c = fpgetc (fp);
  121. }
  122. while (nobreak (c));
  123. continue;
  124. }
  125. if (c == '/')
  126. {
  127. c = fpgetc (fp);
  128. if (c == '/')
  129. {
  130. do
  131. {
  132. c = fpgetc (fp);
  133. }
  134. while (nobreak (c));
  135. continue;
  136. }
  137. if (c == '*')
  138. {
  139. while ((c != '/') &amp;&amp; (c != EOF))
  140. {
  141. while ((c != '*') &amp;&amp; (c != EOF))
  142. {
  143. c = fpgetc (fp);
  144. }
  145. c = fpgetc (fp);
  146. }
  147. continue;
  148. }
  149. continue;
  150. }
  151. if ((c &gt;= '0') &amp;&amp; (c &lt;= '9'))
  152. {
  153. *offset *= 16;
  154. *offset += c - '0';
  155. if (!(++digit % digits))
  156. {
  157. offset++;
  158. extent--;
  159. }
  160. continue;
  161. }
  162. if ((c &gt;= 'A') &amp;&amp; (c &lt;= 'F'))
  163. {
  164. *offset *= 16;
  165. *offset += 10;
  166. *offset += c - 'A';
  167. if (!(++digit % digits))
  168. {
  169. offset++;
  170. extent--;
  171. }
  172. continue;
  173. }
  174. if ((c &gt;= 'a') &amp;&amp; (c &lt;= 'f'))
  175. {
  176. *offset *= 16;
  177. *offset += 10;
  178. *offset += c - 'a';
  179. if (!(++digit % digits))
  180. {
  181. offset++;
  182. extent--;
  183. }
  184. continue;
  185. }
  186. #if 1
  187. error (1, ENOTSUP, &quot;Unexpected character '%c': row %d: col %d&quot;, c, row, col);
  188. #else
  189. return ((size_t)(-1));
  190. #endif
  191. }
  192. if (digit &amp; 1)
  193. {
  194. #if 1
  195. error (1, ENOTSUP, &quot;Odd digit count (%d) in source&quot;, digit);
  196. #else
  197. return ((size_t)(-1));
  198. #endif
  199. }
  200. return (offset - origin);
  201. }
  202. #endif
  203. </pre>
  204. <div class='footerlink'>
  205. [<a href='hexencode.c.html' title=' hexencode.c '>PREV</a>]
  206. [<a href='toolkit.html' title=' Index '>HOME</a>]
  207. [<a href='hexoffset.c.html' title=' hexoffset.c '>NEXT</a>]
  208. </div>
  209. </body>
  210. </html>