2 * schemastypes.c : implementation of the XML Schema Datatypes
3 * definition and validity checking
5 * See Copyright for the status of this software.
7 * Daniel Veillard <veillard@redhat.com>
13 #ifdef LIBXML_SCHEMAS_ENABLED
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>
21 #include <libxml/xpath.h>
22 #include <libxml/uri.h>
24 #include <libxml/xmlschemas.h>
25 #include <libxml/schemasInternals.h>
26 #include <libxml/xmlschemastypes.h>
37 #ifndef LIBXML_XPATH_ENABLED
38 extern double xmlXPathNAN;
39 extern double xmlXPathPINF;
40 extern double xmlXPathNINF;
44 xmlGenericError(xmlGenericErrorContext, \
45 "Unimplemented block at %s:%d\n", \
48 #define XML_SCHEMAS_NAMESPACE_NAME \
49 (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
51 #define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \
54 #define IS_WSP_SPACE_CH(c) ((c) == 0x20)
56 #define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c)
59 typedef struct _xmlSchemaValDate xmlSchemaValDate;
60 typedef xmlSchemaValDate *xmlSchemaValDatePtr;
61 struct _xmlSchemaValDate {
63 unsigned int mon :4; /* 1 <= mon <= 12 */
64 unsigned int day :5; /* 1 <= day <= 31 */
65 unsigned int hour :5; /* 0 <= hour <= 24 */
66 unsigned int min :6; /* 0 <= min <= 59 */
68 unsigned int tz_flag :1; /* is tzo explicitely set? */
69 signed int tzo :12; /* -1440 <= tzo <= 1440;
70 currently only -840 to +840 are needed */
74 typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
75 typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
76 struct _xmlSchemaValDuration {
77 long mon; /* mon stores years also */
79 double sec; /* sec stores min and hour also */
82 typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
83 typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
84 struct _xmlSchemaValDecimal {
85 /* would use long long but not portable */
95 typedef struct _xmlSchemaValQName xmlSchemaValQName;
96 typedef xmlSchemaValQName *xmlSchemaValQNamePtr;
97 struct _xmlSchemaValQName {
102 typedef struct _xmlSchemaValHex xmlSchemaValHex;
103 typedef xmlSchemaValHex *xmlSchemaValHexPtr;
104 struct _xmlSchemaValHex {
109 typedef struct _xmlSchemaValBase64 xmlSchemaValBase64;
110 typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr;
111 struct _xmlSchemaValBase64 {
116 struct _xmlSchemaVal {
117 xmlSchemaValType type;
118 struct _xmlSchemaVal *next;
120 xmlSchemaValDecimal decimal;
121 xmlSchemaValDate date;
122 xmlSchemaValDuration dur;
123 xmlSchemaValQName qname;
125 xmlSchemaValBase64 base64;
133 static int xmlSchemaTypesInitialized = 0;
134 static xmlHashTablePtr xmlSchemaTypesBank = NULL;
139 static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
140 static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
141 static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
142 static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
143 static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
144 static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
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;
152 static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
153 static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
154 static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
155 static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL;
156 static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL;
157 static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
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;
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;
180 static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
181 static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
182 static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
183 static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
184 static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL;
185 static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL;
186 static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL;
187 static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
188 static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
190 /************************************************************************
192 * Datatype error handlers *
194 ************************************************************************/
196 * xmlSchemaTypeErrMemory:
197 * @extra: extra informations
199 * Handle an out of memory condition
202 xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra)
204 __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra);
207 /************************************************************************
209 * Base types support *
211 ************************************************************************/
215 * @type: the value type
217 * Allocate a new simple type value
219 * Returns a pointer to the new value or NULL in case of error
221 static xmlSchemaValPtr
222 xmlSchemaNewValue(xmlSchemaValType type) {
223 xmlSchemaValPtr value;
225 value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
229 memset(value, 0, sizeof(xmlSchemaVal));
234 static xmlSchemaFacetPtr
235 xmlSchemaNewMinLengthFacet(int value)
237 xmlSchemaFacetPtr ret;
239 ret = xmlSchemaNewFacet();
243 ret->type = XML_SCHEMA_FACET_MINLENGTH;
244 ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER);
245 if (ret->val == NULL) {
249 ret->val->value.decimal.lo = value;
254 * xmlSchemaInitBasicType:
255 * @name: the type name
256 * @type: the value type associated
258 * Initialize one primitive built-in type
260 static xmlSchemaTypePtr
261 xmlSchemaInitBasicType(const char *name, xmlSchemaValType type,
262 xmlSchemaTypePtr baseType) {
263 xmlSchemaTypePtr ret;
265 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
267 xmlSchemaTypeErrMemory(NULL, "could not initialize basic types");
270 memset(ret, 0, sizeof(xmlSchemaType));
271 ret->name = (const xmlChar *)name;
272 ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME;
273 ret->type = XML_SCHEMA_TYPE_BASIC;
274 ret->baseType = baseType;
275 ret->contentType = XML_SCHEMA_CONTENT_BASIC;
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:
299 ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE;
308 case XML_SCHEMAS_ANYTYPE:
309 case XML_SCHEMAS_ANYSIMPLETYPE:
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);
316 ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
319 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
322 xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
323 XML_SCHEMAS_NAMESPACE_NAME, ret);
324 ret->builtInType = type;
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.
334 #define UNBOUNDED (1 << 30)
336 typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
337 typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
338 struct _xmlSchemaTreeItem {
339 xmlSchemaTypeType type;
340 xmlSchemaAnnotPtr annot;
341 xmlSchemaTreeItemPtr next;
342 xmlSchemaTreeItemPtr children;
345 typedef struct _xmlSchemaParticle xmlSchemaParticle;
346 typedef xmlSchemaParticle *xmlSchemaParticlePtr;
347 struct _xmlSchemaParticle {
348 xmlSchemaTypeType type;
349 xmlSchemaAnnotPtr annot;
350 xmlSchemaTreeItemPtr next;
351 xmlSchemaTreeItemPtr children;
357 typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
358 typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
359 struct _xmlSchemaModelGroup {
360 xmlSchemaTypeType type;
361 xmlSchemaAnnotPtr annot;
362 xmlSchemaTreeItemPtr next;
363 xmlSchemaTreeItemPtr children;
367 static xmlSchemaParticlePtr
368 xmlSchemaAddParticle(void)
370 xmlSchemaParticlePtr ret = NULL;
372 ret = (xmlSchemaParticlePtr)
373 xmlMalloc(sizeof(xmlSchemaParticle));
375 xmlSchemaTypeErrMemory(NULL, "allocating particle component");
378 memset(ret, 0, sizeof(xmlSchemaParticle));
379 ret->type = XML_SCHEMA_TYPE_PARTICLE;
386 * xmlSchemaInitTypes:
388 * Initialize the default XML Schemas type library
391 xmlSchemaInitTypes(void)
393 if (xmlSchemaTypesInitialized != 0)
395 xmlSchemaTypesBank = xmlHashCreate(40);
399 * 3.4.7 Built-in Complex Type Definition
401 xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
404 xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef;
405 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
407 * Init the content type.
409 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
411 xmlSchemaParticlePtr particle;
412 xmlSchemaModelGroupPtr sequence;
413 xmlSchemaWildcardPtr wild;
414 /* First particle. */
415 particle = xmlSchemaAddParticle();
416 if (particle == NULL)
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");
426 memset(sequence, 0, sizeof(xmlSchemaModelGroup));
427 sequence->type = XML_SCHEMA_TYPE_SEQUENCE;
428 particle->children = (xmlSchemaTreeItemPtr) sequence;
429 /* Second particle. */
430 particle = xmlSchemaAddParticle();
431 if (particle == NULL)
433 particle->minOccurs = 0;
434 particle->maxOccurs = UNBOUNDED;
435 sequence->children = (xmlSchemaTreeItemPtr) particle;
437 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
439 xmlSchemaTypeErrMemory(NULL, "allocating wildcard component");
442 memset(wild, 0, sizeof(xmlSchemaWildcard));
443 wild->type = XML_SCHEMA_TYPE_ANY;
445 wild->processContents = XML_SCHEMAS_ANY_LAX;
446 particle->children = (xmlSchemaTreeItemPtr) wild;
448 * Create the attribute wildcard.
450 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
452 xmlSchemaTypeErrMemory(NULL, "could not create an attribute "
453 "wildcard on anyType");
456 memset(wild, 0, sizeof(xmlSchemaWildcard));
458 wild->processContents = XML_SCHEMAS_ANY_LAX;
459 xmlSchemaTypeAnyTypeDef->attributeWildcard = wild;
461 xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
462 XML_SCHEMAS_ANYSIMPLETYPE,
463 xmlSchemaTypeAnyTypeDef);
465 * primitive datatypes
467 xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
469 xmlSchemaTypeAnySimpleTypeDef);
470 xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
472 xmlSchemaTypeAnySimpleTypeDef);
473 xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
475 xmlSchemaTypeAnySimpleTypeDef);
476 xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
477 XML_SCHEMAS_DATETIME,
478 xmlSchemaTypeAnySimpleTypeDef);
479 xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
481 xmlSchemaTypeAnySimpleTypeDef);
482 xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
484 xmlSchemaTypeAnySimpleTypeDef);
485 xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
486 XML_SCHEMAS_GYEARMONTH,
487 xmlSchemaTypeAnySimpleTypeDef);
488 xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
490 xmlSchemaTypeAnySimpleTypeDef);
491 xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
492 XML_SCHEMAS_GMONTHDAY,
493 xmlSchemaTypeAnySimpleTypeDef);
494 xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
496 xmlSchemaTypeAnySimpleTypeDef);
497 xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
498 XML_SCHEMAS_DURATION,
499 xmlSchemaTypeAnySimpleTypeDef);
500 xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
502 xmlSchemaTypeAnySimpleTypeDef);
503 xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
505 xmlSchemaTypeAnySimpleTypeDef);
506 xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
508 xmlSchemaTypeAnySimpleTypeDef);
509 xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
511 xmlSchemaTypeAnySimpleTypeDef);
512 xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
513 XML_SCHEMAS_HEXBINARY,
514 xmlSchemaTypeAnySimpleTypeDef);
515 xmlSchemaTypeBase64BinaryDef
516 = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY,
517 xmlSchemaTypeAnySimpleTypeDef);
518 xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
519 XML_SCHEMAS_NOTATION,
520 xmlSchemaTypeAnySimpleTypeDef);
521 xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
523 xmlSchemaTypeAnySimpleTypeDef);
528 xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
530 xmlSchemaTypeDecimalDef);
531 xmlSchemaTypeNonPositiveIntegerDef =
532 xmlSchemaInitBasicType("nonPositiveInteger",
533 XML_SCHEMAS_NPINTEGER,
534 xmlSchemaTypeIntegerDef);
535 xmlSchemaTypeNegativeIntegerDef =
536 xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER,
537 xmlSchemaTypeNonPositiveIntegerDef);
538 xmlSchemaTypeLongDef =
539 xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG,
540 xmlSchemaTypeIntegerDef);
541 xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT,
542 xmlSchemaTypeLongDef);
543 xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
545 xmlSchemaTypeIntDef);
546 xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
548 xmlSchemaTypeShortDef);
549 xmlSchemaTypeNonNegativeIntegerDef =
550 xmlSchemaInitBasicType("nonNegativeInteger",
551 XML_SCHEMAS_NNINTEGER,
552 xmlSchemaTypeIntegerDef);
553 xmlSchemaTypeUnsignedLongDef =
554 xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG,
555 xmlSchemaTypeNonNegativeIntegerDef);
556 xmlSchemaTypeUnsignedIntDef =
557 xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT,
558 xmlSchemaTypeUnsignedLongDef);
559 xmlSchemaTypeUnsignedShortDef =
560 xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT,
561 xmlSchemaTypeUnsignedIntDef);
562 xmlSchemaTypeUnsignedByteDef =
563 xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE,
564 xmlSchemaTypeUnsignedShortDef);
565 xmlSchemaTypePositiveIntegerDef =
566 xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER,
567 xmlSchemaTypeNonNegativeIntegerDef);
568 xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
569 XML_SCHEMAS_NORMSTRING,
570 xmlSchemaTypeStringDef);
571 xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
573 xmlSchemaTypeNormStringDef);
574 xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
575 XML_SCHEMAS_LANGUAGE,
576 xmlSchemaTypeTokenDef);
577 xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
579 xmlSchemaTypeTokenDef);
580 xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
582 xmlSchemaTypeTokenDef);
583 xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
585 xmlSchemaTypeNameDef);
586 xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID,
587 xmlSchemaTypeNCNameDef);
588 xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
590 xmlSchemaTypeNCNameDef);
591 xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
593 xmlSchemaTypeNCNameDef);
595 * Derived list types.
598 xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
599 XML_SCHEMAS_ENTITIES,
600 xmlSchemaTypeAnySimpleTypeDef);
601 xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef;
603 xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
605 xmlSchemaTypeAnySimpleTypeDef);
606 xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef;
609 xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
610 XML_SCHEMAS_NMTOKENS,
611 xmlSchemaTypeAnySimpleTypeDef);
612 xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef;
614 xmlSchemaTypesInitialized = 1;
618 * xmlSchemaCleanupTypes:
620 * Cleanup the default XML Schemas type library
623 xmlSchemaCleanupTypes(void) {
624 if (xmlSchemaTypesInitialized == 0)
630 xmlSchemaParticlePtr particle;
631 /* Attribute wildcard. */
632 xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard);
634 particle = (xmlSchemaParticlePtr) xmlSchemaTypeAnyTypeDef->subtypes;
636 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr)
637 particle->children->children->children);
638 xmlFree((xmlSchemaParticlePtr) particle->children->children);
639 /* Sequence model group. */
640 xmlFree((xmlSchemaModelGroupPtr) particle->children);
641 xmlFree((xmlSchemaParticlePtr) particle);
642 xmlSchemaTypeAnyTypeDef->subtypes = NULL;
644 xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
645 xmlSchemaTypesInitialized = 0;
649 * xmlSchemaIsBuiltInTypeFacet:
650 * @type: the built-in type
651 * @facetType: the facet type
653 * Evaluates if a specific facet can be
654 * used in conjunction with a type.
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.
660 xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType)
664 if (type->type != XML_SCHEMA_TYPE_BASIC)
666 switch (type->builtInType) {
667 case XML_SCHEMAS_BOOLEAN:
668 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
669 (facetType == XML_SCHEMA_FACET_WHITESPACE))
673 case XML_SCHEMAS_STRING:
674 case XML_SCHEMAS_NOTATION:
675 case XML_SCHEMAS_QNAME:
676 case XML_SCHEMAS_ANYURI:
677 case XML_SCHEMAS_BASE64BINARY:
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))
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))
701 case XML_SCHEMAS_TIME:
702 case XML_SCHEMAS_GDAY:
703 case XML_SCHEMAS_GMONTH:
704 case XML_SCHEMAS_GMONTHDAY:
705 case XML_SCHEMAS_GYEAR:
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))
729 * xmlSchemaGetBuiltInType:
730 * @type: the type of the built in type
732 * Gives you the type struct for a built-in
733 * type by its type id.
735 * Returns the type if found, NULL otherwise.
738 xmlSchemaGetBuiltInType(xmlSchemaValType type)
740 if (xmlSchemaTypesInitialized == 0)
741 xmlSchemaInitTypes();
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:
759 return (xmlSchemaTypeGMonthDayDef);
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);
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:
835 return (xmlSchemaTypeAnyTypeDef);
842 * xmlSchemaValueAppend:
844 * @cur: the value to be appended
846 * Appends a next sibling to a list of computed values.
848 * Returns 0 if succeeded and -1 on API errors.
851 xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) {
853 if ((prev == NULL) || (cur == NULL))
860 * xmlSchemaValueGetNext:
863 * Accessor for the next sibling of a list of computed values.
865 * Returns the next value or NULL if there was none, or on
869 xmlSchemaValueGetNext(xmlSchemaValPtr cur) {
877 * xmlSchemaValueGetAsString:
880 * Accessor for the string value of a computed value.
882 * Returns the string value or NULL if there was none, or on
886 xmlSchemaValueGetAsString(xmlSchemaValPtr val)
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:
900 case XML_SCHEMAS_IDREF:
901 case XML_SCHEMAS_ENTITY:
902 case XML_SCHEMAS_ANYURI:
903 return (BAD_CAST val->value.str);
911 * xmlSchemaValueGetAsBoolean:
914 * Accessor for the boolean value of a computed value.
916 * Returns 1 if true and 0 if false, or in case of an error. Hmm.
919 xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val)
921 if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN))
923 return (val->value.b);
927 * xmlSchemaNewStringValue:
928 * @type: the value type
931 * Allocate a new simple type value. The type can be
932 * of XML_SCHEMAS_STRING.
933 * WARNING: This one is intended to be expanded for other
934 * string based types. We need this for anySimpleType as well.
935 * The given value is consumed and freed with the struct.
937 * Returns a pointer to the new value or NULL in case of error
940 xmlSchemaNewStringValue(xmlSchemaValType type,
941 const xmlChar *value)
945 if (type != XML_SCHEMAS_STRING)
947 val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
951 memset(val, 0, sizeof(xmlSchemaVal));
953 val->value.str = (xmlChar *) value;
958 * xmlSchemaNewNOTATIONValue:
959 * @name: the notation name
960 * @ns: the notation namespace name or NULL
962 * Allocate a new NOTATION value.
963 * The given values are consumed and freed with the struct.
965 * Returns a pointer to the new value or NULL in case of error
968 xmlSchemaNewNOTATIONValue(const xmlChar *name,
973 val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
977 val->value.qname.name = (xmlChar *)name;
979 val->value.qname.uri = (xmlChar *)ns;
984 * xmlSchemaNewQNameValue:
985 * @namespaceName: the namespace name
986 * @localName: the local name
988 * Allocate a new QName value.
989 * The given values are consumed and freed with the struct.
991 * Returns a pointer to the new value or NULL in case of an error.
994 xmlSchemaNewQNameValue(const xmlChar *namespaceName,
995 const xmlChar *localName)
999 val = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
1003 val->value.qname.name = (xmlChar *) localName;
1004 val->value.qname.uri = (xmlChar *) namespaceName;
1009 * xmlSchemaFreeValue:
1010 * @value: the value to free
1012 * Cleanup the default XML Schemas type library
1015 xmlSchemaFreeValue(xmlSchemaValPtr value) {
1016 xmlSchemaValPtr prev;
1018 while (value != NULL) {
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:
1032 case XML_SCHEMAS_ENTITIES:
1033 case XML_SCHEMAS_ANYURI:
1034 case XML_SCHEMAS_ANYSIMPLETYPE:
1035 if (value->value.str != NULL)
1036 xmlFree(value->value.str);
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);
1045 case XML_SCHEMAS_HEXBINARY:
1046 if (value->value.hex.str != NULL)
1047 xmlFree(value->value.hex.str);
1049 case XML_SCHEMAS_BASE64BINARY:
1050 if (value->value.base64.str != NULL)
1051 xmlFree(value->value.base64.str);
1057 value = value->next;
1063 * xmlSchemaGetPredefinedType:
1064 * @name: the type name
1065 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
1067 * Lookup a type in the default XML Schemas type library
1069 * Returns the type if found, NULL otherwise
1072 xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
1073 if (xmlSchemaTypesInitialized == 0)
1074 xmlSchemaInitTypes();
1077 return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
1081 * xmlSchemaGetBuiltInListSimpleTypeItemType:
1082 * @type: the built-in simple type.
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.
1090 xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type)
1092 if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC))
1094 switch (type->builtInType) {
1095 case XML_SCHEMAS_NMTOKENS:
1096 return (xmlSchemaTypeNmtokenDef );
1097 case XML_SCHEMAS_IDREFS:
1098 return (xmlSchemaTypeIdrefDef);
1099 case XML_SCHEMAS_ENTITIES:
1100 return (xmlSchemaTypeEntityDef);
1106 /****************************************************************
1108 * Convenience macros and functions *
1110 ****************************************************************/
1112 #define IS_TZO_CHAR(c) \
1113 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
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))
1122 #define VALID_TZO(tzo) ((tzo > -840) && (tzo < 840))
1123 #define IS_LEAP(y) \
1124 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
1126 static const unsigned int daysInMonth[12] =
1127 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1128 static const unsigned int daysInMonthLeap[12] =
1129 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1131 #define MAX_DAYINMONTH(yr,mon) \
1132 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
1134 #define VALID_MDAY(dt) \
1135 (IS_LEAP(dt->year) ? \
1136 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
1137 (dt->day <= daysInMonth[dt->mon - 1]))
1139 #define VALID_DATE(dt) \
1140 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
1142 #define VALID_END_OF_DAY(dt) \
1143 ((dt)->hour == 24 && (dt)->min == 0 && (dt)->sec == 0)
1145 #define VALID_TIME(dt) \
1146 (((VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
1147 VALID_SEC(dt->sec)) || VALID_END_OF_DAY(dt)) && \
1150 #define VALID_DATETIME(dt) \
1151 (VALID_DATE(dt) && VALID_TIME(dt))
1153 #define SECS_PER_MIN (60)
1154 #define SECS_PER_HOUR (60 * SECS_PER_MIN)
1155 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
1157 static const long dayInYearByMonth[12] =
1158 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1159 static const long dayInLeapYearByMonth[12] =
1160 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
1162 #define DAY_IN_YEAR(day, month, year) \
1164 dayInLeapYearByMonth[month - 1] : \
1165 dayInYearByMonth[month - 1]) + day)
1168 #define DEBUG_DATE(dt) \
1169 xmlGenericError(xmlGenericErrorContext, \
1170 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
1171 dt->type,dt->value.date.year,dt->value.date.mon, \
1172 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
1173 dt->value.date.sec); \
1174 if (dt->value.date.tz_flag) \
1175 if (dt->value.date.tzo != 0) \
1176 xmlGenericError(xmlGenericErrorContext, \
1177 "%+05d\n",dt->value.date.tzo); \
1179 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
1181 xmlGenericError(xmlGenericErrorContext,"\n")
1183 #define DEBUG_DATE(dt)
1187 * _xmlSchemaParseGYear:
1188 * @dt: pointer to a date structure
1189 * @str: pointer to the string to analyze
1191 * Parses a xs:gYear without time zone and fills in the appropriate
1192 * field of the @dt structure. @str is updated to point just after the
1193 * xs:gYear. It is supposed that @dt->year is big enough to contain
1196 * Returns 0 or the error code
1199 _xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
1200 const xmlChar *cur = *str, *firstChar;
1201 int isneg = 0, digcnt = 0;
1203 if (((*cur < '0') || (*cur > '9')) &&
1204 (*cur != '-') && (*cur != '+'))
1214 while ((*cur >= '0') && (*cur <= '9')) {
1215 dt->year = dt->year * 10 + (*cur - '0');
1220 /* year must be at least 4 digits (CCYY); over 4
1221 * digits cannot have a leading zero. */
1222 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
1226 dt->year = - dt->year;
1228 if (!VALID_YEAR(dt->year))
1237 * @num: the integer to fill in
1238 * @cur: an #xmlChar *
1239 * @invalid: an integer
1241 * Parses a 2-digits integer and updates @num with the value. @cur is
1242 * updated to point just after the integer.
1243 * In case of error, @invalid is set to %TRUE, values of @num and
1244 * @cur are undefined.
1246 #define PARSE_2_DIGITS(num, cur, invalid) \
1247 if ((cur[0] < '0') || (cur[0] > '9') || \
1248 (cur[1] < '0') || (cur[1] > '9')) \
1251 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
1256 * @num: the double to fill in
1257 * @cur: an #xmlChar *
1258 * @invalid: an integer
1260 * Parses a float and updates @num with the value. @cur is
1261 * updated to point just after the float. The float must have a
1262 * 2-digits integer part and may or may not have a decimal part.
1263 * In case of error, @invalid is set to %TRUE, values of @num and
1264 * @cur are undefined.
1266 #define PARSE_FLOAT(num, cur, invalid) \
1267 PARSE_2_DIGITS(num, cur, invalid); \
1268 if (!invalid && (*cur == '.')) { \
1271 if ((*cur < '0') || (*cur > '9')) \
1273 while ((*cur >= '0') && (*cur <= '9')) { \
1275 num += (*cur - '0') * mult; \
1281 * _xmlSchemaParseGMonth:
1282 * @dt: pointer to a date structure
1283 * @str: pointer to the string to analyze
1285 * Parses a xs:gMonth without time zone and fills in the appropriate
1286 * field of the @dt structure. @str is updated to point just after the
1289 * Returns 0 or the error code
1292 _xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
1293 const xmlChar *cur = *str;
1295 unsigned int value = 0;
1297 PARSE_2_DIGITS(value, cur, ret);
1301 if (!VALID_MONTH(value))
1311 * _xmlSchemaParseGDay:
1312 * @dt: pointer to a date structure
1313 * @str: pointer to the string to analyze
1315 * Parses a xs:gDay without time zone and fills in the appropriate
1316 * field of the @dt structure. @str is updated to point just after the
1319 * Returns 0 or the error code
1322 _xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
1323 const xmlChar *cur = *str;
1325 unsigned int value = 0;
1327 PARSE_2_DIGITS(value, cur, ret);
1331 if (!VALID_DAY(value))
1340 * _xmlSchemaParseTime:
1341 * @dt: pointer to a date structure
1342 * @str: pointer to the string to analyze
1344 * Parses a xs:time without time zone and fills in the appropriate
1345 * fields of the @dt structure. @str is updated to point just after the
1347 * In case of error, values of @dt fields are undefined.
1349 * Returns 0 or the error code
1352 _xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
1353 const xmlChar *cur = *str;
1357 PARSE_2_DIGITS(value, cur, ret);
1362 if (!VALID_HOUR(value) && value != 24 /* Allow end-of-day hour */)
1366 /* the ':' insures this string is xs:time */
1369 PARSE_2_DIGITS(value, cur, ret);
1372 if (!VALID_MIN(value))
1380 PARSE_FLOAT(dt->sec, cur, ret);
1384 if (!VALID_TIME(dt))
1392 * _xmlSchemaParseTimeZone:
1393 * @dt: pointer to a date structure
1394 * @str: pointer to the string to analyze
1396 * Parses a time zone without time zone and fills in the appropriate
1397 * field of the @dt structure. @str is updated to point just after the
1400 * Returns 0 or the error code
1403 _xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
1425 int isneg = 0, tmp = 0;
1426 isneg = (*cur == '-');
1430 PARSE_2_DIGITS(tmp, cur, ret);
1433 if (!VALID_HOUR(tmp))
1442 PARSE_2_DIGITS(tmp, cur, ret);
1445 if (!VALID_MIN(tmp))
1450 dt->tzo = - dt->tzo;
1452 if (!VALID_TZO(dt->tzo))
1467 * _xmlSchemaBase64Decode:
1470 * Converts a base64 encoded character to its base 64 value.
1472 * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
1475 _xmlSchemaBase64Decode (const xmlChar ch) {
1476 if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
1477 if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
1478 if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
1479 if ('+' == ch) return 62;
1480 if ('/' == ch) return 63;
1481 if ('=' == ch) return 64;
1485 /****************************************************************
1487 * XML Schema Dates/Times Datatypes Handling *
1489 ****************************************************************/
1493 * @num: the integer to fill in
1494 * @cur: an #xmlChar *
1495 * @num_type: an integer flag
1497 * Parses a digits integer and updates @num with the value. @cur is
1498 * updated to point just after the integer.
1499 * In case of error, @num_type is set to -1, values of @num and
1500 * @cur are undefined.
1502 #define PARSE_DIGITS(num, cur, num_type) \
1503 if ((*cur < '0') || (*cur > '9')) \
1506 while ((*cur >= '0') && (*cur <= '9')) { \
1507 num = num * 10 + (*cur - '0'); \
1513 * @num: the double to fill in
1514 * @cur: an #xmlChar *
1515 * @num_type: an integer flag
1517 * Parses a float or integer and updates @num with the value. @cur is
1518 * updated to point just after the number. If the number is a float,
1519 * then it must have an integer part and a decimal part; @num_type will
1520 * be set to 1. If there is no decimal part, @num_type is set to zero.
1521 * In case of error, @num_type is set to -1, values of @num and
1522 * @cur are undefined.
1524 #define PARSE_NUM(num, cur, num_type) \
1526 PARSE_DIGITS(num, cur, num_type); \
1527 if (!num_type && (*cur == '.')) { \
1530 if ((*cur < '0') || (*cur > '9')) \
1534 while ((*cur >= '0') && (*cur <= '9')) { \
1536 num += (*cur - '0') * mult; \
1542 * xmlSchemaValidateDates:
1543 * @type: the expected type or XML_SCHEMAS_UNKNOWN
1544 * @dateTime: string to analyze
1545 * @val: the return computed value
1547 * Check that @dateTime conforms to the lexical space of one of the date types.
1548 * if true a value is computed and returned in @val.
1550 * Returns 0 if this validates, a positive error code number otherwise
1551 * and -1 in case of internal or API error.
1554 xmlSchemaValidateDates (xmlSchemaValType type,
1555 const xmlChar *dateTime, xmlSchemaValPtr *val,
1559 const xmlChar *cur = dateTime;
1561 #define RETURN_TYPE_IF_VALID(t) \
1562 if (IS_TZO_CHAR(*cur)) { \
1563 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
1572 if (dateTime == NULL)
1576 while IS_WSP_BLANK_CH(*cur) cur++;
1578 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
1581 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
1585 if ((cur[0] == '-') && (cur[1] == '-')) {
1587 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
1592 /* is it an xs:gDay? */
1594 if (type == XML_SCHEMAS_GMONTH)
1597 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1601 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
1607 * it should be an xs:gMonthDay or xs:gMonth
1609 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1614 * a '-' char could indicate this type is xs:gMonthDay or
1615 * a negative time zone offset. Check for xs:gMonthDay first.
1616 * Also the first three char's of a negative tzo (-MM:SS) can
1617 * appear to be a valid day; so even if the day portion
1618 * of the xs:gMonthDay verifies, we must insure it was not
1622 const xmlChar *rewnd = cur;
1625 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1626 if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
1629 * we can use the VALID_MDAY macro to validate the month
1630 * and day because the leap year test will flag year zero
1631 * as a leap year (even though zero is an invalid year).
1632 * FUTURE TODO: Zero will become valid in XML Schema 1.1
1635 if (VALID_MDAY((&(dt->value.date)))) {
1637 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
1644 * not xs:gMonthDay so rewind and check if just xs:gMonth
1645 * with an optional time zone.
1650 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
1656 * It's a right-truncated date or an xs:time.
1657 * Try to parse an xs:time then fallback on right-truncated dates.
1659 if ((*cur >= '0') && (*cur <= '9')) {
1660 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1662 /* it's an xs:time */
1663 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
1667 /* fallback on date parsing */
1670 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
1674 /* is it an xs:gYear? */
1675 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
1681 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1685 /* is it an xs:gYearMonth? */
1686 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
1692 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1693 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
1696 /* is it an xs:date? */
1697 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
1703 /* it should be an xs:dateTime */
1704 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1708 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
1710 while IS_WSP_BLANK_CH(*cur) cur++;
1711 if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date))))))
1715 dt->type = XML_SCHEMAS_DATETIME;
1719 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
1723 * insure the parsed type is equal to or less significant (right
1724 * truncated) than the desired type.
1726 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
1728 /* time only matches time */
1729 if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
1732 if ((type == XML_SCHEMAS_DATETIME) &&
1733 ((dt->type != XML_SCHEMAS_DATE) ||
1734 (dt->type != XML_SCHEMAS_GYEARMONTH) ||
1735 (dt->type != XML_SCHEMAS_GYEAR)))
1738 if ((type == XML_SCHEMAS_DATE) &&
1739 ((dt->type != XML_SCHEMAS_GYEAR) ||
1740 (dt->type != XML_SCHEMAS_GYEARMONTH)))
1743 if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
1746 if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
1754 xmlSchemaFreeValue(dt);
1760 xmlSchemaFreeValue(dt);
1765 * xmlSchemaValidateDuration:
1766 * @type: the predefined type
1767 * @duration: string to analyze
1768 * @val: the return computed value
1770 * Check that @duration conforms to the lexical space of the duration type.
1771 * if true a value is computed and returned in @val.
1773 * Returns 0 if this validates, a positive error code number otherwise
1774 * and -1 in case of internal or API error.
1777 xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
1778 const xmlChar *duration, xmlSchemaValPtr *val,
1780 const xmlChar *cur = duration;
1781 xmlSchemaValPtr dur;
1783 unsigned int seq = 0;
1785 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
1786 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1787 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
1789 if (duration == NULL)
1793 while IS_WSP_BLANK_CH(*cur) cur++;
1800 /* duration must start with 'P' (after sign) */
1807 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1813 /* input string should be empty or invalid date/time item */
1814 if (seq >= sizeof(desig))
1817 /* T designator must be present for time items */
1824 } else if (seq == 3)
1827 /* parse the number portion of the item */
1828 PARSE_NUM(num, cur, num_type);
1830 if ((num_type == -1) || (*cur == 0))
1833 /* update duration based on item type */
1834 while (seq < sizeof(desig)) {
1835 if (*cur == desig[seq]) {
1837 /* verify numeric type; only seconds can be float */
1838 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1843 dur->value.dur.mon = (long)num * 12;
1846 dur->value.dur.mon += (long)num;
1849 /* convert to seconds using multiplier */
1850 dur->value.dur.sec += num * multi[seq];
1855 break; /* exit loop */
1857 /* no date designators found? */
1858 if ((++seq == 3) || (seq == 6))
1863 while IS_WSP_BLANK_CH(*cur) cur++;
1867 dur->value.dur.mon = -dur->value.dur.mon;
1868 dur->value.dur.day = -dur->value.dur.day;
1869 dur->value.dur.sec = -dur->value.dur.sec;
1875 xmlSchemaFreeValue(dur);
1881 xmlSchemaFreeValue(dur);
1889 * Removes the leading and ending spaces of a string
1891 * Returns the new string or NULL if no change was required.
1894 xmlSchemaStrip(const xmlChar *value) {
1895 const xmlChar *start = value, *end, *f;
1897 if (value == NULL) return(NULL);
1898 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
1900 while (*end != 0) end++;
1903 while ((end > start) && (IS_BLANK_CH(*end))) end--;
1905 if ((start == value) && (f == end)) return(NULL);
1906 return(xmlStrndup(start, end - start));
1910 * xmlSchemaWhiteSpaceReplace:
1913 * Replaces 0xd, 0x9 and 0xa with a space.
1915 * Returns the new string or NULL if no change was required.
1918 xmlSchemaWhiteSpaceReplace(const xmlChar *value) {
1919 const xmlChar *cur = value;
1920 xmlChar *ret = NULL, *mcur;
1925 while ((*cur != 0) &&
1926 (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) {
1931 ret = xmlStrdup(value);
1932 /* TODO FIXME: I guess gcc will bark at this. */
1933 mcur = (xmlChar *) (ret + (cur - value));
1935 if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) )
1938 } while (*mcur != 0);
1943 * xmlSchemaCollapseString:
1946 * Removes and normalize white spaces in the string
1948 * Returns the new string or NULL if no change was required.
1951 xmlSchemaCollapseString(const xmlChar *value) {
1952 const xmlChar *start = value, *end, *f;
1956 if (value == NULL) return(NULL);
1957 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
1960 if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
1963 } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
1972 while ((end > start) && (IS_BLANK_CH(*end))) end--;
1974 if ((start == value) && (f == end)) return(NULL);
1975 return(xmlStrndup(start, end - start));
1977 start = xmlStrdup(start);
1978 if (start == NULL) return(NULL);
1979 g = (xmlChar *) (start + col);
1982 if (IS_BLANK_CH(*end)) {
1984 while (IS_BLANK_CH(*end)) end++;
1991 return((xmlChar *) start);
1995 * xmlSchemaValAtomicListNode:
1996 * @type: the predefined atomic type for a token in the list
1997 * @value: the list value to check
1998 * @ret: the return computed value
1999 * @node: the node containing the value
2001 * Check that a value conforms to the lexical space of the predefined
2002 * list type. if true a value is computed and returned in @ret.
2004 * Returns the number of items if this validates, a negative error code
2008 xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
2009 xmlSchemaValPtr *ret, xmlNodePtr node) {
2010 xmlChar *val, *cur, *endval;
2014 if (value == NULL) {
2017 val = xmlStrdup(value);
2028 while (IS_BLANK_CH(*cur)) *cur++ = 0;
2030 if (IS_BLANK_CH(*cur)) {
2033 while (IS_BLANK_CH(*cur)) *cur++ = 0;
2037 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
2040 if (nb_values == 0) {
2046 while ((*cur == 0) && (cur != endval)) cur++;
2047 while (cur != endval) {
2048 tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
2051 while (*cur != 0) cur++;
2052 while ((*cur == 0) && (cur != endval)) cur++;
2054 /* TODO what return value ? c.f. bug #158628
2065 * xmlSchemaParseUInt:
2066 * @str: pointer to the string R/W
2067 * @llo: pointer to the low result
2068 * @lmi: pointer to the mid result
2069 * @lhi: pointer to the high result
2071 * Parse an unsigned long into 3 fields.
2073 * Returns the number of significant digits in the number or
2074 * -1 if overflow of the capacity and -2 if it's not a number.
2077 xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
2078 unsigned long *lmi, unsigned long *lhi) {
2079 unsigned long lo = 0, mi = 0, hi = 0;
2080 const xmlChar *tmp, *cur = *str;
2083 if (!((*cur >= '0') && (*cur <= '9')))
2086 while (*cur == '0') { /* ignore leading zeroes */
2090 while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
2098 hi = hi * 10 + (*cur++ - '0');
2102 mi = mi * 10 + (*cur++ - '0');
2106 lo = lo * 10 + (*cur++ - '0');
2118 * xmlSchemaValAtomicType:
2119 * @type: the predefined type
2120 * @value: the value to check
2121 * @val: the return computed value
2122 * @node: the node containing the value
2123 * flags: flags to control the vlidation
2125 * Check that a value conforms to the lexical space of the atomic type.
2126 * if true a value is computed and returned in @val.
2127 * This checks the value space for list types as well (IDREFS, NMTOKENS).
2129 * Returns 0 if this validates, a positive error code number otherwise
2130 * and -1 in case of internal or API error.
2133 xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
2134 xmlSchemaValPtr * val, xmlNodePtr node, int flags,
2135 xmlSchemaWhitespaceValueType ws,
2136 int normOnTheFly, int applyNorm, int createStringValue)
2139 xmlChar *norm = NULL;
2142 if (xmlSchemaTypesInitialized == 0)
2143 xmlSchemaInitTypes();
2148 * validating a non existant text node is similar to validating
2152 value = BAD_CAST "";
2156 if ((flags == 0) && (value != NULL)) {
2158 if ((type->builtInType != XML_SCHEMAS_STRING) &&
2159 (type->builtInType != XML_SCHEMAS_ANYTYPE) &&
2160 (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) {
2161 if (type->builtInType == XML_SCHEMAS_NORMSTRING)
2162 norm = xmlSchemaWhiteSpaceReplace(value);
2164 norm = xmlSchemaCollapseString(value);
2170 switch (type->builtInType) {
2171 case XML_SCHEMAS_UNKNOWN:
2173 case XML_SCHEMAS_ANYTYPE:
2174 case XML_SCHEMAS_ANYSIMPLETYPE:
2175 if ((createStringValue) && (val != NULL)) {
2176 v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE);
2178 v->value.str = xmlStrdup(value);
2185 case XML_SCHEMAS_STRING:
2186 if (! normOnTheFly) {
2187 const xmlChar *cur = value;
2189 if (ws == XML_SCHEMA_WHITESPACE_REPLACE) {
2191 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2197 } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
2199 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2201 } else if IS_WSP_SPACE_CH(*cur) {
2203 if IS_WSP_SPACE_CH(*cur)
2211 if (createStringValue && (val != NULL)) {
2213 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2214 norm = xmlSchemaCollapseString(value);
2215 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
2216 norm = xmlSchemaWhiteSpaceReplace(value);
2220 v = xmlSchemaNewValue(XML_SCHEMAS_STRING);
2222 v->value.str = xmlStrdup(value);
2229 case XML_SCHEMAS_NORMSTRING:{
2232 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2233 norm = xmlSchemaCollapseString(value);
2235 norm = xmlSchemaWhiteSpaceReplace(value);
2240 const xmlChar *cur = value;
2242 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2250 v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
2252 v->value.str = xmlStrdup(value);
2260 case XML_SCHEMAS_DECIMAL:{
2261 const xmlChar *cur = value;
2262 unsigned int len, neg, integ, hasLeadingZeroes;
2264 xmlChar *cptr = cval;
2266 if ((cur == NULL) || (*cur == 0))
2270 * xs:decimal has a whitespace-facet value of 'collapse'.
2273 while IS_WSP_BLANK_CH(*cur) cur++;
2276 * First we handle an optional sign.
2282 } else if (*cur == '+')
2285 * Disallow: "", "-", "- "
2290 * Next we "pre-parse" the number, in preparation for calling
2291 * the common routine xmlSchemaParseUInt. We get rid of any
2292 * leading zeroes (because we have reserved only 25 chars),
2293 * and note the position of a decimal point.
2297 hasLeadingZeroes = 0;
2299 * Skip leading zeroes.
2301 while (*cur == '0') {
2303 hasLeadingZeroes = 1;
2307 if ((*cur >= '0') && (*cur <= '9')) {
2310 } else if (*cur == '.') {
2314 if ((*cur >= '0') && (*cur <= '9')) {
2321 * Disallow "." but allow "00."
2323 if ((len == 0) && (!hasLeadingZeroes))
2331 while IS_WSP_BLANK_CH(*cur) cur++;
2333 goto return1; /* error if any extraneous chars */
2335 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
2338 * Now evaluate the significant digits of the number
2344 * Get rid of trailing zeroes in the
2347 while ((len != integ) && (*(cptr-1) == '0')) {
2353 * Terminate the (preparsed) string.
2359 xmlSchemaParseUInt((const xmlChar **)&cptr,
2360 &v->value.decimal.lo,
2361 &v->value.decimal.mi,
2362 &v->value.decimal.hi);
2366 * Set the total digits to 1 if a zero value.
2368 v->value.decimal.sign = neg;
2370 /* Speedup for zero values. */
2371 v->value.decimal.total = 1;
2373 v->value.decimal.total = len;
2375 v->value.decimal.frac = 0;
2377 v->value.decimal.frac = len - integ;
2384 case XML_SCHEMAS_TIME:
2385 case XML_SCHEMAS_GDAY:
2386 case XML_SCHEMAS_GMONTH:
2387 case XML_SCHEMAS_GMONTHDAY:
2388 case XML_SCHEMAS_GYEAR:
2389 case XML_SCHEMAS_GYEARMONTH:
2390 case XML_SCHEMAS_DATE:
2391 case XML_SCHEMAS_DATETIME:
2392 ret = xmlSchemaValidateDates(type->builtInType, value, val,
2395 case XML_SCHEMAS_DURATION:
2396 ret = xmlSchemaValidateDuration(type, value, val,
2399 case XML_SCHEMAS_FLOAT:
2400 case XML_SCHEMAS_DOUBLE: {
2401 const xmlChar *cur = value;
2403 int digits_before = 0;
2404 int digits_after = 0;
2407 while IS_WSP_BLANK_CH(*cur) cur++;
2409 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
2414 if (type == xmlSchemaTypeFloatDef) {
2415 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2417 v->value.f = (float) xmlXPathNAN;
2419 xmlSchemaFreeValue(v);
2423 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2425 v->value.d = xmlXPathNAN;
2427 xmlSchemaFreeValue(v);
2439 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
2444 if (type == xmlSchemaTypeFloatDef) {
2445 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2448 v->value.f = (float) xmlXPathNINF;
2450 v->value.f = (float) xmlXPathPINF;
2452 xmlSchemaFreeValue(v);
2456 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2459 v->value.d = xmlXPathNINF;
2461 v->value.d = xmlXPathPINF;
2463 xmlSchemaFreeValue(v);
2471 if ((neg == 0) && (*cur == '+'))
2473 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
2475 while ((*cur >= '0') && (*cur <= '9')) {
2481 while ((*cur >= '0') && (*cur <= '9')) {
2486 if ((digits_before == 0) && (digits_after == 0))
2488 if ((*cur == 'e') || (*cur == 'E')) {
2490 if ((*cur == '-') || (*cur == '+'))
2492 while ((*cur >= '0') && (*cur <= '9'))
2496 while IS_WSP_BLANK_CH(*cur) cur++;
2501 if (type == xmlSchemaTypeFloatDef) {
2502 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2505 * TODO: sscanf seems not to give the correct
2506 * value for extremely high/low values.
2507 * E.g. "1E-149" results in zero.
2509 if (sscanf((const char *) value, "%f",
2510 &(v->value.f)) == 1) {
2513 xmlSchemaFreeValue(v);
2520 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2523 * TODO: sscanf seems not to give the correct
2524 * value for extremely high/low values.
2526 if (sscanf((const char *) value, "%lf",
2527 &(v->value.d)) == 1) {
2530 xmlSchemaFreeValue(v);
2540 case XML_SCHEMAS_BOOLEAN:{
2541 const xmlChar *cur = value;
2544 while IS_WSP_BLANK_CH(*cur) cur++;
2548 } else if (*cur == '1') {
2551 } else if (*cur == 't') {
2553 if ((*cur++ == 'r') && (*cur++ == 'u') &&
2558 } else if (*cur == 'f') {
2560 if ((*cur++ == 'a') && (*cur++ == 'l') &&
2561 (*cur++ == 's') && (*cur++ == 'e')) {
2568 while IS_WSP_BLANK_CH(*cur) cur++;
2573 if ((cur[0] == '0') && (cur[1] == 0))
2575 else if ((cur[0] == '1') && (cur[1] == 0))
2577 else if ((cur[0] == 't') && (cur[1] == 'r')
2578 && (cur[2] == 'u') && (cur[3] == 'e')
2581 else if ((cur[0] == 'f') && (cur[1] == 'a')
2582 && (cur[2] == 'l') && (cur[3] == 's')
2583 && (cur[4] == 'e') && (cur[5] == 0))
2589 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
2599 case XML_SCHEMAS_TOKEN:{
2600 const xmlChar *cur = value;
2602 if (! normOnTheFly) {
2604 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2606 } else if (*cur == ' ') {
2618 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
2620 v->value.str = xmlStrdup(value);
2628 case XML_SCHEMAS_LANGUAGE:
2630 norm = xmlSchemaCollapseString(value);
2634 if (xmlCheckLanguageID(value) == 1) {
2636 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
2638 v->value.str = xmlStrdup(value);
2647 case XML_SCHEMAS_NMTOKEN:
2648 if (xmlValidateNMToken(value, 1) == 0) {
2650 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
2652 v->value.str = xmlStrdup(value);
2661 case XML_SCHEMAS_NMTOKENS:
2662 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
2669 case XML_SCHEMAS_NAME:
2670 ret = xmlValidateName(value, 1);
2671 if ((ret == 0) && (val != NULL) && (value != NULL)) {
2672 v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
2674 const xmlChar *start = value, *end;
2675 while (IS_BLANK_CH(*start)) start++;
2677 while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
2678 v->value.str = xmlStrndup(start, end - start);
2685 case XML_SCHEMAS_QNAME:{
2686 const xmlChar *uri = NULL;
2687 xmlChar *local = NULL;
2689 ret = xmlValidateQName(value, 1);
2696 local = xmlSplitQName2(value, &prefix);
2697 ns = xmlSearchNs(node->doc, node, prefix);
2698 if ((ns == NULL) && (prefix != NULL)) {
2710 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
2717 v->value.qname.name = local;
2719 v->value.qname.name = xmlStrdup(value);
2721 v->value.qname.uri = xmlStrdup(uri);
2728 case XML_SCHEMAS_NCNAME:
2729 ret = xmlValidateNCName(value, 1);
2730 if ((ret == 0) && (val != NULL)) {
2731 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
2733 v->value.str = xmlStrdup(value);
2740 case XML_SCHEMAS_ID:
2741 ret = xmlValidateNCName(value, 1);
2742 if ((ret == 0) && (val != NULL)) {
2743 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
2745 v->value.str = xmlStrdup(value);
2751 if ((ret == 0) && (node != NULL) &&
2752 (node->type == XML_ATTRIBUTE_NODE)) {
2753 xmlAttrPtr attr = (xmlAttrPtr) node;
2756 * NOTE: the IDness might have already be declared in the DTD
2758 if (attr->atype != XML_ATTRIBUTE_ID) {
2762 strip = xmlSchemaStrip(value);
2763 if (strip != NULL) {
2764 res = xmlAddID(NULL, node->doc, strip, attr);
2767 res = xmlAddID(NULL, node->doc, value, attr);
2771 attr->atype = XML_ATTRIBUTE_ID;
2776 case XML_SCHEMAS_IDREF:
2777 ret = xmlValidateNCName(value, 1);
2778 if ((ret == 0) && (val != NULL)) {
2779 v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
2782 v->value.str = xmlStrdup(value);
2785 if ((ret == 0) && (node != NULL) &&
2786 (node->type == XML_ATTRIBUTE_NODE)) {
2787 xmlAttrPtr attr = (xmlAttrPtr) node;
2790 strip = xmlSchemaStrip(value);
2791 if (strip != NULL) {
2792 xmlAddRef(NULL, node->doc, strip, attr);
2795 xmlAddRef(NULL, node->doc, value, attr);
2796 attr->atype = XML_ATTRIBUTE_IDREF;
2799 case XML_SCHEMAS_IDREFS:
2800 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
2806 if ((ret == 0) && (node != NULL) &&
2807 (node->type == XML_ATTRIBUTE_NODE)) {
2808 xmlAttrPtr attr = (xmlAttrPtr) node;
2810 attr->atype = XML_ATTRIBUTE_IDREFS;
2813 case XML_SCHEMAS_ENTITY:{
2816 ret = xmlValidateNCName(value, 1);
2817 if ((node == NULL) || (node->doc == NULL))
2822 strip = xmlSchemaStrip(value);
2823 if (strip != NULL) {
2824 ent = xmlGetDocEntity(node->doc, strip);
2827 ent = xmlGetDocEntity(node->doc, value);
2829 if ((ent == NULL) ||
2831 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
2834 if ((ret == 0) && (val != NULL)) {
2837 if ((ret == 0) && (node != NULL) &&
2838 (node->type == XML_ATTRIBUTE_NODE)) {
2839 xmlAttrPtr attr = (xmlAttrPtr) node;
2841 attr->atype = XML_ATTRIBUTE_ENTITY;
2845 case XML_SCHEMAS_ENTITIES:
2846 if ((node == NULL) || (node->doc == NULL))
2848 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
2854 if ((ret == 0) && (node != NULL) &&
2855 (node->type == XML_ATTRIBUTE_NODE)) {
2856 xmlAttrPtr attr = (xmlAttrPtr) node;
2858 attr->atype = XML_ATTRIBUTE_ENTITIES;
2861 case XML_SCHEMAS_NOTATION:{
2862 xmlChar *uri = NULL;
2863 xmlChar *local = NULL;
2865 ret = xmlValidateQName(value, 1);
2866 if ((ret == 0) && (node != NULL)) {
2869 local = xmlSplitQName2(value, &prefix);
2870 if (prefix != NULL) {
2873 ns = xmlSearchNs(node->doc, node, prefix);
2876 else if (val != NULL)
2877 uri = xmlStrdup(ns->href);
2879 if ((local != NULL) && ((val == NULL) || (ret != 0)))
2884 if ((node == NULL) || (node->doc == NULL))
2887 ret = xmlValidateNotationUse(NULL, node->doc, value);
2893 if ((ret == 0) && (val != NULL)) {
2894 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
2897 v->value.qname.name = local;
2899 v->value.qname.name = xmlStrdup(value);
2901 v->value.qname.uri = uri;
2914 case XML_SCHEMAS_ANYURI:{
2917 xmlChar *tmpval, *cur;
2919 norm = xmlSchemaCollapseString(value);
2923 tmpval = xmlStrdup(value);
2924 for (cur = tmpval; *cur; ++cur) {
2925 if (*cur < 32 || *cur >= 127 || *cur == ' ' ||
2926 *cur == '<' || *cur == '>' || *cur == '"' ||
2927 *cur == '{' || *cur == '}' || *cur == '|' ||
2928 *cur == '\\' || *cur == '^' || *cur == '`' ||
2932 uri = xmlParseURI((const char *) tmpval);
2940 v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
2943 v->value.str = xmlStrdup(value);
2948 case XML_SCHEMAS_HEXBINARY:{
2949 const xmlChar *cur = value, *start;
2957 while IS_WSP_BLANK_CH(*cur) cur++;
2960 while (((*cur >= '0') && (*cur <= '9')) ||
2961 ((*cur >= 'A') && (*cur <= 'F')) ||
2962 ((*cur >= 'a') && (*cur <= 'f'))) {
2967 while IS_WSP_BLANK_CH(*cur) cur++;
2976 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2980 * Copy only the normalized piece.
2981 * CRITICAL TODO: Check this.
2983 cur = xmlStrndup(start, i);
2985 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
2990 total = i / 2; /* number of octets */
2992 base = (xmlChar *) cur;
2995 *base = *base - ('a' - 'A');
2999 v->value.hex.str = (xmlChar *) cur;
3000 v->value.hex.total = total;
3005 case XML_SCHEMAS_BASE64BINARY:{
3008 * Ignore all stray characters? (yes, currently)
3009 * Worry about long lines? (no, currently)
3013 * "The encoded output stream must be represented in lines of
3014 * no more than 76 characters each. All line breaks or other
3015 * characters not found in Table 1 must be ignored by decoding
3016 * software. In base64 data, characters other than those in
3017 * Table 1, line breaks, and other white space probably
3018 * indicate a transmission error, about which a warning
3019 * message or even a message rejection might be appropriate
3020 * under some circumstances." */
3021 const xmlChar *cur = value;
3023 int total, i = 0, pad = 0;
3028 for (; *cur; ++cur) {
3031 decc = _xmlSchemaBase64Decode(*cur);
3038 for (; *cur; ++cur) {
3041 decc = _xmlSchemaBase64Decode(*cur);
3049 /* rfc2045.txt: "Special processing is performed if fewer than
3050 * 24 bits are available at the end of the data being encoded.
3051 * A full encoding quantum is always completed at the end of a
3052 * body. When fewer than 24 input bits are available in an
3053 * input group, zero bits are added (on the right) to form an
3054 * integral number of 6-bit groups. Padding at the end of the
3055 * data is performed using the "=" character. Since all
3056 * base64 input is an integral number of octets, only the
3057 * following cases can arise: (1) the final quantum of
3058 * encoding input is an integral multiple of 24 bits; here,
3059 * the final unit of encoded output will be an integral
3060 * multiple ofindent: Standard input:701: Warning:old style
3061 * assignment ambiguity in "=*". Assuming "= *" 4 characters
3062 * with no "=" padding, (2) the final
3063 * quantum of encoding input is exactly 8 bits; here, the
3064 * final unit of encoded output will be two characters
3065 * followed by two "=" padding characters, or (3) the final
3066 * quantum of encoding input is exactly 16 bits; here, the
3067 * final unit of encoded output will be three characters
3068 * followed by one "=" padding character." */
3070 total = 3 * (i / 4);
3074 } else if (pad == 1) {
3079 for (decc = _xmlSchemaBase64Decode(*cur);
3080 (decc < 0) || (decc > 63);
3081 decc = _xmlSchemaBase64Decode(*cur))
3083 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
3084 /* 00111100 -> 0x3c */
3088 } else if (pad == 2) {
3093 for (decc = _xmlSchemaBase64Decode(*cur);
3094 (decc < 0) || (decc > 63);
3095 decc = _xmlSchemaBase64Decode(*cur))
3097 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
3098 /* 00110000 -> 0x30 */
3106 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
3110 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
3113 xmlSchemaTypeErrMemory(node, "allocating base64 data");
3117 v->value.base64.str = base;
3118 for (cur = value; *cur; ++cur)
3119 if (_xmlSchemaBase64Decode(*cur) >= 0) {
3124 v->value.base64.total = total;
3129 case XML_SCHEMAS_INTEGER:
3130 case XML_SCHEMAS_PINTEGER:
3131 case XML_SCHEMAS_NPINTEGER:
3132 case XML_SCHEMAS_NINTEGER:
3133 case XML_SCHEMAS_NNINTEGER:{
3134 const xmlChar *cur = value;
3135 unsigned long lo, mi, hi;
3141 while IS_WSP_BLANK_CH(*cur) cur++;
3145 } else if (*cur == '+')
3147 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3151 while IS_WSP_BLANK_CH(*cur) cur++;
3154 if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
3156 ((hi != 0) || (mi != 0) || (lo != 0)))
3158 } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
3161 if ((hi == 0) && (mi == 0) && (lo == 0))
3163 } else if (type->builtInType == XML_SCHEMAS_NINTEGER) {
3166 if ((hi == 0) && (mi == 0) && (lo == 0))
3168 } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
3170 ((hi != 0) || (mi != 0) || (lo != 0)))
3174 v = xmlSchemaNewValue(type->builtInType);
3178 v->value.decimal.lo = lo;
3179 v->value.decimal.mi = mi;
3180 v->value.decimal.hi = hi;
3181 v->value.decimal.sign = sign;
3182 v->value.decimal.frac = 0;
3183 v->value.decimal.total = ret;
3189 case XML_SCHEMAS_LONG:
3190 case XML_SCHEMAS_BYTE:
3191 case XML_SCHEMAS_SHORT:
3192 case XML_SCHEMAS_INT:{
3193 const xmlChar *cur = value;
3194 unsigned long lo, mi, hi;
3202 } else if (*cur == '+')
3204 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3209 if (type->builtInType == XML_SCHEMAS_LONG) {
3213 if (mi >= 33720368) {
3216 if ((sign == 0) && (lo > 54775807))
3218 if ((sign == 1) && (lo > 54775808))
3222 } else if (type->builtInType == XML_SCHEMAS_INT) {
3228 if ((sign == 0) && (lo > 47483647))
3230 if ((sign == 1) && (lo > 47483648))
3233 } else if (type->builtInType == XML_SCHEMAS_SHORT) {
3234 if ((mi != 0) || (hi != 0))
3236 if ((sign == 1) && (lo > 32768))
3238 if ((sign == 0) && (lo > 32767))
3240 } else if (type->builtInType == XML_SCHEMAS_BYTE) {
3241 if ((mi != 0) || (hi != 0))
3243 if ((sign == 1) && (lo > 128))
3245 if ((sign == 0) && (lo > 127))
3249 v = xmlSchemaNewValue(type->builtInType);
3251 v->value.decimal.lo = lo;
3252 v->value.decimal.mi = mi;
3253 v->value.decimal.hi = hi;
3254 v->value.decimal.sign = sign;
3255 v->value.decimal.frac = 0;
3256 v->value.decimal.total = ret;
3262 case XML_SCHEMAS_UINT:
3263 case XML_SCHEMAS_ULONG:
3264 case XML_SCHEMAS_USHORT:
3265 case XML_SCHEMAS_UBYTE:{
3266 const xmlChar *cur = value;
3267 unsigned long lo, mi, hi;
3271 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3276 if (type->builtInType == XML_SCHEMAS_ULONG) {
3280 if (mi >= 67440737) {
3287 } else if (type->builtInType == XML_SCHEMAS_UINT) {
3296 } else if (type->builtInType == XML_SCHEMAS_USHORT) {
3297 if ((mi != 0) || (hi != 0))
3301 } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
3302 if ((mi != 0) || (hi != 0))
3308 v = xmlSchemaNewValue(type->builtInType);
3310 v->value.decimal.lo = lo;
3311 v->value.decimal.mi = mi;
3312 v->value.decimal.hi = hi;
3313 v->value.decimal.sign = 0;
3314 v->value.decimal.frac = 0;
3315 v->value.decimal.total = ret;
3346 * xmlSchemaValPredefTypeNode:
3347 * @type: the predefined type
3348 * @value: the value to check
3349 * @val: the return computed value
3350 * @node: the node containing the value
3352 * Check that a value conforms to the lexical space of the predefined type.
3353 * if true a value is computed and returned in @val.
3355 * Returns 0 if this validates, a positive error code number otherwise
3356 * and -1 in case of internal or API error.
3359 xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
3360 xmlSchemaValPtr *val, xmlNodePtr node) {
3361 return(xmlSchemaValAtomicType(type, value, val, node, 0,
3362 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
3366 * xmlSchemaValPredefTypeNodeNoNorm:
3367 * @type: the predefined type
3368 * @value: the value to check
3369 * @val: the return computed value
3370 * @node: the node containing the value
3372 * Check that a value conforms to the lexical space of the predefined type.
3373 * if true a value is computed and returned in @val.
3374 * This one does apply any normalization to the value.
3376 * Returns 0 if this validates, a positive error code number otherwise
3377 * and -1 in case of internal or API error.
3380 xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
3381 xmlSchemaValPtr *val, xmlNodePtr node) {
3382 return(xmlSchemaValAtomicType(type, value, val, node, 1,
3383 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
3387 * xmlSchemaValidatePredefinedType:
3388 * @type: the predefined type
3389 * @value: the value to check
3390 * @val: the return computed value
3392 * Check that a value conforms to the lexical space of the predefined type.
3393 * if true a value is computed and returned in @val.
3395 * Returns 0 if this validates, a positive error code number otherwise
3396 * and -1 in case of internal or API error.
3399 xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
3400 xmlSchemaValPtr *val) {
3401 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
3405 * xmlSchemaCompareDecimals:
3406 * @x: a first decimal value
3407 * @y: a second decimal value
3409 * Compare 2 decimals
3411 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
3414 xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
3416 xmlSchemaValPtr swp;
3417 int order = 1, integx, integy, dlen;
3418 unsigned long hi, mi, lo;
3421 * First test: If x is -ve and not zero
3423 if ((x->value.decimal.sign) &&
3424 ((x->value.decimal.lo != 0) ||
3425 (x->value.decimal.mi != 0) ||
3426 (x->value.decimal.hi != 0))) {
3428 * Then if y is -ve and not zero reverse the compare
3430 if ((y->value.decimal.sign) &&
3431 ((y->value.decimal.lo != 0) ||
3432 (y->value.decimal.mi != 0) ||
3433 (y->value.decimal.hi != 0)))
3436 * Otherwise (y >= 0) we have the answer
3441 * If x is not -ve and y is -ve we have the answer
3443 } else if ((y->value.decimal.sign) &&
3444 ((y->value.decimal.lo != 0) ||
3445 (y->value.decimal.mi != 0) ||
3446 (y->value.decimal.hi != 0))) {
3450 * If it's not simply determined by a difference in sign,
3451 * then we need to compare the actual values of the two nums.
3452 * To do this, we start by looking at the integral parts.
3453 * If the number of integral digits differ, then we have our
3456 integx = x->value.decimal.total - x->value.decimal.frac;
3457 integy = y->value.decimal.total - y->value.decimal.frac;
3459 * NOTE: We changed the "total" for values like "0.1"
3460 * (or "-0.1" or ".1") to be 1, which was 2 previously.
3461 * Therefore the special case, when such values are
3462 * compared with 0, needs to be handled separately;
3463 * otherwise a zero would be recognized incorrectly as
3464 * greater than those values. This has the nice side effect
3465 * that we gain an overall optimized comparison with zeroes.
3466 * Note that a "0" has a "total" of 1 already.
3469 if (x->value.decimal.lo == 0) {
3472 else if (y->value.decimal.lo != 0)
3479 if (y->value.decimal.lo == 0) {
3482 else if (x->value.decimal.lo != 0)
3489 if (integx > integy)
3491 else if (integy > integx)
3495 * If the number of integral digits is the same for both numbers,
3496 * then things get a little more complicated. We need to "normalize"
3497 * the numbers in order to properly compare them. To do this, we
3498 * look at the total length of each number (length => number of
3499 * significant digits), and divide the "shorter" by 10 (decreasing
3500 * the length) until they are of equal length.
3502 dlen = x->value.decimal.total - y->value.decimal.total;
3503 if (dlen < 0) { /* y has more digits than x */
3505 hi = y->value.decimal.hi;
3506 mi = y->value.decimal.mi;
3507 lo = y->value.decimal.lo;
3510 } else { /* x has more digits than y */
3512 hi = x->value.decimal.hi;
3513 mi = x->value.decimal.mi;
3514 lo = x->value.decimal.lo;
3516 while (dlen > 8) { /* in effect, right shift by 10**8 */
3523 unsigned long rem1, rem2;
3524 rem1 = (hi % 10) * 100000000L;
3526 rem2 = (mi % 10) * 100000000L;
3527 mi = (mi + rem1) / 10;
3528 lo = (lo + rem2) / 10;
3531 if (hi > swp->value.decimal.hi) {
3533 } else if (hi == swp->value.decimal.hi) {
3534 if (mi > swp->value.decimal.mi) {
3536 } else if (mi == swp->value.decimal.mi) {
3537 if (lo > swp->value.decimal.lo) {
3539 } else if (lo == swp->value.decimal.lo) {
3540 if (x->value.decimal.total == y->value.decimal.total) {
3552 * xmlSchemaCompareDurations:
3553 * @x: a first duration value
3554 * @y: a second duration value
3556 * Compare 2 durations
3558 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3562 xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
3564 long carry, mon, day;
3567 long xmon, xday, myear, minday, maxday;
3568 static const long dayRange [2][12] = {
3569 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
3570 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
3572 if ((x == NULL) || (y == NULL))
3576 mon = x->value.dur.mon - y->value.dur.mon;
3579 sec = x->value.dur.sec - y->value.dur.sec;
3580 carry = (long)(sec / SECS_PER_DAY);
3581 sec -= ((double)carry) * SECS_PER_DAY;
3584 day = x->value.dur.day - y->value.dur.day + carry;
3602 if ((day >= 0) && (sec >= 0.0))
3608 } else if ((day <= 0) && (sec <= 0.0)) {
3621 maxday = 366 * ((myear + 3) / 4) +
3622 365 * ((myear - 1) % 4);
3623 minday = maxday - 1;
3627 minday += dayRange[0][xmon];
3628 maxday += dayRange[1][xmon];
3630 if ((maxday == minday) && (maxday == xday))
3631 return(0); /* can this really happen ? */
3642 * macros for adding date/times and durations
3644 #define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
3645 #define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
3646 #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
3647 #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
3651 * @v: the #xmlSchemaValPtr value to duplicate
3653 * Makes a copy of @v. The calling program is responsible for freeing
3654 * the returned value.
3656 * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
3658 static xmlSchemaValPtr
3659 xmlSchemaDupVal (xmlSchemaValPtr v)
3661 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
3665 memcpy(ret, v, sizeof(xmlSchemaVal));
3671 * xmlSchemaCopyValue:
3672 * @val: the precomputed value to be copied
3674 * Copies the precomputed value. This duplicates any string within.
3676 * Returns the copy or NULL if a copy for a data-type is not implemented.
3679 xmlSchemaCopyValue(xmlSchemaValPtr val)
3681 xmlSchemaValPtr ret = NULL, prev = NULL, cur;
3684 * Copy the string values.
3686 while (val != NULL) {
3687 switch (val->type) {
3688 case XML_SCHEMAS_ANYTYPE:
3689 case XML_SCHEMAS_IDREFS:
3690 case XML_SCHEMAS_ENTITIES:
3691 case XML_SCHEMAS_NMTOKENS:
3692 xmlSchemaFreeValue(ret);
3694 case XML_SCHEMAS_ANYSIMPLETYPE:
3695 case XML_SCHEMAS_STRING:
3696 case XML_SCHEMAS_NORMSTRING:
3697 case XML_SCHEMAS_TOKEN:
3698 case XML_SCHEMAS_LANGUAGE:
3699 case XML_SCHEMAS_NAME:
3700 case XML_SCHEMAS_NCNAME:
3701 case XML_SCHEMAS_ID:
3702 case XML_SCHEMAS_IDREF:
3703 case XML_SCHEMAS_ENTITY:
3704 case XML_SCHEMAS_NMTOKEN:
3705 case XML_SCHEMAS_ANYURI:
3706 cur = xmlSchemaDupVal(val);
3707 if (val->value.str != NULL)
3708 cur->value.str = xmlStrdup(BAD_CAST val->value.str);
3710 case XML_SCHEMAS_QNAME:
3711 case XML_SCHEMAS_NOTATION:
3712 cur = xmlSchemaDupVal(val);
3713 if (val->value.qname.name != NULL)
3714 cur->value.qname.name =
3715 xmlStrdup(BAD_CAST val->value.qname.name);
3716 if (val->value.qname.uri != NULL)
3717 cur->value.qname.uri =
3718 xmlStrdup(BAD_CAST val->value.qname.uri);
3720 case XML_SCHEMAS_HEXBINARY:
3721 cur = xmlSchemaDupVal(val);
3722 if (val->value.hex.str != NULL)
3723 cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
3725 case XML_SCHEMAS_BASE64BINARY:
3726 cur = xmlSchemaDupVal(val);
3727 if (val->value.base64.str != NULL)
3728 cur->value.base64.str =
3729 xmlStrdup(BAD_CAST val->value.base64.str);
3732 cur = xmlSchemaDupVal(val);
3746 * _xmlSchemaDateAdd:
3747 * @dt: an #xmlSchemaValPtr
3748 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
3750 * Compute a new date/time from @dt and @dur. This function assumes @dt
3751 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
3752 * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
3753 * @dt. The calling program is responsible for freeing the returned value.
3755 * Returns a pointer to a new #xmlSchemaVal or NULL if error.
3757 static xmlSchemaValPtr
3758 _xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
3760 xmlSchemaValPtr ret, tmp;
3761 long carry, tempdays, temp;
3762 xmlSchemaValDatePtr r, d;
3763 xmlSchemaValDurationPtr u;
3765 if ((dt == NULL) || (dur == NULL))
3768 ret = xmlSchemaNewValue(dt->type);
3772 /* make a copy so we don't alter the original value */
3773 tmp = xmlSchemaDupVal(dt);
3775 xmlSchemaFreeValue(ret);
3779 r = &(ret->value.date);
3780 d = &(tmp->value.date);
3781 u = &(dur->value.dur);
3787 /* normalize for time zone offset */
3788 u->sec -= (d->tzo * 60);
3796 carry = d->mon + u->mon;
3797 r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
3798 carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
3800 /* year (may be modified later) */
3801 r->year = d->year + carry;
3811 r->tz_flag = d->tz_flag;
3814 r->sec = d->sec + u->sec;
3815 carry = (long) FQUOTIENT((long)r->sec, 60);
3816 if (r->sec != 0.0) {
3817 r->sec = MODULO(r->sec, 60.0);
3822 r->min = (unsigned int) MODULO(carry, 60);
3823 carry = (long) FQUOTIENT(carry, 60);
3827 r->hour = (unsigned int) MODULO(carry, 24);
3828 carry = (long)FQUOTIENT(carry, 24);
3832 * Note we use tempdays because the temporary values may need more
3835 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
3836 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
3837 tempdays = MAX_DAYINMONTH(r->year, r->mon);
3838 else if (d->day < 1)
3843 tempdays += u->day + carry;
3847 long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13);
3848 long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
3852 * Coverity detected an overrun in daysInMonth
3853 * of size 12 at position 12 with index variable "((r)->mon - 1)"
3859 tempdays += MAX_DAYINMONTH(tyr, tmon);
3861 } else if (VALID_YEAR(r->year) && VALID_MONTH(r->mon) &&
3862 tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
3863 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
3868 temp = r->mon + carry;
3869 r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
3870 r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13);
3882 * adjust the date/time type to the date values
3884 if (ret->type != XML_SCHEMAS_DATETIME) {
3885 if ((r->hour) || (r->min) || (r->sec))
3886 ret->type = XML_SCHEMAS_DATETIME;
3887 else if (ret->type != XML_SCHEMAS_DATE) {
3888 if ((r->mon != 1) && (r->day != 1))
3889 ret->type = XML_SCHEMAS_DATE;
3890 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
3891 ret->type = XML_SCHEMAS_GYEARMONTH;
3895 xmlSchemaFreeValue(tmp);
3901 * xmlSchemaDateNormalize:
3902 * @dt: an #xmlSchemaValPtr of a date/time type value.
3903 * @offset: number of seconds to adjust @dt by.
3905 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3906 * the return value is a time-zone offset is present on @dt.
3908 * Returns a normalized copy of @dt or NULL if error.
3910 static xmlSchemaValPtr
3911 xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
3913 xmlSchemaValPtr dur, ret;
3918 if (((dt->type != XML_SCHEMAS_TIME) &&
3919 (dt->type != XML_SCHEMAS_DATETIME) &&
3920 (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
3921 return xmlSchemaDupVal(dt);
3923 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
3927 dur->value.date.sec -= offset;
3929 ret = _xmlSchemaDateAdd(dt, dur);
3933 xmlSchemaFreeValue(dur);
3935 /* ret->value.date.tzo = 0; */
3940 * _xmlSchemaDateCastYMToDays:
3941 * @dt: an #xmlSchemaValPtr
3943 * Convert mon and year of @dt to total number of days. Take the
3944 * number of years since (or before) 1 AD and add the number of leap
3945 * years. This is a function because negative
3946 * years must be handled a little differently and there is no zero year.
3948 * Returns number of days.
3951 _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
3956 mon = dt->value.date.mon;
3957 if (mon <= 0) mon = 1; /* normalization */
3959 if (dt->value.date.year <= 0)
3960 ret = (dt->value.date.year * 365) +
3961 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
3962 ((dt->value.date.year+1)/400)) +
3963 DAY_IN_YEAR(0, mon, dt->value.date.year);
3965 ret = ((dt->value.date.year-1) * 365) +
3966 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
3967 ((dt->value.date.year-1)/400)) +
3968 DAY_IN_YEAR(0, mon, dt->value.date.year);
3975 * @dt: an #xmlSchemaValPtr
3977 * Calculates the number of seconds in the time portion of @dt.
3981 #define TIME_TO_NUMBER(dt) \
3982 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
3983 (dt->value.date.min * SECS_PER_MIN) + \
3984 (dt->value.date.tzo * SECS_PER_MIN)) + \
3988 * xmlSchemaCompareDates:
3989 * @x: a first date/time value
3990 * @y: a second date/time value
3992 * Compare 2 date/times
3994 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3998 xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
4000 unsigned char xmask, ymask, xor_mask, and_mask;
4001 xmlSchemaValPtr p1, p2, q1, q2;
4002 long p1d, p2d, q1d, q2d;
4004 if ((x == NULL) || (y == NULL))
4007 if (x->value.date.tz_flag) {
4009 if (!y->value.date.tz_flag) {
4010 p1 = xmlSchemaDateNormalize(x, 0);
4011 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4012 /* normalize y + 14:00 */
4013 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
4015 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4017 xmlSchemaFreeValue(p1);
4018 xmlSchemaFreeValue(q1);
4020 } else if (p1d == q1d) {
4023 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4025 xmlSchemaFreeValue(p1);
4026 xmlSchemaFreeValue(q1);
4030 /* normalize y - 14:00 */
4031 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
4032 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
4035 else if (p1d == q2d) {
4036 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
4040 ret = 2; /* indeterminate */
4042 xmlSchemaFreeValue(p1);
4043 xmlSchemaFreeValue(q1);
4044 xmlSchemaFreeValue(q2);
4049 xmlSchemaFreeValue(p1);
4050 xmlSchemaFreeValue(q1);
4053 } else if (y->value.date.tz_flag) {
4054 q1 = xmlSchemaDateNormalize(y, 0);
4055 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4057 /* normalize x - 14:00 */
4058 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
4059 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4062 xmlSchemaFreeValue(p1);
4063 xmlSchemaFreeValue(q1);
4065 } else if (p1d == q1d) {
4068 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4070 xmlSchemaFreeValue(p1);
4071 xmlSchemaFreeValue(q1);
4075 /* normalize x + 14:00 */
4076 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
4077 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
4081 } else if (p2d == q1d) {
4082 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
4086 ret = 2; /* indeterminate */
4088 xmlSchemaFreeValue(p1);
4089 xmlSchemaFreeValue(q1);
4090 xmlSchemaFreeValue(p2);
4095 xmlSchemaFreeValue(p1);
4096 xmlSchemaFreeValue(q1);
4101 * if the same type then calculate the difference
4103 if (x->type == y->type) {
4105 q1 = xmlSchemaDateNormalize(y, 0);
4106 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4108 p1 = xmlSchemaDateNormalize(x, 0);
4109 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4113 } else if (p1d > q1d) {
4118 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4125 xmlSchemaFreeValue(p1);
4126 xmlSchemaFreeValue(q1);
4131 case XML_SCHEMAS_DATETIME:
4134 case XML_SCHEMAS_DATE:
4137 case XML_SCHEMAS_GYEAR:
4140 case XML_SCHEMAS_GMONTH:
4143 case XML_SCHEMAS_GDAY:
4146 case XML_SCHEMAS_GYEARMONTH:
4149 case XML_SCHEMAS_GMONTHDAY:
4152 case XML_SCHEMAS_TIME:
4161 case XML_SCHEMAS_DATETIME:
4164 case XML_SCHEMAS_DATE:
4167 case XML_SCHEMAS_GYEAR:
4170 case XML_SCHEMAS_GMONTH:
4173 case XML_SCHEMAS_GDAY:
4176 case XML_SCHEMAS_GYEARMONTH:
4179 case XML_SCHEMAS_GMONTHDAY:
4182 case XML_SCHEMAS_TIME:
4190 xor_mask = xmask ^ ymask; /* mark type differences */
4191 and_mask = xmask & ymask; /* mark field specification */
4195 return 2; /* indeterminate */
4196 else if (and_mask & 1) {
4197 if (x->value.date.year < y->value.date.year)
4199 else if (x->value.date.year > y->value.date.year)
4205 return 2; /* indeterminate */
4206 else if (and_mask & 2) {
4207 if (x->value.date.mon < y->value.date.mon)
4209 else if (x->value.date.mon > y->value.date.mon)
4215 return 2; /* indeterminate */
4216 else if (and_mask & 4) {
4217 if (x->value.date.day < y->value.date.day)
4219 else if (x->value.date.day > y->value.date.day)
4225 return 2; /* indeterminate */
4226 else if (and_mask & 8) {
4227 if (x->value.date.hour < y->value.date.hour)
4229 else if (x->value.date.hour > y->value.date.hour)
4231 else if (x->value.date.min < y->value.date.min)
4233 else if (x->value.date.min > y->value.date.min)
4235 else if (x->value.date.sec < y->value.date.sec)
4237 else if (x->value.date.sec > y->value.date.sec)
4245 * xmlSchemaComparePreserveReplaceStrings:
4246 * @x: a first string value
4247 * @y: a second string value
4248 * @invert: inverts the result if x < y or x > y.
4250 * Compare 2 string for their normalized values.
4251 * @x is a string with whitespace of "preserve", @y is
4252 * a string with a whitespace of "replace". I.e. @x could
4253 * be an "xsd:string" and @y an "xsd:normalizedString".
4255 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4259 xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
4265 while ((*x != 0) && (*y != 0)) {
4266 if (IS_WSP_REPLACE_CH(*y)) {
4267 if (! IS_WSP_SPACE_CH(*x)) {
4268 if ((*x - 0x20) < 0) {
4314 * xmlSchemaComparePreserveCollapseStrings:
4315 * @x: a first string value
4316 * @y: a second string value
4318 * Compare 2 string for their normalized values.
4319 * @x is a string with whitespace of "preserve", @y is
4320 * a string with a whitespace of "collapse". I.e. @x could
4321 * be an "xsd:string" and @y an "xsd:normalizedString".
4323 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4327 xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
4334 * Skip leading blank chars of the collapsed string.
4336 while IS_WSP_BLANK_CH(*y)
4339 while ((*x != 0) && (*y != 0)) {
4340 if IS_WSP_BLANK_CH(*y) {
4341 if (! IS_WSP_SPACE_CH(*x)) {
4343 * The yv character would have been replaced to 0x20.
4345 if ((*x - 0x20) < 0) {
4360 * Skip contiguous blank chars of the collapsed string.
4362 while IS_WSP_BLANK_CH(*y)
4388 * Skip trailing blank chars of the collapsed string.
4390 while IS_WSP_BLANK_CH(*y)
4403 * xmlSchemaComparePreserveCollapseStrings:
4404 * @x: a first string value
4405 * @y: a second string value
4407 * Compare 2 string for their normalized values.
4408 * @x is a string with whitespace of "preserve", @y is
4409 * a string with a whitespace of "collapse". I.e. @x could
4410 * be an "xsd:string" and @y an "xsd:normalizedString".
4412 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4416 xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
4423 * Skip leading blank chars of the collapsed string.
4425 while IS_WSP_BLANK_CH(*y)
4428 while ((*x != 0) && (*y != 0)) {
4429 if IS_WSP_BLANK_CH(*y) {
4430 if (! IS_WSP_BLANK_CH(*x)) {
4432 * The yv character would have been replaced to 0x20.
4434 if ((*x - 0x20) < 0) {
4449 * Skip contiguous blank chars of the collapsed string.
4451 while IS_WSP_BLANK_CH(*y)
4454 if IS_WSP_BLANK_CH(*x) {
4456 * The xv character would have been replaced to 0x20.
4458 if ((0x20 - *y) < 0) {
4485 * Skip trailing blank chars of the collapsed string.
4487 while IS_WSP_BLANK_CH(*y)
4501 * xmlSchemaCompareReplacedStrings:
4502 * @x: a first string value
4503 * @y: a second string value
4505 * Compare 2 string for their normalized values.
4507 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4511 xmlSchemaCompareReplacedStrings(const xmlChar *x,
4516 while ((*x != 0) && (*y != 0)) {
4517 if IS_WSP_BLANK_CH(*y) {
4518 if (! IS_WSP_BLANK_CH(*x)) {
4519 if ((*x - 0x20) < 0)
4525 if IS_WSP_BLANK_CH(*x) {
4526 if ((0x20 - *y) < 0)
4548 * xmlSchemaCompareNormStrings:
4549 * @x: a first string value
4550 * @y: a second string value
4552 * Compare 2 string for their normalized values.
4554 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4558 xmlSchemaCompareNormStrings(const xmlChar *x,
4562 while (IS_BLANK_CH(*x)) x++;
4563 while (IS_BLANK_CH(*y)) y++;
4564 while ((*x != 0) && (*y != 0)) {
4565 if (IS_BLANK_CH(*x)) {
4566 if (!IS_BLANK_CH(*y)) {
4570 while (IS_BLANK_CH(*x)) x++;
4571 while (IS_BLANK_CH(*y)) y++;
4581 while (IS_BLANK_CH(*x)) x++;
4586 while (IS_BLANK_CH(*y)) y++;
4594 * xmlSchemaCompareFloats:
4595 * @x: a first float or double value
4596 * @y: a second float or double value
4600 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4604 xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4607 if ((x == NULL) || (y == NULL))
4611 * Cast everything to doubles.
4613 if (x->type == XML_SCHEMAS_DOUBLE)
4615 else if (x->type == XML_SCHEMAS_FLOAT)
4620 if (y->type == XML_SCHEMAS_DOUBLE)
4622 else if (y->type == XML_SCHEMAS_FLOAT)
4628 * Check for special cases.
4630 if (xmlXPathIsNaN(d1)) {
4631 if (xmlXPathIsNaN(d2))
4635 if (xmlXPathIsNaN(d2))
4637 if (d1 == xmlXPathPINF) {
4638 if (d2 == xmlXPathPINF)
4642 if (d2 == xmlXPathPINF)
4644 if (d1 == xmlXPathNINF) {
4645 if (d2 == xmlXPathNINF)
4649 if (d2 == xmlXPathNINF)
4653 * basic tests, the last one we should have equality, but
4654 * portability is more important than speed and handling
4655 * NaN or Inf in a portable way is always a challenge, so ...
4667 * xmlSchemaCompareValues:
4669 * @xvalue: the first value as a string (optional)
4670 * @xwtsp: the whitespace type
4671 * @y: a second value
4672 * @xvalue: the second value as a string (optional)
4673 * @ywtsp: the whitespace type
4677 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4678 * comparable and -2 in case of error
4681 xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
4683 const xmlChar *xvalue,
4684 xmlSchemaWhitespaceValueType xws,
4685 xmlSchemaValType ytype,
4687 const xmlChar *yvalue,
4688 xmlSchemaWhitespaceValueType yws)
4691 case XML_SCHEMAS_UNKNOWN:
4692 case XML_SCHEMAS_ANYTYPE:
4694 case XML_SCHEMAS_INTEGER:
4695 case XML_SCHEMAS_NPINTEGER:
4696 case XML_SCHEMAS_NINTEGER:
4697 case XML_SCHEMAS_NNINTEGER:
4698 case XML_SCHEMAS_PINTEGER:
4699 case XML_SCHEMAS_INT:
4700 case XML_SCHEMAS_UINT:
4701 case XML_SCHEMAS_LONG:
4702 case XML_SCHEMAS_ULONG:
4703 case XML_SCHEMAS_SHORT:
4704 case XML_SCHEMAS_USHORT:
4705 case XML_SCHEMAS_BYTE:
4706 case XML_SCHEMAS_UBYTE:
4707 case XML_SCHEMAS_DECIMAL:
4708 if ((x == NULL) || (y == NULL))
4711 return(xmlSchemaCompareDecimals(x, y));
4712 if ((ytype == XML_SCHEMAS_DECIMAL) ||
4713 (ytype == XML_SCHEMAS_INTEGER) ||
4714 (ytype == XML_SCHEMAS_NPINTEGER) ||
4715 (ytype == XML_SCHEMAS_NINTEGER) ||
4716 (ytype == XML_SCHEMAS_NNINTEGER) ||
4717 (ytype == XML_SCHEMAS_PINTEGER) ||
4718 (ytype == XML_SCHEMAS_INT) ||
4719 (ytype == XML_SCHEMAS_UINT) ||
4720 (ytype == XML_SCHEMAS_LONG) ||
4721 (ytype == XML_SCHEMAS_ULONG) ||
4722 (ytype == XML_SCHEMAS_SHORT) ||
4723 (ytype == XML_SCHEMAS_USHORT) ||
4724 (ytype == XML_SCHEMAS_BYTE) ||
4725 (ytype == XML_SCHEMAS_UBYTE))
4726 return(xmlSchemaCompareDecimals(x, y));
4728 case XML_SCHEMAS_DURATION:
4729 if ((x == NULL) || (y == NULL))
4731 if (ytype == XML_SCHEMAS_DURATION)
4732 return(xmlSchemaCompareDurations(x, y));
4734 case XML_SCHEMAS_TIME:
4735 case XML_SCHEMAS_GDAY:
4736 case XML_SCHEMAS_GMONTH:
4737 case XML_SCHEMAS_GMONTHDAY:
4738 case XML_SCHEMAS_GYEAR:
4739 case XML_SCHEMAS_GYEARMONTH:
4740 case XML_SCHEMAS_DATE:
4741 case XML_SCHEMAS_DATETIME:
4742 if ((x == NULL) || (y == NULL))
4744 if ((ytype == XML_SCHEMAS_DATETIME) ||
4745 (ytype == XML_SCHEMAS_TIME) ||
4746 (ytype == XML_SCHEMAS_GDAY) ||
4747 (ytype == XML_SCHEMAS_GMONTH) ||
4748 (ytype == XML_SCHEMAS_GMONTHDAY) ||
4749 (ytype == XML_SCHEMAS_GYEAR) ||
4750 (ytype == XML_SCHEMAS_DATE) ||
4751 (ytype == XML_SCHEMAS_GYEARMONTH))
4752 return (xmlSchemaCompareDates(x, y));
4755 * Note that we will support comparison of string types against
4756 * anySimpleType as well.
4758 case XML_SCHEMAS_ANYSIMPLETYPE:
4759 case XML_SCHEMAS_STRING:
4760 case XML_SCHEMAS_NORMSTRING:
4761 case XML_SCHEMAS_TOKEN:
4762 case XML_SCHEMAS_LANGUAGE:
4763 case XML_SCHEMAS_NMTOKEN:
4764 case XML_SCHEMAS_NAME:
4765 case XML_SCHEMAS_NCNAME:
4766 case XML_SCHEMAS_ID:
4767 case XML_SCHEMAS_IDREF:
4768 case XML_SCHEMAS_ENTITY:
4769 case XML_SCHEMAS_ANYURI:
4771 const xmlChar *xv, *yv;
4782 * TODO: Compare those against QName.
4784 if (ytype == XML_SCHEMAS_QNAME) {
4790 if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
4791 (ytype == XML_SCHEMAS_STRING) ||
4792 (ytype == XML_SCHEMAS_NORMSTRING) ||
4793 (ytype == XML_SCHEMAS_TOKEN) ||
4794 (ytype == XML_SCHEMAS_LANGUAGE) ||
4795 (ytype == XML_SCHEMAS_NMTOKEN) ||
4796 (ytype == XML_SCHEMAS_NAME) ||
4797 (ytype == XML_SCHEMAS_NCNAME) ||
4798 (ytype == XML_SCHEMAS_ID) ||
4799 (ytype == XML_SCHEMAS_IDREF) ||
4800 (ytype == XML_SCHEMAS_ENTITY) ||
4801 (ytype == XML_SCHEMAS_ANYURI)) {
4803 if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4805 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4806 /* TODO: What about x < y or x > y. */
4807 if (xmlStrEqual(xv, yv))
4811 } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
4812 return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
4813 else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
4814 return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
4816 } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
4818 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
4819 return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
4820 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
4821 return (xmlSchemaCompareReplacedStrings(xv, yv));
4822 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
4823 return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
4825 } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
4827 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
4828 return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
4829 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
4830 return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
4831 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
4832 return (xmlSchemaCompareNormStrings(xv, yv));
4839 case XML_SCHEMAS_QNAME:
4840 case XML_SCHEMAS_NOTATION:
4841 if ((x == NULL) || (y == NULL))
4843 if ((ytype == XML_SCHEMAS_QNAME) ||
4844 (ytype == XML_SCHEMAS_NOTATION)) {
4845 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
4846 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
4851 case XML_SCHEMAS_FLOAT:
4852 case XML_SCHEMAS_DOUBLE:
4853 if ((x == NULL) || (y == NULL))
4855 if ((ytype == XML_SCHEMAS_FLOAT) ||
4856 (ytype == XML_SCHEMAS_DOUBLE))
4857 return (xmlSchemaCompareFloats(x, y));
4859 case XML_SCHEMAS_BOOLEAN:
4860 if ((x == NULL) || (y == NULL))
4862 if (ytype == XML_SCHEMAS_BOOLEAN) {
4863 if (x->value.b == y->value.b)
4865 if (x->value.b == 0)
4870 case XML_SCHEMAS_HEXBINARY:
4871 if ((x == NULL) || (y == NULL))
4873 if (ytype == XML_SCHEMAS_HEXBINARY) {
4874 if (x->value.hex.total == y->value.hex.total) {
4875 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
4881 else if (x->value.hex.total > y->value.hex.total)
4887 case XML_SCHEMAS_BASE64BINARY:
4888 if ((x == NULL) || (y == NULL))
4890 if (ytype == XML_SCHEMAS_BASE64BINARY) {
4891 if (x->value.base64.total == y->value.base64.total) {
4892 int ret = xmlStrcmp(x->value.base64.str,
4893 y->value.base64.str);
4901 else if (x->value.base64.total > y->value.base64.total)
4907 case XML_SCHEMAS_IDREFS:
4908 case XML_SCHEMAS_ENTITIES:
4909 case XML_SCHEMAS_NMTOKENS:
4917 * xmlSchemaCompareValues:
4919 * @y: a second value
4923 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4927 xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4928 xmlSchemaWhitespaceValueType xws, yws;
4930 if ((x == NULL) || (y == NULL))
4932 if (x->type == XML_SCHEMAS_STRING)
4933 xws = XML_SCHEMA_WHITESPACE_PRESERVE;
4934 else if (x->type == XML_SCHEMAS_NORMSTRING)
4935 xws = XML_SCHEMA_WHITESPACE_REPLACE;
4937 xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4939 if (y->type == XML_SCHEMAS_STRING)
4940 yws = XML_SCHEMA_WHITESPACE_PRESERVE;
4941 else if (y->type == XML_SCHEMAS_NORMSTRING)
4942 yws = XML_SCHEMA_WHITESPACE_REPLACE;
4944 yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4946 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4951 * xmlSchemaCompareValuesWhtsp:
4953 * @xws: the whitespace value of x
4954 * @y: a second value
4955 * @yws: the whitespace value of y
4959 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4963 xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
4964 xmlSchemaWhitespaceValueType xws,
4966 xmlSchemaWhitespaceValueType yws)
4968 if ((x == NULL) || (y == NULL))
4970 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4975 * xmlSchemaCompareValuesWhtspExt:
4977 * @xws: the whitespace value of x
4978 * @y: a second value
4979 * @yws: the whitespace value of y
4983 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4987 xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
4989 const xmlChar *xvalue,
4990 xmlSchemaWhitespaceValueType xws,
4991 xmlSchemaValType ytype,
4993 const xmlChar *yvalue,
4994 xmlSchemaWhitespaceValueType yws)
4996 return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
5004 * Computes the UTF8 length of the normalized value of the string
5006 * Returns the length or -1 in case of error.
5009 xmlSchemaNormLen(const xmlChar *value) {
5016 while (IS_BLANK_CH(*utf)) utf++;
5018 if (utf[0] & 0x80) {
5019 if ((utf[1] & 0xc0) != 0x80)
5021 if ((utf[0] & 0xe0) == 0xe0) {
5022 if ((utf[2] & 0xc0) != 0x80)
5024 if ((utf[0] & 0xf0) == 0xf0) {
5025 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
5034 } else if (IS_BLANK_CH(*utf)) {
5035 while (IS_BLANK_CH(*utf)) utf++;
5047 * xmlSchemaGetFacetValueAsULong:
5048 * @facet: an schemas type facet
5050 * Extract the value of a facet
5052 * Returns the value as a long
5055 xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
5058 * TODO: Check if this is a decimal.
5062 return ((unsigned long) facet->val->value.decimal.lo);
5066 * xmlSchemaValidateListSimpleTypeFacet:
5067 * @facet: the facet to check
5068 * @value: the lexical repr of the value to validate
5069 * @actualLen: the number of list items
5070 * @expectedLen: the resulting expected number of list items
5072 * Checks the value of a list simple type against a facet.
5074 * Returns 0 if the value is valid, a positive error code
5075 * number otherwise and -1 in case of an internal error.
5078 xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
5079 const xmlChar *value,
5080 unsigned long actualLen,
5081 unsigned long *expectedLen)
5086 * TODO: Check if this will work with large numbers.
5087 * (compare value.decimal.mi and value.decimal.hi as well?).
5089 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5090 if (actualLen != facet->val->value.decimal.lo) {
5091 if (expectedLen != NULL)
5092 *expectedLen = facet->val->value.decimal.lo;
5093 return (XML_SCHEMAV_CVC_LENGTH_VALID);
5095 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5096 if (actualLen < facet->val->value.decimal.lo) {
5097 if (expectedLen != NULL)
5098 *expectedLen = facet->val->value.decimal.lo;
5099 return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
5101 } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
5102 if (actualLen > facet->val->value.decimal.lo) {
5103 if (expectedLen != NULL)
5104 *expectedLen = facet->val->value.decimal.lo;
5105 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5109 * NOTE: That we can pass NULL as xmlSchemaValPtr to
5110 * xmlSchemaValidateFacet, since the remaining facet types
5111 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
5113 return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
5118 * xmlSchemaValidateLengthFacet:
5119 * @type: the built-in type
5120 * @facet: the facet to check
5121 * @value: the lexical repr. of the value to be validated
5122 * @val: the precomputed value
5123 * @ws: the whitespace type of the value
5124 * @length: the actual length of the value
5126 * Checka a value against a "length", "minLength" and "maxLength"
5127 * facet; sets @length to the computed length of @value.
5129 * Returns 0 if the value is valid, a positive error code
5130 * otherwise and -1 in case of an internal or API error.
5133 xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
5134 xmlSchemaValType valType,
5135 const xmlChar *value,
5136 xmlSchemaValPtr val,
5137 unsigned long *length,
5138 xmlSchemaWhitespaceValueType ws)
5140 unsigned int len = 0;
5142 if ((length == NULL) || (facet == NULL))
5145 if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
5146 (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
5147 (facet->type != XML_SCHEMA_FACET_MINLENGTH))
5151 * TODO: length, maxLength and minLength must be of type
5152 * nonNegativeInteger only. Check if decimal is used somehow.
5154 if ((facet->val == NULL) ||
5155 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5156 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5157 (facet->val->value.decimal.frac != 0)) {
5160 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
5161 len = val->value.hex.total;
5162 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5163 len = val->value.base64.total;
5166 case XML_SCHEMAS_STRING:
5167 case XML_SCHEMAS_NORMSTRING:
5168 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5170 * This is to ensure API compatibility with the old
5171 * xmlSchemaValidateLengthFacet(). Anyway, this was and
5172 * is not the correct handling.
5173 * TODO: Get rid of this case somehow.
5175 if (valType == XML_SCHEMAS_STRING)
5176 len = xmlUTF8Strlen(value);
5178 len = xmlSchemaNormLen(value);
5179 } else if (value != NULL) {
5180 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5181 len = xmlSchemaNormLen(value);
5184 * Should be OK for "preserve" as well.
5186 len = xmlUTF8Strlen(value);
5189 case XML_SCHEMAS_IDREF:
5190 case XML_SCHEMAS_TOKEN:
5191 case XML_SCHEMAS_LANGUAGE:
5192 case XML_SCHEMAS_NMTOKEN:
5193 case XML_SCHEMAS_NAME:
5194 case XML_SCHEMAS_NCNAME:
5195 case XML_SCHEMAS_ID:
5197 * FIXME: What exactly to do with anyURI?
5199 case XML_SCHEMAS_ANYURI:
5201 len = xmlSchemaNormLen(value);
5203 case XML_SCHEMAS_QNAME:
5204 case XML_SCHEMAS_NOTATION:
5206 * For QName and NOTATION, those facets are
5207 * deprecated and should be ignored.
5214 *length = (unsigned long) len;
5216 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
5218 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5219 if (len != facet->val->value.decimal.lo)
5220 return(XML_SCHEMAV_CVC_LENGTH_VALID);
5221 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5222 if (len < facet->val->value.decimal.lo)
5223 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
5225 if (len > facet->val->value.decimal.lo)
5226 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5233 * xmlSchemaValidateLengthFacet:
5234 * @type: the built-in type
5235 * @facet: the facet to check
5236 * @value: the lexical repr. of the value to be validated
5237 * @val: the precomputed value
5238 * @length: the actual length of the value
5240 * Checka a value against a "length", "minLength" and "maxLength"
5241 * facet; sets @length to the computed length of @value.
5243 * Returns 0 if the value is valid, a positive error code
5244 * otherwise and -1 in case of an internal or API error.
5247 xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
5248 xmlSchemaFacetPtr facet,
5249 const xmlChar *value,
5250 xmlSchemaValPtr val,
5251 unsigned long *length)
5255 return (xmlSchemaValidateLengthFacetInternal(facet,
5256 type->builtInType, value, val, length,
5257 XML_SCHEMA_WHITESPACE_UNKNOWN));
5261 * xmlSchemaValidateLengthFacetWhtsp:
5262 * @facet: the facet to check
5263 * @valType: the built-in type
5264 * @value: the lexical repr. of the value to be validated
5265 * @val: the precomputed value
5266 * @ws: the whitespace type of the value
5267 * @length: the actual length of the value
5269 * Checka a value against a "length", "minLength" and "maxLength"
5270 * facet; sets @length to the computed length of @value.
5272 * Returns 0 if the value is valid, a positive error code
5273 * otherwise and -1 in case of an internal or API error.
5276 xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
5277 xmlSchemaValType valType,
5278 const xmlChar *value,
5279 xmlSchemaValPtr val,
5280 unsigned long *length,
5281 xmlSchemaWhitespaceValueType ws)
5283 return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
5288 * xmlSchemaValidateFacetInternal:
5289 * @facet: the facet to check
5290 * @fws: the whitespace type of the facet's value
5291 * @valType: the built-in type of the value
5292 * @value: the lexical repr of the value to validate
5293 * @val: the precomputed value
5294 * @ws: the whitespace type of the value
5296 * Check a value against a facet condition
5298 * Returns 0 if the element is schemas valid, a positive error code
5299 * number otherwise and -1 in case of internal or API error.
5302 xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
5303 xmlSchemaWhitespaceValueType fws,
5304 xmlSchemaValType valType,
5305 const xmlChar *value,
5306 xmlSchemaValPtr val,
5307 xmlSchemaWhitespaceValueType ws)
5315 switch (facet->type) {
5316 case XML_SCHEMA_FACET_PATTERN:
5318 * NOTE that for patterns, the @value needs to be the normalized
5319 * value, *not* the lexical initial value or the canonical value.
5324 * If string-derived type, regexp must be tested on the value space of
5326 * See https://www.w3.org/TR/xmlschema-2/#rf-pattern
5328 stringType = val && ((val->type >= XML_SCHEMAS_STRING && val->type <= XML_SCHEMAS_NORMSTRING)
5329 || (val->type >= XML_SCHEMAS_TOKEN && val->type <= XML_SCHEMAS_NCNAME));
5330 ret = xmlRegexpExec(facet->regexp,
5331 (stringType && val->value.str) ? val->value.str : value);
5335 return(XML_SCHEMAV_CVC_PATTERN_VALID);
5337 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
5338 ret = xmlSchemaCompareValues(val, facet->val);
5343 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
5344 case XML_SCHEMA_FACET_MAXINCLUSIVE:
5345 ret = xmlSchemaCompareValues(val, facet->val);
5348 if ((ret == -1) || (ret == 0))
5350 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
5351 case XML_SCHEMA_FACET_MINEXCLUSIVE:
5352 ret = xmlSchemaCompareValues(val, facet->val);
5357 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
5358 case XML_SCHEMA_FACET_MININCLUSIVE:
5359 ret = xmlSchemaCompareValues(val, facet->val);
5362 if ((ret == 1) || (ret == 0))
5364 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
5365 case XML_SCHEMA_FACET_WHITESPACE:
5366 /* TODO whitespaces */
5368 * NOTE: Whitespace should be handled to normalize
5369 * the value to be validated against a the facets;
5370 * not to normalize the value in-between.
5373 case XML_SCHEMA_FACET_ENUMERATION:
5374 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5376 * This is to ensure API compatibility with the old
5377 * xmlSchemaValidateFacet().
5378 * TODO: Get rid of this case.
5380 if ((facet->value != NULL) &&
5381 (xmlStrEqual(facet->value, value)))
5384 ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
5385 facet->val, facet->value, fws, valType, val,
5392 return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
5393 case XML_SCHEMA_FACET_LENGTH:
5395 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5396 * then any {value} is facet-valid."
5398 if ((valType == XML_SCHEMAS_QNAME) ||
5399 (valType == XML_SCHEMAS_NOTATION))
5401 /* No break on purpose. */
5402 case XML_SCHEMA_FACET_MAXLENGTH:
5403 case XML_SCHEMA_FACET_MINLENGTH: {
5404 unsigned int len = 0;
5406 if ((valType == XML_SCHEMAS_QNAME) ||
5407 (valType == XML_SCHEMAS_NOTATION))
5410 * TODO: length, maxLength and minLength must be of type
5411 * nonNegativeInteger only. Check if decimal is used somehow.
5413 if ((facet->val == NULL) ||
5414 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5415 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5416 (facet->val->value.decimal.frac != 0)) {
5419 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
5420 len = val->value.hex.total;
5421 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5422 len = val->value.base64.total;
5425 case XML_SCHEMAS_STRING:
5426 case XML_SCHEMAS_NORMSTRING:
5427 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5429 * This is to ensure API compatibility with the old
5430 * xmlSchemaValidateFacet(). Anyway, this was and
5431 * is not the correct handling.
5432 * TODO: Get rid of this case somehow.
5434 if (valType == XML_SCHEMAS_STRING)
5435 len = xmlUTF8Strlen(value);
5437 len = xmlSchemaNormLen(value);
5438 } else if (value != NULL) {
5439 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5440 len = xmlSchemaNormLen(value);
5443 * Should be OK for "preserve" as well.
5445 len = xmlUTF8Strlen(value);
5448 case XML_SCHEMAS_IDREF:
5449 case XML_SCHEMAS_TOKEN:
5450 case XML_SCHEMAS_LANGUAGE:
5451 case XML_SCHEMAS_NMTOKEN:
5452 case XML_SCHEMAS_NAME:
5453 case XML_SCHEMAS_NCNAME:
5454 case XML_SCHEMAS_ID:
5455 case XML_SCHEMAS_ANYURI:
5457 len = xmlSchemaNormLen(value);
5463 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5464 if (len != facet->val->value.decimal.lo)
5465 return(XML_SCHEMAV_CVC_LENGTH_VALID);
5466 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5467 if (len < facet->val->value.decimal.lo)
5468 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
5470 if (len > facet->val->value.decimal.lo)
5471 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5475 case XML_SCHEMA_FACET_TOTALDIGITS:
5476 case XML_SCHEMA_FACET_FRACTIONDIGITS:
5478 if ((facet->val == NULL) ||
5479 ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
5480 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5481 (facet->val->value.decimal.frac != 0)) {
5484 if ((val == NULL) ||
5485 ((val->type != XML_SCHEMAS_DECIMAL) &&
5486 (val->type != XML_SCHEMAS_INTEGER) &&
5487 (val->type != XML_SCHEMAS_NPINTEGER) &&
5488 (val->type != XML_SCHEMAS_NINTEGER) &&
5489 (val->type != XML_SCHEMAS_NNINTEGER) &&
5490 (val->type != XML_SCHEMAS_PINTEGER) &&
5491 (val->type != XML_SCHEMAS_INT) &&
5492 (val->type != XML_SCHEMAS_UINT) &&
5493 (val->type != XML_SCHEMAS_LONG) &&
5494 (val->type != XML_SCHEMAS_ULONG) &&
5495 (val->type != XML_SCHEMAS_SHORT) &&
5496 (val->type != XML_SCHEMAS_USHORT) &&
5497 (val->type != XML_SCHEMAS_BYTE) &&
5498 (val->type != XML_SCHEMAS_UBYTE))) {
5501 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
5502 if (val->value.decimal.total > facet->val->value.decimal.lo)
5503 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
5505 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
5506 if (val->value.decimal.frac > facet->val->value.decimal.lo)
5507 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
5518 * xmlSchemaValidateFacet:
5519 * @base: the base type
5520 * @facet: the facet to check
5521 * @value: the lexical repr of the value to validate
5522 * @val: the precomputed value
5524 * Check a value against a facet condition
5526 * Returns 0 if the element is schemas valid, a positive error code
5527 * number otherwise and -1 in case of internal or API error.
5530 xmlSchemaValidateFacet(xmlSchemaTypePtr base,
5531 xmlSchemaFacetPtr facet,
5532 const xmlChar *value,
5533 xmlSchemaValPtr val)
5536 * This tries to ensure API compatibility regarding the old
5537 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5538 * xmlSchemaValidateFacetWhtsp().
5541 return(xmlSchemaValidateFacetInternal(facet,
5542 XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
5543 XML_SCHEMA_WHITESPACE_UNKNOWN));
5544 else if (base != NULL)
5545 return(xmlSchemaValidateFacetInternal(facet,
5546 XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
5547 XML_SCHEMA_WHITESPACE_UNKNOWN));
5552 * xmlSchemaValidateFacetWhtsp:
5553 * @facet: the facet to check
5554 * @fws: the whitespace type of the facet's value
5555 * @valType: the built-in type of the value
5556 * @value: the lexical (or normalized for pattern) repr of the value to validate
5557 * @val: the precomputed value
5558 * @ws: the whitespace type of the value
5560 * Check a value against a facet condition. This takes value normalization
5561 * according to the specified whitespace types into account.
5562 * Note that @value needs to be the *normalized* value if the facet
5563 * is of type "pattern".
5565 * Returns 0 if the element is schemas valid, a positive error code
5566 * number otherwise and -1 in case of internal or API error.
5569 xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
5570 xmlSchemaWhitespaceValueType fws,
5571 xmlSchemaValType valType,
5572 const xmlChar *value,
5573 xmlSchemaValPtr val,
5574 xmlSchemaWhitespaceValueType ws)
5576 return(xmlSchemaValidateFacetInternal(facet, fws, valType,
5585 #define DBL_EPSILON 1E-9
5588 #define INTEGER_DIGITS DBL_DIG
5589 #define FRACTION_DIGITS (DBL_DIG + 1)
5590 #define EXPONENT_DIGITS (3 + 2)
5593 * xmlXPathFormatNumber:
5594 * @number: number to format
5595 * @buffer: output buffer
5596 * @buffersize: size of output buffer
5598 * Convert the number into a string representation.
5601 xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
5603 switch (xmlXPathIsInf(number)) {
5605 if (buffersize > (int)sizeof("INF"))
5606 snprintf(buffer, buffersize, "INF");
5609 if (buffersize > (int)sizeof("-INF"))
5610 snprintf(buffer, buffersize, "-INF");
5613 if (xmlXPathIsNaN(number)) {
5614 if (buffersize > (int)sizeof("NaN"))
5615 snprintf(buffer, buffersize, "NaN");
5616 } else if (number == 0) {
5617 snprintf(buffer, buffersize, "0.0E0");
5619 /* 3 is sign, decimal point, and terminating zero */
5620 char work[DBL_DIG + EXPONENT_DIGITS + 3];
5621 int integer_place, fraction_place;
5623 char *after_fraction;
5624 double absolute_value;
5627 absolute_value = fabs(number);
5630 * Result is in work, and after_fraction points
5631 * just past the fractional part.
5632 * Use scientific notation
5634 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
5635 fraction_place = DBL_DIG - 1;
5636 snprintf(work, sizeof(work),"%*.*e",
5637 integer_place, fraction_place, number);
5638 after_fraction = strchr(work + DBL_DIG, 'e');
5639 /* Remove fractional trailing zeroes */
5640 ptr = after_fraction;
5641 while (*(--ptr) == '0')
5645 while ((*ptr++ = *after_fraction++) != 0);
5647 /* Finally copy result back to caller */
5648 size = strlen(work) + 1;
5649 if (size > buffersize) {
5650 work[buffersize - 1] = 0;
5653 memmove(buffer, work, size);
5661 * xmlSchemaGetCanonValue:
5662 * @val: the precomputed value
5663 * @retValue: the returned value
5665 * Get the canonical lexical representation of the value.
5666 * The caller has to FREE the returned retValue.
5668 * WARNING: Some value types are not supported yet, resulting
5669 * in a @retValue of "???".
5671 * TODO: XML Schema 1.0 does not define canonical representations
5672 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5673 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
5676 * Returns 0 if the value could be built, 1 if the value type is
5677 * not supported yet and -1 in case of API errors.
5680 xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
5682 if ((retValue == NULL) || (val == NULL))
5685 switch (val->type) {
5686 case XML_SCHEMAS_STRING:
5687 if (val->value.str == NULL)
5688 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5691 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5693 case XML_SCHEMAS_NORMSTRING:
5694 if (val->value.str == NULL)
5695 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5697 *retValue = xmlSchemaWhiteSpaceReplace(
5698 (const xmlChar *) val->value.str);
5699 if ((*retValue) == NULL)
5700 *retValue = BAD_CAST xmlStrdup(
5701 (const xmlChar *) val->value.str);
5704 case XML_SCHEMAS_TOKEN:
5705 case XML_SCHEMAS_LANGUAGE:
5706 case XML_SCHEMAS_NMTOKEN:
5707 case XML_SCHEMAS_NAME:
5708 case XML_SCHEMAS_NCNAME:
5709 case XML_SCHEMAS_ID:
5710 case XML_SCHEMAS_IDREF:
5711 case XML_SCHEMAS_ENTITY:
5712 case XML_SCHEMAS_NOTATION: /* Unclear */
5713 case XML_SCHEMAS_ANYURI: /* Unclear */
5714 if (val->value.str == NULL)
5717 BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
5718 if (*retValue == NULL)
5720 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5722 case XML_SCHEMAS_QNAME:
5723 /* TODO: Unclear in XML Schema 1.0. */
5724 if (val->value.qname.uri == NULL) {
5725 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
5728 *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
5729 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5730 BAD_CAST val->value.qname.uri);
5731 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5733 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5734 BAD_CAST val->value.qname.uri);
5737 case XML_SCHEMAS_DECIMAL:
5739 * TODO: Lookout for a more simple implementation.
5741 if ((val->value.decimal.total == 1) &&
5742 (val->value.decimal.lo == 0)) {
5743 *retValue = xmlStrdup(BAD_CAST "0.0");
5745 xmlSchemaValDecimal dec = val->value.decimal;
5747 char *buf = NULL, *offs;
5749 /* Add room for the decimal point as well. */
5750 bufsize = dec.total + 2;
5753 /* Add room for leading/trailing zero. */
5754 if ((dec.frac == 0) || (dec.frac == dec.total))
5756 buf = xmlMalloc(bufsize);
5762 if (dec.frac == dec.total) {
5767 snprintf(offs, bufsize - (offs - buf),
5768 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5769 else if (dec.mi != 0)
5770 snprintf(offs, bufsize - (offs - buf),
5771 "%lu%lu", dec.mi, dec.lo);
5773 snprintf(offs, bufsize - (offs - buf),
5776 if (dec.frac != 0) {
5777 if (dec.frac != dec.total) {
5778 int diff = dec.total - dec.frac;
5780 * Insert the decimal point.
5782 memmove(offs + diff + 1, offs + diff, dec.frac +1);
5787 * Insert missing zeroes behind the decimal point.
5789 while (*(offs + i) != 0)
5791 if (i < dec.total) {
5792 memmove(offs + (dec.total - i), offs, i +1);
5793 memset(offs, '0', dec.total - i);
5798 * Append decimal point and zero.
5800 offs = buf + bufsize - 1;
5805 *retValue = BAD_CAST buf;
5808 case XML_SCHEMAS_INTEGER:
5809 case XML_SCHEMAS_PINTEGER:
5810 case XML_SCHEMAS_NPINTEGER:
5811 case XML_SCHEMAS_NINTEGER:
5812 case XML_SCHEMAS_NNINTEGER:
5813 case XML_SCHEMAS_LONG:
5814 case XML_SCHEMAS_BYTE:
5815 case XML_SCHEMAS_SHORT:
5816 case XML_SCHEMAS_INT:
5817 case XML_SCHEMAS_UINT:
5818 case XML_SCHEMAS_ULONG:
5819 case XML_SCHEMAS_USHORT:
5820 case XML_SCHEMAS_UBYTE:
5821 if ((val->value.decimal.total == 1) &&
5822 (val->value.decimal.lo == 0))
5823 *retValue = xmlStrdup(BAD_CAST "0");
5825 xmlSchemaValDecimal dec = val->value.decimal;
5826 int bufsize = dec.total + 1;
5828 /* Add room for the decimal point as well. */
5831 *retValue = xmlMalloc(bufsize);
5832 if (*retValue == NULL)
5836 snprintf((char *) *retValue, bufsize,
5837 "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5839 snprintf((char *) *retValue, bufsize,
5840 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5841 } else if (dec.mi != 0) {
5843 snprintf((char *) *retValue, bufsize,
5844 "-%lu%lu", dec.mi, dec.lo);
5846 snprintf((char *) *retValue, bufsize,
5847 "%lu%lu", dec.mi, dec.lo);
5850 snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
5852 snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
5856 case XML_SCHEMAS_BOOLEAN:
5858 *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
5860 *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
5862 case XML_SCHEMAS_DURATION: {
5865 unsigned long mon, day, hour = 0, min = 0;
5866 double sec = 0, left;
5868 /* TODO: Unclear in XML Schema 1.0 */
5870 * TODO: This results in a normalized output of the value
5871 * - which is NOT conformant to the spec -
5872 * since the exact values of each property are not
5873 * recoverable. Think about extending the structure to
5874 * provide a field for every property.
5876 year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
5877 mon = labs(val->value.dur.mon) - 12 * year;
5879 day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
5880 left = fabs(val->value.dur.sec) - day * 86400;
5882 hour = (unsigned long) FQUOTIENT(left, 3600);
5883 left = left - (hour * 3600);
5885 min = (unsigned long) FQUOTIENT(left, 60);
5886 sec = left - (min * 60);
5889 if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
5890 snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5891 year, mon, day, hour, min, sec);
5893 snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5894 year, mon, day, hour, min, sec);
5895 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5898 case XML_SCHEMAS_GYEAR: {
5900 /* TODO: Unclear in XML Schema 1.0 */
5901 /* TODO: What to do with the timezone? */
5902 snprintf(buf, 30, "%04ld", val->value.date.year);
5903 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5906 case XML_SCHEMAS_GMONTH: {
5907 /* TODO: Unclear in XML Schema 1.0 */
5908 /* TODO: What to do with the timezone? */
5909 *retValue = xmlMalloc(6);
5910 if (*retValue == NULL)
5912 snprintf((char *) *retValue, 6, "--%02u",
5913 val->value.date.mon);
5916 case XML_SCHEMAS_GDAY: {
5917 /* TODO: Unclear in XML Schema 1.0 */
5918 /* TODO: What to do with the timezone? */
5919 *retValue = xmlMalloc(6);
5920 if (*retValue == NULL)
5922 snprintf((char *) *retValue, 6, "---%02u",
5923 val->value.date.day);
5926 case XML_SCHEMAS_GMONTHDAY: {
5927 /* TODO: Unclear in XML Schema 1.0 */
5928 /* TODO: What to do with the timezone? */
5929 *retValue = xmlMalloc(8);
5930 if (*retValue == NULL)
5932 snprintf((char *) *retValue, 8, "--%02u-%02u",
5933 val->value.date.mon, val->value.date.day);
5936 case XML_SCHEMAS_GYEARMONTH: {
5938 /* TODO: Unclear in XML Schema 1.0 */
5939 /* TODO: What to do with the timezone? */
5940 if (val->value.date.year < 0)
5941 snprintf(buf, 35, "-%04ld-%02u",
5942 labs(val->value.date.year),
5943 val->value.date.mon);
5945 snprintf(buf, 35, "%04ld-%02u",
5946 val->value.date.year, val->value.date.mon);
5947 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5950 case XML_SCHEMAS_TIME:
5954 if (val->value.date.tz_flag) {
5955 xmlSchemaValPtr norm;
5957 norm = xmlSchemaDateNormalize(val, 0);
5961 * TODO: Check if "%.14g" is portable.
5964 "%02u:%02u:%02.14gZ",
5965 norm->value.date.hour,
5966 norm->value.date.min,
5967 norm->value.date.sec);
5968 xmlSchemaFreeValue(norm);
5971 "%02u:%02u:%02.14g",
5972 val->value.date.hour,
5973 val->value.date.min,
5974 val->value.date.sec);
5976 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5979 case XML_SCHEMAS_DATE:
5983 if (val->value.date.tz_flag) {
5984 xmlSchemaValPtr norm;
5986 norm = xmlSchemaDateNormalize(val, 0);
5990 * TODO: Append the canonical value of the
5991 * recoverable timezone and not "Z".
5995 norm->value.date.year, norm->value.date.mon,
5996 norm->value.date.day);
5997 xmlSchemaFreeValue(norm);
6001 val->value.date.year, val->value.date.mon,
6002 val->value.date.day);
6004 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6007 case XML_SCHEMAS_DATETIME:
6011 if (val->value.date.tz_flag) {
6012 xmlSchemaValPtr norm;
6014 norm = xmlSchemaDateNormalize(val, 0);
6018 * TODO: Check if "%.14g" is portable.
6021 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
6022 norm->value.date.year, norm->value.date.mon,
6023 norm->value.date.day, norm->value.date.hour,
6024 norm->value.date.min, norm->value.date.sec);
6025 xmlSchemaFreeValue(norm);
6028 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
6029 val->value.date.year, val->value.date.mon,
6030 val->value.date.day, val->value.date.hour,
6031 val->value.date.min, val->value.date.sec);
6033 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6036 case XML_SCHEMAS_HEXBINARY:
6037 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
6039 case XML_SCHEMAS_BASE64BINARY:
6041 * TODO: Is the following spec piece implemented?:
6042 * SPEC: "Note: For some values the canonical form defined
6043 * above does not conform to [RFC 2045], which requires breaking
6044 * with linefeeds at appropriate intervals."
6046 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
6048 case XML_SCHEMAS_FLOAT: {
6051 * |m| < 16777216, -149 <= e <= 104.
6052 * TODO: Handle, NaN, INF, -INF. The format is not
6053 * yet conformant. The c type float does not cover
6056 snprintf(buf, 30, "%01.14e", val->value.f);
6057 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6060 case XML_SCHEMAS_DOUBLE: {
6062 /* |m| < 9007199254740992, -1075 <= e <= 970 */
6064 * TODO: Handle, NaN, INF, -INF. The format is not
6065 * yet conformant. The c type float does not cover
6068 snprintf(buf, 40, "%01.14e", val->value.d);
6069 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6073 *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
6076 if (*retValue == NULL)
6082 * xmlSchemaGetCanonValueWhtsp:
6083 * @val: the precomputed value
6084 * @retValue: the returned value
6085 * @ws: the whitespace type of the value
6087 * Get the canonical representation of the value.
6088 * The caller has to free the returned @retValue.
6090 * Returns 0 if the value could be built, 1 if the value type is
6091 * not supported yet and -1 in case of API errors.
6094 xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
6095 const xmlChar **retValue,
6096 xmlSchemaWhitespaceValueType ws)
6098 if ((retValue == NULL) || (val == NULL))
6100 if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
6101 (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
6105 switch (val->type) {
6106 case XML_SCHEMAS_STRING:
6107 if (val->value.str == NULL)
6108 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6109 else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6110 *retValue = xmlSchemaCollapseString(val->value.str);
6111 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
6112 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6113 if ((*retValue) == NULL)
6114 *retValue = BAD_CAST xmlStrdup(val->value.str);
6116 case XML_SCHEMAS_NORMSTRING:
6117 if (val->value.str == NULL)
6118 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6120 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6121 *retValue = xmlSchemaCollapseString(val->value.str);
6123 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6124 if ((*retValue) == NULL)
6125 *retValue = BAD_CAST xmlStrdup(val->value.str);
6129 return (xmlSchemaGetCanonValue(val, retValue));
6135 * xmlSchemaGetValType:
6136 * @val: a schemas value
6138 * Accessor for the type of a value
6140 * Returns the xmlSchemaValType of the value
6143 xmlSchemaGetValType(xmlSchemaValPtr val)
6146 return(XML_SCHEMAS_UNKNOWN);
6150 #define bottom_xmlschemastypes
6151 #include "elfgcchack.h"
6152 #endif /* LIBXML_SCHEMAS_ENABLED */