annotate libcommon/number_decode.c @ 8:4a9bf783491d

phone number decoding factored out
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 00:07:24 +0000
parents
children f5a26c1d0b93
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements functions for decoding phone numbers.
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 static char gsm_address_digits[16] =
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 {'0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'};
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 decode_phone_number(data, nbytes, out)
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 u_char *data;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 unsigned nbytes;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 char *out;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 {
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 u_char *dp, *endp;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 int c;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 dp = data;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 endp = data + nbytes;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 while (dp < endp) {
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 c = *dp & 0xF;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (c == 0xF)
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 return(-1);
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 *out++ = gsm_address_digits[c];
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 c = *dp >> 4;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (c == 0xF) {
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (dp + 1 == endp)
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 break;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 else
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 return(-1);
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 *out++ = gsm_address_digits[c];
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 dp++;
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 *out = '\0';
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 return(0);
4a9bf783491d phone number decoding factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 }