FreeCalypso > hg > freecalypso-tools
comparison uptools/libcoding/gsm7_decode.c @ 328:978571e23318
uptools started with libcoding
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 03 Feb 2018 20:07:05 +0000 |
parents | |
children | 1c599681fd60 |
comparison
equal
deleted
inserted
replaced
327:973d885a68a0 | 328:978571e23318 |
---|---|
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 | |
8 extern u_short gsm7_decode_table[128]; | |
9 extern u_short gsm7ext_decode_table[128]; | |
10 | |
11 gsm7_to_ascii_or_ext(inbuf, inlen, outbuf, outlenp, ascii_ext, newline_ok, errp) | |
12 u_char *inbuf, *outbuf; | |
13 unsigned inlen, *outlenp, *errp; | |
14 { | |
15 u_char *inp, *endp, *outp; | |
16 unsigned errcnt = 0; | |
17 unsigned gsm, uni; | |
18 | |
19 inp = inbuf; | |
20 endp = inbuf + inlen; | |
21 outp = outbuf; | |
22 while (inp < endp) { | |
23 gsm = *inp++; | |
24 if (gsm == 0x1B && inp < endp) | |
25 uni = gsm7ext_decode_table[*inp++]; | |
26 else | |
27 uni = gsm7_decode_table[gsm]; | |
28 if (uni == '\r') { | |
29 *outp++ = '\\'; | |
30 *outp++ = 'r'; | |
31 errcnt++; | |
32 } else if (uni == '\n') { | |
33 if (newline_ok) | |
34 *outp++ = '\n'; | |
35 else { | |
36 *outp++ = '\\'; | |
37 *outp++ = 'n'; | |
38 errcnt++; | |
39 } | |
40 } else if (!uni || !is_decoded_char_ok(uni, ascii_ext)) { | |
41 *outp++ = '?'; | |
42 errcnt++; | |
43 } else if (ascii_ext == 2) | |
44 outp += emit_utf8_char(uni, outp); | |
45 else | |
46 *outp++ = uni; | |
47 } | |
48 *outp = '\0'; | |
49 if (outlenp) | |
50 *outlenp = outp - outbuf; | |
51 if (errp) | |
52 *errp = errcnt; | |
53 } |