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