FreeCalypso > hg > fc-pcsc-tools
annotate libcommon/number_encode.c @ 93:1743802e494e
doc/Low-level-commands: savebin command documented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 17 Feb 2021 19:35:56 +0000 |
parents | 52ec2d3eb851 |
children |
rev | line source |
---|---|
11
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements functions for encoding phone numbers. |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <ctype.h> |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdlib.h> |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 digit_char_to_gsm(ch) |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 switch (ch) { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 case '0': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 case '1': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 case '2': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 case '3': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 case '4': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 case '5': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 case '6': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 case '7': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 case '8': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 case '9': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 return (ch - '0'); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 case '*': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 return 0xA; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 case '#': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 return 0xB; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 case 'a': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 case 'b': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 case 'c': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 return (ch - 'a' + 0xC); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 case 'A': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 case 'B': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 case 'C': |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 return (ch - 'A' + 0xC); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 return (-1); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 void |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 pack_digit_bytes(digits, dest, num_digit_bytes) |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 u_char *digits, *dest; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 unsigned num_digit_bytes; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 u_char *sp, *dp; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 unsigned n; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 sp = digits; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 dp = dest; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 for (n = 0; n < num_digit_bytes; n++) { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 *dp++ = sp[0] | (sp[1] << 4); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 sp += 2; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 } |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 |
43
52ec2d3eb851
encode_phone_number_arg() now supports both encoding modes
Mychaela Falconia <falcon@freecalypso.org>
parents:
11
diff
changeset
|
56 encode_phone_number_arg(arg, fixp, mode) |
11
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 char *arg; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 u_char *fixp; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 u_char digits[20]; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 unsigned ndigits, num_digit_bytes; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 char *cp, *endp; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 int c; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 cp = arg; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (*cp == '+') { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 fixp[1] = 0x91; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 cp++; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } else |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 fixp[1] = 0x81; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (digit_char_to_gsm(*cp) < 0) { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 inv_arg: fprintf(stderr, "error: invalid phone number argument\n"); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 return(-1); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 } |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 for (ndigits = 0; ; ndigits++) { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 c = digit_char_to_gsm(*cp); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 if (c < 0) |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 break; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 cp++; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 if (ndigits >= 20) { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 fprintf(stderr, "error: too many number digits\n"); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 return(-1); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 } |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 digits[ndigits] = c; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 } |
43
52ec2d3eb851
encode_phone_number_arg() now supports both encoding modes
Mychaela Falconia <falcon@freecalypso.org>
parents:
11
diff
changeset
|
86 if (mode) |
52ec2d3eb851
encode_phone_number_arg() now supports both encoding modes
Mychaela Falconia <falcon@freecalypso.org>
parents:
11
diff
changeset
|
87 fixp[0] = ndigits; |
11
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 if (ndigits & 1) |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 digits[ndigits++] = 0xF; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 num_digit_bytes = ndigits >> 1; |
43
52ec2d3eb851
encode_phone_number_arg() now supports both encoding modes
Mychaela Falconia <falcon@freecalypso.org>
parents:
11
diff
changeset
|
91 if (!mode) |
52ec2d3eb851
encode_phone_number_arg() now supports both encoding modes
Mychaela Falconia <falcon@freecalypso.org>
parents:
11
diff
changeset
|
92 fixp[0] = num_digit_bytes + 1; |
11
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 pack_digit_bytes(digits, fixp + 2, num_digit_bytes); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 if (*cp == ',') { |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 cp++; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 if (!isdigit(*cp)) |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 goto inv_arg; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 fixp[1] = strtoul(cp, &endp, 0); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 if (*endp) |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 goto inv_arg; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 } else if (*cp) |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 goto inv_arg; |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 return(0); |
62cdfed70de7
phone number encoding factored out of pb-update code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 } |