123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- /*====================================================================*
- *
- * size_t hexload (void * memory, size_t extent, FILE * fp);
- *
- * memory.h
- *
- * read a file and convert hexadecimal octets to binary bytes then
- * store them in consecutive memory locations up to a given length;
- * return the actual number of bytes stored;
- *
- * digits may be consecutive or separated by white space or comment
- * text; a colon terminates a frame, to allow multiple frames in a
- * on one file;
- *
- * Motley Tools by Charles Maier;
- * Copyright (c) 2001-2006 by Charles Maier Associates;
- * Licensed under the Internet Software Consortium License;
- *
- *--------------------------------------------------------------------*/
- #ifndef HEXLOAD_SOURCE
- #define HEXLOAD_SOURCE
- #include <stdio.h>
- #include <ctype.h>
- #include <stdint.h>
- #include <errno.h>
- #include "../tools/memory.h"
- #include "../tools/error.h"
- #include "../tools/chars.h"
- /*====================================================================*
- * private variables;
- *--------------------------------------------------------------------*/
- static unsigned row = 1;
- static unsigned col = 1;
- /*====================================================================*
- *
- * signed fpgetc (FILE * fp)
- *
- * return the next input character after updating the file cursor
- * position;
- *
- * Motley Tools by Charles Maier;
- * Copyright (c) 2001-2006 by Charles Maier Associates;
- * Licensed under the Internet Software Consortium License;
- *
- *--------------------------------------------------------------------*/
- static signed fpgetc (FILE * fp)
- {
- extern unsigned row;
- extern unsigned col;
- signed c = getc (fp);
- if (c == '\n')
- {
- row++;
- col = 0;
- }
- else
- {
- col++;
- }
- return (c);
- }
- /*====================================================================*
- *
- * size_t hexload (void * memory, size_t extent, FILE * fp);
- *
- * memory.h
- *
- * read a file and convert hexadecimal octets to binary bytes then
- * store them in consecutive memory locations up to a given length;
- * return the actual number of bytes stored;
- *
- * digits may be consecutive or separated by white space or comment
- * text; a colon terminates a frame, to allow multiple frames in a
- * on one file;
- *
- * Motley Tools by Charles Maier;
- * Copyright (c) 2001-2006 by Charles Maier Associates;
- * Licensed under the Internet Software Consortium License;
- *
- *--------------------------------------------------------------------*/
- size_t hexload (void * memory, size_t extent, FILE * fp)
- {
- extern unsigned row;
- extern unsigned col;
- byte * origin = (uint8_t *)(memory);
- byte * offset = (uint8_t *)(memory);
- unsigned digits = sizeof (* offset) << 1;
- unsigned digit = 0;
- signed c = EOF;
- while ((extent) && ((c = fpgetc (fp)) != EOF) && (c != ';'))
- {
- if (isspace (c))
- {
- continue;
- }
- if (c == '#')
- {
- do
- {
- c = fpgetc (fp);
- }
- while (nobreak (c));
- continue;
- }
- if (c == '/')
- {
- c = fpgetc (fp);
- if (c == '/')
- {
- do
- {
- c = fpgetc (fp);
- }
- while (nobreak (c));
- continue;
- }
- if (c == '*')
- {
- while ((c != '/') && (c != EOF))
- {
- while ((c != '*') && (c != EOF))
- {
- c = fpgetc (fp);
- }
- c = fpgetc (fp);
- }
- continue;
- }
- continue;
- }
- if ((c >= '0') && (c <= '9'))
- {
- *offset *= 16;
- *offset += c - '0';
- if (!(++digit % digits))
- {
- offset++;
- extent--;
- }
- continue;
- }
- if ((c >= 'A') && (c <= 'F'))
- {
- *offset *= 16;
- *offset += 10;
- *offset += c - 'A';
- if (!(++digit % digits))
- {
- offset++;
- extent--;
- }
- continue;
- }
- if ((c >= 'a') && (c <= 'f'))
- {
- *offset *= 16;
- *offset += 10;
- *offset += c - 'a';
- if (!(++digit % digits))
- {
- offset++;
- extent--;
- }
- continue;
- }
- #if 1
- error (1, ENOTSUP, "Unexpected character '%c': row %d: col %d", c, row, col);
- #else
- return ((size_t)(-1));
- #endif
- }
- if (digit & 1)
- {
- #if 1
- error (1, ENOTSUP, "Odd digit count (%d) in source", digit);
- #else
- return ((size_t)(-1));
- #endif
- }
- return (offset - origin);
- }
- #endif
|