1 /*
2 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include <assert.h>
27 #include <limits.h>
28 #include <setjmp.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "jni.h"
33 #include "jvm.h"
34
35 typedef unsigned short unicode;
36
37 static char *
38 skip_over_fieldname(char *name, jboolean slash_okay,
39 unsigned int len);
40 static char *
41 skip_over_field_signature(char *name, jboolean void_okay,
42 unsigned int len);
43
44 /*
45 * Return non-zero if the character is a valid in JVM class name, zero
46 * otherwise. The only characters currently disallowed from JVM class
47 * names are given in the table below:
48 *
49 * Character Hex Decimal
50 * '.' 0x2e 46
51 * '/' 0x2f 47
52 * ';' 0x3b 59
53 * '[' 0x5b 91
206 case JVM_SIGNATURE_ARRAY:
207 array_dim++;
208 /* JVMS 2nd ed. 4.10 */
209 /* The number of dimensions in an array is limited to 255 ... */
210 if (array_dim > 255) {
211 return 0;
212 }
213 /* The rest of what's there better be a legal signature. */
214 name++;
215 length--;
216 void_okay = JNI_FALSE;
217 break;
218
219 default:
220 return 0;
221 }
222 }
223 return 0;
224 }
225
226
227 /* Used in java/lang/Class.c */
228 /* Determine if the specified name is legal
229 * UTF name for a classname.
230 *
231 * Note that this routine expects the internal form of qualified classes:
232 * the dots should have been replaced by slashes.
233 */
234 JNIEXPORT jboolean
235 VerifyClassname(char *name, jboolean allowArrayClass)
236 {
237 size_t s = strlen(name);
238 assert(s <= UINT_MAX);
239 unsigned int length = (unsigned int)s;
240 char *p;
241
242 if (length > 0 && name[0] == JVM_SIGNATURE_ARRAY) {
243 if (!allowArrayClass) {
244 return JNI_FALSE;
245 } else {
246 /* Everything that's left better be a field signature */
247 p = skip_over_field_signature(name, JNI_FALSE, length);
248 }
249 } else {
250 /* skip over the fieldname. Slashes are okay */
251 p = skip_over_fieldname(name, JNI_TRUE, length);
252 }
253 return (p != 0 && p - name == (ptrdiff_t)length);
254 }
255
256 /*
257 * Translates '.' to '/'. Returns JNI_TRUE is any / were present.
258 */
259 JNIEXPORT jboolean
260 VerifyFixClassname(char *name)
261 {
262 char *p = name;
263 jboolean slashesFound = JNI_FALSE;
264 int valid = 1;
265
266 while (valid != 0 && *p != '\0') {
267 if (*p == '/') {
268 slashesFound = JNI_TRUE;
269 p++;
270 } else if (*p == '.') {
271 *p++ = '/';
272 } else {
273 next_utf2unicode(&p, &valid);
274 }
275 }
276
277 return slashesFound && valid != 0;
278 }
|
1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include <assert.h>
27 #include <limits.h>
28 #include <setjmp.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "jni.h"
33 #include "jvm.h"
34 #include "check_classname.h"
35
36 typedef unsigned short unicode;
37
38 static char *
39 skip_over_fieldname(char *name, jboolean slash_okay,
40 unsigned int len);
41 static char *
42 skip_over_field_signature(char *name, jboolean void_okay,
43 unsigned int len);
44
45 /*
46 * Return non-zero if the character is a valid in JVM class name, zero
47 * otherwise. The only characters currently disallowed from JVM class
48 * names are given in the table below:
49 *
50 * Character Hex Decimal
51 * '.' 0x2e 46
52 * '/' 0x2f 47
53 * ';' 0x3b 59
54 * '[' 0x5b 91
207 case JVM_SIGNATURE_ARRAY:
208 array_dim++;
209 /* JVMS 2nd ed. 4.10 */
210 /* The number of dimensions in an array is limited to 255 ... */
211 if (array_dim > 255) {
212 return 0;
213 }
214 /* The rest of what's there better be a legal signature. */
215 name++;
216 length--;
217 void_okay = JNI_FALSE;
218 break;
219
220 default:
221 return 0;
222 }
223 }
224 return 0;
225 }
226
227 /* Determine if the specified name is legal
228 * UTF name for a classname.
229 *
230 * Note that this routine expects the internal form of qualified classes:
231 * the dots should have been replaced by slashes.
232 */
233 jboolean verifyClassname(char *name, jboolean allowArrayClass)
234 {
235 size_t s = strlen(name);
236 assert(s <= UINT_MAX);
237 unsigned int length = (unsigned int)s;
238 char *p;
239
240 if (length > 0 && name[0] == JVM_SIGNATURE_ARRAY) {
241 if (!allowArrayClass) {
242 return JNI_FALSE;
243 } else {
244 /* Everything that's left better be a field signature */
245 p = skip_over_field_signature(name, JNI_FALSE, length);
246 }
247 } else {
248 /* skip over the fieldname. Slashes are okay */
249 p = skip_over_fieldname(name, JNI_TRUE, length);
250 }
251 return (p != 0 && p - name == (ptrdiff_t)length);
252 }
253
254 /*
255 * Translates '.' to '/'. Returns JNI_TRUE if any / were present.
256 */
257 jboolean verifyFixClassname(char *name)
258 {
259 char *p = name;
260 jboolean slashesFound = JNI_FALSE;
261 int valid = 1;
262
263 while (valid != 0 && *p != '\0') {
264 if (*p == '/') {
265 slashesFound = JNI_TRUE;
266 p++;
267 } else if (*p == '.') {
268 *p++ = '/';
269 } else {
270 next_utf2unicode(&p, &valid);
271 }
272 }
273
274 return slashesFound && valid != 0;
275 }
276
277 /*
278 * Translates '.' to '/'.
279 */
280 void fixClassname(char *name)
281 {
282 char *p = name;
283 int valid = 1;
284
285 while (valid != 0 && *p != '\0') {
286 if (*p == '.') {
287 *p++ = '/';
288 } else {
289 next_utf2unicode(&p, &valid);
290 }
291 }
292 }
|