0N/A/*
2362N/A * Copyright (c) 2003, 2006, 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 <ctype.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <sys/types.h>
0N/A#include "jni.h"
0N/A#include "jli_util.h"
0N/A#include "version_comp.h"
0N/A
0N/A/*
0N/A * A collection of useful strings. One should think of these as #define
0N/A * entries, but actual strings can be more efficient (with many compilers).
0N/A */
0N/Astatic const char *separators = ".-_";
0N/Astatic const char *zero_string = "0";
0N/A
0N/A/*
0N/A * Validate a string as parsable as a "Java int". If so parsable,
0N/A * return true (non-zero) and store the numeric value at the address
0N/A * passed in as "value"; otherwise return false (zero).
0N/A *
0N/A * Note that the maximum allowable value is 2147483647 as defined by
0N/A * the "Java Language Specification" which precludes the use of native
0N/A * conversion routines which may have other limits.
0N/A *
0N/A * Also note that we don't have to worry about the alternate maximum
0N/A * allowable value of 2147483648 because it is only allowed after
0N/A * the unary negation operator and this grammar doesn't have one
0N/A * of those.
0N/A *
0N/A * Finally, note that a value which exceeds the maximum jint value will
0N/A * return false (zero). This results in the otherwise purely numeric
0N/A * string being compared as a string of characters (as per the spec.)
0N/A */
0N/Astatic int
0N/Aisjavaint(const char *s, jint *value)
0N/A{
0N/A jlong sum = 0;
0N/A jint digit;
0N/A while (*s != '\0')
0N/A if (isdigit(*s)) {
0N/A digit = (jint)((int)(*s++) - (int)('0'));
0N/A sum = (sum * 10) + digit;
0N/A if (sum > 2147483647)
0N/A return (0); /* Overflows jint (but not jlong) */
0N/A } else
0N/A return (0);
0N/A *value = (jint)sum;
0N/A return (1);
0N/A}
0N/A
0N/A/*
0N/A * Modeled after strcmp(), compare two strings (as in the grammar defined
0N/A * in Appendix A of JSR 56). If both strings can be interpreted as
0N/A * Java ints, do a numeric comparison, else it is strcmp().
0N/A */
0N/Astatic int
0N/Acomp_string(const char *s1, const char *s2)
0N/A{
0N/A jint v1, v2;
0N/A if (isjavaint(s1, &v1) && isjavaint(s2, &v2))
0N/A return ((int)(v1 - v2));
0N/A else
0N/A return (JLI_StrCmp(s1, s2));
0N/A}
0N/A
0N/A/*
0N/A * Modeled after strcmp(), compare two version-ids for a Prefix
0N/A * Match as defined in JSR 56.
0N/A */
0N/Aint
0N/AJLI_PrefixVersionId(const char *id1, char *id2)
0N/A{
0N/A char *s1 = JLI_StringDup(id1);
0N/A char *s2 = JLI_StringDup(id2);
0N/A char *m1 = s1;
0N/A char *m2 = s2;
0N/A char *end1 = NULL;
0N/A char *end2 = NULL;
0N/A int res = 0;
0N/A
0N/A do {
0N/A
0N/A if ((s1 != NULL) && ((end1 = JLI_StrPBrk(s1, ".-_")) != NULL))
0N/A *end1 = '\0';
0N/A if ((s2 != NULL) && ((end2 = JLI_StrPBrk(s2, ".-_")) != NULL))
0N/A *end2 = '\0';
0N/A
0N/A res = comp_string(s1, s2);
0N/A
0N/A if (end1 != NULL)
0N/A s1 = end1 + 1;
0N/A else
0N/A s1 = NULL;
0N/A if (end2 != NULL)
0N/A s2 = end2 + 1;
0N/A else
0N/A s2 = NULL;
0N/A
0N/A } while (res == 0 && ((s1 != NULL) && (s2 != NULL)));
0N/A
0N/A JLI_MemFree(m1);
0N/A JLI_MemFree(m2);
0N/A return (res);
0N/A}
0N/A
0N/A/*
0N/A * Modeled after strcmp(), compare two version-ids for an Exact
0N/A * Match as defined in JSR 56.
0N/A */
0N/Aint
0N/AJLI_ExactVersionId(const char *id1, char *id2)
0N/A{
0N/A char *s1 = JLI_StringDup(id1);
0N/A char *s2 = JLI_StringDup(id2);
0N/A char *m1 = s1;
0N/A char *m2 = s2;
0N/A char *end1 = NULL;
0N/A char *end2 = NULL;
0N/A int res = 0;
0N/A
0N/A do {
0N/A
0N/A if ((s1 != NULL) && ((end1 = JLI_StrPBrk(s1, separators)) != NULL))
0N/A *end1 = '\0';
0N/A if ((s2 != NULL) && ((end2 = JLI_StrPBrk(s2, separators)) != NULL))
0N/A *end2 = '\0';
0N/A
0N/A if ((s1 != NULL) && (s2 == NULL))
0N/A res = comp_string(s1, zero_string);
0N/A else if ((s1 == NULL) && (s2 != NULL))
0N/A res = comp_string(zero_string, s2);
0N/A else
0N/A res = comp_string(s1, s2);
0N/A
0N/A if (end1 != NULL)
0N/A s1 = end1 + 1;
0N/A else
0N/A s1 = NULL;
0N/A if (end2 != NULL)
0N/A s2 = end2 + 1;
0N/A else
0N/A s2 = NULL;
0N/A
0N/A } while (res == 0 && ((s1 != NULL) || (s2 != NULL)));
0N/A
0N/A JLI_MemFree(m1);
0N/A JLI_MemFree(m2);
0N/A return (res);
0N/A}
0N/A
0N/A/*
0N/A * Return true if this simple-element (as defined in JSR 56) forms
0N/A * an acceptable match.
0N/A *
0N/A * JSR 56 is modified by the Java Web Start <rel> Developer Guide
0N/A * where it is stated "... Java Web Start will not consider an installed
0N/A * non-FCS (i.e., milestone) JRE as a match. ... a JRE from Sun
0N/A * Microsystems, Inc., is by convention a non-FCS (milestone) JRE
0N/A * if there is a dash (-) in the version string."
0N/A *
0N/A * An undocumented caveat to the above is that an exact match with a
0N/A * hyphen is accepted as a development extension.
0N/A *
0N/A * These modifications are addressed by the specific comparisons
0N/A * for releases with hyphens.
0N/A */
0N/Astatic int
0N/Aacceptable_simple_element(const char *release, char *simple_element)
0N/A{
0N/A char *modifier;
0N/A modifier = simple_element + JLI_StrLen(simple_element) - 1;
0N/A if (*modifier == '*') {
0N/A *modifier = '\0';
0N/A if (JLI_StrChr(release, '-'))
0N/A return ((JLI_StrCmp(release, simple_element) == 0)?1:0);
0N/A return ((JLI_PrefixVersionId(release, simple_element) == 0)?1:0);
0N/A } else if (*modifier == '+') {
0N/A *modifier = '\0';
0N/A if (JLI_StrChr(release, '-'))
0N/A return ((JLI_StrCmp(release, simple_element) == 0)?1:0);
0N/A return ((JLI_ExactVersionId(release, simple_element) >= 0)?1:0);
0N/A } else {
0N/A return ((JLI_ExactVersionId(release, simple_element) == 0)?1:0);
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * Return true if this element (as defined in JSR 56) forms
0N/A * an acceptable match. An element is the intersection (and)
0N/A * of multiple simple-elements.
0N/A */
0N/Astatic int
0N/Aacceptable_element(const char *release, char *element)
0N/A{
0N/A char *end;
0N/A do {
0N/A if ((end = JLI_StrChr(element, '&')) != NULL)
0N/A *end = '\0';
0N/A if (!acceptable_simple_element(release, element))
0N/A return (0);
0N/A if (end != NULL)
0N/A element = end + 1;
0N/A } while (end != NULL);
0N/A return (1);
0N/A}
0N/A
0N/A/*
0N/A * Checks if release is acceptable by the specification version-string.
0N/A * Return true if this version-string (as defined in JSR 56) forms
0N/A * an acceptable match. A version-string is the union (or) of multiple
0N/A * elements.
0N/A */
0N/Aint
0N/AJLI_AcceptableRelease(const char *release, char *version_string)
0N/A{
0N/A char *vs;
0N/A char *m1;
0N/A char *end;
0N/A m1 = vs = JLI_StringDup(version_string);
0N/A do {
0N/A if ((end = JLI_StrChr(vs, ' ')) != NULL)
0N/A *end = '\0';
0N/A if (acceptable_element(release, vs)) {
0N/A JLI_MemFree(m1);
0N/A return (1);
0N/A }
0N/A if (end != NULL)
0N/A vs = end + 1;
0N/A } while (end != NULL);
0N/A JLI_MemFree(m1);
0N/A return (0);
0N/A}
0N/A
0N/A/*
0N/A * Return true if this is a valid simple-element (as defined in JSR 56).
0N/A *
0N/A * The official grammar for a simple-element is:
0N/A *
0N/A * simple-element ::= version-id | version-id modifier
0N/A * modifier ::= '+' | '*'
0N/A * version-id ::= string ( separator string )*
0N/A * string ::= char ( char )*
0N/A * char ::= Any ASCII character except a space, an
0N/A * ampersand, a separator or a modifier
0N/A * separator ::= '.' | '-' | '_'
0N/A *
0N/A * However, for efficiency, it is time to abandon the top down parser
0N/A * implementation. After deleting the potential trailing modifier, we
0N/A * are left with a version-id.
0N/A *
0N/A * Note that a valid version-id has three simple properties:
0N/A *
0N/A * 1) Doesn't contain a space, an ampersand or a modifier.
0N/A *
0N/A * 2) Doesn't begin or end with a separator.
0N/A *
0N/A * 3) Doesn't contain two adjacent separators.
0N/A *
0N/A * Any other line noise constitutes a valid version-id.
0N/A */
0N/Astatic int
0N/Avalid_simple_element(char *simple_element)
0N/A{
0N/A char *last;
0N/A size_t len;
0N/A
0N/A if ((simple_element == NULL) || ((len = JLI_StrLen(simple_element)) == 0))
0N/A return (0);
0N/A last = simple_element + len - 1;
0N/A if (*last == '*' || *last == '+') {
0N/A if (--len == 0)
0N/A return (0);
0N/A *last-- = '\0';
0N/A }
0N/A if (JLI_StrPBrk(simple_element, " &+*") != NULL) /* Property #1 */
0N/A return (0);
0N/A if ((JLI_StrChr(".-_", *simple_element) != NULL) || /* Property #2 */
0N/A (JLI_StrChr(".-_", *last) != NULL))
0N/A return (0);
0N/A for (; simple_element != last; simple_element++) /* Property #3 */
0N/A if ((JLI_StrChr(".-_", *simple_element) != NULL) &&
0N/A (JLI_StrChr(".-_", *(simple_element + 1)) != NULL))
0N/A return (0);
0N/A return (1);
0N/A}
0N/A
0N/A/*
0N/A * Return true if this is a valid element (as defined in JSR 56).
0N/A * An element is the intersection (and) of multiple simple-elements.
0N/A */
0N/Astatic int
0N/Avalid_element(char *element)
0N/A{
0N/A char *end;
0N/A if ((element == NULL) || (JLI_StrLen(element) == 0))
0N/A return (0);
0N/A do {
0N/A if ((end = JLI_StrChr(element, '&')) != NULL)
0N/A *end = '\0';
0N/A if (!valid_simple_element(element))
0N/A return (0);
0N/A if (end != NULL)
0N/A element = end + 1;
0N/A } while (end != NULL);
0N/A return (1);
0N/A}
0N/A
0N/A/*
0N/A * Validates a version string by the extended JSR 56 grammar.
0N/A */
0N/Aint
0N/AJLI_ValidVersionString(char *version_string)
0N/A{
0N/A char *vs;
0N/A char *m1;
0N/A char *end;
0N/A if ((version_string == NULL) || (JLI_StrLen(version_string) == 0))
0N/A return (0);
0N/A m1 = vs = JLI_StringDup(version_string);
0N/A do {
0N/A if ((end = JLI_StrChr(vs, ' ')) != NULL)
0N/A *end = '\0';
0N/A if (!valid_element(vs)) {
0N/A JLI_MemFree(m1);
0N/A return (0);
0N/A }
0N/A if (end != NULL)
0N/A vs = end + 1;
0N/A } while (end != NULL);
0N/A JLI_MemFree(m1);
0N/A return (1);
0N/A}