annotate libcoding/number_encode.c @ 4:d88c5ab03447

libcoding/number_encode.c: import from fc-sim-tools/libutil
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 05 Aug 2023 03:17:09 +0000
parents
children c79d09284c5f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (digit_char_to_gsm(*cp) < 0) {
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 inv_arg: fprintf(stderr, "error: invalid phone number argument\n");
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 return(-1);
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 }
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 for (ndigits = 0; ; ndigits++) {
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 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
77 if (c < 0)
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 break;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 cp++;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 if (ndigits >= 20) {
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 fprintf(stderr, "error: too many number digits\n");
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 return(-1);
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 }
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 digits[ndigits] = c;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 if (mode)
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 fixp[0] = ndigits;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 if (ndigits & 1)
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 digits[ndigits++] = 0xF;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 num_digit_bytes = ndigits >> 1;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 if (!mode)
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 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
93 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
94 if (*cp == ',') {
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 cp++;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 if (!isdigit(*cp))
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 goto inv_arg;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 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
99 if (*endp)
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 goto inv_arg;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 } else if (*cp)
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 goto inv_arg;
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 return(0);
d88c5ab03447 libcoding/number_encode.c: import from fc-sim-tools/libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 }