Path: blob/master/src/java.instrument/share/native/libinstrument/PathCharsValidator.c
41149 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <string.h>27#include "jni.h"2829#ifndef max30#define max(a,b) ( (a>b) ? a : b )31#endif32#ifndef min33#define min(a,b) ( (a<b) ? a : b )34#endif3536/*37* Validates that a URI path component does not contain any illegal characters38* - ported from src/share/classes/java/net/URI.java39*/4041static jlong L_HEX;42static jlong H_HEX;43static jlong L_PATH;44static jlong H_PATH;4546/* Compute the low-order mask for the characters in the given string */47static jlong lowMask(char* s) {48size_t n = strlen(s);49jlong m = 0;50size_t i;51for (i = 0; i < n; i++) {52int c = (int)s[i];53if (c < 64)54m |= ((jlong)1 << c);55}56return m;57}5859/* Compute the high-order mask for the characters in the given string */60static jlong highMask(char* s) {61size_t n = strlen(s);62jlong m = 0;63size_t i;64for (i = 0; i < n; i++) {65int c = (int)s[i];66if ((c >= 64) && (c < 128))67m |= ((jlong)1 << (c - 64));68}69return m;70}7172/*73* Compute a low-order mask for the characters74* between first and last, inclusive75*/76static jlong lowMaskRange(char first, char last) {77jlong m = 0;78int f = max(min(first, 63), 0);79int l = max(min(last, 63), 0);80int i;8182for (i = f; i <= l; i++) {83m |= (jlong)1 << i;84}85return m;86}8788/*89* Compute a high-order mask for the characters90* between first and last, inclusive91*/92static jlong highMaskRange(char first, char last) {93jlong m = 0;94int f = max(min(first, 127), 64) - 64;95int l = max(min(last, 127), 64) - 64;96int i;97for (i = f; i <= l; i++) {98m |= (jlong)1 << i;99}100return m;101}102103/*104* Tell whether the given character is permitted by the given mask pair105*/106static int match(int c, jlong lowMask, jlong highMask) {107if (c >= 0 && c < 64)108if ((((jlong)1 << c) & lowMask) != 0) return 1;109if (c >= 64 && c < 128)110if ((((jlong)1 << (c - 64)) & highMask) != 0) return 1;111return 0;112}113114static void initialize() {115// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |116// "8" | "9"117jlong L_DIGIT = lowMaskRange('0', '9');118jlong H_DIGIT = 0;119120// upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |121// "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |122// "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"123jlong L_UPALPHA = 0;124jlong H_UPALPHA = highMaskRange('A', 'Z');125126// lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |127// "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |128// "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"129jlong L_LOWALPHA = 0;130jlong H_LOWALPHA = highMaskRange('a', 'z');131132// alpha = lowalpha | upalpha133jlong L_ALPHA = L_LOWALPHA | L_UPALPHA;134jlong H_ALPHA = H_LOWALPHA | H_UPALPHA;135136// alphanum = alpha | digit137jlong L_ALPHANUM = L_DIGIT | L_ALPHA;138jlong H_ALPHANUM = H_DIGIT | H_ALPHA;139140// mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |141// "(" | ")"142jlong L_MARK = lowMask("-_.!~*'()");143jlong H_MARK = highMask("-_.!~*'()");144145// unreserved = alphanum | mark146jlong L_UNRESERVED = L_ALPHANUM | L_MARK;147jlong H_UNRESERVED = H_ALPHANUM | H_MARK;148149// pchar = unreserved |150// ":" | "@" | "&" | "=" | "+" | "$" | ","151jlong L_PCHAR = L_UNRESERVED | lowMask(":@&=+$,");152jlong H_PCHAR = H_UNRESERVED | highMask(":@&=+$,");153154// hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |155// "a" | "b" | "c" | "d" | "e" | "f"156L_HEX = L_DIGIT;157H_HEX = highMaskRange('A', 'F') | highMaskRange('a', 'f');158159// All valid path characters160L_PATH = L_PCHAR | lowMask(";/");161H_PATH = H_PCHAR | highMask(";/");162}163164165/*166* Validates that the given URI path component does not contain any167* illegal characters. Returns 0 if only validate characters are present.168*/169int validatePathChars(const char* path) {170size_t i, n;171172/* initialize on first usage */173if (L_HEX == 0) {174initialize();175}176177i=0;178n = strlen(path);179while (i < n) {180int c = (int)(signed char)path[i];181182/* definitely not us-ascii */183if (c < 0) return -1;184185/* start of an escapted character */186if (c == '%') {187if (i + 3 <= n) {188int h1 = (int)(signed char)path[i+1];189int h2 = (int)(signed char)path[i+2];190if (h1 < 0 || h2 < 0) return -1;191if (!match(h1, L_HEX, H_HEX)) return -1;192if (!match(h2, L_HEX, H_HEX)) return -1;193i += 3;194} else {195/* malformed escape pair */196return -1;197}198} else {199if (!match(c, L_PATH, H_PATH)) return -1;200i++;201}202}203204return 0;205}206207208