comparison libutil/alpha_valid.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
comparison
equal deleted inserted replaced
7:b25d4dfe5798 8:34bbb0585cab
1 /*
2 * This module contains functions for validating alpha fields
3 * that exist in various SIM files.
4 */
5
6 #include <sys/types.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 static
13 validate_classic_gsm(data, nbytes, textlenp)
14 u_char *data;
15 unsigned nbytes, *textlenp;
16 {
17 u_char *dp;
18 unsigned n;
19
20 dp = data;
21 for (n = 0; n < nbytes; n++) {
22 if (*dp == 0xFF)
23 break;
24 if (*dp & 0x80)
25 return(-1);
26 dp++;
27 }
28 if (textlenp)
29 *textlenp = n;
30 for (; n < nbytes; n++)
31 if (*dp++ != 0xFF)
32 return(-1);
33 return(0);
34 }
35
36 static
37 validate_ucs2_80(data, nbytes, textlenp)
38 u_char *data;
39 unsigned nbytes, *textlenp;
40 {
41 u_char *dp, *endp;
42
43 if (nbytes < 3)
44 return(-1);
45 dp = data + 1;
46 endp = data + nbytes;
47 while (dp < endp) {
48 if (dp + 1 == endp) {
49 if (*dp != 0xFF)
50 return(-1);
51 if (textlenp)
52 *textlenp = dp - data;
53 return(0);
54 }
55 if (dp[0] == 0xFF && dp[1] == 0xFF)
56 break;
57 dp += 2;
58 }
59 if (textlenp)
60 *textlenp = dp - data;
61 while (dp < endp)
62 if (*dp++ != 0xFF)
63 return(-1);
64 return(0);
65 }
66
67 static
68 validate_ucs2_81(data, nbytes, textlenp)
69 u_char *data;
70 unsigned nbytes, *textlenp;
71 {
72 u_char *dp, *endp;
73 unsigned textlen;
74
75 if (nbytes < 4)
76 return(-1);
77 if (!data[1])
78 return(-1);
79 textlen = data[1] + 3;
80 if (textlen > nbytes)
81 return(-1);
82 if (textlenp)
83 *textlenp = textlen;
84 dp = data + textlen;
85 endp = data + nbytes;
86 while (dp < endp)
87 if (*dp++ != 0xFF)
88 return(-1);
89 return(0);
90 }
91
92 static
93 validate_ucs2_82(data, nbytes, textlenp)
94 u_char *data;
95 unsigned nbytes, *textlenp;
96 {
97 u_char *dp, *endp;
98 unsigned textlen;
99
100 if (nbytes < 5)
101 return(-1);
102 if (!data[1])
103 return(-1);
104 textlen = data[1] + 4;
105 if (textlen > nbytes)
106 return(-1);
107 if (textlenp)
108 *textlenp = textlen;
109 dp = data + textlen;
110 endp = data + nbytes;
111 while (dp < endp)
112 if (*dp++ != 0xFF)
113 return(-1);
114 return(0);
115 }
116
117 static
118 validate_empty(data, nbytes, textlenp)
119 u_char *data;
120 unsigned nbytes, *textlenp;
121 {
122 u_char *dp;
123 unsigned n;
124
125 dp = data;
126 for (n = 0; n < nbytes; n++)
127 if (*dp++ != 0xFF)
128 return(-1);
129 if (textlenp)
130 *textlenp = 0;
131 return(0);
132 }
133
134 validate_alpha_field(data, nbytes, textlenp)
135 u_char *data;
136 unsigned nbytes, *textlenp;
137 {
138 if (data[0] < 0x80)
139 return validate_classic_gsm(data, nbytes, textlenp);
140 switch (data[0]) {
141 case 0x80:
142 return validate_ucs2_80(data, nbytes, textlenp);
143 case 0x81:
144 return validate_ucs2_81(data, nbytes, textlenp);
145 case 0x82:
146 return validate_ucs2_82(data, nbytes, textlenp);
147 case 0xFF:
148 return validate_empty(data, nbytes, textlenp);
149 default:
150 return -1;
151 }
152 }