Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/bin/version_comp.c
38767 views
/*1* Copyright (c) 2003, 2006, 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 <ctype.h>26#include <stdlib.h>27#include <string.h>28#include <sys/types.h>29#include "jni.h"30#include "jli_util.h"31#include "version_comp.h"3233/*34* A collection of useful strings. One should think of these as #define35* entries, but actual strings can be more efficient (with many compilers).36*/37static const char *separators = ".-_";38static const char *zero_string = "0";3940/*41* Validate a string as parsable as a "Java int". If so parsable,42* return true (non-zero) and store the numeric value at the address43* passed in as "value"; otherwise return false (zero).44*45* Note that the maximum allowable value is 2147483647 as defined by46* the "Java Language Specification" which precludes the use of native47* conversion routines which may have other limits.48*49* Also note that we don't have to worry about the alternate maximum50* allowable value of 2147483648 because it is only allowed after51* the unary negation operator and this grammar doesn't have one52* of those.53*54* Finally, note that a value which exceeds the maximum jint value will55* return false (zero). This results in the otherwise purely numeric56* string being compared as a string of characters (as per the spec.)57*/58static int59isjavaint(const char *s, jint *value)60{61jlong sum = 0;62jint digit;63while (*s != '\0')64if (isdigit(*s)) {65digit = (jint)((int)(*s++) - (int)('0'));66sum = (sum * 10) + digit;67if (sum > 2147483647)68return (0); /* Overflows jint (but not jlong) */69} else70return (0);71*value = (jint)sum;72return (1);73}7475/*76* Modeled after strcmp(), compare two strings (as in the grammar defined77* in Appendix A of JSR 56). If both strings can be interpreted as78* Java ints, do a numeric comparison, else it is strcmp().79*/80static int81comp_string(const char *s1, const char *s2)82{83jint v1, v2;84if (isjavaint(s1, &v1) && isjavaint(s2, &v2))85return ((int)(v1 - v2));86else87return (JLI_StrCmp(s1, s2));88}8990/*91* Modeled after strcmp(), compare two version-ids for a Prefix92* Match as defined in JSR 56.93*/94int95JLI_PrefixVersionId(const char *id1, char *id2)96{97char *s1 = JLI_StringDup(id1);98char *s2 = JLI_StringDup(id2);99char *m1 = s1;100char *m2 = s2;101char *end1 = NULL;102char *end2 = NULL;103int res = 0;104105do {106107if ((s1 != NULL) && ((end1 = JLI_StrPBrk(s1, ".-_")) != NULL))108*end1 = '\0';109if ((s2 != NULL) && ((end2 = JLI_StrPBrk(s2, ".-_")) != NULL))110*end2 = '\0';111112res = comp_string(s1, s2);113114if (end1 != NULL)115s1 = end1 + 1;116else117s1 = NULL;118if (end2 != NULL)119s2 = end2 + 1;120else121s2 = NULL;122123} while (res == 0 && ((s1 != NULL) && (s2 != NULL)));124125JLI_MemFree(m1);126JLI_MemFree(m2);127return (res);128}129130/*131* Modeled after strcmp(), compare two version-ids for an Exact132* Match as defined in JSR 56.133*/134int135JLI_ExactVersionId(const char *id1, char *id2)136{137char *s1 = JLI_StringDup(id1);138char *s2 = JLI_StringDup(id2);139char *m1 = s1;140char *m2 = s2;141char *end1 = NULL;142char *end2 = NULL;143int res = 0;144145do {146147if ((s1 != NULL) && ((end1 = JLI_StrPBrk(s1, separators)) != NULL))148*end1 = '\0';149if ((s2 != NULL) && ((end2 = JLI_StrPBrk(s2, separators)) != NULL))150*end2 = '\0';151152if ((s1 != NULL) && (s2 == NULL))153res = comp_string(s1, zero_string);154else if ((s1 == NULL) && (s2 != NULL))155res = comp_string(zero_string, s2);156else157res = comp_string(s1, s2);158159if (end1 != NULL)160s1 = end1 + 1;161else162s1 = NULL;163if (end2 != NULL)164s2 = end2 + 1;165else166s2 = NULL;167168} while (res == 0 && ((s1 != NULL) || (s2 != NULL)));169170JLI_MemFree(m1);171JLI_MemFree(m2);172return (res);173}174175/*176* Return true if this simple-element (as defined in JSR 56) forms177* an acceptable match.178*179* JSR 56 is modified by the Java Web Start <rel> Developer Guide180* where it is stated "... Java Web Start will not consider an installed181* non-FCS (i.e., milestone) JRE as a match. ... a JRE from Sun182* Microsystems, Inc., is by convention a non-FCS (milestone) JRE183* if there is a dash (-) in the version string."184*185* An undocumented caveat to the above is that an exact match with a186* hyphen is accepted as a development extension.187*188* These modifications are addressed by the specific comparisons189* for releases with hyphens.190*/191static int192acceptable_simple_element(const char *release, char *simple_element)193{194char *modifier;195modifier = simple_element + JLI_StrLen(simple_element) - 1;196if (*modifier == '*') {197*modifier = '\0';198if (JLI_StrChr(release, '-'))199return ((JLI_StrCmp(release, simple_element) == 0)?1:0);200return ((JLI_PrefixVersionId(release, simple_element) == 0)?1:0);201} else if (*modifier == '+') {202*modifier = '\0';203if (JLI_StrChr(release, '-'))204return ((JLI_StrCmp(release, simple_element) == 0)?1:0);205return ((JLI_ExactVersionId(release, simple_element) >= 0)?1:0);206} else {207return ((JLI_ExactVersionId(release, simple_element) == 0)?1:0);208}209}210211/*212* Return true if this element (as defined in JSR 56) forms213* an acceptable match. An element is the intersection (and)214* of multiple simple-elements.215*/216static int217acceptable_element(const char *release, char *element)218{219char *end;220do {221if ((end = JLI_StrChr(element, '&')) != NULL)222*end = '\0';223if (!acceptable_simple_element(release, element))224return (0);225if (end != NULL)226element = end + 1;227} while (end != NULL);228return (1);229}230231/*232* Checks if release is acceptable by the specification version-string.233* Return true if this version-string (as defined in JSR 56) forms234* an acceptable match. A version-string is the union (or) of multiple235* elements.236*/237int238JLI_AcceptableRelease(const char *release, char *version_string)239{240char *vs;241char *m1;242char *end;243m1 = vs = JLI_StringDup(version_string);244do {245if ((end = JLI_StrChr(vs, ' ')) != NULL)246*end = '\0';247if (acceptable_element(release, vs)) {248JLI_MemFree(m1);249return (1);250}251if (end != NULL)252vs = end + 1;253} while (end != NULL);254JLI_MemFree(m1);255return (0);256}257258/*259* Return true if this is a valid simple-element (as defined in JSR 56).260*261* The official grammar for a simple-element is:262*263* simple-element ::= version-id | version-id modifier264* modifier ::= '+' | '*'265* version-id ::= string ( separator string )*266* string ::= char ( char )*267* char ::= Any ASCII character except a space, an268* ampersand, a separator or a modifier269* separator ::= '.' | '-' | '_'270*271* However, for efficiency, it is time to abandon the top down parser272* implementation. After deleting the potential trailing modifier, we273* are left with a version-id.274*275* Note that a valid version-id has three simple properties:276*277* 1) Doesn't contain a space, an ampersand or a modifier.278*279* 2) Doesn't begin or end with a separator.280*281* 3) Doesn't contain two adjacent separators.282*283* Any other line noise constitutes a valid version-id.284*/285static int286valid_simple_element(char *simple_element)287{288char *last;289size_t len;290291if ((simple_element == NULL) || ((len = JLI_StrLen(simple_element)) == 0))292return (0);293last = simple_element + len - 1;294if (*last == '*' || *last == '+') {295if (--len == 0)296return (0);297*last-- = '\0';298}299if (JLI_StrPBrk(simple_element, " &+*") != NULL) /* Property #1 */300return (0);301if ((JLI_StrChr(".-_", *simple_element) != NULL) || /* Property #2 */302(JLI_StrChr(".-_", *last) != NULL))303return (0);304for (; simple_element != last; simple_element++) /* Property #3 */305if ((JLI_StrChr(".-_", *simple_element) != NULL) &&306(JLI_StrChr(".-_", *(simple_element + 1)) != NULL))307return (0);308return (1);309}310311/*312* Return true if this is a valid element (as defined in JSR 56).313* An element is the intersection (and) of multiple simple-elements.314*/315static int316valid_element(char *element)317{318char *end;319if ((element == NULL) || (JLI_StrLen(element) == 0))320return (0);321do {322if ((end = JLI_StrChr(element, '&')) != NULL)323*end = '\0';324if (!valid_simple_element(element))325return (0);326if (end != NULL)327element = end + 1;328} while (end != NULL);329return (1);330}331332/*333* Validates a version string by the extended JSR 56 grammar.334*/335int336JLI_ValidVersionString(char *version_string)337{338char *vs;339char *m1;340char *end;341if ((version_string == NULL) || (JLI_StrLen(version_string) == 0))342return (0);343m1 = vs = JLI_StringDup(version_string);344do {345if ((end = JLI_StrChr(vs, ' ')) != NULL)346*end = '\0';347if (!valid_element(vs)) {348JLI_MemFree(m1);349return (0);350}351if (end != NULL)352vs = end + 1;353} while (end != NULL);354JLI_MemFree(m1);355return (1);356}357358359