src/java.instrument/share/native/libinstrument/PathCharsValidator.c
author rwestberg
Fri, 01 Jun 2018 10:15:48 +0200
changeset 50336 1b6ea6bcd21a
parent 47216 71c04702a3d5
child 56230 489867818774
permissions -rw-r--r--
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test Reviewed-by: egahlin, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#ifndef max
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#define max(a,b) ( (a>b) ? a : b )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#ifndef min
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#define min(a,b) ( (a<b) ? a : b )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Validates that a URI path component does not contain any illegal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * - ported from src/share/classes/java/net/URI.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static jlong L_HEX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static jlong H_HEX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static jlong L_PATH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static jlong H_PATH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/* Compute the low-order mask for the characters in the given string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static jlong lowMask(char* s) {
23566
760a74c1b589 8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents: 5506
diff changeset
    49
    size_t n = strlen(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    jlong m = 0;
23566
760a74c1b589 8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents: 5506
diff changeset
    51
    size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    for (i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int c = (int)s[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (c < 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            m |= ((jlong)1 << c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/* Compute the high-order mask for the characters in the given string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
static jlong highMask(char* s) {
23566
760a74c1b589 8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents: 5506
diff changeset
    62
    size_t n = strlen(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    jlong m = 0;
23566
760a74c1b589 8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents: 5506
diff changeset
    64
    size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    for (i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int c = (int)s[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if ((c >= 64) && (c < 128))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            m |= ((jlong)1 << (c - 64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Compute a low-order mask for the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * between first and last, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static jlong lowMaskRange(char first, char last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    jlong m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    int f = max(min(first, 63), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int l = max(min(last, 63), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    for (i = f; i <= l; i++)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        m |= (jlong)1 << i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Compute a high-order mask for the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * between first and last, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
static jlong highMaskRange(char first, char last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    jlong m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    int f = max(min(first, 127), 64) - 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    int l = max(min(last, 127), 64) - 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    for (i = f; i <= l; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        m |= (jlong)1 << i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Tell whether the given character is permitted by the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
static int match(int c, jlong lowMask, jlong highMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (c >= 0 && c < 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if ((((jlong)1 << c) & lowMask) != 0) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    if (c >= 64 && c < 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if ((((jlong)1 << (c - 64)) & highMask) != 0) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
static void initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    //            "8" | "9"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    jlong L_DIGIT = lowMaskRange('0', '9');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    jlong H_DIGIT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // upalpha  = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    //            "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    //            "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    jlong L_UPALPHA = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    jlong H_UPALPHA = highMaskRange('A', 'Z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    //            "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    //            "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    jlong L_LOWALPHA = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    jlong H_LOWALPHA = highMaskRange('a', 'z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // alpha         = lowalpha | upalpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    jlong L_ALPHA = L_LOWALPHA | L_UPALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    jlong H_ALPHA = H_LOWALPHA | H_UPALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // alphanum      = alpha | digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    jlong L_ALPHANUM = L_DIGIT | L_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    jlong H_ALPHANUM = H_DIGIT | H_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    //                 "(" | ")"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    jlong L_MARK = lowMask("-_.!~*'()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    jlong H_MARK = highMask("-_.!~*'()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // unreserved    = alphanum | mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    jlong L_UNRESERVED = L_ALPHANUM | L_MARK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    jlong H_UNRESERVED = H_ALPHANUM | H_MARK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // pchar         = unreserved |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    //                 ":" | "@" | "&" | "=" | "+" | "$" | ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    jlong L_PCHAR = L_UNRESERVED | lowMask(":@&=+$,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    jlong H_PCHAR = H_UNRESERVED | highMask(":@&=+$,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    // hex           = digit | "A" | "B" | "C" | "D" | "E" | "F" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    //                         "a" | "b" | "c" | "d" | "e" | "f"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    L_HEX = L_DIGIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    H_HEX = highMaskRange('A', 'F') | highMaskRange('a', 'f');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // All valid path characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    L_PATH = L_PCHAR | lowMask(";/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    H_PATH = H_PCHAR | highMask(";/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * Validates that the given URI path component does not contain any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * illegal characters. Returns 0 if only validate characters are present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
int validatePathChars(const char* path) {
23566
760a74c1b589 8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents: 5506
diff changeset
   171
    size_t i, n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /* initialize on first usage */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    if (L_HEX == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    n = strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    while (i < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int c = (int)(signed char)path[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        /* definitely not us-ascii */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (c < 0) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        /* start of an escapted character */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (c == '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (i + 3 <= n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                int h1 = (int)(signed char)path[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                int h2 = (int)(signed char)path[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (h1 < 0 || h2 < 0) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                if (!match(h1, L_HEX, H_HEX)) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (!match(h2, L_HEX, H_HEX)) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                i += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
               /* malformed escape pair */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
               return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (!match(c, L_PATH, H_PATH)) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}