FreeCalypso > hg > fc-sim-tools
comparison libutil/gsm7_decode.c @ 68:c5e7c9e1d857
GSM7 to qstring decoding: rework in a new way, emit \E for Euro
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 25 Mar 2021 00:04:08 +0000 |
parents | 34bbb0585cab |
children |
comparison
equal
deleted
inserted
replaced
67:1b905e730abd | 68:c5e7c9e1d857 |
---|---|
1 /* | 1 /* |
2 * This module contains functions for decoding GSM7 strings | 2 * This module implements a function for decoding GSM7 strings |
3 * that exist in various SIM files. | 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. | |
4 */ | 6 */ |
5 | 7 |
6 #include <sys/types.h> | 8 #include <sys/types.h> |
7 #include <stdio.h> | 9 #include <stdio.h> |
8 | 10 |
9 static char gsm7_decode_table[128] = { | 11 static char basic_table[128] = { |
10 '@', 0, '$', 0, 0, 0, 0, 0, | 12 '@', 0, '$', 0, 0, 0, 0, 0, |
11 0, 0, '\n', 0, 0, '\r', 0, 0, | 13 0, 0, 'n'|0x80, 0, 0, 'r'|0x80, 0, 0, |
12 0, '_', 0, 0, 0, 0, 0, 0, | 14 0, '_', 0, 0, 0, 0, 0, 0, |
13 0, 0, 0, 0, 0, 0, 0, 0, | 15 0, 0, 0, 0, 0, 0, 0, 0, |
14 ' ', '!', '"', '#', 0, '%', '&', 0x27, | 16 ' ', '!', '"'|0x80, '#', 0, '%', '&', 0x27, |
15 '(', ')', '*', '+', ',', '-', '.', '/', | 17 '(', ')', '*', '+', ',', '-', '.', '/', |
16 '0', '1', '2', '3', '4', '5', '6', '7', | 18 '0', '1', '2', '3', '4', '5', '6', '7', |
17 '8', '9', ':', ';', '<', '=', '>', '?', | 19 '8', '9', ':', ';', '<', '=', '>', '?', |
18 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', | 20 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
19 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | 21 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
20 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | 22 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
21 'X', 'Y', 'Z', 0, 0, 0, 0, 0, | 23 'X', 'Y', 'Z', 0, 0, 0, 0, 0, |
22 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', | 24 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', |
23 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', | 25 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
24 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', | 26 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', |
25 'x', 'y', 'z', 0, 0, 0, 0, 0 | 27 'x', 'y', 'z', 0, 0, 0, 0, 0 |
26 }; | 28 }; |
27 | 29 |
28 static char gsm7ext_decode_table[128] = { | 30 static char escape_table[128] = { |
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
30 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, |
31 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, |
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '[', '~', ']', 0, | 34 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, 0, 0, | 35 '|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
34 0, 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, |
35 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, |
36 0, 0, 0, 0, 0, 0, 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 |
37 }; | 39 }; |
38 | 40 |
39 void | 41 void |
40 print_gsm7_string_to_file(data, nbytes, outf) | 42 print_gsm7_string_to_file(data, nbytes, outf) |
41 u_char *data; | 43 u_char *data; |
56 putc('\\', outf); | 58 putc('\\', outf); |
57 putc('e', outf); | 59 putc('e', outf); |
58 continue; | 60 continue; |
59 } | 61 } |
60 b = *dp++; | 62 b = *dp++; |
61 c = gsm7ext_decode_table[b]; | 63 c = escape_table[b]; |
62 if (!c) { | 64 if (!c) { |
63 fprintf(outf, "\\e\\%02X", b); | 65 fprintf(outf, "\\e\\%02X", b); |
64 continue; | 66 continue; |
65 } | 67 } |
66 } else { | 68 } else { |
67 c = gsm7_decode_table[b]; | 69 c = basic_table[b]; |
68 if (!c) { | 70 if (!c) { |
69 fprintf(outf, "\\%02X", b); | 71 fprintf(outf, "\\%02X", b); |
70 continue; | 72 continue; |
71 } | 73 } |
72 } | 74 } |
73 if (c == '\n') { | 75 if (c & 0x80) { |
74 putc('\\', outf); | 76 putc('\\', outf); |
75 putc('n', outf); | 77 c &= 0x7F; |
76 continue; | |
77 } | 78 } |
78 if (c == '\r') { | |
79 putc('\\', outf); | |
80 putc('r', outf); | |
81 continue; | |
82 } | |
83 if (c == '"' || c == '\\') | |
84 putc('\\', outf); | |
85 putc(c, outf); | 79 putc(c, outf); |
86 } | 80 } |
87 putc('"', outf); | 81 putc('"', outf); |
88 } | 82 } |