0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <stdio.h>
0N/A#include <string.h>
0N/A#include "jni.h"
0N/A
0N/A#ifndef max
0N/A#define max(a,b) ( (a>b) ? a : b )
0N/A#endif
0N/A#ifndef min
0N/A#define min(a,b) ( (a<b) ? a : b )
0N/A#endif
0N/A
0N/A/*
0N/A * Validates that a URI path component does not contain any illegal characters
0N/A * - ported from src/share/classes/java/net/URI.java
0N/A */
0N/A
0N/Astatic jlong L_HEX;
0N/Astatic jlong H_HEX;
0N/Astatic jlong L_PATH;
0N/Astatic jlong H_PATH;
0N/A
0N/A/* Compute the low-order mask for the characters in the given string */
0N/Astatic jlong lowMask(char* s) {
0N/A int n = strlen(s);
0N/A jlong m = 0;
0N/A int i;
0N/A for (i = 0; i < n; i++) {
0N/A int c = (int)s[i];
0N/A if (c < 64)
0N/A m |= ((jlong)1 << c);
0N/A }
0N/A return m;
0N/A}
0N/A
0N/A/* Compute the high-order mask for the characters in the given string */
0N/Astatic jlong highMask(char* s) {
0N/A int n = strlen(s);
0N/A jlong m = 0;
0N/A int i;
0N/A for (i = 0; i < n; i++) {
0N/A int c = (int)s[i];
0N/A if ((c >= 64) && (c < 128))
0N/A m |= ((jlong)1 << (c - 64));
0N/A }
0N/A return m;
0N/A}
0N/A
0N/A/*
0N/A * Compute a low-order mask for the characters
0N/A * between first and last, inclusive
0N/A */
0N/Astatic jlong lowMaskRange(char first, char last) {
0N/A jlong m = 0;
0N/A int f = max(min(first, 63), 0);
0N/A int l = max(min(last, 63), 0);
0N/A int i;
0N/A
0N/A for (i = f; i <= l; i++) {
0N/A m |= (jlong)1 << i;
0N/A }
0N/A return m;
0N/A}
0N/A
0N/A/*
0N/A * Compute a high-order mask for the characters
0N/A * between first and last, inclusive
0N/A */
0N/Astatic jlong highMaskRange(char first, char last) {
0N/A jlong m = 0;
0N/A int f = max(min(first, 127), 64) - 64;
0N/A int l = max(min(last, 127), 64) - 64;
0N/A int i;
0N/A for (i = f; i <= l; i++) {
0N/A m |= (jlong)1 << i;
0N/A }
0N/A return m;
0N/A}
0N/A
0N/A/*
0N/A * Tell whether the given character is permitted by the given mask pair
0N/A */
0N/Astatic int match(int c, jlong lowMask, jlong highMask) {
0N/A if (c >= 0 && c < 64)
0N/A if ((((jlong)1 << c) & lowMask) != 0) return 1;
0N/A if (c >= 64 && c < 128)
0N/A if ((((jlong)1 << (c - 64)) & highMask) != 0) return 1;
0N/A return 0;
0N/A}
0N/A
0N/Astatic void initialize() {
0N/A // digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
0N/A // "8" | "9"
0N/A jlong L_DIGIT = lowMaskRange('0', '9');
0N/A jlong H_DIGIT = 0;
0N/A
0N/A // upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
0N/A // "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
0N/A // "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
0N/A jlong L_UPALPHA = 0;
0N/A jlong H_UPALPHA = highMaskRange('A', 'Z');
0N/A
0N/A // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
0N/A // "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
0N/A // "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
0N/A jlong L_LOWALPHA = 0;
0N/A jlong H_LOWALPHA = highMaskRange('a', 'z');
0N/A
0N/A // alpha = lowalpha | upalpha
0N/A jlong L_ALPHA = L_LOWALPHA | L_UPALPHA;
0N/A jlong H_ALPHA = H_LOWALPHA | H_UPALPHA;
0N/A
0N/A // alphanum = alpha | digit
0N/A jlong L_ALPHANUM = L_DIGIT | L_ALPHA;
0N/A jlong H_ALPHANUM = H_DIGIT | H_ALPHA;
0N/A
0N/A // mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
0N/A // "(" | ")"
0N/A jlong L_MARK = lowMask("-_.!~*'()");
0N/A jlong H_MARK = highMask("-_.!~*'()");
0N/A
0N/A // unreserved = alphanum | mark
0N/A jlong L_UNRESERVED = L_ALPHANUM | L_MARK;
0N/A jlong H_UNRESERVED = H_ALPHANUM | H_MARK;
0N/A
0N/A // pchar = unreserved |
0N/A // ":" | "@" | "&" | "=" | "+" | "$" | ","
0N/A jlong L_PCHAR = L_UNRESERVED | lowMask(":@&=+$,");
0N/A jlong H_PCHAR = H_UNRESERVED | highMask(":@&=+$,");
0N/A
0N/A // hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
0N/A // "a" | "b" | "c" | "d" | "e" | "f"
0N/A L_HEX = L_DIGIT;
0N/A H_HEX = highMaskRange('A', 'F') | highMaskRange('a', 'f');
0N/A
0N/A // All valid path characters
0N/A L_PATH = L_PCHAR | lowMask(";/");
0N/A H_PATH = H_PCHAR | highMask(";/");
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Validates that the given URI path component does not contain any
0N/A * illegal characters. Returns 0 if only validate characters are present.
0N/A */
0N/Aint validatePathChars(const char* path) {
0N/A int i, n;
0N/A
0N/A /* initialize on first usage */
0N/A if (L_HEX == 0) {
0N/A initialize();
0N/A }
0N/A
0N/A i=0;
0N/A n = strlen(path);
0N/A while (i < n) {
0N/A int c = (int)(signed char)path[i];
0N/A
0N/A /* definitely not us-ascii */
0N/A if (c < 0) return -1;
0N/A
0N/A /* start of an escapted character */
0N/A if (c == '%') {
0N/A if (i + 3 <= n) {
0N/A int h1 = (int)(signed char)path[i+1];
0N/A int h2 = (int)(signed char)path[i+2];
0N/A if (h1 < 0 || h2 < 0) return -1;
0N/A if (!match(h1, L_HEX, H_HEX)) return -1;
0N/A if (!match(h2, L_HEX, H_HEX)) return -1;
0N/A i += 3;
0N/A } else {
0N/A /* malformed escape pair */
0N/A return -1;
0N/A }
0N/A } else {
0N/A if (!match(c, L_PATH, H_PATH)) return -1;
0N/A i++;
0N/A }
0N/A }
0N/A
0N/A return 0;
0N/A}