hexload.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*====================================================================*
  2. *
  3. * size_t hexload (void * memory, size_t extent, FILE * fp);
  4. *
  5. * memory.h
  6. *
  7. * read a file and convert hexadecimal octets to binary bytes then
  8. * store them in consecutive memory locations up to a given length;
  9. * return the actual number of bytes stored;
  10. *
  11. * digits may be consecutive or separated by white space or comment
  12. * text; a colon terminates a frame, to allow multiple frames in a
  13. * on one file;
  14. *
  15. * Motley Tools by Charles Maier;
  16. * Copyright (c) 2001-2006 by Charles Maier Associates;
  17. * Licensed under the Internet Software Consortium License;
  18. *
  19. *--------------------------------------------------------------------*/
  20. #ifndef HEXLOAD_SOURCE
  21. #define HEXLOAD_SOURCE
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <stdint.h>
  25. #include <errno.h>
  26. #include "../tools/memory.h"
  27. #include "../tools/error.h"
  28. #include "../tools/chars.h"
  29. /*====================================================================*
  30. * private variables;
  31. *--------------------------------------------------------------------*/
  32. static unsigned row = 1;
  33. static unsigned col = 1;
  34. /*====================================================================*
  35. *
  36. * signed fpgetc (FILE * fp)
  37. *
  38. * return the next input character after updating the file cursor
  39. * position;
  40. *
  41. * Motley Tools by Charles Maier;
  42. * Copyright (c) 2001-2006 by Charles Maier Associates;
  43. * Licensed under the Internet Software Consortium License;
  44. *
  45. *--------------------------------------------------------------------*/
  46. static signed fpgetc (FILE * fp)
  47. {
  48. extern unsigned row;
  49. extern unsigned col;
  50. signed c = getc (fp);
  51. if (c == '\n')
  52. {
  53. row++;
  54. col = 0;
  55. }
  56. else
  57. {
  58. col++;
  59. }
  60. return (c);
  61. }
  62. /*====================================================================*
  63. *
  64. * size_t hexload (void * memory, size_t extent, FILE * fp);
  65. *
  66. * memory.h
  67. *
  68. * read a file and convert hexadecimal octets to binary bytes then
  69. * store them in consecutive memory locations up to a given length;
  70. * return the actual number of bytes stored;
  71. *
  72. * digits may be consecutive or separated by white space or comment
  73. * text; a colon terminates a frame, to allow multiple frames in a
  74. * on one file;
  75. *
  76. * Motley Tools by Charles Maier;
  77. * Copyright (c) 2001-2006 by Charles Maier Associates;
  78. * Licensed under the Internet Software Consortium License;
  79. *
  80. *--------------------------------------------------------------------*/
  81. size_t hexload (void * memory, size_t extent, FILE * fp)
  82. {
  83. extern unsigned row;
  84. extern unsigned col;
  85. byte * origin = (uint8_t *)(memory);
  86. byte * offset = (uint8_t *)(memory);
  87. unsigned digits = sizeof (* offset) << 1;
  88. unsigned digit = 0;
  89. signed c = EOF;
  90. while ((extent) && ((c = fpgetc (fp)) != EOF) && (c != ';'))
  91. {
  92. if (isspace (c))
  93. {
  94. continue;
  95. }
  96. if (c == '#')
  97. {
  98. do
  99. {
  100. c = fpgetc (fp);
  101. }
  102. while (nobreak (c));
  103. continue;
  104. }
  105. if (c == '/')
  106. {
  107. c = fpgetc (fp);
  108. if (c == '/')
  109. {
  110. do
  111. {
  112. c = fpgetc (fp);
  113. }
  114. while (nobreak (c));
  115. continue;
  116. }
  117. if (c == '*')
  118. {
  119. while ((c != '/') && (c != EOF))
  120. {
  121. while ((c != '*') && (c != EOF))
  122. {
  123. c = fpgetc (fp);
  124. }
  125. c = fpgetc (fp);
  126. }
  127. continue;
  128. }
  129. continue;
  130. }
  131. if ((c >= '0') && (c <= '9'))
  132. {
  133. *offset *= 16;
  134. *offset += c - '0';
  135. if (!(++digit % digits))
  136. {
  137. offset++;
  138. extent--;
  139. }
  140. continue;
  141. }
  142. if ((c >= 'A') && (c <= 'F'))
  143. {
  144. *offset *= 16;
  145. *offset += 10;
  146. *offset += c - 'A';
  147. if (!(++digit % digits))
  148. {
  149. offset++;
  150. extent--;
  151. }
  152. continue;
  153. }
  154. if ((c >= 'a') && (c <= 'f'))
  155. {
  156. *offset *= 16;
  157. *offset += 10;
  158. *offset += c - 'a';
  159. if (!(++digit % digits))
  160. {
  161. offset++;
  162. extent--;
  163. }
  164. continue;
  165. }
  166. #if 1
  167. error (1, ENOTSUP, "Unexpected character '%c': row %d: col %d", c, row, col);
  168. #else
  169. return ((size_t)(-1));
  170. #endif
  171. }
  172. if (digit & 1)
  173. {
  174. #if 1
  175. error (1, ENOTSUP, "Odd digit count (%d) in source", digit);
  176. #else
  177. return ((size_t)(-1));
  178. #endif
  179. }
  180. return (offset - origin);
  181. }
  182. #endif