src/share/bin/version_comp.c::JLI_ValidVersionString
/*
* Validates a version string by the extended JSR 56 grammar.
*/
int
JLI_ValidVersionString(char *version_string)
{
char *vs;
char *m1;
char *end;
if ((version_string == NULL) || (JLI_StrLen(version_string) == 0))
return (0);
//拷贝一份版本号副本
m1 = vs = JLI_StringDup(version_string);
do {
//搜索vs中第一次出现' '的位置,如果未找到返回NULL
//如果找到,将vs从空格前截断,end指针指向剩余部分(包含空格)
if ((end = JLI_StrChr(vs, ' ')) != NULL)
*end = '\0';//end首地址赋值为'\0'
//校验元素vs,如果vs包含空格,仅判断空格前的部分
//如果元素无效直接释放内存并返回
if (!valid_element(vs)) {
JLI_MemFree(m1);
return (0);
}
//如果vs包含空格,重新将vs赋值指向空格后的字符串,继续循环判断
if (end != NULL)
vs = end + 1;
} while (end != NULL);
JLI_MemFree(m1);
return (1);
}
src/share/bin/version_comp.c::valid_element
/*
* Return true if this is a valid element (as defined in JSR 56).
* An element is the intersection (and) of multiple simple-elements.
*/
static int
valid_element(char *element)
{
char *end;
if ((element == NULL) || (JLI_StrLen(element) == 0))
return (0);
do {
//搜索vs中第一次出现'&'的位置,如果未找到返回NULL
//如果找到,将element从空格前截断,end指针指向剩余部分(包含空格)
if ((end = JLI_StrChr(element, '&')) != NULL)
*end = '\0';//end首地址赋值为'\0'
//校验元素element,如果vs包含空格,仅判断空格前的部分
//如果元素无效直接释放内存并返回
if (!valid_simple_element(element))
return (0);
//如果vs包含空格,重新将element赋值指向空格后的字符串,继续循环判断
if (end != NULL)
element = end + 1;
} while (end != NULL);
return (1);
}
src/share/bin/version_comp.c::valid_simple_element
/*
* Return true if this is a valid simple-element (as defined in JSR 56).
*
* The official grammar for a simple-element is:
*
* simple-element ::= version-id | version-id modifier
* modifier ::= '+' | '*'
* version-id ::= string ( separator string )*
* string ::= char ( char )*
* char ::= Any ASCII character except a space, an
* ampersand, a separator or a modifier
* separator ::= '.' | '-' | '_'
*
* However, for efficiency, it is time to abandon the top down parser
* implementation. After deleting the potential trailing modifier, we
* are left with a version-id.
*
* Note that a valid version-id has three simple properties:
*
* 1) Doesn't contain a space, an ampersand or a modifier.
*
* 2) Doesn't begin or end with a separator.
*
* 3) Doesn't contain two adjacent separators.
*
* Any other line noise constitutes a valid version-id.
*/
static int
valid_simple_element(char *simple_element)
{
char *last;
size_t len;
//如果验证字符串为NULL或空串,直接返回0
if ((simple_element == NULL) || ((len = JLI_StrLen(simple_element)) == 0))
return (0);
//字符串最后一个字符为 '*' | '+'
last = simple_element + len - 1;
if (*last == '*' || *last == '+') {
//字符串长度为1,直接返回0
if (--len == 0)
return (0);
//先执行--,即last字符指针向前移动一个单位,再设置对应内存的值为'\0'
//参考C语言运算符优先级:http://c.biancheng.net/view/161.html
//意图应该是删除最后的 '*' | '+'
*last-- = '\0';
}
//判断是否包含:' ' | '+' | '*',如果是,版本号无效
if (JLI_StrPBrk(simple_element, " &+*") != NULL) /* Property #1 */
return (0);
//判断是否以分隔符开始或结束:'.' | '-' | '_',如果是,版本号无效
if ((JLI_StrChr(".-_", *simple_element) != NULL) || /* Property #2 */
(JLI_StrChr(".-_", *last) != NULL))
return (0);
//判断是否包含两个连续的分隔符:'.' | '-' | '_',如果是,版本号无效
for (; simple_element != last; simple_element++) /* Property #3 */
if ((JLI_StrChr(".-_", *simple_element) != NULL) &&
(JLI_StrChr(".-_", *(simple_element + 1)) != NULL))
return (0);
return (1);
}