comparison 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
comparison
equal deleted inserted replaced
22:8a2038c6a156 23:e56bb9f09ff1
2 * This library module implements the function for encoding from ISO 8859-1 2 * This library module implements the function for encoding from ISO 8859-1
3 * into the GSM 7-bit default alphabet (03.38 or 23.038). 3 * into the GSM 7-bit default alphabet (03.38 or 23.038).
4 */ 4 */
5 5
6 #include <sys/types.h> 6 #include <sys/types.h>
7 #include <ctype.h>
7 8
8 extern u_char gsm7_encode_table[256]; 9 extern u_char gsm7_encode_table[256];
9 10
10 latin1_to_gsm7(inbuf, outbuf, outmax, outlenp) 11 static unsigned
12 handle_escape(ipp)
13 u_char **ipp;
14 {
15 unsigned c;
16
17 c = *(*ipp)++;
18 if (c >= '0' && c <= '7' && isxdigit(**ipp)) {
19 c = ((c - '0') << 4) | decode_hex_digit(*(*ipp)++);
20 return c;
21 }
22 switch (c) {
23 case 'n':
24 return '\n';
25 case 'r':
26 return '\r';
27 case 'e':
28 return 0x1B;
29 case 'E': /* Euro currency symbol */
30 return 0xE5;
31 case '"':
32 case '\\':
33 c = gsm7_encode_table[c];
34 return c;
35 default:
36 return 0xFF;
37 }
38 }
39
40 latin1_to_gsm7(inbuf, outbuf, outmax, outlenp, allow_escape)
11 u_char *inbuf, *outbuf; 41 u_char *inbuf, *outbuf;
12 unsigned outmax, *outlenp; 42 unsigned outmax, *outlenp;
13 { 43 {
14 u_char *ip = inbuf, *op = outbuf; 44 u_char *ip = inbuf, *op = outbuf;
15 unsigned outcnt = 0, c, n; 45 unsigned outcnt = 0, c, n;
16 46
17 while (c = *ip++) { 47 while (c = *ip++) {
18 c = gsm7_encode_table[c]; 48 if (c == '\\' && allow_escape) {
19 if (c == 0xFF) 49 c = handle_escape(&ip);
20 return(-1); 50 if (c == 0xFF)
51 return(-3);
52 } else {
53 c = gsm7_encode_table[c];
54 if (c == 0xFF)
55 return(-1);
56 }
21 if (c & 0x80) 57 if (c & 0x80)
22 n = 2; 58 n = 2;
23 else 59 else
24 n = 1; 60 n = 1;
25 if (outcnt + n > outmax) 61 if (outcnt + n > outmax)