📄 xmlschemastypes.c
字号:
/**
* _xmlSchemaBase64Decode:
* @ch: a character
*
* Converts a base64 encoded character to its base 64 value.
*
* Returns 0-63 (value), 64 (pad), or -1 (not recognized)
*/
static int
_xmlSchemaBase64Decode (const xmlChar ch) {
if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
if ('+' == ch) return 62;
if ('/' == ch) return 63;
if ('=' == ch) return 64;
return -1;
}
/****************************************************************
* *
* XML Schema Dates/Times Datatypes Handling *
* *
****************************************************************/
/**
* PARSE_DIGITS:
* @num: the integer to fill in
* @cur: an #xmlChar *
* @num_type: an integer flag
*
* Parses a digits integer and updates @num with the value. @cur is
* updated to point just after the integer.
* In case of error, @num_type is set to -1, values of @num and
* @cur are undefined.
*/
#define PARSE_DIGITS(num, cur, num_type) \
if ((*cur < '0') || (*cur > '9')) \
num_type = -1; \
else \
while ((*cur >= '0') && (*cur <= '9')) { \
num = num * 10 + (*cur - '0'); \
cur++; \
}
/**
* PARSE_NUM:
* @num: the double to fill in
* @cur: an #xmlChar *
* @num_type: an integer flag
*
* Parses a float or integer and updates @num with the value. @cur is
* updated to point just after the number. If the number is a float,
* then it must have an integer part and a decimal part; @num_type will
* be set to 1. If there is no decimal part, @num_type is set to zero.
* In case of error, @num_type is set to -1, values of @num and
* @cur are undefined.
*/
#define PARSE_NUM(num, cur, num_type) \
num = 0; \
PARSE_DIGITS(num, cur, num_type); \
if (!num_type && (*cur == '.')) { \
double mult = 1; \
cur++; \
if ((*cur < '0') || (*cur > '9')) \
num_type = -1; \
else \
num_type = 1; \
while ((*cur >= '0') && (*cur <= '9')) { \
mult /= 10; \
num += (*cur - '0') * mult; \
cur++; \
} \
}
/**
* xmlSchemaValidateDates:
* @type: the expected type or XML_SCHEMAS_UNKNOWN
* @dateTime: string to analyze
* @val: the return computed value
*
* Check that @dateTime conforms to the lexical space of one of the date types.
* if true a value is computed and returned in @val.
*
* Returns 0 if this validates, a positive error code number otherwise
* and -1 in case of internal or API error.
*/
static int
xmlSchemaValidateDates (xmlSchemaValType type,
const xmlChar *dateTime, xmlSchemaValPtr *val,
int collapse) {
xmlSchemaValPtr dt;
int ret;
const xmlChar *cur = dateTime;
#define RETURN_TYPE_IF_VALID(t) \
if (IS_TZO_CHAR(*cur)) { \
ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
if (ret == 0) { \
if (*cur != 0) \
goto error; \
dt->type = t; \
goto done; \
} \
}
if (dateTime == NULL)
return -1;
if (collapse)
while IS_WSP_BLANK_CH(*cur) cur++;
if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
return 1;
dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
if (dt == NULL)
return -1;
if ((cur[0] == '-') && (cur[1] == '-')) {
/*
* It's an incomplete date (xs:gMonthDay, xs:gMonth or
* xs:gDay)
*/
cur += 2;
/* is it an xs:gDay? */
if (*cur == '-') {
if (type == XML_SCHEMAS_GMONTH)
goto error;
++cur;
ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
if (ret != 0)
goto error;
RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
goto error;
}
/*
* it should be an xs:gMonthDay or xs:gMonth
*/
ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
if (ret != 0)
goto error;
/*
* a '-' char could indicate this type is xs:gMonthDay or
* a negative time zone offset. Check for xs:gMonthDay first.
* Also the first three char's of a negative tzo (-MM:SS) can
* appear to be a valid day; so even if the day portion
* of the xs:gMonthDay verifies, we must insure it was not
* a tzo.
*/
if (*cur == '-') {
const xmlChar *rewnd = cur;
cur++;
ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
/*
* we can use the VALID_MDAY macro to validate the month
* and day because the leap year test will flag year zero
* as a leap year (even though zero is an invalid year).
* FUTURE TODO: Zero will become valid in XML Schema 1.1
* probably.
*/
if (VALID_MDAY((&(dt->value.date)))) {
RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
goto error;
}
}
/*
* not xs:gMonthDay so rewind and check if just xs:gMonth
* with an optional time zone.
*/
cur = rewnd;
}
RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
goto error;
}
/*
* It's a right-truncated date or an xs:time.
* Try to parse an xs:time then fallback on right-truncated dates.
*/
if ((*cur >= '0') && (*cur <= '9')) {
ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
if (ret == 0) {
/* it's an xs:time */
RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
}
}
/* fallback on date parsing */
cur = dateTime;
ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
if (ret != 0)
goto error;
/* is it an xs:gYear? */
RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
if (*cur != '-')
goto error;
cur++;
ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
if (ret != 0)
goto error;
/* is it an xs:gYearMonth? */
RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
if (*cur != '-')
goto error;
cur++;
ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
goto error;
/* is it an xs:date? */
RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
if (*cur != 'T')
goto error;
cur++;
/* it should be an xs:dateTime */
ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
if (ret != 0)
goto error;
ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
if (collapse)
while IS_WSP_BLANK_CH(*cur) cur++;
if ((ret != 0) || (*cur != 0) || !VALID_DATETIME((&(dt->value.date))))
goto error;
dt->type = XML_SCHEMAS_DATETIME;
done:
#if 1
if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
goto error;
#else
/*
* insure the parsed type is equal to or less significant (right
* truncated) than the desired type.
*/
if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
/* time only matches time */
if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
goto error;
if ((type == XML_SCHEMAS_DATETIME) &&
((dt->type != XML_SCHEMAS_DATE) ||
(dt->type != XML_SCHEMAS_GYEARMONTH) ||
(dt->type != XML_SCHEMAS_GYEAR)))
goto error;
if ((type == XML_SCHEMAS_DATE) &&
((dt->type != XML_SCHEMAS_GYEAR) ||
(dt->type != XML_SCHEMAS_GYEARMONTH)))
goto error;
if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
goto error;
if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
goto error;
}
#endif
if (val != NULL)
*val = dt;
else
xmlSchemaFreeValue(dt);
return 0;
error:
if (dt != NULL)
xmlSchemaFreeValue(dt);
return 1;
}
/**
* xmlSchemaValidateDuration:
* @type: the predefined type
* @duration: string to analyze
* @val: the return computed value
*
* Check that @duration conforms to the lexical space of the duration type.
* if true a value is computed and returned in @val.
*
* Returns 0 if this validates, a positive error code number otherwise
* and -1 in case of internal or API error.
*/
static int
xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
const xmlChar *duration, xmlSchemaValPtr *val,
int collapse) {
const xmlChar *cur = duration;
xmlSchemaValPtr dur;
int isneg = 0;
unsigned int seq = 0;
double num;
int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
if (duration == NULL)
return -1;
if (collapse)
while IS_WSP_BLANK_CH(*cur) cur++;
if (*cur == '-') {
isneg = 1;
cur++;
}
/* duration must start with 'P' (after sign) */
if (*cur++ != 'P')
return 1;
if (*cur == 0)
return 1;
dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
if (dur == NULL)
return -1;
while (*cur != 0) {
/* input string should be empty or invalid date/time item */
if (seq >= sizeof(desig))
goto error;
/* T designator must be present for time items */
if (*cur == 'T') {
if (seq <= 3) {
seq = 3;
cur++;
} else
return 1;
} else if (seq == 3)
goto error;
/* parse the number portion of the item */
PARSE_NUM(num, cur, num_type);
if ((num_type == -1) || (*cur == 0))
goto error;
/* update duration based on item type */
while (seq < sizeof(desig)) {
if (*cur == desig[seq]) {
/* verify numeric type; only seconds can be float */
if ((num_type != 0) && (seq < (sizeof(desig)-1)))
goto error;
switch (seq) {
case 0:
dur->value.dur.mon = (long)num * 12;
break;
case 1:
dur->value.dur.mon += (long)num;
break;
default:
/* convert to seconds using multiplier */
dur->value.dur.sec += num * multi[seq];
seq++;
break;
}
break; /* exit loop */
}
/* no date designators found? */
if ((++seq == 3) || (seq == 6))
goto error;
}
cur++;
if (collapse)
while IS_WSP_BLANK_CH(*cur) cur++;
}
if (isneg) {
dur->value.dur.mon = -dur->value.dur.mon;
dur->value.dur.day = -dur->value.dur.day;
dur->value.dur.sec = -dur->value.dur.sec;
}
if (val != NULL)
*val = dur;
else
xmlSchemaFreeValue(dur);
return 0;
error:
if (dur != NULL)
xmlSchemaFreeValue(dur);
return 1;
}
/**
* xmlSchemaStrip:
* @value: a value
*
* Removes the leading and ending spaces of a string
*
* Returns the new string or NULL if no change was required.
*/
static xmlChar *
xmlSchemaStrip(const xmlChar *value) {
const xmlChar *start = value, *end, *f;
if (value == NULL) return(NULL);
while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
end = start;
while (*end != 0) end++;
f = end;
end--;
while ((end > start) && (IS_BLANK_CH(*end))) end--;
end++;
if ((start == value) && (f == end)) return(NULL);
return(xmlStrndup(start, end - start));
}
/**
* xmlSchemaWhiteSpaceReplace:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -