FreeCalypso > hg > sms-coding-utils
comparison libcoding/gsm7_decode.c @ 27:7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
that are needed for sms-pdu-decode & pcm-sms-decode
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 13 Jun 2024 02:29:29 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
26:c8cb05b69118 | 27:7418ca2e9949 |
---|---|
1 /* | |
2 * This library module implements the decoding of GSM7-encoded data | |
3 * into ASCII, ISO 8859-1 or UTF-8. | |
4 */ | |
5 | |
6 #include <sys/types.h> | |
7 #include <stdio.h> | |
8 | |
9 extern u_short gsm7_decode_table[128]; | |
10 extern u_short gsm7ext_decode_table[128]; | |
11 | |
12 gsm7_to_ascii_or_ext(inbuf, inlen, outbuf, outlenp, ascii_ext, newline_ok) | |
13 u_char *inbuf, *outbuf; | |
14 unsigned inlen, *outlenp; | |
15 { | |
16 u_char *inp, *endp, *outp; | |
17 unsigned gsm, uni; | |
18 int is_ext; | |
19 | |
20 inp = inbuf; | |
21 endp = inbuf + inlen; | |
22 outp = outbuf; | |
23 while (inp < endp) { | |
24 gsm = *inp++; | |
25 if (gsm == 0x1B && inp < endp && *inp != 0x1B && *inp != '\n' | |
26 && *inp != '\r') { | |
27 gsm = *inp++; | |
28 uni = gsm7ext_decode_table[gsm]; | |
29 if (uni == '\\') { | |
30 *outp++ = '\\'; | |
31 *outp++ = '\\'; | |
32 continue; | |
33 } | |
34 if (uni == 0x20AC && ascii_ext < 2) { | |
35 *outp++ = '\\'; | |
36 *outp++ = 'E'; | |
37 continue; | |
38 } | |
39 is_ext = 1; | |
40 } else { | |
41 switch (gsm) { | |
42 case 0x1B: | |
43 *outp++ = '\\'; | |
44 *outp++ = 'e'; | |
45 continue; | |
46 case '\n': | |
47 if (newline_ok) | |
48 *outp++ = '\n'; | |
49 else { | |
50 *outp++ = '\\'; | |
51 *outp++ = 'n'; | |
52 } | |
53 continue; | |
54 case '\r': | |
55 *outp++ = '\\'; | |
56 *outp++ = 'r'; | |
57 continue; | |
58 } | |
59 uni = gsm7_decode_table[gsm]; | |
60 is_ext = 0; | |
61 } | |
62 if (!uni || !is_decoded_char_ok(uni, ascii_ext)) { | |
63 if (is_ext) { | |
64 *outp++ = '\\'; | |
65 *outp++ = 'e'; | |
66 } | |
67 sprintf(outp, "\\%02X", gsm); | |
68 outp += 3; | |
69 } else if (ascii_ext == 2) | |
70 outp += emit_utf8_char(uni, outp); | |
71 else | |
72 *outp++ = uni; | |
73 } | |
74 *outp = '\0'; | |
75 if (outlenp) | |
76 *outlenp = outp - outbuf; | |
77 } |