FreeCalypso > hg > sms-coding-utils
annotate libcoding/gsm7_encode.c @ 23:e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 02 Sep 2023 19:22:05 +0000 |
parents | 2d0082216916 |
children |
rev | line source |
---|---|
0
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This library module implements the function for encoding from ISO 8859-1 |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * into the GSM 7-bit default alphabet (03.38 or 23.038). |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
23
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
7 #include <ctype.h> |
0
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 extern u_char gsm7_encode_table[256]; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
23
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
11 static unsigned |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
12 handle_escape(ipp) |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
13 u_char **ipp; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
14 { |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
15 unsigned c; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
16 |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
17 c = *(*ipp)++; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
18 if (c >= '0' && c <= '7' && isxdigit(**ipp)) { |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
19 c = ((c - '0') << 4) | decode_hex_digit(*(*ipp)++); |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
20 return c; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
21 } |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
22 switch (c) { |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
23 case 'n': |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
24 return '\n'; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
25 case 'r': |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
26 return '\r'; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
27 case 'e': |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
28 return 0x1B; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
29 case 'E': /* Euro currency symbol */ |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
30 return 0xE5; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
31 case '"': |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
32 case '\\': |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
33 c = gsm7_encode_table[c]; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
34 return c; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
35 default: |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
36 return 0xFF; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
37 } |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
38 } |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
39 |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
40 latin1_to_gsm7(inbuf, outbuf, outmax, outlenp, allow_escape) |
0
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 u_char *inbuf, *outbuf; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 unsigned outmax, *outlenp; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 { |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 u_char *ip = inbuf, *op = outbuf; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 unsigned outcnt = 0, c, n; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 while (c = *ip++) { |
23
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
48 if (c == '\\' && allow_escape) { |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
49 c = handle_escape(&ip); |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
50 if (c == 0xFF) |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
51 return(-3); |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
52 } else { |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
53 c = gsm7_encode_table[c]; |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
54 if (c == 0xFF) |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
55 return(-1); |
e56bb9f09ff1
sms-encode-text: port over -e option from fcup-smsend
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
56 } |
0
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 if (c & 0x80) |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 n = 2; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 else |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 n = 1; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 if (outcnt + n > outmax) |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 return(-2); |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 if (c & 0x80) { |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 *op++ = 0x1B; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 *op++ = c & 0x7F; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 } else |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 *op++ = c; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 outcnt += n; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 *outlenp = outcnt; |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 return(0); |
2d0082216916
libcoding: beginning with a subset of uptools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 } |