blob: 60f5414223cd8861265c55bd3dcdf4aecc5c788b [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 */
Patrick Monnerata1dca812016-04-11 20:03:19 +020065 unsigned int hour :5; /* 0 <= hour <= 24 */
Daniel Veillard070803b2002-05-03 07:29:38 +000066 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
Patrick Monnerata1dca812016-04-11 20:03:19 +02001142#define VALID_END_OF_DAY(dt) \
1143 ((dt)->hour == 24 && (dt)->min == 0 && (dt)->sec == 0)
1144
Daniel Veillard070803b2002-05-03 07:29:38 +00001145#define VALID_TIME(dt) \
Patrick Monnerata1dca812016-04-11 20:03:19 +02001146 (((VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
1147 VALID_SEC(dt->sec)) || VALID_END_OF_DAY(dt)) && \
1148 VALID_TZO(dt->tzo))
Daniel Veillard070803b2002-05-03 07:29:38 +00001149
1150#define VALID_DATETIME(dt) \
1151 (VALID_DATE(dt) && VALID_TIME(dt))
1152
1153#define SECS_PER_MIN (60)
1154#define SECS_PER_HOUR (60 * SECS_PER_MIN)
1155#define SECS_PER_DAY (24 * SECS_PER_HOUR)
1156
Daniel Veillard5a872412002-05-22 06:40:27 +00001157static const long dayInYearByMonth[12] =
1158 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1159static const long dayInLeapYearByMonth[12] =
1160 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
1161
1162#define DAY_IN_YEAR(day, month, year) \
1163 ((IS_LEAP(year) ? \
1164 dayInLeapYearByMonth[month - 1] : \
1165 dayInYearByMonth[month - 1]) + day)
1166
1167#ifdef DEBUG
1168#define DEBUG_DATE(dt) \
1169 xmlGenericError(xmlGenericErrorContext, \
1170 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
1171 dt->type,dt->value.date.year,dt->value.date.mon, \
1172 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
1173 dt->value.date.sec); \
1174 if (dt->value.date.tz_flag) \
1175 if (dt->value.date.tzo != 0) \
1176 xmlGenericError(xmlGenericErrorContext, \
1177 "%+05d\n",dt->value.date.tzo); \
1178 else \
1179 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
1180 else \
1181 xmlGenericError(xmlGenericErrorContext,"\n")
1182#else
1183#define DEBUG_DATE(dt)
1184#endif
1185
Daniel Veillard070803b2002-05-03 07:29:38 +00001186/**
1187 * _xmlSchemaParseGYear:
1188 * @dt: pointer to a date structure
1189 * @str: pointer to the string to analyze
1190 *
1191 * Parses a xs:gYear without time zone and fills in the appropriate
1192 * field of the @dt structure. @str is updated to point just after the
1193 * xs:gYear. It is supposed that @dt->year is big enough to contain
1194 * the year.
1195 *
1196 * Returns 0 or the error code
1197 */
1198static int
1199_xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
1200 const xmlChar *cur = *str, *firstChar;
1201 int isneg = 0, digcnt = 0;
1202
1203 if (((*cur < '0') || (*cur > '9')) &&
1204 (*cur != '-') && (*cur != '+'))
1205 return -1;
1206
1207 if (*cur == '-') {
1208 isneg = 1;
1209 cur++;
1210 }
1211
1212 firstChar = cur;
1213
1214 while ((*cur >= '0') && (*cur <= '9')) {
1215 dt->year = dt->year * 10 + (*cur - '0');
1216 cur++;
1217 digcnt++;
1218 }
1219
1220 /* year must be at least 4 digits (CCYY); over 4
1221 * digits cannot have a leading zero. */
1222 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
1223 return 1;
1224
1225 if (isneg)
1226 dt->year = - dt->year;
1227
1228 if (!VALID_YEAR(dt->year))
1229 return 2;
1230
1231 *str = cur;
1232 return 0;
1233}
1234
1235/**
1236 * PARSE_2_DIGITS:
1237 * @num: the integer to fill in
1238 * @cur: an #xmlChar *
1239 * @invalid: an integer
1240 *
1241 * Parses a 2-digits integer and updates @num with the value. @cur is
1242 * updated to point just after the integer.
1243 * In case of error, @invalid is set to %TRUE, values of @num and
1244 * @cur are undefined.
1245 */
1246#define PARSE_2_DIGITS(num, cur, invalid) \
1247 if ((cur[0] < '0') || (cur[0] > '9') || \
1248 (cur[1] < '0') || (cur[1] > '9')) \
1249 invalid = 1; \
1250 else \
1251 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
1252 cur += 2;
1253
1254/**
1255 * PARSE_FLOAT:
1256 * @num: the double to fill in
1257 * @cur: an #xmlChar *
1258 * @invalid: an integer
1259 *
1260 * Parses a float and updates @num with the value. @cur is
1261 * updated to point just after the float. The float must have a
1262 * 2-digits integer part and may or may not have a decimal part.
1263 * In case of error, @invalid is set to %TRUE, values of @num and
1264 * @cur are undefined.
1265 */
1266#define PARSE_FLOAT(num, cur, invalid) \
1267 PARSE_2_DIGITS(num, cur, invalid); \
1268 if (!invalid && (*cur == '.')) { \
1269 double mult = 1; \
1270 cur++; \
1271 if ((*cur < '0') || (*cur > '9')) \
1272 invalid = 1; \
1273 while ((*cur >= '0') && (*cur <= '9')) { \
1274 mult /= 10; \
1275 num += (*cur - '0') * mult; \
1276 cur++; \
1277 } \
1278 }
1279
1280/**
1281 * _xmlSchemaParseGMonth:
1282 * @dt: pointer to a date structure
1283 * @str: pointer to the string to analyze
1284 *
1285 * Parses a xs:gMonth without time zone and fills in the appropriate
1286 * field of the @dt structure. @str is updated to point just after the
1287 * xs:gMonth.
1288 *
1289 * Returns 0 or the error code
1290 */
1291static int
1292_xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
1293 const xmlChar *cur = *str;
1294 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001295 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001296
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001297 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001298 if (ret != 0)
1299 return ret;
1300
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001301 if (!VALID_MONTH(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001302 return 2;
1303
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001304 dt->mon = value;
1305
Daniel Veillard070803b2002-05-03 07:29:38 +00001306 *str = cur;
1307 return 0;
1308}
1309
1310/**
1311 * _xmlSchemaParseGDay:
1312 * @dt: pointer to a date structure
1313 * @str: pointer to the string to analyze
1314 *
1315 * Parses a xs:gDay without time zone and fills in the appropriate
1316 * field of the @dt structure. @str is updated to point just after the
1317 * xs:gDay.
1318 *
1319 * Returns 0 or the error code
1320 */
1321static int
1322_xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
1323 const xmlChar *cur = *str;
1324 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001325 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001326
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001327 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001328 if (ret != 0)
1329 return ret;
1330
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001331 if (!VALID_DAY(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001332 return 2;
1333
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001334 dt->day = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001335 *str = cur;
1336 return 0;
1337}
1338
1339/**
1340 * _xmlSchemaParseTime:
1341 * @dt: pointer to a date structure
1342 * @str: pointer to the string to analyze
1343 *
1344 * Parses a xs:time without time zone and fills in the appropriate
1345 * fields of the @dt structure. @str is updated to point just after the
1346 * xs:time.
1347 * In case of error, values of @dt fields are undefined.
1348 *
1349 * Returns 0 or the error code
1350 */
1351static int
1352_xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001353 const xmlChar *cur = *str;
Daniel Veillard070803b2002-05-03 07:29:38 +00001354 int ret = 0;
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +00001355 int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001356
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001357 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001358 if (ret != 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001359 return ret;
Daniel Veillard070803b2002-05-03 07:29:38 +00001360 if (*cur != ':')
1361 return 1;
Patrick Monnerata1dca812016-04-11 20:03:19 +02001362 if (!VALID_HOUR(value) && value != 24 /* Allow end-of-day hour */)
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001363 return 2;
Daniel Veillard070803b2002-05-03 07:29:38 +00001364 cur++;
1365
1366 /* the ':' insures this string is xs:time */
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001367 dt->hour = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001368
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001369 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001370 if (ret != 0)
1371 return ret;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001372 if (!VALID_MIN(value))
1373 return 2;
1374 dt->min = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001375
1376 if (*cur != ':')
1377 return 1;
1378 cur++;
1379
1380 PARSE_FLOAT(dt->sec, cur, ret);
1381 if (ret != 0)
1382 return ret;
1383
Patrick Monnerata1dca812016-04-11 20:03:19 +02001384 if (!VALID_TIME(dt))
Daniel Veillard070803b2002-05-03 07:29:38 +00001385 return 2;
1386
1387 *str = cur;
1388 return 0;
1389}
1390
1391/**
1392 * _xmlSchemaParseTimeZone:
1393 * @dt: pointer to a date structure
1394 * @str: pointer to the string to analyze
1395 *
1396 * Parses a time zone without time zone and fills in the appropriate
1397 * field of the @dt structure. @str is updated to point just after the
1398 * time zone.
1399 *
1400 * Returns 0 or the error code
1401 */
1402static int
1403_xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
Daniel Veillard14b56432006-03-09 18:41:40 +00001404 const xmlChar *cur;
Daniel Veillard070803b2002-05-03 07:29:38 +00001405 int ret = 0;
1406
1407 if (str == NULL)
1408 return -1;
Daniel Veillard14b56432006-03-09 18:41:40 +00001409 cur = *str;
Daniel Veillard070803b2002-05-03 07:29:38 +00001410
1411 switch (*cur) {
1412 case 0:
1413 dt->tz_flag = 0;
1414 dt->tzo = 0;
1415 break;
1416
1417 case 'Z':
1418 dt->tz_flag = 1;
1419 dt->tzo = 0;
1420 cur++;
1421 break;
1422
1423 case '+':
1424 case '-': {
1425 int isneg = 0, tmp = 0;
1426 isneg = (*cur == '-');
1427
1428 cur++;
1429
1430 PARSE_2_DIGITS(tmp, cur, ret);
1431 if (ret != 0)
1432 return ret;
1433 if (!VALID_HOUR(tmp))
1434 return 2;
1435
1436 if (*cur != ':')
1437 return 1;
1438 cur++;
1439
1440 dt->tzo = tmp * 60;
1441
1442 PARSE_2_DIGITS(tmp, cur, ret);
1443 if (ret != 0)
1444 return ret;
1445 if (!VALID_MIN(tmp))
1446 return 2;
1447
1448 dt->tzo += tmp;
1449 if (isneg)
1450 dt->tzo = - dt->tzo;
1451
1452 if (!VALID_TZO(dt->tzo))
1453 return 2;
1454
Daniel Veillard5a872412002-05-22 06:40:27 +00001455 dt->tz_flag = 1;
Daniel Veillard070803b2002-05-03 07:29:38 +00001456 break;
1457 }
1458 default:
1459 return 1;
1460 }
1461
1462 *str = cur;
1463 return 0;
1464}
1465
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001466/**
1467 * _xmlSchemaBase64Decode:
1468 * @ch: a character
1469 *
1470 * Converts a base64 encoded character to its base 64 value.
1471 *
1472 * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
1473 */
1474static int
1475_xmlSchemaBase64Decode (const xmlChar ch) {
1476 if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
1477 if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
1478 if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
1479 if ('+' == ch) return 62;
1480 if ('/' == ch) return 63;
1481 if ('=' == ch) return 64;
1482 return -1;
1483}
1484
Daniel Veillard070803b2002-05-03 07:29:38 +00001485/****************************************************************
1486 * *
1487 * XML Schema Dates/Times Datatypes Handling *
1488 * *
1489 ****************************************************************/
1490
1491/**
1492 * PARSE_DIGITS:
1493 * @num: the integer to fill in
1494 * @cur: an #xmlChar *
1495 * @num_type: an integer flag
1496 *
1497 * Parses a digits integer and updates @num with the value. @cur is
1498 * updated to point just after the integer.
1499 * In case of error, @num_type is set to -1, values of @num and
1500 * @cur are undefined.
1501 */
1502#define PARSE_DIGITS(num, cur, num_type) \
1503 if ((*cur < '0') || (*cur > '9')) \
1504 num_type = -1; \
1505 else \
1506 while ((*cur >= '0') && (*cur <= '9')) { \
1507 num = num * 10 + (*cur - '0'); \
1508 cur++; \
1509 }
1510
1511/**
1512 * PARSE_NUM:
1513 * @num: the double to fill in
1514 * @cur: an #xmlChar *
1515 * @num_type: an integer flag
1516 *
1517 * Parses a float or integer and updates @num with the value. @cur is
1518 * updated to point just after the number. If the number is a float,
1519 * then it must have an integer part and a decimal part; @num_type will
1520 * be set to 1. If there is no decimal part, @num_type is set to zero.
1521 * In case of error, @num_type is set to -1, values of @num and
1522 * @cur are undefined.
1523 */
1524#define PARSE_NUM(num, cur, num_type) \
1525 num = 0; \
1526 PARSE_DIGITS(num, cur, num_type); \
1527 if (!num_type && (*cur == '.')) { \
1528 double mult = 1; \
1529 cur++; \
1530 if ((*cur < '0') || (*cur > '9')) \
1531 num_type = -1; \
1532 else \
1533 num_type = 1; \
1534 while ((*cur >= '0') && (*cur <= '9')) { \
1535 mult /= 10; \
1536 num += (*cur - '0') * mult; \
1537 cur++; \
1538 } \
1539 }
1540
1541/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001542 * xmlSchemaValidateDates:
Daniel Veillard455cc072003-03-31 10:13:23 +00001543 * @type: the expected type or XML_SCHEMAS_UNKNOWN
Daniel Veillard070803b2002-05-03 07:29:38 +00001544 * @dateTime: string to analyze
1545 * @val: the return computed value
1546 *
1547 * Check that @dateTime conforms to the lexical space of one of the date types.
1548 * if true a value is computed and returned in @val.
1549 *
1550 * Returns 0 if this validates, a positive error code number otherwise
1551 * and -1 in case of internal or API error.
1552 */
1553static int
Daniel Veillard455cc072003-03-31 10:13:23 +00001554xmlSchemaValidateDates (xmlSchemaValType type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001555 const xmlChar *dateTime, xmlSchemaValPtr *val,
1556 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001557 xmlSchemaValPtr dt;
1558 int ret;
1559 const xmlChar *cur = dateTime;
1560
1561#define RETURN_TYPE_IF_VALID(t) \
1562 if (IS_TZO_CHAR(*cur)) { \
1563 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
1564 if (ret == 0) { \
1565 if (*cur != 0) \
1566 goto error; \
1567 dt->type = t; \
Daniel Veillard455cc072003-03-31 10:13:23 +00001568 goto done; \
Daniel Veillard070803b2002-05-03 07:29:38 +00001569 } \
1570 }
1571
1572 if (dateTime == NULL)
1573 return -1;
1574
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001575 if (collapse)
1576 while IS_WSP_BLANK_CH(*cur) cur++;
1577
Daniel Veillard070803b2002-05-03 07:29:38 +00001578 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
1579 return 1;
1580
1581 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
1582 if (dt == NULL)
1583 return -1;
1584
1585 if ((cur[0] == '-') && (cur[1] == '-')) {
1586 /*
1587 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
1588 * xs:gDay)
1589 */
1590 cur += 2;
1591
1592 /* is it an xs:gDay? */
1593 if (*cur == '-') {
Daniel Veillard455cc072003-03-31 10:13:23 +00001594 if (type == XML_SCHEMAS_GMONTH)
1595 goto error;
Daniel Veillard070803b2002-05-03 07:29:38 +00001596 ++cur;
1597 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1598 if (ret != 0)
1599 goto error;
1600
1601 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
1602
1603 goto error;
1604 }
1605
1606 /*
1607 * it should be an xs:gMonthDay or xs:gMonth
1608 */
1609 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1610 if (ret != 0)
1611 goto error;
1612
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001613 /*
1614 * a '-' char could indicate this type is xs:gMonthDay or
1615 * a negative time zone offset. Check for xs:gMonthDay first.
1616 * Also the first three char's of a negative tzo (-MM:SS) can
1617 * appear to be a valid day; so even if the day portion
1618 * of the xs:gMonthDay verifies, we must insure it was not
1619 * a tzo.
1620 */
1621 if (*cur == '-') {
1622 const xmlChar *rewnd = cur;
1623 cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001624
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001625 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001626 if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
1627
1628 /*
1629 * we can use the VALID_MDAY macro to validate the month
1630 * and day because the leap year test will flag year zero
1631 * as a leap year (even though zero is an invalid year).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001632 * FUTURE TODO: Zero will become valid in XML Schema 1.1
1633 * probably.
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001634 */
1635 if (VALID_MDAY((&(dt->value.date)))) {
1636
1637 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
1638
1639 goto error;
1640 }
1641 }
1642
1643 /*
1644 * not xs:gMonthDay so rewind and check if just xs:gMonth
1645 * with an optional time zone.
1646 */
1647 cur = rewnd;
1648 }
1649
1650 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
Daniel Veillard070803b2002-05-03 07:29:38 +00001651
1652 goto error;
1653 }
1654
1655 /*
1656 * It's a right-truncated date or an xs:time.
1657 * Try to parse an xs:time then fallback on right-truncated dates.
1658 */
1659 if ((*cur >= '0') && (*cur <= '9')) {
1660 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1661 if (ret == 0) {
1662 /* it's an xs:time */
1663 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
1664 }
1665 }
1666
1667 /* fallback on date parsing */
1668 cur = dateTime;
1669
1670 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
1671 if (ret != 0)
1672 goto error;
1673
1674 /* is it an xs:gYear? */
1675 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
1676
1677 if (*cur != '-')
1678 goto error;
1679 cur++;
1680
1681 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1682 if (ret != 0)
1683 goto error;
1684
1685 /* is it an xs:gYearMonth? */
1686 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
1687
1688 if (*cur != '-')
1689 goto error;
1690 cur++;
1691
1692 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1693 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
1694 goto error;
1695
1696 /* is it an xs:date? */
1697 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
1698
1699 if (*cur != 'T')
1700 goto error;
1701 cur++;
1702
1703 /* it should be an xs:dateTime */
1704 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1705 if (ret != 0)
1706 goto error;
1707
1708 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001709 if (collapse)
1710 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00001711 if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date))))))
Daniel Veillard070803b2002-05-03 07:29:38 +00001712 goto error;
1713
Daniel Veillard455cc072003-03-31 10:13:23 +00001714
Daniel Veillard070803b2002-05-03 07:29:38 +00001715 dt->type = XML_SCHEMAS_DATETIME;
1716
Daniel Veillard455cc072003-03-31 10:13:23 +00001717done:
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001718#if 1
1719 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
1720 goto error;
1721#else
1722 /*
1723 * insure the parsed type is equal to or less significant (right
1724 * truncated) than the desired type.
1725 */
1726 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
1727
1728 /* time only matches time */
1729 if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
1730 goto error;
1731
1732 if ((type == XML_SCHEMAS_DATETIME) &&
1733 ((dt->type != XML_SCHEMAS_DATE) ||
1734 (dt->type != XML_SCHEMAS_GYEARMONTH) ||
1735 (dt->type != XML_SCHEMAS_GYEAR)))
1736 goto error;
1737
1738 if ((type == XML_SCHEMAS_DATE) &&
1739 ((dt->type != XML_SCHEMAS_GYEAR) ||
1740 (dt->type != XML_SCHEMAS_GYEARMONTH)))
1741 goto error;
1742
1743 if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
1744 goto error;
1745
1746 if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
1747 goto error;
1748 }
Daniel Veillard455cc072003-03-31 10:13:23 +00001749#endif
1750
Daniel Veillard070803b2002-05-03 07:29:38 +00001751 if (val != NULL)
1752 *val = dt;
Daniel Veillard80b19092003-03-28 13:29:53 +00001753 else
1754 xmlSchemaFreeValue(dt);
Daniel Veillard070803b2002-05-03 07:29:38 +00001755
1756 return 0;
1757
1758error:
1759 if (dt != NULL)
1760 xmlSchemaFreeValue(dt);
1761 return 1;
1762}
1763
1764/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001765 * xmlSchemaValidateDuration:
Daniel Veillard070803b2002-05-03 07:29:38 +00001766 * @type: the predefined type
1767 * @duration: string to analyze
1768 * @val: the return computed value
1769 *
1770 * Check that @duration conforms to the lexical space of the duration type.
1771 * if true a value is computed and returned in @val.
1772 *
1773 * Returns 0 if this validates, a positive error code number otherwise
1774 * and -1 in case of internal or API error.
1775 */
1776static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00001777xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001778 const xmlChar *duration, xmlSchemaValPtr *val,
1779 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001780 const xmlChar *cur = duration;
1781 xmlSchemaValPtr dur;
1782 int isneg = 0;
1783 unsigned int seq = 0;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001784 double num;
1785 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
1786 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1787 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
Daniel Veillard070803b2002-05-03 07:29:38 +00001788
1789 if (duration == NULL)
1790 return -1;
1791
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001792 if (collapse)
1793 while IS_WSP_BLANK_CH(*cur) cur++;
1794
Daniel Veillard070803b2002-05-03 07:29:38 +00001795 if (*cur == '-') {
1796 isneg = 1;
1797 cur++;
1798 }
1799
1800 /* duration must start with 'P' (after sign) */
1801 if (*cur++ != 'P')
1802 return 1;
1803
Daniel Veillard80b19092003-03-28 13:29:53 +00001804 if (*cur == 0)
1805 return 1;
1806
Daniel Veillard070803b2002-05-03 07:29:38 +00001807 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1808 if (dur == NULL)
1809 return -1;
1810
1811 while (*cur != 0) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001812
1813 /* input string should be empty or invalid date/time item */
1814 if (seq >= sizeof(desig))
1815 goto error;
1816
1817 /* T designator must be present for time items */
1818 if (*cur == 'T') {
1819 if (seq <= 3) {
1820 seq = 3;
1821 cur++;
1822 } else
1823 return 1;
1824 } else if (seq == 3)
1825 goto error;
1826
1827 /* parse the number portion of the item */
1828 PARSE_NUM(num, cur, num_type);
1829
1830 if ((num_type == -1) || (*cur == 0))
1831 goto error;
1832
1833 /* update duration based on item type */
1834 while (seq < sizeof(desig)) {
1835 if (*cur == desig[seq]) {
1836
1837 /* verify numeric type; only seconds can be float */
1838 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1839 goto error;
1840
1841 switch (seq) {
1842 case 0:
1843 dur->value.dur.mon = (long)num * 12;
1844 break;
1845 case 1:
1846 dur->value.dur.mon += (long)num;
1847 break;
1848 default:
1849 /* convert to seconds using multiplier */
1850 dur->value.dur.sec += num * multi[seq];
1851 seq++;
1852 break;
1853 }
1854
1855 break; /* exit loop */
1856 }
1857 /* no date designators found? */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00001858 if ((++seq == 3) || (seq == 6))
Daniel Veillard070803b2002-05-03 07:29:38 +00001859 goto error;
1860 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001861 cur++;
1862 if (collapse)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001863 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001864 }
1865
1866 if (isneg) {
1867 dur->value.dur.mon = -dur->value.dur.mon;
1868 dur->value.dur.day = -dur->value.dur.day;
1869 dur->value.dur.sec = -dur->value.dur.sec;
1870 }
1871
1872 if (val != NULL)
1873 *val = dur;
Daniel Veillard80b19092003-03-28 13:29:53 +00001874 else
1875 xmlSchemaFreeValue(dur);
Daniel Veillard070803b2002-05-03 07:29:38 +00001876
1877 return 0;
1878
1879error:
1880 if (dur != NULL)
1881 xmlSchemaFreeValue(dur);
1882 return 1;
1883}
1884
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001885/**
1886 * xmlSchemaStrip:
1887 * @value: a value
1888 *
1889 * Removes the leading and ending spaces of a string
1890 *
1891 * Returns the new string or NULL if no change was required.
1892 */
1893static xmlChar *
1894xmlSchemaStrip(const xmlChar *value) {
1895 const xmlChar *start = value, *end, *f;
1896
1897 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001898 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001899 end = start;
1900 while (*end != 0) end++;
1901 f = end;
1902 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001903 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001904 end++;
1905 if ((start == value) && (f == end)) return(NULL);
1906 return(xmlStrndup(start, end - start));
1907}
Daniel Veillard96a4b252003-02-06 08:22:32 +00001908
1909/**
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001910 * xmlSchemaWhiteSpaceReplace:
1911 * @value: a value
1912 *
1913 * Replaces 0xd, 0x9 and 0xa with a space.
1914 *
1915 * Returns the new string or NULL if no change was required.
1916 */
1917xmlChar *
1918xmlSchemaWhiteSpaceReplace(const xmlChar *value) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001919 const xmlChar *cur = value;
1920 xmlChar *ret = NULL, *mcur;
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001921
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001922 if (value == NULL)
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001923 return(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001924
1925 while ((*cur != 0) &&
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001926 (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) {
1927 cur++;
1928 }
1929 if (*cur == 0)
1930 return (NULL);
1931 ret = xmlStrdup(value);
1932 /* TODO FIXME: I guess gcc will bark at this. */
1933 mcur = (xmlChar *) (ret + (cur - value));
1934 do {
1935 if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) )
1936 *mcur = ' ';
1937 mcur++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001938 } while (*mcur != 0);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001939 return(ret);
1940}
1941
1942/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001943 * xmlSchemaCollapseString:
1944 * @value: a value
1945 *
1946 * Removes and normalize white spaces in the string
1947 *
1948 * Returns the new string or NULL if no change was required.
1949 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00001950xmlChar *
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001951xmlSchemaCollapseString(const xmlChar *value) {
1952 const xmlChar *start = value, *end, *f;
1953 xmlChar *g;
1954 int col = 0;
1955
1956 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001957 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001958 end = start;
1959 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001960 if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001961 col = end - start;
1962 break;
1963 } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
1964 col = end - start;
1965 break;
1966 }
1967 end++;
1968 }
1969 if (col == 0) {
1970 f = end;
1971 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001972 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001973 end++;
1974 if ((start == value) && (f == end)) return(NULL);
1975 return(xmlStrndup(start, end - start));
1976 }
1977 start = xmlStrdup(start);
1978 if (start == NULL) return(NULL);
1979 g = (xmlChar *) (start + col);
1980 end = g;
1981 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001982 if (IS_BLANK_CH(*end)) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001983 end++;
William M. Brack76e95df2003-10-18 16:20:14 +00001984 while (IS_BLANK_CH(*end)) end++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001985 if (*end != 0)
1986 *g++ = ' ';
1987 } else
1988 *g++ = *end++;
1989 }
1990 *g = 0;
1991 return((xmlChar *) start);
1992}
1993
1994/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001995 * xmlSchemaValAtomicListNode:
1996 * @type: the predefined atomic type for a token in the list
1997 * @value: the list value to check
1998 * @ret: the return computed value
1999 * @node: the node containing the value
2000 *
2001 * Check that a value conforms to the lexical space of the predefined
2002 * list type. if true a value is computed and returned in @ret.
2003 *
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002004 * Returns the number of items if this validates, a negative error code
2005 * number otherwise
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002006 */
2007static int
2008xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
2009 xmlSchemaValPtr *ret, xmlNodePtr node) {
2010 xmlChar *val, *cur, *endval;
2011 int nb_values = 0;
Daniel Veillard580ced82003-03-21 21:22:48 +00002012 int tmp = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002013
2014 if (value == NULL) {
2015 return(-1);
2016 }
2017 val = xmlStrdup(value);
2018 if (val == NULL) {
2019 return(-1);
2020 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002021 if (ret != NULL) {
2022 *ret = NULL;
2023 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002024 cur = val;
2025 /*
2026 * Split the list
2027 */
William M. Brack76e95df2003-10-18 16:20:14 +00002028 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002029 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00002030 if (IS_BLANK_CH(*cur)) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002031 *cur = 0;
2032 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00002033 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002034 } else {
2035 nb_values++;
2036 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00002037 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002038 }
2039 }
2040 if (nb_values == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002041 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002042 return(nb_values);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002043 }
2044 endval = cur;
2045 cur = val;
2046 while ((*cur == 0) && (cur != endval)) cur++;
2047 while (cur != endval) {
2048 tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
2049 if (tmp != 0)
2050 break;
2051 while (*cur != 0) cur++;
2052 while ((*cur == 0) && (cur != endval)) cur++;
2053 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002054 /* TODO what return value ? c.f. bug #158628
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002055 if (ret != NULL) {
2056 TODO
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002057 } */
2058 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002059 if (tmp == 0)
2060 return(nb_values);
2061 return(-1);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002062}
2063
2064/**
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002065 * xmlSchemaParseUInt:
2066 * @str: pointer to the string R/W
2067 * @llo: pointer to the low result
2068 * @lmi: pointer to the mid result
2069 * @lhi: pointer to the high result
2070 *
2071 * Parse an unsigned long into 3 fields.
2072 *
William M. Brackec3b4b72005-03-15 15:50:17 +00002073 * Returns the number of significant digits in the number or
Daniel Veillardbfc42632008-04-03 10:43:52 +00002074 * -1 if overflow of the capacity and -2 if it's not a number.
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002075 */
2076static int
2077xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002078 unsigned long *lmi, unsigned long *lhi) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002079 unsigned long lo = 0, mi = 0, hi = 0;
2080 const xmlChar *tmp, *cur = *str;
2081 int ret = 0, i = 0;
2082
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002083 if (!((*cur >= '0') && (*cur <= '9')))
Daniel Veillardbfc42632008-04-03 10:43:52 +00002084 return(-2);
2085
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002086 while (*cur == '0') { /* ignore leading zeroes */
2087 cur++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002088 }
2089 tmp = cur;
2090 while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002091 i++;tmp++;ret++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002092 }
2093 if (i > 24) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002094 *str = tmp;
2095 return(-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002096 }
2097 while (i > 16) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002098 hi = hi * 10 + (*cur++ - '0');
2099 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002100 }
2101 while (i > 8) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002102 mi = mi * 10 + (*cur++ - '0');
2103 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002104 }
2105 while (i > 0) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002106 lo = lo * 10 + (*cur++ - '0');
2107 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002108 }
2109
2110 *str = cur;
2111 *llo = lo;
2112 *lmi = mi;
2113 *lhi = hi;
2114 return(ret);
2115}
2116
2117/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002118 * xmlSchemaValAtomicType:
2119 * @type: the predefined type
2120 * @value: the value to check
2121 * @val: the return computed value
2122 * @node: the node containing the value
2123 * flags: flags to control the vlidation
2124 *
2125 * Check that a value conforms to the lexical space of the atomic type.
2126 * if true a value is computed and returned in @val.
Daniel Veillard01fa6152004-06-29 17:04:39 +00002127 * This checks the value space for list types as well (IDREFS, NMTOKENS).
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002128 *
2129 * Returns 0 if this validates, a positive error code number otherwise
2130 * and -1 in case of internal or API error.
2131 */
2132static int
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002133xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002134 xmlSchemaValPtr * val, xmlNodePtr node, int flags,
2135 xmlSchemaWhitespaceValueType ws,
2136 int normOnTheFly, int applyNorm, int createStringValue)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002137{
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002138 xmlSchemaValPtr v;
2139 xmlChar *norm = NULL;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00002140 int ret = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002141
2142 if (xmlSchemaTypesInitialized == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00002143 xmlSchemaInitTypes();
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002144 if (type == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002145 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002146
Daniel Veillardeebd6332004-08-26 10:30:44 +00002147 /*
2148 * validating a non existant text node is similar to validating
2149 * an empty one.
2150 */
2151 if (value == NULL)
2152 value = BAD_CAST "";
2153
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002154 if (val != NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002155 *val = NULL;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002156 if ((flags == 0) && (value != NULL)) {
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00002157
Daniel Veillard01fa6152004-06-29 17:04:39 +00002158 if ((type->builtInType != XML_SCHEMAS_STRING) &&
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002159 (type->builtInType != XML_SCHEMAS_ANYTYPE) &&
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00002160 (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) {
2161 if (type->builtInType == XML_SCHEMAS_NORMSTRING)
2162 norm = xmlSchemaWhiteSpaceReplace(value);
2163 else
2164 norm = xmlSchemaCollapseString(value);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002165 if (norm != NULL)
2166 value = norm;
2167 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002168 }
2169
Daniel Veillard01fa6152004-06-29 17:04:39 +00002170 switch (type->builtInType) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002171 case XML_SCHEMAS_UNKNOWN:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002172 goto error;
William M. Brack2f2a6632004-08-20 23:09:47 +00002173 case XML_SCHEMAS_ANYTYPE:
2174 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002175 if ((createStringValue) && (val != NULL)) {
2176 v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE);
2177 if (v != NULL) {
2178 v->value.str = xmlStrdup(value);
2179 *val = v;
2180 } else {
2181 goto error;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002182 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002183 }
William M. Brack2f2a6632004-08-20 23:09:47 +00002184 goto return0;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002185 case XML_SCHEMAS_STRING:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002186 if (! normOnTheFly) {
2187 const xmlChar *cur = value;
2188
2189 if (ws == XML_SCHEMA_WHITESPACE_REPLACE) {
2190 while (*cur != 0) {
2191 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2192 goto return1;
2193 } else {
2194 cur++;
2195 }
2196 }
2197 } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
2198 while (*cur != 0) {
2199 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2200 goto return1;
2201 } else if IS_WSP_SPACE_CH(*cur) {
2202 cur++;
2203 if IS_WSP_SPACE_CH(*cur)
2204 goto return1;
2205 } else {
2206 cur++;
2207 }
2208 }
2209 }
2210 }
2211 if (createStringValue && (val != NULL)) {
2212 if (applyNorm) {
2213 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2214 norm = xmlSchemaCollapseString(value);
2215 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
2216 norm = xmlSchemaWhiteSpaceReplace(value);
2217 if (norm != NULL)
2218 value = norm;
2219 }
2220 v = xmlSchemaNewValue(XML_SCHEMAS_STRING);
2221 if (v != NULL) {
2222 v->value.str = xmlStrdup(value);
2223 *val = v;
2224 } else {
2225 goto error;
2226 }
2227 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002228 goto return0;
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002229 case XML_SCHEMAS_NORMSTRING:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002230 if (normOnTheFly) {
2231 if (applyNorm) {
2232 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2233 norm = xmlSchemaCollapseString(value);
2234 else
2235 norm = xmlSchemaWhiteSpaceReplace(value);
2236 if (norm != NULL)
2237 value = norm;
2238 }
2239 } else {
2240 const xmlChar *cur = value;
2241 while (*cur != 0) {
2242 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2243 goto return1;
2244 } else {
2245 cur++;
2246 }
2247 }
2248 }
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002249 if (val != NULL) {
2250 v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
2251 if (v != NULL) {
2252 v->value.str = xmlStrdup(value);
2253 *val = v;
2254 } else {
2255 goto error;
2256 }
2257 }
2258 goto return0;
2259 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002260 case XML_SCHEMAS_DECIMAL:{
William M. Brack273670f2005-03-11 15:55:14 +00002261 const xmlChar *cur = value;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002262 unsigned int len, neg, integ, hasLeadingZeroes;
William M. Brack273670f2005-03-11 15:55:14 +00002263 xmlChar cval[25];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002264 xmlChar *cptr = cval;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002265
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002266 if ((cur == NULL) || (*cur == 0))
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002267 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002268
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002269 /*
2270 * xs:decimal has a whitespace-facet value of 'collapse'.
2271 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002272 if (normOnTheFly)
2273 while IS_WSP_BLANK_CH(*cur) cur++;
2274
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002275 /*
2276 * First we handle an optional sign.
2277 */
2278 neg = 0;
2279 if (*cur == '-') {
2280 neg = 1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002281 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002282 } else if (*cur == '+')
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002283 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002284 /*
2285 * Disallow: "", "-", "- "
2286 */
2287 if (*cur == 0)
2288 goto return1;
William M. Brack273670f2005-03-11 15:55:14 +00002289 /*
2290 * Next we "pre-parse" the number, in preparation for calling
2291 * the common routine xmlSchemaParseUInt. We get rid of any
2292 * leading zeroes (because we have reserved only 25 chars),
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002293 * and note the position of a decimal point.
William M. Brack273670f2005-03-11 15:55:14 +00002294 */
2295 len = 0;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002296 integ = ~0u;
2297 hasLeadingZeroes = 0;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002298 /*
2299 * Skip leading zeroes.
2300 */
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002301 while (*cur == '0') {
William M. Brack273670f2005-03-11 15:55:14 +00002302 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002303 hasLeadingZeroes = 1;
2304 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002305 if (*cur != 0) {
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002306 do {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002307 if ((*cur >= '0') && (*cur <= '9')) {
2308 *cptr++ = *cur++;
2309 len++;
2310 } else if (*cur == '.') {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002311 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002312 integ = len;
2313 do {
2314 if ((*cur >= '0') && (*cur <= '9')) {
2315 *cptr++ = *cur++;
2316 len++;
2317 } else
2318 break;
2319 } while (len < 24);
2320 /*
2321 * Disallow "." but allow "00."
2322 */
2323 if ((len == 0) && (!hasLeadingZeroes))
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002324 goto return1;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002325 break;
2326 } else
2327 break;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002328 } while (len < 24);
William M. Brack273670f2005-03-11 15:55:14 +00002329 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002330 if (normOnTheFly)
2331 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brack273670f2005-03-11 15:55:14 +00002332 if (*cur != 0)
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002333 goto return1; /* error if any extraneous chars */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002334 if (val != NULL) {
2335 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
2336 if (v != NULL) {
William M. Brack273670f2005-03-11 15:55:14 +00002337 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002338 * Now evaluate the significant digits of the number
2339 */
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002340 if (len != 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002341
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002342 if (integ != ~0u) {
2343 /*
2344 * Get rid of trailing zeroes in the
2345 * fractional part.
2346 */
2347 while ((len != integ) && (*(cptr-1) == '0')) {
2348 cptr--;
2349 len--;
2350 }
2351 }
2352 /*
2353 * Terminate the (preparsed) string.
2354 */
2355 if (len != 0) {
Daniel Veillardbfc42632008-04-03 10:43:52 +00002356 *cptr = 0;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002357 cptr = cval;
Daniel Veillardbfc42632008-04-03 10:43:52 +00002358
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002359 xmlSchemaParseUInt((const xmlChar **)&cptr,
2360 &v->value.decimal.lo,
2361 &v->value.decimal.mi,
2362 &v->value.decimal.hi);
2363 }
2364 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002365 /*
2366 * Set the total digits to 1 if a zero value.
2367 */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002368 v->value.decimal.sign = neg;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002369 if (len == 0) {
2370 /* Speedup for zero values. */
2371 v->value.decimal.total = 1;
William M. Brack273670f2005-03-11 15:55:14 +00002372 } else {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002373 v->value.decimal.total = len;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002374 if (integ == ~0u)
2375 v->value.decimal.frac = 0;
2376 else
2377 v->value.decimal.frac = len - integ;
William M. Brack273670f2005-03-11 15:55:14 +00002378 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002379 *val = v;
2380 }
2381 }
2382 goto return0;
2383 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002384 case XML_SCHEMAS_TIME:
2385 case XML_SCHEMAS_GDAY:
2386 case XML_SCHEMAS_GMONTH:
2387 case XML_SCHEMAS_GMONTHDAY:
2388 case XML_SCHEMAS_GYEAR:
2389 case XML_SCHEMAS_GYEARMONTH:
2390 case XML_SCHEMAS_DATE:
2391 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002392 ret = xmlSchemaValidateDates(type->builtInType, value, val,
2393 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002394 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002395 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002396 ret = xmlSchemaValidateDuration(type, value, val,
2397 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002398 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002399 case XML_SCHEMAS_FLOAT:
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002400 case XML_SCHEMAS_DOUBLE: {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002401 const xmlChar *cur = value;
2402 int neg = 0;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002403 int digits_before = 0;
2404 int digits_after = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002405
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002406 if (normOnTheFly)
2407 while IS_WSP_BLANK_CH(*cur) cur++;
2408
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002409 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
2410 cur += 3;
2411 if (*cur != 0)
2412 goto return1;
2413 if (val != NULL) {
2414 if (type == xmlSchemaTypeFloatDef) {
2415 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2416 if (v != NULL) {
2417 v->value.f = (float) xmlXPathNAN;
2418 } else {
2419 xmlSchemaFreeValue(v);
2420 goto error;
2421 }
2422 } else {
2423 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2424 if (v != NULL) {
2425 v->value.d = xmlXPathNAN;
2426 } else {
2427 xmlSchemaFreeValue(v);
2428 goto error;
2429 }
2430 }
2431 *val = v;
2432 }
2433 goto return0;
2434 }
2435 if (*cur == '-') {
2436 neg = 1;
2437 cur++;
2438 }
2439 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
2440 cur += 3;
2441 if (*cur != 0)
2442 goto return1;
2443 if (val != NULL) {
2444 if (type == xmlSchemaTypeFloatDef) {
2445 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2446 if (v != NULL) {
2447 if (neg)
2448 v->value.f = (float) xmlXPathNINF;
2449 else
2450 v->value.f = (float) xmlXPathPINF;
2451 } else {
2452 xmlSchemaFreeValue(v);
2453 goto error;
2454 }
2455 } else {
2456 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2457 if (v != NULL) {
2458 if (neg)
2459 v->value.d = xmlXPathNINF;
2460 else
2461 v->value.d = xmlXPathPINF;
2462 } else {
2463 xmlSchemaFreeValue(v);
2464 goto error;
2465 }
2466 }
2467 *val = v;
2468 }
2469 goto return0;
2470 }
2471 if ((neg == 0) && (*cur == '+'))
2472 cur++;
2473 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
2474 goto return1;
2475 while ((*cur >= '0') && (*cur <= '9')) {
2476 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002477 digits_before++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002478 }
2479 if (*cur == '.') {
2480 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002481 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002482 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002483 digits_after++;
2484 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002485 }
Daniel Veillard1ea95902010-07-28 14:49:55 +02002486 if ((digits_before == 0) && (digits_after == 0))
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002487 goto return1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002488 if ((*cur == 'e') || (*cur == 'E')) {
2489 cur++;
2490 if ((*cur == '-') || (*cur == '+'))
2491 cur++;
2492 while ((*cur >= '0') && (*cur <= '9'))
2493 cur++;
2494 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002495 if (normOnTheFly)
2496 while IS_WSP_BLANK_CH(*cur) cur++;
2497
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002498 if (*cur != 0)
2499 goto return1;
2500 if (val != NULL) {
2501 if (type == xmlSchemaTypeFloatDef) {
2502 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2503 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002504 /*
2505 * TODO: sscanf seems not to give the correct
2506 * value for extremely high/low values.
2507 * E.g. "1E-149" results in zero.
2508 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002509 if (sscanf((const char *) value, "%f",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002510 &(v->value.f)) == 1) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002511 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002512 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002513 xmlSchemaFreeValue(v);
2514 goto return1;
2515 }
2516 } else {
2517 goto error;
2518 }
2519 } else {
2520 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2521 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002522 /*
2523 * TODO: sscanf seems not to give the correct
2524 * value for extremely high/low values.
2525 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002526 if (sscanf((const char *) value, "%lf",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002527 &(v->value.d)) == 1) {
2528 *val = v;
2529 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002530 xmlSchemaFreeValue(v);
2531 goto return1;
2532 }
2533 } else {
2534 goto error;
2535 }
2536 }
2537 }
2538 goto return0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00002539 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002540 case XML_SCHEMAS_BOOLEAN:{
2541 const xmlChar *cur = value;
2542
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002543 if (normOnTheFly) {
2544 while IS_WSP_BLANK_CH(*cur) cur++;
2545 if (*cur == '0') {
2546 ret = 0;
2547 cur++;
2548 } else if (*cur == '1') {
2549 ret = 1;
2550 cur++;
2551 } else if (*cur == 't') {
2552 cur++;
2553 if ((*cur++ == 'r') && (*cur++ == 'u') &&
2554 (*cur++ == 'e')) {
2555 ret = 1;
2556 } else
2557 goto return1;
2558 } else if (*cur == 'f') {
2559 cur++;
2560 if ((*cur++ == 'a') && (*cur++ == 'l') &&
2561 (*cur++ == 's') && (*cur++ == 'e')) {
2562 ret = 0;
2563 } else
2564 goto return1;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00002565 } else
2566 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002567 if (*cur != 0) {
2568 while IS_WSP_BLANK_CH(*cur) cur++;
2569 if (*cur != 0)
2570 goto return1;
2571 }
2572 } else {
2573 if ((cur[0] == '0') && (cur[1] == 0))
2574 ret = 0;
2575 else if ((cur[0] == '1') && (cur[1] == 0))
2576 ret = 1;
2577 else if ((cur[0] == 't') && (cur[1] == 'r')
2578 && (cur[2] == 'u') && (cur[3] == 'e')
2579 && (cur[4] == 0))
2580 ret = 1;
2581 else if ((cur[0] == 'f') && (cur[1] == 'a')
2582 && (cur[2] == 'l') && (cur[3] == 's')
2583 && (cur[4] == 'e') && (cur[5] == 0))
2584 ret = 0;
2585 else
2586 goto return1;
2587 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002588 if (val != NULL) {
2589 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
2590 if (v != NULL) {
2591 v->value.b = ret;
2592 *val = v;
2593 } else {
2594 goto error;
2595 }
2596 }
2597 goto return0;
2598 }
2599 case XML_SCHEMAS_TOKEN:{
2600 const xmlChar *cur = value;
2601
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002602 if (! normOnTheFly) {
2603 while (*cur != 0) {
2604 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2605 goto return1;
2606 } else if (*cur == ' ') {
2607 cur++;
2608 if (*cur == 0)
2609 goto return1;
2610 if (*cur == ' ')
2611 goto return1;
2612 } else {
2613 cur++;
2614 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002615 }
2616 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002617 if (val != NULL) {
2618 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
2619 if (v != NULL) {
2620 v->value.str = xmlStrdup(value);
2621 *val = v;
2622 } else {
2623 goto error;
2624 }
2625 }
2626 goto return0;
2627 }
2628 case XML_SCHEMAS_LANGUAGE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002629 if (normOnTheFly) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002630 norm = xmlSchemaCollapseString(value);
2631 if (norm != NULL)
2632 value = norm;
2633 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002634 if (xmlCheckLanguageID(value) == 1) {
2635 if (val != NULL) {
2636 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
2637 if (v != NULL) {
2638 v->value.str = xmlStrdup(value);
2639 *val = v;
2640 } else {
2641 goto error;
2642 }
2643 }
2644 goto return0;
2645 }
2646 goto return1;
2647 case XML_SCHEMAS_NMTOKEN:
2648 if (xmlValidateNMToken(value, 1) == 0) {
2649 if (val != NULL) {
2650 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
2651 if (v != NULL) {
2652 v->value.str = xmlStrdup(value);
2653 *val = v;
2654 } else {
2655 goto error;
2656 }
2657 }
2658 goto return0;
2659 }
2660 goto return1;
2661 case XML_SCHEMAS_NMTOKENS:
2662 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
2663 value, val, node);
2664 if (ret > 0)
2665 ret = 0;
2666 else
2667 ret = 1;
2668 goto done;
2669 case XML_SCHEMAS_NAME:
2670 ret = xmlValidateName(value, 1);
Daniel Veillarddf292f72005-01-16 19:00:15 +00002671 if ((ret == 0) && (val != NULL) && (value != NULL)) {
2672 v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
2673 if (v != NULL) {
2674 const xmlChar *start = value, *end;
2675 while (IS_BLANK_CH(*start)) start++;
2676 end = start;
2677 while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
2678 v->value.str = xmlStrndup(start, end - start);
2679 *val = v;
2680 } else {
2681 goto error;
2682 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002683 }
2684 goto done;
2685 case XML_SCHEMAS_QNAME:{
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002686 const xmlChar *uri = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002687 xmlChar *local = NULL;
2688
2689 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002690 if (ret != 0)
2691 goto done;
2692 if (node != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002693 xmlChar *prefix;
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002694 xmlNsPtr ns;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002695
2696 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002697 ns = xmlSearchNs(node->doc, node, prefix);
2698 if ((ns == NULL) && (prefix != NULL)) {
2699 xmlFree(prefix);
2700 if (local != NULL)
2701 xmlFree(local);
2702 goto return1;
2703 }
2704 if (ns != NULL)
2705 uri = ns->href;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002706 if (prefix != NULL)
2707 xmlFree(prefix);
2708 }
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002709 if (val != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002710 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002711 if (v == NULL) {
2712 if (local != NULL)
2713 xmlFree(local);
2714 goto error;
2715 }
2716 if (local != NULL)
2717 v->value.qname.name = local;
2718 else
2719 v->value.qname.name = xmlStrdup(value);
2720 if (uri != NULL)
2721 v->value.qname.uri = xmlStrdup(uri);
2722 *val = v;
2723 } else
2724 if (local != NULL)
2725 xmlFree(local);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002726 goto done;
2727 }
2728 case XML_SCHEMAS_NCNAME:
2729 ret = xmlValidateNCName(value, 1);
2730 if ((ret == 0) && (val != NULL)) {
2731 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
2732 if (v != NULL) {
2733 v->value.str = xmlStrdup(value);
2734 *val = v;
2735 } else {
2736 goto error;
2737 }
2738 }
2739 goto done;
2740 case XML_SCHEMAS_ID:
2741 ret = xmlValidateNCName(value, 1);
2742 if ((ret == 0) && (val != NULL)) {
2743 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
2744 if (v != NULL) {
2745 v->value.str = xmlStrdup(value);
2746 *val = v;
2747 } else {
2748 goto error;
2749 }
2750 }
2751 if ((ret == 0) && (node != NULL) &&
2752 (node->type == XML_ATTRIBUTE_NODE)) {
2753 xmlAttrPtr attr = (xmlAttrPtr) node;
2754
2755 /*
2756 * NOTE: the IDness might have already be declared in the DTD
2757 */
2758 if (attr->atype != XML_ATTRIBUTE_ID) {
2759 xmlIDPtr res;
2760 xmlChar *strip;
2761
2762 strip = xmlSchemaStrip(value);
2763 if (strip != NULL) {
2764 res = xmlAddID(NULL, node->doc, strip, attr);
2765 xmlFree(strip);
2766 } else
2767 res = xmlAddID(NULL, node->doc, value, attr);
2768 if (res == NULL) {
2769 ret = 2;
2770 } else {
2771 attr->atype = XML_ATTRIBUTE_ID;
2772 }
2773 }
2774 }
2775 goto done;
2776 case XML_SCHEMAS_IDREF:
2777 ret = xmlValidateNCName(value, 1);
2778 if ((ret == 0) && (val != NULL)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00002779 v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
2780 if (v == NULL)
2781 goto error;
2782 v->value.str = xmlStrdup(value);
2783 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002784 }
2785 if ((ret == 0) && (node != NULL) &&
2786 (node->type == XML_ATTRIBUTE_NODE)) {
2787 xmlAttrPtr attr = (xmlAttrPtr) node;
2788 xmlChar *strip;
2789
2790 strip = xmlSchemaStrip(value);
2791 if (strip != NULL) {
2792 xmlAddRef(NULL, node->doc, strip, attr);
2793 xmlFree(strip);
2794 } else
2795 xmlAddRef(NULL, node->doc, value, attr);
2796 attr->atype = XML_ATTRIBUTE_IDREF;
2797 }
2798 goto done;
2799 case XML_SCHEMAS_IDREFS:
2800 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
2801 value, val, node);
2802 if (ret < 0)
2803 ret = 2;
2804 else
2805 ret = 0;
2806 if ((ret == 0) && (node != NULL) &&
2807 (node->type == XML_ATTRIBUTE_NODE)) {
2808 xmlAttrPtr attr = (xmlAttrPtr) node;
2809
2810 attr->atype = XML_ATTRIBUTE_IDREFS;
2811 }
2812 goto done;
2813 case XML_SCHEMAS_ENTITY:{
2814 xmlChar *strip;
2815
2816 ret = xmlValidateNCName(value, 1);
2817 if ((node == NULL) || (node->doc == NULL))
2818 ret = 3;
2819 if (ret == 0) {
2820 xmlEntityPtr ent;
2821
2822 strip = xmlSchemaStrip(value);
2823 if (strip != NULL) {
2824 ent = xmlGetDocEntity(node->doc, strip);
2825 xmlFree(strip);
2826 } else {
2827 ent = xmlGetDocEntity(node->doc, value);
2828 }
2829 if ((ent == NULL) ||
2830 (ent->etype !=
2831 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
2832 ret = 4;
2833 }
2834 if ((ret == 0) && (val != NULL)) {
2835 TODO;
2836 }
2837 if ((ret == 0) && (node != NULL) &&
2838 (node->type == XML_ATTRIBUTE_NODE)) {
2839 xmlAttrPtr attr = (xmlAttrPtr) node;
2840
2841 attr->atype = XML_ATTRIBUTE_ENTITY;
2842 }
2843 goto done;
2844 }
2845 case XML_SCHEMAS_ENTITIES:
2846 if ((node == NULL) || (node->doc == NULL))
2847 goto return3;
2848 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
2849 value, val, node);
2850 if (ret <= 0)
2851 ret = 1;
2852 else
2853 ret = 0;
2854 if ((ret == 0) && (node != NULL) &&
2855 (node->type == XML_ATTRIBUTE_NODE)) {
2856 xmlAttrPtr attr = (xmlAttrPtr) node;
2857
2858 attr->atype = XML_ATTRIBUTE_ENTITIES;
2859 }
2860 goto done;
2861 case XML_SCHEMAS_NOTATION:{
2862 xmlChar *uri = NULL;
2863 xmlChar *local = NULL;
2864
2865 ret = xmlValidateQName(value, 1);
2866 if ((ret == 0) && (node != NULL)) {
2867 xmlChar *prefix;
2868
2869 local = xmlSplitQName2(value, &prefix);
2870 if (prefix != NULL) {
2871 xmlNsPtr ns;
2872
2873 ns = xmlSearchNs(node->doc, node, prefix);
2874 if (ns == NULL)
2875 ret = 1;
2876 else if (val != NULL)
2877 uri = xmlStrdup(ns->href);
2878 }
2879 if ((local != NULL) && ((val == NULL) || (ret != 0)))
2880 xmlFree(local);
2881 if (prefix != NULL)
2882 xmlFree(prefix);
2883 }
2884 if ((node == NULL) || (node->doc == NULL))
2885 ret = 3;
2886 if (ret == 0) {
2887 ret = xmlValidateNotationUse(NULL, node->doc, value);
2888 if (ret == 1)
2889 ret = 0;
2890 else
2891 ret = 1;
2892 }
2893 if ((ret == 0) && (val != NULL)) {
2894 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
2895 if (v != NULL) {
2896 if (local != NULL)
2897 v->value.qname.name = local;
2898 else
2899 v->value.qname.name = xmlStrdup(value);
2900 if (uri != NULL)
2901 v->value.qname.uri = uri;
2902
2903 *val = v;
2904 } else {
2905 if (local != NULL)
2906 xmlFree(local);
2907 if (uri != NULL)
2908 xmlFree(uri);
2909 goto error;
2910 }
2911 }
2912 goto done;
2913 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002914 case XML_SCHEMAS_ANYURI:{
Daniel Veillard11c466a2004-03-14 12:20:15 +00002915 if (*value != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002916 xmlURIPtr uri;
Vincent Lefevre933e5de2009-08-07 16:42:24 +02002917 xmlChar *tmpval, *cur;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002918 if (normOnTheFly) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002919 norm = xmlSchemaCollapseString(value);
2920 if (norm != NULL)
2921 value = norm;
2922 }
Vincent Lefevre933e5de2009-08-07 16:42:24 +02002923 tmpval = xmlStrdup(value);
2924 for (cur = tmpval; *cur; ++cur) {
2925 if (*cur < 32 || *cur >= 127 || *cur == ' ' ||
2926 *cur == '<' || *cur == '>' || *cur == '"' ||
2927 *cur == '{' || *cur == '}' || *cur == '|' ||
2928 *cur == '\\' || *cur == '^' || *cur == '`' ||
2929 *cur == '\'')
2930 *cur = '_';
2931 }
2932 uri = xmlParseURI((const char *) tmpval);
2933 xmlFree(tmpval);
Daniel Veillard11c466a2004-03-14 12:20:15 +00002934 if (uri == NULL)
2935 goto return1;
2936 xmlFreeURI(uri);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002937 }
Daniel Veillard11c466a2004-03-14 12:20:15 +00002938
2939 if (val != NULL) {
2940 v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
2941 if (v == NULL)
2942 goto error;
2943 v->value.str = xmlStrdup(value);
2944 *val = v;
2945 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002946 goto return0;
2947 }
2948 case XML_SCHEMAS_HEXBINARY:{
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002949 const xmlChar *cur = value, *start;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002950 xmlChar *base;
2951 int total, i = 0;
2952
Daniel Veillardf34a20e2004-08-31 08:42:17 +00002953 if (cur == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002954 goto return1;
2955
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002956 if (normOnTheFly)
2957 while IS_WSP_BLANK_CH(*cur) cur++;
2958
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002959 start = cur;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002960 while (((*cur >= '0') && (*cur <= '9')) ||
2961 ((*cur >= 'A') && (*cur <= 'F')) ||
2962 ((*cur >= 'a') && (*cur <= 'f'))) {
2963 i++;
2964 cur++;
2965 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002966 if (normOnTheFly)
2967 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002968
2969 if (*cur != 0)
2970 goto return1;
2971 if ((i % 2) != 0)
2972 goto return1;
2973
2974 if (val != NULL) {
2975
2976 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2977 if (v == NULL)
2978 goto error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002979 /*
2980 * Copy only the normalized piece.
2981 * CRITICAL TODO: Check this.
2982 */
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002983 cur = xmlStrndup(start, i);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002984 if (cur == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002985 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002986 xmlFree(v);
2987 goto return1;
2988 }
2989
2990 total = i / 2; /* number of octets */
2991
2992 base = (xmlChar *) cur;
2993 while (i-- > 0) {
2994 if (*base >= 'a')
2995 *base = *base - ('a' - 'A');
2996 base++;
2997 }
2998
2999 v->value.hex.str = (xmlChar *) cur;
3000 v->value.hex.total = total;
3001 *val = v;
3002 }
3003 goto return0;
3004 }
3005 case XML_SCHEMAS_BASE64BINARY:{
3006 /* ISSUE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003007 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003008 * Ignore all stray characters? (yes, currently)
3009 * Worry about long lines? (no, currently)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003010 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003011 * rfc2045.txt:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003012 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003013 * "The encoded output stream must be represented in lines of
3014 * no more than 76 characters each. All line breaks or other
3015 * characters not found in Table 1 must be ignored by decoding
3016 * software. In base64 data, characters other than those in
3017 * Table 1, line breaks, and other white space probably
3018 * indicate a transmission error, about which a warning
3019 * message or even a message rejection might be appropriate
3020 * under some circumstances." */
3021 const xmlChar *cur = value;
3022 xmlChar *base;
3023 int total, i = 0, pad = 0;
3024
3025 if (cur == NULL)
3026 goto return1;
3027
3028 for (; *cur; ++cur) {
3029 int decc;
3030
3031 decc = _xmlSchemaBase64Decode(*cur);
3032 if (decc < 0) ;
3033 else if (decc < 64)
3034 i++;
3035 else
3036 break;
3037 }
3038 for (; *cur; ++cur) {
3039 int decc;
3040
3041 decc = _xmlSchemaBase64Decode(*cur);
3042 if (decc < 0) ;
3043 else if (decc < 64)
3044 goto return1;
3045 if (decc == 64)
3046 pad++;
3047 }
3048
3049 /* rfc2045.txt: "Special processing is performed if fewer than
3050 * 24 bits are available at the end of the data being encoded.
3051 * A full encoding quantum is always completed at the end of a
3052 * body. When fewer than 24 input bits are available in an
3053 * input group, zero bits are added (on the right) to form an
3054 * integral number of 6-bit groups. Padding at the end of the
3055 * data is performed using the "=" character. Since all
3056 * base64 input is an integral number of octets, only the
3057 * following cases can arise: (1) the final quantum of
3058 * encoding input is an integral multiple of 24 bits; here,
3059 * the final unit of encoded output will be an integral
3060 * multiple ofindent: Standard input:701: Warning:old style
3061 * assignment ambiguity in "=*". Assuming "= *" 4 characters
3062 * with no "=" padding, (2) the final
3063 * quantum of encoding input is exactly 8 bits; here, the
3064 * final unit of encoded output will be two characters
3065 * followed by two "=" padding characters, or (3) the final
3066 * quantum of encoding input is exactly 16 bits; here, the
3067 * final unit of encoded output will be three characters
3068 * followed by one "=" padding character." */
3069
3070 total = 3 * (i / 4);
3071 if (pad == 0) {
3072 if (i % 4 != 0)
3073 goto return1;
3074 } else if (pad == 1) {
3075 int decc;
3076
3077 if (i % 4 != 3)
3078 goto return1;
3079 for (decc = _xmlSchemaBase64Decode(*cur);
3080 (decc < 0) || (decc > 63);
3081 decc = _xmlSchemaBase64Decode(*cur))
3082 --cur;
3083 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
3084 /* 00111100 -> 0x3c */
3085 if (decc & ~0x3c)
3086 goto return1;
3087 total += 2;
3088 } else if (pad == 2) {
3089 int decc;
3090
3091 if (i % 4 != 2)
3092 goto return1;
3093 for (decc = _xmlSchemaBase64Decode(*cur);
3094 (decc < 0) || (decc > 63);
3095 decc = _xmlSchemaBase64Decode(*cur))
3096 --cur;
3097 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
3098 /* 00110000 -> 0x30 */
3099 if (decc & ~0x30)
3100 goto return1;
3101 total += 1;
3102 } else
3103 goto return1;
3104
3105 if (val != NULL) {
3106 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
3107 if (v == NULL)
3108 goto error;
3109 base =
3110 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
3111 sizeof(xmlChar));
3112 if (base == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003113 xmlSchemaTypeErrMemory(node, "allocating base64 data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003114 xmlFree(v);
3115 goto return1;
3116 }
3117 v->value.base64.str = base;
3118 for (cur = value; *cur; ++cur)
3119 if (_xmlSchemaBase64Decode(*cur) >= 0) {
3120 *base = *cur;
3121 ++base;
3122 }
3123 *base = 0;
3124 v->value.base64.total = total;
3125 *val = v;
3126 }
3127 goto return0;
3128 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003129 case XML_SCHEMAS_INTEGER:
3130 case XML_SCHEMAS_PINTEGER:
3131 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003132 case XML_SCHEMAS_NINTEGER:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003133 case XML_SCHEMAS_NNINTEGER:{
3134 const xmlChar *cur = value;
3135 unsigned long lo, mi, hi;
William M. Brackec3b4b72005-03-15 15:50:17 +00003136 int sign = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003137
3138 if (cur == NULL)
3139 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003140 if (normOnTheFly)
3141 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003142 if (*cur == '-') {
3143 sign = 1;
3144 cur++;
3145 } else if (*cur == '+')
3146 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003147 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
Daniel Veillardbfc42632008-04-03 10:43:52 +00003148 if (ret < 0)
William M. Brackec3b4b72005-03-15 15:50:17 +00003149 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003150 if (normOnTheFly)
3151 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003152 if (*cur != 0)
3153 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003154 if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003155 if ((sign == 0) &&
3156 ((hi != 0) || (mi != 0) || (lo != 0)))
3157 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003158 } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003159 if (sign == 1)
3160 goto return1;
3161 if ((hi == 0) && (mi == 0) && (lo == 0))
3162 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003163 } else if (type->builtInType == XML_SCHEMAS_NINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003164 if (sign == 0)
3165 goto return1;
3166 if ((hi == 0) && (mi == 0) && (lo == 0))
3167 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003168 } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003169 if ((sign == 1) &&
3170 ((hi != 0) || (mi != 0) || (lo != 0)))
3171 goto return1;
3172 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003173 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003174 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003175 if (v != NULL) {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003176 if (ret == 0)
3177 ret++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003178 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003179 v->value.decimal.mi = mi;
3180 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003181 v->value.decimal.sign = sign;
3182 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003183 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003184 *val = v;
3185 }
3186 }
3187 goto return0;
3188 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003189 case XML_SCHEMAS_LONG:
3190 case XML_SCHEMAS_BYTE:
3191 case XML_SCHEMAS_SHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003192 case XML_SCHEMAS_INT:{
Daniel Veillardbfc42632008-04-03 10:43:52 +00003193 const xmlChar *cur = value;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003194 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003195 int sign = 0;
3196
3197 if (cur == NULL)
3198 goto return1;
3199 if (*cur == '-') {
3200 sign = 1;
3201 cur++;
3202 } else if (*cur == '+')
3203 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003204 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3205 if (ret < 0)
3206 goto return1;
3207 if (*cur != 0)
3208 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003209 if (type->builtInType == XML_SCHEMAS_LONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003210 if (hi >= 922) {
3211 if (hi > 922)
3212 goto return1;
3213 if (mi >= 33720368) {
3214 if (mi > 33720368)
3215 goto return1;
3216 if ((sign == 0) && (lo > 54775807))
3217 goto return1;
3218 if ((sign == 1) && (lo > 54775808))
3219 goto return1;
3220 }
3221 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003222 } else if (type->builtInType == XML_SCHEMAS_INT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003223 if (hi != 0)
3224 goto return1;
3225 if (mi >= 21) {
3226 if (mi > 21)
3227 goto return1;
3228 if ((sign == 0) && (lo > 47483647))
3229 goto return1;
3230 if ((sign == 1) && (lo > 47483648))
3231 goto return1;
3232 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003233 } else if (type->builtInType == XML_SCHEMAS_SHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003234 if ((mi != 0) || (hi != 0))
3235 goto return1;
3236 if ((sign == 1) && (lo > 32768))
3237 goto return1;
3238 if ((sign == 0) && (lo > 32767))
3239 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003240 } else if (type->builtInType == XML_SCHEMAS_BYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003241 if ((mi != 0) || (hi != 0))
3242 goto return1;
3243 if ((sign == 1) && (lo > 128))
3244 goto return1;
3245 if ((sign == 0) && (lo > 127))
3246 goto return1;
3247 }
3248 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003249 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003250 if (v != NULL) {
3251 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003252 v->value.decimal.mi = mi;
3253 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003254 v->value.decimal.sign = sign;
3255 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003256 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003257 *val = v;
3258 }
3259 }
3260 goto return0;
3261 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003262 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003263 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003264 case XML_SCHEMAS_USHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003265 case XML_SCHEMAS_UBYTE:{
3266 const xmlChar *cur = value;
3267 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003268
3269 if (cur == NULL)
3270 goto return1;
William M. Brackec3b4b72005-03-15 15:50:17 +00003271 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3272 if (ret < 0)
3273 goto return1;
3274 if (*cur != 0)
3275 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003276 if (type->builtInType == XML_SCHEMAS_ULONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003277 if (hi >= 1844) {
3278 if (hi > 1844)
3279 goto return1;
3280 if (mi >= 67440737) {
3281 if (mi > 67440737)
3282 goto return1;
3283 if (lo > 9551615)
3284 goto return1;
3285 }
3286 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003287 } else if (type->builtInType == XML_SCHEMAS_UINT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003288 if (hi != 0)
3289 goto return1;
3290 if (mi >= 42) {
3291 if (mi > 42)
3292 goto return1;
3293 if (lo > 94967295)
3294 goto return1;
3295 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003296 } else if (type->builtInType == XML_SCHEMAS_USHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003297 if ((mi != 0) || (hi != 0))
3298 goto return1;
3299 if (lo > 65535)
3300 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003301 } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003302 if ((mi != 0) || (hi != 0))
3303 goto return1;
3304 if (lo > 255)
3305 goto return1;
3306 }
3307 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003308 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003309 if (v != NULL) {
3310 v->value.decimal.lo = lo;
3311 v->value.decimal.mi = mi;
3312 v->value.decimal.hi = hi;
3313 v->value.decimal.sign = 0;
3314 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003315 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003316 *val = v;
3317 }
3318 }
3319 goto return0;
3320 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003321 }
3322
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003323 done:
3324 if (norm != NULL)
3325 xmlFree(norm);
3326 return (ret);
3327 return3:
3328 if (norm != NULL)
3329 xmlFree(norm);
3330 return (3);
3331 return1:
3332 if (norm != NULL)
3333 xmlFree(norm);
3334 return (1);
3335 return0:
3336 if (norm != NULL)
3337 xmlFree(norm);
3338 return (0);
3339 error:
3340 if (norm != NULL)
3341 xmlFree(norm);
3342 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003343}
3344
3345/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003346 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00003347 * @type: the predefined type
3348 * @value: the value to check
3349 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003350 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00003351 *
3352 * Check that a value conforms to the lexical space of the predefined type.
3353 * if true a value is computed and returned in @val.
3354 *
3355 * Returns 0 if this validates, a positive error code number otherwise
3356 * and -1 in case of internal or API error.
3357 */
3358int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003359xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
3360 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003361 return(xmlSchemaValAtomicType(type, value, val, node, 0,
3362 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00003363}
3364
3365/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00003366 * xmlSchemaValPredefTypeNodeNoNorm:
3367 * @type: the predefined type
3368 * @value: the value to check
3369 * @val: the return computed value
3370 * @node: the node containing the value
3371 *
3372 * Check that a value conforms to the lexical space of the predefined type.
3373 * if true a value is computed and returned in @val.
3374 * This one does apply any normalization to the value.
3375 *
3376 * Returns 0 if this validates, a positive error code number otherwise
3377 * and -1 in case of internal or API error.
3378 */
3379int
3380xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
3381 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003382 return(xmlSchemaValAtomicType(type, value, val, node, 1,
3383 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
Daniel Veillardc0826a72004-08-10 14:17:33 +00003384}
3385
3386/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003387 * xmlSchemaValidatePredefinedType:
3388 * @type: the predefined type
3389 * @value: the value to check
3390 * @val: the return computed value
3391 *
3392 * Check that a value conforms to the lexical space of the predefined type.
3393 * if true a value is computed and returned in @val.
3394 *
3395 * Returns 0 if this validates, a positive error code number otherwise
3396 * and -1 in case of internal or API error.
3397 */
3398int
3399xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
3400 xmlSchemaValPtr *val) {
3401 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
3402}
3403
3404/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003405 * xmlSchemaCompareDecimals:
3406 * @x: a first decimal value
3407 * @y: a second decimal value
3408 *
3409 * Compare 2 decimals
3410 *
3411 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
3412 */
3413static int
3414xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
3415{
3416 xmlSchemaValPtr swp;
William M. Brack273670f2005-03-11 15:55:14 +00003417 int order = 1, integx, integy, dlen;
3418 unsigned long hi, mi, lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003419
William M. Brack273670f2005-03-11 15:55:14 +00003420 /*
3421 * First test: If x is -ve and not zero
3422 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003423 if ((x->value.decimal.sign) &&
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003424 ((x->value.decimal.lo != 0) ||
3425 (x->value.decimal.mi != 0) ||
3426 (x->value.decimal.hi != 0))) {
William M. Brack273670f2005-03-11 15:55:14 +00003427 /*
3428 * Then if y is -ve and not zero reverse the compare
3429 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003430 if ((y->value.decimal.sign) &&
3431 ((y->value.decimal.lo != 0) ||
3432 (y->value.decimal.mi != 0) ||
3433 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00003434 order = -1;
William M. Brack273670f2005-03-11 15:55:14 +00003435 /*
3436 * Otherwise (y >= 0) we have the answer
3437 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003438 else
3439 return (-1);
William M. Brack273670f2005-03-11 15:55:14 +00003440 /*
3441 * If x is not -ve and y is -ve we have the answer
3442 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003443 } else if ((y->value.decimal.sign) &&
3444 ((y->value.decimal.lo != 0) ||
3445 (y->value.decimal.mi != 0) ||
3446 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003447 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00003448 }
William M. Brack273670f2005-03-11 15:55:14 +00003449 /*
3450 * If it's not simply determined by a difference in sign,
3451 * then we need to compare the actual values of the two nums.
3452 * To do this, we start by looking at the integral parts.
3453 * If the number of integral digits differ, then we have our
3454 * answer.
3455 */
3456 integx = x->value.decimal.total - x->value.decimal.frac;
3457 integy = y->value.decimal.total - y->value.decimal.frac;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003458 /*
3459 * NOTE: We changed the "total" for values like "0.1"
3460 * (or "-0.1" or ".1") to be 1, which was 2 previously.
3461 * Therefore the special case, when such values are
3462 * compared with 0, needs to be handled separately;
3463 * otherwise a zero would be recognized incorrectly as
3464 * greater than those values. This has the nice side effect
3465 * that we gain an overall optimized comparison with zeroes.
3466 * Note that a "0" has a "total" of 1 already.
3467 */
3468 if (integx == 1) {
3469 if (x->value.decimal.lo == 0) {
3470 if (integy != 1)
3471 return -order;
3472 else if (y->value.decimal.lo != 0)
3473 return -order;
3474 else
3475 return(0);
3476 }
3477 }
3478 if (integy == 1) {
3479 if (y->value.decimal.lo == 0) {
3480 if (integx != 1)
3481 return order;
3482 else if (x->value.decimal.lo != 0)
3483 return order;
3484 else
3485 return(0);
3486 }
3487 }
3488
William M. Brack273670f2005-03-11 15:55:14 +00003489 if (integx > integy)
3490 return order;
3491 else if (integy > integx)
3492 return -order;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003493
William M. Brack273670f2005-03-11 15:55:14 +00003494 /*
3495 * If the number of integral digits is the same for both numbers,
3496 * then things get a little more complicated. We need to "normalize"
3497 * the numbers in order to properly compare them. To do this, we
3498 * look at the total length of each number (length => number of
3499 * significant digits), and divide the "shorter" by 10 (decreasing
3500 * the length) until they are of equal length.
3501 */
3502 dlen = x->value.decimal.total - y->value.decimal.total;
3503 if (dlen < 0) { /* y has more digits than x */
3504 swp = x;
3505 hi = y->value.decimal.hi;
3506 mi = y->value.decimal.mi;
3507 lo = y->value.decimal.lo;
3508 dlen = -dlen;
3509 order = -order;
3510 } else { /* x has more digits than y */
3511 swp = y;
3512 hi = x->value.decimal.hi;
3513 mi = x->value.decimal.mi;
3514 lo = x->value.decimal.lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003515 }
William M. Brack273670f2005-03-11 15:55:14 +00003516 while (dlen > 8) { /* in effect, right shift by 10**8 */
3517 lo = mi;
3518 mi = hi;
3519 hi = 0;
3520 dlen -= 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00003521 }
William M. Brack273670f2005-03-11 15:55:14 +00003522 while (dlen > 0) {
3523 unsigned long rem1, rem2;
3524 rem1 = (hi % 10) * 100000000L;
3525 hi = hi / 10;
3526 rem2 = (mi % 10) * 100000000L;
3527 mi = (mi + rem1) / 10;
3528 lo = (lo + rem2) / 10;
3529 dlen--;
3530 }
3531 if (hi > swp->value.decimal.hi) {
3532 return order;
3533 } else if (hi == swp->value.decimal.hi) {
3534 if (mi > swp->value.decimal.mi) {
3535 return order;
3536 } else if (mi == swp->value.decimal.mi) {
3537 if (lo > swp->value.decimal.lo) {
3538 return order;
3539 } else if (lo == swp->value.decimal.lo) {
3540 if (x->value.decimal.total == y->value.decimal.total) {
3541 return 0;
3542 } else {
3543 return order;
3544 }
3545 }
3546 }
3547 }
3548 return -order;
Daniel Veillard4255d502002-04-16 15:50:10 +00003549}
3550
3551/**
Daniel Veillard070803b2002-05-03 07:29:38 +00003552 * xmlSchemaCompareDurations:
3553 * @x: a first duration value
3554 * @y: a second duration value
3555 *
3556 * Compare 2 durations
3557 *
3558 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3559 * case of error
3560 */
3561static int
3562xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
3563{
3564 long carry, mon, day;
3565 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00003566 int invert = 1;
3567 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00003568 static const long dayRange [2][12] = {
3569 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
3570 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
3571
3572 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00003573 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00003574
3575 /* months */
3576 mon = x->value.dur.mon - y->value.dur.mon;
3577
3578 /* seconds */
3579 sec = x->value.dur.sec - y->value.dur.sec;
Daniel Veillardbbcf1272011-11-10 23:23:10 +08003580 carry = (long)(sec / SECS_PER_DAY);
3581 sec -= ((double)carry) * SECS_PER_DAY;
Daniel Veillard070803b2002-05-03 07:29:38 +00003582
3583 /* days */
3584 day = x->value.dur.day - y->value.dur.day + carry;
3585
3586 /* easy test */
3587 if (mon == 0) {
3588 if (day == 0)
3589 if (sec == 0.0)
3590 return 0;
3591 else if (sec < 0.0)
3592 return -1;
3593 else
3594 return 1;
3595 else if (day < 0)
3596 return -1;
3597 else
3598 return 1;
3599 }
3600
3601 if (mon > 0) {
3602 if ((day >= 0) && (sec >= 0.0))
3603 return 1;
3604 else {
3605 xmon = mon;
3606 xday = -day;
3607 }
3608 } else if ((day <= 0) && (sec <= 0.0)) {
3609 return -1;
3610 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00003611 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00003612 xmon = -mon;
3613 xday = day;
3614 }
3615
3616 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00003617 if (myear == 0) {
3618 minday = 0;
3619 maxday = 0;
3620 } else {
3621 maxday = 366 * ((myear + 3) / 4) +
3622 365 * ((myear - 1) % 4);
3623 minday = maxday - 1;
3624 }
3625
Daniel Veillard070803b2002-05-03 07:29:38 +00003626 xmon = xmon % 12;
3627 minday += dayRange[0][xmon];
3628 maxday += dayRange[1][xmon];
3629
Daniel Veillard80b19092003-03-28 13:29:53 +00003630 if ((maxday == minday) && (maxday == xday))
3631 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00003632 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00003633 return(-invert);
3634 if (minday > xday)
3635 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00003636
3637 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00003638 return 2;
3639}
3640
3641/*
3642 * macros for adding date/times and durations
3643 */
3644#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
3645#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
3646#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
3647#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
3648
3649/**
Daniel Veillard669adfc2004-05-29 20:12:46 +00003650 * xmlSchemaDupVal:
3651 * @v: the #xmlSchemaValPtr value to duplicate
3652 *
3653 * Makes a copy of @v. The calling program is responsible for freeing
3654 * the returned value.
3655 *
3656 * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
3657 */
3658static xmlSchemaValPtr
3659xmlSchemaDupVal (xmlSchemaValPtr v)
3660{
3661 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
3662 if (ret == NULL)
3663 return NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003664
Daniel Veillard669adfc2004-05-29 20:12:46 +00003665 memcpy(ret, v, sizeof(xmlSchemaVal));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003666 ret->next = NULL;
Daniel Veillard669adfc2004-05-29 20:12:46 +00003667 return ret;
3668}
3669
3670/**
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003671 * xmlSchemaCopyValue:
3672 * @val: the precomputed value to be copied
3673 *
3674 * Copies the precomputed value. This duplicates any string within.
3675 *
3676 * Returns the copy or NULL if a copy for a data-type is not implemented.
3677 */
3678xmlSchemaValPtr
3679xmlSchemaCopyValue(xmlSchemaValPtr val)
3680{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003681 xmlSchemaValPtr ret = NULL, prev = NULL, cur;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003682
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003683 /*
3684 * Copy the string values.
3685 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003686 while (val != NULL) {
3687 switch (val->type) {
3688 case XML_SCHEMAS_ANYTYPE:
3689 case XML_SCHEMAS_IDREFS:
3690 case XML_SCHEMAS_ENTITIES:
3691 case XML_SCHEMAS_NMTOKENS:
3692 xmlSchemaFreeValue(ret);
3693 return (NULL);
3694 case XML_SCHEMAS_ANYSIMPLETYPE:
3695 case XML_SCHEMAS_STRING:
3696 case XML_SCHEMAS_NORMSTRING:
3697 case XML_SCHEMAS_TOKEN:
3698 case XML_SCHEMAS_LANGUAGE:
3699 case XML_SCHEMAS_NAME:
3700 case XML_SCHEMAS_NCNAME:
3701 case XML_SCHEMAS_ID:
3702 case XML_SCHEMAS_IDREF:
3703 case XML_SCHEMAS_ENTITY:
3704 case XML_SCHEMAS_NMTOKEN:
3705 case XML_SCHEMAS_ANYURI:
3706 cur = xmlSchemaDupVal(val);
3707 if (val->value.str != NULL)
3708 cur->value.str = xmlStrdup(BAD_CAST val->value.str);
3709 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003710 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003711 case XML_SCHEMAS_NOTATION:
3712 cur = xmlSchemaDupVal(val);
3713 if (val->value.qname.name != NULL)
3714 cur->value.qname.name =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003715 xmlStrdup(BAD_CAST val->value.qname.name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003716 if (val->value.qname.uri != NULL)
3717 cur->value.qname.uri =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003718 xmlStrdup(BAD_CAST val->value.qname.uri);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003719 break;
3720 case XML_SCHEMAS_HEXBINARY:
3721 cur = xmlSchemaDupVal(val);
3722 if (val->value.hex.str != NULL)
3723 cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
3724 break;
3725 case XML_SCHEMAS_BASE64BINARY:
3726 cur = xmlSchemaDupVal(val);
3727 if (val->value.base64.str != NULL)
3728 cur->value.base64.str =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003729 xmlStrdup(BAD_CAST val->value.base64.str);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003730 break;
3731 default:
3732 cur = xmlSchemaDupVal(val);
3733 break;
3734 }
3735 if (ret == NULL)
3736 ret = cur;
3737 else
3738 prev->next = cur;
3739 prev = cur;
3740 val = val->next;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003741 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003742 return (ret);
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003743}
3744
3745/**
Daniel Veillard5a872412002-05-22 06:40:27 +00003746 * _xmlSchemaDateAdd:
3747 * @dt: an #xmlSchemaValPtr
3748 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
3749 *
3750 * Compute a new date/time from @dt and @dur. This function assumes @dt
3751 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
Daniel Veillard669adfc2004-05-29 20:12:46 +00003752 * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
3753 * @dt. The calling program is responsible for freeing the returned value.
Daniel Veillard5a872412002-05-22 06:40:27 +00003754 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003755 * Returns a pointer to a new #xmlSchemaVal or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003756 */
3757static xmlSchemaValPtr
3758_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
3759{
Daniel Veillard669adfc2004-05-29 20:12:46 +00003760 xmlSchemaValPtr ret, tmp;
Daniel Veillard5a872412002-05-22 06:40:27 +00003761 long carry, tempdays, temp;
3762 xmlSchemaValDatePtr r, d;
3763 xmlSchemaValDurationPtr u;
3764
3765 if ((dt == NULL) || (dur == NULL))
3766 return NULL;
3767
3768 ret = xmlSchemaNewValue(dt->type);
3769 if (ret == NULL)
3770 return NULL;
3771
Daniel Veillard669adfc2004-05-29 20:12:46 +00003772 /* make a copy so we don't alter the original value */
3773 tmp = xmlSchemaDupVal(dt);
3774 if (tmp == NULL) {
3775 xmlSchemaFreeValue(ret);
3776 return NULL;
3777 }
3778
Daniel Veillard5a872412002-05-22 06:40:27 +00003779 r = &(ret->value.date);
Daniel Veillard669adfc2004-05-29 20:12:46 +00003780 d = &(tmp->value.date);
Daniel Veillard5a872412002-05-22 06:40:27 +00003781 u = &(dur->value.dur);
3782
3783 /* normalization */
3784 if (d->mon == 0)
3785 d->mon = 1;
3786
3787 /* normalize for time zone offset */
3788 u->sec -= (d->tzo * 60);
3789 d->tzo = 0;
3790
3791 /* normalization */
3792 if (d->day == 0)
3793 d->day = 1;
3794
3795 /* month */
3796 carry = d->mon + u->mon;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003797 r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
3798 carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003799
3800 /* year (may be modified later) */
3801 r->year = d->year + carry;
3802 if (r->year == 0) {
3803 if (d->year > 0)
3804 r->year--;
3805 else
3806 r->year++;
3807 }
3808
3809 /* time zone */
3810 r->tzo = d->tzo;
3811 r->tz_flag = d->tz_flag;
3812
3813 /* seconds */
3814 r->sec = d->sec + u->sec;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003815 carry = (long) FQUOTIENT((long)r->sec, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003816 if (r->sec != 0.0) {
3817 r->sec = MODULO(r->sec, 60.0);
3818 }
3819
3820 /* minute */
3821 carry += d->min;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003822 r->min = (unsigned int) MODULO(carry, 60);
3823 carry = (long) FQUOTIENT(carry, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003824
3825 /* hours */
3826 carry += d->hour;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003827 r->hour = (unsigned int) MODULO(carry, 24);
3828 carry = (long)FQUOTIENT(carry, 24);
Daniel Veillard5a872412002-05-22 06:40:27 +00003829
3830 /*
3831 * days
3832 * Note we use tempdays because the temporary values may need more
3833 * than 5 bits
3834 */
3835 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
3836 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
3837 tempdays = MAX_DAYINMONTH(r->year, r->mon);
3838 else if (d->day < 1)
3839 tempdays = 1;
3840 else
3841 tempdays = d->day;
3842
3843 tempdays += u->day + carry;
3844
3845 while (1) {
3846 if (tempdays < 1) {
Daniel Veillard5e9576a2005-11-21 11:23:47 +00003847 long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13);
3848 long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003849 if (tyr == 0)
3850 tyr--;
Daniel Veillard14b56432006-03-09 18:41:40 +00003851 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003852 * Coverity detected an overrun in daysInMonth
Daniel Veillard14b56432006-03-09 18:41:40 +00003853 * of size 12 at position 12 with index variable "((r)->mon - 1)"
3854 */
David Kilzer30cf4392014-07-14 22:29:56 +08003855 if (tmon < 1)
3856 tmon = 1;
Daniel Veillard14b56432006-03-09 18:41:40 +00003857 if (tmon > 12)
3858 tmon = 12;
Daniel Veillard5a872412002-05-22 06:40:27 +00003859 tempdays += MAX_DAYINMONTH(tyr, tmon);
3860 carry = -1;
Philip Withnall4ba5d312014-06-20 21:37:21 +01003861 } else if (VALID_YEAR(r->year) && VALID_MONTH(r->mon) &&
3862 tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003863 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
3864 carry = 1;
3865 } else
3866 break;
3867
3868 temp = r->mon + carry;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003869 r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
3870 r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003871 if (r->year == 0) {
3872 if (temp < 1)
3873 r->year--;
3874 else
3875 r->year++;
3876 }
3877 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003878
Daniel Veillard5a872412002-05-22 06:40:27 +00003879 r->day = tempdays;
3880
3881 /*
3882 * adjust the date/time type to the date values
3883 */
3884 if (ret->type != XML_SCHEMAS_DATETIME) {
3885 if ((r->hour) || (r->min) || (r->sec))
3886 ret->type = XML_SCHEMAS_DATETIME;
3887 else if (ret->type != XML_SCHEMAS_DATE) {
3888 if ((r->mon != 1) && (r->day != 1))
3889 ret->type = XML_SCHEMAS_DATE;
3890 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
3891 ret->type = XML_SCHEMAS_GYEARMONTH;
3892 }
3893 }
3894
Daniel Veillard669adfc2004-05-29 20:12:46 +00003895 xmlSchemaFreeValue(tmp);
Daniel Veillard5a872412002-05-22 06:40:27 +00003896
Daniel Veillard5a872412002-05-22 06:40:27 +00003897 return ret;
3898}
3899
3900/**
3901 * xmlSchemaDateNormalize:
Daniel Veillard669adfc2004-05-29 20:12:46 +00003902 * @dt: an #xmlSchemaValPtr of a date/time type value.
3903 * @offset: number of seconds to adjust @dt by.
Daniel Veillard5a872412002-05-22 06:40:27 +00003904 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003905 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3906 * the return value is a time-zone offset is present on @dt.
Daniel Veillard5a872412002-05-22 06:40:27 +00003907 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003908 * Returns a normalized copy of @dt or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003909 */
3910static xmlSchemaValPtr
3911xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
3912{
3913 xmlSchemaValPtr dur, ret;
3914
3915 if (dt == NULL)
3916 return NULL;
3917
3918 if (((dt->type != XML_SCHEMAS_TIME) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003919 (dt->type != XML_SCHEMAS_DATETIME) &&
3920 (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
Daniel Veillard5a872412002-05-22 06:40:27 +00003921 return xmlSchemaDupVal(dt);
3922
3923 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
3924 if (dur == NULL)
3925 return NULL;
3926
3927 dur->value.date.sec -= offset;
3928
3929 ret = _xmlSchemaDateAdd(dt, dur);
3930 if (ret == NULL)
3931 return NULL;
3932
3933 xmlSchemaFreeValue(dur);
3934
3935 /* ret->value.date.tzo = 0; */
3936 return ret;
3937}
3938
3939/**
3940 * _xmlSchemaDateCastYMToDays:
3941 * @dt: an #xmlSchemaValPtr
3942 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003943 * Convert mon and year of @dt to total number of days. Take the
Daniel Veillard5a872412002-05-22 06:40:27 +00003944 * number of years since (or before) 1 AD and add the number of leap
3945 * years. This is a function because negative
3946 * years must be handled a little differently and there is no zero year.
3947 *
3948 * Returns number of days.
3949 */
3950static long
3951_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
3952{
3953 long ret;
Daniel Veillard49e89632004-09-23 16:24:36 +00003954 int mon;
Daniel Veillard5a872412002-05-22 06:40:27 +00003955
Daniel Veillard49e89632004-09-23 16:24:36 +00003956 mon = dt->value.date.mon;
3957 if (mon <= 0) mon = 1; /* normalization */
3958
3959 if (dt->value.date.year <= 0)
Daniel Veillard5a872412002-05-22 06:40:27 +00003960 ret = (dt->value.date.year * 365) +
3961 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
3962 ((dt->value.date.year+1)/400)) +
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 else
3965 ret = ((dt->value.date.year-1) * 365) +
3966 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
3967 ((dt->value.date.year-1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003968 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003969
3970 return ret;
3971}
3972
3973/**
3974 * TIME_TO_NUMBER:
3975 * @dt: an #xmlSchemaValPtr
3976 *
3977 * Calculates the number of seconds in the time portion of @dt.
3978 *
3979 * Returns seconds.
3980 */
3981#define TIME_TO_NUMBER(dt) \
3982 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00003983 (dt->value.date.min * SECS_PER_MIN) + \
3984 (dt->value.date.tzo * SECS_PER_MIN)) + \
3985 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00003986
3987/**
3988 * xmlSchemaCompareDates:
3989 * @x: a first date/time value
3990 * @y: a second date/time value
3991 *
3992 * Compare 2 date/times
3993 *
3994 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3995 * case of error
3996 */
3997static int
3998xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
3999{
4000 unsigned char xmask, ymask, xor_mask, and_mask;
4001 xmlSchemaValPtr p1, p2, q1, q2;
4002 long p1d, p2d, q1d, q2d;
4003
4004 if ((x == NULL) || (y == NULL))
4005 return -2;
4006
4007 if (x->value.date.tz_flag) {
4008
4009 if (!y->value.date.tz_flag) {
4010 p1 = xmlSchemaDateNormalize(x, 0);
4011 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4012 /* normalize y + 14:00 */
4013 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
4014
4015 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004016 if (p1d < q1d) {
4017 xmlSchemaFreeValue(p1);
4018 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004019 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004020 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004021 double sec;
4022
4023 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004024 if (sec < 0.0) {
4025 xmlSchemaFreeValue(p1);
4026 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004027 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004028 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004029 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004030 /* normalize y - 14:00 */
4031 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
4032 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
4033 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004034 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004035 else if (p1d == q2d) {
4036 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
4037 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004038 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004039 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004040 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004041 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004042 xmlSchemaFreeValue(p1);
4043 xmlSchemaFreeValue(q1);
4044 xmlSchemaFreeValue(q2);
4045 if (ret != 0)
4046 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004047 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004048 } else {
4049 xmlSchemaFreeValue(p1);
4050 xmlSchemaFreeValue(q1);
4051 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004052 }
4053 } else if (y->value.date.tz_flag) {
4054 q1 = xmlSchemaDateNormalize(y, 0);
4055 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4056
4057 /* normalize x - 14:00 */
4058 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
4059 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4060
Daniel Veillardfdc91562002-07-01 21:52:03 +00004061 if (p1d < q1d) {
4062 xmlSchemaFreeValue(p1);
4063 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004064 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004065 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004066 double sec;
4067
4068 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004069 if (sec < 0.0) {
4070 xmlSchemaFreeValue(p1);
4071 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004072 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004073 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004074 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004075 /* normalize x + 14:00 */
4076 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
4077 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
4078
Daniel Veillard6560a422003-03-27 21:25:38 +00004079 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004080 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00004081 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004082 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
4083 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004084 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004085 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004086 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004087 }
Daniel Veillard6560a422003-03-27 21:25:38 +00004088 xmlSchemaFreeValue(p1);
4089 xmlSchemaFreeValue(q1);
4090 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004091 if (ret != 0)
4092 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004093 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004094 } else {
4095 xmlSchemaFreeValue(p1);
4096 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004097 }
4098 }
4099
4100 /*
4101 * if the same type then calculate the difference
4102 */
4103 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004104 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004105 q1 = xmlSchemaDateNormalize(y, 0);
4106 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4107
4108 p1 = xmlSchemaDateNormalize(x, 0);
4109 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4110
Daniel Veillardfdc91562002-07-01 21:52:03 +00004111 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004112 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004113 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004114 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004115 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00004116 double sec;
4117
4118 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4119 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004120 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004121 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004122 ret = 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004123
Daniel Veillard5a872412002-05-22 06:40:27 +00004124 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004125 xmlSchemaFreeValue(p1);
4126 xmlSchemaFreeValue(q1);
4127 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004128 }
4129
4130 switch (x->type) {
4131 case XML_SCHEMAS_DATETIME:
4132 xmask = 0xf;
4133 break;
4134 case XML_SCHEMAS_DATE:
4135 xmask = 0x7;
4136 break;
4137 case XML_SCHEMAS_GYEAR:
4138 xmask = 0x1;
4139 break;
4140 case XML_SCHEMAS_GMONTH:
4141 xmask = 0x2;
4142 break;
4143 case XML_SCHEMAS_GDAY:
4144 xmask = 0x3;
4145 break;
4146 case XML_SCHEMAS_GYEARMONTH:
4147 xmask = 0x3;
4148 break;
4149 case XML_SCHEMAS_GMONTHDAY:
4150 xmask = 0x6;
4151 break;
4152 case XML_SCHEMAS_TIME:
4153 xmask = 0x8;
4154 break;
4155 default:
4156 xmask = 0;
4157 break;
4158 }
4159
4160 switch (y->type) {
4161 case XML_SCHEMAS_DATETIME:
4162 ymask = 0xf;
4163 break;
4164 case XML_SCHEMAS_DATE:
4165 ymask = 0x7;
4166 break;
4167 case XML_SCHEMAS_GYEAR:
4168 ymask = 0x1;
4169 break;
4170 case XML_SCHEMAS_GMONTH:
4171 ymask = 0x2;
4172 break;
4173 case XML_SCHEMAS_GDAY:
4174 ymask = 0x3;
4175 break;
4176 case XML_SCHEMAS_GYEARMONTH:
4177 ymask = 0x3;
4178 break;
4179 case XML_SCHEMAS_GMONTHDAY:
4180 ymask = 0x6;
4181 break;
4182 case XML_SCHEMAS_TIME:
4183 ymask = 0x8;
4184 break;
4185 default:
4186 ymask = 0;
4187 break;
4188 }
4189
4190 xor_mask = xmask ^ ymask; /* mark type differences */
4191 and_mask = xmask & ymask; /* mark field specification */
4192
4193 /* year */
4194 if (xor_mask & 1)
4195 return 2; /* indeterminate */
4196 else if (and_mask & 1) {
4197 if (x->value.date.year < y->value.date.year)
4198 return -1;
4199 else if (x->value.date.year > y->value.date.year)
4200 return 1;
4201 }
4202
4203 /* month */
4204 if (xor_mask & 2)
4205 return 2; /* indeterminate */
4206 else if (and_mask & 2) {
4207 if (x->value.date.mon < y->value.date.mon)
4208 return -1;
4209 else if (x->value.date.mon > y->value.date.mon)
4210 return 1;
4211 }
4212
4213 /* day */
4214 if (xor_mask & 4)
4215 return 2; /* indeterminate */
4216 else if (and_mask & 4) {
4217 if (x->value.date.day < y->value.date.day)
4218 return -1;
4219 else if (x->value.date.day > y->value.date.day)
4220 return 1;
4221 }
4222
4223 /* time */
4224 if (xor_mask & 8)
4225 return 2; /* indeterminate */
4226 else if (and_mask & 8) {
4227 if (x->value.date.hour < y->value.date.hour)
4228 return -1;
4229 else if (x->value.date.hour > y->value.date.hour)
4230 return 1;
4231 else if (x->value.date.min < y->value.date.min)
4232 return -1;
4233 else if (x->value.date.min > y->value.date.min)
4234 return 1;
4235 else if (x->value.date.sec < y->value.date.sec)
4236 return -1;
4237 else if (x->value.date.sec > y->value.date.sec)
4238 return 1;
4239 }
4240
Daniel Veillard070803b2002-05-03 07:29:38 +00004241 return 0;
4242}
4243
4244/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004245 * xmlSchemaComparePreserveReplaceStrings:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004246 * @x: a first string value
4247 * @y: a second string value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004248 * @invert: inverts the result if x < y or x > y.
4249 *
4250 * Compare 2 string for their normalized values.
4251 * @x is a string with whitespace of "preserve", @y is
4252 * a string with a whitespace of "replace". I.e. @x could
4253 * be an "xsd:string" and @y an "xsd:normalizedString".
4254 *
4255 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4256 * case of error
4257 */
4258static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004259xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
4260 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004261 int invert)
4262{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004263 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004264
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004265 while ((*x != 0) && (*y != 0)) {
4266 if (IS_WSP_REPLACE_CH(*y)) {
4267 if (! IS_WSP_SPACE_CH(*x)) {
4268 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004269 if (invert)
4270 return(1);
4271 else
4272 return(-1);
4273 } else {
4274 if (invert)
4275 return(-1);
4276 else
4277 return(1);
4278 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004279 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004280 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004281 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004282 if (tmp < 0) {
4283 if (invert)
4284 return(1);
4285 else
4286 return(-1);
4287 }
4288 if (tmp > 0) {
4289 if (invert)
4290 return(-1);
4291 else
4292 return(1);
4293 }
4294 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004295 x++;
4296 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004297 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004298 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004299 if (invert)
4300 return(-1);
4301 else
4302 return(1);
4303 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004304 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004305 if (invert)
4306 return(1);
4307 else
4308 return(-1);
4309 }
4310 return(0);
4311}
4312
4313/**
4314 * xmlSchemaComparePreserveCollapseStrings:
4315 * @x: a first string value
4316 * @y: a second string value
4317 *
4318 * Compare 2 string for their normalized values.
4319 * @x is a string with whitespace of "preserve", @y is
4320 * a string with a whitespace of "collapse". I.e. @x could
4321 * be an "xsd:string" and @y an "xsd:normalizedString".
4322 *
4323 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4324 * case of error
4325 */
4326static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004327xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
4328 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004329 int invert)
4330{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004331 int tmp;
4332
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004333 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004334 * Skip leading blank chars of the collapsed string.
4335 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004336 while IS_WSP_BLANK_CH(*y)
4337 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004338
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004339 while ((*x != 0) && (*y != 0)) {
4340 if IS_WSP_BLANK_CH(*y) {
4341 if (! IS_WSP_SPACE_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004342 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004343 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004344 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004345 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004346 if (invert)
4347 return(1);
4348 else
4349 return(-1);
4350 } else {
4351 if (invert)
4352 return(-1);
4353 else
4354 return(1);
4355 }
4356 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004357 x++;
4358 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004359 /*
4360 * Skip contiguous blank chars of the collapsed string.
4361 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004362 while IS_WSP_BLANK_CH(*y)
4363 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004364 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004365 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004366 if (tmp < 0) {
4367 if (invert)
4368 return(1);
4369 else
4370 return(-1);
4371 }
4372 if (tmp > 0) {
4373 if (invert)
4374 return(-1);
4375 else
4376 return(1);
4377 }
4378 }
4379 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004380 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004381 if (invert)
4382 return(-1);
4383 else
4384 return(1);
4385 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004386 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004387 /*
4388 * Skip trailing blank chars of the collapsed string.
4389 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004390 while IS_WSP_BLANK_CH(*y)
4391 y++;
4392 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004393 if (invert)
4394 return(1);
4395 else
4396 return(-1);
4397 }
4398 }
4399 return(0);
4400}
4401
4402/**
4403 * xmlSchemaComparePreserveCollapseStrings:
4404 * @x: a first string value
4405 * @y: a second string value
4406 *
4407 * Compare 2 string for their normalized values.
4408 * @x is a string with whitespace of "preserve", @y is
4409 * a string with a whitespace of "collapse". I.e. @x could
4410 * be an "xsd:string" and @y an "xsd:normalizedString".
4411 *
4412 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4413 * case of error
4414 */
4415static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004416xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
4417 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004418 int invert)
4419{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004420 int tmp;
4421
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004422 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004423 * Skip leading blank chars of the collapsed string.
4424 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004425 while IS_WSP_BLANK_CH(*y)
4426 y++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004427
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004428 while ((*x != 0) && (*y != 0)) {
4429 if IS_WSP_BLANK_CH(*y) {
4430 if (! IS_WSP_BLANK_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004431 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004432 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004433 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004434 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004435 if (invert)
4436 return(1);
4437 else
4438 return(-1);
4439 } else {
4440 if (invert)
4441 return(-1);
4442 else
4443 return(1);
4444 }
4445 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004446 x++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004447 y++;
4448 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004449 * Skip contiguous blank chars of the collapsed string.
4450 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004451 while IS_WSP_BLANK_CH(*y)
4452 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004453 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004454 if IS_WSP_BLANK_CH(*x) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004455 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004456 * The xv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004457 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004458 if ((0x20 - *y) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004459 if (invert)
4460 return(1);
4461 else
4462 return(-1);
4463 } else {
4464 if (invert)
4465 return(-1);
4466 else
4467 return(1);
4468 }
4469 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004470 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004471 if (tmp < 0)
4472 return(-1);
4473 if (tmp > 0)
4474 return(1);
4475 }
4476 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004477 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004478 if (invert)
4479 return(-1);
4480 else
4481 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004482 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004483 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004484 /*
4485 * Skip trailing blank chars of the collapsed string.
4486 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004487 while IS_WSP_BLANK_CH(*y)
4488 y++;
4489 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004490 if (invert)
4491 return(1);
4492 else
4493 return(-1);
4494 }
4495 }
4496 return(0);
4497}
4498
4499
4500/**
4501 * xmlSchemaCompareReplacedStrings:
4502 * @x: a first string value
4503 * @y: a second string value
4504 *
4505 * Compare 2 string for their normalized values.
4506 *
4507 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4508 * case of error
4509 */
4510static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004511xmlSchemaCompareReplacedStrings(const xmlChar *x,
4512 const xmlChar *y)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004513{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004514 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004515
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004516 while ((*x != 0) && (*y != 0)) {
4517 if IS_WSP_BLANK_CH(*y) {
4518 if (! IS_WSP_BLANK_CH(*x)) {
4519 if ((*x - 0x20) < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004520 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004521 else
4522 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004523 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004524 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004525 if IS_WSP_BLANK_CH(*x) {
4526 if ((0x20 - *y) < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004527 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004528 else
4529 return(1);
4530 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004531 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004532 if (tmp < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004533 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004534 if (tmp > 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004535 return(1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004536 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004537 x++;
4538 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004539 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004540 if (*x != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004541 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004542 if (*y != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004543 return(-1);
4544 return(0);
4545}
4546
4547/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004548 * xmlSchemaCompareNormStrings:
4549 * @x: a first string value
4550 * @y: a second string value
4551 *
4552 * Compare 2 string for their normalized values.
4553 *
4554 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4555 * case of error
4556 */
4557static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004558xmlSchemaCompareNormStrings(const xmlChar *x,
4559 const xmlChar *y) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00004560 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004561
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004562 while (IS_BLANK_CH(*x)) x++;
4563 while (IS_BLANK_CH(*y)) y++;
4564 while ((*x != 0) && (*y != 0)) {
4565 if (IS_BLANK_CH(*x)) {
4566 if (!IS_BLANK_CH(*y)) {
4567 tmp = *x - *y;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004568 return(tmp);
4569 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004570 while (IS_BLANK_CH(*x)) x++;
4571 while (IS_BLANK_CH(*y)) y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004572 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004573 tmp = *x++ - *y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004574 if (tmp < 0)
4575 return(-1);
4576 if (tmp > 0)
4577 return(1);
4578 }
4579 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004580 if (*x != 0) {
4581 while (IS_BLANK_CH(*x)) x++;
4582 if (*x != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004583 return(1);
4584 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004585 if (*y != 0) {
4586 while (IS_BLANK_CH(*y)) y++;
4587 if (*y != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004588 return(-1);
4589 }
4590 return(0);
4591}
4592
4593/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004594 * xmlSchemaCompareFloats:
4595 * @x: a first float or double value
4596 * @y: a second float or double value
4597 *
4598 * Compare 2 values
4599 *
4600 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4601 * case of error
4602 */
4603static int
4604xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4605 double d1, d2;
4606
4607 if ((x == NULL) || (y == NULL))
4608 return(-2);
4609
4610 /*
4611 * Cast everything to doubles.
4612 */
4613 if (x->type == XML_SCHEMAS_DOUBLE)
4614 d1 = x->value.d;
4615 else if (x->type == XML_SCHEMAS_FLOAT)
4616 d1 = x->value.f;
4617 else
4618 return(-2);
4619
4620 if (y->type == XML_SCHEMAS_DOUBLE)
4621 d2 = y->value.d;
4622 else if (y->type == XML_SCHEMAS_FLOAT)
4623 d2 = y->value.f;
4624 else
4625 return(-2);
4626
4627 /*
4628 * Check for special cases.
4629 */
4630 if (xmlXPathIsNaN(d1)) {
4631 if (xmlXPathIsNaN(d2))
4632 return(0);
4633 return(1);
4634 }
4635 if (xmlXPathIsNaN(d2))
4636 return(-1);
4637 if (d1 == xmlXPathPINF) {
4638 if (d2 == xmlXPathPINF)
4639 return(0);
4640 return(1);
4641 }
4642 if (d2 == xmlXPathPINF)
4643 return(-1);
4644 if (d1 == xmlXPathNINF) {
4645 if (d2 == xmlXPathNINF)
4646 return(0);
4647 return(-1);
4648 }
4649 if (d2 == xmlXPathNINF)
4650 return(1);
4651
4652 /*
4653 * basic tests, the last one we should have equality, but
4654 * portability is more important than speed and handling
4655 * NaN or Inf in a portable way is always a challenge, so ...
4656 */
4657 if (d1 < d2)
4658 return(-1);
4659 if (d1 > d2)
4660 return(1);
4661 if (d1 == d2)
4662 return(0);
4663 return(2);
4664}
4665
4666/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004667 * xmlSchemaCompareValues:
4668 * @x: a first value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004669 * @xvalue: the first value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004670 * @xwtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004671 * @y: a second value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004672 * @xvalue: the second value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004673 * @ywtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004674 *
4675 * Compare 2 values
4676 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004677 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4678 * comparable and -2 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004679 */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004680static int
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004681xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
4682 xmlSchemaValPtr x,
4683 const xmlChar *xvalue,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004684 xmlSchemaWhitespaceValueType xws,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004685 xmlSchemaValType ytype,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004686 xmlSchemaValPtr y,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004687 const xmlChar *yvalue,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004688 xmlSchemaWhitespaceValueType yws)
4689{
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004690 switch (xtype) {
Daniel Veillard80b19092003-03-28 13:29:53 +00004691 case XML_SCHEMAS_UNKNOWN:
William M. Brack2f2a6632004-08-20 23:09:47 +00004692 case XML_SCHEMAS_ANYTYPE:
Daniel Veillard80b19092003-03-28 13:29:53 +00004693 return(-2);
4694 case XML_SCHEMAS_INTEGER:
4695 case XML_SCHEMAS_NPINTEGER:
4696 case XML_SCHEMAS_NINTEGER:
4697 case XML_SCHEMAS_NNINTEGER:
4698 case XML_SCHEMAS_PINTEGER:
4699 case XML_SCHEMAS_INT:
4700 case XML_SCHEMAS_UINT:
4701 case XML_SCHEMAS_LONG:
4702 case XML_SCHEMAS_ULONG:
4703 case XML_SCHEMAS_SHORT:
4704 case XML_SCHEMAS_USHORT:
4705 case XML_SCHEMAS_BYTE:
4706 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00004707 case XML_SCHEMAS_DECIMAL:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004708 if ((x == NULL) || (y == NULL))
4709 return(-2);
4710 if (ytype == xtype)
Daniel Veillard80b19092003-03-28 13:29:53 +00004711 return(xmlSchemaCompareDecimals(x, y));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004712 if ((ytype == XML_SCHEMAS_DECIMAL) ||
4713 (ytype == XML_SCHEMAS_INTEGER) ||
4714 (ytype == XML_SCHEMAS_NPINTEGER) ||
4715 (ytype == XML_SCHEMAS_NINTEGER) ||
4716 (ytype == XML_SCHEMAS_NNINTEGER) ||
4717 (ytype == XML_SCHEMAS_PINTEGER) ||
4718 (ytype == XML_SCHEMAS_INT) ||
4719 (ytype == XML_SCHEMAS_UINT) ||
4720 (ytype == XML_SCHEMAS_LONG) ||
4721 (ytype == XML_SCHEMAS_ULONG) ||
4722 (ytype == XML_SCHEMAS_SHORT) ||
4723 (ytype == XML_SCHEMAS_USHORT) ||
4724 (ytype == XML_SCHEMAS_BYTE) ||
4725 (ytype == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00004726 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004727 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00004728 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004729 if ((x == NULL) || (y == NULL))
4730 return(-2);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004731 if (ytype == XML_SCHEMAS_DURATION)
Daniel Veillard070803b2002-05-03 07:29:38 +00004732 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004733 return(-2);
4734 case XML_SCHEMAS_TIME:
4735 case XML_SCHEMAS_GDAY:
4736 case XML_SCHEMAS_GMONTH:
4737 case XML_SCHEMAS_GMONTHDAY:
4738 case XML_SCHEMAS_GYEAR:
4739 case XML_SCHEMAS_GYEARMONTH:
4740 case XML_SCHEMAS_DATE:
4741 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004742 if ((x == NULL) || (y == NULL))
4743 return(-2);
4744 if ((ytype == XML_SCHEMAS_DATETIME) ||
4745 (ytype == XML_SCHEMAS_TIME) ||
4746 (ytype == XML_SCHEMAS_GDAY) ||
4747 (ytype == XML_SCHEMAS_GMONTH) ||
4748 (ytype == XML_SCHEMAS_GMONTHDAY) ||
4749 (ytype == XML_SCHEMAS_GYEAR) ||
4750 (ytype == XML_SCHEMAS_DATE) ||
4751 (ytype == XML_SCHEMAS_GYEARMONTH))
Daniel Veillard5a872412002-05-22 06:40:27 +00004752 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004753 return (-2);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004754 /*
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +00004755 * Note that we will support comparison of string types against
4756 * anySimpleType as well.
4757 */
4758 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004759 case XML_SCHEMAS_STRING:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004760 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00004761 case XML_SCHEMAS_TOKEN:
4762 case XML_SCHEMAS_LANGUAGE:
4763 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00004764 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00004765 case XML_SCHEMAS_NCNAME:
4766 case XML_SCHEMAS_ID:
4767 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00004768 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00004769 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004770 {
4771 const xmlChar *xv, *yv;
4772
4773 if (x == NULL)
4774 xv = xvalue;
4775 else
4776 xv = x->value.str;
4777 if (y == NULL)
4778 yv = yvalue;
4779 else
4780 yv = y->value.str;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004781 /*
4782 * TODO: Compare those against QName.
4783 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004784 if (ytype == XML_SCHEMAS_QNAME) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004785 TODO
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004786 if (y == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004787 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004788 return (-2);
4789 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004790 if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
4791 (ytype == XML_SCHEMAS_STRING) ||
4792 (ytype == XML_SCHEMAS_NORMSTRING) ||
4793 (ytype == XML_SCHEMAS_TOKEN) ||
4794 (ytype == XML_SCHEMAS_LANGUAGE) ||
4795 (ytype == XML_SCHEMAS_NMTOKEN) ||
4796 (ytype == XML_SCHEMAS_NAME) ||
4797 (ytype == XML_SCHEMAS_NCNAME) ||
4798 (ytype == XML_SCHEMAS_ID) ||
4799 (ytype == XML_SCHEMAS_IDREF) ||
4800 (ytype == XML_SCHEMAS_ENTITY) ||
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004801 (ytype == XML_SCHEMAS_ANYURI)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004802
4803 if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4804
4805 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4806 /* TODO: What about x < y or x > y. */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004807 if (xmlStrEqual(xv, yv))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004808 return (0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004809 else
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004810 return (2);
4811 } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004812 return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004813 else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004814 return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004815
4816 } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
4817
4818 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004819 return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004820 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004821 return (xmlSchemaCompareReplacedStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004822 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004823 return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004824
4825 } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
4826
4827 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004828 return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004829 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004830 return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004831 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004832 return (xmlSchemaCompareNormStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004833 } else
4834 return (-2);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004835
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004836 }
Daniel Veillardc4c21552003-03-29 10:53:38 +00004837 return (-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004838 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004839 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004840 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004841 if ((x == NULL) || (y == NULL))
4842 return(-2);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004843 if ((ytype == XML_SCHEMAS_QNAME) ||
4844 (ytype == XML_SCHEMAS_NOTATION)) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004845 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
4846 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
4847 return(0);
4848 return(2);
4849 }
4850 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004851 case XML_SCHEMAS_FLOAT:
4852 case XML_SCHEMAS_DOUBLE:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004853 if ((x == NULL) || (y == NULL))
4854 return(-2);
4855 if ((ytype == XML_SCHEMAS_FLOAT) ||
4856 (ytype == XML_SCHEMAS_DOUBLE))
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004857 return (xmlSchemaCompareFloats(x, y));
4858 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004859 case XML_SCHEMAS_BOOLEAN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004860 if ((x == NULL) || (y == NULL))
4861 return(-2);
4862 if (ytype == XML_SCHEMAS_BOOLEAN) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004863 if (x->value.b == y->value.b)
4864 return(0);
4865 if (x->value.b == 0)
4866 return(-1);
4867 return(1);
4868 }
4869 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00004870 case XML_SCHEMAS_HEXBINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004871 if ((x == NULL) || (y == NULL))
4872 return(-2);
4873 if (ytype == XML_SCHEMAS_HEXBINARY) {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00004874 if (x->value.hex.total == y->value.hex.total) {
4875 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
4876 if (ret > 0)
4877 return(1);
4878 else if (ret == 0)
4879 return(0);
4880 }
4881 else if (x->value.hex.total > y->value.hex.total)
4882 return(1);
4883
4884 return(-1);
4885 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00004886 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004887 case XML_SCHEMAS_BASE64BINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004888 if ((x == NULL) || (y == NULL))
4889 return(-2);
4890 if (ytype == XML_SCHEMAS_BASE64BINARY) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004891 if (x->value.base64.total == y->value.base64.total) {
4892 int ret = xmlStrcmp(x->value.base64.str,
4893 y->value.base64.str);
4894 if (ret > 0)
4895 return(1);
4896 else if (ret == 0)
4897 return(0);
Daniel Veillardd369b132005-07-14 15:54:44 +00004898 else
4899 return(-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004900 }
4901 else if (x->value.base64.total > y->value.base64.total)
4902 return(1);
4903 else
4904 return(-1);
4905 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004906 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004907 case XML_SCHEMAS_IDREFS:
4908 case XML_SCHEMAS_ENTITIES:
4909 case XML_SCHEMAS_NMTOKENS:
4910 TODO
4911 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004912 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004913 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00004914}
4915
4916/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004917 * xmlSchemaCompareValues:
4918 * @x: a first value
4919 * @y: a second value
4920 *
4921 * Compare 2 values
4922 *
4923 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4924 * case of error
4925 */
4926int
4927xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4928 xmlSchemaWhitespaceValueType xws, yws;
4929
Daniel Veillard5e094142005-02-18 19:36:12 +00004930 if ((x == NULL) || (y == NULL))
4931 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004932 if (x->type == XML_SCHEMAS_STRING)
4933 xws = XML_SCHEMA_WHITESPACE_PRESERVE;
4934 else if (x->type == XML_SCHEMAS_NORMSTRING)
4935 xws = XML_SCHEMA_WHITESPACE_REPLACE;
4936 else
4937 xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4938
4939 if (y->type == XML_SCHEMAS_STRING)
4940 yws = XML_SCHEMA_WHITESPACE_PRESERVE;
Gaurave79226c2013-11-28 22:50:57 +08004941 else if (y->type == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004942 yws = XML_SCHEMA_WHITESPACE_REPLACE;
4943 else
4944 yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4945
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004946 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4947 y, NULL, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004948}
4949
4950/**
4951 * xmlSchemaCompareValuesWhtsp:
4952 * @x: a first value
4953 * @xws: the whitespace value of x
4954 * @y: a second value
4955 * @yws: the whitespace value of y
4956 *
4957 * Compare 2 values
4958 *
4959 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4960 * case of error
4961 */
4962int
4963xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004964 xmlSchemaWhitespaceValueType xws,
4965 xmlSchemaValPtr y,
4966 xmlSchemaWhitespaceValueType yws)
4967{
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00004968 if ((x == NULL) || (y == NULL))
4969 return(-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004970 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4971 y, NULL, yws));
4972}
4973
4974/**
4975 * xmlSchemaCompareValuesWhtspExt:
4976 * @x: a first value
4977 * @xws: the whitespace value of x
4978 * @y: a second value
4979 * @yws: the whitespace value of y
4980 *
4981 * Compare 2 values
4982 *
4983 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4984 * case of error
4985 */
4986static int
4987xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
4988 xmlSchemaValPtr x,
4989 const xmlChar *xvalue,
4990 xmlSchemaWhitespaceValueType xws,
4991 xmlSchemaValType ytype,
4992 xmlSchemaValPtr y,
4993 const xmlChar *yvalue,
4994 xmlSchemaWhitespaceValueType yws)
4995{
4996 return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
4997 yvalue, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004998}
4999
5000/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00005001 * xmlSchemaNormLen:
5002 * @value: a string
5003 *
5004 * Computes the UTF8 length of the normalized value of the string
5005 *
5006 * Returns the length or -1 in case of error.
5007 */
5008static int
5009xmlSchemaNormLen(const xmlChar *value) {
5010 const xmlChar *utf;
5011 int ret = 0;
5012
5013 if (value == NULL)
5014 return(-1);
5015 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00005016 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00005017 while (*utf != 0) {
5018 if (utf[0] & 0x80) {
5019 if ((utf[1] & 0xc0) != 0x80)
5020 return(-1);
5021 if ((utf[0] & 0xe0) == 0xe0) {
5022 if ((utf[2] & 0xc0) != 0x80)
5023 return(-1);
5024 if ((utf[0] & 0xf0) == 0xf0) {
5025 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
5026 return(-1);
5027 utf += 4;
5028 } else {
5029 utf += 3;
5030 }
5031 } else {
5032 utf += 2;
5033 }
William M. Brack76e95df2003-10-18 16:20:14 +00005034 } else if (IS_BLANK_CH(*utf)) {
5035 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00005036 if (*utf == 0)
5037 break;
5038 } else {
5039 utf++;
5040 }
5041 ret++;
5042 }
5043 return(ret);
5044}
5045
Daniel Veillard6927b102004-10-27 17:29:04 +00005046/**
5047 * xmlSchemaGetFacetValueAsULong:
5048 * @facet: an schemas type facet
5049 *
5050 * Extract the value of a facet
5051 *
5052 * Returns the value as a long
5053 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005054unsigned long
5055xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
5056{
5057 /*
5058 * TODO: Check if this is a decimal.
5059 */
William M. Brack094dd862004-11-14 14:28:34 +00005060 if (facet == NULL)
5061 return 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005062 return ((unsigned long) facet->val->value.decimal.lo);
5063}
5064
Daniel Veillardc4c21552003-03-29 10:53:38 +00005065/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00005066 * xmlSchemaValidateListSimpleTypeFacet:
5067 * @facet: the facet to check
5068 * @value: the lexical repr of the value to validate
5069 * @actualLen: the number of list items
5070 * @expectedLen: the resulting expected number of list items
5071 *
5072 * Checks the value of a list simple type against a facet.
5073 *
5074 * Returns 0 if the value is valid, a positive error code
5075 * number otherwise and -1 in case of an internal error.
5076 */
5077int
5078xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
5079 const xmlChar *value,
5080 unsigned long actualLen,
5081 unsigned long *expectedLen)
5082{
Daniel Veillardce682bc2004-11-05 17:22:25 +00005083 if (facet == NULL)
5084 return(-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005085 /*
5086 * TODO: Check if this will work with large numbers.
5087 * (compare value.decimal.mi and value.decimal.hi as well?).
5088 */
5089 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5090 if (actualLen != facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005091 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005092 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005093 return (XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005094 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005095 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5096 if (actualLen < facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005097 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005098 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005099 return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
5100 }
5101 } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
5102 if (actualLen > facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005103 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005104 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005105 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5106 }
5107 } else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005108 /*
5109 * NOTE: That we can pass NULL as xmlSchemaValPtr to
Daniel Veillard01fa6152004-06-29 17:04:39 +00005110 * xmlSchemaValidateFacet, since the remaining facet types
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005111 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
Daniel Veillard01fa6152004-06-29 17:04:39 +00005112 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005113 return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
Daniel Veillard01fa6152004-06-29 17:04:39 +00005114 return (0);
5115}
5116
5117/**
Daniel Veillard6927b102004-10-27 17:29:04 +00005118 * xmlSchemaValidateLengthFacet:
Daniel Veillardc0826a72004-08-10 14:17:33 +00005119 * @type: the built-in type
5120 * @facet: the facet to check
5121 * @value: the lexical repr. of the value to be validated
5122 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005123 * @ws: the whitespace type of the value
5124 * @length: the actual length of the value
5125 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005126 * Checka a value against a "length", "minLength" and "maxLength"
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005127 * facet; sets @length to the computed length of @value.
5128 *
5129 * Returns 0 if the value is valid, a positive error code
5130 * otherwise and -1 in case of an internal or API error.
5131 */
5132static int
5133xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
Nico Weberaae48e62012-02-29 09:44:35 +08005134 xmlSchemaValType valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005135 const xmlChar *value,
Nico Weberaae48e62012-02-29 09:44:35 +08005136 xmlSchemaValPtr val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005137 unsigned long *length,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005138 xmlSchemaWhitespaceValueType ws)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005139{
5140 unsigned int len = 0;
5141
5142 if ((length == NULL) || (facet == NULL))
5143 return (-1);
5144 *length = 0;
5145 if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
5146 (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
5147 (facet->type != XML_SCHEMA_FACET_MINLENGTH))
5148 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005149
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005150 /*
5151 * TODO: length, maxLength and minLength must be of type
5152 * nonNegativeInteger only. Check if decimal is used somehow.
5153 */
5154 if ((facet->val == NULL) ||
5155 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5156 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5157 (facet->val->value.decimal.frac != 0)) {
5158 return(-1);
5159 }
5160 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
5161 len = val->value.hex.total;
5162 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5163 len = val->value.base64.total;
5164 else {
5165 switch (valType) {
5166 case XML_SCHEMAS_STRING:
5167 case XML_SCHEMAS_NORMSTRING:
5168 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5169 /*
5170 * This is to ensure API compatibility with the old
5171 * xmlSchemaValidateLengthFacet(). Anyway, this was and
5172 * is not the correct handling.
5173 * TODO: Get rid of this case somehow.
5174 */
5175 if (valType == XML_SCHEMAS_STRING)
5176 len = xmlUTF8Strlen(value);
5177 else
5178 len = xmlSchemaNormLen(value);
5179 } else if (value != NULL) {
5180 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5181 len = xmlSchemaNormLen(value);
5182 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005183 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005184 * Should be OK for "preserve" as well.
5185 */
5186 len = xmlUTF8Strlen(value);
5187 }
5188 break;
5189 case XML_SCHEMAS_IDREF:
5190 case XML_SCHEMAS_TOKEN:
5191 case XML_SCHEMAS_LANGUAGE:
5192 case XML_SCHEMAS_NMTOKEN:
5193 case XML_SCHEMAS_NAME:
5194 case XML_SCHEMAS_NCNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005195 case XML_SCHEMAS_ID:
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005196 /*
5197 * FIXME: What exactly to do with anyURI?
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005198 */
5199 case XML_SCHEMAS_ANYURI:
5200 if (value != NULL)
5201 len = xmlSchemaNormLen(value);
5202 break;
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005203 case XML_SCHEMAS_QNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005204 case XML_SCHEMAS_NOTATION:
5205 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005206 * For QName and NOTATION, those facets are
5207 * deprecated and should be ignored.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005208 */
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005209 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005210 default:
5211 TODO
5212 }
5213 }
5214 *length = (unsigned long) len;
5215 /*
5216 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
5217 */
5218 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5219 if (len != facet->val->value.decimal.lo)
5220 return(XML_SCHEMAV_CVC_LENGTH_VALID);
5221 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5222 if (len < facet->val->value.decimal.lo)
5223 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
5224 } else {
5225 if (len > facet->val->value.decimal.lo)
5226 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5227 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005228
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005229 return (0);
5230}
5231
5232/**
5233 * xmlSchemaValidateLengthFacet:
5234 * @type: the built-in type
5235 * @facet: the facet to check
5236 * @value: the lexical repr. of the value to be validated
5237 * @val: the precomputed value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005238 * @length: the actual length of the value
5239 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005240 * Checka a value against a "length", "minLength" and "maxLength"
Daniel Veillardc0826a72004-08-10 14:17:33 +00005241 * facet; sets @length to the computed length of @value.
5242 *
5243 * Returns 0 if the value is valid, a positive error code
5244 * otherwise and -1 in case of an internal or API error.
5245 */
5246int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005247xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005248 xmlSchemaFacetPtr facet,
5249 const xmlChar *value,
5250 xmlSchemaValPtr val,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005251 unsigned long *length)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005252{
Daniel Veillardcc5e2332005-03-16 21:55:35 +00005253 if (type == NULL)
5254 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005255 return (xmlSchemaValidateLengthFacetInternal(facet,
5256 type->builtInType, value, val, length,
5257 XML_SCHEMA_WHITESPACE_UNKNOWN));
5258}
Daniel Veillardc0826a72004-08-10 14:17:33 +00005259
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005260/**
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005261 * xmlSchemaValidateLengthFacetWhtsp:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005262 * @facet: the facet to check
5263 * @valType: the built-in type
5264 * @value: the lexical repr. of the value to be validated
5265 * @val: the precomputed value
5266 * @ws: the whitespace type of the value
5267 * @length: the actual length of the value
5268 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005269 * Checka a value against a "length", "minLength" and "maxLength"
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005270 * facet; sets @length to the computed length of @value.
5271 *
5272 * Returns 0 if the value is valid, a positive error code
5273 * otherwise and -1 in case of an internal or API error.
5274 */
5275int
5276xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
5277 xmlSchemaValType valType,
5278 const xmlChar *value,
5279 xmlSchemaValPtr val,
5280 unsigned long *length,
5281 xmlSchemaWhitespaceValueType ws)
5282{
5283 return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
5284 length, ws));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005285}
5286
5287/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005288 * xmlSchemaValidateFacetInternal:
Daniel Veillard4255d502002-04-16 15:50:10 +00005289 * @facet: the facet to check
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005290 * @fws: the whitespace type of the facet's value
5291 * @valType: the built-in type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005292 * @value: the lexical repr of the value to validate
5293 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005294 * @ws: the whitespace type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005295 *
5296 * Check a value against a facet condition
5297 *
5298 * Returns 0 if the element is schemas valid, a positive error code
5299 * number otherwise and -1 in case of internal or API error.
5300 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005301static int
5302xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
5303 xmlSchemaWhitespaceValueType fws,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005304 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005305 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005306 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005307 xmlSchemaWhitespaceValueType ws)
Daniel Veillard4255d502002-04-16 15:50:10 +00005308{
5309 int ret;
5310
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005311 if (facet == NULL)
5312 return(-1);
5313
Daniel Veillard4255d502002-04-16 15:50:10 +00005314 switch (facet->type) {
5315 case XML_SCHEMA_FACET_PATTERN:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005316 /*
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005317 * NOTE that for patterns, the @value needs to be the normalized
5318 * value, *not* the lexical initial value or the canonical value.
5319 */
5320 if (value == NULL)
5321 return(-1);
Audric Schiltknechtcad102b2016-04-15 22:41:24 +08005322 /*
5323 * If string-derived type, regexp must be tested on the value space of
5324 * the datatype.
5325 * See https://www.w3.org/TR/xmlschema-2/#rf-pattern
5326 */
5327 const int stringType = val && ((val->type >= XML_SCHEMAS_STRING && val->type <= XML_SCHEMAS_NORMSTRING)
5328 || (val->type >= XML_SCHEMAS_TOKEN && val->type <= XML_SCHEMAS_NCNAME));
5329 ret = xmlRegexpExec(facet->regexp,
5330 (stringType && val->value.str) ? val->value.str : value);
Daniel Veillard4255d502002-04-16 15:50:10 +00005331 if (ret == 1)
5332 return(0);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005333 if (ret == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005334 return(XML_SCHEMAV_CVC_PATTERN_VALID);
Daniel Veillard4255d502002-04-16 15:50:10 +00005335 return(ret);
5336 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
5337 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005338 if (ret == -2)
Daniel Veillard4255d502002-04-16 15:50:10 +00005339 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005340 if (ret == -1)
5341 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005342 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005343 case XML_SCHEMA_FACET_MAXINCLUSIVE:
5344 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005345 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005346 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005347 if ((ret == -1) || (ret == 0))
5348 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005349 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005350 case XML_SCHEMA_FACET_MINEXCLUSIVE:
5351 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005352 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005353 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005354 if (ret == 1)
5355 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005356 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005357 case XML_SCHEMA_FACET_MININCLUSIVE:
5358 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005359 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005360 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005361 if ((ret == 1) || (ret == 0))
5362 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005363 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
Daniel Veillard8651f532002-04-17 09:06:27 +00005364 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005365 /* TODO whitespaces */
Daniel Veillard01fa6152004-06-29 17:04:39 +00005366 /*
5367 * NOTE: Whitespace should be handled to normalize
5368 * the value to be validated against a the facets;
5369 * not to normalize the value in-between.
5370 */
Daniel Veillard8651f532002-04-17 09:06:27 +00005371 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00005372 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005373 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5374 /*
5375 * This is to ensure API compatibility with the old
5376 * xmlSchemaValidateFacet().
5377 * TODO: Get rid of this case.
5378 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005379 if ((facet->value != NULL) &&
5380 (xmlStrEqual(facet->value, value)))
5381 return(0);
5382 } else {
5383 ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
5384 facet->val, facet->value, fws, valType, val,
5385 value, ws);
5386 if (ret == -2)
5387 return(-1);
5388 if (ret == 0)
5389 return(0);
5390 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005391 return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005392 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005393 /*
5394 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5395 * then any {value} is facet-valid."
5396 */
5397 if ((valType == XML_SCHEMAS_QNAME) ||
5398 (valType == XML_SCHEMAS_NOTATION))
5399 return (0);
5400 /* No break on purpose. */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005401 case XML_SCHEMA_FACET_MAXLENGTH:
5402 case XML_SCHEMA_FACET_MINLENGTH: {
5403 unsigned int len = 0;
5404
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005405 if ((valType == XML_SCHEMAS_QNAME) ||
5406 (valType == XML_SCHEMAS_NOTATION))
5407 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005408 /*
5409 * TODO: length, maxLength and minLength must be of type
5410 * nonNegativeInteger only. Check if decimal is used somehow.
5411 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005412 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005413 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5414 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005415 (facet->val->value.decimal.frac != 0)) {
5416 return(-1);
5417 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005418 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005419 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00005420 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5421 len = val->value.base64.total;
5422 else {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005423 switch (valType) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005424 case XML_SCHEMAS_STRING:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005425 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005426 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5427 /*
5428 * This is to ensure API compatibility with the old
5429 * xmlSchemaValidateFacet(). Anyway, this was and
5430 * is not the correct handling.
5431 * TODO: Get rid of this case somehow.
5432 */
5433 if (valType == XML_SCHEMAS_STRING)
5434 len = xmlUTF8Strlen(value);
5435 else
5436 len = xmlSchemaNormLen(value);
5437 } else if (value != NULL) {
5438 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5439 len = xmlSchemaNormLen(value);
5440 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005441 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005442 * Should be OK for "preserve" as well.
5443 */
5444 len = xmlUTF8Strlen(value);
5445 }
5446 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005447 case XML_SCHEMAS_IDREF:
Daniel Veillard560c2a42003-07-06 21:13:49 +00005448 case XML_SCHEMAS_TOKEN:
5449 case XML_SCHEMAS_LANGUAGE:
5450 case XML_SCHEMAS_NMTOKEN:
5451 case XML_SCHEMAS_NAME:
5452 case XML_SCHEMAS_NCNAME:
5453 case XML_SCHEMAS_ID:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005454 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005455 if (value != NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005456 len = xmlSchemaNormLen(value);
5457 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00005458 default:
5459 TODO
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005460 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005461 }
5462 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005463 if (len != facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005464 return(XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005465 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005466 if (len < facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005467 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005468 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005469 if (len > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005470 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005471 }
5472 break;
5473 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005474 case XML_SCHEMA_FACET_TOTALDIGITS:
5475 case XML_SCHEMA_FACET_FRACTIONDIGITS:
5476
5477 if ((facet->val == NULL) ||
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00005478 ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
Daniel Veillard560c2a42003-07-06 21:13:49 +00005479 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5480 (facet->val->value.decimal.frac != 0)) {
5481 return(-1);
5482 }
5483 if ((val == NULL) ||
5484 ((val->type != XML_SCHEMAS_DECIMAL) &&
5485 (val->type != XML_SCHEMAS_INTEGER) &&
5486 (val->type != XML_SCHEMAS_NPINTEGER) &&
5487 (val->type != XML_SCHEMAS_NINTEGER) &&
5488 (val->type != XML_SCHEMAS_NNINTEGER) &&
5489 (val->type != XML_SCHEMAS_PINTEGER) &&
5490 (val->type != XML_SCHEMAS_INT) &&
5491 (val->type != XML_SCHEMAS_UINT) &&
5492 (val->type != XML_SCHEMAS_LONG) &&
5493 (val->type != XML_SCHEMAS_ULONG) &&
5494 (val->type != XML_SCHEMAS_SHORT) &&
5495 (val->type != XML_SCHEMAS_USHORT) &&
5496 (val->type != XML_SCHEMAS_BYTE) &&
5497 (val->type != XML_SCHEMAS_UBYTE))) {
5498 return(-1);
5499 }
5500 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
5501 if (val->value.decimal.total > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005502 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005503
5504 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
5505 if (val->value.decimal.frac > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005506 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005507 }
5508 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00005509 default:
5510 TODO
5511 }
5512 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00005513
Daniel Veillard4255d502002-04-16 15:50:10 +00005514}
5515
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005516/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005517 * xmlSchemaValidateFacet:
5518 * @base: the base type
5519 * @facet: the facet to check
5520 * @value: the lexical repr of the value to validate
5521 * @val: the precomputed value
5522 *
5523 * Check a value against a facet condition
5524 *
5525 * Returns 0 if the element is schemas valid, a positive error code
5526 * number otherwise and -1 in case of internal or API error.
5527 */
5528int
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005529xmlSchemaValidateFacet(xmlSchemaTypePtr base,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005530 xmlSchemaFacetPtr facet,
5531 const xmlChar *value,
5532 xmlSchemaValPtr val)
5533{
5534 /*
5535 * This tries to ensure API compatibility regarding the old
5536 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5537 * xmlSchemaValidateFacetWhtsp().
5538 */
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005539 if (val != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005540 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005541 XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005542 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005543 else if (base != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005544 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005545 XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005546 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005547 return(-1);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005548}
5549
5550/**
5551 * xmlSchemaValidateFacetWhtsp:
5552 * @facet: the facet to check
5553 * @fws: the whitespace type of the facet's value
5554 * @valType: the built-in type of the value
5555 * @value: the lexical (or normalized for pattern) repr of the value to validate
5556 * @val: the precomputed value
5557 * @ws: the whitespace type of the value
5558 *
5559 * Check a value against a facet condition. This takes value normalization
5560 * according to the specified whitespace types into account.
5561 * Note that @value needs to be the *normalized* value if the facet
5562 * is of type "pattern".
5563 *
5564 * Returns 0 if the element is schemas valid, a positive error code
5565 * number otherwise and -1 in case of internal or API error.
5566 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005567int
5568xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
5569 xmlSchemaWhitespaceValueType fws,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005570 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005571 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005572 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005573 xmlSchemaWhitespaceValueType ws)
5574{
5575 return(xmlSchemaValidateFacetInternal(facet, fws, valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005576 value, val, ws));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005577}
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005578
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005579#if 0
5580#ifndef DBL_DIG
5581#define DBL_DIG 16
5582#endif
5583#ifndef DBL_EPSILON
5584#define DBL_EPSILON 1E-9
5585#endif
5586
5587#define INTEGER_DIGITS DBL_DIG
5588#define FRACTION_DIGITS (DBL_DIG + 1)
5589#define EXPONENT_DIGITS (3 + 2)
5590
5591/**
5592 * xmlXPathFormatNumber:
5593 * @number: number to format
5594 * @buffer: output buffer
5595 * @buffersize: size of output buffer
5596 *
5597 * Convert the number into a string representation.
5598 */
5599static void
5600xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
5601{
5602 switch (xmlXPathIsInf(number)) {
5603 case 1:
5604 if (buffersize > (int)sizeof("INF"))
5605 snprintf(buffer, buffersize, "INF");
5606 break;
5607 case -1:
5608 if (buffersize > (int)sizeof("-INF"))
5609 snprintf(buffer, buffersize, "-INF");
5610 break;
5611 default:
5612 if (xmlXPathIsNaN(number)) {
5613 if (buffersize > (int)sizeof("NaN"))
5614 snprintf(buffer, buffersize, "NaN");
5615 } else if (number == 0) {
5616 snprintf(buffer, buffersize, "0.0E0");
5617 } else {
5618 /* 3 is sign, decimal point, and terminating zero */
5619 char work[DBL_DIG + EXPONENT_DIGITS + 3];
5620 int integer_place, fraction_place;
5621 char *ptr;
5622 char *after_fraction;
5623 double absolute_value;
5624 int size;
5625
5626 absolute_value = fabs(number);
5627
5628 /*
5629 * Result is in work, and after_fraction points
5630 * just past the fractional part.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005631 * Use scientific notation
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005632 */
5633 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
5634 fraction_place = DBL_DIG - 1;
5635 snprintf(work, sizeof(work),"%*.*e",
5636 integer_place, fraction_place, number);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005637 after_fraction = strchr(work + DBL_DIG, 'e');
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005638 /* Remove fractional trailing zeroes */
5639 ptr = after_fraction;
5640 while (*(--ptr) == '0')
5641 ;
5642 if (*ptr != '.')
5643 ptr++;
5644 while ((*ptr++ = *after_fraction++) != 0);
5645
5646 /* Finally copy result back to caller */
5647 size = strlen(work) + 1;
5648 if (size > buffersize) {
5649 work[buffersize - 1] = 0;
5650 size = buffersize;
5651 }
5652 memmove(buffer, work, size);
5653 }
5654 break;
5655 }
5656}
5657#endif
5658
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005659/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005660 * xmlSchemaGetCanonValue:
5661 * @val: the precomputed value
5662 * @retValue: the returned value
5663 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01005664 * Get the canonical lexical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005665 * The caller has to FREE the returned retValue.
5666 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005667 * WARNING: Some value types are not supported yet, resulting
5668 * in a @retValue of "???".
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005669 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005670 * TODO: XML Schema 1.0 does not define canonical representations
5671 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5672 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005673 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005674 *
5675 * Returns 0 if the value could be built, 1 if the value type is
5676 * not supported yet and -1 in case of API errors.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005677 */
5678int
Daniel Veillardb5839c32005-02-19 18:27:14 +00005679xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005680{
Daniel Veillardb5839c32005-02-19 18:27:14 +00005681 if ((retValue == NULL) || (val == NULL))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005682 return (-1);
5683 *retValue = NULL;
5684 switch (val->type) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005685 case XML_SCHEMAS_STRING:
5686 if (val->value.str == NULL)
5687 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5688 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005689 *retValue =
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005690 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5691 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005692 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005693 if (val->value.str == NULL)
5694 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5695 else {
5696 *retValue = xmlSchemaWhiteSpaceReplace(
5697 (const xmlChar *) val->value.str);
5698 if ((*retValue) == NULL)
5699 *retValue = BAD_CAST xmlStrdup(
5700 (const xmlChar *) val->value.str);
5701 }
5702 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005703 case XML_SCHEMAS_TOKEN:
5704 case XML_SCHEMAS_LANGUAGE:
5705 case XML_SCHEMAS_NMTOKEN:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005706 case XML_SCHEMAS_NAME:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005707 case XML_SCHEMAS_NCNAME:
5708 case XML_SCHEMAS_ID:
5709 case XML_SCHEMAS_IDREF:
5710 case XML_SCHEMAS_ENTITY:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005711 case XML_SCHEMAS_NOTATION: /* Unclear */
5712 case XML_SCHEMAS_ANYURI: /* Unclear */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005713 if (val->value.str == NULL)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005714 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005715 *retValue =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005716 BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
5717 if (*retValue == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005718 *retValue =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005719 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005720 break;
5721 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005722 /* TODO: Unclear in XML Schema 1.0. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005723 if (val->value.qname.uri == NULL) {
5724 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
5725 return (0);
5726 } else {
5727 *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
5728 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5729 BAD_CAST val->value.qname.uri);
5730 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5731 BAD_CAST "}");
5732 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5733 BAD_CAST val->value.qname.uri);
5734 }
5735 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005736 case XML_SCHEMAS_DECIMAL:
5737 /*
5738 * TODO: Lookout for a more simple implementation.
5739 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005740 if ((val->value.decimal.total == 1) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005741 (val->value.decimal.lo == 0)) {
5742 *retValue = xmlStrdup(BAD_CAST "0.0");
5743 } else {
5744 xmlSchemaValDecimal dec = val->value.decimal;
5745 int bufsize;
5746 char *buf = NULL, *offs;
5747
5748 /* Add room for the decimal point as well. */
5749 bufsize = dec.total + 2;
5750 if (dec.sign)
5751 bufsize++;
5752 /* Add room for leading/trailing zero. */
5753 if ((dec.frac == 0) || (dec.frac == dec.total))
5754 bufsize++;
5755 buf = xmlMalloc(bufsize);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005756 if (buf == NULL)
5757 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005758 offs = buf;
5759 if (dec.sign)
5760 *offs++ = '-';
5761 if (dec.frac == dec.total) {
5762 *offs++ = '0';
5763 *offs++ = '.';
5764 }
5765 if (dec.hi != 0)
5766 snprintf(offs, bufsize - (offs - buf),
5767 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5768 else if (dec.mi != 0)
5769 snprintf(offs, bufsize - (offs - buf),
5770 "%lu%lu", dec.mi, dec.lo);
5771 else
5772 snprintf(offs, bufsize - (offs - buf),
5773 "%lu", dec.lo);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005774
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005775 if (dec.frac != 0) {
5776 if (dec.frac != dec.total) {
5777 int diff = dec.total - dec.frac;
5778 /*
5779 * Insert the decimal point.
5780 */
5781 memmove(offs + diff + 1, offs + diff, dec.frac +1);
5782 offs[diff] = '.';
5783 } else {
5784 unsigned int i = 0;
5785 /*
5786 * Insert missing zeroes behind the decimal point.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005787 */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005788 while (*(offs + i) != 0)
5789 i++;
5790 if (i < dec.total) {
5791 memmove(offs + (dec.total - i), offs, i +1);
5792 memset(offs, '0', dec.total - i);
5793 }
5794 }
5795 } else {
5796 /*
5797 * Append decimal point and zero.
5798 */
5799 offs = buf + bufsize - 1;
5800 *offs-- = 0;
5801 *offs-- = '0';
5802 *offs-- = '.';
5803 }
5804 *retValue = BAD_CAST buf;
5805 }
5806 break;
5807 case XML_SCHEMAS_INTEGER:
5808 case XML_SCHEMAS_PINTEGER:
5809 case XML_SCHEMAS_NPINTEGER:
5810 case XML_SCHEMAS_NINTEGER:
5811 case XML_SCHEMAS_NNINTEGER:
5812 case XML_SCHEMAS_LONG:
5813 case XML_SCHEMAS_BYTE:
5814 case XML_SCHEMAS_SHORT:
5815 case XML_SCHEMAS_INT:
5816 case XML_SCHEMAS_UINT:
5817 case XML_SCHEMAS_ULONG:
5818 case XML_SCHEMAS_USHORT:
5819 case XML_SCHEMAS_UBYTE:
5820 if ((val->value.decimal.total == 1) &&
5821 (val->value.decimal.lo == 0))
5822 *retValue = xmlStrdup(BAD_CAST "0");
5823 else {
5824 xmlSchemaValDecimal dec = val->value.decimal;
5825 int bufsize = dec.total + 1;
5826
5827 /* Add room for the decimal point as well. */
5828 if (dec.sign)
5829 bufsize++;
5830 *retValue = xmlMalloc(bufsize);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005831 if (*retValue == NULL)
5832 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005833 if (dec.hi != 0) {
5834 if (dec.sign)
5835 snprintf((char *) *retValue, bufsize,
5836 "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5837 else
5838 snprintf((char *) *retValue, bufsize,
5839 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5840 } else if (dec.mi != 0) {
5841 if (dec.sign)
5842 snprintf((char *) *retValue, bufsize,
5843 "-%lu%lu", dec.mi, dec.lo);
5844 else
5845 snprintf((char *) *retValue, bufsize,
5846 "%lu%lu", dec.mi, dec.lo);
5847 } else {
5848 if (dec.sign)
5849 snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
5850 else
5851 snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
5852 }
5853 }
5854 break;
5855 case XML_SCHEMAS_BOOLEAN:
5856 if (val->value.b)
5857 *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
5858 else
5859 *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
5860 break;
5861 case XML_SCHEMAS_DURATION: {
5862 char buf[100];
5863 unsigned long year;
5864 unsigned long mon, day, hour = 0, min = 0;
5865 double sec = 0, left;
5866
5867 /* TODO: Unclear in XML Schema 1.0 */
5868 /*
5869 * TODO: This results in a normalized output of the value
5870 * - which is NOT conformant to the spec -
5871 * since the exact values of each property are not
5872 * recoverable. Think about extending the structure to
5873 * provide a field for every property.
5874 */
5875 year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
5876 mon = labs(val->value.dur.mon) - 12 * year;
5877
5878 day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
5879 left = fabs(val->value.dur.sec) - day * 86400;
5880 if (left > 0) {
5881 hour = (unsigned long) FQUOTIENT(left, 3600);
5882 left = left - (hour * 3600);
5883 if (left > 0) {
5884 min = (unsigned long) FQUOTIENT(left, 60);
5885 sec = left - (min * 60);
5886 }
5887 }
5888 if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
5889 snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5890 year, mon, day, hour, min, sec);
5891 else
5892 snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5893 year, mon, day, hour, min, sec);
5894 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5895 }
5896 break;
5897 case XML_SCHEMAS_GYEAR: {
5898 char buf[30];
5899 /* TODO: Unclear in XML Schema 1.0 */
5900 /* TODO: What to do with the timezone? */
5901 snprintf(buf, 30, "%04ld", val->value.date.year);
5902 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5903 }
5904 break;
5905 case XML_SCHEMAS_GMONTH: {
5906 /* TODO: Unclear in XML Schema 1.0 */
5907 /* TODO: What to do with the timezone? */
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005908 *retValue = xmlMalloc(6);
5909 if (*retValue == NULL)
5910 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005911 snprintf((char *) *retValue, 6, "--%02u",
5912 val->value.date.mon);
5913 }
5914 break;
5915 case XML_SCHEMAS_GDAY: {
5916 /* TODO: Unclear in XML Schema 1.0 */
5917 /* TODO: What to do with the timezone? */
5918 *retValue = xmlMalloc(6);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005919 if (*retValue == NULL)
5920 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005921 snprintf((char *) *retValue, 6, "---%02u",
5922 val->value.date.day);
5923 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005924 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005925 case XML_SCHEMAS_GMONTHDAY: {
5926 /* TODO: Unclear in XML Schema 1.0 */
5927 /* TODO: What to do with the timezone? */
5928 *retValue = xmlMalloc(8);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005929 if (*retValue == NULL)
5930 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005931 snprintf((char *) *retValue, 8, "--%02u-%02u",
5932 val->value.date.mon, val->value.date.day);
5933 }
5934 break;
5935 case XML_SCHEMAS_GYEARMONTH: {
5936 char buf[35];
5937 /* TODO: Unclear in XML Schema 1.0 */
5938 /* TODO: What to do with the timezone? */
5939 if (val->value.date.year < 0)
5940 snprintf(buf, 35, "-%04ld-%02u",
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005941 labs(val->value.date.year),
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005942 val->value.date.mon);
5943 else
5944 snprintf(buf, 35, "%04ld-%02u",
5945 val->value.date.year, val->value.date.mon);
5946 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5947 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005948 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005949 case XML_SCHEMAS_TIME:
5950 {
5951 char buf[30];
5952
5953 if (val->value.date.tz_flag) {
5954 xmlSchemaValPtr norm;
5955
5956 norm = xmlSchemaDateNormalize(val, 0);
5957 if (norm == NULL)
5958 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005959 /*
5960 * TODO: Check if "%.14g" is portable.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005961 */
5962 snprintf(buf, 30,
5963 "%02u:%02u:%02.14gZ",
5964 norm->value.date.hour,
5965 norm->value.date.min,
5966 norm->value.date.sec);
5967 xmlSchemaFreeValue(norm);
5968 } else {
5969 snprintf(buf, 30,
5970 "%02u:%02u:%02.14g",
5971 val->value.date.hour,
5972 val->value.date.min,
5973 val->value.date.sec);
5974 }
5975 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005976 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005977 break;
5978 case XML_SCHEMAS_DATE:
5979 {
5980 char buf[30];
5981
5982 if (val->value.date.tz_flag) {
5983 xmlSchemaValPtr norm;
5984
5985 norm = xmlSchemaDateNormalize(val, 0);
5986 if (norm == NULL)
5987 return (-1);
5988 /*
5989 * TODO: Append the canonical value of the
5990 * recoverable timezone and not "Z".
5991 */
5992 snprintf(buf, 30,
5993 "%04ld:%02u:%02uZ",
5994 norm->value.date.year, norm->value.date.mon,
5995 norm->value.date.day);
5996 xmlSchemaFreeValue(norm);
5997 } else {
5998 snprintf(buf, 30,
5999 "%04ld:%02u:%02u",
6000 val->value.date.year, val->value.date.mon,
6001 val->value.date.day);
6002 }
6003 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006004 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006005 break;
6006 case XML_SCHEMAS_DATETIME:
6007 {
6008 char buf[50];
6009
6010 if (val->value.date.tz_flag) {
6011 xmlSchemaValPtr norm;
6012
6013 norm = xmlSchemaDateNormalize(val, 0);
6014 if (norm == NULL)
6015 return (-1);
6016 /*
6017 * TODO: Check if "%.14g" is portable.
6018 */
6019 snprintf(buf, 50,
6020 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
6021 norm->value.date.year, norm->value.date.mon,
6022 norm->value.date.day, norm->value.date.hour,
6023 norm->value.date.min, norm->value.date.sec);
6024 xmlSchemaFreeValue(norm);
6025 } else {
6026 snprintf(buf, 50,
6027 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
6028 val->value.date.year, val->value.date.mon,
6029 val->value.date.day, val->value.date.hour,
6030 val->value.date.min, val->value.date.sec);
6031 }
6032 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6033 }
6034 break;
6035 case XML_SCHEMAS_HEXBINARY:
6036 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
6037 break;
6038 case XML_SCHEMAS_BASE64BINARY:
6039 /*
6040 * TODO: Is the following spec piece implemented?:
6041 * SPEC: "Note: For some values the canonical form defined
6042 * above does not conform to [RFC 2045], which requires breaking
6043 * with linefeeds at appropriate intervals."
6044 */
6045 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
6046 break;
6047 case XML_SCHEMAS_FLOAT: {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006048 char buf[30];
6049 /*
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006050 * |m| < 16777216, -149 <= e <= 104.
6051 * TODO: Handle, NaN, INF, -INF. The format is not
6052 * yet conformant. The c type float does not cover
6053 * the whole range.
6054 */
6055 snprintf(buf, 30, "%01.14e", val->value.f);
6056 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6057 }
6058 break;
6059 case XML_SCHEMAS_DOUBLE: {
6060 char buf[40];
6061 /* |m| < 9007199254740992, -1075 <= e <= 970 */
6062 /*
6063 * TODO: Handle, NaN, INF, -INF. The format is not
6064 * yet conformant. The c type float does not cover
6065 * the whole range.
6066 */
6067 snprintf(buf, 40, "%01.14e", val->value.d);
6068 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6069 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006070 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006071 default:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006072 *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006073 return (1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006074 }
Daniel Veillard26ab0e62006-10-11 12:32:51 +00006075 if (*retValue == NULL)
6076 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006077 return (0);
6078}
6079
Daniel Veillardbda59572005-04-01 17:15:17 +00006080/**
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006081 * xmlSchemaGetCanonValueWhtsp:
6082 * @val: the precomputed value
6083 * @retValue: the returned value
6084 * @ws: the whitespace type of the value
6085 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01006086 * Get the canonical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006087 * The caller has to free the returned @retValue.
6088 *
6089 * Returns 0 if the value could be built, 1 if the value type is
6090 * not supported yet and -1 in case of API errors.
6091 */
6092int
6093xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
6094 const xmlChar **retValue,
6095 xmlSchemaWhitespaceValueType ws)
6096{
6097 if ((retValue == NULL) || (val == NULL))
6098 return (-1);
6099 if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
6100 (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
6101 return (-1);
6102
6103 *retValue = NULL;
6104 switch (val->type) {
6105 case XML_SCHEMAS_STRING:
6106 if (val->value.str == NULL)
6107 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6108 else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6109 *retValue = xmlSchemaCollapseString(val->value.str);
6110 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
6111 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6112 if ((*retValue) == NULL)
6113 *retValue = BAD_CAST xmlStrdup(val->value.str);
6114 break;
6115 case XML_SCHEMAS_NORMSTRING:
6116 if (val->value.str == NULL)
6117 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6118 else {
6119 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6120 *retValue = xmlSchemaCollapseString(val->value.str);
6121 else
6122 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6123 if ((*retValue) == NULL)
6124 *retValue = BAD_CAST xmlStrdup(val->value.str);
6125 }
6126 break;
6127 default:
6128 return (xmlSchemaGetCanonValue(val, retValue));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006129 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006130 return (0);
6131}
6132
6133/**
Daniel Veillardbda59572005-04-01 17:15:17 +00006134 * xmlSchemaGetValType:
6135 * @val: a schemas value
6136 *
6137 * Accessor for the type of a value
6138 *
6139 * Returns the xmlSchemaValType of the value
6140 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006141xmlSchemaValType
6142xmlSchemaGetValType(xmlSchemaValPtr val)
6143{
Daniel Veillardbda59572005-04-01 17:15:17 +00006144 if (val == NULL)
6145 return(XML_SCHEMAS_UNKNOWN);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006146 return (val->type);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006147}
6148
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006149#define bottom_xmlschemastypes
6150#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00006151#endif /* LIBXML_SCHEMAS_ENABLED */