Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * schemastypes.c : implementation of the XML Schema Datatypes |
| 3 | * definition and validity checking |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
| 7 | * Daniel Veillard <veillard@redhat.com> |
| 8 | */ |
| 9 | |
| 10 | #define IN_LIBXML |
| 11 | #include "libxml.h" |
| 12 | |
| 13 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14 | |
| 15 | #include <string.h> |
| 16 | #include <libxml/xmlmemory.h> |
| 17 | #include <libxml/parser.h> |
| 18 | #include <libxml/parserInternals.h> |
| 19 | #include <libxml/hash.h> |
| 20 | #include <libxml/valid.h> |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 21 | #include <libxml/xpath.h> |
| 22 | #include <libxml/uri.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 23 | |
| 24 | #include <libxml/xmlschemas.h> |
| 25 | #include <libxml/schemasInternals.h> |
| 26 | #include <libxml/xmlschemastypes.h> |
| 27 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 28 | #ifdef HAVE_MATH_H |
| 29 | #include <math.h> |
| 30 | #endif |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 31 | #ifdef HAVE_FLOAT_H |
| 32 | #include <float.h> |
| 33 | #endif |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 35 | #define DEBUG |
| 36 | |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 37 | #ifndef LIBXML_XPATH_ENABLED |
| 38 | extern double xmlXPathNAN; |
| 39 | extern double xmlXPathPINF; |
| 40 | extern double xmlXPathNINF; |
| 41 | #endif |
| 42 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 43 | #define TODO \ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 44 | xmlGenericError(xmlGenericErrorContext, \ |
| 45 | "Unimplemented block at %s:%d\n", \ |
| 46 | __FILE__, __LINE__); |
| 47 | |
| 48 | #define XML_SCHEMAS_NAMESPACE_NAME \ |
| 49 | (const xmlChar *)"http://www.w3.org/2001/XMLSchema" |
| 50 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 51 | #define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \ |
| 52 | ((c) == 0xd)) |
| 53 | |
| 54 | #define IS_WSP_SPACE_CH(c) ((c) == 0x20) |
| 55 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 56 | #define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c) |
| 57 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 58 | /* Date value */ |
| 59 | typedef struct _xmlSchemaValDate xmlSchemaValDate; |
| 60 | typedef xmlSchemaValDate *xmlSchemaValDatePtr; |
| 61 | struct _xmlSchemaValDate { |
| 62 | long year; |
| 63 | unsigned int mon :4; /* 1 <= mon <= 12 */ |
| 64 | unsigned int day :5; /* 1 <= day <= 31 */ |
| 65 | unsigned int hour :5; /* 0 <= hour <= 23 */ |
| 66 | unsigned int min :6; /* 0 <= min <= 59 */ |
| 67 | double sec; |
Daniel Veillard | a77cf71 | 2003-05-09 23:09:55 +0000 | [diff] [blame] | 68 | unsigned int tz_flag :1; /* is tzo explicitely set? */ |
Kasimier T. Buchcik | 285b367 | 2005-05-12 13:10:22 +0000 | [diff] [blame] | 69 | signed int tzo :12; /* -1440 <= tzo <= 1440; |
| 70 | currently only -840 to +840 are needed */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /* Duration value */ |
| 74 | typedef struct _xmlSchemaValDuration xmlSchemaValDuration; |
| 75 | typedef xmlSchemaValDuration *xmlSchemaValDurationPtr; |
| 76 | struct _xmlSchemaValDuration { |
| 77 | long mon; /* mon stores years also */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 78 | long day; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 79 | double sec; /* sec stores min and hour also */ |
| 80 | }; |
| 81 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 82 | typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal; |
| 83 | typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr; |
| 84 | struct _xmlSchemaValDecimal { |
| 85 | /* would use long long but not portable */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 86 | unsigned long lo; |
| 87 | unsigned long mi; |
| 88 | unsigned long hi; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 89 | unsigned int extra; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 90 | unsigned int sign:1; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 91 | unsigned int frac:7; |
| 92 | unsigned int total:8; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 95 | typedef struct _xmlSchemaValQName xmlSchemaValQName; |
| 96 | typedef xmlSchemaValQName *xmlSchemaValQNamePtr; |
| 97 | struct _xmlSchemaValQName { |
| 98 | xmlChar *name; |
| 99 | xmlChar *uri; |
| 100 | }; |
| 101 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 102 | typedef struct _xmlSchemaValHex xmlSchemaValHex; |
| 103 | typedef xmlSchemaValHex *xmlSchemaValHexPtr; |
| 104 | struct _xmlSchemaValHex { |
| 105 | xmlChar *str; |
| 106 | unsigned int total; |
| 107 | }; |
| 108 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 109 | typedef struct _xmlSchemaValBase64 xmlSchemaValBase64; |
| 110 | typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr; |
| 111 | struct _xmlSchemaValBase64 { |
| 112 | xmlChar *str; |
| 113 | unsigned int total; |
| 114 | }; |
| 115 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 116 | struct _xmlSchemaVal { |
| 117 | xmlSchemaValType type; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 118 | struct _xmlSchemaVal *next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 119 | union { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 120 | xmlSchemaValDecimal decimal; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 121 | xmlSchemaValDate date; |
| 122 | xmlSchemaValDuration dur; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 123 | xmlSchemaValQName qname; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 124 | xmlSchemaValHex hex; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 125 | xmlSchemaValBase64 base64; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 126 | float f; |
| 127 | double d; |
Daniel Veillard | c5a70f2 | 2003-02-06 23:41:59 +0000 | [diff] [blame] | 128 | int b; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 129 | xmlChar *str; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 130 | } value; |
| 131 | }; |
| 132 | |
| 133 | static int xmlSchemaTypesInitialized = 0; |
| 134 | static xmlHashTablePtr xmlSchemaTypesBank = NULL; |
| 135 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Basic types |
| 138 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 139 | static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL; |
| 140 | static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL; |
| 141 | static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL; |
| 142 | static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 143 | static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 144 | static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 145 | static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL; |
| 146 | static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL; |
| 147 | static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL; |
| 148 | static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL; |
| 149 | static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL; |
| 150 | static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL; |
| 151 | static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 152 | static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL; |
Daniel Veillard | c5a70f2 | 2003-02-06 23:41:59 +0000 | [diff] [blame] | 153 | static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 154 | static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL; |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 155 | static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 156 | static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 157 | static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 158 | |
| 159 | /* |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 160 | * Derived types |
| 161 | */ |
| 162 | static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL; |
| 163 | static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL; |
| 164 | static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL; |
| 165 | static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL; |
| 166 | static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL; |
| 167 | static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL; |
| 168 | static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL; |
| 169 | static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL; |
| 170 | static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL; |
| 171 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL; |
| 172 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL; |
| 173 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL; |
| 174 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 175 | static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL; |
| 176 | static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL; |
| 177 | static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL; |
| 178 | static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL; |
| 179 | static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 180 | static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 181 | static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL; |
| 182 | static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL; |
| 183 | static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL; |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 184 | static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL; |
| 185 | static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 186 | static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 187 | static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL; |
| 188 | static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 189 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 190 | /************************************************************************ |
| 191 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 192 | * Datatype error handlers * |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 193 | * * |
| 194 | ************************************************************************/ |
| 195 | /** |
| 196 | * xmlSchemaTypeErrMemory: |
| 197 | * @extra: extra informations |
| 198 | * |
| 199 | * Handle an out of memory condition |
| 200 | */ |
| 201 | static void |
| 202 | xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra) |
| 203 | { |
| 204 | __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra); |
| 205 | } |
| 206 | |
| 207 | /************************************************************************ |
| 208 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 209 | * Base types support * |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 210 | * * |
| 211 | ************************************************************************/ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * xmlSchemaNewValue: |
| 215 | * @type: the value type |
| 216 | * |
| 217 | * Allocate a new simple type value |
| 218 | * |
| 219 | * Returns a pointer to the new value or NULL in case of error |
| 220 | */ |
| 221 | static xmlSchemaValPtr |
| 222 | xmlSchemaNewValue(xmlSchemaValType type) { |
| 223 | xmlSchemaValPtr value; |
| 224 | |
| 225 | value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal)); |
| 226 | if (value == NULL) { |
| 227 | return(NULL); |
| 228 | } |
| 229 | memset(value, 0, sizeof(xmlSchemaVal)); |
| 230 | value->type = type; |
| 231 | return(value); |
| 232 | } |
| 233 | |
| 234 | static xmlSchemaFacetPtr |
| 235 | xmlSchemaNewMinLengthFacet(int value) |
| 236 | { |
| 237 | xmlSchemaFacetPtr ret; |
| 238 | |
| 239 | ret = xmlSchemaNewFacet(); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 240 | if (ret == NULL) { |
| 241 | return(NULL); |
| 242 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 243 | ret->type = XML_SCHEMA_FACET_MINLENGTH; |
| 244 | ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER); |
Daniel Veillard | 717042d | 2013-07-22 14:28:20 +0800 | [diff] [blame] | 245 | if (ret->val == NULL) { |
| 246 | xmlFree(ret); |
| 247 | return(NULL); |
| 248 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 249 | ret->val->value.decimal.lo = value; |
| 250 | return (ret); |
| 251 | } |
| 252 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 253 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 254 | * xmlSchemaInitBasicType: |
| 255 | * @name: the type name |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 256 | * @type: the value type associated |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 257 | * |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 258 | * Initialize one primitive built-in type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 259 | */ |
| 260 | static xmlSchemaTypePtr |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 261 | xmlSchemaInitBasicType(const char *name, xmlSchemaValType type, |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 262 | xmlSchemaTypePtr baseType) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 263 | xmlSchemaTypePtr ret; |
| 264 | |
| 265 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 266 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 267 | xmlSchemaTypeErrMemory(NULL, "could not initialize basic types"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 268 | return(NULL); |
| 269 | } |
| 270 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 271 | ret->name = (const xmlChar *)name; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 272 | ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 273 | ret->type = XML_SCHEMA_TYPE_BASIC; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 274 | ret->baseType = baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 275 | ret->contentType = XML_SCHEMA_CONTENT_BASIC; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 276 | /* |
| 277 | * Primitive types. |
| 278 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 279 | switch (type) { |
| 280 | case XML_SCHEMAS_STRING: |
| 281 | case XML_SCHEMAS_DECIMAL: |
| 282 | case XML_SCHEMAS_DATE: |
| 283 | case XML_SCHEMAS_DATETIME: |
| 284 | case XML_SCHEMAS_TIME: |
| 285 | case XML_SCHEMAS_GYEAR: |
| 286 | case XML_SCHEMAS_GYEARMONTH: |
| 287 | case XML_SCHEMAS_GMONTH: |
| 288 | case XML_SCHEMAS_GMONTHDAY: |
| 289 | case XML_SCHEMAS_GDAY: |
| 290 | case XML_SCHEMAS_DURATION: |
| 291 | case XML_SCHEMAS_FLOAT: |
| 292 | case XML_SCHEMAS_DOUBLE: |
| 293 | case XML_SCHEMAS_BOOLEAN: |
| 294 | case XML_SCHEMAS_ANYURI: |
| 295 | case XML_SCHEMAS_HEXBINARY: |
| 296 | case XML_SCHEMAS_BASE64BINARY: |
| 297 | case XML_SCHEMAS_QNAME: |
| 298 | case XML_SCHEMAS_NOTATION: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 299 | ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 300 | break; |
William M. Brack | 96d2eff | 2004-06-30 11:48:47 +0000 | [diff] [blame] | 301 | default: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 302 | break; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 303 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 304 | /* |
| 305 | * Set variety. |
| 306 | */ |
| 307 | switch (type) { |
| 308 | case XML_SCHEMAS_ANYTYPE: |
| 309 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 310 | break; |
| 311 | case XML_SCHEMAS_IDREFS: |
| 312 | case XML_SCHEMAS_NMTOKENS: |
| 313 | case XML_SCHEMAS_ENTITIES: |
| 314 | ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST; |
| 315 | ret->facets = xmlSchemaNewMinLengthFacet(1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 316 | ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 317 | break; |
| 318 | default: |
| 319 | ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC; |
| 320 | break; |
| 321 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 322 | xmlHashAddEntry2(xmlSchemaTypesBank, ret->name, |
| 323 | XML_SCHEMAS_NAMESPACE_NAME, ret); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 324 | ret->builtInType = type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 325 | return(ret); |
| 326 | } |
| 327 | |
| 328 | /* |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 329 | * WARNING: Those type reside normally in xmlschemas.c but are |
| 330 | * redefined here locally in oder of being able to use them for xs:anyType- |
| 331 | * TODO: Remove those definition if we move the types to a header file. |
| 332 | * TODO: Always keep those structs up-to-date with the originals. |
| 333 | */ |
| 334 | #define UNBOUNDED (1 << 30) |
| 335 | |
| 336 | typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem; |
| 337 | typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr; |
| 338 | struct _xmlSchemaTreeItem { |
| 339 | xmlSchemaTypeType type; |
| 340 | xmlSchemaAnnotPtr annot; |
| 341 | xmlSchemaTreeItemPtr next; |
| 342 | xmlSchemaTreeItemPtr children; |
| 343 | }; |
| 344 | |
| 345 | typedef struct _xmlSchemaParticle xmlSchemaParticle; |
| 346 | typedef xmlSchemaParticle *xmlSchemaParticlePtr; |
| 347 | struct _xmlSchemaParticle { |
| 348 | xmlSchemaTypeType type; |
| 349 | xmlSchemaAnnotPtr annot; |
| 350 | xmlSchemaTreeItemPtr next; |
| 351 | xmlSchemaTreeItemPtr children; |
| 352 | int minOccurs; |
| 353 | int maxOccurs; |
| 354 | xmlNodePtr node; |
| 355 | }; |
| 356 | |
| 357 | typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup; |
| 358 | typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr; |
| 359 | struct _xmlSchemaModelGroup { |
| 360 | xmlSchemaTypeType type; |
| 361 | xmlSchemaAnnotPtr annot; |
| 362 | xmlSchemaTreeItemPtr next; |
| 363 | xmlSchemaTreeItemPtr children; |
| 364 | xmlNodePtr node; |
| 365 | }; |
| 366 | |
| 367 | static xmlSchemaParticlePtr |
| 368 | xmlSchemaAddParticle(void) |
| 369 | { |
| 370 | xmlSchemaParticlePtr ret = NULL; |
| 371 | |
| 372 | ret = (xmlSchemaParticlePtr) |
| 373 | xmlMalloc(sizeof(xmlSchemaParticle)); |
| 374 | if (ret == NULL) { |
| 375 | xmlSchemaTypeErrMemory(NULL, "allocating particle component"); |
| 376 | return (NULL); |
| 377 | } |
| 378 | memset(ret, 0, sizeof(xmlSchemaParticle)); |
| 379 | ret->type = XML_SCHEMA_TYPE_PARTICLE; |
| 380 | ret->minOccurs = 1; |
| 381 | ret->maxOccurs = 1; |
| 382 | return (ret); |
| 383 | } |
| 384 | |
| 385 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 386 | * xmlSchemaInitTypes: |
| 387 | * |
| 388 | * Initialize the default XML Schemas type library |
| 389 | */ |
| 390 | void |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 391 | xmlSchemaInitTypes(void) |
| 392 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 393 | if (xmlSchemaTypesInitialized != 0) |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 394 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 395 | xmlSchemaTypesBank = xmlHashCreate(40); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 396 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 397 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 398 | /* |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 399 | * 3.4.7 Built-in Complex Type Definition |
| 400 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 401 | xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType", |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 402 | XML_SCHEMAS_ANYTYPE, |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 403 | NULL); |
| 404 | xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef; |
| 405 | xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 406 | /* |
| 407 | * Init the content type. |
| 408 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 409 | xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 410 | { |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 411 | xmlSchemaParticlePtr particle; |
| 412 | xmlSchemaModelGroupPtr sequence; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 413 | xmlSchemaWildcardPtr wild; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 414 | /* First particle. */ |
| 415 | particle = xmlSchemaAddParticle(); |
| 416 | if (particle == NULL) |
| 417 | return; |
| 418 | xmlSchemaTypeAnyTypeDef->subtypes = (xmlSchemaTypePtr) particle; |
| 419 | /* Sequence model group. */ |
| 420 | sequence = (xmlSchemaModelGroupPtr) |
| 421 | xmlMalloc(sizeof(xmlSchemaModelGroup)); |
| 422 | if (sequence == NULL) { |
| 423 | xmlSchemaTypeErrMemory(NULL, "allocating model group component"); |
| 424 | return; |
| 425 | } |
| 426 | memset(sequence, 0, sizeof(xmlSchemaModelGroup)); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 427 | sequence->type = XML_SCHEMA_TYPE_SEQUENCE; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 428 | particle->children = (xmlSchemaTreeItemPtr) sequence; |
| 429 | /* Second particle. */ |
| 430 | particle = xmlSchemaAddParticle(); |
| 431 | if (particle == NULL) |
| 432 | return; |
| 433 | particle->minOccurs = 0; |
| 434 | particle->maxOccurs = UNBOUNDED; |
| 435 | sequence->children = (xmlSchemaTreeItemPtr) particle; |
| 436 | /* The wildcard */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 437 | wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard)); |
| 438 | if (wild == NULL) { |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 439 | xmlSchemaTypeErrMemory(NULL, "allocating wildcard component"); |
| 440 | return; |
| 441 | } |
| 442 | memset(wild, 0, sizeof(xmlSchemaWildcard)); |
| 443 | wild->type = XML_SCHEMA_TYPE_ANY; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 444 | wild->any = 1; |
| 445 | wild->processContents = XML_SCHEMAS_ANY_LAX; |
| 446 | particle->children = (xmlSchemaTreeItemPtr) wild; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 447 | /* |
| 448 | * Create the attribute wildcard. |
| 449 | */ |
| 450 | wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard)); |
| 451 | if (wild == NULL) { |
| 452 | xmlSchemaTypeErrMemory(NULL, "could not create an attribute " |
| 453 | "wildcard on anyType"); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | memset(wild, 0, sizeof(xmlSchemaWildcard)); |
| 457 | wild->any = 1; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 458 | wild->processContents = XML_SCHEMAS_ANY_LAX; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 459 | xmlSchemaTypeAnyTypeDef->attributeWildcard = wild; |
| 460 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 461 | xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType", |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 462 | XML_SCHEMAS_ANYSIMPLETYPE, |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 463 | xmlSchemaTypeAnyTypeDef); |
| 464 | /* |
| 465 | * primitive datatypes |
| 466 | */ |
| 467 | xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string", |
| 468 | XML_SCHEMAS_STRING, |
| 469 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 470 | xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 471 | XML_SCHEMAS_DECIMAL, |
| 472 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 473 | xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 474 | XML_SCHEMAS_DATE, |
| 475 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 476 | xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 477 | XML_SCHEMAS_DATETIME, |
| 478 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 479 | xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 480 | XML_SCHEMAS_TIME, |
| 481 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 482 | xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 483 | XML_SCHEMAS_GYEAR, |
| 484 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 485 | xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 486 | XML_SCHEMAS_GYEARMONTH, |
| 487 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 488 | xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 489 | XML_SCHEMAS_GMONTH, |
| 490 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 491 | xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 492 | XML_SCHEMAS_GMONTHDAY, |
| 493 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 494 | xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 495 | XML_SCHEMAS_GDAY, |
| 496 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 497 | xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 498 | XML_SCHEMAS_DURATION, |
| 499 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 500 | xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 501 | XML_SCHEMAS_FLOAT, |
| 502 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 503 | xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 504 | XML_SCHEMAS_DOUBLE, |
| 505 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 506 | xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 507 | XML_SCHEMAS_BOOLEAN, |
| 508 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 509 | xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 510 | XML_SCHEMAS_ANYURI, |
| 511 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 512 | xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 513 | XML_SCHEMAS_HEXBINARY, |
| 514 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 515 | xmlSchemaTypeBase64BinaryDef |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 516 | = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY, |
| 517 | xmlSchemaTypeAnySimpleTypeDef); |
| 518 | xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION", |
| 519 | XML_SCHEMAS_NOTATION, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 520 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 521 | xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName", |
| 522 | XML_SCHEMAS_QNAME, |
| 523 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 524 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 525 | /* |
| 526 | * derived datatypes |
| 527 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 528 | xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 529 | XML_SCHEMAS_INTEGER, |
| 530 | xmlSchemaTypeDecimalDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 531 | xmlSchemaTypeNonPositiveIntegerDef = |
| 532 | xmlSchemaInitBasicType("nonPositiveInteger", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 533 | XML_SCHEMAS_NPINTEGER, |
| 534 | xmlSchemaTypeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 535 | xmlSchemaTypeNegativeIntegerDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 536 | xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER, |
| 537 | xmlSchemaTypeNonPositiveIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 538 | xmlSchemaTypeLongDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 539 | xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG, |
| 540 | xmlSchemaTypeIntegerDef); |
| 541 | xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT, |
| 542 | xmlSchemaTypeLongDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 543 | xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 544 | XML_SCHEMAS_SHORT, |
| 545 | xmlSchemaTypeIntDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 546 | xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 547 | XML_SCHEMAS_BYTE, |
| 548 | xmlSchemaTypeShortDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 549 | xmlSchemaTypeNonNegativeIntegerDef = |
| 550 | xmlSchemaInitBasicType("nonNegativeInteger", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 551 | XML_SCHEMAS_NNINTEGER, |
| 552 | xmlSchemaTypeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 553 | xmlSchemaTypeUnsignedLongDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 554 | xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG, |
| 555 | xmlSchemaTypeNonNegativeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 556 | xmlSchemaTypeUnsignedIntDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 557 | xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT, |
| 558 | xmlSchemaTypeUnsignedLongDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 559 | xmlSchemaTypeUnsignedShortDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 560 | xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT, |
| 561 | xmlSchemaTypeUnsignedIntDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 562 | xmlSchemaTypeUnsignedByteDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 563 | xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE, |
| 564 | xmlSchemaTypeUnsignedShortDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 565 | xmlSchemaTypePositiveIntegerDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 566 | xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER, |
| 567 | xmlSchemaTypeNonNegativeIntegerDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 568 | xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 569 | XML_SCHEMAS_NORMSTRING, |
| 570 | xmlSchemaTypeStringDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 571 | xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 572 | XML_SCHEMAS_TOKEN, |
| 573 | xmlSchemaTypeNormStringDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 574 | xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 575 | XML_SCHEMAS_LANGUAGE, |
| 576 | xmlSchemaTypeTokenDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 577 | xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 578 | XML_SCHEMAS_NAME, |
| 579 | xmlSchemaTypeTokenDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 580 | xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 581 | XML_SCHEMAS_NMTOKEN, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 582 | xmlSchemaTypeTokenDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 583 | xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName", |
| 584 | XML_SCHEMAS_NCNAME, |
| 585 | xmlSchemaTypeNameDef); |
| 586 | xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 587 | xmlSchemaTypeNCNameDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 588 | xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF", |
| 589 | XML_SCHEMAS_IDREF, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 590 | xmlSchemaTypeNCNameDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 591 | xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY", |
| 592 | XML_SCHEMAS_ENTITY, |
| 593 | xmlSchemaTypeNCNameDef); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 594 | /* |
| 595 | * Derived list types. |
| 596 | */ |
| 597 | /* ENTITIES */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 598 | xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES", |
| 599 | XML_SCHEMAS_ENTITIES, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 600 | xmlSchemaTypeAnySimpleTypeDef); |
| 601 | xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef; |
| 602 | /* IDREFS */ |
| 603 | xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS", |
| 604 | XML_SCHEMAS_IDREFS, |
| 605 | xmlSchemaTypeAnySimpleTypeDef); |
| 606 | xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef; |
| 607 | |
| 608 | /* NMTOKENS */ |
| 609 | xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS", |
| 610 | XML_SCHEMAS_NMTOKENS, |
| 611 | xmlSchemaTypeAnySimpleTypeDef); |
| 612 | xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef; |
| 613 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 614 | xmlSchemaTypesInitialized = 1; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * xmlSchemaCleanupTypes: |
| 619 | * |
| 620 | * Cleanup the default XML Schemas type library |
| 621 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 622 | void |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 623 | xmlSchemaCleanupTypes(void) { |
| 624 | if (xmlSchemaTypesInitialized == 0) |
| 625 | return; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 626 | /* |
| 627 | * Free xs:anyType. |
| 628 | */ |
| 629 | { |
| 630 | xmlSchemaParticlePtr particle; |
| 631 | /* Attribute wildcard. */ |
| 632 | xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard); |
| 633 | /* Content type. */ |
| 634 | particle = (xmlSchemaParticlePtr) xmlSchemaTypeAnyTypeDef->subtypes; |
| 635 | /* Wildcard. */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 636 | xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 637 | particle->children->children->children); |
| 638 | xmlFree((xmlSchemaParticlePtr) particle->children->children); |
| 639 | /* Sequence model group. */ |
| 640 | xmlFree((xmlSchemaModelGroupPtr) particle->children); |
| 641 | xmlFree((xmlSchemaParticlePtr) particle); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 642 | xmlSchemaTypeAnyTypeDef->subtypes = NULL; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 643 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 644 | xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType); |
| 645 | xmlSchemaTypesInitialized = 0; |
| 646 | } |
| 647 | |
| 648 | /** |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 649 | * xmlSchemaIsBuiltInTypeFacet: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 650 | * @type: the built-in type |
| 651 | * @facetType: the facet type |
| 652 | * |
| 653 | * Evaluates if a specific facet can be |
| 654 | * used in conjunction with a type. |
| 655 | * |
| 656 | * Returns 1 if the facet can be used with the given built-in type, |
| 657 | * 0 otherwise and -1 in case the type is not a built-in type. |
| 658 | */ |
| 659 | int |
| 660 | xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType) |
| 661 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 662 | if (type == NULL) |
| 663 | return (-1); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 664 | if (type->type != XML_SCHEMA_TYPE_BASIC) |
| 665 | return (-1); |
| 666 | switch (type->builtInType) { |
| 667 | case XML_SCHEMAS_BOOLEAN: |
| 668 | if ((facetType == XML_SCHEMA_FACET_PATTERN) || |
| 669 | (facetType == XML_SCHEMA_FACET_WHITESPACE)) |
| 670 | return (1); |
| 671 | else |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 672 | return (0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 673 | case XML_SCHEMAS_STRING: |
| 674 | case XML_SCHEMAS_NOTATION: |
| 675 | case XML_SCHEMAS_QNAME: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 676 | case XML_SCHEMAS_ANYURI: |
| 677 | case XML_SCHEMAS_BASE64BINARY: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 678 | case XML_SCHEMAS_HEXBINARY: |
| 679 | if ((facetType == XML_SCHEMA_FACET_LENGTH) || |
| 680 | (facetType == XML_SCHEMA_FACET_MINLENGTH) || |
| 681 | (facetType == XML_SCHEMA_FACET_MAXLENGTH) || |
| 682 | (facetType == XML_SCHEMA_FACET_PATTERN) || |
| 683 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 684 | (facetType == XML_SCHEMA_FACET_WHITESPACE)) |
| 685 | return (1); |
| 686 | else |
| 687 | return (0); |
| 688 | case XML_SCHEMAS_DECIMAL: |
| 689 | if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) || |
| 690 | (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) || |
| 691 | (facetType == XML_SCHEMA_FACET_PATTERN) || |
| 692 | (facetType == XML_SCHEMA_FACET_WHITESPACE) || |
| 693 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 694 | (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) || |
| 695 | (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) || |
| 696 | (facetType == XML_SCHEMA_FACET_MININCLUSIVE) || |
| 697 | (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE)) |
| 698 | return (1); |
| 699 | else |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 700 | return (0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 701 | case XML_SCHEMAS_TIME: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 702 | case XML_SCHEMAS_GDAY: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 703 | case XML_SCHEMAS_GMONTH: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 704 | case XML_SCHEMAS_GMONTHDAY: |
| 705 | case XML_SCHEMAS_GYEAR: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 706 | case XML_SCHEMAS_GYEARMONTH: |
| 707 | case XML_SCHEMAS_DATE: |
| 708 | case XML_SCHEMAS_DATETIME: |
| 709 | case XML_SCHEMAS_DURATION: |
| 710 | case XML_SCHEMAS_FLOAT: |
| 711 | case XML_SCHEMAS_DOUBLE: |
| 712 | if ((facetType == XML_SCHEMA_FACET_PATTERN) || |
| 713 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 714 | (facetType == XML_SCHEMA_FACET_WHITESPACE) || |
| 715 | (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) || |
| 716 | (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) || |
| 717 | (facetType == XML_SCHEMA_FACET_MININCLUSIVE) || |
| 718 | (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE)) |
| 719 | return (1); |
| 720 | else |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 721 | return (0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 722 | default: |
Daniel Veillard | c7e3cc4 | 2004-09-28 12:33:52 +0000 | [diff] [blame] | 723 | break; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 724 | } |
| 725 | return (0); |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * xmlSchemaGetBuiltInType: |
| 730 | * @type: the type of the built in type |
| 731 | * |
| 732 | * Gives you the type struct for a built-in |
| 733 | * type by its type id. |
| 734 | * |
| 735 | * Returns the type if found, NULL otherwise. |
| 736 | */ |
| 737 | xmlSchemaTypePtr |
| 738 | xmlSchemaGetBuiltInType(xmlSchemaValType type) |
| 739 | { |
| 740 | if (xmlSchemaTypesInitialized == 0) |
| 741 | xmlSchemaInitTypes(); |
| 742 | switch (type) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 743 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 744 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 745 | return (xmlSchemaTypeAnySimpleTypeDef); |
| 746 | case XML_SCHEMAS_STRING: |
| 747 | return (xmlSchemaTypeStringDef); |
| 748 | case XML_SCHEMAS_NORMSTRING: |
| 749 | return (xmlSchemaTypeNormStringDef); |
| 750 | case XML_SCHEMAS_DECIMAL: |
| 751 | return (xmlSchemaTypeDecimalDef); |
| 752 | case XML_SCHEMAS_TIME: |
| 753 | return (xmlSchemaTypeTimeDef); |
| 754 | case XML_SCHEMAS_GDAY: |
| 755 | return (xmlSchemaTypeGDayDef); |
| 756 | case XML_SCHEMAS_GMONTH: |
| 757 | return (xmlSchemaTypeGMonthDef); |
| 758 | case XML_SCHEMAS_GMONTHDAY: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 759 | return (xmlSchemaTypeGMonthDayDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 760 | case XML_SCHEMAS_GYEAR: |
| 761 | return (xmlSchemaTypeGYearDef); |
| 762 | case XML_SCHEMAS_GYEARMONTH: |
| 763 | return (xmlSchemaTypeGYearMonthDef); |
| 764 | case XML_SCHEMAS_DATE: |
| 765 | return (xmlSchemaTypeDateDef); |
| 766 | case XML_SCHEMAS_DATETIME: |
| 767 | return (xmlSchemaTypeDatetimeDef); |
| 768 | case XML_SCHEMAS_DURATION: |
| 769 | return (xmlSchemaTypeDurationDef); |
| 770 | case XML_SCHEMAS_FLOAT: |
| 771 | return (xmlSchemaTypeFloatDef); |
| 772 | case XML_SCHEMAS_DOUBLE: |
| 773 | return (xmlSchemaTypeDoubleDef); |
| 774 | case XML_SCHEMAS_BOOLEAN: |
| 775 | return (xmlSchemaTypeBooleanDef); |
| 776 | case XML_SCHEMAS_TOKEN: |
| 777 | return (xmlSchemaTypeTokenDef); |
| 778 | case XML_SCHEMAS_LANGUAGE: |
| 779 | return (xmlSchemaTypeLanguageDef); |
| 780 | case XML_SCHEMAS_NMTOKEN: |
| 781 | return (xmlSchemaTypeNmtokenDef); |
| 782 | case XML_SCHEMAS_NMTOKENS: |
| 783 | return (xmlSchemaTypeNmtokensDef); |
| 784 | case XML_SCHEMAS_NAME: |
| 785 | return (xmlSchemaTypeNameDef); |
| 786 | case XML_SCHEMAS_QNAME: |
| 787 | return (xmlSchemaTypeQNameDef); |
| 788 | case XML_SCHEMAS_NCNAME: |
| 789 | return (xmlSchemaTypeNCNameDef); |
| 790 | case XML_SCHEMAS_ID: |
| 791 | return (xmlSchemaTypeIdDef); |
| 792 | case XML_SCHEMAS_IDREF: |
| 793 | return (xmlSchemaTypeIdrefDef); |
| 794 | case XML_SCHEMAS_IDREFS: |
| 795 | return (xmlSchemaTypeIdrefsDef); |
| 796 | case XML_SCHEMAS_ENTITY: |
| 797 | return (xmlSchemaTypeEntityDef); |
| 798 | case XML_SCHEMAS_ENTITIES: |
| 799 | return (xmlSchemaTypeEntitiesDef); |
| 800 | case XML_SCHEMAS_NOTATION: |
| 801 | return (xmlSchemaTypeNotationDef); |
| 802 | case XML_SCHEMAS_ANYURI: |
| 803 | return (xmlSchemaTypeAnyURIDef); |
| 804 | case XML_SCHEMAS_INTEGER: |
| 805 | return (xmlSchemaTypeIntegerDef); |
| 806 | case XML_SCHEMAS_NPINTEGER: |
| 807 | return (xmlSchemaTypeNonPositiveIntegerDef); |
| 808 | case XML_SCHEMAS_NINTEGER: |
| 809 | return (xmlSchemaTypeNegativeIntegerDef); |
| 810 | case XML_SCHEMAS_NNINTEGER: |
| 811 | return (xmlSchemaTypeNonNegativeIntegerDef); |
| 812 | case XML_SCHEMAS_PINTEGER: |
| 813 | return (xmlSchemaTypePositiveIntegerDef); |
| 814 | case XML_SCHEMAS_INT: |
| 815 | return (xmlSchemaTypeIntDef); |
| 816 | case XML_SCHEMAS_UINT: |
| 817 | return (xmlSchemaTypeUnsignedIntDef); |
| 818 | case XML_SCHEMAS_LONG: |
| 819 | return (xmlSchemaTypeLongDef); |
| 820 | case XML_SCHEMAS_ULONG: |
| 821 | return (xmlSchemaTypeUnsignedLongDef); |
| 822 | case XML_SCHEMAS_SHORT: |
| 823 | return (xmlSchemaTypeShortDef); |
| 824 | case XML_SCHEMAS_USHORT: |
| 825 | return (xmlSchemaTypeUnsignedShortDef); |
| 826 | case XML_SCHEMAS_BYTE: |
| 827 | return (xmlSchemaTypeByteDef); |
| 828 | case XML_SCHEMAS_UBYTE: |
| 829 | return (xmlSchemaTypeUnsignedByteDef); |
| 830 | case XML_SCHEMAS_HEXBINARY: |
| 831 | return (xmlSchemaTypeHexBinaryDef); |
| 832 | case XML_SCHEMAS_BASE64BINARY: |
| 833 | return (xmlSchemaTypeBase64BinaryDef); |
| 834 | case XML_SCHEMAS_ANYTYPE: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 835 | return (xmlSchemaTypeAnyTypeDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 836 | default: |
| 837 | return (NULL); |
| 838 | } |
| 839 | } |
| 840 | |
Kasimier T. Buchcik | 7f6e024 | 2005-06-15 13:36:10 +0000 | [diff] [blame] | 841 | /** |
| 842 | * xmlSchemaValueAppend: |
| 843 | * @prev: the value |
| 844 | * @cur: the value to be appended |
| 845 | * |
| 846 | * Appends a next sibling to a list of computed values. |
| 847 | * |
| 848 | * Returns 0 if succeeded and -1 on API errors. |
| 849 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 850 | int |
| 851 | xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 852 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 853 | if ((prev == NULL) || (cur == NULL)) |
| 854 | return (-1); |
| 855 | prev->next = cur; |
| 856 | return (0); |
| 857 | } |
| 858 | |
Kasimier T. Buchcik | 7f6e024 | 2005-06-15 13:36:10 +0000 | [diff] [blame] | 859 | /** |
| 860 | * xmlSchemaValueGetNext: |
| 861 | * @cur: the value |
| 862 | * |
| 863 | * Accessor for the next sibling of a list of computed values. |
| 864 | * |
| 865 | * Returns the next value or NULL if there was none, or on |
| 866 | * API errors. |
| 867 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 868 | xmlSchemaValPtr |
| 869 | xmlSchemaValueGetNext(xmlSchemaValPtr cur) { |
| 870 | |
| 871 | if (cur == NULL) |
| 872 | return (NULL); |
| 873 | return (cur->next); |
| 874 | } |
| 875 | |
Kasimier T. Buchcik | 7f6e024 | 2005-06-15 13:36:10 +0000 | [diff] [blame] | 876 | /** |
| 877 | * xmlSchemaValueGetAsString: |
| 878 | * @val: the value |
| 879 | * |
| 880 | * Accessor for the string value of a computed value. |
| 881 | * |
| 882 | * Returns the string value or NULL if there was none, or on |
| 883 | * API errors. |
| 884 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 885 | const xmlChar * |
| 886 | xmlSchemaValueGetAsString(xmlSchemaValPtr val) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 887 | { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 888 | if (val == NULL) |
| 889 | return (NULL); |
| 890 | switch (val->type) { |
| 891 | case XML_SCHEMAS_STRING: |
| 892 | case XML_SCHEMAS_NORMSTRING: |
| 893 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 894 | case XML_SCHEMAS_TOKEN: |
| 895 | case XML_SCHEMAS_LANGUAGE: |
| 896 | case XML_SCHEMAS_NMTOKEN: |
| 897 | case XML_SCHEMAS_NAME: |
| 898 | case XML_SCHEMAS_NCNAME: |
| 899 | case XML_SCHEMAS_ID: |
| 900 | case XML_SCHEMAS_IDREF: |
| 901 | case XML_SCHEMAS_ENTITY: |
| 902 | case XML_SCHEMAS_ANYURI: |
| 903 | return (BAD_CAST val->value.str); |
| 904 | default: |
| 905 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 906 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 907 | return (NULL); |
| 908 | } |
| 909 | |
Kasimier T. Buchcik | 7f6e024 | 2005-06-15 13:36:10 +0000 | [diff] [blame] | 910 | /** |
| 911 | * xmlSchemaValueGetAsBoolean: |
| 912 | * @val: the value |
| 913 | * |
| 914 | * Accessor for the boolean value of a computed value. |
| 915 | * |
| 916 | * Returns 1 if true and 0 if false, or in case of an error. Hmm. |
| 917 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 918 | int |
| 919 | xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 920 | { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 921 | if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN)) |
| 922 | return (0); |
| 923 | return (val->value.b); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 927 | * xmlSchemaNewStringValue: |
| 928 | * @type: the value type |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 929 | * @value: the value |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 930 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 931 | * Allocate a new simple type value. The type can be |
| 932 | * of XML_SCHEMAS_STRING. |
Kasimier T. Buchcik | 2ee855d | 2005-03-07 11:14:14 +0000 | [diff] [blame] | 933 | * WARNING: This one is intended to be expanded for other |
| 934 | * string based types. We need this for anySimpleType as well. |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 935 | * The given value is consumed and freed with the struct. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 936 | * |
| 937 | * Returns a pointer to the new value or NULL in case of error |
| 938 | */ |
| 939 | xmlSchemaValPtr |
| 940 | xmlSchemaNewStringValue(xmlSchemaValType type, |
| 941 | const xmlChar *value) |
| 942 | { |
| 943 | xmlSchemaValPtr val; |
| 944 | |
| 945 | if (type != XML_SCHEMAS_STRING) |
| 946 | return(NULL); |
| 947 | val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal)); |
| 948 | if (val == NULL) { |
| 949 | return(NULL); |
| 950 | } |
| 951 | memset(val, 0, sizeof(xmlSchemaVal)); |
| 952 | val->type = type; |
| 953 | val->value.str = (xmlChar *) value; |
| 954 | return(val); |
| 955 | } |
| 956 | |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 957 | /** |
| 958 | * xmlSchemaNewNOTATIONValue: |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 959 | * @name: the notation name |
| 960 | * @ns: the notation namespace name or NULL |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 961 | * |
| 962 | * Allocate a new NOTATION value. |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 963 | * The given values are consumed and freed with the struct. |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 964 | * |
| 965 | * Returns a pointer to the new value or NULL in case of error |
| 966 | */ |
| 967 | xmlSchemaValPtr |
| 968 | xmlSchemaNewNOTATIONValue(const xmlChar *name, |
| 969 | const xmlChar *ns) |
| 970 | { |
| 971 | xmlSchemaValPtr val; |
| 972 | |
| 973 | val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION); |
| 974 | if (val == NULL) |
| 975 | return (NULL); |
| 976 | |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 977 | val->value.qname.name = (xmlChar *)name; |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 978 | if (ns != NULL) |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 979 | val->value.qname.uri = (xmlChar *)ns; |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 980 | return(val); |
| 981 | } |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 982 | |
| 983 | /** |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 984 | * xmlSchemaNewQNameValue: |
| 985 | * @namespaceName: the namespace name |
| 986 | * @localName: the local name |
| 987 | * |
| 988 | * Allocate a new QName value. |
| 989 | * The given values are consumed and freed with the struct. |
| 990 | * |
| 991 | * Returns a pointer to the new value or NULL in case of an error. |
| 992 | */ |
| 993 | xmlSchemaValPtr |
| 994 | xmlSchemaNewQNameValue(const xmlChar *namespaceName, |
| 995 | const xmlChar *localName) |
| 996 | { |
| 997 | xmlSchemaValPtr val; |
| 998 | |
| 999 | val = xmlSchemaNewValue(XML_SCHEMAS_QNAME); |
| 1000 | if (val == NULL) |
| 1001 | return (NULL); |
| 1002 | |
| 1003 | val->value.qname.name = (xmlChar *) localName; |
| 1004 | val->value.qname.uri = (xmlChar *) namespaceName; |
| 1005 | return(val); |
| 1006 | } |
| 1007 | |
| 1008 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1009 | * xmlSchemaFreeValue: |
| 1010 | * @value: the value to free |
| 1011 | * |
| 1012 | * Cleanup the default XML Schemas type library |
| 1013 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1014 | void |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1015 | xmlSchemaFreeValue(xmlSchemaValPtr value) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1016 | xmlSchemaValPtr prev; |
| 1017 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1018 | while (value != NULL) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1019 | switch (value->type) { |
| 1020 | case XML_SCHEMAS_STRING: |
| 1021 | case XML_SCHEMAS_NORMSTRING: |
| 1022 | case XML_SCHEMAS_TOKEN: |
| 1023 | case XML_SCHEMAS_LANGUAGE: |
| 1024 | case XML_SCHEMAS_NMTOKEN: |
| 1025 | case XML_SCHEMAS_NMTOKENS: |
| 1026 | case XML_SCHEMAS_NAME: |
| 1027 | case XML_SCHEMAS_NCNAME: |
| 1028 | case XML_SCHEMAS_ID: |
| 1029 | case XML_SCHEMAS_IDREF: |
| 1030 | case XML_SCHEMAS_IDREFS: |
| 1031 | case XML_SCHEMAS_ENTITY: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1032 | case XML_SCHEMAS_ENTITIES: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1033 | case XML_SCHEMAS_ANYURI: |
| 1034 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 1035 | if (value->value.str != NULL) |
| 1036 | xmlFree(value->value.str); |
| 1037 | break; |
| 1038 | case XML_SCHEMAS_NOTATION: |
| 1039 | case XML_SCHEMAS_QNAME: |
| 1040 | if (value->value.qname.uri != NULL) |
| 1041 | xmlFree(value->value.qname.uri); |
| 1042 | if (value->value.qname.name != NULL) |
| 1043 | xmlFree(value->value.qname.name); |
| 1044 | break; |
| 1045 | case XML_SCHEMAS_HEXBINARY: |
| 1046 | if (value->value.hex.str != NULL) |
| 1047 | xmlFree(value->value.hex.str); |
| 1048 | break; |
| 1049 | case XML_SCHEMAS_BASE64BINARY: |
| 1050 | if (value->value.base64.str != NULL) |
| 1051 | xmlFree(value->value.base64.str); |
| 1052 | break; |
| 1053 | default: |
| 1054 | break; |
| 1055 | } |
| 1056 | prev = value; |
| 1057 | value = value->next; |
| 1058 | xmlFree(prev); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1059 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * xmlSchemaGetPredefinedType: |
| 1064 | * @name: the type name |
| 1065 | * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema" |
| 1066 | * |
| 1067 | * Lookup a type in the default XML Schemas type library |
| 1068 | * |
| 1069 | * Returns the type if found, NULL otherwise |
| 1070 | */ |
| 1071 | xmlSchemaTypePtr |
| 1072 | xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) { |
| 1073 | if (xmlSchemaTypesInitialized == 0) |
| 1074 | xmlSchemaInitTypes(); |
| 1075 | if (name == NULL) |
| 1076 | return(NULL); |
| 1077 | return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns)); |
| 1078 | } |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1079 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1080 | /** |
| 1081 | * xmlSchemaGetBuiltInListSimpleTypeItemType: |
| 1082 | * @type: the built-in simple type. |
| 1083 | * |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 1084 | * Lookup function |
| 1085 | * |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 1086 | * Returns the item type of @type as defined by the built-in datatype |
| 1087 | * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error. |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1088 | */ |
| 1089 | xmlSchemaTypePtr |
| 1090 | xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type) |
| 1091 | { |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 1092 | if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC)) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1093 | return (NULL); |
| 1094 | switch (type->builtInType) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1095 | case XML_SCHEMAS_NMTOKENS: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1096 | return (xmlSchemaTypeNmtokenDef ); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1097 | case XML_SCHEMAS_IDREFS: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1098 | return (xmlSchemaTypeIdrefDef); |
| 1099 | case XML_SCHEMAS_ENTITIES: |
| 1100 | return (xmlSchemaTypeEntityDef); |
| 1101 | default: |
| 1102 | return (NULL); |
| 1103 | } |
| 1104 | } |
| 1105 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1106 | /**************************************************************** |
| 1107 | * * |
| 1108 | * Convenience macros and functions * |
| 1109 | * * |
| 1110 | ****************************************************************/ |
| 1111 | |
| 1112 | #define IS_TZO_CHAR(c) \ |
| 1113 | ((c == 0) || (c == 'Z') || (c == '+') || (c == '-')) |
| 1114 | |
| 1115 | #define VALID_YEAR(yr) (yr != 0) |
| 1116 | #define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12)) |
| 1117 | /* VALID_DAY should only be used when month is unknown */ |
| 1118 | #define VALID_DAY(day) ((day >= 1) && (day <= 31)) |
| 1119 | #define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23)) |
| 1120 | #define VALID_MIN(min) ((min >= 0) && (min <= 59)) |
| 1121 | #define VALID_SEC(sec) ((sec >= 0) && (sec < 60)) |
Kasimier T. Buchcik | 690a680 | 2005-05-12 13:16:01 +0000 | [diff] [blame] | 1122 | #define VALID_TZO(tzo) ((tzo > -840) && (tzo < 840)) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1123 | #define IS_LEAP(y) \ |
| 1124 | (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) |
| 1125 | |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 1126 | static const unsigned int daysInMonth[12] = |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1127 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 1128 | static const unsigned int daysInMonthLeap[12] = |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1129 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| 1130 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1131 | #define MAX_DAYINMONTH(yr,mon) \ |
| 1132 | (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1]) |
| 1133 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1134 | #define VALID_MDAY(dt) \ |
| 1135 | (IS_LEAP(dt->year) ? \ |
| 1136 | (dt->day <= daysInMonthLeap[dt->mon - 1]) : \ |
| 1137 | (dt->day <= daysInMonth[dt->mon - 1])) |
| 1138 | |
| 1139 | #define VALID_DATE(dt) \ |
| 1140 | (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt)) |
| 1141 | |
| 1142 | #define VALID_TIME(dt) \ |
| 1143 | (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \ |
| 1144 | VALID_SEC(dt->sec) && VALID_TZO(dt->tzo)) |
| 1145 | |
| 1146 | #define VALID_DATETIME(dt) \ |
| 1147 | (VALID_DATE(dt) && VALID_TIME(dt)) |
| 1148 | |
| 1149 | #define SECS_PER_MIN (60) |
| 1150 | #define SECS_PER_HOUR (60 * SECS_PER_MIN) |
| 1151 | #define SECS_PER_DAY (24 * SECS_PER_HOUR) |
| 1152 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1153 | static const long dayInYearByMonth[12] = |
| 1154 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; |
| 1155 | static const long dayInLeapYearByMonth[12] = |
| 1156 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; |
| 1157 | |
| 1158 | #define DAY_IN_YEAR(day, month, year) \ |
| 1159 | ((IS_LEAP(year) ? \ |
| 1160 | dayInLeapYearByMonth[month - 1] : \ |
| 1161 | dayInYearByMonth[month - 1]) + day) |
| 1162 | |
| 1163 | #ifdef DEBUG |
| 1164 | #define DEBUG_DATE(dt) \ |
| 1165 | xmlGenericError(xmlGenericErrorContext, \ |
| 1166 | "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \ |
| 1167 | dt->type,dt->value.date.year,dt->value.date.mon, \ |
| 1168 | dt->value.date.day,dt->value.date.hour,dt->value.date.min, \ |
| 1169 | dt->value.date.sec); \ |
| 1170 | if (dt->value.date.tz_flag) \ |
| 1171 | if (dt->value.date.tzo != 0) \ |
| 1172 | xmlGenericError(xmlGenericErrorContext, \ |
| 1173 | "%+05d\n",dt->value.date.tzo); \ |
| 1174 | else \ |
| 1175 | xmlGenericError(xmlGenericErrorContext, "Z\n"); \ |
| 1176 | else \ |
| 1177 | xmlGenericError(xmlGenericErrorContext,"\n") |
| 1178 | #else |
| 1179 | #define DEBUG_DATE(dt) |
| 1180 | #endif |
| 1181 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1182 | /** |
| 1183 | * _xmlSchemaParseGYear: |
| 1184 | * @dt: pointer to a date structure |
| 1185 | * @str: pointer to the string to analyze |
| 1186 | * |
| 1187 | * Parses a xs:gYear without time zone and fills in the appropriate |
| 1188 | * field of the @dt structure. @str is updated to point just after the |
| 1189 | * xs:gYear. It is supposed that @dt->year is big enough to contain |
| 1190 | * the year. |
| 1191 | * |
| 1192 | * Returns 0 or the error code |
| 1193 | */ |
| 1194 | static int |
| 1195 | _xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1196 | const xmlChar *cur = *str, *firstChar; |
| 1197 | int isneg = 0, digcnt = 0; |
| 1198 | |
| 1199 | if (((*cur < '0') || (*cur > '9')) && |
| 1200 | (*cur != '-') && (*cur != '+')) |
| 1201 | return -1; |
| 1202 | |
| 1203 | if (*cur == '-') { |
| 1204 | isneg = 1; |
| 1205 | cur++; |
| 1206 | } |
| 1207 | |
| 1208 | firstChar = cur; |
| 1209 | |
| 1210 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1211 | dt->year = dt->year * 10 + (*cur - '0'); |
| 1212 | cur++; |
| 1213 | digcnt++; |
| 1214 | } |
| 1215 | |
| 1216 | /* year must be at least 4 digits (CCYY); over 4 |
| 1217 | * digits cannot have a leading zero. */ |
| 1218 | if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0'))) |
| 1219 | return 1; |
| 1220 | |
| 1221 | if (isneg) |
| 1222 | dt->year = - dt->year; |
| 1223 | |
| 1224 | if (!VALID_YEAR(dt->year)) |
| 1225 | return 2; |
| 1226 | |
| 1227 | *str = cur; |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * PARSE_2_DIGITS: |
| 1233 | * @num: the integer to fill in |
| 1234 | * @cur: an #xmlChar * |
| 1235 | * @invalid: an integer |
| 1236 | * |
| 1237 | * Parses a 2-digits integer and updates @num with the value. @cur is |
| 1238 | * updated to point just after the integer. |
| 1239 | * In case of error, @invalid is set to %TRUE, values of @num and |
| 1240 | * @cur are undefined. |
| 1241 | */ |
| 1242 | #define PARSE_2_DIGITS(num, cur, invalid) \ |
| 1243 | if ((cur[0] < '0') || (cur[0] > '9') || \ |
| 1244 | (cur[1] < '0') || (cur[1] > '9')) \ |
| 1245 | invalid = 1; \ |
| 1246 | else \ |
| 1247 | num = (cur[0] - '0') * 10 + (cur[1] - '0'); \ |
| 1248 | cur += 2; |
| 1249 | |
| 1250 | /** |
| 1251 | * PARSE_FLOAT: |
| 1252 | * @num: the double to fill in |
| 1253 | * @cur: an #xmlChar * |
| 1254 | * @invalid: an integer |
| 1255 | * |
| 1256 | * Parses a float and updates @num with the value. @cur is |
| 1257 | * updated to point just after the float. The float must have a |
| 1258 | * 2-digits integer part and may or may not have a decimal part. |
| 1259 | * In case of error, @invalid is set to %TRUE, values of @num and |
| 1260 | * @cur are undefined. |
| 1261 | */ |
| 1262 | #define PARSE_FLOAT(num, cur, invalid) \ |
| 1263 | PARSE_2_DIGITS(num, cur, invalid); \ |
| 1264 | if (!invalid && (*cur == '.')) { \ |
| 1265 | double mult = 1; \ |
| 1266 | cur++; \ |
| 1267 | if ((*cur < '0') || (*cur > '9')) \ |
| 1268 | invalid = 1; \ |
| 1269 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 1270 | mult /= 10; \ |
| 1271 | num += (*cur - '0') * mult; \ |
| 1272 | cur++; \ |
| 1273 | } \ |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * _xmlSchemaParseGMonth: |
| 1278 | * @dt: pointer to a date structure |
| 1279 | * @str: pointer to the string to analyze |
| 1280 | * |
| 1281 | * Parses a xs:gMonth without time zone and fills in the appropriate |
| 1282 | * field of the @dt structure. @str is updated to point just after the |
| 1283 | * xs:gMonth. |
| 1284 | * |
| 1285 | * Returns 0 or the error code |
| 1286 | */ |
| 1287 | static int |
| 1288 | _xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1289 | const xmlChar *cur = *str; |
| 1290 | int ret = 0; |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1291 | unsigned int value = 0; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1292 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1293 | PARSE_2_DIGITS(value, cur, ret); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1294 | if (ret != 0) |
| 1295 | return ret; |
| 1296 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1297 | if (!VALID_MONTH(value)) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1298 | return 2; |
| 1299 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1300 | dt->mon = value; |
| 1301 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1302 | *str = cur; |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | /** |
| 1307 | * _xmlSchemaParseGDay: |
| 1308 | * @dt: pointer to a date structure |
| 1309 | * @str: pointer to the string to analyze |
| 1310 | * |
| 1311 | * Parses a xs:gDay without time zone and fills in the appropriate |
| 1312 | * field of the @dt structure. @str is updated to point just after the |
| 1313 | * xs:gDay. |
| 1314 | * |
| 1315 | * Returns 0 or the error code |
| 1316 | */ |
| 1317 | static int |
| 1318 | _xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1319 | const xmlChar *cur = *str; |
| 1320 | int ret = 0; |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1321 | unsigned int value = 0; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1322 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1323 | PARSE_2_DIGITS(value, cur, ret); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1324 | if (ret != 0) |
| 1325 | return ret; |
| 1326 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1327 | if (!VALID_DAY(value)) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1328 | return 2; |
| 1329 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1330 | dt->day = value; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1331 | *str = cur; |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * _xmlSchemaParseTime: |
| 1337 | * @dt: pointer to a date structure |
| 1338 | * @str: pointer to the string to analyze |
| 1339 | * |
| 1340 | * Parses a xs:time without time zone and fills in the appropriate |
| 1341 | * fields of the @dt structure. @str is updated to point just after the |
| 1342 | * xs:time. |
| 1343 | * In case of error, values of @dt fields are undefined. |
| 1344 | * |
| 1345 | * Returns 0 or the error code |
| 1346 | */ |
| 1347 | static int |
| 1348 | _xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1349 | const xmlChar *cur = *str; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1350 | int ret = 0; |
Kasimier T. Buchcik | 285b367 | 2005-05-12 13:10:22 +0000 | [diff] [blame] | 1351 | int value = 0; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1352 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1353 | PARSE_2_DIGITS(value, cur, ret); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1354 | if (ret != 0) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1355 | return ret; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1356 | if (*cur != ':') |
| 1357 | return 1; |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1358 | if (!VALID_HOUR(value)) |
| 1359 | return 2; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1360 | cur++; |
| 1361 | |
| 1362 | /* the ':' insures this string is xs:time */ |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1363 | dt->hour = value; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1364 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1365 | PARSE_2_DIGITS(value, cur, ret); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1366 | if (ret != 0) |
| 1367 | return ret; |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1368 | if (!VALID_MIN(value)) |
| 1369 | return 2; |
| 1370 | dt->min = value; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (*cur != ':') |
| 1373 | return 1; |
| 1374 | cur++; |
| 1375 | |
| 1376 | PARSE_FLOAT(dt->sec, cur, ret); |
| 1377 | if (ret != 0) |
| 1378 | return ret; |
| 1379 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1380 | if ((!VALID_SEC(dt->sec)) || (!VALID_TZO(dt->tzo))) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1381 | return 2; |
| 1382 | |
| 1383 | *str = cur; |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * _xmlSchemaParseTimeZone: |
| 1389 | * @dt: pointer to a date structure |
| 1390 | * @str: pointer to the string to analyze |
| 1391 | * |
| 1392 | * Parses a time zone without time zone and fills in the appropriate |
| 1393 | * field of the @dt structure. @str is updated to point just after the |
| 1394 | * time zone. |
| 1395 | * |
| 1396 | * Returns 0 or the error code |
| 1397 | */ |
| 1398 | static int |
| 1399 | _xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 1400 | const xmlChar *cur; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1401 | int ret = 0; |
| 1402 | |
| 1403 | if (str == NULL) |
| 1404 | return -1; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 1405 | cur = *str; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1406 | |
| 1407 | switch (*cur) { |
| 1408 | case 0: |
| 1409 | dt->tz_flag = 0; |
| 1410 | dt->tzo = 0; |
| 1411 | break; |
| 1412 | |
| 1413 | case 'Z': |
| 1414 | dt->tz_flag = 1; |
| 1415 | dt->tzo = 0; |
| 1416 | cur++; |
| 1417 | break; |
| 1418 | |
| 1419 | case '+': |
| 1420 | case '-': { |
| 1421 | int isneg = 0, tmp = 0; |
| 1422 | isneg = (*cur == '-'); |
| 1423 | |
| 1424 | cur++; |
| 1425 | |
| 1426 | PARSE_2_DIGITS(tmp, cur, ret); |
| 1427 | if (ret != 0) |
| 1428 | return ret; |
| 1429 | if (!VALID_HOUR(tmp)) |
| 1430 | return 2; |
| 1431 | |
| 1432 | if (*cur != ':') |
| 1433 | return 1; |
| 1434 | cur++; |
| 1435 | |
| 1436 | dt->tzo = tmp * 60; |
| 1437 | |
| 1438 | PARSE_2_DIGITS(tmp, cur, ret); |
| 1439 | if (ret != 0) |
| 1440 | return ret; |
| 1441 | if (!VALID_MIN(tmp)) |
| 1442 | return 2; |
| 1443 | |
| 1444 | dt->tzo += tmp; |
| 1445 | if (isneg) |
| 1446 | dt->tzo = - dt->tzo; |
| 1447 | |
| 1448 | if (!VALID_TZO(dt->tzo)) |
| 1449 | return 2; |
| 1450 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1451 | dt->tz_flag = 1; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1452 | break; |
| 1453 | } |
| 1454 | default: |
| 1455 | return 1; |
| 1456 | } |
| 1457 | |
| 1458 | *str = cur; |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1462 | /** |
| 1463 | * _xmlSchemaBase64Decode: |
| 1464 | * @ch: a character |
| 1465 | * |
| 1466 | * Converts a base64 encoded character to its base 64 value. |
| 1467 | * |
| 1468 | * Returns 0-63 (value), 64 (pad), or -1 (not recognized) |
| 1469 | */ |
| 1470 | static int |
| 1471 | _xmlSchemaBase64Decode (const xmlChar ch) { |
| 1472 | if (('A' <= ch) && (ch <= 'Z')) return ch - 'A'; |
| 1473 | if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26; |
| 1474 | if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52; |
| 1475 | if ('+' == ch) return 62; |
| 1476 | if ('/' == ch) return 63; |
| 1477 | if ('=' == ch) return 64; |
| 1478 | return -1; |
| 1479 | } |
| 1480 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1481 | /**************************************************************** |
| 1482 | * * |
| 1483 | * XML Schema Dates/Times Datatypes Handling * |
| 1484 | * * |
| 1485 | ****************************************************************/ |
| 1486 | |
| 1487 | /** |
| 1488 | * PARSE_DIGITS: |
| 1489 | * @num: the integer to fill in |
| 1490 | * @cur: an #xmlChar * |
| 1491 | * @num_type: an integer flag |
| 1492 | * |
| 1493 | * Parses a digits integer and updates @num with the value. @cur is |
| 1494 | * updated to point just after the integer. |
| 1495 | * In case of error, @num_type is set to -1, values of @num and |
| 1496 | * @cur are undefined. |
| 1497 | */ |
| 1498 | #define PARSE_DIGITS(num, cur, num_type) \ |
| 1499 | if ((*cur < '0') || (*cur > '9')) \ |
| 1500 | num_type = -1; \ |
| 1501 | else \ |
| 1502 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 1503 | num = num * 10 + (*cur - '0'); \ |
| 1504 | cur++; \ |
| 1505 | } |
| 1506 | |
| 1507 | /** |
| 1508 | * PARSE_NUM: |
| 1509 | * @num: the double to fill in |
| 1510 | * @cur: an #xmlChar * |
| 1511 | * @num_type: an integer flag |
| 1512 | * |
| 1513 | * Parses a float or integer and updates @num with the value. @cur is |
| 1514 | * updated to point just after the number. If the number is a float, |
| 1515 | * then it must have an integer part and a decimal part; @num_type will |
| 1516 | * be set to 1. If there is no decimal part, @num_type is set to zero. |
| 1517 | * In case of error, @num_type is set to -1, values of @num and |
| 1518 | * @cur are undefined. |
| 1519 | */ |
| 1520 | #define PARSE_NUM(num, cur, num_type) \ |
| 1521 | num = 0; \ |
| 1522 | PARSE_DIGITS(num, cur, num_type); \ |
| 1523 | if (!num_type && (*cur == '.')) { \ |
| 1524 | double mult = 1; \ |
| 1525 | cur++; \ |
| 1526 | if ((*cur < '0') || (*cur > '9')) \ |
| 1527 | num_type = -1; \ |
| 1528 | else \ |
| 1529 | num_type = 1; \ |
| 1530 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 1531 | mult /= 10; \ |
| 1532 | num += (*cur - '0') * mult; \ |
| 1533 | cur++; \ |
| 1534 | } \ |
| 1535 | } |
| 1536 | |
| 1537 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1538 | * xmlSchemaValidateDates: |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1539 | * @type: the expected type or XML_SCHEMAS_UNKNOWN |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1540 | * @dateTime: string to analyze |
| 1541 | * @val: the return computed value |
| 1542 | * |
| 1543 | * Check that @dateTime conforms to the lexical space of one of the date types. |
| 1544 | * if true a value is computed and returned in @val. |
| 1545 | * |
| 1546 | * Returns 0 if this validates, a positive error code number otherwise |
| 1547 | * and -1 in case of internal or API error. |
| 1548 | */ |
| 1549 | static int |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1550 | xmlSchemaValidateDates (xmlSchemaValType type, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1551 | const xmlChar *dateTime, xmlSchemaValPtr *val, |
| 1552 | int collapse) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1553 | xmlSchemaValPtr dt; |
| 1554 | int ret; |
| 1555 | const xmlChar *cur = dateTime; |
| 1556 | |
| 1557 | #define RETURN_TYPE_IF_VALID(t) \ |
| 1558 | if (IS_TZO_CHAR(*cur)) { \ |
| 1559 | ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \ |
| 1560 | if (ret == 0) { \ |
| 1561 | if (*cur != 0) \ |
| 1562 | goto error; \ |
| 1563 | dt->type = t; \ |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1564 | goto done; \ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1565 | } \ |
| 1566 | } |
| 1567 | |
| 1568 | if (dateTime == NULL) |
| 1569 | return -1; |
| 1570 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1571 | if (collapse) |
| 1572 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 1573 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1574 | if ((*cur != '-') && (*cur < '0') && (*cur > '9')) |
| 1575 | return 1; |
| 1576 | |
| 1577 | dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN); |
| 1578 | if (dt == NULL) |
| 1579 | return -1; |
| 1580 | |
| 1581 | if ((cur[0] == '-') && (cur[1] == '-')) { |
| 1582 | /* |
| 1583 | * It's an incomplete date (xs:gMonthDay, xs:gMonth or |
| 1584 | * xs:gDay) |
| 1585 | */ |
| 1586 | cur += 2; |
| 1587 | |
| 1588 | /* is it an xs:gDay? */ |
| 1589 | if (*cur == '-') { |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1590 | if (type == XML_SCHEMAS_GMONTH) |
| 1591 | goto error; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1592 | ++cur; |
| 1593 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 1594 | if (ret != 0) |
| 1595 | goto error; |
| 1596 | |
| 1597 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY); |
| 1598 | |
| 1599 | goto error; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
| 1603 | * it should be an xs:gMonthDay or xs:gMonth |
| 1604 | */ |
| 1605 | ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur); |
| 1606 | if (ret != 0) |
| 1607 | goto error; |
| 1608 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1609 | /* |
| 1610 | * a '-' char could indicate this type is xs:gMonthDay or |
| 1611 | * a negative time zone offset. Check for xs:gMonthDay first. |
| 1612 | * Also the first three char's of a negative tzo (-MM:SS) can |
| 1613 | * appear to be a valid day; so even if the day portion |
| 1614 | * of the xs:gMonthDay verifies, we must insure it was not |
| 1615 | * a tzo. |
| 1616 | */ |
| 1617 | if (*cur == '-') { |
| 1618 | const xmlChar *rewnd = cur; |
| 1619 | cur++; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1620 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1621 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1622 | if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) { |
| 1623 | |
| 1624 | /* |
| 1625 | * we can use the VALID_MDAY macro to validate the month |
| 1626 | * and day because the leap year test will flag year zero |
| 1627 | * as a leap year (even though zero is an invalid year). |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1628 | * FUTURE TODO: Zero will become valid in XML Schema 1.1 |
| 1629 | * probably. |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1630 | */ |
| 1631 | if (VALID_MDAY((&(dt->value.date)))) { |
| 1632 | |
| 1633 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY); |
| 1634 | |
| 1635 | goto error; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * not xs:gMonthDay so rewind and check if just xs:gMonth |
| 1641 | * with an optional time zone. |
| 1642 | */ |
| 1643 | cur = rewnd; |
| 1644 | } |
| 1645 | |
| 1646 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1647 | |
| 1648 | goto error; |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * It's a right-truncated date or an xs:time. |
| 1653 | * Try to parse an xs:time then fallback on right-truncated dates. |
| 1654 | */ |
| 1655 | if ((*cur >= '0') && (*cur <= '9')) { |
| 1656 | ret = _xmlSchemaParseTime(&(dt->value.date), &cur); |
| 1657 | if (ret == 0) { |
| 1658 | /* it's an xs:time */ |
| 1659 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME); |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | /* fallback on date parsing */ |
| 1664 | cur = dateTime; |
| 1665 | |
| 1666 | ret = _xmlSchemaParseGYear(&(dt->value.date), &cur); |
| 1667 | if (ret != 0) |
| 1668 | goto error; |
| 1669 | |
| 1670 | /* is it an xs:gYear? */ |
| 1671 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR); |
| 1672 | |
| 1673 | if (*cur != '-') |
| 1674 | goto error; |
| 1675 | cur++; |
| 1676 | |
| 1677 | ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur); |
| 1678 | if (ret != 0) |
| 1679 | goto error; |
| 1680 | |
| 1681 | /* is it an xs:gYearMonth? */ |
| 1682 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH); |
| 1683 | |
| 1684 | if (*cur != '-') |
| 1685 | goto error; |
| 1686 | cur++; |
| 1687 | |
| 1688 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 1689 | if ((ret != 0) || !VALID_DATE((&(dt->value.date)))) |
| 1690 | goto error; |
| 1691 | |
| 1692 | /* is it an xs:date? */ |
| 1693 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE); |
| 1694 | |
| 1695 | if (*cur != 'T') |
| 1696 | goto error; |
| 1697 | cur++; |
| 1698 | |
| 1699 | /* it should be an xs:dateTime */ |
| 1700 | ret = _xmlSchemaParseTime(&(dt->value.date), &cur); |
| 1701 | if (ret != 0) |
| 1702 | goto error; |
| 1703 | |
| 1704 | ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1705 | if (collapse) |
| 1706 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 6a0baa0 | 2005-12-10 11:11:12 +0000 | [diff] [blame] | 1707 | if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date)))))) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1708 | goto error; |
| 1709 | |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1710 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1711 | dt->type = XML_SCHEMAS_DATETIME; |
| 1712 | |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1713 | done: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1714 | #if 1 |
| 1715 | if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) |
| 1716 | goto error; |
| 1717 | #else |
| 1718 | /* |
| 1719 | * insure the parsed type is equal to or less significant (right |
| 1720 | * truncated) than the desired type. |
| 1721 | */ |
| 1722 | if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) { |
| 1723 | |
| 1724 | /* time only matches time */ |
| 1725 | if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME)) |
| 1726 | goto error; |
| 1727 | |
| 1728 | if ((type == XML_SCHEMAS_DATETIME) && |
| 1729 | ((dt->type != XML_SCHEMAS_DATE) || |
| 1730 | (dt->type != XML_SCHEMAS_GYEARMONTH) || |
| 1731 | (dt->type != XML_SCHEMAS_GYEAR))) |
| 1732 | goto error; |
| 1733 | |
| 1734 | if ((type == XML_SCHEMAS_DATE) && |
| 1735 | ((dt->type != XML_SCHEMAS_GYEAR) || |
| 1736 | (dt->type != XML_SCHEMAS_GYEARMONTH))) |
| 1737 | goto error; |
| 1738 | |
| 1739 | if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR)) |
| 1740 | goto error; |
| 1741 | |
| 1742 | if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH)) |
| 1743 | goto error; |
| 1744 | } |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1745 | #endif |
| 1746 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1747 | if (val != NULL) |
| 1748 | *val = dt; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1749 | else |
| 1750 | xmlSchemaFreeValue(dt); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1751 | |
| 1752 | return 0; |
| 1753 | |
| 1754 | error: |
| 1755 | if (dt != NULL) |
| 1756 | xmlSchemaFreeValue(dt); |
| 1757 | return 1; |
| 1758 | } |
| 1759 | |
| 1760 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1761 | * xmlSchemaValidateDuration: |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1762 | * @type: the predefined type |
| 1763 | * @duration: string to analyze |
| 1764 | * @val: the return computed value |
| 1765 | * |
| 1766 | * Check that @duration conforms to the lexical space of the duration type. |
| 1767 | * if true a value is computed and returned in @val. |
| 1768 | * |
| 1769 | * Returns 0 if this validates, a positive error code number otherwise |
| 1770 | * and -1 in case of internal or API error. |
| 1771 | */ |
| 1772 | static int |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 1773 | xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1774 | const xmlChar *duration, xmlSchemaValPtr *val, |
| 1775 | int collapse) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1776 | const xmlChar *cur = duration; |
| 1777 | xmlSchemaValPtr dur; |
| 1778 | int isneg = 0; |
| 1779 | unsigned int seq = 0; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1780 | double num; |
| 1781 | int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */ |
| 1782 | const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'}; |
| 1783 | const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0}; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1784 | |
| 1785 | if (duration == NULL) |
| 1786 | return -1; |
| 1787 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1788 | if (collapse) |
| 1789 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 1790 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1791 | if (*cur == '-') { |
| 1792 | isneg = 1; |
| 1793 | cur++; |
| 1794 | } |
| 1795 | |
| 1796 | /* duration must start with 'P' (after sign) */ |
| 1797 | if (*cur++ != 'P') |
| 1798 | return 1; |
| 1799 | |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1800 | if (*cur == 0) |
| 1801 | return 1; |
| 1802 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1803 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 1804 | if (dur == NULL) |
| 1805 | return -1; |
| 1806 | |
| 1807 | while (*cur != 0) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1808 | |
| 1809 | /* input string should be empty or invalid date/time item */ |
| 1810 | if (seq >= sizeof(desig)) |
| 1811 | goto error; |
| 1812 | |
| 1813 | /* T designator must be present for time items */ |
| 1814 | if (*cur == 'T') { |
| 1815 | if (seq <= 3) { |
| 1816 | seq = 3; |
| 1817 | cur++; |
| 1818 | } else |
| 1819 | return 1; |
| 1820 | } else if (seq == 3) |
| 1821 | goto error; |
| 1822 | |
| 1823 | /* parse the number portion of the item */ |
| 1824 | PARSE_NUM(num, cur, num_type); |
| 1825 | |
| 1826 | if ((num_type == -1) || (*cur == 0)) |
| 1827 | goto error; |
| 1828 | |
| 1829 | /* update duration based on item type */ |
| 1830 | while (seq < sizeof(desig)) { |
| 1831 | if (*cur == desig[seq]) { |
| 1832 | |
| 1833 | /* verify numeric type; only seconds can be float */ |
| 1834 | if ((num_type != 0) && (seq < (sizeof(desig)-1))) |
| 1835 | goto error; |
| 1836 | |
| 1837 | switch (seq) { |
| 1838 | case 0: |
| 1839 | dur->value.dur.mon = (long)num * 12; |
| 1840 | break; |
| 1841 | case 1: |
| 1842 | dur->value.dur.mon += (long)num; |
| 1843 | break; |
| 1844 | default: |
| 1845 | /* convert to seconds using multiplier */ |
| 1846 | dur->value.dur.sec += num * multi[seq]; |
| 1847 | seq++; |
| 1848 | break; |
| 1849 | } |
| 1850 | |
| 1851 | break; /* exit loop */ |
| 1852 | } |
| 1853 | /* no date designators found? */ |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 1854 | if ((++seq == 3) || (seq == 6)) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1855 | goto error; |
| 1856 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1857 | cur++; |
| 1858 | if (collapse) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1859 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | if (isneg) { |
| 1863 | dur->value.dur.mon = -dur->value.dur.mon; |
| 1864 | dur->value.dur.day = -dur->value.dur.day; |
| 1865 | dur->value.dur.sec = -dur->value.dur.sec; |
| 1866 | } |
| 1867 | |
| 1868 | if (val != NULL) |
| 1869 | *val = dur; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1870 | else |
| 1871 | xmlSchemaFreeValue(dur); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1872 | |
| 1873 | return 0; |
| 1874 | |
| 1875 | error: |
| 1876 | if (dur != NULL) |
| 1877 | xmlSchemaFreeValue(dur); |
| 1878 | return 1; |
| 1879 | } |
| 1880 | |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1881 | /** |
| 1882 | * xmlSchemaStrip: |
| 1883 | * @value: a value |
| 1884 | * |
| 1885 | * Removes the leading and ending spaces of a string |
| 1886 | * |
| 1887 | * Returns the new string or NULL if no change was required. |
| 1888 | */ |
| 1889 | static xmlChar * |
| 1890 | xmlSchemaStrip(const xmlChar *value) { |
| 1891 | const xmlChar *start = value, *end, *f; |
| 1892 | |
| 1893 | if (value == NULL) return(NULL); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1894 | while ((*start != 0) && (IS_BLANK_CH(*start))) start++; |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1895 | end = start; |
| 1896 | while (*end != 0) end++; |
| 1897 | f = end; |
| 1898 | end--; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1899 | while ((end > start) && (IS_BLANK_CH(*end))) end--; |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1900 | end++; |
| 1901 | if ((start == value) && (f == end)) return(NULL); |
| 1902 | return(xmlStrndup(start, end - start)); |
| 1903 | } |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1904 | |
| 1905 | /** |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1906 | * xmlSchemaWhiteSpaceReplace: |
| 1907 | * @value: a value |
| 1908 | * |
| 1909 | * Replaces 0xd, 0x9 and 0xa with a space. |
| 1910 | * |
| 1911 | * Returns the new string or NULL if no change was required. |
| 1912 | */ |
| 1913 | xmlChar * |
| 1914 | xmlSchemaWhiteSpaceReplace(const xmlChar *value) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1915 | const xmlChar *cur = value; |
| 1916 | xmlChar *ret = NULL, *mcur; |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1917 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1918 | if (value == NULL) |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1919 | return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1920 | |
| 1921 | while ((*cur != 0) && |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1922 | (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) { |
| 1923 | cur++; |
| 1924 | } |
| 1925 | if (*cur == 0) |
| 1926 | return (NULL); |
| 1927 | ret = xmlStrdup(value); |
| 1928 | /* TODO FIXME: I guess gcc will bark at this. */ |
| 1929 | mcur = (xmlChar *) (ret + (cur - value)); |
| 1930 | do { |
| 1931 | if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) ) |
| 1932 | *mcur = ' '; |
| 1933 | mcur++; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1934 | } while (*mcur != 0); |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1935 | return(ret); |
| 1936 | } |
| 1937 | |
| 1938 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1939 | * xmlSchemaCollapseString: |
| 1940 | * @value: a value |
| 1941 | * |
| 1942 | * Removes and normalize white spaces in the string |
| 1943 | * |
| 1944 | * Returns the new string or NULL if no change was required. |
| 1945 | */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1946 | xmlChar * |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1947 | xmlSchemaCollapseString(const xmlChar *value) { |
| 1948 | const xmlChar *start = value, *end, *f; |
| 1949 | xmlChar *g; |
| 1950 | int col = 0; |
| 1951 | |
| 1952 | if (value == NULL) return(NULL); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1953 | while ((*start != 0) && (IS_BLANK_CH(*start))) start++; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1954 | end = start; |
| 1955 | while (*end != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1956 | if ((*end == ' ') && (IS_BLANK_CH(end[1]))) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1957 | col = end - start; |
| 1958 | break; |
| 1959 | } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) { |
| 1960 | col = end - start; |
| 1961 | break; |
| 1962 | } |
| 1963 | end++; |
| 1964 | } |
| 1965 | if (col == 0) { |
| 1966 | f = end; |
| 1967 | end--; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1968 | while ((end > start) && (IS_BLANK_CH(*end))) end--; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1969 | end++; |
| 1970 | if ((start == value) && (f == end)) return(NULL); |
| 1971 | return(xmlStrndup(start, end - start)); |
| 1972 | } |
| 1973 | start = xmlStrdup(start); |
| 1974 | if (start == NULL) return(NULL); |
| 1975 | g = (xmlChar *) (start + col); |
| 1976 | end = g; |
| 1977 | while (*end != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1978 | if (IS_BLANK_CH(*end)) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1979 | end++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1980 | while (IS_BLANK_CH(*end)) end++; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1981 | if (*end != 0) |
| 1982 | *g++ = ' '; |
| 1983 | } else |
| 1984 | *g++ = *end++; |
| 1985 | } |
| 1986 | *g = 0; |
| 1987 | return((xmlChar *) start); |
| 1988 | } |
| 1989 | |
| 1990 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1991 | * xmlSchemaValAtomicListNode: |
| 1992 | * @type: the predefined atomic type for a token in the list |
| 1993 | * @value: the list value to check |
| 1994 | * @ret: the return computed value |
| 1995 | * @node: the node containing the value |
| 1996 | * |
| 1997 | * Check that a value conforms to the lexical space of the predefined |
| 1998 | * list type. if true a value is computed and returned in @ret. |
| 1999 | * |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 2000 | * Returns the number of items if this validates, a negative error code |
| 2001 | * number otherwise |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2002 | */ |
| 2003 | static int |
| 2004 | xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value, |
| 2005 | xmlSchemaValPtr *ret, xmlNodePtr node) { |
| 2006 | xmlChar *val, *cur, *endval; |
| 2007 | int nb_values = 0; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2008 | int tmp = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2009 | |
| 2010 | if (value == NULL) { |
| 2011 | return(-1); |
| 2012 | } |
| 2013 | val = xmlStrdup(value); |
| 2014 | if (val == NULL) { |
| 2015 | return(-1); |
| 2016 | } |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 2017 | if (ret != NULL) { |
| 2018 | *ret = NULL; |
| 2019 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2020 | cur = val; |
| 2021 | /* |
| 2022 | * Split the list |
| 2023 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2024 | while (IS_BLANK_CH(*cur)) *cur++ = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2025 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2026 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2027 | *cur = 0; |
| 2028 | cur++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2029 | while (IS_BLANK_CH(*cur)) *cur++ = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2030 | } else { |
| 2031 | nb_values++; |
| 2032 | cur++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2033 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2034 | } |
| 2035 | } |
| 2036 | if (nb_values == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2037 | xmlFree(val); |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 2038 | return(nb_values); |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2039 | } |
| 2040 | endval = cur; |
| 2041 | cur = val; |
| 2042 | while ((*cur == 0) && (cur != endval)) cur++; |
| 2043 | while (cur != endval) { |
| 2044 | tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node); |
| 2045 | if (tmp != 0) |
| 2046 | break; |
| 2047 | while (*cur != 0) cur++; |
| 2048 | while ((*cur == 0) && (cur != endval)) cur++; |
| 2049 | } |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 2050 | /* TODO what return value ? c.f. bug #158628 |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2051 | if (ret != NULL) { |
| 2052 | TODO |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 2053 | } */ |
| 2054 | xmlFree(val); |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 2055 | if (tmp == 0) |
| 2056 | return(nb_values); |
| 2057 | return(-1); |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | /** |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2061 | * xmlSchemaParseUInt: |
| 2062 | * @str: pointer to the string R/W |
| 2063 | * @llo: pointer to the low result |
| 2064 | * @lmi: pointer to the mid result |
| 2065 | * @lhi: pointer to the high result |
| 2066 | * |
| 2067 | * Parse an unsigned long into 3 fields. |
| 2068 | * |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 2069 | * Returns the number of significant digits in the number or |
Daniel Veillard | bfc4263 | 2008-04-03 10:43:52 +0000 | [diff] [blame] | 2070 | * -1 if overflow of the capacity and -2 if it's not a number. |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2071 | */ |
| 2072 | static int |
| 2073 | xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2074 | unsigned long *lmi, unsigned long *lhi) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2075 | unsigned long lo = 0, mi = 0, hi = 0; |
| 2076 | const xmlChar *tmp, *cur = *str; |
| 2077 | int ret = 0, i = 0; |
| 2078 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2079 | if (!((*cur >= '0') && (*cur <= '9'))) |
Daniel Veillard | bfc4263 | 2008-04-03 10:43:52 +0000 | [diff] [blame] | 2080 | return(-2); |
| 2081 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2082 | while (*cur == '0') { /* ignore leading zeroes */ |
| 2083 | cur++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2084 | } |
| 2085 | tmp = cur; |
| 2086 | while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2087 | i++;tmp++;ret++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2088 | } |
| 2089 | if (i > 24) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2090 | *str = tmp; |
| 2091 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2092 | } |
| 2093 | while (i > 16) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2094 | hi = hi * 10 + (*cur++ - '0'); |
| 2095 | i--; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2096 | } |
| 2097 | while (i > 8) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2098 | mi = mi * 10 + (*cur++ - '0'); |
| 2099 | i--; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2100 | } |
| 2101 | while (i > 0) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2102 | lo = lo * 10 + (*cur++ - '0'); |
| 2103 | i--; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | *str = cur; |
| 2107 | *llo = lo; |
| 2108 | *lmi = mi; |
| 2109 | *lhi = hi; |
| 2110 | return(ret); |
| 2111 | } |
| 2112 | |
| 2113 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2114 | * xmlSchemaValAtomicType: |
| 2115 | * @type: the predefined type |
| 2116 | * @value: the value to check |
| 2117 | * @val: the return computed value |
| 2118 | * @node: the node containing the value |
| 2119 | * flags: flags to control the vlidation |
| 2120 | * |
| 2121 | * Check that a value conforms to the lexical space of the atomic type. |
| 2122 | * if true a value is computed and returned in @val. |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2123 | * This checks the value space for list types as well (IDREFS, NMTOKENS). |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2124 | * |
| 2125 | * Returns 0 if this validates, a positive error code number otherwise |
| 2126 | * and -1 in case of internal or API error. |
| 2127 | */ |
| 2128 | static int |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2129 | xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2130 | xmlSchemaValPtr * val, xmlNodePtr node, int flags, |
| 2131 | xmlSchemaWhitespaceValueType ws, |
| 2132 | int normOnTheFly, int applyNorm, int createStringValue) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2133 | { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2134 | xmlSchemaValPtr v; |
| 2135 | xmlChar *norm = NULL; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 2136 | int ret = 0; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2137 | |
| 2138 | if (xmlSchemaTypesInitialized == 0) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2139 | xmlSchemaInitTypes(); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2140 | if (type == NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2141 | return (-1); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2142 | |
Daniel Veillard | eebd633 | 2004-08-26 10:30:44 +0000 | [diff] [blame] | 2143 | /* |
| 2144 | * validating a non existant text node is similar to validating |
| 2145 | * an empty one. |
| 2146 | */ |
| 2147 | if (value == NULL) |
| 2148 | value = BAD_CAST ""; |
| 2149 | |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2150 | if (val != NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2151 | *val = NULL; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2152 | if ((flags == 0) && (value != NULL)) { |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 2153 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2154 | if ((type->builtInType != XML_SCHEMAS_STRING) && |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2155 | (type->builtInType != XML_SCHEMAS_ANYTYPE) && |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 2156 | (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) { |
| 2157 | if (type->builtInType == XML_SCHEMAS_NORMSTRING) |
| 2158 | norm = xmlSchemaWhiteSpaceReplace(value); |
| 2159 | else |
| 2160 | norm = xmlSchemaCollapseString(value); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2161 | if (norm != NULL) |
| 2162 | value = norm; |
| 2163 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2166 | switch (type->builtInType) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2167 | case XML_SCHEMAS_UNKNOWN: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2168 | goto error; |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 2169 | case XML_SCHEMAS_ANYTYPE: |
| 2170 | case XML_SCHEMAS_ANYSIMPLETYPE: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2171 | if ((createStringValue) && (val != NULL)) { |
| 2172 | v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE); |
| 2173 | if (v != NULL) { |
| 2174 | v->value.str = xmlStrdup(value); |
| 2175 | *val = v; |
| 2176 | } else { |
| 2177 | goto error; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2178 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2179 | } |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 2180 | goto return0; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2181 | case XML_SCHEMAS_STRING: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2182 | if (! normOnTheFly) { |
| 2183 | const xmlChar *cur = value; |
| 2184 | |
| 2185 | if (ws == XML_SCHEMA_WHITESPACE_REPLACE) { |
| 2186 | while (*cur != 0) { |
| 2187 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2188 | goto return1; |
| 2189 | } else { |
| 2190 | cur++; |
| 2191 | } |
| 2192 | } |
| 2193 | } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) { |
| 2194 | while (*cur != 0) { |
| 2195 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2196 | goto return1; |
| 2197 | } else if IS_WSP_SPACE_CH(*cur) { |
| 2198 | cur++; |
| 2199 | if IS_WSP_SPACE_CH(*cur) |
| 2200 | goto return1; |
| 2201 | } else { |
| 2202 | cur++; |
| 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | if (createStringValue && (val != NULL)) { |
| 2208 | if (applyNorm) { |
| 2209 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 2210 | norm = xmlSchemaCollapseString(value); |
| 2211 | else if (ws == XML_SCHEMA_WHITESPACE_REPLACE) |
| 2212 | norm = xmlSchemaWhiteSpaceReplace(value); |
| 2213 | if (norm != NULL) |
| 2214 | value = norm; |
| 2215 | } |
| 2216 | v = xmlSchemaNewValue(XML_SCHEMAS_STRING); |
| 2217 | if (v != NULL) { |
| 2218 | v->value.str = xmlStrdup(value); |
| 2219 | *val = v; |
| 2220 | } else { |
| 2221 | goto error; |
| 2222 | } |
| 2223 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2224 | goto return0; |
Daniel Veillard | 1516d5b | 2004-01-22 07:27:45 +0000 | [diff] [blame] | 2225 | case XML_SCHEMAS_NORMSTRING:{ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2226 | if (normOnTheFly) { |
| 2227 | if (applyNorm) { |
| 2228 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 2229 | norm = xmlSchemaCollapseString(value); |
| 2230 | else |
| 2231 | norm = xmlSchemaWhiteSpaceReplace(value); |
| 2232 | if (norm != NULL) |
| 2233 | value = norm; |
| 2234 | } |
| 2235 | } else { |
| 2236 | const xmlChar *cur = value; |
| 2237 | while (*cur != 0) { |
| 2238 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2239 | goto return1; |
| 2240 | } else { |
| 2241 | cur++; |
| 2242 | } |
| 2243 | } |
| 2244 | } |
Daniel Veillard | 1516d5b | 2004-01-22 07:27:45 +0000 | [diff] [blame] | 2245 | if (val != NULL) { |
| 2246 | v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING); |
| 2247 | if (v != NULL) { |
| 2248 | v->value.str = xmlStrdup(value); |
| 2249 | *val = v; |
| 2250 | } else { |
| 2251 | goto error; |
| 2252 | } |
| 2253 | } |
| 2254 | goto return0; |
| 2255 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2256 | case XML_SCHEMAS_DECIMAL:{ |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2257 | const xmlChar *cur = value; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2258 | unsigned int len, neg, integ, hasLeadingZeroes; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2259 | xmlChar cval[25]; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2260 | xmlChar *cptr = cval; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2261 | |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2262 | if ((cur == NULL) || (*cur == 0)) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2263 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2264 | |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2265 | /* |
| 2266 | * xs:decimal has a whitespace-facet value of 'collapse'. |
| 2267 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2268 | if (normOnTheFly) |
| 2269 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2270 | |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2271 | /* |
| 2272 | * First we handle an optional sign. |
| 2273 | */ |
| 2274 | neg = 0; |
| 2275 | if (*cur == '-') { |
| 2276 | neg = 1; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2277 | cur++; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2278 | } else if (*cur == '+') |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2279 | cur++; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2280 | /* |
| 2281 | * Disallow: "", "-", "- " |
| 2282 | */ |
| 2283 | if (*cur == 0) |
| 2284 | goto return1; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2285 | /* |
| 2286 | * Next we "pre-parse" the number, in preparation for calling |
| 2287 | * the common routine xmlSchemaParseUInt. We get rid of any |
| 2288 | * leading zeroes (because we have reserved only 25 chars), |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2289 | * and note the position of a decimal point. |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2290 | */ |
| 2291 | len = 0; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2292 | integ = ~0u; |
| 2293 | hasLeadingZeroes = 0; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2294 | /* |
| 2295 | * Skip leading zeroes. |
| 2296 | */ |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2297 | while (*cur == '0') { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2298 | cur++; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2299 | hasLeadingZeroes = 1; |
| 2300 | } |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2301 | if (*cur != 0) { |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2302 | do { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2303 | if ((*cur >= '0') && (*cur <= '9')) { |
| 2304 | *cptr++ = *cur++; |
| 2305 | len++; |
| 2306 | } else if (*cur == '.') { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2307 | cur++; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2308 | integ = len; |
| 2309 | do { |
| 2310 | if ((*cur >= '0') && (*cur <= '9')) { |
| 2311 | *cptr++ = *cur++; |
| 2312 | len++; |
| 2313 | } else |
| 2314 | break; |
| 2315 | } while (len < 24); |
| 2316 | /* |
| 2317 | * Disallow "." but allow "00." |
| 2318 | */ |
| 2319 | if ((len == 0) && (!hasLeadingZeroes)) |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2320 | goto return1; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2321 | break; |
| 2322 | } else |
| 2323 | break; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2324 | } while (len < 24); |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2325 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2326 | if (normOnTheFly) |
| 2327 | while IS_WSP_BLANK_CH(*cur) cur++; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2328 | if (*cur != 0) |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2329 | goto return1; /* error if any extraneous chars */ |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2330 | if (val != NULL) { |
| 2331 | v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL); |
| 2332 | if (v != NULL) { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2333 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2334 | * Now evaluate the significant digits of the number |
| 2335 | */ |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2336 | if (len != 0) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2337 | |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2338 | if (integ != ~0u) { |
| 2339 | /* |
| 2340 | * Get rid of trailing zeroes in the |
| 2341 | * fractional part. |
| 2342 | */ |
| 2343 | while ((len != integ) && (*(cptr-1) == '0')) { |
| 2344 | cptr--; |
| 2345 | len--; |
| 2346 | } |
| 2347 | } |
| 2348 | /* |
| 2349 | * Terminate the (preparsed) string. |
| 2350 | */ |
| 2351 | if (len != 0) { |
Daniel Veillard | bfc4263 | 2008-04-03 10:43:52 +0000 | [diff] [blame] | 2352 | *cptr = 0; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2353 | cptr = cval; |
Daniel Veillard | bfc4263 | 2008-04-03 10:43:52 +0000 | [diff] [blame] | 2354 | |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2355 | xmlSchemaParseUInt((const xmlChar **)&cptr, |
| 2356 | &v->value.decimal.lo, |
| 2357 | &v->value.decimal.mi, |
| 2358 | &v->value.decimal.hi); |
| 2359 | } |
| 2360 | } |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2361 | /* |
| 2362 | * Set the total digits to 1 if a zero value. |
| 2363 | */ |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2364 | v->value.decimal.sign = neg; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2365 | if (len == 0) { |
| 2366 | /* Speedup for zero values. */ |
| 2367 | v->value.decimal.total = 1; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2368 | } else { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2369 | v->value.decimal.total = len; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2370 | if (integ == ~0u) |
| 2371 | v->value.decimal.frac = 0; |
| 2372 | else |
| 2373 | v->value.decimal.frac = len - integ; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2374 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2375 | *val = v; |
| 2376 | } |
| 2377 | } |
| 2378 | goto return0; |
| 2379 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2380 | case XML_SCHEMAS_TIME: |
| 2381 | case XML_SCHEMAS_GDAY: |
| 2382 | case XML_SCHEMAS_GMONTH: |
| 2383 | case XML_SCHEMAS_GMONTHDAY: |
| 2384 | case XML_SCHEMAS_GYEAR: |
| 2385 | case XML_SCHEMAS_GYEARMONTH: |
| 2386 | case XML_SCHEMAS_DATE: |
| 2387 | case XML_SCHEMAS_DATETIME: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2388 | ret = xmlSchemaValidateDates(type->builtInType, value, val, |
| 2389 | normOnTheFly); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2390 | break; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2391 | case XML_SCHEMAS_DURATION: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2392 | ret = xmlSchemaValidateDuration(type, value, val, |
| 2393 | normOnTheFly); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2394 | break; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2395 | case XML_SCHEMAS_FLOAT: |
Csaba Raduly | 5f8f5e7 | 2010-07-28 11:41:23 +0200 | [diff] [blame] | 2396 | case XML_SCHEMAS_DOUBLE: { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2397 | const xmlChar *cur = value; |
| 2398 | int neg = 0; |
Csaba Raduly | 5f8f5e7 | 2010-07-28 11:41:23 +0200 | [diff] [blame] | 2399 | int digits_before = 0; |
| 2400 | int digits_after = 0; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2401 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2402 | if (normOnTheFly) |
| 2403 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2404 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2405 | if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) { |
| 2406 | cur += 3; |
| 2407 | if (*cur != 0) |
| 2408 | goto return1; |
| 2409 | if (val != NULL) { |
| 2410 | if (type == xmlSchemaTypeFloatDef) { |
| 2411 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2412 | if (v != NULL) { |
| 2413 | v->value.f = (float) xmlXPathNAN; |
| 2414 | } else { |
| 2415 | xmlSchemaFreeValue(v); |
| 2416 | goto error; |
| 2417 | } |
| 2418 | } else { |
| 2419 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2420 | if (v != NULL) { |
| 2421 | v->value.d = xmlXPathNAN; |
| 2422 | } else { |
| 2423 | xmlSchemaFreeValue(v); |
| 2424 | goto error; |
| 2425 | } |
| 2426 | } |
| 2427 | *val = v; |
| 2428 | } |
| 2429 | goto return0; |
| 2430 | } |
| 2431 | if (*cur == '-') { |
| 2432 | neg = 1; |
| 2433 | cur++; |
| 2434 | } |
| 2435 | if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) { |
| 2436 | cur += 3; |
| 2437 | if (*cur != 0) |
| 2438 | goto return1; |
| 2439 | if (val != NULL) { |
| 2440 | if (type == xmlSchemaTypeFloatDef) { |
| 2441 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2442 | if (v != NULL) { |
| 2443 | if (neg) |
| 2444 | v->value.f = (float) xmlXPathNINF; |
| 2445 | else |
| 2446 | v->value.f = (float) xmlXPathPINF; |
| 2447 | } else { |
| 2448 | xmlSchemaFreeValue(v); |
| 2449 | goto error; |
| 2450 | } |
| 2451 | } else { |
| 2452 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2453 | if (v != NULL) { |
| 2454 | if (neg) |
| 2455 | v->value.d = xmlXPathNINF; |
| 2456 | else |
| 2457 | v->value.d = xmlXPathPINF; |
| 2458 | } else { |
| 2459 | xmlSchemaFreeValue(v); |
| 2460 | goto error; |
| 2461 | } |
| 2462 | } |
| 2463 | *val = v; |
| 2464 | } |
| 2465 | goto return0; |
| 2466 | } |
| 2467 | if ((neg == 0) && (*cur == '+')) |
| 2468 | cur++; |
| 2469 | if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-')) |
| 2470 | goto return1; |
| 2471 | while ((*cur >= '0') && (*cur <= '9')) { |
| 2472 | cur++; |
Csaba Raduly | 5f8f5e7 | 2010-07-28 11:41:23 +0200 | [diff] [blame] | 2473 | digits_before++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2474 | } |
| 2475 | if (*cur == '.') { |
| 2476 | cur++; |
Csaba Raduly | 5f8f5e7 | 2010-07-28 11:41:23 +0200 | [diff] [blame] | 2477 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2478 | cur++; |
Csaba Raduly | 5f8f5e7 | 2010-07-28 11:41:23 +0200 | [diff] [blame] | 2479 | digits_after++; |
| 2480 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2481 | } |
Daniel Veillard | 1ea9590 | 2010-07-28 14:49:55 +0200 | [diff] [blame] | 2482 | if ((digits_before == 0) && (digits_after == 0)) |
Csaba Raduly | 5f8f5e7 | 2010-07-28 11:41:23 +0200 | [diff] [blame] | 2483 | goto return1; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2484 | if ((*cur == 'e') || (*cur == 'E')) { |
| 2485 | cur++; |
| 2486 | if ((*cur == '-') || (*cur == '+')) |
| 2487 | cur++; |
| 2488 | while ((*cur >= '0') && (*cur <= '9')) |
| 2489 | cur++; |
| 2490 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2491 | if (normOnTheFly) |
| 2492 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2493 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2494 | if (*cur != 0) |
| 2495 | goto return1; |
| 2496 | if (val != NULL) { |
| 2497 | if (type == xmlSchemaTypeFloatDef) { |
| 2498 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2499 | if (v != NULL) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2500 | /* |
| 2501 | * TODO: sscanf seems not to give the correct |
| 2502 | * value for extremely high/low values. |
| 2503 | * E.g. "1E-149" results in zero. |
| 2504 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2505 | if (sscanf((const char *) value, "%f", |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2506 | &(v->value.f)) == 1) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2507 | *val = v; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2508 | } else { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2509 | xmlSchemaFreeValue(v); |
| 2510 | goto return1; |
| 2511 | } |
| 2512 | } else { |
| 2513 | goto error; |
| 2514 | } |
| 2515 | } else { |
| 2516 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2517 | if (v != NULL) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2518 | /* |
| 2519 | * TODO: sscanf seems not to give the correct |
| 2520 | * value for extremely high/low values. |
| 2521 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2522 | if (sscanf((const char *) value, "%lf", |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2523 | &(v->value.d)) == 1) { |
| 2524 | *val = v; |
| 2525 | } else { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2526 | xmlSchemaFreeValue(v); |
| 2527 | goto return1; |
| 2528 | } |
| 2529 | } else { |
| 2530 | goto error; |
| 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | goto return0; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 2535 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2536 | case XML_SCHEMAS_BOOLEAN:{ |
| 2537 | const xmlChar *cur = value; |
| 2538 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2539 | if (normOnTheFly) { |
| 2540 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2541 | if (*cur == '0') { |
| 2542 | ret = 0; |
| 2543 | cur++; |
| 2544 | } else if (*cur == '1') { |
| 2545 | ret = 1; |
| 2546 | cur++; |
| 2547 | } else if (*cur == 't') { |
| 2548 | cur++; |
| 2549 | if ((*cur++ == 'r') && (*cur++ == 'u') && |
| 2550 | (*cur++ == 'e')) { |
| 2551 | ret = 1; |
| 2552 | } else |
| 2553 | goto return1; |
| 2554 | } else if (*cur == 'f') { |
| 2555 | cur++; |
| 2556 | if ((*cur++ == 'a') && (*cur++ == 'l') && |
| 2557 | (*cur++ == 's') && (*cur++ == 'e')) { |
| 2558 | ret = 0; |
| 2559 | } else |
| 2560 | goto return1; |
Kasimier T. Buchcik | 1869be5 | 2006-02-20 13:37:55 +0000 | [diff] [blame] | 2561 | } else |
| 2562 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2563 | if (*cur != 0) { |
| 2564 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2565 | if (*cur != 0) |
| 2566 | goto return1; |
| 2567 | } |
| 2568 | } else { |
| 2569 | if ((cur[0] == '0') && (cur[1] == 0)) |
| 2570 | ret = 0; |
| 2571 | else if ((cur[0] == '1') && (cur[1] == 0)) |
| 2572 | ret = 1; |
| 2573 | else if ((cur[0] == 't') && (cur[1] == 'r') |
| 2574 | && (cur[2] == 'u') && (cur[3] == 'e') |
| 2575 | && (cur[4] == 0)) |
| 2576 | ret = 1; |
| 2577 | else if ((cur[0] == 'f') && (cur[1] == 'a') |
| 2578 | && (cur[2] == 'l') && (cur[3] == 's') |
| 2579 | && (cur[4] == 'e') && (cur[5] == 0)) |
| 2580 | ret = 0; |
| 2581 | else |
| 2582 | goto return1; |
| 2583 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2584 | if (val != NULL) { |
| 2585 | v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN); |
| 2586 | if (v != NULL) { |
| 2587 | v->value.b = ret; |
| 2588 | *val = v; |
| 2589 | } else { |
| 2590 | goto error; |
| 2591 | } |
| 2592 | } |
| 2593 | goto return0; |
| 2594 | } |
| 2595 | case XML_SCHEMAS_TOKEN:{ |
| 2596 | const xmlChar *cur = value; |
| 2597 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2598 | if (! normOnTheFly) { |
| 2599 | while (*cur != 0) { |
| 2600 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2601 | goto return1; |
| 2602 | } else if (*cur == ' ') { |
| 2603 | cur++; |
| 2604 | if (*cur == 0) |
| 2605 | goto return1; |
| 2606 | if (*cur == ' ') |
| 2607 | goto return1; |
| 2608 | } else { |
| 2609 | cur++; |
| 2610 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2611 | } |
| 2612 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2613 | if (val != NULL) { |
| 2614 | v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN); |
| 2615 | if (v != NULL) { |
| 2616 | v->value.str = xmlStrdup(value); |
| 2617 | *val = v; |
| 2618 | } else { |
| 2619 | goto error; |
| 2620 | } |
| 2621 | } |
| 2622 | goto return0; |
| 2623 | } |
| 2624 | case XML_SCHEMAS_LANGUAGE: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2625 | if (normOnTheFly) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2626 | norm = xmlSchemaCollapseString(value); |
| 2627 | if (norm != NULL) |
| 2628 | value = norm; |
| 2629 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2630 | if (xmlCheckLanguageID(value) == 1) { |
| 2631 | if (val != NULL) { |
| 2632 | v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE); |
| 2633 | if (v != NULL) { |
| 2634 | v->value.str = xmlStrdup(value); |
| 2635 | *val = v; |
| 2636 | } else { |
| 2637 | goto error; |
| 2638 | } |
| 2639 | } |
| 2640 | goto return0; |
| 2641 | } |
| 2642 | goto return1; |
| 2643 | case XML_SCHEMAS_NMTOKEN: |
| 2644 | if (xmlValidateNMToken(value, 1) == 0) { |
| 2645 | if (val != NULL) { |
| 2646 | v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN); |
| 2647 | if (v != NULL) { |
| 2648 | v->value.str = xmlStrdup(value); |
| 2649 | *val = v; |
| 2650 | } else { |
| 2651 | goto error; |
| 2652 | } |
| 2653 | } |
| 2654 | goto return0; |
| 2655 | } |
| 2656 | goto return1; |
| 2657 | case XML_SCHEMAS_NMTOKENS: |
| 2658 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef, |
| 2659 | value, val, node); |
| 2660 | if (ret > 0) |
| 2661 | ret = 0; |
| 2662 | else |
| 2663 | ret = 1; |
| 2664 | goto done; |
| 2665 | case XML_SCHEMAS_NAME: |
| 2666 | ret = xmlValidateName(value, 1); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 2667 | if ((ret == 0) && (val != NULL) && (value != NULL)) { |
| 2668 | v = xmlSchemaNewValue(XML_SCHEMAS_NAME); |
| 2669 | if (v != NULL) { |
| 2670 | const xmlChar *start = value, *end; |
| 2671 | while (IS_BLANK_CH(*start)) start++; |
| 2672 | end = start; |
| 2673 | while ((*end != 0) && (!IS_BLANK_CH(*end))) end++; |
| 2674 | v->value.str = xmlStrndup(start, end - start); |
| 2675 | *val = v; |
| 2676 | } else { |
| 2677 | goto error; |
| 2678 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2679 | } |
| 2680 | goto done; |
| 2681 | case XML_SCHEMAS_QNAME:{ |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2682 | const xmlChar *uri = NULL; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2683 | xmlChar *local = NULL; |
| 2684 | |
| 2685 | ret = xmlValidateQName(value, 1); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2686 | if (ret != 0) |
| 2687 | goto done; |
| 2688 | if (node != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2689 | xmlChar *prefix; |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2690 | xmlNsPtr ns; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2691 | |
| 2692 | local = xmlSplitQName2(value, &prefix); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2693 | ns = xmlSearchNs(node->doc, node, prefix); |
| 2694 | if ((ns == NULL) && (prefix != NULL)) { |
| 2695 | xmlFree(prefix); |
| 2696 | if (local != NULL) |
| 2697 | xmlFree(local); |
| 2698 | goto return1; |
| 2699 | } |
| 2700 | if (ns != NULL) |
| 2701 | uri = ns->href; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2702 | if (prefix != NULL) |
| 2703 | xmlFree(prefix); |
| 2704 | } |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2705 | if (val != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2706 | v = xmlSchemaNewValue(XML_SCHEMAS_QNAME); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2707 | if (v == NULL) { |
| 2708 | if (local != NULL) |
| 2709 | xmlFree(local); |
| 2710 | goto error; |
| 2711 | } |
| 2712 | if (local != NULL) |
| 2713 | v->value.qname.name = local; |
| 2714 | else |
| 2715 | v->value.qname.name = xmlStrdup(value); |
| 2716 | if (uri != NULL) |
| 2717 | v->value.qname.uri = xmlStrdup(uri); |
| 2718 | *val = v; |
| 2719 | } else |
| 2720 | if (local != NULL) |
| 2721 | xmlFree(local); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2722 | goto done; |
| 2723 | } |
| 2724 | case XML_SCHEMAS_NCNAME: |
| 2725 | ret = xmlValidateNCName(value, 1); |
| 2726 | if ((ret == 0) && (val != NULL)) { |
| 2727 | v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME); |
| 2728 | if (v != NULL) { |
| 2729 | v->value.str = xmlStrdup(value); |
| 2730 | *val = v; |
| 2731 | } else { |
| 2732 | goto error; |
| 2733 | } |
| 2734 | } |
| 2735 | goto done; |
| 2736 | case XML_SCHEMAS_ID: |
| 2737 | ret = xmlValidateNCName(value, 1); |
| 2738 | if ((ret == 0) && (val != NULL)) { |
| 2739 | v = xmlSchemaNewValue(XML_SCHEMAS_ID); |
| 2740 | if (v != NULL) { |
| 2741 | v->value.str = xmlStrdup(value); |
| 2742 | *val = v; |
| 2743 | } else { |
| 2744 | goto error; |
| 2745 | } |
| 2746 | } |
| 2747 | if ((ret == 0) && (node != NULL) && |
| 2748 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2749 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2750 | |
| 2751 | /* |
| 2752 | * NOTE: the IDness might have already be declared in the DTD |
| 2753 | */ |
| 2754 | if (attr->atype != XML_ATTRIBUTE_ID) { |
| 2755 | xmlIDPtr res; |
| 2756 | xmlChar *strip; |
| 2757 | |
| 2758 | strip = xmlSchemaStrip(value); |
| 2759 | if (strip != NULL) { |
| 2760 | res = xmlAddID(NULL, node->doc, strip, attr); |
| 2761 | xmlFree(strip); |
| 2762 | } else |
| 2763 | res = xmlAddID(NULL, node->doc, value, attr); |
| 2764 | if (res == NULL) { |
| 2765 | ret = 2; |
| 2766 | } else { |
| 2767 | attr->atype = XML_ATTRIBUTE_ID; |
| 2768 | } |
| 2769 | } |
| 2770 | } |
| 2771 | goto done; |
| 2772 | case XML_SCHEMAS_IDREF: |
| 2773 | ret = xmlValidateNCName(value, 1); |
| 2774 | if ((ret == 0) && (val != NULL)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 2775 | v = xmlSchemaNewValue(XML_SCHEMAS_IDREF); |
| 2776 | if (v == NULL) |
| 2777 | goto error; |
| 2778 | v->value.str = xmlStrdup(value); |
| 2779 | *val = v; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2780 | } |
| 2781 | if ((ret == 0) && (node != NULL) && |
| 2782 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2783 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2784 | xmlChar *strip; |
| 2785 | |
| 2786 | strip = xmlSchemaStrip(value); |
| 2787 | if (strip != NULL) { |
| 2788 | xmlAddRef(NULL, node->doc, strip, attr); |
| 2789 | xmlFree(strip); |
| 2790 | } else |
| 2791 | xmlAddRef(NULL, node->doc, value, attr); |
| 2792 | attr->atype = XML_ATTRIBUTE_IDREF; |
| 2793 | } |
| 2794 | goto done; |
| 2795 | case XML_SCHEMAS_IDREFS: |
| 2796 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef, |
| 2797 | value, val, node); |
| 2798 | if (ret < 0) |
| 2799 | ret = 2; |
| 2800 | else |
| 2801 | ret = 0; |
| 2802 | if ((ret == 0) && (node != NULL) && |
| 2803 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2804 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2805 | |
| 2806 | attr->atype = XML_ATTRIBUTE_IDREFS; |
| 2807 | } |
| 2808 | goto done; |
| 2809 | case XML_SCHEMAS_ENTITY:{ |
| 2810 | xmlChar *strip; |
| 2811 | |
| 2812 | ret = xmlValidateNCName(value, 1); |
| 2813 | if ((node == NULL) || (node->doc == NULL)) |
| 2814 | ret = 3; |
| 2815 | if (ret == 0) { |
| 2816 | xmlEntityPtr ent; |
| 2817 | |
| 2818 | strip = xmlSchemaStrip(value); |
| 2819 | if (strip != NULL) { |
| 2820 | ent = xmlGetDocEntity(node->doc, strip); |
| 2821 | xmlFree(strip); |
| 2822 | } else { |
| 2823 | ent = xmlGetDocEntity(node->doc, value); |
| 2824 | } |
| 2825 | if ((ent == NULL) || |
| 2826 | (ent->etype != |
| 2827 | XML_EXTERNAL_GENERAL_UNPARSED_ENTITY)) |
| 2828 | ret = 4; |
| 2829 | } |
| 2830 | if ((ret == 0) && (val != NULL)) { |
| 2831 | TODO; |
| 2832 | } |
| 2833 | if ((ret == 0) && (node != NULL) && |
| 2834 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2835 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2836 | |
| 2837 | attr->atype = XML_ATTRIBUTE_ENTITY; |
| 2838 | } |
| 2839 | goto done; |
| 2840 | } |
| 2841 | case XML_SCHEMAS_ENTITIES: |
| 2842 | if ((node == NULL) || (node->doc == NULL)) |
| 2843 | goto return3; |
| 2844 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef, |
| 2845 | value, val, node); |
| 2846 | if (ret <= 0) |
| 2847 | ret = 1; |
| 2848 | else |
| 2849 | ret = 0; |
| 2850 | if ((ret == 0) && (node != NULL) && |
| 2851 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2852 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2853 | |
| 2854 | attr->atype = XML_ATTRIBUTE_ENTITIES; |
| 2855 | } |
| 2856 | goto done; |
| 2857 | case XML_SCHEMAS_NOTATION:{ |
| 2858 | xmlChar *uri = NULL; |
| 2859 | xmlChar *local = NULL; |
| 2860 | |
| 2861 | ret = xmlValidateQName(value, 1); |
| 2862 | if ((ret == 0) && (node != NULL)) { |
| 2863 | xmlChar *prefix; |
| 2864 | |
| 2865 | local = xmlSplitQName2(value, &prefix); |
| 2866 | if (prefix != NULL) { |
| 2867 | xmlNsPtr ns; |
| 2868 | |
| 2869 | ns = xmlSearchNs(node->doc, node, prefix); |
| 2870 | if (ns == NULL) |
| 2871 | ret = 1; |
| 2872 | else if (val != NULL) |
| 2873 | uri = xmlStrdup(ns->href); |
| 2874 | } |
| 2875 | if ((local != NULL) && ((val == NULL) || (ret != 0))) |
| 2876 | xmlFree(local); |
| 2877 | if (prefix != NULL) |
| 2878 | xmlFree(prefix); |
| 2879 | } |
| 2880 | if ((node == NULL) || (node->doc == NULL)) |
| 2881 | ret = 3; |
| 2882 | if (ret == 0) { |
| 2883 | ret = xmlValidateNotationUse(NULL, node->doc, value); |
| 2884 | if (ret == 1) |
| 2885 | ret = 0; |
| 2886 | else |
| 2887 | ret = 1; |
| 2888 | } |
| 2889 | if ((ret == 0) && (val != NULL)) { |
| 2890 | v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION); |
| 2891 | if (v != NULL) { |
| 2892 | if (local != NULL) |
| 2893 | v->value.qname.name = local; |
| 2894 | else |
| 2895 | v->value.qname.name = xmlStrdup(value); |
| 2896 | if (uri != NULL) |
| 2897 | v->value.qname.uri = uri; |
| 2898 | |
| 2899 | *val = v; |
| 2900 | } else { |
| 2901 | if (local != NULL) |
| 2902 | xmlFree(local); |
| 2903 | if (uri != NULL) |
| 2904 | xmlFree(uri); |
| 2905 | goto error; |
| 2906 | } |
| 2907 | } |
| 2908 | goto done; |
| 2909 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2910 | case XML_SCHEMAS_ANYURI:{ |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2911 | if (*value != 0) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2912 | xmlURIPtr uri; |
Vincent Lefevre | 933e5de | 2009-08-07 16:42:24 +0200 | [diff] [blame] | 2913 | xmlChar *tmpval, *cur; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2914 | if (normOnTheFly) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2915 | norm = xmlSchemaCollapseString(value); |
| 2916 | if (norm != NULL) |
| 2917 | value = norm; |
| 2918 | } |
Vincent Lefevre | 933e5de | 2009-08-07 16:42:24 +0200 | [diff] [blame] | 2919 | tmpval = xmlStrdup(value); |
| 2920 | for (cur = tmpval; *cur; ++cur) { |
| 2921 | if (*cur < 32 || *cur >= 127 || *cur == ' ' || |
| 2922 | *cur == '<' || *cur == '>' || *cur == '"' || |
| 2923 | *cur == '{' || *cur == '}' || *cur == '|' || |
| 2924 | *cur == '\\' || *cur == '^' || *cur == '`' || |
| 2925 | *cur == '\'') |
| 2926 | *cur = '_'; |
| 2927 | } |
| 2928 | uri = xmlParseURI((const char *) tmpval); |
| 2929 | xmlFree(tmpval); |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2930 | if (uri == NULL) |
| 2931 | goto return1; |
| 2932 | xmlFreeURI(uri); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2933 | } |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2934 | |
| 2935 | if (val != NULL) { |
| 2936 | v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI); |
| 2937 | if (v == NULL) |
| 2938 | goto error; |
| 2939 | v->value.str = xmlStrdup(value); |
| 2940 | *val = v; |
| 2941 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2942 | goto return0; |
| 2943 | } |
| 2944 | case XML_SCHEMAS_HEXBINARY:{ |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2945 | const xmlChar *cur = value, *start; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2946 | xmlChar *base; |
| 2947 | int total, i = 0; |
| 2948 | |
Daniel Veillard | f34a20e | 2004-08-31 08:42:17 +0000 | [diff] [blame] | 2949 | if (cur == NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2950 | goto return1; |
| 2951 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2952 | if (normOnTheFly) |
| 2953 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2954 | |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2955 | start = cur; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2956 | while (((*cur >= '0') && (*cur <= '9')) || |
| 2957 | ((*cur >= 'A') && (*cur <= 'F')) || |
| 2958 | ((*cur >= 'a') && (*cur <= 'f'))) { |
| 2959 | i++; |
| 2960 | cur++; |
| 2961 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2962 | if (normOnTheFly) |
| 2963 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2964 | |
| 2965 | if (*cur != 0) |
| 2966 | goto return1; |
| 2967 | if ((i % 2) != 0) |
| 2968 | goto return1; |
| 2969 | |
| 2970 | if (val != NULL) { |
| 2971 | |
| 2972 | v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY); |
| 2973 | if (v == NULL) |
| 2974 | goto error; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2975 | /* |
| 2976 | * Copy only the normalized piece. |
| 2977 | * CRITICAL TODO: Check this. |
| 2978 | */ |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2979 | cur = xmlStrndup(start, i); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2980 | if (cur == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2981 | xmlSchemaTypeErrMemory(node, "allocating hexbin data"); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2982 | xmlFree(v); |
| 2983 | goto return1; |
| 2984 | } |
| 2985 | |
| 2986 | total = i / 2; /* number of octets */ |
| 2987 | |
| 2988 | base = (xmlChar *) cur; |
| 2989 | while (i-- > 0) { |
| 2990 | if (*base >= 'a') |
| 2991 | *base = *base - ('a' - 'A'); |
| 2992 | base++; |
| 2993 | } |
| 2994 | |
| 2995 | v->value.hex.str = (xmlChar *) cur; |
| 2996 | v->value.hex.total = total; |
| 2997 | *val = v; |
| 2998 | } |
| 2999 | goto return0; |
| 3000 | } |
| 3001 | case XML_SCHEMAS_BASE64BINARY:{ |
| 3002 | /* ISSUE: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3003 | * |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3004 | * Ignore all stray characters? (yes, currently) |
| 3005 | * Worry about long lines? (no, currently) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3006 | * |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3007 | * rfc2045.txt: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3008 | * |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3009 | * "The encoded output stream must be represented in lines of |
| 3010 | * no more than 76 characters each. All line breaks or other |
| 3011 | * characters not found in Table 1 must be ignored by decoding |
| 3012 | * software. In base64 data, characters other than those in |
| 3013 | * Table 1, line breaks, and other white space probably |
| 3014 | * indicate a transmission error, about which a warning |
| 3015 | * message or even a message rejection might be appropriate |
| 3016 | * under some circumstances." */ |
| 3017 | const xmlChar *cur = value; |
| 3018 | xmlChar *base; |
| 3019 | int total, i = 0, pad = 0; |
| 3020 | |
| 3021 | if (cur == NULL) |
| 3022 | goto return1; |
| 3023 | |
| 3024 | for (; *cur; ++cur) { |
| 3025 | int decc; |
| 3026 | |
| 3027 | decc = _xmlSchemaBase64Decode(*cur); |
| 3028 | if (decc < 0) ; |
| 3029 | else if (decc < 64) |
| 3030 | i++; |
| 3031 | else |
| 3032 | break; |
| 3033 | } |
| 3034 | for (; *cur; ++cur) { |
| 3035 | int decc; |
| 3036 | |
| 3037 | decc = _xmlSchemaBase64Decode(*cur); |
| 3038 | if (decc < 0) ; |
| 3039 | else if (decc < 64) |
| 3040 | goto return1; |
| 3041 | if (decc == 64) |
| 3042 | pad++; |
| 3043 | } |
| 3044 | |
| 3045 | /* rfc2045.txt: "Special processing is performed if fewer than |
| 3046 | * 24 bits are available at the end of the data being encoded. |
| 3047 | * A full encoding quantum is always completed at the end of a |
| 3048 | * body. When fewer than 24 input bits are available in an |
| 3049 | * input group, zero bits are added (on the right) to form an |
| 3050 | * integral number of 6-bit groups. Padding at the end of the |
| 3051 | * data is performed using the "=" character. Since all |
| 3052 | * base64 input is an integral number of octets, only the |
| 3053 | * following cases can arise: (1) the final quantum of |
| 3054 | * encoding input is an integral multiple of 24 bits; here, |
| 3055 | * the final unit of encoded output will be an integral |
| 3056 | * multiple ofindent: Standard input:701: Warning:old style |
| 3057 | * assignment ambiguity in "=*". Assuming "= *" 4 characters |
| 3058 | * with no "=" padding, (2) the final |
| 3059 | * quantum of encoding input is exactly 8 bits; here, the |
| 3060 | * final unit of encoded output will be two characters |
| 3061 | * followed by two "=" padding characters, or (3) the final |
| 3062 | * quantum of encoding input is exactly 16 bits; here, the |
| 3063 | * final unit of encoded output will be three characters |
| 3064 | * followed by one "=" padding character." */ |
| 3065 | |
| 3066 | total = 3 * (i / 4); |
| 3067 | if (pad == 0) { |
| 3068 | if (i % 4 != 0) |
| 3069 | goto return1; |
| 3070 | } else if (pad == 1) { |
| 3071 | int decc; |
| 3072 | |
| 3073 | if (i % 4 != 3) |
| 3074 | goto return1; |
| 3075 | for (decc = _xmlSchemaBase64Decode(*cur); |
| 3076 | (decc < 0) || (decc > 63); |
| 3077 | decc = _xmlSchemaBase64Decode(*cur)) |
| 3078 | --cur; |
| 3079 | /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/ |
| 3080 | /* 00111100 -> 0x3c */ |
| 3081 | if (decc & ~0x3c) |
| 3082 | goto return1; |
| 3083 | total += 2; |
| 3084 | } else if (pad == 2) { |
| 3085 | int decc; |
| 3086 | |
| 3087 | if (i % 4 != 2) |
| 3088 | goto return1; |
| 3089 | for (decc = _xmlSchemaBase64Decode(*cur); |
| 3090 | (decc < 0) || (decc > 63); |
| 3091 | decc = _xmlSchemaBase64Decode(*cur)) |
| 3092 | --cur; |
| 3093 | /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */ |
| 3094 | /* 00110000 -> 0x30 */ |
| 3095 | if (decc & ~0x30) |
| 3096 | goto return1; |
| 3097 | total += 1; |
| 3098 | } else |
| 3099 | goto return1; |
| 3100 | |
| 3101 | if (val != NULL) { |
| 3102 | v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY); |
| 3103 | if (v == NULL) |
| 3104 | goto error; |
| 3105 | base = |
| 3106 | (xmlChar *) xmlMallocAtomic((i + pad + 1) * |
| 3107 | sizeof(xmlChar)); |
| 3108 | if (base == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3109 | xmlSchemaTypeErrMemory(node, "allocating base64 data"); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3110 | xmlFree(v); |
| 3111 | goto return1; |
| 3112 | } |
| 3113 | v->value.base64.str = base; |
| 3114 | for (cur = value; *cur; ++cur) |
| 3115 | if (_xmlSchemaBase64Decode(*cur) >= 0) { |
| 3116 | *base = *cur; |
| 3117 | ++base; |
| 3118 | } |
| 3119 | *base = 0; |
| 3120 | v->value.base64.total = total; |
| 3121 | *val = v; |
| 3122 | } |
| 3123 | goto return0; |
| 3124 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3125 | case XML_SCHEMAS_INTEGER: |
| 3126 | case XML_SCHEMAS_PINTEGER: |
| 3127 | case XML_SCHEMAS_NPINTEGER: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3128 | case XML_SCHEMAS_NINTEGER: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3129 | case XML_SCHEMAS_NNINTEGER:{ |
| 3130 | const xmlChar *cur = value; |
| 3131 | unsigned long lo, mi, hi; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3132 | int sign = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3133 | |
| 3134 | if (cur == NULL) |
| 3135 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3136 | if (normOnTheFly) |
| 3137 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3138 | if (*cur == '-') { |
| 3139 | sign = 1; |
| 3140 | cur++; |
| 3141 | } else if (*cur == '+') |
| 3142 | cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3143 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
Daniel Veillard | bfc4263 | 2008-04-03 10:43:52 +0000 | [diff] [blame] | 3144 | if (ret < 0) |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3145 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3146 | if (normOnTheFly) |
| 3147 | while IS_WSP_BLANK_CH(*cur) cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3148 | if (*cur != 0) |
| 3149 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3150 | if (type->builtInType == XML_SCHEMAS_NPINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3151 | if ((sign == 0) && |
| 3152 | ((hi != 0) || (mi != 0) || (lo != 0))) |
| 3153 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3154 | } else if (type->builtInType == XML_SCHEMAS_PINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3155 | if (sign == 1) |
| 3156 | goto return1; |
| 3157 | if ((hi == 0) && (mi == 0) && (lo == 0)) |
| 3158 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3159 | } else if (type->builtInType == XML_SCHEMAS_NINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3160 | if (sign == 0) |
| 3161 | goto return1; |
| 3162 | if ((hi == 0) && (mi == 0) && (lo == 0)) |
| 3163 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3164 | } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3165 | if ((sign == 1) && |
| 3166 | ((hi != 0) || (mi != 0) || (lo != 0))) |
| 3167 | goto return1; |
| 3168 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3169 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3170 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3171 | if (v != NULL) { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 3172 | if (ret == 0) |
| 3173 | ret++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3174 | v->value.decimal.lo = lo; |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3175 | v->value.decimal.mi = mi; |
| 3176 | v->value.decimal.hi = hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3177 | v->value.decimal.sign = sign; |
| 3178 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3179 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3180 | *val = v; |
| 3181 | } |
| 3182 | } |
| 3183 | goto return0; |
| 3184 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3185 | case XML_SCHEMAS_LONG: |
| 3186 | case XML_SCHEMAS_BYTE: |
| 3187 | case XML_SCHEMAS_SHORT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3188 | case XML_SCHEMAS_INT:{ |
Daniel Veillard | bfc4263 | 2008-04-03 10:43:52 +0000 | [diff] [blame] | 3189 | const xmlChar *cur = value; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3190 | unsigned long lo, mi, hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3191 | int sign = 0; |
| 3192 | |
| 3193 | if (cur == NULL) |
| 3194 | goto return1; |
| 3195 | if (*cur == '-') { |
| 3196 | sign = 1; |
| 3197 | cur++; |
| 3198 | } else if (*cur == '+') |
| 3199 | cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3200 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 3201 | if (ret < 0) |
| 3202 | goto return1; |
| 3203 | if (*cur != 0) |
| 3204 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3205 | if (type->builtInType == XML_SCHEMAS_LONG) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3206 | if (hi >= 922) { |
| 3207 | if (hi > 922) |
| 3208 | goto return1; |
| 3209 | if (mi >= 33720368) { |
| 3210 | if (mi > 33720368) |
| 3211 | goto return1; |
| 3212 | if ((sign == 0) && (lo > 54775807)) |
| 3213 | goto return1; |
| 3214 | if ((sign == 1) && (lo > 54775808)) |
| 3215 | goto return1; |
| 3216 | } |
| 3217 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3218 | } else if (type->builtInType == XML_SCHEMAS_INT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3219 | if (hi != 0) |
| 3220 | goto return1; |
| 3221 | if (mi >= 21) { |
| 3222 | if (mi > 21) |
| 3223 | goto return1; |
| 3224 | if ((sign == 0) && (lo > 47483647)) |
| 3225 | goto return1; |
| 3226 | if ((sign == 1) && (lo > 47483648)) |
| 3227 | goto return1; |
| 3228 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3229 | } else if (type->builtInType == XML_SCHEMAS_SHORT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3230 | if ((mi != 0) || (hi != 0)) |
| 3231 | goto return1; |
| 3232 | if ((sign == 1) && (lo > 32768)) |
| 3233 | goto return1; |
| 3234 | if ((sign == 0) && (lo > 32767)) |
| 3235 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3236 | } else if (type->builtInType == XML_SCHEMAS_BYTE) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3237 | if ((mi != 0) || (hi != 0)) |
| 3238 | goto return1; |
| 3239 | if ((sign == 1) && (lo > 128)) |
| 3240 | goto return1; |
| 3241 | if ((sign == 0) && (lo > 127)) |
| 3242 | goto return1; |
| 3243 | } |
| 3244 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3245 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3246 | if (v != NULL) { |
| 3247 | v->value.decimal.lo = lo; |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3248 | v->value.decimal.mi = mi; |
| 3249 | v->value.decimal.hi = hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3250 | v->value.decimal.sign = sign; |
| 3251 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3252 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3253 | *val = v; |
| 3254 | } |
| 3255 | } |
| 3256 | goto return0; |
| 3257 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3258 | case XML_SCHEMAS_UINT: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3259 | case XML_SCHEMAS_ULONG: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3260 | case XML_SCHEMAS_USHORT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3261 | case XML_SCHEMAS_UBYTE:{ |
| 3262 | const xmlChar *cur = value; |
| 3263 | unsigned long lo, mi, hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3264 | |
| 3265 | if (cur == NULL) |
| 3266 | goto return1; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3267 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 3268 | if (ret < 0) |
| 3269 | goto return1; |
| 3270 | if (*cur != 0) |
| 3271 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3272 | if (type->builtInType == XML_SCHEMAS_ULONG) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3273 | if (hi >= 1844) { |
| 3274 | if (hi > 1844) |
| 3275 | goto return1; |
| 3276 | if (mi >= 67440737) { |
| 3277 | if (mi > 67440737) |
| 3278 | goto return1; |
| 3279 | if (lo > 9551615) |
| 3280 | goto return1; |
| 3281 | } |
| 3282 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3283 | } else if (type->builtInType == XML_SCHEMAS_UINT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3284 | if (hi != 0) |
| 3285 | goto return1; |
| 3286 | if (mi >= 42) { |
| 3287 | if (mi > 42) |
| 3288 | goto return1; |
| 3289 | if (lo > 94967295) |
| 3290 | goto return1; |
| 3291 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3292 | } else if (type->builtInType == XML_SCHEMAS_USHORT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3293 | if ((mi != 0) || (hi != 0)) |
| 3294 | goto return1; |
| 3295 | if (lo > 65535) |
| 3296 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3297 | } else if (type->builtInType == XML_SCHEMAS_UBYTE) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3298 | if ((mi != 0) || (hi != 0)) |
| 3299 | goto return1; |
| 3300 | if (lo > 255) |
| 3301 | goto return1; |
| 3302 | } |
| 3303 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3304 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3305 | if (v != NULL) { |
| 3306 | v->value.decimal.lo = lo; |
| 3307 | v->value.decimal.mi = mi; |
| 3308 | v->value.decimal.hi = hi; |
| 3309 | v->value.decimal.sign = 0; |
| 3310 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3311 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3312 | *val = v; |
| 3313 | } |
| 3314 | } |
| 3315 | goto return0; |
| 3316 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3319 | done: |
| 3320 | if (norm != NULL) |
| 3321 | xmlFree(norm); |
| 3322 | return (ret); |
| 3323 | return3: |
| 3324 | if (norm != NULL) |
| 3325 | xmlFree(norm); |
| 3326 | return (3); |
| 3327 | return1: |
| 3328 | if (norm != NULL) |
| 3329 | xmlFree(norm); |
| 3330 | return (1); |
| 3331 | return0: |
| 3332 | if (norm != NULL) |
| 3333 | xmlFree(norm); |
| 3334 | return (0); |
| 3335 | error: |
| 3336 | if (norm != NULL) |
| 3337 | xmlFree(norm); |
| 3338 | return (-1); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3342 | * xmlSchemaValPredefTypeNode: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3343 | * @type: the predefined type |
| 3344 | * @value: the value to check |
| 3345 | * @val: the return computed value |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3346 | * @node: the node containing the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3347 | * |
| 3348 | * Check that a value conforms to the lexical space of the predefined type. |
| 3349 | * if true a value is computed and returned in @val. |
| 3350 | * |
| 3351 | * Returns 0 if this validates, a positive error code number otherwise |
| 3352 | * and -1 in case of internal or API error. |
| 3353 | */ |
| 3354 | int |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3355 | xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value, |
| 3356 | xmlSchemaValPtr *val, xmlNodePtr node) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3357 | return(xmlSchemaValAtomicType(type, value, val, node, 0, |
| 3358 | XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | /** |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3362 | * xmlSchemaValPredefTypeNodeNoNorm: |
| 3363 | * @type: the predefined type |
| 3364 | * @value: the value to check |
| 3365 | * @val: the return computed value |
| 3366 | * @node: the node containing the value |
| 3367 | * |
| 3368 | * Check that a value conforms to the lexical space of the predefined type. |
| 3369 | * if true a value is computed and returned in @val. |
| 3370 | * This one does apply any normalization to the value. |
| 3371 | * |
| 3372 | * Returns 0 if this validates, a positive error code number otherwise |
| 3373 | * and -1 in case of internal or API error. |
| 3374 | */ |
| 3375 | int |
| 3376 | xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value, |
| 3377 | xmlSchemaValPtr *val, xmlNodePtr node) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3378 | return(xmlSchemaValAtomicType(type, value, val, node, 1, |
| 3379 | XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1)); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3383 | * xmlSchemaValidatePredefinedType: |
| 3384 | * @type: the predefined type |
| 3385 | * @value: the value to check |
| 3386 | * @val: the return computed value |
| 3387 | * |
| 3388 | * Check that a value conforms to the lexical space of the predefined type. |
| 3389 | * if true a value is computed and returned in @val. |
| 3390 | * |
| 3391 | * Returns 0 if this validates, a positive error code number otherwise |
| 3392 | * and -1 in case of internal or API error. |
| 3393 | */ |
| 3394 | int |
| 3395 | xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value, |
| 3396 | xmlSchemaValPtr *val) { |
| 3397 | return(xmlSchemaValPredefTypeNode(type, value, val, NULL)); |
| 3398 | } |
| 3399 | |
| 3400 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3401 | * xmlSchemaCompareDecimals: |
| 3402 | * @x: a first decimal value |
| 3403 | * @y: a second decimal value |
| 3404 | * |
| 3405 | * Compare 2 decimals |
| 3406 | * |
| 3407 | * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error |
| 3408 | */ |
| 3409 | static int |
| 3410 | xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3411 | { |
| 3412 | xmlSchemaValPtr swp; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3413 | int order = 1, integx, integy, dlen; |
| 3414 | unsigned long hi, mi, lo; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3415 | |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3416 | /* |
| 3417 | * First test: If x is -ve and not zero |
| 3418 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3419 | if ((x->value.decimal.sign) && |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3420 | ((x->value.decimal.lo != 0) || |
| 3421 | (x->value.decimal.mi != 0) || |
| 3422 | (x->value.decimal.hi != 0))) { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3423 | /* |
| 3424 | * Then if y is -ve and not zero reverse the compare |
| 3425 | */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3426 | if ((y->value.decimal.sign) && |
| 3427 | ((y->value.decimal.lo != 0) || |
| 3428 | (y->value.decimal.mi != 0) || |
| 3429 | (y->value.decimal.hi != 0))) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3430 | order = -1; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3431 | /* |
| 3432 | * Otherwise (y >= 0) we have the answer |
| 3433 | */ |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3434 | else |
| 3435 | return (-1); |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3436 | /* |
| 3437 | * If x is not -ve and y is -ve we have the answer |
| 3438 | */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3439 | } else if ((y->value.decimal.sign) && |
| 3440 | ((y->value.decimal.lo != 0) || |
| 3441 | (y->value.decimal.mi != 0) || |
| 3442 | (y->value.decimal.hi != 0))) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3443 | return (1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3444 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3445 | /* |
| 3446 | * If it's not simply determined by a difference in sign, |
| 3447 | * then we need to compare the actual values of the two nums. |
| 3448 | * To do this, we start by looking at the integral parts. |
| 3449 | * If the number of integral digits differ, then we have our |
| 3450 | * answer. |
| 3451 | */ |
| 3452 | integx = x->value.decimal.total - x->value.decimal.frac; |
| 3453 | integy = y->value.decimal.total - y->value.decimal.frac; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 3454 | /* |
| 3455 | * NOTE: We changed the "total" for values like "0.1" |
| 3456 | * (or "-0.1" or ".1") to be 1, which was 2 previously. |
| 3457 | * Therefore the special case, when such values are |
| 3458 | * compared with 0, needs to be handled separately; |
| 3459 | * otherwise a zero would be recognized incorrectly as |
| 3460 | * greater than those values. This has the nice side effect |
| 3461 | * that we gain an overall optimized comparison with zeroes. |
| 3462 | * Note that a "0" has a "total" of 1 already. |
| 3463 | */ |
| 3464 | if (integx == 1) { |
| 3465 | if (x->value.decimal.lo == 0) { |
| 3466 | if (integy != 1) |
| 3467 | return -order; |
| 3468 | else if (y->value.decimal.lo != 0) |
| 3469 | return -order; |
| 3470 | else |
| 3471 | return(0); |
| 3472 | } |
| 3473 | } |
| 3474 | if (integy == 1) { |
| 3475 | if (y->value.decimal.lo == 0) { |
| 3476 | if (integx != 1) |
| 3477 | return order; |
| 3478 | else if (x->value.decimal.lo != 0) |
| 3479 | return order; |
| 3480 | else |
| 3481 | return(0); |
| 3482 | } |
| 3483 | } |
| 3484 | |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3485 | if (integx > integy) |
| 3486 | return order; |
| 3487 | else if (integy > integx) |
| 3488 | return -order; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 3489 | |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3490 | /* |
| 3491 | * If the number of integral digits is the same for both numbers, |
| 3492 | * then things get a little more complicated. We need to "normalize" |
| 3493 | * the numbers in order to properly compare them. To do this, we |
| 3494 | * look at the total length of each number (length => number of |
| 3495 | * significant digits), and divide the "shorter" by 10 (decreasing |
| 3496 | * the length) until they are of equal length. |
| 3497 | */ |
| 3498 | dlen = x->value.decimal.total - y->value.decimal.total; |
| 3499 | if (dlen < 0) { /* y has more digits than x */ |
| 3500 | swp = x; |
| 3501 | hi = y->value.decimal.hi; |
| 3502 | mi = y->value.decimal.mi; |
| 3503 | lo = y->value.decimal.lo; |
| 3504 | dlen = -dlen; |
| 3505 | order = -order; |
| 3506 | } else { /* x has more digits than y */ |
| 3507 | swp = y; |
| 3508 | hi = x->value.decimal.hi; |
| 3509 | mi = x->value.decimal.mi; |
| 3510 | lo = x->value.decimal.lo; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3511 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3512 | while (dlen > 8) { /* in effect, right shift by 10**8 */ |
| 3513 | lo = mi; |
| 3514 | mi = hi; |
| 3515 | hi = 0; |
| 3516 | dlen -= 8; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3517 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3518 | while (dlen > 0) { |
| 3519 | unsigned long rem1, rem2; |
| 3520 | rem1 = (hi % 10) * 100000000L; |
| 3521 | hi = hi / 10; |
| 3522 | rem2 = (mi % 10) * 100000000L; |
| 3523 | mi = (mi + rem1) / 10; |
| 3524 | lo = (lo + rem2) / 10; |
| 3525 | dlen--; |
| 3526 | } |
| 3527 | if (hi > swp->value.decimal.hi) { |
| 3528 | return order; |
| 3529 | } else if (hi == swp->value.decimal.hi) { |
| 3530 | if (mi > swp->value.decimal.mi) { |
| 3531 | return order; |
| 3532 | } else if (mi == swp->value.decimal.mi) { |
| 3533 | if (lo > swp->value.decimal.lo) { |
| 3534 | return order; |
| 3535 | } else if (lo == swp->value.decimal.lo) { |
| 3536 | if (x->value.decimal.total == y->value.decimal.total) { |
| 3537 | return 0; |
| 3538 | } else { |
| 3539 | return order; |
| 3540 | } |
| 3541 | } |
| 3542 | } |
| 3543 | } |
| 3544 | return -order; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | /** |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3548 | * xmlSchemaCompareDurations: |
| 3549 | * @x: a first duration value |
| 3550 | * @y: a second duration value |
| 3551 | * |
| 3552 | * Compare 2 durations |
| 3553 | * |
| 3554 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 3555 | * case of error |
| 3556 | */ |
| 3557 | static int |
| 3558 | xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3559 | { |
| 3560 | long carry, mon, day; |
| 3561 | double sec; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3562 | int invert = 1; |
| 3563 | long xmon, xday, myear, minday, maxday; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3564 | static const long dayRange [2][12] = { |
| 3565 | { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, }, |
| 3566 | { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} }; |
| 3567 | |
| 3568 | if ((x == NULL) || (y == NULL)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3569 | return -2; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3570 | |
| 3571 | /* months */ |
| 3572 | mon = x->value.dur.mon - y->value.dur.mon; |
| 3573 | |
| 3574 | /* seconds */ |
| 3575 | sec = x->value.dur.sec - y->value.dur.sec; |
Daniel Veillard | bbcf127 | 2011-11-10 23:23:10 +0800 | [diff] [blame] | 3576 | carry = (long)(sec / SECS_PER_DAY); |
| 3577 | sec -= ((double)carry) * SECS_PER_DAY; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3578 | |
| 3579 | /* days */ |
| 3580 | day = x->value.dur.day - y->value.dur.day + carry; |
| 3581 | |
| 3582 | /* easy test */ |
| 3583 | if (mon == 0) { |
| 3584 | if (day == 0) |
| 3585 | if (sec == 0.0) |
| 3586 | return 0; |
| 3587 | else if (sec < 0.0) |
| 3588 | return -1; |
| 3589 | else |
| 3590 | return 1; |
| 3591 | else if (day < 0) |
| 3592 | return -1; |
| 3593 | else |
| 3594 | return 1; |
| 3595 | } |
| 3596 | |
| 3597 | if (mon > 0) { |
| 3598 | if ((day >= 0) && (sec >= 0.0)) |
| 3599 | return 1; |
| 3600 | else { |
| 3601 | xmon = mon; |
| 3602 | xday = -day; |
| 3603 | } |
| 3604 | } else if ((day <= 0) && (sec <= 0.0)) { |
| 3605 | return -1; |
| 3606 | } else { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3607 | invert = -1; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3608 | xmon = -mon; |
| 3609 | xday = day; |
| 3610 | } |
| 3611 | |
| 3612 | myear = xmon / 12; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3613 | if (myear == 0) { |
| 3614 | minday = 0; |
| 3615 | maxday = 0; |
| 3616 | } else { |
| 3617 | maxday = 366 * ((myear + 3) / 4) + |
| 3618 | 365 * ((myear - 1) % 4); |
| 3619 | minday = maxday - 1; |
| 3620 | } |
| 3621 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3622 | xmon = xmon % 12; |
| 3623 | minday += dayRange[0][xmon]; |
| 3624 | maxday += dayRange[1][xmon]; |
| 3625 | |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3626 | if ((maxday == minday) && (maxday == xday)) |
| 3627 | return(0); /* can this really happen ? */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3628 | if (maxday < xday) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3629 | return(-invert); |
| 3630 | if (minday > xday) |
| 3631 | return(invert); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3632 | |
| 3633 | /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3634 | return 2; |
| 3635 | } |
| 3636 | |
| 3637 | /* |
| 3638 | * macros for adding date/times and durations |
| 3639 | */ |
| 3640 | #define FQUOTIENT(a,b) (floor(((double)a/(double)b))) |
| 3641 | #define MODULO(a,b) (a - FQUOTIENT(a,b) * b) |
| 3642 | #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low))) |
| 3643 | #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low) |
| 3644 | |
| 3645 | /** |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3646 | * xmlSchemaDupVal: |
| 3647 | * @v: the #xmlSchemaValPtr value to duplicate |
| 3648 | * |
| 3649 | * Makes a copy of @v. The calling program is responsible for freeing |
| 3650 | * the returned value. |
| 3651 | * |
| 3652 | * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error. |
| 3653 | */ |
| 3654 | static xmlSchemaValPtr |
| 3655 | xmlSchemaDupVal (xmlSchemaValPtr v) |
| 3656 | { |
| 3657 | xmlSchemaValPtr ret = xmlSchemaNewValue(v->type); |
| 3658 | if (ret == NULL) |
| 3659 | return NULL; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3660 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3661 | memcpy(ret, v, sizeof(xmlSchemaVal)); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3662 | ret->next = NULL; |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3663 | return ret; |
| 3664 | } |
| 3665 | |
| 3666 | /** |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3667 | * xmlSchemaCopyValue: |
| 3668 | * @val: the precomputed value to be copied |
| 3669 | * |
| 3670 | * Copies the precomputed value. This duplicates any string within. |
| 3671 | * |
| 3672 | * Returns the copy or NULL if a copy for a data-type is not implemented. |
| 3673 | */ |
| 3674 | xmlSchemaValPtr |
| 3675 | xmlSchemaCopyValue(xmlSchemaValPtr val) |
| 3676 | { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3677 | xmlSchemaValPtr ret = NULL, prev = NULL, cur; |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3678 | |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3679 | /* |
| 3680 | * Copy the string values. |
| 3681 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3682 | while (val != NULL) { |
| 3683 | switch (val->type) { |
| 3684 | case XML_SCHEMAS_ANYTYPE: |
| 3685 | case XML_SCHEMAS_IDREFS: |
| 3686 | case XML_SCHEMAS_ENTITIES: |
| 3687 | case XML_SCHEMAS_NMTOKENS: |
| 3688 | xmlSchemaFreeValue(ret); |
| 3689 | return (NULL); |
| 3690 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 3691 | case XML_SCHEMAS_STRING: |
| 3692 | case XML_SCHEMAS_NORMSTRING: |
| 3693 | case XML_SCHEMAS_TOKEN: |
| 3694 | case XML_SCHEMAS_LANGUAGE: |
| 3695 | case XML_SCHEMAS_NAME: |
| 3696 | case XML_SCHEMAS_NCNAME: |
| 3697 | case XML_SCHEMAS_ID: |
| 3698 | case XML_SCHEMAS_IDREF: |
| 3699 | case XML_SCHEMAS_ENTITY: |
| 3700 | case XML_SCHEMAS_NMTOKEN: |
| 3701 | case XML_SCHEMAS_ANYURI: |
| 3702 | cur = xmlSchemaDupVal(val); |
| 3703 | if (val->value.str != NULL) |
| 3704 | cur->value.str = xmlStrdup(BAD_CAST val->value.str); |
| 3705 | break; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3706 | case XML_SCHEMAS_QNAME: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3707 | case XML_SCHEMAS_NOTATION: |
| 3708 | cur = xmlSchemaDupVal(val); |
| 3709 | if (val->value.qname.name != NULL) |
| 3710 | cur->value.qname.name = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3711 | xmlStrdup(BAD_CAST val->value.qname.name); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3712 | if (val->value.qname.uri != NULL) |
| 3713 | cur->value.qname.uri = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3714 | xmlStrdup(BAD_CAST val->value.qname.uri); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3715 | break; |
| 3716 | case XML_SCHEMAS_HEXBINARY: |
| 3717 | cur = xmlSchemaDupVal(val); |
| 3718 | if (val->value.hex.str != NULL) |
| 3719 | cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str); |
| 3720 | break; |
| 3721 | case XML_SCHEMAS_BASE64BINARY: |
| 3722 | cur = xmlSchemaDupVal(val); |
| 3723 | if (val->value.base64.str != NULL) |
| 3724 | cur->value.base64.str = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3725 | xmlStrdup(BAD_CAST val->value.base64.str); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3726 | break; |
| 3727 | default: |
| 3728 | cur = xmlSchemaDupVal(val); |
| 3729 | break; |
| 3730 | } |
| 3731 | if (ret == NULL) |
| 3732 | ret = cur; |
| 3733 | else |
| 3734 | prev->next = cur; |
| 3735 | prev = cur; |
| 3736 | val = val->next; |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3737 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3738 | return (ret); |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3742 | * _xmlSchemaDateAdd: |
| 3743 | * @dt: an #xmlSchemaValPtr |
| 3744 | * @dur: an #xmlSchemaValPtr of type #XS_DURATION |
| 3745 | * |
| 3746 | * Compute a new date/time from @dt and @dur. This function assumes @dt |
| 3747 | * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH, |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3748 | * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as |
| 3749 | * @dt. The calling program is responsible for freeing the returned value. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3750 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3751 | * Returns a pointer to a new #xmlSchemaVal or NULL if error. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3752 | */ |
| 3753 | static xmlSchemaValPtr |
| 3754 | _xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur) |
| 3755 | { |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3756 | xmlSchemaValPtr ret, tmp; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3757 | long carry, tempdays, temp; |
| 3758 | xmlSchemaValDatePtr r, d; |
| 3759 | xmlSchemaValDurationPtr u; |
| 3760 | |
| 3761 | if ((dt == NULL) || (dur == NULL)) |
| 3762 | return NULL; |
| 3763 | |
| 3764 | ret = xmlSchemaNewValue(dt->type); |
| 3765 | if (ret == NULL) |
| 3766 | return NULL; |
| 3767 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3768 | /* make a copy so we don't alter the original value */ |
| 3769 | tmp = xmlSchemaDupVal(dt); |
| 3770 | if (tmp == NULL) { |
| 3771 | xmlSchemaFreeValue(ret); |
| 3772 | return NULL; |
| 3773 | } |
| 3774 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3775 | r = &(ret->value.date); |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3776 | d = &(tmp->value.date); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3777 | u = &(dur->value.dur); |
| 3778 | |
| 3779 | /* normalization */ |
| 3780 | if (d->mon == 0) |
| 3781 | d->mon = 1; |
| 3782 | |
| 3783 | /* normalize for time zone offset */ |
| 3784 | u->sec -= (d->tzo * 60); |
| 3785 | d->tzo = 0; |
| 3786 | |
| 3787 | /* normalization */ |
| 3788 | if (d->day == 0) |
| 3789 | d->day = 1; |
| 3790 | |
| 3791 | /* month */ |
| 3792 | carry = d->mon + u->mon; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3793 | r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13); |
| 3794 | carry = (long) FQUOTIENT_RANGE(carry, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3795 | |
| 3796 | /* year (may be modified later) */ |
| 3797 | r->year = d->year + carry; |
| 3798 | if (r->year == 0) { |
| 3799 | if (d->year > 0) |
| 3800 | r->year--; |
| 3801 | else |
| 3802 | r->year++; |
| 3803 | } |
| 3804 | |
| 3805 | /* time zone */ |
| 3806 | r->tzo = d->tzo; |
| 3807 | r->tz_flag = d->tz_flag; |
| 3808 | |
| 3809 | /* seconds */ |
| 3810 | r->sec = d->sec + u->sec; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3811 | carry = (long) FQUOTIENT((long)r->sec, 60); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3812 | if (r->sec != 0.0) { |
| 3813 | r->sec = MODULO(r->sec, 60.0); |
| 3814 | } |
| 3815 | |
| 3816 | /* minute */ |
| 3817 | carry += d->min; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3818 | r->min = (unsigned int) MODULO(carry, 60); |
| 3819 | carry = (long) FQUOTIENT(carry, 60); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3820 | |
| 3821 | /* hours */ |
| 3822 | carry += d->hour; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3823 | r->hour = (unsigned int) MODULO(carry, 24); |
| 3824 | carry = (long)FQUOTIENT(carry, 24); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3825 | |
| 3826 | /* |
| 3827 | * days |
| 3828 | * Note we use tempdays because the temporary values may need more |
| 3829 | * than 5 bits |
| 3830 | */ |
| 3831 | if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) && |
| 3832 | (d->day > MAX_DAYINMONTH(r->year, r->mon))) |
| 3833 | tempdays = MAX_DAYINMONTH(r->year, r->mon); |
| 3834 | else if (d->day < 1) |
| 3835 | tempdays = 1; |
| 3836 | else |
| 3837 | tempdays = d->day; |
| 3838 | |
| 3839 | tempdays += u->day + carry; |
| 3840 | |
| 3841 | while (1) { |
| 3842 | if (tempdays < 1) { |
Daniel Veillard | 5e9576a | 2005-11-21 11:23:47 +0000 | [diff] [blame] | 3843 | long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13); |
| 3844 | long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3845 | if (tyr == 0) |
| 3846 | tyr--; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3847 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3848 | * Coverity detected an overrun in daysInMonth |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3849 | * of size 12 at position 12 with index variable "((r)->mon - 1)" |
| 3850 | */ |
David Kilzer | 30cf439 | 2014-07-14 22:29:56 +0800 | [diff] [blame] | 3851 | if (tmon < 1) |
| 3852 | tmon = 1; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3853 | if (tmon > 12) |
| 3854 | tmon = 12; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3855 | tempdays += MAX_DAYINMONTH(tyr, tmon); |
| 3856 | carry = -1; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3857 | } else if (tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3858 | tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon); |
| 3859 | carry = 1; |
| 3860 | } else |
| 3861 | break; |
| 3862 | |
| 3863 | temp = r->mon + carry; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3864 | r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13); |
| 3865 | r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3866 | if (r->year == 0) { |
| 3867 | if (temp < 1) |
| 3868 | r->year--; |
| 3869 | else |
| 3870 | r->year++; |
| 3871 | } |
| 3872 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3873 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3874 | r->day = tempdays; |
| 3875 | |
| 3876 | /* |
| 3877 | * adjust the date/time type to the date values |
| 3878 | */ |
| 3879 | if (ret->type != XML_SCHEMAS_DATETIME) { |
| 3880 | if ((r->hour) || (r->min) || (r->sec)) |
| 3881 | ret->type = XML_SCHEMAS_DATETIME; |
| 3882 | else if (ret->type != XML_SCHEMAS_DATE) { |
| 3883 | if ((r->mon != 1) && (r->day != 1)) |
| 3884 | ret->type = XML_SCHEMAS_DATE; |
| 3885 | else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1)) |
| 3886 | ret->type = XML_SCHEMAS_GYEARMONTH; |
| 3887 | } |
| 3888 | } |
| 3889 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3890 | xmlSchemaFreeValue(tmp); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3891 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3892 | return ret; |
| 3893 | } |
| 3894 | |
| 3895 | /** |
| 3896 | * xmlSchemaDateNormalize: |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3897 | * @dt: an #xmlSchemaValPtr of a date/time type value. |
| 3898 | * @offset: number of seconds to adjust @dt by. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3899 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3900 | * Normalize @dt to GMT time. The @offset parameter is subtracted from |
| 3901 | * the return value is a time-zone offset is present on @dt. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3902 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3903 | * Returns a normalized copy of @dt or NULL if error. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3904 | */ |
| 3905 | static xmlSchemaValPtr |
| 3906 | xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset) |
| 3907 | { |
| 3908 | xmlSchemaValPtr dur, ret; |
| 3909 | |
| 3910 | if (dt == NULL) |
| 3911 | return NULL; |
| 3912 | |
| 3913 | if (((dt->type != XML_SCHEMAS_TIME) && |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 3914 | (dt->type != XML_SCHEMAS_DATETIME) && |
| 3915 | (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3916 | return xmlSchemaDupVal(dt); |
| 3917 | |
| 3918 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 3919 | if (dur == NULL) |
| 3920 | return NULL; |
| 3921 | |
| 3922 | dur->value.date.sec -= offset; |
| 3923 | |
| 3924 | ret = _xmlSchemaDateAdd(dt, dur); |
| 3925 | if (ret == NULL) |
| 3926 | return NULL; |
| 3927 | |
| 3928 | xmlSchemaFreeValue(dur); |
| 3929 | |
| 3930 | /* ret->value.date.tzo = 0; */ |
| 3931 | return ret; |
| 3932 | } |
| 3933 | |
| 3934 | /** |
| 3935 | * _xmlSchemaDateCastYMToDays: |
| 3936 | * @dt: an #xmlSchemaValPtr |
| 3937 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3938 | * Convert mon and year of @dt to total number of days. Take the |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3939 | * number of years since (or before) 1 AD and add the number of leap |
| 3940 | * years. This is a function because negative |
| 3941 | * years must be handled a little differently and there is no zero year. |
| 3942 | * |
| 3943 | * Returns number of days. |
| 3944 | */ |
| 3945 | static long |
| 3946 | _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt) |
| 3947 | { |
| 3948 | long ret; |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3949 | int mon; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3950 | |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3951 | mon = dt->value.date.mon; |
| 3952 | if (mon <= 0) mon = 1; /* normalization */ |
| 3953 | |
| 3954 | if (dt->value.date.year <= 0) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3955 | ret = (dt->value.date.year * 365) + |
| 3956 | (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+ |
| 3957 | ((dt->value.date.year+1)/400)) + |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3958 | DAY_IN_YEAR(0, mon, dt->value.date.year); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3959 | else |
| 3960 | ret = ((dt->value.date.year-1) * 365) + |
| 3961 | (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+ |
| 3962 | ((dt->value.date.year-1)/400)) + |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3963 | DAY_IN_YEAR(0, mon, dt->value.date.year); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3964 | |
| 3965 | return ret; |
| 3966 | } |
| 3967 | |
| 3968 | /** |
| 3969 | * TIME_TO_NUMBER: |
| 3970 | * @dt: an #xmlSchemaValPtr |
| 3971 | * |
| 3972 | * Calculates the number of seconds in the time portion of @dt. |
| 3973 | * |
| 3974 | * Returns seconds. |
| 3975 | */ |
| 3976 | #define TIME_TO_NUMBER(dt) \ |
| 3977 | ((double)((dt->value.date.hour * SECS_PER_HOUR) + \ |
Daniel Veillard | b3721c2 | 2003-03-31 11:22:25 +0000 | [diff] [blame] | 3978 | (dt->value.date.min * SECS_PER_MIN) + \ |
| 3979 | (dt->value.date.tzo * SECS_PER_MIN)) + \ |
| 3980 | dt->value.date.sec) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3981 | |
| 3982 | /** |
| 3983 | * xmlSchemaCompareDates: |
| 3984 | * @x: a first date/time value |
| 3985 | * @y: a second date/time value |
| 3986 | * |
| 3987 | * Compare 2 date/times |
| 3988 | * |
| 3989 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 3990 | * case of error |
| 3991 | */ |
| 3992 | static int |
| 3993 | xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3994 | { |
| 3995 | unsigned char xmask, ymask, xor_mask, and_mask; |
| 3996 | xmlSchemaValPtr p1, p2, q1, q2; |
| 3997 | long p1d, p2d, q1d, q2d; |
| 3998 | |
| 3999 | if ((x == NULL) || (y == NULL)) |
| 4000 | return -2; |
| 4001 | |
| 4002 | if (x->value.date.tz_flag) { |
| 4003 | |
| 4004 | if (!y->value.date.tz_flag) { |
| 4005 | p1 = xmlSchemaDateNormalize(x, 0); |
| 4006 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 4007 | /* normalize y + 14:00 */ |
| 4008 | q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR)); |
| 4009 | |
| 4010 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4011 | if (p1d < q1d) { |
| 4012 | xmlSchemaFreeValue(p1); |
| 4013 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4014 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4015 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4016 | double sec; |
| 4017 | |
| 4018 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4019 | if (sec < 0.0) { |
| 4020 | xmlSchemaFreeValue(p1); |
| 4021 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4022 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4023 | } else { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4024 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4025 | /* normalize y - 14:00 */ |
| 4026 | q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR)); |
| 4027 | q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day; |
| 4028 | if (p1d > q2d) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4029 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4030 | else if (p1d == q2d) { |
| 4031 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2); |
| 4032 | if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4033 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4034 | else |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4035 | ret = 2; /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4036 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4037 | xmlSchemaFreeValue(p1); |
| 4038 | xmlSchemaFreeValue(q1); |
| 4039 | xmlSchemaFreeValue(q2); |
| 4040 | if (ret != 0) |
| 4041 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4042 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4043 | } else { |
| 4044 | xmlSchemaFreeValue(p1); |
| 4045 | xmlSchemaFreeValue(q1); |
| 4046 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4047 | } |
| 4048 | } else if (y->value.date.tz_flag) { |
| 4049 | q1 = xmlSchemaDateNormalize(y, 0); |
| 4050 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 4051 | |
| 4052 | /* normalize x - 14:00 */ |
| 4053 | p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR)); |
| 4054 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 4055 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4056 | if (p1d < q1d) { |
| 4057 | xmlSchemaFreeValue(p1); |
| 4058 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4059 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4060 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4061 | double sec; |
| 4062 | |
| 4063 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4064 | if (sec < 0.0) { |
| 4065 | xmlSchemaFreeValue(p1); |
| 4066 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4067 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4068 | } else { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4069 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4070 | /* normalize x + 14:00 */ |
| 4071 | p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR)); |
| 4072 | p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day; |
| 4073 | |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 4074 | if (p2d > q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4075 | ret = 1; |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 4076 | } else if (p2d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4077 | sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1); |
| 4078 | if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4079 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4080 | else |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4081 | ret = 2; /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4082 | } |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 4083 | xmlSchemaFreeValue(p1); |
| 4084 | xmlSchemaFreeValue(q1); |
| 4085 | xmlSchemaFreeValue(p2); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4086 | if (ret != 0) |
| 4087 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4088 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4089 | } else { |
| 4090 | xmlSchemaFreeValue(p1); |
| 4091 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | |
| 4095 | /* |
| 4096 | * if the same type then calculate the difference |
| 4097 | */ |
| 4098 | if (x->type == y->type) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4099 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4100 | q1 = xmlSchemaDateNormalize(y, 0); |
| 4101 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 4102 | |
| 4103 | p1 = xmlSchemaDateNormalize(x, 0); |
| 4104 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 4105 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4106 | if (p1d < q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4107 | ret = -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4108 | } else if (p1d > q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4109 | ret = 1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4110 | } else { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4111 | double sec; |
| 4112 | |
| 4113 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
| 4114 | if (sec < 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4115 | ret = -1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4116 | else if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4117 | ret = 1; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4118 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4119 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4120 | xmlSchemaFreeValue(p1); |
| 4121 | xmlSchemaFreeValue(q1); |
| 4122 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | switch (x->type) { |
| 4126 | case XML_SCHEMAS_DATETIME: |
| 4127 | xmask = 0xf; |
| 4128 | break; |
| 4129 | case XML_SCHEMAS_DATE: |
| 4130 | xmask = 0x7; |
| 4131 | break; |
| 4132 | case XML_SCHEMAS_GYEAR: |
| 4133 | xmask = 0x1; |
| 4134 | break; |
| 4135 | case XML_SCHEMAS_GMONTH: |
| 4136 | xmask = 0x2; |
| 4137 | break; |
| 4138 | case XML_SCHEMAS_GDAY: |
| 4139 | xmask = 0x3; |
| 4140 | break; |
| 4141 | case XML_SCHEMAS_GYEARMONTH: |
| 4142 | xmask = 0x3; |
| 4143 | break; |
| 4144 | case XML_SCHEMAS_GMONTHDAY: |
| 4145 | xmask = 0x6; |
| 4146 | break; |
| 4147 | case XML_SCHEMAS_TIME: |
| 4148 | xmask = 0x8; |
| 4149 | break; |
| 4150 | default: |
| 4151 | xmask = 0; |
| 4152 | break; |
| 4153 | } |
| 4154 | |
| 4155 | switch (y->type) { |
| 4156 | case XML_SCHEMAS_DATETIME: |
| 4157 | ymask = 0xf; |
| 4158 | break; |
| 4159 | case XML_SCHEMAS_DATE: |
| 4160 | ymask = 0x7; |
| 4161 | break; |
| 4162 | case XML_SCHEMAS_GYEAR: |
| 4163 | ymask = 0x1; |
| 4164 | break; |
| 4165 | case XML_SCHEMAS_GMONTH: |
| 4166 | ymask = 0x2; |
| 4167 | break; |
| 4168 | case XML_SCHEMAS_GDAY: |
| 4169 | ymask = 0x3; |
| 4170 | break; |
| 4171 | case XML_SCHEMAS_GYEARMONTH: |
| 4172 | ymask = 0x3; |
| 4173 | break; |
| 4174 | case XML_SCHEMAS_GMONTHDAY: |
| 4175 | ymask = 0x6; |
| 4176 | break; |
| 4177 | case XML_SCHEMAS_TIME: |
| 4178 | ymask = 0x8; |
| 4179 | break; |
| 4180 | default: |
| 4181 | ymask = 0; |
| 4182 | break; |
| 4183 | } |
| 4184 | |
| 4185 | xor_mask = xmask ^ ymask; /* mark type differences */ |
| 4186 | and_mask = xmask & ymask; /* mark field specification */ |
| 4187 | |
| 4188 | /* year */ |
| 4189 | if (xor_mask & 1) |
| 4190 | return 2; /* indeterminate */ |
| 4191 | else if (and_mask & 1) { |
| 4192 | if (x->value.date.year < y->value.date.year) |
| 4193 | return -1; |
| 4194 | else if (x->value.date.year > y->value.date.year) |
| 4195 | return 1; |
| 4196 | } |
| 4197 | |
| 4198 | /* month */ |
| 4199 | if (xor_mask & 2) |
| 4200 | return 2; /* indeterminate */ |
| 4201 | else if (and_mask & 2) { |
| 4202 | if (x->value.date.mon < y->value.date.mon) |
| 4203 | return -1; |
| 4204 | else if (x->value.date.mon > y->value.date.mon) |
| 4205 | return 1; |
| 4206 | } |
| 4207 | |
| 4208 | /* day */ |
| 4209 | if (xor_mask & 4) |
| 4210 | return 2; /* indeterminate */ |
| 4211 | else if (and_mask & 4) { |
| 4212 | if (x->value.date.day < y->value.date.day) |
| 4213 | return -1; |
| 4214 | else if (x->value.date.day > y->value.date.day) |
| 4215 | return 1; |
| 4216 | } |
| 4217 | |
| 4218 | /* time */ |
| 4219 | if (xor_mask & 8) |
| 4220 | return 2; /* indeterminate */ |
| 4221 | else if (and_mask & 8) { |
| 4222 | if (x->value.date.hour < y->value.date.hour) |
| 4223 | return -1; |
| 4224 | else if (x->value.date.hour > y->value.date.hour) |
| 4225 | return 1; |
| 4226 | else if (x->value.date.min < y->value.date.min) |
| 4227 | return -1; |
| 4228 | else if (x->value.date.min > y->value.date.min) |
| 4229 | return 1; |
| 4230 | else if (x->value.date.sec < y->value.date.sec) |
| 4231 | return -1; |
| 4232 | else if (x->value.date.sec > y->value.date.sec) |
| 4233 | return 1; |
| 4234 | } |
| 4235 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4236 | return 0; |
| 4237 | } |
| 4238 | |
| 4239 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4240 | * xmlSchemaComparePreserveReplaceStrings: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4241 | * @x: a first string value |
| 4242 | * @y: a second string value |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4243 | * @invert: inverts the result if x < y or x > y. |
| 4244 | * |
| 4245 | * Compare 2 string for their normalized values. |
| 4246 | * @x is a string with whitespace of "preserve", @y is |
| 4247 | * a string with a whitespace of "replace". I.e. @x could |
| 4248 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4249 | * |
| 4250 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4251 | * case of error |
| 4252 | */ |
| 4253 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4254 | xmlSchemaComparePreserveReplaceStrings(const xmlChar *x, |
| 4255 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4256 | int invert) |
| 4257 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4258 | int tmp; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4259 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4260 | while ((*x != 0) && (*y != 0)) { |
| 4261 | if (IS_WSP_REPLACE_CH(*y)) { |
| 4262 | if (! IS_WSP_SPACE_CH(*x)) { |
| 4263 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4264 | if (invert) |
| 4265 | return(1); |
| 4266 | else |
| 4267 | return(-1); |
| 4268 | } else { |
| 4269 | if (invert) |
| 4270 | return(-1); |
| 4271 | else |
| 4272 | return(1); |
| 4273 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4274 | } |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4275 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4276 | tmp = *x - *y; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4277 | if (tmp < 0) { |
| 4278 | if (invert) |
| 4279 | return(1); |
| 4280 | else |
| 4281 | return(-1); |
| 4282 | } |
| 4283 | if (tmp > 0) { |
| 4284 | if (invert) |
| 4285 | return(-1); |
| 4286 | else |
| 4287 | return(1); |
| 4288 | } |
| 4289 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4290 | x++; |
| 4291 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4292 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4293 | if (*x != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4294 | if (invert) |
| 4295 | return(-1); |
| 4296 | else |
| 4297 | return(1); |
| 4298 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4299 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4300 | if (invert) |
| 4301 | return(1); |
| 4302 | else |
| 4303 | return(-1); |
| 4304 | } |
| 4305 | return(0); |
| 4306 | } |
| 4307 | |
| 4308 | /** |
| 4309 | * xmlSchemaComparePreserveCollapseStrings: |
| 4310 | * @x: a first string value |
| 4311 | * @y: a second string value |
| 4312 | * |
| 4313 | * Compare 2 string for their normalized values. |
| 4314 | * @x is a string with whitespace of "preserve", @y is |
| 4315 | * a string with a whitespace of "collapse". I.e. @x could |
| 4316 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4317 | * |
| 4318 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4319 | * case of error |
| 4320 | */ |
| 4321 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4322 | xmlSchemaComparePreserveCollapseStrings(const xmlChar *x, |
| 4323 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4324 | int invert) |
| 4325 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4326 | int tmp; |
| 4327 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4328 | /* |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4329 | * Skip leading blank chars of the collapsed string. |
| 4330 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4331 | while IS_WSP_BLANK_CH(*y) |
| 4332 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4333 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4334 | while ((*x != 0) && (*y != 0)) { |
| 4335 | if IS_WSP_BLANK_CH(*y) { |
| 4336 | if (! IS_WSP_SPACE_CH(*x)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4337 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4338 | * The yv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4339 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4340 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4341 | if (invert) |
| 4342 | return(1); |
| 4343 | else |
| 4344 | return(-1); |
| 4345 | } else { |
| 4346 | if (invert) |
| 4347 | return(-1); |
| 4348 | else |
| 4349 | return(1); |
| 4350 | } |
| 4351 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4352 | x++; |
| 4353 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4354 | /* |
| 4355 | * Skip contiguous blank chars of the collapsed string. |
| 4356 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4357 | while IS_WSP_BLANK_CH(*y) |
| 4358 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4359 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4360 | tmp = *x++ - *y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4361 | if (tmp < 0) { |
| 4362 | if (invert) |
| 4363 | return(1); |
| 4364 | else |
| 4365 | return(-1); |
| 4366 | } |
| 4367 | if (tmp > 0) { |
| 4368 | if (invert) |
| 4369 | return(-1); |
| 4370 | else |
| 4371 | return(1); |
| 4372 | } |
| 4373 | } |
| 4374 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4375 | if (*x != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4376 | if (invert) |
| 4377 | return(-1); |
| 4378 | else |
| 4379 | return(1); |
| 4380 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4381 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4382 | /* |
| 4383 | * Skip trailing blank chars of the collapsed string. |
| 4384 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4385 | while IS_WSP_BLANK_CH(*y) |
| 4386 | y++; |
| 4387 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4388 | if (invert) |
| 4389 | return(1); |
| 4390 | else |
| 4391 | return(-1); |
| 4392 | } |
| 4393 | } |
| 4394 | return(0); |
| 4395 | } |
| 4396 | |
| 4397 | /** |
| 4398 | * xmlSchemaComparePreserveCollapseStrings: |
| 4399 | * @x: a first string value |
| 4400 | * @y: a second string value |
| 4401 | * |
| 4402 | * Compare 2 string for their normalized values. |
| 4403 | * @x is a string with whitespace of "preserve", @y is |
| 4404 | * a string with a whitespace of "collapse". I.e. @x could |
| 4405 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4406 | * |
| 4407 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4408 | * case of error |
| 4409 | */ |
| 4410 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4411 | xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x, |
| 4412 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4413 | int invert) |
| 4414 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4415 | int tmp; |
| 4416 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4417 | /* |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4418 | * Skip leading blank chars of the collapsed string. |
| 4419 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4420 | while IS_WSP_BLANK_CH(*y) |
| 4421 | y++; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4422 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4423 | while ((*x != 0) && (*y != 0)) { |
| 4424 | if IS_WSP_BLANK_CH(*y) { |
| 4425 | if (! IS_WSP_BLANK_CH(*x)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4426 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4427 | * The yv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4428 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4429 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4430 | if (invert) |
| 4431 | return(1); |
| 4432 | else |
| 4433 | return(-1); |
| 4434 | } else { |
| 4435 | if (invert) |
| 4436 | return(-1); |
| 4437 | else |
| 4438 | return(1); |
| 4439 | } |
| 4440 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4441 | x++; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4442 | y++; |
| 4443 | /* |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4444 | * Skip contiguous blank chars of the collapsed string. |
| 4445 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4446 | while IS_WSP_BLANK_CH(*y) |
| 4447 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4448 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4449 | if IS_WSP_BLANK_CH(*x) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4450 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4451 | * The xv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4452 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4453 | if ((0x20 - *y) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4454 | if (invert) |
| 4455 | return(1); |
| 4456 | else |
| 4457 | return(-1); |
| 4458 | } else { |
| 4459 | if (invert) |
| 4460 | return(-1); |
| 4461 | else |
| 4462 | return(1); |
| 4463 | } |
| 4464 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4465 | tmp = *x++ - *y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4466 | if (tmp < 0) |
| 4467 | return(-1); |
| 4468 | if (tmp > 0) |
| 4469 | return(1); |
| 4470 | } |
| 4471 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4472 | if (*x != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4473 | if (invert) |
| 4474 | return(-1); |
| 4475 | else |
| 4476 | return(1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4477 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4478 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4479 | /* |
| 4480 | * Skip trailing blank chars of the collapsed string. |
| 4481 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4482 | while IS_WSP_BLANK_CH(*y) |
| 4483 | y++; |
| 4484 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4485 | if (invert) |
| 4486 | return(1); |
| 4487 | else |
| 4488 | return(-1); |
| 4489 | } |
| 4490 | } |
| 4491 | return(0); |
| 4492 | } |
| 4493 | |
| 4494 | |
| 4495 | /** |
| 4496 | * xmlSchemaCompareReplacedStrings: |
| 4497 | * @x: a first string value |
| 4498 | * @y: a second string value |
| 4499 | * |
| 4500 | * Compare 2 string for their normalized values. |
| 4501 | * |
| 4502 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4503 | * case of error |
| 4504 | */ |
| 4505 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4506 | xmlSchemaCompareReplacedStrings(const xmlChar *x, |
| 4507 | const xmlChar *y) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4508 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4509 | int tmp; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4510 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4511 | while ((*x != 0) && (*y != 0)) { |
| 4512 | if IS_WSP_BLANK_CH(*y) { |
| 4513 | if (! IS_WSP_BLANK_CH(*x)) { |
| 4514 | if ((*x - 0x20) < 0) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4515 | return(-1); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4516 | else |
| 4517 | return(1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4518 | } |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4519 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4520 | if IS_WSP_BLANK_CH(*x) { |
| 4521 | if ((0x20 - *y) < 0) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4522 | return(-1); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4523 | else |
| 4524 | return(1); |
| 4525 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4526 | tmp = *x - *y; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4527 | if (tmp < 0) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4528 | return(-1); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4529 | if (tmp > 0) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4530 | return(1); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4531 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4532 | x++; |
| 4533 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4534 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4535 | if (*x != 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4536 | return(1); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4537 | if (*y != 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4538 | return(-1); |
| 4539 | return(0); |
| 4540 | } |
| 4541 | |
| 4542 | /** |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4543 | * xmlSchemaCompareNormStrings: |
| 4544 | * @x: a first string value |
| 4545 | * @y: a second string value |
| 4546 | * |
| 4547 | * Compare 2 string for their normalized values. |
| 4548 | * |
| 4549 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4550 | * case of error |
| 4551 | */ |
| 4552 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4553 | xmlSchemaCompareNormStrings(const xmlChar *x, |
| 4554 | const xmlChar *y) { |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4555 | int tmp; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4556 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4557 | while (IS_BLANK_CH(*x)) x++; |
| 4558 | while (IS_BLANK_CH(*y)) y++; |
| 4559 | while ((*x != 0) && (*y != 0)) { |
| 4560 | if (IS_BLANK_CH(*x)) { |
| 4561 | if (!IS_BLANK_CH(*y)) { |
| 4562 | tmp = *x - *y; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4563 | return(tmp); |
| 4564 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4565 | while (IS_BLANK_CH(*x)) x++; |
| 4566 | while (IS_BLANK_CH(*y)) y++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4567 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4568 | tmp = *x++ - *y++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4569 | if (tmp < 0) |
| 4570 | return(-1); |
| 4571 | if (tmp > 0) |
| 4572 | return(1); |
| 4573 | } |
| 4574 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4575 | if (*x != 0) { |
| 4576 | while (IS_BLANK_CH(*x)) x++; |
| 4577 | if (*x != 0) |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4578 | return(1); |
| 4579 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4580 | if (*y != 0) { |
| 4581 | while (IS_BLANK_CH(*y)) y++; |
| 4582 | if (*y != 0) |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4583 | return(-1); |
| 4584 | } |
| 4585 | return(0); |
| 4586 | } |
| 4587 | |
| 4588 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4589 | * xmlSchemaCompareFloats: |
| 4590 | * @x: a first float or double value |
| 4591 | * @y: a second float or double value |
| 4592 | * |
| 4593 | * Compare 2 values |
| 4594 | * |
| 4595 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4596 | * case of error |
| 4597 | */ |
| 4598 | static int |
| 4599 | xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 4600 | double d1, d2; |
| 4601 | |
| 4602 | if ((x == NULL) || (y == NULL)) |
| 4603 | return(-2); |
| 4604 | |
| 4605 | /* |
| 4606 | * Cast everything to doubles. |
| 4607 | */ |
| 4608 | if (x->type == XML_SCHEMAS_DOUBLE) |
| 4609 | d1 = x->value.d; |
| 4610 | else if (x->type == XML_SCHEMAS_FLOAT) |
| 4611 | d1 = x->value.f; |
| 4612 | else |
| 4613 | return(-2); |
| 4614 | |
| 4615 | if (y->type == XML_SCHEMAS_DOUBLE) |
| 4616 | d2 = y->value.d; |
| 4617 | else if (y->type == XML_SCHEMAS_FLOAT) |
| 4618 | d2 = y->value.f; |
| 4619 | else |
| 4620 | return(-2); |
| 4621 | |
| 4622 | /* |
| 4623 | * Check for special cases. |
| 4624 | */ |
| 4625 | if (xmlXPathIsNaN(d1)) { |
| 4626 | if (xmlXPathIsNaN(d2)) |
| 4627 | return(0); |
| 4628 | return(1); |
| 4629 | } |
| 4630 | if (xmlXPathIsNaN(d2)) |
| 4631 | return(-1); |
| 4632 | if (d1 == xmlXPathPINF) { |
| 4633 | if (d2 == xmlXPathPINF) |
| 4634 | return(0); |
| 4635 | return(1); |
| 4636 | } |
| 4637 | if (d2 == xmlXPathPINF) |
| 4638 | return(-1); |
| 4639 | if (d1 == xmlXPathNINF) { |
| 4640 | if (d2 == xmlXPathNINF) |
| 4641 | return(0); |
| 4642 | return(-1); |
| 4643 | } |
| 4644 | if (d2 == xmlXPathNINF) |
| 4645 | return(1); |
| 4646 | |
| 4647 | /* |
| 4648 | * basic tests, the last one we should have equality, but |
| 4649 | * portability is more important than speed and handling |
| 4650 | * NaN or Inf in a portable way is always a challenge, so ... |
| 4651 | */ |
| 4652 | if (d1 < d2) |
| 4653 | return(-1); |
| 4654 | if (d1 > d2) |
| 4655 | return(1); |
| 4656 | if (d1 == d2) |
| 4657 | return(0); |
| 4658 | return(2); |
| 4659 | } |
| 4660 | |
| 4661 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4662 | * xmlSchemaCompareValues: |
| 4663 | * @x: a first value |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4664 | * @xvalue: the first value as a string (optional) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4665 | * @xwtsp: the whitespace type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4666 | * @y: a second value |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4667 | * @xvalue: the second value as a string (optional) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4668 | * @ywtsp: the whitespace type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4669 | * |
| 4670 | * Compare 2 values |
| 4671 | * |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 4672 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not |
| 4673 | * comparable and -2 in case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4674 | */ |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4675 | static int |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4676 | xmlSchemaCompareValuesInternal(xmlSchemaValType xtype, |
| 4677 | xmlSchemaValPtr x, |
| 4678 | const xmlChar *xvalue, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4679 | xmlSchemaWhitespaceValueType xws, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4680 | xmlSchemaValType ytype, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4681 | xmlSchemaValPtr y, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4682 | const xmlChar *yvalue, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4683 | xmlSchemaWhitespaceValueType yws) |
| 4684 | { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4685 | switch (xtype) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4686 | case XML_SCHEMAS_UNKNOWN: |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 4687 | case XML_SCHEMAS_ANYTYPE: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4688 | return(-2); |
| 4689 | case XML_SCHEMAS_INTEGER: |
| 4690 | case XML_SCHEMAS_NPINTEGER: |
| 4691 | case XML_SCHEMAS_NINTEGER: |
| 4692 | case XML_SCHEMAS_NNINTEGER: |
| 4693 | case XML_SCHEMAS_PINTEGER: |
| 4694 | case XML_SCHEMAS_INT: |
| 4695 | case XML_SCHEMAS_UINT: |
| 4696 | case XML_SCHEMAS_LONG: |
| 4697 | case XML_SCHEMAS_ULONG: |
| 4698 | case XML_SCHEMAS_SHORT: |
| 4699 | case XML_SCHEMAS_USHORT: |
| 4700 | case XML_SCHEMAS_BYTE: |
| 4701 | case XML_SCHEMAS_UBYTE: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4702 | case XML_SCHEMAS_DECIMAL: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4703 | if ((x == NULL) || (y == NULL)) |
| 4704 | return(-2); |
| 4705 | if (ytype == xtype) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4706 | return(xmlSchemaCompareDecimals(x, y)); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4707 | if ((ytype == XML_SCHEMAS_DECIMAL) || |
| 4708 | (ytype == XML_SCHEMAS_INTEGER) || |
| 4709 | (ytype == XML_SCHEMAS_NPINTEGER) || |
| 4710 | (ytype == XML_SCHEMAS_NINTEGER) || |
| 4711 | (ytype == XML_SCHEMAS_NNINTEGER) || |
| 4712 | (ytype == XML_SCHEMAS_PINTEGER) || |
| 4713 | (ytype == XML_SCHEMAS_INT) || |
| 4714 | (ytype == XML_SCHEMAS_UINT) || |
| 4715 | (ytype == XML_SCHEMAS_LONG) || |
| 4716 | (ytype == XML_SCHEMAS_ULONG) || |
| 4717 | (ytype == XML_SCHEMAS_SHORT) || |
| 4718 | (ytype == XML_SCHEMAS_USHORT) || |
| 4719 | (ytype == XML_SCHEMAS_BYTE) || |
| 4720 | (ytype == XML_SCHEMAS_UBYTE)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4721 | return(xmlSchemaCompareDecimals(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4722 | return(-2); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4723 | case XML_SCHEMAS_DURATION: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4724 | if ((x == NULL) || (y == NULL)) |
| 4725 | return(-2); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4726 | if (ytype == XML_SCHEMAS_DURATION) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4727 | return(xmlSchemaCompareDurations(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4728 | return(-2); |
| 4729 | case XML_SCHEMAS_TIME: |
| 4730 | case XML_SCHEMAS_GDAY: |
| 4731 | case XML_SCHEMAS_GMONTH: |
| 4732 | case XML_SCHEMAS_GMONTHDAY: |
| 4733 | case XML_SCHEMAS_GYEAR: |
| 4734 | case XML_SCHEMAS_GYEARMONTH: |
| 4735 | case XML_SCHEMAS_DATE: |
| 4736 | case XML_SCHEMAS_DATETIME: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4737 | if ((x == NULL) || (y == NULL)) |
| 4738 | return(-2); |
| 4739 | if ((ytype == XML_SCHEMAS_DATETIME) || |
| 4740 | (ytype == XML_SCHEMAS_TIME) || |
| 4741 | (ytype == XML_SCHEMAS_GDAY) || |
| 4742 | (ytype == XML_SCHEMAS_GMONTH) || |
| 4743 | (ytype == XML_SCHEMAS_GMONTHDAY) || |
| 4744 | (ytype == XML_SCHEMAS_GYEAR) || |
| 4745 | (ytype == XML_SCHEMAS_DATE) || |
| 4746 | (ytype == XML_SCHEMAS_GYEARMONTH)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4747 | return (xmlSchemaCompareDates(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4748 | return (-2); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4749 | /* |
Kasimier T. Buchcik | 2ee855d | 2005-03-07 11:14:14 +0000 | [diff] [blame] | 4750 | * Note that we will support comparison of string types against |
| 4751 | * anySimpleType as well. |
| 4752 | */ |
| 4753 | case XML_SCHEMAS_ANYSIMPLETYPE: |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4754 | case XML_SCHEMAS_STRING: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4755 | case XML_SCHEMAS_NORMSTRING: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4756 | case XML_SCHEMAS_TOKEN: |
| 4757 | case XML_SCHEMAS_LANGUAGE: |
| 4758 | case XML_SCHEMAS_NMTOKEN: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4759 | case XML_SCHEMAS_NAME: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4760 | case XML_SCHEMAS_NCNAME: |
| 4761 | case XML_SCHEMAS_ID: |
| 4762 | case XML_SCHEMAS_IDREF: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4763 | case XML_SCHEMAS_ENTITY: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4764 | case XML_SCHEMAS_ANYURI: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4765 | { |
| 4766 | const xmlChar *xv, *yv; |
| 4767 | |
| 4768 | if (x == NULL) |
| 4769 | xv = xvalue; |
| 4770 | else |
| 4771 | xv = x->value.str; |
| 4772 | if (y == NULL) |
| 4773 | yv = yvalue; |
| 4774 | else |
| 4775 | yv = y->value.str; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4776 | /* |
| 4777 | * TODO: Compare those against QName. |
| 4778 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4779 | if (ytype == XML_SCHEMAS_QNAME) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4780 | TODO |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4781 | if (y == NULL) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4782 | return(-2); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4783 | return (-2); |
| 4784 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4785 | if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) || |
| 4786 | (ytype == XML_SCHEMAS_STRING) || |
| 4787 | (ytype == XML_SCHEMAS_NORMSTRING) || |
| 4788 | (ytype == XML_SCHEMAS_TOKEN) || |
| 4789 | (ytype == XML_SCHEMAS_LANGUAGE) || |
| 4790 | (ytype == XML_SCHEMAS_NMTOKEN) || |
| 4791 | (ytype == XML_SCHEMAS_NAME) || |
| 4792 | (ytype == XML_SCHEMAS_NCNAME) || |
| 4793 | (ytype == XML_SCHEMAS_ID) || |
| 4794 | (ytype == XML_SCHEMAS_IDREF) || |
| 4795 | (ytype == XML_SCHEMAS_ENTITY) || |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4796 | (ytype == XML_SCHEMAS_ANYURI)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4797 | |
| 4798 | if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) { |
| 4799 | |
| 4800 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) { |
| 4801 | /* TODO: What about x < y or x > y. */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4802 | if (xmlStrEqual(xv, yv)) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4803 | return (0); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4804 | else |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4805 | return (2); |
| 4806 | } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4807 | return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4808 | else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4809 | return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4810 | |
| 4811 | } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) { |
| 4812 | |
| 4813 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4814 | return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4815 | if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4816 | return (xmlSchemaCompareReplacedStrings(xv, yv)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4817 | if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4818 | return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4819 | |
| 4820 | } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) { |
| 4821 | |
| 4822 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4823 | return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4824 | if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4825 | return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4826 | if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4827 | return (xmlSchemaCompareNormStrings(xv, yv)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4828 | } else |
| 4829 | return (-2); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4830 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4831 | } |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4832 | return (-2); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4833 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4834 | case XML_SCHEMAS_QNAME: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 4835 | case XML_SCHEMAS_NOTATION: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4836 | if ((x == NULL) || (y == NULL)) |
| 4837 | return(-2); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 4838 | if ((ytype == XML_SCHEMAS_QNAME) || |
| 4839 | (ytype == XML_SCHEMAS_NOTATION)) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4840 | if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) && |
| 4841 | (xmlStrEqual(x->value.qname.uri, y->value.qname.uri))) |
| 4842 | return(0); |
| 4843 | return(2); |
| 4844 | } |
| 4845 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4846 | case XML_SCHEMAS_FLOAT: |
| 4847 | case XML_SCHEMAS_DOUBLE: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4848 | if ((x == NULL) || (y == NULL)) |
| 4849 | return(-2); |
| 4850 | if ((ytype == XML_SCHEMAS_FLOAT) || |
| 4851 | (ytype == XML_SCHEMAS_DOUBLE)) |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4852 | return (xmlSchemaCompareFloats(x, y)); |
| 4853 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4854 | case XML_SCHEMAS_BOOLEAN: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4855 | if ((x == NULL) || (y == NULL)) |
| 4856 | return(-2); |
| 4857 | if (ytype == XML_SCHEMAS_BOOLEAN) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4858 | if (x->value.b == y->value.b) |
| 4859 | return(0); |
| 4860 | if (x->value.b == 0) |
| 4861 | return(-1); |
| 4862 | return(1); |
| 4863 | } |
| 4864 | return (-2); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4865 | case XML_SCHEMAS_HEXBINARY: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4866 | if ((x == NULL) || (y == NULL)) |
| 4867 | return(-2); |
| 4868 | if (ytype == XML_SCHEMAS_HEXBINARY) { |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 4869 | if (x->value.hex.total == y->value.hex.total) { |
| 4870 | int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str); |
| 4871 | if (ret > 0) |
| 4872 | return(1); |
| 4873 | else if (ret == 0) |
| 4874 | return(0); |
| 4875 | } |
| 4876 | else if (x->value.hex.total > y->value.hex.total) |
| 4877 | return(1); |
| 4878 | |
| 4879 | return(-1); |
| 4880 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4881 | return (-2); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4882 | case XML_SCHEMAS_BASE64BINARY: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4883 | if ((x == NULL) || (y == NULL)) |
| 4884 | return(-2); |
| 4885 | if (ytype == XML_SCHEMAS_BASE64BINARY) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4886 | if (x->value.base64.total == y->value.base64.total) { |
| 4887 | int ret = xmlStrcmp(x->value.base64.str, |
| 4888 | y->value.base64.str); |
| 4889 | if (ret > 0) |
| 4890 | return(1); |
| 4891 | else if (ret == 0) |
| 4892 | return(0); |
Daniel Veillard | d369b13 | 2005-07-14 15:54:44 +0000 | [diff] [blame] | 4893 | else |
| 4894 | return(-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4895 | } |
| 4896 | else if (x->value.base64.total > y->value.base64.total) |
| 4897 | return(1); |
| 4898 | else |
| 4899 | return(-1); |
| 4900 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4901 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4902 | case XML_SCHEMAS_IDREFS: |
| 4903 | case XML_SCHEMAS_ENTITIES: |
| 4904 | case XML_SCHEMAS_NMTOKENS: |
| 4905 | TODO |
| 4906 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4907 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4908 | return -2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4912 | * xmlSchemaCompareValues: |
| 4913 | * @x: a first value |
| 4914 | * @y: a second value |
| 4915 | * |
| 4916 | * Compare 2 values |
| 4917 | * |
| 4918 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4919 | * case of error |
| 4920 | */ |
| 4921 | int |
| 4922 | xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 4923 | xmlSchemaWhitespaceValueType xws, yws; |
| 4924 | |
Daniel Veillard | 5e09414 | 2005-02-18 19:36:12 +0000 | [diff] [blame] | 4925 | if ((x == NULL) || (y == NULL)) |
| 4926 | return(-2); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4927 | if (x->type == XML_SCHEMAS_STRING) |
| 4928 | xws = XML_SCHEMA_WHITESPACE_PRESERVE; |
| 4929 | else if (x->type == XML_SCHEMAS_NORMSTRING) |
| 4930 | xws = XML_SCHEMA_WHITESPACE_REPLACE; |
| 4931 | else |
| 4932 | xws = XML_SCHEMA_WHITESPACE_COLLAPSE; |
| 4933 | |
| 4934 | if (y->type == XML_SCHEMAS_STRING) |
| 4935 | yws = XML_SCHEMA_WHITESPACE_PRESERVE; |
Gaurav | e79226c | 2013-11-28 22:50:57 +0800 | [diff] [blame] | 4936 | else if (y->type == XML_SCHEMAS_NORMSTRING) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4937 | yws = XML_SCHEMA_WHITESPACE_REPLACE; |
| 4938 | else |
| 4939 | yws = XML_SCHEMA_WHITESPACE_COLLAPSE; |
| 4940 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4941 | return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type, |
| 4942 | y, NULL, yws)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | /** |
| 4946 | * xmlSchemaCompareValuesWhtsp: |
| 4947 | * @x: a first value |
| 4948 | * @xws: the whitespace value of x |
| 4949 | * @y: a second value |
| 4950 | * @yws: the whitespace value of y |
| 4951 | * |
| 4952 | * Compare 2 values |
| 4953 | * |
| 4954 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4955 | * case of error |
| 4956 | */ |
| 4957 | int |
| 4958 | xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4959 | xmlSchemaWhitespaceValueType xws, |
| 4960 | xmlSchemaValPtr y, |
| 4961 | xmlSchemaWhitespaceValueType yws) |
| 4962 | { |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 4963 | if ((x == NULL) || (y == NULL)) |
| 4964 | return(-2); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4965 | return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type, |
| 4966 | y, NULL, yws)); |
| 4967 | } |
| 4968 | |
| 4969 | /** |
| 4970 | * xmlSchemaCompareValuesWhtspExt: |
| 4971 | * @x: a first value |
| 4972 | * @xws: the whitespace value of x |
| 4973 | * @y: a second value |
| 4974 | * @yws: the whitespace value of y |
| 4975 | * |
| 4976 | * Compare 2 values |
| 4977 | * |
| 4978 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4979 | * case of error |
| 4980 | */ |
| 4981 | static int |
| 4982 | xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype, |
| 4983 | xmlSchemaValPtr x, |
| 4984 | const xmlChar *xvalue, |
| 4985 | xmlSchemaWhitespaceValueType xws, |
| 4986 | xmlSchemaValType ytype, |
| 4987 | xmlSchemaValPtr y, |
| 4988 | const xmlChar *yvalue, |
| 4989 | xmlSchemaWhitespaceValueType yws) |
| 4990 | { |
| 4991 | return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y, |
| 4992 | yvalue, yws)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | /** |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4996 | * xmlSchemaNormLen: |
| 4997 | * @value: a string |
| 4998 | * |
| 4999 | * Computes the UTF8 length of the normalized value of the string |
| 5000 | * |
| 5001 | * Returns the length or -1 in case of error. |
| 5002 | */ |
| 5003 | static int |
| 5004 | xmlSchemaNormLen(const xmlChar *value) { |
| 5005 | const xmlChar *utf; |
| 5006 | int ret = 0; |
| 5007 | |
| 5008 | if (value == NULL) |
| 5009 | return(-1); |
| 5010 | utf = value; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5011 | while (IS_BLANK_CH(*utf)) utf++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 5012 | while (*utf != 0) { |
| 5013 | if (utf[0] & 0x80) { |
| 5014 | if ((utf[1] & 0xc0) != 0x80) |
| 5015 | return(-1); |
| 5016 | if ((utf[0] & 0xe0) == 0xe0) { |
| 5017 | if ((utf[2] & 0xc0) != 0x80) |
| 5018 | return(-1); |
| 5019 | if ((utf[0] & 0xf0) == 0xf0) { |
| 5020 | if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80) |
| 5021 | return(-1); |
| 5022 | utf += 4; |
| 5023 | } else { |
| 5024 | utf += 3; |
| 5025 | } |
| 5026 | } else { |
| 5027 | utf += 2; |
| 5028 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5029 | } else if (IS_BLANK_CH(*utf)) { |
| 5030 | while (IS_BLANK_CH(*utf)) utf++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 5031 | if (*utf == 0) |
| 5032 | break; |
| 5033 | } else { |
| 5034 | utf++; |
| 5035 | } |
| 5036 | ret++; |
| 5037 | } |
| 5038 | return(ret); |
| 5039 | } |
| 5040 | |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 5041 | /** |
| 5042 | * xmlSchemaGetFacetValueAsULong: |
| 5043 | * @facet: an schemas type facet |
| 5044 | * |
| 5045 | * Extract the value of a facet |
| 5046 | * |
| 5047 | * Returns the value as a long |
| 5048 | */ |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5049 | unsigned long |
| 5050 | xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet) |
| 5051 | { |
| 5052 | /* |
| 5053 | * TODO: Check if this is a decimal. |
| 5054 | */ |
William M. Brack | 094dd86 | 2004-11-14 14:28:34 +0000 | [diff] [blame] | 5055 | if (facet == NULL) |
| 5056 | return 0; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5057 | return ((unsigned long) facet->val->value.decimal.lo); |
| 5058 | } |
| 5059 | |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 5060 | /** |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5061 | * xmlSchemaValidateListSimpleTypeFacet: |
| 5062 | * @facet: the facet to check |
| 5063 | * @value: the lexical repr of the value to validate |
| 5064 | * @actualLen: the number of list items |
| 5065 | * @expectedLen: the resulting expected number of list items |
| 5066 | * |
| 5067 | * Checks the value of a list simple type against a facet. |
| 5068 | * |
| 5069 | * Returns 0 if the value is valid, a positive error code |
| 5070 | * number otherwise and -1 in case of an internal error. |
| 5071 | */ |
| 5072 | int |
| 5073 | xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet, |
| 5074 | const xmlChar *value, |
| 5075 | unsigned long actualLen, |
| 5076 | unsigned long *expectedLen) |
| 5077 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 5078 | if (facet == NULL) |
| 5079 | return(-1); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5080 | /* |
| 5081 | * TODO: Check if this will work with large numbers. |
| 5082 | * (compare value.decimal.mi and value.decimal.hi as well?). |
| 5083 | */ |
| 5084 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 5085 | if (actualLen != facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5086 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5087 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5088 | return (XML_SCHEMAV_CVC_LENGTH_VALID); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5089 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5090 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 5091 | if (actualLen < facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5092 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5093 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5094 | return (XML_SCHEMAV_CVC_MINLENGTH_VALID); |
| 5095 | } |
| 5096 | } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) { |
| 5097 | if (actualLen > facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5098 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5099 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5100 | return (XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
| 5101 | } |
| 5102 | } else |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5103 | /* |
| 5104 | * NOTE: That we can pass NULL as xmlSchemaValPtr to |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5105 | * xmlSchemaValidateFacet, since the remaining facet types |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5106 | * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION. |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5107 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5108 | return(xmlSchemaValidateFacet(NULL, facet, value, NULL)); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5109 | return (0); |
| 5110 | } |
| 5111 | |
| 5112 | /** |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 5113 | * xmlSchemaValidateLengthFacet: |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5114 | * @type: the built-in type |
| 5115 | * @facet: the facet to check |
| 5116 | * @value: the lexical repr. of the value to be validated |
| 5117 | * @val: the precomputed value |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5118 | * @ws: the whitespace type of the value |
| 5119 | * @length: the actual length of the value |
| 5120 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5121 | * Checka a value against a "length", "minLength" and "maxLength" |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5122 | * facet; sets @length to the computed length of @value. |
| 5123 | * |
| 5124 | * Returns 0 if the value is valid, a positive error code |
| 5125 | * otherwise and -1 in case of an internal or API error. |
| 5126 | */ |
| 5127 | static int |
| 5128 | xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet, |
Nico Weber | aae48e6 | 2012-02-29 09:44:35 +0800 | [diff] [blame] | 5129 | xmlSchemaValType valType, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5130 | const xmlChar *value, |
Nico Weber | aae48e6 | 2012-02-29 09:44:35 +0800 | [diff] [blame] | 5131 | xmlSchemaValPtr val, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5132 | unsigned long *length, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5133 | xmlSchemaWhitespaceValueType ws) |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5134 | { |
| 5135 | unsigned int len = 0; |
| 5136 | |
| 5137 | if ((length == NULL) || (facet == NULL)) |
| 5138 | return (-1); |
| 5139 | *length = 0; |
| 5140 | if ((facet->type != XML_SCHEMA_FACET_LENGTH) && |
| 5141 | (facet->type != XML_SCHEMA_FACET_MAXLENGTH) && |
| 5142 | (facet->type != XML_SCHEMA_FACET_MINLENGTH)) |
| 5143 | return (-1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5144 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5145 | /* |
| 5146 | * TODO: length, maxLength and minLength must be of type |
| 5147 | * nonNegativeInteger only. Check if decimal is used somehow. |
| 5148 | */ |
| 5149 | if ((facet->val == NULL) || |
| 5150 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 5151 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
| 5152 | (facet->val->value.decimal.frac != 0)) { |
| 5153 | return(-1); |
| 5154 | } |
| 5155 | if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY)) |
| 5156 | len = val->value.hex.total; |
| 5157 | else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY)) |
| 5158 | len = val->value.base64.total; |
| 5159 | else { |
| 5160 | switch (valType) { |
| 5161 | case XML_SCHEMAS_STRING: |
| 5162 | case XML_SCHEMAS_NORMSTRING: |
| 5163 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5164 | /* |
| 5165 | * This is to ensure API compatibility with the old |
| 5166 | * xmlSchemaValidateLengthFacet(). Anyway, this was and |
| 5167 | * is not the correct handling. |
| 5168 | * TODO: Get rid of this case somehow. |
| 5169 | */ |
| 5170 | if (valType == XML_SCHEMAS_STRING) |
| 5171 | len = xmlUTF8Strlen(value); |
| 5172 | else |
| 5173 | len = xmlSchemaNormLen(value); |
| 5174 | } else if (value != NULL) { |
| 5175 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 5176 | len = xmlSchemaNormLen(value); |
| 5177 | else |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5178 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5179 | * Should be OK for "preserve" as well. |
| 5180 | */ |
| 5181 | len = xmlUTF8Strlen(value); |
| 5182 | } |
| 5183 | break; |
| 5184 | case XML_SCHEMAS_IDREF: |
| 5185 | case XML_SCHEMAS_TOKEN: |
| 5186 | case XML_SCHEMAS_LANGUAGE: |
| 5187 | case XML_SCHEMAS_NMTOKEN: |
| 5188 | case XML_SCHEMAS_NAME: |
| 5189 | case XML_SCHEMAS_NCNAME: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5190 | case XML_SCHEMAS_ID: |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5191 | /* |
| 5192 | * FIXME: What exactly to do with anyURI? |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5193 | */ |
| 5194 | case XML_SCHEMAS_ANYURI: |
| 5195 | if (value != NULL) |
| 5196 | len = xmlSchemaNormLen(value); |
| 5197 | break; |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5198 | case XML_SCHEMAS_QNAME: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5199 | case XML_SCHEMAS_NOTATION: |
| 5200 | /* |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 5201 | * For QName and NOTATION, those facets are |
| 5202 | * deprecated and should be ignored. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5203 | */ |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5204 | return (0); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5205 | default: |
| 5206 | TODO |
| 5207 | } |
| 5208 | } |
| 5209 | *length = (unsigned long) len; |
| 5210 | /* |
| 5211 | * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi". |
| 5212 | */ |
| 5213 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 5214 | if (len != facet->val->value.decimal.lo) |
| 5215 | return(XML_SCHEMAV_CVC_LENGTH_VALID); |
| 5216 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 5217 | if (len < facet->val->value.decimal.lo) |
| 5218 | return(XML_SCHEMAV_CVC_MINLENGTH_VALID); |
| 5219 | } else { |
| 5220 | if (len > facet->val->value.decimal.lo) |
| 5221 | return(XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
| 5222 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5223 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5224 | return (0); |
| 5225 | } |
| 5226 | |
| 5227 | /** |
| 5228 | * xmlSchemaValidateLengthFacet: |
| 5229 | * @type: the built-in type |
| 5230 | * @facet: the facet to check |
| 5231 | * @value: the lexical repr. of the value to be validated |
| 5232 | * @val: the precomputed value |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5233 | * @length: the actual length of the value |
| 5234 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5235 | * Checka a value against a "length", "minLength" and "maxLength" |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5236 | * facet; sets @length to the computed length of @value. |
| 5237 | * |
| 5238 | * Returns 0 if the value is valid, a positive error code |
| 5239 | * otherwise and -1 in case of an internal or API error. |
| 5240 | */ |
| 5241 | int |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5242 | xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type, |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5243 | xmlSchemaFacetPtr facet, |
| 5244 | const xmlChar *value, |
| 5245 | xmlSchemaValPtr val, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5246 | unsigned long *length) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5247 | { |
Daniel Veillard | cc5e233 | 2005-03-16 21:55:35 +0000 | [diff] [blame] | 5248 | if (type == NULL) |
| 5249 | return(-1); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5250 | return (xmlSchemaValidateLengthFacetInternal(facet, |
| 5251 | type->builtInType, value, val, length, |
| 5252 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
| 5253 | } |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5254 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5255 | /** |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5256 | * xmlSchemaValidateLengthFacetWhtsp: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5257 | * @facet: the facet to check |
| 5258 | * @valType: the built-in type |
| 5259 | * @value: the lexical repr. of the value to be validated |
| 5260 | * @val: the precomputed value |
| 5261 | * @ws: the whitespace type of the value |
| 5262 | * @length: the actual length of the value |
| 5263 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5264 | * Checka a value against a "length", "minLength" and "maxLength" |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5265 | * facet; sets @length to the computed length of @value. |
| 5266 | * |
| 5267 | * Returns 0 if the value is valid, a positive error code |
| 5268 | * otherwise and -1 in case of an internal or API error. |
| 5269 | */ |
| 5270 | int |
| 5271 | xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet, |
| 5272 | xmlSchemaValType valType, |
| 5273 | const xmlChar *value, |
| 5274 | xmlSchemaValPtr val, |
| 5275 | unsigned long *length, |
| 5276 | xmlSchemaWhitespaceValueType ws) |
| 5277 | { |
| 5278 | return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val, |
| 5279 | length, ws)); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5280 | } |
| 5281 | |
| 5282 | /** |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5283 | * xmlSchemaValidateFacetInternal: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5284 | * @facet: the facet to check |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5285 | * @fws: the whitespace type of the facet's value |
| 5286 | * @valType: the built-in type of the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5287 | * @value: the lexical repr of the value to validate |
| 5288 | * @val: the precomputed value |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5289 | * @ws: the whitespace type of the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5290 | * |
| 5291 | * Check a value against a facet condition |
| 5292 | * |
| 5293 | * Returns 0 if the element is schemas valid, a positive error code |
| 5294 | * number otherwise and -1 in case of internal or API error. |
| 5295 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5296 | static int |
| 5297 | xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet, |
| 5298 | xmlSchemaWhitespaceValueType fws, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5299 | xmlSchemaValType valType, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5300 | const xmlChar *value, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5301 | xmlSchemaValPtr val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5302 | xmlSchemaWhitespaceValueType ws) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5303 | { |
| 5304 | int ret; |
| 5305 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5306 | if (facet == NULL) |
| 5307 | return(-1); |
| 5308 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5309 | switch (facet->type) { |
| 5310 | case XML_SCHEMA_FACET_PATTERN: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5311 | /* |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5312 | * NOTE that for patterns, the @value needs to be the normalized |
| 5313 | * value, *not* the lexical initial value or the canonical value. |
| 5314 | */ |
| 5315 | if (value == NULL) |
| 5316 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5317 | ret = xmlRegexpExec(facet->regexp, value); |
| 5318 | if (ret == 1) |
| 5319 | return(0); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5320 | if (ret == 0) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5321 | return(XML_SCHEMAV_CVC_PATTERN_VALID); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5322 | return(ret); |
| 5323 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 5324 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5325 | if (ret == -2) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5326 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5327 | if (ret == -1) |
| 5328 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5329 | return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5330 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 5331 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5332 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5333 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5334 | if ((ret == -1) || (ret == 0)) |
| 5335 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5336 | return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5337 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 5338 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5339 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5340 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5341 | if (ret == 1) |
| 5342 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5343 | return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5344 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 5345 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5346 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5347 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5348 | if ((ret == 1) || (ret == 0)) |
| 5349 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5350 | return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID); |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5351 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5352 | /* TODO whitespaces */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5353 | /* |
| 5354 | * NOTE: Whitespace should be handled to normalize |
| 5355 | * the value to be validated against a the facets; |
| 5356 | * not to normalize the value in-between. |
| 5357 | */ |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5358 | return(0); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 5359 | case XML_SCHEMA_FACET_ENUMERATION: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5360 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5361 | /* |
| 5362 | * This is to ensure API compatibility with the old |
| 5363 | * xmlSchemaValidateFacet(). |
| 5364 | * TODO: Get rid of this case. |
| 5365 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5366 | if ((facet->value != NULL) && |
| 5367 | (xmlStrEqual(facet->value, value))) |
| 5368 | return(0); |
| 5369 | } else { |
| 5370 | ret = xmlSchemaCompareValuesWhtspExt(facet->val->type, |
| 5371 | facet->val, facet->value, fws, valType, val, |
| 5372 | value, ws); |
| 5373 | if (ret == -2) |
| 5374 | return(-1); |
| 5375 | if (ret == 0) |
| 5376 | return(0); |
| 5377 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5378 | return(XML_SCHEMAV_CVC_ENUMERATION_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5379 | case XML_SCHEMA_FACET_LENGTH: |
Kasimier T. Buchcik | aba15f7 | 2005-04-01 15:17:27 +0000 | [diff] [blame] | 5380 | /* |
| 5381 | * SPEC (1.3) "if {primitive type definition} is QName or NOTATION, |
| 5382 | * then any {value} is facet-valid." |
| 5383 | */ |
| 5384 | if ((valType == XML_SCHEMAS_QNAME) || |
| 5385 | (valType == XML_SCHEMAS_NOTATION)) |
| 5386 | return (0); |
| 5387 | /* No break on purpose. */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5388 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 5389 | case XML_SCHEMA_FACET_MINLENGTH: { |
| 5390 | unsigned int len = 0; |
| 5391 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 5392 | if ((valType == XML_SCHEMAS_QNAME) || |
| 5393 | (valType == XML_SCHEMAS_NOTATION)) |
| 5394 | return (0); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5395 | /* |
| 5396 | * TODO: length, maxLength and minLength must be of type |
| 5397 | * nonNegativeInteger only. Check if decimal is used somehow. |
| 5398 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5399 | if ((facet->val == NULL) || |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5400 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 5401 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5402 | (facet->val->value.decimal.frac != 0)) { |
| 5403 | return(-1); |
| 5404 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5405 | if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY)) |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 5406 | len = val->value.hex.total; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 5407 | else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY)) |
| 5408 | len = val->value.base64.total; |
| 5409 | else { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5410 | switch (valType) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5411 | case XML_SCHEMAS_STRING: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5412 | case XML_SCHEMAS_NORMSTRING: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5413 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5414 | /* |
| 5415 | * This is to ensure API compatibility with the old |
| 5416 | * xmlSchemaValidateFacet(). Anyway, this was and |
| 5417 | * is not the correct handling. |
| 5418 | * TODO: Get rid of this case somehow. |
| 5419 | */ |
| 5420 | if (valType == XML_SCHEMAS_STRING) |
| 5421 | len = xmlUTF8Strlen(value); |
| 5422 | else |
| 5423 | len = xmlSchemaNormLen(value); |
| 5424 | } else if (value != NULL) { |
| 5425 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 5426 | len = xmlSchemaNormLen(value); |
| 5427 | else |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5428 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5429 | * Should be OK for "preserve" as well. |
| 5430 | */ |
| 5431 | len = xmlUTF8Strlen(value); |
| 5432 | } |
| 5433 | break; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5434 | case XML_SCHEMAS_IDREF: |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5435 | case XML_SCHEMAS_TOKEN: |
| 5436 | case XML_SCHEMAS_LANGUAGE: |
| 5437 | case XML_SCHEMAS_NMTOKEN: |
| 5438 | case XML_SCHEMAS_NAME: |
| 5439 | case XML_SCHEMAS_NCNAME: |
| 5440 | case XML_SCHEMAS_ID: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5441 | case XML_SCHEMAS_ANYURI: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5442 | if (value != NULL) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5443 | len = xmlSchemaNormLen(value); |
| 5444 | break; |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5445 | default: |
| 5446 | TODO |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5447 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5448 | } |
| 5449 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5450 | if (len != facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5451 | return(XML_SCHEMAV_CVC_LENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5452 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5453 | if (len < facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5454 | return(XML_SCHEMAV_CVC_MINLENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5455 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5456 | if (len > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5457 | return(XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5458 | } |
| 5459 | break; |
| 5460 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5461 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 5462 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 5463 | |
| 5464 | if ((facet->val == NULL) || |
Kasimier T. Buchcik | 69dea3a | 2005-11-07 14:02:44 +0000 | [diff] [blame] | 5465 | ((facet->val->type != XML_SCHEMAS_PINTEGER) && |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5466 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
| 5467 | (facet->val->value.decimal.frac != 0)) { |
| 5468 | return(-1); |
| 5469 | } |
| 5470 | if ((val == NULL) || |
| 5471 | ((val->type != XML_SCHEMAS_DECIMAL) && |
| 5472 | (val->type != XML_SCHEMAS_INTEGER) && |
| 5473 | (val->type != XML_SCHEMAS_NPINTEGER) && |
| 5474 | (val->type != XML_SCHEMAS_NINTEGER) && |
| 5475 | (val->type != XML_SCHEMAS_NNINTEGER) && |
| 5476 | (val->type != XML_SCHEMAS_PINTEGER) && |
| 5477 | (val->type != XML_SCHEMAS_INT) && |
| 5478 | (val->type != XML_SCHEMAS_UINT) && |
| 5479 | (val->type != XML_SCHEMAS_LONG) && |
| 5480 | (val->type != XML_SCHEMAS_ULONG) && |
| 5481 | (val->type != XML_SCHEMAS_SHORT) && |
| 5482 | (val->type != XML_SCHEMAS_USHORT) && |
| 5483 | (val->type != XML_SCHEMAS_BYTE) && |
| 5484 | (val->type != XML_SCHEMAS_UBYTE))) { |
| 5485 | return(-1); |
| 5486 | } |
| 5487 | if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) { |
| 5488 | if (val->value.decimal.total > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5489 | return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5490 | |
| 5491 | } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) { |
| 5492 | if (val->value.decimal.frac > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5493 | return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5494 | } |
| 5495 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5496 | default: |
| 5497 | TODO |
| 5498 | } |
| 5499 | return(0); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 5500 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5503 | /** |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5504 | * xmlSchemaValidateFacet: |
| 5505 | * @base: the base type |
| 5506 | * @facet: the facet to check |
| 5507 | * @value: the lexical repr of the value to validate |
| 5508 | * @val: the precomputed value |
| 5509 | * |
| 5510 | * Check a value against a facet condition |
| 5511 | * |
| 5512 | * Returns 0 if the element is schemas valid, a positive error code |
| 5513 | * number otherwise and -1 in case of internal or API error. |
| 5514 | */ |
| 5515 | int |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5516 | xmlSchemaValidateFacet(xmlSchemaTypePtr base, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5517 | xmlSchemaFacetPtr facet, |
| 5518 | const xmlChar *value, |
| 5519 | xmlSchemaValPtr val) |
| 5520 | { |
| 5521 | /* |
| 5522 | * This tries to ensure API compatibility regarding the old |
| 5523 | * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and |
| 5524 | * xmlSchemaValidateFacetWhtsp(). |
| 5525 | */ |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5526 | if (val != NULL) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5527 | return(xmlSchemaValidateFacetInternal(facet, |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5528 | XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5529 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5530 | else if (base != NULL) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5531 | return(xmlSchemaValidateFacetInternal(facet, |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5532 | XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5533 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5534 | return(-1); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | /** |
| 5538 | * xmlSchemaValidateFacetWhtsp: |
| 5539 | * @facet: the facet to check |
| 5540 | * @fws: the whitespace type of the facet's value |
| 5541 | * @valType: the built-in type of the value |
| 5542 | * @value: the lexical (or normalized for pattern) repr of the value to validate |
| 5543 | * @val: the precomputed value |
| 5544 | * @ws: the whitespace type of the value |
| 5545 | * |
| 5546 | * Check a value against a facet condition. This takes value normalization |
| 5547 | * according to the specified whitespace types into account. |
| 5548 | * Note that @value needs to be the *normalized* value if the facet |
| 5549 | * is of type "pattern". |
| 5550 | * |
| 5551 | * Returns 0 if the element is schemas valid, a positive error code |
| 5552 | * number otherwise and -1 in case of internal or API error. |
| 5553 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5554 | int |
| 5555 | xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet, |
| 5556 | xmlSchemaWhitespaceValueType fws, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5557 | xmlSchemaValType valType, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5558 | const xmlChar *value, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5559 | xmlSchemaValPtr val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5560 | xmlSchemaWhitespaceValueType ws) |
| 5561 | { |
| 5562 | return(xmlSchemaValidateFacetInternal(facet, fws, valType, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5563 | value, val, ws)); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5564 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5565 | |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5566 | #if 0 |
| 5567 | #ifndef DBL_DIG |
| 5568 | #define DBL_DIG 16 |
| 5569 | #endif |
| 5570 | #ifndef DBL_EPSILON |
| 5571 | #define DBL_EPSILON 1E-9 |
| 5572 | #endif |
| 5573 | |
| 5574 | #define INTEGER_DIGITS DBL_DIG |
| 5575 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 5576 | #define EXPONENT_DIGITS (3 + 2) |
| 5577 | |
| 5578 | /** |
| 5579 | * xmlXPathFormatNumber: |
| 5580 | * @number: number to format |
| 5581 | * @buffer: output buffer |
| 5582 | * @buffersize: size of output buffer |
| 5583 | * |
| 5584 | * Convert the number into a string representation. |
| 5585 | */ |
| 5586 | static void |
| 5587 | xmlSchemaFormatFloat(double number, char buffer[], int buffersize) |
| 5588 | { |
| 5589 | switch (xmlXPathIsInf(number)) { |
| 5590 | case 1: |
| 5591 | if (buffersize > (int)sizeof("INF")) |
| 5592 | snprintf(buffer, buffersize, "INF"); |
| 5593 | break; |
| 5594 | case -1: |
| 5595 | if (buffersize > (int)sizeof("-INF")) |
| 5596 | snprintf(buffer, buffersize, "-INF"); |
| 5597 | break; |
| 5598 | default: |
| 5599 | if (xmlXPathIsNaN(number)) { |
| 5600 | if (buffersize > (int)sizeof("NaN")) |
| 5601 | snprintf(buffer, buffersize, "NaN"); |
| 5602 | } else if (number == 0) { |
| 5603 | snprintf(buffer, buffersize, "0.0E0"); |
| 5604 | } else { |
| 5605 | /* 3 is sign, decimal point, and terminating zero */ |
| 5606 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 5607 | int integer_place, fraction_place; |
| 5608 | char *ptr; |
| 5609 | char *after_fraction; |
| 5610 | double absolute_value; |
| 5611 | int size; |
| 5612 | |
| 5613 | absolute_value = fabs(number); |
| 5614 | |
| 5615 | /* |
| 5616 | * Result is in work, and after_fraction points |
| 5617 | * just past the fractional part. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5618 | * Use scientific notation |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5619 | */ |
| 5620 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 5621 | fraction_place = DBL_DIG - 1; |
| 5622 | snprintf(work, sizeof(work),"%*.*e", |
| 5623 | integer_place, fraction_place, number); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5624 | after_fraction = strchr(work + DBL_DIG, 'e'); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5625 | /* Remove fractional trailing zeroes */ |
| 5626 | ptr = after_fraction; |
| 5627 | while (*(--ptr) == '0') |
| 5628 | ; |
| 5629 | if (*ptr != '.') |
| 5630 | ptr++; |
| 5631 | while ((*ptr++ = *after_fraction++) != 0); |
| 5632 | |
| 5633 | /* Finally copy result back to caller */ |
| 5634 | size = strlen(work) + 1; |
| 5635 | if (size > buffersize) { |
| 5636 | work[buffersize - 1] = 0; |
| 5637 | size = buffersize; |
| 5638 | } |
| 5639 | memmove(buffer, work, size); |
| 5640 | } |
| 5641 | break; |
| 5642 | } |
| 5643 | } |
| 5644 | #endif |
| 5645 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5646 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5647 | * xmlSchemaGetCanonValue: |
| 5648 | * @val: the precomputed value |
| 5649 | * @retValue: the returned value |
| 5650 | * |
Jan Pokorný | 7a7cad6 | 2013-11-29 23:26:26 +0100 | [diff] [blame] | 5651 | * Get the canonical lexical representation of the value. |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5652 | * The caller has to FREE the returned retValue. |
| 5653 | * |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5654 | * WARNING: Some value types are not supported yet, resulting |
| 5655 | * in a @retValue of "???". |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5656 | * |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5657 | * TODO: XML Schema 1.0 does not define canonical representations |
| 5658 | * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay, |
| 5659 | * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5660 | * |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5661 | * |
| 5662 | * Returns 0 if the value could be built, 1 if the value type is |
| 5663 | * not supported yet and -1 in case of API errors. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5664 | */ |
| 5665 | int |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 5666 | xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5667 | { |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 5668 | if ((retValue == NULL) || (val == NULL)) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5669 | return (-1); |
| 5670 | *retValue = NULL; |
| 5671 | switch (val->type) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5672 | case XML_SCHEMAS_STRING: |
| 5673 | if (val->value.str == NULL) |
| 5674 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 5675 | else |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5676 | *retValue = |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5677 | BAD_CAST xmlStrdup((const xmlChar *) val->value.str); |
| 5678 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5679 | case XML_SCHEMAS_NORMSTRING: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5680 | if (val->value.str == NULL) |
| 5681 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 5682 | else { |
| 5683 | *retValue = xmlSchemaWhiteSpaceReplace( |
| 5684 | (const xmlChar *) val->value.str); |
| 5685 | if ((*retValue) == NULL) |
| 5686 | *retValue = BAD_CAST xmlStrdup( |
| 5687 | (const xmlChar *) val->value.str); |
| 5688 | } |
| 5689 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5690 | case XML_SCHEMAS_TOKEN: |
| 5691 | case XML_SCHEMAS_LANGUAGE: |
| 5692 | case XML_SCHEMAS_NMTOKEN: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5693 | case XML_SCHEMAS_NAME: |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5694 | case XML_SCHEMAS_NCNAME: |
| 5695 | case XML_SCHEMAS_ID: |
| 5696 | case XML_SCHEMAS_IDREF: |
| 5697 | case XML_SCHEMAS_ENTITY: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5698 | case XML_SCHEMAS_NOTATION: /* Unclear */ |
| 5699 | case XML_SCHEMAS_ANYURI: /* Unclear */ |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5700 | if (val->value.str == NULL) |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5701 | return (-1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5702 | *retValue = |
Kasimier T. Buchcik | aba15f7 | 2005-04-01 15:17:27 +0000 | [diff] [blame] | 5703 | BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str); |
| 5704 | if (*retValue == NULL) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5705 | *retValue = |
Kasimier T. Buchcik | aba15f7 | 2005-04-01 15:17:27 +0000 | [diff] [blame] | 5706 | BAD_CAST xmlStrdup((const xmlChar *) val->value.str); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5707 | break; |
| 5708 | case XML_SCHEMAS_QNAME: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5709 | /* TODO: Unclear in XML Schema 1.0. */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5710 | if (val->value.qname.uri == NULL) { |
| 5711 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name); |
| 5712 | return (0); |
| 5713 | } else { |
| 5714 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "{"); |
| 5715 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5716 | BAD_CAST val->value.qname.uri); |
| 5717 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5718 | BAD_CAST "}"); |
| 5719 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5720 | BAD_CAST val->value.qname.uri); |
| 5721 | } |
| 5722 | break; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5723 | case XML_SCHEMAS_DECIMAL: |
| 5724 | /* |
| 5725 | * TODO: Lookout for a more simple implementation. |
| 5726 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5727 | if ((val->value.decimal.total == 1) && |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5728 | (val->value.decimal.lo == 0)) { |
| 5729 | *retValue = xmlStrdup(BAD_CAST "0.0"); |
| 5730 | } else { |
| 5731 | xmlSchemaValDecimal dec = val->value.decimal; |
| 5732 | int bufsize; |
| 5733 | char *buf = NULL, *offs; |
| 5734 | |
| 5735 | /* Add room for the decimal point as well. */ |
| 5736 | bufsize = dec.total + 2; |
| 5737 | if (dec.sign) |
| 5738 | bufsize++; |
| 5739 | /* Add room for leading/trailing zero. */ |
| 5740 | if ((dec.frac == 0) || (dec.frac == dec.total)) |
| 5741 | bufsize++; |
| 5742 | buf = xmlMalloc(bufsize); |
Daniel Veillard | 26ab0e6 | 2006-10-11 12:32:51 +0000 | [diff] [blame] | 5743 | if (buf == NULL) |
| 5744 | return(-1); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5745 | offs = buf; |
| 5746 | if (dec.sign) |
| 5747 | *offs++ = '-'; |
| 5748 | if (dec.frac == dec.total) { |
| 5749 | *offs++ = '0'; |
| 5750 | *offs++ = '.'; |
| 5751 | } |
| 5752 | if (dec.hi != 0) |
| 5753 | snprintf(offs, bufsize - (offs - buf), |
| 5754 | "%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5755 | else if (dec.mi != 0) |
| 5756 | snprintf(offs, bufsize - (offs - buf), |
| 5757 | "%lu%lu", dec.mi, dec.lo); |
| 5758 | else |
| 5759 | snprintf(offs, bufsize - (offs - buf), |
| 5760 | "%lu", dec.lo); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5761 | |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5762 | if (dec.frac != 0) { |
| 5763 | if (dec.frac != dec.total) { |
| 5764 | int diff = dec.total - dec.frac; |
| 5765 | /* |
| 5766 | * Insert the decimal point. |
| 5767 | */ |
| 5768 | memmove(offs + diff + 1, offs + diff, dec.frac +1); |
| 5769 | offs[diff] = '.'; |
| 5770 | } else { |
| 5771 | unsigned int i = 0; |
| 5772 | /* |
| 5773 | * Insert missing zeroes behind the decimal point. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5774 | */ |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5775 | while (*(offs + i) != 0) |
| 5776 | i++; |
| 5777 | if (i < dec.total) { |
| 5778 | memmove(offs + (dec.total - i), offs, i +1); |
| 5779 | memset(offs, '0', dec.total - i); |
| 5780 | } |
| 5781 | } |
| 5782 | } else { |
| 5783 | /* |
| 5784 | * Append decimal point and zero. |
| 5785 | */ |
| 5786 | offs = buf + bufsize - 1; |
| 5787 | *offs-- = 0; |
| 5788 | *offs-- = '0'; |
| 5789 | *offs-- = '.'; |
| 5790 | } |
| 5791 | *retValue = BAD_CAST buf; |
| 5792 | } |
| 5793 | break; |
| 5794 | case XML_SCHEMAS_INTEGER: |
| 5795 | case XML_SCHEMAS_PINTEGER: |
| 5796 | case XML_SCHEMAS_NPINTEGER: |
| 5797 | case XML_SCHEMAS_NINTEGER: |
| 5798 | case XML_SCHEMAS_NNINTEGER: |
| 5799 | case XML_SCHEMAS_LONG: |
| 5800 | case XML_SCHEMAS_BYTE: |
| 5801 | case XML_SCHEMAS_SHORT: |
| 5802 | case XML_SCHEMAS_INT: |
| 5803 | case XML_SCHEMAS_UINT: |
| 5804 | case XML_SCHEMAS_ULONG: |
| 5805 | case XML_SCHEMAS_USHORT: |
| 5806 | case XML_SCHEMAS_UBYTE: |
| 5807 | if ((val->value.decimal.total == 1) && |
| 5808 | (val->value.decimal.lo == 0)) |
| 5809 | *retValue = xmlStrdup(BAD_CAST "0"); |
| 5810 | else { |
| 5811 | xmlSchemaValDecimal dec = val->value.decimal; |
| 5812 | int bufsize = dec.total + 1; |
| 5813 | |
| 5814 | /* Add room for the decimal point as well. */ |
| 5815 | if (dec.sign) |
| 5816 | bufsize++; |
| 5817 | *retValue = xmlMalloc(bufsize); |
Daniel Veillard | 26ab0e6 | 2006-10-11 12:32:51 +0000 | [diff] [blame] | 5818 | if (*retValue == NULL) |
| 5819 | return(-1); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5820 | if (dec.hi != 0) { |
| 5821 | if (dec.sign) |
| 5822 | snprintf((char *) *retValue, bufsize, |
| 5823 | "-%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5824 | else |
| 5825 | snprintf((char *) *retValue, bufsize, |
| 5826 | "%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5827 | } else if (dec.mi != 0) { |
| 5828 | if (dec.sign) |
| 5829 | snprintf((char *) *retValue, bufsize, |
| 5830 | "-%lu%lu", dec.mi, dec.lo); |
| 5831 | else |
| 5832 | snprintf((char *) *retValue, bufsize, |
| 5833 | "%lu%lu", dec.mi, dec.lo); |
| 5834 | } else { |
| 5835 | if (dec.sign) |
| 5836 | snprintf((char *) *retValue, bufsize, "-%lu", dec.lo); |
| 5837 | else |
| 5838 | snprintf((char *) *retValue, bufsize, "%lu", dec.lo); |
| 5839 | } |
| 5840 | } |
| 5841 | break; |
| 5842 | case XML_SCHEMAS_BOOLEAN: |
| 5843 | if (val->value.b) |
| 5844 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "true"); |
| 5845 | else |
| 5846 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "false"); |
| 5847 | break; |
| 5848 | case XML_SCHEMAS_DURATION: { |
| 5849 | char buf[100]; |
| 5850 | unsigned long year; |
| 5851 | unsigned long mon, day, hour = 0, min = 0; |
| 5852 | double sec = 0, left; |
| 5853 | |
| 5854 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5855 | /* |
| 5856 | * TODO: This results in a normalized output of the value |
| 5857 | * - which is NOT conformant to the spec - |
| 5858 | * since the exact values of each property are not |
| 5859 | * recoverable. Think about extending the structure to |
| 5860 | * provide a field for every property. |
| 5861 | */ |
| 5862 | year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12); |
| 5863 | mon = labs(val->value.dur.mon) - 12 * year; |
| 5864 | |
| 5865 | day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400); |
| 5866 | left = fabs(val->value.dur.sec) - day * 86400; |
| 5867 | if (left > 0) { |
| 5868 | hour = (unsigned long) FQUOTIENT(left, 3600); |
| 5869 | left = left - (hour * 3600); |
| 5870 | if (left > 0) { |
| 5871 | min = (unsigned long) FQUOTIENT(left, 60); |
| 5872 | sec = left - (min * 60); |
| 5873 | } |
| 5874 | } |
| 5875 | if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0)) |
| 5876 | snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS", |
| 5877 | year, mon, day, hour, min, sec); |
| 5878 | else |
| 5879 | snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS", |
| 5880 | year, mon, day, hour, min, sec); |
| 5881 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5882 | } |
| 5883 | break; |
| 5884 | case XML_SCHEMAS_GYEAR: { |
| 5885 | char buf[30]; |
| 5886 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5887 | /* TODO: What to do with the timezone? */ |
| 5888 | snprintf(buf, 30, "%04ld", val->value.date.year); |
| 5889 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5890 | } |
| 5891 | break; |
| 5892 | case XML_SCHEMAS_GMONTH: { |
| 5893 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5894 | /* TODO: What to do with the timezone? */ |
Daniel Veillard | 26ab0e6 | 2006-10-11 12:32:51 +0000 | [diff] [blame] | 5895 | *retValue = xmlMalloc(6); |
| 5896 | if (*retValue == NULL) |
| 5897 | return(-1); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5898 | snprintf((char *) *retValue, 6, "--%02u", |
| 5899 | val->value.date.mon); |
| 5900 | } |
| 5901 | break; |
| 5902 | case XML_SCHEMAS_GDAY: { |
| 5903 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5904 | /* TODO: What to do with the timezone? */ |
| 5905 | *retValue = xmlMalloc(6); |
Daniel Veillard | 26ab0e6 | 2006-10-11 12:32:51 +0000 | [diff] [blame] | 5906 | if (*retValue == NULL) |
| 5907 | return(-1); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5908 | snprintf((char *) *retValue, 6, "---%02u", |
| 5909 | val->value.date.day); |
| 5910 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5911 | break; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5912 | case XML_SCHEMAS_GMONTHDAY: { |
| 5913 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5914 | /* TODO: What to do with the timezone? */ |
| 5915 | *retValue = xmlMalloc(8); |
Daniel Veillard | 26ab0e6 | 2006-10-11 12:32:51 +0000 | [diff] [blame] | 5916 | if (*retValue == NULL) |
| 5917 | return(-1); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5918 | snprintf((char *) *retValue, 8, "--%02u-%02u", |
| 5919 | val->value.date.mon, val->value.date.day); |
| 5920 | } |
| 5921 | break; |
| 5922 | case XML_SCHEMAS_GYEARMONTH: { |
| 5923 | char buf[35]; |
| 5924 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5925 | /* TODO: What to do with the timezone? */ |
| 5926 | if (val->value.date.year < 0) |
| 5927 | snprintf(buf, 35, "-%04ld-%02u", |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5928 | labs(val->value.date.year), |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5929 | val->value.date.mon); |
| 5930 | else |
| 5931 | snprintf(buf, 35, "%04ld-%02u", |
| 5932 | val->value.date.year, val->value.date.mon); |
| 5933 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5934 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5935 | break; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5936 | case XML_SCHEMAS_TIME: |
| 5937 | { |
| 5938 | char buf[30]; |
| 5939 | |
| 5940 | if (val->value.date.tz_flag) { |
| 5941 | xmlSchemaValPtr norm; |
| 5942 | |
| 5943 | norm = xmlSchemaDateNormalize(val, 0); |
| 5944 | if (norm == NULL) |
| 5945 | return (-1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5946 | /* |
| 5947 | * TODO: Check if "%.14g" is portable. |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5948 | */ |
| 5949 | snprintf(buf, 30, |
| 5950 | "%02u:%02u:%02.14gZ", |
| 5951 | norm->value.date.hour, |
| 5952 | norm->value.date.min, |
| 5953 | norm->value.date.sec); |
| 5954 | xmlSchemaFreeValue(norm); |
| 5955 | } else { |
| 5956 | snprintf(buf, 30, |
| 5957 | "%02u:%02u:%02.14g", |
| 5958 | val->value.date.hour, |
| 5959 | val->value.date.min, |
| 5960 | val->value.date.sec); |
| 5961 | } |
| 5962 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5963 | } |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5964 | break; |
| 5965 | case XML_SCHEMAS_DATE: |
| 5966 | { |
| 5967 | char buf[30]; |
| 5968 | |
| 5969 | if (val->value.date.tz_flag) { |
| 5970 | xmlSchemaValPtr norm; |
| 5971 | |
| 5972 | norm = xmlSchemaDateNormalize(val, 0); |
| 5973 | if (norm == NULL) |
| 5974 | return (-1); |
| 5975 | /* |
| 5976 | * TODO: Append the canonical value of the |
| 5977 | * recoverable timezone and not "Z". |
| 5978 | */ |
| 5979 | snprintf(buf, 30, |
| 5980 | "%04ld:%02u:%02uZ", |
| 5981 | norm->value.date.year, norm->value.date.mon, |
| 5982 | norm->value.date.day); |
| 5983 | xmlSchemaFreeValue(norm); |
| 5984 | } else { |
| 5985 | snprintf(buf, 30, |
| 5986 | "%04ld:%02u:%02u", |
| 5987 | val->value.date.year, val->value.date.mon, |
| 5988 | val->value.date.day); |
| 5989 | } |
| 5990 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5991 | } |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5992 | break; |
| 5993 | case XML_SCHEMAS_DATETIME: |
| 5994 | { |
| 5995 | char buf[50]; |
| 5996 | |
| 5997 | if (val->value.date.tz_flag) { |
| 5998 | xmlSchemaValPtr norm; |
| 5999 | |
| 6000 | norm = xmlSchemaDateNormalize(val, 0); |
| 6001 | if (norm == NULL) |
| 6002 | return (-1); |
| 6003 | /* |
| 6004 | * TODO: Check if "%.14g" is portable. |
| 6005 | */ |
| 6006 | snprintf(buf, 50, |
| 6007 | "%04ld:%02u:%02uT%02u:%02u:%02.14gZ", |
| 6008 | norm->value.date.year, norm->value.date.mon, |
| 6009 | norm->value.date.day, norm->value.date.hour, |
| 6010 | norm->value.date.min, norm->value.date.sec); |
| 6011 | xmlSchemaFreeValue(norm); |
| 6012 | } else { |
| 6013 | snprintf(buf, 50, |
| 6014 | "%04ld:%02u:%02uT%02u:%02u:%02.14g", |
| 6015 | val->value.date.year, val->value.date.mon, |
| 6016 | val->value.date.day, val->value.date.hour, |
| 6017 | val->value.date.min, val->value.date.sec); |
| 6018 | } |
| 6019 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 6020 | } |
| 6021 | break; |
| 6022 | case XML_SCHEMAS_HEXBINARY: |
| 6023 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str); |
| 6024 | break; |
| 6025 | case XML_SCHEMAS_BASE64BINARY: |
| 6026 | /* |
| 6027 | * TODO: Is the following spec piece implemented?: |
| 6028 | * SPEC: "Note: For some values the canonical form defined |
| 6029 | * above does not conform to [RFC 2045], which requires breaking |
| 6030 | * with linefeeds at appropriate intervals." |
| 6031 | */ |
| 6032 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str); |
| 6033 | break; |
| 6034 | case XML_SCHEMAS_FLOAT: { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6035 | char buf[30]; |
| 6036 | /* |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 6037 | * |m| < 16777216, -149 <= e <= 104. |
| 6038 | * TODO: Handle, NaN, INF, -INF. The format is not |
| 6039 | * yet conformant. The c type float does not cover |
| 6040 | * the whole range. |
| 6041 | */ |
| 6042 | snprintf(buf, 30, "%01.14e", val->value.f); |
| 6043 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 6044 | } |
| 6045 | break; |
| 6046 | case XML_SCHEMAS_DOUBLE: { |
| 6047 | char buf[40]; |
| 6048 | /* |m| < 9007199254740992, -1075 <= e <= 970 */ |
| 6049 | /* |
| 6050 | * TODO: Handle, NaN, INF, -INF. The format is not |
| 6051 | * yet conformant. The c type float does not cover |
| 6052 | * the whole range. |
| 6053 | */ |
| 6054 | snprintf(buf, 40, "%01.14e", val->value.d); |
| 6055 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 6056 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6057 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 6058 | default: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6059 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "???"); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 6060 | return (1); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 6061 | } |
Daniel Veillard | 26ab0e6 | 2006-10-11 12:32:51 +0000 | [diff] [blame] | 6062 | if (*retValue == NULL) |
| 6063 | return(-1); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6064 | return (0); |
| 6065 | } |
| 6066 | |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 6067 | /** |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 6068 | * xmlSchemaGetCanonValueWhtsp: |
| 6069 | * @val: the precomputed value |
| 6070 | * @retValue: the returned value |
| 6071 | * @ws: the whitespace type of the value |
| 6072 | * |
Jan Pokorný | 7a7cad6 | 2013-11-29 23:26:26 +0100 | [diff] [blame] | 6073 | * Get the canonical representation of the value. |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 6074 | * The caller has to free the returned @retValue. |
| 6075 | * |
| 6076 | * Returns 0 if the value could be built, 1 if the value type is |
| 6077 | * not supported yet and -1 in case of API errors. |
| 6078 | */ |
| 6079 | int |
| 6080 | xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val, |
| 6081 | const xmlChar **retValue, |
| 6082 | xmlSchemaWhitespaceValueType ws) |
| 6083 | { |
| 6084 | if ((retValue == NULL) || (val == NULL)) |
| 6085 | return (-1); |
| 6086 | if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) || |
| 6087 | (ws > XML_SCHEMA_WHITESPACE_COLLAPSE)) |
| 6088 | return (-1); |
| 6089 | |
| 6090 | *retValue = NULL; |
| 6091 | switch (val->type) { |
| 6092 | case XML_SCHEMAS_STRING: |
| 6093 | if (val->value.str == NULL) |
| 6094 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 6095 | else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 6096 | *retValue = xmlSchemaCollapseString(val->value.str); |
| 6097 | else if (ws == XML_SCHEMA_WHITESPACE_REPLACE) |
| 6098 | *retValue = xmlSchemaWhiteSpaceReplace(val->value.str); |
| 6099 | if ((*retValue) == NULL) |
| 6100 | *retValue = BAD_CAST xmlStrdup(val->value.str); |
| 6101 | break; |
| 6102 | case XML_SCHEMAS_NORMSTRING: |
| 6103 | if (val->value.str == NULL) |
| 6104 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 6105 | else { |
| 6106 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 6107 | *retValue = xmlSchemaCollapseString(val->value.str); |
| 6108 | else |
| 6109 | *retValue = xmlSchemaWhiteSpaceReplace(val->value.str); |
| 6110 | if ((*retValue) == NULL) |
| 6111 | *retValue = BAD_CAST xmlStrdup(val->value.str); |
| 6112 | } |
| 6113 | break; |
| 6114 | default: |
| 6115 | return (xmlSchemaGetCanonValue(val, retValue)); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6116 | } |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 6117 | return (0); |
| 6118 | } |
| 6119 | |
| 6120 | /** |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 6121 | * xmlSchemaGetValType: |
| 6122 | * @val: a schemas value |
| 6123 | * |
| 6124 | * Accessor for the type of a value |
| 6125 | * |
| 6126 | * Returns the xmlSchemaValType of the value |
| 6127 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6128 | xmlSchemaValType |
| 6129 | xmlSchemaGetValType(xmlSchemaValPtr val) |
| 6130 | { |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 6131 | if (val == NULL) |
| 6132 | return(XML_SCHEMAS_UNKNOWN); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6133 | return (val->type); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 6136 | #define bottom_xmlschemastypes |
| 6137 | #include "elfgcchack.h" |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6138 | #endif /* LIBXML_SCHEMAS_ENABLED */ |