FreeCalypso > hg > freecalypso-tools
comparison target-utils/simagent/stringarg.c @ 776:fac3176de18d
simagent: bare Tx implemented
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Fri, 12 Mar 2021 23:36:46 +0000 |
| parents | |
| children | 0cffc53991f9 |
comparison
equal
deleted
inserted
replaced
| 775:6ec781e61e68 | 776:fac3176de18d |
|---|---|
| 1 /* | |
| 2 * This module contains the function that parses our hex string arguments. | |
| 3 */ | |
| 4 | |
| 5 #include <ctype.h> | |
| 6 #include "types.h" | |
| 7 | |
| 8 static | |
| 9 decode_hex_digit(c) | |
| 10 { | |
| 11 if (isdigit(c)) | |
| 12 return c - '0'; | |
| 13 else if (islower(c)) | |
| 14 return c - 'a' + 10; | |
| 15 else | |
| 16 return c - 'A' + 10; | |
| 17 } | |
| 18 | |
| 19 decode_hex_string_arg(arg, buf, maxlen) | |
| 20 char *arg; | |
| 21 u8 *buf; | |
| 22 unsigned maxlen; | |
| 23 { | |
| 24 unsigned count; | |
| 25 | |
| 26 for (count = 0; ; ) { | |
| 27 while (isspace(*arg)) | |
| 28 arg++; | |
| 29 if (!*arg) | |
| 30 break; | |
| 31 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) { | |
| 32 printf("ERROR: invalid hex string input\n"); | |
| 33 return; | |
| 34 } | |
| 35 if (count >= maxlen) { | |
| 36 printf("ERROR: hex string input is too long\n"); | |
| 37 return; | |
| 38 } | |
| 39 buf[count++] = (decode_hex_digit(arg[0]) << 4) | | |
| 40 decode_hex_digit(arg[1]); | |
| 41 } | |
| 42 return count; | |
| 43 } |
