FreeCalypso > hg > fc-sim-tools
comparison libutil/gsm7_encode.c @ 8:34bbb0585cab
libutil: import from previous fc-pcsc-tools version
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 14 Mar 2021 05:42:37 +0000 |
parents | |
children | 1b905e730abd |
comparison
equal
deleted
inserted
replaced
7:b25d4dfe5798 | 8:34bbb0585cab |
---|---|
1 /* | |
2 * This module implements functions for parsing quoted string | |
3 * arguments intended for GSM7 string encoding, and actually | |
4 * encoding them into GSM7 binary strings. | |
5 */ | |
6 | |
7 #include <sys/types.h> | |
8 #include <ctype.h> | |
9 #include <stdio.h> | |
10 | |
11 extern u_char gsm7_encode_table[256]; | |
12 | |
13 qstring_arg_to_gsm7(arg, record, maxlen) | |
14 char *arg; | |
15 u_char *record; | |
16 unsigned maxlen; | |
17 { | |
18 unsigned acclen, nadd; | |
19 char *cp; | |
20 int c; | |
21 | |
22 cp = arg; | |
23 for (acclen = 0; *cp; ) { | |
24 c = *cp++; | |
25 if (c == '\\') { | |
26 if (*cp == '\0') { | |
27 fprintf(stderr, | |
28 "error: dangling backslash escape\n"); | |
29 return(-1); | |
30 } | |
31 c = *cp++; | |
32 if (c >= '0' && c <= '7' && isxdigit(*cp)) { | |
33 c = ((c - '0') << 4) | decode_hex_digit(*cp++); | |
34 goto bypass_encoding; | |
35 } | |
36 switch (c) { | |
37 case 'n': | |
38 c = '\n'; | |
39 goto bypass_encoding; | |
40 case 'r': | |
41 c = '\r'; | |
42 goto bypass_encoding; | |
43 case 'e': | |
44 c = 0x1B; | |
45 goto bypass_encoding; | |
46 case '"': | |
47 case '\\': | |
48 break; | |
49 default: | |
50 fprintf(stderr, | |
51 "error: non-understood backslash escape\n"); | |
52 return(-1); | |
53 } | |
54 } | |
55 c = gsm7_encode_table[c]; | |
56 if (c == 0xFF) { | |
57 fprintf(stderr, | |
58 "error: character in alpha tag string cannot be encoded in GSM7\n"); | |
59 return(-1); | |
60 } | |
61 bypass_encoding: | |
62 if (c & 0x80) | |
63 nadd = 2; | |
64 else | |
65 nadd = 1; | |
66 if (acclen + nadd > maxlen) { | |
67 fprintf(stderr, | |
68 "error: alpha tag string is longer than SIM limit\n"); | |
69 return(-1); | |
70 } | |
71 if (c & 0x80) | |
72 record[acclen++] = 0x1B; | |
73 record[acclen++] = c & 0x7F; | |
74 } | |
75 return(acclen); | |
76 } |