blob: ffb25059c2fa349413d3c5f1cf938958f92c0b23 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * schemastypes.c : implementation of the XML Schema Datatypes
3 * definition and validity checking
4 *
5 * See Copyright for the status of this software.
6 *
7 * Daniel Veillard <veillard@redhat.com>
8 */
9
10#define IN_LIBXML
11#include "libxml.h"
12
13#ifdef LIBXML_SCHEMAS_ENABLED
14
15#include <string.h>
16#include <libxml/xmlmemory.h>
17#include <libxml/parser.h>
18#include <libxml/parserInternals.h>
19#include <libxml/hash.h>
20#include <libxml/valid.h>
Daniel Veillard96a4b252003-02-06 08:22:32 +000021#include <libxml/xpath.h>
22#include <libxml/uri.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000023
24#include <libxml/xmlschemas.h>
25#include <libxml/schemasInternals.h>
26#include <libxml/xmlschemastypes.h>
27
Daniel Veillard070803b2002-05-03 07:29:38 +000028#ifdef HAVE_MATH_H
29#include <math.h>
30#endif
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000031#ifdef HAVE_FLOAT_H
32#include <float.h>
33#endif
Daniel Veillard070803b2002-05-03 07:29:38 +000034
Daniel Veillard4255d502002-04-16 15:50:10 +000035#define DEBUG
36
Daniel Veillardd0cf7f62004-11-09 16:17:02 +000037#ifndef LIBXML_XPATH_ENABLED
38extern double xmlXPathNAN;
39extern double xmlXPathPINF;
40extern double xmlXPathNINF;
41#endif
42
Daniel Veillard4255d502002-04-16 15:50:10 +000043#define TODO \
44 xmlGenericError(xmlGenericErrorContext, \
45 "Unimplemented block at %s:%d\n", \
46 __FILE__, __LINE__);
47
48#define XML_SCHEMAS_NAMESPACE_NAME \
49 (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
50
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000051#define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \
52 ((c) == 0xd))
53
54#define IS_WSP_SPACE_CH(c) ((c) == 0x20)
55
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000056#define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c)
57
Daniel Veillard070803b2002-05-03 07:29:38 +000058/* Date value */
59typedef struct _xmlSchemaValDate xmlSchemaValDate;
60typedef xmlSchemaValDate *xmlSchemaValDatePtr;
61struct _xmlSchemaValDate {
62 long year;
63 unsigned int mon :4; /* 1 <= mon <= 12 */
64 unsigned int day :5; /* 1 <= day <= 31 */
65 unsigned int hour :5; /* 0 <= hour <= 23 */
66 unsigned int min :6; /* 0 <= min <= 59 */
67 double sec;
Daniel Veillarda77cf712003-05-09 23:09:55 +000068 unsigned int tz_flag :1; /* is tzo explicitely set? */
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +000069 signed int tzo :12; /* -1440 <= tzo <= 1440;
70 currently only -840 to +840 are needed */
Daniel Veillard070803b2002-05-03 07:29:38 +000071};
72
73/* Duration value */
74typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
75typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
76struct _xmlSchemaValDuration {
77 long mon; /* mon stores years also */
78 long day;
79 double sec; /* sec stores min and hour also */
80};
81
Daniel Veillard4255d502002-04-16 15:50:10 +000082typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
83typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
84struct _xmlSchemaValDecimal {
85 /* would use long long but not portable */
Daniel Veillarde637c4a2003-03-30 21:10:09 +000086 unsigned long lo;
87 unsigned long mi;
88 unsigned long hi;
Daniel Veillard4255d502002-04-16 15:50:10 +000089 unsigned int extra;
Daniel Veillard5a872412002-05-22 06:40:27 +000090 unsigned int sign:1;
William M. Brackc1939562003-08-05 15:52:22 +000091 unsigned int frac:7;
92 unsigned int total:8;
Daniel Veillard4255d502002-04-16 15:50:10 +000093};
94
Daniel Veillarde637c4a2003-03-30 21:10:09 +000095typedef struct _xmlSchemaValQName xmlSchemaValQName;
96typedef xmlSchemaValQName *xmlSchemaValQNamePtr;
97struct _xmlSchemaValQName {
98 xmlChar *name;
99 xmlChar *uri;
100};
101
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000102typedef struct _xmlSchemaValHex xmlSchemaValHex;
103typedef xmlSchemaValHex *xmlSchemaValHexPtr;
104struct _xmlSchemaValHex {
105 xmlChar *str;
106 unsigned int total;
107};
108
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000109typedef struct _xmlSchemaValBase64 xmlSchemaValBase64;
110typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr;
111struct _xmlSchemaValBase64 {
112 xmlChar *str;
113 unsigned int total;
114};
115
Daniel Veillard4255d502002-04-16 15:50:10 +0000116struct _xmlSchemaVal {
117 xmlSchemaValType type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000118 struct _xmlSchemaVal *next;
Daniel Veillard4255d502002-04-16 15:50:10 +0000119 union {
Daniel Veillard5a872412002-05-22 06:40:27 +0000120 xmlSchemaValDecimal decimal;
Daniel Veillard070803b2002-05-03 07:29:38 +0000121 xmlSchemaValDate date;
122 xmlSchemaValDuration dur;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000123 xmlSchemaValQName qname;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000124 xmlSchemaValHex hex;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000125 xmlSchemaValBase64 base64;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000126 float f;
127 double d;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000128 int b;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000129 xmlChar *str;
Daniel Veillard4255d502002-04-16 15:50:10 +0000130 } value;
131};
132
133static int xmlSchemaTypesInitialized = 0;
134static xmlHashTablePtr xmlSchemaTypesBank = NULL;
135
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000136/*
137 * Basic types
138 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000139static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
140static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
141static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
142static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000143static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000144static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000145static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL;
146static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL;
147static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL;
148static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL;
149static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL;
150static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL;
151static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000152static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000153static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000154static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
Daniel Veillard560c2a42003-07-06 21:13:49 +0000155static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000156static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000157static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000158
159/*
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000160 * Derived types
161 */
162static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
163static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL;
164static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL;
165static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
166static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL;
167static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL;
168static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL;
169static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL;
170static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL;
171static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL;
172static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL;
173static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL;
174static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000175static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL;
176static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL;
177static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL;
178static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL;
179static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000180static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000181static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
182static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
183static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000184static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL;
185static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000186static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000187static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
188static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000189
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000190/************************************************************************
191 * *
192 * Datatype error handlers *
193 * *
194 ************************************************************************/
195/**
196 * xmlSchemaTypeErrMemory:
197 * @extra: extra informations
198 *
199 * Handle an out of memory condition
200 */
201static void
202xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra)
203{
204 __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra);
205}
206
207/************************************************************************
208 * *
209 * Base types support *
210 * *
211 ************************************************************************/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000212
213/**
214 * xmlSchemaNewValue:
215 * @type: the value type
216 *
217 * Allocate a new simple type value
218 *
219 * Returns a pointer to the new value or NULL in case of error
220 */
221static xmlSchemaValPtr
222xmlSchemaNewValue(xmlSchemaValType type) {
223 xmlSchemaValPtr value;
224
225 value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
226 if (value == NULL) {
227 return(NULL);
228 }
229 memset(value, 0, sizeof(xmlSchemaVal));
230 value->type = type;
231 return(value);
232}
233
234static xmlSchemaFacetPtr
235xmlSchemaNewMinLengthFacet(int value)
236{
237 xmlSchemaFacetPtr ret;
238
239 ret = xmlSchemaNewFacet();
240 ret->type = XML_SCHEMA_FACET_MINLENGTH;
241 ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER);
242 ret->val->value.decimal.lo = value;
243 return (ret);
244}
245
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000246/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000247 * xmlSchemaInitBasicType:
248 * @name: the type name
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000249 * @type: the value type associated
Daniel Veillard4255d502002-04-16 15:50:10 +0000250 *
Daniel Veillard01fa6152004-06-29 17:04:39 +0000251 * Initialize one primitive built-in type
Daniel Veillard4255d502002-04-16 15:50:10 +0000252 */
253static xmlSchemaTypePtr
Daniel Veillard01fa6152004-06-29 17:04:39 +0000254xmlSchemaInitBasicType(const char *name, xmlSchemaValType type,
255 xmlSchemaTypePtr baseType) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000256 xmlSchemaTypePtr ret;
257
258 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
259 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000260 xmlSchemaTypeErrMemory(NULL, "could not initialize basic types");
Daniel Veillard4255d502002-04-16 15:50:10 +0000261 return(NULL);
262 }
263 memset(ret, 0, sizeof(xmlSchemaType));
Daniel Veillardbe9c6322003-11-22 20:37:51 +0000264 ret->name = (const xmlChar *)name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000265 ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME;
Daniel Veillard4255d502002-04-16 15:50:10 +0000266 ret->type = XML_SCHEMA_TYPE_BASIC;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000267 ret->baseType = baseType;
Daniel Veillard4255d502002-04-16 15:50:10 +0000268 ret->contentType = XML_SCHEMA_CONTENT_BASIC;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000269 /*
270 * Primitive types.
271 */
272 switch (type) {
Daniel Veillard01fa6152004-06-29 17:04:39 +0000273 case XML_SCHEMAS_STRING:
274 case XML_SCHEMAS_DECIMAL:
275 case XML_SCHEMAS_DATE:
276 case XML_SCHEMAS_DATETIME:
277 case XML_SCHEMAS_TIME:
278 case XML_SCHEMAS_GYEAR:
279 case XML_SCHEMAS_GYEARMONTH:
280 case XML_SCHEMAS_GMONTH:
281 case XML_SCHEMAS_GMONTHDAY:
282 case XML_SCHEMAS_GDAY:
283 case XML_SCHEMAS_DURATION:
284 case XML_SCHEMAS_FLOAT:
285 case XML_SCHEMAS_DOUBLE:
286 case XML_SCHEMAS_BOOLEAN:
287 case XML_SCHEMAS_ANYURI:
288 case XML_SCHEMAS_HEXBINARY:
289 case XML_SCHEMAS_BASE64BINARY:
290 case XML_SCHEMAS_QNAME:
291 case XML_SCHEMAS_NOTATION:
292 ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000293 break;
William M. Brack96d2eff2004-06-30 11:48:47 +0000294 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000295 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000296 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000297 /*
298 * Set variety.
299 */
300 switch (type) {
301 case XML_SCHEMAS_ANYTYPE:
302 case XML_SCHEMAS_ANYSIMPLETYPE:
303 break;
304 case XML_SCHEMAS_IDREFS:
305 case XML_SCHEMAS_NMTOKENS:
306 case XML_SCHEMAS_ENTITIES:
307 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
308 ret->facets = xmlSchemaNewMinLengthFacet(1);
309 ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
310 break;
311 default:
312 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
313 break;
314 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000315 xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
316 XML_SCHEMAS_NAMESPACE_NAME, ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000317 ret->builtInType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +0000318 return(ret);
319}
320
321/*
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000322* WARNING: Those type reside normally in xmlschemas.c but are
323* redefined here locally in oder of being able to use them for xs:anyType-
324* TODO: Remove those definition if we move the types to a header file.
325* TODO: Always keep those structs up-to-date with the originals.
326*/
327#define UNBOUNDED (1 << 30)
328
329typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
330typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
331struct _xmlSchemaTreeItem {
332 xmlSchemaTypeType type;
333 xmlSchemaAnnotPtr annot;
334 xmlSchemaTreeItemPtr next;
335 xmlSchemaTreeItemPtr children;
336};
337
338typedef struct _xmlSchemaParticle xmlSchemaParticle;
339typedef xmlSchemaParticle *xmlSchemaParticlePtr;
340struct _xmlSchemaParticle {
341 xmlSchemaTypeType type;
342 xmlSchemaAnnotPtr annot;
343 xmlSchemaTreeItemPtr next;
344 xmlSchemaTreeItemPtr children;
345 int minOccurs;
346 int maxOccurs;
347 xmlNodePtr node;
348};
349
350typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
351typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
352struct _xmlSchemaModelGroup {
353 xmlSchemaTypeType type;
354 xmlSchemaAnnotPtr annot;
355 xmlSchemaTreeItemPtr next;
356 xmlSchemaTreeItemPtr children;
357 xmlNodePtr node;
358};
359
360static xmlSchemaParticlePtr
361xmlSchemaAddParticle(void)
362{
363 xmlSchemaParticlePtr ret = NULL;
364
365 ret = (xmlSchemaParticlePtr)
366 xmlMalloc(sizeof(xmlSchemaParticle));
367 if (ret == NULL) {
368 xmlSchemaTypeErrMemory(NULL, "allocating particle component");
369 return (NULL);
370 }
371 memset(ret, 0, sizeof(xmlSchemaParticle));
372 ret->type = XML_SCHEMA_TYPE_PARTICLE;
373 ret->minOccurs = 1;
374 ret->maxOccurs = 1;
375 return (ret);
376}
377
378/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000379 * xmlSchemaInitTypes:
380 *
381 * Initialize the default XML Schemas type library
382 */
383void
Daniel Veillard6560a422003-03-27 21:25:38 +0000384xmlSchemaInitTypes(void)
385{
Daniel Veillard4255d502002-04-16 15:50:10 +0000386 if (xmlSchemaTypesInitialized != 0)
Daniel Veillard6560a422003-03-27 21:25:38 +0000387 return;
Daniel Veillard4255d502002-04-16 15:50:10 +0000388 xmlSchemaTypesBank = xmlHashCreate(40);
Daniel Veillard6560a422003-03-27 21:25:38 +0000389
Daniel Veillard01fa6152004-06-29 17:04:39 +0000390
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000391 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +0000392 * 3.4.7 Built-in Complex Type Definition
393 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000394 xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
William M. Brack2f2a6632004-08-20 23:09:47 +0000395 XML_SCHEMAS_ANYTYPE,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000396 NULL);
397 xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef;
398 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000399 /*
400 * Init the content type.
401 */
402 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000403 {
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000404 xmlSchemaParticlePtr particle;
405 xmlSchemaModelGroupPtr sequence;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000406 xmlSchemaWildcardPtr wild;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000407 /* First particle. */
408 particle = xmlSchemaAddParticle();
409 if (particle == NULL)
410 return;
411 xmlSchemaTypeAnyTypeDef->subtypes = (xmlSchemaTypePtr) particle;
412 /* Sequence model group. */
413 sequence = (xmlSchemaModelGroupPtr)
414 xmlMalloc(sizeof(xmlSchemaModelGroup));
415 if (sequence == NULL) {
416 xmlSchemaTypeErrMemory(NULL, "allocating model group component");
417 return;
418 }
419 memset(sequence, 0, sizeof(xmlSchemaModelGroup));
420 sequence->type = XML_SCHEMA_TYPE_SEQUENCE;
421 particle->children = (xmlSchemaTreeItemPtr) sequence;
422 /* Second particle. */
423 particle = xmlSchemaAddParticle();
424 if (particle == NULL)
425 return;
426 particle->minOccurs = 0;
427 particle->maxOccurs = UNBOUNDED;
428 sequence->children = (xmlSchemaTreeItemPtr) particle;
429 /* The wildcard */
Daniel Veillard01fa6152004-06-29 17:04:39 +0000430 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
431 if (wild == NULL) {
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000432 xmlSchemaTypeErrMemory(NULL, "allocating wildcard component");
433 return;
434 }
435 memset(wild, 0, sizeof(xmlSchemaWildcard));
436 wild->type = XML_SCHEMA_TYPE_ANY;
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +0000437 wild->any = 1;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000438 wild->processContents = XML_SCHEMAS_ANY_LAX;
439 particle->children = (xmlSchemaTreeItemPtr) wild;
440 /*
441 * Create the attribute wildcard.
442 */
443 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
444 if (wild == NULL) {
445 xmlSchemaTypeErrMemory(NULL, "could not create an attribute "
446 "wildcard on anyType");
Daniel Veillard01fa6152004-06-29 17:04:39 +0000447 return;
448 }
449 memset(wild, 0, sizeof(xmlSchemaWildcard));
450 wild->any = 1;
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +0000451 wild->processContents = XML_SCHEMAS_ANY_LAX;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000452 xmlSchemaTypeAnyTypeDef->attributeWildcard = wild;
453 }
454 xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
William M. Brack2f2a6632004-08-20 23:09:47 +0000455 XML_SCHEMAS_ANYSIMPLETYPE,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000456 xmlSchemaTypeAnyTypeDef);
457 /*
458 * primitive datatypes
459 */
460 xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
461 XML_SCHEMAS_STRING,
462 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000463 xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000464 XML_SCHEMAS_DECIMAL,
465 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000466 xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000467 XML_SCHEMAS_DATE,
468 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000469 xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000470 XML_SCHEMAS_DATETIME,
471 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000472 xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000473 XML_SCHEMAS_TIME,
474 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000475 xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000476 XML_SCHEMAS_GYEAR,
477 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000478 xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000479 XML_SCHEMAS_GYEARMONTH,
480 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000481 xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000482 XML_SCHEMAS_GMONTH,
483 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000484 xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000485 XML_SCHEMAS_GMONTHDAY,
486 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000487 xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000488 XML_SCHEMAS_GDAY,
489 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000490 xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000491 XML_SCHEMAS_DURATION,
492 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000493 xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000494 XML_SCHEMAS_FLOAT,
495 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000496 xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000497 XML_SCHEMAS_DOUBLE,
498 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000499 xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000500 XML_SCHEMAS_BOOLEAN,
501 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000502 xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000503 XML_SCHEMAS_ANYURI,
504 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard560c2a42003-07-06 21:13:49 +0000505 xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000506 XML_SCHEMAS_HEXBINARY,
507 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000508 xmlSchemaTypeBase64BinaryDef
Daniel Veillard01fa6152004-06-29 17:04:39 +0000509 = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY,
510 xmlSchemaTypeAnySimpleTypeDef);
511 xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
512 XML_SCHEMAS_NOTATION,
513 xmlSchemaTypeAnySimpleTypeDef);
514 xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
515 XML_SCHEMAS_QNAME,
516 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard4255d502002-04-16 15:50:10 +0000517
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000518 /*
519 * derived datatypes
520 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000521 xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000522 XML_SCHEMAS_INTEGER,
523 xmlSchemaTypeDecimalDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000524 xmlSchemaTypeNonPositiveIntegerDef =
525 xmlSchemaInitBasicType("nonPositiveInteger",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000526 XML_SCHEMAS_NPINTEGER,
527 xmlSchemaTypeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000528 xmlSchemaTypeNegativeIntegerDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000529 xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER,
530 xmlSchemaTypeNonPositiveIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000531 xmlSchemaTypeLongDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000532 xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG,
533 xmlSchemaTypeIntegerDef);
534 xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT,
535 xmlSchemaTypeLongDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000536 xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000537 XML_SCHEMAS_SHORT,
538 xmlSchemaTypeIntDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000539 xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000540 XML_SCHEMAS_BYTE,
541 xmlSchemaTypeShortDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000542 xmlSchemaTypeNonNegativeIntegerDef =
543 xmlSchemaInitBasicType("nonNegativeInteger",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000544 XML_SCHEMAS_NNINTEGER,
545 xmlSchemaTypeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000546 xmlSchemaTypeUnsignedLongDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000547 xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG,
548 xmlSchemaTypeNonNegativeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000549 xmlSchemaTypeUnsignedIntDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000550 xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT,
551 xmlSchemaTypeUnsignedLongDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000552 xmlSchemaTypeUnsignedShortDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000553 xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT,
554 xmlSchemaTypeUnsignedIntDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000555 xmlSchemaTypeUnsignedByteDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000556 xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE,
557 xmlSchemaTypeUnsignedShortDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000558 xmlSchemaTypePositiveIntegerDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000559 xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER,
560 xmlSchemaTypeNonNegativeIntegerDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000561 xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000562 XML_SCHEMAS_NORMSTRING,
563 xmlSchemaTypeStringDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000564 xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000565 XML_SCHEMAS_TOKEN,
566 xmlSchemaTypeNormStringDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000567 xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000568 XML_SCHEMAS_LANGUAGE,
569 xmlSchemaTypeTokenDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000570 xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000571 XML_SCHEMAS_NAME,
572 xmlSchemaTypeTokenDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000573 xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000574 XML_SCHEMAS_NMTOKEN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000575 xmlSchemaTypeTokenDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000576 xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
577 XML_SCHEMAS_NCNAME,
578 xmlSchemaTypeNameDef);
579 xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000580 xmlSchemaTypeNCNameDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000581 xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
582 XML_SCHEMAS_IDREF,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000583 xmlSchemaTypeNCNameDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000584 xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
585 XML_SCHEMAS_ENTITY,
586 xmlSchemaTypeNCNameDef);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000587 /*
588 * Derived list types.
589 */
590 /* ENTITIES */
Daniel Veillard01fa6152004-06-29 17:04:39 +0000591 xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
592 XML_SCHEMAS_ENTITIES,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000593 xmlSchemaTypeAnySimpleTypeDef);
594 xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef;
595 /* IDREFS */
596 xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
597 XML_SCHEMAS_IDREFS,
598 xmlSchemaTypeAnySimpleTypeDef);
599 xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef;
600
601 /* NMTOKENS */
602 xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
603 XML_SCHEMAS_NMTOKENS,
604 xmlSchemaTypeAnySimpleTypeDef);
605 xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef;
606
Daniel Veillard4255d502002-04-16 15:50:10 +0000607 xmlSchemaTypesInitialized = 1;
608}
609
610/**
611 * xmlSchemaCleanupTypes:
612 *
613 * Cleanup the default XML Schemas type library
614 */
615void
616xmlSchemaCleanupTypes(void) {
617 if (xmlSchemaTypesInitialized == 0)
618 return;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000619 /*
620 * Free xs:anyType.
621 */
622 {
623 xmlSchemaParticlePtr particle;
624 /* Attribute wildcard. */
625 xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard);
626 /* Content type. */
627 particle = (xmlSchemaParticlePtr) xmlSchemaTypeAnyTypeDef->subtypes;
628 /* Wildcard. */
629 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr)
630 particle->children->children->children);
631 xmlFree((xmlSchemaParticlePtr) particle->children->children);
632 /* Sequence model group. */
633 xmlFree((xmlSchemaModelGroupPtr) particle->children);
634 xmlFree((xmlSchemaParticlePtr) particle);
635 xmlSchemaTypeAnyTypeDef->subtypes = NULL;
636 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000637 xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
638 xmlSchemaTypesInitialized = 0;
639}
640
641/**
Daniel Veillard6927b102004-10-27 17:29:04 +0000642 * xmlSchemaIsBuiltInTypeFacet:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000643 * @type: the built-in type
644 * @facetType: the facet type
645 *
646 * Evaluates if a specific facet can be
647 * used in conjunction with a type.
648 *
649 * Returns 1 if the facet can be used with the given built-in type,
650 * 0 otherwise and -1 in case the type is not a built-in type.
651 */
652int
653xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType)
654{
Daniel Veillardce682bc2004-11-05 17:22:25 +0000655 if (type == NULL)
656 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000657 if (type->type != XML_SCHEMA_TYPE_BASIC)
658 return (-1);
659 switch (type->builtInType) {
660 case XML_SCHEMAS_BOOLEAN:
661 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
662 (facetType == XML_SCHEMA_FACET_WHITESPACE))
663 return (1);
664 else
665 return (0);
666 case XML_SCHEMAS_STRING:
667 case XML_SCHEMAS_NOTATION:
668 case XML_SCHEMAS_QNAME:
669 case XML_SCHEMAS_ANYURI:
670 case XML_SCHEMAS_BASE64BINARY:
671 case XML_SCHEMAS_HEXBINARY:
672 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
673 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
674 (facetType == XML_SCHEMA_FACET_MAXLENGTH) ||
675 (facetType == XML_SCHEMA_FACET_PATTERN) ||
676 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
677 (facetType == XML_SCHEMA_FACET_WHITESPACE))
678 return (1);
679 else
680 return (0);
681 case XML_SCHEMAS_DECIMAL:
682 if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) ||
683 (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) ||
684 (facetType == XML_SCHEMA_FACET_PATTERN) ||
685 (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
686 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
687 (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
688 (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
689 (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
690 (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
691 return (1);
692 else
693 return (0);
694 case XML_SCHEMAS_TIME:
695 case XML_SCHEMAS_GDAY:
696 case XML_SCHEMAS_GMONTH:
697 case XML_SCHEMAS_GMONTHDAY:
698 case XML_SCHEMAS_GYEAR:
699 case XML_SCHEMAS_GYEARMONTH:
700 case XML_SCHEMAS_DATE:
701 case XML_SCHEMAS_DATETIME:
702 case XML_SCHEMAS_DURATION:
703 case XML_SCHEMAS_FLOAT:
704 case XML_SCHEMAS_DOUBLE:
705 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
706 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
707 (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
708 (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
709 (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
710 (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
711 (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
712 return (1);
713 else
714 return (0);
715 default:
Daniel Veillardc7e3cc42004-09-28 12:33:52 +0000716 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000717 }
718 return (0);
719}
720
721/**
722 * xmlSchemaGetBuiltInType:
723 * @type: the type of the built in type
724 *
725 * Gives you the type struct for a built-in
726 * type by its type id.
727 *
728 * Returns the type if found, NULL otherwise.
729 */
730xmlSchemaTypePtr
731xmlSchemaGetBuiltInType(xmlSchemaValType type)
732{
733 if (xmlSchemaTypesInitialized == 0)
734 xmlSchemaInitTypes();
735 switch (type) {
736
737 case XML_SCHEMAS_ANYSIMPLETYPE:
738 return (xmlSchemaTypeAnySimpleTypeDef);
739 case XML_SCHEMAS_STRING:
740 return (xmlSchemaTypeStringDef);
741 case XML_SCHEMAS_NORMSTRING:
742 return (xmlSchemaTypeNormStringDef);
743 case XML_SCHEMAS_DECIMAL:
744 return (xmlSchemaTypeDecimalDef);
745 case XML_SCHEMAS_TIME:
746 return (xmlSchemaTypeTimeDef);
747 case XML_SCHEMAS_GDAY:
748 return (xmlSchemaTypeGDayDef);
749 case XML_SCHEMAS_GMONTH:
750 return (xmlSchemaTypeGMonthDef);
751 case XML_SCHEMAS_GMONTHDAY:
752 return (xmlSchemaTypeGMonthDayDef);
753 case XML_SCHEMAS_GYEAR:
754 return (xmlSchemaTypeGYearDef);
755 case XML_SCHEMAS_GYEARMONTH:
756 return (xmlSchemaTypeGYearMonthDef);
757 case XML_SCHEMAS_DATE:
758 return (xmlSchemaTypeDateDef);
759 case XML_SCHEMAS_DATETIME:
760 return (xmlSchemaTypeDatetimeDef);
761 case XML_SCHEMAS_DURATION:
762 return (xmlSchemaTypeDurationDef);
763 case XML_SCHEMAS_FLOAT:
764 return (xmlSchemaTypeFloatDef);
765 case XML_SCHEMAS_DOUBLE:
766 return (xmlSchemaTypeDoubleDef);
767 case XML_SCHEMAS_BOOLEAN:
768 return (xmlSchemaTypeBooleanDef);
769 case XML_SCHEMAS_TOKEN:
770 return (xmlSchemaTypeTokenDef);
771 case XML_SCHEMAS_LANGUAGE:
772 return (xmlSchemaTypeLanguageDef);
773 case XML_SCHEMAS_NMTOKEN:
774 return (xmlSchemaTypeNmtokenDef);
775 case XML_SCHEMAS_NMTOKENS:
776 return (xmlSchemaTypeNmtokensDef);
777 case XML_SCHEMAS_NAME:
778 return (xmlSchemaTypeNameDef);
779 case XML_SCHEMAS_QNAME:
780 return (xmlSchemaTypeQNameDef);
781 case XML_SCHEMAS_NCNAME:
782 return (xmlSchemaTypeNCNameDef);
783 case XML_SCHEMAS_ID:
784 return (xmlSchemaTypeIdDef);
785 case XML_SCHEMAS_IDREF:
786 return (xmlSchemaTypeIdrefDef);
787 case XML_SCHEMAS_IDREFS:
788 return (xmlSchemaTypeIdrefsDef);
789 case XML_SCHEMAS_ENTITY:
790 return (xmlSchemaTypeEntityDef);
791 case XML_SCHEMAS_ENTITIES:
792 return (xmlSchemaTypeEntitiesDef);
793 case XML_SCHEMAS_NOTATION:
794 return (xmlSchemaTypeNotationDef);
795 case XML_SCHEMAS_ANYURI:
796 return (xmlSchemaTypeAnyURIDef);
797 case XML_SCHEMAS_INTEGER:
798 return (xmlSchemaTypeIntegerDef);
799 case XML_SCHEMAS_NPINTEGER:
800 return (xmlSchemaTypeNonPositiveIntegerDef);
801 case XML_SCHEMAS_NINTEGER:
802 return (xmlSchemaTypeNegativeIntegerDef);
803 case XML_SCHEMAS_NNINTEGER:
804 return (xmlSchemaTypeNonNegativeIntegerDef);
805 case XML_SCHEMAS_PINTEGER:
806 return (xmlSchemaTypePositiveIntegerDef);
807 case XML_SCHEMAS_INT:
808 return (xmlSchemaTypeIntDef);
809 case XML_SCHEMAS_UINT:
810 return (xmlSchemaTypeUnsignedIntDef);
811 case XML_SCHEMAS_LONG:
812 return (xmlSchemaTypeLongDef);
813 case XML_SCHEMAS_ULONG:
814 return (xmlSchemaTypeUnsignedLongDef);
815 case XML_SCHEMAS_SHORT:
816 return (xmlSchemaTypeShortDef);
817 case XML_SCHEMAS_USHORT:
818 return (xmlSchemaTypeUnsignedShortDef);
819 case XML_SCHEMAS_BYTE:
820 return (xmlSchemaTypeByteDef);
821 case XML_SCHEMAS_UBYTE:
822 return (xmlSchemaTypeUnsignedByteDef);
823 case XML_SCHEMAS_HEXBINARY:
824 return (xmlSchemaTypeHexBinaryDef);
825 case XML_SCHEMAS_BASE64BINARY:
826 return (xmlSchemaTypeBase64BinaryDef);
827 case XML_SCHEMAS_ANYTYPE:
828 return (xmlSchemaTypeAnyTypeDef);
829 default:
830 return (NULL);
831 }
832}
833
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000834/**
835 * xmlSchemaValueAppend:
836 * @prev: the value
837 * @cur: the value to be appended
838 *
839 * Appends a next sibling to a list of computed values.
840 *
841 * Returns 0 if succeeded and -1 on API errors.
842 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000843int
844xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000845
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000846 if ((prev == NULL) || (cur == NULL))
847 return (-1);
848 prev->next = cur;
849 return (0);
850}
851
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000852/**
853 * xmlSchemaValueGetNext:
854 * @cur: the value
855 *
856 * Accessor for the next sibling of a list of computed values.
857 *
858 * Returns the next value or NULL if there was none, or on
859 * API errors.
860 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000861xmlSchemaValPtr
862xmlSchemaValueGetNext(xmlSchemaValPtr cur) {
863
864 if (cur == NULL)
865 return (NULL);
866 return (cur->next);
867}
868
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000869/**
870 * xmlSchemaValueGetAsString:
871 * @val: the value
872 *
873 * Accessor for the string value of a computed value.
874 *
875 * Returns the string value or NULL if there was none, or on
876 * API errors.
877 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000878const xmlChar *
879xmlSchemaValueGetAsString(xmlSchemaValPtr val)
880{
881 if (val == NULL)
882 return (NULL);
883 switch (val->type) {
884 case XML_SCHEMAS_STRING:
885 case XML_SCHEMAS_NORMSTRING:
886 case XML_SCHEMAS_ANYSIMPLETYPE:
887 case XML_SCHEMAS_TOKEN:
888 case XML_SCHEMAS_LANGUAGE:
889 case XML_SCHEMAS_NMTOKEN:
890 case XML_SCHEMAS_NAME:
891 case XML_SCHEMAS_NCNAME:
892 case XML_SCHEMAS_ID:
893 case XML_SCHEMAS_IDREF:
894 case XML_SCHEMAS_ENTITY:
895 case XML_SCHEMAS_ANYURI:
896 return (BAD_CAST val->value.str);
897 default:
898 break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000899 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000900 return (NULL);
901}
902
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000903/**
904 * xmlSchemaValueGetAsBoolean:
905 * @val: the value
906 *
907 * Accessor for the boolean value of a computed value.
908 *
909 * Returns 1 if true and 0 if false, or in case of an error. Hmm.
910 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000911int
912xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val)
913{
914 if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN))
915 return (0);
916 return (val->value.b);
Daniel Veillard4255d502002-04-16 15:50:10 +0000917}
918
919/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000920 * xmlSchemaNewStringValue:
921 * @type: the value type
Daniel Veillardb5839c32005-02-19 18:27:14 +0000922 * @value: the value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000923 *
924 * Allocate a new simple type value. The type can be
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +0000925 * of XML_SCHEMAS_STRING.
926 * WARNING: This one is intended to be expanded for other
927 * string based types. We need this for anySimpleType as well.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000928 * The given value is consumed and freed with the struct.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000929 *
930 * Returns a pointer to the new value or NULL in case of error
931 */
932xmlSchemaValPtr
933xmlSchemaNewStringValue(xmlSchemaValType type,
934 const xmlChar *value)
935{
936 xmlSchemaValPtr val;
937
938 if (type != XML_SCHEMAS_STRING)
939 return(NULL);
940 val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
941 if (val == NULL) {
942 return(NULL);
943 }
944 memset(val, 0, sizeof(xmlSchemaVal));
945 val->type = type;
946 val->value.str = (xmlChar *) value;
947 return(val);
948}
949
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000950/**
951 * xmlSchemaNewNOTATIONValue:
Daniel Veillardb5839c32005-02-19 18:27:14 +0000952 * @name: the notation name
953 * @ns: the notation namespace name or NULL
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000954 *
955 * Allocate a new NOTATION value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000956 * The given values are consumed and freed with the struct.
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000957 *
958 * Returns a pointer to the new value or NULL in case of error
959 */
960xmlSchemaValPtr
961xmlSchemaNewNOTATIONValue(const xmlChar *name,
962 const xmlChar *ns)
963{
964 xmlSchemaValPtr val;
965
966 val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
967 if (val == NULL)
968 return (NULL);
969
William M. Brack12d37ab2005-02-21 13:54:07 +0000970 val->value.qname.name = (xmlChar *)name;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000971 if (ns != NULL)
William M. Brack12d37ab2005-02-21 13:54:07 +0000972 val->value.qname.uri = (xmlChar *)ns;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000973 return(val);
974}
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000975
976/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000977 * xmlSchemaNewQNameValue:
978 * @namespaceName: the namespace name
979 * @localName: the local name
980 *
981 * Allocate a new QName value.
982 * The given values are consumed and freed with the struct.
983 *
984 * Returns a pointer to the new value or NULL in case of an error.
985 */
986xmlSchemaValPtr
987xmlSchemaNewQNameValue(const xmlChar *namespaceName,
988 const xmlChar *localName)
989{
990 xmlSchemaValPtr val;
991
992 val = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
993 if (val == NULL)
994 return (NULL);
995
996 val->value.qname.name = (xmlChar *) localName;
997 val->value.qname.uri = (xmlChar *) namespaceName;
998 return(val);
999}
1000
1001/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001002 * xmlSchemaFreeValue:
1003 * @value: the value to free
1004 *
1005 * Cleanup the default XML Schemas type library
1006 */
1007void
1008xmlSchemaFreeValue(xmlSchemaValPtr value) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001009 xmlSchemaValPtr prev;
1010
1011 while (value != NULL) {
1012 switch (value->type) {
1013 case XML_SCHEMAS_STRING:
1014 case XML_SCHEMAS_NORMSTRING:
1015 case XML_SCHEMAS_TOKEN:
1016 case XML_SCHEMAS_LANGUAGE:
1017 case XML_SCHEMAS_NMTOKEN:
1018 case XML_SCHEMAS_NMTOKENS:
1019 case XML_SCHEMAS_NAME:
1020 case XML_SCHEMAS_NCNAME:
1021 case XML_SCHEMAS_ID:
1022 case XML_SCHEMAS_IDREF:
1023 case XML_SCHEMAS_IDREFS:
1024 case XML_SCHEMAS_ENTITY:
1025 case XML_SCHEMAS_ENTITIES:
1026 case XML_SCHEMAS_ANYURI:
1027 case XML_SCHEMAS_ANYSIMPLETYPE:
1028 if (value->value.str != NULL)
1029 xmlFree(value->value.str);
1030 break;
1031 case XML_SCHEMAS_NOTATION:
1032 case XML_SCHEMAS_QNAME:
1033 if (value->value.qname.uri != NULL)
1034 xmlFree(value->value.qname.uri);
1035 if (value->value.qname.name != NULL)
1036 xmlFree(value->value.qname.name);
1037 break;
1038 case XML_SCHEMAS_HEXBINARY:
1039 if (value->value.hex.str != NULL)
1040 xmlFree(value->value.hex.str);
1041 break;
1042 case XML_SCHEMAS_BASE64BINARY:
1043 if (value->value.base64.str != NULL)
1044 xmlFree(value->value.base64.str);
1045 break;
1046 default:
1047 break;
1048 }
1049 prev = value;
1050 value = value->next;
1051 xmlFree(prev);
1052 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001053}
1054
1055/**
1056 * xmlSchemaGetPredefinedType:
1057 * @name: the type name
1058 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
1059 *
1060 * Lookup a type in the default XML Schemas type library
1061 *
1062 * Returns the type if found, NULL otherwise
1063 */
1064xmlSchemaTypePtr
1065xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
1066 if (xmlSchemaTypesInitialized == 0)
1067 xmlSchemaInitTypes();
1068 if (name == NULL)
1069 return(NULL);
1070 return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
1071}
Daniel Veillard070803b2002-05-03 07:29:38 +00001072
Daniel Veillard01fa6152004-06-29 17:04:39 +00001073/**
1074 * xmlSchemaGetBuiltInListSimpleTypeItemType:
1075 * @type: the built-in simple type.
1076 *
Daniel Veillard6927b102004-10-27 17:29:04 +00001077 * Lookup function
1078 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00001079 * Returns the item type of @type as defined by the built-in datatype
1080 * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error.
Daniel Veillard01fa6152004-06-29 17:04:39 +00001081 */
1082xmlSchemaTypePtr
1083xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type)
1084{
Daniel Veillard42595322004-11-08 10:52:06 +00001085 if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC))
Daniel Veillard01fa6152004-06-29 17:04:39 +00001086 return (NULL);
1087 switch (type->builtInType) {
1088 case XML_SCHEMAS_NMTOKENS:
1089 return (xmlSchemaTypeNmtokenDef );
1090 case XML_SCHEMAS_IDREFS:
1091 return (xmlSchemaTypeIdrefDef);
1092 case XML_SCHEMAS_ENTITIES:
1093 return (xmlSchemaTypeEntityDef);
1094 default:
1095 return (NULL);
1096 }
1097}
1098
Daniel Veillard070803b2002-05-03 07:29:38 +00001099/****************************************************************
1100 * *
1101 * Convenience macros and functions *
1102 * *
1103 ****************************************************************/
1104
1105#define IS_TZO_CHAR(c) \
1106 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
1107
1108#define VALID_YEAR(yr) (yr != 0)
1109#define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
1110/* VALID_DAY should only be used when month is unknown */
1111#define VALID_DAY(day) ((day >= 1) && (day <= 31))
1112#define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
1113#define VALID_MIN(min) ((min >= 0) && (min <= 59))
1114#define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
Kasimier T. Buchcik690a6802005-05-12 13:16:01 +00001115#define VALID_TZO(tzo) ((tzo > -840) && (tzo < 840))
Daniel Veillard070803b2002-05-03 07:29:38 +00001116#define IS_LEAP(y) \
1117 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
1118
Daniel Veillardebe25d42004-03-25 09:35:49 +00001119static const unsigned int daysInMonth[12] =
Daniel Veillard070803b2002-05-03 07:29:38 +00001120 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Daniel Veillardebe25d42004-03-25 09:35:49 +00001121static const unsigned int daysInMonthLeap[12] =
Daniel Veillard070803b2002-05-03 07:29:38 +00001122 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1123
Daniel Veillard5a872412002-05-22 06:40:27 +00001124#define MAX_DAYINMONTH(yr,mon) \
1125 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
1126
Daniel Veillard070803b2002-05-03 07:29:38 +00001127#define VALID_MDAY(dt) \
1128 (IS_LEAP(dt->year) ? \
1129 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
1130 (dt->day <= daysInMonth[dt->mon - 1]))
1131
1132#define VALID_DATE(dt) \
1133 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
1134
1135#define VALID_TIME(dt) \
1136 (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
1137 VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
1138
1139#define VALID_DATETIME(dt) \
1140 (VALID_DATE(dt) && VALID_TIME(dt))
1141
1142#define SECS_PER_MIN (60)
1143#define SECS_PER_HOUR (60 * SECS_PER_MIN)
1144#define SECS_PER_DAY (24 * SECS_PER_HOUR)
1145
Daniel Veillard5a872412002-05-22 06:40:27 +00001146static const long dayInYearByMonth[12] =
1147 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1148static const long dayInLeapYearByMonth[12] =
1149 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
1150
1151#define DAY_IN_YEAR(day, month, year) \
1152 ((IS_LEAP(year) ? \
1153 dayInLeapYearByMonth[month - 1] : \
1154 dayInYearByMonth[month - 1]) + day)
1155
1156#ifdef DEBUG
1157#define DEBUG_DATE(dt) \
1158 xmlGenericError(xmlGenericErrorContext, \
1159 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
1160 dt->type,dt->value.date.year,dt->value.date.mon, \
1161 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
1162 dt->value.date.sec); \
1163 if (dt->value.date.tz_flag) \
1164 if (dt->value.date.tzo != 0) \
1165 xmlGenericError(xmlGenericErrorContext, \
1166 "%+05d\n",dt->value.date.tzo); \
1167 else \
1168 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
1169 else \
1170 xmlGenericError(xmlGenericErrorContext,"\n")
1171#else
1172#define DEBUG_DATE(dt)
1173#endif
1174
Daniel Veillard070803b2002-05-03 07:29:38 +00001175/**
1176 * _xmlSchemaParseGYear:
1177 * @dt: pointer to a date structure
1178 * @str: pointer to the string to analyze
1179 *
1180 * Parses a xs:gYear without time zone and fills in the appropriate
1181 * field of the @dt structure. @str is updated to point just after the
1182 * xs:gYear. It is supposed that @dt->year is big enough to contain
1183 * the year.
1184 *
1185 * Returns 0 or the error code
1186 */
1187static int
1188_xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
1189 const xmlChar *cur = *str, *firstChar;
1190 int isneg = 0, digcnt = 0;
1191
1192 if (((*cur < '0') || (*cur > '9')) &&
1193 (*cur != '-') && (*cur != '+'))
1194 return -1;
1195
1196 if (*cur == '-') {
1197 isneg = 1;
1198 cur++;
1199 }
1200
1201 firstChar = cur;
1202
1203 while ((*cur >= '0') && (*cur <= '9')) {
1204 dt->year = dt->year * 10 + (*cur - '0');
1205 cur++;
1206 digcnt++;
1207 }
1208
1209 /* year must be at least 4 digits (CCYY); over 4
1210 * digits cannot have a leading zero. */
1211 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
1212 return 1;
1213
1214 if (isneg)
1215 dt->year = - dt->year;
1216
1217 if (!VALID_YEAR(dt->year))
1218 return 2;
1219
1220 *str = cur;
1221 return 0;
1222}
1223
1224/**
1225 * PARSE_2_DIGITS:
1226 * @num: the integer to fill in
1227 * @cur: an #xmlChar *
1228 * @invalid: an integer
1229 *
1230 * Parses a 2-digits integer and updates @num with the value. @cur is
1231 * updated to point just after the integer.
1232 * In case of error, @invalid is set to %TRUE, values of @num and
1233 * @cur are undefined.
1234 */
1235#define PARSE_2_DIGITS(num, cur, invalid) \
1236 if ((cur[0] < '0') || (cur[0] > '9') || \
1237 (cur[1] < '0') || (cur[1] > '9')) \
1238 invalid = 1; \
1239 else \
1240 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
1241 cur += 2;
1242
1243/**
1244 * PARSE_FLOAT:
1245 * @num: the double to fill in
1246 * @cur: an #xmlChar *
1247 * @invalid: an integer
1248 *
1249 * Parses a float and updates @num with the value. @cur is
1250 * updated to point just after the float. The float must have a
1251 * 2-digits integer part and may or may not have a decimal part.
1252 * In case of error, @invalid is set to %TRUE, values of @num and
1253 * @cur are undefined.
1254 */
1255#define PARSE_FLOAT(num, cur, invalid) \
1256 PARSE_2_DIGITS(num, cur, invalid); \
1257 if (!invalid && (*cur == '.')) { \
1258 double mult = 1; \
1259 cur++; \
1260 if ((*cur < '0') || (*cur > '9')) \
1261 invalid = 1; \
1262 while ((*cur >= '0') && (*cur <= '9')) { \
1263 mult /= 10; \
1264 num += (*cur - '0') * mult; \
1265 cur++; \
1266 } \
1267 }
1268
1269/**
1270 * _xmlSchemaParseGMonth:
1271 * @dt: pointer to a date structure
1272 * @str: pointer to the string to analyze
1273 *
1274 * Parses a xs:gMonth without time zone and fills in the appropriate
1275 * field of the @dt structure. @str is updated to point just after the
1276 * xs:gMonth.
1277 *
1278 * Returns 0 or the error code
1279 */
1280static int
1281_xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
1282 const xmlChar *cur = *str;
1283 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001284 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001285
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001286 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001287 if (ret != 0)
1288 return ret;
1289
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001290 if (!VALID_MONTH(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001291 return 2;
1292
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001293 dt->mon = value;
1294
Daniel Veillard070803b2002-05-03 07:29:38 +00001295 *str = cur;
1296 return 0;
1297}
1298
1299/**
1300 * _xmlSchemaParseGDay:
1301 * @dt: pointer to a date structure
1302 * @str: pointer to the string to analyze
1303 *
1304 * Parses a xs:gDay without time zone and fills in the appropriate
1305 * field of the @dt structure. @str is updated to point just after the
1306 * xs:gDay.
1307 *
1308 * Returns 0 or the error code
1309 */
1310static int
1311_xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
1312 const xmlChar *cur = *str;
1313 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001314 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001315
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001316 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001317 if (ret != 0)
1318 return ret;
1319
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001320 if (!VALID_DAY(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001321 return 2;
1322
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001323 dt->day = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001324 *str = cur;
1325 return 0;
1326}
1327
1328/**
1329 * _xmlSchemaParseTime:
1330 * @dt: pointer to a date structure
1331 * @str: pointer to the string to analyze
1332 *
1333 * Parses a xs:time without time zone and fills in the appropriate
1334 * fields of the @dt structure. @str is updated to point just after the
1335 * xs:time.
1336 * In case of error, values of @dt fields are undefined.
1337 *
1338 * Returns 0 or the error code
1339 */
1340static int
1341_xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +00001342 const xmlChar *cur = *str;
Daniel Veillard070803b2002-05-03 07:29:38 +00001343 int ret = 0;
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +00001344 int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001345
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001346 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001347 if (ret != 0)
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001348 return ret;
Daniel Veillard070803b2002-05-03 07:29:38 +00001349 if (*cur != ':')
1350 return 1;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001351 if (!VALID_HOUR(value))
1352 return 2;
Daniel Veillard070803b2002-05-03 07:29:38 +00001353 cur++;
1354
1355 /* the ':' insures this string is xs:time */
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001356 dt->hour = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001357
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001358 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001359 if (ret != 0)
1360 return ret;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001361 if (!VALID_MIN(value))
1362 return 2;
1363 dt->min = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001364
1365 if (*cur != ':')
1366 return 1;
1367 cur++;
1368
1369 PARSE_FLOAT(dt->sec, cur, ret);
1370 if (ret != 0)
1371 return ret;
1372
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001373 if ((!VALID_SEC(dt->sec)) || (!VALID_TZO(dt->tzo)))
Daniel Veillard070803b2002-05-03 07:29:38 +00001374 return 2;
1375
1376 *str = cur;
1377 return 0;
1378}
1379
1380/**
1381 * _xmlSchemaParseTimeZone:
1382 * @dt: pointer to a date structure
1383 * @str: pointer to the string to analyze
1384 *
1385 * Parses a time zone without time zone and fills in the appropriate
1386 * field of the @dt structure. @str is updated to point just after the
1387 * time zone.
1388 *
1389 * Returns 0 or the error code
1390 */
1391static int
1392_xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
1393 const xmlChar *cur = *str;
1394 int ret = 0;
1395
1396 if (str == NULL)
1397 return -1;
1398
1399 switch (*cur) {
1400 case 0:
1401 dt->tz_flag = 0;
1402 dt->tzo = 0;
1403 break;
1404
1405 case 'Z':
1406 dt->tz_flag = 1;
1407 dt->tzo = 0;
1408 cur++;
1409 break;
1410
1411 case '+':
1412 case '-': {
1413 int isneg = 0, tmp = 0;
1414 isneg = (*cur == '-');
1415
1416 cur++;
1417
1418 PARSE_2_DIGITS(tmp, cur, ret);
1419 if (ret != 0)
1420 return ret;
1421 if (!VALID_HOUR(tmp))
1422 return 2;
1423
1424 if (*cur != ':')
1425 return 1;
1426 cur++;
1427
1428 dt->tzo = tmp * 60;
1429
1430 PARSE_2_DIGITS(tmp, cur, ret);
1431 if (ret != 0)
1432 return ret;
1433 if (!VALID_MIN(tmp))
1434 return 2;
1435
1436 dt->tzo += tmp;
1437 if (isneg)
1438 dt->tzo = - dt->tzo;
1439
1440 if (!VALID_TZO(dt->tzo))
1441 return 2;
1442
Daniel Veillard5a872412002-05-22 06:40:27 +00001443 dt->tz_flag = 1;
Daniel Veillard070803b2002-05-03 07:29:38 +00001444 break;
1445 }
1446 default:
1447 return 1;
1448 }
1449
1450 *str = cur;
1451 return 0;
1452}
1453
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001454/**
1455 * _xmlSchemaBase64Decode:
1456 * @ch: a character
1457 *
1458 * Converts a base64 encoded character to its base 64 value.
1459 *
1460 * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
1461 */
1462static int
1463_xmlSchemaBase64Decode (const xmlChar ch) {
1464 if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
1465 if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
1466 if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
1467 if ('+' == ch) return 62;
1468 if ('/' == ch) return 63;
1469 if ('=' == ch) return 64;
1470 return -1;
1471}
1472
Daniel Veillard070803b2002-05-03 07:29:38 +00001473/****************************************************************
1474 * *
1475 * XML Schema Dates/Times Datatypes Handling *
1476 * *
1477 ****************************************************************/
1478
1479/**
1480 * PARSE_DIGITS:
1481 * @num: the integer to fill in
1482 * @cur: an #xmlChar *
1483 * @num_type: an integer flag
1484 *
1485 * Parses a digits integer and updates @num with the value. @cur is
1486 * updated to point just after the integer.
1487 * In case of error, @num_type is set to -1, values of @num and
1488 * @cur are undefined.
1489 */
1490#define PARSE_DIGITS(num, cur, num_type) \
1491 if ((*cur < '0') || (*cur > '9')) \
1492 num_type = -1; \
1493 else \
1494 while ((*cur >= '0') && (*cur <= '9')) { \
1495 num = num * 10 + (*cur - '0'); \
1496 cur++; \
1497 }
1498
1499/**
1500 * PARSE_NUM:
1501 * @num: the double to fill in
1502 * @cur: an #xmlChar *
1503 * @num_type: an integer flag
1504 *
1505 * Parses a float or integer and updates @num with the value. @cur is
1506 * updated to point just after the number. If the number is a float,
1507 * then it must have an integer part and a decimal part; @num_type will
1508 * be set to 1. If there is no decimal part, @num_type is set to zero.
1509 * In case of error, @num_type is set to -1, values of @num and
1510 * @cur are undefined.
1511 */
1512#define PARSE_NUM(num, cur, num_type) \
1513 num = 0; \
1514 PARSE_DIGITS(num, cur, num_type); \
1515 if (!num_type && (*cur == '.')) { \
1516 double mult = 1; \
1517 cur++; \
1518 if ((*cur < '0') || (*cur > '9')) \
1519 num_type = -1; \
1520 else \
1521 num_type = 1; \
1522 while ((*cur >= '0') && (*cur <= '9')) { \
1523 mult /= 10; \
1524 num += (*cur - '0') * mult; \
1525 cur++; \
1526 } \
1527 }
1528
1529/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001530 * xmlSchemaValidateDates:
Daniel Veillard455cc072003-03-31 10:13:23 +00001531 * @type: the expected type or XML_SCHEMAS_UNKNOWN
Daniel Veillard070803b2002-05-03 07:29:38 +00001532 * @dateTime: string to analyze
1533 * @val: the return computed value
1534 *
1535 * Check that @dateTime conforms to the lexical space of one of the date types.
1536 * if true a value is computed and returned in @val.
1537 *
1538 * Returns 0 if this validates, a positive error code number otherwise
1539 * and -1 in case of internal or API error.
1540 */
1541static int
Daniel Veillard455cc072003-03-31 10:13:23 +00001542xmlSchemaValidateDates (xmlSchemaValType type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001543 const xmlChar *dateTime, xmlSchemaValPtr *val,
1544 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001545 xmlSchemaValPtr dt;
1546 int ret;
1547 const xmlChar *cur = dateTime;
1548
1549#define RETURN_TYPE_IF_VALID(t) \
1550 if (IS_TZO_CHAR(*cur)) { \
1551 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
1552 if (ret == 0) { \
1553 if (*cur != 0) \
1554 goto error; \
1555 dt->type = t; \
Daniel Veillard455cc072003-03-31 10:13:23 +00001556 goto done; \
Daniel Veillard070803b2002-05-03 07:29:38 +00001557 } \
1558 }
1559
1560 if (dateTime == NULL)
1561 return -1;
1562
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001563 if (collapse)
1564 while IS_WSP_BLANK_CH(*cur) cur++;
1565
Daniel Veillard070803b2002-05-03 07:29:38 +00001566 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
1567 return 1;
1568
1569 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
1570 if (dt == NULL)
1571 return -1;
1572
1573 if ((cur[0] == '-') && (cur[1] == '-')) {
1574 /*
1575 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
1576 * xs:gDay)
1577 */
1578 cur += 2;
1579
1580 /* is it an xs:gDay? */
1581 if (*cur == '-') {
Daniel Veillard455cc072003-03-31 10:13:23 +00001582 if (type == XML_SCHEMAS_GMONTH)
1583 goto error;
Daniel Veillard070803b2002-05-03 07:29:38 +00001584 ++cur;
1585 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1586 if (ret != 0)
1587 goto error;
1588
1589 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
1590
1591 goto error;
1592 }
1593
1594 /*
1595 * it should be an xs:gMonthDay or xs:gMonth
1596 */
1597 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1598 if (ret != 0)
1599 goto error;
1600
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001601 /*
1602 * a '-' char could indicate this type is xs:gMonthDay or
1603 * a negative time zone offset. Check for xs:gMonthDay first.
1604 * Also the first three char's of a negative tzo (-MM:SS) can
1605 * appear to be a valid day; so even if the day portion
1606 * of the xs:gMonthDay verifies, we must insure it was not
1607 * a tzo.
1608 */
1609 if (*cur == '-') {
1610 const xmlChar *rewnd = cur;
1611 cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001612
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001613 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1614 if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
1615
1616 /*
1617 * we can use the VALID_MDAY macro to validate the month
1618 * and day because the leap year test will flag year zero
1619 * as a leap year (even though zero is an invalid year).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001620 * FUTURE TODO: Zero will become valid in XML Schema 1.1
1621 * probably.
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001622 */
1623 if (VALID_MDAY((&(dt->value.date)))) {
1624
1625 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
1626
1627 goto error;
1628 }
1629 }
1630
1631 /*
1632 * not xs:gMonthDay so rewind and check if just xs:gMonth
1633 * with an optional time zone.
1634 */
1635 cur = rewnd;
1636 }
1637
1638 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
Daniel Veillard070803b2002-05-03 07:29:38 +00001639
1640 goto error;
1641 }
1642
1643 /*
1644 * It's a right-truncated date or an xs:time.
1645 * Try to parse an xs:time then fallback on right-truncated dates.
1646 */
1647 if ((*cur >= '0') && (*cur <= '9')) {
1648 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1649 if (ret == 0) {
1650 /* it's an xs:time */
1651 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
1652 }
1653 }
1654
1655 /* fallback on date parsing */
1656 cur = dateTime;
1657
1658 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
1659 if (ret != 0)
1660 goto error;
1661
1662 /* is it an xs:gYear? */
1663 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
1664
1665 if (*cur != '-')
1666 goto error;
1667 cur++;
1668
1669 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1670 if (ret != 0)
1671 goto error;
1672
1673 /* is it an xs:gYearMonth? */
1674 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
1675
1676 if (*cur != '-')
1677 goto error;
1678 cur++;
1679
1680 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1681 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
1682 goto error;
1683
1684 /* is it an xs:date? */
1685 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
1686
1687 if (*cur != 'T')
1688 goto error;
1689 cur++;
1690
1691 /* it should be an xs:dateTime */
1692 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1693 if (ret != 0)
1694 goto error;
1695
1696 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001697 if (collapse)
1698 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00001699 if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date))))))
Daniel Veillard070803b2002-05-03 07:29:38 +00001700 goto error;
1701
Daniel Veillard455cc072003-03-31 10:13:23 +00001702
Daniel Veillard070803b2002-05-03 07:29:38 +00001703 dt->type = XML_SCHEMAS_DATETIME;
1704
Daniel Veillard455cc072003-03-31 10:13:23 +00001705done:
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001706#if 1
1707 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
1708 goto error;
1709#else
1710 /*
1711 * insure the parsed type is equal to or less significant (right
1712 * truncated) than the desired type.
1713 */
1714 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
1715
1716 /* time only matches time */
1717 if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
1718 goto error;
1719
1720 if ((type == XML_SCHEMAS_DATETIME) &&
1721 ((dt->type != XML_SCHEMAS_DATE) ||
1722 (dt->type != XML_SCHEMAS_GYEARMONTH) ||
1723 (dt->type != XML_SCHEMAS_GYEAR)))
1724 goto error;
1725
1726 if ((type == XML_SCHEMAS_DATE) &&
1727 ((dt->type != XML_SCHEMAS_GYEAR) ||
1728 (dt->type != XML_SCHEMAS_GYEARMONTH)))
1729 goto error;
1730
1731 if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
1732 goto error;
1733
1734 if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
1735 goto error;
1736 }
Daniel Veillard455cc072003-03-31 10:13:23 +00001737#endif
1738
Daniel Veillard070803b2002-05-03 07:29:38 +00001739 if (val != NULL)
1740 *val = dt;
Daniel Veillard80b19092003-03-28 13:29:53 +00001741 else
1742 xmlSchemaFreeValue(dt);
Daniel Veillard070803b2002-05-03 07:29:38 +00001743
1744 return 0;
1745
1746error:
1747 if (dt != NULL)
1748 xmlSchemaFreeValue(dt);
1749 return 1;
1750}
1751
1752/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001753 * xmlSchemaValidateDuration:
Daniel Veillard070803b2002-05-03 07:29:38 +00001754 * @type: the predefined type
1755 * @duration: string to analyze
1756 * @val: the return computed value
1757 *
1758 * Check that @duration conforms to the lexical space of the duration type.
1759 * if true a value is computed and returned in @val.
1760 *
1761 * Returns 0 if this validates, a positive error code number otherwise
1762 * and -1 in case of internal or API error.
1763 */
1764static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00001765xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001766 const xmlChar *duration, xmlSchemaValPtr *val,
1767 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001768 const xmlChar *cur = duration;
1769 xmlSchemaValPtr dur;
1770 int isneg = 0;
1771 unsigned int seq = 0;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001772 double num;
1773 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
1774 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1775 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
Daniel Veillard070803b2002-05-03 07:29:38 +00001776
1777 if (duration == NULL)
1778 return -1;
1779
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001780 if (collapse)
1781 while IS_WSP_BLANK_CH(*cur) cur++;
1782
Daniel Veillard070803b2002-05-03 07:29:38 +00001783 if (*cur == '-') {
1784 isneg = 1;
1785 cur++;
1786 }
1787
1788 /* duration must start with 'P' (after sign) */
1789 if (*cur++ != 'P')
1790 return 1;
1791
Daniel Veillard80b19092003-03-28 13:29:53 +00001792 if (*cur == 0)
1793 return 1;
1794
Daniel Veillard070803b2002-05-03 07:29:38 +00001795 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1796 if (dur == NULL)
1797 return -1;
1798
1799 while (*cur != 0) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001800
1801 /* input string should be empty or invalid date/time item */
1802 if (seq >= sizeof(desig))
1803 goto error;
1804
1805 /* T designator must be present for time items */
1806 if (*cur == 'T') {
1807 if (seq <= 3) {
1808 seq = 3;
1809 cur++;
1810 } else
1811 return 1;
1812 } else if (seq == 3)
1813 goto error;
1814
1815 /* parse the number portion of the item */
1816 PARSE_NUM(num, cur, num_type);
1817
1818 if ((num_type == -1) || (*cur == 0))
1819 goto error;
1820
1821 /* update duration based on item type */
1822 while (seq < sizeof(desig)) {
1823 if (*cur == desig[seq]) {
1824
1825 /* verify numeric type; only seconds can be float */
1826 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1827 goto error;
1828
1829 switch (seq) {
1830 case 0:
1831 dur->value.dur.mon = (long)num * 12;
1832 break;
1833 case 1:
1834 dur->value.dur.mon += (long)num;
1835 break;
1836 default:
1837 /* convert to seconds using multiplier */
1838 dur->value.dur.sec += num * multi[seq];
1839 seq++;
1840 break;
1841 }
1842
1843 break; /* exit loop */
1844 }
1845 /* no date designators found? */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00001846 if ((++seq == 3) || (seq == 6))
Daniel Veillard070803b2002-05-03 07:29:38 +00001847 goto error;
1848 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001849 cur++;
1850 if (collapse)
1851 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001852 }
1853
1854 if (isneg) {
1855 dur->value.dur.mon = -dur->value.dur.mon;
1856 dur->value.dur.day = -dur->value.dur.day;
1857 dur->value.dur.sec = -dur->value.dur.sec;
1858 }
1859
1860 if (val != NULL)
1861 *val = dur;
Daniel Veillard80b19092003-03-28 13:29:53 +00001862 else
1863 xmlSchemaFreeValue(dur);
Daniel Veillard070803b2002-05-03 07:29:38 +00001864
1865 return 0;
1866
1867error:
1868 if (dur != NULL)
1869 xmlSchemaFreeValue(dur);
1870 return 1;
1871}
1872
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001873/**
1874 * xmlSchemaStrip:
1875 * @value: a value
1876 *
1877 * Removes the leading and ending spaces of a string
1878 *
1879 * Returns the new string or NULL if no change was required.
1880 */
1881static xmlChar *
1882xmlSchemaStrip(const xmlChar *value) {
1883 const xmlChar *start = value, *end, *f;
1884
1885 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001886 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001887 end = start;
1888 while (*end != 0) end++;
1889 f = end;
1890 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001891 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001892 end++;
1893 if ((start == value) && (f == end)) return(NULL);
1894 return(xmlStrndup(start, end - start));
1895}
Daniel Veillard96a4b252003-02-06 08:22:32 +00001896
1897/**
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001898 * xmlSchemaWhiteSpaceReplace:
1899 * @value: a value
1900 *
1901 * Replaces 0xd, 0x9 and 0xa with a space.
1902 *
1903 * Returns the new string or NULL if no change was required.
1904 */
1905xmlChar *
1906xmlSchemaWhiteSpaceReplace(const xmlChar *value) {
1907 const xmlChar *cur = value;
1908 xmlChar *ret = NULL, *mcur;
1909
1910 if (value == NULL)
1911 return(NULL);
1912
1913 while ((*cur != 0) &&
1914 (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) {
1915 cur++;
1916 }
1917 if (*cur == 0)
1918 return (NULL);
1919 ret = xmlStrdup(value);
1920 /* TODO FIXME: I guess gcc will bark at this. */
1921 mcur = (xmlChar *) (ret + (cur - value));
1922 do {
1923 if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) )
1924 *mcur = ' ';
1925 mcur++;
1926 } while (*mcur != 0);
1927 return(ret);
1928}
1929
1930/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001931 * xmlSchemaCollapseString:
1932 * @value: a value
1933 *
1934 * Removes and normalize white spaces in the string
1935 *
1936 * Returns the new string or NULL if no change was required.
1937 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00001938xmlChar *
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001939xmlSchemaCollapseString(const xmlChar *value) {
1940 const xmlChar *start = value, *end, *f;
1941 xmlChar *g;
1942 int col = 0;
1943
1944 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001945 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001946 end = start;
1947 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001948 if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001949 col = end - start;
1950 break;
1951 } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
1952 col = end - start;
1953 break;
1954 }
1955 end++;
1956 }
1957 if (col == 0) {
1958 f = end;
1959 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001960 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001961 end++;
1962 if ((start == value) && (f == end)) return(NULL);
1963 return(xmlStrndup(start, end - start));
1964 }
1965 start = xmlStrdup(start);
1966 if (start == NULL) return(NULL);
1967 g = (xmlChar *) (start + col);
1968 end = g;
1969 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001970 if (IS_BLANK_CH(*end)) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001971 end++;
William M. Brack76e95df2003-10-18 16:20:14 +00001972 while (IS_BLANK_CH(*end)) end++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001973 if (*end != 0)
1974 *g++ = ' ';
1975 } else
1976 *g++ = *end++;
1977 }
1978 *g = 0;
1979 return((xmlChar *) start);
1980}
1981
1982/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001983 * xmlSchemaValAtomicListNode:
1984 * @type: the predefined atomic type for a token in the list
1985 * @value: the list value to check
1986 * @ret: the return computed value
1987 * @node: the node containing the value
1988 *
1989 * Check that a value conforms to the lexical space of the predefined
1990 * list type. if true a value is computed and returned in @ret.
1991 *
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001992 * Returns the number of items if this validates, a negative error code
1993 * number otherwise
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001994 */
1995static int
1996xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
1997 xmlSchemaValPtr *ret, xmlNodePtr node) {
1998 xmlChar *val, *cur, *endval;
1999 int nb_values = 0;
Daniel Veillard580ced82003-03-21 21:22:48 +00002000 int tmp = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002001
2002 if (value == NULL) {
2003 return(-1);
2004 }
2005 val = xmlStrdup(value);
2006 if (val == NULL) {
2007 return(-1);
2008 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002009 if (ret != NULL) {
2010 *ret = NULL;
2011 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002012 cur = val;
2013 /*
2014 * Split the list
2015 */
William M. Brack76e95df2003-10-18 16:20:14 +00002016 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002017 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00002018 if (IS_BLANK_CH(*cur)) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002019 *cur = 0;
2020 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00002021 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002022 } else {
2023 nb_values++;
2024 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00002025 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002026 }
2027 }
2028 if (nb_values == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002029 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002030 return(nb_values);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002031 }
2032 endval = cur;
2033 cur = val;
2034 while ((*cur == 0) && (cur != endval)) cur++;
2035 while (cur != endval) {
2036 tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
2037 if (tmp != 0)
2038 break;
2039 while (*cur != 0) cur++;
2040 while ((*cur == 0) && (cur != endval)) cur++;
2041 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002042 /* TODO what return value ? c.f. bug #158628
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002043 if (ret != NULL) {
2044 TODO
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002045 } */
2046 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002047 if (tmp == 0)
2048 return(nb_values);
2049 return(-1);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002050}
2051
2052/**
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002053 * xmlSchemaParseUInt:
2054 * @str: pointer to the string R/W
2055 * @llo: pointer to the low result
2056 * @lmi: pointer to the mid result
2057 * @lhi: pointer to the high result
2058 *
2059 * Parse an unsigned long into 3 fields.
2060 *
William M. Brackec3b4b72005-03-15 15:50:17 +00002061 * Returns the number of significant digits in the number or
2062 * -1 if overflow of the capacity
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002063 */
2064static int
2065xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002066 unsigned long *lmi, unsigned long *lhi) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002067 unsigned long lo = 0, mi = 0, hi = 0;
2068 const xmlChar *tmp, *cur = *str;
2069 int ret = 0, i = 0;
2070
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002071 while (*cur == '0') { /* ignore leading zeroes */
2072 cur++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002073 }
2074 tmp = cur;
2075 while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002076 i++;tmp++;ret++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002077 }
2078 if (i > 24) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002079 *str = tmp;
2080 return(-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002081 }
2082 while (i > 16) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002083 hi = hi * 10 + (*cur++ - '0');
2084 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002085 }
2086 while (i > 8) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002087 mi = mi * 10 + (*cur++ - '0');
2088 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002089 }
2090 while (i > 0) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002091 lo = lo * 10 + (*cur++ - '0');
2092 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002093 }
2094
2095 *str = cur;
2096 *llo = lo;
2097 *lmi = mi;
2098 *lhi = hi;
2099 return(ret);
2100}
2101
2102/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002103 * xmlSchemaValAtomicType:
2104 * @type: the predefined type
2105 * @value: the value to check
2106 * @val: the return computed value
2107 * @node: the node containing the value
2108 * flags: flags to control the vlidation
2109 *
2110 * Check that a value conforms to the lexical space of the atomic type.
2111 * if true a value is computed and returned in @val.
Daniel Veillard01fa6152004-06-29 17:04:39 +00002112 * This checks the value space for list types as well (IDREFS, NMTOKENS).
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002113 *
2114 * Returns 0 if this validates, a positive error code number otherwise
2115 * and -1 in case of internal or API error.
2116 */
2117static int
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002118xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002119 xmlSchemaValPtr * val, xmlNodePtr node, int flags,
2120 xmlSchemaWhitespaceValueType ws,
2121 int normOnTheFly, int applyNorm, int createStringValue)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002122{
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002123 xmlSchemaValPtr v;
2124 xmlChar *norm = NULL;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00002125 int ret = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002126
2127 if (xmlSchemaTypesInitialized == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00002128 xmlSchemaInitTypes();
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002129 if (type == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002130 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002131
Daniel Veillardeebd6332004-08-26 10:30:44 +00002132 /*
2133 * validating a non existant text node is similar to validating
2134 * an empty one.
2135 */
2136 if (value == NULL)
2137 value = BAD_CAST "";
2138
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002139 if (val != NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002140 *val = NULL;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002141 if ((flags == 0) && (value != NULL)) {
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00002142
Daniel Veillard01fa6152004-06-29 17:04:39 +00002143 if ((type->builtInType != XML_SCHEMAS_STRING) &&
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00002144 (type->builtInType != XML_SCHEMAS_ANYTYPE) &&
2145 (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) {
2146 if (type->builtInType == XML_SCHEMAS_NORMSTRING)
2147 norm = xmlSchemaWhiteSpaceReplace(value);
2148 else
2149 norm = xmlSchemaCollapseString(value);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002150 if (norm != NULL)
2151 value = norm;
2152 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002153 }
2154
Daniel Veillard01fa6152004-06-29 17:04:39 +00002155 switch (type->builtInType) {
William M. Brack2f2a6632004-08-20 23:09:47 +00002156 case XML_SCHEMAS_UNKNOWN:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002157 goto error;
William M. Brack2f2a6632004-08-20 23:09:47 +00002158 case XML_SCHEMAS_ANYTYPE:
2159 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002160 if ((createStringValue) && (val != NULL)) {
2161 v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE);
2162 if (v != NULL) {
2163 v->value.str = xmlStrdup(value);
2164 *val = v;
2165 } else {
2166 goto error;
2167 }
2168 }
William M. Brack2f2a6632004-08-20 23:09:47 +00002169 goto return0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002170 case XML_SCHEMAS_STRING:
2171 if (! normOnTheFly) {
2172 const xmlChar *cur = value;
2173
2174 if (ws == XML_SCHEMA_WHITESPACE_REPLACE) {
2175 while (*cur != 0) {
2176 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2177 goto return1;
2178 } else {
2179 cur++;
2180 }
2181 }
2182 } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
2183 while (*cur != 0) {
2184 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2185 goto return1;
2186 } else if IS_WSP_SPACE_CH(*cur) {
2187 cur++;
2188 if IS_WSP_SPACE_CH(*cur)
2189 goto return1;
2190 } else {
2191 cur++;
2192 }
2193 }
2194 }
2195 }
2196 if (createStringValue && (val != NULL)) {
2197 if (applyNorm) {
2198 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2199 norm = xmlSchemaCollapseString(value);
2200 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
2201 norm = xmlSchemaWhiteSpaceReplace(value);
2202 if (norm != NULL)
2203 value = norm;
2204 }
2205 v = xmlSchemaNewValue(XML_SCHEMAS_STRING);
2206 if (v != NULL) {
2207 v->value.str = xmlStrdup(value);
2208 *val = v;
2209 } else {
2210 goto error;
2211 }
2212 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002213 goto return0;
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002214 case XML_SCHEMAS_NORMSTRING:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002215 if (normOnTheFly) {
2216 if (applyNorm) {
2217 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2218 norm = xmlSchemaCollapseString(value);
2219 else
2220 norm = xmlSchemaWhiteSpaceReplace(value);
2221 if (norm != NULL)
2222 value = norm;
2223 }
2224 } else {
2225 const xmlChar *cur = value;
2226 while (*cur != 0) {
2227 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2228 goto return1;
2229 } else {
2230 cur++;
2231 }
2232 }
2233 }
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002234 if (val != NULL) {
2235 v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
2236 if (v != NULL) {
2237 v->value.str = xmlStrdup(value);
2238 *val = v;
2239 } else {
2240 goto error;
2241 }
2242 }
2243 goto return0;
2244 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002245 case XML_SCHEMAS_DECIMAL:{
William M. Brack273670f2005-03-11 15:55:14 +00002246 const xmlChar *cur = value;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002247 unsigned int len, neg, integ, hasLeadingZeroes;
William M. Brack273670f2005-03-11 15:55:14 +00002248 xmlChar cval[25];
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002249 xmlChar *cptr = cval;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002250
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002251 if ((cur == NULL) || (*cur == 0))
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002252 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002253
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002254 /*
2255 * xs:decimal has a whitespace-facet value of 'collapse'.
2256 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002257 if (normOnTheFly)
2258 while IS_WSP_BLANK_CH(*cur) cur++;
2259
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002260 /*
2261 * First we handle an optional sign.
2262 */
2263 neg = 0;
2264 if (*cur == '-') {
2265 neg = 1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002266 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002267 } else if (*cur == '+')
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002268 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002269 /*
2270 * Disallow: "", "-", "- "
2271 */
2272 if (*cur == 0)
2273 goto return1;
William M. Brack273670f2005-03-11 15:55:14 +00002274 /*
2275 * Next we "pre-parse" the number, in preparation for calling
2276 * the common routine xmlSchemaParseUInt. We get rid of any
2277 * leading zeroes (because we have reserved only 25 chars),
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002278 * and note the position of a decimal point.
William M. Brack273670f2005-03-11 15:55:14 +00002279 */
2280 len = 0;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002281 integ = ~0u;
2282 hasLeadingZeroes = 0;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002283 /*
2284 * Skip leading zeroes.
2285 */
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002286 while (*cur == '0') {
William M. Brack273670f2005-03-11 15:55:14 +00002287 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002288 hasLeadingZeroes = 1;
2289 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002290 if (*cur != 0) {
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002291 do {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002292 if ((*cur >= '0') && (*cur <= '9')) {
2293 *cptr++ = *cur++;
2294 len++;
2295 } else if (*cur == '.') {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002296 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002297 integ = len;
2298 do {
2299 if ((*cur >= '0') && (*cur <= '9')) {
2300 *cptr++ = *cur++;
2301 len++;
2302 } else
2303 break;
2304 } while (len < 24);
2305 /*
2306 * Disallow "." but allow "00."
2307 */
2308 if ((len == 0) && (!hasLeadingZeroes))
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002309 goto return1;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002310 break;
2311 } else
2312 break;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002313 } while (len < 24);
William M. Brack273670f2005-03-11 15:55:14 +00002314 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002315 if (normOnTheFly)
2316 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brack273670f2005-03-11 15:55:14 +00002317 if (*cur != 0)
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002318 goto return1; /* error if any extraneous chars */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002319 if (val != NULL) {
2320 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
2321 if (v != NULL) {
William M. Brack273670f2005-03-11 15:55:14 +00002322 /*
William M. Brack273670f2005-03-11 15:55:14 +00002323 * Now evaluate the significant digits of the number
2324 */
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002325 if (len != 0) {
2326
2327 if (integ != ~0u) {
2328 /*
2329 * Get rid of trailing zeroes in the
2330 * fractional part.
2331 */
2332 while ((len != integ) && (*(cptr-1) == '0')) {
2333 cptr--;
2334 len--;
2335 }
2336 }
2337 /*
2338 * Terminate the (preparsed) string.
2339 */
2340 if (len != 0) {
2341 *cptr = 0;
2342 cptr = cval;
2343
2344 xmlSchemaParseUInt((const xmlChar **)&cptr,
2345 &v->value.decimal.lo,
2346 &v->value.decimal.mi,
2347 &v->value.decimal.hi);
2348 }
2349 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002350 /*
2351 * Set the total digits to 1 if a zero value.
2352 */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002353 v->value.decimal.sign = neg;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002354 if (len == 0) {
2355 /* Speedup for zero values. */
2356 v->value.decimal.total = 1;
William M. Brack273670f2005-03-11 15:55:14 +00002357 } else {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002358 v->value.decimal.total = len;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002359 if (integ == ~0u)
2360 v->value.decimal.frac = 0;
2361 else
2362 v->value.decimal.frac = len - integ;
William M. Brack273670f2005-03-11 15:55:14 +00002363 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002364 *val = v;
2365 }
2366 }
2367 goto return0;
2368 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002369 case XML_SCHEMAS_TIME:
2370 case XML_SCHEMAS_GDAY:
2371 case XML_SCHEMAS_GMONTH:
2372 case XML_SCHEMAS_GMONTHDAY:
2373 case XML_SCHEMAS_GYEAR:
2374 case XML_SCHEMAS_GYEARMONTH:
2375 case XML_SCHEMAS_DATE:
2376 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002377 ret = xmlSchemaValidateDates(type->builtInType, value, val,
2378 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002379 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002380 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002381 ret = xmlSchemaValidateDuration(type, value, val,
2382 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002383 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002384 case XML_SCHEMAS_FLOAT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002385 case XML_SCHEMAS_DOUBLE:{
2386 const xmlChar *cur = value;
2387 int neg = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002388
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002389 if (cur == NULL)
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00002390 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002391
2392 if (normOnTheFly)
2393 while IS_WSP_BLANK_CH(*cur) cur++;
2394
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002395 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
2396 cur += 3;
2397 if (*cur != 0)
2398 goto return1;
2399 if (val != NULL) {
2400 if (type == xmlSchemaTypeFloatDef) {
2401 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2402 if (v != NULL) {
2403 v->value.f = (float) xmlXPathNAN;
2404 } else {
2405 xmlSchemaFreeValue(v);
2406 goto error;
2407 }
2408 } else {
2409 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2410 if (v != NULL) {
2411 v->value.d = xmlXPathNAN;
2412 } else {
2413 xmlSchemaFreeValue(v);
2414 goto error;
2415 }
2416 }
2417 *val = v;
2418 }
2419 goto return0;
2420 }
2421 if (*cur == '-') {
2422 neg = 1;
2423 cur++;
2424 }
2425 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
2426 cur += 3;
2427 if (*cur != 0)
2428 goto return1;
2429 if (val != NULL) {
2430 if (type == xmlSchemaTypeFloatDef) {
2431 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2432 if (v != NULL) {
2433 if (neg)
2434 v->value.f = (float) xmlXPathNINF;
2435 else
2436 v->value.f = (float) xmlXPathPINF;
2437 } else {
2438 xmlSchemaFreeValue(v);
2439 goto error;
2440 }
2441 } else {
2442 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2443 if (v != NULL) {
2444 if (neg)
2445 v->value.d = xmlXPathNINF;
2446 else
2447 v->value.d = xmlXPathPINF;
2448 } else {
2449 xmlSchemaFreeValue(v);
2450 goto error;
2451 }
2452 }
2453 *val = v;
2454 }
2455 goto return0;
2456 }
2457 if ((neg == 0) && (*cur == '+'))
2458 cur++;
2459 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
2460 goto return1;
2461 while ((*cur >= '0') && (*cur <= '9')) {
2462 cur++;
2463 }
2464 if (*cur == '.') {
2465 cur++;
2466 while ((*cur >= '0') && (*cur <= '9'))
2467 cur++;
2468 }
2469 if ((*cur == 'e') || (*cur == 'E')) {
2470 cur++;
2471 if ((*cur == '-') || (*cur == '+'))
2472 cur++;
2473 while ((*cur >= '0') && (*cur <= '9'))
2474 cur++;
2475 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002476 if (normOnTheFly)
2477 while IS_WSP_BLANK_CH(*cur) cur++;
2478
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002479 if (*cur != 0)
2480 goto return1;
2481 if (val != NULL) {
2482 if (type == xmlSchemaTypeFloatDef) {
2483 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2484 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002485 /*
2486 * TODO: sscanf seems not to give the correct
2487 * value for extremely high/low values.
2488 * E.g. "1E-149" results in zero.
2489 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002490 if (sscanf((const char *) value, "%f",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002491 &(v->value.f)) == 1) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002492 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002493 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002494 xmlSchemaFreeValue(v);
2495 goto return1;
2496 }
2497 } else {
2498 goto error;
2499 }
2500 } else {
2501 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2502 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002503 /*
2504 * TODO: sscanf seems not to give the correct
2505 * value for extremely high/low values.
2506 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002507 if (sscanf((const char *) value, "%lf",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002508 &(v->value.d)) == 1) {
2509 *val = v;
2510 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002511 xmlSchemaFreeValue(v);
2512 goto return1;
2513 }
2514 } else {
2515 goto error;
2516 }
2517 }
2518 }
2519 goto return0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00002520 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002521 case XML_SCHEMAS_BOOLEAN:{
2522 const xmlChar *cur = value;
2523
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002524 if (normOnTheFly) {
2525 while IS_WSP_BLANK_CH(*cur) cur++;
2526 if (*cur == '0') {
2527 ret = 0;
2528 cur++;
2529 } else if (*cur == '1') {
2530 ret = 1;
2531 cur++;
2532 } else if (*cur == 't') {
2533 cur++;
2534 if ((*cur++ == 'r') && (*cur++ == 'u') &&
2535 (*cur++ == 'e')) {
2536 ret = 1;
2537 } else
2538 goto return1;
2539 } else if (*cur == 'f') {
2540 cur++;
2541 if ((*cur++ == 'a') && (*cur++ == 'l') &&
2542 (*cur++ == 's') && (*cur++ == 'e')) {
2543 ret = 0;
2544 } else
2545 goto return1;
2546 }
2547 if (*cur != 0) {
2548 while IS_WSP_BLANK_CH(*cur) cur++;
2549 if (*cur != 0)
2550 goto return1;
2551 }
2552 } else {
2553 if ((cur[0] == '0') && (cur[1] == 0))
2554 ret = 0;
2555 else if ((cur[0] == '1') && (cur[1] == 0))
2556 ret = 1;
2557 else if ((cur[0] == 't') && (cur[1] == 'r')
2558 && (cur[2] == 'u') && (cur[3] == 'e')
2559 && (cur[4] == 0))
2560 ret = 1;
2561 else if ((cur[0] == 'f') && (cur[1] == 'a')
2562 && (cur[2] == 'l') && (cur[3] == 's')
2563 && (cur[4] == 'e') && (cur[5] == 0))
2564 ret = 0;
2565 else
2566 goto return1;
2567 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002568 if (val != NULL) {
2569 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
2570 if (v != NULL) {
2571 v->value.b = ret;
2572 *val = v;
2573 } else {
2574 goto error;
2575 }
2576 }
2577 goto return0;
2578 }
2579 case XML_SCHEMAS_TOKEN:{
2580 const xmlChar *cur = value;
2581
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002582 if (! normOnTheFly) {
2583 while (*cur != 0) {
2584 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2585 goto return1;
2586 } else if (*cur == ' ') {
2587 cur++;
2588 if (*cur == 0)
2589 goto return1;
2590 if (*cur == ' ')
2591 goto return1;
2592 } else {
2593 cur++;
2594 }
2595 }
2596 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002597 if (val != NULL) {
2598 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
2599 if (v != NULL) {
2600 v->value.str = xmlStrdup(value);
2601 *val = v;
2602 } else {
2603 goto error;
2604 }
2605 }
2606 goto return0;
2607 }
2608 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002609 if (normOnTheFly) {
2610 norm = xmlSchemaCollapseString(value);
2611 if (norm != NULL)
2612 value = norm;
2613 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002614 if (xmlCheckLanguageID(value) == 1) {
2615 if (val != NULL) {
2616 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
2617 if (v != NULL) {
2618 v->value.str = xmlStrdup(value);
2619 *val = v;
2620 } else {
2621 goto error;
2622 }
2623 }
2624 goto return0;
2625 }
2626 goto return1;
2627 case XML_SCHEMAS_NMTOKEN:
2628 if (xmlValidateNMToken(value, 1) == 0) {
2629 if (val != NULL) {
2630 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
2631 if (v != NULL) {
2632 v->value.str = xmlStrdup(value);
2633 *val = v;
2634 } else {
2635 goto error;
2636 }
2637 }
2638 goto return0;
2639 }
2640 goto return1;
2641 case XML_SCHEMAS_NMTOKENS:
2642 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
2643 value, val, node);
2644 if (ret > 0)
2645 ret = 0;
2646 else
2647 ret = 1;
2648 goto done;
2649 case XML_SCHEMAS_NAME:
2650 ret = xmlValidateName(value, 1);
Daniel Veillarddf292f72005-01-16 19:00:15 +00002651 if ((ret == 0) && (val != NULL) && (value != NULL)) {
2652 v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
2653 if (v != NULL) {
2654 const xmlChar *start = value, *end;
2655 while (IS_BLANK_CH(*start)) start++;
2656 end = start;
2657 while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
2658 v->value.str = xmlStrndup(start, end - start);
2659 *val = v;
2660 } else {
2661 goto error;
2662 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002663 }
2664 goto done;
2665 case XML_SCHEMAS_QNAME:{
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002666 const xmlChar *uri = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002667 xmlChar *local = NULL;
2668
2669 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002670 if (ret != 0)
2671 goto done;
2672 if (node != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002673 xmlChar *prefix;
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002674 xmlNsPtr ns;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002675
2676 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002677 ns = xmlSearchNs(node->doc, node, prefix);
2678 if ((ns == NULL) && (prefix != NULL)) {
2679 xmlFree(prefix);
2680 if (local != NULL)
2681 xmlFree(local);
2682 goto return1;
2683 }
2684 if (ns != NULL)
2685 uri = ns->href;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002686 if (prefix != NULL)
2687 xmlFree(prefix);
2688 }
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002689 if (val != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002690 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002691 if (v == NULL) {
2692 if (local != NULL)
2693 xmlFree(local);
2694 goto error;
2695 }
2696 if (local != NULL)
2697 v->value.qname.name = local;
2698 else
2699 v->value.qname.name = xmlStrdup(value);
2700 if (uri != NULL)
2701 v->value.qname.uri = xmlStrdup(uri);
2702 *val = v;
2703 } else
2704 if (local != NULL)
2705 xmlFree(local);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002706 goto done;
2707 }
2708 case XML_SCHEMAS_NCNAME:
2709 ret = xmlValidateNCName(value, 1);
2710 if ((ret == 0) && (val != NULL)) {
2711 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
2712 if (v != NULL) {
2713 v->value.str = xmlStrdup(value);
2714 *val = v;
2715 } else {
2716 goto error;
2717 }
2718 }
2719 goto done;
2720 case XML_SCHEMAS_ID:
2721 ret = xmlValidateNCName(value, 1);
2722 if ((ret == 0) && (val != NULL)) {
2723 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
2724 if (v != NULL) {
2725 v->value.str = xmlStrdup(value);
2726 *val = v;
2727 } else {
2728 goto error;
2729 }
2730 }
2731 if ((ret == 0) && (node != NULL) &&
2732 (node->type == XML_ATTRIBUTE_NODE)) {
2733 xmlAttrPtr attr = (xmlAttrPtr) node;
2734
2735 /*
2736 * NOTE: the IDness might have already be declared in the DTD
2737 */
2738 if (attr->atype != XML_ATTRIBUTE_ID) {
2739 xmlIDPtr res;
2740 xmlChar *strip;
2741
2742 strip = xmlSchemaStrip(value);
2743 if (strip != NULL) {
2744 res = xmlAddID(NULL, node->doc, strip, attr);
2745 xmlFree(strip);
2746 } else
2747 res = xmlAddID(NULL, node->doc, value, attr);
2748 if (res == NULL) {
2749 ret = 2;
2750 } else {
2751 attr->atype = XML_ATTRIBUTE_ID;
2752 }
2753 }
2754 }
2755 goto done;
2756 case XML_SCHEMAS_IDREF:
2757 ret = xmlValidateNCName(value, 1);
2758 if ((ret == 0) && (val != NULL)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00002759 v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
2760 if (v == NULL)
2761 goto error;
2762 v->value.str = xmlStrdup(value);
2763 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002764 }
2765 if ((ret == 0) && (node != NULL) &&
2766 (node->type == XML_ATTRIBUTE_NODE)) {
2767 xmlAttrPtr attr = (xmlAttrPtr) node;
2768 xmlChar *strip;
2769
2770 strip = xmlSchemaStrip(value);
2771 if (strip != NULL) {
2772 xmlAddRef(NULL, node->doc, strip, attr);
2773 xmlFree(strip);
2774 } else
2775 xmlAddRef(NULL, node->doc, value, attr);
2776 attr->atype = XML_ATTRIBUTE_IDREF;
2777 }
2778 goto done;
2779 case XML_SCHEMAS_IDREFS:
2780 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
2781 value, val, node);
2782 if (ret < 0)
2783 ret = 2;
2784 else
2785 ret = 0;
2786 if ((ret == 0) && (node != NULL) &&
2787 (node->type == XML_ATTRIBUTE_NODE)) {
2788 xmlAttrPtr attr = (xmlAttrPtr) node;
2789
2790 attr->atype = XML_ATTRIBUTE_IDREFS;
2791 }
2792 goto done;
2793 case XML_SCHEMAS_ENTITY:{
2794 xmlChar *strip;
2795
2796 ret = xmlValidateNCName(value, 1);
2797 if ((node == NULL) || (node->doc == NULL))
2798 ret = 3;
2799 if (ret == 0) {
2800 xmlEntityPtr ent;
2801
2802 strip = xmlSchemaStrip(value);
2803 if (strip != NULL) {
2804 ent = xmlGetDocEntity(node->doc, strip);
2805 xmlFree(strip);
2806 } else {
2807 ent = xmlGetDocEntity(node->doc, value);
2808 }
2809 if ((ent == NULL) ||
2810 (ent->etype !=
2811 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
2812 ret = 4;
2813 }
2814 if ((ret == 0) && (val != NULL)) {
2815 TODO;
2816 }
2817 if ((ret == 0) && (node != NULL) &&
2818 (node->type == XML_ATTRIBUTE_NODE)) {
2819 xmlAttrPtr attr = (xmlAttrPtr) node;
2820
2821 attr->atype = XML_ATTRIBUTE_ENTITY;
2822 }
2823 goto done;
2824 }
2825 case XML_SCHEMAS_ENTITIES:
2826 if ((node == NULL) || (node->doc == NULL))
2827 goto return3;
2828 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
2829 value, val, node);
2830 if (ret <= 0)
2831 ret = 1;
2832 else
2833 ret = 0;
2834 if ((ret == 0) && (node != NULL) &&
2835 (node->type == XML_ATTRIBUTE_NODE)) {
2836 xmlAttrPtr attr = (xmlAttrPtr) node;
2837
2838 attr->atype = XML_ATTRIBUTE_ENTITIES;
2839 }
2840 goto done;
2841 case XML_SCHEMAS_NOTATION:{
2842 xmlChar *uri = NULL;
2843 xmlChar *local = NULL;
2844
2845 ret = xmlValidateQName(value, 1);
2846 if ((ret == 0) && (node != NULL)) {
2847 xmlChar *prefix;
2848
2849 local = xmlSplitQName2(value, &prefix);
2850 if (prefix != NULL) {
2851 xmlNsPtr ns;
2852
2853 ns = xmlSearchNs(node->doc, node, prefix);
2854 if (ns == NULL)
2855 ret = 1;
2856 else if (val != NULL)
2857 uri = xmlStrdup(ns->href);
2858 }
2859 if ((local != NULL) && ((val == NULL) || (ret != 0)))
2860 xmlFree(local);
2861 if (prefix != NULL)
2862 xmlFree(prefix);
2863 }
2864 if ((node == NULL) || (node->doc == NULL))
2865 ret = 3;
2866 if (ret == 0) {
2867 ret = xmlValidateNotationUse(NULL, node->doc, value);
2868 if (ret == 1)
2869 ret = 0;
2870 else
2871 ret = 1;
2872 }
2873 if ((ret == 0) && (val != NULL)) {
2874 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
2875 if (v != NULL) {
2876 if (local != NULL)
2877 v->value.qname.name = local;
2878 else
2879 v->value.qname.name = xmlStrdup(value);
2880 if (uri != NULL)
2881 v->value.qname.uri = uri;
2882
2883 *val = v;
2884 } else {
2885 if (local != NULL)
2886 xmlFree(local);
2887 if (uri != NULL)
2888 xmlFree(uri);
2889 goto error;
2890 }
2891 }
2892 goto done;
2893 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002894 case XML_SCHEMAS_ANYURI:{
Daniel Veillard11c466a2004-03-14 12:20:15 +00002895 if (*value != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002896 xmlURIPtr uri;
2897 if (normOnTheFly) {
2898 norm = xmlSchemaCollapseString(value);
2899 if (norm != NULL)
2900 value = norm;
2901 }
2902 uri = xmlParseURI((const char *) value);
Daniel Veillard11c466a2004-03-14 12:20:15 +00002903 if (uri == NULL)
2904 goto return1;
2905 xmlFreeURI(uri);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002906 }
Daniel Veillard11c466a2004-03-14 12:20:15 +00002907
2908 if (val != NULL) {
2909 v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
2910 if (v == NULL)
2911 goto error;
2912 v->value.str = xmlStrdup(value);
2913 *val = v;
2914 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002915 goto return0;
2916 }
2917 case XML_SCHEMAS_HEXBINARY:{
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002918 const xmlChar *cur = value, *start;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002919 xmlChar *base;
2920 int total, i = 0;
2921
Daniel Veillardf34a20e2004-08-31 08:42:17 +00002922 if (cur == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002923 goto return1;
2924
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002925 if (normOnTheFly)
2926 while IS_WSP_BLANK_CH(*cur) cur++;
2927
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002928 start = cur;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002929 while (((*cur >= '0') && (*cur <= '9')) ||
2930 ((*cur >= 'A') && (*cur <= 'F')) ||
2931 ((*cur >= 'a') && (*cur <= 'f'))) {
2932 i++;
2933 cur++;
2934 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002935 if (normOnTheFly)
2936 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002937
2938 if (*cur != 0)
2939 goto return1;
2940 if ((i % 2) != 0)
2941 goto return1;
2942
2943 if (val != NULL) {
2944
2945 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2946 if (v == NULL)
2947 goto error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002948 /*
2949 * Copy only the normalized piece.
2950 * CRITICAL TODO: Check this.
2951 */
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002952 cur = xmlStrndup(start, i);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002953 if (cur == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002954 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002955 xmlFree(v);
2956 goto return1;
2957 }
2958
2959 total = i / 2; /* number of octets */
2960
2961 base = (xmlChar *) cur;
2962 while (i-- > 0) {
2963 if (*base >= 'a')
2964 *base = *base - ('a' - 'A');
2965 base++;
2966 }
2967
2968 v->value.hex.str = (xmlChar *) cur;
2969 v->value.hex.total = total;
2970 *val = v;
2971 }
2972 goto return0;
2973 }
2974 case XML_SCHEMAS_BASE64BINARY:{
2975 /* ISSUE:
2976 *
2977 * Ignore all stray characters? (yes, currently)
2978 * Worry about long lines? (no, currently)
2979 *
2980 * rfc2045.txt:
2981 *
2982 * "The encoded output stream must be represented in lines of
2983 * no more than 76 characters each. All line breaks or other
2984 * characters not found in Table 1 must be ignored by decoding
2985 * software. In base64 data, characters other than those in
2986 * Table 1, line breaks, and other white space probably
2987 * indicate a transmission error, about which a warning
2988 * message or even a message rejection might be appropriate
2989 * under some circumstances." */
2990 const xmlChar *cur = value;
2991 xmlChar *base;
2992 int total, i = 0, pad = 0;
2993
2994 if (cur == NULL)
2995 goto return1;
2996
2997 for (; *cur; ++cur) {
2998 int decc;
2999
3000 decc = _xmlSchemaBase64Decode(*cur);
3001 if (decc < 0) ;
3002 else if (decc < 64)
3003 i++;
3004 else
3005 break;
3006 }
3007 for (; *cur; ++cur) {
3008 int decc;
3009
3010 decc = _xmlSchemaBase64Decode(*cur);
3011 if (decc < 0) ;
3012 else if (decc < 64)
3013 goto return1;
3014 if (decc == 64)
3015 pad++;
3016 }
3017
3018 /* rfc2045.txt: "Special processing is performed if fewer than
3019 * 24 bits are available at the end of the data being encoded.
3020 * A full encoding quantum is always completed at the end of a
3021 * body. When fewer than 24 input bits are available in an
3022 * input group, zero bits are added (on the right) to form an
3023 * integral number of 6-bit groups. Padding at the end of the
3024 * data is performed using the "=" character. Since all
3025 * base64 input is an integral number of octets, only the
3026 * following cases can arise: (1) the final quantum of
3027 * encoding input is an integral multiple of 24 bits; here,
3028 * the final unit of encoded output will be an integral
3029 * multiple ofindent: Standard input:701: Warning:old style
3030 * assignment ambiguity in "=*". Assuming "= *" 4 characters
3031 * with no "=" padding, (2) the final
3032 * quantum of encoding input is exactly 8 bits; here, the
3033 * final unit of encoded output will be two characters
3034 * followed by two "=" padding characters, or (3) the final
3035 * quantum of encoding input is exactly 16 bits; here, the
3036 * final unit of encoded output will be three characters
3037 * followed by one "=" padding character." */
3038
3039 total = 3 * (i / 4);
3040 if (pad == 0) {
3041 if (i % 4 != 0)
3042 goto return1;
3043 } else if (pad == 1) {
3044 int decc;
3045
3046 if (i % 4 != 3)
3047 goto return1;
3048 for (decc = _xmlSchemaBase64Decode(*cur);
3049 (decc < 0) || (decc > 63);
3050 decc = _xmlSchemaBase64Decode(*cur))
3051 --cur;
3052 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
3053 /* 00111100 -> 0x3c */
3054 if (decc & ~0x3c)
3055 goto return1;
3056 total += 2;
3057 } else if (pad == 2) {
3058 int decc;
3059
3060 if (i % 4 != 2)
3061 goto return1;
3062 for (decc = _xmlSchemaBase64Decode(*cur);
3063 (decc < 0) || (decc > 63);
3064 decc = _xmlSchemaBase64Decode(*cur))
3065 --cur;
3066 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
3067 /* 00110000 -> 0x30 */
3068 if (decc & ~0x30)
3069 goto return1;
3070 total += 1;
3071 } else
3072 goto return1;
3073
3074 if (val != NULL) {
3075 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
3076 if (v == NULL)
3077 goto error;
3078 base =
3079 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
3080 sizeof(xmlChar));
3081 if (base == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003082 xmlSchemaTypeErrMemory(node, "allocating base64 data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003083 xmlFree(v);
3084 goto return1;
3085 }
3086 v->value.base64.str = base;
3087 for (cur = value; *cur; ++cur)
3088 if (_xmlSchemaBase64Decode(*cur) >= 0) {
3089 *base = *cur;
3090 ++base;
3091 }
3092 *base = 0;
3093 v->value.base64.total = total;
3094 *val = v;
3095 }
3096 goto return0;
3097 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003098 case XML_SCHEMAS_INTEGER:
3099 case XML_SCHEMAS_PINTEGER:
3100 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003101 case XML_SCHEMAS_NINTEGER:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003102 case XML_SCHEMAS_NNINTEGER:{
3103 const xmlChar *cur = value;
3104 unsigned long lo, mi, hi;
William M. Brackec3b4b72005-03-15 15:50:17 +00003105 int sign = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003106
3107 if (cur == NULL)
3108 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003109 if (normOnTheFly)
3110 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003111 if (*cur == '-') {
3112 sign = 1;
3113 cur++;
3114 } else if (*cur == '+')
3115 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003116 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3117 if (ret == -1)
3118 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003119 if (normOnTheFly)
3120 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003121 if (*cur != 0)
3122 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003123 if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003124 if ((sign == 0) &&
3125 ((hi != 0) || (mi != 0) || (lo != 0)))
3126 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003127 } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003128 if (sign == 1)
3129 goto return1;
3130 if ((hi == 0) && (mi == 0) && (lo == 0))
3131 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003132 } else if (type->builtInType == XML_SCHEMAS_NINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003133 if (sign == 0)
3134 goto return1;
3135 if ((hi == 0) && (mi == 0) && (lo == 0))
3136 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003137 } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003138 if ((sign == 1) &&
3139 ((hi != 0) || (mi != 0) || (lo != 0)))
3140 goto return1;
3141 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003142 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003143 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003144 if (v != NULL) {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003145 if (ret == 0)
3146 ret++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003147 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003148 v->value.decimal.mi = mi;
3149 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003150 v->value.decimal.sign = sign;
3151 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003152 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003153 *val = v;
3154 }
3155 }
3156 goto return0;
3157 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003158 case XML_SCHEMAS_LONG:
3159 case XML_SCHEMAS_BYTE:
3160 case XML_SCHEMAS_SHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003161 case XML_SCHEMAS_INT:{
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00003162 const xmlChar *cur = value;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003163 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003164 int sign = 0;
3165
3166 if (cur == NULL)
3167 goto return1;
3168 if (*cur == '-') {
3169 sign = 1;
3170 cur++;
3171 } else if (*cur == '+')
3172 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003173 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3174 if (ret < 0)
3175 goto return1;
3176 if (*cur != 0)
3177 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003178 if (type->builtInType == XML_SCHEMAS_LONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003179 if (hi >= 922) {
3180 if (hi > 922)
3181 goto return1;
3182 if (mi >= 33720368) {
3183 if (mi > 33720368)
3184 goto return1;
3185 if ((sign == 0) && (lo > 54775807))
3186 goto return1;
3187 if ((sign == 1) && (lo > 54775808))
3188 goto return1;
3189 }
3190 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003191 } else if (type->builtInType == XML_SCHEMAS_INT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003192 if (hi != 0)
3193 goto return1;
3194 if (mi >= 21) {
3195 if (mi > 21)
3196 goto return1;
3197 if ((sign == 0) && (lo > 47483647))
3198 goto return1;
3199 if ((sign == 1) && (lo > 47483648))
3200 goto return1;
3201 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003202 } else if (type->builtInType == XML_SCHEMAS_SHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003203 if ((mi != 0) || (hi != 0))
3204 goto return1;
3205 if ((sign == 1) && (lo > 32768))
3206 goto return1;
3207 if ((sign == 0) && (lo > 32767))
3208 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003209 } else if (type->builtInType == XML_SCHEMAS_BYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003210 if ((mi != 0) || (hi != 0))
3211 goto return1;
3212 if ((sign == 1) && (lo > 128))
3213 goto return1;
3214 if ((sign == 0) && (lo > 127))
3215 goto return1;
3216 }
3217 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003218 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003219 if (v != NULL) {
3220 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003221 v->value.decimal.mi = mi;
3222 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003223 v->value.decimal.sign = sign;
3224 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003225 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003226 *val = v;
3227 }
3228 }
3229 goto return0;
3230 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003231 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003232 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003233 case XML_SCHEMAS_USHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003234 case XML_SCHEMAS_UBYTE:{
3235 const xmlChar *cur = value;
3236 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003237
3238 if (cur == NULL)
3239 goto return1;
William M. Brackec3b4b72005-03-15 15:50:17 +00003240 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3241 if (ret < 0)
3242 goto return1;
3243 if (*cur != 0)
3244 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003245 if (type->builtInType == XML_SCHEMAS_ULONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003246 if (hi >= 1844) {
3247 if (hi > 1844)
3248 goto return1;
3249 if (mi >= 67440737) {
3250 if (mi > 67440737)
3251 goto return1;
3252 if (lo > 9551615)
3253 goto return1;
3254 }
3255 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003256 } else if (type->builtInType == XML_SCHEMAS_UINT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003257 if (hi != 0)
3258 goto return1;
3259 if (mi >= 42) {
3260 if (mi > 42)
3261 goto return1;
3262 if (lo > 94967295)
3263 goto return1;
3264 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003265 } else if (type->builtInType == XML_SCHEMAS_USHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003266 if ((mi != 0) || (hi != 0))
3267 goto return1;
3268 if (lo > 65535)
3269 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003270 } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003271 if ((mi != 0) || (hi != 0))
3272 goto return1;
3273 if (lo > 255)
3274 goto return1;
3275 }
3276 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003277 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003278 if (v != NULL) {
3279 v->value.decimal.lo = lo;
3280 v->value.decimal.mi = mi;
3281 v->value.decimal.hi = hi;
3282 v->value.decimal.sign = 0;
3283 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003284 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003285 *val = v;
3286 }
3287 }
3288 goto return0;
3289 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003290 }
3291
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003292 done:
3293 if (norm != NULL)
3294 xmlFree(norm);
3295 return (ret);
3296 return3:
3297 if (norm != NULL)
3298 xmlFree(norm);
3299 return (3);
3300 return1:
3301 if (norm != NULL)
3302 xmlFree(norm);
3303 return (1);
3304 return0:
3305 if (norm != NULL)
3306 xmlFree(norm);
3307 return (0);
3308 error:
3309 if (norm != NULL)
3310 xmlFree(norm);
3311 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003312}
3313
3314/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003315 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00003316 * @type: the predefined type
3317 * @value: the value to check
3318 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003319 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00003320 *
3321 * Check that a value conforms to the lexical space of the predefined type.
3322 * if true a value is computed and returned in @val.
3323 *
3324 * Returns 0 if this validates, a positive error code number otherwise
3325 * and -1 in case of internal or API error.
3326 */
3327int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003328xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
3329 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003330 return(xmlSchemaValAtomicType(type, value, val, node, 0,
3331 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00003332}
3333
3334/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00003335 * xmlSchemaValPredefTypeNodeNoNorm:
3336 * @type: the predefined type
3337 * @value: the value to check
3338 * @val: the return computed value
3339 * @node: the node containing the value
3340 *
3341 * Check that a value conforms to the lexical space of the predefined type.
3342 * if true a value is computed and returned in @val.
3343 * This one does apply any normalization to the value.
3344 *
3345 * Returns 0 if this validates, a positive error code number otherwise
3346 * and -1 in case of internal or API error.
3347 */
3348int
3349xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
3350 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003351 return(xmlSchemaValAtomicType(type, value, val, node, 1,
3352 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
Daniel Veillardc0826a72004-08-10 14:17:33 +00003353}
3354
3355/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003356 * xmlSchemaValidatePredefinedType:
3357 * @type: the predefined type
3358 * @value: the value to check
3359 * @val: the return computed value
3360 *
3361 * Check that a value conforms to the lexical space of the predefined type.
3362 * if true a value is computed and returned in @val.
3363 *
3364 * Returns 0 if this validates, a positive error code number otherwise
3365 * and -1 in case of internal or API error.
3366 */
3367int
3368xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
3369 xmlSchemaValPtr *val) {
3370 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
3371}
3372
3373/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003374 * xmlSchemaCompareDecimals:
3375 * @x: a first decimal value
3376 * @y: a second decimal value
3377 *
3378 * Compare 2 decimals
3379 *
3380 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
3381 */
3382static int
3383xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
3384{
3385 xmlSchemaValPtr swp;
William M. Brack273670f2005-03-11 15:55:14 +00003386 int order = 1, integx, integy, dlen;
3387 unsigned long hi, mi, lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003388
William M. Brack273670f2005-03-11 15:55:14 +00003389 /*
3390 * First test: If x is -ve and not zero
3391 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003392 if ((x->value.decimal.sign) &&
3393 ((x->value.decimal.lo != 0) ||
3394 (x->value.decimal.mi != 0) ||
3395 (x->value.decimal.hi != 0))) {
William M. Brack273670f2005-03-11 15:55:14 +00003396 /*
3397 * Then if y is -ve and not zero reverse the compare
3398 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003399 if ((y->value.decimal.sign) &&
3400 ((y->value.decimal.lo != 0) ||
3401 (y->value.decimal.mi != 0) ||
3402 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00003403 order = -1;
William M. Brack273670f2005-03-11 15:55:14 +00003404 /*
3405 * Otherwise (y >= 0) we have the answer
3406 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003407 else
3408 return (-1);
William M. Brack273670f2005-03-11 15:55:14 +00003409 /*
3410 * If x is not -ve and y is -ve we have the answer
3411 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003412 } else if ((y->value.decimal.sign) &&
3413 ((y->value.decimal.lo != 0) ||
3414 (y->value.decimal.mi != 0) ||
3415 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003416 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00003417 }
William M. Brack273670f2005-03-11 15:55:14 +00003418 /*
3419 * If it's not simply determined by a difference in sign,
3420 * then we need to compare the actual values of the two nums.
3421 * To do this, we start by looking at the integral parts.
3422 * If the number of integral digits differ, then we have our
3423 * answer.
3424 */
3425 integx = x->value.decimal.total - x->value.decimal.frac;
3426 integy = y->value.decimal.total - y->value.decimal.frac;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003427 /*
3428 * NOTE: We changed the "total" for values like "0.1"
3429 * (or "-0.1" or ".1") to be 1, which was 2 previously.
3430 * Therefore the special case, when such values are
3431 * compared with 0, needs to be handled separately;
3432 * otherwise a zero would be recognized incorrectly as
3433 * greater than those values. This has the nice side effect
3434 * that we gain an overall optimized comparison with zeroes.
3435 * Note that a "0" has a "total" of 1 already.
3436 */
3437 if (integx == 1) {
3438 if (x->value.decimal.lo == 0) {
3439 if (integy != 1)
3440 return -order;
3441 else if (y->value.decimal.lo != 0)
3442 return -order;
3443 else
3444 return(0);
3445 }
3446 }
3447 if (integy == 1) {
3448 if (y->value.decimal.lo == 0) {
3449 if (integx != 1)
3450 return order;
3451 else if (x->value.decimal.lo != 0)
3452 return order;
3453 else
3454 return(0);
3455 }
3456 }
3457
William M. Brack273670f2005-03-11 15:55:14 +00003458 if (integx > integy)
3459 return order;
3460 else if (integy > integx)
3461 return -order;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003462
William M. Brack273670f2005-03-11 15:55:14 +00003463 /*
3464 * If the number of integral digits is the same for both numbers,
3465 * then things get a little more complicated. We need to "normalize"
3466 * the numbers in order to properly compare them. To do this, we
3467 * look at the total length of each number (length => number of
3468 * significant digits), and divide the "shorter" by 10 (decreasing
3469 * the length) until they are of equal length.
3470 */
3471 dlen = x->value.decimal.total - y->value.decimal.total;
3472 if (dlen < 0) { /* y has more digits than x */
3473 swp = x;
3474 hi = y->value.decimal.hi;
3475 mi = y->value.decimal.mi;
3476 lo = y->value.decimal.lo;
3477 dlen = -dlen;
3478 order = -order;
3479 } else { /* x has more digits than y */
3480 swp = y;
3481 hi = x->value.decimal.hi;
3482 mi = x->value.decimal.mi;
3483 lo = x->value.decimal.lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003484 }
William M. Brack273670f2005-03-11 15:55:14 +00003485 while (dlen > 8) { /* in effect, right shift by 10**8 */
3486 lo = mi;
3487 mi = hi;
3488 hi = 0;
3489 dlen -= 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00003490 }
William M. Brack273670f2005-03-11 15:55:14 +00003491 while (dlen > 0) {
3492 unsigned long rem1, rem2;
3493 rem1 = (hi % 10) * 100000000L;
3494 hi = hi / 10;
3495 rem2 = (mi % 10) * 100000000L;
3496 mi = (mi + rem1) / 10;
3497 lo = (lo + rem2) / 10;
3498 dlen--;
3499 }
3500 if (hi > swp->value.decimal.hi) {
3501 return order;
3502 } else if (hi == swp->value.decimal.hi) {
3503 if (mi > swp->value.decimal.mi) {
3504 return order;
3505 } else if (mi == swp->value.decimal.mi) {
3506 if (lo > swp->value.decimal.lo) {
3507 return order;
3508 } else if (lo == swp->value.decimal.lo) {
3509 if (x->value.decimal.total == y->value.decimal.total) {
3510 return 0;
3511 } else {
3512 return order;
3513 }
3514 }
3515 }
3516 }
3517 return -order;
Daniel Veillard4255d502002-04-16 15:50:10 +00003518}
3519
3520/**
Daniel Veillard070803b2002-05-03 07:29:38 +00003521 * xmlSchemaCompareDurations:
3522 * @x: a first duration value
3523 * @y: a second duration value
3524 *
3525 * Compare 2 durations
3526 *
3527 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3528 * case of error
3529 */
3530static int
3531xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
3532{
3533 long carry, mon, day;
3534 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00003535 int invert = 1;
3536 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00003537 static const long dayRange [2][12] = {
3538 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
3539 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
3540
3541 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00003542 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00003543
3544 /* months */
3545 mon = x->value.dur.mon - y->value.dur.mon;
3546
3547 /* seconds */
3548 sec = x->value.dur.sec - y->value.dur.sec;
3549 carry = (long)sec / SECS_PER_DAY;
3550 sec -= (double)(carry * SECS_PER_DAY);
3551
3552 /* days */
3553 day = x->value.dur.day - y->value.dur.day + carry;
3554
3555 /* easy test */
3556 if (mon == 0) {
3557 if (day == 0)
3558 if (sec == 0.0)
3559 return 0;
3560 else if (sec < 0.0)
3561 return -1;
3562 else
3563 return 1;
3564 else if (day < 0)
3565 return -1;
3566 else
3567 return 1;
3568 }
3569
3570 if (mon > 0) {
3571 if ((day >= 0) && (sec >= 0.0))
3572 return 1;
3573 else {
3574 xmon = mon;
3575 xday = -day;
3576 }
3577 } else if ((day <= 0) && (sec <= 0.0)) {
3578 return -1;
3579 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00003580 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00003581 xmon = -mon;
3582 xday = day;
3583 }
3584
3585 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00003586 if (myear == 0) {
3587 minday = 0;
3588 maxday = 0;
3589 } else {
3590 maxday = 366 * ((myear + 3) / 4) +
3591 365 * ((myear - 1) % 4);
3592 minday = maxday - 1;
3593 }
3594
Daniel Veillard070803b2002-05-03 07:29:38 +00003595 xmon = xmon % 12;
3596 minday += dayRange[0][xmon];
3597 maxday += dayRange[1][xmon];
3598
Daniel Veillard80b19092003-03-28 13:29:53 +00003599 if ((maxday == minday) && (maxday == xday))
3600 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00003601 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00003602 return(-invert);
3603 if (minday > xday)
3604 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00003605
3606 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00003607 return 2;
3608}
3609
3610/*
3611 * macros for adding date/times and durations
3612 */
3613#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
3614#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
3615#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
3616#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
3617
3618/**
Daniel Veillard669adfc2004-05-29 20:12:46 +00003619 * xmlSchemaDupVal:
3620 * @v: the #xmlSchemaValPtr value to duplicate
3621 *
3622 * Makes a copy of @v. The calling program is responsible for freeing
3623 * the returned value.
3624 *
3625 * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
3626 */
3627static xmlSchemaValPtr
3628xmlSchemaDupVal (xmlSchemaValPtr v)
3629{
3630 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
3631 if (ret == NULL)
3632 return NULL;
3633
3634 memcpy(ret, v, sizeof(xmlSchemaVal));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003635 ret->next = NULL;
Daniel Veillard669adfc2004-05-29 20:12:46 +00003636 return ret;
3637}
3638
3639/**
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003640 * xmlSchemaCopyValue:
3641 * @val: the precomputed value to be copied
3642 *
3643 * Copies the precomputed value. This duplicates any string within.
3644 *
3645 * Returns the copy or NULL if a copy for a data-type is not implemented.
3646 */
3647xmlSchemaValPtr
3648xmlSchemaCopyValue(xmlSchemaValPtr val)
3649{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003650 xmlSchemaValPtr ret = NULL, prev = NULL, cur;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003651
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003652 /*
3653 * Copy the string values.
3654 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003655 while (val != NULL) {
3656 switch (val->type) {
3657 case XML_SCHEMAS_ANYTYPE:
3658 case XML_SCHEMAS_IDREFS:
3659 case XML_SCHEMAS_ENTITIES:
3660 case XML_SCHEMAS_NMTOKENS:
3661 xmlSchemaFreeValue(ret);
3662 return (NULL);
3663 case XML_SCHEMAS_ANYSIMPLETYPE:
3664 case XML_SCHEMAS_STRING:
3665 case XML_SCHEMAS_NORMSTRING:
3666 case XML_SCHEMAS_TOKEN:
3667 case XML_SCHEMAS_LANGUAGE:
3668 case XML_SCHEMAS_NAME:
3669 case XML_SCHEMAS_NCNAME:
3670 case XML_SCHEMAS_ID:
3671 case XML_SCHEMAS_IDREF:
3672 case XML_SCHEMAS_ENTITY:
3673 case XML_SCHEMAS_NMTOKEN:
3674 case XML_SCHEMAS_ANYURI:
3675 cur = xmlSchemaDupVal(val);
3676 if (val->value.str != NULL)
3677 cur->value.str = xmlStrdup(BAD_CAST val->value.str);
3678 break;
3679 case XML_SCHEMAS_QNAME:
3680 case XML_SCHEMAS_NOTATION:
3681 cur = xmlSchemaDupVal(val);
3682 if (val->value.qname.name != NULL)
3683 cur->value.qname.name =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003684 xmlStrdup(BAD_CAST val->value.qname.name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003685 if (val->value.qname.uri != NULL)
3686 cur->value.qname.uri =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003687 xmlStrdup(BAD_CAST val->value.qname.uri);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003688 break;
3689 case XML_SCHEMAS_HEXBINARY:
3690 cur = xmlSchemaDupVal(val);
3691 if (val->value.hex.str != NULL)
3692 cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
3693 break;
3694 case XML_SCHEMAS_BASE64BINARY:
3695 cur = xmlSchemaDupVal(val);
3696 if (val->value.base64.str != NULL)
3697 cur->value.base64.str =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003698 xmlStrdup(BAD_CAST val->value.base64.str);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003699 break;
3700 default:
3701 cur = xmlSchemaDupVal(val);
3702 break;
3703 }
3704 if (ret == NULL)
3705 ret = cur;
3706 else
3707 prev->next = cur;
3708 prev = cur;
3709 val = val->next;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003710 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003711 return (ret);
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003712}
3713
3714/**
Daniel Veillard5a872412002-05-22 06:40:27 +00003715 * _xmlSchemaDateAdd:
3716 * @dt: an #xmlSchemaValPtr
3717 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
3718 *
3719 * Compute a new date/time from @dt and @dur. This function assumes @dt
3720 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
Daniel Veillard669adfc2004-05-29 20:12:46 +00003721 * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
3722 * @dt. The calling program is responsible for freeing the returned value.
Daniel Veillard5a872412002-05-22 06:40:27 +00003723 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003724 * Returns a pointer to a new #xmlSchemaVal or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003725 */
3726static xmlSchemaValPtr
3727_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
3728{
Daniel Veillard669adfc2004-05-29 20:12:46 +00003729 xmlSchemaValPtr ret, tmp;
Daniel Veillard5a872412002-05-22 06:40:27 +00003730 long carry, tempdays, temp;
3731 xmlSchemaValDatePtr r, d;
3732 xmlSchemaValDurationPtr u;
3733
3734 if ((dt == NULL) || (dur == NULL))
3735 return NULL;
3736
3737 ret = xmlSchemaNewValue(dt->type);
3738 if (ret == NULL)
3739 return NULL;
3740
Daniel Veillard669adfc2004-05-29 20:12:46 +00003741 /* make a copy so we don't alter the original value */
3742 tmp = xmlSchemaDupVal(dt);
3743 if (tmp == NULL) {
3744 xmlSchemaFreeValue(ret);
3745 return NULL;
3746 }
3747
Daniel Veillard5a872412002-05-22 06:40:27 +00003748 r = &(ret->value.date);
Daniel Veillard669adfc2004-05-29 20:12:46 +00003749 d = &(tmp->value.date);
Daniel Veillard5a872412002-05-22 06:40:27 +00003750 u = &(dur->value.dur);
3751
3752 /* normalization */
3753 if (d->mon == 0)
3754 d->mon = 1;
3755
3756 /* normalize for time zone offset */
3757 u->sec -= (d->tzo * 60);
3758 d->tzo = 0;
3759
3760 /* normalization */
3761 if (d->day == 0)
3762 d->day = 1;
3763
3764 /* month */
3765 carry = d->mon + u->mon;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003766 r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
3767 carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003768
3769 /* year (may be modified later) */
3770 r->year = d->year + carry;
3771 if (r->year == 0) {
3772 if (d->year > 0)
3773 r->year--;
3774 else
3775 r->year++;
3776 }
3777
3778 /* time zone */
3779 r->tzo = d->tzo;
3780 r->tz_flag = d->tz_flag;
3781
3782 /* seconds */
3783 r->sec = d->sec + u->sec;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003784 carry = (long) FQUOTIENT((long)r->sec, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003785 if (r->sec != 0.0) {
3786 r->sec = MODULO(r->sec, 60.0);
3787 }
3788
3789 /* minute */
3790 carry += d->min;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003791 r->min = (unsigned int) MODULO(carry, 60);
3792 carry = (long) FQUOTIENT(carry, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003793
3794 /* hours */
3795 carry += d->hour;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003796 r->hour = (unsigned int) MODULO(carry, 24);
3797 carry = (long)FQUOTIENT(carry, 24);
Daniel Veillard5a872412002-05-22 06:40:27 +00003798
3799 /*
3800 * days
3801 * Note we use tempdays because the temporary values may need more
3802 * than 5 bits
3803 */
3804 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
3805 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
3806 tempdays = MAX_DAYINMONTH(r->year, r->mon);
3807 else if (d->day < 1)
3808 tempdays = 1;
3809 else
3810 tempdays = d->day;
3811
3812 tempdays += u->day + carry;
3813
3814 while (1) {
3815 if (tempdays < 1) {
Daniel Veillard5e9576a2005-11-21 11:23:47 +00003816 long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13);
3817 long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003818 if (tyr == 0)
3819 tyr--;
3820 tempdays += MAX_DAYINMONTH(tyr, tmon);
3821 carry = -1;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003822 } else if (tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003823 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
3824 carry = 1;
3825 } else
3826 break;
3827
3828 temp = r->mon + carry;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003829 r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
3830 r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003831 if (r->year == 0) {
3832 if (temp < 1)
3833 r->year--;
3834 else
3835 r->year++;
3836 }
3837 }
3838
3839 r->day = tempdays;
3840
3841 /*
3842 * adjust the date/time type to the date values
3843 */
3844 if (ret->type != XML_SCHEMAS_DATETIME) {
3845 if ((r->hour) || (r->min) || (r->sec))
3846 ret->type = XML_SCHEMAS_DATETIME;
3847 else if (ret->type != XML_SCHEMAS_DATE) {
3848 if ((r->mon != 1) && (r->day != 1))
3849 ret->type = XML_SCHEMAS_DATE;
3850 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
3851 ret->type = XML_SCHEMAS_GYEARMONTH;
3852 }
3853 }
3854
Daniel Veillard669adfc2004-05-29 20:12:46 +00003855 xmlSchemaFreeValue(tmp);
Daniel Veillard5a872412002-05-22 06:40:27 +00003856
Daniel Veillard5a872412002-05-22 06:40:27 +00003857 return ret;
3858}
3859
3860/**
3861 * xmlSchemaDateNormalize:
Daniel Veillard669adfc2004-05-29 20:12:46 +00003862 * @dt: an #xmlSchemaValPtr of a date/time type value.
3863 * @offset: number of seconds to adjust @dt by.
Daniel Veillard5a872412002-05-22 06:40:27 +00003864 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003865 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3866 * the return value is a time-zone offset is present on @dt.
Daniel Veillard5a872412002-05-22 06:40:27 +00003867 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003868 * Returns a normalized copy of @dt or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003869 */
3870static xmlSchemaValPtr
3871xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
3872{
3873 xmlSchemaValPtr dur, ret;
3874
3875 if (dt == NULL)
3876 return NULL;
3877
3878 if (((dt->type != XML_SCHEMAS_TIME) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003879 (dt->type != XML_SCHEMAS_DATETIME) &&
3880 (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
Daniel Veillard5a872412002-05-22 06:40:27 +00003881 return xmlSchemaDupVal(dt);
3882
3883 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
3884 if (dur == NULL)
3885 return NULL;
3886
3887 dur->value.date.sec -= offset;
3888
3889 ret = _xmlSchemaDateAdd(dt, dur);
3890 if (ret == NULL)
3891 return NULL;
3892
3893 xmlSchemaFreeValue(dur);
3894
3895 /* ret->value.date.tzo = 0; */
3896 return ret;
3897}
3898
3899/**
3900 * _xmlSchemaDateCastYMToDays:
3901 * @dt: an #xmlSchemaValPtr
3902 *
3903 * Convert mon and year of @dt to total number of days. Take the
3904 * number of years since (or before) 1 AD and add the number of leap
3905 * years. This is a function because negative
3906 * years must be handled a little differently and there is no zero year.
3907 *
3908 * Returns number of days.
3909 */
3910static long
3911_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
3912{
3913 long ret;
Daniel Veillard49e89632004-09-23 16:24:36 +00003914 int mon;
Daniel Veillard5a872412002-05-22 06:40:27 +00003915
Daniel Veillard49e89632004-09-23 16:24:36 +00003916 mon = dt->value.date.mon;
3917 if (mon <= 0) mon = 1; /* normalization */
3918
3919 if (dt->value.date.year <= 0)
Daniel Veillard5a872412002-05-22 06:40:27 +00003920 ret = (dt->value.date.year * 365) +
3921 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
3922 ((dt->value.date.year+1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003923 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003924 else
3925 ret = ((dt->value.date.year-1) * 365) +
3926 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
3927 ((dt->value.date.year-1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003928 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003929
3930 return ret;
3931}
3932
3933/**
3934 * TIME_TO_NUMBER:
3935 * @dt: an #xmlSchemaValPtr
3936 *
3937 * Calculates the number of seconds in the time portion of @dt.
3938 *
3939 * Returns seconds.
3940 */
3941#define TIME_TO_NUMBER(dt) \
3942 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00003943 (dt->value.date.min * SECS_PER_MIN) + \
3944 (dt->value.date.tzo * SECS_PER_MIN)) + \
3945 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00003946
3947/**
3948 * xmlSchemaCompareDates:
3949 * @x: a first date/time value
3950 * @y: a second date/time value
3951 *
3952 * Compare 2 date/times
3953 *
3954 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3955 * case of error
3956 */
3957static int
3958xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
3959{
3960 unsigned char xmask, ymask, xor_mask, and_mask;
3961 xmlSchemaValPtr p1, p2, q1, q2;
3962 long p1d, p2d, q1d, q2d;
3963
3964 if ((x == NULL) || (y == NULL))
3965 return -2;
3966
3967 if (x->value.date.tz_flag) {
3968
3969 if (!y->value.date.tz_flag) {
3970 p1 = xmlSchemaDateNormalize(x, 0);
3971 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
3972 /* normalize y + 14:00 */
3973 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
3974
3975 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003976 if (p1d < q1d) {
3977 xmlSchemaFreeValue(p1);
3978 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003979 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003980 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003981 double sec;
3982
3983 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003984 if (sec < 0.0) {
3985 xmlSchemaFreeValue(p1);
3986 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003987 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003988 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003989 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00003990 /* normalize y - 14:00 */
3991 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
3992 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
3993 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003994 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00003995 else if (p1d == q2d) {
3996 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
3997 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003998 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00003999 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004000 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004001 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004002 xmlSchemaFreeValue(p1);
4003 xmlSchemaFreeValue(q1);
4004 xmlSchemaFreeValue(q2);
4005 if (ret != 0)
4006 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004007 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004008 } else {
4009 xmlSchemaFreeValue(p1);
4010 xmlSchemaFreeValue(q1);
4011 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004012 }
4013 } else if (y->value.date.tz_flag) {
4014 q1 = xmlSchemaDateNormalize(y, 0);
4015 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4016
4017 /* normalize x - 14:00 */
4018 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
4019 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4020
Daniel Veillardfdc91562002-07-01 21:52:03 +00004021 if (p1d < q1d) {
4022 xmlSchemaFreeValue(p1);
4023 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004024 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004025 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004026 double sec;
4027
4028 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004029 if (sec < 0.0) {
4030 xmlSchemaFreeValue(p1);
4031 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004032 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004033 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004034 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004035 /* normalize x + 14:00 */
4036 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
4037 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
4038
Daniel Veillard6560a422003-03-27 21:25:38 +00004039 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004040 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00004041 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004042 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
4043 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004044 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004045 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004046 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004047 }
Daniel Veillard6560a422003-03-27 21:25:38 +00004048 xmlSchemaFreeValue(p1);
4049 xmlSchemaFreeValue(q1);
4050 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004051 if (ret != 0)
4052 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004053 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004054 } else {
4055 xmlSchemaFreeValue(p1);
4056 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004057 }
4058 }
4059
4060 /*
4061 * if the same type then calculate the difference
4062 */
4063 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004064 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004065 q1 = xmlSchemaDateNormalize(y, 0);
4066 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4067
4068 p1 = xmlSchemaDateNormalize(x, 0);
4069 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4070
Daniel Veillardfdc91562002-07-01 21:52:03 +00004071 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004072 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004073 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004074 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004075 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00004076 double sec;
4077
4078 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4079 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004080 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004081 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004082 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004083
4084 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004085 xmlSchemaFreeValue(p1);
4086 xmlSchemaFreeValue(q1);
4087 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004088 }
4089
4090 switch (x->type) {
4091 case XML_SCHEMAS_DATETIME:
4092 xmask = 0xf;
4093 break;
4094 case XML_SCHEMAS_DATE:
4095 xmask = 0x7;
4096 break;
4097 case XML_SCHEMAS_GYEAR:
4098 xmask = 0x1;
4099 break;
4100 case XML_SCHEMAS_GMONTH:
4101 xmask = 0x2;
4102 break;
4103 case XML_SCHEMAS_GDAY:
4104 xmask = 0x3;
4105 break;
4106 case XML_SCHEMAS_GYEARMONTH:
4107 xmask = 0x3;
4108 break;
4109 case XML_SCHEMAS_GMONTHDAY:
4110 xmask = 0x6;
4111 break;
4112 case XML_SCHEMAS_TIME:
4113 xmask = 0x8;
4114 break;
4115 default:
4116 xmask = 0;
4117 break;
4118 }
4119
4120 switch (y->type) {
4121 case XML_SCHEMAS_DATETIME:
4122 ymask = 0xf;
4123 break;
4124 case XML_SCHEMAS_DATE:
4125 ymask = 0x7;
4126 break;
4127 case XML_SCHEMAS_GYEAR:
4128 ymask = 0x1;
4129 break;
4130 case XML_SCHEMAS_GMONTH:
4131 ymask = 0x2;
4132 break;
4133 case XML_SCHEMAS_GDAY:
4134 ymask = 0x3;
4135 break;
4136 case XML_SCHEMAS_GYEARMONTH:
4137 ymask = 0x3;
4138 break;
4139 case XML_SCHEMAS_GMONTHDAY:
4140 ymask = 0x6;
4141 break;
4142 case XML_SCHEMAS_TIME:
4143 ymask = 0x8;
4144 break;
4145 default:
4146 ymask = 0;
4147 break;
4148 }
4149
4150 xor_mask = xmask ^ ymask; /* mark type differences */
4151 and_mask = xmask & ymask; /* mark field specification */
4152
4153 /* year */
4154 if (xor_mask & 1)
4155 return 2; /* indeterminate */
4156 else if (and_mask & 1) {
4157 if (x->value.date.year < y->value.date.year)
4158 return -1;
4159 else if (x->value.date.year > y->value.date.year)
4160 return 1;
4161 }
4162
4163 /* month */
4164 if (xor_mask & 2)
4165 return 2; /* indeterminate */
4166 else if (and_mask & 2) {
4167 if (x->value.date.mon < y->value.date.mon)
4168 return -1;
4169 else if (x->value.date.mon > y->value.date.mon)
4170 return 1;
4171 }
4172
4173 /* day */
4174 if (xor_mask & 4)
4175 return 2; /* indeterminate */
4176 else if (and_mask & 4) {
4177 if (x->value.date.day < y->value.date.day)
4178 return -1;
4179 else if (x->value.date.day > y->value.date.day)
4180 return 1;
4181 }
4182
4183 /* time */
4184 if (xor_mask & 8)
4185 return 2; /* indeterminate */
4186 else if (and_mask & 8) {
4187 if (x->value.date.hour < y->value.date.hour)
4188 return -1;
4189 else if (x->value.date.hour > y->value.date.hour)
4190 return 1;
4191 else if (x->value.date.min < y->value.date.min)
4192 return -1;
4193 else if (x->value.date.min > y->value.date.min)
4194 return 1;
4195 else if (x->value.date.sec < y->value.date.sec)
4196 return -1;
4197 else if (x->value.date.sec > y->value.date.sec)
4198 return 1;
4199 }
4200
Daniel Veillard070803b2002-05-03 07:29:38 +00004201 return 0;
4202}
4203
4204/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004205 * xmlSchemaComparePreserveReplaceStrings:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004206 * @x: a first string value
4207 * @y: a second string value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004208 * @invert: inverts the result if x < y or x > y.
4209 *
4210 * Compare 2 string for their normalized values.
4211 * @x is a string with whitespace of "preserve", @y is
4212 * a string with a whitespace of "replace". I.e. @x could
4213 * be an "xsd:string" and @y an "xsd:normalizedString".
4214 *
4215 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4216 * case of error
4217 */
4218static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004219xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
4220 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004221 int invert)
4222{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004223 int tmp;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004224
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004225 while ((*x != 0) && (*y != 0)) {
4226 if (IS_WSP_REPLACE_CH(*y)) {
4227 if (! IS_WSP_SPACE_CH(*x)) {
4228 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004229 if (invert)
4230 return(1);
4231 else
4232 return(-1);
4233 } else {
4234 if (invert)
4235 return(-1);
4236 else
4237 return(1);
4238 }
4239 }
4240 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004241 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004242 if (tmp < 0) {
4243 if (invert)
4244 return(1);
4245 else
4246 return(-1);
4247 }
4248 if (tmp > 0) {
4249 if (invert)
4250 return(-1);
4251 else
4252 return(1);
4253 }
4254 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004255 x++;
4256 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004257 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004258 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004259 if (invert)
4260 return(-1);
4261 else
4262 return(1);
4263 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004264 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004265 if (invert)
4266 return(1);
4267 else
4268 return(-1);
4269 }
4270 return(0);
4271}
4272
4273/**
4274 * xmlSchemaComparePreserveCollapseStrings:
4275 * @x: a first string value
4276 * @y: a second string value
4277 *
4278 * Compare 2 string for their normalized values.
4279 * @x is a string with whitespace of "preserve", @y is
4280 * a string with a whitespace of "collapse". I.e. @x could
4281 * be an "xsd:string" and @y an "xsd:normalizedString".
4282 *
4283 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4284 * case of error
4285 */
4286static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004287xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
4288 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004289 int invert)
4290{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004291 int tmp;
4292
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004293 /*
4294 * Skip leading blank chars of the collapsed string.
4295 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004296 while IS_WSP_BLANK_CH(*y)
4297 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004298
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004299 while ((*x != 0) && (*y != 0)) {
4300 if IS_WSP_BLANK_CH(*y) {
4301 if (! IS_WSP_SPACE_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004302 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004303 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004304 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004305 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004306 if (invert)
4307 return(1);
4308 else
4309 return(-1);
4310 } else {
4311 if (invert)
4312 return(-1);
4313 else
4314 return(1);
4315 }
4316 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004317 x++;
4318 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004319 /*
4320 * Skip contiguous blank chars of the collapsed string.
4321 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004322 while IS_WSP_BLANK_CH(*y)
4323 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004324 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004325 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004326 if (tmp < 0) {
4327 if (invert)
4328 return(1);
4329 else
4330 return(-1);
4331 }
4332 if (tmp > 0) {
4333 if (invert)
4334 return(-1);
4335 else
4336 return(1);
4337 }
4338 }
4339 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004340 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004341 if (invert)
4342 return(-1);
4343 else
4344 return(1);
4345 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004346 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004347 /*
4348 * Skip trailing blank chars of the collapsed string.
4349 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004350 while IS_WSP_BLANK_CH(*y)
4351 y++;
4352 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004353 if (invert)
4354 return(1);
4355 else
4356 return(-1);
4357 }
4358 }
4359 return(0);
4360}
4361
4362/**
4363 * xmlSchemaComparePreserveCollapseStrings:
4364 * @x: a first string value
4365 * @y: a second string value
4366 *
4367 * Compare 2 string for their normalized values.
4368 * @x is a string with whitespace of "preserve", @y is
4369 * a string with a whitespace of "collapse". I.e. @x could
4370 * be an "xsd:string" and @y an "xsd:normalizedString".
4371 *
4372 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4373 * case of error
4374 */
4375static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004376xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
4377 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004378 int invert)
4379{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004380 int tmp;
4381
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004382 /*
4383 * Skip leading blank chars of the collapsed string.
4384 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004385 while IS_WSP_BLANK_CH(*y)
4386 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004387
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004388 while ((*x != 0) && (*y != 0)) {
4389 if IS_WSP_BLANK_CH(*y) {
4390 if (! IS_WSP_BLANK_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004391 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004392 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004393 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004394 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004395 if (invert)
4396 return(1);
4397 else
4398 return(-1);
4399 } else {
4400 if (invert)
4401 return(-1);
4402 else
4403 return(1);
4404 }
4405 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004406 x++;
4407 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004408 /*
4409 * Skip contiguous blank chars of the collapsed string.
4410 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004411 while IS_WSP_BLANK_CH(*y)
4412 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004413 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004414 if IS_WSP_BLANK_CH(*x) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004415 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004416 * The xv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004417 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004418 if ((0x20 - *y) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004419 if (invert)
4420 return(1);
4421 else
4422 return(-1);
4423 } else {
4424 if (invert)
4425 return(-1);
4426 else
4427 return(1);
4428 }
4429 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004430 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004431 if (tmp < 0)
4432 return(-1);
4433 if (tmp > 0)
4434 return(1);
4435 }
4436 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004437 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004438 if (invert)
4439 return(-1);
4440 else
4441 return(1);
4442 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004443 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004444 /*
4445 * Skip trailing blank chars of the collapsed string.
4446 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004447 while IS_WSP_BLANK_CH(*y)
4448 y++;
4449 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004450 if (invert)
4451 return(1);
4452 else
4453 return(-1);
4454 }
4455 }
4456 return(0);
4457}
4458
4459
4460/**
4461 * xmlSchemaCompareReplacedStrings:
4462 * @x: a first string value
4463 * @y: a second string value
4464 *
4465 * Compare 2 string for their normalized values.
4466 *
4467 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4468 * case of error
4469 */
4470static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004471xmlSchemaCompareReplacedStrings(const xmlChar *x,
4472 const xmlChar *y)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004473{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004474 int tmp;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004475
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004476 while ((*x != 0) && (*y != 0)) {
4477 if IS_WSP_BLANK_CH(*y) {
4478 if (! IS_WSP_BLANK_CH(*x)) {
4479 if ((*x - 0x20) < 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004480 return(-1);
4481 else
4482 return(1);
4483 }
4484 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004485 if IS_WSP_BLANK_CH(*x) {
4486 if ((0x20 - *y) < 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004487 return(-1);
4488 else
4489 return(1);
4490 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004491 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004492 if (tmp < 0)
4493 return(-1);
4494 if (tmp > 0)
4495 return(1);
4496 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004497 x++;
4498 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004499 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004500 if (*x != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004501 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004502 if (*y != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004503 return(-1);
4504 return(0);
4505}
4506
4507/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004508 * xmlSchemaCompareNormStrings:
4509 * @x: a first string value
4510 * @y: a second string value
4511 *
4512 * Compare 2 string for their normalized values.
4513 *
4514 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4515 * case of error
4516 */
4517static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004518xmlSchemaCompareNormStrings(const xmlChar *x,
4519 const xmlChar *y) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00004520 int tmp;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004521
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004522 while (IS_BLANK_CH(*x)) x++;
4523 while (IS_BLANK_CH(*y)) y++;
4524 while ((*x != 0) && (*y != 0)) {
4525 if (IS_BLANK_CH(*x)) {
4526 if (!IS_BLANK_CH(*y)) {
4527 tmp = *x - *y;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004528 return(tmp);
4529 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004530 while (IS_BLANK_CH(*x)) x++;
4531 while (IS_BLANK_CH(*y)) y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004532 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004533 tmp = *x++ - *y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004534 if (tmp < 0)
4535 return(-1);
4536 if (tmp > 0)
4537 return(1);
4538 }
4539 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004540 if (*x != 0) {
4541 while (IS_BLANK_CH(*x)) x++;
4542 if (*x != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004543 return(1);
4544 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004545 if (*y != 0) {
4546 while (IS_BLANK_CH(*y)) y++;
4547 if (*y != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004548 return(-1);
4549 }
4550 return(0);
4551}
4552
4553/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004554 * xmlSchemaCompareFloats:
4555 * @x: a first float or double value
4556 * @y: a second float or double value
4557 *
4558 * Compare 2 values
4559 *
4560 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4561 * case of error
4562 */
4563static int
4564xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4565 double d1, d2;
4566
4567 if ((x == NULL) || (y == NULL))
4568 return(-2);
4569
4570 /*
4571 * Cast everything to doubles.
4572 */
4573 if (x->type == XML_SCHEMAS_DOUBLE)
4574 d1 = x->value.d;
4575 else if (x->type == XML_SCHEMAS_FLOAT)
4576 d1 = x->value.f;
4577 else
4578 return(-2);
4579
4580 if (y->type == XML_SCHEMAS_DOUBLE)
4581 d2 = y->value.d;
4582 else if (y->type == XML_SCHEMAS_FLOAT)
4583 d2 = y->value.f;
4584 else
4585 return(-2);
4586
4587 /*
4588 * Check for special cases.
4589 */
4590 if (xmlXPathIsNaN(d1)) {
4591 if (xmlXPathIsNaN(d2))
4592 return(0);
4593 return(1);
4594 }
4595 if (xmlXPathIsNaN(d2))
4596 return(-1);
4597 if (d1 == xmlXPathPINF) {
4598 if (d2 == xmlXPathPINF)
4599 return(0);
4600 return(1);
4601 }
4602 if (d2 == xmlXPathPINF)
4603 return(-1);
4604 if (d1 == xmlXPathNINF) {
4605 if (d2 == xmlXPathNINF)
4606 return(0);
4607 return(-1);
4608 }
4609 if (d2 == xmlXPathNINF)
4610 return(1);
4611
4612 /*
4613 * basic tests, the last one we should have equality, but
4614 * portability is more important than speed and handling
4615 * NaN or Inf in a portable way is always a challenge, so ...
4616 */
4617 if (d1 < d2)
4618 return(-1);
4619 if (d1 > d2)
4620 return(1);
4621 if (d1 == d2)
4622 return(0);
4623 return(2);
4624}
4625
4626/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004627 * xmlSchemaCompareValues:
4628 * @x: a first value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004629 * @xvalue: the first value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004630 * @xwtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004631 * @y: a second value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004632 * @xvalue: the second value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004633 * @ywtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004634 *
4635 * Compare 2 values
4636 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004637 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4638 * comparable and -2 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004639 */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004640static int
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004641xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
4642 xmlSchemaValPtr x,
4643 const xmlChar *xvalue,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004644 xmlSchemaWhitespaceValueType xws,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004645 xmlSchemaValType ytype,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004646 xmlSchemaValPtr y,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004647 const xmlChar *yvalue,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004648 xmlSchemaWhitespaceValueType yws)
4649{
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004650 switch (xtype) {
Daniel Veillard80b19092003-03-28 13:29:53 +00004651 case XML_SCHEMAS_UNKNOWN:
William M. Brack2f2a6632004-08-20 23:09:47 +00004652 case XML_SCHEMAS_ANYTYPE:
Daniel Veillard80b19092003-03-28 13:29:53 +00004653 return(-2);
4654 case XML_SCHEMAS_INTEGER:
4655 case XML_SCHEMAS_NPINTEGER:
4656 case XML_SCHEMAS_NINTEGER:
4657 case XML_SCHEMAS_NNINTEGER:
4658 case XML_SCHEMAS_PINTEGER:
4659 case XML_SCHEMAS_INT:
4660 case XML_SCHEMAS_UINT:
4661 case XML_SCHEMAS_LONG:
4662 case XML_SCHEMAS_ULONG:
4663 case XML_SCHEMAS_SHORT:
4664 case XML_SCHEMAS_USHORT:
4665 case XML_SCHEMAS_BYTE:
4666 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00004667 case XML_SCHEMAS_DECIMAL:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004668 if ((x == NULL) || (y == NULL))
4669 return(-2);
4670 if (ytype == xtype)
Daniel Veillard80b19092003-03-28 13:29:53 +00004671 return(xmlSchemaCompareDecimals(x, y));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004672 if ((ytype == XML_SCHEMAS_DECIMAL) ||
4673 (ytype == XML_SCHEMAS_INTEGER) ||
4674 (ytype == XML_SCHEMAS_NPINTEGER) ||
4675 (ytype == XML_SCHEMAS_NINTEGER) ||
4676 (ytype == XML_SCHEMAS_NNINTEGER) ||
4677 (ytype == XML_SCHEMAS_PINTEGER) ||
4678 (ytype == XML_SCHEMAS_INT) ||
4679 (ytype == XML_SCHEMAS_UINT) ||
4680 (ytype == XML_SCHEMAS_LONG) ||
4681 (ytype == XML_SCHEMAS_ULONG) ||
4682 (ytype == XML_SCHEMAS_SHORT) ||
4683 (ytype == XML_SCHEMAS_USHORT) ||
4684 (ytype == XML_SCHEMAS_BYTE) ||
4685 (ytype == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00004686 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004687 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00004688 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004689 if ((x == NULL) || (y == NULL))
4690 return(-2);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004691 if (ytype == XML_SCHEMAS_DURATION)
Daniel Veillard070803b2002-05-03 07:29:38 +00004692 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004693 return(-2);
4694 case XML_SCHEMAS_TIME:
4695 case XML_SCHEMAS_GDAY:
4696 case XML_SCHEMAS_GMONTH:
4697 case XML_SCHEMAS_GMONTHDAY:
4698 case XML_SCHEMAS_GYEAR:
4699 case XML_SCHEMAS_GYEARMONTH:
4700 case XML_SCHEMAS_DATE:
4701 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004702 if ((x == NULL) || (y == NULL))
4703 return(-2);
4704 if ((ytype == XML_SCHEMAS_DATETIME) ||
4705 (ytype == XML_SCHEMAS_TIME) ||
4706 (ytype == XML_SCHEMAS_GDAY) ||
4707 (ytype == XML_SCHEMAS_GMONTH) ||
4708 (ytype == XML_SCHEMAS_GMONTHDAY) ||
4709 (ytype == XML_SCHEMAS_GYEAR) ||
4710 (ytype == XML_SCHEMAS_DATE) ||
4711 (ytype == XML_SCHEMAS_GYEARMONTH))
Daniel Veillard5a872412002-05-22 06:40:27 +00004712 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004713 return (-2);
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +00004714 /*
4715 * Note that we will support comparison of string types against
4716 * anySimpleType as well.
4717 */
4718 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004719 case XML_SCHEMAS_STRING:
4720 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00004721 case XML_SCHEMAS_TOKEN:
4722 case XML_SCHEMAS_LANGUAGE:
4723 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00004724 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00004725 case XML_SCHEMAS_NCNAME:
4726 case XML_SCHEMAS_ID:
4727 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00004728 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00004729 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004730 {
4731 const xmlChar *xv, *yv;
4732
4733 if (x == NULL)
4734 xv = xvalue;
4735 else
4736 xv = x->value.str;
4737 if (y == NULL)
4738 yv = yvalue;
4739 else
4740 yv = y->value.str;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004741 /*
4742 * TODO: Compare those against QName.
4743 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004744 if (ytype == XML_SCHEMAS_QNAME) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004745 TODO
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004746 if (y == NULL)
4747 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004748 return (-2);
4749 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004750 if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
4751 (ytype == XML_SCHEMAS_STRING) ||
4752 (ytype == XML_SCHEMAS_NORMSTRING) ||
4753 (ytype == XML_SCHEMAS_TOKEN) ||
4754 (ytype == XML_SCHEMAS_LANGUAGE) ||
4755 (ytype == XML_SCHEMAS_NMTOKEN) ||
4756 (ytype == XML_SCHEMAS_NAME) ||
4757 (ytype == XML_SCHEMAS_NCNAME) ||
4758 (ytype == XML_SCHEMAS_ID) ||
4759 (ytype == XML_SCHEMAS_IDREF) ||
4760 (ytype == XML_SCHEMAS_ENTITY) ||
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004761 (ytype == XML_SCHEMAS_ANYURI)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004762
4763 if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4764
4765 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4766 /* TODO: What about x < y or x > y. */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004767 if (xmlStrEqual(xv, yv))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004768 return (0);
4769 else
4770 return (2);
4771 } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004772 return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004773 else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004774 return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004775
4776 } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
4777
4778 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004779 return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004780 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004781 return (xmlSchemaCompareReplacedStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004782 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004783 return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004784
4785 } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
4786
4787 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004788 return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004789 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004790 return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004791 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004792 return (xmlSchemaCompareNormStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004793 } else
4794 return (-2);
4795
4796 }
Daniel Veillardc4c21552003-03-29 10:53:38 +00004797 return (-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004798 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004799 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004800 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004801 if ((x == NULL) || (y == NULL))
4802 return(-2);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004803 if ((ytype == XML_SCHEMAS_QNAME) ||
4804 (ytype == XML_SCHEMAS_NOTATION)) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004805 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
4806 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
4807 return(0);
4808 return(2);
4809 }
4810 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004811 case XML_SCHEMAS_FLOAT:
4812 case XML_SCHEMAS_DOUBLE:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004813 if ((x == NULL) || (y == NULL))
4814 return(-2);
4815 if ((ytype == XML_SCHEMAS_FLOAT) ||
4816 (ytype == XML_SCHEMAS_DOUBLE))
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004817 return (xmlSchemaCompareFloats(x, y));
4818 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004819 case XML_SCHEMAS_BOOLEAN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004820 if ((x == NULL) || (y == NULL))
4821 return(-2);
4822 if (ytype == XML_SCHEMAS_BOOLEAN) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004823 if (x->value.b == y->value.b)
4824 return(0);
4825 if (x->value.b == 0)
4826 return(-1);
4827 return(1);
4828 }
4829 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00004830 case XML_SCHEMAS_HEXBINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004831 if ((x == NULL) || (y == NULL))
4832 return(-2);
4833 if (ytype == XML_SCHEMAS_HEXBINARY) {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00004834 if (x->value.hex.total == y->value.hex.total) {
4835 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
4836 if (ret > 0)
4837 return(1);
4838 else if (ret == 0)
4839 return(0);
4840 }
4841 else if (x->value.hex.total > y->value.hex.total)
4842 return(1);
4843
4844 return(-1);
4845 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00004846 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004847 case XML_SCHEMAS_BASE64BINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004848 if ((x == NULL) || (y == NULL))
4849 return(-2);
4850 if (ytype == XML_SCHEMAS_BASE64BINARY) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004851 if (x->value.base64.total == y->value.base64.total) {
4852 int ret = xmlStrcmp(x->value.base64.str,
4853 y->value.base64.str);
4854 if (ret > 0)
4855 return(1);
4856 else if (ret == 0)
4857 return(0);
Daniel Veillardd369b132005-07-14 15:54:44 +00004858 else
4859 return(-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004860 }
4861 else if (x->value.base64.total > y->value.base64.total)
4862 return(1);
4863 else
4864 return(-1);
4865 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004866 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004867 case XML_SCHEMAS_IDREFS:
4868 case XML_SCHEMAS_ENTITIES:
4869 case XML_SCHEMAS_NMTOKENS:
4870 TODO
4871 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004872 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004873 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00004874}
4875
4876/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004877 * xmlSchemaCompareValues:
4878 * @x: a first value
4879 * @y: a second value
4880 *
4881 * Compare 2 values
4882 *
4883 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4884 * case of error
4885 */
4886int
4887xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4888 xmlSchemaWhitespaceValueType xws, yws;
4889
Daniel Veillard5e094142005-02-18 19:36:12 +00004890 if ((x == NULL) || (y == NULL))
4891 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004892 if (x->type == XML_SCHEMAS_STRING)
4893 xws = XML_SCHEMA_WHITESPACE_PRESERVE;
4894 else if (x->type == XML_SCHEMAS_NORMSTRING)
4895 xws = XML_SCHEMA_WHITESPACE_REPLACE;
4896 else
4897 xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4898
4899 if (y->type == XML_SCHEMAS_STRING)
4900 yws = XML_SCHEMA_WHITESPACE_PRESERVE;
4901 else if (x->type == XML_SCHEMAS_NORMSTRING)
4902 yws = XML_SCHEMA_WHITESPACE_REPLACE;
4903 else
4904 yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4905
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004906 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4907 y, NULL, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004908}
4909
4910/**
4911 * xmlSchemaCompareValuesWhtsp:
4912 * @x: a first value
4913 * @xws: the whitespace value of x
4914 * @y: a second value
4915 * @yws: the whitespace value of y
4916 *
4917 * Compare 2 values
4918 *
4919 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4920 * case of error
4921 */
4922int
4923xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004924 xmlSchemaWhitespaceValueType xws,
4925 xmlSchemaValPtr y,
4926 xmlSchemaWhitespaceValueType yws)
4927{
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00004928 if ((x == NULL) || (y == NULL))
4929 return(-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004930 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4931 y, NULL, yws));
4932}
4933
4934/**
4935 * xmlSchemaCompareValuesWhtspExt:
4936 * @x: a first value
4937 * @xws: the whitespace value of x
4938 * @y: a second value
4939 * @yws: the whitespace value of y
4940 *
4941 * Compare 2 values
4942 *
4943 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4944 * case of error
4945 */
4946static int
4947xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
4948 xmlSchemaValPtr x,
4949 const xmlChar *xvalue,
4950 xmlSchemaWhitespaceValueType xws,
4951 xmlSchemaValType ytype,
4952 xmlSchemaValPtr y,
4953 const xmlChar *yvalue,
4954 xmlSchemaWhitespaceValueType yws)
4955{
4956 return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
4957 yvalue, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004958}
4959
4960/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004961 * xmlSchemaNormLen:
4962 * @value: a string
4963 *
4964 * Computes the UTF8 length of the normalized value of the string
4965 *
4966 * Returns the length or -1 in case of error.
4967 */
4968static int
4969xmlSchemaNormLen(const xmlChar *value) {
4970 const xmlChar *utf;
4971 int ret = 0;
4972
4973 if (value == NULL)
4974 return(-1);
4975 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00004976 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004977 while (*utf != 0) {
4978 if (utf[0] & 0x80) {
4979 if ((utf[1] & 0xc0) != 0x80)
4980 return(-1);
4981 if ((utf[0] & 0xe0) == 0xe0) {
4982 if ((utf[2] & 0xc0) != 0x80)
4983 return(-1);
4984 if ((utf[0] & 0xf0) == 0xf0) {
4985 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
4986 return(-1);
4987 utf += 4;
4988 } else {
4989 utf += 3;
4990 }
4991 } else {
4992 utf += 2;
4993 }
William M. Brack76e95df2003-10-18 16:20:14 +00004994 } else if (IS_BLANK_CH(*utf)) {
4995 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004996 if (*utf == 0)
4997 break;
4998 } else {
4999 utf++;
5000 }
5001 ret++;
5002 }
5003 return(ret);
5004}
5005
Daniel Veillard6927b102004-10-27 17:29:04 +00005006/**
5007 * xmlSchemaGetFacetValueAsULong:
5008 * @facet: an schemas type facet
5009 *
5010 * Extract the value of a facet
5011 *
5012 * Returns the value as a long
5013 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005014unsigned long
5015xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
5016{
5017 /*
5018 * TODO: Check if this is a decimal.
5019 */
William M. Brack094dd862004-11-14 14:28:34 +00005020 if (facet == NULL)
5021 return 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005022 return ((unsigned long) facet->val->value.decimal.lo);
5023}
5024
Daniel Veillardc4c21552003-03-29 10:53:38 +00005025/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00005026 * xmlSchemaValidateListSimpleTypeFacet:
5027 * @facet: the facet to check
5028 * @value: the lexical repr of the value to validate
5029 * @actualLen: the number of list items
5030 * @expectedLen: the resulting expected number of list items
5031 *
5032 * Checks the value of a list simple type against a facet.
5033 *
5034 * Returns 0 if the value is valid, a positive error code
5035 * number otherwise and -1 in case of an internal error.
5036 */
5037int
5038xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
5039 const xmlChar *value,
5040 unsigned long actualLen,
5041 unsigned long *expectedLen)
5042{
Daniel Veillardce682bc2004-11-05 17:22:25 +00005043 if (facet == NULL)
5044 return(-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005045 /*
5046 * TODO: Check if this will work with large numbers.
5047 * (compare value.decimal.mi and value.decimal.hi as well?).
5048 */
5049 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5050 if (actualLen != facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005051 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005052 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005053 return (XML_SCHEMAV_CVC_LENGTH_VALID);
5054 }
5055 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5056 if (actualLen < facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005057 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005058 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005059 return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
5060 }
5061 } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
5062 if (actualLen > facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005063 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005064 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005065 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5066 }
5067 } else
5068 /*
5069 * NOTE: That we can pass NULL as xmlSchemaValPtr to
5070 * xmlSchemaValidateFacet, since the remaining facet types
5071 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
5072 */
5073 return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
5074 return (0);
5075}
5076
5077/**
Daniel Veillard6927b102004-10-27 17:29:04 +00005078 * xmlSchemaValidateLengthFacet:
Daniel Veillardc0826a72004-08-10 14:17:33 +00005079 * @type: the built-in type
5080 * @facet: the facet to check
5081 * @value: the lexical repr. of the value to be validated
5082 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005083 * @ws: the whitespace type of the value
5084 * @length: the actual length of the value
5085 *
5086 * Checka a value against a "length", "minLength" and "maxLength"
5087 * facet; sets @length to the computed length of @value.
5088 *
5089 * Returns 0 if the value is valid, a positive error code
5090 * otherwise and -1 in case of an internal or API error.
5091 */
5092static int
5093xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
5094 xmlSchemaTypeType valType,
5095 const xmlChar *value,
5096 xmlSchemaValPtr val,
5097 unsigned long *length,
5098 xmlSchemaWhitespaceValueType ws)
5099{
5100 unsigned int len = 0;
5101
5102 if ((length == NULL) || (facet == NULL))
5103 return (-1);
5104 *length = 0;
5105 if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
5106 (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
5107 (facet->type != XML_SCHEMA_FACET_MINLENGTH))
5108 return (-1);
5109
5110 /*
5111 * TODO: length, maxLength and minLength must be of type
5112 * nonNegativeInteger only. Check if decimal is used somehow.
5113 */
5114 if ((facet->val == NULL) ||
5115 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5116 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5117 (facet->val->value.decimal.frac != 0)) {
5118 return(-1);
5119 }
5120 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
5121 len = val->value.hex.total;
5122 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5123 len = val->value.base64.total;
5124 else {
5125 switch (valType) {
5126 case XML_SCHEMAS_STRING:
5127 case XML_SCHEMAS_NORMSTRING:
5128 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5129 /*
5130 * This is to ensure API compatibility with the old
5131 * xmlSchemaValidateLengthFacet(). Anyway, this was and
5132 * is not the correct handling.
5133 * TODO: Get rid of this case somehow.
5134 */
5135 if (valType == XML_SCHEMAS_STRING)
5136 len = xmlUTF8Strlen(value);
5137 else
5138 len = xmlSchemaNormLen(value);
5139 } else if (value != NULL) {
5140 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5141 len = xmlSchemaNormLen(value);
5142 else
5143 /*
5144 * Should be OK for "preserve" as well.
5145 */
5146 len = xmlUTF8Strlen(value);
5147 }
5148 break;
5149 case XML_SCHEMAS_IDREF:
5150 case XML_SCHEMAS_TOKEN:
5151 case XML_SCHEMAS_LANGUAGE:
5152 case XML_SCHEMAS_NMTOKEN:
5153 case XML_SCHEMAS_NAME:
5154 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005155 case XML_SCHEMAS_ID:
5156 /*
5157 * FIXME: What exactly to do with anyURI?
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005158 */
5159 case XML_SCHEMAS_ANYURI:
5160 if (value != NULL)
5161 len = xmlSchemaNormLen(value);
5162 break;
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005163 case XML_SCHEMAS_QNAME:
5164 case XML_SCHEMAS_NOTATION:
5165 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005166 * For QName and NOTATION, those facets are
5167 * deprecated and should be ignored.
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005168 */
5169 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005170 default:
5171 TODO
5172 }
5173 }
5174 *length = (unsigned long) len;
5175 /*
5176 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
5177 */
5178 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5179 if (len != facet->val->value.decimal.lo)
5180 return(XML_SCHEMAV_CVC_LENGTH_VALID);
5181 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5182 if (len < facet->val->value.decimal.lo)
5183 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
5184 } else {
5185 if (len > facet->val->value.decimal.lo)
5186 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5187 }
5188
5189 return (0);
5190}
5191
5192/**
5193 * xmlSchemaValidateLengthFacet:
5194 * @type: the built-in type
5195 * @facet: the facet to check
5196 * @value: the lexical repr. of the value to be validated
5197 * @val: the precomputed value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005198 * @length: the actual length of the value
5199 *
5200 * Checka a value against a "length", "minLength" and "maxLength"
5201 * facet; sets @length to the computed length of @value.
5202 *
5203 * Returns 0 if the value is valid, a positive error code
5204 * otherwise and -1 in case of an internal or API error.
5205 */
5206int
5207xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
5208 xmlSchemaFacetPtr facet,
5209 const xmlChar *value,
5210 xmlSchemaValPtr val,
5211 unsigned long *length)
5212{
Daniel Veillardcc5e2332005-03-16 21:55:35 +00005213 if (type == NULL)
5214 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005215 return (xmlSchemaValidateLengthFacetInternal(facet,
5216 type->builtInType, value, val, length,
5217 XML_SCHEMA_WHITESPACE_UNKNOWN));
5218}
Daniel Veillardc0826a72004-08-10 14:17:33 +00005219
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005220/**
5221 * xmlSchemaValidateLengthFacetWhtsp:
5222 * @facet: the facet to check
5223 * @valType: the built-in type
5224 * @value: the lexical repr. of the value to be validated
5225 * @val: the precomputed value
5226 * @ws: the whitespace type of the value
5227 * @length: the actual length of the value
5228 *
5229 * Checka a value against a "length", "minLength" and "maxLength"
5230 * facet; sets @length to the computed length of @value.
5231 *
5232 * Returns 0 if the value is valid, a positive error code
5233 * otherwise and -1 in case of an internal or API error.
5234 */
5235int
5236xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
5237 xmlSchemaValType valType,
5238 const xmlChar *value,
5239 xmlSchemaValPtr val,
5240 unsigned long *length,
5241 xmlSchemaWhitespaceValueType ws)
5242{
5243 return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
5244 length, ws));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005245}
5246
5247/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005248 * xmlSchemaValidateFacetInternal:
Daniel Veillard4255d502002-04-16 15:50:10 +00005249 * @facet: the facet to check
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005250 * @fws: the whitespace type of the facet's value
5251 * @valType: the built-in type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005252 * @value: the lexical repr of the value to validate
5253 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005254 * @ws: the whitespace type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005255 *
5256 * Check a value against a facet condition
5257 *
5258 * Returns 0 if the element is schemas valid, a positive error code
5259 * number otherwise and -1 in case of internal or API error.
5260 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005261static int
5262xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
5263 xmlSchemaWhitespaceValueType fws,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005264 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005265 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005266 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005267 xmlSchemaWhitespaceValueType ws)
Daniel Veillard4255d502002-04-16 15:50:10 +00005268{
5269 int ret;
5270
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005271 if (facet == NULL)
5272 return(-1);
5273
Daniel Veillard4255d502002-04-16 15:50:10 +00005274 switch (facet->type) {
5275 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005276 /*
5277 * NOTE that for patterns, the @value needs to be the normalized
5278 * value, *not* the lexical initial value or the canonical value.
5279 */
5280 if (value == NULL)
5281 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005282 ret = xmlRegexpExec(facet->regexp, value);
5283 if (ret == 1)
5284 return(0);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005285 if (ret == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005286 return(XML_SCHEMAV_CVC_PATTERN_VALID);
Daniel Veillard4255d502002-04-16 15:50:10 +00005287 return(ret);
5288 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
5289 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005290 if (ret == -2)
Daniel Veillard4255d502002-04-16 15:50:10 +00005291 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005292 if (ret == -1)
5293 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005294 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005295 case XML_SCHEMA_FACET_MAXINCLUSIVE:
5296 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005297 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005298 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005299 if ((ret == -1) || (ret == 0))
5300 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005301 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005302 case XML_SCHEMA_FACET_MINEXCLUSIVE:
5303 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005304 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005305 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005306 if (ret == 1)
5307 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005308 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005309 case XML_SCHEMA_FACET_MININCLUSIVE:
5310 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005311 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005312 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005313 if ((ret == 1) || (ret == 0))
5314 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005315 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
Daniel Veillard8651f532002-04-17 09:06:27 +00005316 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005317 /* TODO whitespaces */
Daniel Veillard01fa6152004-06-29 17:04:39 +00005318 /*
5319 * NOTE: Whitespace should be handled to normalize
5320 * the value to be validated against a the facets;
5321 * not to normalize the value in-between.
5322 */
Daniel Veillard8651f532002-04-17 09:06:27 +00005323 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00005324 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005325 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5326 /*
5327 * This is to ensure API compatibility with the old
5328 * xmlSchemaValidateFacet().
5329 * TODO: Get rid of this case.
5330 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005331 if ((facet->value != NULL) &&
5332 (xmlStrEqual(facet->value, value)))
5333 return(0);
5334 } else {
5335 ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
5336 facet->val, facet->value, fws, valType, val,
5337 value, ws);
5338 if (ret == -2)
5339 return(-1);
5340 if (ret == 0)
5341 return(0);
5342 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005343 return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005344 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005345 /*
5346 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5347 * then any {value} is facet-valid."
5348 */
5349 if ((valType == XML_SCHEMAS_QNAME) ||
5350 (valType == XML_SCHEMAS_NOTATION))
5351 return (0);
5352 /* No break on purpose. */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005353 case XML_SCHEMA_FACET_MAXLENGTH:
5354 case XML_SCHEMA_FACET_MINLENGTH: {
5355 unsigned int len = 0;
5356
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005357 if ((valType == XML_SCHEMAS_QNAME) ||
5358 (valType == XML_SCHEMAS_NOTATION))
5359 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005360 /*
5361 * TODO: length, maxLength and minLength must be of type
5362 * nonNegativeInteger only. Check if decimal is used somehow.
5363 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005364 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005365 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5366 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005367 (facet->val->value.decimal.frac != 0)) {
5368 return(-1);
5369 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005370 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005371 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00005372 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5373 len = val->value.base64.total;
5374 else {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005375 switch (valType) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005376 case XML_SCHEMAS_STRING:
5377 case XML_SCHEMAS_NORMSTRING:
5378 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5379 /*
5380 * This is to ensure API compatibility with the old
5381 * xmlSchemaValidateFacet(). Anyway, this was and
5382 * is not the correct handling.
5383 * TODO: Get rid of this case somehow.
5384 */
5385 if (valType == XML_SCHEMAS_STRING)
5386 len = xmlUTF8Strlen(value);
5387 else
5388 len = xmlSchemaNormLen(value);
5389 } else if (value != NULL) {
5390 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5391 len = xmlSchemaNormLen(value);
5392 else
5393 /*
5394 * Should be OK for "preserve" as well.
5395 */
5396 len = xmlUTF8Strlen(value);
5397 }
5398 break;
5399 case XML_SCHEMAS_IDREF:
Daniel Veillard560c2a42003-07-06 21:13:49 +00005400 case XML_SCHEMAS_TOKEN:
5401 case XML_SCHEMAS_LANGUAGE:
5402 case XML_SCHEMAS_NMTOKEN:
5403 case XML_SCHEMAS_NAME:
5404 case XML_SCHEMAS_NCNAME:
5405 case XML_SCHEMAS_ID:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005406 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005407 if (value != NULL)
5408 len = xmlSchemaNormLen(value);
5409 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00005410 default:
5411 TODO
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005412 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005413 }
5414 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005415 if (len != facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005416 return(XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005417 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005418 if (len < facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005419 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005420 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005421 if (len > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005422 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005423 }
5424 break;
5425 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005426 case XML_SCHEMA_FACET_TOTALDIGITS:
5427 case XML_SCHEMA_FACET_FRACTIONDIGITS:
5428
5429 if ((facet->val == NULL) ||
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00005430 ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
Daniel Veillard560c2a42003-07-06 21:13:49 +00005431 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5432 (facet->val->value.decimal.frac != 0)) {
5433 return(-1);
5434 }
5435 if ((val == NULL) ||
5436 ((val->type != XML_SCHEMAS_DECIMAL) &&
5437 (val->type != XML_SCHEMAS_INTEGER) &&
5438 (val->type != XML_SCHEMAS_NPINTEGER) &&
5439 (val->type != XML_SCHEMAS_NINTEGER) &&
5440 (val->type != XML_SCHEMAS_NNINTEGER) &&
5441 (val->type != XML_SCHEMAS_PINTEGER) &&
5442 (val->type != XML_SCHEMAS_INT) &&
5443 (val->type != XML_SCHEMAS_UINT) &&
5444 (val->type != XML_SCHEMAS_LONG) &&
5445 (val->type != XML_SCHEMAS_ULONG) &&
5446 (val->type != XML_SCHEMAS_SHORT) &&
5447 (val->type != XML_SCHEMAS_USHORT) &&
5448 (val->type != XML_SCHEMAS_BYTE) &&
5449 (val->type != XML_SCHEMAS_UBYTE))) {
5450 return(-1);
5451 }
5452 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
5453 if (val->value.decimal.total > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005454 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005455
5456 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
5457 if (val->value.decimal.frac > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005458 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005459 }
5460 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00005461 default:
5462 TODO
5463 }
5464 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00005465
Daniel Veillard4255d502002-04-16 15:50:10 +00005466}
5467
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005468/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005469 * xmlSchemaValidateFacet:
5470 * @base: the base type
5471 * @facet: the facet to check
5472 * @value: the lexical repr of the value to validate
5473 * @val: the precomputed value
5474 *
5475 * Check a value against a facet condition
5476 *
5477 * Returns 0 if the element is schemas valid, a positive error code
5478 * number otherwise and -1 in case of internal or API error.
5479 */
5480int
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005481xmlSchemaValidateFacet(xmlSchemaTypePtr base,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005482 xmlSchemaFacetPtr facet,
5483 const xmlChar *value,
5484 xmlSchemaValPtr val)
5485{
5486 /*
5487 * This tries to ensure API compatibility regarding the old
5488 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5489 * xmlSchemaValidateFacetWhtsp().
5490 */
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005491 if (val != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005492 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005493 XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005494 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005495 else if (base != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005496 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005497 XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005498 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005499 return(-1);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005500}
5501
5502/**
5503 * xmlSchemaValidateFacetWhtsp:
5504 * @facet: the facet to check
5505 * @fws: the whitespace type of the facet's value
5506 * @valType: the built-in type of the value
5507 * @value: the lexical (or normalized for pattern) repr of the value to validate
5508 * @val: the precomputed value
5509 * @ws: the whitespace type of the value
5510 *
5511 * Check a value against a facet condition. This takes value normalization
5512 * according to the specified whitespace types into account.
5513 * Note that @value needs to be the *normalized* value if the facet
5514 * is of type "pattern".
5515 *
5516 * Returns 0 if the element is schemas valid, a positive error code
5517 * number otherwise and -1 in case of internal or API error.
5518 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005519int
5520xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
5521 xmlSchemaWhitespaceValueType fws,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005522 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005523 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005524 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005525 xmlSchemaWhitespaceValueType ws)
5526{
5527 return(xmlSchemaValidateFacetInternal(facet, fws, valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005528 value, val, ws));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005529}
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005530
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005531#if 0
5532#ifndef DBL_DIG
5533#define DBL_DIG 16
5534#endif
5535#ifndef DBL_EPSILON
5536#define DBL_EPSILON 1E-9
5537#endif
5538
5539#define INTEGER_DIGITS DBL_DIG
5540#define FRACTION_DIGITS (DBL_DIG + 1)
5541#define EXPONENT_DIGITS (3 + 2)
5542
5543/**
5544 * xmlXPathFormatNumber:
5545 * @number: number to format
5546 * @buffer: output buffer
5547 * @buffersize: size of output buffer
5548 *
5549 * Convert the number into a string representation.
5550 */
5551static void
5552xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
5553{
5554 switch (xmlXPathIsInf(number)) {
5555 case 1:
5556 if (buffersize > (int)sizeof("INF"))
5557 snprintf(buffer, buffersize, "INF");
5558 break;
5559 case -1:
5560 if (buffersize > (int)sizeof("-INF"))
5561 snprintf(buffer, buffersize, "-INF");
5562 break;
5563 default:
5564 if (xmlXPathIsNaN(number)) {
5565 if (buffersize > (int)sizeof("NaN"))
5566 snprintf(buffer, buffersize, "NaN");
5567 } else if (number == 0) {
5568 snprintf(buffer, buffersize, "0.0E0");
5569 } else {
5570 /* 3 is sign, decimal point, and terminating zero */
5571 char work[DBL_DIG + EXPONENT_DIGITS + 3];
5572 int integer_place, fraction_place;
5573 char *ptr;
5574 char *after_fraction;
5575 double absolute_value;
5576 int size;
5577
5578 absolute_value = fabs(number);
5579
5580 /*
5581 * Result is in work, and after_fraction points
5582 * just past the fractional part.
5583 * Use scientific notation
5584 */
5585 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
5586 fraction_place = DBL_DIG - 1;
5587 snprintf(work, sizeof(work),"%*.*e",
5588 integer_place, fraction_place, number);
5589 after_fraction = strchr(work + DBL_DIG, 'e');
5590 /* Remove fractional trailing zeroes */
5591 ptr = after_fraction;
5592 while (*(--ptr) == '0')
5593 ;
5594 if (*ptr != '.')
5595 ptr++;
5596 while ((*ptr++ = *after_fraction++) != 0);
5597
5598 /* Finally copy result back to caller */
5599 size = strlen(work) + 1;
5600 if (size > buffersize) {
5601 work[buffersize - 1] = 0;
5602 size = buffersize;
5603 }
5604 memmove(buffer, work, size);
5605 }
5606 break;
5607 }
5608}
5609#endif
5610
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005611/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005612 * xmlSchemaGetCanonValue:
5613 * @val: the precomputed value
5614 * @retValue: the returned value
5615 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005616 * Get a the cononical lexical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005617 * The caller has to FREE the returned retValue.
5618 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005619 * WARNING: Some value types are not supported yet, resulting
5620 * in a @retValue of "???".
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005621 *
5622 * TODO: XML Schema 1.0 does not define canonical representations
5623 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5624 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005625 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005626 *
5627 * Returns 0 if the value could be built, 1 if the value type is
5628 * not supported yet and -1 in case of API errors.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005629 */
5630int
Daniel Veillardb5839c32005-02-19 18:27:14 +00005631xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005632{
Daniel Veillardb5839c32005-02-19 18:27:14 +00005633 if ((retValue == NULL) || (val == NULL))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005634 return (-1);
5635 *retValue = NULL;
5636 switch (val->type) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005637 case XML_SCHEMAS_STRING:
5638 if (val->value.str == NULL)
5639 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5640 else
5641 *retValue =
5642 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5643 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005644 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005645 if (val->value.str == NULL)
5646 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5647 else {
5648 *retValue = xmlSchemaWhiteSpaceReplace(
5649 (const xmlChar *) val->value.str);
5650 if ((*retValue) == NULL)
5651 *retValue = BAD_CAST xmlStrdup(
5652 (const xmlChar *) val->value.str);
5653 }
5654 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005655 case XML_SCHEMAS_TOKEN:
5656 case XML_SCHEMAS_LANGUAGE:
5657 case XML_SCHEMAS_NMTOKEN:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005658 case XML_SCHEMAS_NAME:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005659 case XML_SCHEMAS_NCNAME:
5660 case XML_SCHEMAS_ID:
5661 case XML_SCHEMAS_IDREF:
5662 case XML_SCHEMAS_ENTITY:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005663 case XML_SCHEMAS_NOTATION: /* Unclear */
5664 case XML_SCHEMAS_ANYURI: /* Unclear */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005665 if (val->value.str == NULL)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005666 return (-1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005667 *retValue =
5668 BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
5669 if (*retValue == NULL)
5670 *retValue =
5671 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005672 break;
5673 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005674 /* TODO: Unclear in XML Schema 1.0. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005675 if (val->value.qname.uri == NULL) {
5676 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
5677 return (0);
5678 } else {
5679 *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
5680 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5681 BAD_CAST val->value.qname.uri);
5682 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5683 BAD_CAST "}");
5684 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5685 BAD_CAST val->value.qname.uri);
5686 }
5687 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005688 case XML_SCHEMAS_DECIMAL:
5689 /*
5690 * TODO: Lookout for a more simple implementation.
5691 */
5692 if ((val->value.decimal.total == 1) &&
5693 (val->value.decimal.lo == 0)) {
5694 *retValue = xmlStrdup(BAD_CAST "0.0");
5695 } else {
5696 xmlSchemaValDecimal dec = val->value.decimal;
5697 int bufsize;
5698 char *buf = NULL, *offs;
5699
5700 /* Add room for the decimal point as well. */
5701 bufsize = dec.total + 2;
5702 if (dec.sign)
5703 bufsize++;
5704 /* Add room for leading/trailing zero. */
5705 if ((dec.frac == 0) || (dec.frac == dec.total))
5706 bufsize++;
5707 buf = xmlMalloc(bufsize);
5708 offs = buf;
5709 if (dec.sign)
5710 *offs++ = '-';
5711 if (dec.frac == dec.total) {
5712 *offs++ = '0';
5713 *offs++ = '.';
5714 }
5715 if (dec.hi != 0)
5716 snprintf(offs, bufsize - (offs - buf),
5717 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5718 else if (dec.mi != 0)
5719 snprintf(offs, bufsize - (offs - buf),
5720 "%lu%lu", dec.mi, dec.lo);
5721 else
5722 snprintf(offs, bufsize - (offs - buf),
5723 "%lu", dec.lo);
5724
5725 if (dec.frac != 0) {
5726 if (dec.frac != dec.total) {
5727 int diff = dec.total - dec.frac;
5728 /*
5729 * Insert the decimal point.
5730 */
5731 memmove(offs + diff + 1, offs + diff, dec.frac +1);
5732 offs[diff] = '.';
5733 } else {
5734 unsigned int i = 0;
5735 /*
5736 * Insert missing zeroes behind the decimal point.
5737 */
5738 while (*(offs + i) != 0)
5739 i++;
5740 if (i < dec.total) {
5741 memmove(offs + (dec.total - i), offs, i +1);
5742 memset(offs, '0', dec.total - i);
5743 }
5744 }
5745 } else {
5746 /*
5747 * Append decimal point and zero.
5748 */
5749 offs = buf + bufsize - 1;
5750 *offs-- = 0;
5751 *offs-- = '0';
5752 *offs-- = '.';
5753 }
5754 *retValue = BAD_CAST buf;
5755 }
5756 break;
5757 case XML_SCHEMAS_INTEGER:
5758 case XML_SCHEMAS_PINTEGER:
5759 case XML_SCHEMAS_NPINTEGER:
5760 case XML_SCHEMAS_NINTEGER:
5761 case XML_SCHEMAS_NNINTEGER:
5762 case XML_SCHEMAS_LONG:
5763 case XML_SCHEMAS_BYTE:
5764 case XML_SCHEMAS_SHORT:
5765 case XML_SCHEMAS_INT:
5766 case XML_SCHEMAS_UINT:
5767 case XML_SCHEMAS_ULONG:
5768 case XML_SCHEMAS_USHORT:
5769 case XML_SCHEMAS_UBYTE:
5770 if ((val->value.decimal.total == 1) &&
5771 (val->value.decimal.lo == 0))
5772 *retValue = xmlStrdup(BAD_CAST "0");
5773 else {
5774 xmlSchemaValDecimal dec = val->value.decimal;
5775 int bufsize = dec.total + 1;
5776
5777 /* Add room for the decimal point as well. */
5778 if (dec.sign)
5779 bufsize++;
5780 *retValue = xmlMalloc(bufsize);
5781 if (dec.hi != 0) {
5782 if (dec.sign)
5783 snprintf((char *) *retValue, bufsize,
5784 "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5785 else
5786 snprintf((char *) *retValue, bufsize,
5787 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5788 } else if (dec.mi != 0) {
5789 if (dec.sign)
5790 snprintf((char *) *retValue, bufsize,
5791 "-%lu%lu", dec.mi, dec.lo);
5792 else
5793 snprintf((char *) *retValue, bufsize,
5794 "%lu%lu", dec.mi, dec.lo);
5795 } else {
5796 if (dec.sign)
5797 snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
5798 else
5799 snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
5800 }
5801 }
5802 break;
5803 case XML_SCHEMAS_BOOLEAN:
5804 if (val->value.b)
5805 *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
5806 else
5807 *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
5808 break;
5809 case XML_SCHEMAS_DURATION: {
5810 char buf[100];
5811 unsigned long year;
5812 unsigned long mon, day, hour = 0, min = 0;
5813 double sec = 0, left;
5814
5815 /* TODO: Unclear in XML Schema 1.0 */
5816 /*
5817 * TODO: This results in a normalized output of the value
5818 * - which is NOT conformant to the spec -
5819 * since the exact values of each property are not
5820 * recoverable. Think about extending the structure to
5821 * provide a field for every property.
5822 */
5823 year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
5824 mon = labs(val->value.dur.mon) - 12 * year;
5825
5826 day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
5827 left = fabs(val->value.dur.sec) - day * 86400;
5828 if (left > 0) {
5829 hour = (unsigned long) FQUOTIENT(left, 3600);
5830 left = left - (hour * 3600);
5831 if (left > 0) {
5832 min = (unsigned long) FQUOTIENT(left, 60);
5833 sec = left - (min * 60);
5834 }
5835 }
5836 if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
5837 snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5838 year, mon, day, hour, min, sec);
5839 else
5840 snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5841 year, mon, day, hour, min, sec);
5842 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5843 }
5844 break;
5845 case XML_SCHEMAS_GYEAR: {
5846 char buf[30];
5847 /* TODO: Unclear in XML Schema 1.0 */
5848 /* TODO: What to do with the timezone? */
5849 snprintf(buf, 30, "%04ld", val->value.date.year);
5850 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5851 }
5852 break;
5853 case XML_SCHEMAS_GMONTH: {
5854 /* TODO: Unclear in XML Schema 1.0 */
5855 /* TODO: What to do with the timezone? */
5856 *retValue = xmlMalloc(5);
5857 snprintf((char *) *retValue, 6, "--%02u",
5858 val->value.date.mon);
5859 }
5860 break;
5861 case XML_SCHEMAS_GDAY: {
5862 /* TODO: Unclear in XML Schema 1.0 */
5863 /* TODO: What to do with the timezone? */
5864 *retValue = xmlMalloc(6);
5865 snprintf((char *) *retValue, 6, "---%02u",
5866 val->value.date.day);
5867 }
5868 break;
5869 case XML_SCHEMAS_GMONTHDAY: {
5870 /* TODO: Unclear in XML Schema 1.0 */
5871 /* TODO: What to do with the timezone? */
5872 *retValue = xmlMalloc(8);
5873 snprintf((char *) *retValue, 8, "--%02u-%02u",
5874 val->value.date.mon, val->value.date.day);
5875 }
5876 break;
5877 case XML_SCHEMAS_GYEARMONTH: {
5878 char buf[35];
5879 /* TODO: Unclear in XML Schema 1.0 */
5880 /* TODO: What to do with the timezone? */
5881 if (val->value.date.year < 0)
5882 snprintf(buf, 35, "-%04ld-%02u",
5883 labs(val->value.date.year),
5884 val->value.date.mon);
5885 else
5886 snprintf(buf, 35, "%04ld-%02u",
5887 val->value.date.year, val->value.date.mon);
5888 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5889 }
5890 break;
5891 case XML_SCHEMAS_TIME:
5892 {
5893 char buf[30];
5894
5895 if (val->value.date.tz_flag) {
5896 xmlSchemaValPtr norm;
5897
5898 norm = xmlSchemaDateNormalize(val, 0);
5899 if (norm == NULL)
5900 return (-1);
5901 /*
5902 * TODO: Check if "%.14g" is portable.
5903 */
5904 snprintf(buf, 30,
5905 "%02u:%02u:%02.14gZ",
5906 norm->value.date.hour,
5907 norm->value.date.min,
5908 norm->value.date.sec);
5909 xmlSchemaFreeValue(norm);
5910 } else {
5911 snprintf(buf, 30,
5912 "%02u:%02u:%02.14g",
5913 val->value.date.hour,
5914 val->value.date.min,
5915 val->value.date.sec);
5916 }
5917 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5918 }
5919 break;
5920 case XML_SCHEMAS_DATE:
5921 {
5922 char buf[30];
5923
5924 if (val->value.date.tz_flag) {
5925 xmlSchemaValPtr norm;
5926
5927 norm = xmlSchemaDateNormalize(val, 0);
5928 if (norm == NULL)
5929 return (-1);
5930 /*
5931 * TODO: Append the canonical value of the
5932 * recoverable timezone and not "Z".
5933 */
5934 snprintf(buf, 30,
5935 "%04ld:%02u:%02uZ",
5936 norm->value.date.year, norm->value.date.mon,
5937 norm->value.date.day);
5938 xmlSchemaFreeValue(norm);
5939 } else {
5940 snprintf(buf, 30,
5941 "%04ld:%02u:%02u",
5942 val->value.date.year, val->value.date.mon,
5943 val->value.date.day);
5944 }
5945 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5946 }
5947 break;
5948 case XML_SCHEMAS_DATETIME:
5949 {
5950 char buf[50];
5951
5952 if (val->value.date.tz_flag) {
5953 xmlSchemaValPtr norm;
5954
5955 norm = xmlSchemaDateNormalize(val, 0);
5956 if (norm == NULL)
5957 return (-1);
5958 /*
5959 * TODO: Check if "%.14g" is portable.
5960 */
5961 snprintf(buf, 50,
5962 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
5963 norm->value.date.year, norm->value.date.mon,
5964 norm->value.date.day, norm->value.date.hour,
5965 norm->value.date.min, norm->value.date.sec);
5966 xmlSchemaFreeValue(norm);
5967 } else {
5968 snprintf(buf, 50,
5969 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
5970 val->value.date.year, val->value.date.mon,
5971 val->value.date.day, val->value.date.hour,
5972 val->value.date.min, val->value.date.sec);
5973 }
5974 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5975 }
5976 break;
5977 case XML_SCHEMAS_HEXBINARY:
5978 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
5979 break;
5980 case XML_SCHEMAS_BASE64BINARY:
5981 /*
5982 * TODO: Is the following spec piece implemented?:
5983 * SPEC: "Note: For some values the canonical form defined
5984 * above does not conform to [RFC 2045], which requires breaking
5985 * with linefeeds at appropriate intervals."
5986 */
5987 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
5988 break;
5989 case XML_SCHEMAS_FLOAT: {
5990 char buf[30];
5991 /*
5992 * |m| < 16777216, -149 <= e <= 104.
5993 * TODO: Handle, NaN, INF, -INF. The format is not
5994 * yet conformant. The c type float does not cover
5995 * the whole range.
5996 */
5997 snprintf(buf, 30, "%01.14e", val->value.f);
5998 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5999 }
6000 break;
6001 case XML_SCHEMAS_DOUBLE: {
6002 char buf[40];
6003 /* |m| < 9007199254740992, -1075 <= e <= 970 */
6004 /*
6005 * TODO: Handle, NaN, INF, -INF. The format is not
6006 * yet conformant. The c type float does not cover
6007 * the whole range.
6008 */
6009 snprintf(buf, 40, "%01.14e", val->value.d);
6010 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6011 }
6012 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006013 default:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006014 *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006015 return (1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006016 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006017 return (0);
6018}
6019
Daniel Veillardbda59572005-04-01 17:15:17 +00006020/**
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006021 * xmlSchemaGetCanonValueWhtsp:
6022 * @val: the precomputed value
6023 * @retValue: the returned value
6024 * @ws: the whitespace type of the value
6025 *
6026 * Get a the cononical representation of the value.
6027 * The caller has to free the returned @retValue.
6028 *
6029 * Returns 0 if the value could be built, 1 if the value type is
6030 * not supported yet and -1 in case of API errors.
6031 */
6032int
6033xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
6034 const xmlChar **retValue,
6035 xmlSchemaWhitespaceValueType ws)
6036{
6037 if ((retValue == NULL) || (val == NULL))
6038 return (-1);
6039 if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
6040 (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
6041 return (-1);
6042
6043 *retValue = NULL;
6044 switch (val->type) {
6045 case XML_SCHEMAS_STRING:
6046 if (val->value.str == NULL)
6047 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6048 else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6049 *retValue = xmlSchemaCollapseString(val->value.str);
6050 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
6051 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6052 if ((*retValue) == NULL)
6053 *retValue = BAD_CAST xmlStrdup(val->value.str);
6054 break;
6055 case XML_SCHEMAS_NORMSTRING:
6056 if (val->value.str == NULL)
6057 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6058 else {
6059 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6060 *retValue = xmlSchemaCollapseString(val->value.str);
6061 else
6062 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6063 if ((*retValue) == NULL)
6064 *retValue = BAD_CAST xmlStrdup(val->value.str);
6065 }
6066 break;
6067 default:
6068 return (xmlSchemaGetCanonValue(val, retValue));
6069 }
6070 return (0);
6071}
6072
6073/**
Daniel Veillardbda59572005-04-01 17:15:17 +00006074 * xmlSchemaGetValType:
6075 * @val: a schemas value
6076 *
6077 * Accessor for the type of a value
6078 *
6079 * Returns the xmlSchemaValType of the value
6080 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006081xmlSchemaValType
6082xmlSchemaGetValType(xmlSchemaValPtr val)
6083{
Daniel Veillardbda59572005-04-01 17:15:17 +00006084 if (val == NULL)
6085 return(XML_SCHEMAS_UNKNOWN);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006086 return (val->type);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006087}
6088
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006089#define bottom_xmlschemastypes
6090#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00006091#endif /* LIBXML_SCHEMAS_ENABLED */