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