FreeCalypso > hg > freecalypso-tools
annotate uptools/libcoding/number_encode.c @ 702:36e6a1ff9608
lunadrv: rd and wr commands added
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 07 May 2020 02:40:13 +0000 |
parents | 7154a231e4b5 |
children |
rev | line source |
---|---|
358
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This library module implements the function that parses SMS destination |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * addresses given by the user and encodes them into the GSM 03.40 |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * TP-Destination-Address structure. |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 */ |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/types.h> |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <ctype.h> |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdlib.h> |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 digit_char_to_gsm(ch) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 { |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 switch (ch) { |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 case '0': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 case '1': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 case '2': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 case '3': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 case '4': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 case '5': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 case '6': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 case '7': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 case '8': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 case '9': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 return (ch - '0'); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 case '*': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 return 0xA; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 case '#': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 return 0xB; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 case 'a': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 case 'b': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 case 'c': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 return (ch - 'a' + 0xC); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 case 'A': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 case 'B': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 case 'C': |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 return (ch - 'A' + 0xC); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 } |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 return (-1); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 } |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 parse_and_encode_dest_addr(input, outbuf) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 char *input; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 u_char *outbuf; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 { |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 char *cp, *endp; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 int d; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 u_char digits[20]; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 unsigned n; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 cp = input; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 if (*cp == '+') { |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 outbuf[1] = 0x91; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 cp++; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 } else |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 outbuf[1] = 0x81; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 if (digit_char_to_gsm(*cp) < 0) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 return(-1); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 n = 0; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 while ((d = digit_char_to_gsm(*cp)) >= 0) { |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 cp++; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 if (n >= 20) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 return(-1); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 digits[n++] = d; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 } |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 outbuf[0] = n; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 while (n < 20) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 digits[n++] = 0xF; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 if (*cp == ',') { |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 cp++; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 if (!isdigit(*cp)) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 return(-1); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 outbuf[1] = strtoul(cp, &endp, 0); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 if (*endp) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 return(-1); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 } else if (*cp) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 return(-1); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 for (n = 0; n < 10; n++) |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 outbuf[2+n] = (digits[n*2+1] << 4) | digits[n*2]; |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 return(0); |
7154a231e4b5
uptools/libcoding: SMS dest address parsing and encoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 } |