comparison libutil/alpha_fromfile.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 4c475732660b
comparison
equal deleted inserted replaced
7:b25d4dfe5798 8:34bbb0585cab
1 /*
2 * This module implements functions for parsing alpha tag strings
3 * from input data files, to be used by commands like pb-update
4 * and smsp-restore.
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 char *
14 alpha_from_file_qstring(cp, record, maxlen, filename_for_errs, lineno_for_errs)
15 char *cp, *filename_for_errs;
16 u_char *record;
17 unsigned maxlen;
18 {
19 unsigned acclen, nadd;
20 int c;
21
22 for (acclen = 0; ; ) {
23 if (*cp == '\0') {
24 unterm_qstring: fprintf(stderr,
25 "%s line %d: unterminated quoted string\n",
26 filename_for_errs, lineno_for_errs);
27 return(0);
28 }
29 if (*cp == '"')
30 break;
31 c = *cp++;
32 if (c == '\\') {
33 if (*cp == '\0')
34 goto unterm_qstring;
35 c = *cp++;
36 if (c >= '0' && c <= '7' && isxdigit(*cp)) {
37 c = ((c - '0') << 4) | decode_hex_digit(*cp++);
38 goto bypass_encoding;
39 }
40 switch (c) {
41 case 'n':
42 c = '\n';
43 goto bypass_encoding;
44 case 'r':
45 c = '\r';
46 goto bypass_encoding;
47 case 'e':
48 c = 0x1B;
49 goto bypass_encoding;
50 case '"':
51 case '\\':
52 break;
53 default:
54 fprintf(stderr,
55 "%s line %d: non-understood backslash escape\n",
56 filename_for_errs, lineno_for_errs);
57 return(0);
58 }
59 }
60 c = gsm7_encode_table[c];
61 if (c == 0xFF) {
62 fprintf(stderr,
63 "%s line %d: character in quoted string cannot be encoded in GSM7\n",
64 filename_for_errs, lineno_for_errs);
65 return(0);
66 }
67 bypass_encoding:
68 if (c & 0x80)
69 nadd = 2;
70 else
71 nadd = 1;
72 if (acclen + nadd > maxlen) {
73 fprintf(stderr,
74 "%s line %d: alpha tag string is longer than SIM limit\n",
75 filename_for_errs, lineno_for_errs);
76 return(0);
77 }
78 if (c & 0x80)
79 record[acclen++] = 0x1B;
80 record[acclen++] = c & 0x7F;
81 }
82 return(cp + 1);
83 }
84
85 char *
86 alpha_from_file_hex(cp, record, maxlen, filename_for_errs, lineno_for_errs)
87 char *cp, *filename_for_errs;
88 u_char *record;
89 unsigned maxlen;
90 {
91 unsigned acclen;
92
93 for (acclen = 0; ; ) {
94 if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
95 break;
96 if (acclen >= maxlen) {
97 fprintf(stderr,
98 "%s line %d: alpha tag string is longer than SIM limit\n",
99 filename_for_errs, lineno_for_errs);
100 return(0);
101 }
102 record[acclen++] = (decode_hex_digit(cp[0]) << 4) |
103 decode_hex_digit(cp[1]);
104 cp += 2;
105 }
106 return(cp);
107 }