FreeCalypso > hg > fc-pcsc-tools
comparison simtool/pbupd_file.c @ 18:2ef261371347
alpha tag from file parsing functions factored out of pb-update
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 12 Feb 2021 03:21:39 +0000 |
parents | d4f8c511affe |
children | 51167ee0151b |
comparison
equal
deleted
inserted
replaced
17:b8d27c72747a | 18:2ef261371347 |
---|---|
8 #include <strings.h> | 8 #include <strings.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
11 #include "curfile.h" | 11 #include "curfile.h" |
12 | 12 |
13 extern u_char gsm7_encode_table[256]; | 13 extern char *alpha_from_file_qstring(); |
14 | 14 extern char *alpha_from_file_hex(); |
15 static char * | |
16 decode_qstring_alpha(cp, record, maxlen, filename_for_errs, lineno_for_errs) | |
17 char *cp, *filename_for_errs; | |
18 u_char *record; | |
19 unsigned maxlen; | |
20 { | |
21 unsigned acclen, nadd; | |
22 int c; | |
23 | |
24 for (acclen = 0; ; ) { | |
25 if (*cp == '\0') { | |
26 unterm_qstring: fprintf(stderr, | |
27 "%s line %d: unterminated quoted string\n", | |
28 filename_for_errs, lineno_for_errs); | |
29 return(0); | |
30 } | |
31 if (*cp == '"') | |
32 break; | |
33 c = *cp++; | |
34 if (c == '\\') { | |
35 if (*cp == '\0') | |
36 goto unterm_qstring; | |
37 c = *cp++; | |
38 switch (c) { | |
39 case 'n': | |
40 c = '\n'; | |
41 break; | |
42 case 'r': | |
43 c = '\r'; | |
44 break; | |
45 case '"': | |
46 case '\\': | |
47 break; | |
48 default: | |
49 fprintf(stderr, | |
50 "%s line %d: non-understood backslash escape\n", | |
51 filename_for_errs, lineno_for_errs); | |
52 return(0); | |
53 } | |
54 } | |
55 c = gsm7_encode_table[c]; | |
56 if (c == 0xFF) { | |
57 fprintf(stderr, | |
58 "%s line %d: character in quoted string cannot be encoded in GSM7\n", | |
59 filename_for_errs, lineno_for_errs); | |
60 return(0); | |
61 } | |
62 if (c & 0x80) | |
63 nadd = 2; | |
64 else | |
65 nadd = 1; | |
66 if (acclen + nadd > maxlen) { | |
67 fprintf(stderr, | |
68 "%s line %d: alpha tag string is longer than SIM limit\n", | |
69 filename_for_errs, lineno_for_errs); | |
70 return(0); | |
71 } | |
72 if (c & 0x80) | |
73 record[acclen++] = 0x1B; | |
74 record[acclen++] = c & 0x7F; | |
75 } | |
76 return(cp + 1); | |
77 } | |
78 | |
79 static char * | |
80 decode_hex_alpha(cp, record, maxlen, filename_for_errs, lineno_for_errs) | |
81 char *cp, *filename_for_errs; | |
82 u_char *record; | |
83 unsigned maxlen; | |
84 { | |
85 unsigned acclen; | |
86 | |
87 for (acclen = 0; ; ) { | |
88 if (!isxdigit(cp[0]) || !isxdigit(cp[1])) | |
89 break; | |
90 if (acclen >= maxlen) { | |
91 fprintf(stderr, | |
92 "%s line %d: alpha tag string is longer than SIM limit\n", | |
93 filename_for_errs, lineno_for_errs); | |
94 return(0); | |
95 } | |
96 record[acclen++] = (decode_hex_digit(cp[0]) << 4) | | |
97 decode_hex_digit(cp[1]); | |
98 cp += 2; | |
99 } | |
100 return(cp); | |
101 } | |
102 | 15 |
103 static | 16 static |
104 process_record(line, pb_record_len, pb_record_count, filename_for_errs, | 17 process_record(line, pb_record_len, pb_record_count, filename_for_errs, |
105 lineno_for_errs) | 18 lineno_for_errs) |
106 char *line, *filename_for_errs; | 19 char *line, *filename_for_errs; |
175 while (isspace(*cp)) | 88 while (isspace(*cp)) |
176 cp++; | 89 cp++; |
177 } | 90 } |
178 if (*cp == '"') { | 91 if (*cp == '"') { |
179 cp++; | 92 cp++; |
180 cp = decode_qstring_alpha(cp, record, pb_record_len - 14, | 93 cp = alpha_from_file_qstring(cp, record, pb_record_len - 14, |
181 filename_for_errs, lineno_for_errs); | 94 filename_for_errs, |
95 lineno_for_errs); | |
182 if (!cp) | 96 if (!cp) |
183 return(-1); | 97 return(-1); |
184 } else if (!strncasecmp(cp, "HEX", 3)) { | 98 } else if (!strncasecmp(cp, "HEX", 3)) { |
185 cp += 3; | 99 cp += 3; |
186 while (isspace(*cp)) | 100 while (isspace(*cp)) |
187 cp++; | 101 cp++; |
188 cp = decode_hex_alpha(cp, record, pb_record_len - 14, | 102 cp = alpha_from_file_hex(cp, record, pb_record_len - 14, |
189 filename_for_errs, lineno_for_errs); | 103 filename_for_errs, lineno_for_errs); |
190 if (!cp) | 104 if (!cp) |
191 return(-1); | 105 return(-1); |
192 } else | 106 } else |
193 goto inv_syntax; | 107 goto inv_syntax; |
194 while (isspace(*cp)) | 108 while (isspace(*cp)) |