annotate libutil/alpha_fromfile.c @ 22:f893cdde97a4

updated top-level README added
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 08:20:20 +0000
parents 34bbb0585cab
children 4c475732660b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements functions for parsing alpha tag strings
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * from input data files, to be used by commands like pb-update
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * and smsp-restore.
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <ctype.h>
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 extern u_char gsm7_encode_table[256];
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 char *
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 alpha_from_file_qstring(cp, record, maxlen, filename_for_errs, lineno_for_errs)
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 char *cp, *filename_for_errs;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 u_char *record;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 unsigned maxlen;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 unsigned acclen, nadd;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 int c;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 for (acclen = 0; ; ) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 if (*cp == '\0') {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 unterm_qstring: fprintf(stderr,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 "%s line %d: unterminated quoted string\n",
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 filename_for_errs, lineno_for_errs);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 return(0);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (*cp == '"')
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 break;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 c = *cp++;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (c == '\\') {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (*cp == '\0')
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 goto unterm_qstring;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 c = *cp++;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (c >= '0' && c <= '7' && isxdigit(*cp)) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 c = ((c - '0') << 4) | decode_hex_digit(*cp++);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 goto bypass_encoding;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 switch (c) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 case 'n':
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 c = '\n';
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 goto bypass_encoding;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 case 'r':
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 c = '\r';
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 goto bypass_encoding;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 case 'e':
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 c = 0x1B;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 goto bypass_encoding;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 case '"':
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 case '\\':
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 break;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 default:
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 fprintf(stderr,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 "%s line %d: non-understood backslash escape\n",
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 filename_for_errs, lineno_for_errs);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 return(0);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 c = gsm7_encode_table[c];
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (c == 0xFF) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 fprintf(stderr,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 "%s line %d: character in quoted string cannot be encoded in GSM7\n",
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 filename_for_errs, lineno_for_errs);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return(0);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 bypass_encoding:
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (c & 0x80)
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 nadd = 2;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 else
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 nadd = 1;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (acclen + nadd > maxlen) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 fprintf(stderr,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 "%s line %d: alpha tag string is longer than SIM limit\n",
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 filename_for_errs, lineno_for_errs);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 return(0);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 if (c & 0x80)
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 record[acclen++] = 0x1B;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 record[acclen++] = c & 0x7F;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 return(cp + 1);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 char *
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 alpha_from_file_hex(cp, record, maxlen, filename_for_errs, lineno_for_errs)
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 char *cp, *filename_for_errs;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 u_char *record;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 unsigned maxlen;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 unsigned acclen;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 for (acclen = 0; ; ) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 break;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 if (acclen >= maxlen) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 fprintf(stderr,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 "%s line %d: alpha tag string is longer than SIM limit\n",
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 filename_for_errs, lineno_for_errs);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 return(0);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 record[acclen++] = (decode_hex_digit(cp[0]) << 4) |
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 decode_hex_digit(cp[1]);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 cp += 2;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 return(cp);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 }