comparison libcommon/alpha_fromfile.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 simtool/pbupd_file.c@d4f8c511affe
children 90e7020df08a
comparison
equal deleted inserted replaced
17:b8d27c72747a 18:2ef261371347
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 switch (c) {
37 case 'n':
38 c = '\n';
39 break;
40 case 'r':
41 c = '\r';
42 break;
43 case '"':
44 case '\\':
45 break;
46 default:
47 fprintf(stderr,
48 "%s line %d: non-understood backslash escape\n",
49 filename_for_errs, lineno_for_errs);
50 return(0);
51 }
52 }
53 c = gsm7_encode_table[c];
54 if (c == 0xFF) {
55 fprintf(stderr,
56 "%s line %d: character in quoted string cannot be encoded in GSM7\n",
57 filename_for_errs, lineno_for_errs);
58 return(0);
59 }
60 if (c & 0x80)
61 nadd = 2;
62 else
63 nadd = 1;
64 if (acclen + nadd > maxlen) {
65 fprintf(stderr,
66 "%s line %d: alpha tag string is longer than SIM limit\n",
67 filename_for_errs, lineno_for_errs);
68 return(0);
69 }
70 if (c & 0x80)
71 record[acclen++] = 0x1B;
72 record[acclen++] = c & 0x7F;
73 }
74 return(cp + 1);
75 }
76
77 char *
78 alpha_from_file_hex(cp, record, maxlen, filename_for_errs, lineno_for_errs)
79 char *cp, *filename_for_errs;
80 u_char *record;
81 unsigned maxlen;
82 {
83 unsigned acclen;
84
85 for (acclen = 0; ; ) {
86 if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
87 break;
88 if (acclen >= maxlen) {
89 fprintf(stderr,
90 "%s line %d: alpha tag string is longer than SIM limit\n",
91 filename_for_errs, lineno_for_errs);
92 return(0);
93 }
94 record[acclen++] = (decode_hex_digit(cp[0]) << 4) |
95 decode_hex_digit(cp[1]);
96 cp += 2;
97 }
98 return(cp);
99 }