blob: 3e751fff9f279067962a89c4b70fdacf60a08fa5 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * schemastypes.c : implementation of the XML Schema Datatypes
3 * definition and validity checking
4 *
5 * See Copyright for the status of this software.
6 *
7 * Daniel Veillard <veillard@redhat.com>
8 */
9
10#define IN_LIBXML
11#include "libxml.h"
12
13#ifdef LIBXML_SCHEMAS_ENABLED
14
15#include <string.h>
16#include <libxml/xmlmemory.h>
17#include <libxml/parser.h>
18#include <libxml/parserInternals.h>
19#include <libxml/hash.h>
20#include <libxml/valid.h>
Daniel Veillard96a4b252003-02-06 08:22:32 +000021#include <libxml/xpath.h>
22#include <libxml/uri.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000023
24#include <libxml/xmlschemas.h>
25#include <libxml/schemasInternals.h>
26#include <libxml/xmlschemastypes.h>
27
Daniel Veillard070803b2002-05-03 07:29:38 +000028#ifdef HAVE_MATH_H
29#include <math.h>
30#endif
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000031#ifdef HAVE_FLOAT_H
32#include <float.h>
33#endif
Daniel Veillard070803b2002-05-03 07:29:38 +000034
Daniel Veillard4255d502002-04-16 15:50:10 +000035#define DEBUG
36
Daniel Veillardd0cf7f62004-11-09 16:17:02 +000037#ifndef LIBXML_XPATH_ENABLED
38extern double xmlXPathNAN;
39extern double xmlXPathPINF;
40extern double xmlXPathNINF;
41#endif
42
Daniel Veillard4255d502002-04-16 15:50:10 +000043#define TODO \
44 xmlGenericError(xmlGenericErrorContext, \
45 "Unimplemented block at %s:%d\n", \
46 __FILE__, __LINE__);
47
48#define XML_SCHEMAS_NAMESPACE_NAME \
49 (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
50
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000051#define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \
52 ((c) == 0xd))
53
54#define IS_WSP_SPACE_CH(c) ((c) == 0x20)
55
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000056#define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c)
57
Daniel Veillard070803b2002-05-03 07:29:38 +000058/* Date value */
59typedef struct _xmlSchemaValDate xmlSchemaValDate;
60typedef xmlSchemaValDate *xmlSchemaValDatePtr;
61struct _xmlSchemaValDate {
62 long year;
63 unsigned int mon :4; /* 1 <= mon <= 12 */
64 unsigned int day :5; /* 1 <= day <= 31 */
65 unsigned int hour :5; /* 0 <= hour <= 23 */
66 unsigned int min :6; /* 0 <= min <= 59 */
67 double sec;
Daniel Veillarda77cf712003-05-09 23:09:55 +000068 unsigned int tz_flag :1; /* is tzo explicitely set? */
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +000069 signed int tzo :12; /* -1440 <= tzo <= 1440;
70 currently only -840 to +840 are needed */
Daniel Veillard070803b2002-05-03 07:29:38 +000071};
72
73/* Duration value */
74typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
75typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
76struct _xmlSchemaValDuration {
77 long mon; /* mon stores years also */
78 long day;
79 double sec; /* sec stores min and hour also */
80};
81
Daniel Veillard4255d502002-04-16 15:50:10 +000082typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
83typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
84struct _xmlSchemaValDecimal {
85 /* would use long long but not portable */
Daniel Veillarde637c4a2003-03-30 21:10:09 +000086 unsigned long lo;
87 unsigned long mi;
88 unsigned long hi;
Daniel Veillard4255d502002-04-16 15:50:10 +000089 unsigned int extra;
Daniel Veillard5a872412002-05-22 06:40:27 +000090 unsigned int sign:1;
William M. Brackc1939562003-08-05 15:52:22 +000091 unsigned int frac:7;
92 unsigned int total:8;
Daniel Veillard4255d502002-04-16 15:50:10 +000093};
94
Daniel Veillarde637c4a2003-03-30 21:10:09 +000095typedef struct _xmlSchemaValQName xmlSchemaValQName;
96typedef xmlSchemaValQName *xmlSchemaValQNamePtr;
97struct _xmlSchemaValQName {
98 xmlChar *name;
99 xmlChar *uri;
100};
101
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000102typedef struct _xmlSchemaValHex xmlSchemaValHex;
103typedef xmlSchemaValHex *xmlSchemaValHexPtr;
104struct _xmlSchemaValHex {
105 xmlChar *str;
106 unsigned int total;
107};
108
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000109typedef struct _xmlSchemaValBase64 xmlSchemaValBase64;
110typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr;
111struct _xmlSchemaValBase64 {
112 xmlChar *str;
113 unsigned int total;
114};
115
Daniel Veillard4255d502002-04-16 15:50:10 +0000116struct _xmlSchemaVal {
117 xmlSchemaValType type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000118 struct _xmlSchemaVal *next;
Daniel Veillard4255d502002-04-16 15:50:10 +0000119 union {
Daniel Veillard5a872412002-05-22 06:40:27 +0000120 xmlSchemaValDecimal decimal;
Daniel Veillard070803b2002-05-03 07:29:38 +0000121 xmlSchemaValDate date;
122 xmlSchemaValDuration dur;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000123 xmlSchemaValQName qname;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000124 xmlSchemaValHex hex;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000125 xmlSchemaValBase64 base64;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000126 float f;
127 double d;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000128 int b;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000129 xmlChar *str;
Daniel Veillard4255d502002-04-16 15:50:10 +0000130 } value;
131};
132
133static int xmlSchemaTypesInitialized = 0;
134static xmlHashTablePtr xmlSchemaTypesBank = NULL;
135
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000136/*
137 * Basic types
138 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000139static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
140static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
141static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
142static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000143static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000144static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000145static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL;
146static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL;
147static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL;
148static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL;
149static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL;
150static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL;
151static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000152static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000153static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000154static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
Daniel Veillard560c2a42003-07-06 21:13:49 +0000155static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000156static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000157static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000158
159/*
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000160 * Derived types
161 */
162static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
163static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL;
164static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL;
165static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
166static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL;
167static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL;
168static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL;
169static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL;
170static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL;
171static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL;
172static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL;
173static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL;
174static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000175static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL;
176static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL;
177static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL;
178static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL;
179static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000180static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000181static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
182static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
183static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000184static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL;
185static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000186static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000187static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
188static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000189
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000190/************************************************************************
191 * *
192 * Datatype error handlers *
193 * *
194 ************************************************************************/
195/**
196 * xmlSchemaTypeErrMemory:
197 * @extra: extra informations
198 *
199 * Handle an out of memory condition
200 */
201static void
202xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra)
203{
204 __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra);
205}
206
207/************************************************************************
208 * *
209 * Base types support *
210 * *
211 ************************************************************************/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000212
213/**
214 * xmlSchemaNewValue:
215 * @type: the value type
216 *
217 * Allocate a new simple type value
218 *
219 * Returns a pointer to the new value or NULL in case of error
220 */
221static xmlSchemaValPtr
222xmlSchemaNewValue(xmlSchemaValType type) {
223 xmlSchemaValPtr value;
224
225 value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
226 if (value == NULL) {
227 return(NULL);
228 }
229 memset(value, 0, sizeof(xmlSchemaVal));
230 value->type = type;
231 return(value);
232}
233
234static xmlSchemaFacetPtr
235xmlSchemaNewMinLengthFacet(int value)
236{
237 xmlSchemaFacetPtr ret;
238
239 ret = xmlSchemaNewFacet();
240 ret->type = XML_SCHEMA_FACET_MINLENGTH;
241 ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER);
242 ret->val->value.decimal.lo = value;
243 return (ret);
244}
245
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000246/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000247 * xmlSchemaInitBasicType:
248 * @name: the type name
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000249 * @type: the value type associated
Daniel Veillard4255d502002-04-16 15:50:10 +0000250 *
Daniel Veillard01fa6152004-06-29 17:04:39 +0000251 * Initialize one primitive built-in type
Daniel Veillard4255d502002-04-16 15:50:10 +0000252 */
253static xmlSchemaTypePtr
Daniel Veillard01fa6152004-06-29 17:04:39 +0000254xmlSchemaInitBasicType(const char *name, xmlSchemaValType type,
255 xmlSchemaTypePtr baseType) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000256 xmlSchemaTypePtr ret;
257
258 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
259 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000260 xmlSchemaTypeErrMemory(NULL, "could not initialize basic types");
Daniel Veillard4255d502002-04-16 15:50:10 +0000261 return(NULL);
262 }
263 memset(ret, 0, sizeof(xmlSchemaType));
Daniel Veillardbe9c6322003-11-22 20:37:51 +0000264 ret->name = (const xmlChar *)name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000265 ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME;
Daniel Veillard4255d502002-04-16 15:50:10 +0000266 ret->type = XML_SCHEMA_TYPE_BASIC;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000267 ret->baseType = baseType;
Daniel Veillard4255d502002-04-16 15:50:10 +0000268 ret->contentType = XML_SCHEMA_CONTENT_BASIC;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000269 /*
270 * Primitive types.
271 */
272 switch (type) {
Daniel Veillard01fa6152004-06-29 17:04:39 +0000273 case XML_SCHEMAS_STRING:
274 case XML_SCHEMAS_DECIMAL:
275 case XML_SCHEMAS_DATE:
276 case XML_SCHEMAS_DATETIME:
277 case XML_SCHEMAS_TIME:
278 case XML_SCHEMAS_GYEAR:
279 case XML_SCHEMAS_GYEARMONTH:
280 case XML_SCHEMAS_GMONTH:
281 case XML_SCHEMAS_GMONTHDAY:
282 case XML_SCHEMAS_GDAY:
283 case XML_SCHEMAS_DURATION:
284 case XML_SCHEMAS_FLOAT:
285 case XML_SCHEMAS_DOUBLE:
286 case XML_SCHEMAS_BOOLEAN:
287 case XML_SCHEMAS_ANYURI:
288 case XML_SCHEMAS_HEXBINARY:
289 case XML_SCHEMAS_BASE64BINARY:
290 case XML_SCHEMAS_QNAME:
291 case XML_SCHEMAS_NOTATION:
292 ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000293 break;
William M. Brack96d2eff2004-06-30 11:48:47 +0000294 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000295 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000296 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000297 /*
298 * Set variety.
299 */
300 switch (type) {
301 case XML_SCHEMAS_ANYTYPE:
302 case XML_SCHEMAS_ANYSIMPLETYPE:
303 break;
304 case XML_SCHEMAS_IDREFS:
305 case XML_SCHEMAS_NMTOKENS:
306 case XML_SCHEMAS_ENTITIES:
307 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
308 ret->facets = xmlSchemaNewMinLengthFacet(1);
309 ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
310 break;
311 default:
312 ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
313 break;
314 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000315 xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
316 XML_SCHEMAS_NAMESPACE_NAME, ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000317 ret->builtInType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +0000318 return(ret);
319}
320
321/*
322 * xmlSchemaInitTypes:
323 *
324 * Initialize the default XML Schemas type library
325 */
326void
Daniel Veillard6560a422003-03-27 21:25:38 +0000327xmlSchemaInitTypes(void)
328{
Daniel Veillard4255d502002-04-16 15:50:10 +0000329 if (xmlSchemaTypesInitialized != 0)
Daniel Veillard6560a422003-03-27 21:25:38 +0000330 return;
Daniel Veillard4255d502002-04-16 15:50:10 +0000331 xmlSchemaTypesBank = xmlHashCreate(40);
Daniel Veillard6560a422003-03-27 21:25:38 +0000332
Daniel Veillard01fa6152004-06-29 17:04:39 +0000333
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000334 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +0000335 * 3.4.7 Built-in Complex Type Definition
336 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000337 xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
William M. Brack2f2a6632004-08-20 23:09:47 +0000338 XML_SCHEMAS_ANYTYPE,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000339 NULL);
340 xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef;
341 xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
342 {
343 xmlSchemaWildcardPtr wild;
344
345 wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
346 if (wild == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +0000347 xmlSchemaTypeErrMemory(NULL, "could not create an attribute wildcard on anyType");
Daniel Veillard01fa6152004-06-29 17:04:39 +0000348 return;
349 }
350 memset(wild, 0, sizeof(xmlSchemaWildcard));
351 wild->any = 1;
352 wild->processContents = XML_SCHEMAS_ANY_LAX;
353 wild->minOccurs = 1;
354 wild->maxOccurs = 1;
355 xmlSchemaTypeAnyTypeDef->attributeWildcard = wild;
356 }
357 xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
William M. Brack2f2a6632004-08-20 23:09:47 +0000358 XML_SCHEMAS_ANYSIMPLETYPE,
Daniel Veillard01fa6152004-06-29 17:04:39 +0000359 xmlSchemaTypeAnyTypeDef);
360 /*
361 * primitive datatypes
362 */
363 xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
364 XML_SCHEMAS_STRING,
365 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000366 xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000367 XML_SCHEMAS_DECIMAL,
368 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000369 xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000370 XML_SCHEMAS_DATE,
371 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000372 xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000373 XML_SCHEMAS_DATETIME,
374 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000375 xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000376 XML_SCHEMAS_TIME,
377 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000378 xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000379 XML_SCHEMAS_GYEAR,
380 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000381 xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000382 XML_SCHEMAS_GYEARMONTH,
383 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000384 xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000385 XML_SCHEMAS_GMONTH,
386 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000387 xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000388 XML_SCHEMAS_GMONTHDAY,
389 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000390 xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000391 XML_SCHEMAS_GDAY,
392 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000393 xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000394 XML_SCHEMAS_DURATION,
395 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000396 xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000397 XML_SCHEMAS_FLOAT,
398 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000399 xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000400 XML_SCHEMAS_DOUBLE,
401 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000402 xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000403 XML_SCHEMAS_BOOLEAN,
404 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000405 xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000406 XML_SCHEMAS_ANYURI,
407 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard560c2a42003-07-06 21:13:49 +0000408 xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000409 XML_SCHEMAS_HEXBINARY,
410 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000411 xmlSchemaTypeBase64BinaryDef
Daniel Veillard01fa6152004-06-29 17:04:39 +0000412 = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY,
413 xmlSchemaTypeAnySimpleTypeDef);
414 xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
415 XML_SCHEMAS_NOTATION,
416 xmlSchemaTypeAnySimpleTypeDef);
417 xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
418 XML_SCHEMAS_QNAME,
419 xmlSchemaTypeAnySimpleTypeDef);
Daniel Veillard4255d502002-04-16 15:50:10 +0000420
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000421 /*
422 * derived datatypes
423 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000424 xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000425 XML_SCHEMAS_INTEGER,
426 xmlSchemaTypeDecimalDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000427 xmlSchemaTypeNonPositiveIntegerDef =
428 xmlSchemaInitBasicType("nonPositiveInteger",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000429 XML_SCHEMAS_NPINTEGER,
430 xmlSchemaTypeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000431 xmlSchemaTypeNegativeIntegerDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000432 xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER,
433 xmlSchemaTypeNonPositiveIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000434 xmlSchemaTypeLongDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000435 xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG,
436 xmlSchemaTypeIntegerDef);
437 xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT,
438 xmlSchemaTypeLongDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000439 xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000440 XML_SCHEMAS_SHORT,
441 xmlSchemaTypeIntDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000442 xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000443 XML_SCHEMAS_BYTE,
444 xmlSchemaTypeShortDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000445 xmlSchemaTypeNonNegativeIntegerDef =
446 xmlSchemaInitBasicType("nonNegativeInteger",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000447 XML_SCHEMAS_NNINTEGER,
448 xmlSchemaTypeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000449 xmlSchemaTypeUnsignedLongDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000450 xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG,
451 xmlSchemaTypeNonNegativeIntegerDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000452 xmlSchemaTypeUnsignedIntDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000453 xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT,
454 xmlSchemaTypeUnsignedLongDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000455 xmlSchemaTypeUnsignedShortDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000456 xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT,
457 xmlSchemaTypeUnsignedIntDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000458 xmlSchemaTypeUnsignedByteDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000459 xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE,
460 xmlSchemaTypeUnsignedShortDef);
Daniel Veillard6560a422003-03-27 21:25:38 +0000461 xmlSchemaTypePositiveIntegerDef =
Daniel Veillard01fa6152004-06-29 17:04:39 +0000462 xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER,
463 xmlSchemaTypeNonNegativeIntegerDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000464 xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000465 XML_SCHEMAS_NORMSTRING,
466 xmlSchemaTypeStringDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000467 xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000468 XML_SCHEMAS_TOKEN,
469 xmlSchemaTypeNormStringDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000470 xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000471 XML_SCHEMAS_LANGUAGE,
472 xmlSchemaTypeTokenDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000473 xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000474 XML_SCHEMAS_NAME,
475 xmlSchemaTypeTokenDef);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000476 xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
Daniel Veillard01fa6152004-06-29 17:04:39 +0000477 XML_SCHEMAS_NMTOKEN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000478 xmlSchemaTypeTokenDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000479 xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
480 XML_SCHEMAS_NCNAME,
481 xmlSchemaTypeNameDef);
482 xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000483 xmlSchemaTypeNCNameDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000484 xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
485 XML_SCHEMAS_IDREF,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000486 xmlSchemaTypeNCNameDef);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000487 xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
488 XML_SCHEMAS_ENTITY,
489 xmlSchemaTypeNCNameDef);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000490 /*
491 * Derived list types.
492 */
493 /* ENTITIES */
Daniel Veillard01fa6152004-06-29 17:04:39 +0000494 xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
495 XML_SCHEMAS_ENTITIES,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000496 xmlSchemaTypeAnySimpleTypeDef);
497 xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef;
498 /* IDREFS */
499 xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
500 XML_SCHEMAS_IDREFS,
501 xmlSchemaTypeAnySimpleTypeDef);
502 xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef;
503
504 /* NMTOKENS */
505 xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
506 XML_SCHEMAS_NMTOKENS,
507 xmlSchemaTypeAnySimpleTypeDef);
508 xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef;
509
Daniel Veillard4255d502002-04-16 15:50:10 +0000510 xmlSchemaTypesInitialized = 1;
511}
512
513/**
514 * xmlSchemaCleanupTypes:
515 *
516 * Cleanup the default XML Schemas type library
517 */
518void
519xmlSchemaCleanupTypes(void) {
520 if (xmlSchemaTypesInitialized == 0)
521 return;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000522 xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard);
Daniel Veillard4255d502002-04-16 15:50:10 +0000523 xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
524 xmlSchemaTypesInitialized = 0;
525}
526
527/**
Daniel Veillard6927b102004-10-27 17:29:04 +0000528 * xmlSchemaIsBuiltInTypeFacet:
Daniel Veillard01fa6152004-06-29 17:04:39 +0000529 * @type: the built-in type
530 * @facetType: the facet type
531 *
532 * Evaluates if a specific facet can be
533 * used in conjunction with a type.
534 *
535 * Returns 1 if the facet can be used with the given built-in type,
536 * 0 otherwise and -1 in case the type is not a built-in type.
537 */
538int
539xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType)
540{
Daniel Veillardce682bc2004-11-05 17:22:25 +0000541 if (type == NULL)
542 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000543 if (type->type != XML_SCHEMA_TYPE_BASIC)
544 return (-1);
545 switch (type->builtInType) {
546 case XML_SCHEMAS_BOOLEAN:
547 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
548 (facetType == XML_SCHEMA_FACET_WHITESPACE))
549 return (1);
550 else
551 return (0);
552 case XML_SCHEMAS_STRING:
553 case XML_SCHEMAS_NOTATION:
554 case XML_SCHEMAS_QNAME:
555 case XML_SCHEMAS_ANYURI:
556 case XML_SCHEMAS_BASE64BINARY:
557 case XML_SCHEMAS_HEXBINARY:
558 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
559 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
560 (facetType == XML_SCHEMA_FACET_MAXLENGTH) ||
561 (facetType == XML_SCHEMA_FACET_PATTERN) ||
562 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
563 (facetType == XML_SCHEMA_FACET_WHITESPACE))
564 return (1);
565 else
566 return (0);
567 case XML_SCHEMAS_DECIMAL:
568 if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) ||
569 (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) ||
570 (facetType == XML_SCHEMA_FACET_PATTERN) ||
571 (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
572 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
573 (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
574 (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
575 (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
576 (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
577 return (1);
578 else
579 return (0);
580 case XML_SCHEMAS_TIME:
581 case XML_SCHEMAS_GDAY:
582 case XML_SCHEMAS_GMONTH:
583 case XML_SCHEMAS_GMONTHDAY:
584 case XML_SCHEMAS_GYEAR:
585 case XML_SCHEMAS_GYEARMONTH:
586 case XML_SCHEMAS_DATE:
587 case XML_SCHEMAS_DATETIME:
588 case XML_SCHEMAS_DURATION:
589 case XML_SCHEMAS_FLOAT:
590 case XML_SCHEMAS_DOUBLE:
591 if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
592 (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
593 (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
594 (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
595 (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
596 (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
597 (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
598 return (1);
599 else
600 return (0);
601 default:
Daniel Veillardc7e3cc42004-09-28 12:33:52 +0000602 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +0000603 }
604 return (0);
605}
606
607/**
608 * xmlSchemaGetBuiltInType:
609 * @type: the type of the built in type
610 *
611 * Gives you the type struct for a built-in
612 * type by its type id.
613 *
614 * Returns the type if found, NULL otherwise.
615 */
616xmlSchemaTypePtr
617xmlSchemaGetBuiltInType(xmlSchemaValType type)
618{
619 if (xmlSchemaTypesInitialized == 0)
620 xmlSchemaInitTypes();
621 switch (type) {
622
623 case XML_SCHEMAS_ANYSIMPLETYPE:
624 return (xmlSchemaTypeAnySimpleTypeDef);
625 case XML_SCHEMAS_STRING:
626 return (xmlSchemaTypeStringDef);
627 case XML_SCHEMAS_NORMSTRING:
628 return (xmlSchemaTypeNormStringDef);
629 case XML_SCHEMAS_DECIMAL:
630 return (xmlSchemaTypeDecimalDef);
631 case XML_SCHEMAS_TIME:
632 return (xmlSchemaTypeTimeDef);
633 case XML_SCHEMAS_GDAY:
634 return (xmlSchemaTypeGDayDef);
635 case XML_SCHEMAS_GMONTH:
636 return (xmlSchemaTypeGMonthDef);
637 case XML_SCHEMAS_GMONTHDAY:
638 return (xmlSchemaTypeGMonthDayDef);
639 case XML_SCHEMAS_GYEAR:
640 return (xmlSchemaTypeGYearDef);
641 case XML_SCHEMAS_GYEARMONTH:
642 return (xmlSchemaTypeGYearMonthDef);
643 case XML_SCHEMAS_DATE:
644 return (xmlSchemaTypeDateDef);
645 case XML_SCHEMAS_DATETIME:
646 return (xmlSchemaTypeDatetimeDef);
647 case XML_SCHEMAS_DURATION:
648 return (xmlSchemaTypeDurationDef);
649 case XML_SCHEMAS_FLOAT:
650 return (xmlSchemaTypeFloatDef);
651 case XML_SCHEMAS_DOUBLE:
652 return (xmlSchemaTypeDoubleDef);
653 case XML_SCHEMAS_BOOLEAN:
654 return (xmlSchemaTypeBooleanDef);
655 case XML_SCHEMAS_TOKEN:
656 return (xmlSchemaTypeTokenDef);
657 case XML_SCHEMAS_LANGUAGE:
658 return (xmlSchemaTypeLanguageDef);
659 case XML_SCHEMAS_NMTOKEN:
660 return (xmlSchemaTypeNmtokenDef);
661 case XML_SCHEMAS_NMTOKENS:
662 return (xmlSchemaTypeNmtokensDef);
663 case XML_SCHEMAS_NAME:
664 return (xmlSchemaTypeNameDef);
665 case XML_SCHEMAS_QNAME:
666 return (xmlSchemaTypeQNameDef);
667 case XML_SCHEMAS_NCNAME:
668 return (xmlSchemaTypeNCNameDef);
669 case XML_SCHEMAS_ID:
670 return (xmlSchemaTypeIdDef);
671 case XML_SCHEMAS_IDREF:
672 return (xmlSchemaTypeIdrefDef);
673 case XML_SCHEMAS_IDREFS:
674 return (xmlSchemaTypeIdrefsDef);
675 case XML_SCHEMAS_ENTITY:
676 return (xmlSchemaTypeEntityDef);
677 case XML_SCHEMAS_ENTITIES:
678 return (xmlSchemaTypeEntitiesDef);
679 case XML_SCHEMAS_NOTATION:
680 return (xmlSchemaTypeNotationDef);
681 case XML_SCHEMAS_ANYURI:
682 return (xmlSchemaTypeAnyURIDef);
683 case XML_SCHEMAS_INTEGER:
684 return (xmlSchemaTypeIntegerDef);
685 case XML_SCHEMAS_NPINTEGER:
686 return (xmlSchemaTypeNonPositiveIntegerDef);
687 case XML_SCHEMAS_NINTEGER:
688 return (xmlSchemaTypeNegativeIntegerDef);
689 case XML_SCHEMAS_NNINTEGER:
690 return (xmlSchemaTypeNonNegativeIntegerDef);
691 case XML_SCHEMAS_PINTEGER:
692 return (xmlSchemaTypePositiveIntegerDef);
693 case XML_SCHEMAS_INT:
694 return (xmlSchemaTypeIntDef);
695 case XML_SCHEMAS_UINT:
696 return (xmlSchemaTypeUnsignedIntDef);
697 case XML_SCHEMAS_LONG:
698 return (xmlSchemaTypeLongDef);
699 case XML_SCHEMAS_ULONG:
700 return (xmlSchemaTypeUnsignedLongDef);
701 case XML_SCHEMAS_SHORT:
702 return (xmlSchemaTypeShortDef);
703 case XML_SCHEMAS_USHORT:
704 return (xmlSchemaTypeUnsignedShortDef);
705 case XML_SCHEMAS_BYTE:
706 return (xmlSchemaTypeByteDef);
707 case XML_SCHEMAS_UBYTE:
708 return (xmlSchemaTypeUnsignedByteDef);
709 case XML_SCHEMAS_HEXBINARY:
710 return (xmlSchemaTypeHexBinaryDef);
711 case XML_SCHEMAS_BASE64BINARY:
712 return (xmlSchemaTypeBase64BinaryDef);
713 case XML_SCHEMAS_ANYTYPE:
714 return (xmlSchemaTypeAnyTypeDef);
715 default:
716 return (NULL);
717 }
718}
719
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000720int
721xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000722
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000723 if ((prev == NULL) || (cur == NULL))
724 return (-1);
725 prev->next = cur;
726 return (0);
727}
728
729xmlSchemaValPtr
730xmlSchemaValueGetNext(xmlSchemaValPtr cur) {
731
732 if (cur == NULL)
733 return (NULL);
734 return (cur->next);
735}
736
737const xmlChar *
738xmlSchemaValueGetAsString(xmlSchemaValPtr val)
739{
740 if (val == NULL)
741 return (NULL);
742 switch (val->type) {
743 case XML_SCHEMAS_STRING:
744 case XML_SCHEMAS_NORMSTRING:
745 case XML_SCHEMAS_ANYSIMPLETYPE:
746 case XML_SCHEMAS_TOKEN:
747 case XML_SCHEMAS_LANGUAGE:
748 case XML_SCHEMAS_NMTOKEN:
749 case XML_SCHEMAS_NAME:
750 case XML_SCHEMAS_NCNAME:
751 case XML_SCHEMAS_ID:
752 case XML_SCHEMAS_IDREF:
753 case XML_SCHEMAS_ENTITY:
754 case XML_SCHEMAS_ANYURI:
755 return (BAD_CAST val->value.str);
756 default:
757 break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000758 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000759 return (NULL);
760}
761
762int
763xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val)
764{
765 if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN))
766 return (0);
767 return (val->value.b);
Daniel Veillard4255d502002-04-16 15:50:10 +0000768}
769
770/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000771 * xmlSchemaNewStringValue:
772 * @type: the value type
Daniel Veillardb5839c32005-02-19 18:27:14 +0000773 * @value: the value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000774 *
775 * Allocate a new simple type value. The type can be
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +0000776 * of XML_SCHEMAS_STRING.
777 * WARNING: This one is intended to be expanded for other
778 * string based types. We need this for anySimpleType as well.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000779 * The given value is consumed and freed with the struct.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000780 *
781 * Returns a pointer to the new value or NULL in case of error
782 */
783xmlSchemaValPtr
784xmlSchemaNewStringValue(xmlSchemaValType type,
785 const xmlChar *value)
786{
787 xmlSchemaValPtr val;
788
789 if (type != XML_SCHEMAS_STRING)
790 return(NULL);
791 val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
792 if (val == NULL) {
793 return(NULL);
794 }
795 memset(val, 0, sizeof(xmlSchemaVal));
796 val->type = type;
797 val->value.str = (xmlChar *) value;
798 return(val);
799}
800
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000801/**
802 * xmlSchemaNewNOTATIONValue:
Daniel Veillardb5839c32005-02-19 18:27:14 +0000803 * @name: the notation name
804 * @ns: the notation namespace name or NULL
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000805 *
806 * Allocate a new NOTATION value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000807 * The given values are consumed and freed with the struct.
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000808 *
809 * Returns a pointer to the new value or NULL in case of error
810 */
811xmlSchemaValPtr
812xmlSchemaNewNOTATIONValue(const xmlChar *name,
813 const xmlChar *ns)
814{
815 xmlSchemaValPtr val;
816
817 val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
818 if (val == NULL)
819 return (NULL);
820
William M. Brack12d37ab2005-02-21 13:54:07 +0000821 val->value.qname.name = (xmlChar *)name;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000822 if (ns != NULL)
William M. Brack12d37ab2005-02-21 13:54:07 +0000823 val->value.qname.uri = (xmlChar *)ns;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000824 return(val);
825}
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +0000826
827/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000828 * xmlSchemaNewQNameValue:
829 * @namespaceName: the namespace name
830 * @localName: the local name
831 *
832 * Allocate a new QName value.
833 * The given values are consumed and freed with the struct.
834 *
835 * Returns a pointer to the new value or NULL in case of an error.
836 */
837xmlSchemaValPtr
838xmlSchemaNewQNameValue(const xmlChar *namespaceName,
839 const xmlChar *localName)
840{
841 xmlSchemaValPtr val;
842
843 val = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
844 if (val == NULL)
845 return (NULL);
846
847 val->value.qname.name = (xmlChar *) localName;
848 val->value.qname.uri = (xmlChar *) namespaceName;
849 return(val);
850}
851
852/**
Daniel Veillard4255d502002-04-16 15:50:10 +0000853 * xmlSchemaFreeValue:
854 * @value: the value to free
855 *
856 * Cleanup the default XML Schemas type library
857 */
858void
859xmlSchemaFreeValue(xmlSchemaValPtr value) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000860 xmlSchemaValPtr prev;
861
862 while (value != NULL) {
863 switch (value->type) {
864 case XML_SCHEMAS_STRING:
865 case XML_SCHEMAS_NORMSTRING:
866 case XML_SCHEMAS_TOKEN:
867 case XML_SCHEMAS_LANGUAGE:
868 case XML_SCHEMAS_NMTOKEN:
869 case XML_SCHEMAS_NMTOKENS:
870 case XML_SCHEMAS_NAME:
871 case XML_SCHEMAS_NCNAME:
872 case XML_SCHEMAS_ID:
873 case XML_SCHEMAS_IDREF:
874 case XML_SCHEMAS_IDREFS:
875 case XML_SCHEMAS_ENTITY:
876 case XML_SCHEMAS_ENTITIES:
877 case XML_SCHEMAS_ANYURI:
878 case XML_SCHEMAS_ANYSIMPLETYPE:
879 if (value->value.str != NULL)
880 xmlFree(value->value.str);
881 break;
882 case XML_SCHEMAS_NOTATION:
883 case XML_SCHEMAS_QNAME:
884 if (value->value.qname.uri != NULL)
885 xmlFree(value->value.qname.uri);
886 if (value->value.qname.name != NULL)
887 xmlFree(value->value.qname.name);
888 break;
889 case XML_SCHEMAS_HEXBINARY:
890 if (value->value.hex.str != NULL)
891 xmlFree(value->value.hex.str);
892 break;
893 case XML_SCHEMAS_BASE64BINARY:
894 if (value->value.base64.str != NULL)
895 xmlFree(value->value.base64.str);
896 break;
897 default:
898 break;
899 }
900 prev = value;
901 value = value->next;
902 xmlFree(prev);
903 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000904}
905
906/**
907 * xmlSchemaGetPredefinedType:
908 * @name: the type name
909 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
910 *
911 * Lookup a type in the default XML Schemas type library
912 *
913 * Returns the type if found, NULL otherwise
914 */
915xmlSchemaTypePtr
916xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
917 if (xmlSchemaTypesInitialized == 0)
918 xmlSchemaInitTypes();
919 if (name == NULL)
920 return(NULL);
921 return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
922}
Daniel Veillard070803b2002-05-03 07:29:38 +0000923
Daniel Veillard01fa6152004-06-29 17:04:39 +0000924/**
925 * xmlSchemaGetBuiltInListSimpleTypeItemType:
926 * @type: the built-in simple type.
927 *
Daniel Veillard6927b102004-10-27 17:29:04 +0000928 * Lookup function
929 *
Daniel Veillardc0826a72004-08-10 14:17:33 +0000930 * Returns the item type of @type as defined by the built-in datatype
931 * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error.
Daniel Veillard01fa6152004-06-29 17:04:39 +0000932 */
933xmlSchemaTypePtr
934xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type)
935{
Daniel Veillard42595322004-11-08 10:52:06 +0000936 if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC))
Daniel Veillard01fa6152004-06-29 17:04:39 +0000937 return (NULL);
938 switch (type->builtInType) {
939 case XML_SCHEMAS_NMTOKENS:
940 return (xmlSchemaTypeNmtokenDef );
941 case XML_SCHEMAS_IDREFS:
942 return (xmlSchemaTypeIdrefDef);
943 case XML_SCHEMAS_ENTITIES:
944 return (xmlSchemaTypeEntityDef);
945 default:
946 return (NULL);
947 }
948}
949
Daniel Veillard070803b2002-05-03 07:29:38 +0000950/****************************************************************
951 * *
952 * Convenience macros and functions *
953 * *
954 ****************************************************************/
955
956#define IS_TZO_CHAR(c) \
957 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
958
959#define VALID_YEAR(yr) (yr != 0)
960#define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
961/* VALID_DAY should only be used when month is unknown */
962#define VALID_DAY(day) ((day >= 1) && (day <= 31))
963#define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
964#define VALID_MIN(min) ((min >= 0) && (min <= 59))
965#define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
Kasimier T. Buchcik690a6802005-05-12 13:16:01 +0000966#define VALID_TZO(tzo) ((tzo > -840) && (tzo < 840))
Daniel Veillard070803b2002-05-03 07:29:38 +0000967#define IS_LEAP(y) \
968 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
969
Daniel Veillardebe25d42004-03-25 09:35:49 +0000970static const unsigned int daysInMonth[12] =
Daniel Veillard070803b2002-05-03 07:29:38 +0000971 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Daniel Veillardebe25d42004-03-25 09:35:49 +0000972static const unsigned int daysInMonthLeap[12] =
Daniel Veillard070803b2002-05-03 07:29:38 +0000973 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
974
Daniel Veillard5a872412002-05-22 06:40:27 +0000975#define MAX_DAYINMONTH(yr,mon) \
976 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
977
Daniel Veillard070803b2002-05-03 07:29:38 +0000978#define VALID_MDAY(dt) \
979 (IS_LEAP(dt->year) ? \
980 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
981 (dt->day <= daysInMonth[dt->mon - 1]))
982
983#define VALID_DATE(dt) \
984 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
985
986#define VALID_TIME(dt) \
987 (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
988 VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
989
990#define VALID_DATETIME(dt) \
991 (VALID_DATE(dt) && VALID_TIME(dt))
992
993#define SECS_PER_MIN (60)
994#define SECS_PER_HOUR (60 * SECS_PER_MIN)
995#define SECS_PER_DAY (24 * SECS_PER_HOUR)
996
Daniel Veillard5a872412002-05-22 06:40:27 +0000997static const long dayInYearByMonth[12] =
998 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
999static const long dayInLeapYearByMonth[12] =
1000 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
1001
1002#define DAY_IN_YEAR(day, month, year) \
1003 ((IS_LEAP(year) ? \
1004 dayInLeapYearByMonth[month - 1] : \
1005 dayInYearByMonth[month - 1]) + day)
1006
1007#ifdef DEBUG
1008#define DEBUG_DATE(dt) \
1009 xmlGenericError(xmlGenericErrorContext, \
1010 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
1011 dt->type,dt->value.date.year,dt->value.date.mon, \
1012 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
1013 dt->value.date.sec); \
1014 if (dt->value.date.tz_flag) \
1015 if (dt->value.date.tzo != 0) \
1016 xmlGenericError(xmlGenericErrorContext, \
1017 "%+05d\n",dt->value.date.tzo); \
1018 else \
1019 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
1020 else \
1021 xmlGenericError(xmlGenericErrorContext,"\n")
1022#else
1023#define DEBUG_DATE(dt)
1024#endif
1025
Daniel Veillard070803b2002-05-03 07:29:38 +00001026/**
1027 * _xmlSchemaParseGYear:
1028 * @dt: pointer to a date structure
1029 * @str: pointer to the string to analyze
1030 *
1031 * Parses a xs:gYear without time zone and fills in the appropriate
1032 * field of the @dt structure. @str is updated to point just after the
1033 * xs:gYear. It is supposed that @dt->year is big enough to contain
1034 * the year.
1035 *
1036 * Returns 0 or the error code
1037 */
1038static int
1039_xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
1040 const xmlChar *cur = *str, *firstChar;
1041 int isneg = 0, digcnt = 0;
1042
1043 if (((*cur < '0') || (*cur > '9')) &&
1044 (*cur != '-') && (*cur != '+'))
1045 return -1;
1046
1047 if (*cur == '-') {
1048 isneg = 1;
1049 cur++;
1050 }
1051
1052 firstChar = cur;
1053
1054 while ((*cur >= '0') && (*cur <= '9')) {
1055 dt->year = dt->year * 10 + (*cur - '0');
1056 cur++;
1057 digcnt++;
1058 }
1059
1060 /* year must be at least 4 digits (CCYY); over 4
1061 * digits cannot have a leading zero. */
1062 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
1063 return 1;
1064
1065 if (isneg)
1066 dt->year = - dt->year;
1067
1068 if (!VALID_YEAR(dt->year))
1069 return 2;
1070
1071 *str = cur;
1072 return 0;
1073}
1074
1075/**
1076 * PARSE_2_DIGITS:
1077 * @num: the integer to fill in
1078 * @cur: an #xmlChar *
1079 * @invalid: an integer
1080 *
1081 * Parses a 2-digits integer and updates @num with the value. @cur is
1082 * updated to point just after the integer.
1083 * In case of error, @invalid is set to %TRUE, values of @num and
1084 * @cur are undefined.
1085 */
1086#define PARSE_2_DIGITS(num, cur, invalid) \
1087 if ((cur[0] < '0') || (cur[0] > '9') || \
1088 (cur[1] < '0') || (cur[1] > '9')) \
1089 invalid = 1; \
1090 else \
1091 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
1092 cur += 2;
1093
1094/**
1095 * PARSE_FLOAT:
1096 * @num: the double to fill in
1097 * @cur: an #xmlChar *
1098 * @invalid: an integer
1099 *
1100 * Parses a float and updates @num with the value. @cur is
1101 * updated to point just after the float. The float must have a
1102 * 2-digits integer part and may or may not have a decimal part.
1103 * In case of error, @invalid is set to %TRUE, values of @num and
1104 * @cur are undefined.
1105 */
1106#define PARSE_FLOAT(num, cur, invalid) \
1107 PARSE_2_DIGITS(num, cur, invalid); \
1108 if (!invalid && (*cur == '.')) { \
1109 double mult = 1; \
1110 cur++; \
1111 if ((*cur < '0') || (*cur > '9')) \
1112 invalid = 1; \
1113 while ((*cur >= '0') && (*cur <= '9')) { \
1114 mult /= 10; \
1115 num += (*cur - '0') * mult; \
1116 cur++; \
1117 } \
1118 }
1119
1120/**
1121 * _xmlSchemaParseGMonth:
1122 * @dt: pointer to a date structure
1123 * @str: pointer to the string to analyze
1124 *
1125 * Parses a xs:gMonth without time zone and fills in the appropriate
1126 * field of the @dt structure. @str is updated to point just after the
1127 * xs:gMonth.
1128 *
1129 * Returns 0 or the error code
1130 */
1131static int
1132_xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
1133 const xmlChar *cur = *str;
1134 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001135 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001136
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001137 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001138 if (ret != 0)
1139 return ret;
1140
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001141 if (!VALID_MONTH(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001142 return 2;
1143
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001144 dt->mon = value;
1145
Daniel Veillard070803b2002-05-03 07:29:38 +00001146 *str = cur;
1147 return 0;
1148}
1149
1150/**
1151 * _xmlSchemaParseGDay:
1152 * @dt: pointer to a date structure
1153 * @str: pointer to the string to analyze
1154 *
1155 * Parses a xs:gDay without time zone and fills in the appropriate
1156 * field of the @dt structure. @str is updated to point just after the
1157 * xs:gDay.
1158 *
1159 * Returns 0 or the error code
1160 */
1161static int
1162_xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
1163 const xmlChar *cur = *str;
1164 int ret = 0;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001165 unsigned int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001166
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001167 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001168 if (ret != 0)
1169 return ret;
1170
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001171 if (!VALID_DAY(value))
Daniel Veillard070803b2002-05-03 07:29:38 +00001172 return 2;
1173
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001174 dt->day = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001175 *str = cur;
1176 return 0;
1177}
1178
1179/**
1180 * _xmlSchemaParseTime:
1181 * @dt: pointer to a date structure
1182 * @str: pointer to the string to analyze
1183 *
1184 * Parses a xs:time without time zone and fills in the appropriate
1185 * fields of the @dt structure. @str is updated to point just after the
1186 * xs:time.
1187 * In case of error, values of @dt fields are undefined.
1188 *
1189 * Returns 0 or the error code
1190 */
1191static int
1192_xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +00001193 const xmlChar *cur = *str;
Daniel Veillard070803b2002-05-03 07:29:38 +00001194 int ret = 0;
Kasimier T. Buchcik285b3672005-05-12 13:10:22 +00001195 int value = 0;
Daniel Veillard070803b2002-05-03 07:29:38 +00001196
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001197 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001198 if (ret != 0)
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001199 return ret;
Daniel Veillard070803b2002-05-03 07:29:38 +00001200 if (*cur != ':')
1201 return 1;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001202 if (!VALID_HOUR(value))
1203 return 2;
Daniel Veillard070803b2002-05-03 07:29:38 +00001204 cur++;
1205
1206 /* the ':' insures this string is xs:time */
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001207 dt->hour = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001208
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001209 PARSE_2_DIGITS(value, cur, ret);
Daniel Veillard070803b2002-05-03 07:29:38 +00001210 if (ret != 0)
1211 return ret;
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001212 if (!VALID_MIN(value))
1213 return 2;
1214 dt->min = value;
Daniel Veillard070803b2002-05-03 07:29:38 +00001215
1216 if (*cur != ':')
1217 return 1;
1218 cur++;
1219
1220 PARSE_FLOAT(dt->sec, cur, ret);
1221 if (ret != 0)
1222 return ret;
1223
Kasimier T. Buchcik64dc4d22005-04-19 15:58:31 +00001224 if ((!VALID_SEC(dt->sec)) || (!VALID_TZO(dt->tzo)))
Daniel Veillard070803b2002-05-03 07:29:38 +00001225 return 2;
1226
1227 *str = cur;
1228 return 0;
1229}
1230
1231/**
1232 * _xmlSchemaParseTimeZone:
1233 * @dt: pointer to a date structure
1234 * @str: pointer to the string to analyze
1235 *
1236 * Parses a time zone without time zone and fills in the appropriate
1237 * field of the @dt structure. @str is updated to point just after the
1238 * time zone.
1239 *
1240 * Returns 0 or the error code
1241 */
1242static int
1243_xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
1244 const xmlChar *cur = *str;
1245 int ret = 0;
1246
1247 if (str == NULL)
1248 return -1;
1249
1250 switch (*cur) {
1251 case 0:
1252 dt->tz_flag = 0;
1253 dt->tzo = 0;
1254 break;
1255
1256 case 'Z':
1257 dt->tz_flag = 1;
1258 dt->tzo = 0;
1259 cur++;
1260 break;
1261
1262 case '+':
1263 case '-': {
1264 int isneg = 0, tmp = 0;
1265 isneg = (*cur == '-');
1266
1267 cur++;
1268
1269 PARSE_2_DIGITS(tmp, cur, ret);
1270 if (ret != 0)
1271 return ret;
1272 if (!VALID_HOUR(tmp))
1273 return 2;
1274
1275 if (*cur != ':')
1276 return 1;
1277 cur++;
1278
1279 dt->tzo = tmp * 60;
1280
1281 PARSE_2_DIGITS(tmp, cur, ret);
1282 if (ret != 0)
1283 return ret;
1284 if (!VALID_MIN(tmp))
1285 return 2;
1286
1287 dt->tzo += tmp;
1288 if (isneg)
1289 dt->tzo = - dt->tzo;
1290
1291 if (!VALID_TZO(dt->tzo))
1292 return 2;
1293
Daniel Veillard5a872412002-05-22 06:40:27 +00001294 dt->tz_flag = 1;
Daniel Veillard070803b2002-05-03 07:29:38 +00001295 break;
1296 }
1297 default:
1298 return 1;
1299 }
1300
1301 *str = cur;
1302 return 0;
1303}
1304
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001305/**
1306 * _xmlSchemaBase64Decode:
1307 * @ch: a character
1308 *
1309 * Converts a base64 encoded character to its base 64 value.
1310 *
1311 * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
1312 */
1313static int
1314_xmlSchemaBase64Decode (const xmlChar ch) {
1315 if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
1316 if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
1317 if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
1318 if ('+' == ch) return 62;
1319 if ('/' == ch) return 63;
1320 if ('=' == ch) return 64;
1321 return -1;
1322}
1323
Daniel Veillard070803b2002-05-03 07:29:38 +00001324/****************************************************************
1325 * *
1326 * XML Schema Dates/Times Datatypes Handling *
1327 * *
1328 ****************************************************************/
1329
1330/**
1331 * PARSE_DIGITS:
1332 * @num: the integer to fill in
1333 * @cur: an #xmlChar *
1334 * @num_type: an integer flag
1335 *
1336 * Parses a digits integer and updates @num with the value. @cur is
1337 * updated to point just after the integer.
1338 * In case of error, @num_type is set to -1, values of @num and
1339 * @cur are undefined.
1340 */
1341#define PARSE_DIGITS(num, cur, num_type) \
1342 if ((*cur < '0') || (*cur > '9')) \
1343 num_type = -1; \
1344 else \
1345 while ((*cur >= '0') && (*cur <= '9')) { \
1346 num = num * 10 + (*cur - '0'); \
1347 cur++; \
1348 }
1349
1350/**
1351 * PARSE_NUM:
1352 * @num: the double to fill in
1353 * @cur: an #xmlChar *
1354 * @num_type: an integer flag
1355 *
1356 * Parses a float or integer and updates @num with the value. @cur is
1357 * updated to point just after the number. If the number is a float,
1358 * then it must have an integer part and a decimal part; @num_type will
1359 * be set to 1. If there is no decimal part, @num_type is set to zero.
1360 * In case of error, @num_type is set to -1, values of @num and
1361 * @cur are undefined.
1362 */
1363#define PARSE_NUM(num, cur, num_type) \
1364 num = 0; \
1365 PARSE_DIGITS(num, cur, num_type); \
1366 if (!num_type && (*cur == '.')) { \
1367 double mult = 1; \
1368 cur++; \
1369 if ((*cur < '0') || (*cur > '9')) \
1370 num_type = -1; \
1371 else \
1372 num_type = 1; \
1373 while ((*cur >= '0') && (*cur <= '9')) { \
1374 mult /= 10; \
1375 num += (*cur - '0') * mult; \
1376 cur++; \
1377 } \
1378 }
1379
1380/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001381 * xmlSchemaValidateDates:
Daniel Veillard455cc072003-03-31 10:13:23 +00001382 * @type: the expected type or XML_SCHEMAS_UNKNOWN
Daniel Veillard070803b2002-05-03 07:29:38 +00001383 * @dateTime: string to analyze
1384 * @val: the return computed value
1385 *
1386 * Check that @dateTime conforms to the lexical space of one of the date types.
1387 * if true a value is computed and returned in @val.
1388 *
1389 * Returns 0 if this validates, a positive error code number otherwise
1390 * and -1 in case of internal or API error.
1391 */
1392static int
Daniel Veillard455cc072003-03-31 10:13:23 +00001393xmlSchemaValidateDates (xmlSchemaValType type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001394 const xmlChar *dateTime, xmlSchemaValPtr *val,
1395 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001396 xmlSchemaValPtr dt;
1397 int ret;
1398 const xmlChar *cur = dateTime;
1399
1400#define RETURN_TYPE_IF_VALID(t) \
1401 if (IS_TZO_CHAR(*cur)) { \
1402 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
1403 if (ret == 0) { \
1404 if (*cur != 0) \
1405 goto error; \
1406 dt->type = t; \
Daniel Veillard455cc072003-03-31 10:13:23 +00001407 goto done; \
Daniel Veillard070803b2002-05-03 07:29:38 +00001408 } \
1409 }
1410
1411 if (dateTime == NULL)
1412 return -1;
1413
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001414 if (collapse)
1415 while IS_WSP_BLANK_CH(*cur) cur++;
1416
Daniel Veillard070803b2002-05-03 07:29:38 +00001417 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
1418 return 1;
1419
1420 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
1421 if (dt == NULL)
1422 return -1;
1423
1424 if ((cur[0] == '-') && (cur[1] == '-')) {
1425 /*
1426 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
1427 * xs:gDay)
1428 */
1429 cur += 2;
1430
1431 /* is it an xs:gDay? */
1432 if (*cur == '-') {
Daniel Veillard455cc072003-03-31 10:13:23 +00001433 if (type == XML_SCHEMAS_GMONTH)
1434 goto error;
Daniel Veillard070803b2002-05-03 07:29:38 +00001435 ++cur;
1436 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1437 if (ret != 0)
1438 goto error;
1439
1440 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
1441
1442 goto error;
1443 }
1444
1445 /*
1446 * it should be an xs:gMonthDay or xs:gMonth
1447 */
1448 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1449 if (ret != 0)
1450 goto error;
1451
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001452 /*
1453 * a '-' char could indicate this type is xs:gMonthDay or
1454 * a negative time zone offset. Check for xs:gMonthDay first.
1455 * Also the first three char's of a negative tzo (-MM:SS) can
1456 * appear to be a valid day; so even if the day portion
1457 * of the xs:gMonthDay verifies, we must insure it was not
1458 * a tzo.
1459 */
1460 if (*cur == '-') {
1461 const xmlChar *rewnd = cur;
1462 cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001463
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001464 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1465 if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
1466
1467 /*
1468 * we can use the VALID_MDAY macro to validate the month
1469 * and day because the leap year test will flag year zero
1470 * as a leap year (even though zero is an invalid year).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001471 * FUTURE TODO: Zero will become valid in XML Schema 1.1
1472 * probably.
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001473 */
1474 if (VALID_MDAY((&(dt->value.date)))) {
1475
1476 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
1477
1478 goto error;
1479 }
1480 }
1481
1482 /*
1483 * not xs:gMonthDay so rewind and check if just xs:gMonth
1484 * with an optional time zone.
1485 */
1486 cur = rewnd;
1487 }
1488
1489 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
Daniel Veillard070803b2002-05-03 07:29:38 +00001490
1491 goto error;
1492 }
1493
1494 /*
1495 * It's a right-truncated date or an xs:time.
1496 * Try to parse an xs:time then fallback on right-truncated dates.
1497 */
1498 if ((*cur >= '0') && (*cur <= '9')) {
1499 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1500 if (ret == 0) {
1501 /* it's an xs:time */
1502 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
1503 }
1504 }
1505
1506 /* fallback on date parsing */
1507 cur = dateTime;
1508
1509 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
1510 if (ret != 0)
1511 goto error;
1512
1513 /* is it an xs:gYear? */
1514 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
1515
1516 if (*cur != '-')
1517 goto error;
1518 cur++;
1519
1520 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1521 if (ret != 0)
1522 goto error;
1523
1524 /* is it an xs:gYearMonth? */
1525 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
1526
1527 if (*cur != '-')
1528 goto error;
1529 cur++;
1530
1531 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1532 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
1533 goto error;
1534
1535 /* is it an xs:date? */
1536 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
1537
1538 if (*cur != 'T')
1539 goto error;
1540 cur++;
1541
1542 /* it should be an xs:dateTime */
1543 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1544 if (ret != 0)
1545 goto error;
1546
1547 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001548 if (collapse)
1549 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001550 if ((ret != 0) || (*cur != 0) || !VALID_DATETIME((&(dt->value.date))))
1551 goto error;
1552
Daniel Veillard455cc072003-03-31 10:13:23 +00001553
Daniel Veillard070803b2002-05-03 07:29:38 +00001554 dt->type = XML_SCHEMAS_DATETIME;
1555
Daniel Veillard455cc072003-03-31 10:13:23 +00001556done:
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001557#if 1
1558 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
1559 goto error;
1560#else
1561 /*
1562 * insure the parsed type is equal to or less significant (right
1563 * truncated) than the desired type.
1564 */
1565 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
1566
1567 /* time only matches time */
1568 if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
1569 goto error;
1570
1571 if ((type == XML_SCHEMAS_DATETIME) &&
1572 ((dt->type != XML_SCHEMAS_DATE) ||
1573 (dt->type != XML_SCHEMAS_GYEARMONTH) ||
1574 (dt->type != XML_SCHEMAS_GYEAR)))
1575 goto error;
1576
1577 if ((type == XML_SCHEMAS_DATE) &&
1578 ((dt->type != XML_SCHEMAS_GYEAR) ||
1579 (dt->type != XML_SCHEMAS_GYEARMONTH)))
1580 goto error;
1581
1582 if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
1583 goto error;
1584
1585 if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
1586 goto error;
1587 }
Daniel Veillard455cc072003-03-31 10:13:23 +00001588#endif
1589
Daniel Veillard070803b2002-05-03 07:29:38 +00001590 if (val != NULL)
1591 *val = dt;
Daniel Veillard80b19092003-03-28 13:29:53 +00001592 else
1593 xmlSchemaFreeValue(dt);
Daniel Veillard070803b2002-05-03 07:29:38 +00001594
1595 return 0;
1596
1597error:
1598 if (dt != NULL)
1599 xmlSchemaFreeValue(dt);
1600 return 1;
1601}
1602
1603/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001604 * xmlSchemaValidateDuration:
Daniel Veillard070803b2002-05-03 07:29:38 +00001605 * @type: the predefined type
1606 * @duration: string to analyze
1607 * @val: the return computed value
1608 *
1609 * Check that @duration conforms to the lexical space of the duration type.
1610 * if true a value is computed and returned in @val.
1611 *
1612 * Returns 0 if this validates, a positive error code number otherwise
1613 * and -1 in case of internal or API error.
1614 */
1615static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00001616xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001617 const xmlChar *duration, xmlSchemaValPtr *val,
1618 int collapse) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001619 const xmlChar *cur = duration;
1620 xmlSchemaValPtr dur;
1621 int isneg = 0;
1622 unsigned int seq = 0;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001623 double num;
1624 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
1625 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1626 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
Daniel Veillard070803b2002-05-03 07:29:38 +00001627
1628 if (duration == NULL)
1629 return -1;
1630
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001631 if (collapse)
1632 while IS_WSP_BLANK_CH(*cur) cur++;
1633
Daniel Veillard070803b2002-05-03 07:29:38 +00001634 if (*cur == '-') {
1635 isneg = 1;
1636 cur++;
1637 }
1638
1639 /* duration must start with 'P' (after sign) */
1640 if (*cur++ != 'P')
1641 return 1;
1642
Daniel Veillard80b19092003-03-28 13:29:53 +00001643 if (*cur == 0)
1644 return 1;
1645
Daniel Veillard070803b2002-05-03 07:29:38 +00001646 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1647 if (dur == NULL)
1648 return -1;
1649
1650 while (*cur != 0) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001651
1652 /* input string should be empty or invalid date/time item */
1653 if (seq >= sizeof(desig))
1654 goto error;
1655
1656 /* T designator must be present for time items */
1657 if (*cur == 'T') {
1658 if (seq <= 3) {
1659 seq = 3;
1660 cur++;
1661 } else
1662 return 1;
1663 } else if (seq == 3)
1664 goto error;
1665
1666 /* parse the number portion of the item */
1667 PARSE_NUM(num, cur, num_type);
1668
1669 if ((num_type == -1) || (*cur == 0))
1670 goto error;
1671
1672 /* update duration based on item type */
1673 while (seq < sizeof(desig)) {
1674 if (*cur == desig[seq]) {
1675
1676 /* verify numeric type; only seconds can be float */
1677 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1678 goto error;
1679
1680 switch (seq) {
1681 case 0:
1682 dur->value.dur.mon = (long)num * 12;
1683 break;
1684 case 1:
1685 dur->value.dur.mon += (long)num;
1686 break;
1687 default:
1688 /* convert to seconds using multiplier */
1689 dur->value.dur.sec += num * multi[seq];
1690 seq++;
1691 break;
1692 }
1693
1694 break; /* exit loop */
1695 }
1696 /* no date designators found? */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00001697 if ((++seq == 3) || (seq == 6))
Daniel Veillard070803b2002-05-03 07:29:38 +00001698 goto error;
1699 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001700 cur++;
1701 if (collapse)
1702 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +00001703 }
1704
1705 if (isneg) {
1706 dur->value.dur.mon = -dur->value.dur.mon;
1707 dur->value.dur.day = -dur->value.dur.day;
1708 dur->value.dur.sec = -dur->value.dur.sec;
1709 }
1710
1711 if (val != NULL)
1712 *val = dur;
Daniel Veillard80b19092003-03-28 13:29:53 +00001713 else
1714 xmlSchemaFreeValue(dur);
Daniel Veillard070803b2002-05-03 07:29:38 +00001715
1716 return 0;
1717
1718error:
1719 if (dur != NULL)
1720 xmlSchemaFreeValue(dur);
1721 return 1;
1722}
1723
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001724/**
1725 * xmlSchemaStrip:
1726 * @value: a value
1727 *
1728 * Removes the leading and ending spaces of a string
1729 *
1730 * Returns the new string or NULL if no change was required.
1731 */
1732static xmlChar *
1733xmlSchemaStrip(const xmlChar *value) {
1734 const xmlChar *start = value, *end, *f;
1735
1736 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001737 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001738 end = start;
1739 while (*end != 0) end++;
1740 f = end;
1741 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001742 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001743 end++;
1744 if ((start == value) && (f == end)) return(NULL);
1745 return(xmlStrndup(start, end - start));
1746}
Daniel Veillard96a4b252003-02-06 08:22:32 +00001747
1748/**
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001749 * xmlSchemaWhiteSpaceReplace:
1750 * @value: a value
1751 *
1752 * Replaces 0xd, 0x9 and 0xa with a space.
1753 *
1754 * Returns the new string or NULL if no change was required.
1755 */
1756xmlChar *
1757xmlSchemaWhiteSpaceReplace(const xmlChar *value) {
1758 const xmlChar *cur = value;
1759 xmlChar *ret = NULL, *mcur;
1760
1761 if (value == NULL)
1762 return(NULL);
1763
1764 while ((*cur != 0) &&
1765 (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) {
1766 cur++;
1767 }
1768 if (*cur == 0)
1769 return (NULL);
1770 ret = xmlStrdup(value);
1771 /* TODO FIXME: I guess gcc will bark at this. */
1772 mcur = (xmlChar *) (ret + (cur - value));
1773 do {
1774 if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) )
1775 *mcur = ' ';
1776 mcur++;
1777 } while (*mcur != 0);
1778 return(ret);
1779}
1780
1781/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001782 * xmlSchemaCollapseString:
1783 * @value: a value
1784 *
1785 * Removes and normalize white spaces in the string
1786 *
1787 * Returns the new string or NULL if no change was required.
1788 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00001789xmlChar *
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001790xmlSchemaCollapseString(const xmlChar *value) {
1791 const xmlChar *start = value, *end, *f;
1792 xmlChar *g;
1793 int col = 0;
1794
1795 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001796 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001797 end = start;
1798 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001799 if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001800 col = end - start;
1801 break;
1802 } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
1803 col = end - start;
1804 break;
1805 }
1806 end++;
1807 }
1808 if (col == 0) {
1809 f = end;
1810 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001811 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001812 end++;
1813 if ((start == value) && (f == end)) return(NULL);
1814 return(xmlStrndup(start, end - start));
1815 }
1816 start = xmlStrdup(start);
1817 if (start == NULL) return(NULL);
1818 g = (xmlChar *) (start + col);
1819 end = g;
1820 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001821 if (IS_BLANK_CH(*end)) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001822 end++;
William M. Brack76e95df2003-10-18 16:20:14 +00001823 while (IS_BLANK_CH(*end)) end++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001824 if (*end != 0)
1825 *g++ = ' ';
1826 } else
1827 *g++ = *end++;
1828 }
1829 *g = 0;
1830 return((xmlChar *) start);
1831}
1832
1833/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001834 * xmlSchemaValAtomicListNode:
1835 * @type: the predefined atomic type for a token in the list
1836 * @value: the list value to check
1837 * @ret: the return computed value
1838 * @node: the node containing the value
1839 *
1840 * Check that a value conforms to the lexical space of the predefined
1841 * list type. if true a value is computed and returned in @ret.
1842 *
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001843 * Returns the number of items if this validates, a negative error code
1844 * number otherwise
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001845 */
1846static int
1847xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
1848 xmlSchemaValPtr *ret, xmlNodePtr node) {
1849 xmlChar *val, *cur, *endval;
1850 int nb_values = 0;
Daniel Veillard580ced82003-03-21 21:22:48 +00001851 int tmp = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001852
1853 if (value == NULL) {
1854 return(-1);
1855 }
1856 val = xmlStrdup(value);
1857 if (val == NULL) {
1858 return(-1);
1859 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00001860 if (ret != NULL) {
1861 *ret = NULL;
1862 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001863 cur = val;
1864 /*
1865 * Split the list
1866 */
William M. Brack76e95df2003-10-18 16:20:14 +00001867 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001868 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001869 if (IS_BLANK_CH(*cur)) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001870 *cur = 0;
1871 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00001872 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001873 } else {
1874 nb_values++;
1875 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00001876 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001877 }
1878 }
1879 if (nb_values == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001880 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001881 return(nb_values);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001882 }
1883 endval = cur;
1884 cur = val;
1885 while ((*cur == 0) && (cur != endval)) cur++;
1886 while (cur != endval) {
1887 tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
1888 if (tmp != 0)
1889 break;
1890 while (*cur != 0) cur++;
1891 while ((*cur == 0) && (cur != endval)) cur++;
1892 }
Daniel Veillard6fc5db02005-01-16 00:05:58 +00001893 /* TODO what return value ? c.f. bug #158628
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001894 if (ret != NULL) {
1895 TODO
Daniel Veillard6fc5db02005-01-16 00:05:58 +00001896 } */
1897 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001898 if (tmp == 0)
1899 return(nb_values);
1900 return(-1);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001901}
1902
1903/**
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001904 * xmlSchemaParseUInt:
1905 * @str: pointer to the string R/W
1906 * @llo: pointer to the low result
1907 * @lmi: pointer to the mid result
1908 * @lhi: pointer to the high result
1909 *
1910 * Parse an unsigned long into 3 fields.
1911 *
William M. Brackec3b4b72005-03-15 15:50:17 +00001912 * Returns the number of significant digits in the number or
1913 * -1 if overflow of the capacity
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001914 */
1915static int
1916xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001917 unsigned long *lmi, unsigned long *lhi) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001918 unsigned long lo = 0, mi = 0, hi = 0;
1919 const xmlChar *tmp, *cur = *str;
1920 int ret = 0, i = 0;
1921
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001922 while (*cur == '0') { /* ignore leading zeroes */
1923 cur++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001924 }
1925 tmp = cur;
1926 while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001927 i++;tmp++;ret++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001928 }
1929 if (i > 24) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001930 *str = tmp;
1931 return(-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001932 }
1933 while (i > 16) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001934 hi = hi * 10 + (*cur++ - '0');
1935 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001936 }
1937 while (i > 8) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001938 mi = mi * 10 + (*cur++ - '0');
1939 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001940 }
1941 while (i > 0) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001942 lo = lo * 10 + (*cur++ - '0');
1943 i--;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001944 }
1945
1946 *str = cur;
1947 *llo = lo;
1948 *lmi = mi;
1949 *lhi = hi;
1950 return(ret);
1951}
1952
1953/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001954 * xmlSchemaValAtomicType:
1955 * @type: the predefined type
1956 * @value: the value to check
1957 * @val: the return computed value
1958 * @node: the node containing the value
1959 * flags: flags to control the vlidation
1960 *
1961 * Check that a value conforms to the lexical space of the atomic type.
1962 * if true a value is computed and returned in @val.
Daniel Veillard01fa6152004-06-29 17:04:39 +00001963 * This checks the value space for list types as well (IDREFS, NMTOKENS).
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001964 *
1965 * Returns 0 if this validates, a positive error code number otherwise
1966 * and -1 in case of internal or API error.
1967 */
1968static int
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001969xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001970 xmlSchemaValPtr * val, xmlNodePtr node, int flags,
1971 xmlSchemaWhitespaceValueType ws,
1972 int normOnTheFly, int applyNorm, int createStringValue)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001973{
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001974 xmlSchemaValPtr v;
1975 xmlChar *norm = NULL;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001976 int ret = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001977
1978 if (xmlSchemaTypesInitialized == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00001979 xmlSchemaInitTypes();
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001980 if (type == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001981 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001982
Daniel Veillardeebd6332004-08-26 10:30:44 +00001983 /*
1984 * validating a non existant text node is similar to validating
1985 * an empty one.
1986 */
1987 if (value == NULL)
1988 value = BAD_CAST "";
1989
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001990 if (val != NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001991 *val = NULL;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001992 if ((flags == 0) && (value != NULL)) {
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001993
Daniel Veillard01fa6152004-06-29 17:04:39 +00001994 if ((type->builtInType != XML_SCHEMAS_STRING) &&
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00001995 (type->builtInType != XML_SCHEMAS_ANYTYPE) &&
1996 (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) {
1997 if (type->builtInType == XML_SCHEMAS_NORMSTRING)
1998 norm = xmlSchemaWhiteSpaceReplace(value);
1999 else
2000 norm = xmlSchemaCollapseString(value);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002001 if (norm != NULL)
2002 value = norm;
2003 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002004 }
2005
Daniel Veillard01fa6152004-06-29 17:04:39 +00002006 switch (type->builtInType) {
William M. Brack2f2a6632004-08-20 23:09:47 +00002007 case XML_SCHEMAS_UNKNOWN:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002008 goto error;
William M. Brack2f2a6632004-08-20 23:09:47 +00002009 case XML_SCHEMAS_ANYTYPE:
2010 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002011 if ((createStringValue) && (val != NULL)) {
2012 v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE);
2013 if (v != NULL) {
2014 v->value.str = xmlStrdup(value);
2015 *val = v;
2016 } else {
2017 goto error;
2018 }
2019 }
William M. Brack2f2a6632004-08-20 23:09:47 +00002020 goto return0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002021 case XML_SCHEMAS_STRING:
2022 if (! normOnTheFly) {
2023 const xmlChar *cur = value;
2024
2025 if (ws == XML_SCHEMA_WHITESPACE_REPLACE) {
2026 while (*cur != 0) {
2027 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2028 goto return1;
2029 } else {
2030 cur++;
2031 }
2032 }
2033 } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
2034 while (*cur != 0) {
2035 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2036 goto return1;
2037 } else if IS_WSP_SPACE_CH(*cur) {
2038 cur++;
2039 if IS_WSP_SPACE_CH(*cur)
2040 goto return1;
2041 } else {
2042 cur++;
2043 }
2044 }
2045 }
2046 }
2047 if (createStringValue && (val != NULL)) {
2048 if (applyNorm) {
2049 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2050 norm = xmlSchemaCollapseString(value);
2051 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
2052 norm = xmlSchemaWhiteSpaceReplace(value);
2053 if (norm != NULL)
2054 value = norm;
2055 }
2056 v = xmlSchemaNewValue(XML_SCHEMAS_STRING);
2057 if (v != NULL) {
2058 v->value.str = xmlStrdup(value);
2059 *val = v;
2060 } else {
2061 goto error;
2062 }
2063 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002064 goto return0;
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002065 case XML_SCHEMAS_NORMSTRING:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002066 if (normOnTheFly) {
2067 if (applyNorm) {
2068 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
2069 norm = xmlSchemaCollapseString(value);
2070 else
2071 norm = xmlSchemaWhiteSpaceReplace(value);
2072 if (norm != NULL)
2073 value = norm;
2074 }
2075 } else {
2076 const xmlChar *cur = value;
2077 while (*cur != 0) {
2078 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2079 goto return1;
2080 } else {
2081 cur++;
2082 }
2083 }
2084 }
Daniel Veillard1516d5b2004-01-22 07:27:45 +00002085 if (val != NULL) {
2086 v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
2087 if (v != NULL) {
2088 v->value.str = xmlStrdup(value);
2089 *val = v;
2090 } else {
2091 goto error;
2092 }
2093 }
2094 goto return0;
2095 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002096 case XML_SCHEMAS_DECIMAL:{
William M. Brack273670f2005-03-11 15:55:14 +00002097 const xmlChar *cur = value;
2098 unsigned int len, neg = 0;
2099 xmlChar cval[25];
2100 xmlChar *cptr = cval;
2101 int dec = -1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002102
2103 if (cur == NULL)
2104 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002105
2106 if (normOnTheFly)
2107 while IS_WSP_BLANK_CH(*cur) cur++;
2108
William M. Brack273670f2005-03-11 15:55:14 +00002109 /* First we handle an optional sign */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002110 if (*cur == '+')
2111 cur++;
2112 else if (*cur == '-') {
2113 neg = 1;
2114 cur++;
2115 }
William M. Brack273670f2005-03-11 15:55:14 +00002116 /*
2117 * Next we "pre-parse" the number, in preparation for calling
2118 * the common routine xmlSchemaParseUInt. We get rid of any
2119 * leading zeroes (because we have reserved only 25 chars),
2120 * and note the position of any decimal point.
2121 */
2122 len = 0;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002123 /*
2124 * Skip leading zeroes.
2125 */
2126 while (*cur == '0')
William M. Brack273670f2005-03-11 15:55:14 +00002127 cur++;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002128 if (*cur != 0) {
2129 while (len < 24) {
2130 if ((*cur >= '0') && (*cur <= '9')) {
2131 *cptr++ = *cur++;
2132 len++;
2133 } else if (*cur == '.') {
2134 if (dec != -1)
2135 goto return1; /* multiple decimal points */
2136 cur++;
2137 if ((*cur == 0) && (cur -1 == value))
2138 goto return1;
2139
2140 dec = len;
2141 while ((len < 24) && (*cur >= '0') &&
2142 (*cur <= '9')) {
2143 *cptr++ = *cur++;
2144 len++;
2145 }
2146 break;
2147 } else
2148 break;
2149 }
William M. Brack273670f2005-03-11 15:55:14 +00002150 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002151 if (normOnTheFly)
2152 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brack273670f2005-03-11 15:55:14 +00002153 if (*cur != 0)
2154 goto return1; /* error if any extraneous chars */
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002155 if (val != NULL) {
2156 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
2157 if (v != NULL) {
William M. Brack273670f2005-03-11 15:55:14 +00002158 /*
2159 * If a mixed decimal, get rid of trailing zeroes
2160 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002161 if (dec != -1) {
William M. Brack273670f2005-03-11 15:55:14 +00002162 while ((cptr > cval) && (*(cptr-1) == '0')) {
2163 cptr--;
2164 len--;
2165 }
2166 }
2167 *cptr = 0; /* Terminate our (preparsed) string */
2168 cptr = cval;
2169 /*
2170 * Now evaluate the significant digits of the number
2171 */
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002172 if (*cptr != 0)
2173 xmlSchemaParseUInt((const xmlChar **)&cptr,
William M. Brack273670f2005-03-11 15:55:14 +00002174 &v->value.decimal.lo,
2175 &v->value.decimal.mi,
2176 &v->value.decimal.hi);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002177 /*
2178 * Set the total digits to 1 if a zero value.
2179 */
2180 if (len == 0)
2181 len++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002182 v->value.decimal.sign = neg;
William M. Brack273670f2005-03-11 15:55:14 +00002183 if (dec == -1) {
2184 v->value.decimal.frac = 0;
2185 v->value.decimal.total = len;
2186 } else {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002187 v->value.decimal.frac = len - dec;
2188 v->value.decimal.total = len;
William M. Brack273670f2005-03-11 15:55:14 +00002189 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002190 *val = v;
2191 }
2192 }
2193 goto return0;
2194 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002195 case XML_SCHEMAS_TIME:
2196 case XML_SCHEMAS_GDAY:
2197 case XML_SCHEMAS_GMONTH:
2198 case XML_SCHEMAS_GMONTHDAY:
2199 case XML_SCHEMAS_GYEAR:
2200 case XML_SCHEMAS_GYEARMONTH:
2201 case XML_SCHEMAS_DATE:
2202 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002203 ret = xmlSchemaValidateDates(type->builtInType, value, val,
2204 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002205 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002206 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002207 ret = xmlSchemaValidateDuration(type, value, val,
2208 normOnTheFly);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002209 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002210 case XML_SCHEMAS_FLOAT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002211 case XML_SCHEMAS_DOUBLE:{
2212 const xmlChar *cur = value;
2213 int neg = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002214
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002215 if (cur == NULL)
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00002216 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002217
2218 if (normOnTheFly)
2219 while IS_WSP_BLANK_CH(*cur) cur++;
2220
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002221 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
2222 cur += 3;
2223 if (*cur != 0)
2224 goto return1;
2225 if (val != NULL) {
2226 if (type == xmlSchemaTypeFloatDef) {
2227 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2228 if (v != NULL) {
2229 v->value.f = (float) xmlXPathNAN;
2230 } else {
2231 xmlSchemaFreeValue(v);
2232 goto error;
2233 }
2234 } else {
2235 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2236 if (v != NULL) {
2237 v->value.d = xmlXPathNAN;
2238 } else {
2239 xmlSchemaFreeValue(v);
2240 goto error;
2241 }
2242 }
2243 *val = v;
2244 }
2245 goto return0;
2246 }
2247 if (*cur == '-') {
2248 neg = 1;
2249 cur++;
2250 }
2251 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
2252 cur += 3;
2253 if (*cur != 0)
2254 goto return1;
2255 if (val != NULL) {
2256 if (type == xmlSchemaTypeFloatDef) {
2257 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2258 if (v != NULL) {
2259 if (neg)
2260 v->value.f = (float) xmlXPathNINF;
2261 else
2262 v->value.f = (float) xmlXPathPINF;
2263 } else {
2264 xmlSchemaFreeValue(v);
2265 goto error;
2266 }
2267 } else {
2268 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2269 if (v != NULL) {
2270 if (neg)
2271 v->value.d = xmlXPathNINF;
2272 else
2273 v->value.d = xmlXPathPINF;
2274 } else {
2275 xmlSchemaFreeValue(v);
2276 goto error;
2277 }
2278 }
2279 *val = v;
2280 }
2281 goto return0;
2282 }
2283 if ((neg == 0) && (*cur == '+'))
2284 cur++;
2285 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
2286 goto return1;
2287 while ((*cur >= '0') && (*cur <= '9')) {
2288 cur++;
2289 }
2290 if (*cur == '.') {
2291 cur++;
2292 while ((*cur >= '0') && (*cur <= '9'))
2293 cur++;
2294 }
2295 if ((*cur == 'e') || (*cur == 'E')) {
2296 cur++;
2297 if ((*cur == '-') || (*cur == '+'))
2298 cur++;
2299 while ((*cur >= '0') && (*cur <= '9'))
2300 cur++;
2301 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002302 if (normOnTheFly)
2303 while IS_WSP_BLANK_CH(*cur) cur++;
2304
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002305 if (*cur != 0)
2306 goto return1;
2307 if (val != NULL) {
2308 if (type == xmlSchemaTypeFloatDef) {
2309 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
2310 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002311 /*
2312 * TODO: sscanf seems not to give the correct
2313 * value for extremely high/low values.
2314 * E.g. "1E-149" results in zero.
2315 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002316 if (sscanf((const char *) value, "%f",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002317 &(v->value.f)) == 1) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002318 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002319 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002320 xmlSchemaFreeValue(v);
2321 goto return1;
2322 }
2323 } else {
2324 goto error;
2325 }
2326 } else {
2327 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
2328 if (v != NULL) {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002329 /*
2330 * TODO: sscanf seems not to give the correct
2331 * value for extremely high/low values.
2332 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002333 if (sscanf((const char *) value, "%lf",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002334 &(v->value.d)) == 1) {
2335 *val = v;
2336 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002337 xmlSchemaFreeValue(v);
2338 goto return1;
2339 }
2340 } else {
2341 goto error;
2342 }
2343 }
2344 }
2345 goto return0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00002346 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002347 case XML_SCHEMAS_BOOLEAN:{
2348 const xmlChar *cur = value;
2349
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002350 if (normOnTheFly) {
2351 while IS_WSP_BLANK_CH(*cur) cur++;
2352 if (*cur == '0') {
2353 ret = 0;
2354 cur++;
2355 } else if (*cur == '1') {
2356 ret = 1;
2357 cur++;
2358 } else if (*cur == 't') {
2359 cur++;
2360 if ((*cur++ == 'r') && (*cur++ == 'u') &&
2361 (*cur++ == 'e')) {
2362 ret = 1;
2363 } else
2364 goto return1;
2365 } else if (*cur == 'f') {
2366 cur++;
2367 if ((*cur++ == 'a') && (*cur++ == 'l') &&
2368 (*cur++ == 's') && (*cur++ == 'e')) {
2369 ret = 0;
2370 } else
2371 goto return1;
2372 }
2373 if (*cur != 0) {
2374 while IS_WSP_BLANK_CH(*cur) cur++;
2375 if (*cur != 0)
2376 goto return1;
2377 }
2378 } else {
2379 if ((cur[0] == '0') && (cur[1] == 0))
2380 ret = 0;
2381 else if ((cur[0] == '1') && (cur[1] == 0))
2382 ret = 1;
2383 else if ((cur[0] == 't') && (cur[1] == 'r')
2384 && (cur[2] == 'u') && (cur[3] == 'e')
2385 && (cur[4] == 0))
2386 ret = 1;
2387 else if ((cur[0] == 'f') && (cur[1] == 'a')
2388 && (cur[2] == 'l') && (cur[3] == 's')
2389 && (cur[4] == 'e') && (cur[5] == 0))
2390 ret = 0;
2391 else
2392 goto return1;
2393 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002394 if (val != NULL) {
2395 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
2396 if (v != NULL) {
2397 v->value.b = ret;
2398 *val = v;
2399 } else {
2400 goto error;
2401 }
2402 }
2403 goto return0;
2404 }
2405 case XML_SCHEMAS_TOKEN:{
2406 const xmlChar *cur = value;
2407
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002408 if (! normOnTheFly) {
2409 while (*cur != 0) {
2410 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
2411 goto return1;
2412 } else if (*cur == ' ') {
2413 cur++;
2414 if (*cur == 0)
2415 goto return1;
2416 if (*cur == ' ')
2417 goto return1;
2418 } else {
2419 cur++;
2420 }
2421 }
2422 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002423 if (val != NULL) {
2424 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
2425 if (v != NULL) {
2426 v->value.str = xmlStrdup(value);
2427 *val = v;
2428 } else {
2429 goto error;
2430 }
2431 }
2432 goto return0;
2433 }
2434 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002435 if (normOnTheFly) {
2436 norm = xmlSchemaCollapseString(value);
2437 if (norm != NULL)
2438 value = norm;
2439 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002440 if (xmlCheckLanguageID(value) == 1) {
2441 if (val != NULL) {
2442 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
2443 if (v != NULL) {
2444 v->value.str = xmlStrdup(value);
2445 *val = v;
2446 } else {
2447 goto error;
2448 }
2449 }
2450 goto return0;
2451 }
2452 goto return1;
2453 case XML_SCHEMAS_NMTOKEN:
2454 if (xmlValidateNMToken(value, 1) == 0) {
2455 if (val != NULL) {
2456 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
2457 if (v != NULL) {
2458 v->value.str = xmlStrdup(value);
2459 *val = v;
2460 } else {
2461 goto error;
2462 }
2463 }
2464 goto return0;
2465 }
2466 goto return1;
2467 case XML_SCHEMAS_NMTOKENS:
2468 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
2469 value, val, node);
2470 if (ret > 0)
2471 ret = 0;
2472 else
2473 ret = 1;
2474 goto done;
2475 case XML_SCHEMAS_NAME:
2476 ret = xmlValidateName(value, 1);
Daniel Veillarddf292f72005-01-16 19:00:15 +00002477 if ((ret == 0) && (val != NULL) && (value != NULL)) {
2478 v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
2479 if (v != NULL) {
2480 const xmlChar *start = value, *end;
2481 while (IS_BLANK_CH(*start)) start++;
2482 end = start;
2483 while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
2484 v->value.str = xmlStrndup(start, end - start);
2485 *val = v;
2486 } else {
2487 goto error;
2488 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002489 }
2490 goto done;
2491 case XML_SCHEMAS_QNAME:{
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002492 const xmlChar *uri = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002493 xmlChar *local = NULL;
2494
2495 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002496 if (ret != 0)
2497 goto done;
2498 if (node != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002499 xmlChar *prefix;
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002500 xmlNsPtr ns;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002501
2502 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002503 ns = xmlSearchNs(node->doc, node, prefix);
2504 if ((ns == NULL) && (prefix != NULL)) {
2505 xmlFree(prefix);
2506 if (local != NULL)
2507 xmlFree(local);
2508 goto return1;
2509 }
2510 if (ns != NULL)
2511 uri = ns->href;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002512 if (prefix != NULL)
2513 xmlFree(prefix);
2514 }
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002515 if (val != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002516 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
Kasimier T. Buchcik1c720df2005-06-09 14:54:59 +00002517 if (v == NULL) {
2518 if (local != NULL)
2519 xmlFree(local);
2520 goto error;
2521 }
2522 if (local != NULL)
2523 v->value.qname.name = local;
2524 else
2525 v->value.qname.name = xmlStrdup(value);
2526 if (uri != NULL)
2527 v->value.qname.uri = xmlStrdup(uri);
2528 *val = v;
2529 } else
2530 if (local != NULL)
2531 xmlFree(local);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002532 goto done;
2533 }
2534 case XML_SCHEMAS_NCNAME:
2535 ret = xmlValidateNCName(value, 1);
2536 if ((ret == 0) && (val != NULL)) {
2537 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
2538 if (v != NULL) {
2539 v->value.str = xmlStrdup(value);
2540 *val = v;
2541 } else {
2542 goto error;
2543 }
2544 }
2545 goto done;
2546 case XML_SCHEMAS_ID:
2547 ret = xmlValidateNCName(value, 1);
2548 if ((ret == 0) && (val != NULL)) {
2549 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
2550 if (v != NULL) {
2551 v->value.str = xmlStrdup(value);
2552 *val = v;
2553 } else {
2554 goto error;
2555 }
2556 }
2557 if ((ret == 0) && (node != NULL) &&
2558 (node->type == XML_ATTRIBUTE_NODE)) {
2559 xmlAttrPtr attr = (xmlAttrPtr) node;
2560
2561 /*
2562 * NOTE: the IDness might have already be declared in the DTD
2563 */
2564 if (attr->atype != XML_ATTRIBUTE_ID) {
2565 xmlIDPtr res;
2566 xmlChar *strip;
2567
2568 strip = xmlSchemaStrip(value);
2569 if (strip != NULL) {
2570 res = xmlAddID(NULL, node->doc, strip, attr);
2571 xmlFree(strip);
2572 } else
2573 res = xmlAddID(NULL, node->doc, value, attr);
2574 if (res == NULL) {
2575 ret = 2;
2576 } else {
2577 attr->atype = XML_ATTRIBUTE_ID;
2578 }
2579 }
2580 }
2581 goto done;
2582 case XML_SCHEMAS_IDREF:
2583 ret = xmlValidateNCName(value, 1);
2584 if ((ret == 0) && (val != NULL)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00002585 v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
2586 if (v == NULL)
2587 goto error;
2588 v->value.str = xmlStrdup(value);
2589 *val = v;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002590 }
2591 if ((ret == 0) && (node != NULL) &&
2592 (node->type == XML_ATTRIBUTE_NODE)) {
2593 xmlAttrPtr attr = (xmlAttrPtr) node;
2594 xmlChar *strip;
2595
2596 strip = xmlSchemaStrip(value);
2597 if (strip != NULL) {
2598 xmlAddRef(NULL, node->doc, strip, attr);
2599 xmlFree(strip);
2600 } else
2601 xmlAddRef(NULL, node->doc, value, attr);
2602 attr->atype = XML_ATTRIBUTE_IDREF;
2603 }
2604 goto done;
2605 case XML_SCHEMAS_IDREFS:
2606 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
2607 value, val, node);
2608 if (ret < 0)
2609 ret = 2;
2610 else
2611 ret = 0;
2612 if ((ret == 0) && (node != NULL) &&
2613 (node->type == XML_ATTRIBUTE_NODE)) {
2614 xmlAttrPtr attr = (xmlAttrPtr) node;
2615
2616 attr->atype = XML_ATTRIBUTE_IDREFS;
2617 }
2618 goto done;
2619 case XML_SCHEMAS_ENTITY:{
2620 xmlChar *strip;
2621
2622 ret = xmlValidateNCName(value, 1);
2623 if ((node == NULL) || (node->doc == NULL))
2624 ret = 3;
2625 if (ret == 0) {
2626 xmlEntityPtr ent;
2627
2628 strip = xmlSchemaStrip(value);
2629 if (strip != NULL) {
2630 ent = xmlGetDocEntity(node->doc, strip);
2631 xmlFree(strip);
2632 } else {
2633 ent = xmlGetDocEntity(node->doc, value);
2634 }
2635 if ((ent == NULL) ||
2636 (ent->etype !=
2637 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
2638 ret = 4;
2639 }
2640 if ((ret == 0) && (val != NULL)) {
2641 TODO;
2642 }
2643 if ((ret == 0) && (node != NULL) &&
2644 (node->type == XML_ATTRIBUTE_NODE)) {
2645 xmlAttrPtr attr = (xmlAttrPtr) node;
2646
2647 attr->atype = XML_ATTRIBUTE_ENTITY;
2648 }
2649 goto done;
2650 }
2651 case XML_SCHEMAS_ENTITIES:
2652 if ((node == NULL) || (node->doc == NULL))
2653 goto return3;
2654 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
2655 value, val, node);
2656 if (ret <= 0)
2657 ret = 1;
2658 else
2659 ret = 0;
2660 if ((ret == 0) && (node != NULL) &&
2661 (node->type == XML_ATTRIBUTE_NODE)) {
2662 xmlAttrPtr attr = (xmlAttrPtr) node;
2663
2664 attr->atype = XML_ATTRIBUTE_ENTITIES;
2665 }
2666 goto done;
2667 case XML_SCHEMAS_NOTATION:{
2668 xmlChar *uri = NULL;
2669 xmlChar *local = NULL;
2670
2671 ret = xmlValidateQName(value, 1);
2672 if ((ret == 0) && (node != NULL)) {
2673 xmlChar *prefix;
2674
2675 local = xmlSplitQName2(value, &prefix);
2676 if (prefix != NULL) {
2677 xmlNsPtr ns;
2678
2679 ns = xmlSearchNs(node->doc, node, prefix);
2680 if (ns == NULL)
2681 ret = 1;
2682 else if (val != NULL)
2683 uri = xmlStrdup(ns->href);
2684 }
2685 if ((local != NULL) && ((val == NULL) || (ret != 0)))
2686 xmlFree(local);
2687 if (prefix != NULL)
2688 xmlFree(prefix);
2689 }
2690 if ((node == NULL) || (node->doc == NULL))
2691 ret = 3;
2692 if (ret == 0) {
2693 ret = xmlValidateNotationUse(NULL, node->doc, value);
2694 if (ret == 1)
2695 ret = 0;
2696 else
2697 ret = 1;
2698 }
2699 if ((ret == 0) && (val != NULL)) {
2700 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
2701 if (v != NULL) {
2702 if (local != NULL)
2703 v->value.qname.name = local;
2704 else
2705 v->value.qname.name = xmlStrdup(value);
2706 if (uri != NULL)
2707 v->value.qname.uri = uri;
2708
2709 *val = v;
2710 } else {
2711 if (local != NULL)
2712 xmlFree(local);
2713 if (uri != NULL)
2714 xmlFree(uri);
2715 goto error;
2716 }
2717 }
2718 goto done;
2719 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002720 case XML_SCHEMAS_ANYURI:{
Daniel Veillard11c466a2004-03-14 12:20:15 +00002721 if (*value != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002722 xmlURIPtr uri;
2723 if (normOnTheFly) {
2724 norm = xmlSchemaCollapseString(value);
2725 if (norm != NULL)
2726 value = norm;
2727 }
2728 uri = xmlParseURI((const char *) value);
Daniel Veillard11c466a2004-03-14 12:20:15 +00002729 if (uri == NULL)
2730 goto return1;
2731 xmlFreeURI(uri);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002732 }
Daniel Veillard11c466a2004-03-14 12:20:15 +00002733
2734 if (val != NULL) {
2735 v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
2736 if (v == NULL)
2737 goto error;
2738 v->value.str = xmlStrdup(value);
2739 *val = v;
2740 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002741 goto return0;
2742 }
2743 case XML_SCHEMAS_HEXBINARY:{
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002744 const xmlChar *cur = value, *start;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002745 xmlChar *base;
2746 int total, i = 0;
2747
Daniel Veillardf34a20e2004-08-31 08:42:17 +00002748 if (cur == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002749 goto return1;
2750
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002751 if (normOnTheFly)
2752 while IS_WSP_BLANK_CH(*cur) cur++;
2753
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002754 start = cur;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002755 while (((*cur >= '0') && (*cur <= '9')) ||
2756 ((*cur >= 'A') && (*cur <= 'F')) ||
2757 ((*cur >= 'a') && (*cur <= 'f'))) {
2758 i++;
2759 cur++;
2760 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002761 if (normOnTheFly)
2762 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002763
2764 if (*cur != 0)
2765 goto return1;
2766 if ((i % 2) != 0)
2767 goto return1;
2768
2769 if (val != NULL) {
2770
2771 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2772 if (v == NULL)
2773 goto error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002774 /*
2775 * Copy only the normalized piece.
2776 * CRITICAL TODO: Check this.
2777 */
Kasimier T. Buchcik8dd1e1b2005-06-09 13:14:38 +00002778 cur = xmlStrndup(start, i);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002779 if (cur == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002780 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002781 xmlFree(v);
2782 goto return1;
2783 }
2784
2785 total = i / 2; /* number of octets */
2786
2787 base = (xmlChar *) cur;
2788 while (i-- > 0) {
2789 if (*base >= 'a')
2790 *base = *base - ('a' - 'A');
2791 base++;
2792 }
2793
2794 v->value.hex.str = (xmlChar *) cur;
2795 v->value.hex.total = total;
2796 *val = v;
2797 }
2798 goto return0;
2799 }
2800 case XML_SCHEMAS_BASE64BINARY:{
2801 /* ISSUE:
2802 *
2803 * Ignore all stray characters? (yes, currently)
2804 * Worry about long lines? (no, currently)
2805 *
2806 * rfc2045.txt:
2807 *
2808 * "The encoded output stream must be represented in lines of
2809 * no more than 76 characters each. All line breaks or other
2810 * characters not found in Table 1 must be ignored by decoding
2811 * software. In base64 data, characters other than those in
2812 * Table 1, line breaks, and other white space probably
2813 * indicate a transmission error, about which a warning
2814 * message or even a message rejection might be appropriate
2815 * under some circumstances." */
2816 const xmlChar *cur = value;
2817 xmlChar *base;
2818 int total, i = 0, pad = 0;
2819
2820 if (cur == NULL)
2821 goto return1;
2822
2823 for (; *cur; ++cur) {
2824 int decc;
2825
2826 decc = _xmlSchemaBase64Decode(*cur);
2827 if (decc < 0) ;
2828 else if (decc < 64)
2829 i++;
2830 else
2831 break;
2832 }
2833 for (; *cur; ++cur) {
2834 int decc;
2835
2836 decc = _xmlSchemaBase64Decode(*cur);
2837 if (decc < 0) ;
2838 else if (decc < 64)
2839 goto return1;
2840 if (decc == 64)
2841 pad++;
2842 }
2843
2844 /* rfc2045.txt: "Special processing is performed if fewer than
2845 * 24 bits are available at the end of the data being encoded.
2846 * A full encoding quantum is always completed at the end of a
2847 * body. When fewer than 24 input bits are available in an
2848 * input group, zero bits are added (on the right) to form an
2849 * integral number of 6-bit groups. Padding at the end of the
2850 * data is performed using the "=" character. Since all
2851 * base64 input is an integral number of octets, only the
2852 * following cases can arise: (1) the final quantum of
2853 * encoding input is an integral multiple of 24 bits; here,
2854 * the final unit of encoded output will be an integral
2855 * multiple ofindent: Standard input:701: Warning:old style
2856 * assignment ambiguity in "=*". Assuming "= *" 4 characters
2857 * with no "=" padding, (2) the final
2858 * quantum of encoding input is exactly 8 bits; here, the
2859 * final unit of encoded output will be two characters
2860 * followed by two "=" padding characters, or (3) the final
2861 * quantum of encoding input is exactly 16 bits; here, the
2862 * final unit of encoded output will be three characters
2863 * followed by one "=" padding character." */
2864
2865 total = 3 * (i / 4);
2866 if (pad == 0) {
2867 if (i % 4 != 0)
2868 goto return1;
2869 } else if (pad == 1) {
2870 int decc;
2871
2872 if (i % 4 != 3)
2873 goto return1;
2874 for (decc = _xmlSchemaBase64Decode(*cur);
2875 (decc < 0) || (decc > 63);
2876 decc = _xmlSchemaBase64Decode(*cur))
2877 --cur;
2878 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
2879 /* 00111100 -> 0x3c */
2880 if (decc & ~0x3c)
2881 goto return1;
2882 total += 2;
2883 } else if (pad == 2) {
2884 int decc;
2885
2886 if (i % 4 != 2)
2887 goto return1;
2888 for (decc = _xmlSchemaBase64Decode(*cur);
2889 (decc < 0) || (decc > 63);
2890 decc = _xmlSchemaBase64Decode(*cur))
2891 --cur;
2892 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
2893 /* 00110000 -> 0x30 */
2894 if (decc & ~0x30)
2895 goto return1;
2896 total += 1;
2897 } else
2898 goto return1;
2899
2900 if (val != NULL) {
2901 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
2902 if (v == NULL)
2903 goto error;
2904 base =
2905 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
2906 sizeof(xmlChar));
2907 if (base == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002908 xmlSchemaTypeErrMemory(node, "allocating base64 data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002909 xmlFree(v);
2910 goto return1;
2911 }
2912 v->value.base64.str = base;
2913 for (cur = value; *cur; ++cur)
2914 if (_xmlSchemaBase64Decode(*cur) >= 0) {
2915 *base = *cur;
2916 ++base;
2917 }
2918 *base = 0;
2919 v->value.base64.total = total;
2920 *val = v;
2921 }
2922 goto return0;
2923 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002924 case XML_SCHEMAS_INTEGER:
2925 case XML_SCHEMAS_PINTEGER:
2926 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002927 case XML_SCHEMAS_NINTEGER:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002928 case XML_SCHEMAS_NNINTEGER:{
2929 const xmlChar *cur = value;
2930 unsigned long lo, mi, hi;
William M. Brackec3b4b72005-03-15 15:50:17 +00002931 int sign = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002932
2933 if (cur == NULL)
2934 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002935 if (normOnTheFly)
2936 while IS_WSP_BLANK_CH(*cur) cur++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002937 if (*cur == '-') {
2938 sign = 1;
2939 cur++;
2940 } else if (*cur == '+')
2941 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00002942 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2943 if (ret == -1)
2944 goto return1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002945 if (normOnTheFly)
2946 while IS_WSP_BLANK_CH(*cur) cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00002947 if (*cur != 0)
2948 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00002949 if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002950 if ((sign == 0) &&
2951 ((hi != 0) || (mi != 0) || (lo != 0)))
2952 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00002953 } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002954 if (sign == 1)
2955 goto return1;
2956 if ((hi == 0) && (mi == 0) && (lo == 0))
2957 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00002958 } else if (type->builtInType == XML_SCHEMAS_NINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002959 if (sign == 0)
2960 goto return1;
2961 if ((hi == 0) && (mi == 0) && (lo == 0))
2962 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00002963 } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002964 if ((sign == 1) &&
2965 ((hi != 0) || (mi != 0) || (lo != 0)))
2966 goto return1;
2967 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002968 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00002969 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002970 if (v != NULL) {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00002971 if (ret == 0)
2972 ret++;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002973 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00002974 v->value.decimal.mi = mi;
2975 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002976 v->value.decimal.sign = sign;
2977 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00002978 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002979 *val = v;
2980 }
2981 }
2982 goto return0;
2983 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002984 case XML_SCHEMAS_LONG:
2985 case XML_SCHEMAS_BYTE:
2986 case XML_SCHEMAS_SHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002987 case XML_SCHEMAS_INT:{
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002988 const xmlChar *cur = value;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002989 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002990 int sign = 0;
2991
2992 if (cur == NULL)
2993 goto return1;
2994 if (*cur == '-') {
2995 sign = 1;
2996 cur++;
2997 } else if (*cur == '+')
2998 cur++;
William M. Brackec3b4b72005-03-15 15:50:17 +00002999 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3000 if (ret < 0)
3001 goto return1;
3002 if (*cur != 0)
3003 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003004 if (type->builtInType == XML_SCHEMAS_LONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003005 if (hi >= 922) {
3006 if (hi > 922)
3007 goto return1;
3008 if (mi >= 33720368) {
3009 if (mi > 33720368)
3010 goto return1;
3011 if ((sign == 0) && (lo > 54775807))
3012 goto return1;
3013 if ((sign == 1) && (lo > 54775808))
3014 goto return1;
3015 }
3016 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003017 } else if (type->builtInType == XML_SCHEMAS_INT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003018 if (hi != 0)
3019 goto return1;
3020 if (mi >= 21) {
3021 if (mi > 21)
3022 goto return1;
3023 if ((sign == 0) && (lo > 47483647))
3024 goto return1;
3025 if ((sign == 1) && (lo > 47483648))
3026 goto return1;
3027 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003028 } else if (type->builtInType == XML_SCHEMAS_SHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003029 if ((mi != 0) || (hi != 0))
3030 goto return1;
3031 if ((sign == 1) && (lo > 32768))
3032 goto return1;
3033 if ((sign == 0) && (lo > 32767))
3034 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003035 } else if (type->builtInType == XML_SCHEMAS_BYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003036 if ((mi != 0) || (hi != 0))
3037 goto return1;
3038 if ((sign == 1) && (lo > 128))
3039 goto return1;
3040 if ((sign == 0) && (lo > 127))
3041 goto return1;
3042 }
3043 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003044 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003045 if (v != NULL) {
3046 v->value.decimal.lo = lo;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00003047 v->value.decimal.mi = mi;
3048 v->value.decimal.hi = hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003049 v->value.decimal.sign = sign;
3050 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003051 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003052 *val = v;
3053 }
3054 }
3055 goto return0;
3056 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003057 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003058 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003059 case XML_SCHEMAS_USHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003060 case XML_SCHEMAS_UBYTE:{
3061 const xmlChar *cur = value;
3062 unsigned long lo, mi, hi;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003063
3064 if (cur == NULL)
3065 goto return1;
William M. Brackec3b4b72005-03-15 15:50:17 +00003066 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
3067 if (ret < 0)
3068 goto return1;
3069 if (*cur != 0)
3070 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003071 if (type->builtInType == XML_SCHEMAS_ULONG) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003072 if (hi >= 1844) {
3073 if (hi > 1844)
3074 goto return1;
3075 if (mi >= 67440737) {
3076 if (mi > 67440737)
3077 goto return1;
3078 if (lo > 9551615)
3079 goto return1;
3080 }
3081 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003082 } else if (type->builtInType == XML_SCHEMAS_UINT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003083 if (hi != 0)
3084 goto return1;
3085 if (mi >= 42) {
3086 if (mi > 42)
3087 goto return1;
3088 if (lo > 94967295)
3089 goto return1;
3090 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003091 } else if (type->builtInType == XML_SCHEMAS_USHORT) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003092 if ((mi != 0) || (hi != 0))
3093 goto return1;
3094 if (lo > 65535)
3095 goto return1;
Daniel Veillard01fa6152004-06-29 17:04:39 +00003096 } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003097 if ((mi != 0) || (hi != 0))
3098 goto return1;
3099 if (lo > 255)
3100 goto return1;
3101 }
3102 if (val != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +00003103 v = xmlSchemaNewValue(type->builtInType);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003104 if (v != NULL) {
3105 v->value.decimal.lo = lo;
3106 v->value.decimal.mi = mi;
3107 v->value.decimal.hi = hi;
3108 v->value.decimal.sign = 0;
3109 v->value.decimal.frac = 0;
William M. Brackec3b4b72005-03-15 15:50:17 +00003110 v->value.decimal.total = ret;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003111 *val = v;
3112 }
3113 }
3114 goto return0;
3115 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003116 }
3117
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003118 done:
3119 if (norm != NULL)
3120 xmlFree(norm);
3121 return (ret);
3122 return3:
3123 if (norm != NULL)
3124 xmlFree(norm);
3125 return (3);
3126 return1:
3127 if (norm != NULL)
3128 xmlFree(norm);
3129 return (1);
3130 return0:
3131 if (norm != NULL)
3132 xmlFree(norm);
3133 return (0);
3134 error:
3135 if (norm != NULL)
3136 xmlFree(norm);
3137 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003138}
3139
3140/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003141 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00003142 * @type: the predefined type
3143 * @value: the value to check
3144 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003145 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00003146 *
3147 * Check that a value conforms to the lexical space of the predefined type.
3148 * if true a value is computed and returned in @val.
3149 *
3150 * Returns 0 if this validates, a positive error code number otherwise
3151 * and -1 in case of internal or API error.
3152 */
3153int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003154xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
3155 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003156 return(xmlSchemaValAtomicType(type, value, val, node, 0,
3157 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00003158}
3159
3160/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00003161 * xmlSchemaValPredefTypeNodeNoNorm:
3162 * @type: the predefined type
3163 * @value: the value to check
3164 * @val: the return computed value
3165 * @node: the node containing the value
3166 *
3167 * Check that a value conforms to the lexical space of the predefined type.
3168 * if true a value is computed and returned in @val.
3169 * This one does apply any normalization to the value.
3170 *
3171 * Returns 0 if this validates, a positive error code number otherwise
3172 * and -1 in case of internal or API error.
3173 */
3174int
3175xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
3176 xmlSchemaValPtr *val, xmlNodePtr node) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003177 return(xmlSchemaValAtomicType(type, value, val, node, 1,
3178 XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
Daniel Veillardc0826a72004-08-10 14:17:33 +00003179}
3180
3181/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003182 * xmlSchemaValidatePredefinedType:
3183 * @type: the predefined type
3184 * @value: the value to check
3185 * @val: the return computed value
3186 *
3187 * Check that a value conforms to the lexical space of the predefined type.
3188 * if true a value is computed and returned in @val.
3189 *
3190 * Returns 0 if this validates, a positive error code number otherwise
3191 * and -1 in case of internal or API error.
3192 */
3193int
3194xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
3195 xmlSchemaValPtr *val) {
3196 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
3197}
3198
3199/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003200 * xmlSchemaCompareDecimals:
3201 * @x: a first decimal value
3202 * @y: a second decimal value
3203 *
3204 * Compare 2 decimals
3205 *
3206 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
3207 */
3208static int
3209xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
3210{
3211 xmlSchemaValPtr swp;
William M. Brack273670f2005-03-11 15:55:14 +00003212 int order = 1, integx, integy, dlen;
3213 unsigned long hi, mi, lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003214
William M. Brack273670f2005-03-11 15:55:14 +00003215 /*
3216 * First test: If x is -ve and not zero
3217 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003218 if ((x->value.decimal.sign) &&
3219 ((x->value.decimal.lo != 0) ||
3220 (x->value.decimal.mi != 0) ||
3221 (x->value.decimal.hi != 0))) {
William M. Brack273670f2005-03-11 15:55:14 +00003222 /*
3223 * Then if y is -ve and not zero reverse the compare
3224 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003225 if ((y->value.decimal.sign) &&
3226 ((y->value.decimal.lo != 0) ||
3227 (y->value.decimal.mi != 0) ||
3228 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00003229 order = -1;
William M. Brack273670f2005-03-11 15:55:14 +00003230 /*
3231 * Otherwise (y >= 0) we have the answer
3232 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003233 else
3234 return (-1);
William M. Brack273670f2005-03-11 15:55:14 +00003235 /*
3236 * If x is not -ve and y is -ve we have the answer
3237 */
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003238 } else if ((y->value.decimal.sign) &&
3239 ((y->value.decimal.lo != 0) ||
3240 (y->value.decimal.mi != 0) ||
3241 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003242 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00003243 }
William M. Brack273670f2005-03-11 15:55:14 +00003244 /*
3245 * If it's not simply determined by a difference in sign,
3246 * then we need to compare the actual values of the two nums.
3247 * To do this, we start by looking at the integral parts.
3248 * If the number of integral digits differ, then we have our
3249 * answer.
3250 */
3251 integx = x->value.decimal.total - x->value.decimal.frac;
3252 integy = y->value.decimal.total - y->value.decimal.frac;
3253 if (integx > integy)
3254 return order;
3255 else if (integy > integx)
3256 return -order;
3257 /*
3258 * If the number of integral digits is the same for both numbers,
3259 * then things get a little more complicated. We need to "normalize"
3260 * the numbers in order to properly compare them. To do this, we
3261 * look at the total length of each number (length => number of
3262 * significant digits), and divide the "shorter" by 10 (decreasing
3263 * the length) until they are of equal length.
3264 */
3265 dlen = x->value.decimal.total - y->value.decimal.total;
3266 if (dlen < 0) { /* y has more digits than x */
3267 swp = x;
3268 hi = y->value.decimal.hi;
3269 mi = y->value.decimal.mi;
3270 lo = y->value.decimal.lo;
3271 dlen = -dlen;
3272 order = -order;
3273 } else { /* x has more digits than y */
3274 swp = y;
3275 hi = x->value.decimal.hi;
3276 mi = x->value.decimal.mi;
3277 lo = x->value.decimal.lo;
Daniel Veillard4255d502002-04-16 15:50:10 +00003278 }
William M. Brack273670f2005-03-11 15:55:14 +00003279 while (dlen > 8) { /* in effect, right shift by 10**8 */
3280 lo = mi;
3281 mi = hi;
3282 hi = 0;
3283 dlen -= 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00003284 }
William M. Brack273670f2005-03-11 15:55:14 +00003285 while (dlen > 0) {
3286 unsigned long rem1, rem2;
3287 rem1 = (hi % 10) * 100000000L;
3288 hi = hi / 10;
3289 rem2 = (mi % 10) * 100000000L;
3290 mi = (mi + rem1) / 10;
3291 lo = (lo + rem2) / 10;
3292 dlen--;
3293 }
3294 if (hi > swp->value.decimal.hi) {
3295 return order;
3296 } else if (hi == swp->value.decimal.hi) {
3297 if (mi > swp->value.decimal.mi) {
3298 return order;
3299 } else if (mi == swp->value.decimal.mi) {
3300 if (lo > swp->value.decimal.lo) {
3301 return order;
3302 } else if (lo == swp->value.decimal.lo) {
3303 if (x->value.decimal.total == y->value.decimal.total) {
3304 return 0;
3305 } else {
3306 return order;
3307 }
3308 }
3309 }
3310 }
3311 return -order;
Daniel Veillard4255d502002-04-16 15:50:10 +00003312}
3313
3314/**
Daniel Veillard070803b2002-05-03 07:29:38 +00003315 * xmlSchemaCompareDurations:
3316 * @x: a first duration value
3317 * @y: a second duration value
3318 *
3319 * Compare 2 durations
3320 *
3321 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3322 * case of error
3323 */
3324static int
3325xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
3326{
3327 long carry, mon, day;
3328 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00003329 int invert = 1;
3330 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00003331 static const long dayRange [2][12] = {
3332 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
3333 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
3334
3335 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00003336 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00003337
3338 /* months */
3339 mon = x->value.dur.mon - y->value.dur.mon;
3340
3341 /* seconds */
3342 sec = x->value.dur.sec - y->value.dur.sec;
3343 carry = (long)sec / SECS_PER_DAY;
3344 sec -= (double)(carry * SECS_PER_DAY);
3345
3346 /* days */
3347 day = x->value.dur.day - y->value.dur.day + carry;
3348
3349 /* easy test */
3350 if (mon == 0) {
3351 if (day == 0)
3352 if (sec == 0.0)
3353 return 0;
3354 else if (sec < 0.0)
3355 return -1;
3356 else
3357 return 1;
3358 else if (day < 0)
3359 return -1;
3360 else
3361 return 1;
3362 }
3363
3364 if (mon > 0) {
3365 if ((day >= 0) && (sec >= 0.0))
3366 return 1;
3367 else {
3368 xmon = mon;
3369 xday = -day;
3370 }
3371 } else if ((day <= 0) && (sec <= 0.0)) {
3372 return -1;
3373 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00003374 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00003375 xmon = -mon;
3376 xday = day;
3377 }
3378
3379 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00003380 if (myear == 0) {
3381 minday = 0;
3382 maxday = 0;
3383 } else {
3384 maxday = 366 * ((myear + 3) / 4) +
3385 365 * ((myear - 1) % 4);
3386 minday = maxday - 1;
3387 }
3388
Daniel Veillard070803b2002-05-03 07:29:38 +00003389 xmon = xmon % 12;
3390 minday += dayRange[0][xmon];
3391 maxday += dayRange[1][xmon];
3392
Daniel Veillard80b19092003-03-28 13:29:53 +00003393 if ((maxday == minday) && (maxday == xday))
3394 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00003395 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00003396 return(-invert);
3397 if (minday > xday)
3398 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00003399
3400 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00003401 return 2;
3402}
3403
3404/*
3405 * macros for adding date/times and durations
3406 */
3407#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
3408#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
3409#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
3410#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
3411
3412/**
Daniel Veillard669adfc2004-05-29 20:12:46 +00003413 * xmlSchemaDupVal:
3414 * @v: the #xmlSchemaValPtr value to duplicate
3415 *
3416 * Makes a copy of @v. The calling program is responsible for freeing
3417 * the returned value.
3418 *
3419 * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
3420 */
3421static xmlSchemaValPtr
3422xmlSchemaDupVal (xmlSchemaValPtr v)
3423{
3424 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
3425 if (ret == NULL)
3426 return NULL;
3427
3428 memcpy(ret, v, sizeof(xmlSchemaVal));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003429 ret->next = NULL;
Daniel Veillard669adfc2004-05-29 20:12:46 +00003430 return ret;
3431}
3432
3433/**
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003434 * xmlSchemaCopyValue:
3435 * @val: the precomputed value to be copied
3436 *
3437 * Copies the precomputed value. This duplicates any string within.
3438 *
3439 * Returns the copy or NULL if a copy for a data-type is not implemented.
3440 */
3441xmlSchemaValPtr
3442xmlSchemaCopyValue(xmlSchemaValPtr val)
3443{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003444 xmlSchemaValPtr ret = NULL, prev = NULL, cur;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003445
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003446 /*
3447 * Copy the string values.
3448 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003449 while (val != NULL) {
3450 switch (val->type) {
3451 case XML_SCHEMAS_ANYTYPE:
3452 case XML_SCHEMAS_IDREFS:
3453 case XML_SCHEMAS_ENTITIES:
3454 case XML_SCHEMAS_NMTOKENS:
3455 xmlSchemaFreeValue(ret);
3456 return (NULL);
3457 case XML_SCHEMAS_ANYSIMPLETYPE:
3458 case XML_SCHEMAS_STRING:
3459 case XML_SCHEMAS_NORMSTRING:
3460 case XML_SCHEMAS_TOKEN:
3461 case XML_SCHEMAS_LANGUAGE:
3462 case XML_SCHEMAS_NAME:
3463 case XML_SCHEMAS_NCNAME:
3464 case XML_SCHEMAS_ID:
3465 case XML_SCHEMAS_IDREF:
3466 case XML_SCHEMAS_ENTITY:
3467 case XML_SCHEMAS_NMTOKEN:
3468 case XML_SCHEMAS_ANYURI:
3469 cur = xmlSchemaDupVal(val);
3470 if (val->value.str != NULL)
3471 cur->value.str = xmlStrdup(BAD_CAST val->value.str);
3472 break;
3473 case XML_SCHEMAS_QNAME:
3474 case XML_SCHEMAS_NOTATION:
3475 cur = xmlSchemaDupVal(val);
3476 if (val->value.qname.name != NULL)
3477 cur->value.qname.name =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003478 xmlStrdup(BAD_CAST val->value.qname.name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003479 if (val->value.qname.uri != NULL)
3480 cur->value.qname.uri =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003481 xmlStrdup(BAD_CAST val->value.qname.uri);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003482 break;
3483 case XML_SCHEMAS_HEXBINARY:
3484 cur = xmlSchemaDupVal(val);
3485 if (val->value.hex.str != NULL)
3486 cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
3487 break;
3488 case XML_SCHEMAS_BASE64BINARY:
3489 cur = xmlSchemaDupVal(val);
3490 if (val->value.base64.str != NULL)
3491 cur->value.base64.str =
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003492 xmlStrdup(BAD_CAST val->value.base64.str);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003493 break;
3494 default:
3495 cur = xmlSchemaDupVal(val);
3496 break;
3497 }
3498 if (ret == NULL)
3499 ret = cur;
3500 else
3501 prev->next = cur;
3502 prev = cur;
3503 val = val->next;
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003504 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003505 return (ret);
Kasimier T. Buchcik285ebab2005-03-04 18:04:59 +00003506}
3507
3508/**
Daniel Veillard5a872412002-05-22 06:40:27 +00003509 * _xmlSchemaDateAdd:
3510 * @dt: an #xmlSchemaValPtr
3511 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
3512 *
3513 * Compute a new date/time from @dt and @dur. This function assumes @dt
3514 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
Daniel Veillard669adfc2004-05-29 20:12:46 +00003515 * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
3516 * @dt. The calling program is responsible for freeing the returned value.
Daniel Veillard5a872412002-05-22 06:40:27 +00003517 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003518 * Returns a pointer to a new #xmlSchemaVal or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003519 */
3520static xmlSchemaValPtr
3521_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
3522{
Daniel Veillard669adfc2004-05-29 20:12:46 +00003523 xmlSchemaValPtr ret, tmp;
Daniel Veillard5a872412002-05-22 06:40:27 +00003524 long carry, tempdays, temp;
3525 xmlSchemaValDatePtr r, d;
3526 xmlSchemaValDurationPtr u;
3527
3528 if ((dt == NULL) || (dur == NULL))
3529 return NULL;
3530
3531 ret = xmlSchemaNewValue(dt->type);
3532 if (ret == NULL)
3533 return NULL;
3534
Daniel Veillard669adfc2004-05-29 20:12:46 +00003535 /* make a copy so we don't alter the original value */
3536 tmp = xmlSchemaDupVal(dt);
3537 if (tmp == NULL) {
3538 xmlSchemaFreeValue(ret);
3539 return NULL;
3540 }
3541
Daniel Veillard5a872412002-05-22 06:40:27 +00003542 r = &(ret->value.date);
Daniel Veillard669adfc2004-05-29 20:12:46 +00003543 d = &(tmp->value.date);
Daniel Veillard5a872412002-05-22 06:40:27 +00003544 u = &(dur->value.dur);
3545
3546 /* normalization */
3547 if (d->mon == 0)
3548 d->mon = 1;
3549
3550 /* normalize for time zone offset */
3551 u->sec -= (d->tzo * 60);
3552 d->tzo = 0;
3553
3554 /* normalization */
3555 if (d->day == 0)
3556 d->day = 1;
3557
3558 /* month */
3559 carry = d->mon + u->mon;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003560 r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
3561 carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003562
3563 /* year (may be modified later) */
3564 r->year = d->year + carry;
3565 if (r->year == 0) {
3566 if (d->year > 0)
3567 r->year--;
3568 else
3569 r->year++;
3570 }
3571
3572 /* time zone */
3573 r->tzo = d->tzo;
3574 r->tz_flag = d->tz_flag;
3575
3576 /* seconds */
3577 r->sec = d->sec + u->sec;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003578 carry = (long) FQUOTIENT((long)r->sec, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003579 if (r->sec != 0.0) {
3580 r->sec = MODULO(r->sec, 60.0);
3581 }
3582
3583 /* minute */
3584 carry += d->min;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003585 r->min = (unsigned int) MODULO(carry, 60);
3586 carry = (long) FQUOTIENT(carry, 60);
Daniel Veillard5a872412002-05-22 06:40:27 +00003587
3588 /* hours */
3589 carry += d->hour;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003590 r->hour = (unsigned int) MODULO(carry, 24);
3591 carry = (long)FQUOTIENT(carry, 24);
Daniel Veillard5a872412002-05-22 06:40:27 +00003592
3593 /*
3594 * days
3595 * Note we use tempdays because the temporary values may need more
3596 * than 5 bits
3597 */
3598 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
3599 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
3600 tempdays = MAX_DAYINMONTH(r->year, r->mon);
3601 else if (d->day < 1)
3602 tempdays = 1;
3603 else
3604 tempdays = d->day;
3605
3606 tempdays += u->day + carry;
3607
3608 while (1) {
3609 if (tempdays < 1) {
Daniel Veillardebe25d42004-03-25 09:35:49 +00003610 long tmon = (long) MODULO_RANGE(r->mon-1, 1, 13);
3611 long tyr = r->year + (long)FQUOTIENT_RANGE(r->mon-1, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003612 if (tyr == 0)
3613 tyr--;
3614 tempdays += MAX_DAYINMONTH(tyr, tmon);
3615 carry = -1;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003616 } else if (tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003617 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
3618 carry = 1;
3619 } else
3620 break;
3621
3622 temp = r->mon + carry;
Daniel Veillardebe25d42004-03-25 09:35:49 +00003623 r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
3624 r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13);
Daniel Veillard5a872412002-05-22 06:40:27 +00003625 if (r->year == 0) {
3626 if (temp < 1)
3627 r->year--;
3628 else
3629 r->year++;
3630 }
3631 }
3632
3633 r->day = tempdays;
3634
3635 /*
3636 * adjust the date/time type to the date values
3637 */
3638 if (ret->type != XML_SCHEMAS_DATETIME) {
3639 if ((r->hour) || (r->min) || (r->sec))
3640 ret->type = XML_SCHEMAS_DATETIME;
3641 else if (ret->type != XML_SCHEMAS_DATE) {
3642 if ((r->mon != 1) && (r->day != 1))
3643 ret->type = XML_SCHEMAS_DATE;
3644 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
3645 ret->type = XML_SCHEMAS_GYEARMONTH;
3646 }
3647 }
3648
Daniel Veillard669adfc2004-05-29 20:12:46 +00003649 xmlSchemaFreeValue(tmp);
Daniel Veillard5a872412002-05-22 06:40:27 +00003650
Daniel Veillard5a872412002-05-22 06:40:27 +00003651 return ret;
3652}
3653
3654/**
3655 * xmlSchemaDateNormalize:
Daniel Veillard669adfc2004-05-29 20:12:46 +00003656 * @dt: an #xmlSchemaValPtr of a date/time type value.
3657 * @offset: number of seconds to adjust @dt by.
Daniel Veillard5a872412002-05-22 06:40:27 +00003658 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003659 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3660 * the return value is a time-zone offset is present on @dt.
Daniel Veillard5a872412002-05-22 06:40:27 +00003661 *
Daniel Veillard669adfc2004-05-29 20:12:46 +00003662 * Returns a normalized copy of @dt or NULL if error.
Daniel Veillard5a872412002-05-22 06:40:27 +00003663 */
3664static xmlSchemaValPtr
3665xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
3666{
3667 xmlSchemaValPtr dur, ret;
3668
3669 if (dt == NULL)
3670 return NULL;
3671
3672 if (((dt->type != XML_SCHEMAS_TIME) &&
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00003673 (dt->type != XML_SCHEMAS_DATETIME) &&
3674 (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
Daniel Veillard5a872412002-05-22 06:40:27 +00003675 return xmlSchemaDupVal(dt);
3676
3677 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
3678 if (dur == NULL)
3679 return NULL;
3680
3681 dur->value.date.sec -= offset;
3682
3683 ret = _xmlSchemaDateAdd(dt, dur);
3684 if (ret == NULL)
3685 return NULL;
3686
3687 xmlSchemaFreeValue(dur);
3688
3689 /* ret->value.date.tzo = 0; */
3690 return ret;
3691}
3692
3693/**
3694 * _xmlSchemaDateCastYMToDays:
3695 * @dt: an #xmlSchemaValPtr
3696 *
3697 * Convert mon and year of @dt to total number of days. Take the
3698 * number of years since (or before) 1 AD and add the number of leap
3699 * years. This is a function because negative
3700 * years must be handled a little differently and there is no zero year.
3701 *
3702 * Returns number of days.
3703 */
3704static long
3705_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
3706{
3707 long ret;
Daniel Veillard49e89632004-09-23 16:24:36 +00003708 int mon;
Daniel Veillard5a872412002-05-22 06:40:27 +00003709
Daniel Veillard49e89632004-09-23 16:24:36 +00003710 mon = dt->value.date.mon;
3711 if (mon <= 0) mon = 1; /* normalization */
3712
3713 if (dt->value.date.year <= 0)
Daniel Veillard5a872412002-05-22 06:40:27 +00003714 ret = (dt->value.date.year * 365) +
3715 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
3716 ((dt->value.date.year+1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003717 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003718 else
3719 ret = ((dt->value.date.year-1) * 365) +
3720 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
3721 ((dt->value.date.year-1)/400)) +
Daniel Veillard49e89632004-09-23 16:24:36 +00003722 DAY_IN_YEAR(0, mon, dt->value.date.year);
Daniel Veillard5a872412002-05-22 06:40:27 +00003723
3724 return ret;
3725}
3726
3727/**
3728 * TIME_TO_NUMBER:
3729 * @dt: an #xmlSchemaValPtr
3730 *
3731 * Calculates the number of seconds in the time portion of @dt.
3732 *
3733 * Returns seconds.
3734 */
3735#define TIME_TO_NUMBER(dt) \
3736 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00003737 (dt->value.date.min * SECS_PER_MIN) + \
3738 (dt->value.date.tzo * SECS_PER_MIN)) + \
3739 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00003740
3741/**
3742 * xmlSchemaCompareDates:
3743 * @x: a first date/time value
3744 * @y: a second date/time value
3745 *
3746 * Compare 2 date/times
3747 *
3748 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3749 * case of error
3750 */
3751static int
3752xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
3753{
3754 unsigned char xmask, ymask, xor_mask, and_mask;
3755 xmlSchemaValPtr p1, p2, q1, q2;
3756 long p1d, p2d, q1d, q2d;
3757
3758 if ((x == NULL) || (y == NULL))
3759 return -2;
3760
3761 if (x->value.date.tz_flag) {
3762
3763 if (!y->value.date.tz_flag) {
3764 p1 = xmlSchemaDateNormalize(x, 0);
3765 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
3766 /* normalize y + 14:00 */
3767 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
3768
3769 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003770 if (p1d < q1d) {
3771 xmlSchemaFreeValue(p1);
3772 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003773 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003774 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003775 double sec;
3776
3777 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003778 if (sec < 0.0) {
3779 xmlSchemaFreeValue(p1);
3780 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003781 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003782 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003783 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00003784 /* normalize y - 14:00 */
3785 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
3786 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
3787 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003788 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00003789 else if (p1d == q2d) {
3790 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
3791 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003792 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00003793 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003794 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00003795 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003796 xmlSchemaFreeValue(p1);
3797 xmlSchemaFreeValue(q1);
3798 xmlSchemaFreeValue(q2);
3799 if (ret != 0)
3800 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00003801 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003802 } else {
3803 xmlSchemaFreeValue(p1);
3804 xmlSchemaFreeValue(q1);
3805 }
Daniel Veillard5a872412002-05-22 06:40:27 +00003806 }
3807 } else if (y->value.date.tz_flag) {
3808 q1 = xmlSchemaDateNormalize(y, 0);
3809 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
3810
3811 /* normalize x - 14:00 */
3812 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
3813 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
3814
Daniel Veillardfdc91562002-07-01 21:52:03 +00003815 if (p1d < q1d) {
3816 xmlSchemaFreeValue(p1);
3817 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003818 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003819 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003820 double sec;
3821
3822 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003823 if (sec < 0.0) {
3824 xmlSchemaFreeValue(p1);
3825 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003826 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003827 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003828 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00003829 /* normalize x + 14:00 */
3830 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
3831 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
3832
Daniel Veillard6560a422003-03-27 21:25:38 +00003833 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003834 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00003835 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00003836 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
3837 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003838 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00003839 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003840 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00003841 }
Daniel Veillard6560a422003-03-27 21:25:38 +00003842 xmlSchemaFreeValue(p1);
3843 xmlSchemaFreeValue(q1);
3844 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003845 if (ret != 0)
3846 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00003847 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003848 } else {
3849 xmlSchemaFreeValue(p1);
3850 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00003851 }
3852 }
3853
3854 /*
3855 * if the same type then calculate the difference
3856 */
3857 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003858 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00003859 q1 = xmlSchemaDateNormalize(y, 0);
3860 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
3861
3862 p1 = xmlSchemaDateNormalize(x, 0);
3863 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
3864
Daniel Veillardfdc91562002-07-01 21:52:03 +00003865 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003866 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003867 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003868 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00003869 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00003870 double sec;
3871
3872 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
3873 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003874 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00003875 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003876 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00003877
3878 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003879 xmlSchemaFreeValue(p1);
3880 xmlSchemaFreeValue(q1);
3881 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00003882 }
3883
3884 switch (x->type) {
3885 case XML_SCHEMAS_DATETIME:
3886 xmask = 0xf;
3887 break;
3888 case XML_SCHEMAS_DATE:
3889 xmask = 0x7;
3890 break;
3891 case XML_SCHEMAS_GYEAR:
3892 xmask = 0x1;
3893 break;
3894 case XML_SCHEMAS_GMONTH:
3895 xmask = 0x2;
3896 break;
3897 case XML_SCHEMAS_GDAY:
3898 xmask = 0x3;
3899 break;
3900 case XML_SCHEMAS_GYEARMONTH:
3901 xmask = 0x3;
3902 break;
3903 case XML_SCHEMAS_GMONTHDAY:
3904 xmask = 0x6;
3905 break;
3906 case XML_SCHEMAS_TIME:
3907 xmask = 0x8;
3908 break;
3909 default:
3910 xmask = 0;
3911 break;
3912 }
3913
3914 switch (y->type) {
3915 case XML_SCHEMAS_DATETIME:
3916 ymask = 0xf;
3917 break;
3918 case XML_SCHEMAS_DATE:
3919 ymask = 0x7;
3920 break;
3921 case XML_SCHEMAS_GYEAR:
3922 ymask = 0x1;
3923 break;
3924 case XML_SCHEMAS_GMONTH:
3925 ymask = 0x2;
3926 break;
3927 case XML_SCHEMAS_GDAY:
3928 ymask = 0x3;
3929 break;
3930 case XML_SCHEMAS_GYEARMONTH:
3931 ymask = 0x3;
3932 break;
3933 case XML_SCHEMAS_GMONTHDAY:
3934 ymask = 0x6;
3935 break;
3936 case XML_SCHEMAS_TIME:
3937 ymask = 0x8;
3938 break;
3939 default:
3940 ymask = 0;
3941 break;
3942 }
3943
3944 xor_mask = xmask ^ ymask; /* mark type differences */
3945 and_mask = xmask & ymask; /* mark field specification */
3946
3947 /* year */
3948 if (xor_mask & 1)
3949 return 2; /* indeterminate */
3950 else if (and_mask & 1) {
3951 if (x->value.date.year < y->value.date.year)
3952 return -1;
3953 else if (x->value.date.year > y->value.date.year)
3954 return 1;
3955 }
3956
3957 /* month */
3958 if (xor_mask & 2)
3959 return 2; /* indeterminate */
3960 else if (and_mask & 2) {
3961 if (x->value.date.mon < y->value.date.mon)
3962 return -1;
3963 else if (x->value.date.mon > y->value.date.mon)
3964 return 1;
3965 }
3966
3967 /* day */
3968 if (xor_mask & 4)
3969 return 2; /* indeterminate */
3970 else if (and_mask & 4) {
3971 if (x->value.date.day < y->value.date.day)
3972 return -1;
3973 else if (x->value.date.day > y->value.date.day)
3974 return 1;
3975 }
3976
3977 /* time */
3978 if (xor_mask & 8)
3979 return 2; /* indeterminate */
3980 else if (and_mask & 8) {
3981 if (x->value.date.hour < y->value.date.hour)
3982 return -1;
3983 else if (x->value.date.hour > y->value.date.hour)
3984 return 1;
3985 else if (x->value.date.min < y->value.date.min)
3986 return -1;
3987 else if (x->value.date.min > y->value.date.min)
3988 return 1;
3989 else if (x->value.date.sec < y->value.date.sec)
3990 return -1;
3991 else if (x->value.date.sec > y->value.date.sec)
3992 return 1;
3993 }
3994
Daniel Veillard070803b2002-05-03 07:29:38 +00003995 return 0;
3996}
3997
3998/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00003999 * xmlSchemaComparePreserveReplaceStrings:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004000 * @x: a first string value
4001 * @y: a second string value
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004002 * @invert: inverts the result if x < y or x > y.
4003 *
4004 * Compare 2 string for their normalized values.
4005 * @x is a string with whitespace of "preserve", @y is
4006 * a string with a whitespace of "replace". I.e. @x could
4007 * be an "xsd:string" and @y an "xsd:normalizedString".
4008 *
4009 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4010 * case of error
4011 */
4012static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004013xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
4014 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004015 int invert)
4016{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004017 int tmp;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004018
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004019 while ((*x != 0) && (*y != 0)) {
4020 if (IS_WSP_REPLACE_CH(*y)) {
4021 if (! IS_WSP_SPACE_CH(*x)) {
4022 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004023 if (invert)
4024 return(1);
4025 else
4026 return(-1);
4027 } else {
4028 if (invert)
4029 return(-1);
4030 else
4031 return(1);
4032 }
4033 }
4034 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004035 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004036 if (tmp < 0) {
4037 if (invert)
4038 return(1);
4039 else
4040 return(-1);
4041 }
4042 if (tmp > 0) {
4043 if (invert)
4044 return(-1);
4045 else
4046 return(1);
4047 }
4048 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004049 x++;
4050 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004051 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004052 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004053 if (invert)
4054 return(-1);
4055 else
4056 return(1);
4057 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004058 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004059 if (invert)
4060 return(1);
4061 else
4062 return(-1);
4063 }
4064 return(0);
4065}
4066
4067/**
4068 * xmlSchemaComparePreserveCollapseStrings:
4069 * @x: a first string value
4070 * @y: a second string value
4071 *
4072 * Compare 2 string for their normalized values.
4073 * @x is a string with whitespace of "preserve", @y is
4074 * a string with a whitespace of "collapse". I.e. @x could
4075 * be an "xsd:string" and @y an "xsd:normalizedString".
4076 *
4077 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4078 * case of error
4079 */
4080static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004081xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
4082 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004083 int invert)
4084{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004085 int tmp;
4086
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004087 /*
4088 * Skip leading blank chars of the collapsed string.
4089 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004090 while IS_WSP_BLANK_CH(*y)
4091 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004092
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004093 while ((*x != 0) && (*y != 0)) {
4094 if IS_WSP_BLANK_CH(*y) {
4095 if (! IS_WSP_SPACE_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004096 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004097 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004098 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004099 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004100 if (invert)
4101 return(1);
4102 else
4103 return(-1);
4104 } else {
4105 if (invert)
4106 return(-1);
4107 else
4108 return(1);
4109 }
4110 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004111 x++;
4112 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004113 /*
4114 * Skip contiguous blank chars of the collapsed string.
4115 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004116 while IS_WSP_BLANK_CH(*y)
4117 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004118 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004119 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004120 if (tmp < 0) {
4121 if (invert)
4122 return(1);
4123 else
4124 return(-1);
4125 }
4126 if (tmp > 0) {
4127 if (invert)
4128 return(-1);
4129 else
4130 return(1);
4131 }
4132 }
4133 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004134 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004135 if (invert)
4136 return(-1);
4137 else
4138 return(1);
4139 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004140 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004141 /*
4142 * Skip trailing blank chars of the collapsed string.
4143 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004144 while IS_WSP_BLANK_CH(*y)
4145 y++;
4146 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004147 if (invert)
4148 return(1);
4149 else
4150 return(-1);
4151 }
4152 }
4153 return(0);
4154}
4155
4156/**
4157 * xmlSchemaComparePreserveCollapseStrings:
4158 * @x: a first string value
4159 * @y: a second string value
4160 *
4161 * Compare 2 string for their normalized values.
4162 * @x is a string with whitespace of "preserve", @y is
4163 * a string with a whitespace of "collapse". I.e. @x could
4164 * be an "xsd:string" and @y an "xsd:normalizedString".
4165 *
4166 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4167 * case of error
4168 */
4169static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004170xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
4171 const xmlChar *y,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004172 int invert)
4173{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004174 int tmp;
4175
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004176 /*
4177 * Skip leading blank chars of the collapsed string.
4178 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004179 while IS_WSP_BLANK_CH(*y)
4180 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004181
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004182 while ((*x != 0) && (*y != 0)) {
4183 if IS_WSP_BLANK_CH(*y) {
4184 if (! IS_WSP_BLANK_CH(*x)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004185 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004186 * The yv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004187 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004188 if ((*x - 0x20) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004189 if (invert)
4190 return(1);
4191 else
4192 return(-1);
4193 } else {
4194 if (invert)
4195 return(-1);
4196 else
4197 return(1);
4198 }
4199 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004200 x++;
4201 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004202 /*
4203 * Skip contiguous blank chars of the collapsed string.
4204 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004205 while IS_WSP_BLANK_CH(*y)
4206 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004207 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004208 if IS_WSP_BLANK_CH(*x) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004209 /*
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004210 * The xv character would have been replaced to 0x20.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004211 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004212 if ((0x20 - *y) < 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004213 if (invert)
4214 return(1);
4215 else
4216 return(-1);
4217 } else {
4218 if (invert)
4219 return(-1);
4220 else
4221 return(1);
4222 }
4223 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004224 tmp = *x++ - *y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004225 if (tmp < 0)
4226 return(-1);
4227 if (tmp > 0)
4228 return(1);
4229 }
4230 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004231 if (*x != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004232 if (invert)
4233 return(-1);
4234 else
4235 return(1);
4236 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004237 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004238 /*
4239 * Skip trailing blank chars of the collapsed string.
4240 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004241 while IS_WSP_BLANK_CH(*y)
4242 y++;
4243 if (*y != 0) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004244 if (invert)
4245 return(1);
4246 else
4247 return(-1);
4248 }
4249 }
4250 return(0);
4251}
4252
4253
4254/**
4255 * xmlSchemaCompareReplacedStrings:
4256 * @x: a first string value
4257 * @y: a second string value
4258 *
4259 * Compare 2 string for their normalized values.
4260 *
4261 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4262 * case of error
4263 */
4264static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004265xmlSchemaCompareReplacedStrings(const xmlChar *x,
4266 const xmlChar *y)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004267{
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004268 int tmp;
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004269
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004270 while ((*x != 0) && (*y != 0)) {
4271 if IS_WSP_BLANK_CH(*y) {
4272 if (! IS_WSP_BLANK_CH(*x)) {
4273 if ((*x - 0x20) < 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004274 return(-1);
4275 else
4276 return(1);
4277 }
4278 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004279 if IS_WSP_BLANK_CH(*x) {
4280 if ((0x20 - *y) < 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004281 return(-1);
4282 else
4283 return(1);
4284 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004285 tmp = *x - *y;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004286 if (tmp < 0)
4287 return(-1);
4288 if (tmp > 0)
4289 return(1);
4290 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004291 x++;
4292 y++;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004293 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004294 if (*x != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004295 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004296 if (*y != 0)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004297 return(-1);
4298 return(0);
4299}
4300
4301/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004302 * xmlSchemaCompareNormStrings:
4303 * @x: a first string value
4304 * @y: a second string value
4305 *
4306 * Compare 2 string for their normalized values.
4307 *
4308 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4309 * case of error
4310 */
4311static int
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004312xmlSchemaCompareNormStrings(const xmlChar *x,
4313 const xmlChar *y) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00004314 int tmp;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004315
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004316 while (IS_BLANK_CH(*x)) x++;
4317 while (IS_BLANK_CH(*y)) y++;
4318 while ((*x != 0) && (*y != 0)) {
4319 if (IS_BLANK_CH(*x)) {
4320 if (!IS_BLANK_CH(*y)) {
4321 tmp = *x - *y;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004322 return(tmp);
4323 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004324 while (IS_BLANK_CH(*x)) x++;
4325 while (IS_BLANK_CH(*y)) y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004326 } else {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004327 tmp = *x++ - *y++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004328 if (tmp < 0)
4329 return(-1);
4330 if (tmp > 0)
4331 return(1);
4332 }
4333 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004334 if (*x != 0) {
4335 while (IS_BLANK_CH(*x)) x++;
4336 if (*x != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004337 return(1);
4338 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004339 if (*y != 0) {
4340 while (IS_BLANK_CH(*y)) y++;
4341 if (*y != 0)
Daniel Veillardc4c21552003-03-29 10:53:38 +00004342 return(-1);
4343 }
4344 return(0);
4345}
4346
4347/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004348 * xmlSchemaCompareFloats:
4349 * @x: a first float or double value
4350 * @y: a second float or double value
4351 *
4352 * Compare 2 values
4353 *
4354 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4355 * case of error
4356 */
4357static int
4358xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4359 double d1, d2;
4360
4361 if ((x == NULL) || (y == NULL))
4362 return(-2);
4363
4364 /*
4365 * Cast everything to doubles.
4366 */
4367 if (x->type == XML_SCHEMAS_DOUBLE)
4368 d1 = x->value.d;
4369 else if (x->type == XML_SCHEMAS_FLOAT)
4370 d1 = x->value.f;
4371 else
4372 return(-2);
4373
4374 if (y->type == XML_SCHEMAS_DOUBLE)
4375 d2 = y->value.d;
4376 else if (y->type == XML_SCHEMAS_FLOAT)
4377 d2 = y->value.f;
4378 else
4379 return(-2);
4380
4381 /*
4382 * Check for special cases.
4383 */
4384 if (xmlXPathIsNaN(d1)) {
4385 if (xmlXPathIsNaN(d2))
4386 return(0);
4387 return(1);
4388 }
4389 if (xmlXPathIsNaN(d2))
4390 return(-1);
4391 if (d1 == xmlXPathPINF) {
4392 if (d2 == xmlXPathPINF)
4393 return(0);
4394 return(1);
4395 }
4396 if (d2 == xmlXPathPINF)
4397 return(-1);
4398 if (d1 == xmlXPathNINF) {
4399 if (d2 == xmlXPathNINF)
4400 return(0);
4401 return(-1);
4402 }
4403 if (d2 == xmlXPathNINF)
4404 return(1);
4405
4406 /*
4407 * basic tests, the last one we should have equality, but
4408 * portability is more important than speed and handling
4409 * NaN or Inf in a portable way is always a challenge, so ...
4410 */
4411 if (d1 < d2)
4412 return(-1);
4413 if (d1 > d2)
4414 return(1);
4415 if (d1 == d2)
4416 return(0);
4417 return(2);
4418}
4419
4420/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004421 * xmlSchemaCompareValues:
4422 * @x: a first value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004423 * @xvalue: the first value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004424 * @xwtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004425 * @y: a second value
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004426 * @xvalue: the second value as a string (optional)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004427 * @ywtsp: the whitespace type
Daniel Veillard4255d502002-04-16 15:50:10 +00004428 *
4429 * Compare 2 values
4430 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004431 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4432 * comparable and -2 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004433 */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004434static int
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004435xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
4436 xmlSchemaValPtr x,
4437 const xmlChar *xvalue,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004438 xmlSchemaWhitespaceValueType xws,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004439 xmlSchemaValType ytype,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004440 xmlSchemaValPtr y,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004441 const xmlChar *yvalue,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004442 xmlSchemaWhitespaceValueType yws)
4443{
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004444 switch (xtype) {
Daniel Veillard80b19092003-03-28 13:29:53 +00004445 case XML_SCHEMAS_UNKNOWN:
William M. Brack2f2a6632004-08-20 23:09:47 +00004446 case XML_SCHEMAS_ANYTYPE:
Daniel Veillard80b19092003-03-28 13:29:53 +00004447 return(-2);
4448 case XML_SCHEMAS_INTEGER:
4449 case XML_SCHEMAS_NPINTEGER:
4450 case XML_SCHEMAS_NINTEGER:
4451 case XML_SCHEMAS_NNINTEGER:
4452 case XML_SCHEMAS_PINTEGER:
4453 case XML_SCHEMAS_INT:
4454 case XML_SCHEMAS_UINT:
4455 case XML_SCHEMAS_LONG:
4456 case XML_SCHEMAS_ULONG:
4457 case XML_SCHEMAS_SHORT:
4458 case XML_SCHEMAS_USHORT:
4459 case XML_SCHEMAS_BYTE:
4460 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00004461 case XML_SCHEMAS_DECIMAL:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004462 if ((x == NULL) || (y == NULL))
4463 return(-2);
4464 if (ytype == xtype)
Daniel Veillard80b19092003-03-28 13:29:53 +00004465 return(xmlSchemaCompareDecimals(x, y));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004466 if ((ytype == XML_SCHEMAS_DECIMAL) ||
4467 (ytype == XML_SCHEMAS_INTEGER) ||
4468 (ytype == XML_SCHEMAS_NPINTEGER) ||
4469 (ytype == XML_SCHEMAS_NINTEGER) ||
4470 (ytype == XML_SCHEMAS_NNINTEGER) ||
4471 (ytype == XML_SCHEMAS_PINTEGER) ||
4472 (ytype == XML_SCHEMAS_INT) ||
4473 (ytype == XML_SCHEMAS_UINT) ||
4474 (ytype == XML_SCHEMAS_LONG) ||
4475 (ytype == XML_SCHEMAS_ULONG) ||
4476 (ytype == XML_SCHEMAS_SHORT) ||
4477 (ytype == XML_SCHEMAS_USHORT) ||
4478 (ytype == XML_SCHEMAS_BYTE) ||
4479 (ytype == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00004480 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004481 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00004482 case XML_SCHEMAS_DURATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004483 if ((x == NULL) || (y == NULL))
4484 return(-2);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004485 if (ytype == XML_SCHEMAS_DURATION)
Daniel Veillard070803b2002-05-03 07:29:38 +00004486 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004487 return(-2);
4488 case XML_SCHEMAS_TIME:
4489 case XML_SCHEMAS_GDAY:
4490 case XML_SCHEMAS_GMONTH:
4491 case XML_SCHEMAS_GMONTHDAY:
4492 case XML_SCHEMAS_GYEAR:
4493 case XML_SCHEMAS_GYEARMONTH:
4494 case XML_SCHEMAS_DATE:
4495 case XML_SCHEMAS_DATETIME:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004496 if ((x == NULL) || (y == NULL))
4497 return(-2);
4498 if ((ytype == XML_SCHEMAS_DATETIME) ||
4499 (ytype == XML_SCHEMAS_TIME) ||
4500 (ytype == XML_SCHEMAS_GDAY) ||
4501 (ytype == XML_SCHEMAS_GMONTH) ||
4502 (ytype == XML_SCHEMAS_GMONTHDAY) ||
4503 (ytype == XML_SCHEMAS_GYEAR) ||
4504 (ytype == XML_SCHEMAS_DATE) ||
4505 (ytype == XML_SCHEMAS_GYEARMONTH))
Daniel Veillard5a872412002-05-22 06:40:27 +00004506 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00004507 return (-2);
Kasimier T. Buchcik2ee855d2005-03-07 11:14:14 +00004508 /*
4509 * Note that we will support comparison of string types against
4510 * anySimpleType as well.
4511 */
4512 case XML_SCHEMAS_ANYSIMPLETYPE:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004513 case XML_SCHEMAS_STRING:
4514 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00004515 case XML_SCHEMAS_TOKEN:
4516 case XML_SCHEMAS_LANGUAGE:
4517 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00004518 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00004519 case XML_SCHEMAS_NCNAME:
4520 case XML_SCHEMAS_ID:
4521 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00004522 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00004523 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004524 {
4525 const xmlChar *xv, *yv;
4526
4527 if (x == NULL)
4528 xv = xvalue;
4529 else
4530 xv = x->value.str;
4531 if (y == NULL)
4532 yv = yvalue;
4533 else
4534 yv = y->value.str;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004535 /*
4536 * TODO: Compare those against QName.
4537 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004538 if (ytype == XML_SCHEMAS_QNAME) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004539 TODO
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004540 if (y == NULL)
4541 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004542 return (-2);
4543 }
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004544 if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
4545 (ytype == XML_SCHEMAS_STRING) ||
4546 (ytype == XML_SCHEMAS_NORMSTRING) ||
4547 (ytype == XML_SCHEMAS_TOKEN) ||
4548 (ytype == XML_SCHEMAS_LANGUAGE) ||
4549 (ytype == XML_SCHEMAS_NMTOKEN) ||
4550 (ytype == XML_SCHEMAS_NAME) ||
4551 (ytype == XML_SCHEMAS_NCNAME) ||
4552 (ytype == XML_SCHEMAS_ID) ||
4553 (ytype == XML_SCHEMAS_IDREF) ||
4554 (ytype == XML_SCHEMAS_ENTITY) ||
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004555 (ytype == XML_SCHEMAS_ANYURI)) {
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004556
4557 if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4558
4559 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
4560 /* TODO: What about x < y or x > y. */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004561 if (xmlStrEqual(xv, yv))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004562 return (0);
4563 else
4564 return (2);
4565 } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004566 return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004567 else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004568 return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004569
4570 } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
4571
4572 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004573 return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004574 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004575 return (xmlSchemaCompareReplacedStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004576 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004577 return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004578
4579 } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
4580
4581 if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004582 return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004583 if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004584 return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004585 if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004586 return (xmlSchemaCompareNormStrings(xv, yv));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004587 } else
4588 return (-2);
4589
4590 }
Daniel Veillardc4c21552003-03-29 10:53:38 +00004591 return (-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004592 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004593 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004594 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004595 if ((x == NULL) || (y == NULL))
4596 return(-2);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00004597 if ((ytype == XML_SCHEMAS_QNAME) ||
4598 (ytype == XML_SCHEMAS_NOTATION)) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00004599 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
4600 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
4601 return(0);
4602 return(2);
4603 }
4604 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004605 case XML_SCHEMAS_FLOAT:
4606 case XML_SCHEMAS_DOUBLE:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004607 if ((x == NULL) || (y == NULL))
4608 return(-2);
4609 if ((ytype == XML_SCHEMAS_FLOAT) ||
4610 (ytype == XML_SCHEMAS_DOUBLE))
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004611 return (xmlSchemaCompareFloats(x, y));
4612 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004613 case XML_SCHEMAS_BOOLEAN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004614 if ((x == NULL) || (y == NULL))
4615 return(-2);
4616 if (ytype == XML_SCHEMAS_BOOLEAN) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00004617 if (x->value.b == y->value.b)
4618 return(0);
4619 if (x->value.b == 0)
4620 return(-1);
4621 return(1);
4622 }
4623 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00004624 case XML_SCHEMAS_HEXBINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004625 if ((x == NULL) || (y == NULL))
4626 return(-2);
4627 if (ytype == XML_SCHEMAS_HEXBINARY) {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00004628 if (x->value.hex.total == y->value.hex.total) {
4629 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
4630 if (ret > 0)
4631 return(1);
4632 else if (ret == 0)
4633 return(0);
4634 }
4635 else if (x->value.hex.total > y->value.hex.total)
4636 return(1);
4637
4638 return(-1);
4639 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00004640 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004641 case XML_SCHEMAS_BASE64BINARY:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004642 if ((x == NULL) || (y == NULL))
4643 return(-2);
4644 if (ytype == XML_SCHEMAS_BASE64BINARY) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00004645 if (x->value.base64.total == y->value.base64.total) {
4646 int ret = xmlStrcmp(x->value.base64.str,
4647 y->value.base64.str);
4648 if (ret > 0)
4649 return(1);
4650 else if (ret == 0)
4651 return(0);
4652 }
4653 else if (x->value.base64.total > y->value.base64.total)
4654 return(1);
4655 else
4656 return(-1);
4657 }
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004658 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00004659 case XML_SCHEMAS_IDREFS:
4660 case XML_SCHEMAS_ENTITIES:
4661 case XML_SCHEMAS_NMTOKENS:
4662 TODO
4663 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004664 }
Daniel Veillard5a872412002-05-22 06:40:27 +00004665 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00004666}
4667
4668/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004669 * xmlSchemaCompareValues:
4670 * @x: a first value
4671 * @y: a second value
4672 *
4673 * Compare 2 values
4674 *
4675 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4676 * case of error
4677 */
4678int
4679xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
4680 xmlSchemaWhitespaceValueType xws, yws;
4681
Daniel Veillard5e094142005-02-18 19:36:12 +00004682 if ((x == NULL) || (y == NULL))
4683 return(-2);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004684 if (x->type == XML_SCHEMAS_STRING)
4685 xws = XML_SCHEMA_WHITESPACE_PRESERVE;
4686 else if (x->type == XML_SCHEMAS_NORMSTRING)
4687 xws = XML_SCHEMA_WHITESPACE_REPLACE;
4688 else
4689 xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4690
4691 if (y->type == XML_SCHEMAS_STRING)
4692 yws = XML_SCHEMA_WHITESPACE_PRESERVE;
4693 else if (x->type == XML_SCHEMAS_NORMSTRING)
4694 yws = XML_SCHEMA_WHITESPACE_REPLACE;
4695 else
4696 yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
4697
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004698 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4699 y, NULL, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004700}
4701
4702/**
4703 * xmlSchemaCompareValuesWhtsp:
4704 * @x: a first value
4705 * @xws: the whitespace value of x
4706 * @y: a second value
4707 * @yws: the whitespace value of y
4708 *
4709 * Compare 2 values
4710 *
4711 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4712 * case of error
4713 */
4714int
4715xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004716 xmlSchemaWhitespaceValueType xws,
4717 xmlSchemaValPtr y,
4718 xmlSchemaWhitespaceValueType yws)
4719{
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00004720 if ((x == NULL) || (y == NULL))
4721 return(-2);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00004722 return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
4723 y, NULL, yws));
4724}
4725
4726/**
4727 * xmlSchemaCompareValuesWhtspExt:
4728 * @x: a first value
4729 * @xws: the whitespace value of x
4730 * @y: a second value
4731 * @yws: the whitespace value of y
4732 *
4733 * Compare 2 values
4734 *
4735 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4736 * case of error
4737 */
4738static int
4739xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
4740 xmlSchemaValPtr x,
4741 const xmlChar *xvalue,
4742 xmlSchemaWhitespaceValueType xws,
4743 xmlSchemaValType ytype,
4744 xmlSchemaValPtr y,
4745 const xmlChar *yvalue,
4746 xmlSchemaWhitespaceValueType yws)
4747{
4748 return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
4749 yvalue, yws));
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00004750}
4751
4752/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00004753 * xmlSchemaNormLen:
4754 * @value: a string
4755 *
4756 * Computes the UTF8 length of the normalized value of the string
4757 *
4758 * Returns the length or -1 in case of error.
4759 */
4760static int
4761xmlSchemaNormLen(const xmlChar *value) {
4762 const xmlChar *utf;
4763 int ret = 0;
4764
4765 if (value == NULL)
4766 return(-1);
4767 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00004768 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004769 while (*utf != 0) {
4770 if (utf[0] & 0x80) {
4771 if ((utf[1] & 0xc0) != 0x80)
4772 return(-1);
4773 if ((utf[0] & 0xe0) == 0xe0) {
4774 if ((utf[2] & 0xc0) != 0x80)
4775 return(-1);
4776 if ((utf[0] & 0xf0) == 0xf0) {
4777 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
4778 return(-1);
4779 utf += 4;
4780 } else {
4781 utf += 3;
4782 }
4783 } else {
4784 utf += 2;
4785 }
William M. Brack76e95df2003-10-18 16:20:14 +00004786 } else if (IS_BLANK_CH(*utf)) {
4787 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00004788 if (*utf == 0)
4789 break;
4790 } else {
4791 utf++;
4792 }
4793 ret++;
4794 }
4795 return(ret);
4796}
4797
Daniel Veillard6927b102004-10-27 17:29:04 +00004798/**
4799 * xmlSchemaGetFacetValueAsULong:
4800 * @facet: an schemas type facet
4801 *
4802 * Extract the value of a facet
4803 *
4804 * Returns the value as a long
4805 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00004806unsigned long
4807xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
4808{
4809 /*
4810 * TODO: Check if this is a decimal.
4811 */
William M. Brack094dd862004-11-14 14:28:34 +00004812 if (facet == NULL)
4813 return 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00004814 return ((unsigned long) facet->val->value.decimal.lo);
4815}
4816
Daniel Veillardc4c21552003-03-29 10:53:38 +00004817/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00004818 * xmlSchemaValidateListSimpleTypeFacet:
4819 * @facet: the facet to check
4820 * @value: the lexical repr of the value to validate
4821 * @actualLen: the number of list items
4822 * @expectedLen: the resulting expected number of list items
4823 *
4824 * Checks the value of a list simple type against a facet.
4825 *
4826 * Returns 0 if the value is valid, a positive error code
4827 * number otherwise and -1 in case of an internal error.
4828 */
4829int
4830xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
4831 const xmlChar *value,
4832 unsigned long actualLen,
4833 unsigned long *expectedLen)
4834{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004835 if (facet == NULL)
4836 return(-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004837 /*
4838 * TODO: Check if this will work with large numbers.
4839 * (compare value.decimal.mi and value.decimal.hi as well?).
4840 */
4841 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
4842 if (actualLen != facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004843 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004844 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00004845 return (XML_SCHEMAV_CVC_LENGTH_VALID);
4846 }
4847 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
4848 if (actualLen < facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004849 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004850 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00004851 return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
4852 }
4853 } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
4854 if (actualLen > facet->val->value.decimal.lo) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004855 if (expectedLen != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004856 *expectedLen = facet->val->value.decimal.lo;
Daniel Veillard01fa6152004-06-29 17:04:39 +00004857 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
4858 }
4859 } else
4860 /*
4861 * NOTE: That we can pass NULL as xmlSchemaValPtr to
4862 * xmlSchemaValidateFacet, since the remaining facet types
4863 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
4864 */
4865 return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
4866 return (0);
4867}
4868
4869/**
Daniel Veillard6927b102004-10-27 17:29:04 +00004870 * xmlSchemaValidateLengthFacet:
Daniel Veillardc0826a72004-08-10 14:17:33 +00004871 * @type: the built-in type
4872 * @facet: the facet to check
4873 * @value: the lexical repr. of the value to be validated
4874 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004875 * @ws: the whitespace type of the value
4876 * @length: the actual length of the value
4877 *
4878 * Checka a value against a "length", "minLength" and "maxLength"
4879 * facet; sets @length to the computed length of @value.
4880 *
4881 * Returns 0 if the value is valid, a positive error code
4882 * otherwise and -1 in case of an internal or API error.
4883 */
4884static int
4885xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
4886 xmlSchemaTypeType valType,
4887 const xmlChar *value,
4888 xmlSchemaValPtr val,
4889 unsigned long *length,
4890 xmlSchemaWhitespaceValueType ws)
4891{
4892 unsigned int len = 0;
4893
4894 if ((length == NULL) || (facet == NULL))
4895 return (-1);
4896 *length = 0;
4897 if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
4898 (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
4899 (facet->type != XML_SCHEMA_FACET_MINLENGTH))
4900 return (-1);
4901
4902 /*
4903 * TODO: length, maxLength and minLength must be of type
4904 * nonNegativeInteger only. Check if decimal is used somehow.
4905 */
4906 if ((facet->val == NULL) ||
4907 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
4908 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
4909 (facet->val->value.decimal.frac != 0)) {
4910 return(-1);
4911 }
4912 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
4913 len = val->value.hex.total;
4914 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
4915 len = val->value.base64.total;
4916 else {
4917 switch (valType) {
4918 case XML_SCHEMAS_STRING:
4919 case XML_SCHEMAS_NORMSTRING:
4920 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
4921 /*
4922 * This is to ensure API compatibility with the old
4923 * xmlSchemaValidateLengthFacet(). Anyway, this was and
4924 * is not the correct handling.
4925 * TODO: Get rid of this case somehow.
4926 */
4927 if (valType == XML_SCHEMAS_STRING)
4928 len = xmlUTF8Strlen(value);
4929 else
4930 len = xmlSchemaNormLen(value);
4931 } else if (value != NULL) {
4932 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
4933 len = xmlSchemaNormLen(value);
4934 else
4935 /*
4936 * Should be OK for "preserve" as well.
4937 */
4938 len = xmlUTF8Strlen(value);
4939 }
4940 break;
4941 case XML_SCHEMAS_IDREF:
4942 case XML_SCHEMAS_TOKEN:
4943 case XML_SCHEMAS_LANGUAGE:
4944 case XML_SCHEMAS_NMTOKEN:
4945 case XML_SCHEMAS_NAME:
4946 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00004947 case XML_SCHEMAS_ID:
4948 /*
4949 * FIXME: What exactly to do with anyURI?
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004950 */
4951 case XML_SCHEMAS_ANYURI:
4952 if (value != NULL)
4953 len = xmlSchemaNormLen(value);
4954 break;
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00004955 case XML_SCHEMAS_QNAME:
4956 case XML_SCHEMAS_NOTATION:
4957 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004958 * For QName and NOTATION, those facets are
4959 * deprecated and should be ignored.
Kasimier T. Buchcikbd6c3f72005-05-25 17:29:36 +00004960 */
4961 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00004962 default:
4963 TODO
4964 }
4965 }
4966 *length = (unsigned long) len;
4967 /*
4968 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
4969 */
4970 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
4971 if (len != facet->val->value.decimal.lo)
4972 return(XML_SCHEMAV_CVC_LENGTH_VALID);
4973 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
4974 if (len < facet->val->value.decimal.lo)
4975 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
4976 } else {
4977 if (len > facet->val->value.decimal.lo)
4978 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
4979 }
4980
4981 return (0);
4982}
4983
4984/**
4985 * xmlSchemaValidateLengthFacet:
4986 * @type: the built-in type
4987 * @facet: the facet to check
4988 * @value: the lexical repr. of the value to be validated
4989 * @val: the precomputed value
Daniel Veillardc0826a72004-08-10 14:17:33 +00004990 * @length: the actual length of the value
4991 *
4992 * Checka a value against a "length", "minLength" and "maxLength"
4993 * facet; sets @length to the computed length of @value.
4994 *
4995 * Returns 0 if the value is valid, a positive error code
4996 * otherwise and -1 in case of an internal or API error.
4997 */
4998int
4999xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
5000 xmlSchemaFacetPtr facet,
5001 const xmlChar *value,
5002 xmlSchemaValPtr val,
5003 unsigned long *length)
5004{
Daniel Veillardcc5e2332005-03-16 21:55:35 +00005005 if (type == NULL)
5006 return(-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005007 return (xmlSchemaValidateLengthFacetInternal(facet,
5008 type->builtInType, value, val, length,
5009 XML_SCHEMA_WHITESPACE_UNKNOWN));
5010}
Daniel Veillardc0826a72004-08-10 14:17:33 +00005011
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005012/**
5013 * xmlSchemaValidateLengthFacetWhtsp:
5014 * @facet: the facet to check
5015 * @valType: the built-in type
5016 * @value: the lexical repr. of the value to be validated
5017 * @val: the precomputed value
5018 * @ws: the whitespace type of the value
5019 * @length: the actual length of the value
5020 *
5021 * Checka a value against a "length", "minLength" and "maxLength"
5022 * facet; sets @length to the computed length of @value.
5023 *
5024 * Returns 0 if the value is valid, a positive error code
5025 * otherwise and -1 in case of an internal or API error.
5026 */
5027int
5028xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
5029 xmlSchemaValType valType,
5030 const xmlChar *value,
5031 xmlSchemaValPtr val,
5032 unsigned long *length,
5033 xmlSchemaWhitespaceValueType ws)
5034{
5035 return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
5036 length, ws));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005037}
5038
5039/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005040 * xmlSchemaValidateFacetInternal:
Daniel Veillard4255d502002-04-16 15:50:10 +00005041 * @facet: the facet to check
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005042 * @fws: the whitespace type of the facet's value
5043 * @valType: the built-in type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005044 * @value: the lexical repr of the value to validate
5045 * @val: the precomputed value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005046 * @ws: the whitespace type of the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005047 *
5048 * Check a value against a facet condition
5049 *
5050 * Returns 0 if the element is schemas valid, a positive error code
5051 * number otherwise and -1 in case of internal or API error.
5052 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005053static int
5054xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
5055 xmlSchemaWhitespaceValueType fws,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005056 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005057 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005058 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005059 xmlSchemaWhitespaceValueType ws)
Daniel Veillard4255d502002-04-16 15:50:10 +00005060{
5061 int ret;
5062
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005063 if (facet == NULL)
5064 return(-1);
5065
Daniel Veillard4255d502002-04-16 15:50:10 +00005066 switch (facet->type) {
5067 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005068 /*
5069 * NOTE that for patterns, the @value needs to be the normalized
5070 * value, *not* the lexical initial value or the canonical value.
5071 */
5072 if (value == NULL)
5073 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005074 ret = xmlRegexpExec(facet->regexp, value);
5075 if (ret == 1)
5076 return(0);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005077 if (ret == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005078 return(XML_SCHEMAV_CVC_PATTERN_VALID);
Daniel Veillard4255d502002-04-16 15:50:10 +00005079 return(ret);
5080 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
5081 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005082 if (ret == -2)
Daniel Veillard4255d502002-04-16 15:50:10 +00005083 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005084 if (ret == -1)
5085 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005086 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005087 case XML_SCHEMA_FACET_MAXINCLUSIVE:
5088 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005089 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005090 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005091 if ((ret == -1) || (ret == 0))
5092 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005093 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005094 case XML_SCHEMA_FACET_MINEXCLUSIVE:
5095 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005096 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005097 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005098 if (ret == 1)
5099 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005100 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
Daniel Veillard070803b2002-05-03 07:29:38 +00005101 case XML_SCHEMA_FACET_MININCLUSIVE:
5102 ret = xmlSchemaCompareValues(val, facet->val);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005103 if (ret == -2)
Daniel Veillard070803b2002-05-03 07:29:38 +00005104 return(-1);
Daniel Veillard070803b2002-05-03 07:29:38 +00005105 if ((ret == 1) || (ret == 0))
5106 return(0);
Daniel Veillard01fa6152004-06-29 17:04:39 +00005107 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
Daniel Veillard8651f532002-04-17 09:06:27 +00005108 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005109 /* TODO whitespaces */
Daniel Veillard01fa6152004-06-29 17:04:39 +00005110 /*
5111 * NOTE: Whitespace should be handled to normalize
5112 * the value to be validated against a the facets;
5113 * not to normalize the value in-between.
5114 */
Daniel Veillard8651f532002-04-17 09:06:27 +00005115 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00005116 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005117 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5118 /*
5119 * This is to ensure API compatibility with the old
5120 * xmlSchemaValidateFacet().
5121 * TODO: Get rid of this case.
5122 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005123 if ((facet->value != NULL) &&
5124 (xmlStrEqual(facet->value, value)))
5125 return(0);
5126 } else {
5127 ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
5128 facet->val, facet->value, fws, valType, val,
5129 value, ws);
5130 if (ret == -2)
5131 return(-1);
5132 if (ret == 0)
5133 return(0);
5134 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005135 return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005136 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005137 /*
5138 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5139 * then any {value} is facet-valid."
5140 */
5141 if ((valType == XML_SCHEMAS_QNAME) ||
5142 (valType == XML_SCHEMAS_NOTATION))
5143 return (0);
5144 /* No break on purpose. */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005145 case XML_SCHEMA_FACET_MAXLENGTH:
5146 case XML_SCHEMA_FACET_MINLENGTH: {
5147 unsigned int len = 0;
5148
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005149 if ((valType == XML_SCHEMAS_QNAME) ||
5150 (valType == XML_SCHEMAS_NOTATION))
5151 return (0);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005152 /*
5153 * TODO: length, maxLength and minLength must be of type
5154 * nonNegativeInteger only. Check if decimal is used somehow.
5155 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005156 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005157 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5158 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005159 (facet->val->value.decimal.frac != 0)) {
5160 return(-1);
5161 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005162 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005163 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00005164 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
5165 len = val->value.base64.total;
5166 else {
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005167 switch (valType) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005168 case XML_SCHEMAS_STRING:
5169 case XML_SCHEMAS_NORMSTRING:
5170 if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
5171 /*
5172 * This is to ensure API compatibility with the old
5173 * xmlSchemaValidateFacet(). Anyway, this was and
5174 * is not the correct handling.
5175 * TODO: Get rid of this case somehow.
5176 */
5177 if (valType == XML_SCHEMAS_STRING)
5178 len = xmlUTF8Strlen(value);
5179 else
5180 len = xmlSchemaNormLen(value);
5181 } else if (value != NULL) {
5182 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5183 len = xmlSchemaNormLen(value);
5184 else
5185 /*
5186 * Should be OK for "preserve" as well.
5187 */
5188 len = xmlUTF8Strlen(value);
5189 }
5190 break;
5191 case XML_SCHEMAS_IDREF:
Daniel Veillard560c2a42003-07-06 21:13:49 +00005192 case XML_SCHEMAS_TOKEN:
5193 case XML_SCHEMAS_LANGUAGE:
5194 case XML_SCHEMAS_NMTOKEN:
5195 case XML_SCHEMAS_NAME:
5196 case XML_SCHEMAS_NCNAME:
5197 case XML_SCHEMAS_ID:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005198 case XML_SCHEMAS_ANYURI:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005199 if (value != NULL)
5200 len = xmlSchemaNormLen(value);
5201 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00005202 default:
5203 TODO
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00005204 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005205 }
5206 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005207 if (len != facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005208 return(XML_SCHEMAV_CVC_LENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005209 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005210 if (len < facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005211 return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005212 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005213 if (len > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005214 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005215 }
5216 break;
5217 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00005218 case XML_SCHEMA_FACET_TOTALDIGITS:
5219 case XML_SCHEMA_FACET_FRACTIONDIGITS:
5220
5221 if ((facet->val == NULL) ||
5222 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
5223 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
5224 (facet->val->value.decimal.frac != 0)) {
5225 return(-1);
5226 }
5227 if ((val == NULL) ||
5228 ((val->type != XML_SCHEMAS_DECIMAL) &&
5229 (val->type != XML_SCHEMAS_INTEGER) &&
5230 (val->type != XML_SCHEMAS_NPINTEGER) &&
5231 (val->type != XML_SCHEMAS_NINTEGER) &&
5232 (val->type != XML_SCHEMAS_NNINTEGER) &&
5233 (val->type != XML_SCHEMAS_PINTEGER) &&
5234 (val->type != XML_SCHEMAS_INT) &&
5235 (val->type != XML_SCHEMAS_UINT) &&
5236 (val->type != XML_SCHEMAS_LONG) &&
5237 (val->type != XML_SCHEMAS_ULONG) &&
5238 (val->type != XML_SCHEMAS_SHORT) &&
5239 (val->type != XML_SCHEMAS_USHORT) &&
5240 (val->type != XML_SCHEMAS_BYTE) &&
5241 (val->type != XML_SCHEMAS_UBYTE))) {
5242 return(-1);
5243 }
5244 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
5245 if (val->value.decimal.total > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005246 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005247
5248 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
5249 if (val->value.decimal.frac > facet->val->value.decimal.lo)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005250 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
Daniel Veillard560c2a42003-07-06 21:13:49 +00005251 }
5252 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00005253 default:
5254 TODO
5255 }
5256 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00005257
Daniel Veillard4255d502002-04-16 15:50:10 +00005258}
5259
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005260/**
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005261 * xmlSchemaValidateFacet:
5262 * @base: the base type
5263 * @facet: the facet to check
5264 * @value: the lexical repr of the value to validate
5265 * @val: the precomputed value
5266 *
5267 * Check a value against a facet condition
5268 *
5269 * Returns 0 if the element is schemas valid, a positive error code
5270 * number otherwise and -1 in case of internal or API error.
5271 */
5272int
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005273xmlSchemaValidateFacet(xmlSchemaTypePtr base,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005274 xmlSchemaFacetPtr facet,
5275 const xmlChar *value,
5276 xmlSchemaValPtr val)
5277{
5278 /*
5279 * This tries to ensure API compatibility regarding the old
5280 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5281 * xmlSchemaValidateFacetWhtsp().
5282 */
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005283 if (val != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005284 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005285 XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005286 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005287 else if (base != NULL)
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005288 return(xmlSchemaValidateFacetInternal(facet,
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005289 XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005290 XML_SCHEMA_WHITESPACE_UNKNOWN));
Kasimier T. Buchcik9dbb0ce2005-03-16 16:39:23 +00005291 return(-1);
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005292}
5293
5294/**
5295 * xmlSchemaValidateFacetWhtsp:
5296 * @facet: the facet to check
5297 * @fws: the whitespace type of the facet's value
5298 * @valType: the built-in type of the value
5299 * @value: the lexical (or normalized for pattern) repr of the value to validate
5300 * @val: the precomputed value
5301 * @ws: the whitespace type of the value
5302 *
5303 * Check a value against a facet condition. This takes value normalization
5304 * according to the specified whitespace types into account.
5305 * Note that @value needs to be the *normalized* value if the facet
5306 * is of type "pattern".
5307 *
5308 * Returns 0 if the element is schemas valid, a positive error code
5309 * number otherwise and -1 in case of internal or API error.
5310 */
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005311int
5312xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
5313 xmlSchemaWhitespaceValueType fws,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005314 xmlSchemaValType valType,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005315 const xmlChar *value,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005316 xmlSchemaValPtr val,
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005317 xmlSchemaWhitespaceValueType ws)
5318{
5319 return(xmlSchemaValidateFacetInternal(facet, fws, valType,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005320 value, val, ws));
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005321}
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005322
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005323#if 0
5324#ifndef DBL_DIG
5325#define DBL_DIG 16
5326#endif
5327#ifndef DBL_EPSILON
5328#define DBL_EPSILON 1E-9
5329#endif
5330
5331#define INTEGER_DIGITS DBL_DIG
5332#define FRACTION_DIGITS (DBL_DIG + 1)
5333#define EXPONENT_DIGITS (3 + 2)
5334
5335/**
5336 * xmlXPathFormatNumber:
5337 * @number: number to format
5338 * @buffer: output buffer
5339 * @buffersize: size of output buffer
5340 *
5341 * Convert the number into a string representation.
5342 */
5343static void
5344xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
5345{
5346 switch (xmlXPathIsInf(number)) {
5347 case 1:
5348 if (buffersize > (int)sizeof("INF"))
5349 snprintf(buffer, buffersize, "INF");
5350 break;
5351 case -1:
5352 if (buffersize > (int)sizeof("-INF"))
5353 snprintf(buffer, buffersize, "-INF");
5354 break;
5355 default:
5356 if (xmlXPathIsNaN(number)) {
5357 if (buffersize > (int)sizeof("NaN"))
5358 snprintf(buffer, buffersize, "NaN");
5359 } else if (number == 0) {
5360 snprintf(buffer, buffersize, "0.0E0");
5361 } else {
5362 /* 3 is sign, decimal point, and terminating zero */
5363 char work[DBL_DIG + EXPONENT_DIGITS + 3];
5364 int integer_place, fraction_place;
5365 char *ptr;
5366 char *after_fraction;
5367 double absolute_value;
5368 int size;
5369
5370 absolute_value = fabs(number);
5371
5372 /*
5373 * Result is in work, and after_fraction points
5374 * just past the fractional part.
5375 * Use scientific notation
5376 */
5377 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
5378 fraction_place = DBL_DIG - 1;
5379 snprintf(work, sizeof(work),"%*.*e",
5380 integer_place, fraction_place, number);
5381 after_fraction = strchr(work + DBL_DIG, 'e');
5382 /* Remove fractional trailing zeroes */
5383 ptr = after_fraction;
5384 while (*(--ptr) == '0')
5385 ;
5386 if (*ptr != '.')
5387 ptr++;
5388 while ((*ptr++ = *after_fraction++) != 0);
5389
5390 /* Finally copy result back to caller */
5391 size = strlen(work) + 1;
5392 if (size > buffersize) {
5393 work[buffersize - 1] = 0;
5394 size = buffersize;
5395 }
5396 memmove(buffer, work, size);
5397 }
5398 break;
5399 }
5400}
5401#endif
5402
Kasimier T. Buchcik6c81d0e2005-03-15 14:58:11 +00005403/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005404 * xmlSchemaGetCanonValue:
5405 * @val: the precomputed value
5406 * @retValue: the returned value
5407 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005408 * Get a the cononical lexical representation of the value.
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005409 * The caller has to FREE the returned retValue.
5410 *
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005411 * WARNING: Some value types are not supported yet, resulting
5412 * in a @retValue of "???".
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005413 *
5414 * TODO: XML Schema 1.0 does not define canonical representations
5415 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5416 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005417 *
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005418 *
5419 * Returns 0 if the value could be built, 1 if the value type is
5420 * not supported yet and -1 in case of API errors.
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005421 */
5422int
Daniel Veillardb5839c32005-02-19 18:27:14 +00005423xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005424{
Daniel Veillardb5839c32005-02-19 18:27:14 +00005425 if ((retValue == NULL) || (val == NULL))
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005426 return (-1);
5427 *retValue = NULL;
5428 switch (val->type) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005429 case XML_SCHEMAS_STRING:
5430 if (val->value.str == NULL)
5431 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5432 else
5433 *retValue =
5434 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
5435 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005436 case XML_SCHEMAS_NORMSTRING:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005437 if (val->value.str == NULL)
5438 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5439 else {
5440 *retValue = xmlSchemaWhiteSpaceReplace(
5441 (const xmlChar *) val->value.str);
5442 if ((*retValue) == NULL)
5443 *retValue = BAD_CAST xmlStrdup(
5444 (const xmlChar *) val->value.str);
5445 }
5446 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005447 case XML_SCHEMAS_TOKEN:
5448 case XML_SCHEMAS_LANGUAGE:
5449 case XML_SCHEMAS_NMTOKEN:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005450 case XML_SCHEMAS_NAME:
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005451 case XML_SCHEMAS_NCNAME:
5452 case XML_SCHEMAS_ID:
5453 case XML_SCHEMAS_IDREF:
5454 case XML_SCHEMAS_ENTITY:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005455 case XML_SCHEMAS_NOTATION: /* Unclear */
5456 case XML_SCHEMAS_ANYURI: /* Unclear */
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005457 if (val->value.str == NULL)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005458 return (-1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005459 *retValue =
5460 BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
5461 if (*retValue == NULL)
5462 *retValue =
5463 BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005464 break;
5465 case XML_SCHEMAS_QNAME:
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005466 /* TODO: Unclear in XML Schema 1.0. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005467 if (val->value.qname.uri == NULL) {
5468 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
5469 return (0);
5470 } else {
5471 *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
5472 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5473 BAD_CAST val->value.qname.uri);
5474 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5475 BAD_CAST "}");
5476 *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
5477 BAD_CAST val->value.qname.uri);
5478 }
5479 break;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005480 case XML_SCHEMAS_DECIMAL:
5481 /*
5482 * TODO: Lookout for a more simple implementation.
5483 */
5484 if ((val->value.decimal.total == 1) &&
5485 (val->value.decimal.lo == 0)) {
5486 *retValue = xmlStrdup(BAD_CAST "0.0");
5487 } else {
5488 xmlSchemaValDecimal dec = val->value.decimal;
5489 int bufsize;
5490 char *buf = NULL, *offs;
5491
5492 /* Add room for the decimal point as well. */
5493 bufsize = dec.total + 2;
5494 if (dec.sign)
5495 bufsize++;
5496 /* Add room for leading/trailing zero. */
5497 if ((dec.frac == 0) || (dec.frac == dec.total))
5498 bufsize++;
5499 buf = xmlMalloc(bufsize);
5500 offs = buf;
5501 if (dec.sign)
5502 *offs++ = '-';
5503 if (dec.frac == dec.total) {
5504 *offs++ = '0';
5505 *offs++ = '.';
5506 }
5507 if (dec.hi != 0)
5508 snprintf(offs, bufsize - (offs - buf),
5509 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5510 else if (dec.mi != 0)
5511 snprintf(offs, bufsize - (offs - buf),
5512 "%lu%lu", dec.mi, dec.lo);
5513 else
5514 snprintf(offs, bufsize - (offs - buf),
5515 "%lu", dec.lo);
5516
5517 if (dec.frac != 0) {
5518 if (dec.frac != dec.total) {
5519 int diff = dec.total - dec.frac;
5520 /*
5521 * Insert the decimal point.
5522 */
5523 memmove(offs + diff + 1, offs + diff, dec.frac +1);
5524 offs[diff] = '.';
5525 } else {
5526 unsigned int i = 0;
5527 /*
5528 * Insert missing zeroes behind the decimal point.
5529 */
5530 while (*(offs + i) != 0)
5531 i++;
5532 if (i < dec.total) {
5533 memmove(offs + (dec.total - i), offs, i +1);
5534 memset(offs, '0', dec.total - i);
5535 }
5536 }
5537 } else {
5538 /*
5539 * Append decimal point and zero.
5540 */
5541 offs = buf + bufsize - 1;
5542 *offs-- = 0;
5543 *offs-- = '0';
5544 *offs-- = '.';
5545 }
5546 *retValue = BAD_CAST buf;
5547 }
5548 break;
5549 case XML_SCHEMAS_INTEGER:
5550 case XML_SCHEMAS_PINTEGER:
5551 case XML_SCHEMAS_NPINTEGER:
5552 case XML_SCHEMAS_NINTEGER:
5553 case XML_SCHEMAS_NNINTEGER:
5554 case XML_SCHEMAS_LONG:
5555 case XML_SCHEMAS_BYTE:
5556 case XML_SCHEMAS_SHORT:
5557 case XML_SCHEMAS_INT:
5558 case XML_SCHEMAS_UINT:
5559 case XML_SCHEMAS_ULONG:
5560 case XML_SCHEMAS_USHORT:
5561 case XML_SCHEMAS_UBYTE:
5562 if ((val->value.decimal.total == 1) &&
5563 (val->value.decimal.lo == 0))
5564 *retValue = xmlStrdup(BAD_CAST "0");
5565 else {
5566 xmlSchemaValDecimal dec = val->value.decimal;
5567 int bufsize = dec.total + 1;
5568
5569 /* Add room for the decimal point as well. */
5570 if (dec.sign)
5571 bufsize++;
5572 *retValue = xmlMalloc(bufsize);
5573 if (dec.hi != 0) {
5574 if (dec.sign)
5575 snprintf((char *) *retValue, bufsize,
5576 "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5577 else
5578 snprintf((char *) *retValue, bufsize,
5579 "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
5580 } else if (dec.mi != 0) {
5581 if (dec.sign)
5582 snprintf((char *) *retValue, bufsize,
5583 "-%lu%lu", dec.mi, dec.lo);
5584 else
5585 snprintf((char *) *retValue, bufsize,
5586 "%lu%lu", dec.mi, dec.lo);
5587 } else {
5588 if (dec.sign)
5589 snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
5590 else
5591 snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
5592 }
5593 }
5594 break;
5595 case XML_SCHEMAS_BOOLEAN:
5596 if (val->value.b)
5597 *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
5598 else
5599 *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
5600 break;
5601 case XML_SCHEMAS_DURATION: {
5602 char buf[100];
5603 unsigned long year;
5604 unsigned long mon, day, hour = 0, min = 0;
5605 double sec = 0, left;
5606
5607 /* TODO: Unclear in XML Schema 1.0 */
5608 /*
5609 * TODO: This results in a normalized output of the value
5610 * - which is NOT conformant to the spec -
5611 * since the exact values of each property are not
5612 * recoverable. Think about extending the structure to
5613 * provide a field for every property.
5614 */
5615 year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
5616 mon = labs(val->value.dur.mon) - 12 * year;
5617
5618 day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
5619 left = fabs(val->value.dur.sec) - day * 86400;
5620 if (left > 0) {
5621 hour = (unsigned long) FQUOTIENT(left, 3600);
5622 left = left - (hour * 3600);
5623 if (left > 0) {
5624 min = (unsigned long) FQUOTIENT(left, 60);
5625 sec = left - (min * 60);
5626 }
5627 }
5628 if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
5629 snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5630 year, mon, day, hour, min, sec);
5631 else
5632 snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5633 year, mon, day, hour, min, sec);
5634 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5635 }
5636 break;
5637 case XML_SCHEMAS_GYEAR: {
5638 char buf[30];
5639 /* TODO: Unclear in XML Schema 1.0 */
5640 /* TODO: What to do with the timezone? */
5641 snprintf(buf, 30, "%04ld", val->value.date.year);
5642 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5643 }
5644 break;
5645 case XML_SCHEMAS_GMONTH: {
5646 /* TODO: Unclear in XML Schema 1.0 */
5647 /* TODO: What to do with the timezone? */
5648 *retValue = xmlMalloc(5);
5649 snprintf((char *) *retValue, 6, "--%02u",
5650 val->value.date.mon);
5651 }
5652 break;
5653 case XML_SCHEMAS_GDAY: {
5654 /* TODO: Unclear in XML Schema 1.0 */
5655 /* TODO: What to do with the timezone? */
5656 *retValue = xmlMalloc(6);
5657 snprintf((char *) *retValue, 6, "---%02u",
5658 val->value.date.day);
5659 }
5660 break;
5661 case XML_SCHEMAS_GMONTHDAY: {
5662 /* TODO: Unclear in XML Schema 1.0 */
5663 /* TODO: What to do with the timezone? */
5664 *retValue = xmlMalloc(8);
5665 snprintf((char *) *retValue, 8, "--%02u-%02u",
5666 val->value.date.mon, val->value.date.day);
5667 }
5668 break;
5669 case XML_SCHEMAS_GYEARMONTH: {
5670 char buf[35];
5671 /* TODO: Unclear in XML Schema 1.0 */
5672 /* TODO: What to do with the timezone? */
5673 if (val->value.date.year < 0)
5674 snprintf(buf, 35, "-%04ld-%02u",
5675 labs(val->value.date.year),
5676 val->value.date.mon);
5677 else
5678 snprintf(buf, 35, "%04ld-%02u",
5679 val->value.date.year, val->value.date.mon);
5680 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5681 }
5682 break;
5683 case XML_SCHEMAS_TIME:
5684 {
5685 char buf[30];
5686
5687 if (val->value.date.tz_flag) {
5688 xmlSchemaValPtr norm;
5689
5690 norm = xmlSchemaDateNormalize(val, 0);
5691 if (norm == NULL)
5692 return (-1);
5693 /*
5694 * TODO: Check if "%.14g" is portable.
5695 */
5696 snprintf(buf, 30,
5697 "%02u:%02u:%02.14gZ",
5698 norm->value.date.hour,
5699 norm->value.date.min,
5700 norm->value.date.sec);
5701 xmlSchemaFreeValue(norm);
5702 } else {
5703 snprintf(buf, 30,
5704 "%02u:%02u:%02.14g",
5705 val->value.date.hour,
5706 val->value.date.min,
5707 val->value.date.sec);
5708 }
5709 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5710 }
5711 break;
5712 case XML_SCHEMAS_DATE:
5713 {
5714 char buf[30];
5715
5716 if (val->value.date.tz_flag) {
5717 xmlSchemaValPtr norm;
5718
5719 norm = xmlSchemaDateNormalize(val, 0);
5720 if (norm == NULL)
5721 return (-1);
5722 /*
5723 * TODO: Append the canonical value of the
5724 * recoverable timezone and not "Z".
5725 */
5726 snprintf(buf, 30,
5727 "%04ld:%02u:%02uZ",
5728 norm->value.date.year, norm->value.date.mon,
5729 norm->value.date.day);
5730 xmlSchemaFreeValue(norm);
5731 } else {
5732 snprintf(buf, 30,
5733 "%04ld:%02u:%02u",
5734 val->value.date.year, val->value.date.mon,
5735 val->value.date.day);
5736 }
5737 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5738 }
5739 break;
5740 case XML_SCHEMAS_DATETIME:
5741 {
5742 char buf[50];
5743
5744 if (val->value.date.tz_flag) {
5745 xmlSchemaValPtr norm;
5746
5747 norm = xmlSchemaDateNormalize(val, 0);
5748 if (norm == NULL)
5749 return (-1);
5750 /*
5751 * TODO: Check if "%.14g" is portable.
5752 */
5753 snprintf(buf, 50,
5754 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
5755 norm->value.date.year, norm->value.date.mon,
5756 norm->value.date.day, norm->value.date.hour,
5757 norm->value.date.min, norm->value.date.sec);
5758 xmlSchemaFreeValue(norm);
5759 } else {
5760 snprintf(buf, 50,
5761 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
5762 val->value.date.year, val->value.date.mon,
5763 val->value.date.day, val->value.date.hour,
5764 val->value.date.min, val->value.date.sec);
5765 }
5766 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5767 }
5768 break;
5769 case XML_SCHEMAS_HEXBINARY:
5770 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
5771 break;
5772 case XML_SCHEMAS_BASE64BINARY:
5773 /*
5774 * TODO: Is the following spec piece implemented?:
5775 * SPEC: "Note: For some values the canonical form defined
5776 * above does not conform to [RFC 2045], which requires breaking
5777 * with linefeeds at appropriate intervals."
5778 */
5779 *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
5780 break;
5781 case XML_SCHEMAS_FLOAT: {
5782 char buf[30];
5783 /*
5784 * |m| < 16777216, -149 <= e <= 104.
5785 * TODO: Handle, NaN, INF, -INF. The format is not
5786 * yet conformant. The c type float does not cover
5787 * the whole range.
5788 */
5789 snprintf(buf, 30, "%01.14e", val->value.f);
5790 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5791 }
5792 break;
5793 case XML_SCHEMAS_DOUBLE: {
5794 char buf[40];
5795 /* |m| < 9007199254740992, -1075 <= e <= 970 */
5796 /*
5797 * TODO: Handle, NaN, INF, -INF. The format is not
5798 * yet conformant. The c type float does not cover
5799 * the whole range.
5800 */
5801 snprintf(buf, 40, "%01.14e", val->value.d);
5802 *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
5803 }
5804 break;
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005805 default:
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005806 *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005807 return (1);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005808 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005809 return (0);
5810}
5811
Daniel Veillardbda59572005-04-01 17:15:17 +00005812/**
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +00005813 * xmlSchemaGetCanonValueWhtsp:
5814 * @val: the precomputed value
5815 * @retValue: the returned value
5816 * @ws: the whitespace type of the value
5817 *
5818 * Get a the cononical representation of the value.
5819 * The caller has to free the returned @retValue.
5820 *
5821 * Returns 0 if the value could be built, 1 if the value type is
5822 * not supported yet and -1 in case of API errors.
5823 */
5824int
5825xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
5826 const xmlChar **retValue,
5827 xmlSchemaWhitespaceValueType ws)
5828{
5829 if ((retValue == NULL) || (val == NULL))
5830 return (-1);
5831 if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
5832 (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
5833 return (-1);
5834
5835 *retValue = NULL;
5836 switch (val->type) {
5837 case XML_SCHEMAS_STRING:
5838 if (val->value.str == NULL)
5839 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5840 else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5841 *retValue = xmlSchemaCollapseString(val->value.str);
5842 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
5843 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
5844 if ((*retValue) == NULL)
5845 *retValue = BAD_CAST xmlStrdup(val->value.str);
5846 break;
5847 case XML_SCHEMAS_NORMSTRING:
5848 if (val->value.str == NULL)
5849 *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
5850 else {
5851 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
5852 *retValue = xmlSchemaCollapseString(val->value.str);
5853 else
5854 *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
5855 if ((*retValue) == NULL)
5856 *retValue = BAD_CAST xmlStrdup(val->value.str);
5857 }
5858 break;
5859 default:
5860 return (xmlSchemaGetCanonValue(val, retValue));
5861 }
5862 return (0);
5863}
5864
5865/**
Daniel Veillardbda59572005-04-01 17:15:17 +00005866 * xmlSchemaGetValType:
5867 * @val: a schemas value
5868 *
5869 * Accessor for the type of a value
5870 *
5871 * Returns the xmlSchemaValType of the value
5872 */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005873xmlSchemaValType
5874xmlSchemaGetValType(xmlSchemaValPtr val)
5875{
Daniel Veillardbda59572005-04-01 17:15:17 +00005876 if (val == NULL)
5877 return(XML_SCHEMAS_UNKNOWN);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00005878 return (val->type);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +00005879}
5880
Daniel Veillard5d4644e2005-04-01 13:11:58 +00005881#define bottom_xmlschemastypes
5882#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00005883#endif /* LIBXML_SCHEMAS_ENABLED */