blob: c9674ba90132d727cb557cf8a1da575b4e32d7cb [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
Stéphane Michaut454e3972017-08-28 14:30:43 +020010/* To avoid EBCDIC trouble when parsing on zOS */
11#if defined(__MVS__)
12#pragma convert("ISO8859-1")
13#endif
14
Daniel Veillard4255d502002-04-16 15:50:10 +000015#define IN_LIBXML
16#include "libxml.h"
17
18#ifdef LIBXML_SCHEMAS_ENABLED
19
20#include <string.h>
21#include <libxml/xmlmemory.h>
22#include <libxml/parser.h>
23#include <libxml/parserInternals.h>
24#include <libxml/hash.h>
25#include <libxml/valid.h>
Daniel Veillard96a4b252003-02-06 08:22:32 +000026#include <libxml/xpath.h>
27#include <libxml/uri.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000028
29#include <libxml/xmlschemas.h>
30#include <libxml/schemasInternals.h>
31#include <libxml/xmlschemastypes.h>
32
Daniel Veillard070803b2002-05-03 07:29:38 +000033#ifdef HAVE_MATH_H
34#include <math.h>
35#endif
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000036#ifdef HAVE_FLOAT_H
37#include <float.h>
38#endif
Daniel Veillard070803b2002-05-03 07:29:38 +000039
Daniel Veillard4255d502002-04-16 15:50:10 +000040#define DEBUG
41
Daniel Veillardd0cf7f62004-11-09 16:17:02 +000042#ifndef LIBXML_XPATH_ENABLED
43extern double xmlXPathNAN;
44extern double xmlXPathPINF;
45extern double xmlXPathNINF;
46#endif
47
Daniel Veillardf8e3db02012-09-11 13:26:36 +080048#define TODO \
Daniel Veillard4255d502002-04-16 15:50:10 +000049 xmlGenericError(xmlGenericErrorContext, \
50 "Unimplemented block at %s:%d\n", \
51 __FILE__, __LINE__);
52
53#define XML_SCHEMAS_NAMESPACE_NAME \
54 (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
55
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000056#define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \
57 ((c) == 0xd))
58
59#define IS_WSP_SPACE_CH(c) ((c) == 0x20)
60
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000061#define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c)
62
Daniel Veillard070803b2002-05-03 07:29:38 +000063/* Date value */
64typedef struct _xmlSchemaValDate xmlSchemaValDate;
65typedef xmlSchemaValDate *xmlSchemaValDatePtr;
66struct _xmlSchemaValDate {
67 long year;
68 unsigned int mon :4; /* 1 <= mon <= 12 */
69 unsigned int day :5; /* 1 <= day <= 31 */
Patrick Monnerata1dca812016-04-11 20:03:19 +020070 unsigned int hour :5; /* 0 <= hour <= 24 */
Daniel Veillard070803b2002-05-03 07:29:38 +000071 unsigned int min :6; /* 0 <= min <= 59 */
72 double sec;
Daniel Veillarda77cf712003-05-09 23:09:55 +000073 unsigned int tz_flag :1; /* is tzo explicitely set? */
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +000074 signed int tzo :12; /* -1440 <= tzo <= 1440;
75 currently only -840 to +840 are needed */
Daniel Veillard070803b2002-05-03 07:29:38 +000076};
77
78/* Duration value */
79typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
80typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
81struct _xmlSchemaValDuration {
82 long mon; /* mon stores years also */
Daniel Veillardf8e3db02012-09-11 13:26:36 +080083 long day;
Daniel Veillard070803b2002-05-03 07:29:38 +000084 double sec; /* sec stores min and hour also */
85};
86
Daniel Veillard4255d502002-04-16 15:50:10 +000087typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
88typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
89struct _xmlSchemaValDecimal {
90 /* would use long long but not portable */
Daniel Veillarde637c4a2003-03-30 21:10:09 +000091 unsigned long lo;
92 unsigned long mi;
93 unsigned long hi;
Daniel Veillard4255d502002-04-16 15:50:10 +000094 unsigned int extra;
Daniel Veillard5a872412002-05-22 06:40:27 +000095 unsigned int sign:1;
William M. Brackc1939562003-08-05 15:52:22 +000096 unsigned int frac:7;
97 unsigned int total:8;
Daniel Veillard4255d502002-04-16 15:50:10 +000098};
99
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000100typedef struct _xmlSchemaValQName xmlSchemaValQName;
101typedef xmlSchemaValQName *xmlSchemaValQNamePtr;
102struct _xmlSchemaValQName {
103 xmlChar *name;
104 xmlChar *uri;
105};
106
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000107typedef struct _xmlSchemaValHex xmlSchemaValHex;
108typedef xmlSchemaValHex *xmlSchemaValHexPtr;
109struct _xmlSchemaValHex {
110 xmlChar *str;
111 unsigned int total;
112};
113
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000114typedef struct _xmlSchemaValBase64 xmlSchemaValBase64;
115typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr;
116struct _xmlSchemaValBase64 {
117 xmlChar *str;
118 unsigned int total;
119};
120
Daniel Veillard4255d502002-04-16 15:50:10 +0000121struct _xmlSchemaVal {
122 xmlSchemaValType type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000123 struct _xmlSchemaVal *next;
Daniel Veillard4255d502002-04-16 15:50:10 +0000124 union {
Daniel Veillard5a872412002-05-22 06:40:27 +0000125 xmlSchemaValDecimal decimal;
Daniel Veillard070803b2002-05-03 07:29:38 +0000126 xmlSchemaValDate date;
127 xmlSchemaValDuration dur;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000128 xmlSchemaValQName qname;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000129 xmlSchemaValHex hex;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000130 xmlSchemaValBase64 base64;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000131 float f;
132 double d;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000133 int b;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000134 xmlChar *str;
Daniel Veillard4255d502002-04-16 15:50:10 +0000135 } value;
136};
137
138static int xmlSchemaTypesInitialized = 0;
139static xmlHashTablePtr xmlSchemaTypesBank = NULL;
140
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000141/*
142 * Basic types
143 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000144static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
145static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
146static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
147static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000148static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000149static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000150static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL;
151static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL;
152static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL;
153static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL;
154static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL;
155static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL;
156static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000157static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000158static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000159static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
Daniel Veillard560c2a42003-07-06 21:13:49 +0000160static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000161static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000162static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000163
164/*
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000165 * Derived types
166 */
167static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
168static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL;
169static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL;
170static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
171static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL;
172static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL;
173static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL;
174static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL;
175static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL;
176static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL;
177static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL;
178static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL;
179static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000180static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL;
181static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL;
182static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL;
183static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL;
184static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000185static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000186static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
187static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
188static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000189static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL;
190static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000191static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000192static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
193static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000194
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000195/************************************************************************
196 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800197 * Datatype error handlers *
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000198 * *
199 ************************************************************************/
200/**
201 * xmlSchemaTypeErrMemory:
202 * @extra: extra informations
203 *
204 * Handle an out of memory condition
205 */
206static void
207xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra)
208{
209 __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra);
210}
211
212/************************************************************************
213 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800214 * Base types support *
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000215 * *
216 ************************************************************************/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000217
218/**
219 * xmlSchemaNewValue:
220 * @type: the value type
221 *
222 * Allocate a new simple type value
223 *
224 * Returns a pointer to the new value or NULL in case of error
225 */
226static xmlSchemaValPtr
227xmlSchemaNewValue(xmlSchemaValType type) {
228 xmlSchemaValPtr value;
229
230 value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
231 if (value == NULL) {
232 return(NULL);
233 }
234 memset(value, 0, sizeof(xmlSchemaVal));
235 value->type = type;
236 return(value);
237}
238
239static xmlSchemaFacetPtr
240xmlSchemaNewMinLengthFacet(int value)
241{
242 xmlSchemaFacetPtr ret;
243
244 ret = xmlSchemaNewFacet();
Daniel Veillard14b56432006-03-09 18:41:40 +0000245 if (ret == NULL) {
246 return(NULL);
247 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000248 ret->type = XML_SCHEMA_FACET_MINLENGTH;
249 ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER);
Daniel Veillard717042d2013-07-22 14:28:20 +0800250 if (ret->val == NULL) {
251 xmlFree(ret);
252 return(NULL);
253 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000254 ret->val->value.decimal.lo = value;
255 return (ret);
256}
257
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000258/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000259 * xmlSchemaInitBasicType:
260 * @name: the type name
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000261 * @type: the value type associated
Daniel Veillard4255d502002-04-16 15:50:10 +0000262 *
Daniel Veillard01fa6152004-06-29 17:04:39 +0000263 * Initialize one primitive built-in type
Daniel Veillard4255d502002-04-16 15:50:10 +0000264 */
265static xmlSchemaTypePtr
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800266xmlSchemaInitBasicType(const char *name, xmlSchemaValType type,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000267 xmlSchemaTypePtr baseType) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000268 xmlSchemaTypePtr ret;
269
270 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
271 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000272 xmlSchemaTypeErrMemory(NULL, "could not initialize basic types");
Daniel Veillard4255d502002-04-16 15:50:10 +0000273 return(NULL);
274 }
275 memset(ret, 0, sizeof(xmlSchemaType));
Daniel Veillardbe9c6322003-11-22 20:37:51 +0000276 ret->name = (const xmlChar *)name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000277 ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME;
Daniel Veillard4255d502002-04-16 15:50:10 +0000278 ret->type = XML_SCHEMA_TYPE_BASIC;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800279 ret->baseType = baseType;
Daniel Veillard4255d502002-04-16 15:50:10 +0000280 ret->contentType = XML_SCHEMA_CONTENT_BASIC;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000281 /*
282 * Primitive types.
283 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800284 switch (type) {
285 case XML_SCHEMAS_STRING:
286 case XML_SCHEMAS_DECIMAL:
287 case XML_SCHEMAS_DATE:
288 case XML_SCHEMAS_DATETIME:
289 case XML_SCHEMAS_TIME:
290 case XML_SCHEMAS_GYEAR:
291 case XML_SCHEMAS_GYEARMONTH:
292 case XML_SCHEMAS_GMONTH:
293 case XML_SCHEMAS_GMONTHDAY:
294 case XML_SCHEMAS_GDAY:
295 case XML_SCHEMAS_DURATION:
296 case XML_SCHEMAS_FLOAT:
297 case XML_SCHEMAS_DOUBLE:
298 case XML_SCHEMAS_BOOLEAN:
299 case XML_SCHEMAS_ANYURI:
300 case XML_SCHEMAS_HEXBINARY:
301 case XML_SCHEMAS_BASE64BINARY:
302 case XML_SCHEMAS_QNAME:
303 case XML_SCHEMAS_NOTATION:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000304 ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000305 break;
William M. Brack96d2eff2004-06-30 11:48:47 +0000306 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000307 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000308 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000309 /*
310 * Set variety.
311 */
312 switch (type) {
313 case XML_SCHEMAS_ANYTYPE:
314 case XML_SCHEMAS_ANYSIMPLETYPE:
315 break;
316 case XML_SCHEMAS_IDREFS:
317 case XML_SCHEMAS_NMTOKENS:
318 case XML_SCHEMAS_ENTITIES:
319 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
320 ret->facets = xmlSchemaNewMinLengthFacet(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800321 ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000322 break;
323 default:
324 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
325 break;
326 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000327 xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
328 XML_SCHEMAS_NAMESPACE_NAME, ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000329 ret->builtInType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +0000330 return(ret);
331}
332
333/*
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000334* WARNING: Those type reside normally in xmlschemas.c but are
335* redefined here locally in oder of being able to use them for xs:anyType-
336* TODO: Remove those definition if we move the types to a header file.
337* TODO: Always keep those structs up-to-date with the originals.
338*/
339#define UNBOUNDED (1 << 30)
340
341typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
342typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
343struct _xmlSchemaTreeItem {
344 xmlSchemaTypeType type;
345 xmlSchemaAnnotPtr annot;
346 xmlSchemaTreeItemPtr next;
347 xmlSchemaTreeItemPtr children;
348};
349
350typedef struct _xmlSchemaParticle xmlSchemaParticle;
351typedef xmlSchemaParticle *xmlSchemaParticlePtr;
352struct _xmlSchemaParticle {
353 xmlSchemaTypeType type;
354 xmlSchemaAnnotPtr annot;
355 xmlSchemaTreeItemPtr next;
356 xmlSchemaTreeItemPtr children;
357 int minOccurs;
358 int maxOccurs;
359 xmlNodePtr node;
360};
361
362typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
363typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
364struct _xmlSchemaModelGroup {
365 xmlSchemaTypeType type;
366 xmlSchemaAnnotPtr annot;
367 xmlSchemaTreeItemPtr next;
368 xmlSchemaTreeItemPtr children;
369 xmlNodePtr node;
370};
371
372static xmlSchemaParticlePtr
373xmlSchemaAddParticle(void)
374{
375 xmlSchemaParticlePtr ret = NULL;
376
377 ret = (xmlSchemaParticlePtr)
378 xmlMalloc(sizeof(xmlSchemaParticle));
379 if (ret == NULL) {
380 xmlSchemaTypeErrMemory(NULL, "allocating particle component");
381 return (NULL);
382 }
383 memset(ret, 0, sizeof(xmlSchemaParticle));
384 ret->type = XML_SCHEMA_TYPE_PARTICLE;
385 ret->minOccurs = 1;
386 ret->maxOccurs = 1;
387 return (ret);
388}
389
390/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000391 * xmlSchemaInitTypes:
392 *
393 * Initialize the default XML Schemas type library
394 */
395void
Daniel Veillard6560a422003-03-27 21:25:38 +0000396xmlSchemaInitTypes(void)
397{
Daniel Veillard4255d502002-04-16 15:50:10 +0000398 if (xmlSchemaTypesInitialized != 0)
Daniel Veillard6560a422003-03-27 21:25:38 +0000399 return;
Daniel Veillard4255d502002-04-16 15:50:10 +0000400 xmlSchemaTypesBank = xmlHashCreate(40);
Daniel Veillard6560a422003-03-27 21:25:38 +0000401
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800402
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000403 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +0000404 * 3.4.7 Built-in Complex Type Definition
405 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000406 xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800407 XML_SCHEMAS_ANYTYPE,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000408 NULL);
409 xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef;
410 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000411 /*
412 * Init the content type.
413 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800414 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000415 {
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000416 xmlSchemaParticlePtr particle;
417 xmlSchemaModelGroupPtr sequence;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000418 xmlSchemaWildcardPtr wild;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000419 /* First particle. */
420 particle = xmlSchemaAddParticle();
421 if (particle == NULL)
422 return;
423 xmlSchemaTypeAnyTypeDef->subtypes = (xmlSchemaTypePtr) particle;
424 /* Sequence model group. */
425 sequence = (xmlSchemaModelGroupPtr)
426 xmlMalloc(sizeof(xmlSchemaModelGroup));
427 if (sequence == NULL) {
428 xmlSchemaTypeErrMemory(NULL, "allocating model group component");
429 return;
430 }
431 memset(sequence, 0, sizeof(xmlSchemaModelGroup));
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800432 sequence->type = XML_SCHEMA_TYPE_SEQUENCE;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000433 particle->children = (xmlSchemaTreeItemPtr) sequence;
434 /* Second particle. */
435 particle = xmlSchemaAddParticle();
436 if (particle == NULL)
437 return;
438 particle->minOccurs = 0;
439 particle->maxOccurs = UNBOUNDED;
440 sequence->children = (xmlSchemaTreeItemPtr) particle;
441 /* The wildcard */
Daniel Veillard01fa6152004-06-29 17:04:39 +0000442 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
443 if (wild == NULL) {
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000444 xmlSchemaTypeErrMemory(NULL, "allocating wildcard component");
445 return;
446 }
447 memset(wild, 0, sizeof(xmlSchemaWildcard));
448 wild->type = XML_SCHEMA_TYPE_ANY;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800449 wild->any = 1;
450 wild->processContents = XML_SCHEMAS_ANY_LAX;
451 particle->children = (xmlSchemaTreeItemPtr) wild;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000452 /*
453 * Create the attribute wildcard.
454 */
455 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
456 if (wild == NULL) {
457 xmlSchemaTypeErrMemory(NULL, "could not create an attribute "
458 "wildcard on anyType");
Daniel Veillard01fa6152004-06-29 17:04:39 +0000459 return;
460 }
461 memset(wild, 0, sizeof(xmlSchemaWildcard));
462 wild->any = 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800463 wild->processContents = XML_SCHEMAS_ANY_LAX;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000464 xmlSchemaTypeAnyTypeDef->attributeWildcard = wild;
465 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800466 xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
William M. Brack2f2a6632004-08-20 23:09:47 +0000467 XML_SCHEMAS_ANYSIMPLETYPE,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000468 xmlSchemaTypeAnyTypeDef);
469 /*
470 * primitive datatypes
471 */
472 xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
473 XML_SCHEMAS_STRING,
474 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000475 xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000476 XML_SCHEMAS_DECIMAL,
477 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000478 xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000479 XML_SCHEMAS_DATE,
480 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000481 xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000482 XML_SCHEMAS_DATETIME,
483 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000484 xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000485 XML_SCHEMAS_TIME,
486 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000487 xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000488 XML_SCHEMAS_GYEAR,
489 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000490 xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000491 XML_SCHEMAS_GYEARMONTH,
492 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000493 xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000494 XML_SCHEMAS_GMONTH,
495 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000496 xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000497 XML_SCHEMAS_GMONTHDAY,
498 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000499 xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000500 XML_SCHEMAS_GDAY,
501 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000502 xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000503 XML_SCHEMAS_DURATION,
504 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000505 xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000506 XML_SCHEMAS_FLOAT,
507 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000508 xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000509 XML_SCHEMAS_DOUBLE,
510 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000511 xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000512 XML_SCHEMAS_BOOLEAN,
513 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000514 xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000515 XML_SCHEMAS_ANYURI,
516 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard560c2a42003-07-06 21:13:49 +0000517 xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000518 XML_SCHEMAS_HEXBINARY,
519 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000520 xmlSchemaTypeBase64BinaryDef
Daniel Veillard01fa6152004-06-29 17:04:39 +0000521 = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY,
522 xmlSchemaTypeAnySimpleTypeDef);
523 xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
524 XML_SCHEMAS_NOTATION,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800525 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000526 xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
527 XML_SCHEMAS_QNAME,
528 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard4255d502002-04-16 15:50:10 +0000529
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000530 /*
531 * derived datatypes
532 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000533 xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000534 XML_SCHEMAS_INTEGER,
535 xmlSchemaTypeDecimalDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000536 xmlSchemaTypeNonPositiveIntegerDef =
537 xmlSchemaInitBasicType("nonPositiveInteger",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000538 XML_SCHEMAS_NPINTEGER,
539 xmlSchemaTypeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000540 xmlSchemaTypeNegativeIntegerDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000541 xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER,
542 xmlSchemaTypeNonPositiveIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000543 xmlSchemaTypeLongDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000544 xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG,
545 xmlSchemaTypeIntegerDef);
546 xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT,
547 xmlSchemaTypeLongDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000548 xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000549 XML_SCHEMAS_SHORT,
550 xmlSchemaTypeIntDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000551 xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000552 XML_SCHEMAS_BYTE,
553 xmlSchemaTypeShortDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000554 xmlSchemaTypeNonNegativeIntegerDef =
555 xmlSchemaInitBasicType("nonNegativeInteger",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000556 XML_SCHEMAS_NNINTEGER,
557 xmlSchemaTypeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000558 xmlSchemaTypeUnsignedLongDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000559 xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG,
560 xmlSchemaTypeNonNegativeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000561 xmlSchemaTypeUnsignedIntDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000562 xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT,
563 xmlSchemaTypeUnsignedLongDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000564 xmlSchemaTypeUnsignedShortDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000565 xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT,
566 xmlSchemaTypeUnsignedIntDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000567 xmlSchemaTypeUnsignedByteDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000568 xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE,
569 xmlSchemaTypeUnsignedShortDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000570 xmlSchemaTypePositiveIntegerDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000571 xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER,
572 xmlSchemaTypeNonNegativeIntegerDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000573 xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000574 XML_SCHEMAS_NORMSTRING,
575 xmlSchemaTypeStringDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000576 xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000577 XML_SCHEMAS_TOKEN,
578 xmlSchemaTypeNormStringDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000579 xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000580 XML_SCHEMAS_LANGUAGE,
581 xmlSchemaTypeTokenDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000582 xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000583 XML_SCHEMAS_NAME,
584 xmlSchemaTypeTokenDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000585 xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000586 XML_SCHEMAS_NMTOKEN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000587 xmlSchemaTypeTokenDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000588 xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
589 XML_SCHEMAS_NCNAME,
590 xmlSchemaTypeNameDef);
591 xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000592 xmlSchemaTypeNCNameDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000593 xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
594 XML_SCHEMAS_IDREF,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800595 xmlSchemaTypeNCNameDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000596 xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
597 XML_SCHEMAS_ENTITY,
598 xmlSchemaTypeNCNameDef);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000599 /*
600 * Derived list types.
601 */
602 /* ENTITIES */
Daniel Veillard01fa6152004-06-29 17:04:39 +0000603 xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
604 XML_SCHEMAS_ENTITIES,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000605 xmlSchemaTypeAnySimpleTypeDef);
606 xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef;
607 /* IDREFS */
608 xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
609 XML_SCHEMAS_IDREFS,
610 xmlSchemaTypeAnySimpleTypeDef);
611 xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef;
612
613 /* NMTOKENS */
614 xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
615 XML_SCHEMAS_NMTOKENS,
616 xmlSchemaTypeAnySimpleTypeDef);
617 xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef;
618
Daniel Veillard4255d502002-04-16 15:50:10 +0000619 xmlSchemaTypesInitialized = 1;
620}
621
622/**
623 * xmlSchemaCleanupTypes:
624 *
625 * Cleanup the default XML Schemas type library
626 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800627void
Daniel Veillard4255d502002-04-16 15:50:10 +0000628xmlSchemaCleanupTypes(void) {
629 if (xmlSchemaTypesInitialized == 0)
630 return;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000631 /*
632 * Free xs:anyType.
633 */
634 {
635 xmlSchemaParticlePtr particle;
636 /* Attribute wildcard. */
637 xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard);
638 /* Content type. */
639 particle = (xmlSchemaParticlePtr) xmlSchemaTypeAnyTypeDef->subtypes;
640 /* Wildcard. */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800641 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr)
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000642 particle->children->children->children);
643 xmlFree((xmlSchemaParticlePtr) particle->children->children);
644 /* Sequence model group. */
645 xmlFree((xmlSchemaModelGroupPtr) particle->children);
646 xmlFree((xmlSchemaParticlePtr) particle);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800647 xmlSchemaTypeAnyTypeDef->subtypes = NULL;
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000648 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000649 xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
650 xmlSchemaTypesInitialized = 0;
651}
652
653/**
Daniel Veillard6927b102004-10-27 17:29:04 +0000654 * xmlSchemaIsBuiltInTypeFacet:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000655 * @type: the built-in type
656 * @facetType: the facet type
657 *
658 * Evaluates if a specific facet can be
659 * used in conjunction with a type.
660 *
661 * Returns 1 if the facet can be used with the given built-in type,
662 * 0 otherwise and -1 in case the type is not a built-in type.
663 */
664int
665xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType)
666{
Daniel Veillardce682bc2004-11-05 17:22:25 +0000667 if (type == NULL)
668 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000669 if (type->type != XML_SCHEMA_TYPE_BASIC)
670 return (-1);
671 switch (type->builtInType) {
672 case XML_SCHEMAS_BOOLEAN:
673 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
674 (facetType == XML_SCHEMA_FACET_WHITESPACE))
675 return (1);
676 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800677 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000678 case XML_SCHEMAS_STRING:
679 case XML_SCHEMAS_NOTATION:
680 case XML_SCHEMAS_QNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800681 case XML_SCHEMAS_ANYURI:
682 case XML_SCHEMAS_BASE64BINARY:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000683 case XML_SCHEMAS_HEXBINARY:
684 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
685 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
686 (facetType == XML_SCHEMA_FACET_MAXLENGTH) ||
687 (facetType == XML_SCHEMA_FACET_PATTERN) ||
688 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
689 (facetType == XML_SCHEMA_FACET_WHITESPACE))
690 return (1);
691 else
692 return (0);
693 case XML_SCHEMAS_DECIMAL:
694 if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) ||
695 (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) ||
696 (facetType == XML_SCHEMA_FACET_PATTERN) ||
697 (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
698 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
699 (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
700 (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
701 (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
702 (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
703 return (1);
704 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800705 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000706 case XML_SCHEMAS_TIME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800707 case XML_SCHEMAS_GDAY:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000708 case XML_SCHEMAS_GMONTH:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800709 case XML_SCHEMAS_GMONTHDAY:
710 case XML_SCHEMAS_GYEAR:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000711 case XML_SCHEMAS_GYEARMONTH:
712 case XML_SCHEMAS_DATE:
713 case XML_SCHEMAS_DATETIME:
714 case XML_SCHEMAS_DURATION:
715 case XML_SCHEMAS_FLOAT:
716 case XML_SCHEMAS_DOUBLE:
717 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
718 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
719 (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
720 (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
721 (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
722 (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
723 (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
724 return (1);
725 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800726 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000727 default:
Daniel Veillardc7e3cc42004-09-28 12:33:52 +0000728 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000729 }
730 return (0);
731}
732
733/**
734 * xmlSchemaGetBuiltInType:
735 * @type: the type of the built in type
736 *
737 * Gives you the type struct for a built-in
738 * type by its type id.
739 *
740 * Returns the type if found, NULL otherwise.
741 */
742xmlSchemaTypePtr
743xmlSchemaGetBuiltInType(xmlSchemaValType type)
744{
745 if (xmlSchemaTypesInitialized == 0)
746 xmlSchemaInitTypes();
747 switch (type) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800748
Daniel Veillard01fa6152004-06-29 17:04:39 +0000749 case XML_SCHEMAS_ANYSIMPLETYPE:
750 return (xmlSchemaTypeAnySimpleTypeDef);
751 case XML_SCHEMAS_STRING:
752 return (xmlSchemaTypeStringDef);
753 case XML_SCHEMAS_NORMSTRING:
754 return (xmlSchemaTypeNormStringDef);
755 case XML_SCHEMAS_DECIMAL:
756 return (xmlSchemaTypeDecimalDef);
757 case XML_SCHEMAS_TIME:
758 return (xmlSchemaTypeTimeDef);
759 case XML_SCHEMAS_GDAY:
760 return (xmlSchemaTypeGDayDef);
761 case XML_SCHEMAS_GMONTH:
762 return (xmlSchemaTypeGMonthDef);
763 case XML_SCHEMAS_GMONTHDAY:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800764 return (xmlSchemaTypeGMonthDayDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000765 case XML_SCHEMAS_GYEAR:
766 return (xmlSchemaTypeGYearDef);
767 case XML_SCHEMAS_GYEARMONTH:
768 return (xmlSchemaTypeGYearMonthDef);
769 case XML_SCHEMAS_DATE:
770 return (xmlSchemaTypeDateDef);
771 case XML_SCHEMAS_DATETIME:
772 return (xmlSchemaTypeDatetimeDef);
773 case XML_SCHEMAS_DURATION:
774 return (xmlSchemaTypeDurationDef);
775 case XML_SCHEMAS_FLOAT:
776 return (xmlSchemaTypeFloatDef);
777 case XML_SCHEMAS_DOUBLE:
778 return (xmlSchemaTypeDoubleDef);
779 case XML_SCHEMAS_BOOLEAN:
780 return (xmlSchemaTypeBooleanDef);
781 case XML_SCHEMAS_TOKEN:
782 return (xmlSchemaTypeTokenDef);
783 case XML_SCHEMAS_LANGUAGE:
784 return (xmlSchemaTypeLanguageDef);
785 case XML_SCHEMAS_NMTOKEN:
786 return (xmlSchemaTypeNmtokenDef);
787 case XML_SCHEMAS_NMTOKENS:
788 return (xmlSchemaTypeNmtokensDef);
789 case XML_SCHEMAS_NAME:
790 return (xmlSchemaTypeNameDef);
791 case XML_SCHEMAS_QNAME:
792 return (xmlSchemaTypeQNameDef);
793 case XML_SCHEMAS_NCNAME:
794 return (xmlSchemaTypeNCNameDef);
795 case XML_SCHEMAS_ID:
796 return (xmlSchemaTypeIdDef);
797 case XML_SCHEMAS_IDREF:
798 return (xmlSchemaTypeIdrefDef);
799 case XML_SCHEMAS_IDREFS:
800 return (xmlSchemaTypeIdrefsDef);
801 case XML_SCHEMAS_ENTITY:
802 return (xmlSchemaTypeEntityDef);
803 case XML_SCHEMAS_ENTITIES:
804 return (xmlSchemaTypeEntitiesDef);
805 case XML_SCHEMAS_NOTATION:
806 return (xmlSchemaTypeNotationDef);
807 case XML_SCHEMAS_ANYURI:
808 return (xmlSchemaTypeAnyURIDef);
809 case XML_SCHEMAS_INTEGER:
810 return (xmlSchemaTypeIntegerDef);
811 case XML_SCHEMAS_NPINTEGER:
812 return (xmlSchemaTypeNonPositiveIntegerDef);
813 case XML_SCHEMAS_NINTEGER:
814 return (xmlSchemaTypeNegativeIntegerDef);
815 case XML_SCHEMAS_NNINTEGER:
816 return (xmlSchemaTypeNonNegativeIntegerDef);
817 case XML_SCHEMAS_PINTEGER:
818 return (xmlSchemaTypePositiveIntegerDef);
819 case XML_SCHEMAS_INT:
820 return (xmlSchemaTypeIntDef);
821 case XML_SCHEMAS_UINT:
822 return (xmlSchemaTypeUnsignedIntDef);
823 case XML_SCHEMAS_LONG:
824 return (xmlSchemaTypeLongDef);
825 case XML_SCHEMAS_ULONG:
826 return (xmlSchemaTypeUnsignedLongDef);
827 case XML_SCHEMAS_SHORT:
828 return (xmlSchemaTypeShortDef);
829 case XML_SCHEMAS_USHORT:
830 return (xmlSchemaTypeUnsignedShortDef);
831 case XML_SCHEMAS_BYTE:
832 return (xmlSchemaTypeByteDef);
833 case XML_SCHEMAS_UBYTE:
834 return (xmlSchemaTypeUnsignedByteDef);
835 case XML_SCHEMAS_HEXBINARY:
836 return (xmlSchemaTypeHexBinaryDef);
837 case XML_SCHEMAS_BASE64BINARY:
838 return (xmlSchemaTypeBase64BinaryDef);
839 case XML_SCHEMAS_ANYTYPE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800840 return (xmlSchemaTypeAnyTypeDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000841 default:
842 return (NULL);
843 }
844}
845
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000846/**
847 * xmlSchemaValueAppend:
848 * @prev: the value
849 * @cur: the value to be appended
850 *
851 * Appends a next sibling to a list of computed values.
852 *
853 * Returns 0 if succeeded and -1 on API errors.
854 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000855int
856xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000857
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000858 if ((prev == NULL) || (cur == NULL))
859 return (-1);
860 prev->next = cur;
861 return (0);
862}
863
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000864/**
865 * xmlSchemaValueGetNext:
866 * @cur: the value
867 *
868 * Accessor for the next sibling of a list of computed values.
869 *
870 * Returns the next value or NULL if there was none, or on
871 * API errors.
872 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000873xmlSchemaValPtr
874xmlSchemaValueGetNext(xmlSchemaValPtr cur) {
875
876 if (cur == NULL)
877 return (NULL);
878 return (cur->next);
879}
880
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000881/**
882 * xmlSchemaValueGetAsString:
883 * @val: the value
884 *
885 * Accessor for the string value of a computed value.
886 *
887 * Returns the string value or NULL if there was none, or on
888 * API errors.
889 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000890const xmlChar *
891xmlSchemaValueGetAsString(xmlSchemaValPtr val)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800892{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000893 if (val == NULL)
894 return (NULL);
895 switch (val->type) {
896 case XML_SCHEMAS_STRING:
897 case XML_SCHEMAS_NORMSTRING:
898 case XML_SCHEMAS_ANYSIMPLETYPE:
899 case XML_SCHEMAS_TOKEN:
900 case XML_SCHEMAS_LANGUAGE:
901 case XML_SCHEMAS_NMTOKEN:
902 case XML_SCHEMAS_NAME:
903 case XML_SCHEMAS_NCNAME:
904 case XML_SCHEMAS_ID:
905 case XML_SCHEMAS_IDREF:
906 case XML_SCHEMAS_ENTITY:
907 case XML_SCHEMAS_ANYURI:
908 return (BAD_CAST val->value.str);
909 default:
910 break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000911 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000912 return (NULL);
913}
914
Kasimier T. Buchcik7f6e0242005-06-15 13:36:10 +0000915/**
916 * xmlSchemaValueGetAsBoolean:
917 * @val: the value
918 *
919 * Accessor for the boolean value of a computed value.
920 *
921 * Returns 1 if true and 0 if false, or in case of an error. Hmm.
922 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000923int
924xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800925{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000926 if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN))
927 return (0);
928 return (val->value.b);
Daniel Veillard4255d502002-04-16 15:50:10 +0000929}
930
931/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000932 * xmlSchemaNewStringValue:
933 * @type: the value type
Daniel Veillardb5839c32005-02-19 18:27:14 +0000934 * @value: the value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000935 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800936 * Allocate a new simple type value. The type can be
937 * of XML_SCHEMAS_STRING.
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +0000938 * WARNING: This one is intended to be expanded for other
939 * string based types. We need this for anySimpleType as well.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000940 * The given value is consumed and freed with the struct.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000941 *
942 * Returns a pointer to the new value or NULL in case of error
943 */
944xmlSchemaValPtr
945xmlSchemaNewStringValue(xmlSchemaValType type,
946 const xmlChar *value)
947{
948 xmlSchemaValPtr val;
949
950 if (type != XML_SCHEMAS_STRING)
951 return(NULL);
952 val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
953 if (val == NULL) {
954 return(NULL);
955 }
956 memset(val, 0, sizeof(xmlSchemaVal));
957 val->type = type;
958 val->value.str = (xmlChar *) value;
959 return(val);
960}
961
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000962/**
963 * xmlSchemaNewNOTATIONValue:
Daniel Veillardb5839c32005-02-19 18:27:14 +0000964 * @name: the notation name
965 * @ns: the notation namespace name or NULL
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000966 *
967 * Allocate a new NOTATION value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000968 * The given values are consumed and freed with the struct.
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000969 *
970 * Returns a pointer to the new value or NULL in case of error
971 */
972xmlSchemaValPtr
973xmlSchemaNewNOTATIONValue(const xmlChar *name,
974 const xmlChar *ns)
975{
976 xmlSchemaValPtr val;
977
978 val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
979 if (val == NULL)
980 return (NULL);
981
William M. Brack12d37ab2005-02-21 13:54:07 +0000982 val->value.qname.name = (xmlChar *)name;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000983 if (ns != NULL)
William M. Brack12d37ab2005-02-21 13:54:07 +0000984 val->value.qname.uri = (xmlChar *)ns;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000985 return(val);
986}
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000987
988/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000989 * xmlSchemaNewQNameValue:
990 * @namespaceName: the namespace name
991 * @localName: the local name
992 *
993 * Allocate a new QName value.
994 * The given values are consumed and freed with the struct.
995 *
996 * Returns a pointer to the new value or NULL in case of an error.
997 */
998xmlSchemaValPtr
999xmlSchemaNewQNameValue(const xmlChar *namespaceName,
1000 const xmlChar *localName)
1001{
1002 xmlSchemaValPtr val;
1003
1004 val = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
1005 if (val == NULL)
1006 return (NULL);
1007
1008 val->value.qname.name = (xmlChar *) localName;
1009 val->value.qname.uri = (xmlChar *) namespaceName;
1010 return(val);
1011}
1012
1013/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001014 * xmlSchemaFreeValue:
1015 * @value: the value to free
1016 *
1017 * Cleanup the default XML Schemas type library
1018 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001019void
Daniel Veillard4255d502002-04-16 15:50:10 +00001020xmlSchemaFreeValue(xmlSchemaValPtr value) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001021 xmlSchemaValPtr prev;
1022
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001023 while (value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001024 switch (value->type) {
1025 case XML_SCHEMAS_STRING:
1026 case XML_SCHEMAS_NORMSTRING:
1027 case XML_SCHEMAS_TOKEN:
1028 case XML_SCHEMAS_LANGUAGE:
1029 case XML_SCHEMAS_NMTOKEN:
1030 case XML_SCHEMAS_NMTOKENS:
1031 case XML_SCHEMAS_NAME:
1032 case XML_SCHEMAS_NCNAME:
1033 case XML_SCHEMAS_ID:
1034 case XML_SCHEMAS_IDREF:
1035 case XML_SCHEMAS_IDREFS:
1036 case XML_SCHEMAS_ENTITY:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001037 case XML_SCHEMAS_ENTITIES:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001038 case XML_SCHEMAS_ANYURI:
1039 case XML_SCHEMAS_ANYSIMPLETYPE:
1040 if (value->value.str != NULL)
1041 xmlFree(value->value.str);
1042 break;
1043 case XML_SCHEMAS_NOTATION:
1044 case XML_SCHEMAS_QNAME:
1045 if (value->value.qname.uri != NULL)
1046 xmlFree(value->value.qname.uri);
1047 if (value->value.qname.name != NULL)
1048 xmlFree(value->value.qname.name);
1049 break;
1050 case XML_SCHEMAS_HEXBINARY:
1051 if (value->value.hex.str != NULL)
1052 xmlFree(value->value.hex.str);
1053 break;
1054 case XML_SCHEMAS_BASE64BINARY:
1055 if (value->value.base64.str != NULL)
1056 xmlFree(value->value.base64.str);
1057 break;
1058 default:
1059 break;
1060 }
1061 prev = value;
1062 value = value->next;
1063 xmlFree(prev);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001064 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001065}
1066
1067/**
1068 * xmlSchemaGetPredefinedType:
1069 * @name: the type name
1070 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
1071 *
1072 * Lookup a type in the default XML Schemas type library
1073 *
1074 * Returns the type if found, NULL otherwise
1075 */
1076xmlSchemaTypePtr
1077xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
1078 if (xmlSchemaTypesInitialized == 0)
1079 xmlSchemaInitTypes();
1080 if (name == NULL)
1081 return(NULL);
1082 return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
1083}
Daniel Veillard070803b2002-05-03 07:29:38 +00001084
Daniel Veillard01fa6152004-06-29 17:04:39 +00001085/**
1086 * xmlSchemaGetBuiltInListSimpleTypeItemType:
1087 * @type: the built-in simple type.
1088 *
Daniel Veillard6927b102004-10-27 17:29:04 +00001089 * Lookup function
1090 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00001091 * Returns the item type of @type as defined by the built-in datatype
1092 * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error.
Daniel Veillard01fa6152004-06-29 17:04:39 +00001093 */
1094xmlSchemaTypePtr
1095xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type)
1096{
Daniel Veillard42595322004-11-08 10:52:06 +00001097 if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC))
Daniel Veillard01fa6152004-06-29 17:04:39 +00001098 return (NULL);
1099 switch (type->builtInType) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001100 case XML_SCHEMAS_NMTOKENS:
Daniel Veillard01fa6152004-06-29 17:04:39 +00001101 return (xmlSchemaTypeNmtokenDef );
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001102 case XML_SCHEMAS_IDREFS:
Daniel Veillard01fa6152004-06-29 17:04:39 +00001103 return (xmlSchemaTypeIdrefDef);
1104 case XML_SCHEMAS_ENTITIES:
1105 return (xmlSchemaTypeEntityDef);
1106 default:
1107 return (NULL);
1108 }
1109}
1110
Daniel Veillard070803b2002-05-03 07:29:38 +00001111/****************************************************************
1112 * *
1113 * Convenience macros and functions *
1114 * *
1115 ****************************************************************/
1116
1117#define IS_TZO_CHAR(c) \
1118 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
1119
1120#define VALID_YEAR(yr) (yr != 0)
1121#define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
1122/* VALID_DAY should only be used when month is unknown */
1123#define VALID_DAY(day) ((day >= 1) && (day <= 31))
1124#define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
1125#define VALID_MIN(min) ((min >= 0) && (min <= 59))
1126#define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
Kasimier T. Buchcik690a6802005-05-12 13:16:01 +00001127#define VALID_TZO(tzo) ((tzo > -840) && (tzo < 840))
Daniel Veillard070803b2002-05-03 07:29:38 +00001128#define IS_LEAP(y) \
1129 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
1130
Daniel Veillardebe25d42004-03-25 09:35:49 +00001131static const unsigned int daysInMonth[12] =
Daniel Veillard070803b2002-05-03 07:29:38 +00001132 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Daniel Veillardebe25d42004-03-25 09:35:49 +00001133static const unsigned int daysInMonthLeap[12] =
Daniel Veillard070803b2002-05-03 07:29:38 +00001134 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1135
Daniel Veillard5a872412002-05-22 06:40:27 +00001136#define MAX_DAYINMONTH(yr,mon) \
1137 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
1138
Daniel Veillard070803b2002-05-03 07:29:38 +00001139#define VALID_MDAY(dt) \
1140 (IS_LEAP(dt->year) ? \
1141 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
1142 (dt->day <= daysInMonth[dt->mon - 1]))
1143
1144#define VALID_DATE(dt) \
1145 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
1146
Patrick Monnerata1dca812016-04-11 20:03:19 +02001147#define VALID_END_OF_DAY(dt) \
1148 ((dt)->hour == 24 && (dt)->min == 0 && (dt)->sec == 0)
1149
Daniel Veillard070803b2002-05-03 07:29:38 +00001150#define VALID_TIME(dt) \
Patrick Monnerata1dca812016-04-11 20:03:19 +02001151 (((VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
1152 VALID_SEC(dt->sec)) || VALID_END_OF_DAY(dt)) && \
1153 VALID_TZO(dt->tzo))
Daniel Veillard070803b2002-05-03 07:29:38 +00001154
1155#define VALID_DATETIME(dt) \
1156 (VALID_DATE(dt) && VALID_TIME(dt))
1157
1158#define SECS_PER_MIN (60)
1159#define SECS_PER_HOUR (60 * SECS_PER_MIN)
1160#define SECS_PER_DAY (24 * SECS_PER_HOUR)
1161
Daniel Veillard5a872412002-05-22 06:40:27 +00001162static const long dayInYearByMonth[12] =
1163 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1164static const long dayInLeapYearByMonth[12] =
1165 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
1166
1167#define DAY_IN_YEAR(day, month, year) \
1168 ((IS_LEAP(year) ? \
1169 dayInLeapYearByMonth[month - 1] : \
1170 dayInYearByMonth[month - 1]) + day)
1171
1172#ifdef DEBUG
1173#define DEBUG_DATE(dt) \
1174 xmlGenericError(xmlGenericErrorContext, \
1175 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
1176 dt->type,dt->value.date.year,dt->value.date.mon, \
1177 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
1178 dt->value.date.sec); \
1179 if (dt->value.date.tz_flag) \
1180 if (dt->value.date.tzo != 0) \
1181 xmlGenericError(xmlGenericErrorContext, \
1182 "%+05d\n",dt->value.date.tzo); \
1183 else \
1184 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
1185 else \
1186 xmlGenericError(xmlGenericErrorContext,"\n")
1187#else
1188#define DEBUG_DATE(dt)
1189#endif
1190
Daniel Veillard070803b2002-05-03 07:29:38 +00001191/**
1192 * _xmlSchemaParseGYear:
1193 * @dt: pointer to a date structure
1194 * @str: pointer to the string to analyze
1195 *
1196 * Parses a xs:gYear without time zone and fills in the appropriate
1197 * field of the @dt structure. @str is updated to point just after the
1198 * xs:gYear. It is supposed that @dt->year is big enough to contain
1199 * the year.
1200 *
1201 * Returns 0 or the error code
1202 */
1203static int
1204_xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
1205 const xmlChar *cur = *str, *firstChar;
1206 int isneg = 0, digcnt = 0;
1207
1208 if (((*cur < '0') || (*cur > '9')) &&
1209 (*cur != '-') && (*cur != '+'))
1210 return -1;
1211
1212 if (*cur == '-') {
1213 isneg = 1;
1214 cur++;
1215 }
1216
1217 firstChar = cur;
1218
1219 while ((*cur >= '0') && (*cur <= '9')) {
1220 dt->year = dt->year * 10 + (*cur - '0');
1221 cur++;
1222 digcnt++;
1223 }
1224
1225 /* year must be at least 4 digits (CCYY); over 4
1226 * digits cannot have a leading zero. */
1227 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
1228 return 1;
1229
1230 if (isneg)
1231 dt->year = - dt->year;
1232
1233 if (!VALID_YEAR(dt->year))
1234 return 2;
1235
1236 *str = cur;
1237 return 0;
1238}
1239
1240/**
1241 * PARSE_2_DIGITS:
1242 * @num: the integer to fill in
1243 * @cur: an #xmlChar *
1244 * @invalid: an integer
1245 *
1246 * Parses a 2-digits integer and updates @num with the value. @cur is
1247 * updated to point just after the integer.
1248 * In case of error, @invalid is set to %TRUE, values of @num and
1249 * @cur are undefined.
1250 */
1251#define PARSE_2_DIGITS(num, cur, invalid) \
1252 if ((cur[0] < '0') || (cur[0] > '9') || \
1253 (cur[1] < '0') || (cur[1] > '9')) \
1254 invalid = 1; \
1255 else \
1256 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
1257 cur += 2;
1258
1259/**
1260 * PARSE_FLOAT:
1261 * @num: the double to fill in
1262 * @cur: an #xmlChar *
1263 * @invalid: an integer
1264 *
1265 * Parses a float and updates @num with the value. @cur is
1266 * updated to point just after the float. The float must have a
1267 * 2-digits integer part and may or may not have a decimal part.
1268 * In case of error, @invalid is set to %TRUE, values of @num and
1269 * @cur are undefined.
1270 */
1271#define PARSE_FLOAT(num, cur, invalid) \
1272 PARSE_2_DIGITS(num, cur, invalid); \
1273 if (!invalid && (*cur == '.')) { \
1274 double mult = 1; \
1275 cur++; \
1276 if ((*cur < '0') || (*cur > '9')) \
1277 invalid = 1; \
1278 while ((*cur >= '0') && (*cur <= '9')) { \
1279 mult /= 10; \
1280 num += (*cur - '0') * mult; \
1281 cur++; \
1282 } \
1283 }
1284
1285/**
1286 * _xmlSchemaParseGMonth:
1287 * @dt: pointer to a date structure
1288 * @str: pointer to the string to analyze
1289 *
1290 * Parses a xs:gMonth without time zone and fills in the appropriate
1291 * field of the @dt structure. @str is updated to point just after the
1292 * xs:gMonth.
1293 *
1294 * Returns 0 or the error code
1295 */
1296static int
1297_xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
1298 const xmlChar *cur = *str;
1299 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001300 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001301
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001302 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001303 if (ret != 0)
1304 return ret;
1305
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001306 if (!VALID_MONTH(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001307 return 2;
1308
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001309 dt->mon = value;
1310
Daniel Veillard070803b2002-05-03 07:29:38 +00001311 *str = cur;
1312 return 0;
1313}
1314
1315/**
1316 * _xmlSchemaParseGDay:
1317 * @dt: pointer to a date structure
1318 * @str: pointer to the string to analyze
1319 *
1320 * Parses a xs:gDay without time zone and fills in the appropriate
1321 * field of the @dt structure. @str is updated to point just after the
1322 * xs:gDay.
1323 *
1324 * Returns 0 or the error code
1325 */
1326static int
1327_xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
1328 const xmlChar *cur = *str;
1329 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001330 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001331
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001332 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001333 if (ret != 0)
1334 return ret;
1335
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001336 if (!VALID_DAY(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001337 return 2;
1338
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001339 dt->day = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001340 *str = cur;
1341 return 0;
1342}
1343
1344/**
1345 * _xmlSchemaParseTime:
1346 * @dt: pointer to a date structure
1347 * @str: pointer to the string to analyze
1348 *
1349 * Parses a xs:time without time zone and fills in the appropriate
1350 * fields of the @dt structure. @str is updated to point just after the
1351 * xs:time.
1352 * In case of error, values of @dt fields are undefined.
1353 *
1354 * Returns 0 or the error code
1355 */
1356static int
1357_xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001358 const xmlChar *cur = *str;
Daniel Veillard070803b2002-05-03 07:29:38 +00001359 int ret = 0;
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +00001360 int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001361
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001362 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001363 if (ret != 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001364 return ret;
Daniel Veillard070803b2002-05-03 07:29:38 +00001365 if (*cur != ':')
1366 return 1;
Patrick Monnerata1dca812016-04-11 20:03:19 +02001367 if (!VALID_HOUR(value) && value != 24 /* Allow end-of-day hour */)
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001368 return 2;
Daniel Veillard070803b2002-05-03 07:29:38 +00001369 cur++;
1370
1371 /* the ':' insures this string is xs:time */
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001372 dt->hour = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001373
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001374 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001375 if (ret != 0)
1376 return ret;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001377 if (!VALID_MIN(value))
1378 return 2;
1379 dt->min = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001380
1381 if (*cur != ':')
1382 return 1;
1383 cur++;
1384
1385 PARSE_FLOAT(dt->sec, cur, ret);
1386 if (ret != 0)
1387 return ret;
1388
Patrick Monnerata1dca812016-04-11 20:03:19 +02001389 if (!VALID_TIME(dt))
Daniel Veillard070803b2002-05-03 07:29:38 +00001390 return 2;
1391
1392 *str = cur;
1393 return 0;
1394}
1395
1396/**
1397 * _xmlSchemaParseTimeZone:
1398 * @dt: pointer to a date structure
1399 * @str: pointer to the string to analyze
1400 *
1401 * Parses a time zone without time zone and fills in the appropriate
1402 * field of the @dt structure. @str is updated to point just after the
1403 * time zone.
1404 *
1405 * Returns 0 or the error code
1406 */
1407static int
1408_xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
Daniel Veillard14b56432006-03-09 18:41:40 +00001409 const xmlChar *cur;
Daniel Veillard070803b2002-05-03 07:29:38 +00001410 int ret = 0;
1411
1412 if (str == NULL)
1413 return -1;
Daniel Veillard14b56432006-03-09 18:41:40 +00001414 cur = *str;
Daniel Veillard070803b2002-05-03 07:29:38 +00001415
1416 switch (*cur) {
1417 case 0:
1418 dt->tz_flag = 0;
1419 dt->tzo = 0;
1420 break;
1421
1422 case 'Z':
1423 dt->tz_flag = 1;
1424 dt->tzo = 0;
1425 cur++;
1426 break;
1427
1428 case '+':
1429 case '-': {
1430 int isneg = 0, tmp = 0;
1431 isneg = (*cur == '-');
1432
1433 cur++;
1434
1435 PARSE_2_DIGITS(tmp, cur, ret);
1436 if (ret != 0)
1437 return ret;
1438 if (!VALID_HOUR(tmp))
1439 return 2;
1440
1441 if (*cur != ':')
1442 return 1;
1443 cur++;
1444
1445 dt->tzo = tmp * 60;
1446
1447 PARSE_2_DIGITS(tmp, cur, ret);
1448 if (ret != 0)
1449 return ret;
1450 if (!VALID_MIN(tmp))
1451 return 2;
1452
1453 dt->tzo += tmp;
1454 if (isneg)
1455 dt->tzo = - dt->tzo;
1456
1457 if (!VALID_TZO(dt->tzo))
1458 return 2;
1459
Daniel Veillard5a872412002-05-22 06:40:27 +00001460 dt->tz_flag = 1;
Daniel Veillard070803b2002-05-03 07:29:38 +00001461 break;
1462 }
1463 default:
1464 return 1;
1465 }
1466
1467 *str = cur;
1468 return 0;
1469}
1470
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001471/**
1472 * _xmlSchemaBase64Decode:
1473 * @ch: a character
1474 *
1475 * Converts a base64 encoded character to its base 64 value.
1476 *
1477 * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
1478 */
1479static int
1480_xmlSchemaBase64Decode (const xmlChar ch) {
1481 if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
1482 if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
1483 if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
1484 if ('+' == ch) return 62;
1485 if ('/' == ch) return 63;
1486 if ('=' == ch) return 64;
1487 return -1;
1488}
1489
Daniel Veillard070803b2002-05-03 07:29:38 +00001490/****************************************************************
1491 * *
1492 * XML Schema Dates/Times Datatypes Handling *
1493 * *
1494 ****************************************************************/
1495
1496/**
1497 * PARSE_DIGITS:
1498 * @num: the integer to fill in
1499 * @cur: an #xmlChar *
1500 * @num_type: an integer flag
1501 *
1502 * Parses a digits integer and updates @num with the value. @cur is
1503 * updated to point just after the integer.
1504 * In case of error, @num_type is set to -1, values of @num and
1505 * @cur are undefined.
1506 */
1507#define PARSE_DIGITS(num, cur, num_type) \
1508 if ((*cur < '0') || (*cur > '9')) \
1509 num_type = -1; \
1510 else \
1511 while ((*cur >= '0') && (*cur <= '9')) { \
1512 num = num * 10 + (*cur - '0'); \
1513 cur++; \
1514 }
1515
1516/**
1517 * PARSE_NUM:
1518 * @num: the double to fill in
1519 * @cur: an #xmlChar *
1520 * @num_type: an integer flag
1521 *
1522 * Parses a float or integer and updates @num with the value. @cur is
1523 * updated to point just after the number. If the number is a float,
1524 * then it must have an integer part and a decimal part; @num_type will
1525 * be set to 1. If there is no decimal part, @num_type is set to zero.
1526 * In case of error, @num_type is set to -1, values of @num and
1527 * @cur are undefined.
1528 */
1529#define PARSE_NUM(num, cur, num_type) \
1530 num = 0; \
1531 PARSE_DIGITS(num, cur, num_type); \
1532 if (!num_type && (*cur == '.')) { \
1533 double mult = 1; \
1534 cur++; \
1535 if ((*cur < '0') || (*cur > '9')) \
1536 num_type = -1; \
1537 else \
1538 num_type = 1; \
1539 while ((*cur >= '0') && (*cur <= '9')) { \
1540 mult /= 10; \
1541 num += (*cur - '0') * mult; \
1542 cur++; \
1543 } \
1544 }
1545
1546/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001547 * xmlSchemaValidateDates:
Daniel Veillard455cc072003-03-31 10:13:23 +00001548 * @type: the expected type or XML_SCHEMAS_UNKNOWN
Daniel Veillard070803b2002-05-03 07:29:38 +00001549 * @dateTime: string to analyze
1550 * @val: the return computed value
1551 *
1552 * Check that @dateTime conforms to the lexical space of one of the date types.
1553 * if true a value is computed and returned in @val.
1554 *
1555 * Returns 0 if this validates, a positive error code number otherwise
1556 * and -1 in case of internal or API error.
1557 */
1558static int
Daniel Veillard455cc072003-03-31 10:13:23 +00001559xmlSchemaValidateDates (xmlSchemaValType type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001560 const xmlChar *dateTime, xmlSchemaValPtr *val,
1561 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001562 xmlSchemaValPtr dt;
1563 int ret;
1564 const xmlChar *cur = dateTime;
1565
1566#define RETURN_TYPE_IF_VALID(t) \
1567 if (IS_TZO_CHAR(*cur)) { \
1568 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
1569 if (ret == 0) { \
1570 if (*cur != 0) \
1571 goto error; \
1572 dt->type = t; \
Daniel Veillard455cc072003-03-31 10:13:23 +00001573 goto done; \
Daniel Veillard070803b2002-05-03 07:29:38 +00001574 } \
1575 }
1576
1577 if (dateTime == NULL)
1578 return -1;
1579
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001580 if (collapse)
1581 while IS_WSP_BLANK_CH(*cur) cur++;
1582
Daniel Veillard070803b2002-05-03 07:29:38 +00001583 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
1584 return 1;
1585
1586 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
1587 if (dt == NULL)
1588 return -1;
1589
1590 if ((cur[0] == '-') && (cur[1] == '-')) {
1591 /*
1592 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
1593 * xs:gDay)
1594 */
1595 cur += 2;
1596
1597 /* is it an xs:gDay? */
1598 if (*cur == '-') {
Daniel Veillard455cc072003-03-31 10:13:23 +00001599 if (type == XML_SCHEMAS_GMONTH)
1600 goto error;
Daniel Veillard070803b2002-05-03 07:29:38 +00001601 ++cur;
1602 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1603 if (ret != 0)
1604 goto error;
1605
1606 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
1607
1608 goto error;
1609 }
1610
1611 /*
1612 * it should be an xs:gMonthDay or xs:gMonth
1613 */
1614 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1615 if (ret != 0)
1616 goto error;
1617
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001618 /*
1619 * a '-' char could indicate this type is xs:gMonthDay or
1620 * a negative time zone offset. Check for xs:gMonthDay first.
1621 * Also the first three char's of a negative tzo (-MM:SS) can
1622 * appear to be a valid day; so even if the day portion
1623 * of the xs:gMonthDay verifies, we must insure it was not
1624 * a tzo.
1625 */
1626 if (*cur == '-') {
1627 const xmlChar *rewnd = cur;
1628 cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001629
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001630 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001631 if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
1632
1633 /*
1634 * we can use the VALID_MDAY macro to validate the month
1635 * and day because the leap year test will flag year zero
1636 * as a leap year (even though zero is an invalid year).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001637 * FUTURE TODO: Zero will become valid in XML Schema 1.1
1638 * probably.
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001639 */
1640 if (VALID_MDAY((&(dt->value.date)))) {
1641
1642 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
1643
1644 goto error;
1645 }
1646 }
1647
1648 /*
1649 * not xs:gMonthDay so rewind and check if just xs:gMonth
1650 * with an optional time zone.
1651 */
1652 cur = rewnd;
1653 }
1654
1655 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
Daniel Veillard070803b2002-05-03 07:29:38 +00001656
1657 goto error;
1658 }
1659
1660 /*
1661 * It's a right-truncated date or an xs:time.
1662 * Try to parse an xs:time then fallback on right-truncated dates.
1663 */
1664 if ((*cur >= '0') && (*cur <= '9')) {
1665 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1666 if (ret == 0) {
1667 /* it's an xs:time */
1668 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
1669 }
1670 }
1671
1672 /* fallback on date parsing */
1673 cur = dateTime;
1674
1675 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
1676 if (ret != 0)
1677 goto error;
1678
1679 /* is it an xs:gYear? */
1680 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
1681
1682 if (*cur != '-')
1683 goto error;
1684 cur++;
1685
1686 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1687 if (ret != 0)
1688 goto error;
1689
1690 /* is it an xs:gYearMonth? */
1691 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
1692
1693 if (*cur != '-')
1694 goto error;
1695 cur++;
1696
1697 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1698 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
1699 goto error;
1700
1701 /* is it an xs:date? */
1702 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
1703
1704 if (*cur != 'T')
1705 goto error;
1706 cur++;
1707
1708 /* it should be an xs:dateTime */
1709 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1710 if (ret != 0)
1711 goto error;
1712
1713 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001714 if (collapse)
1715 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00001716 if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date))))))
Daniel Veillard070803b2002-05-03 07:29:38 +00001717 goto error;
1718
Daniel Veillard455cc072003-03-31 10:13:23 +00001719
Daniel Veillard070803b2002-05-03 07:29:38 +00001720 dt->type = XML_SCHEMAS_DATETIME;
1721
Daniel Veillard455cc072003-03-31 10:13:23 +00001722done:
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001723#if 1
1724 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
1725 goto error;
1726#else
1727 /*
1728 * insure the parsed type is equal to or less significant (right
1729 * truncated) than the desired type.
1730 */
1731 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
1732
1733 /* time only matches time */
1734 if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
1735 goto error;
1736
1737 if ((type == XML_SCHEMAS_DATETIME) &&
1738 ((dt->type != XML_SCHEMAS_DATE) ||
1739 (dt->type != XML_SCHEMAS_GYEARMONTH) ||
1740 (dt->type != XML_SCHEMAS_GYEAR)))
1741 goto error;
1742
1743 if ((type == XML_SCHEMAS_DATE) &&
1744 ((dt->type != XML_SCHEMAS_GYEAR) ||
1745 (dt->type != XML_SCHEMAS_GYEARMONTH)))
1746 goto error;
1747
1748 if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
1749 goto error;
1750
1751 if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
1752 goto error;
1753 }
Daniel Veillard455cc072003-03-31 10:13:23 +00001754#endif
1755
Daniel Veillard070803b2002-05-03 07:29:38 +00001756 if (val != NULL)
1757 *val = dt;
Daniel Veillard80b19092003-03-28 13:29:53 +00001758 else
1759 xmlSchemaFreeValue(dt);
Daniel Veillard070803b2002-05-03 07:29:38 +00001760
1761 return 0;
1762
1763error:
1764 if (dt != NULL)
1765 xmlSchemaFreeValue(dt);
1766 return 1;
1767}
1768
1769/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001770 * xmlSchemaValidateDuration:
Daniel Veillard070803b2002-05-03 07:29:38 +00001771 * @type: the predefined type
1772 * @duration: string to analyze
1773 * @val: the return computed value
1774 *
1775 * Check that @duration conforms to the lexical space of the duration type.
1776 * if true a value is computed and returned in @val.
1777 *
1778 * Returns 0 if this validates, a positive error code number otherwise
1779 * and -1 in case of internal or API error.
1780 */
1781static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00001782xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001783 const xmlChar *duration, xmlSchemaValPtr *val,
1784 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001785 const xmlChar *cur = duration;
1786 xmlSchemaValPtr dur;
1787 int isneg = 0;
1788 unsigned int seq = 0;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001789 double num;
1790 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
1791 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1792 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
Daniel Veillard070803b2002-05-03 07:29:38 +00001793
1794 if (duration == NULL)
1795 return -1;
1796
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001797 if (collapse)
1798 while IS_WSP_BLANK_CH(*cur) cur++;
1799
Daniel Veillard070803b2002-05-03 07:29:38 +00001800 if (*cur == '-') {
1801 isneg = 1;
1802 cur++;
1803 }
1804
1805 /* duration must start with 'P' (after sign) */
1806 if (*cur++ != 'P')
1807 return 1;
1808
Daniel Veillard80b19092003-03-28 13:29:53 +00001809 if (*cur == 0)
1810 return 1;
1811
Daniel Veillard070803b2002-05-03 07:29:38 +00001812 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1813 if (dur == NULL)
1814 return -1;
1815
1816 while (*cur != 0) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001817
1818 /* input string should be empty or invalid date/time item */
1819 if (seq >= sizeof(desig))
1820 goto error;
1821
1822 /* T designator must be present for time items */
1823 if (*cur == 'T') {
1824 if (seq <= 3) {
1825 seq = 3;
1826 cur++;
1827 } else
1828 return 1;
1829 } else if (seq == 3)
1830 goto error;
1831
1832 /* parse the number portion of the item */
1833 PARSE_NUM(num, cur, num_type);
1834
1835 if ((num_type == -1) || (*cur == 0))
1836 goto error;
1837
1838 /* update duration based on item type */
1839 while (seq < sizeof(desig)) {
1840 if (*cur == desig[seq]) {
1841
1842 /* verify numeric type; only seconds can be float */
1843 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1844 goto error;
1845
1846 switch (seq) {
1847 case 0:
1848 dur->value.dur.mon = (long)num * 12;
1849 break;
1850 case 1:
1851 dur->value.dur.mon += (long)num;
1852 break;
1853 default:
1854 /* convert to seconds using multiplier */
1855 dur->value.dur.sec += num * multi[seq];
1856 seq++;
1857 break;
1858 }
1859
1860 break; /* exit loop */
1861 }
1862 /* no date designators found? */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00001863 if ((++seq == 3) || (seq == 6))
Daniel Veillard070803b2002-05-03 07:29:38 +00001864 goto error;
1865 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001866 cur++;
1867 if (collapse)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001868 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001869 }
1870
1871 if (isneg) {
1872 dur->value.dur.mon = -dur->value.dur.mon;
1873 dur->value.dur.day = -dur->value.dur.day;
1874 dur->value.dur.sec = -dur->value.dur.sec;
1875 }
1876
1877 if (val != NULL)
1878 *val = dur;
Daniel Veillard80b19092003-03-28 13:29:53 +00001879 else
1880 xmlSchemaFreeValue(dur);
Daniel Veillard070803b2002-05-03 07:29:38 +00001881
1882 return 0;
1883
1884error:
1885 if (dur != NULL)
1886 xmlSchemaFreeValue(dur);
1887 return 1;
1888}
1889
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001890/**
1891 * xmlSchemaStrip:
1892 * @value: a value
1893 *
1894 * Removes the leading and ending spaces of a string
1895 *
1896 * Returns the new string or NULL if no change was required.
1897 */
1898static xmlChar *
1899xmlSchemaStrip(const xmlChar *value) {
1900 const xmlChar *start = value, *end, *f;
1901
1902 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001903 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001904 end = start;
1905 while (*end != 0) end++;
1906 f = end;
1907 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001908 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001909 end++;
1910 if ((start == value) && (f == end)) return(NULL);
1911 return(xmlStrndup(start, end - start));
1912}
Daniel Veillard96a4b252003-02-06 08:22:32 +00001913
1914/**
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001915 * xmlSchemaWhiteSpaceReplace:
1916 * @value: a value
1917 *
1918 * Replaces 0xd, 0x9 and 0xa with a space.
1919 *
1920 * Returns the new string or NULL if no change was required.
1921 */
1922xmlChar *
1923xmlSchemaWhiteSpaceReplace(const xmlChar *value) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001924 const xmlChar *cur = value;
1925 xmlChar *ret = NULL, *mcur;
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001926
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001927 if (value == NULL)
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001928 return(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001929
1930 while ((*cur != 0) &&
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001931 (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) {
1932 cur++;
1933 }
1934 if (*cur == 0)
1935 return (NULL);
1936 ret = xmlStrdup(value);
1937 /* TODO FIXME: I guess gcc will bark at this. */
1938 mcur = (xmlChar *) (ret + (cur - value));
1939 do {
1940 if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) )
1941 *mcur = ' ';
1942 mcur++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001943 } while (*mcur != 0);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001944 return(ret);
1945}
1946
1947/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001948 * xmlSchemaCollapseString:
1949 * @value: a value
1950 *
1951 * Removes and normalize white spaces in the string
1952 *
1953 * Returns the new string or NULL if no change was required.
1954 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00001955xmlChar *
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001956xmlSchemaCollapseString(const xmlChar *value) {
1957 const xmlChar *start = value, *end, *f;
1958 xmlChar *g;
1959 int col = 0;
1960
1961 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001962 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001963 end = start;
1964 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001965 if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001966 col = end - start;
1967 break;
1968 } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
1969 col = end - start;
1970 break;
1971 }
1972 end++;
1973 }
1974 if (col == 0) {
1975 f = end;
1976 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001977 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001978 end++;
1979 if ((start == value) && (f == end)) return(NULL);
1980 return(xmlStrndup(start, end - start));
1981 }
1982 start = xmlStrdup(start);
1983 if (start == NULL) return(NULL);
1984 g = (xmlChar *) (start + col);
1985 end = g;
1986 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001987 if (IS_BLANK_CH(*end)) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001988 end++;
William M. Brack76e95df2003-10-18 16:20:14 +00001989 while (IS_BLANK_CH(*end)) end++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001990 if (*end != 0)
1991 *g++ = ' ';
1992 } else
1993 *g++ = *end++;
1994 }
1995 *g = 0;
1996 return((xmlChar *) start);
1997}
1998
1999/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002000 * xmlSchemaValAtomicListNode:
2001 * @type: the predefined atomic type for a token in the list
2002 * @value: the list value to check
2003 * @ret: the return computed value
2004 * @node: the node containing the value
2005 *
2006 * Check that a value conforms to the lexical space of the predefined
2007 * list type. if true a value is computed and returned in @ret.
2008 *
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002009 * Returns the number of items if this validates, a negative error code
2010 * number otherwise
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002011 */
2012static int
2013xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
2014 xmlSchemaValPtr *ret, xmlNodePtr node) {
2015 xmlChar *val, *cur, *endval;
2016 int nb_values = 0;
Daniel Veillard580ced82003-03-21 21:22:48 +00002017 int tmp = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002018
2019 if (value == NULL) {
2020 return(-1);
2021 }
2022 val = xmlStrdup(value);
2023 if (val == NULL) {
2024 return(-1);
2025 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002026 if (ret != NULL) {
2027 *ret = NULL;
2028 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002029 cur = val;
2030 /*
2031 * Split the list
2032 */
William M. Brack76e95df2003-10-18 16:20:14 +00002033 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002034 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00002035 if (IS_BLANK_CH(*cur)) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002036 *cur = 0;
2037 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00002038 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002039 } else {
2040 nb_values++;
2041 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00002042 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002043 }
2044 }
2045 if (nb_values == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002046 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002047 return(nb_values);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002048 }
2049 endval = cur;
2050 cur = val;
2051 while ((*cur == 0) && (cur != endval)) cur++;
2052 while (cur != endval) {
2053 tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
2054 if (tmp != 0)
2055 break;
2056 while (*cur != 0) cur++;
2057 while ((*cur == 0) && (cur != endval)) cur++;
2058 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002059 /* TODO what return value ? c.f. bug #158628
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002060 if (ret != NULL) {
2061 TODO
Daniel Veillard6fc5db02005-01-16 00:05:58 +00002062 } */
2063 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00002064 if (tmp == 0)
2065 return(nb_values);
2066 return(-1);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002067}
2068
2069/**
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002070 * xmlSchemaParseUInt:
2071 * @str: pointer to the string R/W
2072 * @llo: pointer to the low result
2073 * @lmi: pointer to the mid result
2074 * @lhi: pointer to the high result
2075 *
2076 * Parse an unsigned long into 3 fields.
2077 *
William M. Brackec3b4b72005-03-15 15:50:17 +00002078 * Returns the number of significant digits in the number or
Daniel Veillardbfc42632008-04-03 10:43:52 +00002079 * -1 if overflow of the capacity and -2 if it's not a number.
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002080 */
2081static int
2082xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002083 unsigned long *lmi, unsigned long *lhi) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002084 unsigned long lo = 0, mi = 0, hi = 0;
2085 const xmlChar *tmp, *cur = *str;
2086 int ret = 0, i = 0;
2087
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002088 if (!((*cur >= '0') && (*cur <= '9')))
Daniel Veillardbfc42632008-04-03 10:43:52 +00002089 return(-2);
2090
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002091 while (*cur == '0') { /* ignore leading zeroes */
2092 cur++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002093 }
2094 tmp = cur;
2095 while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002096 i++;tmp++;ret++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002097 }
2098 if (i > 24) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002099 *str = tmp;
2100 return(-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002101 }
2102 while (i > 16) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002103 hi = hi * 10 + (*cur++ - '0');
2104 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002105 }
2106 while (i > 8) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002107 mi = mi * 10 + (*cur++ - '0');
2108 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002109 }
2110 while (i > 0) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002111 lo = lo * 10 + (*cur++ - '0');
2112 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002113 }
2114
2115 *str = cur;
2116 *llo = lo;
2117 *lmi = mi;
2118 *lhi = hi;
2119 return(ret);
2120}
2121
2122/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002123 * xmlSchemaValAtomicType:
2124 * @type: the predefined type
2125 * @value: the value to check
2126 * @val: the return computed value
2127 * @node: the node containing the value
2128 * flags: flags to control the vlidation
2129 *
2130 * Check that a value conforms to the lexical space of the atomic type.
2131 * if true a value is computed and returned in @val.
Daniel Veillard01fa6152004-06-29 17:04:39 +00002132 * This checks the value space for list types as well (IDREFS, NMTOKENS).
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002133 *
2134 * Returns 0 if this validates, a positive error code number otherwise
2135 * and -1 in case of internal or API error.
2136 */
2137static int
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002138xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002139 xmlSchemaValPtr * val, xmlNodePtr node, int flags,
2140 xmlSchemaWhitespaceValueType ws,
2141 int normOnTheFly, int applyNorm, int createStringValue)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002142{
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002143 xmlSchemaValPtr v;
2144 xmlChar *norm = NULL;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00002145 int ret = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002146
2147 if (xmlSchemaTypesInitialized == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00002148 xmlSchemaInitTypes();
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002149 if (type == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002150 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002151
Daniel Veillardeebd6332004-08-26 10:30:44 +00002152 /*
2153 * validating a non existant text node is similar to validating
2154 * an empty one.
2155 */
2156 if (value == NULL)
2157 value = BAD_CAST "";
2158
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002159 if (val != NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002160 *val = NULL;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002161 if ((flags == 0) && (value != NULL)) {
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00002162
Daniel Veillard01fa6152004-06-29 17:04:39 +00002163 if ((type->builtInType != XML_SCHEMAS_STRING) &&
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002164 (type->builtInType != XML_SCHEMAS_ANYTYPE) &&
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00002165 (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) {
2166 if (type->builtInType == XML_SCHEMAS_NORMSTRING)
2167 norm = xmlSchemaWhiteSpaceReplace(value);
2168 else
2169 norm = xmlSchemaCollapseString(value);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002170 if (norm != NULL)
2171 value = norm;
2172 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002173 }
2174
Daniel Veillard01fa6152004-06-29 17:04:39 +00002175 switch (type->builtInType) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002176 case XML_SCHEMAS_UNKNOWN:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002177 goto error;
William M. Brack2f2a6632004-08-20 23:09:47 +00002178 case XML_SCHEMAS_ANYTYPE:
2179 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002180 if ((createStringValue) && (val != NULL)) {
2181 v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE);
2182 if (v != NULL) {
2183 v->value.str = xmlStrdup(value);
2184 *val = v;
2185 } else {
2186 goto error;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002187 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002188 }
William M. Brack2f2a6632004-08-20 23:09:47 +00002189 goto return0;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002190 case XML_SCHEMAS_STRING:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002191 if (! normOnTheFly) {
2192 const xmlChar *cur = value;
2193
2194 if (ws == XML_SCHEMA_WHITESPACE_REPLACE) {
2195 while (*cur != 0) {
2196 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2197 goto return1;
2198 } else {
2199 cur++;
2200 }
2201 }
2202 } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
2203 while (*cur != 0) {
2204 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2205 goto return1;
2206 } else if IS_WSP_SPACE_CH(*cur) {
2207 cur++;
2208 if IS_WSP_SPACE_CH(*cur)
2209 goto return1;
2210 } else {
2211 cur++;
2212 }
2213 }
2214 }
2215 }
2216 if (createStringValue && (val != NULL)) {
2217 if (applyNorm) {
2218 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2219 norm = xmlSchemaCollapseString(value);
2220 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
2221 norm = xmlSchemaWhiteSpaceReplace(value);
2222 if (norm != NULL)
2223 value = norm;
2224 }
2225 v = xmlSchemaNewValue(XML_SCHEMAS_STRING);
2226 if (v != NULL) {
2227 v->value.str = xmlStrdup(value);
2228 *val = v;
2229 } else {
2230 goto error;
2231 }
2232 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002233 goto return0;
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002234 case XML_SCHEMAS_NORMSTRING:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002235 if (normOnTheFly) {
2236 if (applyNorm) {
2237 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2238 norm = xmlSchemaCollapseString(value);
2239 else
2240 norm = xmlSchemaWhiteSpaceReplace(value);
2241 if (norm != NULL)
2242 value = norm;
2243 }
2244 } else {
2245 const xmlChar *cur = value;
2246 while (*cur != 0) {
2247 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2248 goto return1;
2249 } else {
2250 cur++;
2251 }
2252 }
2253 }
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002254 if (val != NULL) {
2255 v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
2256 if (v != NULL) {
2257 v->value.str = xmlStrdup(value);
2258 *val = v;
2259 } else {
2260 goto error;
2261 }
2262 }
2263 goto return0;
2264 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002265 case XML_SCHEMAS_DECIMAL:{
William M. Brack273670f2005-03-11 15:55:14 +00002266 const xmlChar *cur = value;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002267 unsigned int len, neg, integ, hasLeadingZeroes;
William M. Brack273670f2005-03-11 15:55:14 +00002268 xmlChar cval[25];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002269 xmlChar *cptr = cval;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002270
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002271 if ((cur == NULL) || (*cur == 0))
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002272 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002273
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002274 /*
2275 * xs:decimal has a whitespace-facet value of 'collapse'.
2276 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002277 if (normOnTheFly)
2278 while IS_WSP_BLANK_CH(*cur) cur++;
2279
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002280 /*
2281 * First we handle an optional sign.
2282 */
2283 neg = 0;
2284 if (*cur == '-') {
2285 neg = 1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002286 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002287 } else if (*cur == '+')
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002288 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002289 /*
2290 * Disallow: "", "-", "- "
2291 */
2292 if (*cur == 0)
2293 goto return1;
William M. Brack273670f2005-03-11 15:55:14 +00002294 /*
2295 * Next we "pre-parse" the number, in preparation for calling
2296 * the common routine xmlSchemaParseUInt. We get rid of any
2297 * leading zeroes (because we have reserved only 25 chars),
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002298 * and note the position of a decimal point.
William M. Brack273670f2005-03-11 15:55:14 +00002299 */
2300 len = 0;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002301 integ = ~0u;
2302 hasLeadingZeroes = 0;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002303 /*
2304 * Skip leading zeroes.
2305 */
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002306 while (*cur == '0') {
William M. Brack273670f2005-03-11 15:55:14 +00002307 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002308 hasLeadingZeroes = 1;
2309 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002310 if (*cur != 0) {
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002311 do {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002312 if ((*cur >= '0') && (*cur <= '9')) {
2313 *cptr++ = *cur++;
2314 len++;
2315 } else if (*cur == '.') {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002316 cur++;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002317 integ = len;
2318 do {
2319 if ((*cur >= '0') && (*cur <= '9')) {
2320 *cptr++ = *cur++;
2321 len++;
2322 } else
2323 break;
2324 } while (len < 24);
2325 /*
2326 * Disallow "." but allow "00."
2327 */
2328 if ((len == 0) && (!hasLeadingZeroes))
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002329 goto return1;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002330 break;
2331 } else
2332 break;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002333 } while (len < 24);
William M. Brack273670f2005-03-11 15:55:14 +00002334 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002335 if (normOnTheFly)
2336 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brack273670f2005-03-11 15:55:14 +00002337 if (*cur != 0)
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002338 goto return1; /* error if any extraneous chars */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002339 if (val != NULL) {
2340 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
2341 if (v != NULL) {
William M. Brack273670f2005-03-11 15:55:14 +00002342 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002343 * Now evaluate the significant digits of the number
2344 */
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002345 if (len != 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002346
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002347 if (integ != ~0u) {
2348 /*
2349 * Get rid of trailing zeroes in the
2350 * fractional part.
2351 */
2352 while ((len != integ) && (*(cptr-1) == '0')) {
2353 cptr--;
2354 len--;
2355 }
2356 }
2357 /*
2358 * Terminate the (preparsed) string.
2359 */
2360 if (len != 0) {
Daniel Veillardbfc42632008-04-03 10:43:52 +00002361 *cptr = 0;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002362 cptr = cval;
Daniel Veillardbfc42632008-04-03 10:43:52 +00002363
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002364 xmlSchemaParseUInt((const xmlChar **)&cptr,
2365 &v->value.decimal.lo,
2366 &v->value.decimal.mi,
2367 &v->value.decimal.hi);
2368 }
2369 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002370 /*
2371 * Set the total digits to 1 if a zero value.
2372 */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002373 v->value.decimal.sign = neg;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002374 if (len == 0) {
2375 /* Speedup for zero values. */
2376 v->value.decimal.total = 1;
William M. Brack273670f2005-03-11 15:55:14 +00002377 } else {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002378 v->value.decimal.total = len;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00002379 if (integ == ~0u)
2380 v->value.decimal.frac = 0;
2381 else
2382 v->value.decimal.frac = len - integ;
William M. Brack273670f2005-03-11 15:55:14 +00002383 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002384 *val = v;
2385 }
2386 }
2387 goto return0;
2388 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002389 case XML_SCHEMAS_TIME:
2390 case XML_SCHEMAS_GDAY:
2391 case XML_SCHEMAS_GMONTH:
2392 case XML_SCHEMAS_GMONTHDAY:
2393 case XML_SCHEMAS_GYEAR:
2394 case XML_SCHEMAS_GYEARMONTH:
2395 case XML_SCHEMAS_DATE:
2396 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002397 ret = xmlSchemaValidateDates(type->builtInType, value, val,
2398 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002399 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002400 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002401 ret = xmlSchemaValidateDuration(type, value, val,
2402 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002403 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002404 case XML_SCHEMAS_FLOAT:
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002405 case XML_SCHEMAS_DOUBLE: {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002406 const xmlChar *cur = value;
2407 int neg = 0;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002408 int digits_before = 0;
2409 int digits_after = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002410
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002411 if (normOnTheFly)
2412 while IS_WSP_BLANK_CH(*cur) cur++;
2413
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002414 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
2415 cur += 3;
2416 if (*cur != 0)
2417 goto return1;
2418 if (val != NULL) {
2419 if (type == xmlSchemaTypeFloatDef) {
2420 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2421 if (v != NULL) {
2422 v->value.f = (float) xmlXPathNAN;
2423 } else {
2424 xmlSchemaFreeValue(v);
2425 goto error;
2426 }
2427 } else {
2428 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2429 if (v != NULL) {
2430 v->value.d = xmlXPathNAN;
2431 } else {
2432 xmlSchemaFreeValue(v);
2433 goto error;
2434 }
2435 }
2436 *val = v;
2437 }
2438 goto return0;
2439 }
2440 if (*cur == '-') {
2441 neg = 1;
2442 cur++;
2443 }
2444 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
2445 cur += 3;
2446 if (*cur != 0)
2447 goto return1;
2448 if (val != NULL) {
2449 if (type == xmlSchemaTypeFloatDef) {
2450 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2451 if (v != NULL) {
2452 if (neg)
2453 v->value.f = (float) xmlXPathNINF;
2454 else
2455 v->value.f = (float) xmlXPathPINF;
2456 } else {
2457 xmlSchemaFreeValue(v);
2458 goto error;
2459 }
2460 } else {
2461 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2462 if (v != NULL) {
2463 if (neg)
2464 v->value.d = xmlXPathNINF;
2465 else
2466 v->value.d = xmlXPathPINF;
2467 } else {
2468 xmlSchemaFreeValue(v);
2469 goto error;
2470 }
2471 }
2472 *val = v;
2473 }
2474 goto return0;
2475 }
2476 if ((neg == 0) && (*cur == '+'))
2477 cur++;
2478 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
2479 goto return1;
2480 while ((*cur >= '0') && (*cur <= '9')) {
2481 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002482 digits_before++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002483 }
2484 if (*cur == '.') {
2485 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002486 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002487 cur++;
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002488 digits_after++;
2489 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002490 }
Daniel Veillard1ea95902010-07-28 14:49:55 +02002491 if ((digits_before == 0) && (digits_after == 0))
Csaba Raduly5f8f5e72010-07-28 11:41:23 +02002492 goto return1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002493 if ((*cur == 'e') || (*cur == 'E')) {
2494 cur++;
2495 if ((*cur == '-') || (*cur == '+'))
2496 cur++;
2497 while ((*cur >= '0') && (*cur <= '9'))
2498 cur++;
2499 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002500 if (normOnTheFly)
2501 while IS_WSP_BLANK_CH(*cur) cur++;
2502
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002503 if (*cur != 0)
2504 goto return1;
2505 if (val != NULL) {
2506 if (type == xmlSchemaTypeFloatDef) {
2507 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2508 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002509 /*
2510 * TODO: sscanf seems not to give the correct
2511 * value for extremely high/low values.
2512 * E.g. "1E-149" results in zero.
2513 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002514 if (sscanf((const char *) value, "%f",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002515 &(v->value.f)) == 1) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002516 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002517 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002518 xmlSchemaFreeValue(v);
2519 goto return1;
2520 }
2521 } else {
2522 goto error;
2523 }
2524 } else {
2525 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2526 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002527 /*
2528 * TODO: sscanf seems not to give the correct
2529 * value for extremely high/low values.
2530 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002531 if (sscanf((const char *) value, "%lf",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002532 &(v->value.d)) == 1) {
2533 *val = v;
2534 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002535 xmlSchemaFreeValue(v);
2536 goto return1;
2537 }
2538 } else {
2539 goto error;
2540 }
2541 }
2542 }
2543 goto return0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00002544 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002545 case XML_SCHEMAS_BOOLEAN:{
2546 const xmlChar *cur = value;
2547
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002548 if (normOnTheFly) {
2549 while IS_WSP_BLANK_CH(*cur) cur++;
2550 if (*cur == '0') {
2551 ret = 0;
2552 cur++;
2553 } else if (*cur == '1') {
2554 ret = 1;
2555 cur++;
2556 } else if (*cur == 't') {
2557 cur++;
2558 if ((*cur++ == 'r') && (*cur++ == 'u') &&
2559 (*cur++ == 'e')) {
2560 ret = 1;
2561 } else
2562 goto return1;
2563 } else if (*cur == 'f') {
2564 cur++;
2565 if ((*cur++ == 'a') && (*cur++ == 'l') &&
2566 (*cur++ == 's') && (*cur++ == 'e')) {
2567 ret = 0;
2568 } else
2569 goto return1;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00002570 } else
2571 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002572 if (*cur != 0) {
2573 while IS_WSP_BLANK_CH(*cur) cur++;
2574 if (*cur != 0)
2575 goto return1;
2576 }
2577 } else {
2578 if ((cur[0] == '0') && (cur[1] == 0))
2579 ret = 0;
2580 else if ((cur[0] == '1') && (cur[1] == 0))
2581 ret = 1;
2582 else if ((cur[0] == 't') && (cur[1] == 'r')
2583 && (cur[2] == 'u') && (cur[3] == 'e')
2584 && (cur[4] == 0))
2585 ret = 1;
2586 else if ((cur[0] == 'f') && (cur[1] == 'a')
2587 && (cur[2] == 'l') && (cur[3] == 's')
2588 && (cur[4] == 'e') && (cur[5] == 0))
2589 ret = 0;
2590 else
2591 goto return1;
2592 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002593 if (val != NULL) {
2594 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
2595 if (v != NULL) {
2596 v->value.b = ret;
2597 *val = v;
2598 } else {
2599 goto error;
2600 }
2601 }
2602 goto return0;
2603 }
2604 case XML_SCHEMAS_TOKEN:{
2605 const xmlChar *cur = value;
2606
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002607 if (! normOnTheFly) {
2608 while (*cur != 0) {
2609 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2610 goto return1;
2611 } else if (*cur == ' ') {
2612 cur++;
2613 if (*cur == 0)
2614 goto return1;
2615 if (*cur == ' ')
2616 goto return1;
2617 } else {
2618 cur++;
2619 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002620 }
2621 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002622 if (val != NULL) {
2623 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
2624 if (v != NULL) {
2625 v->value.str = xmlStrdup(value);
2626 *val = v;
2627 } else {
2628 goto error;
2629 }
2630 }
2631 goto return0;
2632 }
2633 case XML_SCHEMAS_LANGUAGE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002634 if (normOnTheFly) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002635 norm = xmlSchemaCollapseString(value);
2636 if (norm != NULL)
2637 value = norm;
2638 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002639 if (xmlCheckLanguageID(value) == 1) {
2640 if (val != NULL) {
2641 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
2642 if (v != NULL) {
2643 v->value.str = xmlStrdup(value);
2644 *val = v;
2645 } else {
2646 goto error;
2647 }
2648 }
2649 goto return0;
2650 }
2651 goto return1;
2652 case XML_SCHEMAS_NMTOKEN:
2653 if (xmlValidateNMToken(value, 1) == 0) {
2654 if (val != NULL) {
2655 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
2656 if (v != NULL) {
2657 v->value.str = xmlStrdup(value);
2658 *val = v;
2659 } else {
2660 goto error;
2661 }
2662 }
2663 goto return0;
2664 }
2665 goto return1;
2666 case XML_SCHEMAS_NMTOKENS:
2667 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
2668 value, val, node);
2669 if (ret > 0)
2670 ret = 0;
2671 else
2672 ret = 1;
2673 goto done;
2674 case XML_SCHEMAS_NAME:
2675 ret = xmlValidateName(value, 1);
Daniel Veillarddf292f72005-01-16 19:00:15 +00002676 if ((ret == 0) && (val != NULL) && (value != NULL)) {
2677 v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
2678 if (v != NULL) {
2679 const xmlChar *start = value, *end;
2680 while (IS_BLANK_CH(*start)) start++;
2681 end = start;
2682 while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
2683 v->value.str = xmlStrndup(start, end - start);
2684 *val = v;
2685 } else {
2686 goto error;
2687 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002688 }
2689 goto done;
2690 case XML_SCHEMAS_QNAME:{
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002691 const xmlChar *uri = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002692 xmlChar *local = NULL;
2693
2694 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002695 if (ret != 0)
2696 goto done;
2697 if (node != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002698 xmlChar *prefix;
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002699 xmlNsPtr ns;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002700
2701 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002702 ns = xmlSearchNs(node->doc, node, prefix);
2703 if ((ns == NULL) && (prefix != NULL)) {
2704 xmlFree(prefix);
2705 if (local != NULL)
2706 xmlFree(local);
2707 goto return1;
2708 }
2709 if (ns != NULL)
2710 uri = ns->href;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002711 if (prefix != NULL)
2712 xmlFree(prefix);
2713 }
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002714 if (val != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002715 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002716 if (v == NULL) {
2717 if (local != NULL)
2718 xmlFree(local);
2719 goto error;
2720 }
2721 if (local != NULL)
2722 v->value.qname.name = local;
2723 else
2724 v->value.qname.name = xmlStrdup(value);
2725 if (uri != NULL)
2726 v->value.qname.uri = xmlStrdup(uri);
2727 *val = v;
2728 } else
2729 if (local != NULL)
2730 xmlFree(local);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002731 goto done;
2732 }
2733 case XML_SCHEMAS_NCNAME:
2734 ret = xmlValidateNCName(value, 1);
2735 if ((ret == 0) && (val != NULL)) {
2736 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
2737 if (v != NULL) {
2738 v->value.str = xmlStrdup(value);
2739 *val = v;
2740 } else {
2741 goto error;
2742 }
2743 }
2744 goto done;
2745 case XML_SCHEMAS_ID:
2746 ret = xmlValidateNCName(value, 1);
2747 if ((ret == 0) && (val != NULL)) {
2748 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
2749 if (v != NULL) {
2750 v->value.str = xmlStrdup(value);
2751 *val = v;
2752 } else {
2753 goto error;
2754 }
2755 }
2756 if ((ret == 0) && (node != NULL) &&
2757 (node->type == XML_ATTRIBUTE_NODE)) {
2758 xmlAttrPtr attr = (xmlAttrPtr) node;
2759
2760 /*
2761 * NOTE: the IDness might have already be declared in the DTD
2762 */
2763 if (attr->atype != XML_ATTRIBUTE_ID) {
2764 xmlIDPtr res;
2765 xmlChar *strip;
2766
2767 strip = xmlSchemaStrip(value);
2768 if (strip != NULL) {
2769 res = xmlAddID(NULL, node->doc, strip, attr);
2770 xmlFree(strip);
2771 } else
2772 res = xmlAddID(NULL, node->doc, value, attr);
2773 if (res == NULL) {
2774 ret = 2;
2775 } else {
2776 attr->atype = XML_ATTRIBUTE_ID;
2777 }
2778 }
2779 }
2780 goto done;
2781 case XML_SCHEMAS_IDREF:
2782 ret = xmlValidateNCName(value, 1);
2783 if ((ret == 0) && (val != NULL)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00002784 v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
2785 if (v == NULL)
2786 goto error;
2787 v->value.str = xmlStrdup(value);
2788 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002789 }
2790 if ((ret == 0) && (node != NULL) &&
2791 (node->type == XML_ATTRIBUTE_NODE)) {
2792 xmlAttrPtr attr = (xmlAttrPtr) node;
2793 xmlChar *strip;
2794
2795 strip = xmlSchemaStrip(value);
2796 if (strip != NULL) {
2797 xmlAddRef(NULL, node->doc, strip, attr);
2798 xmlFree(strip);
2799 } else
2800 xmlAddRef(NULL, node->doc, value, attr);
2801 attr->atype = XML_ATTRIBUTE_IDREF;
2802 }
2803 goto done;
2804 case XML_SCHEMAS_IDREFS:
2805 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
2806 value, val, node);
2807 if (ret < 0)
2808 ret = 2;
2809 else
2810 ret = 0;
2811 if ((ret == 0) && (node != NULL) &&
2812 (node->type == XML_ATTRIBUTE_NODE)) {
2813 xmlAttrPtr attr = (xmlAttrPtr) node;
2814
2815 attr->atype = XML_ATTRIBUTE_IDREFS;
2816 }
2817 goto done;
2818 case XML_SCHEMAS_ENTITY:{
2819 xmlChar *strip;
2820
2821 ret = xmlValidateNCName(value, 1);
2822 if ((node == NULL) || (node->doc == NULL))
2823 ret = 3;
2824 if (ret == 0) {
2825 xmlEntityPtr ent;
2826
2827 strip = xmlSchemaStrip(value);
2828 if (strip != NULL) {
2829 ent = xmlGetDocEntity(node->doc, strip);
2830 xmlFree(strip);
2831 } else {
2832 ent = xmlGetDocEntity(node->doc, value);
2833 }
2834 if ((ent == NULL) ||
2835 (ent->etype !=
2836 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
2837 ret = 4;
2838 }
2839 if ((ret == 0) && (val != NULL)) {
2840 TODO;
2841 }
2842 if ((ret == 0) && (node != NULL) &&
2843 (node->type == XML_ATTRIBUTE_NODE)) {
2844 xmlAttrPtr attr = (xmlAttrPtr) node;
2845
2846 attr->atype = XML_ATTRIBUTE_ENTITY;
2847 }
2848 goto done;
2849 }
2850 case XML_SCHEMAS_ENTITIES:
2851 if ((node == NULL) || (node->doc == NULL))
2852 goto return3;
2853 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
2854 value, val, node);
2855 if (ret <= 0)
2856 ret = 1;
2857 else
2858 ret = 0;
2859 if ((ret == 0) && (node != NULL) &&
2860 (node->type == XML_ATTRIBUTE_NODE)) {
2861 xmlAttrPtr attr = (xmlAttrPtr) node;
2862
2863 attr->atype = XML_ATTRIBUTE_ENTITIES;
2864 }
2865 goto done;
2866 case XML_SCHEMAS_NOTATION:{
2867 xmlChar *uri = NULL;
2868 xmlChar *local = NULL;
2869
2870 ret = xmlValidateQName(value, 1);
2871 if ((ret == 0) && (node != NULL)) {
2872 xmlChar *prefix;
2873
2874 local = xmlSplitQName2(value, &prefix);
2875 if (prefix != NULL) {
2876 xmlNsPtr ns;
2877
2878 ns = xmlSearchNs(node->doc, node, prefix);
2879 if (ns == NULL)
2880 ret = 1;
2881 else if (val != NULL)
2882 uri = xmlStrdup(ns->href);
2883 }
2884 if ((local != NULL) && ((val == NULL) || (ret != 0)))
2885 xmlFree(local);
2886 if (prefix != NULL)
2887 xmlFree(prefix);
2888 }
2889 if ((node == NULL) || (node->doc == NULL))
2890 ret = 3;
2891 if (ret == 0) {
2892 ret = xmlValidateNotationUse(NULL, node->doc, value);
2893 if (ret == 1)
2894 ret = 0;
2895 else
2896 ret = 1;
2897 }
2898 if ((ret == 0) && (val != NULL)) {
2899 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
2900 if (v != NULL) {
2901 if (local != NULL)
2902 v->value.qname.name = local;
2903 else
2904 v->value.qname.name = xmlStrdup(value);
2905 if (uri != NULL)
2906 v->value.qname.uri = uri;
2907
2908 *val = v;
2909 } else {
2910 if (local != NULL)
2911 xmlFree(local);
2912 if (uri != NULL)
2913 xmlFree(uri);
2914 goto error;
2915 }
2916 }
2917 goto done;
2918 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002919 case XML_SCHEMAS_ANYURI:{
Daniel Veillard11c466a2004-03-14 12:20:15 +00002920 if (*value != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002921 xmlURIPtr uri;
Vincent Lefevre933e5de2009-08-07 16:42:24 +02002922 xmlChar *tmpval, *cur;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002923 if (normOnTheFly) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002924 norm = xmlSchemaCollapseString(value);
2925 if (norm != NULL)
2926 value = norm;
2927 }
Vincent Lefevre933e5de2009-08-07 16:42:24 +02002928 tmpval = xmlStrdup(value);
2929 for (cur = tmpval; *cur; ++cur) {
2930 if (*cur < 32 || *cur >= 127 || *cur == ' ' ||
2931 *cur == '<' || *cur == '>' || *cur == '"' ||
2932 *cur == '{' || *cur == '}' || *cur == '|' ||
2933 *cur == '\\' || *cur == '^' || *cur == '`' ||
2934 *cur == '\'')
2935 *cur = '_';
2936 }
2937 uri = xmlParseURI((const char *) tmpval);
2938 xmlFree(tmpval);
Daniel Veillard11c466a2004-03-14 12:20:15 +00002939 if (uri == NULL)
2940 goto return1;
2941 xmlFreeURI(uri);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002942 }
Daniel Veillard11c466a2004-03-14 12:20:15 +00002943
2944 if (val != NULL) {
2945 v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
2946 if (v == NULL)
2947 goto error;
2948 v->value.str = xmlStrdup(value);
2949 *val = v;
2950 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002951 goto return0;
2952 }
2953 case XML_SCHEMAS_HEXBINARY:{
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002954 const xmlChar *cur = value, *start;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002955 xmlChar *base;
2956 int total, i = 0;
2957
Daniel Veillardf34a20e2004-08-31 08:42:17 +00002958 if (cur == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002959 goto return1;
2960
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002961 if (normOnTheFly)
2962 while IS_WSP_BLANK_CH(*cur) cur++;
2963
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002964 start = cur;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002965 while (((*cur >= '0') && (*cur <= '9')) ||
2966 ((*cur >= 'A') && (*cur <= 'F')) ||
2967 ((*cur >= 'a') && (*cur <= 'f'))) {
2968 i++;
2969 cur++;
2970 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002971 if (normOnTheFly)
2972 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002973
2974 if (*cur != 0)
2975 goto return1;
2976 if ((i % 2) != 0)
2977 goto return1;
2978
2979 if (val != NULL) {
2980
2981 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2982 if (v == NULL)
2983 goto error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002984 /*
2985 * Copy only the normalized piece.
2986 * CRITICAL TODO: Check this.
2987 */
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002988 cur = xmlStrndup(start, i);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002989 if (cur == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002990 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002991 xmlFree(v);
2992 goto return1;
2993 }
2994
2995 total = i / 2; /* number of octets */
2996
2997 base = (xmlChar *) cur;
2998 while (i-- > 0) {
2999 if (*base >= 'a')
3000 *base = *base - ('a' - 'A');
3001 base++;
3002 }
3003
3004 v->value.hex.str = (xmlChar *) cur;
3005 v->value.hex.total = total;
3006 *val = v;
3007 }
3008 goto return0;
3009 }
3010 case XML_SCHEMAS_BASE64BINARY:{
3011 /* ISSUE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003012 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003013 * Ignore all stray characters? (yes, currently)
3014 * Worry about long lines? (no, currently)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003015 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003016 * rfc2045.txt:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003017 *
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003018 * "The encoded output stream must be represented in lines of
3019 * no more than 76 characters each. All line breaks or other
3020 * characters not found in Table 1 must be ignored by decoding
3021 * software. In base64 data, characters other than those in
3022 * Table 1, line breaks, and other white space probably
3023 * indicate a transmission error, about which a warning
3024 * message or even a message rejection might be appropriate
3025 * under some circumstances." */
3026 const xmlChar *cur = value;
3027 xmlChar *base;
3028 int total, i = 0, pad = 0;
3029
3030 if (cur == NULL)
3031 goto return1;
3032
3033 for (; *cur; ++cur) {
3034 int decc;
3035
3036 decc = _xmlSchemaBase64Decode(*cur);
3037 if (decc < 0) ;
3038 else if (decc < 64)
3039 i++;
3040 else
3041 break;
3042 }
3043 for (; *cur; ++cur) {
3044 int decc;
3045
3046 decc = _xmlSchemaBase64Decode(*cur);
3047 if (decc < 0) ;
3048 else if (decc < 64)
3049 goto return1;
3050 if (decc == 64)
3051 pad++;
3052 }
3053
3054 /* rfc2045.txt: "Special processing is performed if fewer than
3055 * 24 bits are available at the end of the data being encoded.
3056 * A full encoding quantum is always completed at the end of a
3057 * body. When fewer than 24 input bits are available in an
3058 * input group, zero bits are added (on the right) to form an
3059 * integral number of 6-bit groups. Padding at the end of the
3060 * data is performed using the "=" character. Since all
3061 * base64 input is an integral number of octets, only the
3062 * following cases can arise: (1) the final quantum of
3063 * encoding input is an integral multiple of 24 bits; here,
3064 * the final unit of encoded output will be an integral
3065 * multiple ofindent: Standard input:701: Warning:old style
3066 * assignment ambiguity in "=*". Assuming "= *" 4 characters
3067 * with no "=" padding, (2) the final
3068 * quantum of encoding input is exactly 8 bits; here, the
3069 * final unit of encoded output will be two characters
3070 * followed by two "=" padding characters, or (3) the final
3071 * quantum of encoding input is exactly 16 bits; here, the
3072 * final unit of encoded output will be three characters
3073 * followed by one "=" padding character." */
3074
3075 total = 3 * (i / 4);
3076 if (pad == 0) {
3077 if (i % 4 != 0)
3078 goto return1;
3079 } else if (pad == 1) {
3080 int decc;
3081
3082 if (i % 4 != 3)
3083 goto return1;
3084 for (decc = _xmlSchemaBase64Decode(*cur);
3085 (decc < 0) || (decc > 63);
3086 decc = _xmlSchemaBase64Decode(*cur))
3087 --cur;
3088 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
3089 /* 00111100 -> 0x3c */
3090 if (decc & ~0x3c)
3091 goto return1;
3092 total += 2;
3093 } else if (pad == 2) {
3094 int decc;
3095
3096 if (i % 4 != 2)
3097 goto return1;
3098 for (decc = _xmlSchemaBase64Decode(*cur);
3099 (decc < 0) || (decc > 63);
3100 decc = _xmlSchemaBase64Decode(*cur))
3101 --cur;
3102 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
3103 /* 00110000 -> 0x30 */
3104 if (decc & ~0x30)
3105 goto return1;
3106 total += 1;
3107 } else
3108 goto return1;
3109
3110 if (val != NULL) {
3111 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
3112 if (v == NULL)
3113 goto error;
3114 base =
3115 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
3116 sizeof(xmlChar));
3117 if (base == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003118 xmlSchemaTypeErrMemory(node, "allocating base64 data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003119 xmlFree(v);
3120 goto return1;
3121 }
3122 v->value.base64.str = base;
3123 for (cur = value; *cur; ++cur)
3124 if (_xmlSchemaBase64Decode(*cur) >= 0) {
3125 *base = *cur;
3126 ++base;
3127 }
3128 *base = 0;
3129 v->value.base64.total = total;
3130 *val = v;
3131 }
3132 goto return0;
3133 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003134 case XML_SCHEMAS_INTEGER:
3135 case XML_SCHEMAS_PINTEGER:
3136 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003137 case XML_SCHEMAS_NINTEGER:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003138 case XML_SCHEMAS_NNINTEGER:{
3139 const xmlChar *cur = value;
3140 unsigned long lo, mi, hi;
William M. Brackec3b4b72005-03-15 15:50:17 +00003141 int sign = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003142
3143 if (cur == NULL)
3144 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003145 if (normOnTheFly)
3146 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003147 if (*cur == '-') {
3148 sign = 1;
3149 cur++;
3150 } else if (*cur == '+')
3151 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003152 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
Daniel Veillardbfc42632008-04-03 10:43:52 +00003153 if (ret < 0)
William M. Brackec3b4b72005-03-15 15:50:17 +00003154 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003155 if (normOnTheFly)
3156 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003157 if (*cur != 0)
3158 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003159 if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003160 if ((sign == 0) &&
3161 ((hi != 0) || (mi != 0) || (lo != 0)))
3162 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003163 } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003164 if (sign == 1)
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_NINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003169 if (sign == 0)
3170 goto return1;
3171 if ((hi == 0) && (mi == 0) && (lo == 0))
3172 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003173 } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003174 if ((sign == 1) &&
3175 ((hi != 0) || (mi != 0) || (lo != 0)))
3176 goto return1;
3177 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003178 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003179 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003180 if (v != NULL) {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003181 if (ret == 0)
3182 ret++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003183 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003184 v->value.decimal.mi = mi;
3185 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003186 v->value.decimal.sign = sign;
3187 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003188 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003189 *val = v;
3190 }
3191 }
3192 goto return0;
3193 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003194 case XML_SCHEMAS_LONG:
3195 case XML_SCHEMAS_BYTE:
3196 case XML_SCHEMAS_SHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003197 case XML_SCHEMAS_INT:{
Daniel Veillardbfc42632008-04-03 10:43:52 +00003198 const xmlChar *cur = value;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003199 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003200 int sign = 0;
3201
3202 if (cur == NULL)
3203 goto return1;
3204 if (*cur == '-') {
3205 sign = 1;
3206 cur++;
3207 } else if (*cur == '+')
3208 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00003209 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3210 if (ret < 0)
3211 goto return1;
3212 if (*cur != 0)
3213 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003214 if (type->builtInType == XML_SCHEMAS_LONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003215 if (hi >= 922) {
3216 if (hi > 922)
3217 goto return1;
3218 if (mi >= 33720368) {
3219 if (mi > 33720368)
3220 goto return1;
3221 if ((sign == 0) && (lo > 54775807))
3222 goto return1;
3223 if ((sign == 1) && (lo > 54775808))
3224 goto return1;
3225 }
3226 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003227 } else if (type->builtInType == XML_SCHEMAS_INT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003228 if (hi != 0)
3229 goto return1;
3230 if (mi >= 21) {
3231 if (mi > 21)
3232 goto return1;
3233 if ((sign == 0) && (lo > 47483647))
3234 goto return1;
3235 if ((sign == 1) && (lo > 47483648))
3236 goto return1;
3237 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003238 } else if (type->builtInType == XML_SCHEMAS_SHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003239 if ((mi != 0) || (hi != 0))
3240 goto return1;
3241 if ((sign == 1) && (lo > 32768))
3242 goto return1;
3243 if ((sign == 0) && (lo > 32767))
3244 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003245 } else if (type->builtInType == XML_SCHEMAS_BYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003246 if ((mi != 0) || (hi != 0))
3247 goto return1;
3248 if ((sign == 1) && (lo > 128))
3249 goto return1;
3250 if ((sign == 0) && (lo > 127))
3251 goto return1;
3252 }
3253 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003254 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003255 if (v != NULL) {
3256 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003257 v->value.decimal.mi = mi;
3258 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003259 v->value.decimal.sign = sign;
3260 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003261 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003262 *val = v;
3263 }
3264 }
3265 goto return0;
3266 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003267 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003268 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003269 case XML_SCHEMAS_USHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003270 case XML_SCHEMAS_UBYTE:{
3271 const xmlChar *cur = value;
3272 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003273
3274 if (cur == NULL)
3275 goto return1;
William M. Brackec3b4b72005-03-15 15:50:17 +00003276 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3277 if (ret < 0)
3278 goto return1;
3279 if (*cur != 0)
3280 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003281 if (type->builtInType == XML_SCHEMAS_ULONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003282 if (hi >= 1844) {
3283 if (hi > 1844)
3284 goto return1;
3285 if (mi >= 67440737) {
3286 if (mi > 67440737)
3287 goto return1;
3288 if (lo > 9551615)
3289 goto return1;
3290 }
3291 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003292 } else if (type->builtInType == XML_SCHEMAS_UINT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003293 if (hi != 0)
3294 goto return1;
3295 if (mi >= 42) {
3296 if (mi > 42)
3297 goto return1;
3298 if (lo > 94967295)
3299 goto return1;
3300 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003301 } else if (type->builtInType == XML_SCHEMAS_USHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003302 if ((mi != 0) || (hi != 0))
3303 goto return1;
3304 if (lo > 65535)
3305 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003306 } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003307 if ((mi != 0) || (hi != 0))
3308 goto return1;
3309 if (lo > 255)
3310 goto return1;
3311 }
3312 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003313 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003314 if (v != NULL) {
3315 v->value.decimal.lo = lo;
3316 v->value.decimal.mi = mi;
3317 v->value.decimal.hi = hi;
3318 v->value.decimal.sign = 0;
3319 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003320 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003321 *val = v;
3322 }
3323 }
3324 goto return0;
3325 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003326 }
3327
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003328 done:
3329 if (norm != NULL)
3330 xmlFree(norm);
3331 return (ret);
3332 return3:
3333 if (norm != NULL)
3334 xmlFree(norm);
3335 return (3);
3336 return1:
3337 if (norm != NULL)
3338 xmlFree(norm);
3339 return (1);
3340 return0:
3341 if (norm != NULL)
3342 xmlFree(norm);
3343 return (0);
3344 error:
3345 if (norm != NULL)
3346 xmlFree(norm);
3347 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003348}
3349
3350/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003351 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00003352 * @type: the predefined type
3353 * @value: the value to check
3354 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003355 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00003356 *
3357 * Check that a value conforms to the lexical space of the predefined type.
3358 * if true a value is computed and returned in @val.
3359 *
3360 * Returns 0 if this validates, a positive error code number otherwise
3361 * and -1 in case of internal or API error.
3362 */
3363int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003364xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
3365 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003366 return(xmlSchemaValAtomicType(type, value, val, node, 0,
3367 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00003368}
3369
3370/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00003371 * xmlSchemaValPredefTypeNodeNoNorm:
3372 * @type: the predefined type
3373 * @value: the value to check
3374 * @val: the return computed value
3375 * @node: the node containing the value
3376 *
3377 * Check that a value conforms to the lexical space of the predefined type.
3378 * if true a value is computed and returned in @val.
3379 * This one does apply any normalization to the value.
3380 *
3381 * Returns 0 if this validates, a positive error code number otherwise
3382 * and -1 in case of internal or API error.
3383 */
3384int
3385xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
3386 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003387 return(xmlSchemaValAtomicType(type, value, val, node, 1,
3388 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
Daniel Veillardc0826a72004-08-10 14:17:33 +00003389}
3390
3391/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003392 * xmlSchemaValidatePredefinedType:
3393 * @type: the predefined type
3394 * @value: the value to check
3395 * @val: the return computed value
3396 *
3397 * Check that a value conforms to the lexical space of the predefined type.
3398 * if true a value is computed and returned in @val.
3399 *
3400 * Returns 0 if this validates, a positive error code number otherwise
3401 * and -1 in case of internal or API error.
3402 */
3403int
3404xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
3405 xmlSchemaValPtr *val) {
3406 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
3407}
3408
3409/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003410 * xmlSchemaCompareDecimals:
3411 * @x: a first decimal value
3412 * @y: a second decimal value
3413 *
3414 * Compare 2 decimals
3415 *
3416 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
3417 */
3418static int
3419xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
3420{
3421 xmlSchemaValPtr swp;
William M. Brack273670f2005-03-11 15:55:14 +00003422 int order = 1, integx, integy, dlen;
3423 unsigned long hi, mi, lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003424
William M. Brack273670f2005-03-11 15:55:14 +00003425 /*
3426 * First test: If x is -ve and not zero
3427 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003428 if ((x->value.decimal.sign) &&
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003429 ((x->value.decimal.lo != 0) ||
3430 (x->value.decimal.mi != 0) ||
3431 (x->value.decimal.hi != 0))) {
William M. Brack273670f2005-03-11 15:55:14 +00003432 /*
3433 * Then if y is -ve and not zero reverse the compare
3434 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003435 if ((y->value.decimal.sign) &&
3436 ((y->value.decimal.lo != 0) ||
3437 (y->value.decimal.mi != 0) ||
3438 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00003439 order = -1;
William M. Brack273670f2005-03-11 15:55:14 +00003440 /*
3441 * Otherwise (y >= 0) we have the answer
3442 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003443 else
3444 return (-1);
William M. Brack273670f2005-03-11 15:55:14 +00003445 /*
3446 * If x is not -ve and y is -ve we have the answer
3447 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003448 } else if ((y->value.decimal.sign) &&
3449 ((y->value.decimal.lo != 0) ||
3450 (y->value.decimal.mi != 0) ||
3451 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003452 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00003453 }
William M. Brack273670f2005-03-11 15:55:14 +00003454 /*
3455 * If it's not simply determined by a difference in sign,
3456 * then we need to compare the actual values of the two nums.
3457 * To do this, we start by looking at the integral parts.
3458 * If the number of integral digits differ, then we have our
3459 * answer.
3460 */
3461 integx = x->value.decimal.total - x->value.decimal.frac;
3462 integy = y->value.decimal.total - y->value.decimal.frac;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003463 /*
3464 * NOTE: We changed the "total" for values like "0.1"
3465 * (or "-0.1" or ".1") to be 1, which was 2 previously.
3466 * Therefore the special case, when such values are
3467 * compared with 0, needs to be handled separately;
3468 * otherwise a zero would be recognized incorrectly as
3469 * greater than those values. This has the nice side effect
3470 * that we gain an overall optimized comparison with zeroes.
3471 * Note that a "0" has a "total" of 1 already.
3472 */
3473 if (integx == 1) {
3474 if (x->value.decimal.lo == 0) {
3475 if (integy != 1)
3476 return -order;
3477 else if (y->value.decimal.lo != 0)
3478 return -order;
3479 else
3480 return(0);
3481 }
3482 }
3483 if (integy == 1) {
3484 if (y->value.decimal.lo == 0) {
3485 if (integx != 1)
3486 return order;
3487 else if (x->value.decimal.lo != 0)
3488 return order;
3489 else
3490 return(0);
3491 }
3492 }
3493
William M. Brack273670f2005-03-11 15:55:14 +00003494 if (integx > integy)
3495 return order;
3496 else if (integy > integx)
3497 return -order;
Kasimier T. Buchcik72f50c12005-11-28 16:36:30 +00003498
William M. Brack273670f2005-03-11 15:55:14 +00003499 /*
3500 * If the number of integral digits is the same for both numbers,
3501 * then things get a little more complicated. We need to "normalize"
3502 * the numbers in order to properly compare them. To do this, we
3503 * look at the total length of each number (length => number of
3504 * significant digits), and divide the "shorter" by 10 (decreasing
3505 * the length) until they are of equal length.
3506 */
3507 dlen = x->value.decimal.total - y->value.decimal.total;
3508 if (dlen < 0) { /* y has more digits than x */
3509 swp = x;
3510 hi = y->value.decimal.hi;
3511 mi = y->value.decimal.mi;
3512 lo = y->value.decimal.lo;
3513 dlen = -dlen;
3514 order = -order;
3515 } else { /* x has more digits than y */
3516 swp = y;
3517 hi = x->value.decimal.hi;
3518 mi = x->value.decimal.mi;
3519 lo = x->value.decimal.lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003520 }
William M. Brack273670f2005-03-11 15:55:14 +00003521 while (dlen > 8) { /* in effect, right shift by 10**8 */
3522 lo = mi;
3523 mi = hi;
3524 hi = 0;
3525 dlen -= 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00003526 }
William M. Brack273670f2005-03-11 15:55:14 +00003527 while (dlen > 0) {
3528 unsigned long rem1, rem2;
3529 rem1 = (hi % 10) * 100000000L;
3530 hi = hi / 10;
3531 rem2 = (mi % 10) * 100000000L;
3532 mi = (mi + rem1) / 10;
3533 lo = (lo + rem2) / 10;
3534 dlen--;
3535 }
3536 if (hi > swp->value.decimal.hi) {
3537 return order;
3538 } else if (hi == swp->value.decimal.hi) {
3539 if (mi > swp->value.decimal.mi) {
3540 return order;
3541 } else if (mi == swp->value.decimal.mi) {
3542 if (lo > swp->value.decimal.lo) {
3543 return order;
3544 } else if (lo == swp->value.decimal.lo) {
3545 if (x->value.decimal.total == y->value.decimal.total) {
3546 return 0;
3547 } else {
3548 return order;
3549 }
3550 }
3551 }
3552 }
3553 return -order;
Daniel Veillard4255d502002-04-16 15:50:10 +00003554}
3555
3556/**
Daniel Veillard070803b2002-05-03 07:29:38 +00003557 * xmlSchemaCompareDurations:
3558 * @x: a first duration value
3559 * @y: a second duration value
3560 *
3561 * Compare 2 durations
3562 *
3563 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3564 * case of error
3565 */
3566static int
3567xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
3568{
3569 long carry, mon, day;
3570 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00003571 int invert = 1;
3572 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00003573 static const long dayRange [2][12] = {
3574 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
3575 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
3576
3577 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00003578 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00003579
3580 /* months */
3581 mon = x->value.dur.mon - y->value.dur.mon;
3582
3583 /* seconds */
3584 sec = x->value.dur.sec - y->value.dur.sec;
Daniel Veillardbbcf1272011-11-10 23:23:10 +08003585 carry = (long)(sec / SECS_PER_DAY);
3586 sec -= ((double)carry) * SECS_PER_DAY;
Daniel Veillard070803b2002-05-03 07:29:38 +00003587
3588 /* days */
3589 day = x->value.dur.day - y->value.dur.day + carry;
3590
3591 /* easy test */
3592 if (mon == 0) {
3593 if (day == 0)
3594 if (sec == 0.0)
3595 return 0;
3596 else if (sec < 0.0)
3597 return -1;
3598 else
3599 return 1;
3600 else if (day < 0)
3601 return -1;
3602 else
3603 return 1;
3604 }
3605
3606 if (mon > 0) {
3607 if ((day >= 0) && (sec >= 0.0))
3608 return 1;
3609 else {
3610 xmon = mon;
3611 xday = -day;
3612 }
3613 } else if ((day <= 0) && (sec <= 0.0)) {
3614 return -1;
3615 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00003616 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00003617 xmon = -mon;
3618 xday = day;
3619 }
3620
3621 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00003622 if (myear == 0) {
3623 minday = 0;
3624 maxday = 0;
3625 } else {
3626 maxday = 366 * ((myear + 3) / 4) +
3627 365 * ((myear - 1) % 4);
3628 minday = maxday - 1;
3629 }
3630
Daniel Veillard070803b2002-05-03 07:29:38 +00003631 xmon = xmon % 12;
3632 minday += dayRange[0][xmon];
3633 maxday += dayRange[1][xmon];
3634
Daniel Veillard80b19092003-03-28 13:29:53 +00003635 if ((maxday == minday) && (maxday == xday))
3636 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00003637 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00003638 return(-invert);
3639 if (minday > xday)
3640 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00003641
3642 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00003643 return 2;
3644}
3645
3646/*
3647 * macros for adding date/times and durations
3648 */
3649#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
3650#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
3651#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
3652#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
3653
3654/**
Daniel Veillard669adfc2004-05-29 20:12:46 +00003655 * xmlSchemaDupVal:
3656 * @v: the #xmlSchemaValPtr value to duplicate
3657 *
3658 * Makes a copy of @v. The calling program is responsible for freeing
3659 * the returned value.
3660 *
3661 * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
3662 */
3663static xmlSchemaValPtr
3664xmlSchemaDupVal (xmlSchemaValPtr v)
3665{
3666 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
3667 if (ret == NULL)
3668 return NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003669
Daniel Veillard669adfc2004-05-29 20:12:46 +00003670 memcpy(ret, v, sizeof(xmlSchemaVal));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003671 ret->next = NULL;
Daniel Veillard669adfc2004-05-29 20:12:46 +00003672 return ret;
3673}
3674
3675/**
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003676 * xmlSchemaCopyValue:
3677 * @val: the precomputed value to be copied
3678 *
3679 * Copies the precomputed value. This duplicates any string within.
3680 *
3681 * Returns the copy or NULL if a copy for a data-type is not implemented.
3682 */
3683xmlSchemaValPtr
3684xmlSchemaCopyValue(xmlSchemaValPtr val)
3685{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003686 xmlSchemaValPtr ret = NULL, prev = NULL, cur;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003687
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003688 /*
3689 * Copy the string values.
3690 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003691 while (val != NULL) {
3692 switch (val->type) {
3693 case XML_SCHEMAS_ANYTYPE:
3694 case XML_SCHEMAS_IDREFS:
3695 case XML_SCHEMAS_ENTITIES:
3696 case XML_SCHEMAS_NMTOKENS:
3697 xmlSchemaFreeValue(ret);
3698 return (NULL);
3699 case XML_SCHEMAS_ANYSIMPLETYPE:
3700 case XML_SCHEMAS_STRING:
3701 case XML_SCHEMAS_NORMSTRING:
3702 case XML_SCHEMAS_TOKEN:
3703 case XML_SCHEMAS_LANGUAGE:
3704 case XML_SCHEMAS_NAME:
3705 case XML_SCHEMAS_NCNAME:
3706 case XML_SCHEMAS_ID:
3707 case XML_SCHEMAS_IDREF:
3708 case XML_SCHEMAS_ENTITY:
3709 case XML_SCHEMAS_NMTOKEN:
3710 case XML_SCHEMAS_ANYURI:
3711 cur = xmlSchemaDupVal(val);
3712 if (val->value.str != NULL)
3713 cur->value.str = xmlStrdup(BAD_CAST val->value.str);
3714 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003715 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003716 case XML_SCHEMAS_NOTATION:
3717 cur = xmlSchemaDupVal(val);
3718 if (val->value.qname.name != NULL)
3719 cur->value.qname.name =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003720 xmlStrdup(BAD_CAST val->value.qname.name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003721 if (val->value.qname.uri != NULL)
3722 cur->value.qname.uri =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003723 xmlStrdup(BAD_CAST val->value.qname.uri);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003724 break;
3725 case XML_SCHEMAS_HEXBINARY:
3726 cur = xmlSchemaDupVal(val);
3727 if (val->value.hex.str != NULL)
3728 cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
3729 break;
3730 case XML_SCHEMAS_BASE64BINARY:
3731 cur = xmlSchemaDupVal(val);
3732 if (val->value.base64.str != NULL)
3733 cur->value.base64.str =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003734 xmlStrdup(BAD_CAST val->value.base64.str);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003735 break;
3736 default:
3737 cur = xmlSchemaDupVal(val);
3738 break;
3739 }
3740 if (ret == NULL)
3741 ret = cur;
3742 else
3743 prev->next = cur;
3744 prev = cur;
3745 val = val->next;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003746 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003747 return (ret);
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003748}
3749
3750/**
Daniel Veillard5a872412002-05-22 06:40:27 +00003751 * _xmlSchemaDateAdd:
3752 * @dt: an #xmlSchemaValPtr
3753 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
3754 *
3755 * Compute a new date/time from @dt and @dur. This function assumes @dt
3756 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
Daniel Veillard669adfc2004-05-29 20:12:46 +00003757 * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
3758 * @dt. The calling program is responsible for freeing the returned value.
Daniel Veillard5a872412002-05-22 06:40:27 +00003759 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003760 * Returns a pointer to a new #xmlSchemaVal or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003761 */
3762static xmlSchemaValPtr
3763_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
3764{
Daniel Veillard669adfc2004-05-29 20:12:46 +00003765 xmlSchemaValPtr ret, tmp;
Daniel Veillard5a872412002-05-22 06:40:27 +00003766 long carry, tempdays, temp;
3767 xmlSchemaValDatePtr r, d;
3768 xmlSchemaValDurationPtr u;
3769
3770 if ((dt == NULL) || (dur == NULL))
3771 return NULL;
3772
3773 ret = xmlSchemaNewValue(dt->type);
3774 if (ret == NULL)
3775 return NULL;
3776
Daniel Veillard669adfc2004-05-29 20:12:46 +00003777 /* make a copy so we don't alter the original value */
3778 tmp = xmlSchemaDupVal(dt);
3779 if (tmp == NULL) {
3780 xmlSchemaFreeValue(ret);
3781 return NULL;
3782 }
3783
Daniel Veillard5a872412002-05-22 06:40:27 +00003784 r = &(ret->value.date);
Daniel Veillard669adfc2004-05-29 20:12:46 +00003785 d = &(tmp->value.date);
Daniel Veillard5a872412002-05-22 06:40:27 +00003786 u = &(dur->value.dur);
3787
3788 /* normalization */
3789 if (d->mon == 0)
3790 d->mon = 1;
3791
3792 /* normalize for time zone offset */
3793 u->sec -= (d->tzo * 60);
3794 d->tzo = 0;
3795
3796 /* normalization */
3797 if (d->day == 0)
3798 d->day = 1;
3799
3800 /* month */
3801 carry = d->mon + u->mon;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003802 r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
3803 carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003804
3805 /* year (may be modified later) */
3806 r->year = d->year + carry;
3807 if (r->year == 0) {
3808 if (d->year > 0)
3809 r->year--;
3810 else
3811 r->year++;
3812 }
3813
3814 /* time zone */
3815 r->tzo = d->tzo;
3816 r->tz_flag = d->tz_flag;
3817
3818 /* seconds */
3819 r->sec = d->sec + u->sec;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003820 carry = (long) FQUOTIENT((long)r->sec, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003821 if (r->sec != 0.0) {
3822 r->sec = MODULO(r->sec, 60.0);
3823 }
3824
3825 /* minute */
3826 carry += d->min;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003827 r->min = (unsigned int) MODULO(carry, 60);
3828 carry = (long) FQUOTIENT(carry, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003829
3830 /* hours */
3831 carry += d->hour;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003832 r->hour = (unsigned int) MODULO(carry, 24);
3833 carry = (long)FQUOTIENT(carry, 24);
Daniel Veillard5a872412002-05-22 06:40:27 +00003834
3835 /*
3836 * days
3837 * Note we use tempdays because the temporary values may need more
3838 * than 5 bits
3839 */
3840 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
3841 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
3842 tempdays = MAX_DAYINMONTH(r->year, r->mon);
3843 else if (d->day < 1)
3844 tempdays = 1;
3845 else
3846 tempdays = d->day;
3847
3848 tempdays += u->day + carry;
3849
3850 while (1) {
3851 if (tempdays < 1) {
Daniel Veillard5e9576a2005-11-21 11:23:47 +00003852 long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13);
3853 long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003854 if (tyr == 0)
3855 tyr--;
Daniel Veillard14b56432006-03-09 18:41:40 +00003856 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003857 * Coverity detected an overrun in daysInMonth
Daniel Veillard14b56432006-03-09 18:41:40 +00003858 * of size 12 at position 12 with index variable "((r)->mon - 1)"
3859 */
David Kilzer30cf4392014-07-14 22:29:56 +08003860 if (tmon < 1)
3861 tmon = 1;
Daniel Veillard14b56432006-03-09 18:41:40 +00003862 if (tmon > 12)
3863 tmon = 12;
Daniel Veillard5a872412002-05-22 06:40:27 +00003864 tempdays += MAX_DAYINMONTH(tyr, tmon);
3865 carry = -1;
Philip Withnall4ba5d312014-06-20 21:37:21 +01003866 } else if (VALID_YEAR(r->year) && VALID_MONTH(r->mon) &&
3867 tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003868 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
3869 carry = 1;
3870 } else
3871 break;
3872
3873 temp = r->mon + carry;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003874 r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
3875 r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003876 if (r->year == 0) {
3877 if (temp < 1)
3878 r->year--;
3879 else
3880 r->year++;
3881 }
3882 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003883
Daniel Veillard5a872412002-05-22 06:40:27 +00003884 r->day = tempdays;
3885
3886 /*
3887 * adjust the date/time type to the date values
3888 */
3889 if (ret->type != XML_SCHEMAS_DATETIME) {
3890 if ((r->hour) || (r->min) || (r->sec))
3891 ret->type = XML_SCHEMAS_DATETIME;
3892 else if (ret->type != XML_SCHEMAS_DATE) {
3893 if ((r->mon != 1) && (r->day != 1))
3894 ret->type = XML_SCHEMAS_DATE;
3895 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
3896 ret->type = XML_SCHEMAS_GYEARMONTH;
3897 }
3898 }
3899
Daniel Veillard669adfc2004-05-29 20:12:46 +00003900 xmlSchemaFreeValue(tmp);
Daniel Veillard5a872412002-05-22 06:40:27 +00003901
Daniel Veillard5a872412002-05-22 06:40:27 +00003902 return ret;
3903}
3904
3905/**
3906 * xmlSchemaDateNormalize:
Daniel Veillard669adfc2004-05-29 20:12:46 +00003907 * @dt: an #xmlSchemaValPtr of a date/time type value.
3908 * @offset: number of seconds to adjust @dt by.
Daniel Veillard5a872412002-05-22 06:40:27 +00003909 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003910 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3911 * the return value is a time-zone offset is present on @dt.
Daniel Veillard5a872412002-05-22 06:40:27 +00003912 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003913 * Returns a normalized copy of @dt or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003914 */
3915static xmlSchemaValPtr
3916xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
3917{
3918 xmlSchemaValPtr dur, ret;
3919
3920 if (dt == NULL)
3921 return NULL;
3922
3923 if (((dt->type != XML_SCHEMAS_TIME) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003924 (dt->type != XML_SCHEMAS_DATETIME) &&
3925 (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
Daniel Veillard5a872412002-05-22 06:40:27 +00003926 return xmlSchemaDupVal(dt);
3927
3928 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
3929 if (dur == NULL)
3930 return NULL;
3931
3932 dur->value.date.sec -= offset;
3933
3934 ret = _xmlSchemaDateAdd(dt, dur);
3935 if (ret == NULL)
3936 return NULL;
3937
3938 xmlSchemaFreeValue(dur);
3939
3940 /* ret->value.date.tzo = 0; */
3941 return ret;
3942}
3943
3944/**
3945 * _xmlSchemaDateCastYMToDays:
3946 * @dt: an #xmlSchemaValPtr
3947 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003948 * Convert mon and year of @dt to total number of days. Take the
Daniel Veillard5a872412002-05-22 06:40:27 +00003949 * number of years since (or before) 1 AD and add the number of leap
3950 * years. This is a function because negative
3951 * years must be handled a little differently and there is no zero year.
3952 *
3953 * Returns number of days.
3954 */
3955static long
3956_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
3957{
3958 long ret;
Daniel Veillard49e89632004-09-23 16:24:36 +00003959 int mon;
Daniel Veillard5a872412002-05-22 06:40:27 +00003960
Daniel Veillard49e89632004-09-23 16:24:36 +00003961 mon = dt->value.date.mon;
3962 if (mon <= 0) mon = 1; /* normalization */
3963
3964 if (dt->value.date.year <= 0)
Daniel Veillard5a872412002-05-22 06:40:27 +00003965 ret = (dt->value.date.year * 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 else
3970 ret = ((dt->value.date.year-1) * 365) +
3971 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
3972 ((dt->value.date.year-1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003973 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003974
3975 return ret;
3976}
3977
3978/**
3979 * TIME_TO_NUMBER:
3980 * @dt: an #xmlSchemaValPtr
3981 *
3982 * Calculates the number of seconds in the time portion of @dt.
3983 *
3984 * Returns seconds.
3985 */
3986#define TIME_TO_NUMBER(dt) \
3987 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00003988 (dt->value.date.min * SECS_PER_MIN) + \
3989 (dt->value.date.tzo * SECS_PER_MIN)) + \
3990 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00003991
3992/**
3993 * xmlSchemaCompareDates:
3994 * @x: a first date/time value
3995 * @y: a second date/time value
3996 *
3997 * Compare 2 date/times
3998 *
3999 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4000 * case of error
4001 */
4002static int
4003xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
4004{
4005 unsigned char xmask, ymask, xor_mask, and_mask;
4006 xmlSchemaValPtr p1, p2, q1, q2;
4007 long p1d, p2d, q1d, q2d;
4008
4009 if ((x == NULL) || (y == NULL))
4010 return -2;
4011
4012 if (x->value.date.tz_flag) {
4013
4014 if (!y->value.date.tz_flag) {
4015 p1 = xmlSchemaDateNormalize(x, 0);
4016 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4017 /* normalize y + 14:00 */
4018 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
4019
4020 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004021 if (p1d < q1d) {
4022 xmlSchemaFreeValue(p1);
4023 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004024 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004025 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004026 double sec;
4027
4028 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004029 if (sec < 0.0) {
4030 xmlSchemaFreeValue(p1);
4031 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004032 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004033 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004034 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004035 /* normalize y - 14:00 */
4036 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
4037 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
4038 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004039 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004040 else if (p1d == q2d) {
4041 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
4042 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004043 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004044 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004045 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004046 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004047 xmlSchemaFreeValue(p1);
4048 xmlSchemaFreeValue(q1);
4049 xmlSchemaFreeValue(q2);
4050 if (ret != 0)
4051 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004052 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004053 } else {
4054 xmlSchemaFreeValue(p1);
4055 xmlSchemaFreeValue(q1);
4056 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004057 }
4058 } else if (y->value.date.tz_flag) {
4059 q1 = xmlSchemaDateNormalize(y, 0);
4060 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4061
4062 /* normalize x - 14:00 */
4063 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
4064 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4065
Daniel Veillardfdc91562002-07-01 21:52:03 +00004066 if (p1d < q1d) {
4067 xmlSchemaFreeValue(p1);
4068 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004069 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004070 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004071 double sec;
4072
4073 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00004074 if (sec < 0.0) {
4075 xmlSchemaFreeValue(p1);
4076 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004077 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004078 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004079 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004080 /* normalize x + 14:00 */
4081 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
4082 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
4083
Daniel Veillard6560a422003-03-27 21:25:38 +00004084 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004085 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00004086 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00004087 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
4088 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004089 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004090 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004091 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00004092 }
Daniel Veillard6560a422003-03-27 21:25:38 +00004093 xmlSchemaFreeValue(p1);
4094 xmlSchemaFreeValue(q1);
4095 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004096 if (ret != 0)
4097 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004098 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00004099 } else {
4100 xmlSchemaFreeValue(p1);
4101 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00004102 }
4103 }
4104
4105 /*
4106 * if the same type then calculate the difference
4107 */
4108 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004109 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00004110 q1 = xmlSchemaDateNormalize(y, 0);
4111 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
4112
4113 p1 = xmlSchemaDateNormalize(x, 0);
4114 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
4115
Daniel Veillardfdc91562002-07-01 21:52:03 +00004116 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004117 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004118 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004119 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00004120 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00004121 double sec;
4122
4123 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
4124 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004125 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00004126 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004127 ret = 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004128
Daniel Veillard5a872412002-05-22 06:40:27 +00004129 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00004130 xmlSchemaFreeValue(p1);
4131 xmlSchemaFreeValue(q1);
4132 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00004133 }
4134
4135 switch (x->type) {
4136 case XML_SCHEMAS_DATETIME:
4137 xmask = 0xf;
4138 break;
4139 case XML_SCHEMAS_DATE:
4140 xmask = 0x7;
4141 break;
4142 case XML_SCHEMAS_GYEAR:
4143 xmask = 0x1;
4144 break;
4145 case XML_SCHEMAS_GMONTH:
4146 xmask = 0x2;
4147 break;
4148 case XML_SCHEMAS_GDAY:
4149 xmask = 0x3;
4150 break;
4151 case XML_SCHEMAS_GYEARMONTH:
4152 xmask = 0x3;
4153 break;
4154 case XML_SCHEMAS_GMONTHDAY:
4155 xmask = 0x6;
4156 break;
4157 case XML_SCHEMAS_TIME:
4158 xmask = 0x8;
4159 break;
4160 default:
4161 xmask = 0;
4162 break;
4163 }
4164
4165 switch (y->type) {
4166 case XML_SCHEMAS_DATETIME:
4167 ymask = 0xf;
4168 break;
4169 case XML_SCHEMAS_DATE:
4170 ymask = 0x7;
4171 break;
4172 case XML_SCHEMAS_GYEAR:
4173 ymask = 0x1;
4174 break;
4175 case XML_SCHEMAS_GMONTH:
4176 ymask = 0x2;
4177 break;
4178 case XML_SCHEMAS_GDAY:
4179 ymask = 0x3;
4180 break;
4181 case XML_SCHEMAS_GYEARMONTH:
4182 ymask = 0x3;
4183 break;
4184 case XML_SCHEMAS_GMONTHDAY:
4185 ymask = 0x6;
4186 break;
4187 case XML_SCHEMAS_TIME:
4188 ymask = 0x8;
4189 break;
4190 default:
4191 ymask = 0;
4192 break;
4193 }
4194
4195 xor_mask = xmask ^ ymask; /* mark type differences */
4196 and_mask = xmask & ymask; /* mark field specification */
4197
4198 /* year */
4199 if (xor_mask & 1)
4200 return 2; /* indeterminate */
4201 else if (and_mask & 1) {
4202 if (x->value.date.year < y->value.date.year)
4203 return -1;
4204 else if (x->value.date.year > y->value.date.year)
4205 return 1;
4206 }
4207
4208 /* month */
4209 if (xor_mask & 2)
4210 return 2; /* indeterminate */
4211 else if (and_mask & 2) {
4212 if (x->value.date.mon < y->value.date.mon)
4213 return -1;
4214 else if (x->value.date.mon > y->value.date.mon)
4215 return 1;
4216 }
4217
4218 /* day */
4219 if (xor_mask & 4)
4220 return 2; /* indeterminate */
4221 else if (and_mask & 4) {
4222 if (x->value.date.day < y->value.date.day)
4223 return -1;
4224 else if (x->value.date.day > y->value.date.day)
4225 return 1;
4226 }
4227
4228 /* time */
4229 if (xor_mask & 8)
4230 return 2; /* indeterminate */
4231 else if (and_mask & 8) {
4232 if (x->value.date.hour < y->value.date.hour)
4233 return -1;
4234 else if (x->value.date.hour > y->value.date.hour)
4235 return 1;
4236 else if (x->value.date.min < y->value.date.min)
4237 return -1;
4238 else if (x->value.date.min > y->value.date.min)
4239 return 1;
4240 else if (x->value.date.sec < y->value.date.sec)
4241 return -1;
4242 else if (x->value.date.sec > y->value.date.sec)
4243 return 1;
4244 }
4245
Daniel Veillard070803b2002-05-03 07:29:38 +00004246 return 0;
4247}
4248
4249/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004250 * xmlSchemaComparePreserveReplaceStrings:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004251 * @x: a first string value
4252 * @y: a second string value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004253 * @invert: inverts the result if x < y or x > y.
4254 *
4255 * Compare 2 string for their normalized values.
4256 * @x is a string with whitespace of "preserve", @y is
4257 * a string with a whitespace of "replace". I.e. @x could
4258 * be an "xsd:string" and @y an "xsd:normalizedString".
4259 *
4260 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4261 * case of error
4262 */
4263static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004264xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
4265 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004266 int invert)
4267{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004268 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004269
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004270 while ((*x != 0) && (*y != 0)) {
4271 if (IS_WSP_REPLACE_CH(*y)) {
4272 if (! IS_WSP_SPACE_CH(*x)) {
4273 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004274 if (invert)
4275 return(1);
4276 else
4277 return(-1);
4278 } else {
4279 if (invert)
4280 return(-1);
4281 else
4282 return(1);
4283 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004284 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004285 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004286 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004287 if (tmp < 0) {
4288 if (invert)
4289 return(1);
4290 else
4291 return(-1);
4292 }
4293 if (tmp > 0) {
4294 if (invert)
4295 return(-1);
4296 else
4297 return(1);
4298 }
4299 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004300 x++;
4301 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004302 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004303 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004304 if (invert)
4305 return(-1);
4306 else
4307 return(1);
4308 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004309 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004310 if (invert)
4311 return(1);
4312 else
4313 return(-1);
4314 }
4315 return(0);
4316}
4317
4318/**
4319 * xmlSchemaComparePreserveCollapseStrings:
4320 * @x: a first string value
4321 * @y: a second string value
4322 *
4323 * Compare 2 string for their normalized values.
4324 * @x is a string with whitespace of "preserve", @y is
4325 * a string with a whitespace of "collapse". I.e. @x could
4326 * be an "xsd:string" and @y an "xsd:normalizedString".
4327 *
4328 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4329 * case of error
4330 */
4331static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004332xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
4333 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004334 int invert)
4335{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004336 int tmp;
4337
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004338 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004339 * Skip leading blank chars of the collapsed string.
4340 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004341 while IS_WSP_BLANK_CH(*y)
4342 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004343
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004344 while ((*x != 0) && (*y != 0)) {
4345 if IS_WSP_BLANK_CH(*y) {
4346 if (! IS_WSP_SPACE_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004347 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004348 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004349 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004350 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004351 if (invert)
4352 return(1);
4353 else
4354 return(-1);
4355 } else {
4356 if (invert)
4357 return(-1);
4358 else
4359 return(1);
4360 }
4361 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004362 x++;
4363 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004364 /*
4365 * Skip contiguous blank chars of the collapsed string.
4366 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004367 while IS_WSP_BLANK_CH(*y)
4368 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004369 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004370 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004371 if (tmp < 0) {
4372 if (invert)
4373 return(1);
4374 else
4375 return(-1);
4376 }
4377 if (tmp > 0) {
4378 if (invert)
4379 return(-1);
4380 else
4381 return(1);
4382 }
4383 }
4384 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004385 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004386 if (invert)
4387 return(-1);
4388 else
4389 return(1);
4390 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004391 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004392 /*
4393 * Skip trailing blank chars of the collapsed string.
4394 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004395 while IS_WSP_BLANK_CH(*y)
4396 y++;
4397 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004398 if (invert)
4399 return(1);
4400 else
4401 return(-1);
4402 }
4403 }
4404 return(0);
4405}
4406
4407/**
4408 * xmlSchemaComparePreserveCollapseStrings:
4409 * @x: a first string value
4410 * @y: a second string value
4411 *
4412 * Compare 2 string for their normalized values.
4413 * @x is a string with whitespace of "preserve", @y is
4414 * a string with a whitespace of "collapse". I.e. @x could
4415 * be an "xsd:string" and @y an "xsd:normalizedString".
4416 *
4417 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4418 * case of error
4419 */
4420static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004421xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
4422 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004423 int invert)
4424{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004425 int tmp;
4426
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004427 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004428 * Skip leading blank chars of the collapsed string.
4429 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004430 while IS_WSP_BLANK_CH(*y)
4431 y++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004432
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004433 while ((*x != 0) && (*y != 0)) {
4434 if IS_WSP_BLANK_CH(*y) {
4435 if (! IS_WSP_BLANK_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004436 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004437 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004438 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004439 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004440 if (invert)
4441 return(1);
4442 else
4443 return(-1);
4444 } else {
4445 if (invert)
4446 return(-1);
4447 else
4448 return(1);
4449 }
4450 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004451 x++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004452 y++;
4453 /*
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004454 * Skip contiguous blank chars of the collapsed string.
4455 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004456 while IS_WSP_BLANK_CH(*y)
4457 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004458 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004459 if IS_WSP_BLANK_CH(*x) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004460 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004461 * The xv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004462 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004463 if ((0x20 - *y) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004464 if (invert)
4465 return(1);
4466 else
4467 return(-1);
4468 } else {
4469 if (invert)
4470 return(-1);
4471 else
4472 return(1);
4473 }
4474 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004475 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004476 if (tmp < 0)
4477 return(-1);
4478 if (tmp > 0)
4479 return(1);
4480 }
4481 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004482 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004483 if (invert)
4484 return(-1);
4485 else
4486 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004487 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004488 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004489 /*
4490 * Skip trailing blank chars of the collapsed string.
4491 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004492 while IS_WSP_BLANK_CH(*y)
4493 y++;
4494 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004495 if (invert)
4496 return(1);
4497 else
4498 return(-1);
4499 }
4500 }
4501 return(0);
4502}
4503
4504
4505/**
4506 * xmlSchemaCompareReplacedStrings:
4507 * @x: a first string value
4508 * @y: a second string value
4509 *
4510 * Compare 2 string for their normalized values.
4511 *
4512 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4513 * case of error
4514 */
4515static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004516xmlSchemaCompareReplacedStrings(const xmlChar *x,
4517 const xmlChar *y)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004518{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004519 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004520
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004521 while ((*x != 0) && (*y != 0)) {
4522 if IS_WSP_BLANK_CH(*y) {
4523 if (! IS_WSP_BLANK_CH(*x)) {
4524 if ((*x - 0x20) < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004525 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004526 else
4527 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004528 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004529 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004530 if IS_WSP_BLANK_CH(*x) {
4531 if ((0x20 - *y) < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004532 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004533 else
4534 return(1);
4535 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004536 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004537 if (tmp < 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004538 return(-1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004539 if (tmp > 0)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004540 return(1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004541 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004542 x++;
4543 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004544 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004545 if (*x != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004546 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004547 if (*y != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004548 return(-1);
4549 return(0);
4550}
4551
4552/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004553 * xmlSchemaCompareNormStrings:
4554 * @x: a first string value
4555 * @y: a second string value
4556 *
4557 * Compare 2 string for their normalized values.
4558 *
4559 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4560 * case of error
4561 */
4562static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004563xmlSchemaCompareNormStrings(const xmlChar *x,
4564 const xmlChar *y) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00004565 int tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004566
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004567 while (IS_BLANK_CH(*x)) x++;
4568 while (IS_BLANK_CH(*y)) y++;
4569 while ((*x != 0) && (*y != 0)) {
4570 if (IS_BLANK_CH(*x)) {
4571 if (!IS_BLANK_CH(*y)) {
4572 tmp = *x - *y;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004573 return(tmp);
4574 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004575 while (IS_BLANK_CH(*x)) x++;
4576 while (IS_BLANK_CH(*y)) y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004577 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004578 tmp = *x++ - *y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004579 if (tmp < 0)
4580 return(-1);
4581 if (tmp > 0)
4582 return(1);
4583 }
4584 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004585 if (*x != 0) {
4586 while (IS_BLANK_CH(*x)) x++;
4587 if (*x != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004588 return(1);
4589 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004590 if (*y != 0) {
4591 while (IS_BLANK_CH(*y)) y++;
4592 if (*y != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004593 return(-1);
4594 }
4595 return(0);
4596}
4597
4598/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004599 * xmlSchemaCompareFloats:
4600 * @x: a first float or double value
4601 * @y: a second float or double value
4602 *
4603 * Compare 2 values
4604 *
4605 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4606 * case of error
4607 */
4608static int
4609xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4610 double d1, d2;
4611
4612 if ((x == NULL) || (y == NULL))
4613 return(-2);
4614
4615 /*
4616 * Cast everything to doubles.
4617 */
4618 if (x->type == XML_SCHEMAS_DOUBLE)
4619 d1 = x->value.d;
4620 else if (x->type == XML_SCHEMAS_FLOAT)
4621 d1 = x->value.f;
4622 else
4623 return(-2);
4624
4625 if (y->type == XML_SCHEMAS_DOUBLE)
4626 d2 = y->value.d;
4627 else if (y->type == XML_SCHEMAS_FLOAT)
4628 d2 = y->value.f;
4629 else
4630 return(-2);
4631
4632 /*
4633 * Check for special cases.
4634 */
4635 if (xmlXPathIsNaN(d1)) {
4636 if (xmlXPathIsNaN(d2))
4637 return(0);
4638 return(1);
4639 }
4640 if (xmlXPathIsNaN(d2))
4641 return(-1);
4642 if (d1 == xmlXPathPINF) {
4643 if (d2 == xmlXPathPINF)
4644 return(0);
4645 return(1);
4646 }
4647 if (d2 == xmlXPathPINF)
4648 return(-1);
4649 if (d1 == xmlXPathNINF) {
4650 if (d2 == xmlXPathNINF)
4651 return(0);
4652 return(-1);
4653 }
4654 if (d2 == xmlXPathNINF)
4655 return(1);
4656
4657 /*
4658 * basic tests, the last one we should have equality, but
4659 * portability is more important than speed and handling
4660 * NaN or Inf in a portable way is always a challenge, so ...
4661 */
4662 if (d1 < d2)
4663 return(-1);
4664 if (d1 > d2)
4665 return(1);
4666 if (d1 == d2)
4667 return(0);
4668 return(2);
4669}
4670
4671/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004672 * xmlSchemaCompareValues:
4673 * @x: a first value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004674 * @xvalue: the first value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004675 * @xwtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004676 * @y: a second value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004677 * @xvalue: the second value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004678 * @ywtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004679 *
4680 * Compare 2 values
4681 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004682 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4683 * comparable and -2 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004684 */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004685static int
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004686xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
4687 xmlSchemaValPtr x,
4688 const xmlChar *xvalue,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004689 xmlSchemaWhitespaceValueType xws,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004690 xmlSchemaValType ytype,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004691 xmlSchemaValPtr y,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004692 const xmlChar *yvalue,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004693 xmlSchemaWhitespaceValueType yws)
4694{
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004695 switch (xtype) {
Daniel Veillard80b19092003-03-28 13:29:53 +00004696 case XML_SCHEMAS_UNKNOWN:
William M. Brack2f2a6632004-08-20 23:09:47 +00004697 case XML_SCHEMAS_ANYTYPE:
Daniel Veillard80b19092003-03-28 13:29:53 +00004698 return(-2);
4699 case XML_SCHEMAS_INTEGER:
4700 case XML_SCHEMAS_NPINTEGER:
4701 case XML_SCHEMAS_NINTEGER:
4702 case XML_SCHEMAS_NNINTEGER:
4703 case XML_SCHEMAS_PINTEGER:
4704 case XML_SCHEMAS_INT:
4705 case XML_SCHEMAS_UINT:
4706 case XML_SCHEMAS_LONG:
4707 case XML_SCHEMAS_ULONG:
4708 case XML_SCHEMAS_SHORT:
4709 case XML_SCHEMAS_USHORT:
4710 case XML_SCHEMAS_BYTE:
4711 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00004712 case XML_SCHEMAS_DECIMAL:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004713 if ((x == NULL) || (y == NULL))
4714 return(-2);
4715 if (ytype == xtype)
Daniel Veillard80b19092003-03-28 13:29:53 +00004716 return(xmlSchemaCompareDecimals(x, y));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004717 if ((ytype == XML_SCHEMAS_DECIMAL) ||
4718 (ytype == XML_SCHEMAS_INTEGER) ||
4719 (ytype == XML_SCHEMAS_NPINTEGER) ||
4720 (ytype == XML_SCHEMAS_NINTEGER) ||
4721 (ytype == XML_SCHEMAS_NNINTEGER) ||
4722 (ytype == XML_SCHEMAS_PINTEGER) ||
4723 (ytype == XML_SCHEMAS_INT) ||
4724 (ytype == XML_SCHEMAS_UINT) ||
4725 (ytype == XML_SCHEMAS_LONG) ||
4726 (ytype == XML_SCHEMAS_ULONG) ||
4727 (ytype == XML_SCHEMAS_SHORT) ||
4728 (ytype == XML_SCHEMAS_USHORT) ||
4729 (ytype == XML_SCHEMAS_BYTE) ||
4730 (ytype == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00004731 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004732 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00004733 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004734 if ((x == NULL) || (y == NULL))
4735 return(-2);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004736 if (ytype == XML_SCHEMAS_DURATION)
Daniel Veillard070803b2002-05-03 07:29:38 +00004737 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004738 return(-2);
4739 case XML_SCHEMAS_TIME:
4740 case XML_SCHEMAS_GDAY:
4741 case XML_SCHEMAS_GMONTH:
4742 case XML_SCHEMAS_GMONTHDAY:
4743 case XML_SCHEMAS_GYEAR:
4744 case XML_SCHEMAS_GYEARMONTH:
4745 case XML_SCHEMAS_DATE:
4746 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004747 if ((x == NULL) || (y == NULL))
4748 return(-2);
4749 if ((ytype == XML_SCHEMAS_DATETIME) ||
4750 (ytype == XML_SCHEMAS_TIME) ||
4751 (ytype == XML_SCHEMAS_GDAY) ||
4752 (ytype == XML_SCHEMAS_GMONTH) ||
4753 (ytype == XML_SCHEMAS_GMONTHDAY) ||
4754 (ytype == XML_SCHEMAS_GYEAR) ||
4755 (ytype == XML_SCHEMAS_DATE) ||
4756 (ytype == XML_SCHEMAS_GYEARMONTH))
Daniel Veillard5a872412002-05-22 06:40:27 +00004757 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004758 return (-2);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004759 /*
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +00004760 * Note that we will support comparison of string types against
4761 * anySimpleType as well.
4762 */
4763 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004764 case XML_SCHEMAS_STRING:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004765 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00004766 case XML_SCHEMAS_TOKEN:
4767 case XML_SCHEMAS_LANGUAGE:
4768 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00004769 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00004770 case XML_SCHEMAS_NCNAME:
4771 case XML_SCHEMAS_ID:
4772 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00004773 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00004774 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004775 {
4776 const xmlChar *xv, *yv;
4777
4778 if (x == NULL)
4779 xv = xvalue;
4780 else
4781 xv = x->value.str;
4782 if (y == NULL)
4783 yv = yvalue;
4784 else
4785 yv = y->value.str;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004786 /*
4787 * TODO: Compare those against QName.
4788 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004789 if (ytype == XML_SCHEMAS_QNAME) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004790 TODO
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004791 if (y == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004792 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004793 return (-2);
4794 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004795 if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
4796 (ytype == XML_SCHEMAS_STRING) ||
4797 (ytype == XML_SCHEMAS_NORMSTRING) ||
4798 (ytype == XML_SCHEMAS_TOKEN) ||
4799 (ytype == XML_SCHEMAS_LANGUAGE) ||
4800 (ytype == XML_SCHEMAS_NMTOKEN) ||
4801 (ytype == XML_SCHEMAS_NAME) ||
4802 (ytype == XML_SCHEMAS_NCNAME) ||
4803 (ytype == XML_SCHEMAS_ID) ||
4804 (ytype == XML_SCHEMAS_IDREF) ||
4805 (ytype == XML_SCHEMAS_ENTITY) ||
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004806 (ytype == XML_SCHEMAS_ANYURI)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004807
4808 if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4809
4810 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4811 /* TODO: What about x < y or x > y. */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004812 if (xmlStrEqual(xv, yv))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004813 return (0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004814 else
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004815 return (2);
4816 } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004817 return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004818 else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004819 return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004820
4821 } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
4822
4823 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004824 return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004825 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004826 return (xmlSchemaCompareReplacedStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004827 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004828 return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004829
4830 } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
4831
4832 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004833 return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004834 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004835 return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004836 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004837 return (xmlSchemaCompareNormStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004838 } else
4839 return (-2);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004840
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004841 }
Daniel Veillardc4c21552003-03-29 10:53:38 +00004842 return (-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004843 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004844 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004845 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004846 if ((x == NULL) || (y == NULL))
4847 return(-2);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004848 if ((ytype == XML_SCHEMAS_QNAME) ||
4849 (ytype == XML_SCHEMAS_NOTATION)) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004850 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
4851 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
4852 return(0);
4853 return(2);
4854 }
4855 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004856 case XML_SCHEMAS_FLOAT:
4857 case XML_SCHEMAS_DOUBLE:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004858 if ((x == NULL) || (y == NULL))
4859 return(-2);
4860 if ((ytype == XML_SCHEMAS_FLOAT) ||
4861 (ytype == XML_SCHEMAS_DOUBLE))
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004862 return (xmlSchemaCompareFloats(x, y));
4863 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004864 case XML_SCHEMAS_BOOLEAN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004865 if ((x == NULL) || (y == NULL))
4866 return(-2);
4867 if (ytype == XML_SCHEMAS_BOOLEAN) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004868 if (x->value.b == y->value.b)
4869 return(0);
4870 if (x->value.b == 0)
4871 return(-1);
4872 return(1);
4873 }
4874 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00004875 case XML_SCHEMAS_HEXBINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004876 if ((x == NULL) || (y == NULL))
4877 return(-2);
4878 if (ytype == XML_SCHEMAS_HEXBINARY) {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00004879 if (x->value.hex.total == y->value.hex.total) {
4880 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
4881 if (ret > 0)
4882 return(1);
4883 else if (ret == 0)
4884 return(0);
4885 }
4886 else if (x->value.hex.total > y->value.hex.total)
4887 return(1);
4888
4889 return(-1);
4890 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00004891 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004892 case XML_SCHEMAS_BASE64BINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004893 if ((x == NULL) || (y == NULL))
4894 return(-2);
4895 if (ytype == XML_SCHEMAS_BASE64BINARY) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004896 if (x->value.base64.total == y->value.base64.total) {
4897 int ret = xmlStrcmp(x->value.base64.str,
4898 y->value.base64.str);
4899 if (ret > 0)
4900 return(1);
4901 else if (ret == 0)
4902 return(0);
Daniel Veillardd369b132005-07-14 15:54:44 +00004903 else
4904 return(-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004905 }
4906 else if (x->value.base64.total > y->value.base64.total)
4907 return(1);
4908 else
4909 return(-1);
4910 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004911 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004912 case XML_SCHEMAS_IDREFS:
4913 case XML_SCHEMAS_ENTITIES:
4914 case XML_SCHEMAS_NMTOKENS:
4915 TODO
4916 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004917 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004918 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00004919}
4920
4921/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004922 * xmlSchemaCompareValues:
4923 * @x: a first value
4924 * @y: a second value
4925 *
4926 * Compare 2 values
4927 *
4928 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4929 * case of error
4930 */
4931int
4932xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4933 xmlSchemaWhitespaceValueType xws, yws;
4934
Daniel Veillard5e094142005-02-18 19:36:12 +00004935 if ((x == NULL) || (y == NULL))
4936 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004937 if (x->type == XML_SCHEMAS_STRING)
4938 xws = XML_SCHEMA_WHITESPACE_PRESERVE;
4939 else if (x->type == XML_SCHEMAS_NORMSTRING)
4940 xws = XML_SCHEMA_WHITESPACE_REPLACE;
4941 else
4942 xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4943
4944 if (y->type == XML_SCHEMAS_STRING)
4945 yws = XML_SCHEMA_WHITESPACE_PRESERVE;
Gaurave79226c2013-11-28 22:50:57 +08004946 else if (y->type == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004947 yws = XML_SCHEMA_WHITESPACE_REPLACE;
4948 else
4949 yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4950
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004951 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4952 y, NULL, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004953}
4954
4955/**
4956 * xmlSchemaCompareValuesWhtsp:
4957 * @x: a first value
4958 * @xws: the whitespace value of x
4959 * @y: a second value
4960 * @yws: the whitespace value of y
4961 *
4962 * Compare 2 values
4963 *
4964 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4965 * case of error
4966 */
4967int
4968xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004969 xmlSchemaWhitespaceValueType xws,
4970 xmlSchemaValPtr y,
4971 xmlSchemaWhitespaceValueType yws)
4972{
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00004973 if ((x == NULL) || (y == NULL))
4974 return(-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004975 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4976 y, NULL, yws));
4977}
4978
4979/**
4980 * xmlSchemaCompareValuesWhtspExt:
4981 * @x: a first value
4982 * @xws: the whitespace value of x
4983 * @y: a second value
4984 * @yws: the whitespace value of y
4985 *
4986 * Compare 2 values
4987 *
4988 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4989 * case of error
4990 */
4991static int
4992xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
4993 xmlSchemaValPtr x,
4994 const xmlChar *xvalue,
4995 xmlSchemaWhitespaceValueType xws,
4996 xmlSchemaValType ytype,
4997 xmlSchemaValPtr y,
4998 const xmlChar *yvalue,
4999 xmlSchemaWhitespaceValueType yws)
5000{
5001 return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
5002 yvalue, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005003}
5004
5005/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00005006 * xmlSchemaNormLen:
5007 * @value: a string
5008 *
5009 * Computes the UTF8 length of the normalized value of the string
5010 *
5011 * Returns the length or -1 in case of error.
5012 */
5013static int
5014xmlSchemaNormLen(const xmlChar *value) {
5015 const xmlChar *utf;
5016 int ret = 0;
5017
5018 if (value == NULL)
5019 return(-1);
5020 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00005021 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00005022 while (*utf != 0) {
5023 if (utf[0] & 0x80) {
5024 if ((utf[1] & 0xc0) != 0x80)
5025 return(-1);
5026 if ((utf[0] & 0xe0) == 0xe0) {
5027 if ((utf[2] & 0xc0) != 0x80)
5028 return(-1);
5029 if ((utf[0] & 0xf0) == 0xf0) {
5030 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
5031 return(-1);
5032 utf += 4;
5033 } else {
5034 utf += 3;
5035 }
5036 } else {
5037 utf += 2;
5038 }
William M. Brack76e95df2003-10-18 16:20:14 +00005039 } else if (IS_BLANK_CH(*utf)) {
5040 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00005041 if (*utf == 0)
5042 break;
5043 } else {
5044 utf++;
5045 }
5046 ret++;
5047 }
5048 return(ret);
5049}
5050
Daniel Veillard6927b102004-10-27 17:29:04 +00005051/**
5052 * xmlSchemaGetFacetValueAsULong:
5053 * @facet: an schemas type facet
5054 *
5055 * Extract the value of a facet
5056 *
5057 * Returns the value as a long
5058 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005059unsigned long
5060xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
5061{
5062 /*
5063 * TODO: Check if this is a decimal.
5064 */
William M. Brack094dd862004-11-14 14:28:34 +00005065 if (facet == NULL)
5066 return 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005067 return ((unsigned long) facet->val->value.decimal.lo);
5068}
5069
Daniel Veillardc4c21552003-03-29 10:53:38 +00005070/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00005071 * xmlSchemaValidateListSimpleTypeFacet:
5072 * @facet: the facet to check
5073 * @value: the lexical repr of the value to validate
5074 * @actualLen: the number of list items
5075 * @expectedLen: the resulting expected number of list items
5076 *
5077 * Checks the value of a list simple type against a facet.
5078 *
5079 * Returns 0 if the value is valid, a positive error code
5080 * number otherwise and -1 in case of an internal error.
5081 */
5082int
5083xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
5084 const xmlChar *value,
5085 unsigned long actualLen,
5086 unsigned long *expectedLen)
5087{
Daniel Veillardce682bc2004-11-05 17:22:25 +00005088 if (facet == NULL)
5089 return(-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005090 /*
5091 * TODO: Check if this will work with large numbers.
5092 * (compare value.decimal.mi and value.decimal.hi as well?).
5093 */
5094 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5095 if (actualLen != facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005096 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005097 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005098 return (XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005099 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005100 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5101 if (actualLen < facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005102 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005103 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005104 return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
5105 }
5106 } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
5107 if (actualLen > facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005108 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005109 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005110 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5111 }
5112 } else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005113 /*
5114 * NOTE: That we can pass NULL as xmlSchemaValPtr to
Daniel Veillard01fa6152004-06-29 17:04:39 +00005115 * xmlSchemaValidateFacet, since the remaining facet types
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005116 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
Daniel Veillard01fa6152004-06-29 17:04:39 +00005117 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005118 return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
Daniel Veillard01fa6152004-06-29 17:04:39 +00005119 return (0);
5120}
5121
5122/**
Daniel Veillard6927b102004-10-27 17:29:04 +00005123 * xmlSchemaValidateLengthFacet:
Daniel Veillardc0826a72004-08-10 14:17:33 +00005124 * @type: the built-in type
5125 * @facet: the facet to check
5126 * @value: the lexical repr. of the value to be validated
5127 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005128 * @ws: the whitespace type of the value
5129 * @length: the actual length of the value
5130 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005131 * Checka a value against a "length", "minLength" and "maxLength"
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005132 * facet; sets @length to the computed length of @value.
5133 *
5134 * Returns 0 if the value is valid, a positive error code
5135 * otherwise and -1 in case of an internal or API error.
5136 */
5137static int
5138xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
Nico Weberaae48e62012-02-29 09:44:35 +08005139 xmlSchemaValType valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005140 const xmlChar *value,
Nico Weberaae48e62012-02-29 09:44:35 +08005141 xmlSchemaValPtr val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005142 unsigned long *length,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005143 xmlSchemaWhitespaceValueType ws)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005144{
5145 unsigned int len = 0;
5146
5147 if ((length == NULL) || (facet == NULL))
5148 return (-1);
5149 *length = 0;
5150 if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
5151 (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
5152 (facet->type != XML_SCHEMA_FACET_MINLENGTH))
5153 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005154
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005155 /*
5156 * TODO: length, maxLength and minLength must be of type
5157 * nonNegativeInteger only. Check if decimal is used somehow.
5158 */
5159 if ((facet->val == NULL) ||
5160 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5161 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5162 (facet->val->value.decimal.frac != 0)) {
5163 return(-1);
5164 }
5165 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
5166 len = val->value.hex.total;
5167 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5168 len = val->value.base64.total;
5169 else {
5170 switch (valType) {
5171 case XML_SCHEMAS_STRING:
5172 case XML_SCHEMAS_NORMSTRING:
5173 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5174 /*
5175 * This is to ensure API compatibility with the old
5176 * xmlSchemaValidateLengthFacet(). Anyway, this was and
5177 * is not the correct handling.
5178 * TODO: Get rid of this case somehow.
5179 */
5180 if (valType == XML_SCHEMAS_STRING)
5181 len = xmlUTF8Strlen(value);
5182 else
5183 len = xmlSchemaNormLen(value);
5184 } else if (value != NULL) {
5185 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5186 len = xmlSchemaNormLen(value);
5187 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005188 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005189 * Should be OK for "preserve" as well.
5190 */
5191 len = xmlUTF8Strlen(value);
5192 }
5193 break;
5194 case XML_SCHEMAS_IDREF:
5195 case XML_SCHEMAS_TOKEN:
5196 case XML_SCHEMAS_LANGUAGE:
5197 case XML_SCHEMAS_NMTOKEN:
5198 case XML_SCHEMAS_NAME:
5199 case XML_SCHEMAS_NCNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005200 case XML_SCHEMAS_ID:
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005201 /*
5202 * FIXME: What exactly to do with anyURI?
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005203 */
5204 case XML_SCHEMAS_ANYURI:
5205 if (value != NULL)
5206 len = xmlSchemaNormLen(value);
5207 break;
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005208 case XML_SCHEMAS_QNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005209 case XML_SCHEMAS_NOTATION:
5210 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005211 * For QName and NOTATION, those facets are
5212 * deprecated and should be ignored.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005213 */
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00005214 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005215 default:
5216 TODO
5217 }
5218 }
5219 *length = (unsigned long) len;
5220 /*
5221 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
5222 */
5223 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
5224 if (len != facet->val->value.decimal.lo)
5225 return(XML_SCHEMAV_CVC_LENGTH_VALID);
5226 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
5227 if (len < facet->val->value.decimal.lo)
5228 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
5229 } else {
5230 if (len > facet->val->value.decimal.lo)
5231 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
5232 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005233
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005234 return (0);
5235}
5236
5237/**
5238 * xmlSchemaValidateLengthFacet:
5239 * @type: the built-in type
5240 * @facet: the facet to check
5241 * @value: the lexical repr. of the value to be validated
5242 * @val: the precomputed value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005243 * @length: the actual length of the value
5244 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005245 * Checka a value against a "length", "minLength" and "maxLength"
Daniel Veillardc0826a72004-08-10 14:17:33 +00005246 * facet; sets @length to the computed length of @value.
5247 *
5248 * Returns 0 if the value is valid, a positive error code
5249 * otherwise and -1 in case of an internal or API error.
5250 */
5251int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005252xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005253 xmlSchemaFacetPtr facet,
5254 const xmlChar *value,
5255 xmlSchemaValPtr val,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005256 unsigned long *length)
Daniel Veillardc0826a72004-08-10 14:17:33 +00005257{
Daniel Veillardcc5e2332005-03-16 21:55:35 +00005258 if (type == NULL)
5259 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005260 return (xmlSchemaValidateLengthFacetInternal(facet,
5261 type->builtInType, value, val, length,
5262 XML_SCHEMA_WHITESPACE_UNKNOWN));
5263}
Daniel Veillardc0826a72004-08-10 14:17:33 +00005264
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005265/**
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005266 * xmlSchemaValidateLengthFacetWhtsp:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005267 * @facet: the facet to check
5268 * @valType: the built-in type
5269 * @value: the lexical repr. of the value to be validated
5270 * @val: the precomputed value
5271 * @ws: the whitespace type of the value
5272 * @length: the actual length of the value
5273 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005274 * Checka a value against a "length", "minLength" and "maxLength"
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005275 * facet; sets @length to the computed length of @value.
5276 *
5277 * Returns 0 if the value is valid, a positive error code
5278 * otherwise and -1 in case of an internal or API error.
5279 */
5280int
5281xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
5282 xmlSchemaValType valType,
5283 const xmlChar *value,
5284 xmlSchemaValPtr val,
5285 unsigned long *length,
5286 xmlSchemaWhitespaceValueType ws)
5287{
5288 return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
5289 length, ws));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005290}
5291
5292/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005293 * xmlSchemaValidateFacetInternal:
Daniel Veillard4255d502002-04-16 15:50:10 +00005294 * @facet: the facet to check
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005295 * @fws: the whitespace type of the facet's value
5296 * @valType: the built-in type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005297 * @value: the lexical repr of the value to validate
5298 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005299 * @ws: the whitespace type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005300 *
5301 * Check a value against a facet condition
5302 *
5303 * Returns 0 if the element is schemas valid, a positive error code
5304 * number otherwise and -1 in case of internal or API error.
5305 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005306static int
5307xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
5308 xmlSchemaWhitespaceValueType fws,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005309 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005310 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005311 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005312 xmlSchemaWhitespaceValueType ws)
Daniel Veillard4255d502002-04-16 15:50:10 +00005313{
5314 int ret;
Patrick Monnerat90da33c2016-05-03 21:37:52 +08005315 int stringType;
Daniel Veillard4255d502002-04-16 15:50:10 +00005316
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005317 if (facet == NULL)
5318 return(-1);
5319
Daniel Veillard4255d502002-04-16 15:50:10 +00005320 switch (facet->type) {
5321 case XML_SCHEMA_FACET_PATTERN:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005322 /*
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005323 * NOTE that for patterns, the @value needs to be the normalized
5324 * value, *not* the lexical initial value or the canonical value.
5325 */
5326 if (value == NULL)
5327 return(-1);
Audric Schiltknechtcad102b2016-04-15 22:41:24 +08005328 /*
5329 * If string-derived type, regexp must be tested on the value space of
5330 * the datatype.
5331 * See https://www.w3.org/TR/xmlschema-2/#rf-pattern
5332 */
Patrick Monnerat90da33c2016-05-03 21:37:52 +08005333 stringType = val && ((val->type >= XML_SCHEMAS_STRING && val->type <= XML_SCHEMAS_NORMSTRING)
5334 || (val->type >= XML_SCHEMAS_TOKEN && val->type <= XML_SCHEMAS_NCNAME));
Audric Schiltknechtcad102b2016-04-15 22:41:24 +08005335 ret = xmlRegexpExec(facet->regexp,
5336 (stringType && val->value.str) ? val->value.str : value);
Daniel Veillard4255d502002-04-16 15:50:10 +00005337 if (ret == 1)
5338 return(0);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005339 if (ret == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005340 return(XML_SCHEMAV_CVC_PATTERN_VALID);
Daniel Veillard4255d502002-04-16 15:50:10 +00005341 return(ret);
5342 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
5343 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005344 if (ret == -2)
Daniel Veillard4255d502002-04-16 15:50:10 +00005345 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005346 if (ret == -1)
5347 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005348 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005349 case XML_SCHEMA_FACET_MAXINCLUSIVE:
5350 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005351 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005352 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005353 if ((ret == -1) || (ret == 0))
5354 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005355 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005356 case XML_SCHEMA_FACET_MINEXCLUSIVE:
5357 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005358 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005359 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005360 if (ret == 1)
5361 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005362 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005363 case XML_SCHEMA_FACET_MININCLUSIVE:
5364 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005365 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005366 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005367 if ((ret == 1) || (ret == 0))
5368 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005369 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
Daniel Veillard8651f532002-04-17 09:06:27 +00005370 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005371 /* TODO whitespaces */
Daniel Veillard01fa6152004-06-29 17:04:39 +00005372 /*
5373 * NOTE: Whitespace should be handled to normalize
5374 * the value to be validated against a the facets;
5375 * not to normalize the value in-between.
5376 */
Daniel Veillard8651f532002-04-17 09:06:27 +00005377 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00005378 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005379 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5380 /*
5381 * This is to ensure API compatibility with the old
5382 * xmlSchemaValidateFacet().
5383 * TODO: Get rid of this case.
5384 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005385 if ((facet->value != NULL) &&
5386 (xmlStrEqual(facet->value, value)))
5387 return(0);
5388 } else {
5389 ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
5390 facet->val, facet->value, fws, valType, val,
5391 value, ws);
5392 if (ret == -2)
5393 return(-1);
5394 if (ret == 0)
5395 return(0);
5396 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005397 return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005398 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005399 /*
5400 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5401 * then any {value} is facet-valid."
5402 */
5403 if ((valType == XML_SCHEMAS_QNAME) ||
5404 (valType == XML_SCHEMAS_NOTATION))
5405 return (0);
5406 /* No break on purpose. */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005407 case XML_SCHEMA_FACET_MAXLENGTH:
5408 case XML_SCHEMA_FACET_MINLENGTH: {
5409 unsigned int len = 0;
5410
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005411 if ((valType == XML_SCHEMAS_QNAME) ||
5412 (valType == XML_SCHEMAS_NOTATION))
5413 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005414 /*
5415 * TODO: length, maxLength and minLength must be of type
5416 * nonNegativeInteger only. Check if decimal is used somehow.
5417 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005418 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005419 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5420 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005421 (facet->val->value.decimal.frac != 0)) {
5422 return(-1);
5423 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005424 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005425 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00005426 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5427 len = val->value.base64.total;
5428 else {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005429 switch (valType) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005430 case XML_SCHEMAS_STRING:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005431 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005432 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5433 /*
5434 * This is to ensure API compatibility with the old
5435 * xmlSchemaValidateFacet(). Anyway, this was and
5436 * is not the correct handling.
5437 * TODO: Get rid of this case somehow.
5438 */
5439 if (valType == XML_SCHEMAS_STRING)
5440 len = xmlUTF8Strlen(value);
5441 else
5442 len = xmlSchemaNormLen(value);
5443 } else if (value != NULL) {
5444 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5445 len = xmlSchemaNormLen(value);
5446 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005447 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005448 * Should be OK for "preserve" as well.
5449 */
5450 len = xmlUTF8Strlen(value);
5451 }
5452 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005453 case XML_SCHEMAS_IDREF:
Daniel Veillard560c2a42003-07-06 21:13:49 +00005454 case XML_SCHEMAS_TOKEN:
5455 case XML_SCHEMAS_LANGUAGE:
5456 case XML_SCHEMAS_NMTOKEN:
5457 case XML_SCHEMAS_NAME:
5458 case XML_SCHEMAS_NCNAME:
5459 case XML_SCHEMAS_ID:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005460 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005461 if (value != NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005462 len = xmlSchemaNormLen(value);
5463 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00005464 default:
5465 TODO
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005466 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005467 }
5468 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
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_LENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005471 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005472 if (len < facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005473 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005474 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005475 if (len > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005476 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005477 }
5478 break;
5479 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005480 case XML_SCHEMA_FACET_TOTALDIGITS:
5481 case XML_SCHEMA_FACET_FRACTIONDIGITS:
5482
5483 if ((facet->val == NULL) ||
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00005484 ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
Daniel Veillard560c2a42003-07-06 21:13:49 +00005485 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5486 (facet->val->value.decimal.frac != 0)) {
5487 return(-1);
5488 }
5489 if ((val == NULL) ||
5490 ((val->type != XML_SCHEMAS_DECIMAL) &&
5491 (val->type != XML_SCHEMAS_INTEGER) &&
5492 (val->type != XML_SCHEMAS_NPINTEGER) &&
5493 (val->type != XML_SCHEMAS_NINTEGER) &&
5494 (val->type != XML_SCHEMAS_NNINTEGER) &&
5495 (val->type != XML_SCHEMAS_PINTEGER) &&
5496 (val->type != XML_SCHEMAS_INT) &&
5497 (val->type != XML_SCHEMAS_UINT) &&
5498 (val->type != XML_SCHEMAS_LONG) &&
5499 (val->type != XML_SCHEMAS_ULONG) &&
5500 (val->type != XML_SCHEMAS_SHORT) &&
5501 (val->type != XML_SCHEMAS_USHORT) &&
5502 (val->type != XML_SCHEMAS_BYTE) &&
5503 (val->type != XML_SCHEMAS_UBYTE))) {
5504 return(-1);
5505 }
5506 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
5507 if (val->value.decimal.total > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005508 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005509
5510 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
5511 if (val->value.decimal.frac > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005512 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005513 }
5514 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00005515 default:
5516 TODO
5517 }
5518 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00005519
Daniel Veillard4255d502002-04-16 15:50:10 +00005520}
5521
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005522/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005523 * xmlSchemaValidateFacet:
5524 * @base: the base type
5525 * @facet: the facet to check
5526 * @value: the lexical repr of the value to validate
5527 * @val: the precomputed value
5528 *
5529 * Check a value against a facet condition
5530 *
5531 * Returns 0 if the element is schemas valid, a positive error code
5532 * number otherwise and -1 in case of internal or API error.
5533 */
5534int
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005535xmlSchemaValidateFacet(xmlSchemaTypePtr base,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005536 xmlSchemaFacetPtr facet,
5537 const xmlChar *value,
5538 xmlSchemaValPtr val)
5539{
5540 /*
5541 * This tries to ensure API compatibility regarding the old
5542 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5543 * xmlSchemaValidateFacetWhtsp().
5544 */
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005545 if (val != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005546 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005547 XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005548 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005549 else if (base != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005550 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005551 XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005552 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005553 return(-1);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005554}
5555
5556/**
5557 * xmlSchemaValidateFacetWhtsp:
5558 * @facet: the facet to check
5559 * @fws: the whitespace type of the facet's value
5560 * @valType: the built-in type of the value
5561 * @value: the lexical (or normalized for pattern) repr of the value to validate
5562 * @val: the precomputed value
5563 * @ws: the whitespace type of the value
5564 *
5565 * Check a value against a facet condition. This takes value normalization
5566 * according to the specified whitespace types into account.
5567 * Note that @value needs to be the *normalized* value if the facet
5568 * is of type "pattern".
5569 *
5570 * Returns 0 if the element is schemas valid, a positive error code
5571 * number otherwise and -1 in case of internal or API error.
5572 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005573int
5574xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
5575 xmlSchemaWhitespaceValueType fws,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005576 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005577 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005578 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005579 xmlSchemaWhitespaceValueType ws)
5580{
5581 return(xmlSchemaValidateFacetInternal(facet, fws, valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005582 value, val, ws));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005583}
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005584
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005585#if 0
5586#ifndef DBL_DIG
5587#define DBL_DIG 16
5588#endif
5589#ifndef DBL_EPSILON
5590#define DBL_EPSILON 1E-9
5591#endif
5592
5593#define INTEGER_DIGITS DBL_DIG
5594#define FRACTION_DIGITS (DBL_DIG + 1)
5595#define EXPONENT_DIGITS (3 + 2)
5596
5597/**
5598 * xmlXPathFormatNumber:
5599 * @number: number to format
5600 * @buffer: output buffer
5601 * @buffersize: size of output buffer
5602 *
5603 * Convert the number into a string representation.
5604 */
5605static void
5606xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
5607{
5608 switch (xmlXPathIsInf(number)) {
5609 case 1:
5610 if (buffersize > (int)sizeof("INF"))
5611 snprintf(buffer, buffersize, "INF");
5612 break;
5613 case -1:
5614 if (buffersize > (int)sizeof("-INF"))
5615 snprintf(buffer, buffersize, "-INF");
5616 break;
5617 default:
5618 if (xmlXPathIsNaN(number)) {
5619 if (buffersize > (int)sizeof("NaN"))
5620 snprintf(buffer, buffersize, "NaN");
5621 } else if (number == 0) {
5622 snprintf(buffer, buffersize, "0.0E0");
5623 } else {
5624 /* 3 is sign, decimal point, and terminating zero */
5625 char work[DBL_DIG + EXPONENT_DIGITS + 3];
5626 int integer_place, fraction_place;
5627 char *ptr;
5628 char *after_fraction;
5629 double absolute_value;
5630 int size;
5631
5632 absolute_value = fabs(number);
5633
5634 /*
5635 * Result is in work, and after_fraction points
5636 * just past the fractional part.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005637 * Use scientific notation
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005638 */
5639 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
5640 fraction_place = DBL_DIG - 1;
5641 snprintf(work, sizeof(work),"%*.*e",
5642 integer_place, fraction_place, number);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005643 after_fraction = strchr(work + DBL_DIG, 'e');
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005644 /* Remove fractional trailing zeroes */
5645 ptr = after_fraction;
5646 while (*(--ptr) == '0')
5647 ;
5648 if (*ptr != '.')
5649 ptr++;
5650 while ((*ptr++ = *after_fraction++) != 0);
5651
5652 /* Finally copy result back to caller */
5653 size = strlen(work) + 1;
5654 if (size > buffersize) {
5655 work[buffersize - 1] = 0;
5656 size = buffersize;
5657 }
5658 memmove(buffer, work, size);
5659 }
5660 break;
5661 }
5662}
5663#endif
5664
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005665/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005666 * xmlSchemaGetCanonValue:
5667 * @val: the precomputed value
5668 * @retValue: the returned value
5669 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01005670 * Get the canonical lexical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005671 * The caller has to FREE the returned retValue.
5672 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005673 * WARNING: Some value types are not supported yet, resulting
5674 * in a @retValue of "???".
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005675 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005676 * TODO: XML Schema 1.0 does not define canonical representations
5677 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5678 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005679 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005680 *
5681 * Returns 0 if the value could be built, 1 if the value type is
5682 * not supported yet and -1 in case of API errors.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005683 */
5684int
Daniel Veillardb5839c32005-02-19 18:27:14 +00005685xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005686{
Daniel Veillardb5839c32005-02-19 18:27:14 +00005687 if ((retValue == NULL) || (val == NULL))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005688 return (-1);
5689 *retValue = NULL;
5690 switch (val->type) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005691 case XML_SCHEMAS_STRING:
5692 if (val->value.str == NULL)
5693 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5694 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005695 *retValue =
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005696 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5697 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005698 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005699 if (val->value.str == NULL)
5700 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5701 else {
5702 *retValue = xmlSchemaWhiteSpaceReplace(
5703 (const xmlChar *) val->value.str);
5704 if ((*retValue) == NULL)
5705 *retValue = BAD_CAST xmlStrdup(
5706 (const xmlChar *) val->value.str);
5707 }
5708 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005709 case XML_SCHEMAS_TOKEN:
5710 case XML_SCHEMAS_LANGUAGE:
5711 case XML_SCHEMAS_NMTOKEN:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005712 case XML_SCHEMAS_NAME:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005713 case XML_SCHEMAS_NCNAME:
5714 case XML_SCHEMAS_ID:
5715 case XML_SCHEMAS_IDREF:
5716 case XML_SCHEMAS_ENTITY:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005717 case XML_SCHEMAS_NOTATION: /* Unclear */
5718 case XML_SCHEMAS_ANYURI: /* Unclear */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005719 if (val->value.str == NULL)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005720 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005721 *retValue =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005722 BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
5723 if (*retValue == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005724 *retValue =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005725 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005726 break;
5727 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005728 /* TODO: Unclear in XML Schema 1.0. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005729 if (val->value.qname.uri == NULL) {
5730 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
5731 return (0);
5732 } else {
5733 *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
5734 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5735 BAD_CAST val->value.qname.uri);
5736 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5737 BAD_CAST "}");
5738 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5739 BAD_CAST val->value.qname.uri);
5740 }
5741 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005742 case XML_SCHEMAS_DECIMAL:
5743 /*
5744 * TODO: Lookout for a more simple implementation.
5745 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005746 if ((val->value.decimal.total == 1) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005747 (val->value.decimal.lo == 0)) {
5748 *retValue = xmlStrdup(BAD_CAST "0.0");
5749 } else {
5750 xmlSchemaValDecimal dec = val->value.decimal;
5751 int bufsize;
5752 char *buf = NULL, *offs;
5753
5754 /* Add room for the decimal point as well. */
5755 bufsize = dec.total + 2;
5756 if (dec.sign)
5757 bufsize++;
5758 /* Add room for leading/trailing zero. */
5759 if ((dec.frac == 0) || (dec.frac == dec.total))
5760 bufsize++;
5761 buf = xmlMalloc(bufsize);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005762 if (buf == NULL)
5763 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005764 offs = buf;
5765 if (dec.sign)
5766 *offs++ = '-';
5767 if (dec.frac == dec.total) {
5768 *offs++ = '0';
5769 *offs++ = '.';
5770 }
5771 if (dec.hi != 0)
5772 snprintf(offs, bufsize - (offs - buf),
5773 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5774 else if (dec.mi != 0)
5775 snprintf(offs, bufsize - (offs - buf),
5776 "%lu%lu", dec.mi, dec.lo);
5777 else
5778 snprintf(offs, bufsize - (offs - buf),
5779 "%lu", dec.lo);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005780
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005781 if (dec.frac != 0) {
5782 if (dec.frac != dec.total) {
5783 int diff = dec.total - dec.frac;
5784 /*
5785 * Insert the decimal point.
5786 */
5787 memmove(offs + diff + 1, offs + diff, dec.frac +1);
5788 offs[diff] = '.';
5789 } else {
5790 unsigned int i = 0;
5791 /*
5792 * Insert missing zeroes behind the decimal point.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005793 */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005794 while (*(offs + i) != 0)
5795 i++;
5796 if (i < dec.total) {
5797 memmove(offs + (dec.total - i), offs, i +1);
5798 memset(offs, '0', dec.total - i);
5799 }
5800 }
5801 } else {
5802 /*
5803 * Append decimal point and zero.
5804 */
5805 offs = buf + bufsize - 1;
5806 *offs-- = 0;
5807 *offs-- = '0';
5808 *offs-- = '.';
5809 }
5810 *retValue = BAD_CAST buf;
5811 }
5812 break;
5813 case XML_SCHEMAS_INTEGER:
5814 case XML_SCHEMAS_PINTEGER:
5815 case XML_SCHEMAS_NPINTEGER:
5816 case XML_SCHEMAS_NINTEGER:
5817 case XML_SCHEMAS_NNINTEGER:
5818 case XML_SCHEMAS_LONG:
5819 case XML_SCHEMAS_BYTE:
5820 case XML_SCHEMAS_SHORT:
5821 case XML_SCHEMAS_INT:
5822 case XML_SCHEMAS_UINT:
5823 case XML_SCHEMAS_ULONG:
5824 case XML_SCHEMAS_USHORT:
5825 case XML_SCHEMAS_UBYTE:
5826 if ((val->value.decimal.total == 1) &&
5827 (val->value.decimal.lo == 0))
5828 *retValue = xmlStrdup(BAD_CAST "0");
5829 else {
5830 xmlSchemaValDecimal dec = val->value.decimal;
5831 int bufsize = dec.total + 1;
5832
5833 /* Add room for the decimal point as well. */
5834 if (dec.sign)
5835 bufsize++;
5836 *retValue = xmlMalloc(bufsize);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005837 if (*retValue == NULL)
5838 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005839 if (dec.hi != 0) {
5840 if (dec.sign)
5841 snprintf((char *) *retValue, bufsize,
5842 "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5843 else
5844 snprintf((char *) *retValue, bufsize,
5845 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5846 } else if (dec.mi != 0) {
5847 if (dec.sign)
5848 snprintf((char *) *retValue, bufsize,
5849 "-%lu%lu", dec.mi, dec.lo);
5850 else
5851 snprintf((char *) *retValue, bufsize,
5852 "%lu%lu", dec.mi, dec.lo);
5853 } else {
5854 if (dec.sign)
5855 snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
5856 else
5857 snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
5858 }
5859 }
5860 break;
5861 case XML_SCHEMAS_BOOLEAN:
5862 if (val->value.b)
5863 *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
5864 else
5865 *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
5866 break;
5867 case XML_SCHEMAS_DURATION: {
5868 char buf[100];
5869 unsigned long year;
5870 unsigned long mon, day, hour = 0, min = 0;
5871 double sec = 0, left;
5872
5873 /* TODO: Unclear in XML Schema 1.0 */
5874 /*
5875 * TODO: This results in a normalized output of the value
5876 * - which is NOT conformant to the spec -
5877 * since the exact values of each property are not
5878 * recoverable. Think about extending the structure to
5879 * provide a field for every property.
5880 */
5881 year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
5882 mon = labs(val->value.dur.mon) - 12 * year;
5883
5884 day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
5885 left = fabs(val->value.dur.sec) - day * 86400;
5886 if (left > 0) {
5887 hour = (unsigned long) FQUOTIENT(left, 3600);
5888 left = left - (hour * 3600);
5889 if (left > 0) {
5890 min = (unsigned long) FQUOTIENT(left, 60);
5891 sec = left - (min * 60);
5892 }
5893 }
5894 if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
5895 snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5896 year, mon, day, hour, min, sec);
5897 else
5898 snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5899 year, mon, day, hour, min, sec);
5900 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5901 }
5902 break;
5903 case XML_SCHEMAS_GYEAR: {
5904 char buf[30];
5905 /* TODO: Unclear in XML Schema 1.0 */
5906 /* TODO: What to do with the timezone? */
5907 snprintf(buf, 30, "%04ld", val->value.date.year);
5908 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5909 }
5910 break;
5911 case XML_SCHEMAS_GMONTH: {
5912 /* TODO: Unclear in XML Schema 1.0 */
5913 /* TODO: What to do with the timezone? */
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005914 *retValue = xmlMalloc(6);
5915 if (*retValue == NULL)
5916 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005917 snprintf((char *) *retValue, 6, "--%02u",
5918 val->value.date.mon);
5919 }
5920 break;
5921 case XML_SCHEMAS_GDAY: {
5922 /* TODO: Unclear in XML Schema 1.0 */
5923 /* TODO: What to do with the timezone? */
5924 *retValue = xmlMalloc(6);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005925 if (*retValue == NULL)
5926 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005927 snprintf((char *) *retValue, 6, "---%02u",
5928 val->value.date.day);
5929 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005930 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005931 case XML_SCHEMAS_GMONTHDAY: {
5932 /* TODO: Unclear in XML Schema 1.0 */
5933 /* TODO: What to do with the timezone? */
5934 *retValue = xmlMalloc(8);
Daniel Veillard26ab0e62006-10-11 12:32:51 +00005935 if (*retValue == NULL)
5936 return(-1);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005937 snprintf((char *) *retValue, 8, "--%02u-%02u",
5938 val->value.date.mon, val->value.date.day);
5939 }
5940 break;
5941 case XML_SCHEMAS_GYEARMONTH: {
5942 char buf[35];
5943 /* TODO: Unclear in XML Schema 1.0 */
5944 /* TODO: What to do with the timezone? */
5945 if (val->value.date.year < 0)
5946 snprintf(buf, 35, "-%04ld-%02u",
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005947 labs(val->value.date.year),
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005948 val->value.date.mon);
5949 else
5950 snprintf(buf, 35, "%04ld-%02u",
5951 val->value.date.year, val->value.date.mon);
5952 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5953 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005954 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005955 case XML_SCHEMAS_TIME:
5956 {
5957 char buf[30];
5958
5959 if (val->value.date.tz_flag) {
5960 xmlSchemaValPtr norm;
5961
5962 norm = xmlSchemaDateNormalize(val, 0);
5963 if (norm == NULL)
5964 return (-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005965 /*
5966 * TODO: Check if "%.14g" is portable.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005967 */
5968 snprintf(buf, 30,
5969 "%02u:%02u:%02.14gZ",
5970 norm->value.date.hour,
5971 norm->value.date.min,
5972 norm->value.date.sec);
5973 xmlSchemaFreeValue(norm);
5974 } else {
5975 snprintf(buf, 30,
5976 "%02u:%02u:%02.14g",
5977 val->value.date.hour,
5978 val->value.date.min,
5979 val->value.date.sec);
5980 }
5981 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005982 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005983 break;
5984 case XML_SCHEMAS_DATE:
5985 {
5986 char buf[30];
5987
5988 if (val->value.date.tz_flag) {
5989 xmlSchemaValPtr norm;
5990
5991 norm = xmlSchemaDateNormalize(val, 0);
5992 if (norm == NULL)
5993 return (-1);
5994 /*
5995 * TODO: Append the canonical value of the
5996 * recoverable timezone and not "Z".
5997 */
5998 snprintf(buf, 30,
5999 "%04ld:%02u:%02uZ",
6000 norm->value.date.year, norm->value.date.mon,
6001 norm->value.date.day);
6002 xmlSchemaFreeValue(norm);
6003 } else {
6004 snprintf(buf, 30,
6005 "%04ld:%02u:%02u",
6006 val->value.date.year, val->value.date.mon,
6007 val->value.date.day);
6008 }
6009 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006010 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006011 break;
6012 case XML_SCHEMAS_DATETIME:
6013 {
6014 char buf[50];
6015
6016 if (val->value.date.tz_flag) {
6017 xmlSchemaValPtr norm;
6018
6019 norm = xmlSchemaDateNormalize(val, 0);
6020 if (norm == NULL)
6021 return (-1);
6022 /*
6023 * TODO: Check if "%.14g" is portable.
6024 */
6025 snprintf(buf, 50,
6026 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
6027 norm->value.date.year, norm->value.date.mon,
6028 norm->value.date.day, norm->value.date.hour,
6029 norm->value.date.min, norm->value.date.sec);
6030 xmlSchemaFreeValue(norm);
6031 } else {
6032 snprintf(buf, 50,
6033 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
6034 val->value.date.year, val->value.date.mon,
6035 val->value.date.day, val->value.date.hour,
6036 val->value.date.min, val->value.date.sec);
6037 }
6038 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6039 }
6040 break;
6041 case XML_SCHEMAS_HEXBINARY:
6042 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
6043 break;
6044 case XML_SCHEMAS_BASE64BINARY:
6045 /*
6046 * TODO: Is the following spec piece implemented?:
6047 * SPEC: "Note: For some values the canonical form defined
6048 * above does not conform to [RFC 2045], which requires breaking
6049 * with linefeeds at appropriate intervals."
6050 */
6051 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
6052 break;
6053 case XML_SCHEMAS_FLOAT: {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006054 char buf[30];
6055 /*
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006056 * |m| < 16777216, -149 <= e <= 104.
6057 * TODO: Handle, NaN, INF, -INF. The format is not
6058 * yet conformant. The c type float does not cover
6059 * the whole range.
6060 */
6061 snprintf(buf, 30, "%01.14e", val->value.f);
6062 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6063 }
6064 break;
6065 case XML_SCHEMAS_DOUBLE: {
6066 char buf[40];
6067 /* |m| < 9007199254740992, -1075 <= e <= 970 */
6068 /*
6069 * TODO: Handle, NaN, INF, -INF. The format is not
6070 * yet conformant. The c type float does not cover
6071 * the whole range.
6072 */
6073 snprintf(buf, 40, "%01.14e", val->value.d);
6074 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
6075 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006076 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006077 default:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006078 *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006079 return (1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006080 }
Daniel Veillard26ab0e62006-10-11 12:32:51 +00006081 if (*retValue == NULL)
6082 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006083 return (0);
6084}
6085
Daniel Veillardbda59572005-04-01 17:15:17 +00006086/**
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006087 * xmlSchemaGetCanonValueWhtsp:
6088 * @val: the precomputed value
6089 * @retValue: the returned value
6090 * @ws: the whitespace type of the value
6091 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01006092 * Get the canonical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006093 * The caller has to free the returned @retValue.
6094 *
6095 * Returns 0 if the value could be built, 1 if the value type is
6096 * not supported yet and -1 in case of API errors.
6097 */
6098int
6099xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
6100 const xmlChar **retValue,
6101 xmlSchemaWhitespaceValueType ws)
6102{
6103 if ((retValue == NULL) || (val == NULL))
6104 return (-1);
6105 if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
6106 (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
6107 return (-1);
6108
6109 *retValue = NULL;
6110 switch (val->type) {
6111 case XML_SCHEMAS_STRING:
6112 if (val->value.str == NULL)
6113 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6114 else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6115 *retValue = xmlSchemaCollapseString(val->value.str);
6116 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
6117 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6118 if ((*retValue) == NULL)
6119 *retValue = BAD_CAST xmlStrdup(val->value.str);
6120 break;
6121 case XML_SCHEMAS_NORMSTRING:
6122 if (val->value.str == NULL)
6123 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
6124 else {
6125 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
6126 *retValue = xmlSchemaCollapseString(val->value.str);
6127 else
6128 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
6129 if ((*retValue) == NULL)
6130 *retValue = BAD_CAST xmlStrdup(val->value.str);
6131 }
6132 break;
6133 default:
6134 return (xmlSchemaGetCanonValue(val, retValue));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006135 }
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00006136 return (0);
6137}
6138
6139/**
Daniel Veillardbda59572005-04-01 17:15:17 +00006140 * xmlSchemaGetValType:
6141 * @val: a schemas value
6142 *
6143 * Accessor for the type of a value
6144 *
6145 * Returns the xmlSchemaValType of the value
6146 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006147xmlSchemaValType
6148xmlSchemaGetValType(xmlSchemaValPtr val)
6149{
Daniel Veillardbda59572005-04-01 17:15:17 +00006150 if (val == NULL)
6151 return(XML_SCHEMAS_UNKNOWN);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00006152 return (val->type);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00006153}
6154
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006155#define bottom_xmlschemastypes
6156#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00006157#endif /* LIBXML_SCHEMAS_ENABLED */