comparison uptools/libcoding/gsm7_decode_qstring.c @ 799:f023a1c187a9

uptools/libcoding: import print_gsm7_string_to_file() function from fc-sim-tools
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 25 Mar 2021 01:26:28 +0000
parents
children
comparison
equal deleted inserted replaced
798:ccaa1319740c 799:f023a1c187a9
1 /*
2 * This module implements a function for decoding GSM7 strings
3 * to ASCII with output to a stdio file; it is an implementation
4 * of lossless conversion per our SIM-data-formats spec
5 * in freecalypso-docs.
6 */
7
8 #include <sys/types.h>
9 #include <stdio.h>
10
11 static char basic_table[128] = {
12 '@', 0, '$', 0, 0, 0, 0, 0,
13 0, 0, 'n'|0x80, 0, 0, 'r'|0x80, 0, 0,
14 0, '_', 0, 0, 0, 0, 0, 0,
15 0, 0, 0, 0, 0, 0, 0, 0,
16 ' ', '!', '"'|0x80, '#', 0, '%', '&', 0x27,
17 '(', ')', '*', '+', ',', '-', '.', '/',
18 '0', '1', '2', '3', '4', '5', '6', '7',
19 '8', '9', ':', ';', '<', '=', '>', '?',
20 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
21 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
22 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
23 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
24 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
25 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
26 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
27 'x', 'y', 'z', 0, 0, 0, 0, 0
28 };
29
30 static char escape_table[128] = {
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 0, 0, 0, '^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, '{', '}', 0, 0, 0, 0, 0, '\\'|0x80,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '[', '~', ']', 0,
35 '|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 0, 0, 0, 0,'E'|0x80,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
39 };
40
41 void
42 print_gsm7_string_to_file(data, nbytes, outf)
43 u_char *data;
44 unsigned nbytes;
45 FILE *outf;
46 {
47 u_char *dp, *endp;
48 int b, c;
49
50 dp = data;
51 endp = data + nbytes;
52 putc('"', outf);
53 while (dp < endp) {
54 b = *dp++;
55 if (b == 0x1B) {
56 if (dp >= endp || *dp == 0x1B || *dp == '\n' ||
57 *dp == '\r') {
58 putc('\\', outf);
59 putc('e', outf);
60 continue;
61 }
62 b = *dp++;
63 c = escape_table[b];
64 if (!c) {
65 fprintf(outf, "\\e\\%02X", b);
66 continue;
67 }
68 } else {
69 c = basic_table[b];
70 if (!c) {
71 fprintf(outf, "\\%02X", b);
72 continue;
73 }
74 }
75 if (c & 0x80) {
76 putc('\\', outf);
77 c &= 0x7F;
78 }
79 putc(c, outf);
80 }
81 putc('"', outf);
82 }