FreeCalypso > hg > fc-pcsc-tools
comparison libcommon/gsm7_encode.c @ 17:b8d27c72747a
GSM7 encoding function factored out of pb-update-imm
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 12 Feb 2021 02:54:17 +0000 |
parents | simtool/pbupd_imm.c@d4f8c511affe |
children | 90e7020df08a |
comparison
equal
deleted
inserted
replaced
16:a6ca422323b9 | 17:b8d27c72747a |
---|---|
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 switch (c) { | |
33 case 'n': | |
34 c = '\n'; | |
35 break; | |
36 case 'r': | |
37 c = '\r'; | |
38 break; | |
39 case '"': | |
40 case '\\': | |
41 break; | |
42 default: | |
43 fprintf(stderr, | |
44 "error: non-understood backslash escape\n"); | |
45 return(-1); | |
46 } | |
47 } | |
48 c = gsm7_encode_table[c]; | |
49 if (c == 0xFF) { | |
50 fprintf(stderr, | |
51 "error: character in alpha tag string cannot be encoded in GSM7\n"); | |
52 return(-1); | |
53 } | |
54 if (c & 0x80) | |
55 nadd = 2; | |
56 else | |
57 nadd = 1; | |
58 if (acclen + nadd > maxlen) { | |
59 fprintf(stderr, | |
60 "error: alpha tag string is longer than SIM limit\n"); | |
61 return(-1); | |
62 } | |
63 if (c & 0x80) | |
64 record[acclen++] = 0x1B; | |
65 record[acclen++] = c & 0x7F; | |
66 } | |
67 return(acclen); | |
68 } |