blob: 557d0ab228df8eec130a1203042327d20742c85d [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;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00002546 } else
2547 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002548 if (*cur != 0) {
2549 while IS_WSP_BLANK_CH(*cur) cur++;
2550 if (*cur != 0)
2551 goto return1;
2552 }
2553 } else {
2554 if ((cur[0] == '0') && (cur[1] == 0))
2555 ret = 0;
2556 else if ((cur[0] == '1') && (cur[1] == 0))
2557 ret = 1;
2558 else if ((cur[0] == 't') && (cur[1] == 'r')
2559 && (cur[2] == 'u') && (cur[3] == 'e')
2560 && (cur[4] == 0))
2561 ret = 1;
2562 else if ((cur[0] == 'f') && (cur[1] == 'a')
2563 && (cur[2] == 'l') && (cur[3] == 's')
2564 && (cur[4] == 'e') && (cur[5] == 0))
2565 ret = 0;
2566 else
2567 goto return1;
2568 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002569 if (val != NULL) {
2570 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
2571 if (v != NULL) {
2572 v->value.b = ret;
2573 *val = v;
2574 } else {
2575 goto error;
2576 }
2577 }
2578 goto return0;
2579 }
2580 case XML_SCHEMAS_TOKEN:{
2581 const xmlChar *cur = value;
2582
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002583 if (! normOnTheFly) {
2584 while (*cur != 0) {
2585 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2586 goto return1;
2587 } else if (*cur == ' ') {
2588 cur++;
2589 if (*cur == 0)
2590 goto return1;
2591 if (*cur == ' ')
2592 goto return1;
2593 } else {
2594 cur++;
2595 }
2596 }
2597 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002598 if (val != NULL) {
2599 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
2600 if (v != NULL) {
2601 v->value.str = xmlStrdup(value);
2602 *val = v;
2603 } else {
2604 goto error;
2605 }
2606 }
2607 goto return0;
2608 }
2609 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002610 if (normOnTheFly) {
2611 norm = xmlSchemaCollapseString(value);
2612 if (norm != NULL)
2613 value = norm;
2614 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002615 if (xmlCheckLanguageID(value) == 1) {
2616 if (val != NULL) {
2617 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
2618 if (v != NULL) {
2619 v->value.str = xmlStrdup(value);
2620 *val = v;
2621 } else {
2622 goto error;
2623 }
2624 }
2625 goto return0;
2626 }
2627 goto return1;
2628 case XML_SCHEMAS_NMTOKEN:
2629 if (xmlValidateNMToken(value, 1) == 0) {
2630 if (val != NULL) {
2631 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
2632 if (v != NULL) {
2633 v->value.str = xmlStrdup(value);
2634 *val = v;
2635 } else {
2636 goto error;
2637 }
2638 }
2639 goto return0;
2640 }
2641 goto return1;
2642 case XML_SCHEMAS_NMTOKENS:
2643 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
2644 value, val, node);
2645 if (ret > 0)
2646 ret = 0;
2647 else
2648 ret = 1;
2649 goto done;
2650 case XML_SCHEMAS_NAME:
2651 ret = xmlValidateName(value, 1);
Daniel Veillarddf292f72005-01-16 19:00:15 +00002652 if ((ret == 0) && (val != NULL) && (value != NULL)) {
2653 v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
2654 if (v != NULL) {
2655 const xmlChar *start = value, *end;
2656 while (IS_BLANK_CH(*start)) start++;
2657 end = start;
2658 while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
2659 v->value.str = xmlStrndup(start, end - start);
2660 *val = v;
2661 } else {
2662 goto error;
2663 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002664 }
2665 goto done;
2666 case XML_SCHEMAS_QNAME:{
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002667 const xmlChar *uri = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002668 xmlChar *local = NULL;
2669
2670 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002671 if (ret != 0)
2672 goto done;
2673 if (node != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002674 xmlChar *prefix;
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002675 xmlNsPtr ns;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002676
2677 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002678 ns = xmlSearchNs(node->doc, node, prefix);
2679 if ((ns == NULL) && (prefix != NULL)) {
2680 xmlFree(prefix);
2681 if (local != NULL)
2682 xmlFree(local);
2683 goto return1;
2684 }
2685 if (ns != NULL)
2686 uri = ns->href;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002687 if (prefix != NULL)
2688 xmlFree(prefix);
2689 }
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002690 if (val != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002691 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002692 if (v == NULL) {
2693 if (local != NULL)
2694 xmlFree(local);
2695 goto error;
2696 }
2697 if (local != NULL)
2698 v->value.qname.name = local;
2699 else
2700 v->value.qname.name = xmlStrdup(value);
2701 if (uri != NULL)
2702 v->value.qname.uri = xmlStrdup(uri);
2703 *val = v;
2704 } else
2705 if (local != NULL)
2706 xmlFree(local);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002707 goto done;
2708 }
2709 case XML_SCHEMAS_NCNAME:
2710 ret = xmlValidateNCName(value, 1);
2711 if ((ret == 0) && (val != NULL)) {
2712 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
2713 if (v != NULL) {
2714 v->value.str = xmlStrdup(value);
2715 *val = v;
2716 } else {
2717 goto error;
2718 }
2719 }
2720 goto done;
2721 case XML_SCHEMAS_ID:
2722 ret = xmlValidateNCName(value, 1);
2723 if ((ret == 0) && (val != NULL)) {
2724 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
2725 if (v != NULL) {
2726 v->value.str = xmlStrdup(value);
2727 *val = v;
2728 } else {
2729 goto error;
2730 }
2731 }
2732 if ((ret == 0) && (node != NULL) &&
2733 (node->type == XML_ATTRIBUTE_NODE)) {
2734 xmlAttrPtr attr = (xmlAttrPtr) node;
2735
2736 /*
2737 * NOTE: the IDness might have already be declared in the DTD
2738 */
2739 if (attr->atype != XML_ATTRIBUTE_ID) {
2740 xmlIDPtr res;
2741 xmlChar *strip;
2742
2743 strip = xmlSchemaStrip(value);
2744 if (strip != NULL) {
2745 res = xmlAddID(NULL, node->doc, strip, attr);
2746 xmlFree(strip);
2747 } else
2748 res = xmlAddID(NULL, node->doc, value, attr);
2749 if (res == NULL) {
2750 ret = 2;
2751 } else {
2752 attr->atype = XML_ATTRIBUTE_ID;
2753 }
2754 }
2755 }
2756 goto done;
2757 case XML_SCHEMAS_IDREF:
2758 ret = xmlValidateNCName(value, 1);
2759 if ((ret == 0) && (val != NULL)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00002760 v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
2761 if (v == NULL)
2762 goto error;
2763 v->value.str = xmlStrdup(value);
2764 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002765 }
2766 if ((ret == 0) && (node != NULL) &&
2767 (node->type == XML_ATTRIBUTE_NODE)) {
2768 xmlAttrPtr attr = (xmlAttrPtr) node;
2769 xmlChar *strip;
2770
2771 strip = xmlSchemaStrip(value);
2772 if (strip != NULL) {
2773 xmlAddRef(NULL, node->doc, strip, attr);
2774 xmlFree(strip);
2775 } else
2776 xmlAddRef(NULL, node->doc, value, attr);
2777 attr->atype = XML_ATTRIBUTE_IDREF;
2778 }
2779 goto done;
2780 case XML_SCHEMAS_IDREFS:
2781 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
2782 value, val, node);
2783 if (ret < 0)
2784 ret = 2;
2785 else
2786 ret = 0;
2787 if ((ret == 0) && (node != NULL) &&
2788 (node->type == XML_ATTRIBUTE_NODE)) {
2789 xmlAttrPtr attr = (xmlAttrPtr) node;
2790
2791 attr->atype = XML_ATTRIBUTE_IDREFS;
2792 }
2793 goto done;
2794 case XML_SCHEMAS_ENTITY:{
2795 xmlChar *strip;
2796
2797 ret = xmlValidateNCName(value, 1);
2798 if ((node == NULL) || (node->doc == NULL))
2799 ret = 3;
2800 if (ret == 0) {
2801 xmlEntityPtr ent;
2802
2803 strip = xmlSchemaStrip(value);
2804 if (strip != NULL) {
2805 ent = xmlGetDocEntity(node->doc, strip);
2806 xmlFree(strip);
2807 } else {
2808 ent = xmlGetDocEntity(node->doc, value);
2809 }
2810 if ((ent == NULL) ||
2811 (ent->etype !=
2812 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
2813 ret = 4;
2814 }
2815 if ((ret == 0) && (val != NULL)) {
2816 TODO;
2817 }
2818 if ((ret == 0) && (node != NULL) &&
2819 (node->type == XML_ATTRIBUTE_NODE)) {
2820 xmlAttrPtr attr = (xmlAttrPtr) node;
2821
2822 attr->atype = XML_ATTRIBUTE_ENTITY;
2823 }
2824 goto done;
2825 }
2826 case XML_SCHEMAS_ENTITIES:
2827 if ((node == NULL) || (node->doc == NULL))
2828 goto return3;
2829 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
2830 value, val, node);
2831 if (ret <= 0)
2832 ret = 1;
2833 else
2834 ret = 0;
2835 if ((ret == 0) && (node != NULL) &&
2836 (node->type == XML_ATTRIBUTE_NODE)) {
2837 xmlAttrPtr attr = (xmlAttrPtr) node;
2838
2839 attr->atype = XML_ATTRIBUTE_ENTITIES;
2840 }
2841 goto done;
2842 case XML_SCHEMAS_NOTATION:{
2843 xmlChar *uri = NULL;
2844 xmlChar *local = NULL;
2845
2846 ret = xmlValidateQName(value, 1);
2847 if ((ret == 0) && (node != NULL)) {
2848 xmlChar *prefix;
2849
2850 local = xmlSplitQName2(value, &prefix);
2851 if (prefix != NULL) {
2852 xmlNsPtr ns;
2853
2854 ns = xmlSearchNs(node->doc, node, prefix);
2855 if (ns == NULL)
2856 ret = 1;
2857 else if (val != NULL)
2858 uri = xmlStrdup(ns->href);
2859 }
2860 if ((local != NULL) && ((val == NULL) || (ret != 0)))
2861 xmlFree(local);
2862 if (prefix != NULL)
2863 xmlFree(prefix);
2864 }
2865 if ((node == NULL) || (node->doc == NULL))
2866 ret = 3;
2867 if (ret == 0) {
2868 ret = xmlValidateNotationUse(NULL, node->doc, value);
2869 if (ret == 1)
2870 ret = 0;
2871 else
2872 ret = 1;
2873 }
2874 if ((ret == 0) && (val != NULL)) {
2875 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
2876 if (v != NULL) {
2877 if (local != NULL)
2878 v->value.qname.name = local;
2879 else
2880 v->value.qname.name = xmlStrdup(value);
2881 if (uri != NULL)
2882 v->value.qname.uri = uri;
2883
2884 *val = v;
2885 } else {
2886 if (local != NULL)
2887 xmlFree(local);
2888 if (uri != NULL)
2889 xmlFree(uri);
2890 goto error;
2891 }
2892 }
2893 goto done;
2894 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002895 case XML_SCHEMAS_ANYURI:{
Daniel Veillard11c466a2004-03-14 12:20:15 +00002896 if (*value != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002897 xmlURIPtr uri;
2898 if (normOnTheFly) {
2899 norm = xmlSchemaCollapseString(value);
2900 if (norm != NULL)
2901 value = norm;
2902 }
2903 uri = xmlParseURI((const char *) value);
Daniel Veillard11c466a2004-03-14 12:20:15 +00002904 if (uri == NULL)
2905 goto return1;
2906 xmlFreeURI(uri);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002907 }
Daniel Veillard11c466a2004-03-14 12:20:15 +00002908
2909 if (val != NULL) {
2910 v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
2911 if (v == NULL)
2912 goto error;
2913 v->value.str = xmlStrdup(value);
2914 *val = v;
2915 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002916 goto return0;
2917 }
2918 case XML_SCHEMAS_HEXBINARY:{
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002919 const xmlChar *cur = value, *start;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002920 xmlChar *base;
2921 int total, i = 0;
2922
Daniel Veillardf34a20e2004-08-31 08:42:17 +00002923 if (cur == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002924 goto return1;
2925
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002926 if (normOnTheFly)
2927 while IS_WSP_BLANK_CH(*cur) cur++;
2928
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002929 start = cur;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002930 while (((*cur >= '0') && (*cur <= '9')) ||
2931 ((*cur >= 'A') && (*cur <= 'F')) ||
2932 ((*cur >= 'a') && (*cur <= 'f'))) {
2933 i++;
2934 cur++;
2935 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002936 if (normOnTheFly)
2937 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002938
2939 if (*cur != 0)
2940 goto return1;
2941 if ((i % 2) != 0)
2942 goto return1;
2943
2944 if (val != NULL) {
2945
2946 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2947 if (v == NULL)
2948 goto error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002949 /*
2950 * Copy only the normalized piece.
2951 * CRITICAL TODO: Check this.
2952 */
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002953 cur = xmlStrndup(start, i);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002954 if (cur == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002955 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002956 xmlFree(v);
2957 goto return1;
2958 }
2959
2960 total = i / 2; /* number of octets */
2961
2962 base = (xmlChar *) cur;
2963 while (i-- > 0) {
2964 if (*base >= 'a')
2965 *base = *base - ('a' - 'A');
2966 base++;
2967 }
2968
2969 v->value.hex.str = (xmlChar *) cur;
2970 v->value.hex.total = total;
2971 *val = v;
2972 }
2973 goto return0;
2974 }
2975 case XML_SCHEMAS_BASE64BINARY:{
2976 /* ISSUE:
2977 *
2978 * Ignore all stray characters? (yes, currently)
2979 * Worry about long lines? (no, currently)
2980 *
2981 * rfc2045.txt:
2982 *
2983 * "The encoded output stream must be represented in lines of
2984 * no more than 76 characters each. All line breaks or other
2985 * characters not found in Table 1 must be ignored by decoding
2986 * software. In base64 data, characters other than those in
2987 * Table 1, line breaks, and other white space probably
2988 * indicate a transmission error, about which a warning
2989 * message or even a message rejection might be appropriate
2990 * under some circumstances." */
2991 const xmlChar *cur = value;
2992 xmlChar *base;
2993 int total, i = 0, pad = 0;
2994
2995 if (cur == NULL)
2996 goto return1;
2997
2998 for (; *cur; ++cur) {
2999 int decc;
3000
3001 decc = _xmlSchemaBase64Decode(*cur);
3002 if (decc < 0) ;
3003 else if (decc < 64)
3004 i++;
3005 else
3006 break;
3007 }
3008 for (; *cur; ++cur) {
3009 int decc;
3010
3011 decc = _xmlSchemaBase64Decode(*cur);
3012 if (decc < 0) ;
3013 else if (decc < 64)
3014 goto return1;
3015 if (decc == 64)
3016 pad++;
3017 }
3018
3019 /* rfc2045.txt: "Special processing is performed if fewer than
3020 * 24 bits are available at the end of the data being encoded.
3021 * A full encoding quantum is always completed at the end of a
3022 * body. When fewer than 24 input bits are available in an
3023 * input group, zero bits are added (on the right) to form an
3024 * integral number of 6-bit groups. Padding at the end of the
3025 * data is performed using the "=" character. Since all
3026 * base64 input is an integral number of octets, only the
3027 * following cases can arise: (1) the final quantum of
3028 * encoding input is an integral multiple of 24 bits; here,
3029 * the final unit of encoded output will be an integral
3030 * multiple ofindent: Standard input:701: Warning:old style
3031 * assignment ambiguity in "=*". Assuming "= *" 4 characters
3032 * with no "=" padding, (2) the final
3033 * quantum of encoding input is exactly 8 bits; here, the
3034 * final unit of encoded output will be two characters
3035 * followed by two "=" padding characters, or (3) the final
3036 * quantum of encoding input is exactly 16 bits; here, the
3037 * final unit of encoded output will be three characters
3038 * followed by one "=" padding character." */
3039
3040 total = 3 * (i / 4);
3041 if (pad == 0) {
3042 if (i % 4 != 0)
3043 goto return1;
3044 } else if (pad == 1) {
3045 int decc;
3046
3047 if (i % 4 != 3)
3048 goto return1;
3049 for (decc = _xmlSchemaBase64Decode(*cur);
3050 (decc < 0) || (decc > 63);
3051 decc = _xmlSchemaBase64Decode(*cur))
3052 --cur;
3053 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
3054 /* 00111100 -> 0x3c */
3055 if (decc & ~0x3c)
3056 goto return1;
3057 total += 2;
3058 } else if (pad == 2) {
3059 int decc;
3060
3061 if (i % 4 != 2)
3062 goto return1;
3063 for (decc = _xmlSchemaBase64Decode(*cur);
3064 (decc < 0) || (decc > 63);
3065 decc = _xmlSchemaBase64Decode(*cur))
3066 --cur;
3067 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
3068 /* 00110000 -> 0x30 */
3069 if (decc & ~0x30)
3070 goto return1;
3071 total += 1;
3072 } else
3073 goto return1;
3074
3075 if (val != NULL) {
3076 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
3077 if (v == NULL)
3078 goto error;
3079 base =
3080 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
3081 sizeof(xmlChar));
3082 if (base == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003083 xmlSchemaTypeErrMemory(node, "allocating base64 data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003084 xmlFree(v);
3085 goto return1;
3086 }
3087 v->value.base64.str = base;
3088 for (cur = value; *cur; ++cur)
3089 if (_xmlSchemaBase64Decode(*cur) >= 0) {
3090 *base = *cur;
3091 ++base;
3092 }
3093 *base = 0;
3094 v->value.base64.total = total;
3095 *val = v;
3096 }
3097 goto return0;
3098 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003099 case XML_SCHEMAS_INTEGER:
3100 case XML_SCHEMAS_PINTEGER:
3101 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003102 case XML_SCHEMAS_NINTEGER:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003103 case XML_SCHEMAS_NNINTEGER:{
3104 const xmlChar *cur = value;
3105 unsigned long lo, mi, hi;
William M. Brackec3b4b72005-03-15 15:50:17 +00003106 int sign = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003107
3108 if (cur == NULL)
3109 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003110 if (normOnTheFly)
3111 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003112 if (*cur == '-') {
3113 sign = 1;
3114 cur++;
3115 } else if (*cur == '+')
3116 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003117 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3118 if (ret == -1)
3119 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003120 if (normOnTheFly)
3121 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003122 if (*cur != 0)
3123 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003124 if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003125 if ((sign == 0) &&
3126 ((hi != 0) || (mi != 0) || (lo != 0)))
3127 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003128 } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003129 if (sign == 1)
3130 goto return1;
3131 if ((hi == 0) && (mi == 0) && (lo == 0))
3132 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003133 } else if (type->builtInType == XML_SCHEMAS_NINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003134 if (sign == 0)
3135 goto return1;
3136 if ((hi == 0) && (mi == 0) && (lo == 0))
3137 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003138 } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003139 if ((sign == 1) &&
3140 ((hi != 0) || (mi != 0) || (lo != 0)))
3141 goto return1;
3142 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003143 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003144 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003145 if (v != NULL) {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003146 if (ret == 0)
3147 ret++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003148 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003149 v->value.decimal.mi = mi;
3150 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003151 v->value.decimal.sign = sign;
3152 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003153 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003154 *val = v;
3155 }
3156 }
3157 goto return0;
3158 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003159 case XML_SCHEMAS_LONG:
3160 case XML_SCHEMAS_BYTE:
3161 case XML_SCHEMAS_SHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003162 case XML_SCHEMAS_INT:{
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00003163 const xmlChar *cur = value;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003164 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003165 int sign = 0;
3166
3167 if (cur == NULL)
3168 goto return1;
3169 if (*cur == '-') {
3170 sign = 1;
3171 cur++;
3172 } else if (*cur == '+')
3173 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003174 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3175 if (ret < 0)
3176 goto return1;
3177 if (*cur != 0)
3178 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003179 if (type->builtInType == XML_SCHEMAS_LONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003180 if (hi >= 922) {
3181 if (hi > 922)
3182 goto return1;
3183 if (mi >= 33720368) {
3184 if (mi > 33720368)
3185 goto return1;
3186 if ((sign == 0) && (lo > 54775807))
3187 goto return1;
3188 if ((sign == 1) && (lo > 54775808))
3189 goto return1;
3190 }
3191 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003192 } else if (type->builtInType == XML_SCHEMAS_INT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003193 if (hi != 0)
3194 goto return1;
3195 if (mi >= 21) {
3196 if (mi > 21)
3197 goto return1;
3198 if ((sign == 0) && (lo > 47483647))
3199 goto return1;
3200 if ((sign == 1) && (lo > 47483648))
3201 goto return1;
3202 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003203 } else if (type->builtInType == XML_SCHEMAS_SHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003204 if ((mi != 0) || (hi != 0))
3205 goto return1;
3206 if ((sign == 1) && (lo > 32768))
3207 goto return1;
3208 if ((sign == 0) && (lo > 32767))
3209 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003210 } else if (type->builtInType == XML_SCHEMAS_BYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003211 if ((mi != 0) || (hi != 0))
3212 goto return1;
3213 if ((sign == 1) && (lo > 128))
3214 goto return1;
3215 if ((sign == 0) && (lo > 127))
3216 goto return1;
3217 }
3218 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003219 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003220 if (v != NULL) {
3221 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003222 v->value.decimal.mi = mi;
3223 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003224 v->value.decimal.sign = sign;
3225 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003226 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003227 *val = v;
3228 }
3229 }
3230 goto return0;
3231 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003232 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003233 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003234 case XML_SCHEMAS_USHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003235 case XML_SCHEMAS_UBYTE:{
3236 const xmlChar *cur = value;
3237 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003238
3239 if (cur == NULL)
3240 goto return1;
William M. Brackec3b4b72005-03-15 15:50:17 +00003241 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3242 if (ret < 0)
3243 goto return1;
3244 if (*cur != 0)
3245 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003246 if (type->builtInType == XML_SCHEMAS_ULONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003247 if (hi >= 1844) {
3248 if (hi > 1844)
3249 goto return1;
3250 if (mi >= 67440737) {
3251 if (mi > 67440737)
3252 goto return1;
3253 if (lo > 9551615)
3254 goto return1;
3255 }
3256 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003257 } else if (type->builtInType == XML_SCHEMAS_UINT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003258 if (hi != 0)
3259 goto return1;
3260 if (mi >= 42) {
3261 if (mi > 42)
3262 goto return1;
3263 if (lo > 94967295)
3264 goto return1;
3265 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003266 } else if (type->builtInType == XML_SCHEMAS_USHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003267 if ((mi != 0) || (hi != 0))
3268 goto return1;
3269 if (lo > 65535)
3270 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003271 } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003272 if ((mi != 0) || (hi != 0))
3273 goto return1;
3274 if (lo > 255)
3275 goto return1;
3276 }
3277 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003278 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003279 if (v != NULL) {
3280 v->value.decimal.lo = lo;
3281 v->value.decimal.mi = mi;
3282 v->value.decimal.hi = hi;
3283 v->value.decimal.sign = 0;
3284 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003285 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003286 *val = v;
3287 }
3288 }
3289 goto return0;
3290 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003291 }
3292
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003293 done:
3294 if (norm != NULL)
3295 xmlFree(norm);
3296 return (ret);
3297 return3:
3298 if (norm != NULL)
3299 xmlFree(norm);
3300 return (3);
3301 return1:
3302 if (norm != NULL)
3303 xmlFree(norm);
3304 return (1);
3305 return0:
3306 if (norm != NULL)
3307 xmlFree(norm);
3308 return (0);
3309 error:
3310 if (norm != NULL)
3311 xmlFree(norm);
3312 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003313}
3314
3315/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003316 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00003317 * @type: the predefined type
3318 * @value: the value to check
3319 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003320 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00003321 *
3322 * Check that a value conforms to the lexical space of the predefined type.
3323 * if true a value is computed and returned in @val.
3324 *
3325 * Returns 0 if this validates, a positive error code number otherwise
3326 * and -1 in case of internal or API error.
3327 */
3328int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003329xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
3330 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003331 return(xmlSchemaValAtomicType(type, value, val, node, 0,
3332 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00003333}
3334
3335/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00003336 * xmlSchemaValPredefTypeNodeNoNorm:
3337 * @type: the predefined type
3338 * @value: the value to check
3339 * @val: the return computed value
3340 * @node: the node containing the value
3341 *
3342 * Check that a value conforms to the lexical space of the predefined type.
3343 * if true a value is computed and returned in @val.
3344 * This one does apply any normalization to the value.
3345 *
3346 * Returns 0 if this validates, a positive error code number otherwise
3347 * and -1 in case of internal or API error.
3348 */
3349int
3350xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
3351 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003352 return(xmlSchemaValAtomicType(type, value, val, node, 1,
3353 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
Daniel Veillardc0826a72004-08-10 14:17:33 +00003354}
3355
3356/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003357 * xmlSchemaValidatePredefinedType:
3358 * @type: the predefined type
3359 * @value: the value to check
3360 * @val: the return computed value
3361 *
3362 * Check that a value conforms to the lexical space of the predefined type.
3363 * if true a value is computed and returned in @val.
3364 *
3365 * Returns 0 if this validates, a positive error code number otherwise
3366 * and -1 in case of internal or API error.
3367 */
3368int
3369xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
3370 xmlSchemaValPtr *val) {
3371 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
3372}
3373
3374/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003375 * xmlSchemaCompareDecimals:
3376 * @x: a first decimal value
3377 * @y: a second decimal value
3378 *
3379 * Compare 2 decimals
3380 *
3381 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
3382 */
3383static int
3384xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
3385{
3386 xmlSchemaValPtr swp;
William M. Brack273670f2005-03-11 15:55:14 +00003387 int order = 1, integx, integy, dlen;
3388 unsigned long hi, mi, lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003389
William M. Brack273670f2005-03-11 15:55:14 +00003390 /*
3391 * First test: If x is -ve and not zero
3392 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003393 if ((x->value.decimal.sign) &&
3394 ((x->value.decimal.lo != 0) ||
3395 (x->value.decimal.mi != 0) ||
3396 (x->value.decimal.hi != 0))) {
William M. Brack273670f2005-03-11 15:55:14 +00003397 /*
3398 * Then if y is -ve and not zero reverse the compare
3399 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003400 if ((y->value.decimal.sign) &&
3401 ((y->value.decimal.lo != 0) ||
3402 (y->value.decimal.mi != 0) ||
3403 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00003404 order = -1;
William M. Brack273670f2005-03-11 15:55:14 +00003405 /*
3406 * Otherwise (y >= 0) we have the answer
3407 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003408 else
3409 return (-1);
William M. Brack273670f2005-03-11 15:55:14 +00003410 /*
3411 * If x is not -ve and y is -ve we have the answer
3412 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003413 } else if ((y->value.decimal.sign) &&
3414 ((y->value.decimal.lo != 0) ||
3415 (y->value.decimal.mi != 0) ||
3416 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003417 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00003418 }
William M. Brack273670f2005-03-11 15:55:14 +00003419 /*
3420 * If it's not simply determined by a difference in sign,
3421 * then we need to compare the actual values of the two nums.
3422 * To do this, we start by looking at the integral parts.
3423 * If the number of integral digits differ, then we have our
3424 * answer.
3425 */
3426 integx = x->value.decimal.total - x->value.decimal.frac;
3427 integy = y->value.decimal.total - y->value.decimal.frac;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003428 /*
3429 * NOTE: We changed the "total" for values like "0.1"
3430 * (or "-0.1" or ".1") to be 1, which was 2 previously.
3431 * Therefore the special case, when such values are
3432 * compared with 0, needs to be handled separately;
3433 * otherwise a zero would be recognized incorrectly as
3434 * greater than those values. This has the nice side effect
3435 * that we gain an overall optimized comparison with zeroes.
3436 * Note that a "0" has a "total" of 1 already.
3437 */
3438 if (integx == 1) {
3439 if (x->value.decimal.lo == 0) {
3440 if (integy != 1)
3441 return -order;
3442 else if (y->value.decimal.lo != 0)
3443 return -order;
3444 else
3445 return(0);
3446 }
3447 }
3448 if (integy == 1) {
3449 if (y->value.decimal.lo == 0) {
3450 if (integx != 1)
3451 return order;
3452 else if (x->value.decimal.lo != 0)
3453 return order;
3454 else
3455 return(0);
3456 }
3457 }
3458
William M. Brack273670f2005-03-11 15:55:14 +00003459 if (integx > integy)
3460 return order;
3461 else if (integy > integx)
3462 return -order;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003463
William M. Brack273670f2005-03-11 15:55:14 +00003464 /*
3465 * If the number of integral digits is the same for both numbers,
3466 * then things get a little more complicated. We need to "normalize"
3467 * the numbers in order to properly compare them. To do this, we
3468 * look at the total length of each number (length => number of
3469 * significant digits), and divide the "shorter" by 10 (decreasing
3470 * the length) until they are of equal length.
3471 */
3472 dlen = x->value.decimal.total - y->value.decimal.total;
3473 if (dlen < 0) { /* y has more digits than x */
3474 swp = x;
3475 hi = y->value.decimal.hi;
3476 mi = y->value.decimal.mi;
3477 lo = y->value.decimal.lo;
3478 dlen = -dlen;
3479 order = -order;
3480 } else { /* x has more digits than y */
3481 swp = y;
3482 hi = x->value.decimal.hi;
3483 mi = x->value.decimal.mi;
3484 lo = x->value.decimal.lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003485 }
William M. Brack273670f2005-03-11 15:55:14 +00003486 while (dlen > 8) { /* in effect, right shift by 10**8 */
3487 lo = mi;
3488 mi = hi;
3489 hi = 0;
3490 dlen -= 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00003491 }
William M. Brack273670f2005-03-11 15:55:14 +00003492 while (dlen > 0) {
3493 unsigned long rem1, rem2;
3494 rem1 = (hi % 10) * 100000000L;
3495 hi = hi / 10;
3496 rem2 = (mi % 10) * 100000000L;
3497 mi = (mi + rem1) / 10;
3498 lo = (lo + rem2) / 10;
3499 dlen--;
3500 }
3501 if (hi > swp->value.decimal.hi) {
3502 return order;
3503 } else if (hi == swp->value.decimal.hi) {
3504 if (mi > swp->value.decimal.mi) {
3505 return order;
3506 } else if (mi == swp->value.decimal.mi) {
3507 if (lo > swp->value.decimal.lo) {
3508 return order;
3509 } else if (lo == swp->value.decimal.lo) {
3510 if (x->value.decimal.total == y->value.decimal.total) {
3511 return 0;
3512 } else {
3513 return order;
3514 }
3515 }
3516 }
3517 }
3518 return -order;
Daniel Veillard4255d502002-04-16 15:50:10 +00003519}
3520
3521/**
Daniel Veillard070803b2002-05-03 07:29:38 +00003522 * xmlSchemaCompareDurations:
3523 * @x: a first duration value
3524 * @y: a second duration value
3525 *
3526 * Compare 2 durations
3527 *
3528 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3529 * case of error
3530 */
3531static int
3532xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
3533{
3534 long carry, mon, day;
3535 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00003536 int invert = 1;
3537 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00003538 static const long dayRange [2][12] = {
3539 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
3540 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
3541
3542 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00003543 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00003544
3545 /* months */
3546 mon = x->value.dur.mon - y->value.dur.mon;
3547
3548 /* seconds */
3549 sec = x->value.dur.sec - y->value.dur.sec;
3550 carry = (long)sec / SECS_PER_DAY;
3551 sec -= (double)(carry * SECS_PER_DAY);
3552
3553 /* days */
3554 day = x->value.dur.day - y->value.dur.day + carry;
3555
3556 /* easy test */
3557 if (mon == 0) {
3558 if (day == 0)
3559 if (sec == 0.0)
3560 return 0;
3561 else if (sec < 0.0)
3562 return -1;
3563 else
3564 return 1;
3565 else if (day < 0)
3566 return -1;
3567 else
3568 return 1;
3569 }
3570
3571 if (mon > 0) {
3572 if ((day >= 0) && (sec >= 0.0))
3573 return 1;
3574 else {
3575 xmon = mon;
3576 xday = -day;
3577 }
3578 } else if ((day <= 0) && (sec <= 0.0)) {
3579 return -1;
3580 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00003581 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00003582 xmon = -mon;
3583 xday = day;
3584 }
3585
3586 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00003587 if (myear == 0) {
3588 minday = 0;
3589 maxday = 0;
3590 } else {
3591 maxday = 366 * ((myear + 3) / 4) +
3592 365 * ((myear - 1) % 4);
3593 minday = maxday - 1;
3594 }
3595
Daniel Veillard070803b2002-05-03 07:29:38 +00003596 xmon = xmon % 12;
3597 minday += dayRange[0][xmon];
3598 maxday += dayRange[1][xmon];
3599
Daniel Veillard80b19092003-03-28 13:29:53 +00003600 if ((maxday == minday) && (maxday == xday))
3601 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00003602 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00003603 return(-invert);
3604 if (minday > xday)
3605 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00003606
3607 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00003608 return 2;
3609}
3610
3611/*
3612 * macros for adding date/times and durations
3613 */
3614#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
3615#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
3616#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
3617#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
3618
3619/**
Daniel Veillard669adfc2004-05-29 20:12:46 +00003620 * xmlSchemaDupVal:
3621 * @v: the #xmlSchemaValPtr value to duplicate
3622 *
3623 * Makes a copy of @v. The calling program is responsible for freeing
3624 * the returned value.
3625 *
3626 * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
3627 */
3628static xmlSchemaValPtr
3629xmlSchemaDupVal (xmlSchemaValPtr v)
3630{
3631 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
3632 if (ret == NULL)
3633 return NULL;
3634
3635 memcpy(ret, v, sizeof(xmlSchemaVal));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003636 ret->next = NULL;
Daniel Veillard669adfc2004-05-29 20:12:46 +00003637 return ret;
3638}
3639
3640/**
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003641 * xmlSchemaCopyValue:
3642 * @val: the precomputed value to be copied
3643 *
3644 * Copies the precomputed value. This duplicates any string within.
3645 *
3646 * Returns the copy or NULL if a copy for a data-type is not implemented.
3647 */
3648xmlSchemaValPtr
3649xmlSchemaCopyValue(xmlSchemaValPtr val)
3650{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003651 xmlSchemaValPtr ret = NULL, prev = NULL, cur;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003652
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003653 /*
3654 * Copy the string values.
3655 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003656 while (val != NULL) {
3657 switch (val->type) {
3658 case XML_SCHEMAS_ANYTYPE:
3659 case XML_SCHEMAS_IDREFS:
3660 case XML_SCHEMAS_ENTITIES:
3661 case XML_SCHEMAS_NMTOKENS:
3662 xmlSchemaFreeValue(ret);
3663 return (NULL);
3664 case XML_SCHEMAS_ANYSIMPLETYPE:
3665 case XML_SCHEMAS_STRING:
3666 case XML_SCHEMAS_NORMSTRING:
3667 case XML_SCHEMAS_TOKEN:
3668 case XML_SCHEMAS_LANGUAGE:
3669 case XML_SCHEMAS_NAME:
3670 case XML_SCHEMAS_NCNAME:
3671 case XML_SCHEMAS_ID:
3672 case XML_SCHEMAS_IDREF:
3673 case XML_SCHEMAS_ENTITY:
3674 case XML_SCHEMAS_NMTOKEN:
3675 case XML_SCHEMAS_ANYURI:
3676 cur = xmlSchemaDupVal(val);
3677 if (val->value.str != NULL)
3678 cur->value.str = xmlStrdup(BAD_CAST val->value.str);
3679 break;
3680 case XML_SCHEMAS_QNAME:
3681 case XML_SCHEMAS_NOTATION:
3682 cur = xmlSchemaDupVal(val);
3683 if (val->value.qname.name != NULL)
3684 cur->value.qname.name =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003685 xmlStrdup(BAD_CAST val->value.qname.name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003686 if (val->value.qname.uri != NULL)
3687 cur->value.qname.uri =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003688 xmlStrdup(BAD_CAST val->value.qname.uri);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003689 break;
3690 case XML_SCHEMAS_HEXBINARY:
3691 cur = xmlSchemaDupVal(val);
3692 if (val->value.hex.str != NULL)
3693 cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
3694 break;
3695 case XML_SCHEMAS_BASE64BINARY:
3696 cur = xmlSchemaDupVal(val);
3697 if (val->value.base64.str != NULL)
3698 cur->value.base64.str =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003699 xmlStrdup(BAD_CAST val->value.base64.str);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003700 break;
3701 default:
3702 cur = xmlSchemaDupVal(val);
3703 break;
3704 }
3705 if (ret == NULL)
3706 ret = cur;
3707 else
3708 prev->next = cur;
3709 prev = cur;
3710 val = val->next;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003711 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003712 return (ret);
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003713}
3714
3715/**
Daniel Veillard5a872412002-05-22 06:40:27 +00003716 * _xmlSchemaDateAdd:
3717 * @dt: an #xmlSchemaValPtr
3718 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
3719 *
3720 * Compute a new date/time from @dt and @dur. This function assumes @dt
3721 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
Daniel Veillard669adfc2004-05-29 20:12:46 +00003722 * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
3723 * @dt. The calling program is responsible for freeing the returned value.
Daniel Veillard5a872412002-05-22 06:40:27 +00003724 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003725 * Returns a pointer to a new #xmlSchemaVal or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003726 */
3727static xmlSchemaValPtr
3728_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
3729{
Daniel Veillard669adfc2004-05-29 20:12:46 +00003730 xmlSchemaValPtr ret, tmp;
Daniel Veillard5a872412002-05-22 06:40:27 +00003731 long carry, tempdays, temp;
3732 xmlSchemaValDatePtr r, d;
3733 xmlSchemaValDurationPtr u;
3734
3735 if ((dt == NULL) || (dur == NULL))
3736 return NULL;
3737
3738 ret = xmlSchemaNewValue(dt->type);
3739 if (ret == NULL)
3740 return NULL;
3741
Daniel Veillard669adfc2004-05-29 20:12:46 +00003742 /* make a copy so we don't alter the original value */
3743 tmp = xmlSchemaDupVal(dt);
3744 if (tmp == NULL) {
3745 xmlSchemaFreeValue(ret);
3746 return NULL;
3747 }
3748
Daniel Veillard5a872412002-05-22 06:40:27 +00003749 r = &(ret->value.date);
Daniel Veillard669adfc2004-05-29 20:12:46 +00003750 d = &(tmp->value.date);
Daniel Veillard5a872412002-05-22 06:40:27 +00003751 u = &(dur->value.dur);
3752
3753 /* normalization */
3754 if (d->mon == 0)
3755 d->mon = 1;
3756
3757 /* normalize for time zone offset */
3758 u->sec -= (d->tzo * 60);
3759 d->tzo = 0;
3760
3761 /* normalization */
3762 if (d->day == 0)
3763 d->day = 1;
3764
3765 /* month */
3766 carry = d->mon + u->mon;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003767 r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
3768 carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003769
3770 /* year (may be modified later) */
3771 r->year = d->year + carry;
3772 if (r->year == 0) {
3773 if (d->year > 0)
3774 r->year--;
3775 else
3776 r->year++;
3777 }
3778
3779 /* time zone */
3780 r->tzo = d->tzo;
3781 r->tz_flag = d->tz_flag;
3782
3783 /* seconds */
3784 r->sec = d->sec + u->sec;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003785 carry = (long) FQUOTIENT((long)r->sec, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003786 if (r->sec != 0.0) {
3787 r->sec = MODULO(r->sec, 60.0);
3788 }
3789
3790 /* minute */
3791 carry += d->min;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003792 r->min = (unsigned int) MODULO(carry, 60);
3793 carry = (long) FQUOTIENT(carry, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003794
3795 /* hours */
3796 carry += d->hour;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003797 r->hour = (unsigned int) MODULO(carry, 24);
3798 carry = (long)FQUOTIENT(carry, 24);
Daniel Veillard5a872412002-05-22 06:40:27 +00003799
3800 /*
3801 * days
3802 * Note we use tempdays because the temporary values may need more
3803 * than 5 bits
3804 */
3805 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
3806 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
3807 tempdays = MAX_DAYINMONTH(r->year, r->mon);
3808 else if (d->day < 1)
3809 tempdays = 1;
3810 else
3811 tempdays = d->day;
3812
3813 tempdays += u->day + carry;
3814
3815 while (1) {
3816 if (tempdays < 1) {
Daniel Veillard5e9576a2005-11-21 11:23:47 +00003817 long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13);
3818 long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003819 if (tyr == 0)
3820 tyr--;
3821 tempdays += MAX_DAYINMONTH(tyr, tmon);
3822 carry = -1;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003823 } else if (tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003824 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
3825 carry = 1;
3826 } else
3827 break;
3828
3829 temp = r->mon + carry;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003830 r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
3831 r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003832 if (r->year == 0) {
3833 if (temp < 1)
3834 r->year--;
3835 else
3836 r->year++;
3837 }
3838 }
3839
3840 r->day = tempdays;
3841
3842 /*
3843 * adjust the date/time type to the date values
3844 */
3845 if (ret->type != XML_SCHEMAS_DATETIME) {
3846 if ((r->hour) || (r->min) || (r->sec))
3847 ret->type = XML_SCHEMAS_DATETIME;
3848 else if (ret->type != XML_SCHEMAS_DATE) {
3849 if ((r->mon != 1) && (r->day != 1))
3850 ret->type = XML_SCHEMAS_DATE;
3851 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
3852 ret->type = XML_SCHEMAS_GYEARMONTH;
3853 }
3854 }
3855
Daniel Veillard669adfc2004-05-29 20:12:46 +00003856 xmlSchemaFreeValue(tmp);
Daniel Veillard5a872412002-05-22 06:40:27 +00003857
Daniel Veillard5a872412002-05-22 06:40:27 +00003858 return ret;
3859}
3860
3861/**
3862 * xmlSchemaDateNormalize:
Daniel Veillard669adfc2004-05-29 20:12:46 +00003863 * @dt: an #xmlSchemaValPtr of a date/time type value.
3864 * @offset: number of seconds to adjust @dt by.
Daniel Veillard5a872412002-05-22 06:40:27 +00003865 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003866 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3867 * the return value is a time-zone offset is present on @dt.
Daniel Veillard5a872412002-05-22 06:40:27 +00003868 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003869 * Returns a normalized copy of @dt or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003870 */
3871static xmlSchemaValPtr
3872xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
3873{
3874 xmlSchemaValPtr dur, ret;
3875
3876 if (dt == NULL)
3877 return NULL;
3878
3879 if (((dt->type != XML_SCHEMAS_TIME) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003880 (dt->type != XML_SCHEMAS_DATETIME) &&
3881 (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
Daniel Veillard5a872412002-05-22 06:40:27 +00003882 return xmlSchemaDupVal(dt);
3883
3884 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
3885 if (dur == NULL)
3886 return NULL;
3887
3888 dur->value.date.sec -= offset;
3889
3890 ret = _xmlSchemaDateAdd(dt, dur);
3891 if (ret == NULL)
3892 return NULL;
3893
3894 xmlSchemaFreeValue(dur);
3895
3896 /* ret->value.date.tzo = 0; */
3897 return ret;
3898}
3899
3900/**
3901 * _xmlSchemaDateCastYMToDays:
3902 * @dt: an #xmlSchemaValPtr
3903 *
3904 * Convert mon and year of @dt to total number of days. Take the
3905 * number of years since (or before) 1 AD and add the number of leap
3906 * years. This is a function because negative
3907 * years must be handled a little differently and there is no zero year.
3908 *
3909 * Returns number of days.
3910 */
3911static long
3912_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
3913{
3914 long ret;
Daniel Veillard49e89632004-09-23 16:24:36 +00003915 int mon;
Daniel Veillard5a872412002-05-22 06:40:27 +00003916
Daniel Veillard49e89632004-09-23 16:24:36 +00003917 mon = dt->value.date.mon;
3918 if (mon <= 0) mon = 1; /* normalization */
3919
3920 if (dt->value.date.year <= 0)
Daniel Veillard5a872412002-05-22 06:40:27 +00003921 ret = (dt->value.date.year * 365) +
3922 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
3923 ((dt->value.date.year+1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003924 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003925 else
3926 ret = ((dt->value.date.year-1) * 365) +
3927 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
3928 ((dt->value.date.year-1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003929 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003930
3931 return ret;
3932}
3933
3934/**
3935 * TIME_TO_NUMBER:
3936 * @dt: an #xmlSchemaValPtr
3937 *
3938 * Calculates the number of seconds in the time portion of @dt.
3939 *
3940 * Returns seconds.
3941 */
3942#define TIME_TO_NUMBER(dt) \
3943 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00003944 (dt->value.date.min * SECS_PER_MIN) + \
3945 (dt->value.date.tzo * SECS_PER_MIN)) + \
3946 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00003947
3948/**
3949 * xmlSchemaCompareDates:
3950 * @x: a first date/time value
3951 * @y: a second date/time value
3952 *
3953 * Compare 2 date/times
3954 *
3955 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3956 * case of error
3957 */
3958static int
3959xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
3960{
3961 unsigned char xmask, ymask, xor_mask, and_mask;
3962 xmlSchemaValPtr p1, p2, q1, q2;
3963 long p1d, p2d, q1d, q2d;
3964
3965 if ((x == NULL) || (y == NULL))
3966 return -2;
3967
3968 if (x->value.date.tz_flag) {
3969
3970 if (!y->value.date.tz_flag) {
3971 p1 = xmlSchemaDateNormalize(x, 0);
3972 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
3973 /* normalize y + 14:00 */
3974 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
3975
3976 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003977 if (p1d < q1d) {
3978 xmlSchemaFreeValue(p1);
3979 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003980 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003981 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003982 double sec;
3983
3984 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003985 if (sec < 0.0) {
3986 xmlSchemaFreeValue(p1);
3987 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003988 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003989 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003990 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00003991 /* normalize y - 14:00 */
3992 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
3993 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
3994 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003995 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00003996 else if (p1d == q2d) {
3997 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
3998 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003999 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004000 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004001 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004002 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004003 xmlSchemaFreeValue(p1);
4004 xmlSchemaFreeValue(q1);
4005 xmlSchemaFreeValue(q2);
4006 if (ret != 0)
4007 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004008 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004009 } else {
4010 xmlSchemaFreeValue(p1);
4011 xmlSchemaFreeValue(q1);
4012 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004013 }
4014 } else if (y->value.date.tz_flag) {
4015 q1 = xmlSchemaDateNormalize(y, 0);
4016 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4017
4018 /* normalize x - 14:00 */
4019 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
4020 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4021
Daniel Veillardfdc91562002-07-01 21:52:03 +00004022 if (p1d < q1d) {
4023 xmlSchemaFreeValue(p1);
4024 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004025 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004026 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004027 double sec;
4028
4029 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004030 if (sec < 0.0) {
4031 xmlSchemaFreeValue(p1);
4032 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004033 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004034 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004035 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004036 /* normalize x + 14:00 */
4037 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
4038 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
4039
Daniel Veillard6560a422003-03-27 21:25:38 +00004040 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004041 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00004042 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004043 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
4044 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004045 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004046 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004047 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004048 }
Daniel Veillard6560a422003-03-27 21:25:38 +00004049 xmlSchemaFreeValue(p1);
4050 xmlSchemaFreeValue(q1);
4051 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004052 if (ret != 0)
4053 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004054 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004055 } else {
4056 xmlSchemaFreeValue(p1);
4057 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004058 }
4059 }
4060
4061 /*
4062 * if the same type then calculate the difference
4063 */
4064 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004065 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004066 q1 = xmlSchemaDateNormalize(y, 0);
4067 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4068
4069 p1 = xmlSchemaDateNormalize(x, 0);
4070 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4071
Daniel Veillardfdc91562002-07-01 21:52:03 +00004072 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004073 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004074 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004075 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004076 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00004077 double sec;
4078
4079 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4080 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004081 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004082 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004083 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004084
4085 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004086 xmlSchemaFreeValue(p1);
4087 xmlSchemaFreeValue(q1);
4088 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004089 }
4090
4091 switch (x->type) {
4092 case XML_SCHEMAS_DATETIME:
4093 xmask = 0xf;
4094 break;
4095 case XML_SCHEMAS_DATE:
4096 xmask = 0x7;
4097 break;
4098 case XML_SCHEMAS_GYEAR:
4099 xmask = 0x1;
4100 break;
4101 case XML_SCHEMAS_GMONTH:
4102 xmask = 0x2;
4103 break;
4104 case XML_SCHEMAS_GDAY:
4105 xmask = 0x3;
4106 break;
4107 case XML_SCHEMAS_GYEARMONTH:
4108 xmask = 0x3;
4109 break;
4110 case XML_SCHEMAS_GMONTHDAY:
4111 xmask = 0x6;
4112 break;
4113 case XML_SCHEMAS_TIME:
4114 xmask = 0x8;
4115 break;
4116 default:
4117 xmask = 0;
4118 break;
4119 }
4120
4121 switch (y->type) {
4122 case XML_SCHEMAS_DATETIME:
4123 ymask = 0xf;
4124 break;
4125 case XML_SCHEMAS_DATE:
4126 ymask = 0x7;
4127 break;
4128 case XML_SCHEMAS_GYEAR:
4129 ymask = 0x1;
4130 break;
4131 case XML_SCHEMAS_GMONTH:
4132 ymask = 0x2;
4133 break;
4134 case XML_SCHEMAS_GDAY:
4135 ymask = 0x3;
4136 break;
4137 case XML_SCHEMAS_GYEARMONTH:
4138 ymask = 0x3;
4139 break;
4140 case XML_SCHEMAS_GMONTHDAY:
4141 ymask = 0x6;
4142 break;
4143 case XML_SCHEMAS_TIME:
4144 ymask = 0x8;
4145 break;
4146 default:
4147 ymask = 0;
4148 break;
4149 }
4150
4151 xor_mask = xmask ^ ymask; /* mark type differences */
4152 and_mask = xmask & ymask; /* mark field specification */
4153
4154 /* year */
4155 if (xor_mask & 1)
4156 return 2; /* indeterminate */
4157 else if (and_mask & 1) {
4158 if (x->value.date.year < y->value.date.year)
4159 return -1;
4160 else if (x->value.date.year > y->value.date.year)
4161 return 1;
4162 }
4163
4164 /* month */
4165 if (xor_mask & 2)
4166 return 2; /* indeterminate */
4167 else if (and_mask & 2) {
4168 if (x->value.date.mon < y->value.date.mon)
4169 return -1;
4170 else if (x->value.date.mon > y->value.date.mon)
4171 return 1;
4172 }
4173
4174 /* day */
4175 if (xor_mask & 4)
4176 return 2; /* indeterminate */
4177 else if (and_mask & 4) {
4178 if (x->value.date.day < y->value.date.day)
4179 return -1;
4180 else if (x->value.date.day > y->value.date.day)
4181 return 1;
4182 }
4183
4184 /* time */
4185 if (xor_mask & 8)
4186 return 2; /* indeterminate */
4187 else if (and_mask & 8) {
4188 if (x->value.date.hour < y->value.date.hour)
4189 return -1;
4190 else if (x->value.date.hour > y->value.date.hour)
4191 return 1;
4192 else if (x->value.date.min < y->value.date.min)
4193 return -1;
4194 else if (x->value.date.min > y->value.date.min)
4195 return 1;
4196 else if (x->value.date.sec < y->value.date.sec)
4197 return -1;
4198 else if (x->value.date.sec > y->value.date.sec)
4199 return 1;
4200 }
4201
Daniel Veillard070803b2002-05-03 07:29:38 +00004202 return 0;
4203}
4204
4205/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004206 * xmlSchemaComparePreserveReplaceStrings:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004207 * @x: a first string value
4208 * @y: a second string value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004209 * @invert: inverts the result if x < y or x > y.
4210 *
4211 * Compare 2 string for their normalized values.
4212 * @x is a string with whitespace of "preserve", @y is
4213 * a string with a whitespace of "replace". I.e. @x could
4214 * be an "xsd:string" and @y an "xsd:normalizedString".
4215 *
4216 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4217 * case of error
4218 */
4219static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004220xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
4221 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004222 int invert)
4223{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004224 int tmp;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004225
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004226 while ((*x != 0) && (*y != 0)) {
4227 if (IS_WSP_REPLACE_CH(*y)) {
4228 if (! IS_WSP_SPACE_CH(*x)) {
4229 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004230 if (invert)
4231 return(1);
4232 else
4233 return(-1);
4234 } else {
4235 if (invert)
4236 return(-1);
4237 else
4238 return(1);
4239 }
4240 }
4241 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004242 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004243 if (tmp < 0) {
4244 if (invert)
4245 return(1);
4246 else
4247 return(-1);
4248 }
4249 if (tmp > 0) {
4250 if (invert)
4251 return(-1);
4252 else
4253 return(1);
4254 }
4255 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004256 x++;
4257 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004258 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004259 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004260 if (invert)
4261 return(-1);
4262 else
4263 return(1);
4264 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004265 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004266 if (invert)
4267 return(1);
4268 else
4269 return(-1);
4270 }
4271 return(0);
4272}
4273
4274/**
4275 * xmlSchemaComparePreserveCollapseStrings:
4276 * @x: a first string value
4277 * @y: a second string value
4278 *
4279 * Compare 2 string for their normalized values.
4280 * @x is a string with whitespace of "preserve", @y is
4281 * a string with a whitespace of "collapse". I.e. @x could
4282 * be an "xsd:string" and @y an "xsd:normalizedString".
4283 *
4284 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4285 * case of error
4286 */
4287static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004288xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
4289 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004290 int invert)
4291{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004292 int tmp;
4293
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004294 /*
4295 * Skip leading blank chars of the collapsed string.
4296 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004297 while IS_WSP_BLANK_CH(*y)
4298 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004299
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004300 while ((*x != 0) && (*y != 0)) {
4301 if IS_WSP_BLANK_CH(*y) {
4302 if (! IS_WSP_SPACE_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004303 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004304 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004305 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004306 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004307 if (invert)
4308 return(1);
4309 else
4310 return(-1);
4311 } else {
4312 if (invert)
4313 return(-1);
4314 else
4315 return(1);
4316 }
4317 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004318 x++;
4319 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004320 /*
4321 * Skip contiguous blank chars of the collapsed string.
4322 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004323 while IS_WSP_BLANK_CH(*y)
4324 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004325 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004326 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004327 if (tmp < 0) {
4328 if (invert)
4329 return(1);
4330 else
4331 return(-1);
4332 }
4333 if (tmp > 0) {
4334 if (invert)
4335 return(-1);
4336 else
4337 return(1);
4338 }
4339 }
4340 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004341 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004342 if (invert)
4343 return(-1);
4344 else
4345 return(1);
4346 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004347 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004348 /*
4349 * Skip trailing blank chars of the collapsed string.
4350 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004351 while IS_WSP_BLANK_CH(*y)
4352 y++;
4353 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004354 if (invert)
4355 return(1);
4356 else
4357 return(-1);
4358 }
4359 }
4360 return(0);
4361}
4362
4363/**
4364 * xmlSchemaComparePreserveCollapseStrings:
4365 * @x: a first string value
4366 * @y: a second string value
4367 *
4368 * Compare 2 string for their normalized values.
4369 * @x is a string with whitespace of "preserve", @y is
4370 * a string with a whitespace of "collapse". I.e. @x could
4371 * be an "xsd:string" and @y an "xsd:normalizedString".
4372 *
4373 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4374 * case of error
4375 */
4376static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004377xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
4378 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004379 int invert)
4380{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004381 int tmp;
4382
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004383 /*
4384 * Skip leading blank chars of the collapsed string.
4385 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004386 while IS_WSP_BLANK_CH(*y)
4387 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004388
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004389 while ((*x != 0) && (*y != 0)) {
4390 if IS_WSP_BLANK_CH(*y) {
4391 if (! IS_WSP_BLANK_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004392 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004393 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004394 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004395 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004396 if (invert)
4397 return(1);
4398 else
4399 return(-1);
4400 } else {
4401 if (invert)
4402 return(-1);
4403 else
4404 return(1);
4405 }
4406 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004407 x++;
4408 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004409 /*
4410 * Skip contiguous blank chars of the collapsed string.
4411 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004412 while IS_WSP_BLANK_CH(*y)
4413 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004414 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004415 if IS_WSP_BLANK_CH(*x) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004416 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004417 * The xv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004418 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004419 if ((0x20 - *y) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004420 if (invert)
4421 return(1);
4422 else
4423 return(-1);
4424 } else {
4425 if (invert)
4426 return(-1);
4427 else
4428 return(1);
4429 }
4430 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004431 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004432 if (tmp < 0)
4433 return(-1);
4434 if (tmp > 0)
4435 return(1);
4436 }
4437 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004438 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004439 if (invert)
4440 return(-1);
4441 else
4442 return(1);
4443 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004444 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004445 /*
4446 * Skip trailing blank chars of the collapsed string.
4447 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004448 while IS_WSP_BLANK_CH(*y)
4449 y++;
4450 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004451 if (invert)
4452 return(1);
4453 else
4454 return(-1);
4455 }
4456 }
4457 return(0);
4458}
4459
4460
4461/**
4462 * xmlSchemaCompareReplacedStrings:
4463 * @x: a first string value
4464 * @y: a second string value
4465 *
4466 * Compare 2 string for their normalized values.
4467 *
4468 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4469 * case of error
4470 */
4471static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004472xmlSchemaCompareReplacedStrings(const xmlChar *x,
4473 const xmlChar *y)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004474{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004475 int tmp;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004476
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004477 while ((*x != 0) && (*y != 0)) {
4478 if IS_WSP_BLANK_CH(*y) {
4479 if (! IS_WSP_BLANK_CH(*x)) {
4480 if ((*x - 0x20) < 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004481 return(-1);
4482 else
4483 return(1);
4484 }
4485 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004486 if IS_WSP_BLANK_CH(*x) {
4487 if ((0x20 - *y) < 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004488 return(-1);
4489 else
4490 return(1);
4491 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004492 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004493 if (tmp < 0)
4494 return(-1);
4495 if (tmp > 0)
4496 return(1);
4497 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004498 x++;
4499 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004500 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004501 if (*x != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004502 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004503 if (*y != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004504 return(-1);
4505 return(0);
4506}
4507
4508/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004509 * xmlSchemaCompareNormStrings:
4510 * @x: a first string value
4511 * @y: a second string value
4512 *
4513 * Compare 2 string for their normalized values.
4514 *
4515 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4516 * case of error
4517 */
4518static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004519xmlSchemaCompareNormStrings(const xmlChar *x,
4520 const xmlChar *y) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00004521 int tmp;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004522
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004523 while (IS_BLANK_CH(*x)) x++;
4524 while (IS_BLANK_CH(*y)) y++;
4525 while ((*x != 0) && (*y != 0)) {
4526 if (IS_BLANK_CH(*x)) {
4527 if (!IS_BLANK_CH(*y)) {
4528 tmp = *x - *y;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004529 return(tmp);
4530 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004531 while (IS_BLANK_CH(*x)) x++;
4532 while (IS_BLANK_CH(*y)) y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004533 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004534 tmp = *x++ - *y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004535 if (tmp < 0)
4536 return(-1);
4537 if (tmp > 0)
4538 return(1);
4539 }
4540 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004541 if (*x != 0) {
4542 while (IS_BLANK_CH(*x)) x++;
4543 if (*x != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004544 return(1);
4545 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004546 if (*y != 0) {
4547 while (IS_BLANK_CH(*y)) y++;
4548 if (*y != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004549 return(-1);
4550 }
4551 return(0);
4552}
4553
4554/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004555 * xmlSchemaCompareFloats:
4556 * @x: a first float or double value
4557 * @y: a second float or double value
4558 *
4559 * Compare 2 values
4560 *
4561 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4562 * case of error
4563 */
4564static int
4565xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4566 double d1, d2;
4567
4568 if ((x == NULL) || (y == NULL))
4569 return(-2);
4570
4571 /*
4572 * Cast everything to doubles.
4573 */
4574 if (x->type == XML_SCHEMAS_DOUBLE)
4575 d1 = x->value.d;
4576 else if (x->type == XML_SCHEMAS_FLOAT)
4577 d1 = x->value.f;
4578 else
4579 return(-2);
4580
4581 if (y->type == XML_SCHEMAS_DOUBLE)
4582 d2 = y->value.d;
4583 else if (y->type == XML_SCHEMAS_FLOAT)
4584 d2 = y->value.f;
4585 else
4586 return(-2);
4587
4588 /*
4589 * Check for special cases.
4590 */
4591 if (xmlXPathIsNaN(d1)) {
4592 if (xmlXPathIsNaN(d2))
4593 return(0);
4594 return(1);
4595 }
4596 if (xmlXPathIsNaN(d2))
4597 return(-1);
4598 if (d1 == xmlXPathPINF) {
4599 if (d2 == xmlXPathPINF)
4600 return(0);
4601 return(1);
4602 }
4603 if (d2 == xmlXPathPINF)
4604 return(-1);
4605 if (d1 == xmlXPathNINF) {
4606 if (d2 == xmlXPathNINF)
4607 return(0);
4608 return(-1);
4609 }
4610 if (d2 == xmlXPathNINF)
4611 return(1);
4612
4613 /*
4614 * basic tests, the last one we should have equality, but
4615 * portability is more important than speed and handling
4616 * NaN or Inf in a portable way is always a challenge, so ...
4617 */
4618 if (d1 < d2)
4619 return(-1);
4620 if (d1 > d2)
4621 return(1);
4622 if (d1 == d2)
4623 return(0);
4624 return(2);
4625}
4626
4627/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004628 * xmlSchemaCompareValues:
4629 * @x: a first value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004630 * @xvalue: the first value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004631 * @xwtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004632 * @y: a second value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004633 * @xvalue: the second value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004634 * @ywtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004635 *
4636 * Compare 2 values
4637 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004638 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4639 * comparable and -2 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004640 */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004641static int
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004642xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
4643 xmlSchemaValPtr x,
4644 const xmlChar *xvalue,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004645 xmlSchemaWhitespaceValueType xws,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004646 xmlSchemaValType ytype,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004647 xmlSchemaValPtr y,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004648 const xmlChar *yvalue,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004649 xmlSchemaWhitespaceValueType yws)
4650{
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004651 switch (xtype) {
Daniel Veillard80b19092003-03-28 13:29:53 +00004652 case XML_SCHEMAS_UNKNOWN:
William M. Brack2f2a6632004-08-20 23:09:47 +00004653 case XML_SCHEMAS_ANYTYPE:
Daniel Veillard80b19092003-03-28 13:29:53 +00004654 return(-2);
4655 case XML_SCHEMAS_INTEGER:
4656 case XML_SCHEMAS_NPINTEGER:
4657 case XML_SCHEMAS_NINTEGER:
4658 case XML_SCHEMAS_NNINTEGER:
4659 case XML_SCHEMAS_PINTEGER:
4660 case XML_SCHEMAS_INT:
4661 case XML_SCHEMAS_UINT:
4662 case XML_SCHEMAS_LONG:
4663 case XML_SCHEMAS_ULONG:
4664 case XML_SCHEMAS_SHORT:
4665 case XML_SCHEMAS_USHORT:
4666 case XML_SCHEMAS_BYTE:
4667 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00004668 case XML_SCHEMAS_DECIMAL:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004669 if ((x == NULL) || (y == NULL))
4670 return(-2);
4671 if (ytype == xtype)
Daniel Veillard80b19092003-03-28 13:29:53 +00004672 return(xmlSchemaCompareDecimals(x, y));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004673 if ((ytype == XML_SCHEMAS_DECIMAL) ||
4674 (ytype == XML_SCHEMAS_INTEGER) ||
4675 (ytype == XML_SCHEMAS_NPINTEGER) ||
4676 (ytype == XML_SCHEMAS_NINTEGER) ||
4677 (ytype == XML_SCHEMAS_NNINTEGER) ||
4678 (ytype == XML_SCHEMAS_PINTEGER) ||
4679 (ytype == XML_SCHEMAS_INT) ||
4680 (ytype == XML_SCHEMAS_UINT) ||
4681 (ytype == XML_SCHEMAS_LONG) ||
4682 (ytype == XML_SCHEMAS_ULONG) ||
4683 (ytype == XML_SCHEMAS_SHORT) ||
4684 (ytype == XML_SCHEMAS_USHORT) ||
4685 (ytype == XML_SCHEMAS_BYTE) ||
4686 (ytype == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00004687 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004688 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00004689 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004690 if ((x == NULL) || (y == NULL))
4691 return(-2);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004692 if (ytype == XML_SCHEMAS_DURATION)
Daniel Veillard070803b2002-05-03 07:29:38 +00004693 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004694 return(-2);
4695 case XML_SCHEMAS_TIME:
4696 case XML_SCHEMAS_GDAY:
4697 case XML_SCHEMAS_GMONTH:
4698 case XML_SCHEMAS_GMONTHDAY:
4699 case XML_SCHEMAS_GYEAR:
4700 case XML_SCHEMAS_GYEARMONTH:
4701 case XML_SCHEMAS_DATE:
4702 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004703 if ((x == NULL) || (y == NULL))
4704 return(-2);
4705 if ((ytype == XML_SCHEMAS_DATETIME) ||
4706 (ytype == XML_SCHEMAS_TIME) ||
4707 (ytype == XML_SCHEMAS_GDAY) ||
4708 (ytype == XML_SCHEMAS_GMONTH) ||
4709 (ytype == XML_SCHEMAS_GMONTHDAY) ||
4710 (ytype == XML_SCHEMAS_GYEAR) ||
4711 (ytype == XML_SCHEMAS_DATE) ||
4712 (ytype == XML_SCHEMAS_GYEARMONTH))
Daniel Veillard5a872412002-05-22 06:40:27 +00004713 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004714 return (-2);
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +00004715 /*
4716 * Note that we will support comparison of string types against
4717 * anySimpleType as well.
4718 */
4719 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004720 case XML_SCHEMAS_STRING:
4721 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00004722 case XML_SCHEMAS_TOKEN:
4723 case XML_SCHEMAS_LANGUAGE:
4724 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00004725 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00004726 case XML_SCHEMAS_NCNAME:
4727 case XML_SCHEMAS_ID:
4728 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00004729 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00004730 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004731 {
4732 const xmlChar *xv, *yv;
4733
4734 if (x == NULL)
4735 xv = xvalue;
4736 else
4737 xv = x->value.str;
4738 if (y == NULL)
4739 yv = yvalue;
4740 else
4741 yv = y->value.str;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004742 /*
4743 * TODO: Compare those against QName.
4744 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004745 if (ytype == XML_SCHEMAS_QNAME) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004746 TODO
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004747 if (y == NULL)
4748 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004749 return (-2);
4750 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004751 if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
4752 (ytype == XML_SCHEMAS_STRING) ||
4753 (ytype == XML_SCHEMAS_NORMSTRING) ||
4754 (ytype == XML_SCHEMAS_TOKEN) ||
4755 (ytype == XML_SCHEMAS_LANGUAGE) ||
4756 (ytype == XML_SCHEMAS_NMTOKEN) ||
4757 (ytype == XML_SCHEMAS_NAME) ||
4758 (ytype == XML_SCHEMAS_NCNAME) ||
4759 (ytype == XML_SCHEMAS_ID) ||
4760 (ytype == XML_SCHEMAS_IDREF) ||
4761 (ytype == XML_SCHEMAS_ENTITY) ||
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004762 (ytype == XML_SCHEMAS_ANYURI)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004763
4764 if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4765
4766 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4767 /* TODO: What about x < y or x > y. */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004768 if (xmlStrEqual(xv, yv))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004769 return (0);
4770 else
4771 return (2);
4772 } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004773 return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004774 else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004775 return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004776
4777 } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
4778
4779 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004780 return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004781 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004782 return (xmlSchemaCompareReplacedStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004783 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004784 return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004785
4786 } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
4787
4788 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004789 return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004790 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004791 return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004792 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004793 return (xmlSchemaCompareNormStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004794 } else
4795 return (-2);
4796
4797 }
Daniel Veillardc4c21552003-03-29 10:53:38 +00004798 return (-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004799 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004800 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004801 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004802 if ((x == NULL) || (y == NULL))
4803 return(-2);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004804 if ((ytype == XML_SCHEMAS_QNAME) ||
4805 (ytype == XML_SCHEMAS_NOTATION)) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004806 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
4807 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
4808 return(0);
4809 return(2);
4810 }
4811 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004812 case XML_SCHEMAS_FLOAT:
4813 case XML_SCHEMAS_DOUBLE:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004814 if ((x == NULL) || (y == NULL))
4815 return(-2);
4816 if ((ytype == XML_SCHEMAS_FLOAT) ||
4817 (ytype == XML_SCHEMAS_DOUBLE))
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004818 return (xmlSchemaCompareFloats(x, y));
4819 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004820 case XML_SCHEMAS_BOOLEAN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004821 if ((x == NULL) || (y == NULL))
4822 return(-2);
4823 if (ytype == XML_SCHEMAS_BOOLEAN) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004824 if (x->value.b == y->value.b)
4825 return(0);
4826 if (x->value.b == 0)
4827 return(-1);
4828 return(1);
4829 }
4830 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00004831 case XML_SCHEMAS_HEXBINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004832 if ((x == NULL) || (y == NULL))
4833 return(-2);
4834 if (ytype == XML_SCHEMAS_HEXBINARY) {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00004835 if (x->value.hex.total == y->value.hex.total) {
4836 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
4837 if (ret > 0)
4838 return(1);
4839 else if (ret == 0)
4840 return(0);
4841 }
4842 else if (x->value.hex.total > y->value.hex.total)
4843 return(1);
4844
4845 return(-1);
4846 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00004847 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004848 case XML_SCHEMAS_BASE64BINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004849 if ((x == NULL) || (y == NULL))
4850 return(-2);
4851 if (ytype == XML_SCHEMAS_BASE64BINARY) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004852 if (x->value.base64.total == y->value.base64.total) {
4853 int ret = xmlStrcmp(x->value.base64.str,
4854 y->value.base64.str);
4855 if (ret > 0)
4856 return(1);
4857 else if (ret == 0)
4858 return(0);
Daniel Veillardd369b132005-07-14 15:54:44 +00004859 else
4860 return(-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004861 }
4862 else if (x->value.base64.total > y->value.base64.total)
4863 return(1);
4864 else
4865 return(-1);
4866 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004867 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004868 case XML_SCHEMAS_IDREFS:
4869 case XML_SCHEMAS_ENTITIES:
4870 case XML_SCHEMAS_NMTOKENS:
4871 TODO
4872 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004873 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004874 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00004875}
4876
4877/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004878 * xmlSchemaCompareValues:
4879 * @x: a first value
4880 * @y: a second value
4881 *
4882 * Compare 2 values
4883 *
4884 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4885 * case of error
4886 */
4887int
4888xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4889 xmlSchemaWhitespaceValueType xws, yws;
4890
Daniel Veillard5e094142005-02-18 19:36:12 +00004891 if ((x == NULL) || (y == NULL))
4892 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004893 if (x->type == XML_SCHEMAS_STRING)
4894 xws = XML_SCHEMA_WHITESPACE_PRESERVE;
4895 else if (x->type == XML_SCHEMAS_NORMSTRING)
4896 xws = XML_SCHEMA_WHITESPACE_REPLACE;
4897 else
4898 xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4899
4900 if (y->type == XML_SCHEMAS_STRING)
4901 yws = XML_SCHEMA_WHITESPACE_PRESERVE;
4902 else if (x->type == XML_SCHEMAS_NORMSTRING)
4903 yws = XML_SCHEMA_WHITESPACE_REPLACE;
4904 else
4905 yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4906
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004907 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4908 y, NULL, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004909}
4910
4911/**
4912 * xmlSchemaCompareValuesWhtsp:
4913 * @x: a first value
4914 * @xws: the whitespace value of x
4915 * @y: a second value
4916 * @yws: the whitespace value of y
4917 *
4918 * Compare 2 values
4919 *
4920 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4921 * case of error
4922 */
4923int
4924xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004925 xmlSchemaWhitespaceValueType xws,
4926 xmlSchemaValPtr y,
4927 xmlSchemaWhitespaceValueType yws)
4928{
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00004929 if ((x == NULL) || (y == NULL))
4930 return(-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004931 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4932 y, NULL, yws));
4933}
4934
4935/**
4936 * xmlSchemaCompareValuesWhtspExt:
4937 * @x: a first value
4938 * @xws: the whitespace value of x
4939 * @y: a second value
4940 * @yws: the whitespace value of y
4941 *
4942 * Compare 2 values
4943 *
4944 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4945 * case of error
4946 */
4947static int
4948xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
4949 xmlSchemaValPtr x,
4950 const xmlChar *xvalue,
4951 xmlSchemaWhitespaceValueType xws,
4952 xmlSchemaValType ytype,
4953 xmlSchemaValPtr y,
4954 const xmlChar *yvalue,
4955 xmlSchemaWhitespaceValueType yws)
4956{
4957 return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
4958 yvalue, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004959}
4960
4961/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004962 * xmlSchemaNormLen:
4963 * @value: a string
4964 *
4965 * Computes the UTF8 length of the normalized value of the string
4966 *
4967 * Returns the length or -1 in case of error.
4968 */
4969static int
4970xmlSchemaNormLen(const xmlChar *value) {
4971 const xmlChar *utf;
4972 int ret = 0;
4973
4974 if (value == NULL)
4975 return(-1);
4976 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00004977 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004978 while (*utf != 0) {
4979 if (utf[0] & 0x80) {
4980 if ((utf[1] & 0xc0) != 0x80)
4981 return(-1);
4982 if ((utf[0] & 0xe0) == 0xe0) {
4983 if ((utf[2] & 0xc0) != 0x80)
4984 return(-1);
4985 if ((utf[0] & 0xf0) == 0xf0) {
4986 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
4987 return(-1);
4988 utf += 4;
4989 } else {
4990 utf += 3;
4991 }
4992 } else {
4993 utf += 2;
4994 }
William M. Brack76e95df2003-10-18 16:20:14 +00004995 } else if (IS_BLANK_CH(*utf)) {
4996 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004997 if (*utf == 0)
4998 break;
4999 } else {
5000 utf++;
5001 }
5002 ret++;
5003 }
5004 return(ret);
5005}
5006
Daniel Veillard6927b102004-10-27 17:29:04 +00005007/**
5008 * xmlSchemaGetFacetValueAsULong:
5009 * @facet: an schemas type facet
5010 *
5011 * Extract the value of a facet
5012 *
5013 * Returns the value as a long
5014 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005015unsigned long
5016xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
5017{
5018 /*
5019 * TODO: Check if this is a decimal.
5020 */
William M. Brack094dd862004-11-14 14:28:34 +00005021 if (facet == NULL)
5022 return 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005023 return ((unsigned long) facet->val->value.decimal.lo);
5024}
5025
Daniel Veillardc4c21552003-03-29 10:53:38 +00005026/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00005027 * xmlSchemaValidateListSimpleTypeFacet:
5028 * @facet: the facet to check
5029 * @value: the lexical repr of the value to validate
5030 * @actualLen: the number of list items
5031 * @expectedLen: the resulting expected number of list items
5032 *
5033 * Checks the value of a list simple type against a facet.
5034 *
5035 * Returns 0 if the value is valid, a positive error code
5036 * number otherwise and -1 in case of an internal error.
5037 */
5038int
5039xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
5040 const xmlChar *value,
5041 unsigned long actualLen,
5042 unsigned long *expectedLen)
5043{
Daniel Veillardce682bc2004-11-05 17:22:25 +00005044 if (facet == NULL)
5045 return(-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005046 /*
5047 * TODO: Check if this will work with large numbers.
5048 * (compare value.decimal.mi and value.decimal.hi as well?).
5049 */
5050 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5051 if (actualLen != facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005052 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005053 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005054 return (XML_SCHEMAV_CVC_LENGTH_VALID);
5055 }
5056 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5057 if (actualLen < facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005058 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005059 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005060 return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
5061 }
5062 } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
5063 if (actualLen > facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005064 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005065 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005066 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5067 }
5068 } else
5069 /*
5070 * NOTE: That we can pass NULL as xmlSchemaValPtr to
5071 * xmlSchemaValidateFacet, since the remaining facet types
5072 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
5073 */
5074 return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
5075 return (0);
5076}
5077
5078/**
Daniel Veillard6927b102004-10-27 17:29:04 +00005079 * xmlSchemaValidateLengthFacet:
Daniel Veillardc0826a72004-08-10 14:17:33 +00005080 * @type: the built-in type
5081 * @facet: the facet to check
5082 * @value: the lexical repr. of the value to be validated
5083 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005084 * @ws: the whitespace type of the value
5085 * @length: the actual length of the value
5086 *
5087 * Checka a value against a "length", "minLength" and "maxLength"
5088 * facet; sets @length to the computed length of @value.
5089 *
5090 * Returns 0 if the value is valid, a positive error code
5091 * otherwise and -1 in case of an internal or API error.
5092 */
5093static int
5094xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
5095 xmlSchemaTypeType valType,
5096 const xmlChar *value,
5097 xmlSchemaValPtr val,
5098 unsigned long *length,
5099 xmlSchemaWhitespaceValueType ws)
5100{
5101 unsigned int len = 0;
5102
5103 if ((length == NULL) || (facet == NULL))
5104 return (-1);
5105 *length = 0;
5106 if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
5107 (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
5108 (facet->type != XML_SCHEMA_FACET_MINLENGTH))
5109 return (-1);
5110
5111 /*
5112 * TODO: length, maxLength and minLength must be of type
5113 * nonNegativeInteger only. Check if decimal is used somehow.
5114 */
5115 if ((facet->val == NULL) ||
5116 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5117 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5118 (facet->val->value.decimal.frac != 0)) {
5119 return(-1);
5120 }
5121 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
5122 len = val->value.hex.total;
5123 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5124 len = val->value.base64.total;
5125 else {
5126 switch (valType) {
5127 case XML_SCHEMAS_STRING:
5128 case XML_SCHEMAS_NORMSTRING:
5129 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5130 /*
5131 * This is to ensure API compatibility with the old
5132 * xmlSchemaValidateLengthFacet(). Anyway, this was and
5133 * is not the correct handling.
5134 * TODO: Get rid of this case somehow.
5135 */
5136 if (valType == XML_SCHEMAS_STRING)
5137 len = xmlUTF8Strlen(value);
5138 else
5139 len = xmlSchemaNormLen(value);
5140 } else if (value != NULL) {
5141 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5142 len = xmlSchemaNormLen(value);
5143 else
5144 /*
5145 * Should be OK for "preserve" as well.
5146 */
5147 len = xmlUTF8Strlen(value);
5148 }
5149 break;
5150 case XML_SCHEMAS_IDREF:
5151 case XML_SCHEMAS_TOKEN:
5152 case XML_SCHEMAS_LANGUAGE:
5153 case XML_SCHEMAS_NMTOKEN:
5154 case XML_SCHEMAS_NAME:
5155 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005156 case XML_SCHEMAS_ID:
5157 /*
5158 * FIXME: What exactly to do with anyURI?
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005159 */
5160 case XML_SCHEMAS_ANYURI:
5161 if (value != NULL)
5162 len = xmlSchemaNormLen(value);
5163 break;
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005164 case XML_SCHEMAS_QNAME:
5165 case XML_SCHEMAS_NOTATION:
5166 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005167 * For QName and NOTATION, those facets are
5168 * deprecated and should be ignored.
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005169 */
5170 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005171 default:
5172 TODO
5173 }
5174 }
5175 *length = (unsigned long) len;
5176 /*
5177 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
5178 */
5179 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5180 if (len != facet->val->value.decimal.lo)
5181 return(XML_SCHEMAV_CVC_LENGTH_VALID);
5182 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5183 if (len < facet->val->value.decimal.lo)
5184 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
5185 } else {
5186 if (len > facet->val->value.decimal.lo)
5187 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5188 }
5189
5190 return (0);
5191}
5192
5193/**
5194 * xmlSchemaValidateLengthFacet:
5195 * @type: the built-in type
5196 * @facet: the facet to check
5197 * @value: the lexical repr. of the value to be validated
5198 * @val: the precomputed value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005199 * @length: the actual length of the value
5200 *
5201 * Checka a value against a "length", "minLength" and "maxLength"
5202 * facet; sets @length to the computed length of @value.
5203 *
5204 * Returns 0 if the value is valid, a positive error code
5205 * otherwise and -1 in case of an internal or API error.
5206 */
5207int
5208xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
5209 xmlSchemaFacetPtr facet,
5210 const xmlChar *value,
5211 xmlSchemaValPtr val,
5212 unsigned long *length)
5213{
Daniel Veillardcc5e2332005-03-16 21:55:35 +00005214 if (type == NULL)
5215 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005216 return (xmlSchemaValidateLengthFacetInternal(facet,
5217 type->builtInType, value, val, length,
5218 XML_SCHEMA_WHITESPACE_UNKNOWN));
5219}
Daniel Veillardc0826a72004-08-10 14:17:33 +00005220
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005221/**
5222 * xmlSchemaValidateLengthFacetWhtsp:
5223 * @facet: the facet to check
5224 * @valType: the built-in type
5225 * @value: the lexical repr. of the value to be validated
5226 * @val: the precomputed value
5227 * @ws: the whitespace type of the value
5228 * @length: the actual length of the value
5229 *
5230 * Checka a value against a "length", "minLength" and "maxLength"
5231 * facet; sets @length to the computed length of @value.
5232 *
5233 * Returns 0 if the value is valid, a positive error code
5234 * otherwise and -1 in case of an internal or API error.
5235 */
5236int
5237xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
5238 xmlSchemaValType valType,
5239 const xmlChar *value,
5240 xmlSchemaValPtr val,
5241 unsigned long *length,
5242 xmlSchemaWhitespaceValueType ws)
5243{
5244 return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
5245 length, ws));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005246}
5247
5248/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005249 * xmlSchemaValidateFacetInternal:
Daniel Veillard4255d502002-04-16 15:50:10 +00005250 * @facet: the facet to check
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005251 * @fws: the whitespace type of the facet's value
5252 * @valType: the built-in type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005253 * @value: the lexical repr of the value to validate
5254 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005255 * @ws: the whitespace type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005256 *
5257 * Check a value against a facet condition
5258 *
5259 * Returns 0 if the element is schemas valid, a positive error code
5260 * number otherwise and -1 in case of internal or API error.
5261 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005262static int
5263xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
5264 xmlSchemaWhitespaceValueType fws,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005265 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005266 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005267 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005268 xmlSchemaWhitespaceValueType ws)
Daniel Veillard4255d502002-04-16 15:50:10 +00005269{
5270 int ret;
5271
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005272 if (facet == NULL)
5273 return(-1);
5274
Daniel Veillard4255d502002-04-16 15:50:10 +00005275 switch (facet->type) {
5276 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005277 /*
5278 * NOTE that for patterns, the @value needs to be the normalized
5279 * value, *not* the lexical initial value or the canonical value.
5280 */
5281 if (value == NULL)
5282 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005283 ret = xmlRegexpExec(facet->regexp, value);
5284 if (ret == 1)
5285 return(0);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005286 if (ret == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005287 return(XML_SCHEMAV_CVC_PATTERN_VALID);
Daniel Veillard4255d502002-04-16 15:50:10 +00005288 return(ret);
5289 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
5290 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005291 if (ret == -2)
Daniel Veillard4255d502002-04-16 15:50:10 +00005292 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005293 if (ret == -1)
5294 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005295 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005296 case XML_SCHEMA_FACET_MAXINCLUSIVE:
5297 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005298 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005299 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005300 if ((ret == -1) || (ret == 0))
5301 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005302 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005303 case XML_SCHEMA_FACET_MINEXCLUSIVE:
5304 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005305 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005306 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005307 if (ret == 1)
5308 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005309 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005310 case XML_SCHEMA_FACET_MININCLUSIVE:
5311 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005312 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005313 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005314 if ((ret == 1) || (ret == 0))
5315 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005316 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
Daniel Veillard8651f532002-04-17 09:06:27 +00005317 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005318 /* TODO whitespaces */
Daniel Veillard01fa6152004-06-29 17:04:39 +00005319 /*
5320 * NOTE: Whitespace should be handled to normalize
5321 * the value to be validated against a the facets;
5322 * not to normalize the value in-between.
5323 */
Daniel Veillard8651f532002-04-17 09:06:27 +00005324 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00005325 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005326 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5327 /*
5328 * This is to ensure API compatibility with the old
5329 * xmlSchemaValidateFacet().
5330 * TODO: Get rid of this case.
5331 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005332 if ((facet->value != NULL) &&
5333 (xmlStrEqual(facet->value, value)))
5334 return(0);
5335 } else {
5336 ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
5337 facet->val, facet->value, fws, valType, val,
5338 value, ws);
5339 if (ret == -2)
5340 return(-1);
5341 if (ret == 0)
5342 return(0);
5343 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005344 return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005345 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005346 /*
5347 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5348 * then any {value} is facet-valid."
5349 */
5350 if ((valType == XML_SCHEMAS_QNAME) ||
5351 (valType == XML_SCHEMAS_NOTATION))
5352 return (0);
5353 /* No break on purpose. */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005354 case XML_SCHEMA_FACET_MAXLENGTH:
5355 case XML_SCHEMA_FACET_MINLENGTH: {
5356 unsigned int len = 0;
5357
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005358 if ((valType == XML_SCHEMAS_QNAME) ||
5359 (valType == XML_SCHEMAS_NOTATION))
5360 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005361 /*
5362 * TODO: length, maxLength and minLength must be of type
5363 * nonNegativeInteger only. Check if decimal is used somehow.
5364 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005365 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005366 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5367 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005368 (facet->val->value.decimal.frac != 0)) {
5369 return(-1);
5370 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005371 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005372 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00005373 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5374 len = val->value.base64.total;
5375 else {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005376 switch (valType) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005377 case XML_SCHEMAS_STRING:
5378 case XML_SCHEMAS_NORMSTRING:
5379 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5380 /*
5381 * This is to ensure API compatibility with the old
5382 * xmlSchemaValidateFacet(). Anyway, this was and
5383 * is not the correct handling.
5384 * TODO: Get rid of this case somehow.
5385 */
5386 if (valType == XML_SCHEMAS_STRING)
5387 len = xmlUTF8Strlen(value);
5388 else
5389 len = xmlSchemaNormLen(value);
5390 } else if (value != NULL) {
5391 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5392 len = xmlSchemaNormLen(value);
5393 else
5394 /*
5395 * Should be OK for "preserve" as well.
5396 */
5397 len = xmlUTF8Strlen(value);
5398 }
5399 break;
5400 case XML_SCHEMAS_IDREF:
Daniel Veillard560c2a42003-07-06 21:13:49 +00005401 case XML_SCHEMAS_TOKEN:
5402 case XML_SCHEMAS_LANGUAGE:
5403 case XML_SCHEMAS_NMTOKEN:
5404 case XML_SCHEMAS_NAME:
5405 case XML_SCHEMAS_NCNAME:
5406 case XML_SCHEMAS_ID:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005407 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005408 if (value != NULL)
5409 len = xmlSchemaNormLen(value);
5410 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00005411 default:
5412 TODO
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005413 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005414 }
5415 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005416 if (len != facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005417 return(XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005418 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005419 if (len < facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005420 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005421 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005422 if (len > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005423 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005424 }
5425 break;
5426 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005427 case XML_SCHEMA_FACET_TOTALDIGITS:
5428 case XML_SCHEMA_FACET_FRACTIONDIGITS:
5429
5430 if ((facet->val == NULL) ||
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00005431 ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
Daniel Veillard560c2a42003-07-06 21:13:49 +00005432 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5433 (facet->val->value.decimal.frac != 0)) {
5434 return(-1);
5435 }
5436 if ((val == NULL) ||
5437 ((val->type != XML_SCHEMAS_DECIMAL) &&
5438 (val->type != XML_SCHEMAS_INTEGER) &&
5439 (val->type != XML_SCHEMAS_NPINTEGER) &&
5440 (val->type != XML_SCHEMAS_NINTEGER) &&
5441 (val->type != XML_SCHEMAS_NNINTEGER) &&
5442 (val->type != XML_SCHEMAS_PINTEGER) &&
5443 (val->type != XML_SCHEMAS_INT) &&
5444 (val->type != XML_SCHEMAS_UINT) &&
5445 (val->type != XML_SCHEMAS_LONG) &&
5446 (val->type != XML_SCHEMAS_ULONG) &&
5447 (val->type != XML_SCHEMAS_SHORT) &&
5448 (val->type != XML_SCHEMAS_USHORT) &&
5449 (val->type != XML_SCHEMAS_BYTE) &&
5450 (val->type != XML_SCHEMAS_UBYTE))) {
5451 return(-1);
5452 }
5453 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
5454 if (val->value.decimal.total > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005455 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005456
5457 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
5458 if (val->value.decimal.frac > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005459 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005460 }
5461 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00005462 default:
5463 TODO
5464 }
5465 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00005466
Daniel Veillard4255d502002-04-16 15:50:10 +00005467}
5468
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005469/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005470 * xmlSchemaValidateFacet:
5471 * @base: the base type
5472 * @facet: the facet to check
5473 * @value: the lexical repr of the value to validate
5474 * @val: the precomputed value
5475 *
5476 * Check a value against a facet condition
5477 *
5478 * Returns 0 if the element is schemas valid, a positive error code
5479 * number otherwise and -1 in case of internal or API error.
5480 */
5481int
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005482xmlSchemaValidateFacet(xmlSchemaTypePtr base,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005483 xmlSchemaFacetPtr facet,
5484 const xmlChar *value,
5485 xmlSchemaValPtr val)
5486{
5487 /*
5488 * This tries to ensure API compatibility regarding the old
5489 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5490 * xmlSchemaValidateFacetWhtsp().
5491 */
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005492 if (val != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005493 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005494 XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005495 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005496 else if (base != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005497 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005498 XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005499 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005500 return(-1);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005501}
5502
5503/**
5504 * xmlSchemaValidateFacetWhtsp:
5505 * @facet: the facet to check
5506 * @fws: the whitespace type of the facet's value
5507 * @valType: the built-in type of the value
5508 * @value: the lexical (or normalized for pattern) repr of the value to validate
5509 * @val: the precomputed value
5510 * @ws: the whitespace type of the value
5511 *
5512 * Check a value against a facet condition. This takes value normalization
5513 * according to the specified whitespace types into account.
5514 * Note that @value needs to be the *normalized* value if the facet
5515 * is of type "pattern".
5516 *
5517 * Returns 0 if the element is schemas valid, a positive error code
5518 * number otherwise and -1 in case of internal or API error.
5519 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005520int
5521xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
5522 xmlSchemaWhitespaceValueType fws,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005523 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005524 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005525 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005526 xmlSchemaWhitespaceValueType ws)
5527{
5528 return(xmlSchemaValidateFacetInternal(facet, fws, valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005529 value, val, ws));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005530}
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005531
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005532#if 0
5533#ifndef DBL_DIG
5534#define DBL_DIG 16
5535#endif
5536#ifndef DBL_EPSILON
5537#define DBL_EPSILON 1E-9
5538#endif
5539
5540#define INTEGER_DIGITS DBL_DIG
5541#define FRACTION_DIGITS (DBL_DIG + 1)
5542#define EXPONENT_DIGITS (3 + 2)
5543
5544/**
5545 * xmlXPathFormatNumber:
5546 * @number: number to format
5547 * @buffer: output buffer
5548 * @buffersize: size of output buffer
5549 *
5550 * Convert the number into a string representation.
5551 */
5552static void
5553xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
5554{
5555 switch (xmlXPathIsInf(number)) {
5556 case 1:
5557 if (buffersize > (int)sizeof("INF"))
5558 snprintf(buffer, buffersize, "INF");
5559 break;
5560 case -1:
5561 if (buffersize > (int)sizeof("-INF"))
5562 snprintf(buffer, buffersize, "-INF");
5563 break;
5564 default:
5565 if (xmlXPathIsNaN(number)) {
5566 if (buffersize > (int)sizeof("NaN"))
5567 snprintf(buffer, buffersize, "NaN");
5568 } else if (number == 0) {
5569 snprintf(buffer, buffersize, "0.0E0");
5570 } else {
5571 /* 3 is sign, decimal point, and terminating zero */
5572 char work[DBL_DIG + EXPONENT_DIGITS + 3];
5573 int integer_place, fraction_place;
5574 char *ptr;
5575 char *after_fraction;
5576 double absolute_value;
5577 int size;
5578
5579 absolute_value = fabs(number);
5580
5581 /*
5582 * Result is in work, and after_fraction points
5583 * just past the fractional part.
5584 * Use scientific notation
5585 */
5586 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
5587 fraction_place = DBL_DIG - 1;
5588 snprintf(work, sizeof(work),"%*.*e",
5589 integer_place, fraction_place, number);
5590 after_fraction = strchr(work + DBL_DIG, 'e');
5591 /* Remove fractional trailing zeroes */
5592 ptr = after_fraction;
5593 while (*(--ptr) == '0')
5594 ;
5595 if (*ptr != '.')
5596 ptr++;
5597 while ((*ptr++ = *after_fraction++) != 0);
5598
5599 /* Finally copy result back to caller */
5600 size = strlen(work) + 1;
5601 if (size > buffersize) {
5602 work[buffersize - 1] = 0;
5603 size = buffersize;
5604 }
5605 memmove(buffer, work, size);
5606 }
5607 break;
5608 }
5609}
5610#endif
5611
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005612/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005613 * xmlSchemaGetCanonValue:
5614 * @val: the precomputed value
5615 * @retValue: the returned value
5616 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005617 * Get a the cononical lexical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005618 * The caller has to FREE the returned retValue.
5619 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005620 * WARNING: Some value types are not supported yet, resulting
5621 * in a @retValue of "???".
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005622 *
5623 * TODO: XML Schema 1.0 does not define canonical representations
5624 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5625 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005626 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005627 *
5628 * Returns 0 if the value could be built, 1 if the value type is
5629 * not supported yet and -1 in case of API errors.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005630 */
5631int
Daniel Veillardb5839c32005-02-19 18:27:14 +00005632xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005633{
Daniel Veillardb5839c32005-02-19 18:27:14 +00005634 if ((retValue == NULL) || (val == NULL))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005635 return (-1);
5636 *retValue = NULL;
5637 switch (val->type) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005638 case XML_SCHEMAS_STRING:
5639 if (val->value.str == NULL)
5640 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5641 else
5642 *retValue =
5643 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5644 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005645 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005646 if (val->value.str == NULL)
5647 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5648 else {
5649 *retValue = xmlSchemaWhiteSpaceReplace(
5650 (const xmlChar *) val->value.str);
5651 if ((*retValue) == NULL)
5652 *retValue = BAD_CAST xmlStrdup(
5653 (const xmlChar *) val->value.str);
5654 }
5655 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005656 case XML_SCHEMAS_TOKEN:
5657 case XML_SCHEMAS_LANGUAGE:
5658 case XML_SCHEMAS_NMTOKEN:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005659 case XML_SCHEMAS_NAME:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005660 case XML_SCHEMAS_NCNAME:
5661 case XML_SCHEMAS_ID:
5662 case XML_SCHEMAS_IDREF:
5663 case XML_SCHEMAS_ENTITY:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005664 case XML_SCHEMAS_NOTATION: /* Unclear */
5665 case XML_SCHEMAS_ANYURI: /* Unclear */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005666 if (val->value.str == NULL)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005667 return (-1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005668 *retValue =
5669 BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
5670 if (*retValue == NULL)
5671 *retValue =
5672 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005673 break;
5674 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005675 /* TODO: Unclear in XML Schema 1.0. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005676 if (val->value.qname.uri == NULL) {
5677 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
5678 return (0);
5679 } else {
5680 *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
5681 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5682 BAD_CAST val->value.qname.uri);
5683 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5684 BAD_CAST "}");
5685 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5686 BAD_CAST val->value.qname.uri);
5687 }
5688 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005689 case XML_SCHEMAS_DECIMAL:
5690 /*
5691 * TODO: Lookout for a more simple implementation.
5692 */
5693 if ((val->value.decimal.total == 1) &&
5694 (val->value.decimal.lo == 0)) {
5695 *retValue = xmlStrdup(BAD_CAST "0.0");
5696 } else {
5697 xmlSchemaValDecimal dec = val->value.decimal;
5698 int bufsize;
5699 char *buf = NULL, *offs;
5700
5701 /* Add room for the decimal point as well. */
5702 bufsize = dec.total + 2;
5703 if (dec.sign)
5704 bufsize++;
5705 /* Add room for leading/trailing zero. */
5706 if ((dec.frac == 0) || (dec.frac == dec.total))
5707 bufsize++;
5708 buf = xmlMalloc(bufsize);
5709 offs = buf;
5710 if (dec.sign)
5711 *offs++ = '-';
5712 if (dec.frac == dec.total) {
5713 *offs++ = '0';
5714 *offs++ = '.';
5715 }
5716 if (dec.hi != 0)
5717 snprintf(offs, bufsize - (offs - buf),
5718 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5719 else if (dec.mi != 0)
5720 snprintf(offs, bufsize - (offs - buf),
5721 "%lu%lu", dec.mi, dec.lo);
5722 else
5723 snprintf(offs, bufsize - (offs - buf),
5724 "%lu", dec.lo);
5725
5726 if (dec.frac != 0) {
5727 if (dec.frac != dec.total) {
5728 int diff = dec.total - dec.frac;
5729 /*
5730 * Insert the decimal point.
5731 */
5732 memmove(offs + diff + 1, offs + diff, dec.frac +1);
5733 offs[diff] = '.';
5734 } else {
5735 unsigned int i = 0;
5736 /*
5737 * Insert missing zeroes behind the decimal point.
5738 */
5739 while (*(offs + i) != 0)
5740 i++;
5741 if (i < dec.total) {
5742 memmove(offs + (dec.total - i), offs, i +1);
5743 memset(offs, '0', dec.total - i);
5744 }
5745 }
5746 } else {
5747 /*
5748 * Append decimal point and zero.
5749 */
5750 offs = buf + bufsize - 1;
5751 *offs-- = 0;
5752 *offs-- = '0';
5753 *offs-- = '.';
5754 }
5755 *retValue = BAD_CAST buf;
5756 }
5757 break;
5758 case XML_SCHEMAS_INTEGER:
5759 case XML_SCHEMAS_PINTEGER:
5760 case XML_SCHEMAS_NPINTEGER:
5761 case XML_SCHEMAS_NINTEGER:
5762 case XML_SCHEMAS_NNINTEGER:
5763 case XML_SCHEMAS_LONG:
5764 case XML_SCHEMAS_BYTE:
5765 case XML_SCHEMAS_SHORT:
5766 case XML_SCHEMAS_INT:
5767 case XML_SCHEMAS_UINT:
5768 case XML_SCHEMAS_ULONG:
5769 case XML_SCHEMAS_USHORT:
5770 case XML_SCHEMAS_UBYTE:
5771 if ((val->value.decimal.total == 1) &&
5772 (val->value.decimal.lo == 0))
5773 *retValue = xmlStrdup(BAD_CAST "0");
5774 else {
5775 xmlSchemaValDecimal dec = val->value.decimal;
5776 int bufsize = dec.total + 1;
5777
5778 /* Add room for the decimal point as well. */
5779 if (dec.sign)
5780 bufsize++;
5781 *retValue = xmlMalloc(bufsize);
5782 if (dec.hi != 0) {
5783 if (dec.sign)
5784 snprintf((char *) *retValue, bufsize,
5785 "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5786 else
5787 snprintf((char *) *retValue, bufsize,
5788 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5789 } else if (dec.mi != 0) {
5790 if (dec.sign)
5791 snprintf((char *) *retValue, bufsize,
5792 "-%lu%lu", dec.mi, dec.lo);
5793 else
5794 snprintf((char *) *retValue, bufsize,
5795 "%lu%lu", dec.mi, dec.lo);
5796 } else {
5797 if (dec.sign)
5798 snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
5799 else
5800 snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
5801 }
5802 }
5803 break;
5804 case XML_SCHEMAS_BOOLEAN:
5805 if (val->value.b)
5806 *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
5807 else
5808 *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
5809 break;
5810 case XML_SCHEMAS_DURATION: {
5811 char buf[100];
5812 unsigned long year;
5813 unsigned long mon, day, hour = 0, min = 0;
5814 double sec = 0, left;
5815
5816 /* TODO: Unclear in XML Schema 1.0 */
5817 /*
5818 * TODO: This results in a normalized output of the value
5819 * - which is NOT conformant to the spec -
5820 * since the exact values of each property are not
5821 * recoverable. Think about extending the structure to
5822 * provide a field for every property.
5823 */
5824 year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
5825 mon = labs(val->value.dur.mon) - 12 * year;
5826
5827 day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
5828 left = fabs(val->value.dur.sec) - day * 86400;
5829 if (left > 0) {
5830 hour = (unsigned long) FQUOTIENT(left, 3600);
5831 left = left - (hour * 3600);
5832 if (left > 0) {
5833 min = (unsigned long) FQUOTIENT(left, 60);
5834 sec = left - (min * 60);
5835 }
5836 }
5837 if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
5838 snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5839 year, mon, day, hour, min, sec);
5840 else
5841 snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5842 year, mon, day, hour, min, sec);
5843 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5844 }
5845 break;
5846 case XML_SCHEMAS_GYEAR: {
5847 char buf[30];
5848 /* TODO: Unclear in XML Schema 1.0 */
5849 /* TODO: What to do with the timezone? */
5850 snprintf(buf, 30, "%04ld", val->value.date.year);
5851 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5852 }
5853 break;
5854 case XML_SCHEMAS_GMONTH: {
5855 /* TODO: Unclear in XML Schema 1.0 */
5856 /* TODO: What to do with the timezone? */
5857 *retValue = xmlMalloc(5);
5858 snprintf((char *) *retValue, 6, "--%02u",
5859 val->value.date.mon);
5860 }
5861 break;
5862 case XML_SCHEMAS_GDAY: {
5863 /* TODO: Unclear in XML Schema 1.0 */
5864 /* TODO: What to do with the timezone? */
5865 *retValue = xmlMalloc(6);
5866 snprintf((char *) *retValue, 6, "---%02u",
5867 val->value.date.day);
5868 }
5869 break;
5870 case XML_SCHEMAS_GMONTHDAY: {
5871 /* TODO: Unclear in XML Schema 1.0 */
5872 /* TODO: What to do with the timezone? */
5873 *retValue = xmlMalloc(8);
5874 snprintf((char *) *retValue, 8, "--%02u-%02u",
5875 val->value.date.mon, val->value.date.day);
5876 }
5877 break;
5878 case XML_SCHEMAS_GYEARMONTH: {
5879 char buf[35];
5880 /* TODO: Unclear in XML Schema 1.0 */
5881 /* TODO: What to do with the timezone? */
5882 if (val->value.date.year < 0)
5883 snprintf(buf, 35, "-%04ld-%02u",
5884 labs(val->value.date.year),
5885 val->value.date.mon);
5886 else
5887 snprintf(buf, 35, "%04ld-%02u",
5888 val->value.date.year, val->value.date.mon);
5889 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5890 }
5891 break;
5892 case XML_SCHEMAS_TIME:
5893 {
5894 char buf[30];
5895
5896 if (val->value.date.tz_flag) {
5897 xmlSchemaValPtr norm;
5898
5899 norm = xmlSchemaDateNormalize(val, 0);
5900 if (norm == NULL)
5901 return (-1);
5902 /*
5903 * TODO: Check if "%.14g" is portable.
5904 */
5905 snprintf(buf, 30,
5906 "%02u:%02u:%02.14gZ",
5907 norm->value.date.hour,
5908 norm->value.date.min,
5909 norm->value.date.sec);
5910 xmlSchemaFreeValue(norm);
5911 } else {
5912 snprintf(buf, 30,
5913 "%02u:%02u:%02.14g",
5914 val->value.date.hour,
5915 val->value.date.min,
5916 val->value.date.sec);
5917 }
5918 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5919 }
5920 break;
5921 case XML_SCHEMAS_DATE:
5922 {
5923 char buf[30];
5924
5925 if (val->value.date.tz_flag) {
5926 xmlSchemaValPtr norm;
5927
5928 norm = xmlSchemaDateNormalize(val, 0);
5929 if (norm == NULL)
5930 return (-1);
5931 /*
5932 * TODO: Append the canonical value of the
5933 * recoverable timezone and not "Z".
5934 */
5935 snprintf(buf, 30,
5936 "%04ld:%02u:%02uZ",
5937 norm->value.date.year, norm->value.date.mon,
5938 norm->value.date.day);
5939 xmlSchemaFreeValue(norm);
5940 } else {
5941 snprintf(buf, 30,
5942 "%04ld:%02u:%02u",
5943 val->value.date.year, val->value.date.mon,
5944 val->value.date.day);
5945 }
5946 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5947 }
5948 break;
5949 case XML_SCHEMAS_DATETIME:
5950 {
5951 char buf[50];
5952
5953 if (val->value.date.tz_flag) {
5954 xmlSchemaValPtr norm;
5955
5956 norm = xmlSchemaDateNormalize(val, 0);
5957 if (norm == NULL)
5958 return (-1);
5959 /*
5960 * TODO: Check if "%.14g" is portable.
5961 */
5962 snprintf(buf, 50,
5963 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
5964 norm->value.date.year, norm->value.date.mon,
5965 norm->value.date.day, norm->value.date.hour,
5966 norm->value.date.min, norm->value.date.sec);
5967 xmlSchemaFreeValue(norm);
5968 } else {
5969 snprintf(buf, 50,
5970 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
5971 val->value.date.year, val->value.date.mon,
5972 val->value.date.day, val->value.date.hour,
5973 val->value.date.min, val->value.date.sec);
5974 }
5975 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5976 }
5977 break;
5978 case XML_SCHEMAS_HEXBINARY:
5979 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
5980 break;
5981 case XML_SCHEMAS_BASE64BINARY:
5982 /*
5983 * TODO: Is the following spec piece implemented?:
5984 * SPEC: "Note: For some values the canonical form defined
5985 * above does not conform to [RFC 2045], which requires breaking
5986 * with linefeeds at appropriate intervals."
5987 */
5988 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
5989 break;
5990 case XML_SCHEMAS_FLOAT: {
5991 char buf[30];
5992 /*
5993 * |m| < 16777216, -149 <= e <= 104.
5994 * TODO: Handle, NaN, INF, -INF. The format is not
5995 * yet conformant. The c type float does not cover
5996 * the whole range.
5997 */
5998 snprintf(buf, 30, "%01.14e", val->value.f);
5999 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6000 }
6001 break;
6002 case XML_SCHEMAS_DOUBLE: {
6003 char buf[40];
6004 /* |m| < 9007199254740992, -1075 <= e <= 970 */
6005 /*
6006 * TODO: Handle, NaN, INF, -INF. The format is not
6007 * yet conformant. The c type float does not cover
6008 * the whole range.
6009 */
6010 snprintf(buf, 40, "%01.14e", val->value.d);
6011 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6012 }
6013 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006014 default:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006015 *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006016 return (1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006017 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006018 return (0);
6019}
6020
Daniel Veillardbda59572005-04-01 17:15:17 +00006021/**
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006022 * xmlSchemaGetCanonValueWhtsp:
6023 * @val: the precomputed value
6024 * @retValue: the returned value
6025 * @ws: the whitespace type of the value
6026 *
6027 * Get a the cononical representation of the value.
6028 * The caller has to free the returned @retValue.
6029 *
6030 * Returns 0 if the value could be built, 1 if the value type is
6031 * not supported yet and -1 in case of API errors.
6032 */
6033int
6034xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
6035 const xmlChar **retValue,
6036 xmlSchemaWhitespaceValueType ws)
6037{
6038 if ((retValue == NULL) || (val == NULL))
6039 return (-1);
6040 if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
6041 (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
6042 return (-1);
6043
6044 *retValue = NULL;
6045 switch (val->type) {
6046 case XML_SCHEMAS_STRING:
6047 if (val->value.str == NULL)
6048 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6049 else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6050 *retValue = xmlSchemaCollapseString(val->value.str);
6051 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
6052 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6053 if ((*retValue) == NULL)
6054 *retValue = BAD_CAST xmlStrdup(val->value.str);
6055 break;
6056 case XML_SCHEMAS_NORMSTRING:
6057 if (val->value.str == NULL)
6058 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6059 else {
6060 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6061 *retValue = xmlSchemaCollapseString(val->value.str);
6062 else
6063 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6064 if ((*retValue) == NULL)
6065 *retValue = BAD_CAST xmlStrdup(val->value.str);
6066 }
6067 break;
6068 default:
6069 return (xmlSchemaGetCanonValue(val, retValue));
6070 }
6071 return (0);
6072}
6073
6074/**
Daniel Veillardbda59572005-04-01 17:15:17 +00006075 * xmlSchemaGetValType:
6076 * @val: a schemas value
6077 *
6078 * Accessor for the type of a value
6079 *
6080 * Returns the xmlSchemaValType of the value
6081 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006082xmlSchemaValType
6083xmlSchemaGetValType(xmlSchemaValPtr val)
6084{
Daniel Veillardbda59572005-04-01 17:15:17 +00006085 if (val == NULL)
6086 return(XML_SCHEMAS_UNKNOWN);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006087 return (val->type);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006088}
6089
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006090#define bottom_xmlschemastypes
6091#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00006092#endif /* LIBXML_SCHEMAS_ENABLED */