FreeCalypso > hg > freecalypso-hwlab
comparison simtool/hexread.c @ 150:54e33e9238b6
fc-simtool: harmonize hex string code with fc-uicc-tool
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 07 Feb 2021 00:06:50 +0000 |
parents | 4aaf722ab933 |
children |
comparison
equal
deleted
inserted
replaced
149:451ed3bbfe96 | 150:54e33e9238b6 |
---|---|
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <strings.h> | 9 #include <strings.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 | |
13 decode_hex_digit(c) | |
14 { | |
15 if (c >= '0' && c <= '9') | |
16 return(c - '0'); | |
17 if (c >= 'A' && c <= 'F') | |
18 return(c - 'A' + 10); | |
19 if (c >= 'a' && c <= 'f') | |
20 return(c - 'a' + 10); | |
21 return(-1); | |
22 } | |
23 | 12 |
24 read_hex_data_file(filename, databuf) | 13 read_hex_data_file(filename, databuf) |
25 char *filename; | 14 char *filename; |
26 u_char *databuf; | 15 u_char *databuf; |
27 { | 16 { |
63 fprintf(stderr, "%s: no hex data input found\n", filename); | 52 fprintf(stderr, "%s: no hex data input found\n", filename); |
64 return(-1); | 53 return(-1); |
65 } | 54 } |
66 return(count); | 55 return(count); |
67 } | 56 } |
68 | |
69 decode_hex_data_from_string(arg, databuf) | |
70 char *arg; | |
71 u_char *databuf; | |
72 { | |
73 unsigned count; | |
74 | |
75 for (count = 0; ; count++) { | |
76 while (isspace(*arg)) | |
77 arg++; | |
78 if (!*arg) | |
79 break; | |
80 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) { | |
81 fprintf(stderr, "error: invalid hex string input\n"); | |
82 return(-1); | |
83 } | |
84 if (count >= 255) { | |
85 fprintf(stderr, "error: hex input data is too long\n"); | |
86 return(-1); | |
87 } | |
88 databuf[count] = (decode_hex_digit(arg[0]) << 4) | | |
89 decode_hex_digit(arg[1]); | |
90 arg += 2; | |
91 } | |
92 if (!count) { | |
93 fprintf(stderr, "error: empty hex string argument\n"); | |
94 return(-1); | |
95 } | |
96 return(count); | |
97 } |