FreeCalypso > hg > sms-coding-utils
annotate libcoding/gsm7_encode2.c @ 15:5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 27 Aug 2023 06:43:23 +0000 |
parents | |
children |
rev | line source |
---|---|
15
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements functions for parsing quoted string |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * arguments intended for GSM7 string encoding, and actually |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * encoding them into GSM7 binary strings. |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 */ |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/types.h> |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <ctype.h> |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 extern u_char gsm7_encode_table[256]; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 qstring_arg_to_gsm7(arg, record, maxlen) |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char *arg; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 u_char *record; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 unsigned maxlen; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 { |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 unsigned acclen, nadd; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 char *cp; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 int c; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 cp = arg; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 for (acclen = 0; *cp; ) { |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 c = *cp++; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (c == '\\') { |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 if (*cp == '\0') |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 return(-1); |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 c = *cp++; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if (c >= '0' && c <= '7' && isxdigit(*cp)) { |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 c = ((c - '0') << 4) | decode_hex_digit(*cp++); |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 goto bypass_encoding; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 } |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 switch (c) { |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 case 'n': |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 c = '\n'; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 goto bypass_encoding; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 case 'r': |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 c = '\r'; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 goto bypass_encoding; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 case 'e': |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 c = 0x1B; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 goto bypass_encoding; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 case 'E': /* Euro currency symbol */ |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 c = 0xE5; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 goto bypass_encoding; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 case '"': |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 case '\\': |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 break; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 default: |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 return(-1); |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 } |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 } |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 c = gsm7_encode_table[c]; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 if (c == 0xFF) |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 return(-1); |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 bypass_encoding: |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 if (c & 0x80) |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 nadd = 2; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 else |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 nadd = 1; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 if (acclen + nadd > maxlen) |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 return(-1); |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 if (c & 0x80) |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 record[acclen++] = 0x1B; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 record[acclen++] = c & 0x7F; |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 } |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 return(acclen); |
5854e48d0ef7
sms-gen-tpdu: add support for alphanumeric user-addr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 } |