FreeCalypso > hg > fc-pcsc-tools
annotate libcommon/decimal_str.c @ 83:07e395fddfe1
lnd-write command implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 17 Feb 2021 06:08:28 +0000 |
parents | 3ef90bd13fbe |
children |
rev | line source |
---|---|
66
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements some functions for initial parsing of decimal |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * string arguments, intended for implementation of commands like |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * write-iccid and write-imsi. |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 */ |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/types.h> |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <ctype.h> |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 parse_decimal_string_arg(arg, dest, maxdigits) |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 char *arg; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 u_char *dest; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 unsigned maxdigits; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 { |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 unsigned n, ndig; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 if (!*arg) { |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 fprintf(stderr, |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 "error: empty argument given for decimal string\n"); |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 return(-1); |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 } |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 for (n = 0; *arg; ) { |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 if (!isdigit(*arg)) { |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 fprintf(stderr, |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 "error: non-digit char in decimal string argument\n"); |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 return(-1); |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 } |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if (n >= maxdigits) { |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 fprintf(stderr, |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 "error: decimal string exceeds limit of %u digits\n", |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 maxdigits); |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 return(-1); |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 } |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 dest[n++] = *arg++ - '0'; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 ndig = n; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 while (n < maxdigits) |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 dest[n++] = 0xF; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 return ndig; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 } |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 pack_reversed_nibbles(nibbles, bytes, nbytes) |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 u_char *nibbles, *bytes; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 unsigned nbytes; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 { |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 u_char *sp, *dp; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 unsigned n; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 sp = nibbles; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 dp = bytes; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 for (n = 0; n < nbytes; n++) { |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 *dp++ = sp[0] | (sp[1] << 4); |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 sp += 2; |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 } |
3ef90bd13fbe
fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 } |