FreeCalypso > hg > sms-coding-utils
annotate libcoding/number_encode.c @ 25:2a19b44c272e
convert to new ThemWi configure and build system
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 21 May 2024 01:59:17 +0000 |
parents | c79d09284c5f |
children |
rev | line source |
---|---|
4
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements functions for encoding phone numbers. |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <ctype.h> |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdlib.h> |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 digit_char_to_gsm(ch) |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 { |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 switch (ch) { |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 case '0': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 case '1': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 case '2': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 case '3': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 case '4': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 case '5': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 case '6': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 case '7': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 case '8': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 case '9': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 return (ch - '0'); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 case '*': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 return 0xA; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 case '#': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 return 0xB; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 case 'a': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 case 'b': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 case 'c': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 return (ch - 'a' + 0xC); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 case 'A': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 case 'B': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 case 'C': |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 return (ch - 'A' + 0xC); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 return (-1); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 void |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 pack_digit_bytes(digits, dest, num_digit_bytes) |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 u_char *digits, *dest; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 unsigned num_digit_bytes; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 { |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 u_char *sp, *dp; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 unsigned n; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 sp = digits; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 dp = dest; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 for (n = 0; n < num_digit_bytes; n++) { |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 *dp++ = sp[0] | (sp[1] << 4); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 sp += 2; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 } |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 encode_phone_number_arg(arg, fixp, mode) |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 char *arg; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 u_char *fixp; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 { |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 u_char digits[20]; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 unsigned ndigits, num_digit_bytes; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 char *cp, *endp; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 int c; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 cp = arg; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (*cp == '+') { |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 fixp[1] = 0x91; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 cp++; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } else |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 fixp[1] = 0x81; |
5
c79d09284c5f
encode_phone_number_arg(): don't print to stderr, just return -1
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
71 if (digit_char_to_gsm(*cp) < 0) |
4
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 return(-1); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 for (ndigits = 0; ; ndigits++) { |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 c = digit_char_to_gsm(*cp); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 if (c < 0) |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 break; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 cp++; |
5
c79d09284c5f
encode_phone_number_arg(): don't print to stderr, just return -1
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
78 if (ndigits >= 20) |
4
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 return(-1); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 digits[ndigits] = c; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 } |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 if (mode) |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 fixp[0] = ndigits; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 if (ndigits & 1) |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 digits[ndigits++] = 0xF; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 num_digit_bytes = ndigits >> 1; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 if (!mode) |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 fixp[0] = num_digit_bytes + 1; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 pack_digit_bytes(digits, fixp + 2, num_digit_bytes); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 if (*cp == ',') { |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 cp++; |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 if (!isdigit(*cp)) |
5
c79d09284c5f
encode_phone_number_arg(): don't print to stderr, just return -1
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
93 return(-1); |
4
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 fixp[1] = strtoul(cp, &endp, 0); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 if (*endp) |
5
c79d09284c5f
encode_phone_number_arg(): don't print to stderr, just return -1
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
96 return(-1); |
4
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 } else if (*cp) |
5
c79d09284c5f
encode_phone_number_arg(): don't print to stderr, just return -1
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
98 return(-1); |
4
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 return(0); |
d88c5ab03447
libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 } |