123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #ifndef BYTESPEC_SOURCE
- #define BYTESPEC_SOURCE
- #include <ctype.h>
- #include <errno.h>
- #include "../tools/memory.h"
- #include "../tools/number.h"
- #include "../tools/error.h"
- size_t bytespec (char const * string, void * memory, size_t extent)
- {
- char const * number = string;
- byte * origin = (byte *)(memory);
- byte * offset = (byte *)(memory);
- if (!number)
- {
- error (1, EINVAL, "bytespec");
- }
- while (isspace (*number))
- {
- number++;
- }
- while ((*number) && (extent))
- {
- unsigned digit;
- if ((offset > origin) && (*number == HEX_EXTENDER))
- {
- number++;
- }
- if ((digit = todigit (*number++)) >= RADIX_HEX)
- {
- error (1, EINVAL, "You said '%s' but I want a hex digit", string);
- }
- *offset = digit << 4;
- if ((digit = todigit (*number++)) >= RADIX_HEX)
- {
- error (1, EINVAL, "You said '%s' but I want a hex digit", string);
- }
- *offset |= digit;
- offset++;
- extent--;
- }
- while (isspace (*number))
- {
- number++;
- }
- if ((*number) || (extent))
- {
- error (1, EINVAL, "%s is not %d bytes", string, (unsigned)(offset - origin + extent));
- }
- return (offset - origin);
- }
- #endif
|