blob: 6ffba470d66f4328de3c25bd95333e513d1a7246 [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
31
Daniel Veillard4255d502002-04-16 15:50:10 +000032#define DEBUG
33
34#define TODO \
35 xmlGenericError(xmlGenericErrorContext, \
36 "Unimplemented block at %s:%d\n", \
37 __FILE__, __LINE__);
38
39#define XML_SCHEMAS_NAMESPACE_NAME \
40 (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
41
42typedef enum {
43 XML_SCHEMAS_UNKNOWN = 0,
44 XML_SCHEMAS_STRING,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000045 XML_SCHEMAS_NORMSTRING,
Daniel Veillard4255d502002-04-16 15:50:10 +000046 XML_SCHEMAS_DECIMAL,
Daniel Veillard070803b2002-05-03 07:29:38 +000047 XML_SCHEMAS_TIME,
48 XML_SCHEMAS_GDAY,
49 XML_SCHEMAS_GMONTH,
50 XML_SCHEMAS_GMONTHDAY,
51 XML_SCHEMAS_GYEAR,
52 XML_SCHEMAS_GYEARMONTH,
53 XML_SCHEMAS_DATE,
54 XML_SCHEMAS_DATETIME,
55 XML_SCHEMAS_DURATION,
Daniel Veillard84d70a42002-09-16 10:51:38 +000056 XML_SCHEMAS_FLOAT,
57 XML_SCHEMAS_DOUBLE,
Daniel Veillardc5a70f22003-02-06 23:41:59 +000058 XML_SCHEMAS_BOOLEAN,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000059 XML_SCHEMAS_TOKEN,
60 XML_SCHEMAS_LANGUAGE,
61 XML_SCHEMAS_NMTOKEN,
62 XML_SCHEMAS_NMTOKENS,
63 XML_SCHEMAS_NAME,
64 XML_SCHEMAS_QNAME,
65 XML_SCHEMAS_NCNAME,
66 XML_SCHEMAS_ID,
67 XML_SCHEMAS_IDREF,
68 XML_SCHEMAS_IDREFS,
69 XML_SCHEMAS_ENTITY,
70 XML_SCHEMAS_ENTITIES,
71 XML_SCHEMAS_NOTATION,
72 XML_SCHEMAS_ANYURI,
73 XML_SCHEMAS_INTEGER,
74 XML_SCHEMAS_NPINTEGER,
75 XML_SCHEMAS_NINTEGER,
76 XML_SCHEMAS_NNINTEGER,
77 XML_SCHEMAS_PINTEGER,
Daniel Veillard96a4b252003-02-06 08:22:32 +000078 XML_SCHEMAS_INT,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000079 XML_SCHEMAS_UINT,
80 XML_SCHEMAS_LONG,
81 XML_SCHEMAS_ULONG,
82 XML_SCHEMAS_SHORT,
83 XML_SCHEMAS_USHORT,
84 XML_SCHEMAS_BYTE,
Daniel Veillard560c2a42003-07-06 21:13:49 +000085 XML_SCHEMAS_UBYTE,
Daniel Veillard1ac24d32003-08-27 14:15:15 +000086 XML_SCHEMAS_HEXBINARY,
87 XML_SCHEMAS_BASE64BINARY
Daniel Veillard4255d502002-04-16 15:50:10 +000088} xmlSchemaValType;
89
Daniel Veillard5f704af2003-03-05 10:01:43 +000090static unsigned long powten[10] = {
Daniel Veillard4255d502002-04-16 15:50:10 +000091 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000L,
92 100000000L, 1000000000L
93};
94
Daniel Veillard070803b2002-05-03 07:29:38 +000095/* Date value */
96typedef struct _xmlSchemaValDate xmlSchemaValDate;
97typedef xmlSchemaValDate *xmlSchemaValDatePtr;
98struct _xmlSchemaValDate {
99 long year;
100 unsigned int mon :4; /* 1 <= mon <= 12 */
101 unsigned int day :5; /* 1 <= day <= 31 */
102 unsigned int hour :5; /* 0 <= hour <= 23 */
103 unsigned int min :6; /* 0 <= min <= 59 */
104 double sec;
Daniel Veillarda77cf712003-05-09 23:09:55 +0000105 unsigned int tz_flag :1; /* is tzo explicitely set? */
Daniel Veillard070803b2002-05-03 07:29:38 +0000106 int tzo :11; /* -1440 <= tzo <= 1440 */
107};
108
109/* Duration value */
110typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
111typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
112struct _xmlSchemaValDuration {
113 long mon; /* mon stores years also */
114 long day;
115 double sec; /* sec stores min and hour also */
116};
117
Daniel Veillard4255d502002-04-16 15:50:10 +0000118typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
119typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
120struct _xmlSchemaValDecimal {
121 /* would use long long but not portable */
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000122 unsigned long lo;
123 unsigned long mi;
124 unsigned long hi;
Daniel Veillard4255d502002-04-16 15:50:10 +0000125 unsigned int extra;
Daniel Veillard5a872412002-05-22 06:40:27 +0000126 unsigned int sign:1;
William M. Brackc1939562003-08-05 15:52:22 +0000127 unsigned int frac:7;
128 unsigned int total:8;
Daniel Veillard4255d502002-04-16 15:50:10 +0000129};
130
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000131typedef struct _xmlSchemaValQName xmlSchemaValQName;
132typedef xmlSchemaValQName *xmlSchemaValQNamePtr;
133struct _xmlSchemaValQName {
134 xmlChar *name;
135 xmlChar *uri;
136};
137
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000138typedef struct _xmlSchemaValHex xmlSchemaValHex;
139typedef xmlSchemaValHex *xmlSchemaValHexPtr;
140struct _xmlSchemaValHex {
141 xmlChar *str;
142 unsigned int total;
143};
144
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000145typedef struct _xmlSchemaValBase64 xmlSchemaValBase64;
146typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr;
147struct _xmlSchemaValBase64 {
148 xmlChar *str;
149 unsigned int total;
150};
151
Daniel Veillard4255d502002-04-16 15:50:10 +0000152struct _xmlSchemaVal {
153 xmlSchemaValType type;
154 union {
Daniel Veillard5a872412002-05-22 06:40:27 +0000155 xmlSchemaValDecimal decimal;
Daniel Veillard070803b2002-05-03 07:29:38 +0000156 xmlSchemaValDate date;
157 xmlSchemaValDuration dur;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000158 xmlSchemaValQName qname;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000159 xmlSchemaValHex hex;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000160 xmlSchemaValBase64 base64;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000161 float f;
162 double d;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000163 int b;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000164 xmlChar *str;
Daniel Veillard4255d502002-04-16 15:50:10 +0000165 } value;
166};
167
168static int xmlSchemaTypesInitialized = 0;
169static xmlHashTablePtr xmlSchemaTypesBank = NULL;
170
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000171/*
172 * Basic types
173 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000174static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
175static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
176static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
177static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000178static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000179static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000180static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL;
181static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL;
182static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL;
183static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL;
184static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL;
185static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL;
186static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000187static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000188static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000189static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
Daniel Veillard560c2a42003-07-06 21:13:49 +0000190static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000191static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000192static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000193
194/*
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000195 * Derived types
196 */
197static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
198static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL;
199static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL;
200static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
201static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL;
202static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL;
203static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL;
204static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL;
205static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL;
206static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL;
207static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL;
208static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL;
209static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000210static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL;
211static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL;
212static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL;
213static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL;
214static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000215static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000216static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
217static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
218static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000219static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL;
220static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000221static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000222static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
223static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000224
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000225/************************************************************************
226 * *
227 * Datatype error handlers *
228 * *
229 ************************************************************************/
230/**
231 * xmlSchemaTypeErrMemory:
232 * @extra: extra informations
233 *
234 * Handle an out of memory condition
235 */
236static void
237xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra)
238{
239 __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra);
240}
241
242/************************************************************************
243 * *
244 * Base types support *
245 * *
246 ************************************************************************/
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000247/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000248 * xmlSchemaInitBasicType:
249 * @name: the type name
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000250 * @type: the value type associated
Daniel Veillard4255d502002-04-16 15:50:10 +0000251 *
252 * Initialize one default type
253 */
254static xmlSchemaTypePtr
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000255xmlSchemaInitBasicType(const char *name, xmlSchemaValType type) {
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;
Daniel Veillard4255d502002-04-16 15:50:10 +0000265 ret->type = XML_SCHEMA_TYPE_BASIC;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000266 ret->flags = type;
Daniel Veillard4255d502002-04-16 15:50:10 +0000267 ret->contentType = XML_SCHEMA_CONTENT_BASIC;
268 xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
269 XML_SCHEMAS_NAMESPACE_NAME, ret);
270 return(ret);
271}
272
273/*
274 * xmlSchemaInitTypes:
275 *
276 * Initialize the default XML Schemas type library
277 */
278void
Daniel Veillard6560a422003-03-27 21:25:38 +0000279xmlSchemaInitTypes(void)
280{
Daniel Veillard4255d502002-04-16 15:50:10 +0000281 if (xmlSchemaTypesInitialized != 0)
Daniel Veillard6560a422003-03-27 21:25:38 +0000282 return;
Daniel Veillard4255d502002-04-16 15:50:10 +0000283 xmlSchemaTypesBank = xmlHashCreate(40);
Daniel Veillard6560a422003-03-27 21:25:38 +0000284
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000285 /*
286 * primitive datatypes
287 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000288 xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
Daniel Veillard6560a422003-03-27 21:25:38 +0000289 XML_SCHEMAS_STRING);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000290 xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
Daniel Veillard6560a422003-03-27 21:25:38 +0000291 XML_SCHEMAS_UNKNOWN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000292 xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
Daniel Veillard6560a422003-03-27 21:25:38 +0000293 XML_SCHEMAS_UNKNOWN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000294 xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
Daniel Veillard6560a422003-03-27 21:25:38 +0000295 XML_SCHEMAS_DECIMAL);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000296 xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
Daniel Veillard6560a422003-03-27 21:25:38 +0000297 XML_SCHEMAS_DATE);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000298 xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
Daniel Veillard6560a422003-03-27 21:25:38 +0000299 XML_SCHEMAS_DATETIME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000300 xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
Daniel Veillard6560a422003-03-27 21:25:38 +0000301 XML_SCHEMAS_TIME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000302 xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
Daniel Veillard6560a422003-03-27 21:25:38 +0000303 XML_SCHEMAS_GYEAR);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000304 xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
Daniel Veillard6560a422003-03-27 21:25:38 +0000305 XML_SCHEMAS_GYEARMONTH);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000306 xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
Daniel Veillard6560a422003-03-27 21:25:38 +0000307 XML_SCHEMAS_GMONTH);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000308 xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
Daniel Veillard6560a422003-03-27 21:25:38 +0000309 XML_SCHEMAS_GMONTHDAY);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000310 xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
Daniel Veillard6560a422003-03-27 21:25:38 +0000311 XML_SCHEMAS_GDAY);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000312 xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
Daniel Veillard6560a422003-03-27 21:25:38 +0000313 XML_SCHEMAS_DURATION);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000314 xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
Daniel Veillard6560a422003-03-27 21:25:38 +0000315 XML_SCHEMAS_FLOAT);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000316 xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
Daniel Veillard6560a422003-03-27 21:25:38 +0000317 XML_SCHEMAS_DOUBLE);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000318 xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
Daniel Veillard6560a422003-03-27 21:25:38 +0000319 XML_SCHEMAS_BOOLEAN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000320 xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
Daniel Veillard6560a422003-03-27 21:25:38 +0000321 XML_SCHEMAS_ANYURI);
Daniel Veillard560c2a42003-07-06 21:13:49 +0000322 xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
323 XML_SCHEMAS_HEXBINARY);
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000324 xmlSchemaTypeBase64BinaryDef
325 = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY);
Daniel Veillard4255d502002-04-16 15:50:10 +0000326
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000327 /*
328 * derived datatypes
329 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000330 xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
Daniel Veillard6560a422003-03-27 21:25:38 +0000331 XML_SCHEMAS_INTEGER);;
332 xmlSchemaTypeNonPositiveIntegerDef =
333 xmlSchemaInitBasicType("nonPositiveInteger",
334 XML_SCHEMAS_NPINTEGER);;
335 xmlSchemaTypeNegativeIntegerDef =
336 xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER);;
337 xmlSchemaTypeLongDef =
338 xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG);;
339 xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT);;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000340 xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
Daniel Veillard6560a422003-03-27 21:25:38 +0000341 XML_SCHEMAS_SHORT);;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000342 xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
Daniel Veillard6560a422003-03-27 21:25:38 +0000343 XML_SCHEMAS_BYTE);;
344 xmlSchemaTypeNonNegativeIntegerDef =
345 xmlSchemaInitBasicType("nonNegativeInteger",
346 XML_SCHEMAS_NNINTEGER);
347 xmlSchemaTypeUnsignedLongDef =
348 xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG);;
349 xmlSchemaTypeUnsignedIntDef =
350 xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT);;
351 xmlSchemaTypeUnsignedShortDef =
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000352 xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT);;
Daniel Veillard6560a422003-03-27 21:25:38 +0000353 xmlSchemaTypeUnsignedByteDef =
354 xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE);;
355 xmlSchemaTypePositiveIntegerDef =
356 xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER);
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000357
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000358 xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
Daniel Veillard6560a422003-03-27 21:25:38 +0000359 XML_SCHEMAS_NORMSTRING);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000360 xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
Daniel Veillard6560a422003-03-27 21:25:38 +0000361 XML_SCHEMAS_TOKEN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000362 xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
Daniel Veillard6560a422003-03-27 21:25:38 +0000363 XML_SCHEMAS_LANGUAGE);
364 xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000365 xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
Daniel Veillard6560a422003-03-27 21:25:38 +0000366 XML_SCHEMAS_IDREF);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000367 xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
Daniel Veillard6560a422003-03-27 21:25:38 +0000368 XML_SCHEMAS_IDREFS);
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000369 xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
Daniel Veillard6560a422003-03-27 21:25:38 +0000370 XML_SCHEMAS_ENTITY);
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000371 xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
Daniel Veillard6560a422003-03-27 21:25:38 +0000372 XML_SCHEMAS_ENTITIES);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000373 xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
374 XML_SCHEMAS_NOTATION);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000375 xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
Daniel Veillard6560a422003-03-27 21:25:38 +0000376 XML_SCHEMAS_NAME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000377 xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
Daniel Veillard6560a422003-03-27 21:25:38 +0000378 XML_SCHEMAS_QNAME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000379 xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
Daniel Veillard6560a422003-03-27 21:25:38 +0000380 XML_SCHEMAS_NCNAME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000381 xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
Daniel Veillard6560a422003-03-27 21:25:38 +0000382 XML_SCHEMAS_NMTOKEN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000383 xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
Daniel Veillard6560a422003-03-27 21:25:38 +0000384 XML_SCHEMAS_NMTOKENS);
Daniel Veillard4255d502002-04-16 15:50:10 +0000385 xmlSchemaTypesInitialized = 1;
386}
387
388/**
389 * xmlSchemaCleanupTypes:
390 *
391 * Cleanup the default XML Schemas type library
392 */
393void
394xmlSchemaCleanupTypes(void) {
395 if (xmlSchemaTypesInitialized == 0)
396 return;
397 xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
398 xmlSchemaTypesInitialized = 0;
399}
400
401/**
402 * xmlSchemaNewValue:
403 * @type: the value type
404 *
405 * Allocate a new simple type value
406 *
407 * Returns a pointer to the new value or NULL in case of error
408 */
409static xmlSchemaValPtr
410xmlSchemaNewValue(xmlSchemaValType type) {
411 xmlSchemaValPtr value;
412
413 value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
414 if (value == NULL) {
415 return(NULL);
416 }
417 memset(value, 0, sizeof(xmlSchemaVal));
418 value->type = type;
419 return(value);
420}
421
422/**
423 * xmlSchemaFreeValue:
424 * @value: the value to free
425 *
426 * Cleanup the default XML Schemas type library
427 */
428void
429xmlSchemaFreeValue(xmlSchemaValPtr value) {
430 if (value == NULL)
431 return;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000432 switch (value->type) {
433 case XML_SCHEMAS_STRING:
434 case XML_SCHEMAS_NORMSTRING:
435 case XML_SCHEMAS_TOKEN:
436 case XML_SCHEMAS_LANGUAGE:
437 case XML_SCHEMAS_NMTOKEN:
438 case XML_SCHEMAS_NMTOKENS:
439 case XML_SCHEMAS_NAME:
Daniel Veillardc4c21552003-03-29 10:53:38 +0000440 case XML_SCHEMAS_NCNAME:
441 case XML_SCHEMAS_ID:
442 case XML_SCHEMAS_IDREF:
443 case XML_SCHEMAS_IDREFS:
444 case XML_SCHEMAS_ENTITY:
445 case XML_SCHEMAS_ENTITIES:
446 case XML_SCHEMAS_NOTATION:
447 case XML_SCHEMAS_ANYURI:
448 if (value->value.str != NULL)
449 xmlFree(value->value.str);
450 break;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000451 case XML_SCHEMAS_QNAME:
452 if (value->value.qname.uri != NULL)
453 xmlFree(value->value.qname.uri);
454 if (value->value.qname.name != NULL)
455 xmlFree(value->value.qname.name);
456 break;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000457 case XML_SCHEMAS_HEXBINARY:
458 if (value->value.hex.str != NULL)
459 xmlFree(value->value.hex.str);
460 break;
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000461 case XML_SCHEMAS_BASE64BINARY:
462 if (value->value.base64.str != NULL)
463 xmlFree(value->value.base64.str);
464 break;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000465 default:
466 break;
467 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000468 xmlFree(value);
469}
470
471/**
472 * xmlSchemaGetPredefinedType:
473 * @name: the type name
474 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
475 *
476 * Lookup a type in the default XML Schemas type library
477 *
478 * Returns the type if found, NULL otherwise
479 */
480xmlSchemaTypePtr
481xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
482 if (xmlSchemaTypesInitialized == 0)
483 xmlSchemaInitTypes();
484 if (name == NULL)
485 return(NULL);
486 return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
487}
Daniel Veillard070803b2002-05-03 07:29:38 +0000488
489/****************************************************************
490 * *
491 * Convenience macros and functions *
492 * *
493 ****************************************************************/
494
495#define IS_TZO_CHAR(c) \
496 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
497
498#define VALID_YEAR(yr) (yr != 0)
499#define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
500/* VALID_DAY should only be used when month is unknown */
501#define VALID_DAY(day) ((day >= 1) && (day <= 31))
502#define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
503#define VALID_MIN(min) ((min >= 0) && (min <= 59))
504#define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
505#define VALID_TZO(tzo) ((tzo > -1440) && (tzo < 1440))
506#define IS_LEAP(y) \
507 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
508
509static const long daysInMonth[12] =
510 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
511static const long daysInMonthLeap[12] =
512 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
513
Daniel Veillard5a872412002-05-22 06:40:27 +0000514#define MAX_DAYINMONTH(yr,mon) \
515 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
516
Daniel Veillard070803b2002-05-03 07:29:38 +0000517#define VALID_MDAY(dt) \
518 (IS_LEAP(dt->year) ? \
519 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
520 (dt->day <= daysInMonth[dt->mon - 1]))
521
522#define VALID_DATE(dt) \
523 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
524
525#define VALID_TIME(dt) \
526 (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
527 VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
528
529#define VALID_DATETIME(dt) \
530 (VALID_DATE(dt) && VALID_TIME(dt))
531
532#define SECS_PER_MIN (60)
533#define SECS_PER_HOUR (60 * SECS_PER_MIN)
534#define SECS_PER_DAY (24 * SECS_PER_HOUR)
535
Daniel Veillard5a872412002-05-22 06:40:27 +0000536static const long dayInYearByMonth[12] =
537 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
538static const long dayInLeapYearByMonth[12] =
539 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
540
541#define DAY_IN_YEAR(day, month, year) \
542 ((IS_LEAP(year) ? \
543 dayInLeapYearByMonth[month - 1] : \
544 dayInYearByMonth[month - 1]) + day)
545
546#ifdef DEBUG
547#define DEBUG_DATE(dt) \
548 xmlGenericError(xmlGenericErrorContext, \
549 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
550 dt->type,dt->value.date.year,dt->value.date.mon, \
551 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
552 dt->value.date.sec); \
553 if (dt->value.date.tz_flag) \
554 if (dt->value.date.tzo != 0) \
555 xmlGenericError(xmlGenericErrorContext, \
556 "%+05d\n",dt->value.date.tzo); \
557 else \
558 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
559 else \
560 xmlGenericError(xmlGenericErrorContext,"\n")
561#else
562#define DEBUG_DATE(dt)
563#endif
564
Daniel Veillard070803b2002-05-03 07:29:38 +0000565/**
566 * _xmlSchemaParseGYear:
567 * @dt: pointer to a date structure
568 * @str: pointer to the string to analyze
569 *
570 * Parses a xs:gYear without time zone and fills in the appropriate
571 * field of the @dt structure. @str is updated to point just after the
572 * xs:gYear. It is supposed that @dt->year is big enough to contain
573 * the year.
574 *
575 * Returns 0 or the error code
576 */
577static int
578_xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
579 const xmlChar *cur = *str, *firstChar;
580 int isneg = 0, digcnt = 0;
581
582 if (((*cur < '0') || (*cur > '9')) &&
583 (*cur != '-') && (*cur != '+'))
584 return -1;
585
586 if (*cur == '-') {
587 isneg = 1;
588 cur++;
589 }
590
591 firstChar = cur;
592
593 while ((*cur >= '0') && (*cur <= '9')) {
594 dt->year = dt->year * 10 + (*cur - '0');
595 cur++;
596 digcnt++;
597 }
598
599 /* year must be at least 4 digits (CCYY); over 4
600 * digits cannot have a leading zero. */
601 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
602 return 1;
603
604 if (isneg)
605 dt->year = - dt->year;
606
607 if (!VALID_YEAR(dt->year))
608 return 2;
609
610 *str = cur;
611 return 0;
612}
613
614/**
615 * PARSE_2_DIGITS:
616 * @num: the integer to fill in
617 * @cur: an #xmlChar *
618 * @invalid: an integer
619 *
620 * Parses a 2-digits integer and updates @num with the value. @cur is
621 * updated to point just after the integer.
622 * In case of error, @invalid is set to %TRUE, values of @num and
623 * @cur are undefined.
624 */
625#define PARSE_2_DIGITS(num, cur, invalid) \
626 if ((cur[0] < '0') || (cur[0] > '9') || \
627 (cur[1] < '0') || (cur[1] > '9')) \
628 invalid = 1; \
629 else \
630 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
631 cur += 2;
632
633/**
634 * PARSE_FLOAT:
635 * @num: the double to fill in
636 * @cur: an #xmlChar *
637 * @invalid: an integer
638 *
639 * Parses a float and updates @num with the value. @cur is
640 * updated to point just after the float. The float must have a
641 * 2-digits integer part and may or may not have a decimal part.
642 * In case of error, @invalid is set to %TRUE, values of @num and
643 * @cur are undefined.
644 */
645#define PARSE_FLOAT(num, cur, invalid) \
646 PARSE_2_DIGITS(num, cur, invalid); \
647 if (!invalid && (*cur == '.')) { \
648 double mult = 1; \
649 cur++; \
650 if ((*cur < '0') || (*cur > '9')) \
651 invalid = 1; \
652 while ((*cur >= '0') && (*cur <= '9')) { \
653 mult /= 10; \
654 num += (*cur - '0') * mult; \
655 cur++; \
656 } \
657 }
658
659/**
660 * _xmlSchemaParseGMonth:
661 * @dt: pointer to a date structure
662 * @str: pointer to the string to analyze
663 *
664 * Parses a xs:gMonth without time zone and fills in the appropriate
665 * field of the @dt structure. @str is updated to point just after the
666 * xs:gMonth.
667 *
668 * Returns 0 or the error code
669 */
670static int
671_xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
672 const xmlChar *cur = *str;
673 int ret = 0;
674
675 PARSE_2_DIGITS(dt->mon, cur, ret);
676 if (ret != 0)
677 return ret;
678
679 if (!VALID_MONTH(dt->mon))
680 return 2;
681
682 *str = cur;
683 return 0;
684}
685
686/**
687 * _xmlSchemaParseGDay:
688 * @dt: pointer to a date structure
689 * @str: pointer to the string to analyze
690 *
691 * Parses a xs:gDay without time zone and fills in the appropriate
692 * field of the @dt structure. @str is updated to point just after the
693 * xs:gDay.
694 *
695 * Returns 0 or the error code
696 */
697static int
698_xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
699 const xmlChar *cur = *str;
700 int ret = 0;
701
702 PARSE_2_DIGITS(dt->day, cur, ret);
703 if (ret != 0)
704 return ret;
705
706 if (!VALID_DAY(dt->day))
707 return 2;
708
709 *str = cur;
710 return 0;
711}
712
713/**
714 * _xmlSchemaParseTime:
715 * @dt: pointer to a date structure
716 * @str: pointer to the string to analyze
717 *
718 * Parses a xs:time without time zone and fills in the appropriate
719 * fields of the @dt structure. @str is updated to point just after the
720 * xs:time.
721 * In case of error, values of @dt fields are undefined.
722 *
723 * Returns 0 or the error code
724 */
725static int
726_xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
727 const xmlChar *cur = *str;
728 unsigned int hour = 0; /* use temp var in case str is not xs:time */
729 int ret = 0;
730
731 PARSE_2_DIGITS(hour, cur, ret);
732 if (ret != 0)
733 return ret;
734
735 if (*cur != ':')
736 return 1;
737 cur++;
738
739 /* the ':' insures this string is xs:time */
740 dt->hour = hour;
741
742 PARSE_2_DIGITS(dt->min, cur, ret);
743 if (ret != 0)
744 return ret;
745
746 if (*cur != ':')
747 return 1;
748 cur++;
749
750 PARSE_FLOAT(dt->sec, cur, ret);
751 if (ret != 0)
752 return ret;
753
754 if (!VALID_TIME(dt))
755 return 2;
756
757 *str = cur;
758 return 0;
759}
760
761/**
762 * _xmlSchemaParseTimeZone:
763 * @dt: pointer to a date structure
764 * @str: pointer to the string to analyze
765 *
766 * Parses a time zone without time zone and fills in the appropriate
767 * field of the @dt structure. @str is updated to point just after the
768 * time zone.
769 *
770 * Returns 0 or the error code
771 */
772static int
773_xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
774 const xmlChar *cur = *str;
775 int ret = 0;
776
777 if (str == NULL)
778 return -1;
779
780 switch (*cur) {
781 case 0:
782 dt->tz_flag = 0;
783 dt->tzo = 0;
784 break;
785
786 case 'Z':
787 dt->tz_flag = 1;
788 dt->tzo = 0;
789 cur++;
790 break;
791
792 case '+':
793 case '-': {
794 int isneg = 0, tmp = 0;
795 isneg = (*cur == '-');
796
797 cur++;
798
799 PARSE_2_DIGITS(tmp, cur, ret);
800 if (ret != 0)
801 return ret;
802 if (!VALID_HOUR(tmp))
803 return 2;
804
805 if (*cur != ':')
806 return 1;
807 cur++;
808
809 dt->tzo = tmp * 60;
810
811 PARSE_2_DIGITS(tmp, cur, ret);
812 if (ret != 0)
813 return ret;
814 if (!VALID_MIN(tmp))
815 return 2;
816
817 dt->tzo += tmp;
818 if (isneg)
819 dt->tzo = - dt->tzo;
820
821 if (!VALID_TZO(dt->tzo))
822 return 2;
823
Daniel Veillard5a872412002-05-22 06:40:27 +0000824 dt->tz_flag = 1;
Daniel Veillard070803b2002-05-03 07:29:38 +0000825 break;
826 }
827 default:
828 return 1;
829 }
830
831 *str = cur;
832 return 0;
833}
834
Daniel Veillard1ac24d32003-08-27 14:15:15 +0000835/**
836 * _xmlSchemaBase64Decode:
837 * @ch: a character
838 *
839 * Converts a base64 encoded character to its base 64 value.
840 *
841 * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
842 */
843static int
844_xmlSchemaBase64Decode (const xmlChar ch) {
845 if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
846 if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
847 if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
848 if ('+' == ch) return 62;
849 if ('/' == ch) return 63;
850 if ('=' == ch) return 64;
851 return -1;
852}
853
Daniel Veillard070803b2002-05-03 07:29:38 +0000854/****************************************************************
855 * *
856 * XML Schema Dates/Times Datatypes Handling *
857 * *
858 ****************************************************************/
859
860/**
861 * PARSE_DIGITS:
862 * @num: the integer to fill in
863 * @cur: an #xmlChar *
864 * @num_type: an integer flag
865 *
866 * Parses a digits integer and updates @num with the value. @cur is
867 * updated to point just after the integer.
868 * In case of error, @num_type is set to -1, values of @num and
869 * @cur are undefined.
870 */
871#define PARSE_DIGITS(num, cur, num_type) \
872 if ((*cur < '0') || (*cur > '9')) \
873 num_type = -1; \
874 else \
875 while ((*cur >= '0') && (*cur <= '9')) { \
876 num = num * 10 + (*cur - '0'); \
877 cur++; \
878 }
879
880/**
881 * PARSE_NUM:
882 * @num: the double to fill in
883 * @cur: an #xmlChar *
884 * @num_type: an integer flag
885 *
886 * Parses a float or integer and updates @num with the value. @cur is
887 * updated to point just after the number. If the number is a float,
888 * then it must have an integer part and a decimal part; @num_type will
889 * be set to 1. If there is no decimal part, @num_type is set to zero.
890 * In case of error, @num_type is set to -1, values of @num and
891 * @cur are undefined.
892 */
893#define PARSE_NUM(num, cur, num_type) \
894 num = 0; \
895 PARSE_DIGITS(num, cur, num_type); \
896 if (!num_type && (*cur == '.')) { \
897 double mult = 1; \
898 cur++; \
899 if ((*cur < '0') || (*cur > '9')) \
900 num_type = -1; \
901 else \
902 num_type = 1; \
903 while ((*cur >= '0') && (*cur <= '9')) { \
904 mult /= 10; \
905 num += (*cur - '0') * mult; \
906 cur++; \
907 } \
908 }
909
910/**
Daniel Veillard5a872412002-05-22 06:40:27 +0000911 * xmlSchemaValidateDates:
Daniel Veillard455cc072003-03-31 10:13:23 +0000912 * @type: the expected type or XML_SCHEMAS_UNKNOWN
Daniel Veillard070803b2002-05-03 07:29:38 +0000913 * @dateTime: string to analyze
914 * @val: the return computed value
915 *
916 * Check that @dateTime conforms to the lexical space of one of the date types.
917 * if true a value is computed and returned in @val.
918 *
919 * Returns 0 if this validates, a positive error code number otherwise
920 * and -1 in case of internal or API error.
921 */
922static int
Daniel Veillard455cc072003-03-31 10:13:23 +0000923xmlSchemaValidateDates (xmlSchemaValType type,
Daniel Veillard118aed72002-09-24 14:13:13 +0000924 const xmlChar *dateTime, xmlSchemaValPtr *val) {
Daniel Veillard070803b2002-05-03 07:29:38 +0000925 xmlSchemaValPtr dt;
926 int ret;
927 const xmlChar *cur = dateTime;
928
929#define RETURN_TYPE_IF_VALID(t) \
930 if (IS_TZO_CHAR(*cur)) { \
931 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
932 if (ret == 0) { \
933 if (*cur != 0) \
934 goto error; \
935 dt->type = t; \
Daniel Veillard455cc072003-03-31 10:13:23 +0000936 goto done; \
Daniel Veillard070803b2002-05-03 07:29:38 +0000937 } \
938 }
939
940 if (dateTime == NULL)
941 return -1;
942
943 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
944 return 1;
945
946 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
947 if (dt == NULL)
948 return -1;
949
950 if ((cur[0] == '-') && (cur[1] == '-')) {
951 /*
952 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
953 * xs:gDay)
954 */
955 cur += 2;
956
957 /* is it an xs:gDay? */
958 if (*cur == '-') {
Daniel Veillard455cc072003-03-31 10:13:23 +0000959 if (type == XML_SCHEMAS_GMONTH)
960 goto error;
Daniel Veillard070803b2002-05-03 07:29:38 +0000961 ++cur;
962 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
963 if (ret != 0)
964 goto error;
965
966 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
967
968 goto error;
969 }
970
971 /*
972 * it should be an xs:gMonthDay or xs:gMonth
973 */
974 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
975 if (ret != 0)
976 goto error;
977
Daniel Veillardd3b9cd82003-04-09 11:24:17 +0000978 /*
979 * a '-' char could indicate this type is xs:gMonthDay or
980 * a negative time zone offset. Check for xs:gMonthDay first.
981 * Also the first three char's of a negative tzo (-MM:SS) can
982 * appear to be a valid day; so even if the day portion
983 * of the xs:gMonthDay verifies, we must insure it was not
984 * a tzo.
985 */
986 if (*cur == '-') {
987 const xmlChar *rewnd = cur;
988 cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +0000989
Daniel Veillardd3b9cd82003-04-09 11:24:17 +0000990 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
991 if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
992
993 /*
994 * we can use the VALID_MDAY macro to validate the month
995 * and day because the leap year test will flag year zero
996 * as a leap year (even though zero is an invalid year).
997 */
998 if (VALID_MDAY((&(dt->value.date)))) {
999
1000 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
1001
1002 goto error;
1003 }
1004 }
1005
1006 /*
1007 * not xs:gMonthDay so rewind and check if just xs:gMonth
1008 * with an optional time zone.
1009 */
1010 cur = rewnd;
1011 }
1012
1013 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
Daniel Veillard070803b2002-05-03 07:29:38 +00001014
1015 goto error;
1016 }
1017
1018 /*
1019 * It's a right-truncated date or an xs:time.
1020 * Try to parse an xs:time then fallback on right-truncated dates.
1021 */
1022 if ((*cur >= '0') && (*cur <= '9')) {
1023 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1024 if (ret == 0) {
1025 /* it's an xs:time */
1026 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
1027 }
1028 }
1029
1030 /* fallback on date parsing */
1031 cur = dateTime;
1032
1033 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
1034 if (ret != 0)
1035 goto error;
1036
1037 /* is it an xs:gYear? */
1038 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
1039
1040 if (*cur != '-')
1041 goto error;
1042 cur++;
1043
1044 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
1045 if (ret != 0)
1046 goto error;
1047
1048 /* is it an xs:gYearMonth? */
1049 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
1050
1051 if (*cur != '-')
1052 goto error;
1053 cur++;
1054
1055 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
1056 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
1057 goto error;
1058
1059 /* is it an xs:date? */
1060 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
1061
1062 if (*cur != 'T')
1063 goto error;
1064 cur++;
1065
1066 /* it should be an xs:dateTime */
1067 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1068 if (ret != 0)
1069 goto error;
1070
1071 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
1072 if ((ret != 0) || (*cur != 0) || !VALID_DATETIME((&(dt->value.date))))
1073 goto error;
1074
Daniel Veillard455cc072003-03-31 10:13:23 +00001075
Daniel Veillard070803b2002-05-03 07:29:38 +00001076 dt->type = XML_SCHEMAS_DATETIME;
1077
Daniel Veillard455cc072003-03-31 10:13:23 +00001078done:
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001079#if 1
1080 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
1081 goto error;
1082#else
1083 /*
1084 * insure the parsed type is equal to or less significant (right
1085 * truncated) than the desired type.
1086 */
1087 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
1088
1089 /* time only matches time */
1090 if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
1091 goto error;
1092
1093 if ((type == XML_SCHEMAS_DATETIME) &&
1094 ((dt->type != XML_SCHEMAS_DATE) ||
1095 (dt->type != XML_SCHEMAS_GYEARMONTH) ||
1096 (dt->type != XML_SCHEMAS_GYEAR)))
1097 goto error;
1098
1099 if ((type == XML_SCHEMAS_DATE) &&
1100 ((dt->type != XML_SCHEMAS_GYEAR) ||
1101 (dt->type != XML_SCHEMAS_GYEARMONTH)))
1102 goto error;
1103
1104 if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
1105 goto error;
1106
1107 if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
1108 goto error;
1109 }
Daniel Veillard455cc072003-03-31 10:13:23 +00001110#endif
1111
Daniel Veillard070803b2002-05-03 07:29:38 +00001112 if (val != NULL)
1113 *val = dt;
Daniel Veillard80b19092003-03-28 13:29:53 +00001114 else
1115 xmlSchemaFreeValue(dt);
Daniel Veillard070803b2002-05-03 07:29:38 +00001116
1117 return 0;
1118
1119error:
1120 if (dt != NULL)
1121 xmlSchemaFreeValue(dt);
1122 return 1;
1123}
1124
1125/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001126 * xmlSchemaValidateDuration:
Daniel Veillard070803b2002-05-03 07:29:38 +00001127 * @type: the predefined type
1128 * @duration: string to analyze
1129 * @val: the return computed value
1130 *
1131 * Check that @duration conforms to the lexical space of the duration type.
1132 * if true a value is computed and returned in @val.
1133 *
1134 * Returns 0 if this validates, a positive error code number otherwise
1135 * and -1 in case of internal or API error.
1136 */
1137static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00001138xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +00001139 const xmlChar *duration, xmlSchemaValPtr *val) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001140 const xmlChar *cur = duration;
1141 xmlSchemaValPtr dur;
1142 int isneg = 0;
1143 unsigned int seq = 0;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001144 double num;
1145 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
1146 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1147 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
Daniel Veillard070803b2002-05-03 07:29:38 +00001148
1149 if (duration == NULL)
1150 return -1;
1151
1152 if (*cur == '-') {
1153 isneg = 1;
1154 cur++;
1155 }
1156
1157 /* duration must start with 'P' (after sign) */
1158 if (*cur++ != 'P')
1159 return 1;
1160
Daniel Veillard80b19092003-03-28 13:29:53 +00001161 if (*cur == 0)
1162 return 1;
1163
Daniel Veillard070803b2002-05-03 07:29:38 +00001164 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1165 if (dur == NULL)
1166 return -1;
1167
1168 while (*cur != 0) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001169
1170 /* input string should be empty or invalid date/time item */
1171 if (seq >= sizeof(desig))
1172 goto error;
1173
1174 /* T designator must be present for time items */
1175 if (*cur == 'T') {
1176 if (seq <= 3) {
1177 seq = 3;
1178 cur++;
1179 } else
1180 return 1;
1181 } else if (seq == 3)
1182 goto error;
1183
1184 /* parse the number portion of the item */
1185 PARSE_NUM(num, cur, num_type);
1186
1187 if ((num_type == -1) || (*cur == 0))
1188 goto error;
1189
1190 /* update duration based on item type */
1191 while (seq < sizeof(desig)) {
1192 if (*cur == desig[seq]) {
1193
1194 /* verify numeric type; only seconds can be float */
1195 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1196 goto error;
1197
1198 switch (seq) {
1199 case 0:
1200 dur->value.dur.mon = (long)num * 12;
1201 break;
1202 case 1:
1203 dur->value.dur.mon += (long)num;
1204 break;
1205 default:
1206 /* convert to seconds using multiplier */
1207 dur->value.dur.sec += num * multi[seq];
1208 seq++;
1209 break;
1210 }
1211
1212 break; /* exit loop */
1213 }
1214 /* no date designators found? */
1215 if (++seq == 3)
1216 goto error;
1217 }
1218 cur++;
1219 }
1220
1221 if (isneg) {
1222 dur->value.dur.mon = -dur->value.dur.mon;
1223 dur->value.dur.day = -dur->value.dur.day;
1224 dur->value.dur.sec = -dur->value.dur.sec;
1225 }
1226
1227 if (val != NULL)
1228 *val = dur;
Daniel Veillard80b19092003-03-28 13:29:53 +00001229 else
1230 xmlSchemaFreeValue(dur);
Daniel Veillard070803b2002-05-03 07:29:38 +00001231
1232 return 0;
1233
1234error:
1235 if (dur != NULL)
1236 xmlSchemaFreeValue(dur);
1237 return 1;
1238}
1239
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001240/**
1241 * xmlSchemaStrip:
1242 * @value: a value
1243 *
1244 * Removes the leading and ending spaces of a string
1245 *
1246 * Returns the new string or NULL if no change was required.
1247 */
1248static xmlChar *
1249xmlSchemaStrip(const xmlChar *value) {
1250 const xmlChar *start = value, *end, *f;
1251
1252 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001253 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001254 end = start;
1255 while (*end != 0) end++;
1256 f = end;
1257 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001258 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001259 end++;
1260 if ((start == value) && (f == end)) return(NULL);
1261 return(xmlStrndup(start, end - start));
1262}
Daniel Veillard96a4b252003-02-06 08:22:32 +00001263
1264/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001265 * xmlSchemaCollapseString:
1266 * @value: a value
1267 *
1268 * Removes and normalize white spaces in the string
1269 *
1270 * Returns the new string or NULL if no change was required.
1271 */
1272static xmlChar *
1273xmlSchemaCollapseString(const xmlChar *value) {
1274 const xmlChar *start = value, *end, *f;
1275 xmlChar *g;
1276 int col = 0;
1277
1278 if (value == NULL) return(NULL);
William M. Brack76e95df2003-10-18 16:20:14 +00001279 while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001280 end = start;
1281 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001282 if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001283 col = end - start;
1284 break;
1285 } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
1286 col = end - start;
1287 break;
1288 }
1289 end++;
1290 }
1291 if (col == 0) {
1292 f = end;
1293 end--;
William M. Brack76e95df2003-10-18 16:20:14 +00001294 while ((end > start) && (IS_BLANK_CH(*end))) end--;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001295 end++;
1296 if ((start == value) && (f == end)) return(NULL);
1297 return(xmlStrndup(start, end - start));
1298 }
1299 start = xmlStrdup(start);
1300 if (start == NULL) return(NULL);
1301 g = (xmlChar *) (start + col);
1302 end = g;
1303 while (*end != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001304 if (IS_BLANK_CH(*end)) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001305 end++;
William M. Brack76e95df2003-10-18 16:20:14 +00001306 while (IS_BLANK_CH(*end)) end++;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001307 if (*end != 0)
1308 *g++ = ' ';
1309 } else
1310 *g++ = *end++;
1311 }
1312 *g = 0;
1313 return((xmlChar *) start);
1314}
1315
1316/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001317 * xmlSchemaValAtomicListNode:
1318 * @type: the predefined atomic type for a token in the list
1319 * @value: the list value to check
1320 * @ret: the return computed value
1321 * @node: the node containing the value
1322 *
1323 * Check that a value conforms to the lexical space of the predefined
1324 * list type. if true a value is computed and returned in @ret.
1325 *
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001326 * Returns the number of items if this validates, a negative error code
1327 * number otherwise
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001328 */
1329static int
1330xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
1331 xmlSchemaValPtr *ret, xmlNodePtr node) {
1332 xmlChar *val, *cur, *endval;
1333 int nb_values = 0;
Daniel Veillard580ced82003-03-21 21:22:48 +00001334 int tmp = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001335
1336 if (value == NULL) {
1337 return(-1);
1338 }
1339 val = xmlStrdup(value);
1340 if (val == NULL) {
1341 return(-1);
1342 }
1343 cur = val;
1344 /*
1345 * Split the list
1346 */
William M. Brack76e95df2003-10-18 16:20:14 +00001347 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001348 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00001349 if (IS_BLANK_CH(*cur)) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001350 *cur = 0;
1351 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00001352 while (IS_BLANK_CH(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001353 } else {
1354 nb_values++;
1355 cur++;
William M. Brack76e95df2003-10-18 16:20:14 +00001356 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001357 }
1358 }
1359 if (nb_values == 0) {
1360 if (ret != NULL) {
1361 TODO
1362 }
1363 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001364 return(nb_values);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001365 }
1366 endval = cur;
1367 cur = val;
1368 while ((*cur == 0) && (cur != endval)) cur++;
1369 while (cur != endval) {
1370 tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
1371 if (tmp != 0)
1372 break;
1373 while (*cur != 0) cur++;
1374 while ((*cur == 0) && (cur != endval)) cur++;
1375 }
1376 xmlFree(val);
1377 if (ret != NULL) {
1378 TODO
1379 }
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001380 if (tmp == 0)
1381 return(nb_values);
1382 return(-1);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001383}
1384
1385/**
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001386 * xmlSchemaParseUInt:
1387 * @str: pointer to the string R/W
1388 * @llo: pointer to the low result
1389 * @lmi: pointer to the mid result
1390 * @lhi: pointer to the high result
1391 *
1392 * Parse an unsigned long into 3 fields.
1393 *
1394 * Returns the number of chars parsed or -1 if overflow of the capacity
1395 */
1396static int
1397xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
1398 unsigned long *lmi, unsigned long *lhi) {
1399 unsigned long lo = 0, mi = 0, hi = 0;
1400 const xmlChar *tmp, *cur = *str;
1401 int ret = 0, i = 0;
1402
1403 while (*cur == '0') {
1404 ret++;
1405 cur++;
1406 }
1407 tmp = cur;
1408 while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
1409 i++;tmp++;ret++;
1410 }
1411 if (i > 24) {
1412 *str = tmp;
1413 return(-1);
1414 }
1415 while (i > 16) {
1416 hi = hi * 10 + (*cur++ - '0');
1417 i--;
1418 }
1419 while (i > 8) {
1420 mi = mi * 10 + (*cur++ - '0');
1421 i--;
1422 }
1423 while (i > 0) {
1424 lo = lo * 10 + (*cur++ - '0');
1425 i--;
1426 }
1427
1428 *str = cur;
1429 *llo = lo;
1430 *lmi = mi;
1431 *lhi = hi;
1432 return(ret);
1433}
1434
1435/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001436 * xmlSchemaValAtomicType:
1437 * @type: the predefined type
1438 * @value: the value to check
1439 * @val: the return computed value
1440 * @node: the node containing the value
1441 * flags: flags to control the vlidation
1442 *
1443 * Check that a value conforms to the lexical space of the atomic type.
1444 * if true a value is computed and returned in @val.
1445 *
1446 * Returns 0 if this validates, a positive error code number otherwise
1447 * and -1 in case of internal or API error.
1448 */
1449static int
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001450xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
1451 xmlSchemaValPtr * val, xmlNodePtr node, int flags)
1452{
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001453 xmlSchemaValPtr v;
1454 xmlChar *norm = NULL;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001455 int ret = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001456
1457 if (xmlSchemaTypesInitialized == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001458 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001459 if (type == NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001460 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001461
1462 if (val != NULL)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001463 *val = NULL;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001464 if ((flags == 0) && (value != NULL)) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001465 if ((type->flags != XML_SCHEMAS_STRING) &&
1466 (type->flags != XML_SCHEMAS_NORMSTRING)) {
1467 norm = xmlSchemaCollapseString(value);
1468 if (norm != NULL)
1469 value = norm;
1470 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001471 }
1472
1473 switch (type->flags) {
1474 case XML_SCHEMAS_UNKNOWN:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001475 if (type == xmlSchemaTypeAnyTypeDef)
1476 goto return0;
1477 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001478 case XML_SCHEMAS_STRING:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001479 goto return0;
Daniel Veillard1516d5b2004-01-22 07:27:45 +00001480 case XML_SCHEMAS_NORMSTRING:{
1481 const xmlChar *cur = value;
1482
1483 while (*cur != 0) {
1484 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
1485 goto return1;
1486 } else {
1487 cur++;
1488 }
1489 }
1490 if (val != NULL) {
1491 v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
1492 if (v != NULL) {
1493 v->value.str = xmlStrdup(value);
1494 *val = v;
1495 } else {
1496 goto error;
1497 }
1498 }
1499 goto return0;
1500 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001501 case XML_SCHEMAS_DECIMAL:{
1502 const xmlChar *cur = value, *tmp;
1503 unsigned int frac = 0, len, neg = 0;
1504 unsigned long base = 0;
1505
1506 if (cur == NULL)
1507 goto return1;
1508 if (*cur == '+')
1509 cur++;
1510 else if (*cur == '-') {
1511 neg = 1;
1512 cur++;
1513 }
1514 tmp = cur;
1515 while ((*cur >= '0') && (*cur <= '9')) {
1516 base = base * 10 + (*cur - '0');
1517 cur++;
1518 }
1519 len = cur - tmp;
1520 if (*cur == '.') {
1521 cur++;
1522 tmp = cur;
1523 while ((*cur >= '0') && (*cur <= '9')) {
1524 base = base * 10 + (*cur - '0');
1525 cur++;
1526 }
1527 frac = cur - tmp;
1528 }
1529 if (*cur != 0)
1530 goto return1;
1531 if (val != NULL) {
1532 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1533 if (v != NULL) {
1534 v->value.decimal.lo = base;
1535 v->value.decimal.sign = neg;
1536 v->value.decimal.frac = frac;
1537 v->value.decimal.total = frac + len;
1538 *val = v;
1539 }
1540 }
1541 goto return0;
1542 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001543 case XML_SCHEMAS_TIME:
1544 case XML_SCHEMAS_GDAY:
1545 case XML_SCHEMAS_GMONTH:
1546 case XML_SCHEMAS_GMONTHDAY:
1547 case XML_SCHEMAS_GYEAR:
1548 case XML_SCHEMAS_GYEARMONTH:
1549 case XML_SCHEMAS_DATE:
1550 case XML_SCHEMAS_DATETIME:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001551 ret = xmlSchemaValidateDates(type->flags, value, val);
1552 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001553 case XML_SCHEMAS_DURATION:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001554 ret = xmlSchemaValidateDuration(type, value, val);
1555 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001556 case XML_SCHEMAS_FLOAT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001557 case XML_SCHEMAS_DOUBLE:{
1558 const xmlChar *cur = value;
1559 int neg = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001560
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001561 if (cur == NULL)
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00001562 goto return1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001563 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
1564 cur += 3;
1565 if (*cur != 0)
1566 goto return1;
1567 if (val != NULL) {
1568 if (type == xmlSchemaTypeFloatDef) {
1569 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1570 if (v != NULL) {
1571 v->value.f = (float) xmlXPathNAN;
1572 } else {
1573 xmlSchemaFreeValue(v);
1574 goto error;
1575 }
1576 } else {
1577 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1578 if (v != NULL) {
1579 v->value.d = xmlXPathNAN;
1580 } else {
1581 xmlSchemaFreeValue(v);
1582 goto error;
1583 }
1584 }
1585 *val = v;
1586 }
1587 goto return0;
1588 }
1589 if (*cur == '-') {
1590 neg = 1;
1591 cur++;
1592 }
1593 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
1594 cur += 3;
1595 if (*cur != 0)
1596 goto return1;
1597 if (val != NULL) {
1598 if (type == xmlSchemaTypeFloatDef) {
1599 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1600 if (v != NULL) {
1601 if (neg)
1602 v->value.f = (float) xmlXPathNINF;
1603 else
1604 v->value.f = (float) xmlXPathPINF;
1605 } else {
1606 xmlSchemaFreeValue(v);
1607 goto error;
1608 }
1609 } else {
1610 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1611 if (v != NULL) {
1612 if (neg)
1613 v->value.d = xmlXPathNINF;
1614 else
1615 v->value.d = xmlXPathPINF;
1616 } else {
1617 xmlSchemaFreeValue(v);
1618 goto error;
1619 }
1620 }
1621 *val = v;
1622 }
1623 goto return0;
1624 }
1625 if ((neg == 0) && (*cur == '+'))
1626 cur++;
1627 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
1628 goto return1;
1629 while ((*cur >= '0') && (*cur <= '9')) {
1630 cur++;
1631 }
1632 if (*cur == '.') {
1633 cur++;
1634 while ((*cur >= '0') && (*cur <= '9'))
1635 cur++;
1636 }
1637 if ((*cur == 'e') || (*cur == 'E')) {
1638 cur++;
1639 if ((*cur == '-') || (*cur == '+'))
1640 cur++;
1641 while ((*cur >= '0') && (*cur <= '9'))
1642 cur++;
1643 }
1644 if (*cur != 0)
1645 goto return1;
1646 if (val != NULL) {
1647 if (type == xmlSchemaTypeFloatDef) {
1648 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1649 if (v != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001650 if (sscanf((const char *) value, "%f",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001651 &(v->value.f)) == 1) {
1652 *val = v;
1653 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001654 xmlSchemaFreeValue(v);
1655 goto return1;
1656 }
1657 } else {
1658 goto error;
1659 }
1660 } else {
1661 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1662 if (v != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001663 if (sscanf((const char *) value, "%lf",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001664 &(v->value.d)) == 1) {
1665 *val = v;
1666 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001667 xmlSchemaFreeValue(v);
1668 goto return1;
1669 }
1670 } else {
1671 goto error;
1672 }
1673 }
1674 }
1675 goto return0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00001676 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001677 case XML_SCHEMAS_BOOLEAN:{
1678 const xmlChar *cur = value;
1679
1680 if ((cur[0] == '0') && (cur[1] == 0))
1681 ret = 0;
1682 else if ((cur[0] == '1') && (cur[1] == 0))
1683 ret = 1;
1684 else if ((cur[0] == 't') && (cur[1] == 'r')
1685 && (cur[2] == 'u') && (cur[3] == 'e')
1686 && (cur[4] == 0))
1687 ret = 1;
1688 else if ((cur[0] == 'f') && (cur[1] == 'a')
1689 && (cur[2] == 'l') && (cur[3] == 's')
1690 && (cur[4] == 'e') && (cur[5] == 0))
1691 ret = 0;
1692 else
1693 goto return1;
1694 if (val != NULL) {
1695 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
1696 if (v != NULL) {
1697 v->value.b = ret;
1698 *val = v;
1699 } else {
1700 goto error;
1701 }
1702 }
1703 goto return0;
1704 }
1705 case XML_SCHEMAS_TOKEN:{
1706 const xmlChar *cur = value;
1707
William M. Brack76e95df2003-10-18 16:20:14 +00001708 if (IS_BLANK_CH(*cur))
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001709 goto return1;
1710
1711 while (*cur != 0) {
1712 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
1713 goto return1;
1714 } else if (*cur == ' ') {
1715 cur++;
1716 if (*cur == 0)
1717 goto return1;
1718 if (*cur == ' ')
1719 goto return1;
1720 } else {
1721 cur++;
1722 }
1723 }
1724 if (val != NULL) {
1725 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
1726 if (v != NULL) {
1727 v->value.str = xmlStrdup(value);
1728 *val = v;
1729 } else {
1730 goto error;
1731 }
1732 }
1733 goto return0;
1734 }
1735 case XML_SCHEMAS_LANGUAGE:
1736 if (xmlCheckLanguageID(value) == 1) {
1737 if (val != NULL) {
1738 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
1739 if (v != NULL) {
1740 v->value.str = xmlStrdup(value);
1741 *val = v;
1742 } else {
1743 goto error;
1744 }
1745 }
1746 goto return0;
1747 }
1748 goto return1;
1749 case XML_SCHEMAS_NMTOKEN:
1750 if (xmlValidateNMToken(value, 1) == 0) {
1751 if (val != NULL) {
1752 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
1753 if (v != NULL) {
1754 v->value.str = xmlStrdup(value);
1755 *val = v;
1756 } else {
1757 goto error;
1758 }
1759 }
1760 goto return0;
1761 }
1762 goto return1;
1763 case XML_SCHEMAS_NMTOKENS:
1764 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
1765 value, val, node);
1766 if (ret > 0)
1767 ret = 0;
1768 else
1769 ret = 1;
1770 goto done;
1771 case XML_SCHEMAS_NAME:
1772 ret = xmlValidateName(value, 1);
1773 if ((ret == 0) && (val != NULL)) {
1774 TODO;
1775 }
1776 goto done;
1777 case XML_SCHEMAS_QNAME:{
1778 xmlChar *uri = NULL;
1779 xmlChar *local = NULL;
1780
1781 ret = xmlValidateQName(value, 1);
1782 if ((ret == 0) && (node != NULL)) {
1783 xmlChar *prefix;
1784
1785 local = xmlSplitQName2(value, &prefix);
1786 if (prefix != NULL) {
1787 xmlNsPtr ns;
1788
1789 ns = xmlSearchNs(node->doc, node, prefix);
1790 if (ns == NULL)
1791 ret = 1;
1792 else if (val != NULL)
1793 uri = xmlStrdup(ns->href);
1794 }
1795 if ((local != NULL) && ((val == NULL) || (ret != 0)))
1796 xmlFree(local);
1797 if (prefix != NULL)
1798 xmlFree(prefix);
1799 }
1800 if ((ret == 0) && (val != NULL)) {
1801 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
1802 if (v != NULL) {
1803 if (local != NULL)
1804 v->value.qname.name = local;
1805 else
1806 v->value.qname.name = xmlStrdup(value);
1807 if (uri != NULL)
1808 v->value.qname.uri = uri;
1809
1810 *val = v;
1811 } else {
1812 if (local != NULL)
1813 xmlFree(local);
1814 if (uri != NULL)
1815 xmlFree(uri);
1816 goto error;
1817 }
1818 }
1819 goto done;
1820 }
1821 case XML_SCHEMAS_NCNAME:
1822 ret = xmlValidateNCName(value, 1);
1823 if ((ret == 0) && (val != NULL)) {
1824 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
1825 if (v != NULL) {
1826 v->value.str = xmlStrdup(value);
1827 *val = v;
1828 } else {
1829 goto error;
1830 }
1831 }
1832 goto done;
1833 case XML_SCHEMAS_ID:
1834 ret = xmlValidateNCName(value, 1);
1835 if ((ret == 0) && (val != NULL)) {
1836 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
1837 if (v != NULL) {
1838 v->value.str = xmlStrdup(value);
1839 *val = v;
1840 } else {
1841 goto error;
1842 }
1843 }
1844 if ((ret == 0) && (node != NULL) &&
1845 (node->type == XML_ATTRIBUTE_NODE)) {
1846 xmlAttrPtr attr = (xmlAttrPtr) node;
1847
1848 /*
1849 * NOTE: the IDness might have already be declared in the DTD
1850 */
1851 if (attr->atype != XML_ATTRIBUTE_ID) {
1852 xmlIDPtr res;
1853 xmlChar *strip;
1854
1855 strip = xmlSchemaStrip(value);
1856 if (strip != NULL) {
1857 res = xmlAddID(NULL, node->doc, strip, attr);
1858 xmlFree(strip);
1859 } else
1860 res = xmlAddID(NULL, node->doc, value, attr);
1861 if (res == NULL) {
1862 ret = 2;
1863 } else {
1864 attr->atype = XML_ATTRIBUTE_ID;
1865 }
1866 }
1867 }
1868 goto done;
1869 case XML_SCHEMAS_IDREF:
1870 ret = xmlValidateNCName(value, 1);
1871 if ((ret == 0) && (val != NULL)) {
1872 TODO;
1873 }
1874 if ((ret == 0) && (node != NULL) &&
1875 (node->type == XML_ATTRIBUTE_NODE)) {
1876 xmlAttrPtr attr = (xmlAttrPtr) node;
1877 xmlChar *strip;
1878
1879 strip = xmlSchemaStrip(value);
1880 if (strip != NULL) {
1881 xmlAddRef(NULL, node->doc, strip, attr);
1882 xmlFree(strip);
1883 } else
1884 xmlAddRef(NULL, node->doc, value, attr);
1885 attr->atype = XML_ATTRIBUTE_IDREF;
1886 }
1887 goto done;
1888 case XML_SCHEMAS_IDREFS:
1889 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
1890 value, val, node);
1891 if (ret < 0)
1892 ret = 2;
1893 else
1894 ret = 0;
1895 if ((ret == 0) && (node != NULL) &&
1896 (node->type == XML_ATTRIBUTE_NODE)) {
1897 xmlAttrPtr attr = (xmlAttrPtr) node;
1898
1899 attr->atype = XML_ATTRIBUTE_IDREFS;
1900 }
1901 goto done;
1902 case XML_SCHEMAS_ENTITY:{
1903 xmlChar *strip;
1904
1905 ret = xmlValidateNCName(value, 1);
1906 if ((node == NULL) || (node->doc == NULL))
1907 ret = 3;
1908 if (ret == 0) {
1909 xmlEntityPtr ent;
1910
1911 strip = xmlSchemaStrip(value);
1912 if (strip != NULL) {
1913 ent = xmlGetDocEntity(node->doc, strip);
1914 xmlFree(strip);
1915 } else {
1916 ent = xmlGetDocEntity(node->doc, value);
1917 }
1918 if ((ent == NULL) ||
1919 (ent->etype !=
1920 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
1921 ret = 4;
1922 }
1923 if ((ret == 0) && (val != NULL)) {
1924 TODO;
1925 }
1926 if ((ret == 0) && (node != NULL) &&
1927 (node->type == XML_ATTRIBUTE_NODE)) {
1928 xmlAttrPtr attr = (xmlAttrPtr) node;
1929
1930 attr->atype = XML_ATTRIBUTE_ENTITY;
1931 }
1932 goto done;
1933 }
1934 case XML_SCHEMAS_ENTITIES:
1935 if ((node == NULL) || (node->doc == NULL))
1936 goto return3;
1937 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
1938 value, val, node);
1939 if (ret <= 0)
1940 ret = 1;
1941 else
1942 ret = 0;
1943 if ((ret == 0) && (node != NULL) &&
1944 (node->type == XML_ATTRIBUTE_NODE)) {
1945 xmlAttrPtr attr = (xmlAttrPtr) node;
1946
1947 attr->atype = XML_ATTRIBUTE_ENTITIES;
1948 }
1949 goto done;
1950 case XML_SCHEMAS_NOTATION:{
1951 xmlChar *uri = NULL;
1952 xmlChar *local = NULL;
1953
1954 ret = xmlValidateQName(value, 1);
1955 if ((ret == 0) && (node != NULL)) {
1956 xmlChar *prefix;
1957
1958 local = xmlSplitQName2(value, &prefix);
1959 if (prefix != NULL) {
1960 xmlNsPtr ns;
1961
1962 ns = xmlSearchNs(node->doc, node, prefix);
1963 if (ns == NULL)
1964 ret = 1;
1965 else if (val != NULL)
1966 uri = xmlStrdup(ns->href);
1967 }
1968 if ((local != NULL) && ((val == NULL) || (ret != 0)))
1969 xmlFree(local);
1970 if (prefix != NULL)
1971 xmlFree(prefix);
1972 }
1973 if ((node == NULL) || (node->doc == NULL))
1974 ret = 3;
1975 if (ret == 0) {
1976 ret = xmlValidateNotationUse(NULL, node->doc, value);
1977 if (ret == 1)
1978 ret = 0;
1979 else
1980 ret = 1;
1981 }
1982 if ((ret == 0) && (val != NULL)) {
1983 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
1984 if (v != NULL) {
1985 if (local != NULL)
1986 v->value.qname.name = local;
1987 else
1988 v->value.qname.name = xmlStrdup(value);
1989 if (uri != NULL)
1990 v->value.qname.uri = uri;
1991
1992 *val = v;
1993 } else {
1994 if (local != NULL)
1995 xmlFree(local);
1996 if (uri != NULL)
1997 xmlFree(uri);
1998 goto error;
1999 }
2000 }
2001 goto done;
2002 }
2003 case XML_SCHEMAS_ANYURI:{
2004 xmlURIPtr uri;
2005
2006 uri = xmlParseURI((const char *) value);
2007 if (uri == NULL)
2008 goto return1;
2009 if (val != NULL) {
2010 TODO;
2011 }
2012 xmlFreeURI(uri);
2013 goto return0;
2014 }
2015 case XML_SCHEMAS_HEXBINARY:{
2016 const xmlChar *cur = value;
2017 xmlChar *base;
2018 int total, i = 0;
2019
2020 if (cur == NULL)
2021 goto return1;
2022
2023 while (((*cur >= '0') && (*cur <= '9')) ||
2024 ((*cur >= 'A') && (*cur <= 'F')) ||
2025 ((*cur >= 'a') && (*cur <= 'f'))) {
2026 i++;
2027 cur++;
2028 }
2029
2030 if (*cur != 0)
2031 goto return1;
2032 if ((i % 2) != 0)
2033 goto return1;
2034
2035 if (val != NULL) {
2036
2037 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2038 if (v == NULL)
2039 goto error;
2040
2041 cur = xmlStrdup(value);
2042 if (cur == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002043 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002044 xmlFree(v);
2045 goto return1;
2046 }
2047
2048 total = i / 2; /* number of octets */
2049
2050 base = (xmlChar *) cur;
2051 while (i-- > 0) {
2052 if (*base >= 'a')
2053 *base = *base - ('a' - 'A');
2054 base++;
2055 }
2056
2057 v->value.hex.str = (xmlChar *) cur;
2058 v->value.hex.total = total;
2059 *val = v;
2060 }
2061 goto return0;
2062 }
2063 case XML_SCHEMAS_BASE64BINARY:{
2064 /* ISSUE:
2065 *
2066 * Ignore all stray characters? (yes, currently)
2067 * Worry about long lines? (no, currently)
2068 *
2069 * rfc2045.txt:
2070 *
2071 * "The encoded output stream must be represented in lines of
2072 * no more than 76 characters each. All line breaks or other
2073 * characters not found in Table 1 must be ignored by decoding
2074 * software. In base64 data, characters other than those in
2075 * Table 1, line breaks, and other white space probably
2076 * indicate a transmission error, about which a warning
2077 * message or even a message rejection might be appropriate
2078 * under some circumstances." */
2079 const xmlChar *cur = value;
2080 xmlChar *base;
2081 int total, i = 0, pad = 0;
2082
2083 if (cur == NULL)
2084 goto return1;
2085
2086 for (; *cur; ++cur) {
2087 int decc;
2088
2089 decc = _xmlSchemaBase64Decode(*cur);
2090 if (decc < 0) ;
2091 else if (decc < 64)
2092 i++;
2093 else
2094 break;
2095 }
2096 for (; *cur; ++cur) {
2097 int decc;
2098
2099 decc = _xmlSchemaBase64Decode(*cur);
2100 if (decc < 0) ;
2101 else if (decc < 64)
2102 goto return1;
2103 if (decc == 64)
2104 pad++;
2105 }
2106
2107 /* rfc2045.txt: "Special processing is performed if fewer than
2108 * 24 bits are available at the end of the data being encoded.
2109 * A full encoding quantum is always completed at the end of a
2110 * body. When fewer than 24 input bits are available in an
2111 * input group, zero bits are added (on the right) to form an
2112 * integral number of 6-bit groups. Padding at the end of the
2113 * data is performed using the "=" character. Since all
2114 * base64 input is an integral number of octets, only the
2115 * following cases can arise: (1) the final quantum of
2116 * encoding input is an integral multiple of 24 bits; here,
2117 * the final unit of encoded output will be an integral
2118 * multiple ofindent: Standard input:701: Warning:old style
2119 * assignment ambiguity in "=*". Assuming "= *" 4 characters
2120 * with no "=" padding, (2) the final
2121 * quantum of encoding input is exactly 8 bits; here, the
2122 * final unit of encoded output will be two characters
2123 * followed by two "=" padding characters, or (3) the final
2124 * quantum of encoding input is exactly 16 bits; here, the
2125 * final unit of encoded output will be three characters
2126 * followed by one "=" padding character." */
2127
2128 total = 3 * (i / 4);
2129 if (pad == 0) {
2130 if (i % 4 != 0)
2131 goto return1;
2132 } else if (pad == 1) {
2133 int decc;
2134
2135 if (i % 4 != 3)
2136 goto return1;
2137 for (decc = _xmlSchemaBase64Decode(*cur);
2138 (decc < 0) || (decc > 63);
2139 decc = _xmlSchemaBase64Decode(*cur))
2140 --cur;
2141 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
2142 /* 00111100 -> 0x3c */
2143 if (decc & ~0x3c)
2144 goto return1;
2145 total += 2;
2146 } else if (pad == 2) {
2147 int decc;
2148
2149 if (i % 4 != 2)
2150 goto return1;
2151 for (decc = _xmlSchemaBase64Decode(*cur);
2152 (decc < 0) || (decc > 63);
2153 decc = _xmlSchemaBase64Decode(*cur))
2154 --cur;
2155 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
2156 /* 00110000 -> 0x30 */
2157 if (decc & ~0x30)
2158 goto return1;
2159 total += 1;
2160 } else
2161 goto return1;
2162
2163 if (val != NULL) {
2164 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
2165 if (v == NULL)
2166 goto error;
2167 base =
2168 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
2169 sizeof(xmlChar));
2170 if (base == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002171 xmlSchemaTypeErrMemory(node, "allocating base64 data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002172 xmlFree(v);
2173 goto return1;
2174 }
2175 v->value.base64.str = base;
2176 for (cur = value; *cur; ++cur)
2177 if (_xmlSchemaBase64Decode(*cur) >= 0) {
2178 *base = *cur;
2179 ++base;
2180 }
2181 *base = 0;
2182 v->value.base64.total = total;
2183 *val = v;
2184 }
2185 goto return0;
2186 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002187 case XML_SCHEMAS_INTEGER:
2188 case XML_SCHEMAS_PINTEGER:
2189 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002190 case XML_SCHEMAS_NINTEGER:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002191 case XML_SCHEMAS_NNINTEGER:{
2192 const xmlChar *cur = value;
2193 unsigned long lo, mi, hi;
2194 int sign = 0;
2195
2196 if (cur == NULL)
2197 goto return1;
2198 if (*cur == '-') {
2199 sign = 1;
2200 cur++;
2201 } else if (*cur == '+')
2202 cur++;
2203 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2204 if (ret == 0)
2205 goto return1;
2206 if (*cur != 0)
2207 goto return1;
2208 if (type->flags == XML_SCHEMAS_NPINTEGER) {
2209 if ((sign == 0) &&
2210 ((hi != 0) || (mi != 0) || (lo != 0)))
2211 goto return1;
2212 } else if (type->flags == XML_SCHEMAS_PINTEGER) {
2213 if (sign == 1)
2214 goto return1;
2215 if ((hi == 0) && (mi == 0) && (lo == 0))
2216 goto return1;
2217 } else if (type->flags == XML_SCHEMAS_NINTEGER) {
2218 if (sign == 0)
2219 goto return1;
2220 if ((hi == 0) && (mi == 0) && (lo == 0))
2221 goto return1;
2222 } else if (type->flags == XML_SCHEMAS_NNINTEGER) {
2223 if ((sign == 1) &&
2224 ((hi != 0) || (mi != 0) || (lo != 0)))
2225 goto return1;
2226 }
2227 /*
2228 * We can store a value only if no overflow occured
2229 */
2230 if ((ret > 0) && (val != NULL)) {
2231 v = xmlSchemaNewValue(type->flags);
2232 if (v != NULL) {
2233 v->value.decimal.lo = lo;
2234 v->value.decimal.mi = lo;
2235 v->value.decimal.hi = lo;
2236 v->value.decimal.sign = sign;
2237 v->value.decimal.frac = 0;
2238 v->value.decimal.total = cur - value;
2239 *val = v;
2240 }
2241 }
2242 goto return0;
2243 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002244 case XML_SCHEMAS_LONG:
2245 case XML_SCHEMAS_BYTE:
2246 case XML_SCHEMAS_SHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002247 case XML_SCHEMAS_INT:{
2248 const xmlChar *cur = value;
2249 unsigned long lo, mi, hi;
2250 int total = 0;
2251 int sign = 0;
2252
2253 if (cur == NULL)
2254 goto return1;
2255 if (*cur == '-') {
2256 sign = 1;
2257 cur++;
2258 } else if (*cur == '+')
2259 cur++;
2260 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2261 if (ret <= 0)
2262 goto return1;
2263 if (*cur != 0)
2264 goto return1;
2265 if (type->flags == XML_SCHEMAS_LONG) {
2266 if (hi >= 922) {
2267 if (hi > 922)
2268 goto return1;
2269 if (mi >= 33720368) {
2270 if (mi > 33720368)
2271 goto return1;
2272 if ((sign == 0) && (lo > 54775807))
2273 goto return1;
2274 if ((sign == 1) && (lo > 54775808))
2275 goto return1;
2276 }
2277 }
2278 } else if (type->flags == XML_SCHEMAS_INT) {
2279 if (hi != 0)
2280 goto return1;
2281 if (mi >= 21) {
2282 if (mi > 21)
2283 goto return1;
2284 if ((sign == 0) && (lo > 47483647))
2285 goto return1;
2286 if ((sign == 1) && (lo > 47483648))
2287 goto return1;
2288 }
2289 } else if (type->flags == XML_SCHEMAS_SHORT) {
2290 if ((mi != 0) || (hi != 0))
2291 goto return1;
2292 if ((sign == 1) && (lo > 32768))
2293 goto return1;
2294 if ((sign == 0) && (lo > 32767))
2295 goto return1;
2296 } else if (type->flags == XML_SCHEMAS_BYTE) {
2297 if ((mi != 0) || (hi != 0))
2298 goto return1;
2299 if ((sign == 1) && (lo > 128))
2300 goto return1;
2301 if ((sign == 0) && (lo > 127))
2302 goto return1;
2303 }
2304 if (val != NULL) {
2305 v = xmlSchemaNewValue(type->flags);
2306 if (v != NULL) {
2307 v->value.decimal.lo = lo;
2308 v->value.decimal.mi = lo;
2309 v->value.decimal.hi = lo;
2310 v->value.decimal.sign = sign;
2311 v->value.decimal.frac = 0;
2312 v->value.decimal.total = total;
2313 *val = v;
2314 }
2315 }
2316 goto return0;
2317 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002318 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002319 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002320 case XML_SCHEMAS_USHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002321 case XML_SCHEMAS_UBYTE:{
2322 const xmlChar *cur = value;
2323 unsigned long lo, mi, hi;
2324 int total = 0;
2325
2326 if (cur == NULL)
2327 goto return1;
2328 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2329 if (ret <= 0)
2330 goto return1;
2331 if (*cur != 0)
2332 goto return1;
2333 if (type->flags == XML_SCHEMAS_ULONG) {
2334 if (hi >= 1844) {
2335 if (hi > 1844)
2336 goto return1;
2337 if (mi >= 67440737) {
2338 if (mi > 67440737)
2339 goto return1;
2340 if (lo > 9551615)
2341 goto return1;
2342 }
2343 }
2344 } else if (type->flags == XML_SCHEMAS_UINT) {
2345 if (hi != 0)
2346 goto return1;
2347 if (mi >= 42) {
2348 if (mi > 42)
2349 goto return1;
2350 if (lo > 94967295)
2351 goto return1;
2352 }
2353 } else if (type->flags == XML_SCHEMAS_USHORT) {
2354 if ((mi != 0) || (hi != 0))
2355 goto return1;
2356 if (lo > 65535)
2357 goto return1;
2358 } else if (type->flags == XML_SCHEMAS_UBYTE) {
2359 if ((mi != 0) || (hi != 0))
2360 goto return1;
2361 if (lo > 255)
2362 goto return1;
2363 }
2364 if (val != NULL) {
2365 v = xmlSchemaNewValue(type->flags);
2366 if (v != NULL) {
2367 v->value.decimal.lo = lo;
2368 v->value.decimal.mi = mi;
2369 v->value.decimal.hi = hi;
2370 v->value.decimal.sign = 0;
2371 v->value.decimal.frac = 0;
2372 v->value.decimal.total = total;
2373 *val = v;
2374 }
2375 }
2376 goto return0;
2377 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002378 }
2379
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002380 done:
2381 if (norm != NULL)
2382 xmlFree(norm);
2383 return (ret);
2384 return3:
2385 if (norm != NULL)
2386 xmlFree(norm);
2387 return (3);
2388 return1:
2389 if (norm != NULL)
2390 xmlFree(norm);
2391 return (1);
2392 return0:
2393 if (norm != NULL)
2394 xmlFree(norm);
2395 return (0);
2396 error:
2397 if (norm != NULL)
2398 xmlFree(norm);
2399 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002400}
2401
2402/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002403 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00002404 * @type: the predefined type
2405 * @value: the value to check
2406 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002407 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00002408 *
2409 * Check that a value conforms to the lexical space of the predefined type.
2410 * if true a value is computed and returned in @val.
2411 *
2412 * Returns 0 if this validates, a positive error code number otherwise
2413 * and -1 in case of internal or API error.
2414 */
2415int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002416xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
2417 xmlSchemaValPtr *val, xmlNodePtr node) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002418 return(xmlSchemaValAtomicType(type, value, val, node, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00002419}
2420
2421/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002422 * xmlSchemaValidatePredefinedType:
2423 * @type: the predefined type
2424 * @value: the value to check
2425 * @val: the return computed value
2426 *
2427 * Check that a value conforms to the lexical space of the predefined type.
2428 * if true a value is computed and returned in @val.
2429 *
2430 * Returns 0 if this validates, a positive error code number otherwise
2431 * and -1 in case of internal or API error.
2432 */
2433int
2434xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
2435 xmlSchemaValPtr *val) {
2436 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
2437}
2438
2439/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002440 * xmlSchemaCompareDecimals:
2441 * @x: a first decimal value
2442 * @y: a second decimal value
2443 *
2444 * Compare 2 decimals
2445 *
2446 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
2447 */
2448static int
2449xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
2450{
2451 xmlSchemaValPtr swp;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002452 int order = 1, p;
Daniel Veillard4255d502002-04-16 15:50:10 +00002453 unsigned long tmp;
2454
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002455 if ((x->value.decimal.sign) &&
2456 ((x->value.decimal.lo != 0) ||
2457 (x->value.decimal.mi != 0) ||
2458 (x->value.decimal.hi != 0))) {
2459 if ((y->value.decimal.sign) &&
2460 ((y->value.decimal.lo != 0) ||
2461 (y->value.decimal.mi != 0) ||
2462 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00002463 order = -1;
2464 else
2465 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002466 } else if ((y->value.decimal.sign) &&
2467 ((y->value.decimal.lo != 0) ||
2468 (y->value.decimal.mi != 0) ||
2469 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002470 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002471 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002472 if (x->value.decimal.frac == y->value.decimal.frac) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002473 if (x->value.decimal.hi < y->value.decimal.hi)
2474 return (-order);
2475 if (x->value.decimal.hi < y->value.decimal.hi)
2476 return (order);
2477 if (x->value.decimal.mi < y->value.decimal.mi)
2478 return (-order);
2479 if (x->value.decimal.mi < y->value.decimal.mi)
2480 return (order);
2481 if (x->value.decimal.lo < y->value.decimal.lo)
Daniel Veillard80b19092003-03-28 13:29:53 +00002482 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002483 if (x->value.decimal.lo > y->value.decimal.lo)
Daniel Veillard80b19092003-03-28 13:29:53 +00002484 return(order);
2485 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002486 }
2487 if (y->value.decimal.frac > x->value.decimal.frac) {
2488 swp = y;
2489 y = x;
2490 x = swp;
2491 order = -order;
2492 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002493 p = powten[x->value.decimal.frac - y->value.decimal.frac];
2494 tmp = x->value.decimal.lo / p;
2495 if (tmp > y->value.decimal.lo)
Daniel Veillard4255d502002-04-16 15:50:10 +00002496 return (order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002497 if (tmp < y->value.decimal.lo)
Daniel Veillard4255d502002-04-16 15:50:10 +00002498 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002499 tmp = y->value.decimal.lo * p;
2500 if (x->value.decimal.lo < tmp)
Daniel Veillard4255d502002-04-16 15:50:10 +00002501 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002502 if (x->value.decimal.lo == tmp)
Daniel Veillard4255d502002-04-16 15:50:10 +00002503 return (0);
2504 return (order);
2505}
2506
2507/**
Daniel Veillard070803b2002-05-03 07:29:38 +00002508 * xmlSchemaCompareDurations:
2509 * @x: a first duration value
2510 * @y: a second duration value
2511 *
2512 * Compare 2 durations
2513 *
2514 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2515 * case of error
2516 */
2517static int
2518xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
2519{
2520 long carry, mon, day;
2521 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00002522 int invert = 1;
2523 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00002524 static const long dayRange [2][12] = {
2525 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
2526 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
2527
2528 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00002529 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00002530
2531 /* months */
2532 mon = x->value.dur.mon - y->value.dur.mon;
2533
2534 /* seconds */
2535 sec = x->value.dur.sec - y->value.dur.sec;
2536 carry = (long)sec / SECS_PER_DAY;
2537 sec -= (double)(carry * SECS_PER_DAY);
2538
2539 /* days */
2540 day = x->value.dur.day - y->value.dur.day + carry;
2541
2542 /* easy test */
2543 if (mon == 0) {
2544 if (day == 0)
2545 if (sec == 0.0)
2546 return 0;
2547 else if (sec < 0.0)
2548 return -1;
2549 else
2550 return 1;
2551 else if (day < 0)
2552 return -1;
2553 else
2554 return 1;
2555 }
2556
2557 if (mon > 0) {
2558 if ((day >= 0) && (sec >= 0.0))
2559 return 1;
2560 else {
2561 xmon = mon;
2562 xday = -day;
2563 }
2564 } else if ((day <= 0) && (sec <= 0.0)) {
2565 return -1;
2566 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00002567 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00002568 xmon = -mon;
2569 xday = day;
2570 }
2571
2572 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00002573 if (myear == 0) {
2574 minday = 0;
2575 maxday = 0;
2576 } else {
2577 maxday = 366 * ((myear + 3) / 4) +
2578 365 * ((myear - 1) % 4);
2579 minday = maxday - 1;
2580 }
2581
Daniel Veillard070803b2002-05-03 07:29:38 +00002582 xmon = xmon % 12;
2583 minday += dayRange[0][xmon];
2584 maxday += dayRange[1][xmon];
2585
Daniel Veillard80b19092003-03-28 13:29:53 +00002586 if ((maxday == minday) && (maxday == xday))
2587 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00002588 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00002589 return(-invert);
2590 if (minday > xday)
2591 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00002592
2593 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00002594 return 2;
2595}
2596
2597/*
2598 * macros for adding date/times and durations
2599 */
2600#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
2601#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
2602#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
2603#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
2604
2605/**
2606 * _xmlSchemaDateAdd:
2607 * @dt: an #xmlSchemaValPtr
2608 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
2609 *
2610 * Compute a new date/time from @dt and @dur. This function assumes @dt
2611 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
2612 * or #XML_SCHEMAS_GYEAR.
2613 *
2614 * Returns date/time pointer or NULL.
2615 */
2616static xmlSchemaValPtr
2617_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
2618{
2619 xmlSchemaValPtr ret;
2620 long carry, tempdays, temp;
2621 xmlSchemaValDatePtr r, d;
2622 xmlSchemaValDurationPtr u;
2623
2624 if ((dt == NULL) || (dur == NULL))
2625 return NULL;
2626
2627 ret = xmlSchemaNewValue(dt->type);
2628 if (ret == NULL)
2629 return NULL;
2630
2631 r = &(ret->value.date);
2632 d = &(dt->value.date);
2633 u = &(dur->value.dur);
2634
2635 /* normalization */
2636 if (d->mon == 0)
2637 d->mon = 1;
2638
2639 /* normalize for time zone offset */
2640 u->sec -= (d->tzo * 60);
2641 d->tzo = 0;
2642
2643 /* normalization */
2644 if (d->day == 0)
2645 d->day = 1;
2646
2647 /* month */
2648 carry = d->mon + u->mon;
2649 r->mon = MODULO_RANGE(carry, 1, 13);
2650 carry = FQUOTIENT_RANGE(carry, 1, 13);
2651
2652 /* year (may be modified later) */
2653 r->year = d->year + carry;
2654 if (r->year == 0) {
2655 if (d->year > 0)
2656 r->year--;
2657 else
2658 r->year++;
2659 }
2660
2661 /* time zone */
2662 r->tzo = d->tzo;
2663 r->tz_flag = d->tz_flag;
2664
2665 /* seconds */
2666 r->sec = d->sec + u->sec;
2667 carry = FQUOTIENT((long)r->sec, 60);
2668 if (r->sec != 0.0) {
2669 r->sec = MODULO(r->sec, 60.0);
2670 }
2671
2672 /* minute */
2673 carry += d->min;
2674 r->min = MODULO(carry, 60);
2675 carry = FQUOTIENT(carry, 60);
2676
2677 /* hours */
2678 carry += d->hour;
2679 r->hour = MODULO(carry, 24);
2680 carry = FQUOTIENT(carry, 24);
2681
2682 /*
2683 * days
2684 * Note we use tempdays because the temporary values may need more
2685 * than 5 bits
2686 */
2687 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
2688 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
2689 tempdays = MAX_DAYINMONTH(r->year, r->mon);
2690 else if (d->day < 1)
2691 tempdays = 1;
2692 else
2693 tempdays = d->day;
2694
2695 tempdays += u->day + carry;
2696
2697 while (1) {
2698 if (tempdays < 1) {
2699 long tmon = MODULO_RANGE(r->mon-1, 1, 13);
2700 long tyr = r->year + FQUOTIENT_RANGE(r->mon-1, 1, 13);
2701 if (tyr == 0)
2702 tyr--;
2703 tempdays += MAX_DAYINMONTH(tyr, tmon);
2704 carry = -1;
2705 } else if (tempdays > MAX_DAYINMONTH(r->year, r->mon)) {
2706 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
2707 carry = 1;
2708 } else
2709 break;
2710
2711 temp = r->mon + carry;
2712 r->mon = MODULO_RANGE(temp, 1, 13);
2713 r->year = r->year + FQUOTIENT_RANGE(temp, 1, 13);
2714 if (r->year == 0) {
2715 if (temp < 1)
2716 r->year--;
2717 else
2718 r->year++;
2719 }
2720 }
2721
2722 r->day = tempdays;
2723
2724 /*
2725 * adjust the date/time type to the date values
2726 */
2727 if (ret->type != XML_SCHEMAS_DATETIME) {
2728 if ((r->hour) || (r->min) || (r->sec))
2729 ret->type = XML_SCHEMAS_DATETIME;
2730 else if (ret->type != XML_SCHEMAS_DATE) {
2731 if ((r->mon != 1) && (r->day != 1))
2732 ret->type = XML_SCHEMAS_DATE;
2733 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
2734 ret->type = XML_SCHEMAS_GYEARMONTH;
2735 }
2736 }
2737
2738 return ret;
2739}
2740
2741/**
2742 * xmlSchemaDupVal:
2743 * @v: value to duplicate
2744 *
2745 * returns a duplicated value.
2746 */
2747static xmlSchemaValPtr
2748xmlSchemaDupVal (xmlSchemaValPtr v)
2749{
2750 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
2751 if (ret == NULL)
2752 return ret;
2753
2754 memcpy(ret, v, sizeof(xmlSchemaVal));
2755 return ret;
2756}
2757
2758/**
2759 * xmlSchemaDateNormalize:
2760 * @dt: an #xmlSchemaValPtr
2761 *
2762 * Normalize @dt to GMT time.
2763 *
2764 */
2765static xmlSchemaValPtr
2766xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
2767{
2768 xmlSchemaValPtr dur, ret;
2769
2770 if (dt == NULL)
2771 return NULL;
2772
2773 if (((dt->type != XML_SCHEMAS_TIME) &&
2774 (dt->type != XML_SCHEMAS_DATETIME)) || (dt->value.date.tzo == 0))
2775 return xmlSchemaDupVal(dt);
2776
2777 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
2778 if (dur == NULL)
2779 return NULL;
2780
2781 dur->value.date.sec -= offset;
2782
2783 ret = _xmlSchemaDateAdd(dt, dur);
2784 if (ret == NULL)
2785 return NULL;
2786
2787 xmlSchemaFreeValue(dur);
2788
2789 /* ret->value.date.tzo = 0; */
2790 return ret;
2791}
2792
2793/**
2794 * _xmlSchemaDateCastYMToDays:
2795 * @dt: an #xmlSchemaValPtr
2796 *
2797 * Convert mon and year of @dt to total number of days. Take the
2798 * number of years since (or before) 1 AD and add the number of leap
2799 * years. This is a function because negative
2800 * years must be handled a little differently and there is no zero year.
2801 *
2802 * Returns number of days.
2803 */
2804static long
2805_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
2806{
2807 long ret;
2808
2809 if (dt->value.date.year < 0)
2810 ret = (dt->value.date.year * 365) +
2811 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
2812 ((dt->value.date.year+1)/400)) +
2813 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
2814 else
2815 ret = ((dt->value.date.year-1) * 365) +
2816 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
2817 ((dt->value.date.year-1)/400)) +
2818 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
2819
2820 return ret;
2821}
2822
2823/**
2824 * TIME_TO_NUMBER:
2825 * @dt: an #xmlSchemaValPtr
2826 *
2827 * Calculates the number of seconds in the time portion of @dt.
2828 *
2829 * Returns seconds.
2830 */
2831#define TIME_TO_NUMBER(dt) \
2832 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00002833 (dt->value.date.min * SECS_PER_MIN) + \
2834 (dt->value.date.tzo * SECS_PER_MIN)) + \
2835 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00002836
2837/**
2838 * xmlSchemaCompareDates:
2839 * @x: a first date/time value
2840 * @y: a second date/time value
2841 *
2842 * Compare 2 date/times
2843 *
2844 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2845 * case of error
2846 */
2847static int
2848xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
2849{
2850 unsigned char xmask, ymask, xor_mask, and_mask;
2851 xmlSchemaValPtr p1, p2, q1, q2;
2852 long p1d, p2d, q1d, q2d;
2853
2854 if ((x == NULL) || (y == NULL))
2855 return -2;
2856
2857 if (x->value.date.tz_flag) {
2858
2859 if (!y->value.date.tz_flag) {
2860 p1 = xmlSchemaDateNormalize(x, 0);
2861 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2862 /* normalize y + 14:00 */
2863 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
2864
2865 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002866 if (p1d < q1d) {
2867 xmlSchemaFreeValue(p1);
2868 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002869 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002870 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002871 double sec;
2872
2873 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002874 if (sec < 0.0) {
2875 xmlSchemaFreeValue(p1);
2876 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002877 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002878 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002879 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00002880 /* normalize y - 14:00 */
2881 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
2882 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
2883 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002884 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002885 else if (p1d == q2d) {
2886 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
2887 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002888 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002889 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002890 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00002891 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002892 xmlSchemaFreeValue(p1);
2893 xmlSchemaFreeValue(q1);
2894 xmlSchemaFreeValue(q2);
2895 if (ret != 0)
2896 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00002897 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00002898 } else {
2899 xmlSchemaFreeValue(p1);
2900 xmlSchemaFreeValue(q1);
2901 }
Daniel Veillard5a872412002-05-22 06:40:27 +00002902 }
2903 } else if (y->value.date.tz_flag) {
2904 q1 = xmlSchemaDateNormalize(y, 0);
2905 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
2906
2907 /* normalize x - 14:00 */
2908 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
2909 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2910
Daniel Veillardfdc91562002-07-01 21:52:03 +00002911 if (p1d < q1d) {
2912 xmlSchemaFreeValue(p1);
2913 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002914 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002915 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002916 double sec;
2917
2918 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002919 if (sec < 0.0) {
2920 xmlSchemaFreeValue(p1);
2921 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002922 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002923 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002924 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00002925 /* normalize x + 14:00 */
2926 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
2927 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
2928
Daniel Veillard6560a422003-03-27 21:25:38 +00002929 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002930 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00002931 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002932 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
2933 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002934 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002935 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002936 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00002937 }
Daniel Veillard6560a422003-03-27 21:25:38 +00002938 xmlSchemaFreeValue(p1);
2939 xmlSchemaFreeValue(q1);
2940 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002941 if (ret != 0)
2942 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00002943 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00002944 } else {
2945 xmlSchemaFreeValue(p1);
2946 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002947 }
2948 }
2949
2950 /*
2951 * if the same type then calculate the difference
2952 */
2953 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002954 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00002955 q1 = xmlSchemaDateNormalize(y, 0);
2956 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
2957
2958 p1 = xmlSchemaDateNormalize(x, 0);
2959 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2960
Daniel Veillardfdc91562002-07-01 21:52:03 +00002961 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002962 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002963 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002964 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002965 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00002966 double sec;
2967
2968 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
2969 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002970 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002971 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002972 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002973
2974 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002975 xmlSchemaFreeValue(p1);
2976 xmlSchemaFreeValue(q1);
2977 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00002978 }
2979
2980 switch (x->type) {
2981 case XML_SCHEMAS_DATETIME:
2982 xmask = 0xf;
2983 break;
2984 case XML_SCHEMAS_DATE:
2985 xmask = 0x7;
2986 break;
2987 case XML_SCHEMAS_GYEAR:
2988 xmask = 0x1;
2989 break;
2990 case XML_SCHEMAS_GMONTH:
2991 xmask = 0x2;
2992 break;
2993 case XML_SCHEMAS_GDAY:
2994 xmask = 0x3;
2995 break;
2996 case XML_SCHEMAS_GYEARMONTH:
2997 xmask = 0x3;
2998 break;
2999 case XML_SCHEMAS_GMONTHDAY:
3000 xmask = 0x6;
3001 break;
3002 case XML_SCHEMAS_TIME:
3003 xmask = 0x8;
3004 break;
3005 default:
3006 xmask = 0;
3007 break;
3008 }
3009
3010 switch (y->type) {
3011 case XML_SCHEMAS_DATETIME:
3012 ymask = 0xf;
3013 break;
3014 case XML_SCHEMAS_DATE:
3015 ymask = 0x7;
3016 break;
3017 case XML_SCHEMAS_GYEAR:
3018 ymask = 0x1;
3019 break;
3020 case XML_SCHEMAS_GMONTH:
3021 ymask = 0x2;
3022 break;
3023 case XML_SCHEMAS_GDAY:
3024 ymask = 0x3;
3025 break;
3026 case XML_SCHEMAS_GYEARMONTH:
3027 ymask = 0x3;
3028 break;
3029 case XML_SCHEMAS_GMONTHDAY:
3030 ymask = 0x6;
3031 break;
3032 case XML_SCHEMAS_TIME:
3033 ymask = 0x8;
3034 break;
3035 default:
3036 ymask = 0;
3037 break;
3038 }
3039
3040 xor_mask = xmask ^ ymask; /* mark type differences */
3041 and_mask = xmask & ymask; /* mark field specification */
3042
3043 /* year */
3044 if (xor_mask & 1)
3045 return 2; /* indeterminate */
3046 else if (and_mask & 1) {
3047 if (x->value.date.year < y->value.date.year)
3048 return -1;
3049 else if (x->value.date.year > y->value.date.year)
3050 return 1;
3051 }
3052
3053 /* month */
3054 if (xor_mask & 2)
3055 return 2; /* indeterminate */
3056 else if (and_mask & 2) {
3057 if (x->value.date.mon < y->value.date.mon)
3058 return -1;
3059 else if (x->value.date.mon > y->value.date.mon)
3060 return 1;
3061 }
3062
3063 /* day */
3064 if (xor_mask & 4)
3065 return 2; /* indeterminate */
3066 else if (and_mask & 4) {
3067 if (x->value.date.day < y->value.date.day)
3068 return -1;
3069 else if (x->value.date.day > y->value.date.day)
3070 return 1;
3071 }
3072
3073 /* time */
3074 if (xor_mask & 8)
3075 return 2; /* indeterminate */
3076 else if (and_mask & 8) {
3077 if (x->value.date.hour < y->value.date.hour)
3078 return -1;
3079 else if (x->value.date.hour > y->value.date.hour)
3080 return 1;
3081 else if (x->value.date.min < y->value.date.min)
3082 return -1;
3083 else if (x->value.date.min > y->value.date.min)
3084 return 1;
3085 else if (x->value.date.sec < y->value.date.sec)
3086 return -1;
3087 else if (x->value.date.sec > y->value.date.sec)
3088 return 1;
3089 }
3090
Daniel Veillard070803b2002-05-03 07:29:38 +00003091 return 0;
3092}
3093
3094/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00003095 * xmlSchemaCompareNormStrings:
3096 * @x: a first string value
3097 * @y: a second string value
3098 *
3099 * Compare 2 string for their normalized values.
3100 *
3101 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
3102 * case of error
3103 */
3104static int
3105xmlSchemaCompareNormStrings(xmlSchemaValPtr x, xmlSchemaValPtr y) {
3106 const xmlChar *utf1;
3107 const xmlChar *utf2;
3108 int tmp;
3109
3110 if ((x == NULL) || (y == NULL))
3111 return(-2);
3112 utf1 = x->value.str;
3113 utf2 = y->value.str;
3114
William M. Brack76e95df2003-10-18 16:20:14 +00003115 while (IS_BLANK_CH(*utf1)) utf1++;
3116 while (IS_BLANK_CH(*utf2)) utf2++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003117 while ((*utf1 != 0) && (*utf2 != 0)) {
William M. Brack76e95df2003-10-18 16:20:14 +00003118 if (IS_BLANK_CH(*utf1)) {
3119 if (!IS_BLANK_CH(*utf2)) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00003120 tmp = *utf1 - *utf2;
3121 return(tmp);
3122 }
William M. Brack76e95df2003-10-18 16:20:14 +00003123 while (IS_BLANK_CH(*utf1)) utf1++;
3124 while (IS_BLANK_CH(*utf2)) utf2++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003125 } else {
3126 tmp = *utf1++ - *utf2++;
3127 if (tmp < 0)
3128 return(-1);
3129 if (tmp > 0)
3130 return(1);
3131 }
3132 }
3133 if (*utf1 != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003134 while (IS_BLANK_CH(*utf1)) utf1++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003135 if (*utf1 != 0)
3136 return(1);
3137 }
3138 if (*utf2 != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003139 while (IS_BLANK_CH(*utf2)) utf2++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003140 if (*utf2 != 0)
3141 return(-1);
3142 }
3143 return(0);
3144}
3145
3146/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003147 * xmlSchemaCompareFloats:
3148 * @x: a first float or double value
3149 * @y: a second float or double value
3150 *
3151 * Compare 2 values
3152 *
3153 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3154 * case of error
3155 */
3156static int
3157xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
3158 double d1, d2;
3159
3160 if ((x == NULL) || (y == NULL))
3161 return(-2);
3162
3163 /*
3164 * Cast everything to doubles.
3165 */
3166 if (x->type == XML_SCHEMAS_DOUBLE)
3167 d1 = x->value.d;
3168 else if (x->type == XML_SCHEMAS_FLOAT)
3169 d1 = x->value.f;
3170 else
3171 return(-2);
3172
3173 if (y->type == XML_SCHEMAS_DOUBLE)
3174 d2 = y->value.d;
3175 else if (y->type == XML_SCHEMAS_FLOAT)
3176 d2 = y->value.f;
3177 else
3178 return(-2);
3179
3180 /*
3181 * Check for special cases.
3182 */
3183 if (xmlXPathIsNaN(d1)) {
3184 if (xmlXPathIsNaN(d2))
3185 return(0);
3186 return(1);
3187 }
3188 if (xmlXPathIsNaN(d2))
3189 return(-1);
3190 if (d1 == xmlXPathPINF) {
3191 if (d2 == xmlXPathPINF)
3192 return(0);
3193 return(1);
3194 }
3195 if (d2 == xmlXPathPINF)
3196 return(-1);
3197 if (d1 == xmlXPathNINF) {
3198 if (d2 == xmlXPathNINF)
3199 return(0);
3200 return(-1);
3201 }
3202 if (d2 == xmlXPathNINF)
3203 return(1);
3204
3205 /*
3206 * basic tests, the last one we should have equality, but
3207 * portability is more important than speed and handling
3208 * NaN or Inf in a portable way is always a challenge, so ...
3209 */
3210 if (d1 < d2)
3211 return(-1);
3212 if (d1 > d2)
3213 return(1);
3214 if (d1 == d2)
3215 return(0);
3216 return(2);
3217}
3218
3219/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003220 * xmlSchemaCompareValues:
3221 * @x: a first value
3222 * @y: a second value
3223 *
3224 * Compare 2 values
3225 *
Daniel Veillard5a872412002-05-22 06:40:27 +00003226 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3227 * case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003228 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003229int
Daniel Veillard4255d502002-04-16 15:50:10 +00003230xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
3231 if ((x == NULL) || (y == NULL))
3232 return(-2);
3233
3234 switch (x->type) {
Daniel Veillard80b19092003-03-28 13:29:53 +00003235 case XML_SCHEMAS_UNKNOWN:
3236 return(-2);
3237 case XML_SCHEMAS_INTEGER:
3238 case XML_SCHEMAS_NPINTEGER:
3239 case XML_SCHEMAS_NINTEGER:
3240 case XML_SCHEMAS_NNINTEGER:
3241 case XML_SCHEMAS_PINTEGER:
3242 case XML_SCHEMAS_INT:
3243 case XML_SCHEMAS_UINT:
3244 case XML_SCHEMAS_LONG:
3245 case XML_SCHEMAS_ULONG:
3246 case XML_SCHEMAS_SHORT:
3247 case XML_SCHEMAS_USHORT:
3248 case XML_SCHEMAS_BYTE:
3249 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00003250 case XML_SCHEMAS_DECIMAL:
Daniel Veillard80b19092003-03-28 13:29:53 +00003251 if (y->type == x->type)
3252 return(xmlSchemaCompareDecimals(x, y));
3253 if ((y->type == XML_SCHEMAS_DECIMAL) ||
3254 (y->type == XML_SCHEMAS_INTEGER) ||
3255 (y->type == XML_SCHEMAS_NPINTEGER) ||
3256 (y->type == XML_SCHEMAS_NINTEGER) ||
3257 (y->type == XML_SCHEMAS_NNINTEGER) ||
3258 (y->type == XML_SCHEMAS_PINTEGER) ||
3259 (y->type == XML_SCHEMAS_INT) ||
3260 (y->type == XML_SCHEMAS_UINT) ||
3261 (y->type == XML_SCHEMAS_LONG) ||
3262 (y->type == XML_SCHEMAS_ULONG) ||
3263 (y->type == XML_SCHEMAS_SHORT) ||
3264 (y->type == XML_SCHEMAS_USHORT) ||
3265 (y->type == XML_SCHEMAS_BYTE) ||
3266 (y->type == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00003267 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003268 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00003269 case XML_SCHEMAS_DURATION:
3270 if (y->type == XML_SCHEMAS_DURATION)
3271 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003272 return(-2);
3273 case XML_SCHEMAS_TIME:
3274 case XML_SCHEMAS_GDAY:
3275 case XML_SCHEMAS_GMONTH:
3276 case XML_SCHEMAS_GMONTHDAY:
3277 case XML_SCHEMAS_GYEAR:
3278 case XML_SCHEMAS_GYEARMONTH:
3279 case XML_SCHEMAS_DATE:
3280 case XML_SCHEMAS_DATETIME:
3281 if ((y->type == XML_SCHEMAS_DATETIME) ||
3282 (y->type == XML_SCHEMAS_TIME) ||
3283 (y->type == XML_SCHEMAS_GDAY) ||
3284 (y->type == XML_SCHEMAS_GMONTH) ||
3285 (y->type == XML_SCHEMAS_GMONTHDAY) ||
3286 (y->type == XML_SCHEMAS_GYEAR) ||
3287 (y->type == XML_SCHEMAS_DATE) ||
3288 (y->type == XML_SCHEMAS_GYEARMONTH))
3289 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003290 return (-2);
Daniel Veillard80b19092003-03-28 13:29:53 +00003291 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00003292 case XML_SCHEMAS_TOKEN:
3293 case XML_SCHEMAS_LANGUAGE:
3294 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00003295 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00003296 case XML_SCHEMAS_NCNAME:
3297 case XML_SCHEMAS_ID:
3298 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00003299 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00003300 case XML_SCHEMAS_NOTATION:
3301 case XML_SCHEMAS_ANYURI:
Daniel Veillardc4c21552003-03-29 10:53:38 +00003302 if ((y->type == XML_SCHEMAS_NORMSTRING) ||
3303 (y->type == XML_SCHEMAS_TOKEN) ||
3304 (y->type == XML_SCHEMAS_LANGUAGE) ||
3305 (y->type == XML_SCHEMAS_NMTOKEN) ||
3306 (y->type == XML_SCHEMAS_NAME) ||
3307 (y->type == XML_SCHEMAS_QNAME) ||
3308 (y->type == XML_SCHEMAS_NCNAME) ||
3309 (y->type == XML_SCHEMAS_ID) ||
3310 (y->type == XML_SCHEMAS_IDREF) ||
3311 (y->type == XML_SCHEMAS_ENTITY) ||
3312 (y->type == XML_SCHEMAS_NOTATION) ||
3313 (y->type == XML_SCHEMAS_ANYURI))
3314 return (xmlSchemaCompareNormStrings(x, y));
3315 return (-2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003316 case XML_SCHEMAS_QNAME:
3317 if (y->type == XML_SCHEMAS_QNAME) {
3318 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
3319 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
3320 return(0);
3321 return(2);
3322 }
3323 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003324 case XML_SCHEMAS_FLOAT:
3325 case XML_SCHEMAS_DOUBLE:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003326 if ((y->type == XML_SCHEMAS_FLOAT) ||
3327 (y->type == XML_SCHEMAS_DOUBLE))
3328 return (xmlSchemaCompareFloats(x, y));
3329 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003330 case XML_SCHEMAS_BOOLEAN:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003331 if (y->type == XML_SCHEMAS_BOOLEAN) {
3332 if (x->value.b == y->value.b)
3333 return(0);
3334 if (x->value.b == 0)
3335 return(-1);
3336 return(1);
3337 }
3338 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00003339 case XML_SCHEMAS_HEXBINARY:
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003340 if (y->type == XML_SCHEMAS_HEXBINARY) {
3341 if (x->value.hex.total == y->value.hex.total) {
3342 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
3343 if (ret > 0)
3344 return(1);
3345 else if (ret == 0)
3346 return(0);
3347 }
3348 else if (x->value.hex.total > y->value.hex.total)
3349 return(1);
3350
3351 return(-1);
3352 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00003353 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003354 case XML_SCHEMAS_BASE64BINARY:
3355 if (y->type == XML_SCHEMAS_BASE64BINARY) {
3356 if (x->value.base64.total == y->value.base64.total) {
3357 int ret = xmlStrcmp(x->value.base64.str,
3358 y->value.base64.str);
3359 if (ret > 0)
3360 return(1);
3361 else if (ret == 0)
3362 return(0);
3363 }
3364 else if (x->value.base64.total > y->value.base64.total)
3365 return(1);
3366 else
3367 return(-1);
3368 }
3369 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003370 case XML_SCHEMAS_STRING:
3371 case XML_SCHEMAS_IDREFS:
3372 case XML_SCHEMAS_ENTITIES:
3373 case XML_SCHEMAS_NMTOKENS:
3374 TODO
3375 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00003376 }
Daniel Veillard5a872412002-05-22 06:40:27 +00003377 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00003378}
3379
3380/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00003381 * xmlSchemaNormLen:
3382 * @value: a string
3383 *
3384 * Computes the UTF8 length of the normalized value of the string
3385 *
3386 * Returns the length or -1 in case of error.
3387 */
3388static int
3389xmlSchemaNormLen(const xmlChar *value) {
3390 const xmlChar *utf;
3391 int ret = 0;
3392
3393 if (value == NULL)
3394 return(-1);
3395 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00003396 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003397 while (*utf != 0) {
3398 if (utf[0] & 0x80) {
3399 if ((utf[1] & 0xc0) != 0x80)
3400 return(-1);
3401 if ((utf[0] & 0xe0) == 0xe0) {
3402 if ((utf[2] & 0xc0) != 0x80)
3403 return(-1);
3404 if ((utf[0] & 0xf0) == 0xf0) {
3405 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
3406 return(-1);
3407 utf += 4;
3408 } else {
3409 utf += 3;
3410 }
3411 } else {
3412 utf += 2;
3413 }
William M. Brack76e95df2003-10-18 16:20:14 +00003414 } else if (IS_BLANK_CH(*utf)) {
3415 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003416 if (*utf == 0)
3417 break;
3418 } else {
3419 utf++;
3420 }
3421 ret++;
3422 }
3423 return(ret);
3424}
3425
3426/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003427 * xmlSchemaValidateFacet:
Daniel Veillard01c13b52002-12-10 15:19:08 +00003428 * @base: the base type
Daniel Veillard4255d502002-04-16 15:50:10 +00003429 * @facet: the facet to check
3430 * @value: the lexical repr of the value to validate
3431 * @val: the precomputed value
3432 *
3433 * Check a value against a facet condition
3434 *
3435 * Returns 0 if the element is schemas valid, a positive error code
3436 * number otherwise and -1 in case of internal or API error.
3437 */
3438int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00003439xmlSchemaValidateFacet(xmlSchemaTypePtr base ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +00003440 xmlSchemaFacetPtr facet,
Daniel Veillard4255d502002-04-16 15:50:10 +00003441 const xmlChar *value, xmlSchemaValPtr val)
3442{
3443 int ret;
3444
3445 switch (facet->type) {
3446 case XML_SCHEMA_FACET_PATTERN:
3447 ret = xmlRegexpExec(facet->regexp, value);
3448 if (ret == 1)
3449 return(0);
3450 if (ret == 0) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003451 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003452 return(1);
3453 }
3454 return(ret);
3455 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
3456 ret = xmlSchemaCompareValues(val, facet->val);
3457 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003458 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003459 return(-1);
3460 }
3461 if (ret == -1)
3462 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003463 /* error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003464 return(1);
Daniel Veillard070803b2002-05-03 07:29:38 +00003465 case XML_SCHEMA_FACET_MAXINCLUSIVE:
3466 ret = xmlSchemaCompareValues(val, facet->val);
3467 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003468 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003469 return(-1);
3470 }
3471 if ((ret == -1) || (ret == 0))
3472 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003473 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003474 return(1);
3475 case XML_SCHEMA_FACET_MINEXCLUSIVE:
3476 ret = xmlSchemaCompareValues(val, facet->val);
3477 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003478 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003479 return(-1);
3480 }
3481 if (ret == 1)
3482 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003483 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003484 return(1);
3485 case XML_SCHEMA_FACET_MININCLUSIVE:
3486 ret = xmlSchemaCompareValues(val, facet->val);
3487 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003488 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003489 return(-1);
3490 }
3491 if ((ret == 1) || (ret == 0))
3492 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003493 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003494 return(1);
Daniel Veillard8651f532002-04-17 09:06:27 +00003495 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003496 /* TODO whitespaces */
Daniel Veillard8651f532002-04-17 09:06:27 +00003497 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00003498 case XML_SCHEMA_FACET_ENUMERATION:
3499 if ((facet->value != NULL) &&
3500 (xmlStrEqual(facet->value, value)))
3501 return(0);
3502 return(1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003503 case XML_SCHEMA_FACET_LENGTH:
3504 case XML_SCHEMA_FACET_MAXLENGTH:
3505 case XML_SCHEMA_FACET_MINLENGTH: {
3506 unsigned int len = 0;
3507
3508 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003509 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
3510 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003511 (facet->val->value.decimal.frac != 0)) {
3512 return(-1);
3513 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00003514 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003515 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003516 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
3517 len = val->value.base64.total;
3518 else {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003519 switch (base->flags) {
Daniel Veillard560c2a42003-07-06 21:13:49 +00003520 case XML_SCHEMAS_IDREF:
3521 case XML_SCHEMAS_NORMSTRING:
3522 case XML_SCHEMAS_TOKEN:
3523 case XML_SCHEMAS_LANGUAGE:
3524 case XML_SCHEMAS_NMTOKEN:
3525 case XML_SCHEMAS_NAME:
3526 case XML_SCHEMAS_NCNAME:
3527 case XML_SCHEMAS_ID:
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003528 len = xmlSchemaNormLen(value);
3529 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00003530 case XML_SCHEMAS_STRING:
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003531 len = xmlUTF8Strlen(value);
3532 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00003533 default:
3534 TODO
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003535 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003536 }
3537 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003538 if (len != facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003539 return(1);
3540 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003541 if (len < facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003542 return(1);
3543 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003544 if (len > facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003545 return(1);
3546 }
3547 break;
3548 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00003549 case XML_SCHEMA_FACET_TOTALDIGITS:
3550 case XML_SCHEMA_FACET_FRACTIONDIGITS:
3551
3552 if ((facet->val == NULL) ||
3553 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
3554 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
3555 (facet->val->value.decimal.frac != 0)) {
3556 return(-1);
3557 }
3558 if ((val == NULL) ||
3559 ((val->type != XML_SCHEMAS_DECIMAL) &&
3560 (val->type != XML_SCHEMAS_INTEGER) &&
3561 (val->type != XML_SCHEMAS_NPINTEGER) &&
3562 (val->type != XML_SCHEMAS_NINTEGER) &&
3563 (val->type != XML_SCHEMAS_NNINTEGER) &&
3564 (val->type != XML_SCHEMAS_PINTEGER) &&
3565 (val->type != XML_SCHEMAS_INT) &&
3566 (val->type != XML_SCHEMAS_UINT) &&
3567 (val->type != XML_SCHEMAS_LONG) &&
3568 (val->type != XML_SCHEMAS_ULONG) &&
3569 (val->type != XML_SCHEMAS_SHORT) &&
3570 (val->type != XML_SCHEMAS_USHORT) &&
3571 (val->type != XML_SCHEMAS_BYTE) &&
3572 (val->type != XML_SCHEMAS_UBYTE))) {
3573 return(-1);
3574 }
3575 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
3576 if (val->value.decimal.total > facet->val->value.decimal.lo)
3577 return(1);
3578
3579 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
3580 if (val->value.decimal.frac > facet->val->value.decimal.lo)
3581 return(1);
3582 }
3583 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00003584 default:
3585 TODO
3586 }
3587 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003588
Daniel Veillard4255d502002-04-16 15:50:10 +00003589}
3590
3591#endif /* LIBXML_SCHEMAS_ENABLED */