comparison simtool/hexstr.c @ 151:d515cfbb3f39

fc-simtool: hex string parsing: add minimum length parameter
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Feb 2021 00:18:30 +0000
parents 54e33e9238b6
children
comparison
equal deleted inserted replaced
150:54e33e9238b6 151:d515cfbb3f39
18 if (c >= 'a' && c <= 'f') 18 if (c >= 'a' && c <= 'f')
19 return(c - 'a' + 10); 19 return(c - 'a' + 10);
20 return(-1); 20 return(-1);
21 } 21 }
22 22
23 decode_hex_data_from_string(arg, databuf, maxlen) 23 decode_hex_data_from_string(arg, databuf, minlen, maxlen)
24 char *arg; 24 char *arg;
25 u_char *databuf; 25 u_char *databuf;
26 unsigned maxlen; 26 unsigned minlen, maxlen;
27 { 27 {
28 unsigned count; 28 unsigned count;
29 29
30 for (count = 0; ; count++) { 30 for (count = 0; ; count++) {
31 while (isspace(*arg)) 31 while (isspace(*arg))
35 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) { 35 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
36 fprintf(stderr, "error: invalid hex string input\n"); 36 fprintf(stderr, "error: invalid hex string input\n");
37 return(-1); 37 return(-1);
38 } 38 }
39 if (count >= maxlen) { 39 if (count >= maxlen) {
40 fprintf(stderr, "error: hex input data is too long\n"); 40 fprintf(stderr, "error: hex string is too long\n");
41 return(-1); 41 return(-1);
42 } 42 }
43 databuf[count] = (decode_hex_digit(arg[0]) << 4) | 43 databuf[count] = (decode_hex_digit(arg[0]) << 4) |
44 decode_hex_digit(arg[1]); 44 decode_hex_digit(arg[1]);
45 arg += 2; 45 arg += 2;
46 } 46 }
47 if (!count) { 47 if (count < minlen) {
48 fprintf(stderr, "error: empty hex string argument\n"); 48 fprintf(stderr, "error: hex string is too short\n");
49 return(-1); 49 return(-1);
50 } 50 }
51 return(count); 51 return(count);
52 } 52 }