- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathcheck_classname.c
292 lines (268 loc) · 8.68 KB
/
check_classname.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include<assert.h>
#include<limits.h>
#include<setjmp.h>
#include<stdlib.h>
#include<string.h>
#include"jni.h"
#include"jvm.h"
#include"check_classname.h"
typedefunsigned shortunicode;
staticchar*
skip_over_fieldname(char*name, jbooleanslash_okay,
unsigned intlen);
staticchar*
skip_over_field_signature(char*name, jbooleanvoid_okay,
unsigned intlen);
/*
* Return non-zero if the character is a valid in JVM class name, zero
* otherwise. The only characters currently disallowed from JVM class
* names are given in the table below:
*
* Character Hex Decimal
* '.' 0x2e 46
* '/' 0x2f 47
* ';' 0x3b 59
* '[' 0x5b 91
*
* (Method names have further restrictions dealing with the '<' and
* '>' characters.)
*/
staticintisJvmIdentifier(unicodech) {
if( ch>91||ch<46 )
return1; /* Lowercase ASCII letters are > 91 */
else { /* 46 <= ch <= 91 */
if (ch <= 90&&ch >= 60) {
return1; /* Uppercase ASCII recognized here */
} else { /* ch == 91 || 46 <= ch <= 59 */
if (ch==91||ch==59||ch <= 47)
return0;
else
return1;
}
}
}
staticunicode
next_utf2unicode(char**utfstring_ptr, int*valid)
{
unsigned char*ptr= (unsigned char*)(*utfstring_ptr);
unsigned charch, ch2, ch3;
intlength=1; /* default length */
unicoderesult=0x80; /* default bad result; */
*valid=1;
switch ((ch=ptr[0]) >> 4) {
default:
result=ch;
break;
case0x8: case0x9: case0xA: case0xB: case0xF:
/* Shouldn't happen. */
*valid=0;
break;
case0xC: case0xD:
/* 110xxxxx 10xxxxxx */
if (((ch2=ptr[1]) &0xC0) ==0x80) {
unsigned charhigh_five=ch&0x1F;
unsigned charlow_six=ch2&0x3F;
result= (high_five << 6) +low_six;
length=2;
}
break;
case0xE:
/* 1110xxxx 10xxxxxx 10xxxxxx */
if (((ch2=ptr[1]) &0xC0) ==0x80) {
if (((ch3=ptr[2]) &0xC0) ==0x80) {
unsigned charhigh_four=ch&0x0f;
unsigned charmid_six=ch2&0x3f;
unsigned charlow_six=ch3&0x3f;
result= (((high_four << 6) +mid_six) << 6) +low_six;
length=3;
} else {
length=2;
}
}
break;
} /* end of switch */
*utfstring_ptr= (char*)(ptr+length);
returnresult;
}
/* Take pointer to a string. Skip over the longest part of the string that
* could be taken as a fieldname. Allow '/' if slash_okay is JNI_TRUE.
*
* Return a pointer to just past the fieldname. Return NULL if no fieldname
* at all was found, or in the case of slash_okay being true, we saw
* consecutive slashes (meaning we were looking for a qualified path but
* found something that was badly-formed).
*/
staticchar*
skip_over_fieldname(char*name, jbooleanslash_okay,
unsigned intlength)
{
char*p;
unicodech;
unicodelast_ch=0;
intvalid=1;
/* last_ch == 0 implies we are looking at the first char. */
for (p=name; p!=name+length; last_ch=ch) {
char*old_p=p;
ch=*p;
if (ch<128) {
p++;
if (isJvmIdentifier(ch)) {
continue;
}
} else {
char*tmp_p=p;
ch=next_utf2unicode(&tmp_p, &valid);
if (valid==0)
return0;
p=tmp_p;
if (isJvmIdentifier(ch)) {
continue;
}
}
if (slash_okay&&ch=='/'&&last_ch) {
if (last_ch=='/') {
return0; /* Don't permit consecutive slashes */
}
} elseif (ch=='_'||ch=='$') {
} else {
returnlast_ch ? old_p : 0;
}
}
returnlast_ch ? p : 0;
}
/* Take pointer to a string. Skip over the longest part of the string that
* could be taken as a field signature. Allow "void" if void_okay.
*
* Return a pointer to just past the signature. Return NULL if no legal
* signature is found.
*/
staticchar*
skip_over_field_signature(char*name, jbooleanvoid_okay,
unsigned intlength)
{
unsigned intarray_dim=0;
for (;length>0;) {
switch (name[0]) {
caseJVM_SIGNATURE_VOID:
if (!void_okay) return0;
/* FALL THROUGH */
caseJVM_SIGNATURE_BOOLEAN:
caseJVM_SIGNATURE_BYTE:
caseJVM_SIGNATURE_CHAR:
caseJVM_SIGNATURE_SHORT:
caseJVM_SIGNATURE_INT:
caseJVM_SIGNATURE_FLOAT:
caseJVM_SIGNATURE_LONG:
caseJVM_SIGNATURE_DOUBLE:
returnname+1;
caseJVM_SIGNATURE_CLASS: {
/* Skip over the classname, if one is there. */
char*p=
skip_over_fieldname(name+1, JNI_TRUE, --length);
/* The next character better be a semicolon. */
if (p&&p-name-1>0&&p[0] ==';')
returnp+1;
return0;
}
caseJVM_SIGNATURE_ARRAY:
array_dim++;
/* JVMS 2nd ed. 4.10 */
/* The number of dimensions in an array is limited to 255 ... */
if (array_dim>255) {
return0;
}
/* The rest of what's there better be a legal signature. */
name++;
length--;
void_okay=JNI_FALSE;
break;
default:
return0;
}
}
return0;
}
/* Determine if the specified name is legal
* UTF name for a classname.
*
* Note that this routine expects the internal form of qualified classes:
* the dots should have been replaced by slashes.
*/
jbooleanverifyClassname(char*name, jbooleanallowArrayClass)
{
size_ts=strlen(name);
assert(s <= UINT_MAX);
unsigned intlength= (unsigned int)s;
char*p;
if (length>0&&name[0] ==JVM_SIGNATURE_ARRAY) {
if (!allowArrayClass) {
returnJNI_FALSE;
} else {
/* Everything that's left better be a field signature */
p=skip_over_field_signature(name, JNI_FALSE, length);
}
} else {
/* skip over the fieldname. Slashes are okay */
p=skip_over_fieldname(name, JNI_TRUE, length);
}
return (p!=0&&p-name== (ptrdiff_t)length);
}
/*
* Translates '.' to '/'. Returns JNI_TRUE if any / were present.
*/
jbooleanverifyFixClassname(char*name)
{
char*p=name;
jbooleanslashesFound=JNI_FALSE;
intvalid=1;
while (valid!=0&&*p!='\0') {
if (*p=='/') {
slashesFound=JNI_TRUE;
p++;
} elseif (*p=='.') {
*p++='/';
} else {
next_utf2unicode(&p, &valid);
}
}
returnslashesFound&&valid!=0;
}
/*
* Translates '.' to '/'.
*/
voidfixClassname(char*name)
{
char*p=name;
intvalid=1;
while (valid!=0&&*p!='\0') {
if (*p=='.') {
*p++='/';
} else {
next_utf2unicode(&p, &valid);
}
}
}