blob: 03a55df210f537da6680d5759831ddc84c7b04b5 [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 */
3851 if (tmon < 0)
3852 tmon = 0;
3853 if (tmon > 12)
3854 tmon = 12;
Daniel Veillard5a872412002-05-22 06:40:27 +00003855 tempdays += MAX_DAYINMONTH(tyr, tmon);
3856 carry = -1;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003857 } else if (tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003858 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
3859 carry = 1;
3860 } else
3861 break;
3862
3863 temp = r->mon + carry;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003864 r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
3865 r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003866 if (r->year == 0) {
3867 if (temp < 1)
3868 r->year--;
3869 else
3870 r->year++;
3871 }
3872 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003873
Daniel Veillard5a872412002-05-22 06:40:27 +00003874 r->day = tempdays;
3875
3876 /*
3877 * adjust the date/time type to the date values
3878 */
3879 if (ret->type != XML_SCHEMAS_DATETIME) {
3880 if ((r->hour) || (r->min) || (r->sec))
3881 ret->type = XML_SCHEMAS_DATETIME;
3882 else if (ret->type != XML_SCHEMAS_DATE) {
3883 if ((r->mon != 1) && (r->day != 1))
3884 ret->type = XML_SCHEMAS_DATE;
3885 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
3886 ret->type = XML_SCHEMAS_GYEARMONTH;
3887 }
3888 }
3889
Daniel Veillard669adfc2004-05-29 20:12:46 +00003890 xmlSchemaFreeValue(tmp);
Daniel Veillard5a872412002-05-22 06:40:27 +00003891
Daniel Veillard5a872412002-05-22 06:40:27 +00003892 return ret;
3893}
3894
3895/**
3896 * xmlSchemaDateNormalize:
Daniel Veillard669adfc2004-05-29 20:12:46 +00003897 * @dt: an #xmlSchemaValPtr of a date/time type value.
3898 * @offset: number of seconds to adjust @dt by.
Daniel Veillard5a872412002-05-22 06:40:27 +00003899 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003900 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3901 * the return value is a time-zone offset is present on @dt.
Daniel Veillard5a872412002-05-22 06:40:27 +00003902 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003903 * Returns a normalized copy of @dt or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003904 */
3905static xmlSchemaValPtr
3906xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
3907{
3908 xmlSchemaValPtr dur, ret;
3909
3910 if (dt == NULL)
3911 return NULL;
3912
3913 if (((dt->type != XML_SCHEMAS_TIME) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003914 (dt->type != XML_SCHEMAS_DATETIME) &&
3915 (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
Daniel Veillard5a872412002-05-22 06:40:27 +00003916 return xmlSchemaDupVal(dt);
3917
3918 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
3919 if (dur == NULL)
3920 return NULL;
3921
3922 dur->value.date.sec -= offset;
3923
3924 ret = _xmlSchemaDateAdd(dt, dur);
3925 if (ret == NULL)
3926 return NULL;
3927
3928 xmlSchemaFreeValue(dur);
3929
3930 /* ret->value.date.tzo = 0; */
3931 return ret;
3932}
3933
3934/**
3935 * _xmlSchemaDateCastYMToDays:
3936 * @dt: an #xmlSchemaValPtr
3937 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003938 * Convert mon and year of @dt to total number of days. Take the
Daniel Veillard5a872412002-05-22 06:40:27 +00003939 * number of years since (or before) 1 AD and add the number of leap
3940 * years. This is a function because negative
3941 * years must be handled a little differently and there is no zero year.
3942 *
3943 * Returns number of days.
3944 */
3945static long
3946_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
3947{
3948 long ret;
Daniel Veillard49e89632004-09-23 16:24:36 +00003949 int mon;
Daniel Veillard5a872412002-05-22 06:40:27 +00003950
Daniel Veillard49e89632004-09-23 16:24:36 +00003951 mon = dt->value.date.mon;
3952 if (mon <= 0) mon = 1; /* normalization */
3953
3954 if (dt->value.date.year <= 0)
Daniel Veillard5a872412002-05-22 06:40:27 +00003955 ret = (dt->value.date.year * 365) +
3956 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
3957 ((dt->value.date.year+1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003958 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003959 else
3960 ret = ((dt->value.date.year-1) * 365) +
3961 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
3962 ((dt->value.date.year-1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003963 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003964
3965 return ret;
3966}
3967
3968/**
3969 * TIME_TO_NUMBER:
3970 * @dt: an #xmlSchemaValPtr
3971 *
3972 * Calculates the number of seconds in the time portion of @dt.
3973 *
3974 * Returns seconds.
3975 */
3976#define TIME_TO_NUMBER(dt) \
3977 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00003978 (dt->value.date.min * SECS_PER_MIN) + \
3979 (dt->value.date.tzo * SECS_PER_MIN)) + \
3980 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00003981
3982/**
3983 * xmlSchemaCompareDates:
3984 * @x: a first date/time value
3985 * @y: a second date/time value
3986 *
3987 * Compare 2 date/times
3988 *
3989 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3990 * case of error
3991 */
3992static int
3993xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
3994{
3995 unsigned char xmask, ymask, xor_mask, and_mask;
3996 xmlSchemaValPtr p1, p2, q1, q2;
3997 long p1d, p2d, q1d, q2d;
3998
3999 if ((x == NULL) || (y == NULL))
4000 return -2;
4001
4002 if (x->value.date.tz_flag) {
4003
4004 if (!y->value.date.tz_flag) {
4005 p1 = xmlSchemaDateNormalize(x, 0);
4006 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4007 /* normalize y + 14:00 */
4008 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
4009
4010 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004011 if (p1d < q1d) {
4012 xmlSchemaFreeValue(p1);
4013 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004014 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004015 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004016 double sec;
4017
4018 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004019 if (sec < 0.0) {
4020 xmlSchemaFreeValue(p1);
4021 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004022 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004023 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004024 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004025 /* normalize y - 14:00 */
4026 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
4027 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
4028 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004029 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004030 else if (p1d == q2d) {
4031 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
4032 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004033 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004034 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004035 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004036 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004037 xmlSchemaFreeValue(p1);
4038 xmlSchemaFreeValue(q1);
4039 xmlSchemaFreeValue(q2);
4040 if (ret != 0)
4041 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004042 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004043 } else {
4044 xmlSchemaFreeValue(p1);
4045 xmlSchemaFreeValue(q1);
4046 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004047 }
4048 } else if (y->value.date.tz_flag) {
4049 q1 = xmlSchemaDateNormalize(y, 0);
4050 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4051
4052 /* normalize x - 14:00 */
4053 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
4054 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4055
Daniel Veillardfdc91562002-07-01 21:52:03 +00004056 if (p1d < q1d) {
4057 xmlSchemaFreeValue(p1);
4058 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004059 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004060 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004061 double sec;
4062
4063 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004064 if (sec < 0.0) {
4065 xmlSchemaFreeValue(p1);
4066 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004067 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004068 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004069 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004070 /* normalize x + 14:00 */
4071 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
4072 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
4073
Daniel Veillard6560a422003-03-27 21:25:38 +00004074 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004075 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00004076 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004077 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
4078 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004079 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004080 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004081 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004082 }
Daniel Veillard6560a422003-03-27 21:25:38 +00004083 xmlSchemaFreeValue(p1);
4084 xmlSchemaFreeValue(q1);
4085 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004086 if (ret != 0)
4087 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004088 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004089 } else {
4090 xmlSchemaFreeValue(p1);
4091 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004092 }
4093 }
4094
4095 /*
4096 * if the same type then calculate the difference
4097 */
4098 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004099 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004100 q1 = xmlSchemaDateNormalize(y, 0);
4101 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4102
4103 p1 = xmlSchemaDateNormalize(x, 0);
4104 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4105
Daniel Veillardfdc91562002-07-01 21:52:03 +00004106 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004107 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004108 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004109 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004110 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00004111 double sec;
4112
4113 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4114 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004115 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004116 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004117 ret = 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004118
Daniel Veillard5a872412002-05-22 06:40:27 +00004119 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004120 xmlSchemaFreeValue(p1);
4121 xmlSchemaFreeValue(q1);
4122 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004123 }
4124
4125 switch (x->type) {
4126 case XML_SCHEMAS_DATETIME:
4127 xmask = 0xf;
4128 break;
4129 case XML_SCHEMAS_DATE:
4130 xmask = 0x7;
4131 break;
4132 case XML_SCHEMAS_GYEAR:
4133 xmask = 0x1;
4134 break;
4135 case XML_SCHEMAS_GMONTH:
4136 xmask = 0x2;
4137 break;
4138 case XML_SCHEMAS_GDAY:
4139 xmask = 0x3;
4140 break;
4141 case XML_SCHEMAS_GYEARMONTH:
4142 xmask = 0x3;
4143 break;
4144 case XML_SCHEMAS_GMONTHDAY:
4145 xmask = 0x6;
4146 break;
4147 case XML_SCHEMAS_TIME:
4148 xmask = 0x8;
4149 break;
4150 default:
4151 xmask = 0;
4152 break;
4153 }
4154
4155 switch (y->type) {
4156 case XML_SCHEMAS_DATETIME:
4157 ymask = 0xf;
4158 break;
4159 case XML_SCHEMAS_DATE:
4160 ymask = 0x7;
4161 break;
4162 case XML_SCHEMAS_GYEAR:
4163 ymask = 0x1;
4164 break;
4165 case XML_SCHEMAS_GMONTH:
4166 ymask = 0x2;
4167 break;
4168 case XML_SCHEMAS_GDAY:
4169 ymask = 0x3;
4170 break;
4171 case XML_SCHEMAS_GYEARMONTH:
4172 ymask = 0x3;
4173 break;
4174 case XML_SCHEMAS_GMONTHDAY:
4175 ymask = 0x6;
4176 break;
4177 case XML_SCHEMAS_TIME:
4178 ymask = 0x8;
4179 break;
4180 default:
4181 ymask = 0;
4182 break;
4183 }
4184
4185 xor_mask = xmask ^ ymask; /* mark type differences */
4186 and_mask = xmask & ymask; /* mark field specification */
4187
4188 /* year */
4189 if (xor_mask & 1)
4190 return 2; /* indeterminate */
4191 else if (and_mask & 1) {
4192 if (x->value.date.year < y->value.date.year)
4193 return -1;
4194 else if (x->value.date.year > y->value.date.year)
4195 return 1;
4196 }
4197
4198 /* month */
4199 if (xor_mask & 2)
4200 return 2; /* indeterminate */
4201 else if (and_mask & 2) {
4202 if (x->value.date.mon < y->value.date.mon)
4203 return -1;
4204 else if (x->value.date.mon > y->value.date.mon)
4205 return 1;
4206 }
4207
4208 /* day */
4209 if (xor_mask & 4)
4210 return 2; /* indeterminate */
4211 else if (and_mask & 4) {
4212 if (x->value.date.day < y->value.date.day)
4213 return -1;
4214 else if (x->value.date.day > y->value.date.day)
4215 return 1;
4216 }
4217
4218 /* time */
4219 if (xor_mask & 8)
4220 return 2; /* indeterminate */
4221 else if (and_mask & 8) {
4222 if (x->value.date.hour < y->value.date.hour)
4223 return -1;
4224 else if (x->value.date.hour > y->value.date.hour)
4225 return 1;
4226 else if (x->value.date.min < y->value.date.min)
4227 return -1;
4228 else if (x->value.date.min > y->value.date.min)
4229 return 1;
4230 else if (x->value.date.sec < y->value.date.sec)
4231 return -1;
4232 else if (x->value.date.sec > y->value.date.sec)
4233 return 1;
4234 }
4235
Daniel Veillard070803b2002-05-03 07:29:38 +00004236 return 0;
4237}
4238
4239/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004240 * xmlSchemaComparePreserveReplaceStrings:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004241 * @x: a first string value
4242 * @y: a second string value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004243 * @invert: inverts the result if x < y or x > y.
4244 *
4245 * Compare 2 string for their normalized values.
4246 * @x is a string with whitespace of "preserve", @y is
4247 * a string with a whitespace of "replace". I.e. @x could
4248 * be an "xsd:string" and @y an "xsd:normalizedString".
4249 *
4250 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4251 * case of error
4252 */
4253static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004254xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
4255 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004256 int invert)
4257{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004258 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004259
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004260 while ((*x != 0) && (*y != 0)) {
4261 if (IS_WSP_REPLACE_CH(*y)) {
4262 if (! IS_WSP_SPACE_CH(*x)) {
4263 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004264 if (invert)
4265 return(1);
4266 else
4267 return(-1);
4268 } else {
4269 if (invert)
4270 return(-1);
4271 else
4272 return(1);
4273 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004274 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004275 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004276 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004277 if (tmp < 0) {
4278 if (invert)
4279 return(1);
4280 else
4281 return(-1);
4282 }
4283 if (tmp > 0) {
4284 if (invert)
4285 return(-1);
4286 else
4287 return(1);
4288 }
4289 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004290 x++;
4291 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004292 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004293 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004294 if (invert)
4295 return(-1);
4296 else
4297 return(1);
4298 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004299 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004300 if (invert)
4301 return(1);
4302 else
4303 return(-1);
4304 }
4305 return(0);
4306}
4307
4308/**
4309 * xmlSchemaComparePreserveCollapseStrings:
4310 * @x: a first string value
4311 * @y: a second string value
4312 *
4313 * Compare 2 string for their normalized values.
4314 * @x is a string with whitespace of "preserve", @y is
4315 * a string with a whitespace of "collapse". I.e. @x could
4316 * be an "xsd:string" and @y an "xsd:normalizedString".
4317 *
4318 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4319 * case of error
4320 */
4321static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004322xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
4323 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004324 int invert)
4325{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004326 int tmp;
4327
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004328 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004329 * Skip leading blank chars of the collapsed string.
4330 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004331 while IS_WSP_BLANK_CH(*y)
4332 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004333
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004334 while ((*x != 0) && (*y != 0)) {
4335 if IS_WSP_BLANK_CH(*y) {
4336 if (! IS_WSP_SPACE_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004337 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004338 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004339 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004340 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004341 if (invert)
4342 return(1);
4343 else
4344 return(-1);
4345 } else {
4346 if (invert)
4347 return(-1);
4348 else
4349 return(1);
4350 }
4351 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004352 x++;
4353 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004354 /*
4355 * Skip contiguous blank chars of the collapsed string.
4356 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004357 while IS_WSP_BLANK_CH(*y)
4358 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004359 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004360 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004361 if (tmp < 0) {
4362 if (invert)
4363 return(1);
4364 else
4365 return(-1);
4366 }
4367 if (tmp > 0) {
4368 if (invert)
4369 return(-1);
4370 else
4371 return(1);
4372 }
4373 }
4374 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004375 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004376 if (invert)
4377 return(-1);
4378 else
4379 return(1);
4380 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004381 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004382 /*
4383 * Skip trailing blank chars of the collapsed string.
4384 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004385 while IS_WSP_BLANK_CH(*y)
4386 y++;
4387 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004388 if (invert)
4389 return(1);
4390 else
4391 return(-1);
4392 }
4393 }
4394 return(0);
4395}
4396
4397/**
4398 * xmlSchemaComparePreserveCollapseStrings:
4399 * @x: a first string value
4400 * @y: a second string value
4401 *
4402 * Compare 2 string for their normalized values.
4403 * @x is a string with whitespace of "preserve", @y is
4404 * a string with a whitespace of "collapse". I.e. @x could
4405 * be an "xsd:string" and @y an "xsd:normalizedString".
4406 *
4407 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4408 * case of error
4409 */
4410static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004411xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
4412 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004413 int invert)
4414{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004415 int tmp;
4416
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004417 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004418 * Skip leading blank chars of the collapsed string.
4419 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004420 while IS_WSP_BLANK_CH(*y)
4421 y++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004422
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004423 while ((*x != 0) && (*y != 0)) {
4424 if IS_WSP_BLANK_CH(*y) {
4425 if (! IS_WSP_BLANK_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004426 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004427 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004428 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004429 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004430 if (invert)
4431 return(1);
4432 else
4433 return(-1);
4434 } else {
4435 if (invert)
4436 return(-1);
4437 else
4438 return(1);
4439 }
4440 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004441 x++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004442 y++;
4443 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004444 * Skip contiguous blank chars of the collapsed string.
4445 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004446 while IS_WSP_BLANK_CH(*y)
4447 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004448 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004449 if IS_WSP_BLANK_CH(*x) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004450 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004451 * The xv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004452 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004453 if ((0x20 - *y) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004454 if (invert)
4455 return(1);
4456 else
4457 return(-1);
4458 } else {
4459 if (invert)
4460 return(-1);
4461 else
4462 return(1);
4463 }
4464 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004465 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004466 if (tmp < 0)
4467 return(-1);
4468 if (tmp > 0)
4469 return(1);
4470 }
4471 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004472 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004473 if (invert)
4474 return(-1);
4475 else
4476 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004477 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004478 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004479 /*
4480 * Skip trailing blank chars of the collapsed string.
4481 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004482 while IS_WSP_BLANK_CH(*y)
4483 y++;
4484 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004485 if (invert)
4486 return(1);
4487 else
4488 return(-1);
4489 }
4490 }
4491 return(0);
4492}
4493
4494
4495/**
4496 * xmlSchemaCompareReplacedStrings:
4497 * @x: a first string value
4498 * @y: a second string value
4499 *
4500 * Compare 2 string for their normalized values.
4501 *
4502 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4503 * case of error
4504 */
4505static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004506xmlSchemaCompareReplacedStrings(const xmlChar *x,
4507 const xmlChar *y)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004508{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004509 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004510
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004511 while ((*x != 0) && (*y != 0)) {
4512 if IS_WSP_BLANK_CH(*y) {
4513 if (! IS_WSP_BLANK_CH(*x)) {
4514 if ((*x - 0x20) < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004515 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004516 else
4517 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004518 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004519 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004520 if IS_WSP_BLANK_CH(*x) {
4521 if ((0x20 - *y) < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004522 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004523 else
4524 return(1);
4525 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004526 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004527 if (tmp < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004528 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004529 if (tmp > 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004530 return(1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004531 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004532 x++;
4533 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004534 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004535 if (*x != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004536 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004537 if (*y != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004538 return(-1);
4539 return(0);
4540}
4541
4542/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004543 * xmlSchemaCompareNormStrings:
4544 * @x: a first string value
4545 * @y: a second string value
4546 *
4547 * Compare 2 string for their normalized values.
4548 *
4549 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4550 * case of error
4551 */
4552static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004553xmlSchemaCompareNormStrings(const xmlChar *x,
4554 const xmlChar *y) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00004555 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004556
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004557 while (IS_BLANK_CH(*x)) x++;
4558 while (IS_BLANK_CH(*y)) y++;
4559 while ((*x != 0) && (*y != 0)) {
4560 if (IS_BLANK_CH(*x)) {
4561 if (!IS_BLANK_CH(*y)) {
4562 tmp = *x - *y;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004563 return(tmp);
4564 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004565 while (IS_BLANK_CH(*x)) x++;
4566 while (IS_BLANK_CH(*y)) y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004567 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004568 tmp = *x++ - *y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004569 if (tmp < 0)
4570 return(-1);
4571 if (tmp > 0)
4572 return(1);
4573 }
4574 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004575 if (*x != 0) {
4576 while (IS_BLANK_CH(*x)) x++;
4577 if (*x != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004578 return(1);
4579 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004580 if (*y != 0) {
4581 while (IS_BLANK_CH(*y)) y++;
4582 if (*y != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004583 return(-1);
4584 }
4585 return(0);
4586}
4587
4588/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004589 * xmlSchemaCompareFloats:
4590 * @x: a first float or double value
4591 * @y: a second float or double value
4592 *
4593 * Compare 2 values
4594 *
4595 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4596 * case of error
4597 */
4598static int
4599xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4600 double d1, d2;
4601
4602 if ((x == NULL) || (y == NULL))
4603 return(-2);
4604
4605 /*
4606 * Cast everything to doubles.
4607 */
4608 if (x->type == XML_SCHEMAS_DOUBLE)
4609 d1 = x->value.d;
4610 else if (x->type == XML_SCHEMAS_FLOAT)
4611 d1 = x->value.f;
4612 else
4613 return(-2);
4614
4615 if (y->type == XML_SCHEMAS_DOUBLE)
4616 d2 = y->value.d;
4617 else if (y->type == XML_SCHEMAS_FLOAT)
4618 d2 = y->value.f;
4619 else
4620 return(-2);
4621
4622 /*
4623 * Check for special cases.
4624 */
4625 if (xmlXPathIsNaN(d1)) {
4626 if (xmlXPathIsNaN(d2))
4627 return(0);
4628 return(1);
4629 }
4630 if (xmlXPathIsNaN(d2))
4631 return(-1);
4632 if (d1 == xmlXPathPINF) {
4633 if (d2 == xmlXPathPINF)
4634 return(0);
4635 return(1);
4636 }
4637 if (d2 == xmlXPathPINF)
4638 return(-1);
4639 if (d1 == xmlXPathNINF) {
4640 if (d2 == xmlXPathNINF)
4641 return(0);
4642 return(-1);
4643 }
4644 if (d2 == xmlXPathNINF)
4645 return(1);
4646
4647 /*
4648 * basic tests, the last one we should have equality, but
4649 * portability is more important than speed and handling
4650 * NaN or Inf in a portable way is always a challenge, so ...
4651 */
4652 if (d1 < d2)
4653 return(-1);
4654 if (d1 > d2)
4655 return(1);
4656 if (d1 == d2)
4657 return(0);
4658 return(2);
4659}
4660
4661/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004662 * xmlSchemaCompareValues:
4663 * @x: a first value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004664 * @xvalue: the first value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004665 * @xwtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004666 * @y: a second value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004667 * @xvalue: the second value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004668 * @ywtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004669 *
4670 * Compare 2 values
4671 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004672 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4673 * comparable and -2 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004674 */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004675static int
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004676xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
4677 xmlSchemaValPtr x,
4678 const xmlChar *xvalue,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004679 xmlSchemaWhitespaceValueType xws,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004680 xmlSchemaValType ytype,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004681 xmlSchemaValPtr y,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004682 const xmlChar *yvalue,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004683 xmlSchemaWhitespaceValueType yws)
4684{
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004685 switch (xtype) {
Daniel Veillard80b19092003-03-28 13:29:53 +00004686 case XML_SCHEMAS_UNKNOWN:
William M. Brack2f2a6632004-08-20 23:09:47 +00004687 case XML_SCHEMAS_ANYTYPE:
Daniel Veillard80b19092003-03-28 13:29:53 +00004688 return(-2);
4689 case XML_SCHEMAS_INTEGER:
4690 case XML_SCHEMAS_NPINTEGER:
4691 case XML_SCHEMAS_NINTEGER:
4692 case XML_SCHEMAS_NNINTEGER:
4693 case XML_SCHEMAS_PINTEGER:
4694 case XML_SCHEMAS_INT:
4695 case XML_SCHEMAS_UINT:
4696 case XML_SCHEMAS_LONG:
4697 case XML_SCHEMAS_ULONG:
4698 case XML_SCHEMAS_SHORT:
4699 case XML_SCHEMAS_USHORT:
4700 case XML_SCHEMAS_BYTE:
4701 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00004702 case XML_SCHEMAS_DECIMAL:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004703 if ((x == NULL) || (y == NULL))
4704 return(-2);
4705 if (ytype == xtype)
Daniel Veillard80b19092003-03-28 13:29:53 +00004706 return(xmlSchemaCompareDecimals(x, y));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004707 if ((ytype == XML_SCHEMAS_DECIMAL) ||
4708 (ytype == XML_SCHEMAS_INTEGER) ||
4709 (ytype == XML_SCHEMAS_NPINTEGER) ||
4710 (ytype == XML_SCHEMAS_NINTEGER) ||
4711 (ytype == XML_SCHEMAS_NNINTEGER) ||
4712 (ytype == XML_SCHEMAS_PINTEGER) ||
4713 (ytype == XML_SCHEMAS_INT) ||
4714 (ytype == XML_SCHEMAS_UINT) ||
4715 (ytype == XML_SCHEMAS_LONG) ||
4716 (ytype == XML_SCHEMAS_ULONG) ||
4717 (ytype == XML_SCHEMAS_SHORT) ||
4718 (ytype == XML_SCHEMAS_USHORT) ||
4719 (ytype == XML_SCHEMAS_BYTE) ||
4720 (ytype == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00004721 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004722 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00004723 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004724 if ((x == NULL) || (y == NULL))
4725 return(-2);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004726 if (ytype == XML_SCHEMAS_DURATION)
Daniel Veillard070803b2002-05-03 07:29:38 +00004727 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004728 return(-2);
4729 case XML_SCHEMAS_TIME:
4730 case XML_SCHEMAS_GDAY:
4731 case XML_SCHEMAS_GMONTH:
4732 case XML_SCHEMAS_GMONTHDAY:
4733 case XML_SCHEMAS_GYEAR:
4734 case XML_SCHEMAS_GYEARMONTH:
4735 case XML_SCHEMAS_DATE:
4736 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004737 if ((x == NULL) || (y == NULL))
4738 return(-2);
4739 if ((ytype == XML_SCHEMAS_DATETIME) ||
4740 (ytype == XML_SCHEMAS_TIME) ||
4741 (ytype == XML_SCHEMAS_GDAY) ||
4742 (ytype == XML_SCHEMAS_GMONTH) ||
4743 (ytype == XML_SCHEMAS_GMONTHDAY) ||
4744 (ytype == XML_SCHEMAS_GYEAR) ||
4745 (ytype == XML_SCHEMAS_DATE) ||
4746 (ytype == XML_SCHEMAS_GYEARMONTH))
Daniel Veillard5a872412002-05-22 06:40:27 +00004747 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004748 return (-2);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004749 /*
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +00004750 * Note that we will support comparison of string types against
4751 * anySimpleType as well.
4752 */
4753 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004754 case XML_SCHEMAS_STRING:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004755 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00004756 case XML_SCHEMAS_TOKEN:
4757 case XML_SCHEMAS_LANGUAGE:
4758 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00004759 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00004760 case XML_SCHEMAS_NCNAME:
4761 case XML_SCHEMAS_ID:
4762 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00004763 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00004764 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004765 {
4766 const xmlChar *xv, *yv;
4767
4768 if (x == NULL)
4769 xv = xvalue;
4770 else
4771 xv = x->value.str;
4772 if (y == NULL)
4773 yv = yvalue;
4774 else
4775 yv = y->value.str;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004776 /*
4777 * TODO: Compare those against QName.
4778 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004779 if (ytype == XML_SCHEMAS_QNAME) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004780 TODO
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004781 if (y == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004782 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004783 return (-2);
4784 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004785 if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
4786 (ytype == XML_SCHEMAS_STRING) ||
4787 (ytype == XML_SCHEMAS_NORMSTRING) ||
4788 (ytype == XML_SCHEMAS_TOKEN) ||
4789 (ytype == XML_SCHEMAS_LANGUAGE) ||
4790 (ytype == XML_SCHEMAS_NMTOKEN) ||
4791 (ytype == XML_SCHEMAS_NAME) ||
4792 (ytype == XML_SCHEMAS_NCNAME) ||
4793 (ytype == XML_SCHEMAS_ID) ||
4794 (ytype == XML_SCHEMAS_IDREF) ||
4795 (ytype == XML_SCHEMAS_ENTITY) ||
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004796 (ytype == XML_SCHEMAS_ANYURI)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004797
4798 if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4799
4800 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4801 /* TODO: What about x < y or x > y. */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004802 if (xmlStrEqual(xv, yv))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004803 return (0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004804 else
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004805 return (2);
4806 } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004807 return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004808 else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004809 return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004810
4811 } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
4812
4813 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004814 return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004815 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004816 return (xmlSchemaCompareReplacedStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004817 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004818 return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004819
4820 } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
4821
4822 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004823 return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004824 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004825 return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004826 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004827 return (xmlSchemaCompareNormStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004828 } else
4829 return (-2);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004830
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004831 }
Daniel Veillardc4c21552003-03-29 10:53:38 +00004832 return (-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004833 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004834 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004835 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004836 if ((x == NULL) || (y == NULL))
4837 return(-2);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004838 if ((ytype == XML_SCHEMAS_QNAME) ||
4839 (ytype == XML_SCHEMAS_NOTATION)) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004840 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
4841 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
4842 return(0);
4843 return(2);
4844 }
4845 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004846 case XML_SCHEMAS_FLOAT:
4847 case XML_SCHEMAS_DOUBLE:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004848 if ((x == NULL) || (y == NULL))
4849 return(-2);
4850 if ((ytype == XML_SCHEMAS_FLOAT) ||
4851 (ytype == XML_SCHEMAS_DOUBLE))
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004852 return (xmlSchemaCompareFloats(x, y));
4853 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004854 case XML_SCHEMAS_BOOLEAN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004855 if ((x == NULL) || (y == NULL))
4856 return(-2);
4857 if (ytype == XML_SCHEMAS_BOOLEAN) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004858 if (x->value.b == y->value.b)
4859 return(0);
4860 if (x->value.b == 0)
4861 return(-1);
4862 return(1);
4863 }
4864 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00004865 case XML_SCHEMAS_HEXBINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004866 if ((x == NULL) || (y == NULL))
4867 return(-2);
4868 if (ytype == XML_SCHEMAS_HEXBINARY) {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00004869 if (x->value.hex.total == y->value.hex.total) {
4870 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
4871 if (ret > 0)
4872 return(1);
4873 else if (ret == 0)
4874 return(0);
4875 }
4876 else if (x->value.hex.total > y->value.hex.total)
4877 return(1);
4878
4879 return(-1);
4880 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00004881 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004882 case XML_SCHEMAS_BASE64BINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004883 if ((x == NULL) || (y == NULL))
4884 return(-2);
4885 if (ytype == XML_SCHEMAS_BASE64BINARY) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004886 if (x->value.base64.total == y->value.base64.total) {
4887 int ret = xmlStrcmp(x->value.base64.str,
4888 y->value.base64.str);
4889 if (ret > 0)
4890 return(1);
4891 else if (ret == 0)
4892 return(0);
Daniel Veillardd369b132005-07-14 15:54:44 +00004893 else
4894 return(-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004895 }
4896 else if (x->value.base64.total > y->value.base64.total)
4897 return(1);
4898 else
4899 return(-1);
4900 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004901 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004902 case XML_SCHEMAS_IDREFS:
4903 case XML_SCHEMAS_ENTITIES:
4904 case XML_SCHEMAS_NMTOKENS:
4905 TODO
4906 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004907 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004908 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00004909}
4910
4911/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004912 * xmlSchemaCompareValues:
4913 * @x: a first value
4914 * @y: a second value
4915 *
4916 * Compare 2 values
4917 *
4918 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4919 * case of error
4920 */
4921int
4922xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4923 xmlSchemaWhitespaceValueType xws, yws;
4924
Daniel Veillard5e094142005-02-18 19:36:12 +00004925 if ((x == NULL) || (y == NULL))
4926 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004927 if (x->type == XML_SCHEMAS_STRING)
4928 xws = XML_SCHEMA_WHITESPACE_PRESERVE;
4929 else if (x->type == XML_SCHEMAS_NORMSTRING)
4930 xws = XML_SCHEMA_WHITESPACE_REPLACE;
4931 else
4932 xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4933
4934 if (y->type == XML_SCHEMAS_STRING)
4935 yws = XML_SCHEMA_WHITESPACE_PRESERVE;
Gaurave79226c2013-11-28 22:50:57 +08004936 else if (y->type == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004937 yws = XML_SCHEMA_WHITESPACE_REPLACE;
4938 else
4939 yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4940
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004941 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4942 y, NULL, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004943}
4944
4945/**
4946 * xmlSchemaCompareValuesWhtsp:
4947 * @x: a first value
4948 * @xws: the whitespace value of x
4949 * @y: a second value
4950 * @yws: the whitespace value of y
4951 *
4952 * Compare 2 values
4953 *
4954 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4955 * case of error
4956 */
4957int
4958xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004959 xmlSchemaWhitespaceValueType xws,
4960 xmlSchemaValPtr y,
4961 xmlSchemaWhitespaceValueType yws)
4962{
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00004963 if ((x == NULL) || (y == NULL))
4964 return(-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004965 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4966 y, NULL, yws));
4967}
4968
4969/**
4970 * xmlSchemaCompareValuesWhtspExt:
4971 * @x: a first value
4972 * @xws: the whitespace value of x
4973 * @y: a second value
4974 * @yws: the whitespace value of y
4975 *
4976 * Compare 2 values
4977 *
4978 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4979 * case of error
4980 */
4981static int
4982xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
4983 xmlSchemaValPtr x,
4984 const xmlChar *xvalue,
4985 xmlSchemaWhitespaceValueType xws,
4986 xmlSchemaValType ytype,
4987 xmlSchemaValPtr y,
4988 const xmlChar *yvalue,
4989 xmlSchemaWhitespaceValueType yws)
4990{
4991 return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
4992 yvalue, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004993}
4994
4995/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004996 * xmlSchemaNormLen:
4997 * @value: a string
4998 *
4999 * Computes the UTF8 length of the normalized value of the string
5000 *
5001 * Returns the length or -1 in case of error.
5002 */
5003static int
5004xmlSchemaNormLen(const xmlChar *value) {
5005 const xmlChar *utf;
5006 int ret = 0;
5007
5008 if (value == NULL)
5009 return(-1);
5010 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00005011 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00005012 while (*utf != 0) {
5013 if (utf[0] & 0x80) {
5014 if ((utf[1] & 0xc0) != 0x80)
5015 return(-1);
5016 if ((utf[0] & 0xe0) == 0xe0) {
5017 if ((utf[2] & 0xc0) != 0x80)
5018 return(-1);
5019 if ((utf[0] & 0xf0) == 0xf0) {
5020 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
5021 return(-1);
5022 utf += 4;
5023 } else {
5024 utf += 3;
5025 }
5026 } else {
5027 utf += 2;
5028 }
William M. Brack76e95df2003-10-18 16:20:14 +00005029 } else if (IS_BLANK_CH(*utf)) {
5030 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00005031 if (*utf == 0)
5032 break;
5033 } else {
5034 utf++;
5035 }
5036 ret++;
5037 }
5038 return(ret);
5039}
5040
Daniel Veillard6927b102004-10-27 17:29:04 +00005041/**
5042 * xmlSchemaGetFacetValueAsULong:
5043 * @facet: an schemas type facet
5044 *
5045 * Extract the value of a facet
5046 *
5047 * Returns the value as a long
5048 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005049unsigned long
5050xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
5051{
5052 /*
5053 * TODO: Check if this is a decimal.
5054 */
William M. Brack094dd862004-11-14 14:28:34 +00005055 if (facet == NULL)
5056 return 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005057 return ((unsigned long) facet->val->value.decimal.lo);
5058}
5059
Daniel Veillardc4c21552003-03-29 10:53:38 +00005060/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00005061 * xmlSchemaValidateListSimpleTypeFacet:
5062 * @facet: the facet to check
5063 * @value: the lexical repr of the value to validate
5064 * @actualLen: the number of list items
5065 * @expectedLen: the resulting expected number of list items
5066 *
5067 * Checks the value of a list simple type against a facet.
5068 *
5069 * Returns 0 if the value is valid, a positive error code
5070 * number otherwise and -1 in case of an internal error.
5071 */
5072int
5073xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
5074 const xmlChar *value,
5075 unsigned long actualLen,
5076 unsigned long *expectedLen)
5077{
Daniel Veillardce682bc2004-11-05 17:22:25 +00005078 if (facet == NULL)
5079 return(-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005080 /*
5081 * TODO: Check if this will work with large numbers.
5082 * (compare value.decimal.mi and value.decimal.hi as well?).
5083 */
5084 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5085 if (actualLen != facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005086 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005087 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005088 return (XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005089 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005090 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5091 if (actualLen < facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005092 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005093 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005094 return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
5095 }
5096 } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
5097 if (actualLen > facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005098 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005099 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005100 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5101 }
5102 } else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005103 /*
5104 * NOTE: That we can pass NULL as xmlSchemaValPtr to
Daniel Veillard01fa6152004-06-29 17:04:39 +00005105 * xmlSchemaValidateFacet, since the remaining facet types
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005106 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
Daniel Veillard01fa6152004-06-29 17:04:39 +00005107 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005108 return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
Daniel Veillard01fa6152004-06-29 17:04:39 +00005109 return (0);
5110}
5111
5112/**
Daniel Veillard6927b102004-10-27 17:29:04 +00005113 * xmlSchemaValidateLengthFacet:
Daniel Veillardc0826a72004-08-10 14:17:33 +00005114 * @type: the built-in type
5115 * @facet: the facet to check
5116 * @value: the lexical repr. of the value to be validated
5117 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005118 * @ws: the whitespace type of the value
5119 * @length: the actual length of the value
5120 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005121 * Checka a value against a "length", "minLength" and "maxLength"
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005122 * facet; sets @length to the computed length of @value.
5123 *
5124 * Returns 0 if the value is valid, a positive error code
5125 * otherwise and -1 in case of an internal or API error.
5126 */
5127static int
5128xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
Nico Weberaae48e62012-02-29 09:44:35 +08005129 xmlSchemaValType valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005130 const xmlChar *value,
Nico Weberaae48e62012-02-29 09:44:35 +08005131 xmlSchemaValPtr val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005132 unsigned long *length,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005133 xmlSchemaWhitespaceValueType ws)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005134{
5135 unsigned int len = 0;
5136
5137 if ((length == NULL) || (facet == NULL))
5138 return (-1);
5139 *length = 0;
5140 if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
5141 (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
5142 (facet->type != XML_SCHEMA_FACET_MINLENGTH))
5143 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005144
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005145 /*
5146 * TODO: length, maxLength and minLength must be of type
5147 * nonNegativeInteger only. Check if decimal is used somehow.
5148 */
5149 if ((facet->val == NULL) ||
5150 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5151 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5152 (facet->val->value.decimal.frac != 0)) {
5153 return(-1);
5154 }
5155 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
5156 len = val->value.hex.total;
5157 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5158 len = val->value.base64.total;
5159 else {
5160 switch (valType) {
5161 case XML_SCHEMAS_STRING:
5162 case XML_SCHEMAS_NORMSTRING:
5163 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5164 /*
5165 * This is to ensure API compatibility with the old
5166 * xmlSchemaValidateLengthFacet(). Anyway, this was and
5167 * is not the correct handling.
5168 * TODO: Get rid of this case somehow.
5169 */
5170 if (valType == XML_SCHEMAS_STRING)
5171 len = xmlUTF8Strlen(value);
5172 else
5173 len = xmlSchemaNormLen(value);
5174 } else if (value != NULL) {
5175 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5176 len = xmlSchemaNormLen(value);
5177 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005178 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005179 * Should be OK for "preserve" as well.
5180 */
5181 len = xmlUTF8Strlen(value);
5182 }
5183 break;
5184 case XML_SCHEMAS_IDREF:
5185 case XML_SCHEMAS_TOKEN:
5186 case XML_SCHEMAS_LANGUAGE:
5187 case XML_SCHEMAS_NMTOKEN:
5188 case XML_SCHEMAS_NAME:
5189 case XML_SCHEMAS_NCNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005190 case XML_SCHEMAS_ID:
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005191 /*
5192 * FIXME: What exactly to do with anyURI?
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005193 */
5194 case XML_SCHEMAS_ANYURI:
5195 if (value != NULL)
5196 len = xmlSchemaNormLen(value);
5197 break;
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005198 case XML_SCHEMAS_QNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005199 case XML_SCHEMAS_NOTATION:
5200 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005201 * For QName and NOTATION, those facets are
5202 * deprecated and should be ignored.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005203 */
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005204 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005205 default:
5206 TODO
5207 }
5208 }
5209 *length = (unsigned long) len;
5210 /*
5211 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
5212 */
5213 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5214 if (len != facet->val->value.decimal.lo)
5215 return(XML_SCHEMAV_CVC_LENGTH_VALID);
5216 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5217 if (len < facet->val->value.decimal.lo)
5218 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
5219 } else {
5220 if (len > facet->val->value.decimal.lo)
5221 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5222 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005223
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005224 return (0);
5225}
5226
5227/**
5228 * xmlSchemaValidateLengthFacet:
5229 * @type: the built-in type
5230 * @facet: the facet to check
5231 * @value: the lexical repr. of the value to be validated
5232 * @val: the precomputed value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005233 * @length: the actual length of the value
5234 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005235 * Checka a value against a "length", "minLength" and "maxLength"
Daniel Veillardc0826a72004-08-10 14:17:33 +00005236 * facet; sets @length to the computed length of @value.
5237 *
5238 * Returns 0 if the value is valid, a positive error code
5239 * otherwise and -1 in case of an internal or API error.
5240 */
5241int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005242xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005243 xmlSchemaFacetPtr facet,
5244 const xmlChar *value,
5245 xmlSchemaValPtr val,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005246 unsigned long *length)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005247{
Daniel Veillardcc5e2332005-03-16 21:55:35 +00005248 if (type == NULL)
5249 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005250 return (xmlSchemaValidateLengthFacetInternal(facet,
5251 type->builtInType, value, val, length,
5252 XML_SCHEMA_WHITESPACE_UNKNOWN));
5253}
Daniel Veillardc0826a72004-08-10 14:17:33 +00005254
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005255/**
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005256 * xmlSchemaValidateLengthFacetWhtsp:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005257 * @facet: the facet to check
5258 * @valType: the built-in type
5259 * @value: the lexical repr. of the value to be validated
5260 * @val: the precomputed value
5261 * @ws: the whitespace type of the value
5262 * @length: the actual length of the value
5263 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005264 * Checka a value against a "length", "minLength" and "maxLength"
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005265 * facet; sets @length to the computed length of @value.
5266 *
5267 * Returns 0 if the value is valid, a positive error code
5268 * otherwise and -1 in case of an internal or API error.
5269 */
5270int
5271xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
5272 xmlSchemaValType valType,
5273 const xmlChar *value,
5274 xmlSchemaValPtr val,
5275 unsigned long *length,
5276 xmlSchemaWhitespaceValueType ws)
5277{
5278 return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
5279 length, ws));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005280}
5281
5282/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005283 * xmlSchemaValidateFacetInternal:
Daniel Veillard4255d502002-04-16 15:50:10 +00005284 * @facet: the facet to check
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005285 * @fws: the whitespace type of the facet's value
5286 * @valType: the built-in type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005287 * @value: the lexical repr of the value to validate
5288 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005289 * @ws: the whitespace type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005290 *
5291 * Check a value against a facet condition
5292 *
5293 * Returns 0 if the element is schemas valid, a positive error code
5294 * number otherwise and -1 in case of internal or API error.
5295 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005296static int
5297xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
5298 xmlSchemaWhitespaceValueType fws,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005299 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005300 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005301 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005302 xmlSchemaWhitespaceValueType ws)
Daniel Veillard4255d502002-04-16 15:50:10 +00005303{
5304 int ret;
5305
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005306 if (facet == NULL)
5307 return(-1);
5308
Daniel Veillard4255d502002-04-16 15:50:10 +00005309 switch (facet->type) {
5310 case XML_SCHEMA_FACET_PATTERN:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005311 /*
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005312 * NOTE that for patterns, the @value needs to be the normalized
5313 * value, *not* the lexical initial value or the canonical value.
5314 */
5315 if (value == NULL)
5316 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005317 ret = xmlRegexpExec(facet->regexp, value);
5318 if (ret == 1)
5319 return(0);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005320 if (ret == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005321 return(XML_SCHEMAV_CVC_PATTERN_VALID);
Daniel Veillard4255d502002-04-16 15:50:10 +00005322 return(ret);
5323 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
5324 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005325 if (ret == -2)
Daniel Veillard4255d502002-04-16 15:50:10 +00005326 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005327 if (ret == -1)
5328 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005329 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005330 case XML_SCHEMA_FACET_MAXINCLUSIVE:
5331 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005332 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005333 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005334 if ((ret == -1) || (ret == 0))
5335 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005336 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005337 case XML_SCHEMA_FACET_MINEXCLUSIVE:
5338 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005339 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005340 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005341 if (ret == 1)
5342 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005343 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005344 case XML_SCHEMA_FACET_MININCLUSIVE:
5345 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005346 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005347 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005348 if ((ret == 1) || (ret == 0))
5349 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005350 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
Daniel Veillard8651f532002-04-17 09:06:27 +00005351 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005352 /* TODO whitespaces */
Daniel Veillard01fa6152004-06-29 17:04:39 +00005353 /*
5354 * NOTE: Whitespace should be handled to normalize
5355 * the value to be validated against a the facets;
5356 * not to normalize the value in-between.
5357 */
Daniel Veillard8651f532002-04-17 09:06:27 +00005358 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00005359 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005360 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5361 /*
5362 * This is to ensure API compatibility with the old
5363 * xmlSchemaValidateFacet().
5364 * TODO: Get rid of this case.
5365 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005366 if ((facet->value != NULL) &&
5367 (xmlStrEqual(facet->value, value)))
5368 return(0);
5369 } else {
5370 ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
5371 facet->val, facet->value, fws, valType, val,
5372 value, ws);
5373 if (ret == -2)
5374 return(-1);
5375 if (ret == 0)
5376 return(0);
5377 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005378 return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005379 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005380 /*
5381 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5382 * then any {value} is facet-valid."
5383 */
5384 if ((valType == XML_SCHEMAS_QNAME) ||
5385 (valType == XML_SCHEMAS_NOTATION))
5386 return (0);
5387 /* No break on purpose. */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005388 case XML_SCHEMA_FACET_MAXLENGTH:
5389 case XML_SCHEMA_FACET_MINLENGTH: {
5390 unsigned int len = 0;
5391
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005392 if ((valType == XML_SCHEMAS_QNAME) ||
5393 (valType == XML_SCHEMAS_NOTATION))
5394 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005395 /*
5396 * TODO: length, maxLength and minLength must be of type
5397 * nonNegativeInteger only. Check if decimal is used somehow.
5398 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005399 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005400 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5401 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005402 (facet->val->value.decimal.frac != 0)) {
5403 return(-1);
5404 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005405 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005406 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00005407 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5408 len = val->value.base64.total;
5409 else {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005410 switch (valType) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005411 case XML_SCHEMAS_STRING:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005412 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005413 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5414 /*
5415 * This is to ensure API compatibility with the old
5416 * xmlSchemaValidateFacet(). Anyway, this was and
5417 * is not the correct handling.
5418 * TODO: Get rid of this case somehow.
5419 */
5420 if (valType == XML_SCHEMAS_STRING)
5421 len = xmlUTF8Strlen(value);
5422 else
5423 len = xmlSchemaNormLen(value);
5424 } else if (value != NULL) {
5425 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5426 len = xmlSchemaNormLen(value);
5427 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005428 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005429 * Should be OK for "preserve" as well.
5430 */
5431 len = xmlUTF8Strlen(value);
5432 }
5433 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005434 case XML_SCHEMAS_IDREF:
Daniel Veillard560c2a42003-07-06 21:13:49 +00005435 case XML_SCHEMAS_TOKEN:
5436 case XML_SCHEMAS_LANGUAGE:
5437 case XML_SCHEMAS_NMTOKEN:
5438 case XML_SCHEMAS_NAME:
5439 case XML_SCHEMAS_NCNAME:
5440 case XML_SCHEMAS_ID:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005441 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005442 if (value != NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005443 len = xmlSchemaNormLen(value);
5444 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00005445 default:
5446 TODO
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005447 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005448 }
5449 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005450 if (len != facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005451 return(XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005452 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005453 if (len < facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005454 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005455 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005456 if (len > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005457 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005458 }
5459 break;
5460 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005461 case XML_SCHEMA_FACET_TOTALDIGITS:
5462 case XML_SCHEMA_FACET_FRACTIONDIGITS:
5463
5464 if ((facet->val == NULL) ||
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00005465 ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
Daniel Veillard560c2a42003-07-06 21:13:49 +00005466 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5467 (facet->val->value.decimal.frac != 0)) {
5468 return(-1);
5469 }
5470 if ((val == NULL) ||
5471 ((val->type != XML_SCHEMAS_DECIMAL) &&
5472 (val->type != XML_SCHEMAS_INTEGER) &&
5473 (val->type != XML_SCHEMAS_NPINTEGER) &&
5474 (val->type != XML_SCHEMAS_NINTEGER) &&
5475 (val->type != XML_SCHEMAS_NNINTEGER) &&
5476 (val->type != XML_SCHEMAS_PINTEGER) &&
5477 (val->type != XML_SCHEMAS_INT) &&
5478 (val->type != XML_SCHEMAS_UINT) &&
5479 (val->type != XML_SCHEMAS_LONG) &&
5480 (val->type != XML_SCHEMAS_ULONG) &&
5481 (val->type != XML_SCHEMAS_SHORT) &&
5482 (val->type != XML_SCHEMAS_USHORT) &&
5483 (val->type != XML_SCHEMAS_BYTE) &&
5484 (val->type != XML_SCHEMAS_UBYTE))) {
5485 return(-1);
5486 }
5487 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
5488 if (val->value.decimal.total > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005489 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005490
5491 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
5492 if (val->value.decimal.frac > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005493 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005494 }
5495 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00005496 default:
5497 TODO
5498 }
5499 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00005500
Daniel Veillard4255d502002-04-16 15:50:10 +00005501}
5502
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005503/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005504 * xmlSchemaValidateFacet:
5505 * @base: the base type
5506 * @facet: the facet to check
5507 * @value: the lexical repr of the value to validate
5508 * @val: the precomputed value
5509 *
5510 * Check a value against a facet condition
5511 *
5512 * Returns 0 if the element is schemas valid, a positive error code
5513 * number otherwise and -1 in case of internal or API error.
5514 */
5515int
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005516xmlSchemaValidateFacet(xmlSchemaTypePtr base,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005517 xmlSchemaFacetPtr facet,
5518 const xmlChar *value,
5519 xmlSchemaValPtr val)
5520{
5521 /*
5522 * This tries to ensure API compatibility regarding the old
5523 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5524 * xmlSchemaValidateFacetWhtsp().
5525 */
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005526 if (val != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005527 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005528 XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005529 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005530 else if (base != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005531 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005532 XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005533 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005534 return(-1);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005535}
5536
5537/**
5538 * xmlSchemaValidateFacetWhtsp:
5539 * @facet: the facet to check
5540 * @fws: the whitespace type of the facet's value
5541 * @valType: the built-in type of the value
5542 * @value: the lexical (or normalized for pattern) repr of the value to validate
5543 * @val: the precomputed value
5544 * @ws: the whitespace type of the value
5545 *
5546 * Check a value against a facet condition. This takes value normalization
5547 * according to the specified whitespace types into account.
5548 * Note that @value needs to be the *normalized* value if the facet
5549 * is of type "pattern".
5550 *
5551 * Returns 0 if the element is schemas valid, a positive error code
5552 * number otherwise and -1 in case of internal or API error.
5553 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005554int
5555xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
5556 xmlSchemaWhitespaceValueType fws,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005557 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005558 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005559 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005560 xmlSchemaWhitespaceValueType ws)
5561{
5562 return(xmlSchemaValidateFacetInternal(facet, fws, valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005563 value, val, ws));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005564}
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005565
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005566#if 0
5567#ifndef DBL_DIG
5568#define DBL_DIG 16
5569#endif
5570#ifndef DBL_EPSILON
5571#define DBL_EPSILON 1E-9
5572#endif
5573
5574#define INTEGER_DIGITS DBL_DIG
5575#define FRACTION_DIGITS (DBL_DIG + 1)
5576#define EXPONENT_DIGITS (3 + 2)
5577
5578/**
5579 * xmlXPathFormatNumber:
5580 * @number: number to format
5581 * @buffer: output buffer
5582 * @buffersize: size of output buffer
5583 *
5584 * Convert the number into a string representation.
5585 */
5586static void
5587xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
5588{
5589 switch (xmlXPathIsInf(number)) {
5590 case 1:
5591 if (buffersize > (int)sizeof("INF"))
5592 snprintf(buffer, buffersize, "INF");
5593 break;
5594 case -1:
5595 if (buffersize > (int)sizeof("-INF"))
5596 snprintf(buffer, buffersize, "-INF");
5597 break;
5598 default:
5599 if (xmlXPathIsNaN(number)) {
5600 if (buffersize > (int)sizeof("NaN"))
5601 snprintf(buffer, buffersize, "NaN");
5602 } else if (number == 0) {
5603 snprintf(buffer, buffersize, "0.0E0");
5604 } else {
5605 /* 3 is sign, decimal point, and terminating zero */
5606 char work[DBL_DIG + EXPONENT_DIGITS + 3];
5607 int integer_place, fraction_place;
5608 char *ptr;
5609 char *after_fraction;
5610 double absolute_value;
5611 int size;
5612
5613 absolute_value = fabs(number);
5614
5615 /*
5616 * Result is in work, and after_fraction points
5617 * just past the fractional part.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005618 * Use scientific notation
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005619 */
5620 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
5621 fraction_place = DBL_DIG - 1;
5622 snprintf(work, sizeof(work),"%*.*e",
5623 integer_place, fraction_place, number);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005624 after_fraction = strchr(work + DBL_DIG, 'e');
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005625 /* Remove fractional trailing zeroes */
5626 ptr = after_fraction;
5627 while (*(--ptr) == '0')
5628 ;
5629 if (*ptr != '.')
5630 ptr++;
5631 while ((*ptr++ = *after_fraction++) != 0);
5632
5633 /* Finally copy result back to caller */
5634 size = strlen(work) + 1;
5635 if (size > buffersize) {
5636 work[buffersize - 1] = 0;
5637 size = buffersize;
5638 }
5639 memmove(buffer, work, size);
5640 }
5641 break;
5642 }
5643}
5644#endif
5645
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005646/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005647 * xmlSchemaGetCanonValue:
5648 * @val: the precomputed value
5649 * @retValue: the returned value
5650 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01005651 * Get the canonical lexical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005652 * The caller has to FREE the returned retValue.
5653 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005654 * WARNING: Some value types are not supported yet, resulting
5655 * in a @retValue of "???".
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005656 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005657 * TODO: XML Schema 1.0 does not define canonical representations
5658 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5659 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005660 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005661 *
5662 * Returns 0 if the value could be built, 1 if the value type is
5663 * not supported yet and -1 in case of API errors.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005664 */
5665int
Daniel Veillardb5839c32005-02-19 18:27:14 +00005666xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005667{
Daniel Veillardb5839c32005-02-19 18:27:14 +00005668 if ((retValue == NULL) || (val == NULL))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005669 return (-1);
5670 *retValue = NULL;
5671 switch (val->type) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005672 case XML_SCHEMAS_STRING:
5673 if (val->value.str == NULL)
5674 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5675 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005676 *retValue =
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005677 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5678 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005679 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005680 if (val->value.str == NULL)
5681 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5682 else {
5683 *retValue = xmlSchemaWhiteSpaceReplace(
5684 (const xmlChar *) val->value.str);
5685 if ((*retValue) == NULL)
5686 *retValue = BAD_CAST xmlStrdup(
5687 (const xmlChar *) val->value.str);
5688 }
5689 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005690 case XML_SCHEMAS_TOKEN:
5691 case XML_SCHEMAS_LANGUAGE:
5692 case XML_SCHEMAS_NMTOKEN:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005693 case XML_SCHEMAS_NAME:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005694 case XML_SCHEMAS_NCNAME:
5695 case XML_SCHEMAS_ID:
5696 case XML_SCHEMAS_IDREF:
5697 case XML_SCHEMAS_ENTITY:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005698 case XML_SCHEMAS_NOTATION: /* Unclear */
5699 case XML_SCHEMAS_ANYURI: /* Unclear */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005700 if (val->value.str == NULL)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005701 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005702 *retValue =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005703 BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
5704 if (*retValue == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005705 *retValue =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005706 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005707 break;
5708 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005709 /* TODO: Unclear in XML Schema 1.0. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005710 if (val->value.qname.uri == NULL) {
5711 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
5712 return (0);
5713 } else {
5714 *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
5715 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5716 BAD_CAST val->value.qname.uri);
5717 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5718 BAD_CAST "}");
5719 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5720 BAD_CAST val->value.qname.uri);
5721 }
5722 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005723 case XML_SCHEMAS_DECIMAL:
5724 /*
5725 * TODO: Lookout for a more simple implementation.
5726 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005727 if ((val->value.decimal.total == 1) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005728 (val->value.decimal.lo == 0)) {
5729 *retValue = xmlStrdup(BAD_CAST "0.0");
5730 } else {
5731 xmlSchemaValDecimal dec = val->value.decimal;
5732 int bufsize;
5733 char *buf = NULL, *offs;
5734
5735 /* Add room for the decimal point as well. */
5736 bufsize = dec.total + 2;
5737 if (dec.sign)
5738 bufsize++;
5739 /* Add room for leading/trailing zero. */
5740 if ((dec.frac == 0) || (dec.frac == dec.total))
5741 bufsize++;
5742 buf = xmlMalloc(bufsize);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005743 if (buf == NULL)
5744 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005745 offs = buf;
5746 if (dec.sign)
5747 *offs++ = '-';
5748 if (dec.frac == dec.total) {
5749 *offs++ = '0';
5750 *offs++ = '.';
5751 }
5752 if (dec.hi != 0)
5753 snprintf(offs, bufsize - (offs - buf),
5754 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5755 else if (dec.mi != 0)
5756 snprintf(offs, bufsize - (offs - buf),
5757 "%lu%lu", dec.mi, dec.lo);
5758 else
5759 snprintf(offs, bufsize - (offs - buf),
5760 "%lu", dec.lo);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005761
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005762 if (dec.frac != 0) {
5763 if (dec.frac != dec.total) {
5764 int diff = dec.total - dec.frac;
5765 /*
5766 * Insert the decimal point.
5767 */
5768 memmove(offs + diff + 1, offs + diff, dec.frac +1);
5769 offs[diff] = '.';
5770 } else {
5771 unsigned int i = 0;
5772 /*
5773 * Insert missing zeroes behind the decimal point.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005774 */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005775 while (*(offs + i) != 0)
5776 i++;
5777 if (i < dec.total) {
5778 memmove(offs + (dec.total - i), offs, i +1);
5779 memset(offs, '0', dec.total - i);
5780 }
5781 }
5782 } else {
5783 /*
5784 * Append decimal point and zero.
5785 */
5786 offs = buf + bufsize - 1;
5787 *offs-- = 0;
5788 *offs-- = '0';
5789 *offs-- = '.';
5790 }
5791 *retValue = BAD_CAST buf;
5792 }
5793 break;
5794 case XML_SCHEMAS_INTEGER:
5795 case XML_SCHEMAS_PINTEGER:
5796 case XML_SCHEMAS_NPINTEGER:
5797 case XML_SCHEMAS_NINTEGER:
5798 case XML_SCHEMAS_NNINTEGER:
5799 case XML_SCHEMAS_LONG:
5800 case XML_SCHEMAS_BYTE:
5801 case XML_SCHEMAS_SHORT:
5802 case XML_SCHEMAS_INT:
5803 case XML_SCHEMAS_UINT:
5804 case XML_SCHEMAS_ULONG:
5805 case XML_SCHEMAS_USHORT:
5806 case XML_SCHEMAS_UBYTE:
5807 if ((val->value.decimal.total == 1) &&
5808 (val->value.decimal.lo == 0))
5809 *retValue = xmlStrdup(BAD_CAST "0");
5810 else {
5811 xmlSchemaValDecimal dec = val->value.decimal;
5812 int bufsize = dec.total + 1;
5813
5814 /* Add room for the decimal point as well. */
5815 if (dec.sign)
5816 bufsize++;
5817 *retValue = xmlMalloc(bufsize);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005818 if (*retValue == NULL)
5819 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005820 if (dec.hi != 0) {
5821 if (dec.sign)
5822 snprintf((char *) *retValue, bufsize,
5823 "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5824 else
5825 snprintf((char *) *retValue, bufsize,
5826 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5827 } else if (dec.mi != 0) {
5828 if (dec.sign)
5829 snprintf((char *) *retValue, bufsize,
5830 "-%lu%lu", dec.mi, dec.lo);
5831 else
5832 snprintf((char *) *retValue, bufsize,
5833 "%lu%lu", dec.mi, dec.lo);
5834 } else {
5835 if (dec.sign)
5836 snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
5837 else
5838 snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
5839 }
5840 }
5841 break;
5842 case XML_SCHEMAS_BOOLEAN:
5843 if (val->value.b)
5844 *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
5845 else
5846 *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
5847 break;
5848 case XML_SCHEMAS_DURATION: {
5849 char buf[100];
5850 unsigned long year;
5851 unsigned long mon, day, hour = 0, min = 0;
5852 double sec = 0, left;
5853
5854 /* TODO: Unclear in XML Schema 1.0 */
5855 /*
5856 * TODO: This results in a normalized output of the value
5857 * - which is NOT conformant to the spec -
5858 * since the exact values of each property are not
5859 * recoverable. Think about extending the structure to
5860 * provide a field for every property.
5861 */
5862 year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
5863 mon = labs(val->value.dur.mon) - 12 * year;
5864
5865 day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
5866 left = fabs(val->value.dur.sec) - day * 86400;
5867 if (left > 0) {
5868 hour = (unsigned long) FQUOTIENT(left, 3600);
5869 left = left - (hour * 3600);
5870 if (left > 0) {
5871 min = (unsigned long) FQUOTIENT(left, 60);
5872 sec = left - (min * 60);
5873 }
5874 }
5875 if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
5876 snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5877 year, mon, day, hour, min, sec);
5878 else
5879 snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5880 year, mon, day, hour, min, sec);
5881 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5882 }
5883 break;
5884 case XML_SCHEMAS_GYEAR: {
5885 char buf[30];
5886 /* TODO: Unclear in XML Schema 1.0 */
5887 /* TODO: What to do with the timezone? */
5888 snprintf(buf, 30, "%04ld", val->value.date.year);
5889 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5890 }
5891 break;
5892 case XML_SCHEMAS_GMONTH: {
5893 /* TODO: Unclear in XML Schema 1.0 */
5894 /* TODO: What to do with the timezone? */
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005895 *retValue = xmlMalloc(6);
5896 if (*retValue == NULL)
5897 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005898 snprintf((char *) *retValue, 6, "--%02u",
5899 val->value.date.mon);
5900 }
5901 break;
5902 case XML_SCHEMAS_GDAY: {
5903 /* TODO: Unclear in XML Schema 1.0 */
5904 /* TODO: What to do with the timezone? */
5905 *retValue = xmlMalloc(6);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005906 if (*retValue == NULL)
5907 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005908 snprintf((char *) *retValue, 6, "---%02u",
5909 val->value.date.day);
5910 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005911 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005912 case XML_SCHEMAS_GMONTHDAY: {
5913 /* TODO: Unclear in XML Schema 1.0 */
5914 /* TODO: What to do with the timezone? */
5915 *retValue = xmlMalloc(8);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005916 if (*retValue == NULL)
5917 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005918 snprintf((char *) *retValue, 8, "--%02u-%02u",
5919 val->value.date.mon, val->value.date.day);
5920 }
5921 break;
5922 case XML_SCHEMAS_GYEARMONTH: {
5923 char buf[35];
5924 /* TODO: Unclear in XML Schema 1.0 */
5925 /* TODO: What to do with the timezone? */
5926 if (val->value.date.year < 0)
5927 snprintf(buf, 35, "-%04ld-%02u",
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005928 labs(val->value.date.year),
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005929 val->value.date.mon);
5930 else
5931 snprintf(buf, 35, "%04ld-%02u",
5932 val->value.date.year, val->value.date.mon);
5933 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5934 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005935 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005936 case XML_SCHEMAS_TIME:
5937 {
5938 char buf[30];
5939
5940 if (val->value.date.tz_flag) {
5941 xmlSchemaValPtr norm;
5942
5943 norm = xmlSchemaDateNormalize(val, 0);
5944 if (norm == NULL)
5945 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005946 /*
5947 * TODO: Check if "%.14g" is portable.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005948 */
5949 snprintf(buf, 30,
5950 "%02u:%02u:%02.14gZ",
5951 norm->value.date.hour,
5952 norm->value.date.min,
5953 norm->value.date.sec);
5954 xmlSchemaFreeValue(norm);
5955 } else {
5956 snprintf(buf, 30,
5957 "%02u:%02u:%02.14g",
5958 val->value.date.hour,
5959 val->value.date.min,
5960 val->value.date.sec);
5961 }
5962 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005963 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005964 break;
5965 case XML_SCHEMAS_DATE:
5966 {
5967 char buf[30];
5968
5969 if (val->value.date.tz_flag) {
5970 xmlSchemaValPtr norm;
5971
5972 norm = xmlSchemaDateNormalize(val, 0);
5973 if (norm == NULL)
5974 return (-1);
5975 /*
5976 * TODO: Append the canonical value of the
5977 * recoverable timezone and not "Z".
5978 */
5979 snprintf(buf, 30,
5980 "%04ld:%02u:%02uZ",
5981 norm->value.date.year, norm->value.date.mon,
5982 norm->value.date.day);
5983 xmlSchemaFreeValue(norm);
5984 } else {
5985 snprintf(buf, 30,
5986 "%04ld:%02u:%02u",
5987 val->value.date.year, val->value.date.mon,
5988 val->value.date.day);
5989 }
5990 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005991 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005992 break;
5993 case XML_SCHEMAS_DATETIME:
5994 {
5995 char buf[50];
5996
5997 if (val->value.date.tz_flag) {
5998 xmlSchemaValPtr norm;
5999
6000 norm = xmlSchemaDateNormalize(val, 0);
6001 if (norm == NULL)
6002 return (-1);
6003 /*
6004 * TODO: Check if "%.14g" is portable.
6005 */
6006 snprintf(buf, 50,
6007 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
6008 norm->value.date.year, norm->value.date.mon,
6009 norm->value.date.day, norm->value.date.hour,
6010 norm->value.date.min, norm->value.date.sec);
6011 xmlSchemaFreeValue(norm);
6012 } else {
6013 snprintf(buf, 50,
6014 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
6015 val->value.date.year, val->value.date.mon,
6016 val->value.date.day, val->value.date.hour,
6017 val->value.date.min, val->value.date.sec);
6018 }
6019 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6020 }
6021 break;
6022 case XML_SCHEMAS_HEXBINARY:
6023 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
6024 break;
6025 case XML_SCHEMAS_BASE64BINARY:
6026 /*
6027 * TODO: Is the following spec piece implemented?:
6028 * SPEC: "Note: For some values the canonical form defined
6029 * above does not conform to [RFC 2045], which requires breaking
6030 * with linefeeds at appropriate intervals."
6031 */
6032 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
6033 break;
6034 case XML_SCHEMAS_FLOAT: {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006035 char buf[30];
6036 /*
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006037 * |m| < 16777216, -149 <= e <= 104.
6038 * TODO: Handle, NaN, INF, -INF. The format is not
6039 * yet conformant. The c type float does not cover
6040 * the whole range.
6041 */
6042 snprintf(buf, 30, "%01.14e", val->value.f);
6043 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6044 }
6045 break;
6046 case XML_SCHEMAS_DOUBLE: {
6047 char buf[40];
6048 /* |m| < 9007199254740992, -1075 <= e <= 970 */
6049 /*
6050 * TODO: Handle, NaN, INF, -INF. The format is not
6051 * yet conformant. The c type float does not cover
6052 * the whole range.
6053 */
6054 snprintf(buf, 40, "%01.14e", val->value.d);
6055 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6056 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006057 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006058 default:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006059 *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006060 return (1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006061 }
Daniel Veillard26ab0e62006-10-11 12:32:51 +00006062 if (*retValue == NULL)
6063 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006064 return (0);
6065}
6066
Daniel Veillardbda59572005-04-01 17:15:17 +00006067/**
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006068 * xmlSchemaGetCanonValueWhtsp:
6069 * @val: the precomputed value
6070 * @retValue: the returned value
6071 * @ws: the whitespace type of the value
6072 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01006073 * Get the canonical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006074 * The caller has to free the returned @retValue.
6075 *
6076 * Returns 0 if the value could be built, 1 if the value type is
6077 * not supported yet and -1 in case of API errors.
6078 */
6079int
6080xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
6081 const xmlChar **retValue,
6082 xmlSchemaWhitespaceValueType ws)
6083{
6084 if ((retValue == NULL) || (val == NULL))
6085 return (-1);
6086 if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
6087 (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
6088 return (-1);
6089
6090 *retValue = NULL;
6091 switch (val->type) {
6092 case XML_SCHEMAS_STRING:
6093 if (val->value.str == NULL)
6094 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6095 else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6096 *retValue = xmlSchemaCollapseString(val->value.str);
6097 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
6098 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6099 if ((*retValue) == NULL)
6100 *retValue = BAD_CAST xmlStrdup(val->value.str);
6101 break;
6102 case XML_SCHEMAS_NORMSTRING:
6103 if (val->value.str == NULL)
6104 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6105 else {
6106 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6107 *retValue = xmlSchemaCollapseString(val->value.str);
6108 else
6109 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6110 if ((*retValue) == NULL)
6111 *retValue = BAD_CAST xmlStrdup(val->value.str);
6112 }
6113 break;
6114 default:
6115 return (xmlSchemaGetCanonValue(val, retValue));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006116 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006117 return (0);
6118}
6119
6120/**
Daniel Veillardbda59572005-04-01 17:15:17 +00006121 * xmlSchemaGetValType:
6122 * @val: a schemas value
6123 *
6124 * Accessor for the type of a value
6125 *
6126 * Returns the xmlSchemaValType of the value
6127 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006128xmlSchemaValType
6129xmlSchemaGetValType(xmlSchemaValPtr val)
6130{
Daniel Veillardbda59572005-04-01 17:15:17 +00006131 if (val == NULL)
6132 return(XML_SCHEMAS_UNKNOWN);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006133 return (val->type);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006134}
6135
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006136#define bottom_xmlschemastypes
6137#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00006138#endif /* LIBXML_SCHEMAS_ENABLED */