blob: dd859b7975621e2f0f9079de26f4c9ca36f44ef7 [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));
264 ret->name = xmlStrdup((const xmlChar *)name);
265 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 Veillardb6c7f412003-03-29 16:41:55 +00001480 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001481 TODO goto return0;
1482 case XML_SCHEMAS_DECIMAL:{
1483 const xmlChar *cur = value, *tmp;
1484 unsigned int frac = 0, len, neg = 0;
1485 unsigned long base = 0;
1486
1487 if (cur == NULL)
1488 goto return1;
1489 if (*cur == '+')
1490 cur++;
1491 else if (*cur == '-') {
1492 neg = 1;
1493 cur++;
1494 }
1495 tmp = cur;
1496 while ((*cur >= '0') && (*cur <= '9')) {
1497 base = base * 10 + (*cur - '0');
1498 cur++;
1499 }
1500 len = cur - tmp;
1501 if (*cur == '.') {
1502 cur++;
1503 tmp = cur;
1504 while ((*cur >= '0') && (*cur <= '9')) {
1505 base = base * 10 + (*cur - '0');
1506 cur++;
1507 }
1508 frac = cur - tmp;
1509 }
1510 if (*cur != 0)
1511 goto return1;
1512 if (val != NULL) {
1513 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1514 if (v != NULL) {
1515 v->value.decimal.lo = base;
1516 v->value.decimal.sign = neg;
1517 v->value.decimal.frac = frac;
1518 v->value.decimal.total = frac + len;
1519 *val = v;
1520 }
1521 }
1522 goto return0;
1523 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001524 case XML_SCHEMAS_TIME:
1525 case XML_SCHEMAS_GDAY:
1526 case XML_SCHEMAS_GMONTH:
1527 case XML_SCHEMAS_GMONTHDAY:
1528 case XML_SCHEMAS_GYEAR:
1529 case XML_SCHEMAS_GYEARMONTH:
1530 case XML_SCHEMAS_DATE:
1531 case XML_SCHEMAS_DATETIME:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001532 ret = xmlSchemaValidateDates(type->flags, value, val);
1533 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001534 case XML_SCHEMAS_DURATION:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001535 ret = xmlSchemaValidateDuration(type, value, val);
1536 break;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001537 case XML_SCHEMAS_FLOAT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001538 case XML_SCHEMAS_DOUBLE:{
1539 const xmlChar *cur = value;
1540 int neg = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001541
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001542 if (cur == NULL)
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00001543 goto return1;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001544 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
1545 cur += 3;
1546 if (*cur != 0)
1547 goto return1;
1548 if (val != NULL) {
1549 if (type == xmlSchemaTypeFloatDef) {
1550 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1551 if (v != NULL) {
1552 v->value.f = (float) xmlXPathNAN;
1553 } else {
1554 xmlSchemaFreeValue(v);
1555 goto error;
1556 }
1557 } else {
1558 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1559 if (v != NULL) {
1560 v->value.d = xmlXPathNAN;
1561 } else {
1562 xmlSchemaFreeValue(v);
1563 goto error;
1564 }
1565 }
1566 *val = v;
1567 }
1568 goto return0;
1569 }
1570 if (*cur == '-') {
1571 neg = 1;
1572 cur++;
1573 }
1574 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
1575 cur += 3;
1576 if (*cur != 0)
1577 goto return1;
1578 if (val != NULL) {
1579 if (type == xmlSchemaTypeFloatDef) {
1580 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1581 if (v != NULL) {
1582 if (neg)
1583 v->value.f = (float) xmlXPathNINF;
1584 else
1585 v->value.f = (float) xmlXPathPINF;
1586 } else {
1587 xmlSchemaFreeValue(v);
1588 goto error;
1589 }
1590 } else {
1591 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1592 if (v != NULL) {
1593 if (neg)
1594 v->value.d = xmlXPathNINF;
1595 else
1596 v->value.d = xmlXPathPINF;
1597 } else {
1598 xmlSchemaFreeValue(v);
1599 goto error;
1600 }
1601 }
1602 *val = v;
1603 }
1604 goto return0;
1605 }
1606 if ((neg == 0) && (*cur == '+'))
1607 cur++;
1608 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
1609 goto return1;
1610 while ((*cur >= '0') && (*cur <= '9')) {
1611 cur++;
1612 }
1613 if (*cur == '.') {
1614 cur++;
1615 while ((*cur >= '0') && (*cur <= '9'))
1616 cur++;
1617 }
1618 if ((*cur == 'e') || (*cur == 'E')) {
1619 cur++;
1620 if ((*cur == '-') || (*cur == '+'))
1621 cur++;
1622 while ((*cur >= '0') && (*cur <= '9'))
1623 cur++;
1624 }
1625 if (*cur != 0)
1626 goto return1;
1627 if (val != NULL) {
1628 if (type == xmlSchemaTypeFloatDef) {
1629 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1630 if (v != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001631 if (sscanf((const char *) value, "%f",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001632 &(v->value.f)) == 1) {
1633 *val = v;
1634 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001635 xmlSchemaFreeValue(v);
1636 goto return1;
1637 }
1638 } else {
1639 goto error;
1640 }
1641 } else {
1642 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1643 if (v != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001644 if (sscanf((const char *) value, "%lf",
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001645 &(v->value.d)) == 1) {
1646 *val = v;
1647 } else {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001648 xmlSchemaFreeValue(v);
1649 goto return1;
1650 }
1651 } else {
1652 goto error;
1653 }
1654 }
1655 }
1656 goto return0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00001657 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001658 case XML_SCHEMAS_BOOLEAN:{
1659 const xmlChar *cur = value;
1660
1661 if ((cur[0] == '0') && (cur[1] == 0))
1662 ret = 0;
1663 else if ((cur[0] == '1') && (cur[1] == 0))
1664 ret = 1;
1665 else if ((cur[0] == 't') && (cur[1] == 'r')
1666 && (cur[2] == 'u') && (cur[3] == 'e')
1667 && (cur[4] == 0))
1668 ret = 1;
1669 else if ((cur[0] == 'f') && (cur[1] == 'a')
1670 && (cur[2] == 'l') && (cur[3] == 's')
1671 && (cur[4] == 'e') && (cur[5] == 0))
1672 ret = 0;
1673 else
1674 goto return1;
1675 if (val != NULL) {
1676 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
1677 if (v != NULL) {
1678 v->value.b = ret;
1679 *val = v;
1680 } else {
1681 goto error;
1682 }
1683 }
1684 goto return0;
1685 }
1686 case XML_SCHEMAS_TOKEN:{
1687 const xmlChar *cur = value;
1688
William M. Brack76e95df2003-10-18 16:20:14 +00001689 if (IS_BLANK_CH(*cur))
Daniel Veillard1ac24d32003-08-27 14:15:15 +00001690 goto return1;
1691
1692 while (*cur != 0) {
1693 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
1694 goto return1;
1695 } else if (*cur == ' ') {
1696 cur++;
1697 if (*cur == 0)
1698 goto return1;
1699 if (*cur == ' ')
1700 goto return1;
1701 } else {
1702 cur++;
1703 }
1704 }
1705 if (val != NULL) {
1706 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
1707 if (v != NULL) {
1708 v->value.str = xmlStrdup(value);
1709 *val = v;
1710 } else {
1711 goto error;
1712 }
1713 }
1714 goto return0;
1715 }
1716 case XML_SCHEMAS_LANGUAGE:
1717 if (xmlCheckLanguageID(value) == 1) {
1718 if (val != NULL) {
1719 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
1720 if (v != NULL) {
1721 v->value.str = xmlStrdup(value);
1722 *val = v;
1723 } else {
1724 goto error;
1725 }
1726 }
1727 goto return0;
1728 }
1729 goto return1;
1730 case XML_SCHEMAS_NMTOKEN:
1731 if (xmlValidateNMToken(value, 1) == 0) {
1732 if (val != NULL) {
1733 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
1734 if (v != NULL) {
1735 v->value.str = xmlStrdup(value);
1736 *val = v;
1737 } else {
1738 goto error;
1739 }
1740 }
1741 goto return0;
1742 }
1743 goto return1;
1744 case XML_SCHEMAS_NMTOKENS:
1745 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
1746 value, val, node);
1747 if (ret > 0)
1748 ret = 0;
1749 else
1750 ret = 1;
1751 goto done;
1752 case XML_SCHEMAS_NAME:
1753 ret = xmlValidateName(value, 1);
1754 if ((ret == 0) && (val != NULL)) {
1755 TODO;
1756 }
1757 goto done;
1758 case XML_SCHEMAS_QNAME:{
1759 xmlChar *uri = NULL;
1760 xmlChar *local = NULL;
1761
1762 ret = xmlValidateQName(value, 1);
1763 if ((ret == 0) && (node != NULL)) {
1764 xmlChar *prefix;
1765
1766 local = xmlSplitQName2(value, &prefix);
1767 if (prefix != NULL) {
1768 xmlNsPtr ns;
1769
1770 ns = xmlSearchNs(node->doc, node, prefix);
1771 if (ns == NULL)
1772 ret = 1;
1773 else if (val != NULL)
1774 uri = xmlStrdup(ns->href);
1775 }
1776 if ((local != NULL) && ((val == NULL) || (ret != 0)))
1777 xmlFree(local);
1778 if (prefix != NULL)
1779 xmlFree(prefix);
1780 }
1781 if ((ret == 0) && (val != NULL)) {
1782 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
1783 if (v != NULL) {
1784 if (local != NULL)
1785 v->value.qname.name = local;
1786 else
1787 v->value.qname.name = xmlStrdup(value);
1788 if (uri != NULL)
1789 v->value.qname.uri = uri;
1790
1791 *val = v;
1792 } else {
1793 if (local != NULL)
1794 xmlFree(local);
1795 if (uri != NULL)
1796 xmlFree(uri);
1797 goto error;
1798 }
1799 }
1800 goto done;
1801 }
1802 case XML_SCHEMAS_NCNAME:
1803 ret = xmlValidateNCName(value, 1);
1804 if ((ret == 0) && (val != NULL)) {
1805 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
1806 if (v != NULL) {
1807 v->value.str = xmlStrdup(value);
1808 *val = v;
1809 } else {
1810 goto error;
1811 }
1812 }
1813 goto done;
1814 case XML_SCHEMAS_ID:
1815 ret = xmlValidateNCName(value, 1);
1816 if ((ret == 0) && (val != NULL)) {
1817 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
1818 if (v != NULL) {
1819 v->value.str = xmlStrdup(value);
1820 *val = v;
1821 } else {
1822 goto error;
1823 }
1824 }
1825 if ((ret == 0) && (node != NULL) &&
1826 (node->type == XML_ATTRIBUTE_NODE)) {
1827 xmlAttrPtr attr = (xmlAttrPtr) node;
1828
1829 /*
1830 * NOTE: the IDness might have already be declared in the DTD
1831 */
1832 if (attr->atype != XML_ATTRIBUTE_ID) {
1833 xmlIDPtr res;
1834 xmlChar *strip;
1835
1836 strip = xmlSchemaStrip(value);
1837 if (strip != NULL) {
1838 res = xmlAddID(NULL, node->doc, strip, attr);
1839 xmlFree(strip);
1840 } else
1841 res = xmlAddID(NULL, node->doc, value, attr);
1842 if (res == NULL) {
1843 ret = 2;
1844 } else {
1845 attr->atype = XML_ATTRIBUTE_ID;
1846 }
1847 }
1848 }
1849 goto done;
1850 case XML_SCHEMAS_IDREF:
1851 ret = xmlValidateNCName(value, 1);
1852 if ((ret == 0) && (val != NULL)) {
1853 TODO;
1854 }
1855 if ((ret == 0) && (node != NULL) &&
1856 (node->type == XML_ATTRIBUTE_NODE)) {
1857 xmlAttrPtr attr = (xmlAttrPtr) node;
1858 xmlChar *strip;
1859
1860 strip = xmlSchemaStrip(value);
1861 if (strip != NULL) {
1862 xmlAddRef(NULL, node->doc, strip, attr);
1863 xmlFree(strip);
1864 } else
1865 xmlAddRef(NULL, node->doc, value, attr);
1866 attr->atype = XML_ATTRIBUTE_IDREF;
1867 }
1868 goto done;
1869 case XML_SCHEMAS_IDREFS:
1870 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
1871 value, val, node);
1872 if (ret < 0)
1873 ret = 2;
1874 else
1875 ret = 0;
1876 if ((ret == 0) && (node != NULL) &&
1877 (node->type == XML_ATTRIBUTE_NODE)) {
1878 xmlAttrPtr attr = (xmlAttrPtr) node;
1879
1880 attr->atype = XML_ATTRIBUTE_IDREFS;
1881 }
1882 goto done;
1883 case XML_SCHEMAS_ENTITY:{
1884 xmlChar *strip;
1885
1886 ret = xmlValidateNCName(value, 1);
1887 if ((node == NULL) || (node->doc == NULL))
1888 ret = 3;
1889 if (ret == 0) {
1890 xmlEntityPtr ent;
1891
1892 strip = xmlSchemaStrip(value);
1893 if (strip != NULL) {
1894 ent = xmlGetDocEntity(node->doc, strip);
1895 xmlFree(strip);
1896 } else {
1897 ent = xmlGetDocEntity(node->doc, value);
1898 }
1899 if ((ent == NULL) ||
1900 (ent->etype !=
1901 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
1902 ret = 4;
1903 }
1904 if ((ret == 0) && (val != NULL)) {
1905 TODO;
1906 }
1907 if ((ret == 0) && (node != NULL) &&
1908 (node->type == XML_ATTRIBUTE_NODE)) {
1909 xmlAttrPtr attr = (xmlAttrPtr) node;
1910
1911 attr->atype = XML_ATTRIBUTE_ENTITY;
1912 }
1913 goto done;
1914 }
1915 case XML_SCHEMAS_ENTITIES:
1916 if ((node == NULL) || (node->doc == NULL))
1917 goto return3;
1918 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
1919 value, val, node);
1920 if (ret <= 0)
1921 ret = 1;
1922 else
1923 ret = 0;
1924 if ((ret == 0) && (node != NULL) &&
1925 (node->type == XML_ATTRIBUTE_NODE)) {
1926 xmlAttrPtr attr = (xmlAttrPtr) node;
1927
1928 attr->atype = XML_ATTRIBUTE_ENTITIES;
1929 }
1930 goto done;
1931 case XML_SCHEMAS_NOTATION:{
1932 xmlChar *uri = NULL;
1933 xmlChar *local = NULL;
1934
1935 ret = xmlValidateQName(value, 1);
1936 if ((ret == 0) && (node != NULL)) {
1937 xmlChar *prefix;
1938
1939 local = xmlSplitQName2(value, &prefix);
1940 if (prefix != NULL) {
1941 xmlNsPtr ns;
1942
1943 ns = xmlSearchNs(node->doc, node, prefix);
1944 if (ns == NULL)
1945 ret = 1;
1946 else if (val != NULL)
1947 uri = xmlStrdup(ns->href);
1948 }
1949 if ((local != NULL) && ((val == NULL) || (ret != 0)))
1950 xmlFree(local);
1951 if (prefix != NULL)
1952 xmlFree(prefix);
1953 }
1954 if ((node == NULL) || (node->doc == NULL))
1955 ret = 3;
1956 if (ret == 0) {
1957 ret = xmlValidateNotationUse(NULL, node->doc, value);
1958 if (ret == 1)
1959 ret = 0;
1960 else
1961 ret = 1;
1962 }
1963 if ((ret == 0) && (val != NULL)) {
1964 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
1965 if (v != NULL) {
1966 if (local != NULL)
1967 v->value.qname.name = local;
1968 else
1969 v->value.qname.name = xmlStrdup(value);
1970 if (uri != NULL)
1971 v->value.qname.uri = uri;
1972
1973 *val = v;
1974 } else {
1975 if (local != NULL)
1976 xmlFree(local);
1977 if (uri != NULL)
1978 xmlFree(uri);
1979 goto error;
1980 }
1981 }
1982 goto done;
1983 }
1984 case XML_SCHEMAS_ANYURI:{
1985 xmlURIPtr uri;
1986
1987 uri = xmlParseURI((const char *) value);
1988 if (uri == NULL)
1989 goto return1;
1990 if (val != NULL) {
1991 TODO;
1992 }
1993 xmlFreeURI(uri);
1994 goto return0;
1995 }
1996 case XML_SCHEMAS_HEXBINARY:{
1997 const xmlChar *cur = value;
1998 xmlChar *base;
1999 int total, i = 0;
2000
2001 if (cur == NULL)
2002 goto return1;
2003
2004 while (((*cur >= '0') && (*cur <= '9')) ||
2005 ((*cur >= 'A') && (*cur <= 'F')) ||
2006 ((*cur >= 'a') && (*cur <= 'f'))) {
2007 i++;
2008 cur++;
2009 }
2010
2011 if (*cur != 0)
2012 goto return1;
2013 if ((i % 2) != 0)
2014 goto return1;
2015
2016 if (val != NULL) {
2017
2018 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
2019 if (v == NULL)
2020 goto error;
2021
2022 cur = xmlStrdup(value);
2023 if (cur == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002024 xmlSchemaTypeErrMemory(node, "allocating hexbin data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002025 xmlFree(v);
2026 goto return1;
2027 }
2028
2029 total = i / 2; /* number of octets */
2030
2031 base = (xmlChar *) cur;
2032 while (i-- > 0) {
2033 if (*base >= 'a')
2034 *base = *base - ('a' - 'A');
2035 base++;
2036 }
2037
2038 v->value.hex.str = (xmlChar *) cur;
2039 v->value.hex.total = total;
2040 *val = v;
2041 }
2042 goto return0;
2043 }
2044 case XML_SCHEMAS_BASE64BINARY:{
2045 /* ISSUE:
2046 *
2047 * Ignore all stray characters? (yes, currently)
2048 * Worry about long lines? (no, currently)
2049 *
2050 * rfc2045.txt:
2051 *
2052 * "The encoded output stream must be represented in lines of
2053 * no more than 76 characters each. All line breaks or other
2054 * characters not found in Table 1 must be ignored by decoding
2055 * software. In base64 data, characters other than those in
2056 * Table 1, line breaks, and other white space probably
2057 * indicate a transmission error, about which a warning
2058 * message or even a message rejection might be appropriate
2059 * under some circumstances." */
2060 const xmlChar *cur = value;
2061 xmlChar *base;
2062 int total, i = 0, pad = 0;
2063
2064 if (cur == NULL)
2065 goto return1;
2066
2067 for (; *cur; ++cur) {
2068 int decc;
2069
2070 decc = _xmlSchemaBase64Decode(*cur);
2071 if (decc < 0) ;
2072 else if (decc < 64)
2073 i++;
2074 else
2075 break;
2076 }
2077 for (; *cur; ++cur) {
2078 int decc;
2079
2080 decc = _xmlSchemaBase64Decode(*cur);
2081 if (decc < 0) ;
2082 else if (decc < 64)
2083 goto return1;
2084 if (decc == 64)
2085 pad++;
2086 }
2087
2088 /* rfc2045.txt: "Special processing is performed if fewer than
2089 * 24 bits are available at the end of the data being encoded.
2090 * A full encoding quantum is always completed at the end of a
2091 * body. When fewer than 24 input bits are available in an
2092 * input group, zero bits are added (on the right) to form an
2093 * integral number of 6-bit groups. Padding at the end of the
2094 * data is performed using the "=" character. Since all
2095 * base64 input is an integral number of octets, only the
2096 * following cases can arise: (1) the final quantum of
2097 * encoding input is an integral multiple of 24 bits; here,
2098 * the final unit of encoded output will be an integral
2099 * multiple ofindent: Standard input:701: Warning:old style
2100 * assignment ambiguity in "=*". Assuming "= *" 4 characters
2101 * with no "=" padding, (2) the final
2102 * quantum of encoding input is exactly 8 bits; here, the
2103 * final unit of encoded output will be two characters
2104 * followed by two "=" padding characters, or (3) the final
2105 * quantum of encoding input is exactly 16 bits; here, the
2106 * final unit of encoded output will be three characters
2107 * followed by one "=" padding character." */
2108
2109 total = 3 * (i / 4);
2110 if (pad == 0) {
2111 if (i % 4 != 0)
2112 goto return1;
2113 } else if (pad == 1) {
2114 int decc;
2115
2116 if (i % 4 != 3)
2117 goto return1;
2118 for (decc = _xmlSchemaBase64Decode(*cur);
2119 (decc < 0) || (decc > 63);
2120 decc = _xmlSchemaBase64Decode(*cur))
2121 --cur;
2122 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
2123 /* 00111100 -> 0x3c */
2124 if (decc & ~0x3c)
2125 goto return1;
2126 total += 2;
2127 } else if (pad == 2) {
2128 int decc;
2129
2130 if (i % 4 != 2)
2131 goto return1;
2132 for (decc = _xmlSchemaBase64Decode(*cur);
2133 (decc < 0) || (decc > 63);
2134 decc = _xmlSchemaBase64Decode(*cur))
2135 --cur;
2136 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
2137 /* 00110000 -> 0x30 */
2138 if (decc & ~0x30)
2139 goto return1;
2140 total += 1;
2141 } else
2142 goto return1;
2143
2144 if (val != NULL) {
2145 v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
2146 if (v == NULL)
2147 goto error;
2148 base =
2149 (xmlChar *) xmlMallocAtomic((i + pad + 1) *
2150 sizeof(xmlChar));
2151 if (base == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002152 xmlSchemaTypeErrMemory(node, "allocating base64 data");
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002153 xmlFree(v);
2154 goto return1;
2155 }
2156 v->value.base64.str = base;
2157 for (cur = value; *cur; ++cur)
2158 if (_xmlSchemaBase64Decode(*cur) >= 0) {
2159 *base = *cur;
2160 ++base;
2161 }
2162 *base = 0;
2163 v->value.base64.total = total;
2164 *val = v;
2165 }
2166 goto return0;
2167 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002168 case XML_SCHEMAS_INTEGER:
2169 case XML_SCHEMAS_PINTEGER:
2170 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002171 case XML_SCHEMAS_NINTEGER:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002172 case XML_SCHEMAS_NNINTEGER:{
2173 const xmlChar *cur = value;
2174 unsigned long lo, mi, hi;
2175 int sign = 0;
2176
2177 if (cur == NULL)
2178 goto return1;
2179 if (*cur == '-') {
2180 sign = 1;
2181 cur++;
2182 } else if (*cur == '+')
2183 cur++;
2184 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2185 if (ret == 0)
2186 goto return1;
2187 if (*cur != 0)
2188 goto return1;
2189 if (type->flags == XML_SCHEMAS_NPINTEGER) {
2190 if ((sign == 0) &&
2191 ((hi != 0) || (mi != 0) || (lo != 0)))
2192 goto return1;
2193 } else if (type->flags == XML_SCHEMAS_PINTEGER) {
2194 if (sign == 1)
2195 goto return1;
2196 if ((hi == 0) && (mi == 0) && (lo == 0))
2197 goto return1;
2198 } else if (type->flags == XML_SCHEMAS_NINTEGER) {
2199 if (sign == 0)
2200 goto return1;
2201 if ((hi == 0) && (mi == 0) && (lo == 0))
2202 goto return1;
2203 } else if (type->flags == XML_SCHEMAS_NNINTEGER) {
2204 if ((sign == 1) &&
2205 ((hi != 0) || (mi != 0) || (lo != 0)))
2206 goto return1;
2207 }
2208 /*
2209 * We can store a value only if no overflow occured
2210 */
2211 if ((ret > 0) && (val != NULL)) {
2212 v = xmlSchemaNewValue(type->flags);
2213 if (v != NULL) {
2214 v->value.decimal.lo = lo;
2215 v->value.decimal.mi = lo;
2216 v->value.decimal.hi = lo;
2217 v->value.decimal.sign = sign;
2218 v->value.decimal.frac = 0;
2219 v->value.decimal.total = cur - value;
2220 *val = v;
2221 }
2222 }
2223 goto return0;
2224 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002225 case XML_SCHEMAS_LONG:
2226 case XML_SCHEMAS_BYTE:
2227 case XML_SCHEMAS_SHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002228 case XML_SCHEMAS_INT:{
2229 const xmlChar *cur = value;
2230 unsigned long lo, mi, hi;
2231 int total = 0;
2232 int sign = 0;
2233
2234 if (cur == NULL)
2235 goto return1;
2236 if (*cur == '-') {
2237 sign = 1;
2238 cur++;
2239 } else if (*cur == '+')
2240 cur++;
2241 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2242 if (ret <= 0)
2243 goto return1;
2244 if (*cur != 0)
2245 goto return1;
2246 if (type->flags == XML_SCHEMAS_LONG) {
2247 if (hi >= 922) {
2248 if (hi > 922)
2249 goto return1;
2250 if (mi >= 33720368) {
2251 if (mi > 33720368)
2252 goto return1;
2253 if ((sign == 0) && (lo > 54775807))
2254 goto return1;
2255 if ((sign == 1) && (lo > 54775808))
2256 goto return1;
2257 }
2258 }
2259 } else if (type->flags == XML_SCHEMAS_INT) {
2260 if (hi != 0)
2261 goto return1;
2262 if (mi >= 21) {
2263 if (mi > 21)
2264 goto return1;
2265 if ((sign == 0) && (lo > 47483647))
2266 goto return1;
2267 if ((sign == 1) && (lo > 47483648))
2268 goto return1;
2269 }
2270 } else if (type->flags == XML_SCHEMAS_SHORT) {
2271 if ((mi != 0) || (hi != 0))
2272 goto return1;
2273 if ((sign == 1) && (lo > 32768))
2274 goto return1;
2275 if ((sign == 0) && (lo > 32767))
2276 goto return1;
2277 } else if (type->flags == XML_SCHEMAS_BYTE) {
2278 if ((mi != 0) || (hi != 0))
2279 goto return1;
2280 if ((sign == 1) && (lo > 128))
2281 goto return1;
2282 if ((sign == 0) && (lo > 127))
2283 goto return1;
2284 }
2285 if (val != NULL) {
2286 v = xmlSchemaNewValue(type->flags);
2287 if (v != NULL) {
2288 v->value.decimal.lo = lo;
2289 v->value.decimal.mi = lo;
2290 v->value.decimal.hi = lo;
2291 v->value.decimal.sign = sign;
2292 v->value.decimal.frac = 0;
2293 v->value.decimal.total = total;
2294 *val = v;
2295 }
2296 }
2297 goto return0;
2298 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002299 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002300 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002301 case XML_SCHEMAS_USHORT:
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002302 case XML_SCHEMAS_UBYTE:{
2303 const xmlChar *cur = value;
2304 unsigned long lo, mi, hi;
2305 int total = 0;
2306
2307 if (cur == NULL)
2308 goto return1;
2309 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2310 if (ret <= 0)
2311 goto return1;
2312 if (*cur != 0)
2313 goto return1;
2314 if (type->flags == XML_SCHEMAS_ULONG) {
2315 if (hi >= 1844) {
2316 if (hi > 1844)
2317 goto return1;
2318 if (mi >= 67440737) {
2319 if (mi > 67440737)
2320 goto return1;
2321 if (lo > 9551615)
2322 goto return1;
2323 }
2324 }
2325 } else if (type->flags == XML_SCHEMAS_UINT) {
2326 if (hi != 0)
2327 goto return1;
2328 if (mi >= 42) {
2329 if (mi > 42)
2330 goto return1;
2331 if (lo > 94967295)
2332 goto return1;
2333 }
2334 } else if (type->flags == XML_SCHEMAS_USHORT) {
2335 if ((mi != 0) || (hi != 0))
2336 goto return1;
2337 if (lo > 65535)
2338 goto return1;
2339 } else if (type->flags == XML_SCHEMAS_UBYTE) {
2340 if ((mi != 0) || (hi != 0))
2341 goto return1;
2342 if (lo > 255)
2343 goto return1;
2344 }
2345 if (val != NULL) {
2346 v = xmlSchemaNewValue(type->flags);
2347 if (v != NULL) {
2348 v->value.decimal.lo = lo;
2349 v->value.decimal.mi = mi;
2350 v->value.decimal.hi = hi;
2351 v->value.decimal.sign = 0;
2352 v->value.decimal.frac = 0;
2353 v->value.decimal.total = total;
2354 *val = v;
2355 }
2356 }
2357 goto return0;
2358 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002359 }
2360
Daniel Veillard1ac24d32003-08-27 14:15:15 +00002361 done:
2362 if (norm != NULL)
2363 xmlFree(norm);
2364 return (ret);
2365 return3:
2366 if (norm != NULL)
2367 xmlFree(norm);
2368 return (3);
2369 return1:
2370 if (norm != NULL)
2371 xmlFree(norm);
2372 return (1);
2373 return0:
2374 if (norm != NULL)
2375 xmlFree(norm);
2376 return (0);
2377 error:
2378 if (norm != NULL)
2379 xmlFree(norm);
2380 return (-1);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002381}
2382
2383/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002384 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00002385 * @type: the predefined type
2386 * @value: the value to check
2387 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002388 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00002389 *
2390 * Check that a value conforms to the lexical space of the predefined type.
2391 * if true a value is computed and returned in @val.
2392 *
2393 * Returns 0 if this validates, a positive error code number otherwise
2394 * and -1 in case of internal or API error.
2395 */
2396int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002397xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
2398 xmlSchemaValPtr *val, xmlNodePtr node) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002399 return(xmlSchemaValAtomicType(type, value, val, node, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00002400}
2401
2402/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002403 * xmlSchemaValidatePredefinedType:
2404 * @type: the predefined type
2405 * @value: the value to check
2406 * @val: the return computed value
2407 *
2408 * Check that a value conforms to the lexical space of the predefined type.
2409 * if true a value is computed and returned in @val.
2410 *
2411 * Returns 0 if this validates, a positive error code number otherwise
2412 * and -1 in case of internal or API error.
2413 */
2414int
2415xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
2416 xmlSchemaValPtr *val) {
2417 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
2418}
2419
2420/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002421 * xmlSchemaCompareDecimals:
2422 * @x: a first decimal value
2423 * @y: a second decimal value
2424 *
2425 * Compare 2 decimals
2426 *
2427 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
2428 */
2429static int
2430xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
2431{
2432 xmlSchemaValPtr swp;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002433 int order = 1, p;
Daniel Veillard4255d502002-04-16 15:50:10 +00002434 unsigned long tmp;
2435
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002436 if ((x->value.decimal.sign) &&
2437 ((x->value.decimal.lo != 0) ||
2438 (x->value.decimal.mi != 0) ||
2439 (x->value.decimal.hi != 0))) {
2440 if ((y->value.decimal.sign) &&
2441 ((y->value.decimal.lo != 0) ||
2442 (y->value.decimal.mi != 0) ||
2443 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00002444 order = -1;
2445 else
2446 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002447 } else if ((y->value.decimal.sign) &&
2448 ((y->value.decimal.lo != 0) ||
2449 (y->value.decimal.mi != 0) ||
2450 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002451 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002452 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002453 if (x->value.decimal.frac == y->value.decimal.frac) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002454 if (x->value.decimal.hi < y->value.decimal.hi)
2455 return (-order);
2456 if (x->value.decimal.hi < y->value.decimal.hi)
2457 return (order);
2458 if (x->value.decimal.mi < y->value.decimal.mi)
2459 return (-order);
2460 if (x->value.decimal.mi < y->value.decimal.mi)
2461 return (order);
2462 if (x->value.decimal.lo < y->value.decimal.lo)
Daniel Veillard80b19092003-03-28 13:29:53 +00002463 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002464 if (x->value.decimal.lo > y->value.decimal.lo)
Daniel Veillard80b19092003-03-28 13:29:53 +00002465 return(order);
2466 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002467 }
2468 if (y->value.decimal.frac > x->value.decimal.frac) {
2469 swp = y;
2470 y = x;
2471 x = swp;
2472 order = -order;
2473 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002474 p = powten[x->value.decimal.frac - y->value.decimal.frac];
2475 tmp = x->value.decimal.lo / p;
2476 if (tmp > y->value.decimal.lo)
Daniel Veillard4255d502002-04-16 15:50:10 +00002477 return (order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002478 if (tmp < y->value.decimal.lo)
Daniel Veillard4255d502002-04-16 15:50:10 +00002479 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002480 tmp = y->value.decimal.lo * p;
2481 if (x->value.decimal.lo < tmp)
Daniel Veillard4255d502002-04-16 15:50:10 +00002482 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002483 if (x->value.decimal.lo == tmp)
Daniel Veillard4255d502002-04-16 15:50:10 +00002484 return (0);
2485 return (order);
2486}
2487
2488/**
Daniel Veillard070803b2002-05-03 07:29:38 +00002489 * xmlSchemaCompareDurations:
2490 * @x: a first duration value
2491 * @y: a second duration value
2492 *
2493 * Compare 2 durations
2494 *
2495 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2496 * case of error
2497 */
2498static int
2499xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
2500{
2501 long carry, mon, day;
2502 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00002503 int invert = 1;
2504 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00002505 static const long dayRange [2][12] = {
2506 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
2507 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
2508
2509 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00002510 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00002511
2512 /* months */
2513 mon = x->value.dur.mon - y->value.dur.mon;
2514
2515 /* seconds */
2516 sec = x->value.dur.sec - y->value.dur.sec;
2517 carry = (long)sec / SECS_PER_DAY;
2518 sec -= (double)(carry * SECS_PER_DAY);
2519
2520 /* days */
2521 day = x->value.dur.day - y->value.dur.day + carry;
2522
2523 /* easy test */
2524 if (mon == 0) {
2525 if (day == 0)
2526 if (sec == 0.0)
2527 return 0;
2528 else if (sec < 0.0)
2529 return -1;
2530 else
2531 return 1;
2532 else if (day < 0)
2533 return -1;
2534 else
2535 return 1;
2536 }
2537
2538 if (mon > 0) {
2539 if ((day >= 0) && (sec >= 0.0))
2540 return 1;
2541 else {
2542 xmon = mon;
2543 xday = -day;
2544 }
2545 } else if ((day <= 0) && (sec <= 0.0)) {
2546 return -1;
2547 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00002548 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00002549 xmon = -mon;
2550 xday = day;
2551 }
2552
2553 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00002554 if (myear == 0) {
2555 minday = 0;
2556 maxday = 0;
2557 } else {
2558 maxday = 366 * ((myear + 3) / 4) +
2559 365 * ((myear - 1) % 4);
2560 minday = maxday - 1;
2561 }
2562
Daniel Veillard070803b2002-05-03 07:29:38 +00002563 xmon = xmon % 12;
2564 minday += dayRange[0][xmon];
2565 maxday += dayRange[1][xmon];
2566
Daniel Veillard80b19092003-03-28 13:29:53 +00002567 if ((maxday == minday) && (maxday == xday))
2568 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00002569 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00002570 return(-invert);
2571 if (minday > xday)
2572 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00002573
2574 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00002575 return 2;
2576}
2577
2578/*
2579 * macros for adding date/times and durations
2580 */
2581#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
2582#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
2583#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
2584#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
2585
2586/**
2587 * _xmlSchemaDateAdd:
2588 * @dt: an #xmlSchemaValPtr
2589 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
2590 *
2591 * Compute a new date/time from @dt and @dur. This function assumes @dt
2592 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
2593 * or #XML_SCHEMAS_GYEAR.
2594 *
2595 * Returns date/time pointer or NULL.
2596 */
2597static xmlSchemaValPtr
2598_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
2599{
2600 xmlSchemaValPtr ret;
2601 long carry, tempdays, temp;
2602 xmlSchemaValDatePtr r, d;
2603 xmlSchemaValDurationPtr u;
2604
2605 if ((dt == NULL) || (dur == NULL))
2606 return NULL;
2607
2608 ret = xmlSchemaNewValue(dt->type);
2609 if (ret == NULL)
2610 return NULL;
2611
2612 r = &(ret->value.date);
2613 d = &(dt->value.date);
2614 u = &(dur->value.dur);
2615
2616 /* normalization */
2617 if (d->mon == 0)
2618 d->mon = 1;
2619
2620 /* normalize for time zone offset */
2621 u->sec -= (d->tzo * 60);
2622 d->tzo = 0;
2623
2624 /* normalization */
2625 if (d->day == 0)
2626 d->day = 1;
2627
2628 /* month */
2629 carry = d->mon + u->mon;
2630 r->mon = MODULO_RANGE(carry, 1, 13);
2631 carry = FQUOTIENT_RANGE(carry, 1, 13);
2632
2633 /* year (may be modified later) */
2634 r->year = d->year + carry;
2635 if (r->year == 0) {
2636 if (d->year > 0)
2637 r->year--;
2638 else
2639 r->year++;
2640 }
2641
2642 /* time zone */
2643 r->tzo = d->tzo;
2644 r->tz_flag = d->tz_flag;
2645
2646 /* seconds */
2647 r->sec = d->sec + u->sec;
2648 carry = FQUOTIENT((long)r->sec, 60);
2649 if (r->sec != 0.0) {
2650 r->sec = MODULO(r->sec, 60.0);
2651 }
2652
2653 /* minute */
2654 carry += d->min;
2655 r->min = MODULO(carry, 60);
2656 carry = FQUOTIENT(carry, 60);
2657
2658 /* hours */
2659 carry += d->hour;
2660 r->hour = MODULO(carry, 24);
2661 carry = FQUOTIENT(carry, 24);
2662
2663 /*
2664 * days
2665 * Note we use tempdays because the temporary values may need more
2666 * than 5 bits
2667 */
2668 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
2669 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
2670 tempdays = MAX_DAYINMONTH(r->year, r->mon);
2671 else if (d->day < 1)
2672 tempdays = 1;
2673 else
2674 tempdays = d->day;
2675
2676 tempdays += u->day + carry;
2677
2678 while (1) {
2679 if (tempdays < 1) {
2680 long tmon = MODULO_RANGE(r->mon-1, 1, 13);
2681 long tyr = r->year + FQUOTIENT_RANGE(r->mon-1, 1, 13);
2682 if (tyr == 0)
2683 tyr--;
2684 tempdays += MAX_DAYINMONTH(tyr, tmon);
2685 carry = -1;
2686 } else if (tempdays > MAX_DAYINMONTH(r->year, r->mon)) {
2687 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
2688 carry = 1;
2689 } else
2690 break;
2691
2692 temp = r->mon + carry;
2693 r->mon = MODULO_RANGE(temp, 1, 13);
2694 r->year = r->year + FQUOTIENT_RANGE(temp, 1, 13);
2695 if (r->year == 0) {
2696 if (temp < 1)
2697 r->year--;
2698 else
2699 r->year++;
2700 }
2701 }
2702
2703 r->day = tempdays;
2704
2705 /*
2706 * adjust the date/time type to the date values
2707 */
2708 if (ret->type != XML_SCHEMAS_DATETIME) {
2709 if ((r->hour) || (r->min) || (r->sec))
2710 ret->type = XML_SCHEMAS_DATETIME;
2711 else if (ret->type != XML_SCHEMAS_DATE) {
2712 if ((r->mon != 1) && (r->day != 1))
2713 ret->type = XML_SCHEMAS_DATE;
2714 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
2715 ret->type = XML_SCHEMAS_GYEARMONTH;
2716 }
2717 }
2718
2719 return ret;
2720}
2721
2722/**
2723 * xmlSchemaDupVal:
2724 * @v: value to duplicate
2725 *
2726 * returns a duplicated value.
2727 */
2728static xmlSchemaValPtr
2729xmlSchemaDupVal (xmlSchemaValPtr v)
2730{
2731 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
2732 if (ret == NULL)
2733 return ret;
2734
2735 memcpy(ret, v, sizeof(xmlSchemaVal));
2736 return ret;
2737}
2738
2739/**
2740 * xmlSchemaDateNormalize:
2741 * @dt: an #xmlSchemaValPtr
2742 *
2743 * Normalize @dt to GMT time.
2744 *
2745 */
2746static xmlSchemaValPtr
2747xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
2748{
2749 xmlSchemaValPtr dur, ret;
2750
2751 if (dt == NULL)
2752 return NULL;
2753
2754 if (((dt->type != XML_SCHEMAS_TIME) &&
2755 (dt->type != XML_SCHEMAS_DATETIME)) || (dt->value.date.tzo == 0))
2756 return xmlSchemaDupVal(dt);
2757
2758 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
2759 if (dur == NULL)
2760 return NULL;
2761
2762 dur->value.date.sec -= offset;
2763
2764 ret = _xmlSchemaDateAdd(dt, dur);
2765 if (ret == NULL)
2766 return NULL;
2767
2768 xmlSchemaFreeValue(dur);
2769
2770 /* ret->value.date.tzo = 0; */
2771 return ret;
2772}
2773
2774/**
2775 * _xmlSchemaDateCastYMToDays:
2776 * @dt: an #xmlSchemaValPtr
2777 *
2778 * Convert mon and year of @dt to total number of days. Take the
2779 * number of years since (or before) 1 AD and add the number of leap
2780 * years. This is a function because negative
2781 * years must be handled a little differently and there is no zero year.
2782 *
2783 * Returns number of days.
2784 */
2785static long
2786_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
2787{
2788 long ret;
2789
2790 if (dt->value.date.year < 0)
2791 ret = (dt->value.date.year * 365) +
2792 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
2793 ((dt->value.date.year+1)/400)) +
2794 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
2795 else
2796 ret = ((dt->value.date.year-1) * 365) +
2797 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
2798 ((dt->value.date.year-1)/400)) +
2799 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
2800
2801 return ret;
2802}
2803
2804/**
2805 * TIME_TO_NUMBER:
2806 * @dt: an #xmlSchemaValPtr
2807 *
2808 * Calculates the number of seconds in the time portion of @dt.
2809 *
2810 * Returns seconds.
2811 */
2812#define TIME_TO_NUMBER(dt) \
2813 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00002814 (dt->value.date.min * SECS_PER_MIN) + \
2815 (dt->value.date.tzo * SECS_PER_MIN)) + \
2816 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00002817
2818/**
2819 * xmlSchemaCompareDates:
2820 * @x: a first date/time value
2821 * @y: a second date/time value
2822 *
2823 * Compare 2 date/times
2824 *
2825 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2826 * case of error
2827 */
2828static int
2829xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
2830{
2831 unsigned char xmask, ymask, xor_mask, and_mask;
2832 xmlSchemaValPtr p1, p2, q1, q2;
2833 long p1d, p2d, q1d, q2d;
2834
2835 if ((x == NULL) || (y == NULL))
2836 return -2;
2837
2838 if (x->value.date.tz_flag) {
2839
2840 if (!y->value.date.tz_flag) {
2841 p1 = xmlSchemaDateNormalize(x, 0);
2842 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2843 /* normalize y + 14:00 */
2844 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
2845
2846 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002847 if (p1d < q1d) {
2848 xmlSchemaFreeValue(p1);
2849 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002850 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002851 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002852 double sec;
2853
2854 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002855 if (sec < 0.0) {
2856 xmlSchemaFreeValue(p1);
2857 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002858 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002859 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002860 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00002861 /* normalize y - 14:00 */
2862 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
2863 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
2864 if (p1d > q2d)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002865 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002866 else if (p1d == q2d) {
2867 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
2868 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002869 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002870 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002871 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00002872 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002873 xmlSchemaFreeValue(p1);
2874 xmlSchemaFreeValue(q1);
2875 xmlSchemaFreeValue(q2);
2876 if (ret != 0)
2877 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00002878 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00002879 } else {
2880 xmlSchemaFreeValue(p1);
2881 xmlSchemaFreeValue(q1);
2882 }
Daniel Veillard5a872412002-05-22 06:40:27 +00002883 }
2884 } else if (y->value.date.tz_flag) {
2885 q1 = xmlSchemaDateNormalize(y, 0);
2886 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
2887
2888 /* normalize x - 14:00 */
2889 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
2890 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2891
Daniel Veillardfdc91562002-07-01 21:52:03 +00002892 if (p1d < q1d) {
2893 xmlSchemaFreeValue(p1);
2894 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002895 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002896 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002897 double sec;
2898
2899 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002900 if (sec < 0.0) {
2901 xmlSchemaFreeValue(p1);
2902 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002903 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002904 } else {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002905 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00002906 /* normalize x + 14:00 */
2907 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
2908 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
2909
Daniel Veillard6560a422003-03-27 21:25:38 +00002910 if (p2d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002911 ret = 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00002912 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002913 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
2914 if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002915 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002916 else
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002917 ret = 2; /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00002918 }
Daniel Veillard6560a422003-03-27 21:25:38 +00002919 xmlSchemaFreeValue(p1);
2920 xmlSchemaFreeValue(q1);
2921 xmlSchemaFreeValue(p2);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002922 if (ret != 0)
2923 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00002924 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00002925 } else {
2926 xmlSchemaFreeValue(p1);
2927 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002928 }
2929 }
2930
2931 /*
2932 * if the same type then calculate the difference
2933 */
2934 if (x->type == y->type) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002935 int ret = 0;
Daniel Veillard5a872412002-05-22 06:40:27 +00002936 q1 = xmlSchemaDateNormalize(y, 0);
2937 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
2938
2939 p1 = xmlSchemaDateNormalize(x, 0);
2940 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2941
Daniel Veillardfdc91562002-07-01 21:52:03 +00002942 if (p1d < q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002943 ret = -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002944 } else if (p1d > q1d) {
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002945 ret = 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002946 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00002947 double sec;
2948
2949 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
2950 if (sec < 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002951 ret = -1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002952 else if (sec > 0.0)
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002953 ret = 1;
Daniel Veillard5a872412002-05-22 06:40:27 +00002954
2955 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002956 xmlSchemaFreeValue(p1);
2957 xmlSchemaFreeValue(q1);
2958 return(ret);
Daniel Veillard5a872412002-05-22 06:40:27 +00002959 }
2960
2961 switch (x->type) {
2962 case XML_SCHEMAS_DATETIME:
2963 xmask = 0xf;
2964 break;
2965 case XML_SCHEMAS_DATE:
2966 xmask = 0x7;
2967 break;
2968 case XML_SCHEMAS_GYEAR:
2969 xmask = 0x1;
2970 break;
2971 case XML_SCHEMAS_GMONTH:
2972 xmask = 0x2;
2973 break;
2974 case XML_SCHEMAS_GDAY:
2975 xmask = 0x3;
2976 break;
2977 case XML_SCHEMAS_GYEARMONTH:
2978 xmask = 0x3;
2979 break;
2980 case XML_SCHEMAS_GMONTHDAY:
2981 xmask = 0x6;
2982 break;
2983 case XML_SCHEMAS_TIME:
2984 xmask = 0x8;
2985 break;
2986 default:
2987 xmask = 0;
2988 break;
2989 }
2990
2991 switch (y->type) {
2992 case XML_SCHEMAS_DATETIME:
2993 ymask = 0xf;
2994 break;
2995 case XML_SCHEMAS_DATE:
2996 ymask = 0x7;
2997 break;
2998 case XML_SCHEMAS_GYEAR:
2999 ymask = 0x1;
3000 break;
3001 case XML_SCHEMAS_GMONTH:
3002 ymask = 0x2;
3003 break;
3004 case XML_SCHEMAS_GDAY:
3005 ymask = 0x3;
3006 break;
3007 case XML_SCHEMAS_GYEARMONTH:
3008 ymask = 0x3;
3009 break;
3010 case XML_SCHEMAS_GMONTHDAY:
3011 ymask = 0x6;
3012 break;
3013 case XML_SCHEMAS_TIME:
3014 ymask = 0x8;
3015 break;
3016 default:
3017 ymask = 0;
3018 break;
3019 }
3020
3021 xor_mask = xmask ^ ymask; /* mark type differences */
3022 and_mask = xmask & ymask; /* mark field specification */
3023
3024 /* year */
3025 if (xor_mask & 1)
3026 return 2; /* indeterminate */
3027 else if (and_mask & 1) {
3028 if (x->value.date.year < y->value.date.year)
3029 return -1;
3030 else if (x->value.date.year > y->value.date.year)
3031 return 1;
3032 }
3033
3034 /* month */
3035 if (xor_mask & 2)
3036 return 2; /* indeterminate */
3037 else if (and_mask & 2) {
3038 if (x->value.date.mon < y->value.date.mon)
3039 return -1;
3040 else if (x->value.date.mon > y->value.date.mon)
3041 return 1;
3042 }
3043
3044 /* day */
3045 if (xor_mask & 4)
3046 return 2; /* indeterminate */
3047 else if (and_mask & 4) {
3048 if (x->value.date.day < y->value.date.day)
3049 return -1;
3050 else if (x->value.date.day > y->value.date.day)
3051 return 1;
3052 }
3053
3054 /* time */
3055 if (xor_mask & 8)
3056 return 2; /* indeterminate */
3057 else if (and_mask & 8) {
3058 if (x->value.date.hour < y->value.date.hour)
3059 return -1;
3060 else if (x->value.date.hour > y->value.date.hour)
3061 return 1;
3062 else if (x->value.date.min < y->value.date.min)
3063 return -1;
3064 else if (x->value.date.min > y->value.date.min)
3065 return 1;
3066 else if (x->value.date.sec < y->value.date.sec)
3067 return -1;
3068 else if (x->value.date.sec > y->value.date.sec)
3069 return 1;
3070 }
3071
Daniel Veillard070803b2002-05-03 07:29:38 +00003072 return 0;
3073}
3074
3075/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00003076 * xmlSchemaCompareNormStrings:
3077 * @x: a first string value
3078 * @y: a second string value
3079 *
3080 * Compare 2 string for their normalized values.
3081 *
3082 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
3083 * case of error
3084 */
3085static int
3086xmlSchemaCompareNormStrings(xmlSchemaValPtr x, xmlSchemaValPtr y) {
3087 const xmlChar *utf1;
3088 const xmlChar *utf2;
3089 int tmp;
3090
3091 if ((x == NULL) || (y == NULL))
3092 return(-2);
3093 utf1 = x->value.str;
3094 utf2 = y->value.str;
3095
William M. Brack76e95df2003-10-18 16:20:14 +00003096 while (IS_BLANK_CH(*utf1)) utf1++;
3097 while (IS_BLANK_CH(*utf2)) utf2++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003098 while ((*utf1 != 0) && (*utf2 != 0)) {
William M. Brack76e95df2003-10-18 16:20:14 +00003099 if (IS_BLANK_CH(*utf1)) {
3100 if (!IS_BLANK_CH(*utf2)) {
Daniel Veillardc4c21552003-03-29 10:53:38 +00003101 tmp = *utf1 - *utf2;
3102 return(tmp);
3103 }
William M. Brack76e95df2003-10-18 16:20:14 +00003104 while (IS_BLANK_CH(*utf1)) utf1++;
3105 while (IS_BLANK_CH(*utf2)) utf2++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003106 } else {
3107 tmp = *utf1++ - *utf2++;
3108 if (tmp < 0)
3109 return(-1);
3110 if (tmp > 0)
3111 return(1);
3112 }
3113 }
3114 if (*utf1 != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003115 while (IS_BLANK_CH(*utf1)) utf1++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003116 if (*utf1 != 0)
3117 return(1);
3118 }
3119 if (*utf2 != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003120 while (IS_BLANK_CH(*utf2)) utf2++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003121 if (*utf2 != 0)
3122 return(-1);
3123 }
3124 return(0);
3125}
3126
3127/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003128 * xmlSchemaCompareFloats:
3129 * @x: a first float or double value
3130 * @y: a second float or double value
3131 *
3132 * Compare 2 values
3133 *
3134 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3135 * case of error
3136 */
3137static int
3138xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
3139 double d1, d2;
3140
3141 if ((x == NULL) || (y == NULL))
3142 return(-2);
3143
3144 /*
3145 * Cast everything to doubles.
3146 */
3147 if (x->type == XML_SCHEMAS_DOUBLE)
3148 d1 = x->value.d;
3149 else if (x->type == XML_SCHEMAS_FLOAT)
3150 d1 = x->value.f;
3151 else
3152 return(-2);
3153
3154 if (y->type == XML_SCHEMAS_DOUBLE)
3155 d2 = y->value.d;
3156 else if (y->type == XML_SCHEMAS_FLOAT)
3157 d2 = y->value.f;
3158 else
3159 return(-2);
3160
3161 /*
3162 * Check for special cases.
3163 */
3164 if (xmlXPathIsNaN(d1)) {
3165 if (xmlXPathIsNaN(d2))
3166 return(0);
3167 return(1);
3168 }
3169 if (xmlXPathIsNaN(d2))
3170 return(-1);
3171 if (d1 == xmlXPathPINF) {
3172 if (d2 == xmlXPathPINF)
3173 return(0);
3174 return(1);
3175 }
3176 if (d2 == xmlXPathPINF)
3177 return(-1);
3178 if (d1 == xmlXPathNINF) {
3179 if (d2 == xmlXPathNINF)
3180 return(0);
3181 return(-1);
3182 }
3183 if (d2 == xmlXPathNINF)
3184 return(1);
3185
3186 /*
3187 * basic tests, the last one we should have equality, but
3188 * portability is more important than speed and handling
3189 * NaN or Inf in a portable way is always a challenge, so ...
3190 */
3191 if (d1 < d2)
3192 return(-1);
3193 if (d1 > d2)
3194 return(1);
3195 if (d1 == d2)
3196 return(0);
3197 return(2);
3198}
3199
3200/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003201 * xmlSchemaCompareValues:
3202 * @x: a first value
3203 * @y: a second value
3204 *
3205 * Compare 2 values
3206 *
Daniel Veillard5a872412002-05-22 06:40:27 +00003207 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3208 * case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003209 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003210int
Daniel Veillard4255d502002-04-16 15:50:10 +00003211xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
3212 if ((x == NULL) || (y == NULL))
3213 return(-2);
3214
3215 switch (x->type) {
Daniel Veillard80b19092003-03-28 13:29:53 +00003216 case XML_SCHEMAS_UNKNOWN:
3217 return(-2);
3218 case XML_SCHEMAS_INTEGER:
3219 case XML_SCHEMAS_NPINTEGER:
3220 case XML_SCHEMAS_NINTEGER:
3221 case XML_SCHEMAS_NNINTEGER:
3222 case XML_SCHEMAS_PINTEGER:
3223 case XML_SCHEMAS_INT:
3224 case XML_SCHEMAS_UINT:
3225 case XML_SCHEMAS_LONG:
3226 case XML_SCHEMAS_ULONG:
3227 case XML_SCHEMAS_SHORT:
3228 case XML_SCHEMAS_USHORT:
3229 case XML_SCHEMAS_BYTE:
3230 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00003231 case XML_SCHEMAS_DECIMAL:
Daniel Veillard80b19092003-03-28 13:29:53 +00003232 if (y->type == x->type)
3233 return(xmlSchemaCompareDecimals(x, y));
3234 if ((y->type == XML_SCHEMAS_DECIMAL) ||
3235 (y->type == XML_SCHEMAS_INTEGER) ||
3236 (y->type == XML_SCHEMAS_NPINTEGER) ||
3237 (y->type == XML_SCHEMAS_NINTEGER) ||
3238 (y->type == XML_SCHEMAS_NNINTEGER) ||
3239 (y->type == XML_SCHEMAS_PINTEGER) ||
3240 (y->type == XML_SCHEMAS_INT) ||
3241 (y->type == XML_SCHEMAS_UINT) ||
3242 (y->type == XML_SCHEMAS_LONG) ||
3243 (y->type == XML_SCHEMAS_ULONG) ||
3244 (y->type == XML_SCHEMAS_SHORT) ||
3245 (y->type == XML_SCHEMAS_USHORT) ||
3246 (y->type == XML_SCHEMAS_BYTE) ||
3247 (y->type == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00003248 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003249 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00003250 case XML_SCHEMAS_DURATION:
3251 if (y->type == XML_SCHEMAS_DURATION)
3252 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003253 return(-2);
3254 case XML_SCHEMAS_TIME:
3255 case XML_SCHEMAS_GDAY:
3256 case XML_SCHEMAS_GMONTH:
3257 case XML_SCHEMAS_GMONTHDAY:
3258 case XML_SCHEMAS_GYEAR:
3259 case XML_SCHEMAS_GYEARMONTH:
3260 case XML_SCHEMAS_DATE:
3261 case XML_SCHEMAS_DATETIME:
3262 if ((y->type == XML_SCHEMAS_DATETIME) ||
3263 (y->type == XML_SCHEMAS_TIME) ||
3264 (y->type == XML_SCHEMAS_GDAY) ||
3265 (y->type == XML_SCHEMAS_GMONTH) ||
3266 (y->type == XML_SCHEMAS_GMONTHDAY) ||
3267 (y->type == XML_SCHEMAS_GYEAR) ||
3268 (y->type == XML_SCHEMAS_DATE) ||
3269 (y->type == XML_SCHEMAS_GYEARMONTH))
3270 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003271 return (-2);
Daniel Veillard80b19092003-03-28 13:29:53 +00003272 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00003273 case XML_SCHEMAS_TOKEN:
3274 case XML_SCHEMAS_LANGUAGE:
3275 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00003276 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00003277 case XML_SCHEMAS_NCNAME:
3278 case XML_SCHEMAS_ID:
3279 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00003280 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00003281 case XML_SCHEMAS_NOTATION:
3282 case XML_SCHEMAS_ANYURI:
Daniel Veillardc4c21552003-03-29 10:53:38 +00003283 if ((y->type == XML_SCHEMAS_NORMSTRING) ||
3284 (y->type == XML_SCHEMAS_TOKEN) ||
3285 (y->type == XML_SCHEMAS_LANGUAGE) ||
3286 (y->type == XML_SCHEMAS_NMTOKEN) ||
3287 (y->type == XML_SCHEMAS_NAME) ||
3288 (y->type == XML_SCHEMAS_QNAME) ||
3289 (y->type == XML_SCHEMAS_NCNAME) ||
3290 (y->type == XML_SCHEMAS_ID) ||
3291 (y->type == XML_SCHEMAS_IDREF) ||
3292 (y->type == XML_SCHEMAS_ENTITY) ||
3293 (y->type == XML_SCHEMAS_NOTATION) ||
3294 (y->type == XML_SCHEMAS_ANYURI))
3295 return (xmlSchemaCompareNormStrings(x, y));
3296 return (-2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003297 case XML_SCHEMAS_QNAME:
3298 if (y->type == XML_SCHEMAS_QNAME) {
3299 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
3300 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
3301 return(0);
3302 return(2);
3303 }
3304 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003305 case XML_SCHEMAS_FLOAT:
3306 case XML_SCHEMAS_DOUBLE:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003307 if ((y->type == XML_SCHEMAS_FLOAT) ||
3308 (y->type == XML_SCHEMAS_DOUBLE))
3309 return (xmlSchemaCompareFloats(x, y));
3310 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003311 case XML_SCHEMAS_BOOLEAN:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003312 if (y->type == XML_SCHEMAS_BOOLEAN) {
3313 if (x->value.b == y->value.b)
3314 return(0);
3315 if (x->value.b == 0)
3316 return(-1);
3317 return(1);
3318 }
3319 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00003320 case XML_SCHEMAS_HEXBINARY:
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003321 if (y->type == XML_SCHEMAS_HEXBINARY) {
3322 if (x->value.hex.total == y->value.hex.total) {
3323 int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
3324 if (ret > 0)
3325 return(1);
3326 else if (ret == 0)
3327 return(0);
3328 }
3329 else if (x->value.hex.total > y->value.hex.total)
3330 return(1);
3331
3332 return(-1);
3333 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00003334 return (-2);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003335 case XML_SCHEMAS_BASE64BINARY:
3336 if (y->type == XML_SCHEMAS_BASE64BINARY) {
3337 if (x->value.base64.total == y->value.base64.total) {
3338 int ret = xmlStrcmp(x->value.base64.str,
3339 y->value.base64.str);
3340 if (ret > 0)
3341 return(1);
3342 else if (ret == 0)
3343 return(0);
3344 }
3345 else if (x->value.base64.total > y->value.base64.total)
3346 return(1);
3347 else
3348 return(-1);
3349 }
3350 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003351 case XML_SCHEMAS_STRING:
3352 case XML_SCHEMAS_IDREFS:
3353 case XML_SCHEMAS_ENTITIES:
3354 case XML_SCHEMAS_NMTOKENS:
3355 TODO
3356 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00003357 }
Daniel Veillard5a872412002-05-22 06:40:27 +00003358 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00003359}
3360
3361/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00003362 * xmlSchemaNormLen:
3363 * @value: a string
3364 *
3365 * Computes the UTF8 length of the normalized value of the string
3366 *
3367 * Returns the length or -1 in case of error.
3368 */
3369static int
3370xmlSchemaNormLen(const xmlChar *value) {
3371 const xmlChar *utf;
3372 int ret = 0;
3373
3374 if (value == NULL)
3375 return(-1);
3376 utf = value;
William M. Brack76e95df2003-10-18 16:20:14 +00003377 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003378 while (*utf != 0) {
3379 if (utf[0] & 0x80) {
3380 if ((utf[1] & 0xc0) != 0x80)
3381 return(-1);
3382 if ((utf[0] & 0xe0) == 0xe0) {
3383 if ((utf[2] & 0xc0) != 0x80)
3384 return(-1);
3385 if ((utf[0] & 0xf0) == 0xf0) {
3386 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
3387 return(-1);
3388 utf += 4;
3389 } else {
3390 utf += 3;
3391 }
3392 } else {
3393 utf += 2;
3394 }
William M. Brack76e95df2003-10-18 16:20:14 +00003395 } else if (IS_BLANK_CH(*utf)) {
3396 while (IS_BLANK_CH(*utf)) utf++;
Daniel Veillardc4c21552003-03-29 10:53:38 +00003397 if (*utf == 0)
3398 break;
3399 } else {
3400 utf++;
3401 }
3402 ret++;
3403 }
3404 return(ret);
3405}
3406
3407/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003408 * xmlSchemaValidateFacet:
Daniel Veillard01c13b52002-12-10 15:19:08 +00003409 * @base: the base type
Daniel Veillard4255d502002-04-16 15:50:10 +00003410 * @facet: the facet to check
3411 * @value: the lexical repr of the value to validate
3412 * @val: the precomputed value
3413 *
3414 * Check a value against a facet condition
3415 *
3416 * Returns 0 if the element is schemas valid, a positive error code
3417 * number otherwise and -1 in case of internal or API error.
3418 */
3419int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00003420xmlSchemaValidateFacet(xmlSchemaTypePtr base ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +00003421 xmlSchemaFacetPtr facet,
Daniel Veillard4255d502002-04-16 15:50:10 +00003422 const xmlChar *value, xmlSchemaValPtr val)
3423{
3424 int ret;
3425
3426 switch (facet->type) {
3427 case XML_SCHEMA_FACET_PATTERN:
3428 ret = xmlRegexpExec(facet->regexp, value);
3429 if (ret == 1)
3430 return(0);
3431 if (ret == 0) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003432 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003433 return(1);
3434 }
3435 return(ret);
3436 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
3437 ret = xmlSchemaCompareValues(val, facet->val);
3438 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003439 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003440 return(-1);
3441 }
3442 if (ret == -1)
3443 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003444 /* error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003445 return(1);
Daniel Veillard070803b2002-05-03 07:29:38 +00003446 case XML_SCHEMA_FACET_MAXINCLUSIVE:
3447 ret = xmlSchemaCompareValues(val, facet->val);
3448 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003449 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003450 return(-1);
3451 }
3452 if ((ret == -1) || (ret == 0))
3453 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003454 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003455 return(1);
3456 case XML_SCHEMA_FACET_MINEXCLUSIVE:
3457 ret = xmlSchemaCompareValues(val, facet->val);
3458 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003459 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003460 return(-1);
3461 }
3462 if (ret == 1)
3463 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003464 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003465 return(1);
3466 case XML_SCHEMA_FACET_MININCLUSIVE:
3467 ret = xmlSchemaCompareValues(val, facet->val);
3468 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003469 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003470 return(-1);
3471 }
3472 if ((ret == 1) || (ret == 0))
3473 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003474 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003475 return(1);
Daniel Veillard8651f532002-04-17 09:06:27 +00003476 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003477 /* TODO whitespaces */
Daniel Veillard8651f532002-04-17 09:06:27 +00003478 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00003479 case XML_SCHEMA_FACET_ENUMERATION:
3480 if ((facet->value != NULL) &&
3481 (xmlStrEqual(facet->value, value)))
3482 return(0);
3483 return(1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003484 case XML_SCHEMA_FACET_LENGTH:
3485 case XML_SCHEMA_FACET_MAXLENGTH:
3486 case XML_SCHEMA_FACET_MINLENGTH: {
3487 unsigned int len = 0;
3488
3489 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003490 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
3491 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003492 (facet->val->value.decimal.frac != 0)) {
3493 return(-1);
3494 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00003495 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003496 len = val->value.hex.total;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00003497 else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
3498 len = val->value.base64.total;
3499 else {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003500 switch (base->flags) {
Daniel Veillard560c2a42003-07-06 21:13:49 +00003501 case XML_SCHEMAS_IDREF:
3502 case XML_SCHEMAS_NORMSTRING:
3503 case XML_SCHEMAS_TOKEN:
3504 case XML_SCHEMAS_LANGUAGE:
3505 case XML_SCHEMAS_NMTOKEN:
3506 case XML_SCHEMAS_NAME:
3507 case XML_SCHEMAS_NCNAME:
3508 case XML_SCHEMAS_ID:
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003509 len = xmlSchemaNormLen(value);
3510 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00003511 case XML_SCHEMAS_STRING:
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003512 len = xmlUTF8Strlen(value);
3513 break;
Daniel Veillard560c2a42003-07-06 21:13:49 +00003514 default:
3515 TODO
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00003516 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003517 }
3518 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003519 if (len != facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003520 return(1);
3521 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003522 if (len < facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003523 return(1);
3524 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003525 if (len > facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003526 return(1);
3527 }
3528 break;
3529 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00003530 case XML_SCHEMA_FACET_TOTALDIGITS:
3531 case XML_SCHEMA_FACET_FRACTIONDIGITS:
3532
3533 if ((facet->val == NULL) ||
3534 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
3535 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
3536 (facet->val->value.decimal.frac != 0)) {
3537 return(-1);
3538 }
3539 if ((val == NULL) ||
3540 ((val->type != XML_SCHEMAS_DECIMAL) &&
3541 (val->type != XML_SCHEMAS_INTEGER) &&
3542 (val->type != XML_SCHEMAS_NPINTEGER) &&
3543 (val->type != XML_SCHEMAS_NINTEGER) &&
3544 (val->type != XML_SCHEMAS_NNINTEGER) &&
3545 (val->type != XML_SCHEMAS_PINTEGER) &&
3546 (val->type != XML_SCHEMAS_INT) &&
3547 (val->type != XML_SCHEMAS_UINT) &&
3548 (val->type != XML_SCHEMAS_LONG) &&
3549 (val->type != XML_SCHEMAS_ULONG) &&
3550 (val->type != XML_SCHEMAS_SHORT) &&
3551 (val->type != XML_SCHEMAS_USHORT) &&
3552 (val->type != XML_SCHEMAS_BYTE) &&
3553 (val->type != XML_SCHEMAS_UBYTE))) {
3554 return(-1);
3555 }
3556 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
3557 if (val->value.decimal.total > facet->val->value.decimal.lo)
3558 return(1);
3559
3560 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
3561 if (val->value.decimal.frac > facet->val->value.decimal.lo)
3562 return(1);
3563 }
3564 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00003565 default:
3566 TODO
3567 }
3568 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003569
Daniel Veillard4255d502002-04-16 15:50:10 +00003570}
3571
3572#endif /* LIBXML_SCHEMAS_ENABLED */