FreeCalypso > hg > fc-pcsc-tools
annotate libutil/gsm7_encode.c @ 157:f064dbcc5f41
libutil split from libcommon
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 26 Feb 2021 20:19:58 +0000 |
parents | libcommon/gsm7_encode.c@90e7020df08a |
children |
rev | line source |
---|---|
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
17
b8d27c72747a
GSM7 encoding function factored out of pb-update-imm
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
2 * This module implements functions for parsing quoted string |
b8d27c72747a
GSM7 encoding function factored out of pb-update-imm
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
3 * arguments intended for GSM7 string encoding, and actually |
b8d27c72747a
GSM7 encoding function factored out of pb-update-imm
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
4 * encoding them into GSM7 binary strings. |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 */ |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/types.h> |
17
b8d27c72747a
GSM7 encoding function factored out of pb-update-imm
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
8 #include <ctype.h> |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
12
8a34f5b7c812
gsm7_encode_table[] factored out into libcommon
Mychaela Falconia <falcon@freecalypso.org>
parents:
11
diff
changeset
|
11 extern u_char gsm7_encode_table[256]; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
17
b8d27c72747a
GSM7 encoding function factored out of pb-update-imm
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
13 qstring_arg_to_gsm7(arg, record, maxlen) |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char *arg; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 u_char *record; |
10
a76ec3e7da09
simtool/pbupdate.c: a little refactoring from fc-uicc-tool
Mychaela Falconia <falcon@freecalypso.org>
parents:
1
diff
changeset
|
16 unsigned maxlen; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 { |
10
a76ec3e7da09
simtool/pbupdate.c: a little refactoring from fc-uicc-tool
Mychaela Falconia <falcon@freecalypso.org>
parents:
1
diff
changeset
|
18 unsigned acclen, nadd; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 char *cp; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 int c; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 cp = arg; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 for (acclen = 0; *cp; ) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 c = *cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (c == '\\') { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 if (*cp == '\0') { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 fprintf(stderr, |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 "error: dangling backslash escape\n"); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 c = *cp++; |
20
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
32 if (c >= '0' && c <= '7' && isxdigit(*cp)) { |
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
33 c = ((c - '0') << 4) | decode_hex_digit(*cp++); |
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
34 goto bypass_encoding; |
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
35 } |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 switch (c) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 case 'n': |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 c = '\n'; |
20
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
39 goto bypass_encoding; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 case 'r': |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 c = '\r'; |
20
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
42 goto bypass_encoding; |
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
43 case 'e': |
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
44 c = 0x1B; |
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
45 goto bypass_encoding; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 case '"': |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 case '\\': |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 break; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 default: |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 fprintf(stderr, |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 "error: non-understood backslash escape\n"); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 c = gsm7_encode_table[c]; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 if (c == 0xFF) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 fprintf(stderr, |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 "error: character in alpha tag string cannot be encoded in GSM7\n"); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
20
90e7020df08a
GSM7 string parsers accept new bypass-encoding escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
17
diff
changeset
|
61 bypass_encoding: |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (c & 0x80) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 nadd = 2; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 else |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 nadd = 1; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (acclen + nadd > maxlen) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 fprintf(stderr, |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 "error: alpha tag string is longer than SIM limit\n"); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (c & 0x80) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 record[acclen++] = 0x1B; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 record[acclen++] = c & 0x7F; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 } |
17
b8d27c72747a
GSM7 encoding function factored out of pb-update-imm
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
75 return(acclen); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 } |