12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #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 ((unsigned char)* number))
- {
- number++;
- }
- while ((* number) && (extent))
- {
- unsigned digit;
- if ((offset > origin) && (* number == HEX_EXTENDER))
- {
- number++;
- }
- if ((digit = todigit (* number++)) >= RADIX_HEX)
- {
- error (1, EINVAL, "Have '%s' but need a hex digit", string);
- }
- * offset = digit << 4;
- if ((digit = todigit (* number++)) >= RADIX_HEX)
- {
- error (1, EINVAL, "Have '%s' but need a hex digit", string);
- }
- * offset |= digit;
- offset++;
- extent--;
- }
- while (isspace ((unsigned char)* number))
- {
- number++;
- }
- if ((* number) || (extent))
- {
- error (1, EINVAL, "%s is not %d bytes", string, (unsigned) (offset - origin + extent));
- }
- return (offset - origin);
- }
- #endif
|