blob: ff64f50a6485747f340bc290eb10458b4df4a2cb [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 Veillardf8e3db02012-09-11 13:26:36 +080043#define TODO \
Daniel Veillard4255d502002-04-16 15:50:10 +000044 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 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +080078 long day;
Daniel Veillard070803b2002-05-03 07:29:38 +000079 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 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800192 * Datatype error handlers *
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000193 * *
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 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800209 * Base types support *
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000210 * *
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();
Daniel Veillard14b56432006-03-09 18:41:40 +0000240 if (ret == NULL) {
241 return(NULL);
242 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000243 ret->type = XML_SCHEMA_FACET_MINLENGTH;
244 ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER);
Daniel Veillard717042d2013-07-22 14:28:20 +0800245 if (ret->val == NULL) {
246 xmlFree(ret);
247 return(NULL);
248 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000249 ret->val->value.decimal.lo = value;
250 return (ret);
251}
252
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000253/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000254 * xmlSchemaInitBasicType:
255 * @name: the type name
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000256 * @type: the value type associated
Daniel Veillard4255d502002-04-16 15:50:10 +0000257 *
Daniel Veillard01fa6152004-06-29 17:04:39 +0000258 * Initialize one primitive built-in type
Daniel Veillard4255d502002-04-16 15:50:10 +0000259 */
260static xmlSchemaTypePtr
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800261xmlSchemaInitBasicType(const char *name, xmlSchemaValType type,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000262 xmlSchemaTypePtr baseType) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000263 xmlSchemaTypePtr ret;
264
265 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
266 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000267 xmlSchemaTypeErrMemory(NULL, "could not initialize basic types");
Daniel Veillard4255d502002-04-16 15:50:10 +0000268 return(NULL);
269 }
270 memset(ret, 0, sizeof(xmlSchemaType));
Daniel Veillardbe9c6322003-11-22 20:37:51 +0000271 ret->name = (const xmlChar *)name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000272 ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME;
Daniel Veillard4255d502002-04-16 15:50:10 +0000273 ret->type = XML_SCHEMA_TYPE_BASIC;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800274 ret->baseType = baseType;
Daniel Veillard4255d502002-04-16 15:50:10 +0000275 ret->contentType = XML_SCHEMA_CONTENT_BASIC;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000276 /*
277 * Primitive types.
278 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800279 switch (type) {
280 case XML_SCHEMAS_STRING:
281 case XML_SCHEMAS_DECIMAL:
282 case XML_SCHEMAS_DATE:
283 case XML_SCHEMAS_DATETIME:
284 case XML_SCHEMAS_TIME:
285 case XML_SCHEMAS_GYEAR:
286 case XML_SCHEMAS_GYEARMONTH:
287 case XML_SCHEMAS_GMONTH:
288 case XML_SCHEMAS_GMONTHDAY:
289 case XML_SCHEMAS_GDAY:
290 case XML_SCHEMAS_DURATION:
291 case XML_SCHEMAS_FLOAT:
292 case XML_SCHEMAS_DOUBLE:
293 case XML_SCHEMAS_BOOLEAN:
294 case XML_SCHEMAS_ANYURI:
295 case XML_SCHEMAS_HEXBINARY:
296 case XML_SCHEMAS_BASE64BINARY:
297 case XML_SCHEMAS_QNAME:
298 case XML_SCHEMAS_NOTATION:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000299 ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000300 break;
William M. Brack96d2eff2004-06-30 11:48:47 +0000301 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000302 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000303 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000304 /*
305 * Set variety.
306 */
307 switch (type) {
308 case XML_SCHEMAS_ANYTYPE:
309 case XML_SCHEMAS_ANYSIMPLETYPE:
310 break;
311 case XML_SCHEMAS_IDREFS:
312 case XML_SCHEMAS_NMTOKENS:
313 case XML_SCHEMAS_ENTITIES:
314 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
315 ret->facets = xmlSchemaNewMinLengthFacet(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800316 ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000317 break;
318 default:
319 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
320 break;
321 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000322 xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
323 XML_SCHEMAS_NAMESPACE_NAME, ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000324 ret->builtInType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +0000325 return(ret);
326}
327
328/*
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000329* WARNING: Those type reside normally in xmlschemas.c but are
330* redefined here locally in oder of being able to use them for xs:anyType-
331* TODO: Remove those definition if we move the types to a header file.
332* TODO: Always keep those structs up-to-date with the originals.
333*/
334#define UNBOUNDED (1 << 30)
335
336typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
337typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
338struct _xmlSchemaTreeItem {
339 xmlSchemaTypeType type;
340 xmlSchemaAnnotPtr annot;
341 xmlSchemaTreeItemPtr next;
342 xmlSchemaTreeItemPtr children;
343};
344
345typedef struct _xmlSchemaParticle xmlSchemaParticle;
346typedef xmlSchemaParticle *xmlSchemaParticlePtr;
347struct _xmlSchemaParticle {
348 xmlSchemaTypeType type;
349 xmlSchemaAnnotPtr annot;
350 xmlSchemaTreeItemPtr next;
351 xmlSchemaTreeItemPtr children;
352 int minOccurs;
353 int maxOccurs;
354 xmlNodePtr node;
355};
356
357typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
358typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
359struct _xmlSchemaModelGroup {
360 xmlSchemaTypeType type;
361 xmlSchemaAnnotPtr annot;
362 xmlSchemaTreeItemPtr next;
363 xmlSchemaTreeItemPtr children;
364 xmlNodePtr node;
365};
366
367static xmlSchemaParticlePtr
368xmlSchemaAddParticle(void)
369{
370 xmlSchemaParticlePtr ret = NULL;
371
372 ret = (xmlSchemaParticlePtr)
373 xmlMalloc(sizeof(xmlSchemaParticle));
374 if (ret == NULL) {
375 xmlSchemaTypeErrMemory(NULL, "allocating particle component");
376 return (NULL);
377 }
378 memset(ret, 0, sizeof(xmlSchemaParticle));
379 ret->type = XML_SCHEMA_TYPE_PARTICLE;
380 ret->minOccurs = 1;
381 ret->maxOccurs = 1;
382 return (ret);
383}
384
385/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000386 * xmlSchemaInitTypes:
387 *
388 * Initialize the default XML Schemas type library
389 */
390void
Daniel Veillard6560a422003-03-27 21:25:38 +0000391xmlSchemaInitTypes(void)
392{
Daniel Veillard4255d502002-04-16 15:50:10 +0000393 if (xmlSchemaTypesInitialized != 0)
Daniel Veillard6560a422003-03-27 21:25:38 +0000394 return;
Daniel Veillard4255d502002-04-16 15:50:10 +0000395 xmlSchemaTypesBank = xmlHashCreate(40);
Daniel Veillard6560a422003-03-27 21:25:38 +0000396
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800397
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000398 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +0000399 * 3.4.7 Built-in Complex Type Definition
400 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000401 xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800402 XML_SCHEMAS_ANYTYPE,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000403 NULL);
404 xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef;
405 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000406 /*
407 * Init the content type.
408 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800409 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000410 {
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000411 xmlSchemaParticlePtr particle;
412 xmlSchemaModelGroupPtr sequence;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000413 xmlSchemaWildcardPtr wild;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000414 /* First particle. */
415 particle = xmlSchemaAddParticle();
416 if (particle == NULL)
417 return;
418 xmlSchemaTypeAnyTypeDef->subtypes = (xmlSchemaTypePtr) particle;
419 /* Sequence model group. */
420 sequence = (xmlSchemaModelGroupPtr)
421 xmlMalloc(sizeof(xmlSchemaModelGroup));
422 if (sequence == NULL) {
423 xmlSchemaTypeErrMemory(NULL, "allocating model group component");
424 return;
425 }
426 memset(sequence, 0, sizeof(xmlSchemaModelGroup));
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800427 sequence->type = XML_SCHEMA_TYPE_SEQUENCE;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000428 particle->children = (xmlSchemaTreeItemPtr) sequence;
429 /* Second particle. */
430 particle = xmlSchemaAddParticle();
431 if (particle == NULL)
432 return;
433 particle->minOccurs = 0;
434 particle->maxOccurs = UNBOUNDED;
435 sequence->children = (xmlSchemaTreeItemPtr) particle;
436 /* The wildcard */
Daniel Veillard01fa6152004-06-29 17:04:39 +0000437 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
438 if (wild == NULL) {
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000439 xmlSchemaTypeErrMemory(NULL, "allocating wildcard component");
440 return;
441 }
442 memset(wild, 0, sizeof(xmlSchemaWildcard));
443 wild->type = XML_SCHEMA_TYPE_ANY;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800444 wild->any = 1;
445 wild->processContents = XML_SCHEMAS_ANY_LAX;
446 particle->children = (xmlSchemaTreeItemPtr) wild;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000447 /*
448 * Create the attribute wildcard.
449 */
450 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
451 if (wild == NULL) {
452 xmlSchemaTypeErrMemory(NULL, "could not create an attribute "
453 "wildcard on anyType");
Daniel Veillard01fa6152004-06-29 17:04:39 +0000454 return;
455 }
456 memset(wild, 0, sizeof(xmlSchemaWildcard));
457 wild->any = 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800458 wild->processContents = XML_SCHEMAS_ANY_LAX;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000459 xmlSchemaTypeAnyTypeDef->attributeWildcard = wild;
460 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800461 xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
William M. Brack2f2a6632004-08-20 23:09:47 +0000462 XML_SCHEMAS_ANYSIMPLETYPE,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000463 xmlSchemaTypeAnyTypeDef);
464 /*
465 * primitive datatypes
466 */
467 xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
468 XML_SCHEMAS_STRING,
469 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000470 xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000471 XML_SCHEMAS_DECIMAL,
472 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000473 xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000474 XML_SCHEMAS_DATE,
475 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000476 xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000477 XML_SCHEMAS_DATETIME,
478 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000479 xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000480 XML_SCHEMAS_TIME,
481 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000482 xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000483 XML_SCHEMAS_GYEAR,
484 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000485 xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000486 XML_SCHEMAS_GYEARMONTH,
487 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000488 xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000489 XML_SCHEMAS_GMONTH,
490 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000491 xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000492 XML_SCHEMAS_GMONTHDAY,
493 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000494 xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000495 XML_SCHEMAS_GDAY,
496 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000497 xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000498 XML_SCHEMAS_DURATION,
499 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000500 xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000501 XML_SCHEMAS_FLOAT,
502 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000503 xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000504 XML_SCHEMAS_DOUBLE,
505 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000506 xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000507 XML_SCHEMAS_BOOLEAN,
508 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000509 xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000510 XML_SCHEMAS_ANYURI,
511 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard560c2a42003-07-06 21:13:49 +0000512 xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000513 XML_SCHEMAS_HEXBINARY,
514 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000515 xmlSchemaTypeBase64BinaryDef
Daniel Veillard01fa6152004-06-29 17:04:39 +0000516 = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY,
517 xmlSchemaTypeAnySimpleTypeDef);
518 xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
519 XML_SCHEMAS_NOTATION,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800520 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000521 xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
522 XML_SCHEMAS_QNAME,
523 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard4255d502002-04-16 15:50:10 +0000524
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000525 /*
526 * derived datatypes
527 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000528 xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000529 XML_SCHEMAS_INTEGER,
530 xmlSchemaTypeDecimalDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000531 xmlSchemaTypeNonPositiveIntegerDef =
532 xmlSchemaInitBasicType("nonPositiveInteger",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000533 XML_SCHEMAS_NPINTEGER,
534 xmlSchemaTypeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000535 xmlSchemaTypeNegativeIntegerDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000536 xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER,
537 xmlSchemaTypeNonPositiveIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000538 xmlSchemaTypeLongDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000539 xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG,
540 xmlSchemaTypeIntegerDef);
541 xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT,
542 xmlSchemaTypeLongDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000543 xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000544 XML_SCHEMAS_SHORT,
545 xmlSchemaTypeIntDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000546 xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000547 XML_SCHEMAS_BYTE,
548 xmlSchemaTypeShortDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000549 xmlSchemaTypeNonNegativeIntegerDef =
550 xmlSchemaInitBasicType("nonNegativeInteger",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000551 XML_SCHEMAS_NNINTEGER,
552 xmlSchemaTypeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000553 xmlSchemaTypeUnsignedLongDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000554 xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG,
555 xmlSchemaTypeNonNegativeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000556 xmlSchemaTypeUnsignedIntDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000557 xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT,
558 xmlSchemaTypeUnsignedLongDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000559 xmlSchemaTypeUnsignedShortDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000560 xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT,
561 xmlSchemaTypeUnsignedIntDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000562 xmlSchemaTypeUnsignedByteDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000563 xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE,
564 xmlSchemaTypeUnsignedShortDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000565 xmlSchemaTypePositiveIntegerDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000566 xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER,
567 xmlSchemaTypeNonNegativeIntegerDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000568 xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000569 XML_SCHEMAS_NORMSTRING,
570 xmlSchemaTypeStringDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000571 xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000572 XML_SCHEMAS_TOKEN,
573 xmlSchemaTypeNormStringDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000574 xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000575 XML_SCHEMAS_LANGUAGE,
576 xmlSchemaTypeTokenDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000577 xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000578 XML_SCHEMAS_NAME,
579 xmlSchemaTypeTokenDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000580 xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000581 XML_SCHEMAS_NMTOKEN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000582 xmlSchemaTypeTokenDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000583 xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
584 XML_SCHEMAS_NCNAME,
585 xmlSchemaTypeNameDef);
586 xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000587 xmlSchemaTypeNCNameDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000588 xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
589 XML_SCHEMAS_IDREF,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800590 xmlSchemaTypeNCNameDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000591 xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
592 XML_SCHEMAS_ENTITY,
593 xmlSchemaTypeNCNameDef);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000594 /*
595 * Derived list types.
596 */
597 /* ENTITIES */
Daniel Veillard01fa6152004-06-29 17:04:39 +0000598 xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
599 XML_SCHEMAS_ENTITIES,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000600 xmlSchemaTypeAnySimpleTypeDef);
601 xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef;
602 /* IDREFS */
603 xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
604 XML_SCHEMAS_IDREFS,
605 xmlSchemaTypeAnySimpleTypeDef);
606 xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef;
607
608 /* NMTOKENS */
609 xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
610 XML_SCHEMAS_NMTOKENS,
611 xmlSchemaTypeAnySimpleTypeDef);
612 xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef;
613
Daniel Veillard4255d502002-04-16 15:50:10 +0000614 xmlSchemaTypesInitialized = 1;
615}
616
617/**
618 * xmlSchemaCleanupTypes:
619 *
620 * Cleanup the default XML Schemas type library
621 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800622void
Daniel Veillard4255d502002-04-16 15:50:10 +0000623xmlSchemaCleanupTypes(void) {
624 if (xmlSchemaTypesInitialized == 0)
625 return;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000626 /*
627 * Free xs:anyType.
628 */
629 {
630 xmlSchemaParticlePtr particle;
631 /* Attribute wildcard. */
632 xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard);
633 /* Content type. */
634 particle = (xmlSchemaParticlePtr) xmlSchemaTypeAnyTypeDef->subtypes;
635 /* Wildcard. */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800636 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr)
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000637 particle->children->children->children);
638 xmlFree((xmlSchemaParticlePtr) particle->children->children);
639 /* Sequence model group. */
640 xmlFree((xmlSchemaModelGroupPtr) particle->children);
641 xmlFree((xmlSchemaParticlePtr) particle);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800642 xmlSchemaTypeAnyTypeDef->subtypes = NULL;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000643 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000644 xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
645 xmlSchemaTypesInitialized = 0;
646}
647
648/**
Daniel Veillard6927b102004-10-27 17:29:04 +0000649 * xmlSchemaIsBuiltInTypeFacet:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000650 * @type: the built-in type
651 * @facetType: the facet type
652 *
653 * Evaluates if a specific facet can be
654 * used in conjunction with a type.
655 *
656 * Returns 1 if the facet can be used with the given built-in type,
657 * 0 otherwise and -1 in case the type is not a built-in type.
658 */
659int
660xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType)
661{
Daniel Veillardce682bc2004-11-05 17:22:25 +0000662 if (type == NULL)
663 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000664 if (type->type != XML_SCHEMA_TYPE_BASIC)
665 return (-1);
666 switch (type->builtInType) {
667 case XML_SCHEMAS_BOOLEAN:
668 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
669 (facetType == XML_SCHEMA_FACET_WHITESPACE))
670 return (1);
671 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800672 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000673 case XML_SCHEMAS_STRING:
674 case XML_SCHEMAS_NOTATION:
675 case XML_SCHEMAS_QNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800676 case XML_SCHEMAS_ANYURI:
677 case XML_SCHEMAS_BASE64BINARY:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000678 case XML_SCHEMAS_HEXBINARY:
679 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
680 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
681 (facetType == XML_SCHEMA_FACET_MAXLENGTH) ||
682 (facetType == XML_SCHEMA_FACET_PATTERN) ||
683 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
684 (facetType == XML_SCHEMA_FACET_WHITESPACE))
685 return (1);
686 else
687 return (0);
688 case XML_SCHEMAS_DECIMAL:
689 if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) ||
690 (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) ||
691 (facetType == XML_SCHEMA_FACET_PATTERN) ||
692 (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
693 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
694 (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
695 (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
696 (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
697 (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
698 return (1);
699 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800700 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000701 case XML_SCHEMAS_TIME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800702 case XML_SCHEMAS_GDAY:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000703 case XML_SCHEMAS_GMONTH:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800704 case XML_SCHEMAS_GMONTHDAY:
705 case XML_SCHEMAS_GYEAR:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000706 case XML_SCHEMAS_GYEARMONTH:
707 case XML_SCHEMAS_DATE:
708 case XML_SCHEMAS_DATETIME:
709 case XML_SCHEMAS_DURATION:
710 case XML_SCHEMAS_FLOAT:
711 case XML_SCHEMAS_DOUBLE:
712 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
713 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
714 (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
715 (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
716 (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
717 (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
718 (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
719 return (1);
720 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800721 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000722 default:
Daniel Veillardc7e3cc42004-09-28 12:33:52 +0000723 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000724 }
725 return (0);
726}
727
728/**
729 * xmlSchemaGetBuiltInType:
730 * @type: the type of the built in type
731 *
732 * Gives you the type struct for a built-in
733 * type by its type id.
734 *
735 * Returns the type if found, NULL otherwise.
736 */
737xmlSchemaTypePtr
738xmlSchemaGetBuiltInType(xmlSchemaValType type)
739{
740 if (xmlSchemaTypesInitialized == 0)
741 xmlSchemaInitTypes();
742 switch (type) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800743
Daniel Veillard01fa6152004-06-29 17:04:39 +0000744 case XML_SCHEMAS_ANYSIMPLETYPE:
745 return (xmlSchemaTypeAnySimpleTypeDef);
746 case XML_SCHEMAS_STRING:
747 return (xmlSchemaTypeStringDef);
748 case XML_SCHEMAS_NORMSTRING:
749 return (xmlSchemaTypeNormStringDef);
750 case XML_SCHEMAS_DECIMAL:
751 return (xmlSchemaTypeDecimalDef);
752 case XML_SCHEMAS_TIME:
753 return (xmlSchemaTypeTimeDef);
754 case XML_SCHEMAS_GDAY:
755 return (xmlSchemaTypeGDayDef);
756 case XML_SCHEMAS_GMONTH:
757 return (xmlSchemaTypeGMonthDef);
758 case XML_SCHEMAS_GMONTHDAY:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800759 return (xmlSchemaTypeGMonthDayDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000760 case XML_SCHEMAS_GYEAR:
761 return (xmlSchemaTypeGYearDef);
762 case XML_SCHEMAS_GYEARMONTH:
763 return (xmlSchemaTypeGYearMonthDef);
764 case XML_SCHEMAS_DATE:
765 return (xmlSchemaTypeDateDef);
766 case XML_SCHEMAS_DATETIME:
767 return (xmlSchemaTypeDatetimeDef);
768 case XML_SCHEMAS_DURATION:
769 return (xmlSchemaTypeDurationDef);
770 case XML_SCHEMAS_FLOAT:
771 return (xmlSchemaTypeFloatDef);
772 case XML_SCHEMAS_DOUBLE:
773 return (xmlSchemaTypeDoubleDef);
774 case XML_SCHEMAS_BOOLEAN:
775 return (xmlSchemaTypeBooleanDef);
776 case XML_SCHEMAS_TOKEN:
777 return (xmlSchemaTypeTokenDef);
778 case XML_SCHEMAS_LANGUAGE:
779 return (xmlSchemaTypeLanguageDef);
780 case XML_SCHEMAS_NMTOKEN:
781 return (xmlSchemaTypeNmtokenDef);
782 case XML_SCHEMAS_NMTOKENS:
783 return (xmlSchemaTypeNmtokensDef);
784 case XML_SCHEMAS_NAME:
785 return (xmlSchemaTypeNameDef);
786 case XML_SCHEMAS_QNAME:
787 return (xmlSchemaTypeQNameDef);
788 case XML_SCHEMAS_NCNAME:
789 return (xmlSchemaTypeNCNameDef);
790 case XML_SCHEMAS_ID:
791 return (xmlSchemaTypeIdDef);
792 case XML_SCHEMAS_IDREF:
793 return (xmlSchemaTypeIdrefDef);
794 case XML_SCHEMAS_IDREFS:
795 return (xmlSchemaTypeIdrefsDef);
796 case XML_SCHEMAS_ENTITY:
797 return (xmlSchemaTypeEntityDef);
798 case XML_SCHEMAS_ENTITIES:
799 return (xmlSchemaTypeEntitiesDef);
800 case XML_SCHEMAS_NOTATION:
801 return (xmlSchemaTypeNotationDef);
802 case XML_SCHEMAS_ANYURI:
803 return (xmlSchemaTypeAnyURIDef);
804 case XML_SCHEMAS_INTEGER:
805 return (xmlSchemaTypeIntegerDef);
806 case XML_SCHEMAS_NPINTEGER:
807 return (xmlSchemaTypeNonPositiveIntegerDef);
808 case XML_SCHEMAS_NINTEGER:
809 return (xmlSchemaTypeNegativeIntegerDef);
810 case XML_SCHEMAS_NNINTEGER:
811 return (xmlSchemaTypeNonNegativeIntegerDef);
812 case XML_SCHEMAS_PINTEGER:
813 return (xmlSchemaTypePositiveIntegerDef);
814 case XML_SCHEMAS_INT:
815 return (xmlSchemaTypeIntDef);
816 case XML_SCHEMAS_UINT:
817 return (xmlSchemaTypeUnsignedIntDef);
818 case XML_SCHEMAS_LONG:
819 return (xmlSchemaTypeLongDef);
820 case XML_SCHEMAS_ULONG:
821 return (xmlSchemaTypeUnsignedLongDef);
822 case XML_SCHEMAS_SHORT:
823 return (xmlSchemaTypeShortDef);
824 case XML_SCHEMAS_USHORT:
825 return (xmlSchemaTypeUnsignedShortDef);
826 case XML_SCHEMAS_BYTE:
827 return (xmlSchemaTypeByteDef);
828 case XML_SCHEMAS_UBYTE:
829 return (xmlSchemaTypeUnsignedByteDef);
830 case XML_SCHEMAS_HEXBINARY:
831 return (xmlSchemaTypeHexBinaryDef);
832 case XML_SCHEMAS_BASE64BINARY:
833 return (xmlSchemaTypeBase64BinaryDef);
834 case XML_SCHEMAS_ANYTYPE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800835 return (xmlSchemaTypeAnyTypeDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000836 default:
837 return (NULL);
838 }
839}
840
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000841/**
842 * xmlSchemaValueAppend:
843 * @prev: the value
844 * @cur: the value to be appended
845 *
846 * Appends a next sibling to a list of computed values.
847 *
848 * Returns 0 if succeeded and -1 on API errors.
849 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000850int
851xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000852
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000853 if ((prev == NULL) || (cur == NULL))
854 return (-1);
855 prev->next = cur;
856 return (0);
857}
858
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000859/**
860 * xmlSchemaValueGetNext:
861 * @cur: the value
862 *
863 * Accessor for the next sibling of a list of computed values.
864 *
865 * Returns the next value or NULL if there was none, or on
866 * API errors.
867 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000868xmlSchemaValPtr
869xmlSchemaValueGetNext(xmlSchemaValPtr cur) {
870
871 if (cur == NULL)
872 return (NULL);
873 return (cur->next);
874}
875
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000876/**
877 * xmlSchemaValueGetAsString:
878 * @val: the value
879 *
880 * Accessor for the string value of a computed value.
881 *
882 * Returns the string value or NULL if there was none, or on
883 * API errors.
884 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000885const xmlChar *
886xmlSchemaValueGetAsString(xmlSchemaValPtr val)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800887{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000888 if (val == NULL)
889 return (NULL);
890 switch (val->type) {
891 case XML_SCHEMAS_STRING:
892 case XML_SCHEMAS_NORMSTRING:
893 case XML_SCHEMAS_ANYSIMPLETYPE:
894 case XML_SCHEMAS_TOKEN:
895 case XML_SCHEMAS_LANGUAGE:
896 case XML_SCHEMAS_NMTOKEN:
897 case XML_SCHEMAS_NAME:
898 case XML_SCHEMAS_NCNAME:
899 case XML_SCHEMAS_ID:
900 case XML_SCHEMAS_IDREF:
901 case XML_SCHEMAS_ENTITY:
902 case XML_SCHEMAS_ANYURI:
903 return (BAD_CAST val->value.str);
904 default:
905 break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000906 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000907 return (NULL);
908}
909
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000910/**
911 * xmlSchemaValueGetAsBoolean:
912 * @val: the value
913 *
914 * Accessor for the boolean value of a computed value.
915 *
916 * Returns 1 if true and 0 if false, or in case of an error. Hmm.
917 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000918int
919xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800920{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000921 if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN))
922 return (0);
923 return (val->value.b);
Daniel Veillard4255d502002-04-16 15:50:10 +0000924}
925
926/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000927 * xmlSchemaNewStringValue:
928 * @type: the value type
Daniel Veillardb5839c32005-02-19 18:27:14 +0000929 * @value: the value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000930 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800931 * Allocate a new simple type value. The type can be
932 * of XML_SCHEMAS_STRING.
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +0000933 * WARNING: This one is intended to be expanded for other
934 * string based types. We need this for anySimpleType as well.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000935 * The given value is consumed and freed with the struct.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000936 *
937 * Returns a pointer to the new value or NULL in case of error
938 */
939xmlSchemaValPtr
940xmlSchemaNewStringValue(xmlSchemaValType type,
941 const xmlChar *value)
942{
943 xmlSchemaValPtr val;
944
945 if (type != XML_SCHEMAS_STRING)
946 return(NULL);
947 val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
948 if (val == NULL) {
949 return(NULL);
950 }
951 memset(val, 0, sizeof(xmlSchemaVal));
952 val->type = type;
953 val->value.str = (xmlChar *) value;
954 return(val);
955}
956
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000957/**
958 * xmlSchemaNewNOTATIONValue:
Daniel Veillardb5839c32005-02-19 18:27:14 +0000959 * @name: the notation name
960 * @ns: the notation namespace name or NULL
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000961 *
962 * Allocate a new NOTATION value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000963 * The given values are consumed and freed with the struct.
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000964 *
965 * Returns a pointer to the new value or NULL in case of error
966 */
967xmlSchemaValPtr
968xmlSchemaNewNOTATIONValue(const xmlChar *name,
969 const xmlChar *ns)
970{
971 xmlSchemaValPtr val;
972
973 val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
974 if (val == NULL)
975 return (NULL);
976
William M. Brack12d37ab2005-02-21 13:54:07 +0000977 val->value.qname.name = (xmlChar *)name;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000978 if (ns != NULL)
William M. Brack12d37ab2005-02-21 13:54:07 +0000979 val->value.qname.uri = (xmlChar *)ns;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000980 return(val);
981}
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000982
983/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000984 * xmlSchemaNewQNameValue:
985 * @namespaceName: the namespace name
986 * @localName: the local name
987 *
988 * Allocate a new QName value.
989 * The given values are consumed and freed with the struct.
990 *
991 * Returns a pointer to the new value or NULL in case of an error.
992 */
993xmlSchemaValPtr
994xmlSchemaNewQNameValue(const xmlChar *namespaceName,
995 const xmlChar *localName)
996{
997 xmlSchemaValPtr val;
998
999 val = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
1000 if (val == NULL)
1001 return (NULL);
1002
1003 val->value.qname.name = (xmlChar *) localName;
1004 val->value.qname.uri = (xmlChar *) namespaceName;
1005 return(val);
1006}
1007
1008/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001009 * xmlSchemaFreeValue:
1010 * @value: the value to free
1011 *
1012 * Cleanup the default XML Schemas type library
1013 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001014void
Daniel Veillard4255d502002-04-16 15:50:10 +00001015xmlSchemaFreeValue(xmlSchemaValPtr value) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001016 xmlSchemaValPtr prev;
1017
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001018 while (value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001019 switch (value->type) {
1020 case XML_SCHEMAS_STRING:
1021 case XML_SCHEMAS_NORMSTRING:
1022 case XML_SCHEMAS_TOKEN:
1023 case XML_SCHEMAS_LANGUAGE:
1024 case XML_SCHEMAS_NMTOKEN:
1025 case XML_SCHEMAS_NMTOKENS:
1026 case XML_SCHEMAS_NAME:
1027 case XML_SCHEMAS_NCNAME:
1028 case XML_SCHEMAS_ID:
1029 case XML_SCHEMAS_IDREF:
1030 case XML_SCHEMAS_IDREFS:
1031 case XML_SCHEMAS_ENTITY:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001032 case XML_SCHEMAS_ENTITIES:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001033 case XML_SCHEMAS_ANYURI:
1034 case XML_SCHEMAS_ANYSIMPLETYPE:
1035 if (value->value.str != NULL)
1036 xmlFree(value->value.str);
1037 break;
1038 case XML_SCHEMAS_NOTATION:
1039 case XML_SCHEMAS_QNAME:
1040 if (value->value.qname.uri != NULL)
1041 xmlFree(value->value.qname.uri);
1042 if (value->value.qname.name != NULL)
1043 xmlFree(value->value.qname.name);
1044 break;
1045 case XML_SCHEMAS_HEXBINARY:
1046 if (value->value.hex.str != NULL)
1047 xmlFree(value->value.hex.str);
1048 break;
1049 case XML_SCHEMAS_BASE64BINARY:
1050 if (value->value.base64.str != NULL)
1051 xmlFree(value->value.base64.str);
1052 break;
1053 default:
1054 break;
1055 }
1056 prev = value;
1057 value = value->next;
1058 xmlFree(prev);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001059 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001060}
1061
1062/**
1063 * xmlSchemaGetPredefinedType:
1064 * @name: the type name
1065 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
1066 *
1067 * Lookup a type in the default XML Schemas type library
1068 *
1069 * Returns the type if found, NULL otherwise
1070 */
1071xmlSchemaTypePtr
1072xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
1073 if (xmlSchemaTypesInitialized == 0)
1074 xmlSchemaInitTypes();
1075 if (name == NULL)
1076 return(NULL);
1077 return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
1078}
Daniel Veillard070803b2002-05-03 07:29:38 +00001079
Daniel Veillard01fa6152004-06-29 17:04:39 +00001080/**
1081 * xmlSchemaGetBuiltInListSimpleTypeItemType:
1082 * @type: the built-in simple type.
1083 *
Daniel Veillard6927b102004-10-27 17:29:04 +00001084 * Lookup function
1085 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00001086 * Returns the item type of @type as defined by the built-in datatype
1087 * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error.
Daniel Veillard01fa6152004-06-29 17:04:39 +00001088 */
1089xmlSchemaTypePtr
1090xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type)
1091{
Daniel Veillard42595322004-11-08 10:52:06 +00001092 if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC))
Daniel Veillard01fa6152004-06-29 17:04:39 +00001093 return (NULL);
1094 switch (type->builtInType) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001095 case XML_SCHEMAS_NMTOKENS:
Daniel Veillard01fa6152004-06-29 17:04:39 +00001096 return (xmlSchemaTypeNmtokenDef );
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001097 case XML_SCHEMAS_IDREFS:
Daniel Veillard01fa6152004-06-29 17:04:39 +00001098 return (xmlSchemaTypeIdrefDef);
1099 case XML_SCHEMAS_ENTITIES:
1100 return (xmlSchemaTypeEntityDef);
1101 default:
1102 return (NULL);
1103 }
1104}
1105
Daniel Veillard070803b2002-05-03 07:29:38 +00001106/****************************************************************
1107 * *
1108 * Convenience macros and functions *
1109 * *
1110 ****************************************************************/
1111
1112#define IS_TZO_CHAR(c) \
1113 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
1114
1115#define VALID_YEAR(yr) (yr != 0)
1116#define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
1117/* VALID_DAY should only be used when month is unknown */
1118#define VALID_DAY(day) ((day >= 1) && (day <= 31))
1119#define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
1120#define VALID_MIN(min) ((min >= 0) && (min <= 59))
1121#define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
Kasimier T. Buchcik690a6802005-05-12 13:16:01 +00001122#define VALID_TZO(tzo) ((tzo > -840) && (tzo < 840))
Daniel Veillard070803b2002-05-03 07:29:38 +00001123#define IS_LEAP(y) \
1124 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
1125
Daniel Veillardebe25d42004-03-25 09:35:49 +00001126static const unsigned int daysInMonth[12] =
Daniel Veillard070803b2002-05-03 07:29:38 +00001127 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Daniel Veillardebe25d42004-03-25 09:35:49 +00001128static const unsigned int daysInMonthLeap[12] =
Daniel Veillard070803b2002-05-03 07:29:38 +00001129 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1130
Daniel Veillard5a872412002-05-22 06:40:27 +00001131#define MAX_DAYINMONTH(yr,mon) \
1132 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
1133
Daniel Veillard070803b2002-05-03 07:29:38 +00001134#define VALID_MDAY(dt) \
1135 (IS_LEAP(dt->year) ? \
1136 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
1137 (dt->day <= daysInMonth[dt->mon - 1]))
1138
1139#define VALID_DATE(dt) \
1140 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
1141
1142#define VALID_TIME(dt) \
1143 (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
1144 VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
1145
1146#define VALID_DATETIME(dt) \
1147 (VALID_DATE(dt) && VALID_TIME(dt))
1148
1149#define SECS_PER_MIN (60)
1150#define SECS_PER_HOUR (60 * SECS_PER_MIN)
1151#define SECS_PER_DAY (24 * SECS_PER_HOUR)
1152
Daniel Veillard5a872412002-05-22 06:40:27 +00001153static const long dayInYearByMonth[12] =
1154 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1155static const long dayInLeapYearByMonth[12] =
1156 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
1157
1158#define DAY_IN_YEAR(day, month, year) \
1159 ((IS_LEAP(year) ? \
1160 dayInLeapYearByMonth[month - 1] : \
1161 dayInYearByMonth[month - 1]) + day)
1162
1163#ifdef DEBUG
1164#define DEBUG_DATE(dt) \
1165 xmlGenericError(xmlGenericErrorContext, \
1166 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
1167 dt->type,dt->value.date.year,dt->value.date.mon, \
1168 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
1169 dt->value.date.sec); \
1170 if (dt->value.date.tz_flag) \
1171 if (dt->value.date.tzo != 0) \
1172 xmlGenericError(xmlGenericErrorContext, \
1173 "%+05d\n",dt->value.date.tzo); \
1174 else \
1175 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
1176 else \
1177 xmlGenericError(xmlGenericErrorContext,"\n")
1178#else
1179#define DEBUG_DATE(dt)
1180#endif
1181
Daniel Veillard070803b2002-05-03 07:29:38 +00001182/**
1183 * _xmlSchemaParseGYear:
1184 * @dt: pointer to a date structure
1185 * @str: pointer to the string to analyze
1186 *
1187 * Parses a xs:gYear without time zone and fills in the appropriate
1188 * field of the @dt structure. @str is updated to point just after the
1189 * xs:gYear. It is supposed that @dt->year is big enough to contain
1190 * the year.
1191 *
1192 * Returns 0 or the error code
1193 */
1194static int
1195_xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
1196 const xmlChar *cur = *str, *firstChar;
1197 int isneg = 0, digcnt = 0;
1198
1199 if (((*cur < '0') || (*cur > '9')) &&
1200 (*cur != '-') && (*cur != '+'))
1201 return -1;
1202
1203 if (*cur == '-') {
1204 isneg = 1;
1205 cur++;
1206 }
1207
1208 firstChar = cur;
1209
1210 while ((*cur >= '0') && (*cur <= '9')) {
1211 dt->year = dt->year * 10 + (*cur - '0');
1212 cur++;
1213 digcnt++;
1214 }
1215
1216 /* year must be at least 4 digits (CCYY); over 4
1217 * digits cannot have a leading zero. */
1218 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
1219 return 1;
1220
1221 if (isneg)
1222 dt->year = - dt->year;
1223
1224 if (!VALID_YEAR(dt->year))
1225 return 2;
1226
1227 *str = cur;
1228 return 0;
1229}
1230
1231/**
1232 * PARSE_2_DIGITS:
1233 * @num: the integer to fill in
1234 * @cur: an #xmlChar *
1235 * @invalid: an integer
1236 *
1237 * Parses a 2-digits integer and updates @num with the value. @cur is
1238 * updated to point just after the integer.
1239 * In case of error, @invalid is set to %TRUE, values of @num and
1240 * @cur are undefined.
1241 */
1242#define PARSE_2_DIGITS(num, cur, invalid) \
1243 if ((cur[0] < '0') || (cur[0] > '9') || \
1244 (cur[1] < '0') || (cur[1] > '9')) \
1245 invalid = 1; \
1246 else \
1247 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
1248 cur += 2;
1249
1250/**
1251 * PARSE_FLOAT:
1252 * @num: the double to fill in
1253 * @cur: an #xmlChar *
1254 * @invalid: an integer
1255 *
1256 * Parses a float and updates @num with the value. @cur is
1257 * updated to point just after the float. The float must have a
1258 * 2-digits integer part and may or may not have a decimal part.
1259 * In case of error, @invalid is set to %TRUE, values of @num and
1260 * @cur are undefined.
1261 */
1262#define PARSE_FLOAT(num, cur, invalid) \
1263 PARSE_2_DIGITS(num, cur, invalid); \
1264 if (!invalid && (*cur == '.')) { \
1265 double mult = 1; \
1266 cur++; \
1267 if ((*cur < '0') || (*cur > '9')) \
1268 invalid = 1; \
1269 while ((*cur >= '0') && (*cur <= '9')) { \
1270 mult /= 10; \
1271 num += (*cur - '0') * mult; \
1272 cur++; \
1273 } \
1274 }
1275
1276/**
1277 * _xmlSchemaParseGMonth:
1278 * @dt: pointer to a date structure
1279 * @str: pointer to the string to analyze
1280 *
1281 * Parses a xs:gMonth without time zone and fills in the appropriate
1282 * field of the @dt structure. @str is updated to point just after the
1283 * xs:gMonth.
1284 *
1285 * Returns 0 or the error code
1286 */
1287static int
1288_xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
1289 const xmlChar *cur = *str;
1290 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001291 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001292
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001293 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001294 if (ret != 0)
1295 return ret;
1296
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001297 if (!VALID_MONTH(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001298 return 2;
1299
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001300 dt->mon = value;
1301
Daniel Veillard070803b2002-05-03 07:29:38 +00001302 *str = cur;
1303 return 0;
1304}
1305
1306/**
1307 * _xmlSchemaParseGDay:
1308 * @dt: pointer to a date structure
1309 * @str: pointer to the string to analyze
1310 *
1311 * Parses a xs:gDay without time zone and fills in the appropriate
1312 * field of the @dt structure. @str is updated to point just after the
1313 * xs:gDay.
1314 *
1315 * Returns 0 or the error code
1316 */
1317static int
1318_xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
1319 const xmlChar *cur = *str;
1320 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001321 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001322
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001323 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001324 if (ret != 0)
1325 return ret;
1326
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001327 if (!VALID_DAY(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001328 return 2;
1329
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001330 dt->day = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001331 *str = cur;
1332 return 0;
1333}
1334
1335/**
1336 * _xmlSchemaParseTime:
1337 * @dt: pointer to a date structure
1338 * @str: pointer to the string to analyze
1339 *
1340 * Parses a xs:time without time zone and fills in the appropriate
1341 * fields of the @dt structure. @str is updated to point just after the
1342 * xs:time.
1343 * In case of error, values of @dt fields are undefined.
1344 *
1345 * Returns 0 or the error code
1346 */
1347static int
1348_xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001349 const xmlChar *cur = *str;
Daniel Veillard070803b2002-05-03 07:29:38 +00001350 int ret = 0;
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +00001351 int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001352
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001353 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001354 if (ret != 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001355 return ret;
Daniel Veillard070803b2002-05-03 07:29:38 +00001356 if (*cur != ':')
1357 return 1;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001358 if (!VALID_HOUR(value))
1359 return 2;
Daniel Veillard070803b2002-05-03 07:29:38 +00001360 cur++;
1361
1362 /* the ':' insures this string is xs:time */
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001363 dt->hour = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001364
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001365 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001366 if (ret != 0)
1367 return ret;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001368 if (!VALID_MIN(value))
1369 return 2;
1370 dt->min = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001371
1372 if (*cur != ':')
1373 return 1;
1374 cur++;
1375
1376 PARSE_FLOAT(dt->sec, cur, ret);
1377 if (ret != 0)
1378 return ret;
1379
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001380 if ((!VALID_SEC(dt->sec)) || (!VALID_TZO(dt->tzo)))
Daniel Veillard070803b2002-05-03 07:29:38 +00001381 return 2;
1382
1383 *str = cur;
1384 return 0;
1385}
1386
1387/**
1388 * _xmlSchemaParseTimeZone:
1389 * @dt: pointer to a date structure
1390 * @str: pointer to the string to analyze
1391 *
1392 * Parses a time zone without time zone and fills in the appropriate
1393 * field of the @dt structure. @str is updated to point just after the
1394 * time zone.
1395 *
1396 * Returns 0 or the error code
1397 */
1398static int
1399_xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
Daniel Veillard14b56432006-03-09 18:41:40 +00001400 const xmlChar *cur;
Daniel Veillard070803b2002-05-03 07:29:38 +00001401 int ret = 0;
1402
1403 if (str == NULL)
1404 return -1;
Daniel Veillard14b56432006-03-09 18:41:40 +00001405 cur = *str;
Daniel Veillard070803b2002-05-03 07:29:38 +00001406
1407 switch (*cur) {
1408 case 0:
1409 dt->tz_flag = 0;
1410 dt->tzo = 0;
1411 break;
1412
1413 case 'Z':
1414 dt->tz_flag = 1;
1415 dt->tzo = 0;
1416 cur++;
1417 break;
1418
1419 case '+':
1420 case '-': {
1421 int isneg = 0, tmp = 0;
1422 isneg = (*cur == '-');
1423
1424 cur++;
1425
1426 PARSE_2_DIGITS(tmp, cur, ret);
1427 if (ret != 0)
1428 return ret;
1429 if (!VALID_HOUR(tmp))
1430 return 2;
1431
1432 if (*cur != ':')
1433 return 1;
1434 cur++;
1435
1436 dt->tzo = tmp * 60;
1437
1438 PARSE_2_DIGITS(tmp, cur, ret);
1439 if (ret != 0)
1440 return ret;
1441 if (!VALID_MIN(tmp))
1442 return 2;
1443
1444 dt->tzo += tmp;
1445 if (isneg)
1446 dt->tzo = - dt->tzo;
1447
1448 if (!VALID_TZO(dt->tzo))
1449 return 2;
1450
Daniel Veillard5a872412002-05-22 06:40:27 +00001451 dt->tz_flag = 1;
Daniel Veillard070803b2002-05-03 07:29:38 +00001452 break;
1453 }
1454 default:
1455 return 1;
1456 }
1457
1458 *str = cur;
1459 return 0;
1460}
1461
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001462/**
1463 * _xmlSchemaBase64Decode:
1464 * @ch: a character
1465 *
1466 * Converts a base64 encoded character to its base 64 value.
1467 *
1468 * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
1469 */
1470static int
1471_xmlSchemaBase64Decode (const xmlChar ch) {
1472 if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
1473 if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
1474 if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
1475 if ('+' == ch) return 62;
1476 if ('/' == ch) return 63;
1477 if ('=' == ch) return 64;
1478 return -1;
1479}
1480
Daniel Veillard070803b2002-05-03 07:29:38 +00001481/****************************************************************
1482 * *
1483 * XML Schema Dates/Times Datatypes Handling *
1484 * *
1485 ****************************************************************/
1486
1487/**
1488 * PARSE_DIGITS:
1489 * @num: the integer to fill in
1490 * @cur: an #xmlChar *
1491 * @num_type: an integer flag
1492 *
1493 * Parses a digits integer and updates @num with the value. @cur is
1494 * updated to point just after the integer.
1495 * In case of error, @num_type is set to -1, values of @num and
1496 * @cur are undefined.
1497 */
1498#define PARSE_DIGITS(num, cur, num_type) \
1499 if ((*cur < '0') || (*cur > '9')) \
1500 num_type = -1; \
1501 else \
1502 while ((*cur >= '0') && (*cur <= '9')) { \
1503 num = num * 10 + (*cur - '0'); \
1504 cur++; \
1505 }
1506
1507/**
1508 * PARSE_NUM:
1509 * @num: the double to fill in
1510 * @cur: an #xmlChar *
1511 * @num_type: an integer flag
1512 *
1513 * Parses a float or integer and updates @num with the value. @cur is
1514 * updated to point just after the number. If the number is a float,
1515 * then it must have an integer part and a decimal part; @num_type will
1516 * be set to 1. If there is no decimal part, @num_type is set to zero.
1517 * In case of error, @num_type is set to -1, values of @num and
1518 * @cur are undefined.
1519 */
1520#define PARSE_NUM(num, cur, num_type) \
1521 num = 0; \
1522 PARSE_DIGITS(num, cur, num_type); \
1523 if (!num_type && (*cur == '.')) { \
1524 double mult = 1; \
1525 cur++; \
1526 if ((*cur < '0') || (*cur > '9')) \
1527 num_type = -1; \
1528 else \
1529 num_type = 1; \
1530 while ((*cur >= '0') && (*cur <= '9')) { \
1531 mult /= 10; \
1532 num += (*cur - '0') * mult; \
1533 cur++; \
1534 } \
1535 }
1536
1537/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001538 * xmlSchemaValidateDates:
Daniel Veillard455cc072003-03-31 10:13:23 +00001539 * @type: the expected type or XML_SCHEMAS_UNKNOWN
Daniel Veillard070803b2002-05-03 07:29:38 +00001540 * @dateTime: string to analyze
1541 * @val: the return computed value
1542 *
1543 * Check that @dateTime conforms to the lexical space of one of the date types.
1544 * if true a value is computed and returned in @val.
1545 *
1546 * Returns 0 if this validates, a positive error code number otherwise
1547 * and -1 in case of internal or API error.
1548 */
1549static int
Daniel Veillard455cc072003-03-31 10:13:23 +00001550xmlSchemaValidateDates (xmlSchemaValType type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001551 const xmlChar *dateTime, xmlSchemaValPtr *val,
1552 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001553 xmlSchemaValPtr dt;
1554 int ret;
1555 const xmlChar *cur = dateTime;
1556
1557#define RETURN_TYPE_IF_VALID(t) \
1558 if (IS_TZO_CHAR(*cur)) { \
1559 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
1560 if (ret == 0) { \
1561 if (*cur != 0) \
1562 goto error; \
1563 dt->type = t; \
Daniel Veillard455cc072003-03-31 10:13:23 +00001564 goto done; \
Daniel Veillard070803b2002-05-03 07:29:38 +00001565 } \
1566 }
1567
1568 if (dateTime == NULL)
1569 return -1;
1570
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001571 if (collapse)
1572 while IS_WSP_BLANK_CH(*cur) cur++;
1573
Daniel Veillard070803b2002-05-03 07:29:38 +00001574 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
1575 return 1;
1576
1577 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
1578 if (dt == NULL)
1579 return -1;
1580
1581 if ((cur[0] == '-') && (cur[1] == '-')) {
1582 /*
1583 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
1584 * xs:gDay)
1585 */
1586 cur += 2;
1587
1588 /* is it an xs:gDay? */
1589 if (*cur == '-') {
Daniel Veillard455cc072003-03-31 10:13:23 +00001590 if (type == XML_SCHEMAS_GMONTH)
1591 goto error;
Daniel Veillard070803b2002-05-03 07:29:38 +00001592 ++cur;
1593 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1594 if (ret != 0)
1595 goto error;
1596
1597 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
1598
1599 goto error;
1600 }
1601
1602 /*
1603 * it should be an xs:gMonthDay or xs:gMonth
1604 */
1605 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1606 if (ret != 0)
1607 goto error;
1608
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001609 /*
1610 * a '-' char could indicate this type is xs:gMonthDay or
1611 * a negative time zone offset. Check for xs:gMonthDay first.
1612 * Also the first three char's of a negative tzo (-MM:SS) can
1613 * appear to be a valid day; so even if the day portion
1614 * of the xs:gMonthDay verifies, we must insure it was not
1615 * a tzo.
1616 */
1617 if (*cur == '-') {
1618 const xmlChar *rewnd = cur;
1619 cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001620
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001621 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001622 if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
1623
1624 /*
1625 * we can use the VALID_MDAY macro to validate the month
1626 * and day because the leap year test will flag year zero
1627 * as a leap year (even though zero is an invalid year).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001628 * FUTURE TODO: Zero will become valid in XML Schema 1.1
1629 * probably.
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001630 */
1631 if (VALID_MDAY((&(dt->value.date)))) {
1632
1633 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
1634
1635 goto error;
1636 }
1637 }
1638
1639 /*
1640 * not xs:gMonthDay so rewind and check if just xs:gMonth
1641 * with an optional time zone.
1642 */
1643 cur = rewnd;
1644 }
1645
1646 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
Daniel Veillard070803b2002-05-03 07:29:38 +00001647
1648 goto error;
1649 }
1650
1651 /*
1652 * It's a right-truncated date or an xs:time.
1653 * Try to parse an xs:time then fallback on right-truncated dates.
1654 */
1655 if ((*cur >= '0') && (*cur <= '9')) {
1656 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1657 if (ret == 0) {
1658 /* it's an xs:time */
1659 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
1660 }
1661 }
1662
1663 /* fallback on date parsing */
1664 cur = dateTime;
1665
1666 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
1667 if (ret != 0)
1668 goto error;
1669
1670 /* is it an xs:gYear? */
1671 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
1672
1673 if (*cur != '-')
1674 goto error;
1675 cur++;
1676
1677 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1678 if (ret != 0)
1679 goto error;
1680
1681 /* is it an xs:gYearMonth? */
1682 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
1683
1684 if (*cur != '-')
1685 goto error;
1686 cur++;
1687
1688 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1689 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
1690 goto error;
1691
1692 /* is it an xs:date? */
1693 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
1694
1695 if (*cur != 'T')
1696 goto error;
1697 cur++;
1698
1699 /* it should be an xs:dateTime */
1700 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1701 if (ret != 0)
1702 goto error;
1703
1704 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001705 if (collapse)
1706 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00001707 if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date))))))
Daniel Veillard070803b2002-05-03 07:29:38 +00001708 goto error;
1709
Daniel Veillard455cc072003-03-31 10:13:23 +00001710
Daniel Veillard070803b2002-05-03 07:29:38 +00001711 dt->type = XML_SCHEMAS_DATETIME;
1712
Daniel Veillard455cc072003-03-31 10:13:23 +00001713done:
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001714#if 1
1715 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
1716 goto error;
1717#else
1718 /*
1719 * insure the parsed type is equal to or less significant (right
1720 * truncated) than the desired type.
1721 */
1722 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
1723
1724 /* time only matches time */
1725 if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
1726 goto error;
1727
1728 if ((type == XML_SCHEMAS_DATETIME) &&
1729 ((dt->type != XML_SCHEMAS_DATE) ||
1730 (dt->type != XML_SCHEMAS_GYEARMONTH) ||
1731 (dt->type != XML_SCHEMAS_GYEAR)))
1732 goto error;
1733
1734 if ((type == XML_SCHEMAS_DATE) &&
1735 ((dt->type != XML_SCHEMAS_GYEAR) ||
1736 (dt->type != XML_SCHEMAS_GYEARMONTH)))
1737 goto error;
1738
1739 if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
1740 goto error;
1741
1742 if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
1743 goto error;
1744 }
Daniel Veillard455cc072003-03-31 10:13:23 +00001745#endif
1746
Daniel Veillard070803b2002-05-03 07:29:38 +00001747 if (val != NULL)
1748 *val = dt;
Daniel Veillard80b19092003-03-28 13:29:53 +00001749 else
1750 xmlSchemaFreeValue(dt);
Daniel Veillard070803b2002-05-03 07:29:38 +00001751
1752 return 0;
1753
1754error:
1755 if (dt != NULL)
1756 xmlSchemaFreeValue(dt);
1757 return 1;
1758}
1759
1760/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001761 * xmlSchemaValidateDuration:
Daniel Veillard070803b2002-05-03 07:29:38 +00001762 * @type: the predefined type
1763 * @duration: string to analyze
1764 * @val: the return computed value
1765 *
1766 * Check that @duration conforms to the lexical space of the duration type.
1767 * if true a value is computed and returned in @val.
1768 *
1769 * Returns 0 if this validates, a positive error code number otherwise
1770 * and -1 in case of internal or API error.
1771 */
1772static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00001773xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001774 const xmlChar *duration, xmlSchemaValPtr *val,
1775 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001776 const xmlChar *cur = duration;
1777 xmlSchemaValPtr dur;
1778 int isneg = 0;
1779 unsigned int seq = 0;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001780 double num;
1781 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
1782 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1783 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
Daniel Veillard070803b2002-05-03 07:29:38 +00001784
1785 if (duration == NULL)
1786 return -1;
1787
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001788 if (collapse)
1789 while IS_WSP_BLANK_CH(*cur) cur++;
1790
Daniel Veillard070803b2002-05-03 07:29:38 +00001791 if (*cur == '-') {
1792 isneg = 1;
1793 cur++;
1794 }
1795
1796 /* duration must start with 'P' (after sign) */
1797 if (*cur++ != 'P')
1798 return 1;
1799
Daniel Veillard80b19092003-03-28 13:29:53 +00001800 if (*cur == 0)
1801 return 1;
1802
Daniel Veillard070803b2002-05-03 07:29:38 +00001803 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1804 if (dur == NULL)
1805 return -1;
1806
1807 while (*cur != 0) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001808
1809 /* input string should be empty or invalid date/time item */
1810 if (seq >= sizeof(desig))
1811 goto error;
1812
1813 /* T designator must be present for time items */
1814 if (*cur == 'T') {
1815 if (seq <= 3) {
1816 seq = 3;
1817 cur++;
1818 } else
1819 return 1;
1820 } else if (seq == 3)
1821 goto error;
1822
1823 /* parse the number portion of the item */
1824 PARSE_NUM(num, cur, num_type);
1825
1826 if ((num_type == -1) || (*cur == 0))
1827 goto error;
1828
1829 /* update duration based on item type */
1830 while (seq < sizeof(desig)) {
1831 if (*cur == desig[seq]) {
1832
1833 /* verify numeric type; only seconds can be float */
1834 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1835 goto error;
1836
1837 switch (seq) {
1838 case 0:
1839 dur->value.dur.mon = (long)num * 12;
1840 break;
1841 case 1:
1842 dur->value.dur.mon += (long)num;
1843 break;
1844 default:
1845 /* convert to seconds using multiplier */
1846 dur->value.dur.sec += num * multi[seq];
1847 seq++;
1848 break;
1849 }
1850
1851 break; /* exit loop */
1852 }
1853 /* no date designators found? */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00001854 if ((++seq == 3) || (seq == 6))
Daniel Veillard070803b2002-05-03 07:29:38 +00001855 goto error;
1856 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001857 cur++;
1858 if (collapse)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001859 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001860 }
1861
1862 if (isneg) {
1863 dur->value.dur.mon = -dur->value.dur.mon;
1864 dur->value.dur.day = -dur->value.dur.day;
1865 dur->value.dur.sec = -dur->value.dur.sec;
1866 }
1867
1868 if (val != NULL)
1869 *val = dur;
Daniel Veillard80b19092003-03-28 13:29:53 +00001870 else
1871 xmlSchemaFreeValue(dur);
Daniel Veillard070803b2002-05-03 07:29:38 +00001872
1873 return 0;
1874
1875error:
1876 if (dur != NULL)
1877 xmlSchemaFreeValue(dur);
1878 return 1;
1879}
1880
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001881/**
1882 * xmlSchemaStrip:
1883 * @value: a value
1884 *
1885 * Removes the leading and ending spaces of a string
1886 *
1887 * Returns the new string or NULL if no change was required.
1888 */
1889static xmlChar *
1890xmlSchemaStrip(const xmlChar *value) {
1891 const xmlChar *start = value, *end, *f;
1892
1893 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001894 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001895 end = start;
1896 while (*end != 0) end++;
1897 f = end;
1898 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001899 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001900 end++;
1901 if ((start == value) && (f == end)) return(NULL);
1902 return(xmlStrndup(start, end - start));
1903}
Daniel Veillard96a4b252003-02-06 08:22:32 +00001904
1905/**
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001906 * xmlSchemaWhiteSpaceReplace:
1907 * @value: a value
1908 *
1909 * Replaces 0xd, 0x9 and 0xa with a space.
1910 *
1911 * Returns the new string or NULL if no change was required.
1912 */
1913xmlChar *
1914xmlSchemaWhiteSpaceReplace(const xmlChar *value) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001915 const xmlChar *cur = value;
1916 xmlChar *ret = NULL, *mcur;
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001917
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001918 if (value == NULL)
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001919 return(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001920
1921 while ((*cur != 0) &&
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001922 (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) {
1923 cur++;
1924 }
1925 if (*cur == 0)
1926 return (NULL);
1927 ret = xmlStrdup(value);
1928 /* TODO FIXME: I guess gcc will bark at this. */
1929 mcur = (xmlChar *) (ret + (cur - value));
1930 do {
1931 if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) )
1932 *mcur = ' ';
1933 mcur++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001934 } while (*mcur != 0);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001935 return(ret);
1936}
1937
1938/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001939 * xmlSchemaCollapseString:
1940 * @value: a value
1941 *
1942 * Removes and normalize white spaces in the string
1943 *
1944 * Returns the new string or NULL if no change was required.
1945 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00001946xmlChar *
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001947xmlSchemaCollapseString(const xmlChar *value) {
1948 const xmlChar *start = value, *end, *f;
1949 xmlChar *g;
1950 int col = 0;
1951
1952 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001953 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001954 end = start;
1955 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001956 if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001957 col = end - start;
1958 break;
1959 } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
1960 col = end - start;
1961 break;
1962 }
1963 end++;
1964 }
1965 if (col == 0) {
1966 f = end;
1967 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001968 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001969 end++;
1970 if ((start == value) && (f == end)) return(NULL);
1971 return(xmlStrndup(start, end - start));
1972 }
1973 start = xmlStrdup(start);
1974 if (start == NULL) return(NULL);
1975 g = (xmlChar *) (start + col);
1976 end = g;
1977 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001978 if (IS_BLANK_CH(*end)) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001979 end++;
William M. Brack76e95df2003-10-18 16:20:14 +00001980 while (IS_BLANK_CH(*end)) end++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001981 if (*end != 0)
1982 *g++ = ' ';
1983 } else
1984 *g++ = *end++;
1985 }
1986 *g = 0;
1987 return((xmlChar *) start);
1988}
1989
1990/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001991 * xmlSchemaValAtomicListNode:
1992 * @type: the predefined atomic type for a token in the list
1993 * @value: the list value to check
1994 * @ret: the return computed value
1995 * @node: the node containing the value
1996 *
1997 * Check that a value conforms to the lexical space of the predefined
1998 * list type. if true a value is computed and returned in @ret.
1999 *
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002000 * Returns the number of items if this validates, a negative error code
2001 * number otherwise
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002002 */
2003static int
2004xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
2005 xmlSchemaValPtr *ret, xmlNodePtr node) {
2006 xmlChar *val, *cur, *endval;
2007 int nb_values = 0;
Daniel Veillard580ced82003-03-21 21:22:48 +00002008 int tmp = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002009
2010 if (value == NULL) {
2011 return(-1);
2012 }
2013 val = xmlStrdup(value);
2014 if (val == NULL) {
2015 return(-1);
2016 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002017 if (ret != NULL) {
2018 *ret = NULL;
2019 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002020 cur = val;
2021 /*
2022 * Split the list
2023 */
William M. Brack76e95df2003-10-18 16:20:14 +00002024 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002025 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00002026 if (IS_BLANK_CH(*cur)) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002027 *cur = 0;
2028 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00002029 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002030 } else {
2031 nb_values++;
2032 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00002033 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002034 }
2035 }
2036 if (nb_values == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002037 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002038 return(nb_values);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002039 }
2040 endval = cur;
2041 cur = val;
2042 while ((*cur == 0) && (cur != endval)) cur++;
2043 while (cur != endval) {
2044 tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
2045 if (tmp != 0)
2046 break;
2047 while (*cur != 0) cur++;
2048 while ((*cur == 0) && (cur != endval)) cur++;
2049 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002050 /* TODO what return value ? c.f. bug #158628
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002051 if (ret != NULL) {
2052 TODO
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002053 } */
2054 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002055 if (tmp == 0)
2056 return(nb_values);
2057 return(-1);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002058}
2059
2060/**
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002061 * xmlSchemaParseUInt:
2062 * @str: pointer to the string R/W
2063 * @llo: pointer to the low result
2064 * @lmi: pointer to the mid result
2065 * @lhi: pointer to the high result
2066 *
2067 * Parse an unsigned long into 3 fields.
2068 *
William M. Brackec3b4b72005-03-15 15:50:17 +00002069 * Returns the number of significant digits in the number or
Daniel Veillardbfc42632008-04-03 10:43:52 +00002070 * -1 if overflow of the capacity and -2 if it's not a number.
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002071 */
2072static int
2073xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002074 unsigned long *lmi, unsigned long *lhi) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002075 unsigned long lo = 0, mi = 0, hi = 0;
2076 const xmlChar *tmp, *cur = *str;
2077 int ret = 0, i = 0;
2078
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002079 if (!((*cur >= '0') && (*cur <= '9')))
Daniel Veillardbfc42632008-04-03 10:43:52 +00002080 return(-2);
2081
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002082 while (*cur == '0') { /* ignore leading zeroes */
2083 cur++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002084 }
2085 tmp = cur;
2086 while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002087 i++;tmp++;ret++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002088 }
2089 if (i > 24) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002090 *str = tmp;
2091 return(-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002092 }
2093 while (i > 16) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002094 hi = hi * 10 + (*cur++ - '0');
2095 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002096 }
2097 while (i > 8) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002098 mi = mi * 10 + (*cur++ - '0');
2099 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002100 }
2101 while (i > 0) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002102 lo = lo * 10 + (*cur++ - '0');
2103 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002104 }
2105
2106 *str = cur;
2107 *llo = lo;
2108 *lmi = mi;
2109 *lhi = hi;
2110 return(ret);
2111}
2112
2113/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002114 * xmlSchemaValAtomicType:
2115 * @type: the predefined type
2116 * @value: the value to check
2117 * @val: the return computed value
2118 * @node: the node containing the value
2119 * flags: flags to control the vlidation
2120 *
2121 * Check that a value conforms to the lexical space of the atomic type.
2122 * if true a value is computed and returned in @val.
Daniel Veillard01fa6152004-06-29 17:04:39 +00002123 * This checks the value space for list types as well (IDREFS, NMTOKENS).
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002124 *
2125 * Returns 0 if this validates, a positive error code number otherwise
2126 * and -1 in case of internal or API error.
2127 */
2128static int
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002129xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002130 xmlSchemaValPtr * val, xmlNodePtr node, int flags,
2131 xmlSchemaWhitespaceValueType ws,
2132 int normOnTheFly, int applyNorm, int createStringValue)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002133{
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002134 xmlSchemaValPtr v;
2135 xmlChar *norm = NULL;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00002136 int ret = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002137
2138 if (xmlSchemaTypesInitialized == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00002139 xmlSchemaInitTypes();
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002140 if (type == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002141 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002142
Daniel Veillardeebd6332004-08-26 10:30:44 +00002143 /*
2144 * validating a non existant text node is similar to validating
2145 * an empty one.
2146 */
2147 if (value == NULL)
2148 value = BAD_CAST "";
2149
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002150 if (val != NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002151 *val = NULL;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002152 if ((flags == 0) && (value != NULL)) {
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00002153
Daniel Veillard01fa6152004-06-29 17:04:39 +00002154 if ((type->builtInType != XML_SCHEMAS_STRING) &&
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002155 (type->builtInType != XML_SCHEMAS_ANYTYPE) &&
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00002156 (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) {
2157 if (type->builtInType == XML_SCHEMAS_NORMSTRING)
2158 norm = xmlSchemaWhiteSpaceReplace(value);
2159 else
2160 norm = xmlSchemaCollapseString(value);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002161 if (norm != NULL)
2162 value = norm;
2163 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002164 }
2165
Daniel Veillard01fa6152004-06-29 17:04:39 +00002166 switch (type->builtInType) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002167 case XML_SCHEMAS_UNKNOWN:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002168 goto error;
William M. Brack2f2a6632004-08-20 23:09:47 +00002169 case XML_SCHEMAS_ANYTYPE:
2170 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002171 if ((createStringValue) && (val != NULL)) {
2172 v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE);
2173 if (v != NULL) {
2174 v->value.str = xmlStrdup(value);
2175 *val = v;
2176 } else {
2177 goto error;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002178 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002179 }
William M. Brack2f2a6632004-08-20 23:09:47 +00002180 goto return0;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002181 case XML_SCHEMAS_STRING:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002182 if (! normOnTheFly) {
2183 const xmlChar *cur = value;
2184
2185 if (ws == XML_SCHEMA_WHITESPACE_REPLACE) {
2186 while (*cur != 0) {
2187 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2188 goto return1;
2189 } else {
2190 cur++;
2191 }
2192 }
2193 } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
2194 while (*cur != 0) {
2195 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2196 goto return1;
2197 } else if IS_WSP_SPACE_CH(*cur) {
2198 cur++;
2199 if IS_WSP_SPACE_CH(*cur)
2200 goto return1;
2201 } else {
2202 cur++;
2203 }
2204 }
2205 }
2206 }
2207 if (createStringValue && (val != NULL)) {
2208 if (applyNorm) {
2209 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2210 norm = xmlSchemaCollapseString(value);
2211 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
2212 norm = xmlSchemaWhiteSpaceReplace(value);
2213 if (norm != NULL)
2214 value = norm;
2215 }
2216 v = xmlSchemaNewValue(XML_SCHEMAS_STRING);
2217 if (v != NULL) {
2218 v->value.str = xmlStrdup(value);
2219 *val = v;
2220 } else {
2221 goto error;
2222 }
2223 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002224 goto return0;
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002225 case XML_SCHEMAS_NORMSTRING:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002226 if (normOnTheFly) {
2227 if (applyNorm) {
2228 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2229 norm = xmlSchemaCollapseString(value);
2230 else
2231 norm = xmlSchemaWhiteSpaceReplace(value);
2232 if (norm != NULL)
2233 value = norm;
2234 }
2235 } else {
2236 const xmlChar *cur = value;
2237 while (*cur != 0) {
2238 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2239 goto return1;
2240 } else {
2241 cur++;
2242 }
2243 }
2244 }
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002245 if (val != NULL) {
2246 v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
2247 if (v != NULL) {
2248 v->value.str = xmlStrdup(value);
2249 *val = v;
2250 } else {
2251 goto error;
2252 }
2253 }
2254 goto return0;
2255 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002256 case XML_SCHEMAS_DECIMAL:{
William M. Brack273670f2005-03-11 15:55:14 +00002257 const xmlChar *cur = value;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002258 unsigned int len, neg, integ, hasLeadingZeroes;
William M. Brack273670f2005-03-11 15:55:14 +00002259 xmlChar cval[25];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002260 xmlChar *cptr = cval;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002261
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002262 if ((cur == NULL) || (*cur == 0))
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002263 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002264
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002265 /*
2266 * xs:decimal has a whitespace-facet value of 'collapse'.
2267 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002268 if (normOnTheFly)
2269 while IS_WSP_BLANK_CH(*cur) cur++;
2270
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002271 /*
2272 * First we handle an optional sign.
2273 */
2274 neg = 0;
2275 if (*cur == '-') {
2276 neg = 1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002277 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002278 } else if (*cur == '+')
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002279 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002280 /*
2281 * Disallow: "", "-", "- "
2282 */
2283 if (*cur == 0)
2284 goto return1;
William M. Brack273670f2005-03-11 15:55:14 +00002285 /*
2286 * Next we "pre-parse" the number, in preparation for calling
2287 * the common routine xmlSchemaParseUInt. We get rid of any
2288 * leading zeroes (because we have reserved only 25 chars),
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002289 * and note the position of a decimal point.
William M. Brack273670f2005-03-11 15:55:14 +00002290 */
2291 len = 0;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002292 integ = ~0u;
2293 hasLeadingZeroes = 0;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002294 /*
2295 * Skip leading zeroes.
2296 */
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002297 while (*cur == '0') {
William M. Brack273670f2005-03-11 15:55:14 +00002298 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002299 hasLeadingZeroes = 1;
2300 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002301 if (*cur != 0) {
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002302 do {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002303 if ((*cur >= '0') && (*cur <= '9')) {
2304 *cptr++ = *cur++;
2305 len++;
2306 } else if (*cur == '.') {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002307 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002308 integ = len;
2309 do {
2310 if ((*cur >= '0') && (*cur <= '9')) {
2311 *cptr++ = *cur++;
2312 len++;
2313 } else
2314 break;
2315 } while (len < 24);
2316 /*
2317 * Disallow "." but allow "00."
2318 */
2319 if ((len == 0) && (!hasLeadingZeroes))
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002320 goto return1;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002321 break;
2322 } else
2323 break;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002324 } while (len < 24);
William M. Brack273670f2005-03-11 15:55:14 +00002325 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002326 if (normOnTheFly)
2327 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brack273670f2005-03-11 15:55:14 +00002328 if (*cur != 0)
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002329 goto return1; /* error if any extraneous chars */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002330 if (val != NULL) {
2331 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
2332 if (v != NULL) {
William M. Brack273670f2005-03-11 15:55:14 +00002333 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002334 * Now evaluate the significant digits of the number
2335 */
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002336 if (len != 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002337
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002338 if (integ != ~0u) {
2339 /*
2340 * Get rid of trailing zeroes in the
2341 * fractional part.
2342 */
2343 while ((len != integ) && (*(cptr-1) == '0')) {
2344 cptr--;
2345 len--;
2346 }
2347 }
2348 /*
2349 * Terminate the (preparsed) string.
2350 */
2351 if (len != 0) {
Daniel Veillardbfc42632008-04-03 10:43:52 +00002352 *cptr = 0;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002353 cptr = cval;
Daniel Veillardbfc42632008-04-03 10:43:52 +00002354
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002355 xmlSchemaParseUInt((const xmlChar **)&cptr,
2356 &v->value.decimal.lo,
2357 &v->value.decimal.mi,
2358 &v->value.decimal.hi);
2359 }
2360 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002361 /*
2362 * Set the total digits to 1 if a zero value.
2363 */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002364 v->value.decimal.sign = neg;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002365 if (len == 0) {
2366 /* Speedup for zero values. */
2367 v->value.decimal.total = 1;
William M. Brack273670f2005-03-11 15:55:14 +00002368 } else {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002369 v->value.decimal.total = len;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002370 if (integ == ~0u)
2371 v->value.decimal.frac = 0;
2372 else
2373 v->value.decimal.frac = len - integ;
William M. Brack273670f2005-03-11 15:55:14 +00002374 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002375 *val = v;
2376 }
2377 }
2378 goto return0;
2379 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002380 case XML_SCHEMAS_TIME:
2381 case XML_SCHEMAS_GDAY:
2382 case XML_SCHEMAS_GMONTH:
2383 case XML_SCHEMAS_GMONTHDAY:
2384 case XML_SCHEMAS_GYEAR:
2385 case XML_SCHEMAS_GYEARMONTH:
2386 case XML_SCHEMAS_DATE:
2387 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002388 ret = xmlSchemaValidateDates(type->builtInType, value, val,
2389 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002390 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002391 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002392 ret = xmlSchemaValidateDuration(type, value, val,
2393 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002394 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002395 case XML_SCHEMAS_FLOAT:
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002396 case XML_SCHEMAS_DOUBLE: {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002397 const xmlChar *cur = value;
2398 int neg = 0;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002399 int digits_before = 0;
2400 int digits_after = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002401
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002402 if (normOnTheFly)
2403 while IS_WSP_BLANK_CH(*cur) cur++;
2404
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002405 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
2406 cur += 3;
2407 if (*cur != 0)
2408 goto return1;
2409 if (val != NULL) {
2410 if (type == xmlSchemaTypeFloatDef) {
2411 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2412 if (v != NULL) {
2413 v->value.f = (float) xmlXPathNAN;
2414 } else {
2415 xmlSchemaFreeValue(v);
2416 goto error;
2417 }
2418 } else {
2419 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2420 if (v != NULL) {
2421 v->value.d = xmlXPathNAN;
2422 } else {
2423 xmlSchemaFreeValue(v);
2424 goto error;
2425 }
2426 }
2427 *val = v;
2428 }
2429 goto return0;
2430 }
2431 if (*cur == '-') {
2432 neg = 1;
2433 cur++;
2434 }
2435 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
2436 cur += 3;
2437 if (*cur != 0)
2438 goto return1;
2439 if (val != NULL) {
2440 if (type == xmlSchemaTypeFloatDef) {
2441 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2442 if (v != NULL) {
2443 if (neg)
2444 v->value.f = (float) xmlXPathNINF;
2445 else
2446 v->value.f = (float) xmlXPathPINF;
2447 } else {
2448 xmlSchemaFreeValue(v);
2449 goto error;
2450 }
2451 } else {
2452 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2453 if (v != NULL) {
2454 if (neg)
2455 v->value.d = xmlXPathNINF;
2456 else
2457 v->value.d = xmlXPathPINF;
2458 } else {
2459 xmlSchemaFreeValue(v);
2460 goto error;
2461 }
2462 }
2463 *val = v;
2464 }
2465 goto return0;
2466 }
2467 if ((neg == 0) && (*cur == '+'))
2468 cur++;
2469 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
2470 goto return1;
2471 while ((*cur >= '0') && (*cur <= '9')) {
2472 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002473 digits_before++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002474 }
2475 if (*cur == '.') {
2476 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002477 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002478 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002479 digits_after++;
2480 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002481 }
Daniel Veillard1ea95902010-07-28 14:49:55 +02002482 if ((digits_before == 0) && (digits_after == 0))
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002483 goto return1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002484 if ((*cur == 'e') || (*cur == 'E')) {
2485 cur++;
2486 if ((*cur == '-') || (*cur == '+'))
2487 cur++;
2488 while ((*cur >= '0') && (*cur <= '9'))
2489 cur++;
2490 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002491 if (normOnTheFly)
2492 while IS_WSP_BLANK_CH(*cur) cur++;
2493
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002494 if (*cur != 0)
2495 goto return1;
2496 if (val != NULL) {
2497 if (type == xmlSchemaTypeFloatDef) {
2498 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2499 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002500 /*
2501 * TODO: sscanf seems not to give the correct
2502 * value for extremely high/low values.
2503 * E.g. "1E-149" results in zero.
2504 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002505 if (sscanf((const char *) value, "%f",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002506 &(v->value.f)) == 1) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002507 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002508 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002509 xmlSchemaFreeValue(v);
2510 goto return1;
2511 }
2512 } else {
2513 goto error;
2514 }
2515 } else {
2516 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2517 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002518 /*
2519 * TODO: sscanf seems not to give the correct
2520 * value for extremely high/low values.
2521 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002522 if (sscanf((const char *) value, "%lf",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002523 &(v->value.d)) == 1) {
2524 *val = v;
2525 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002526 xmlSchemaFreeValue(v);
2527 goto return1;
2528 }
2529 } else {
2530 goto error;
2531 }
2532 }
2533 }
2534 goto return0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00002535 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002536 case XML_SCHEMAS_BOOLEAN:{
2537 const xmlChar *cur = value;
2538
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002539 if (normOnTheFly) {
2540 while IS_WSP_BLANK_CH(*cur) cur++;
2541 if (*cur == '0') {
2542 ret = 0;
2543 cur++;
2544 } else if (*cur == '1') {
2545 ret = 1;
2546 cur++;
2547 } else if (*cur == 't') {
2548 cur++;
2549 if ((*cur++ == 'r') && (*cur++ == 'u') &&
2550 (*cur++ == 'e')) {
2551 ret = 1;
2552 } else
2553 goto return1;
2554 } else if (*cur == 'f') {
2555 cur++;
2556 if ((*cur++ == 'a') && (*cur++ == 'l') &&
2557 (*cur++ == 's') && (*cur++ == 'e')) {
2558 ret = 0;
2559 } else
2560 goto return1;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00002561 } else
2562 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002563 if (*cur != 0) {
2564 while IS_WSP_BLANK_CH(*cur) cur++;
2565 if (*cur != 0)
2566 goto return1;
2567 }
2568 } else {
2569 if ((cur[0] == '0') && (cur[1] == 0))
2570 ret = 0;
2571 else if ((cur[0] == '1') && (cur[1] == 0))
2572 ret = 1;
2573 else if ((cur[0] == 't') && (cur[1] == 'r')
2574 && (cur[2] == 'u') && (cur[3] == 'e')
2575 && (cur[4] == 0))
2576 ret = 1;
2577 else if ((cur[0] == 'f') && (cur[1] == 'a')
2578 && (cur[2] == 'l') && (cur[3] == 's')
2579 && (cur[4] == 'e') && (cur[5] == 0))
2580 ret = 0;
2581 else
2582 goto return1;
2583 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002584 if (val != NULL) {
2585 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
2586 if (v != NULL) {
2587 v->value.b = ret;
2588 *val = v;
2589 } else {
2590 goto error;
2591 }
2592 }
2593 goto return0;
2594 }
2595 case XML_SCHEMAS_TOKEN:{
2596 const xmlChar *cur = value;
2597
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002598 if (! normOnTheFly) {
2599 while (*cur != 0) {
2600 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2601 goto return1;
2602 } else if (*cur == ' ') {
2603 cur++;
2604 if (*cur == 0)
2605 goto return1;
2606 if (*cur == ' ')
2607 goto return1;
2608 } else {
2609 cur++;
2610 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002611 }
2612 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002613 if (val != NULL) {
2614 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
2615 if (v != NULL) {
2616 v->value.str = xmlStrdup(value);
2617 *val = v;
2618 } else {
2619 goto error;
2620 }
2621 }
2622 goto return0;
2623 }
2624 case XML_SCHEMAS_LANGUAGE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002625 if (normOnTheFly) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002626 norm = xmlSchemaCollapseString(value);
2627 if (norm != NULL)
2628 value = norm;
2629 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002630 if (xmlCheckLanguageID(value) == 1) {
2631 if (val != NULL) {
2632 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
2633 if (v != NULL) {
2634 v->value.str = xmlStrdup(value);
2635 *val = v;
2636 } else {
2637 goto error;
2638 }
2639 }
2640 goto return0;
2641 }
2642 goto return1;
2643 case XML_SCHEMAS_NMTOKEN:
2644 if (xmlValidateNMToken(value, 1) == 0) {
2645 if (val != NULL) {
2646 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
2647 if (v != NULL) {
2648 v->value.str = xmlStrdup(value);
2649 *val = v;
2650 } else {
2651 goto error;
2652 }
2653 }
2654 goto return0;
2655 }
2656 goto return1;
2657 case XML_SCHEMAS_NMTOKENS:
2658 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
2659 value, val, node);
2660 if (ret > 0)
2661 ret = 0;
2662 else
2663 ret = 1;
2664 goto done;
2665 case XML_SCHEMAS_NAME:
2666 ret = xmlValidateName(value, 1);
Daniel Veillarddf292f72005-01-16 19:00:15 +00002667 if ((ret == 0) && (val != NULL) && (value != NULL)) {
2668 v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
2669 if (v != NULL) {
2670 const xmlChar *start = value, *end;
2671 while (IS_BLANK_CH(*start)) start++;
2672 end = start;
2673 while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
2674 v->value.str = xmlStrndup(start, end - start);
2675 *val = v;
2676 } else {
2677 goto error;
2678 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002679 }
2680 goto done;
2681 case XML_SCHEMAS_QNAME:{
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002682 const xmlChar *uri = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002683 xmlChar *local = NULL;
2684
2685 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002686 if (ret != 0)
2687 goto done;
2688 if (node != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002689 xmlChar *prefix;
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002690 xmlNsPtr ns;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002691
2692 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002693 ns = xmlSearchNs(node->doc, node, prefix);
2694 if ((ns == NULL) && (prefix != NULL)) {
2695 xmlFree(prefix);
2696 if (local != NULL)
2697 xmlFree(local);
2698 goto return1;
2699 }
2700 if (ns != NULL)
2701 uri = ns->href;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002702 if (prefix != NULL)
2703 xmlFree(prefix);
2704 }
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002705 if (val != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002706 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002707 if (v == NULL) {
2708 if (local != NULL)
2709 xmlFree(local);
2710 goto error;
2711 }
2712 if (local != NULL)
2713 v->value.qname.name = local;
2714 else
2715 v->value.qname.name = xmlStrdup(value);
2716 if (uri != NULL)
2717 v->value.qname.uri = xmlStrdup(uri);
2718 *val = v;
2719 } else
2720 if (local != NULL)
2721 xmlFree(local);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002722 goto done;
2723 }
2724 case XML_SCHEMAS_NCNAME:
2725 ret = xmlValidateNCName(value, 1);
2726 if ((ret == 0) && (val != NULL)) {
2727 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
2728 if (v != NULL) {
2729 v->value.str = xmlStrdup(value);
2730 *val = v;
2731 } else {
2732 goto error;
2733 }
2734 }
2735 goto done;
2736 case XML_SCHEMAS_ID:
2737 ret = xmlValidateNCName(value, 1);
2738 if ((ret == 0) && (val != NULL)) {
2739 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
2740 if (v != NULL) {
2741 v->value.str = xmlStrdup(value);
2742 *val = v;
2743 } else {
2744 goto error;
2745 }
2746 }
2747 if ((ret == 0) && (node != NULL) &&
2748 (node->type == XML_ATTRIBUTE_NODE)) {
2749 xmlAttrPtr attr = (xmlAttrPtr) node;
2750
2751 /*
2752 * NOTE: the IDness might have already be declared in the DTD
2753 */
2754 if (attr->atype != XML_ATTRIBUTE_ID) {
2755 xmlIDPtr res;
2756 xmlChar *strip;
2757
2758 strip = xmlSchemaStrip(value);
2759 if (strip != NULL) {
2760 res = xmlAddID(NULL, node->doc, strip, attr);
2761 xmlFree(strip);
2762 } else
2763 res = xmlAddID(NULL, node->doc, value, attr);
2764 if (res == NULL) {
2765 ret = 2;
2766 } else {
2767 attr->atype = XML_ATTRIBUTE_ID;
2768 }
2769 }
2770 }
2771 goto done;
2772 case XML_SCHEMAS_IDREF:
2773 ret = xmlValidateNCName(value, 1);
2774 if ((ret == 0) && (val != NULL)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00002775 v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
2776 if (v == NULL)
2777 goto error;
2778 v->value.str = xmlStrdup(value);
2779 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002780 }
2781 if ((ret == 0) && (node != NULL) &&
2782 (node->type == XML_ATTRIBUTE_NODE)) {
2783 xmlAttrPtr attr = (xmlAttrPtr) node;
2784 xmlChar *strip;
2785
2786 strip = xmlSchemaStrip(value);
2787 if (strip != NULL) {
2788 xmlAddRef(NULL, node->doc, strip, attr);
2789 xmlFree(strip);
2790 } else
2791 xmlAddRef(NULL, node->doc, value, attr);
2792 attr->atype = XML_ATTRIBUTE_IDREF;
2793 }
2794 goto done;
2795 case XML_SCHEMAS_IDREFS:
2796 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
2797 value, val, node);
2798 if (ret < 0)
2799 ret = 2;
2800 else
2801 ret = 0;
2802 if ((ret == 0) && (node != NULL) &&
2803 (node->type == XML_ATTRIBUTE_NODE)) {
2804 xmlAttrPtr attr = (xmlAttrPtr) node;
2805
2806 attr->atype = XML_ATTRIBUTE_IDREFS;
2807 }
2808 goto done;
2809 case XML_SCHEMAS_ENTITY:{
2810 xmlChar *strip;
2811
2812 ret = xmlValidateNCName(value, 1);
2813 if ((node == NULL) || (node->doc == NULL))
2814 ret = 3;
2815 if (ret == 0) {
2816 xmlEntityPtr ent;
2817
2818 strip = xmlSchemaStrip(value);
2819 if (strip != NULL) {
2820 ent = xmlGetDocEntity(node->doc, strip);
2821 xmlFree(strip);
2822 } else {
2823 ent = xmlGetDocEntity(node->doc, value);
2824 }
2825 if ((ent == NULL) ||
2826 (ent->etype !=
2827 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
2828 ret = 4;
2829 }
2830 if ((ret == 0) && (val != NULL)) {
2831 TODO;
2832 }
2833 if ((ret == 0) && (node != NULL) &&
2834 (node->type == XML_ATTRIBUTE_NODE)) {
2835 xmlAttrPtr attr = (xmlAttrPtr) node;
2836
2837 attr->atype = XML_ATTRIBUTE_ENTITY;
2838 }
2839 goto done;
2840 }
2841 case XML_SCHEMAS_ENTITIES:
2842 if ((node == NULL) || (node->doc == NULL))
2843 goto return3;
2844 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
2845 value, val, node);
2846 if (ret <= 0)
2847 ret = 1;
2848 else
2849 ret = 0;
2850 if ((ret == 0) && (node != NULL) &&
2851 (node->type == XML_ATTRIBUTE_NODE)) {
2852 xmlAttrPtr attr = (xmlAttrPtr) node;
2853
2854 attr->atype = XML_ATTRIBUTE_ENTITIES;
2855 }
2856 goto done;
2857 case XML_SCHEMAS_NOTATION:{
2858 xmlChar *uri = NULL;
2859 xmlChar *local = NULL;
2860
2861 ret = xmlValidateQName(value, 1);
2862 if ((ret == 0) && (node != NULL)) {
2863 xmlChar *prefix;
2864
2865 local = xmlSplitQName2(value, &prefix);
2866 if (prefix != NULL) {
2867 xmlNsPtr ns;
2868
2869 ns = xmlSearchNs(node->doc, node, prefix);
2870 if (ns == NULL)
2871 ret = 1;
2872 else if (val != NULL)
2873 uri = xmlStrdup(ns->href);
2874 }
2875 if ((local != NULL) && ((val == NULL) || (ret != 0)))
2876 xmlFree(local);
2877 if (prefix != NULL)
2878 xmlFree(prefix);
2879 }
2880 if ((node == NULL) || (node->doc == NULL))
2881 ret = 3;
2882 if (ret == 0) {
2883 ret = xmlValidateNotationUse(NULL, node->doc, value);
2884 if (ret == 1)
2885 ret = 0;
2886 else
2887 ret = 1;
2888 }
2889 if ((ret == 0) && (val != NULL)) {
2890 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
2891 if (v != NULL) {
2892 if (local != NULL)
2893 v->value.qname.name = local;
2894 else
2895 v->value.qname.name = xmlStrdup(value);
2896 if (uri != NULL)
2897 v->value.qname.uri = uri;
2898
2899 *val = v;
2900 } else {
2901 if (local != NULL)
2902 xmlFree(local);
2903 if (uri != NULL)
2904 xmlFree(uri);
2905 goto error;
2906 }
2907 }
2908 goto done;
2909 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002910 case XML_SCHEMAS_ANYURI:{
Daniel Veillard11c466a2004-03-14 12:20:15 +00002911 if (*value != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002912 xmlURIPtr uri;
Vincent Lefevre933e5de2009-08-07 16:42:24 +02002913 xmlChar *tmpval, *cur;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002914 if (normOnTheFly) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002915 norm = xmlSchemaCollapseString(value);
2916 if (norm != NULL)
2917 value = norm;
2918 }
Vincent Lefevre933e5de2009-08-07 16:42:24 +02002919 tmpval = xmlStrdup(value);
2920 for (cur = tmpval; *cur; ++cur) {
2921 if (*cur < 32 || *cur >= 127 || *cur == ' ' ||
2922 *cur == '<' || *cur == '>' || *cur == '"' ||
2923 *cur == '{' || *cur == '}' || *cur == '|' ||
2924 *cur == '\\' || *cur == '^' || *cur == '`' ||
2925 *cur == '\'')
2926 *cur = '_';
2927 }
2928 uri = xmlParseURI((const char *) tmpval);
2929 xmlFree(tmpval);
Daniel Veillard11c466a2004-03-14 12:20:15 +00002930 if (uri == NULL)
2931 goto return1;
2932 xmlFreeURI(uri);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002933 }
Daniel Veillard11c466a2004-03-14 12:20:15 +00002934
2935 if (val != NULL) {
2936 v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
2937 if (v == NULL)
2938 goto error;
2939 v->value.str = xmlStrdup(value);
2940 *val = v;
2941 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002942 goto return0;
2943 }
2944 case XML_SCHEMAS_HEXBINARY:{
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002945 const xmlChar *cur = value, *start;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002946 xmlChar *base;
2947 int total, i = 0;
2948
Daniel Veillardf34a20e2004-08-31 08:42:17 +00002949 if (cur == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002950 goto return1;
2951
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002952 if (normOnTheFly)
2953 while IS_WSP_BLANK_CH(*cur) cur++;
2954
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002955 start = cur;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002956 while (((*cur >= '0') && (*cur <= '9')) ||
2957 ((*cur >= 'A') && (*cur <= 'F')) ||
2958 ((*cur >= 'a') && (*cur <= 'f'))) {
2959 i++;
2960 cur++;
2961 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002962 if (normOnTheFly)
2963 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002964
2965 if (*cur != 0)
2966 goto return1;
2967 if ((i % 2) != 0)
2968 goto return1;
2969
2970 if (val != NULL) {
2971
2972 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2973 if (v == NULL)
2974 goto error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002975 /*
2976 * Copy only the normalized piece.
2977 * CRITICAL TODO: Check this.
2978 */
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002979 cur = xmlStrndup(start, i);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002980 if (cur == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002981 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002982 xmlFree(v);
2983 goto return1;
2984 }
2985
2986 total = i / 2; /* number of octets */
2987
2988 base = (xmlChar *) cur;
2989 while (i-- > 0) {
2990 if (*base >= 'a')
2991 *base = *base - ('a' - 'A');
2992 base++;
2993 }
2994
2995 v->value.hex.str = (xmlChar *) cur;
2996 v->value.hex.total = total;
2997 *val = v;
2998 }
2999 goto return0;
3000 }
3001 case XML_SCHEMAS_BASE64BINARY:{
3002 /* ISSUE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003003 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003004 * Ignore all stray characters? (yes, currently)
3005 * Worry about long lines? (no, currently)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003006 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003007 * rfc2045.txt:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003008 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003009 * "The encoded output stream must be represented in lines of
3010 * no more than 76 characters each. All line breaks or other
3011 * characters not found in Table 1 must be ignored by decoding
3012 * software. In base64 data, characters other than those in
3013 * Table 1, line breaks, and other white space probably
3014 * indicate a transmission error, about which a warning
3015 * message or even a message rejection might be appropriate
3016 * under some circumstances." */
3017 const xmlChar *cur = value;
3018 xmlChar *base;
3019 int total, i = 0, pad = 0;
3020
3021 if (cur == NULL)
3022 goto return1;
3023
3024 for (; *cur; ++cur) {
3025 int decc;
3026
3027 decc = _xmlSchemaBase64Decode(*cur);
3028 if (decc < 0) ;
3029 else if (decc < 64)
3030 i++;
3031 else
3032 break;
3033 }
3034 for (; *cur; ++cur) {
3035 int decc;
3036
3037 decc = _xmlSchemaBase64Decode(*cur);
3038 if (decc < 0) ;
3039 else if (decc < 64)
3040 goto return1;
3041 if (decc == 64)
3042 pad++;
3043 }
3044
3045 /* rfc2045.txt: "Special processing is performed if fewer than
3046 * 24 bits are available at the end of the data being encoded.
3047 * A full encoding quantum is always completed at the end of a
3048 * body. When fewer than 24 input bits are available in an
3049 * input group, zero bits are added (on the right) to form an
3050 * integral number of 6-bit groups. Padding at the end of the
3051 * data is performed using the "=" character. Since all
3052 * base64 input is an integral number of octets, only the
3053 * following cases can arise: (1) the final quantum of
3054 * encoding input is an integral multiple of 24 bits; here,
3055 * the final unit of encoded output will be an integral
3056 * multiple ofindent: Standard input:701: Warning:old style
3057 * assignment ambiguity in "=*". Assuming "= *" 4 characters
3058 * with no "=" padding, (2) the final
3059 * quantum of encoding input is exactly 8 bits; here, the
3060 * final unit of encoded output will be two characters
3061 * followed by two "=" padding characters, or (3) the final
3062 * quantum of encoding input is exactly 16 bits; here, the
3063 * final unit of encoded output will be three characters
3064 * followed by one "=" padding character." */
3065
3066 total = 3 * (i / 4);
3067 if (pad == 0) {
3068 if (i % 4 != 0)
3069 goto return1;
3070 } else if (pad == 1) {
3071 int decc;
3072
3073 if (i % 4 != 3)
3074 goto return1;
3075 for (decc = _xmlSchemaBase64Decode(*cur);
3076 (decc < 0) || (decc > 63);
3077 decc = _xmlSchemaBase64Decode(*cur))
3078 --cur;
3079 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
3080 /* 00111100 -> 0x3c */
3081 if (decc & ~0x3c)
3082 goto return1;
3083 total += 2;
3084 } else if (pad == 2) {
3085 int decc;
3086
3087 if (i % 4 != 2)
3088 goto return1;
3089 for (decc = _xmlSchemaBase64Decode(*cur);
3090 (decc < 0) || (decc > 63);
3091 decc = _xmlSchemaBase64Decode(*cur))
3092 --cur;
3093 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
3094 /* 00110000 -> 0x30 */
3095 if (decc & ~0x30)
3096 goto return1;
3097 total += 1;
3098 } else
3099 goto return1;
3100
3101 if (val != NULL) {
3102 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
3103 if (v == NULL)
3104 goto error;
3105 base =
3106 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
3107 sizeof(xmlChar));
3108 if (base == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003109 xmlSchemaTypeErrMemory(node, "allocating base64 data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003110 xmlFree(v);
3111 goto return1;
3112 }
3113 v->value.base64.str = base;
3114 for (cur = value; *cur; ++cur)
3115 if (_xmlSchemaBase64Decode(*cur) >= 0) {
3116 *base = *cur;
3117 ++base;
3118 }
3119 *base = 0;
3120 v->value.base64.total = total;
3121 *val = v;
3122 }
3123 goto return0;
3124 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003125 case XML_SCHEMAS_INTEGER:
3126 case XML_SCHEMAS_PINTEGER:
3127 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003128 case XML_SCHEMAS_NINTEGER:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003129 case XML_SCHEMAS_NNINTEGER:{
3130 const xmlChar *cur = value;
3131 unsigned long lo, mi, hi;
William M. Brackec3b4b72005-03-15 15:50:17 +00003132 int sign = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003133
3134 if (cur == NULL)
3135 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003136 if (normOnTheFly)
3137 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003138 if (*cur == '-') {
3139 sign = 1;
3140 cur++;
3141 } else if (*cur == '+')
3142 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003143 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
Daniel Veillardbfc42632008-04-03 10:43:52 +00003144 if (ret < 0)
William M. Brackec3b4b72005-03-15 15:50:17 +00003145 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003146 if (normOnTheFly)
3147 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003148 if (*cur != 0)
3149 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003150 if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003151 if ((sign == 0) &&
3152 ((hi != 0) || (mi != 0) || (lo != 0)))
3153 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003154 } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003155 if (sign == 1)
3156 goto return1;
3157 if ((hi == 0) && (mi == 0) && (lo == 0))
3158 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003159 } else if (type->builtInType == XML_SCHEMAS_NINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003160 if (sign == 0)
3161 goto return1;
3162 if ((hi == 0) && (mi == 0) && (lo == 0))
3163 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003164 } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003165 if ((sign == 1) &&
3166 ((hi != 0) || (mi != 0) || (lo != 0)))
3167 goto return1;
3168 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003169 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003170 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003171 if (v != NULL) {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003172 if (ret == 0)
3173 ret++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003174 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003175 v->value.decimal.mi = mi;
3176 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003177 v->value.decimal.sign = sign;
3178 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003179 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003180 *val = v;
3181 }
3182 }
3183 goto return0;
3184 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003185 case XML_SCHEMAS_LONG:
3186 case XML_SCHEMAS_BYTE:
3187 case XML_SCHEMAS_SHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003188 case XML_SCHEMAS_INT:{
Daniel Veillardbfc42632008-04-03 10:43:52 +00003189 const xmlChar *cur = value;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003190 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003191 int sign = 0;
3192
3193 if (cur == NULL)
3194 goto return1;
3195 if (*cur == '-') {
3196 sign = 1;
3197 cur++;
3198 } else if (*cur == '+')
3199 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003200 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3201 if (ret < 0)
3202 goto return1;
3203 if (*cur != 0)
3204 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003205 if (type->builtInType == XML_SCHEMAS_LONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003206 if (hi >= 922) {
3207 if (hi > 922)
3208 goto return1;
3209 if (mi >= 33720368) {
3210 if (mi > 33720368)
3211 goto return1;
3212 if ((sign == 0) && (lo > 54775807))
3213 goto return1;
3214 if ((sign == 1) && (lo > 54775808))
3215 goto return1;
3216 }
3217 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003218 } else if (type->builtInType == XML_SCHEMAS_INT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003219 if (hi != 0)
3220 goto return1;
3221 if (mi >= 21) {
3222 if (mi > 21)
3223 goto return1;
3224 if ((sign == 0) && (lo > 47483647))
3225 goto return1;
3226 if ((sign == 1) && (lo > 47483648))
3227 goto return1;
3228 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003229 } else if (type->builtInType == XML_SCHEMAS_SHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003230 if ((mi != 0) || (hi != 0))
3231 goto return1;
3232 if ((sign == 1) && (lo > 32768))
3233 goto return1;
3234 if ((sign == 0) && (lo > 32767))
3235 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003236 } else if (type->builtInType == XML_SCHEMAS_BYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003237 if ((mi != 0) || (hi != 0))
3238 goto return1;
3239 if ((sign == 1) && (lo > 128))
3240 goto return1;
3241 if ((sign == 0) && (lo > 127))
3242 goto return1;
3243 }
3244 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003245 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003246 if (v != NULL) {
3247 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003248 v->value.decimal.mi = mi;
3249 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003250 v->value.decimal.sign = sign;
3251 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003252 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003253 *val = v;
3254 }
3255 }
3256 goto return0;
3257 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003258 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003259 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003260 case XML_SCHEMAS_USHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003261 case XML_SCHEMAS_UBYTE:{
3262 const xmlChar *cur = value;
3263 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003264
3265 if (cur == NULL)
3266 goto return1;
William M. Brackec3b4b72005-03-15 15:50:17 +00003267 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3268 if (ret < 0)
3269 goto return1;
3270 if (*cur != 0)
3271 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003272 if (type->builtInType == XML_SCHEMAS_ULONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003273 if (hi >= 1844) {
3274 if (hi > 1844)
3275 goto return1;
3276 if (mi >= 67440737) {
3277 if (mi > 67440737)
3278 goto return1;
3279 if (lo > 9551615)
3280 goto return1;
3281 }
3282 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003283 } else if (type->builtInType == XML_SCHEMAS_UINT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003284 if (hi != 0)
3285 goto return1;
3286 if (mi >= 42) {
3287 if (mi > 42)
3288 goto return1;
3289 if (lo > 94967295)
3290 goto return1;
3291 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003292 } else if (type->builtInType == XML_SCHEMAS_USHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003293 if ((mi != 0) || (hi != 0))
3294 goto return1;
3295 if (lo > 65535)
3296 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003297 } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003298 if ((mi != 0) || (hi != 0))
3299 goto return1;
3300 if (lo > 255)
3301 goto return1;
3302 }
3303 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003304 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003305 if (v != NULL) {
3306 v->value.decimal.lo = lo;
3307 v->value.decimal.mi = mi;
3308 v->value.decimal.hi = hi;
3309 v->value.decimal.sign = 0;
3310 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003311 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003312 *val = v;
3313 }
3314 }
3315 goto return0;
3316 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003317 }
3318
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003319 done:
3320 if (norm != NULL)
3321 xmlFree(norm);
3322 return (ret);
3323 return3:
3324 if (norm != NULL)
3325 xmlFree(norm);
3326 return (3);
3327 return1:
3328 if (norm != NULL)
3329 xmlFree(norm);
3330 return (1);
3331 return0:
3332 if (norm != NULL)
3333 xmlFree(norm);
3334 return (0);
3335 error:
3336 if (norm != NULL)
3337 xmlFree(norm);
3338 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003339}
3340
3341/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003342 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00003343 * @type: the predefined type
3344 * @value: the value to check
3345 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003346 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00003347 *
3348 * Check that a value conforms to the lexical space of the predefined type.
3349 * if true a value is computed and returned in @val.
3350 *
3351 * Returns 0 if this validates, a positive error code number otherwise
3352 * and -1 in case of internal or API error.
3353 */
3354int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003355xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
3356 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003357 return(xmlSchemaValAtomicType(type, value, val, node, 0,
3358 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00003359}
3360
3361/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00003362 * xmlSchemaValPredefTypeNodeNoNorm:
3363 * @type: the predefined type
3364 * @value: the value to check
3365 * @val: the return computed value
3366 * @node: the node containing the value
3367 *
3368 * Check that a value conforms to the lexical space of the predefined type.
3369 * if true a value is computed and returned in @val.
3370 * This one does apply any normalization to the value.
3371 *
3372 * Returns 0 if this validates, a positive error code number otherwise
3373 * and -1 in case of internal or API error.
3374 */
3375int
3376xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
3377 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003378 return(xmlSchemaValAtomicType(type, value, val, node, 1,
3379 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
Daniel Veillardc0826a72004-08-10 14:17:33 +00003380}
3381
3382/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003383 * xmlSchemaValidatePredefinedType:
3384 * @type: the predefined type
3385 * @value: the value to check
3386 * @val: the return computed value
3387 *
3388 * Check that a value conforms to the lexical space of the predefined type.
3389 * if true a value is computed and returned in @val.
3390 *
3391 * Returns 0 if this validates, a positive error code number otherwise
3392 * and -1 in case of internal or API error.
3393 */
3394int
3395xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
3396 xmlSchemaValPtr *val) {
3397 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
3398}
3399
3400/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003401 * xmlSchemaCompareDecimals:
3402 * @x: a first decimal value
3403 * @y: a second decimal value
3404 *
3405 * Compare 2 decimals
3406 *
3407 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
3408 */
3409static int
3410xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
3411{
3412 xmlSchemaValPtr swp;
William M. Brack273670f2005-03-11 15:55:14 +00003413 int order = 1, integx, integy, dlen;
3414 unsigned long hi, mi, lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003415
William M. Brack273670f2005-03-11 15:55:14 +00003416 /*
3417 * First test: If x is -ve and not zero
3418 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003419 if ((x->value.decimal.sign) &&
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003420 ((x->value.decimal.lo != 0) ||
3421 (x->value.decimal.mi != 0) ||
3422 (x->value.decimal.hi != 0))) {
William M. Brack273670f2005-03-11 15:55:14 +00003423 /*
3424 * Then if y is -ve and not zero reverse the compare
3425 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003426 if ((y->value.decimal.sign) &&
3427 ((y->value.decimal.lo != 0) ||
3428 (y->value.decimal.mi != 0) ||
3429 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00003430 order = -1;
William M. Brack273670f2005-03-11 15:55:14 +00003431 /*
3432 * Otherwise (y >= 0) we have the answer
3433 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003434 else
3435 return (-1);
William M. Brack273670f2005-03-11 15:55:14 +00003436 /*
3437 * If x is not -ve and y is -ve we have the answer
3438 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003439 } else if ((y->value.decimal.sign) &&
3440 ((y->value.decimal.lo != 0) ||
3441 (y->value.decimal.mi != 0) ||
3442 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003443 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00003444 }
William M. Brack273670f2005-03-11 15:55:14 +00003445 /*
3446 * If it's not simply determined by a difference in sign,
3447 * then we need to compare the actual values of the two nums.
3448 * To do this, we start by looking at the integral parts.
3449 * If the number of integral digits differ, then we have our
3450 * answer.
3451 */
3452 integx = x->value.decimal.total - x->value.decimal.frac;
3453 integy = y->value.decimal.total - y->value.decimal.frac;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003454 /*
3455 * NOTE: We changed the "total" for values like "0.1"
3456 * (or "-0.1" or ".1") to be 1, which was 2 previously.
3457 * Therefore the special case, when such values are
3458 * compared with 0, needs to be handled separately;
3459 * otherwise a zero would be recognized incorrectly as
3460 * greater than those values. This has the nice side effect
3461 * that we gain an overall optimized comparison with zeroes.
3462 * Note that a "0" has a "total" of 1 already.
3463 */
3464 if (integx == 1) {
3465 if (x->value.decimal.lo == 0) {
3466 if (integy != 1)
3467 return -order;
3468 else if (y->value.decimal.lo != 0)
3469 return -order;
3470 else
3471 return(0);
3472 }
3473 }
3474 if (integy == 1) {
3475 if (y->value.decimal.lo == 0) {
3476 if (integx != 1)
3477 return order;
3478 else if (x->value.decimal.lo != 0)
3479 return order;
3480 else
3481 return(0);
3482 }
3483 }
3484
William M. Brack273670f2005-03-11 15:55:14 +00003485 if (integx > integy)
3486 return order;
3487 else if (integy > integx)
3488 return -order;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003489
William M. Brack273670f2005-03-11 15:55:14 +00003490 /*
3491 * If the number of integral digits is the same for both numbers,
3492 * then things get a little more complicated. We need to "normalize"
3493 * the numbers in order to properly compare them. To do this, we
3494 * look at the total length of each number (length => number of
3495 * significant digits), and divide the "shorter" by 10 (decreasing
3496 * the length) until they are of equal length.
3497 */
3498 dlen = x->value.decimal.total - y->value.decimal.total;
3499 if (dlen < 0) { /* y has more digits than x */
3500 swp = x;
3501 hi = y->value.decimal.hi;
3502 mi = y->value.decimal.mi;
3503 lo = y->value.decimal.lo;
3504 dlen = -dlen;
3505 order = -order;
3506 } else { /* x has more digits than y */
3507 swp = y;
3508 hi = x->value.decimal.hi;
3509 mi = x->value.decimal.mi;
3510 lo = x->value.decimal.lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003511 }
William M. Brack273670f2005-03-11 15:55:14 +00003512 while (dlen > 8) { /* in effect, right shift by 10**8 */
3513 lo = mi;
3514 mi = hi;
3515 hi = 0;
3516 dlen -= 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00003517 }
William M. Brack273670f2005-03-11 15:55:14 +00003518 while (dlen > 0) {
3519 unsigned long rem1, rem2;
3520 rem1 = (hi % 10) * 100000000L;
3521 hi = hi / 10;
3522 rem2 = (mi % 10) * 100000000L;
3523 mi = (mi + rem1) / 10;
3524 lo = (lo + rem2) / 10;
3525 dlen--;
3526 }
3527 if (hi > swp->value.decimal.hi) {
3528 return order;
3529 } else if (hi == swp->value.decimal.hi) {
3530 if (mi > swp->value.decimal.mi) {
3531 return order;
3532 } else if (mi == swp->value.decimal.mi) {
3533 if (lo > swp->value.decimal.lo) {
3534 return order;
3535 } else if (lo == swp->value.decimal.lo) {
3536 if (x->value.decimal.total == y->value.decimal.total) {
3537 return 0;
3538 } else {
3539 return order;
3540 }
3541 }
3542 }
3543 }
3544 return -order;
Daniel Veillard4255d502002-04-16 15:50:10 +00003545}
3546
3547/**
Daniel Veillard070803b2002-05-03 07:29:38 +00003548 * xmlSchemaCompareDurations:
3549 * @x: a first duration value
3550 * @y: a second duration value
3551 *
3552 * Compare 2 durations
3553 *
3554 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3555 * case of error
3556 */
3557static int
3558xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
3559{
3560 long carry, mon, day;
3561 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00003562 int invert = 1;
3563 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00003564 static const long dayRange [2][12] = {
3565 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
3566 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
3567
3568 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00003569 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00003570
3571 /* months */
3572 mon = x->value.dur.mon - y->value.dur.mon;
3573
3574 /* seconds */
3575 sec = x->value.dur.sec - y->value.dur.sec;
Daniel Veillardbbcf1272011-11-10 23:23:10 +08003576 carry = (long)(sec / SECS_PER_DAY);
3577 sec -= ((double)carry) * SECS_PER_DAY;
Daniel Veillard070803b2002-05-03 07:29:38 +00003578
3579 /* days */
3580 day = x->value.dur.day - y->value.dur.day + carry;
3581
3582 /* easy test */
3583 if (mon == 0) {
3584 if (day == 0)
3585 if (sec == 0.0)
3586 return 0;
3587 else if (sec < 0.0)
3588 return -1;
3589 else
3590 return 1;
3591 else if (day < 0)
3592 return -1;
3593 else
3594 return 1;
3595 }
3596
3597 if (mon > 0) {
3598 if ((day >= 0) && (sec >= 0.0))
3599 return 1;
3600 else {
3601 xmon = mon;
3602 xday = -day;
3603 }
3604 } else if ((day <= 0) && (sec <= 0.0)) {
3605 return -1;
3606 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00003607 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00003608 xmon = -mon;
3609 xday = day;
3610 }
3611
3612 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00003613 if (myear == 0) {
3614 minday = 0;
3615 maxday = 0;
3616 } else {
3617 maxday = 366 * ((myear + 3) / 4) +
3618 365 * ((myear - 1) % 4);
3619 minday = maxday - 1;
3620 }
3621
Daniel Veillard070803b2002-05-03 07:29:38 +00003622 xmon = xmon % 12;
3623 minday += dayRange[0][xmon];
3624 maxday += dayRange[1][xmon];
3625
Daniel Veillard80b19092003-03-28 13:29:53 +00003626 if ((maxday == minday) && (maxday == xday))
3627 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00003628 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00003629 return(-invert);
3630 if (minday > xday)
3631 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00003632
3633 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00003634 return 2;
3635}
3636
3637/*
3638 * macros for adding date/times and durations
3639 */
3640#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
3641#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
3642#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
3643#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
3644
3645/**
Daniel Veillard669adfc2004-05-29 20:12:46 +00003646 * xmlSchemaDupVal:
3647 * @v: the #xmlSchemaValPtr value to duplicate
3648 *
3649 * Makes a copy of @v. The calling program is responsible for freeing
3650 * the returned value.
3651 *
3652 * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
3653 */
3654static xmlSchemaValPtr
3655xmlSchemaDupVal (xmlSchemaValPtr v)
3656{
3657 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
3658 if (ret == NULL)
3659 return NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003660
Daniel Veillard669adfc2004-05-29 20:12:46 +00003661 memcpy(ret, v, sizeof(xmlSchemaVal));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003662 ret->next = NULL;
Daniel Veillard669adfc2004-05-29 20:12:46 +00003663 return ret;
3664}
3665
3666/**
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003667 * xmlSchemaCopyValue:
3668 * @val: the precomputed value to be copied
3669 *
3670 * Copies the precomputed value. This duplicates any string within.
3671 *
3672 * Returns the copy or NULL if a copy for a data-type is not implemented.
3673 */
3674xmlSchemaValPtr
3675xmlSchemaCopyValue(xmlSchemaValPtr val)
3676{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003677 xmlSchemaValPtr ret = NULL, prev = NULL, cur;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003678
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003679 /*
3680 * Copy the string values.
3681 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003682 while (val != NULL) {
3683 switch (val->type) {
3684 case XML_SCHEMAS_ANYTYPE:
3685 case XML_SCHEMAS_IDREFS:
3686 case XML_SCHEMAS_ENTITIES:
3687 case XML_SCHEMAS_NMTOKENS:
3688 xmlSchemaFreeValue(ret);
3689 return (NULL);
3690 case XML_SCHEMAS_ANYSIMPLETYPE:
3691 case XML_SCHEMAS_STRING:
3692 case XML_SCHEMAS_NORMSTRING:
3693 case XML_SCHEMAS_TOKEN:
3694 case XML_SCHEMAS_LANGUAGE:
3695 case XML_SCHEMAS_NAME:
3696 case XML_SCHEMAS_NCNAME:
3697 case XML_SCHEMAS_ID:
3698 case XML_SCHEMAS_IDREF:
3699 case XML_SCHEMAS_ENTITY:
3700 case XML_SCHEMAS_NMTOKEN:
3701 case XML_SCHEMAS_ANYURI:
3702 cur = xmlSchemaDupVal(val);
3703 if (val->value.str != NULL)
3704 cur->value.str = xmlStrdup(BAD_CAST val->value.str);
3705 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003706 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003707 case XML_SCHEMAS_NOTATION:
3708 cur = xmlSchemaDupVal(val);
3709 if (val->value.qname.name != NULL)
3710 cur->value.qname.name =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003711 xmlStrdup(BAD_CAST val->value.qname.name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003712 if (val->value.qname.uri != NULL)
3713 cur->value.qname.uri =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003714 xmlStrdup(BAD_CAST val->value.qname.uri);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003715 break;
3716 case XML_SCHEMAS_HEXBINARY:
3717 cur = xmlSchemaDupVal(val);
3718 if (val->value.hex.str != NULL)
3719 cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
3720 break;
3721 case XML_SCHEMAS_BASE64BINARY:
3722 cur = xmlSchemaDupVal(val);
3723 if (val->value.base64.str != NULL)
3724 cur->value.base64.str =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003725 xmlStrdup(BAD_CAST val->value.base64.str);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003726 break;
3727 default:
3728 cur = xmlSchemaDupVal(val);
3729 break;
3730 }
3731 if (ret == NULL)
3732 ret = cur;
3733 else
3734 prev->next = cur;
3735 prev = cur;
3736 val = val->next;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003737 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003738 return (ret);
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003739}
3740
3741/**
Daniel Veillard5a872412002-05-22 06:40:27 +00003742 * _xmlSchemaDateAdd:
3743 * @dt: an #xmlSchemaValPtr
3744 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
3745 *
3746 * Compute a new date/time from @dt and @dur. This function assumes @dt
3747 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
Daniel Veillard669adfc2004-05-29 20:12:46 +00003748 * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
3749 * @dt. The calling program is responsible for freeing the returned value.
Daniel Veillard5a872412002-05-22 06:40:27 +00003750 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003751 * Returns a pointer to a new #xmlSchemaVal or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003752 */
3753static xmlSchemaValPtr
3754_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
3755{
Daniel Veillard669adfc2004-05-29 20:12:46 +00003756 xmlSchemaValPtr ret, tmp;
Daniel Veillard5a872412002-05-22 06:40:27 +00003757 long carry, tempdays, temp;
3758 xmlSchemaValDatePtr r, d;
3759 xmlSchemaValDurationPtr u;
3760
3761 if ((dt == NULL) || (dur == NULL))
3762 return NULL;
3763
3764 ret = xmlSchemaNewValue(dt->type);
3765 if (ret == NULL)
3766 return NULL;
3767
Daniel Veillard669adfc2004-05-29 20:12:46 +00003768 /* make a copy so we don't alter the original value */
3769 tmp = xmlSchemaDupVal(dt);
3770 if (tmp == NULL) {
3771 xmlSchemaFreeValue(ret);
3772 return NULL;
3773 }
3774
Daniel Veillard5a872412002-05-22 06:40:27 +00003775 r = &(ret->value.date);
Daniel Veillard669adfc2004-05-29 20:12:46 +00003776 d = &(tmp->value.date);
Daniel Veillard5a872412002-05-22 06:40:27 +00003777 u = &(dur->value.dur);
3778
3779 /* normalization */
3780 if (d->mon == 0)
3781 d->mon = 1;
3782
3783 /* normalize for time zone offset */
3784 u->sec -= (d->tzo * 60);
3785 d->tzo = 0;
3786
3787 /* normalization */
3788 if (d->day == 0)
3789 d->day = 1;
3790
3791 /* month */
3792 carry = d->mon + u->mon;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003793 r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
3794 carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003795
3796 /* year (may be modified later) */
3797 r->year = d->year + carry;
3798 if (r->year == 0) {
3799 if (d->year > 0)
3800 r->year--;
3801 else
3802 r->year++;
3803 }
3804
3805 /* time zone */
3806 r->tzo = d->tzo;
3807 r->tz_flag = d->tz_flag;
3808
3809 /* seconds */
3810 r->sec = d->sec + u->sec;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003811 carry = (long) FQUOTIENT((long)r->sec, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003812 if (r->sec != 0.0) {
3813 r->sec = MODULO(r->sec, 60.0);
3814 }
3815
3816 /* minute */
3817 carry += d->min;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003818 r->min = (unsigned int) MODULO(carry, 60);
3819 carry = (long) FQUOTIENT(carry, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003820
3821 /* hours */
3822 carry += d->hour;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003823 r->hour = (unsigned int) MODULO(carry, 24);
3824 carry = (long)FQUOTIENT(carry, 24);
Daniel Veillard5a872412002-05-22 06:40:27 +00003825
3826 /*
3827 * days
3828 * Note we use tempdays because the temporary values may need more
3829 * than 5 bits
3830 */
3831 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
3832 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
3833 tempdays = MAX_DAYINMONTH(r->year, r->mon);
3834 else if (d->day < 1)
3835 tempdays = 1;
3836 else
3837 tempdays = d->day;
3838
3839 tempdays += u->day + carry;
3840
3841 while (1) {
3842 if (tempdays < 1) {
Daniel Veillard5e9576a2005-11-21 11:23:47 +00003843 long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13);
3844 long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003845 if (tyr == 0)
3846 tyr--;
Daniel Veillard14b56432006-03-09 18:41:40 +00003847 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003848 * Coverity detected an overrun in daysInMonth
Daniel Veillard14b56432006-03-09 18:41:40 +00003849 * of size 12 at position 12 with index variable "((r)->mon - 1)"
3850 */
David Kilzer30cf4392014-07-14 22:29:56 +08003851 if (tmon < 1)
3852 tmon = 1;
Daniel Veillard14b56432006-03-09 18:41:40 +00003853 if (tmon > 12)
3854 tmon = 12;
Daniel Veillard5a872412002-05-22 06:40:27 +00003855 tempdays += MAX_DAYINMONTH(tyr, tmon);
3856 carry = -1;
Philip Withnall4ba5d312014-06-20 21:37:21 +01003857 } else if (VALID_YEAR(r->year) && VALID_MONTH(r->mon) &&
3858 tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003859 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
3860 carry = 1;
3861 } else
3862 break;
3863
3864 temp = r->mon + carry;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003865 r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
3866 r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003867 if (r->year == 0) {
3868 if (temp < 1)
3869 r->year--;
3870 else
3871 r->year++;
3872 }
3873 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003874
Daniel Veillard5a872412002-05-22 06:40:27 +00003875 r->day = tempdays;
3876
3877 /*
3878 * adjust the date/time type to the date values
3879 */
3880 if (ret->type != XML_SCHEMAS_DATETIME) {
3881 if ((r->hour) || (r->min) || (r->sec))
3882 ret->type = XML_SCHEMAS_DATETIME;
3883 else if (ret->type != XML_SCHEMAS_DATE) {
3884 if ((r->mon != 1) && (r->day != 1))
3885 ret->type = XML_SCHEMAS_DATE;
3886 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
3887 ret->type = XML_SCHEMAS_GYEARMONTH;
3888 }
3889 }
3890
Daniel Veillard669adfc2004-05-29 20:12:46 +00003891 xmlSchemaFreeValue(tmp);
Daniel Veillard5a872412002-05-22 06:40:27 +00003892
Daniel Veillard5a872412002-05-22 06:40:27 +00003893 return ret;
3894}
3895
3896/**
3897 * xmlSchemaDateNormalize:
Daniel Veillard669adfc2004-05-29 20:12:46 +00003898 * @dt: an #xmlSchemaValPtr of a date/time type value.
3899 * @offset: number of seconds to adjust @dt by.
Daniel Veillard5a872412002-05-22 06:40:27 +00003900 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003901 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3902 * the return value is a time-zone offset is present on @dt.
Daniel Veillard5a872412002-05-22 06:40:27 +00003903 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003904 * Returns a normalized copy of @dt or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003905 */
3906static xmlSchemaValPtr
3907xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
3908{
3909 xmlSchemaValPtr dur, ret;
3910
3911 if (dt == NULL)
3912 return NULL;
3913
3914 if (((dt->type != XML_SCHEMAS_TIME) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003915 (dt->type != XML_SCHEMAS_DATETIME) &&
3916 (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
Daniel Veillard5a872412002-05-22 06:40:27 +00003917 return xmlSchemaDupVal(dt);
3918
3919 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
3920 if (dur == NULL)
3921 return NULL;
3922
3923 dur->value.date.sec -= offset;
3924
3925 ret = _xmlSchemaDateAdd(dt, dur);
3926 if (ret == NULL)
3927 return NULL;
3928
3929 xmlSchemaFreeValue(dur);
3930
3931 /* ret->value.date.tzo = 0; */
3932 return ret;
3933}
3934
3935/**
3936 * _xmlSchemaDateCastYMToDays:
3937 * @dt: an #xmlSchemaValPtr
3938 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003939 * Convert mon and year of @dt to total number of days. Take the
Daniel Veillard5a872412002-05-22 06:40:27 +00003940 * number of years since (or before) 1 AD and add the number of leap
3941 * years. This is a function because negative
3942 * years must be handled a little differently and there is no zero year.
3943 *
3944 * Returns number of days.
3945 */
3946static long
3947_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
3948{
3949 long ret;
Daniel Veillard49e89632004-09-23 16:24:36 +00003950 int mon;
Daniel Veillard5a872412002-05-22 06:40:27 +00003951
Daniel Veillard49e89632004-09-23 16:24:36 +00003952 mon = dt->value.date.mon;
3953 if (mon <= 0) mon = 1; /* normalization */
3954
3955 if (dt->value.date.year <= 0)
Daniel Veillard5a872412002-05-22 06:40:27 +00003956 ret = (dt->value.date.year * 365) +
3957 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
3958 ((dt->value.date.year+1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003959 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003960 else
3961 ret = ((dt->value.date.year-1) * 365) +
3962 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
3963 ((dt->value.date.year-1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003964 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003965
3966 return ret;
3967}
3968
3969/**
3970 * TIME_TO_NUMBER:
3971 * @dt: an #xmlSchemaValPtr
3972 *
3973 * Calculates the number of seconds in the time portion of @dt.
3974 *
3975 * Returns seconds.
3976 */
3977#define TIME_TO_NUMBER(dt) \
3978 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00003979 (dt->value.date.min * SECS_PER_MIN) + \
3980 (dt->value.date.tzo * SECS_PER_MIN)) + \
3981 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00003982
3983/**
3984 * xmlSchemaCompareDates:
3985 * @x: a first date/time value
3986 * @y: a second date/time value
3987 *
3988 * Compare 2 date/times
3989 *
3990 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3991 * case of error
3992 */
3993static int
3994xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
3995{
3996 unsigned char xmask, ymask, xor_mask, and_mask;
3997 xmlSchemaValPtr p1, p2, q1, q2;
3998 long p1d, p2d, q1d, q2d;
3999
4000 if ((x == NULL) || (y == NULL))
4001 return -2;
4002
4003 if (x->value.date.tz_flag) {
4004
4005 if (!y->value.date.tz_flag) {
4006 p1 = xmlSchemaDateNormalize(x, 0);
4007 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4008 /* normalize y + 14:00 */
4009 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
4010
4011 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004012 if (p1d < q1d) {
4013 xmlSchemaFreeValue(p1);
4014 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004015 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004016 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004017 double sec;
4018
4019 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004020 if (sec < 0.0) {
4021 xmlSchemaFreeValue(p1);
4022 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004023 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004024 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004025 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004026 /* normalize y - 14:00 */
4027 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
4028 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
4029 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004030 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004031 else if (p1d == q2d) {
4032 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
4033 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004034 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004035 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004036 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004037 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004038 xmlSchemaFreeValue(p1);
4039 xmlSchemaFreeValue(q1);
4040 xmlSchemaFreeValue(q2);
4041 if (ret != 0)
4042 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004043 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004044 } else {
4045 xmlSchemaFreeValue(p1);
4046 xmlSchemaFreeValue(q1);
4047 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004048 }
4049 } else if (y->value.date.tz_flag) {
4050 q1 = xmlSchemaDateNormalize(y, 0);
4051 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4052
4053 /* normalize x - 14:00 */
4054 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
4055 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4056
Daniel Veillardfdc91562002-07-01 21:52:03 +00004057 if (p1d < q1d) {
4058 xmlSchemaFreeValue(p1);
4059 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004060 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004061 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004062 double sec;
4063
4064 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004065 if (sec < 0.0) {
4066 xmlSchemaFreeValue(p1);
4067 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004068 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004069 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004070 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004071 /* normalize x + 14:00 */
4072 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
4073 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
4074
Daniel Veillard6560a422003-03-27 21:25:38 +00004075 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004076 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00004077 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004078 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
4079 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004080 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004081 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004082 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004083 }
Daniel Veillard6560a422003-03-27 21:25:38 +00004084 xmlSchemaFreeValue(p1);
4085 xmlSchemaFreeValue(q1);
4086 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004087 if (ret != 0)
4088 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004089 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004090 } else {
4091 xmlSchemaFreeValue(p1);
4092 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004093 }
4094 }
4095
4096 /*
4097 * if the same type then calculate the difference
4098 */
4099 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004100 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004101 q1 = xmlSchemaDateNormalize(y, 0);
4102 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4103
4104 p1 = xmlSchemaDateNormalize(x, 0);
4105 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4106
Daniel Veillardfdc91562002-07-01 21:52:03 +00004107 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004108 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004109 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004110 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004111 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00004112 double sec;
4113
4114 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4115 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004116 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004117 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004118 ret = 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004119
Daniel Veillard5a872412002-05-22 06:40:27 +00004120 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004121 xmlSchemaFreeValue(p1);
4122 xmlSchemaFreeValue(q1);
4123 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004124 }
4125
4126 switch (x->type) {
4127 case XML_SCHEMAS_DATETIME:
4128 xmask = 0xf;
4129 break;
4130 case XML_SCHEMAS_DATE:
4131 xmask = 0x7;
4132 break;
4133 case XML_SCHEMAS_GYEAR:
4134 xmask = 0x1;
4135 break;
4136 case XML_SCHEMAS_GMONTH:
4137 xmask = 0x2;
4138 break;
4139 case XML_SCHEMAS_GDAY:
4140 xmask = 0x3;
4141 break;
4142 case XML_SCHEMAS_GYEARMONTH:
4143 xmask = 0x3;
4144 break;
4145 case XML_SCHEMAS_GMONTHDAY:
4146 xmask = 0x6;
4147 break;
4148 case XML_SCHEMAS_TIME:
4149 xmask = 0x8;
4150 break;
4151 default:
4152 xmask = 0;
4153 break;
4154 }
4155
4156 switch (y->type) {
4157 case XML_SCHEMAS_DATETIME:
4158 ymask = 0xf;
4159 break;
4160 case XML_SCHEMAS_DATE:
4161 ymask = 0x7;
4162 break;
4163 case XML_SCHEMAS_GYEAR:
4164 ymask = 0x1;
4165 break;
4166 case XML_SCHEMAS_GMONTH:
4167 ymask = 0x2;
4168 break;
4169 case XML_SCHEMAS_GDAY:
4170 ymask = 0x3;
4171 break;
4172 case XML_SCHEMAS_GYEARMONTH:
4173 ymask = 0x3;
4174 break;
4175 case XML_SCHEMAS_GMONTHDAY:
4176 ymask = 0x6;
4177 break;
4178 case XML_SCHEMAS_TIME:
4179 ymask = 0x8;
4180 break;
4181 default:
4182 ymask = 0;
4183 break;
4184 }
4185
4186 xor_mask = xmask ^ ymask; /* mark type differences */
4187 and_mask = xmask & ymask; /* mark field specification */
4188
4189 /* year */
4190 if (xor_mask & 1)
4191 return 2; /* indeterminate */
4192 else if (and_mask & 1) {
4193 if (x->value.date.year < y->value.date.year)
4194 return -1;
4195 else if (x->value.date.year > y->value.date.year)
4196 return 1;
4197 }
4198
4199 /* month */
4200 if (xor_mask & 2)
4201 return 2; /* indeterminate */
4202 else if (and_mask & 2) {
4203 if (x->value.date.mon < y->value.date.mon)
4204 return -1;
4205 else if (x->value.date.mon > y->value.date.mon)
4206 return 1;
4207 }
4208
4209 /* day */
4210 if (xor_mask & 4)
4211 return 2; /* indeterminate */
4212 else if (and_mask & 4) {
4213 if (x->value.date.day < y->value.date.day)
4214 return -1;
4215 else if (x->value.date.day > y->value.date.day)
4216 return 1;
4217 }
4218
4219 /* time */
4220 if (xor_mask & 8)
4221 return 2; /* indeterminate */
4222 else if (and_mask & 8) {
4223 if (x->value.date.hour < y->value.date.hour)
4224 return -1;
4225 else if (x->value.date.hour > y->value.date.hour)
4226 return 1;
4227 else if (x->value.date.min < y->value.date.min)
4228 return -1;
4229 else if (x->value.date.min > y->value.date.min)
4230 return 1;
4231 else if (x->value.date.sec < y->value.date.sec)
4232 return -1;
4233 else if (x->value.date.sec > y->value.date.sec)
4234 return 1;
4235 }
4236
Daniel Veillard070803b2002-05-03 07:29:38 +00004237 return 0;
4238}
4239
4240/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004241 * xmlSchemaComparePreserveReplaceStrings:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004242 * @x: a first string value
4243 * @y: a second string value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004244 * @invert: inverts the result if x < y or x > y.
4245 *
4246 * Compare 2 string for their normalized values.
4247 * @x is a string with whitespace of "preserve", @y is
4248 * a string with a whitespace of "replace". I.e. @x could
4249 * be an "xsd:string" and @y an "xsd:normalizedString".
4250 *
4251 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4252 * case of error
4253 */
4254static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004255xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
4256 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004257 int invert)
4258{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004259 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004260
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004261 while ((*x != 0) && (*y != 0)) {
4262 if (IS_WSP_REPLACE_CH(*y)) {
4263 if (! IS_WSP_SPACE_CH(*x)) {
4264 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004265 if (invert)
4266 return(1);
4267 else
4268 return(-1);
4269 } else {
4270 if (invert)
4271 return(-1);
4272 else
4273 return(1);
4274 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004275 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004276 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004277 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004278 if (tmp < 0) {
4279 if (invert)
4280 return(1);
4281 else
4282 return(-1);
4283 }
4284 if (tmp > 0) {
4285 if (invert)
4286 return(-1);
4287 else
4288 return(1);
4289 }
4290 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004291 x++;
4292 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004293 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004294 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004295 if (invert)
4296 return(-1);
4297 else
4298 return(1);
4299 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004300 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004301 if (invert)
4302 return(1);
4303 else
4304 return(-1);
4305 }
4306 return(0);
4307}
4308
4309/**
4310 * xmlSchemaComparePreserveCollapseStrings:
4311 * @x: a first string value
4312 * @y: a second string value
4313 *
4314 * Compare 2 string for their normalized values.
4315 * @x is a string with whitespace of "preserve", @y is
4316 * a string with a whitespace of "collapse". I.e. @x could
4317 * be an "xsd:string" and @y an "xsd:normalizedString".
4318 *
4319 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4320 * case of error
4321 */
4322static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004323xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
4324 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004325 int invert)
4326{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004327 int tmp;
4328
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004329 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004330 * Skip leading blank chars of the collapsed string.
4331 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004332 while IS_WSP_BLANK_CH(*y)
4333 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004334
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004335 while ((*x != 0) && (*y != 0)) {
4336 if IS_WSP_BLANK_CH(*y) {
4337 if (! IS_WSP_SPACE_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004338 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004339 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004340 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004341 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004342 if (invert)
4343 return(1);
4344 else
4345 return(-1);
4346 } else {
4347 if (invert)
4348 return(-1);
4349 else
4350 return(1);
4351 }
4352 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004353 x++;
4354 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004355 /*
4356 * Skip contiguous blank chars of the collapsed string.
4357 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004358 while IS_WSP_BLANK_CH(*y)
4359 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004360 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004361 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004362 if (tmp < 0) {
4363 if (invert)
4364 return(1);
4365 else
4366 return(-1);
4367 }
4368 if (tmp > 0) {
4369 if (invert)
4370 return(-1);
4371 else
4372 return(1);
4373 }
4374 }
4375 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004376 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004377 if (invert)
4378 return(-1);
4379 else
4380 return(1);
4381 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004382 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004383 /*
4384 * Skip trailing blank chars of the collapsed string.
4385 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004386 while IS_WSP_BLANK_CH(*y)
4387 y++;
4388 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004389 if (invert)
4390 return(1);
4391 else
4392 return(-1);
4393 }
4394 }
4395 return(0);
4396}
4397
4398/**
4399 * xmlSchemaComparePreserveCollapseStrings:
4400 * @x: a first string value
4401 * @y: a second string value
4402 *
4403 * Compare 2 string for their normalized values.
4404 * @x is a string with whitespace of "preserve", @y is
4405 * a string with a whitespace of "collapse". I.e. @x could
4406 * be an "xsd:string" and @y an "xsd:normalizedString".
4407 *
4408 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4409 * case of error
4410 */
4411static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004412xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
4413 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004414 int invert)
4415{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004416 int tmp;
4417
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004418 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004419 * Skip leading blank chars of the collapsed string.
4420 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004421 while IS_WSP_BLANK_CH(*y)
4422 y++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004423
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004424 while ((*x != 0) && (*y != 0)) {
4425 if IS_WSP_BLANK_CH(*y) {
4426 if (! IS_WSP_BLANK_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004427 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004428 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004429 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004430 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004431 if (invert)
4432 return(1);
4433 else
4434 return(-1);
4435 } else {
4436 if (invert)
4437 return(-1);
4438 else
4439 return(1);
4440 }
4441 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004442 x++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004443 y++;
4444 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004445 * Skip contiguous blank chars of the collapsed string.
4446 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004447 while IS_WSP_BLANK_CH(*y)
4448 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004449 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004450 if IS_WSP_BLANK_CH(*x) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004451 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004452 * The xv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004453 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004454 if ((0x20 - *y) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004455 if (invert)
4456 return(1);
4457 else
4458 return(-1);
4459 } else {
4460 if (invert)
4461 return(-1);
4462 else
4463 return(1);
4464 }
4465 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004466 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004467 if (tmp < 0)
4468 return(-1);
4469 if (tmp > 0)
4470 return(1);
4471 }
4472 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004473 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004474 if (invert)
4475 return(-1);
4476 else
4477 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004478 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004479 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004480 /*
4481 * Skip trailing blank chars of the collapsed string.
4482 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004483 while IS_WSP_BLANK_CH(*y)
4484 y++;
4485 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004486 if (invert)
4487 return(1);
4488 else
4489 return(-1);
4490 }
4491 }
4492 return(0);
4493}
4494
4495
4496/**
4497 * xmlSchemaCompareReplacedStrings:
4498 * @x: a first string value
4499 * @y: a second string value
4500 *
4501 * Compare 2 string for their normalized values.
4502 *
4503 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4504 * case of error
4505 */
4506static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004507xmlSchemaCompareReplacedStrings(const xmlChar *x,
4508 const xmlChar *y)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004509{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004510 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004511
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004512 while ((*x != 0) && (*y != 0)) {
4513 if IS_WSP_BLANK_CH(*y) {
4514 if (! IS_WSP_BLANK_CH(*x)) {
4515 if ((*x - 0x20) < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004516 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004517 else
4518 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004519 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004520 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004521 if IS_WSP_BLANK_CH(*x) {
4522 if ((0x20 - *y) < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004523 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004524 else
4525 return(1);
4526 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004527 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004528 if (tmp < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004529 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004530 if (tmp > 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004531 return(1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004532 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004533 x++;
4534 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004535 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004536 if (*x != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004537 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004538 if (*y != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004539 return(-1);
4540 return(0);
4541}
4542
4543/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004544 * xmlSchemaCompareNormStrings:
4545 * @x: a first string value
4546 * @y: a second string value
4547 *
4548 * Compare 2 string for their normalized values.
4549 *
4550 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4551 * case of error
4552 */
4553static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004554xmlSchemaCompareNormStrings(const xmlChar *x,
4555 const xmlChar *y) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00004556 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004557
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004558 while (IS_BLANK_CH(*x)) x++;
4559 while (IS_BLANK_CH(*y)) y++;
4560 while ((*x != 0) && (*y != 0)) {
4561 if (IS_BLANK_CH(*x)) {
4562 if (!IS_BLANK_CH(*y)) {
4563 tmp = *x - *y;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004564 return(tmp);
4565 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004566 while (IS_BLANK_CH(*x)) x++;
4567 while (IS_BLANK_CH(*y)) y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004568 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004569 tmp = *x++ - *y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004570 if (tmp < 0)
4571 return(-1);
4572 if (tmp > 0)
4573 return(1);
4574 }
4575 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004576 if (*x != 0) {
4577 while (IS_BLANK_CH(*x)) x++;
4578 if (*x != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004579 return(1);
4580 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004581 if (*y != 0) {
4582 while (IS_BLANK_CH(*y)) y++;
4583 if (*y != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004584 return(-1);
4585 }
4586 return(0);
4587}
4588
4589/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004590 * xmlSchemaCompareFloats:
4591 * @x: a first float or double value
4592 * @y: a second float or double value
4593 *
4594 * Compare 2 values
4595 *
4596 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4597 * case of error
4598 */
4599static int
4600xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4601 double d1, d2;
4602
4603 if ((x == NULL) || (y == NULL))
4604 return(-2);
4605
4606 /*
4607 * Cast everything to doubles.
4608 */
4609 if (x->type == XML_SCHEMAS_DOUBLE)
4610 d1 = x->value.d;
4611 else if (x->type == XML_SCHEMAS_FLOAT)
4612 d1 = x->value.f;
4613 else
4614 return(-2);
4615
4616 if (y->type == XML_SCHEMAS_DOUBLE)
4617 d2 = y->value.d;
4618 else if (y->type == XML_SCHEMAS_FLOAT)
4619 d2 = y->value.f;
4620 else
4621 return(-2);
4622
4623 /*
4624 * Check for special cases.
4625 */
4626 if (xmlXPathIsNaN(d1)) {
4627 if (xmlXPathIsNaN(d2))
4628 return(0);
4629 return(1);
4630 }
4631 if (xmlXPathIsNaN(d2))
4632 return(-1);
4633 if (d1 == xmlXPathPINF) {
4634 if (d2 == xmlXPathPINF)
4635 return(0);
4636 return(1);
4637 }
4638 if (d2 == xmlXPathPINF)
4639 return(-1);
4640 if (d1 == xmlXPathNINF) {
4641 if (d2 == xmlXPathNINF)
4642 return(0);
4643 return(-1);
4644 }
4645 if (d2 == xmlXPathNINF)
4646 return(1);
4647
4648 /*
4649 * basic tests, the last one we should have equality, but
4650 * portability is more important than speed and handling
4651 * NaN or Inf in a portable way is always a challenge, so ...
4652 */
4653 if (d1 < d2)
4654 return(-1);
4655 if (d1 > d2)
4656 return(1);
4657 if (d1 == d2)
4658 return(0);
4659 return(2);
4660}
4661
4662/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004663 * xmlSchemaCompareValues:
4664 * @x: a first value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004665 * @xvalue: the first value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004666 * @xwtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004667 * @y: a second value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004668 * @xvalue: the second value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004669 * @ywtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004670 *
4671 * Compare 2 values
4672 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004673 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4674 * comparable and -2 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004675 */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004676static int
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004677xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
4678 xmlSchemaValPtr x,
4679 const xmlChar *xvalue,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004680 xmlSchemaWhitespaceValueType xws,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004681 xmlSchemaValType ytype,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004682 xmlSchemaValPtr y,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004683 const xmlChar *yvalue,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004684 xmlSchemaWhitespaceValueType yws)
4685{
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004686 switch (xtype) {
Daniel Veillard80b19092003-03-28 13:29:53 +00004687 case XML_SCHEMAS_UNKNOWN:
William M. Brack2f2a6632004-08-20 23:09:47 +00004688 case XML_SCHEMAS_ANYTYPE:
Daniel Veillard80b19092003-03-28 13:29:53 +00004689 return(-2);
4690 case XML_SCHEMAS_INTEGER:
4691 case XML_SCHEMAS_NPINTEGER:
4692 case XML_SCHEMAS_NINTEGER:
4693 case XML_SCHEMAS_NNINTEGER:
4694 case XML_SCHEMAS_PINTEGER:
4695 case XML_SCHEMAS_INT:
4696 case XML_SCHEMAS_UINT:
4697 case XML_SCHEMAS_LONG:
4698 case XML_SCHEMAS_ULONG:
4699 case XML_SCHEMAS_SHORT:
4700 case XML_SCHEMAS_USHORT:
4701 case XML_SCHEMAS_BYTE:
4702 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00004703 case XML_SCHEMAS_DECIMAL:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004704 if ((x == NULL) || (y == NULL))
4705 return(-2);
4706 if (ytype == xtype)
Daniel Veillard80b19092003-03-28 13:29:53 +00004707 return(xmlSchemaCompareDecimals(x, y));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004708 if ((ytype == XML_SCHEMAS_DECIMAL) ||
4709 (ytype == XML_SCHEMAS_INTEGER) ||
4710 (ytype == XML_SCHEMAS_NPINTEGER) ||
4711 (ytype == XML_SCHEMAS_NINTEGER) ||
4712 (ytype == XML_SCHEMAS_NNINTEGER) ||
4713 (ytype == XML_SCHEMAS_PINTEGER) ||
4714 (ytype == XML_SCHEMAS_INT) ||
4715 (ytype == XML_SCHEMAS_UINT) ||
4716 (ytype == XML_SCHEMAS_LONG) ||
4717 (ytype == XML_SCHEMAS_ULONG) ||
4718 (ytype == XML_SCHEMAS_SHORT) ||
4719 (ytype == XML_SCHEMAS_USHORT) ||
4720 (ytype == XML_SCHEMAS_BYTE) ||
4721 (ytype == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00004722 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004723 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00004724 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004725 if ((x == NULL) || (y == NULL))
4726 return(-2);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004727 if (ytype == XML_SCHEMAS_DURATION)
Daniel Veillard070803b2002-05-03 07:29:38 +00004728 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004729 return(-2);
4730 case XML_SCHEMAS_TIME:
4731 case XML_SCHEMAS_GDAY:
4732 case XML_SCHEMAS_GMONTH:
4733 case XML_SCHEMAS_GMONTHDAY:
4734 case XML_SCHEMAS_GYEAR:
4735 case XML_SCHEMAS_GYEARMONTH:
4736 case XML_SCHEMAS_DATE:
4737 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004738 if ((x == NULL) || (y == NULL))
4739 return(-2);
4740 if ((ytype == XML_SCHEMAS_DATETIME) ||
4741 (ytype == XML_SCHEMAS_TIME) ||
4742 (ytype == XML_SCHEMAS_GDAY) ||
4743 (ytype == XML_SCHEMAS_GMONTH) ||
4744 (ytype == XML_SCHEMAS_GMONTHDAY) ||
4745 (ytype == XML_SCHEMAS_GYEAR) ||
4746 (ytype == XML_SCHEMAS_DATE) ||
4747 (ytype == XML_SCHEMAS_GYEARMONTH))
Daniel Veillard5a872412002-05-22 06:40:27 +00004748 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004749 return (-2);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004750 /*
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +00004751 * Note that we will support comparison of string types against
4752 * anySimpleType as well.
4753 */
4754 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004755 case XML_SCHEMAS_STRING:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004756 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00004757 case XML_SCHEMAS_TOKEN:
4758 case XML_SCHEMAS_LANGUAGE:
4759 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00004760 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00004761 case XML_SCHEMAS_NCNAME:
4762 case XML_SCHEMAS_ID:
4763 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00004764 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00004765 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004766 {
4767 const xmlChar *xv, *yv;
4768
4769 if (x == NULL)
4770 xv = xvalue;
4771 else
4772 xv = x->value.str;
4773 if (y == NULL)
4774 yv = yvalue;
4775 else
4776 yv = y->value.str;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004777 /*
4778 * TODO: Compare those against QName.
4779 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004780 if (ytype == XML_SCHEMAS_QNAME) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004781 TODO
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004782 if (y == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004783 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004784 return (-2);
4785 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004786 if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
4787 (ytype == XML_SCHEMAS_STRING) ||
4788 (ytype == XML_SCHEMAS_NORMSTRING) ||
4789 (ytype == XML_SCHEMAS_TOKEN) ||
4790 (ytype == XML_SCHEMAS_LANGUAGE) ||
4791 (ytype == XML_SCHEMAS_NMTOKEN) ||
4792 (ytype == XML_SCHEMAS_NAME) ||
4793 (ytype == XML_SCHEMAS_NCNAME) ||
4794 (ytype == XML_SCHEMAS_ID) ||
4795 (ytype == XML_SCHEMAS_IDREF) ||
4796 (ytype == XML_SCHEMAS_ENTITY) ||
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004797 (ytype == XML_SCHEMAS_ANYURI)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004798
4799 if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4800
4801 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4802 /* TODO: What about x < y or x > y. */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004803 if (xmlStrEqual(xv, yv))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004804 return (0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004805 else
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004806 return (2);
4807 } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004808 return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004809 else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004810 return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004811
4812 } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
4813
4814 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004815 return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004816 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004817 return (xmlSchemaCompareReplacedStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004818 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004819 return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004820
4821 } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
4822
4823 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004824 return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004825 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004826 return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004827 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004828 return (xmlSchemaCompareNormStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004829 } else
4830 return (-2);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004831
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004832 }
Daniel Veillardc4c21552003-03-29 10:53:38 +00004833 return (-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004834 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004835 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004836 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004837 if ((x == NULL) || (y == NULL))
4838 return(-2);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004839 if ((ytype == XML_SCHEMAS_QNAME) ||
4840 (ytype == XML_SCHEMAS_NOTATION)) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004841 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
4842 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
4843 return(0);
4844 return(2);
4845 }
4846 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004847 case XML_SCHEMAS_FLOAT:
4848 case XML_SCHEMAS_DOUBLE:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004849 if ((x == NULL) || (y == NULL))
4850 return(-2);
4851 if ((ytype == XML_SCHEMAS_FLOAT) ||
4852 (ytype == XML_SCHEMAS_DOUBLE))
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004853 return (xmlSchemaCompareFloats(x, y));
4854 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004855 case XML_SCHEMAS_BOOLEAN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004856 if ((x == NULL) || (y == NULL))
4857 return(-2);
4858 if (ytype == XML_SCHEMAS_BOOLEAN) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004859 if (x->value.b == y->value.b)
4860 return(0);
4861 if (x->value.b == 0)
4862 return(-1);
4863 return(1);
4864 }
4865 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00004866 case XML_SCHEMAS_HEXBINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004867 if ((x == NULL) || (y == NULL))
4868 return(-2);
4869 if (ytype == XML_SCHEMAS_HEXBINARY) {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00004870 if (x->value.hex.total == y->value.hex.total) {
4871 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
4872 if (ret > 0)
4873 return(1);
4874 else if (ret == 0)
4875 return(0);
4876 }
4877 else if (x->value.hex.total > y->value.hex.total)
4878 return(1);
4879
4880 return(-1);
4881 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00004882 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004883 case XML_SCHEMAS_BASE64BINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004884 if ((x == NULL) || (y == NULL))
4885 return(-2);
4886 if (ytype == XML_SCHEMAS_BASE64BINARY) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004887 if (x->value.base64.total == y->value.base64.total) {
4888 int ret = xmlStrcmp(x->value.base64.str,
4889 y->value.base64.str);
4890 if (ret > 0)
4891 return(1);
4892 else if (ret == 0)
4893 return(0);
Daniel Veillardd369b132005-07-14 15:54:44 +00004894 else
4895 return(-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004896 }
4897 else if (x->value.base64.total > y->value.base64.total)
4898 return(1);
4899 else
4900 return(-1);
4901 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004902 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004903 case XML_SCHEMAS_IDREFS:
4904 case XML_SCHEMAS_ENTITIES:
4905 case XML_SCHEMAS_NMTOKENS:
4906 TODO
4907 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004908 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004909 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00004910}
4911
4912/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004913 * xmlSchemaCompareValues:
4914 * @x: a first value
4915 * @y: a second value
4916 *
4917 * Compare 2 values
4918 *
4919 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4920 * case of error
4921 */
4922int
4923xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4924 xmlSchemaWhitespaceValueType xws, yws;
4925
Daniel Veillard5e094142005-02-18 19:36:12 +00004926 if ((x == NULL) || (y == NULL))
4927 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004928 if (x->type == XML_SCHEMAS_STRING)
4929 xws = XML_SCHEMA_WHITESPACE_PRESERVE;
4930 else if (x->type == XML_SCHEMAS_NORMSTRING)
4931 xws = XML_SCHEMA_WHITESPACE_REPLACE;
4932 else
4933 xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4934
4935 if (y->type == XML_SCHEMAS_STRING)
4936 yws = XML_SCHEMA_WHITESPACE_PRESERVE;
Gaurave79226c2013-11-28 22:50:57 +08004937 else if (y->type == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004938 yws = XML_SCHEMA_WHITESPACE_REPLACE;
4939 else
4940 yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4941
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004942 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4943 y, NULL, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004944}
4945
4946/**
4947 * xmlSchemaCompareValuesWhtsp:
4948 * @x: a first value
4949 * @xws: the whitespace value of x
4950 * @y: a second value
4951 * @yws: the whitespace value of y
4952 *
4953 * Compare 2 values
4954 *
4955 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4956 * case of error
4957 */
4958int
4959xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004960 xmlSchemaWhitespaceValueType xws,
4961 xmlSchemaValPtr y,
4962 xmlSchemaWhitespaceValueType yws)
4963{
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00004964 if ((x == NULL) || (y == NULL))
4965 return(-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004966 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4967 y, NULL, yws));
4968}
4969
4970/**
4971 * xmlSchemaCompareValuesWhtspExt:
4972 * @x: a first value
4973 * @xws: the whitespace value of x
4974 * @y: a second value
4975 * @yws: the whitespace value of y
4976 *
4977 * Compare 2 values
4978 *
4979 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4980 * case of error
4981 */
4982static int
4983xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
4984 xmlSchemaValPtr x,
4985 const xmlChar *xvalue,
4986 xmlSchemaWhitespaceValueType xws,
4987 xmlSchemaValType ytype,
4988 xmlSchemaValPtr y,
4989 const xmlChar *yvalue,
4990 xmlSchemaWhitespaceValueType yws)
4991{
4992 return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
4993 yvalue, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004994}
4995
4996/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004997 * xmlSchemaNormLen:
4998 * @value: a string
4999 *
5000 * Computes the UTF8 length of the normalized value of the string
5001 *
5002 * Returns the length or -1 in case of error.
5003 */
5004static int
5005xmlSchemaNormLen(const xmlChar *value) {
5006 const xmlChar *utf;
5007 int ret = 0;
5008
5009 if (value == NULL)
5010 return(-1);
5011 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00005012 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00005013 while (*utf != 0) {
5014 if (utf[0] & 0x80) {
5015 if ((utf[1] & 0xc0) != 0x80)
5016 return(-1);
5017 if ((utf[0] & 0xe0) == 0xe0) {
5018 if ((utf[2] & 0xc0) != 0x80)
5019 return(-1);
5020 if ((utf[0] & 0xf0) == 0xf0) {
5021 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
5022 return(-1);
5023 utf += 4;
5024 } else {
5025 utf += 3;
5026 }
5027 } else {
5028 utf += 2;
5029 }
William M. Brack76e95df2003-10-18 16:20:14 +00005030 } else if (IS_BLANK_CH(*utf)) {
5031 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00005032 if (*utf == 0)
5033 break;
5034 } else {
5035 utf++;
5036 }
5037 ret++;
5038 }
5039 return(ret);
5040}
5041
Daniel Veillard6927b102004-10-27 17:29:04 +00005042/**
5043 * xmlSchemaGetFacetValueAsULong:
5044 * @facet: an schemas type facet
5045 *
5046 * Extract the value of a facet
5047 *
5048 * Returns the value as a long
5049 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005050unsigned long
5051xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
5052{
5053 /*
5054 * TODO: Check if this is a decimal.
5055 */
William M. Brack094dd862004-11-14 14:28:34 +00005056 if (facet == NULL)
5057 return 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005058 return ((unsigned long) facet->val->value.decimal.lo);
5059}
5060
Daniel Veillardc4c21552003-03-29 10:53:38 +00005061/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00005062 * xmlSchemaValidateListSimpleTypeFacet:
5063 * @facet: the facet to check
5064 * @value: the lexical repr of the value to validate
5065 * @actualLen: the number of list items
5066 * @expectedLen: the resulting expected number of list items
5067 *
5068 * Checks the value of a list simple type against a facet.
5069 *
5070 * Returns 0 if the value is valid, a positive error code
5071 * number otherwise and -1 in case of an internal error.
5072 */
5073int
5074xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
5075 const xmlChar *value,
5076 unsigned long actualLen,
5077 unsigned long *expectedLen)
5078{
Daniel Veillardce682bc2004-11-05 17:22:25 +00005079 if (facet == NULL)
5080 return(-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005081 /*
5082 * TODO: Check if this will work with large numbers.
5083 * (compare value.decimal.mi and value.decimal.hi as well?).
5084 */
5085 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5086 if (actualLen != facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005087 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005088 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005089 return (XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005090 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005091 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5092 if (actualLen < facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005093 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005094 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005095 return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
5096 }
5097 } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
5098 if (actualLen > facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005099 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005100 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005101 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5102 }
5103 } else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005104 /*
5105 * NOTE: That we can pass NULL as xmlSchemaValPtr to
Daniel Veillard01fa6152004-06-29 17:04:39 +00005106 * xmlSchemaValidateFacet, since the remaining facet types
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005107 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
Daniel Veillard01fa6152004-06-29 17:04:39 +00005108 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005109 return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
Daniel Veillard01fa6152004-06-29 17:04:39 +00005110 return (0);
5111}
5112
5113/**
Daniel Veillard6927b102004-10-27 17:29:04 +00005114 * xmlSchemaValidateLengthFacet:
Daniel Veillardc0826a72004-08-10 14:17:33 +00005115 * @type: the built-in type
5116 * @facet: the facet to check
5117 * @value: the lexical repr. of the value to be validated
5118 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005119 * @ws: the whitespace type of the value
5120 * @length: the actual length of the value
5121 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005122 * Checka a value against a "length", "minLength" and "maxLength"
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005123 * facet; sets @length to the computed length of @value.
5124 *
5125 * Returns 0 if the value is valid, a positive error code
5126 * otherwise and -1 in case of an internal or API error.
5127 */
5128static int
5129xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
Nico Weberaae48e62012-02-29 09:44:35 +08005130 xmlSchemaValType valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005131 const xmlChar *value,
Nico Weberaae48e62012-02-29 09:44:35 +08005132 xmlSchemaValPtr val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005133 unsigned long *length,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005134 xmlSchemaWhitespaceValueType ws)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005135{
5136 unsigned int len = 0;
5137
5138 if ((length == NULL) || (facet == NULL))
5139 return (-1);
5140 *length = 0;
5141 if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
5142 (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
5143 (facet->type != XML_SCHEMA_FACET_MINLENGTH))
5144 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005145
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005146 /*
5147 * TODO: length, maxLength and minLength must be of type
5148 * nonNegativeInteger only. Check if decimal is used somehow.
5149 */
5150 if ((facet->val == NULL) ||
5151 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5152 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5153 (facet->val->value.decimal.frac != 0)) {
5154 return(-1);
5155 }
5156 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
5157 len = val->value.hex.total;
5158 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5159 len = val->value.base64.total;
5160 else {
5161 switch (valType) {
5162 case XML_SCHEMAS_STRING:
5163 case XML_SCHEMAS_NORMSTRING:
5164 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5165 /*
5166 * This is to ensure API compatibility with the old
5167 * xmlSchemaValidateLengthFacet(). Anyway, this was and
5168 * is not the correct handling.
5169 * TODO: Get rid of this case somehow.
5170 */
5171 if (valType == XML_SCHEMAS_STRING)
5172 len = xmlUTF8Strlen(value);
5173 else
5174 len = xmlSchemaNormLen(value);
5175 } else if (value != NULL) {
5176 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5177 len = xmlSchemaNormLen(value);
5178 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005179 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005180 * Should be OK for "preserve" as well.
5181 */
5182 len = xmlUTF8Strlen(value);
5183 }
5184 break;
5185 case XML_SCHEMAS_IDREF:
5186 case XML_SCHEMAS_TOKEN:
5187 case XML_SCHEMAS_LANGUAGE:
5188 case XML_SCHEMAS_NMTOKEN:
5189 case XML_SCHEMAS_NAME:
5190 case XML_SCHEMAS_NCNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005191 case XML_SCHEMAS_ID:
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005192 /*
5193 * FIXME: What exactly to do with anyURI?
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005194 */
5195 case XML_SCHEMAS_ANYURI:
5196 if (value != NULL)
5197 len = xmlSchemaNormLen(value);
5198 break;
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005199 case XML_SCHEMAS_QNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005200 case XML_SCHEMAS_NOTATION:
5201 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005202 * For QName and NOTATION, those facets are
5203 * deprecated and should be ignored.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005204 */
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005205 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005206 default:
5207 TODO
5208 }
5209 }
5210 *length = (unsigned long) len;
5211 /*
5212 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
5213 */
5214 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5215 if (len != facet->val->value.decimal.lo)
5216 return(XML_SCHEMAV_CVC_LENGTH_VALID);
5217 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5218 if (len < facet->val->value.decimal.lo)
5219 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
5220 } else {
5221 if (len > facet->val->value.decimal.lo)
5222 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5223 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005224
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005225 return (0);
5226}
5227
5228/**
5229 * xmlSchemaValidateLengthFacet:
5230 * @type: the built-in type
5231 * @facet: the facet to check
5232 * @value: the lexical repr. of the value to be validated
5233 * @val: the precomputed value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005234 * @length: the actual length of the value
5235 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005236 * Checka a value against a "length", "minLength" and "maxLength"
Daniel Veillardc0826a72004-08-10 14:17:33 +00005237 * facet; sets @length to the computed length of @value.
5238 *
5239 * Returns 0 if the value is valid, a positive error code
5240 * otherwise and -1 in case of an internal or API error.
5241 */
5242int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005243xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005244 xmlSchemaFacetPtr facet,
5245 const xmlChar *value,
5246 xmlSchemaValPtr val,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005247 unsigned long *length)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005248{
Daniel Veillardcc5e2332005-03-16 21:55:35 +00005249 if (type == NULL)
5250 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005251 return (xmlSchemaValidateLengthFacetInternal(facet,
5252 type->builtInType, value, val, length,
5253 XML_SCHEMA_WHITESPACE_UNKNOWN));
5254}
Daniel Veillardc0826a72004-08-10 14:17:33 +00005255
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005256/**
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005257 * xmlSchemaValidateLengthFacetWhtsp:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005258 * @facet: the facet to check
5259 * @valType: the built-in type
5260 * @value: the lexical repr. of the value to be validated
5261 * @val: the precomputed value
5262 * @ws: the whitespace type of the value
5263 * @length: the actual length of the value
5264 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005265 * Checka a value against a "length", "minLength" and "maxLength"
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005266 * facet; sets @length to the computed length of @value.
5267 *
5268 * Returns 0 if the value is valid, a positive error code
5269 * otherwise and -1 in case of an internal or API error.
5270 */
5271int
5272xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
5273 xmlSchemaValType valType,
5274 const xmlChar *value,
5275 xmlSchemaValPtr val,
5276 unsigned long *length,
5277 xmlSchemaWhitespaceValueType ws)
5278{
5279 return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
5280 length, ws));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005281}
5282
5283/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005284 * xmlSchemaValidateFacetInternal:
Daniel Veillard4255d502002-04-16 15:50:10 +00005285 * @facet: the facet to check
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005286 * @fws: the whitespace type of the facet's value
5287 * @valType: the built-in type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005288 * @value: the lexical repr of the value to validate
5289 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005290 * @ws: the whitespace type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005291 *
5292 * Check a value against a facet condition
5293 *
5294 * Returns 0 if the element is schemas valid, a positive error code
5295 * number otherwise and -1 in case of internal or API error.
5296 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005297static int
5298xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
5299 xmlSchemaWhitespaceValueType fws,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005300 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005301 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005302 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005303 xmlSchemaWhitespaceValueType ws)
Daniel Veillard4255d502002-04-16 15:50:10 +00005304{
5305 int ret;
5306
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005307 if (facet == NULL)
5308 return(-1);
5309
Daniel Veillard4255d502002-04-16 15:50:10 +00005310 switch (facet->type) {
5311 case XML_SCHEMA_FACET_PATTERN:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005312 /*
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005313 * NOTE that for patterns, the @value needs to be the normalized
5314 * value, *not* the lexical initial value or the canonical value.
5315 */
5316 if (value == NULL)
5317 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005318 ret = xmlRegexpExec(facet->regexp, value);
5319 if (ret == 1)
5320 return(0);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005321 if (ret == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005322 return(XML_SCHEMAV_CVC_PATTERN_VALID);
Daniel Veillard4255d502002-04-16 15:50:10 +00005323 return(ret);
5324 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
5325 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005326 if (ret == -2)
Daniel Veillard4255d502002-04-16 15:50:10 +00005327 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005328 if (ret == -1)
5329 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005330 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005331 case XML_SCHEMA_FACET_MAXINCLUSIVE:
5332 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005333 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005334 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005335 if ((ret == -1) || (ret == 0))
5336 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005337 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005338 case XML_SCHEMA_FACET_MINEXCLUSIVE:
5339 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005340 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005341 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005342 if (ret == 1)
5343 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005344 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005345 case XML_SCHEMA_FACET_MININCLUSIVE:
5346 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005347 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005348 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005349 if ((ret == 1) || (ret == 0))
5350 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005351 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
Daniel Veillard8651f532002-04-17 09:06:27 +00005352 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005353 /* TODO whitespaces */
Daniel Veillard01fa6152004-06-29 17:04:39 +00005354 /*
5355 * NOTE: Whitespace should be handled to normalize
5356 * the value to be validated against a the facets;
5357 * not to normalize the value in-between.
5358 */
Daniel Veillard8651f532002-04-17 09:06:27 +00005359 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00005360 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005361 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5362 /*
5363 * This is to ensure API compatibility with the old
5364 * xmlSchemaValidateFacet().
5365 * TODO: Get rid of this case.
5366 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005367 if ((facet->value != NULL) &&
5368 (xmlStrEqual(facet->value, value)))
5369 return(0);
5370 } else {
5371 ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
5372 facet->val, facet->value, fws, valType, val,
5373 value, ws);
5374 if (ret == -2)
5375 return(-1);
5376 if (ret == 0)
5377 return(0);
5378 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005379 return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005380 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005381 /*
5382 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5383 * then any {value} is facet-valid."
5384 */
5385 if ((valType == XML_SCHEMAS_QNAME) ||
5386 (valType == XML_SCHEMAS_NOTATION))
5387 return (0);
5388 /* No break on purpose. */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005389 case XML_SCHEMA_FACET_MAXLENGTH:
5390 case XML_SCHEMA_FACET_MINLENGTH: {
5391 unsigned int len = 0;
5392
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005393 if ((valType == XML_SCHEMAS_QNAME) ||
5394 (valType == XML_SCHEMAS_NOTATION))
5395 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005396 /*
5397 * TODO: length, maxLength and minLength must be of type
5398 * nonNegativeInteger only. Check if decimal is used somehow.
5399 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005400 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005401 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5402 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005403 (facet->val->value.decimal.frac != 0)) {
5404 return(-1);
5405 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005406 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005407 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00005408 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5409 len = val->value.base64.total;
5410 else {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005411 switch (valType) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005412 case XML_SCHEMAS_STRING:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005413 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005414 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5415 /*
5416 * This is to ensure API compatibility with the old
5417 * xmlSchemaValidateFacet(). Anyway, this was and
5418 * is not the correct handling.
5419 * TODO: Get rid of this case somehow.
5420 */
5421 if (valType == XML_SCHEMAS_STRING)
5422 len = xmlUTF8Strlen(value);
5423 else
5424 len = xmlSchemaNormLen(value);
5425 } else if (value != NULL) {
5426 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5427 len = xmlSchemaNormLen(value);
5428 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005429 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005430 * Should be OK for "preserve" as well.
5431 */
5432 len = xmlUTF8Strlen(value);
5433 }
5434 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005435 case XML_SCHEMAS_IDREF:
Daniel Veillard560c2a42003-07-06 21:13:49 +00005436 case XML_SCHEMAS_TOKEN:
5437 case XML_SCHEMAS_LANGUAGE:
5438 case XML_SCHEMAS_NMTOKEN:
5439 case XML_SCHEMAS_NAME:
5440 case XML_SCHEMAS_NCNAME:
5441 case XML_SCHEMAS_ID:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005442 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005443 if (value != NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005444 len = xmlSchemaNormLen(value);
5445 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00005446 default:
5447 TODO
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005448 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005449 }
5450 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005451 if (len != facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005452 return(XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005453 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005454 if (len < facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005455 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005456 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005457 if (len > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005458 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005459 }
5460 break;
5461 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005462 case XML_SCHEMA_FACET_TOTALDIGITS:
5463 case XML_SCHEMA_FACET_FRACTIONDIGITS:
5464
5465 if ((facet->val == NULL) ||
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00005466 ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
Daniel Veillard560c2a42003-07-06 21:13:49 +00005467 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5468 (facet->val->value.decimal.frac != 0)) {
5469 return(-1);
5470 }
5471 if ((val == NULL) ||
5472 ((val->type != XML_SCHEMAS_DECIMAL) &&
5473 (val->type != XML_SCHEMAS_INTEGER) &&
5474 (val->type != XML_SCHEMAS_NPINTEGER) &&
5475 (val->type != XML_SCHEMAS_NINTEGER) &&
5476 (val->type != XML_SCHEMAS_NNINTEGER) &&
5477 (val->type != XML_SCHEMAS_PINTEGER) &&
5478 (val->type != XML_SCHEMAS_INT) &&
5479 (val->type != XML_SCHEMAS_UINT) &&
5480 (val->type != XML_SCHEMAS_LONG) &&
5481 (val->type != XML_SCHEMAS_ULONG) &&
5482 (val->type != XML_SCHEMAS_SHORT) &&
5483 (val->type != XML_SCHEMAS_USHORT) &&
5484 (val->type != XML_SCHEMAS_BYTE) &&
5485 (val->type != XML_SCHEMAS_UBYTE))) {
5486 return(-1);
5487 }
5488 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
5489 if (val->value.decimal.total > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005490 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005491
5492 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
5493 if (val->value.decimal.frac > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005494 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005495 }
5496 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00005497 default:
5498 TODO
5499 }
5500 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00005501
Daniel Veillard4255d502002-04-16 15:50:10 +00005502}
5503
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005504/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005505 * xmlSchemaValidateFacet:
5506 * @base: the base type
5507 * @facet: the facet to check
5508 * @value: the lexical repr of the value to validate
5509 * @val: the precomputed value
5510 *
5511 * Check a value against a facet condition
5512 *
5513 * Returns 0 if the element is schemas valid, a positive error code
5514 * number otherwise and -1 in case of internal or API error.
5515 */
5516int
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005517xmlSchemaValidateFacet(xmlSchemaTypePtr base,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005518 xmlSchemaFacetPtr facet,
5519 const xmlChar *value,
5520 xmlSchemaValPtr val)
5521{
5522 /*
5523 * This tries to ensure API compatibility regarding the old
5524 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5525 * xmlSchemaValidateFacetWhtsp().
5526 */
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005527 if (val != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005528 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005529 XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005530 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005531 else if (base != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005532 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005533 XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005534 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005535 return(-1);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005536}
5537
5538/**
5539 * xmlSchemaValidateFacetWhtsp:
5540 * @facet: the facet to check
5541 * @fws: the whitespace type of the facet's value
5542 * @valType: the built-in type of the value
5543 * @value: the lexical (or normalized for pattern) repr of the value to validate
5544 * @val: the precomputed value
5545 * @ws: the whitespace type of the value
5546 *
5547 * Check a value against a facet condition. This takes value normalization
5548 * according to the specified whitespace types into account.
5549 * Note that @value needs to be the *normalized* value if the facet
5550 * is of type "pattern".
5551 *
5552 * Returns 0 if the element is schemas valid, a positive error code
5553 * number otherwise and -1 in case of internal or API error.
5554 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005555int
5556xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
5557 xmlSchemaWhitespaceValueType fws,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005558 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005559 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005560 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005561 xmlSchemaWhitespaceValueType ws)
5562{
5563 return(xmlSchemaValidateFacetInternal(facet, fws, valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005564 value, val, ws));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005565}
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005566
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005567#if 0
5568#ifndef DBL_DIG
5569#define DBL_DIG 16
5570#endif
5571#ifndef DBL_EPSILON
5572#define DBL_EPSILON 1E-9
5573#endif
5574
5575#define INTEGER_DIGITS DBL_DIG
5576#define FRACTION_DIGITS (DBL_DIG + 1)
5577#define EXPONENT_DIGITS (3 + 2)
5578
5579/**
5580 * xmlXPathFormatNumber:
5581 * @number: number to format
5582 * @buffer: output buffer
5583 * @buffersize: size of output buffer
5584 *
5585 * Convert the number into a string representation.
5586 */
5587static void
5588xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
5589{
5590 switch (xmlXPathIsInf(number)) {
5591 case 1:
5592 if (buffersize > (int)sizeof("INF"))
5593 snprintf(buffer, buffersize, "INF");
5594 break;
5595 case -1:
5596 if (buffersize > (int)sizeof("-INF"))
5597 snprintf(buffer, buffersize, "-INF");
5598 break;
5599 default:
5600 if (xmlXPathIsNaN(number)) {
5601 if (buffersize > (int)sizeof("NaN"))
5602 snprintf(buffer, buffersize, "NaN");
5603 } else if (number == 0) {
5604 snprintf(buffer, buffersize, "0.0E0");
5605 } else {
5606 /* 3 is sign, decimal point, and terminating zero */
5607 char work[DBL_DIG + EXPONENT_DIGITS + 3];
5608 int integer_place, fraction_place;
5609 char *ptr;
5610 char *after_fraction;
5611 double absolute_value;
5612 int size;
5613
5614 absolute_value = fabs(number);
5615
5616 /*
5617 * Result is in work, and after_fraction points
5618 * just past the fractional part.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005619 * Use scientific notation
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005620 */
5621 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
5622 fraction_place = DBL_DIG - 1;
5623 snprintf(work, sizeof(work),"%*.*e",
5624 integer_place, fraction_place, number);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005625 after_fraction = strchr(work + DBL_DIG, 'e');
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005626 /* Remove fractional trailing zeroes */
5627 ptr = after_fraction;
5628 while (*(--ptr) == '0')
5629 ;
5630 if (*ptr != '.')
5631 ptr++;
5632 while ((*ptr++ = *after_fraction++) != 0);
5633
5634 /* Finally copy result back to caller */
5635 size = strlen(work) + 1;
5636 if (size > buffersize) {
5637 work[buffersize - 1] = 0;
5638 size = buffersize;
5639 }
5640 memmove(buffer, work, size);
5641 }
5642 break;
5643 }
5644}
5645#endif
5646
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005647/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005648 * xmlSchemaGetCanonValue:
5649 * @val: the precomputed value
5650 * @retValue: the returned value
5651 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01005652 * Get the canonical lexical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005653 * The caller has to FREE the returned retValue.
5654 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005655 * WARNING: Some value types are not supported yet, resulting
5656 * in a @retValue of "???".
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005657 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005658 * TODO: XML Schema 1.0 does not define canonical representations
5659 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5660 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005661 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005662 *
5663 * Returns 0 if the value could be built, 1 if the value type is
5664 * not supported yet and -1 in case of API errors.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005665 */
5666int
Daniel Veillardb5839c32005-02-19 18:27:14 +00005667xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005668{
Daniel Veillardb5839c32005-02-19 18:27:14 +00005669 if ((retValue == NULL) || (val == NULL))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005670 return (-1);
5671 *retValue = NULL;
5672 switch (val->type) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005673 case XML_SCHEMAS_STRING:
5674 if (val->value.str == NULL)
5675 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5676 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005677 *retValue =
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005678 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5679 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005680 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005681 if (val->value.str == NULL)
5682 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5683 else {
5684 *retValue = xmlSchemaWhiteSpaceReplace(
5685 (const xmlChar *) val->value.str);
5686 if ((*retValue) == NULL)
5687 *retValue = BAD_CAST xmlStrdup(
5688 (const xmlChar *) val->value.str);
5689 }
5690 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005691 case XML_SCHEMAS_TOKEN:
5692 case XML_SCHEMAS_LANGUAGE:
5693 case XML_SCHEMAS_NMTOKEN:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005694 case XML_SCHEMAS_NAME:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005695 case XML_SCHEMAS_NCNAME:
5696 case XML_SCHEMAS_ID:
5697 case XML_SCHEMAS_IDREF:
5698 case XML_SCHEMAS_ENTITY:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005699 case XML_SCHEMAS_NOTATION: /* Unclear */
5700 case XML_SCHEMAS_ANYURI: /* Unclear */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005701 if (val->value.str == NULL)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005702 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005703 *retValue =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005704 BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
5705 if (*retValue == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005706 *retValue =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005707 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005708 break;
5709 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005710 /* TODO: Unclear in XML Schema 1.0. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005711 if (val->value.qname.uri == NULL) {
5712 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
5713 return (0);
5714 } else {
5715 *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
5716 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5717 BAD_CAST val->value.qname.uri);
5718 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5719 BAD_CAST "}");
5720 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5721 BAD_CAST val->value.qname.uri);
5722 }
5723 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005724 case XML_SCHEMAS_DECIMAL:
5725 /*
5726 * TODO: Lookout for a more simple implementation.
5727 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005728 if ((val->value.decimal.total == 1) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005729 (val->value.decimal.lo == 0)) {
5730 *retValue = xmlStrdup(BAD_CAST "0.0");
5731 } else {
5732 xmlSchemaValDecimal dec = val->value.decimal;
5733 int bufsize;
5734 char *buf = NULL, *offs;
5735
5736 /* Add room for the decimal point as well. */
5737 bufsize = dec.total + 2;
5738 if (dec.sign)
5739 bufsize++;
5740 /* Add room for leading/trailing zero. */
5741 if ((dec.frac == 0) || (dec.frac == dec.total))
5742 bufsize++;
5743 buf = xmlMalloc(bufsize);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005744 if (buf == NULL)
5745 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005746 offs = buf;
5747 if (dec.sign)
5748 *offs++ = '-';
5749 if (dec.frac == dec.total) {
5750 *offs++ = '0';
5751 *offs++ = '.';
5752 }
5753 if (dec.hi != 0)
5754 snprintf(offs, bufsize - (offs - buf),
5755 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5756 else if (dec.mi != 0)
5757 snprintf(offs, bufsize - (offs - buf),
5758 "%lu%lu", dec.mi, dec.lo);
5759 else
5760 snprintf(offs, bufsize - (offs - buf),
5761 "%lu", dec.lo);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005762
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005763 if (dec.frac != 0) {
5764 if (dec.frac != dec.total) {
5765 int diff = dec.total - dec.frac;
5766 /*
5767 * Insert the decimal point.
5768 */
5769 memmove(offs + diff + 1, offs + diff, dec.frac +1);
5770 offs[diff] = '.';
5771 } else {
5772 unsigned int i = 0;
5773 /*
5774 * Insert missing zeroes behind the decimal point.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005775 */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005776 while (*(offs + i) != 0)
5777 i++;
5778 if (i < dec.total) {
5779 memmove(offs + (dec.total - i), offs, i +1);
5780 memset(offs, '0', dec.total - i);
5781 }
5782 }
5783 } else {
5784 /*
5785 * Append decimal point and zero.
5786 */
5787 offs = buf + bufsize - 1;
5788 *offs-- = 0;
5789 *offs-- = '0';
5790 *offs-- = '.';
5791 }
5792 *retValue = BAD_CAST buf;
5793 }
5794 break;
5795 case XML_SCHEMAS_INTEGER:
5796 case XML_SCHEMAS_PINTEGER:
5797 case XML_SCHEMAS_NPINTEGER:
5798 case XML_SCHEMAS_NINTEGER:
5799 case XML_SCHEMAS_NNINTEGER:
5800 case XML_SCHEMAS_LONG:
5801 case XML_SCHEMAS_BYTE:
5802 case XML_SCHEMAS_SHORT:
5803 case XML_SCHEMAS_INT:
5804 case XML_SCHEMAS_UINT:
5805 case XML_SCHEMAS_ULONG:
5806 case XML_SCHEMAS_USHORT:
5807 case XML_SCHEMAS_UBYTE:
5808 if ((val->value.decimal.total == 1) &&
5809 (val->value.decimal.lo == 0))
5810 *retValue = xmlStrdup(BAD_CAST "0");
5811 else {
5812 xmlSchemaValDecimal dec = val->value.decimal;
5813 int bufsize = dec.total + 1;
5814
5815 /* Add room for the decimal point as well. */
5816 if (dec.sign)
5817 bufsize++;
5818 *retValue = xmlMalloc(bufsize);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005819 if (*retValue == NULL)
5820 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005821 if (dec.hi != 0) {
5822 if (dec.sign)
5823 snprintf((char *) *retValue, bufsize,
5824 "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5825 else
5826 snprintf((char *) *retValue, bufsize,
5827 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5828 } else if (dec.mi != 0) {
5829 if (dec.sign)
5830 snprintf((char *) *retValue, bufsize,
5831 "-%lu%lu", dec.mi, dec.lo);
5832 else
5833 snprintf((char *) *retValue, bufsize,
5834 "%lu%lu", dec.mi, dec.lo);
5835 } else {
5836 if (dec.sign)
5837 snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
5838 else
5839 snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
5840 }
5841 }
5842 break;
5843 case XML_SCHEMAS_BOOLEAN:
5844 if (val->value.b)
5845 *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
5846 else
5847 *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
5848 break;
5849 case XML_SCHEMAS_DURATION: {
5850 char buf[100];
5851 unsigned long year;
5852 unsigned long mon, day, hour = 0, min = 0;
5853 double sec = 0, left;
5854
5855 /* TODO: Unclear in XML Schema 1.0 */
5856 /*
5857 * TODO: This results in a normalized output of the value
5858 * - which is NOT conformant to the spec -
5859 * since the exact values of each property are not
5860 * recoverable. Think about extending the structure to
5861 * provide a field for every property.
5862 */
5863 year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
5864 mon = labs(val->value.dur.mon) - 12 * year;
5865
5866 day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
5867 left = fabs(val->value.dur.sec) - day * 86400;
5868 if (left > 0) {
5869 hour = (unsigned long) FQUOTIENT(left, 3600);
5870 left = left - (hour * 3600);
5871 if (left > 0) {
5872 min = (unsigned long) FQUOTIENT(left, 60);
5873 sec = left - (min * 60);
5874 }
5875 }
5876 if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
5877 snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5878 year, mon, day, hour, min, sec);
5879 else
5880 snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5881 year, mon, day, hour, min, sec);
5882 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5883 }
5884 break;
5885 case XML_SCHEMAS_GYEAR: {
5886 char buf[30];
5887 /* TODO: Unclear in XML Schema 1.0 */
5888 /* TODO: What to do with the timezone? */
5889 snprintf(buf, 30, "%04ld", val->value.date.year);
5890 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5891 }
5892 break;
5893 case XML_SCHEMAS_GMONTH: {
5894 /* TODO: Unclear in XML Schema 1.0 */
5895 /* TODO: What to do with the timezone? */
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005896 *retValue = xmlMalloc(6);
5897 if (*retValue == NULL)
5898 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005899 snprintf((char *) *retValue, 6, "--%02u",
5900 val->value.date.mon);
5901 }
5902 break;
5903 case XML_SCHEMAS_GDAY: {
5904 /* TODO: Unclear in XML Schema 1.0 */
5905 /* TODO: What to do with the timezone? */
5906 *retValue = xmlMalloc(6);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005907 if (*retValue == NULL)
5908 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005909 snprintf((char *) *retValue, 6, "---%02u",
5910 val->value.date.day);
5911 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005912 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005913 case XML_SCHEMAS_GMONTHDAY: {
5914 /* TODO: Unclear in XML Schema 1.0 */
5915 /* TODO: What to do with the timezone? */
5916 *retValue = xmlMalloc(8);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005917 if (*retValue == NULL)
5918 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005919 snprintf((char *) *retValue, 8, "--%02u-%02u",
5920 val->value.date.mon, val->value.date.day);
5921 }
5922 break;
5923 case XML_SCHEMAS_GYEARMONTH: {
5924 char buf[35];
5925 /* TODO: Unclear in XML Schema 1.0 */
5926 /* TODO: What to do with the timezone? */
5927 if (val->value.date.year < 0)
5928 snprintf(buf, 35, "-%04ld-%02u",
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005929 labs(val->value.date.year),
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005930 val->value.date.mon);
5931 else
5932 snprintf(buf, 35, "%04ld-%02u",
5933 val->value.date.year, val->value.date.mon);
5934 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5935 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005936 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005937 case XML_SCHEMAS_TIME:
5938 {
5939 char buf[30];
5940
5941 if (val->value.date.tz_flag) {
5942 xmlSchemaValPtr norm;
5943
5944 norm = xmlSchemaDateNormalize(val, 0);
5945 if (norm == NULL)
5946 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005947 /*
5948 * TODO: Check if "%.14g" is portable.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005949 */
5950 snprintf(buf, 30,
5951 "%02u:%02u:%02.14gZ",
5952 norm->value.date.hour,
5953 norm->value.date.min,
5954 norm->value.date.sec);
5955 xmlSchemaFreeValue(norm);
5956 } else {
5957 snprintf(buf, 30,
5958 "%02u:%02u:%02.14g",
5959 val->value.date.hour,
5960 val->value.date.min,
5961 val->value.date.sec);
5962 }
5963 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005964 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005965 break;
5966 case XML_SCHEMAS_DATE:
5967 {
5968 char buf[30];
5969
5970 if (val->value.date.tz_flag) {
5971 xmlSchemaValPtr norm;
5972
5973 norm = xmlSchemaDateNormalize(val, 0);
5974 if (norm == NULL)
5975 return (-1);
5976 /*
5977 * TODO: Append the canonical value of the
5978 * recoverable timezone and not "Z".
5979 */
5980 snprintf(buf, 30,
5981 "%04ld:%02u:%02uZ",
5982 norm->value.date.year, norm->value.date.mon,
5983 norm->value.date.day);
5984 xmlSchemaFreeValue(norm);
5985 } else {
5986 snprintf(buf, 30,
5987 "%04ld:%02u:%02u",
5988 val->value.date.year, val->value.date.mon,
5989 val->value.date.day);
5990 }
5991 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005992 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005993 break;
5994 case XML_SCHEMAS_DATETIME:
5995 {
5996 char buf[50];
5997
5998 if (val->value.date.tz_flag) {
5999 xmlSchemaValPtr norm;
6000
6001 norm = xmlSchemaDateNormalize(val, 0);
6002 if (norm == NULL)
6003 return (-1);
6004 /*
6005 * TODO: Check if "%.14g" is portable.
6006 */
6007 snprintf(buf, 50,
6008 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
6009 norm->value.date.year, norm->value.date.mon,
6010 norm->value.date.day, norm->value.date.hour,
6011 norm->value.date.min, norm->value.date.sec);
6012 xmlSchemaFreeValue(norm);
6013 } else {
6014 snprintf(buf, 50,
6015 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
6016 val->value.date.year, val->value.date.mon,
6017 val->value.date.day, val->value.date.hour,
6018 val->value.date.min, val->value.date.sec);
6019 }
6020 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6021 }
6022 break;
6023 case XML_SCHEMAS_HEXBINARY:
6024 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
6025 break;
6026 case XML_SCHEMAS_BASE64BINARY:
6027 /*
6028 * TODO: Is the following spec piece implemented?:
6029 * SPEC: "Note: For some values the canonical form defined
6030 * above does not conform to [RFC 2045], which requires breaking
6031 * with linefeeds at appropriate intervals."
6032 */
6033 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
6034 break;
6035 case XML_SCHEMAS_FLOAT: {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006036 char buf[30];
6037 /*
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006038 * |m| < 16777216, -149 <= e <= 104.
6039 * TODO: Handle, NaN, INF, -INF. The format is not
6040 * yet conformant. The c type float does not cover
6041 * the whole range.
6042 */
6043 snprintf(buf, 30, "%01.14e", val->value.f);
6044 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6045 }
6046 break;
6047 case XML_SCHEMAS_DOUBLE: {
6048 char buf[40];
6049 /* |m| < 9007199254740992, -1075 <= e <= 970 */
6050 /*
6051 * TODO: Handle, NaN, INF, -INF. The format is not
6052 * yet conformant. The c type float does not cover
6053 * the whole range.
6054 */
6055 snprintf(buf, 40, "%01.14e", val->value.d);
6056 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6057 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006058 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006059 default:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006060 *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006061 return (1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006062 }
Daniel Veillard26ab0e62006-10-11 12:32:51 +00006063 if (*retValue == NULL)
6064 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006065 return (0);
6066}
6067
Daniel Veillardbda59572005-04-01 17:15:17 +00006068/**
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006069 * xmlSchemaGetCanonValueWhtsp:
6070 * @val: the precomputed value
6071 * @retValue: the returned value
6072 * @ws: the whitespace type of the value
6073 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01006074 * Get the canonical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006075 * The caller has to free the returned @retValue.
6076 *
6077 * Returns 0 if the value could be built, 1 if the value type is
6078 * not supported yet and -1 in case of API errors.
6079 */
6080int
6081xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
6082 const xmlChar **retValue,
6083 xmlSchemaWhitespaceValueType ws)
6084{
6085 if ((retValue == NULL) || (val == NULL))
6086 return (-1);
6087 if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
6088 (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
6089 return (-1);
6090
6091 *retValue = NULL;
6092 switch (val->type) {
6093 case XML_SCHEMAS_STRING:
6094 if (val->value.str == NULL)
6095 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6096 else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6097 *retValue = xmlSchemaCollapseString(val->value.str);
6098 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
6099 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6100 if ((*retValue) == NULL)
6101 *retValue = BAD_CAST xmlStrdup(val->value.str);
6102 break;
6103 case XML_SCHEMAS_NORMSTRING:
6104 if (val->value.str == NULL)
6105 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6106 else {
6107 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6108 *retValue = xmlSchemaCollapseString(val->value.str);
6109 else
6110 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6111 if ((*retValue) == NULL)
6112 *retValue = BAD_CAST xmlStrdup(val->value.str);
6113 }
6114 break;
6115 default:
6116 return (xmlSchemaGetCanonValue(val, retValue));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006117 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006118 return (0);
6119}
6120
6121/**
Daniel Veillardbda59572005-04-01 17:15:17 +00006122 * xmlSchemaGetValType:
6123 * @val: a schemas value
6124 *
6125 * Accessor for the type of a value
6126 *
6127 * Returns the xmlSchemaValType of the value
6128 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006129xmlSchemaValType
6130xmlSchemaGetValType(xmlSchemaValPtr val)
6131{
Daniel Veillardbda59572005-04-01 17:15:17 +00006132 if (val == NULL)
6133 return(XML_SCHEMAS_UNKNOWN);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006134 return (val->type);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006135}
6136
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006137#define bottom_xmlschemastypes
6138#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00006139#endif /* LIBXML_SCHEMAS_ENABLED */