blob: 051af8df67e70f62643ec0a2f9a4ae132efc875e [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,
85 XML_SCHEMAS_UBYTE
Daniel Veillard4255d502002-04-16 15:50:10 +000086} xmlSchemaValType;
87
88unsigned long powten[10] = {
89 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000L,
90 100000000L, 1000000000L
91};
92
Daniel Veillard070803b2002-05-03 07:29:38 +000093/* Date value */
94typedef struct _xmlSchemaValDate xmlSchemaValDate;
95typedef xmlSchemaValDate *xmlSchemaValDatePtr;
96struct _xmlSchemaValDate {
97 long year;
98 unsigned int mon :4; /* 1 <= mon <= 12 */
99 unsigned int day :5; /* 1 <= day <= 31 */
100 unsigned int hour :5; /* 0 <= hour <= 23 */
101 unsigned int min :6; /* 0 <= min <= 59 */
102 double sec;
103 int tz_flag :1; /* is tzo explicitely set? */
104 int tzo :11; /* -1440 <= tzo <= 1440 */
105};
106
107/* Duration value */
108typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
109typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
110struct _xmlSchemaValDuration {
111 long mon; /* mon stores years also */
112 long day;
113 double sec; /* sec stores min and hour also */
114};
115
Daniel Veillard4255d502002-04-16 15:50:10 +0000116typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
117typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
118struct _xmlSchemaValDecimal {
119 /* would use long long but not portable */
120 unsigned long base;
121 unsigned int extra;
Daniel Veillard5a872412002-05-22 06:40:27 +0000122 unsigned int sign:1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000123 int frac:7;
124 int total:8;
125};
126
127struct _xmlSchemaVal {
128 xmlSchemaValType type;
129 union {
Daniel Veillard5a872412002-05-22 06:40:27 +0000130 xmlSchemaValDecimal decimal;
Daniel Veillard070803b2002-05-03 07:29:38 +0000131 xmlSchemaValDate date;
132 xmlSchemaValDuration dur;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000133 float f;
134 double d;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000135 int b;
Daniel Veillard4255d502002-04-16 15:50:10 +0000136 } value;
137};
138
139static int xmlSchemaTypesInitialized = 0;
140static xmlHashTablePtr xmlSchemaTypesBank = NULL;
141
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000142/*
143 * Basic types
144 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000145static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
146static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
147static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
148static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000149static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000150static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000151static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL;
152static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL;
153static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL;
154static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL;
155static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL;
156static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL;
157static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000158static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000159static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000160static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000161static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000162
163/*
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000164 * Derived types
165 */
166static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
167static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL;
168static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL;
169static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
170static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL;
171static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL;
172static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL;
173static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL;
174static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL;
175static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL;
176static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL;
177static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL;
178static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000179static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL;
180static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL;
181static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL;
182static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL;
183static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000184static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000185static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
186static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
187static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
188static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
189static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000190
191/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000192 * xmlSchemaInitBasicType:
193 * @name: the type name
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000194 * @type: the value type associated
Daniel Veillard4255d502002-04-16 15:50:10 +0000195 *
196 * Initialize one default type
197 */
198static xmlSchemaTypePtr
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000199xmlSchemaInitBasicType(const char *name, xmlSchemaValType type) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000200 xmlSchemaTypePtr ret;
201
202 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
203 if (ret == NULL) {
204 xmlGenericError(xmlGenericErrorContext,
205 "Could not initilize type %s: out of memory\n", name);
206 return(NULL);
207 }
208 memset(ret, 0, sizeof(xmlSchemaType));
209 ret->name = xmlStrdup((const xmlChar *)name);
210 ret->type = XML_SCHEMA_TYPE_BASIC;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000211 ret->flags = type;
Daniel Veillard4255d502002-04-16 15:50:10 +0000212 ret->contentType = XML_SCHEMA_CONTENT_BASIC;
213 xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
214 XML_SCHEMAS_NAMESPACE_NAME, ret);
215 return(ret);
216}
217
218/*
219 * xmlSchemaInitTypes:
220 *
221 * Initialize the default XML Schemas type library
222 */
223void
224xmlSchemaInitTypes(void) {
225 if (xmlSchemaTypesInitialized != 0)
226 return;
227 xmlSchemaTypesBank = xmlHashCreate(40);
228
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000229 /*
230 * primitive datatypes
231 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000232 xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
233 XML_SCHEMAS_STRING);
234 xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
235 XML_SCHEMAS_UNKNOWN);
236 xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
237 XML_SCHEMAS_UNKNOWN);
238 xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
239 XML_SCHEMAS_DECIMAL);
240 xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
241 XML_SCHEMAS_DATE);
242 xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
243 XML_SCHEMAS_DATETIME);
244 xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
245 XML_SCHEMAS_TIME);
246 xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
247 XML_SCHEMAS_GYEAR);
248 xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
249 XML_SCHEMAS_GYEARMONTH);
250 xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
251 XML_SCHEMAS_GMONTH);
252 xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
253 XML_SCHEMAS_GMONTHDAY);
254 xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
255 XML_SCHEMAS_GDAY);
256 xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
257 XML_SCHEMAS_DURATION);
258 xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
259 XML_SCHEMAS_FLOAT);
260 xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
261 XML_SCHEMAS_DOUBLE);
262 xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
263 XML_SCHEMAS_BOOLEAN);
264 xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
265 XML_SCHEMAS_ANYURI);
Daniel Veillard4255d502002-04-16 15:50:10 +0000266
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000267 /*
268 * derived datatypes
269 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000270 xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
271 XML_SCHEMAS_INTEGER);;
272 xmlSchemaTypeNonPositiveIntegerDef = xmlSchemaInitBasicType("nonPositiveInteger",
273 XML_SCHEMAS_NPINTEGER);;
274 xmlSchemaTypeNegativeIntegerDef = xmlSchemaInitBasicType("negativeInteger",
275 XML_SCHEMAS_NINTEGER);;
276 xmlSchemaTypeLongDef = xmlSchemaInitBasicType("long",
277 XML_SCHEMAS_LONG);;
278 xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int",
279 XML_SCHEMAS_INT);;
280 xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
281 XML_SCHEMAS_SHORT);;
282 xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
283 XML_SCHEMAS_BYTE);;
284 xmlSchemaTypeNonNegativeIntegerDef = xmlSchemaInitBasicType("nonNegativeInteger",
285 XML_SCHEMAS_NNINTEGER);
286 xmlSchemaTypeUnsignedLongDef = xmlSchemaInitBasicType("unsignedLong",
287 XML_SCHEMAS_ULONG);;
288 xmlSchemaTypeUnsignedIntDef = xmlSchemaInitBasicType("unsignedInt",
289 XML_SCHEMAS_UINT);;
290 xmlSchemaTypeUnsignedShortDef = xmlSchemaInitBasicType("insignedShort",
291 XML_SCHEMAS_USHORT);;
292 xmlSchemaTypeUnsignedByteDef = xmlSchemaInitBasicType("unsignedByte",
293 XML_SCHEMAS_UBYTE);;
294 xmlSchemaTypePositiveIntegerDef = xmlSchemaInitBasicType("positiveInteger",
295 XML_SCHEMAS_PINTEGER);
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000296
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000297 xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
298 XML_SCHEMAS_NORMSTRING);
299 xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
300 XML_SCHEMAS_TOKEN);
301 xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
302 XML_SCHEMAS_LANGUAGE);
303 xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID",
304 XML_SCHEMAS_ID);
305 xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
306 XML_SCHEMAS_IDREF);
307 xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
308 XML_SCHEMAS_IDREFS);
309 xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
310 XML_SCHEMAS_NAME);
311 xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
312 XML_SCHEMAS_QNAME);
313 xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
314 XML_SCHEMAS_NCNAME);
315 xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
316 XML_SCHEMAS_NMTOKEN);
317 xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
318 XML_SCHEMAS_NMTOKENS);
Daniel Veillard4255d502002-04-16 15:50:10 +0000319 xmlSchemaTypesInitialized = 1;
320}
321
322/**
323 * xmlSchemaCleanupTypes:
324 *
325 * Cleanup the default XML Schemas type library
326 */
327void
328xmlSchemaCleanupTypes(void) {
329 if (xmlSchemaTypesInitialized == 0)
330 return;
331 xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
332 xmlSchemaTypesInitialized = 0;
333}
334
335/**
336 * xmlSchemaNewValue:
337 * @type: the value type
338 *
339 * Allocate a new simple type value
340 *
341 * Returns a pointer to the new value or NULL in case of error
342 */
343static xmlSchemaValPtr
344xmlSchemaNewValue(xmlSchemaValType type) {
345 xmlSchemaValPtr value;
346
347 value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
348 if (value == NULL) {
349 return(NULL);
350 }
351 memset(value, 0, sizeof(xmlSchemaVal));
352 value->type = type;
353 return(value);
354}
355
356/**
357 * xmlSchemaFreeValue:
358 * @value: the value to free
359 *
360 * Cleanup the default XML Schemas type library
361 */
362void
363xmlSchemaFreeValue(xmlSchemaValPtr value) {
364 if (value == NULL)
365 return;
366 xmlFree(value);
367}
368
369/**
370 * xmlSchemaGetPredefinedType:
371 * @name: the type name
372 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
373 *
374 * Lookup a type in the default XML Schemas type library
375 *
376 * Returns the type if found, NULL otherwise
377 */
378xmlSchemaTypePtr
379xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
380 if (xmlSchemaTypesInitialized == 0)
381 xmlSchemaInitTypes();
382 if (name == NULL)
383 return(NULL);
384 return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
385}
Daniel Veillard070803b2002-05-03 07:29:38 +0000386
387/****************************************************************
388 * *
389 * Convenience macros and functions *
390 * *
391 ****************************************************************/
392
393#define IS_TZO_CHAR(c) \
394 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
395
396#define VALID_YEAR(yr) (yr != 0)
397#define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
398/* VALID_DAY should only be used when month is unknown */
399#define VALID_DAY(day) ((day >= 1) && (day <= 31))
400#define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
401#define VALID_MIN(min) ((min >= 0) && (min <= 59))
402#define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
403#define VALID_TZO(tzo) ((tzo > -1440) && (tzo < 1440))
404#define IS_LEAP(y) \
405 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
406
407static const long daysInMonth[12] =
408 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
409static const long daysInMonthLeap[12] =
410 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
411
Daniel Veillard5a872412002-05-22 06:40:27 +0000412#define MAX_DAYINMONTH(yr,mon) \
413 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
414
Daniel Veillard070803b2002-05-03 07:29:38 +0000415#define VALID_MDAY(dt) \
416 (IS_LEAP(dt->year) ? \
417 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
418 (dt->day <= daysInMonth[dt->mon - 1]))
419
420#define VALID_DATE(dt) \
421 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
422
423#define VALID_TIME(dt) \
424 (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
425 VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
426
427#define VALID_DATETIME(dt) \
428 (VALID_DATE(dt) && VALID_TIME(dt))
429
430#define SECS_PER_MIN (60)
431#define SECS_PER_HOUR (60 * SECS_PER_MIN)
432#define SECS_PER_DAY (24 * SECS_PER_HOUR)
433
Daniel Veillard5a872412002-05-22 06:40:27 +0000434static const long dayInYearByMonth[12] =
435 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
436static const long dayInLeapYearByMonth[12] =
437 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
438
439#define DAY_IN_YEAR(day, month, year) \
440 ((IS_LEAP(year) ? \
441 dayInLeapYearByMonth[month - 1] : \
442 dayInYearByMonth[month - 1]) + day)
443
444#ifdef DEBUG
445#define DEBUG_DATE(dt) \
446 xmlGenericError(xmlGenericErrorContext, \
447 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
448 dt->type,dt->value.date.year,dt->value.date.mon, \
449 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
450 dt->value.date.sec); \
451 if (dt->value.date.tz_flag) \
452 if (dt->value.date.tzo != 0) \
453 xmlGenericError(xmlGenericErrorContext, \
454 "%+05d\n",dt->value.date.tzo); \
455 else \
456 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
457 else \
458 xmlGenericError(xmlGenericErrorContext,"\n")
459#else
460#define DEBUG_DATE(dt)
461#endif
462
Daniel Veillard070803b2002-05-03 07:29:38 +0000463/**
464 * _xmlSchemaParseGYear:
465 * @dt: pointer to a date structure
466 * @str: pointer to the string to analyze
467 *
468 * Parses a xs:gYear without time zone and fills in the appropriate
469 * field of the @dt structure. @str is updated to point just after the
470 * xs:gYear. It is supposed that @dt->year is big enough to contain
471 * the year.
472 *
473 * Returns 0 or the error code
474 */
475static int
476_xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
477 const xmlChar *cur = *str, *firstChar;
478 int isneg = 0, digcnt = 0;
479
480 if (((*cur < '0') || (*cur > '9')) &&
481 (*cur != '-') && (*cur != '+'))
482 return -1;
483
484 if (*cur == '-') {
485 isneg = 1;
486 cur++;
487 }
488
489 firstChar = cur;
490
491 while ((*cur >= '0') && (*cur <= '9')) {
492 dt->year = dt->year * 10 + (*cur - '0');
493 cur++;
494 digcnt++;
495 }
496
497 /* year must be at least 4 digits (CCYY); over 4
498 * digits cannot have a leading zero. */
499 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
500 return 1;
501
502 if (isneg)
503 dt->year = - dt->year;
504
505 if (!VALID_YEAR(dt->year))
506 return 2;
507
508 *str = cur;
509 return 0;
510}
511
512/**
513 * PARSE_2_DIGITS:
514 * @num: the integer to fill in
515 * @cur: an #xmlChar *
516 * @invalid: an integer
517 *
518 * Parses a 2-digits integer and updates @num with the value. @cur is
519 * updated to point just after the integer.
520 * In case of error, @invalid is set to %TRUE, values of @num and
521 * @cur are undefined.
522 */
523#define PARSE_2_DIGITS(num, cur, invalid) \
524 if ((cur[0] < '0') || (cur[0] > '9') || \
525 (cur[1] < '0') || (cur[1] > '9')) \
526 invalid = 1; \
527 else \
528 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
529 cur += 2;
530
531/**
532 * PARSE_FLOAT:
533 * @num: the double to fill in
534 * @cur: an #xmlChar *
535 * @invalid: an integer
536 *
537 * Parses a float and updates @num with the value. @cur is
538 * updated to point just after the float. The float must have a
539 * 2-digits integer part and may or may not have a decimal part.
540 * In case of error, @invalid is set to %TRUE, values of @num and
541 * @cur are undefined.
542 */
543#define PARSE_FLOAT(num, cur, invalid) \
544 PARSE_2_DIGITS(num, cur, invalid); \
545 if (!invalid && (*cur == '.')) { \
546 double mult = 1; \
547 cur++; \
548 if ((*cur < '0') || (*cur > '9')) \
549 invalid = 1; \
550 while ((*cur >= '0') && (*cur <= '9')) { \
551 mult /= 10; \
552 num += (*cur - '0') * mult; \
553 cur++; \
554 } \
555 }
556
557/**
558 * _xmlSchemaParseGMonth:
559 * @dt: pointer to a date structure
560 * @str: pointer to the string to analyze
561 *
562 * Parses a xs:gMonth without time zone and fills in the appropriate
563 * field of the @dt structure. @str is updated to point just after the
564 * xs:gMonth.
565 *
566 * Returns 0 or the error code
567 */
568static int
569_xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
570 const xmlChar *cur = *str;
571 int ret = 0;
572
573 PARSE_2_DIGITS(dt->mon, cur, ret);
574 if (ret != 0)
575 return ret;
576
577 if (!VALID_MONTH(dt->mon))
578 return 2;
579
580 *str = cur;
581 return 0;
582}
583
584/**
585 * _xmlSchemaParseGDay:
586 * @dt: pointer to a date structure
587 * @str: pointer to the string to analyze
588 *
589 * Parses a xs:gDay without time zone and fills in the appropriate
590 * field of the @dt structure. @str is updated to point just after the
591 * xs:gDay.
592 *
593 * Returns 0 or the error code
594 */
595static int
596_xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
597 const xmlChar *cur = *str;
598 int ret = 0;
599
600 PARSE_2_DIGITS(dt->day, cur, ret);
601 if (ret != 0)
602 return ret;
603
604 if (!VALID_DAY(dt->day))
605 return 2;
606
607 *str = cur;
608 return 0;
609}
610
611/**
612 * _xmlSchemaParseTime:
613 * @dt: pointer to a date structure
614 * @str: pointer to the string to analyze
615 *
616 * Parses a xs:time without time zone and fills in the appropriate
617 * fields of the @dt structure. @str is updated to point just after the
618 * xs:time.
619 * In case of error, values of @dt fields are undefined.
620 *
621 * Returns 0 or the error code
622 */
623static int
624_xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
625 const xmlChar *cur = *str;
626 unsigned int hour = 0; /* use temp var in case str is not xs:time */
627 int ret = 0;
628
629 PARSE_2_DIGITS(hour, cur, ret);
630 if (ret != 0)
631 return ret;
632
633 if (*cur != ':')
634 return 1;
635 cur++;
636
637 /* the ':' insures this string is xs:time */
638 dt->hour = hour;
639
640 PARSE_2_DIGITS(dt->min, cur, ret);
641 if (ret != 0)
642 return ret;
643
644 if (*cur != ':')
645 return 1;
646 cur++;
647
648 PARSE_FLOAT(dt->sec, cur, ret);
649 if (ret != 0)
650 return ret;
651
652 if (!VALID_TIME(dt))
653 return 2;
654
655 *str = cur;
656 return 0;
657}
658
659/**
660 * _xmlSchemaParseTimeZone:
661 * @dt: pointer to a date structure
662 * @str: pointer to the string to analyze
663 *
664 * Parses a time zone without time zone and fills in the appropriate
665 * field of the @dt structure. @str is updated to point just after the
666 * time zone.
667 *
668 * Returns 0 or the error code
669 */
670static int
671_xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
672 const xmlChar *cur = *str;
673 int ret = 0;
674
675 if (str == NULL)
676 return -1;
677
678 switch (*cur) {
679 case 0:
680 dt->tz_flag = 0;
681 dt->tzo = 0;
682 break;
683
684 case 'Z':
685 dt->tz_flag = 1;
686 dt->tzo = 0;
687 cur++;
688 break;
689
690 case '+':
691 case '-': {
692 int isneg = 0, tmp = 0;
693 isneg = (*cur == '-');
694
695 cur++;
696
697 PARSE_2_DIGITS(tmp, cur, ret);
698 if (ret != 0)
699 return ret;
700 if (!VALID_HOUR(tmp))
701 return 2;
702
703 if (*cur != ':')
704 return 1;
705 cur++;
706
707 dt->tzo = tmp * 60;
708
709 PARSE_2_DIGITS(tmp, cur, ret);
710 if (ret != 0)
711 return ret;
712 if (!VALID_MIN(tmp))
713 return 2;
714
715 dt->tzo += tmp;
716 if (isneg)
717 dt->tzo = - dt->tzo;
718
719 if (!VALID_TZO(dt->tzo))
720 return 2;
721
Daniel Veillard5a872412002-05-22 06:40:27 +0000722 dt->tz_flag = 1;
Daniel Veillard070803b2002-05-03 07:29:38 +0000723 break;
724 }
725 default:
726 return 1;
727 }
728
729 *str = cur;
730 return 0;
731}
732
733/****************************************************************
734 * *
735 * XML Schema Dates/Times Datatypes Handling *
736 * *
737 ****************************************************************/
738
739/**
740 * PARSE_DIGITS:
741 * @num: the integer to fill in
742 * @cur: an #xmlChar *
743 * @num_type: an integer flag
744 *
745 * Parses a digits integer and updates @num with the value. @cur is
746 * updated to point just after the integer.
747 * In case of error, @num_type is set to -1, values of @num and
748 * @cur are undefined.
749 */
750#define PARSE_DIGITS(num, cur, num_type) \
751 if ((*cur < '0') || (*cur > '9')) \
752 num_type = -1; \
753 else \
754 while ((*cur >= '0') && (*cur <= '9')) { \
755 num = num * 10 + (*cur - '0'); \
756 cur++; \
757 }
758
759/**
760 * PARSE_NUM:
761 * @num: the double to fill in
762 * @cur: an #xmlChar *
763 * @num_type: an integer flag
764 *
765 * Parses a float or integer and updates @num with the value. @cur is
766 * updated to point just after the number. If the number is a float,
767 * then it must have an integer part and a decimal part; @num_type will
768 * be set to 1. If there is no decimal part, @num_type is set to zero.
769 * In case of error, @num_type is set to -1, values of @num and
770 * @cur are undefined.
771 */
772#define PARSE_NUM(num, cur, num_type) \
773 num = 0; \
774 PARSE_DIGITS(num, cur, num_type); \
775 if (!num_type && (*cur == '.')) { \
776 double mult = 1; \
777 cur++; \
778 if ((*cur < '0') || (*cur > '9')) \
779 num_type = -1; \
780 else \
781 num_type = 1; \
782 while ((*cur >= '0') && (*cur <= '9')) { \
783 mult /= 10; \
784 num += (*cur - '0') * mult; \
785 cur++; \
786 } \
787 }
788
789/**
Daniel Veillard5a872412002-05-22 06:40:27 +0000790 * xmlSchemaValidateDates:
Daniel Veillard070803b2002-05-03 07:29:38 +0000791 * @type: the predefined type
792 * @dateTime: string to analyze
793 * @val: the return computed value
794 *
795 * Check that @dateTime conforms to the lexical space of one of the date types.
796 * if true a value is computed and returned in @val.
797 *
798 * Returns 0 if this validates, a positive error code number otherwise
799 * and -1 in case of internal or API error.
800 */
801static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000802xmlSchemaValidateDates (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +0000803 const xmlChar *dateTime, xmlSchemaValPtr *val) {
Daniel Veillard070803b2002-05-03 07:29:38 +0000804 xmlSchemaValPtr dt;
805 int ret;
806 const xmlChar *cur = dateTime;
807
808#define RETURN_TYPE_IF_VALID(t) \
809 if (IS_TZO_CHAR(*cur)) { \
810 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
811 if (ret == 0) { \
812 if (*cur != 0) \
813 goto error; \
814 dt->type = t; \
815 if (val != NULL) \
816 *val = dt; \
817 return 0; \
818 } \
819 }
820
821 if (dateTime == NULL)
822 return -1;
823
824 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
825 return 1;
826
827 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
828 if (dt == NULL)
829 return -1;
830
831 if ((cur[0] == '-') && (cur[1] == '-')) {
832 /*
833 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
834 * xs:gDay)
835 */
836 cur += 2;
837
838 /* is it an xs:gDay? */
839 if (*cur == '-') {
840 ++cur;
841 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
842 if (ret != 0)
843 goto error;
844
845 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
846
847 goto error;
848 }
849
850 /*
851 * it should be an xs:gMonthDay or xs:gMonth
852 */
853 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
854 if (ret != 0)
855 goto error;
856
857 if (*cur != '-')
858 goto error;
859 cur++;
860
861 /* is it an xs:gMonth? */
862 if (*cur == '-') {
863 cur++;
864 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
865 goto error;
866 }
867
868 /* it should be an xs:gMonthDay */
869 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
870 if (ret != 0)
871 goto error;
872
873 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
874
875 goto error;
876 }
877
878 /*
879 * It's a right-truncated date or an xs:time.
880 * Try to parse an xs:time then fallback on right-truncated dates.
881 */
882 if ((*cur >= '0') && (*cur <= '9')) {
883 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
884 if (ret == 0) {
885 /* it's an xs:time */
886 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
887 }
888 }
889
890 /* fallback on date parsing */
891 cur = dateTime;
892
893 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
894 if (ret != 0)
895 goto error;
896
897 /* is it an xs:gYear? */
898 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
899
900 if (*cur != '-')
901 goto error;
902 cur++;
903
904 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
905 if (ret != 0)
906 goto error;
907
908 /* is it an xs:gYearMonth? */
909 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
910
911 if (*cur != '-')
912 goto error;
913 cur++;
914
915 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
916 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
917 goto error;
918
919 /* is it an xs:date? */
920 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
921
922 if (*cur != 'T')
923 goto error;
924 cur++;
925
926 /* it should be an xs:dateTime */
927 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
928 if (ret != 0)
929 goto error;
930
931 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
932 if ((ret != 0) || (*cur != 0) || !VALID_DATETIME((&(dt->value.date))))
933 goto error;
934
935 dt->type = XML_SCHEMAS_DATETIME;
936
937 if (val != NULL)
938 *val = dt;
939
940 return 0;
941
942error:
943 if (dt != NULL)
944 xmlSchemaFreeValue(dt);
945 return 1;
946}
947
948/**
Daniel Veillard5a872412002-05-22 06:40:27 +0000949 * xmlSchemaValidateDuration:
Daniel Veillard070803b2002-05-03 07:29:38 +0000950 * @type: the predefined type
951 * @duration: string to analyze
952 * @val: the return computed value
953 *
954 * Check that @duration conforms to the lexical space of the duration type.
955 * if true a value is computed and returned in @val.
956 *
957 * Returns 0 if this validates, a positive error code number otherwise
958 * and -1 in case of internal or API error.
959 */
960static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000961xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +0000962 const xmlChar *duration, xmlSchemaValPtr *val) {
Daniel Veillard070803b2002-05-03 07:29:38 +0000963 const xmlChar *cur = duration;
964 xmlSchemaValPtr dur;
965 int isneg = 0;
966 unsigned int seq = 0;
967
968 if (duration == NULL)
969 return -1;
970
971 if (*cur == '-') {
972 isneg = 1;
973 cur++;
974 }
975
976 /* duration must start with 'P' (after sign) */
977 if (*cur++ != 'P')
978 return 1;
979
980 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
981 if (dur == NULL)
982 return -1;
983
984 while (*cur != 0) {
985 double num;
986 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
987 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
988 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
989
990 /* input string should be empty or invalid date/time item */
991 if (seq >= sizeof(desig))
992 goto error;
993
994 /* T designator must be present for time items */
995 if (*cur == 'T') {
996 if (seq <= 3) {
997 seq = 3;
998 cur++;
999 } else
1000 return 1;
1001 } else if (seq == 3)
1002 goto error;
1003
1004 /* parse the number portion of the item */
1005 PARSE_NUM(num, cur, num_type);
1006
1007 if ((num_type == -1) || (*cur == 0))
1008 goto error;
1009
1010 /* update duration based on item type */
1011 while (seq < sizeof(desig)) {
1012 if (*cur == desig[seq]) {
1013
1014 /* verify numeric type; only seconds can be float */
1015 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1016 goto error;
1017
1018 switch (seq) {
1019 case 0:
1020 dur->value.dur.mon = (long)num * 12;
1021 break;
1022 case 1:
1023 dur->value.dur.mon += (long)num;
1024 break;
1025 default:
1026 /* convert to seconds using multiplier */
1027 dur->value.dur.sec += num * multi[seq];
1028 seq++;
1029 break;
1030 }
1031
1032 break; /* exit loop */
1033 }
1034 /* no date designators found? */
1035 if (++seq == 3)
1036 goto error;
1037 }
1038 cur++;
1039 }
1040
1041 if (isneg) {
1042 dur->value.dur.mon = -dur->value.dur.mon;
1043 dur->value.dur.day = -dur->value.dur.day;
1044 dur->value.dur.sec = -dur->value.dur.sec;
1045 }
1046
1047 if (val != NULL)
1048 *val = dur;
1049
1050 return 0;
1051
1052error:
1053 if (dur != NULL)
1054 xmlSchemaFreeValue(dur);
1055 return 1;
1056}
1057
Daniel Veillard96a4b252003-02-06 08:22:32 +00001058
1059/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001060 * xmlSchemaValidatePredefinedType:
1061 * @type: the predefined type
1062 * @value: the value to check
1063 * @val: the return computed value
1064 *
1065 * Check that a value conforms to the lexical space of the predefined type.
1066 * if true a value is computed and returned in @val.
1067 *
1068 * Returns 0 if this validates, a positive error code number otherwise
1069 * and -1 in case of internal or API error.
1070 */
1071int
1072xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
1073 xmlSchemaValPtr *val) {
1074 xmlSchemaValPtr v;
Daniel Veillard96a4b252003-02-06 08:22:32 +00001075 int ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00001076
1077 if (xmlSchemaTypesInitialized == 0)
1078 return(-1);
1079 if (type == NULL)
1080 return(-1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001081
Daniel Veillard4255d502002-04-16 15:50:10 +00001082 if (val != NULL)
1083 *val = NULL;
1084 if (type == xmlSchemaTypeStringDef) {
1085 return(0);
1086 } else if (type == xmlSchemaTypeAnyTypeDef) {
1087 return(0);
1088 } else if (type == xmlSchemaTypeAnySimpleTypeDef) {
1089 return(0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00001090 } else if (type == xmlSchemaTypeNmtokenDef) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001091 if (xmlValidateNmtokenValue(value))
1092 return(0);
1093 return(1);
1094 } else if (type == xmlSchemaTypeDecimalDef) {
1095 const xmlChar *cur = value, *tmp;
Daniel Veillard5a872412002-05-22 06:40:27 +00001096 int frac = 0, len, neg = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001097 unsigned long base = 0;
1098 if (cur == NULL)
1099 return(1);
1100 if (*cur == '+')
1101 cur++;
1102 else if (*cur == '-') {
1103 neg = 1;
1104 cur++;
1105 }
1106 tmp = cur;
1107 while ((*cur >= '0') && (*cur <= '9')) {
1108 base = base * 10 + (*cur - '0');
1109 cur++;
1110 }
Daniel Veillard5a872412002-05-22 06:40:27 +00001111 len = cur - tmp;
Daniel Veillard4255d502002-04-16 15:50:10 +00001112 if (*cur == '.') {
1113 cur++;
1114 tmp = cur;
1115 while ((*cur >= '0') && (*cur <= '9')) {
1116 base = base * 10 + (*cur - '0');
1117 cur++;
1118 }
1119 frac = cur - tmp;
1120 }
1121 if (*cur != 0)
1122 return(1);
1123 if (val != NULL) {
1124 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1125 if (v != NULL) {
1126 v->value.decimal.base = base;
1127 v->value.decimal.sign = neg;
1128 v->value.decimal.frac = frac;
Daniel Veillard5a872412002-05-22 06:40:27 +00001129 v->value.decimal.total = frac + len;
Daniel Veillard4255d502002-04-16 15:50:10 +00001130 *val = v;
1131 }
1132 }
1133 return(0);
Daniel Veillard070803b2002-05-03 07:29:38 +00001134 } else if (type == xmlSchemaTypeDurationDef) {
Daniel Veillard5a872412002-05-22 06:40:27 +00001135 return xmlSchemaValidateDuration(type, value, val);
Daniel Veillard070803b2002-05-03 07:29:38 +00001136 } else if ((type == xmlSchemaTypeDatetimeDef) ||
1137 (type == xmlSchemaTypeTimeDef) ||
1138 (type == xmlSchemaTypeDateDef) ||
1139 (type == xmlSchemaTypeGYearDef) ||
1140 (type == xmlSchemaTypeGYearMonthDef) ||
1141 (type == xmlSchemaTypeGMonthDef) ||
1142 (type == xmlSchemaTypeGMonthDayDef) ||
1143 (type == xmlSchemaTypeGDayDef)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00001144 return xmlSchemaValidateDates(type, value, val);
Daniel Veillard4255d502002-04-16 15:50:10 +00001145 } else if (type == xmlSchemaTypePositiveIntegerDef) {
1146 const xmlChar *cur = value;
1147 unsigned long base = 0;
1148 int total = 0;
1149 if (cur == NULL)
1150 return(1);
1151 if (*cur == '+')
1152 cur++;
1153 while ((*cur >= '0') && (*cur <= '9')) {
1154 base = base * 10 + (*cur - '0');
1155 total++;
1156 cur++;
1157 }
1158 if (*cur != 0)
1159 return(1);
1160 if (val != NULL) {
1161 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1162 if (v != NULL) {
1163 v->value.decimal.base = base;
1164 v->value.decimal.sign = 0;
1165 v->value.decimal.frac = 0;
1166 v->value.decimal.total = total;
1167 *val = v;
1168 }
1169 }
1170 return(0);
1171 } else if (type == xmlSchemaTypeNonNegativeIntegerDef) {
1172 const xmlChar *cur = value;
1173 unsigned long base = 0;
1174 int total = 0;
1175 int sign = 0;
1176 if (cur == NULL)
1177 return(1);
1178 if (*cur == '-') {
1179 sign = 1;
1180 cur++;
1181 } else if (*cur == '+')
1182 cur++;
1183 while ((*cur >= '0') && (*cur <= '9')) {
1184 base = base * 10 + (*cur - '0');
1185 total++;
1186 cur++;
1187 }
1188 if (*cur != 0)
1189 return(1);
1190 if ((sign == 1) && (base != 0))
1191 return(1);
1192 if (val != NULL) {
1193 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1194 if (v != NULL) {
1195 v->value.decimal.base = base;
1196 v->value.decimal.sign = 0;
1197 v->value.decimal.frac = 0;
1198 v->value.decimal.total = total;
1199 *val = v;
1200 }
1201 }
1202 return(0);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001203 } else if (type == xmlSchemaTypeIntDef) {
1204 const xmlChar *cur = value;
Daniel Veillard84d70a42002-09-16 10:51:38 +00001205 unsigned long base = 0;
Daniel Veillard96a4b252003-02-06 08:22:32 +00001206 int total = 0;
1207 int sign = 0;
Daniel Veillard84d70a42002-09-16 10:51:38 +00001208 if (cur == NULL)
1209 return(1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001210 if (*cur == '-') {
1211 sign = 1;
1212 cur++;
1213 } else if (*cur == '+')
1214 cur++;
1215 while (*cur == '0') {
1216 total++;
1217 cur++;
1218 }
1219 while ((*cur >= '0') && (*cur <= '9')) {
1220 base = base * 10 + (*cur - '0');
1221 total++;
1222 cur++;
1223 }
1224 if (*cur != 0)
1225 return(1);
1226 if ((sign == 1) && (total == 0))
1227 return(1);
1228 if (val != NULL) {
1229 v = xmlSchemaNewValue(XML_SCHEMAS_INT);
1230 if (v != NULL) {
1231 v->value.decimal.base = base;
1232 v->value.decimal.sign = sign;
1233 v->value.decimal.frac = 0;
1234 v->value.decimal.total = total;
1235 *val = v;
1236 }
1237 }
1238 return(0);
1239 } else if ((type == xmlSchemaTypeFloatDef) ||
1240 (type == xmlSchemaTypeDoubleDef)) {
1241 const xmlChar *cur = value;
1242 int neg = 0;
1243 if (cur == NULL)
1244 return(1);
1245 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
1246 cur += 3;
1247 if (*cur != 0)
1248 return(1);
1249 if (val != NULL) {
1250 if (type == xmlSchemaTypeFloatDef) {
1251 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1252 if (v != NULL) {
1253 v->value.f = (float) xmlXPathNAN;
1254 } else {
1255 xmlSchemaFreeValue(v);
1256 return(-1);
1257 }
1258 } else {
1259 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1260 if (v != NULL) {
1261 v->value.d = xmlXPathNAN;
1262 } else {
1263 xmlSchemaFreeValue(v);
1264 return(-1);
1265 }
1266 }
1267 *val = v;
1268 }
1269 return(0);
1270 }
Daniel Veillard84d70a42002-09-16 10:51:38 +00001271 if (*cur == '+')
1272 cur++;
1273 else if (*cur == '-') {
1274 neg = 1;
1275 cur++;
1276 }
Daniel Veillardd4310742003-02-18 21:12:46 +00001277 if (cur[0] == 0)
1278 return(1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001279 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
1280 cur += 3;
1281 if (*cur != 0)
1282 return(1);
1283 if (val != NULL) {
1284 if (type == xmlSchemaTypeFloatDef) {
1285 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1286 if (v != NULL) {
1287 if (neg)
1288 v->value.f = (float) xmlXPathNINF;
1289 else
1290 v->value.f = (float) xmlXPathPINF;
1291 } else {
1292 xmlSchemaFreeValue(v);
1293 return(-1);
1294 }
1295 } else {
1296 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1297 if (v != NULL) {
1298 if (neg)
1299 v->value.d = xmlXPathNINF;
1300 else
1301 v->value.d = xmlXPathPINF;
1302 } else {
1303 xmlSchemaFreeValue(v);
1304 return(-1);
1305 }
1306 }
1307 *val = v;
1308 }
1309 return(0);
1310 }
Daniel Veillard84d70a42002-09-16 10:51:38 +00001311 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard84d70a42002-09-16 10:51:38 +00001312 cur++;
1313 }
Daniel Veillard84d70a42002-09-16 10:51:38 +00001314 if (*cur == '.') {
1315 cur++;
Daniel Veillard96a4b252003-02-06 08:22:32 +00001316 while ((*cur >= '0') && (*cur <= '9'))
Daniel Veillard84d70a42002-09-16 10:51:38 +00001317 cur++;
Daniel Veillard84d70a42002-09-16 10:51:38 +00001318 }
Daniel Veillard96a4b252003-02-06 08:22:32 +00001319 if ((*cur == 'e') || (*cur == 'E')) {
1320 cur++;
1321 if (*cur == '-')
1322 cur++;
1323 while ((*cur >= '0') && (*cur <= '9'))
1324 cur++;
1325 }
1326 if (*cur != 0)
1327 return(1);
1328 if (val != NULL) {
1329 if (type == xmlSchemaTypeFloatDef) {
1330 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1331 if (v != NULL) {
1332 if (sscanf((const char *)value, "%f", &(v->value.f))==1) {
1333 *val = v;
1334 } else {
1335 xmlGenericError(xmlGenericErrorContext,
1336 "failed to scanf float %s\n", value);
1337 xmlSchemaFreeValue(v);
1338 return(1);
1339 }
1340 } else {
1341 return(-1);
1342 }
1343 } else {
1344 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1345 if (v != NULL) {
1346 if (sscanf((const char *)value, "%lf", &(v->value.d))==1) {
1347 *val = v;
1348 } else {
1349 xmlGenericError(xmlGenericErrorContext,
1350 "failed to scanf double %s\n", value);
1351 xmlSchemaFreeValue(v);
1352 return(1);
1353 }
1354 } else {
1355 return(-1);
1356 }
1357 }
1358 }
Daniel Veillardb5c05732002-09-20 13:36:25 +00001359 return(0);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001360 } else if (type == xmlSchemaTypeNameDef) {
Daniel Veillardd2298792003-02-14 16:54:11 +00001361 ret = xmlValidateName(value, 1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001362 if ((ret == 0) && (val != NULL)) {
1363 TODO;
1364 }
1365 return(ret);
1366 } else if (type == xmlSchemaTypeQNameDef) {
Daniel Veillardd2298792003-02-14 16:54:11 +00001367 ret = xmlValidateQName(value, 1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001368 if ((ret == 0) && (val != NULL)) {
1369 TODO;
1370 }
1371 return(ret);
1372 } else if (type == xmlSchemaTypeNCNameDef) {
Daniel Veillardd2298792003-02-14 16:54:11 +00001373 ret = xmlValidateNCName(value, 1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001374 if ((ret == 0) && (val != NULL)) {
1375 TODO;
1376 }
1377 return(ret);
1378 } else if (type == xmlSchemaTypeAnyURIDef) {
1379 xmlURIPtr uri;
1380
1381 uri = xmlParseURI((const char *) value);
1382 if (uri == NULL)
1383 return(1);
1384 if (val != NULL) {
1385 TODO;
1386 }
1387 xmlFreeURI(uri);
Daniel Veillardb5c05732002-09-20 13:36:25 +00001388 return(0);
Daniel Veillardc5a70f22003-02-06 23:41:59 +00001389 } else if (type == xmlSchemaTypeBooleanDef) {
1390 const xmlChar *cur = value;
1391
1392 if ((cur[0] == '0') && (cur[1] == 0))
1393 ret = 0;
1394 else if ((cur[0] == '1') && (cur[1] == 0))
1395 ret = 1;
1396 else if ((cur[0] == 't') && (cur[1] == 'r') && (cur[2] == 'u') &&
1397 (cur[3] == 'e') && (cur[4] == 0))
1398 ret = 1;
1399 else if ((cur[0] == 'f') && (cur[1] == 'a') && (cur[2] == 'l') &&
1400 (cur[3] == 's') && (cur[4] == 'e') && (cur[5] == 0))
1401 ret = 0;
1402 else
1403 return(1);
1404 if (val != NULL) {
1405 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
1406 if (v != NULL) {
1407 v->value.b = ret;
1408 *val = v;
1409 } else {
1410 return(-1);
1411 }
1412 }
1413 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001414 } else {
1415 TODO
1416 return(0);
1417 }
Daniel Veillard96a4b252003-02-06 08:22:32 +00001418 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001419}
1420
1421/**
1422 * xmlSchemaCompareDecimals:
1423 * @x: a first decimal value
1424 * @y: a second decimal value
1425 *
1426 * Compare 2 decimals
1427 *
1428 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
1429 */
1430static int
1431xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
1432{
1433 xmlSchemaValPtr swp;
1434 int order = 1;
1435 unsigned long tmp;
1436
1437 if ((x->value.decimal.sign) && (x->value.decimal.sign))
1438 order = -1;
1439 else if (x->value.decimal.sign)
1440 return (-1);
1441 else if (y->value.decimal.sign)
1442 return (1);
1443 if (x->value.decimal.frac == y->value.decimal.frac) {
1444 if (x->value.decimal.base < y->value.decimal.base)
1445 return (-1);
1446 return (x->value.decimal.base > y->value.decimal.base);
1447 }
1448 if (y->value.decimal.frac > x->value.decimal.frac) {
1449 swp = y;
1450 y = x;
1451 x = swp;
1452 order = -order;
1453 }
1454 tmp =
1455 x->value.decimal.base / powten[x->value.decimal.frac -
1456 y->value.decimal.frac];
1457 if (tmp > y->value.decimal.base)
1458 return (order);
1459 if (tmp < y->value.decimal.base)
1460 return (-order);
1461 tmp =
1462 y->value.decimal.base * powten[x->value.decimal.frac -
1463 y->value.decimal.frac];
1464 if (x->value.decimal.base < tmp)
1465 return (-order);
1466 if (x->value.decimal.base == tmp)
1467 return (0);
1468 return (order);
1469}
1470
1471/**
Daniel Veillard070803b2002-05-03 07:29:38 +00001472 * xmlSchemaCompareDurations:
1473 * @x: a first duration value
1474 * @y: a second duration value
1475 *
1476 * Compare 2 durations
1477 *
1478 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
1479 * case of error
1480 */
1481static int
1482xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
1483{
1484 long carry, mon, day;
1485 double sec;
1486 long xmon, xday, myear, lyear, minday, maxday;
1487 static const long dayRange [2][12] = {
1488 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
1489 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
1490
1491 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00001492 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00001493
1494 /* months */
1495 mon = x->value.dur.mon - y->value.dur.mon;
1496
1497 /* seconds */
1498 sec = x->value.dur.sec - y->value.dur.sec;
1499 carry = (long)sec / SECS_PER_DAY;
1500 sec -= (double)(carry * SECS_PER_DAY);
1501
1502 /* days */
1503 day = x->value.dur.day - y->value.dur.day + carry;
1504
1505 /* easy test */
1506 if (mon == 0) {
1507 if (day == 0)
1508 if (sec == 0.0)
1509 return 0;
1510 else if (sec < 0.0)
1511 return -1;
1512 else
1513 return 1;
1514 else if (day < 0)
1515 return -1;
1516 else
1517 return 1;
1518 }
1519
1520 if (mon > 0) {
1521 if ((day >= 0) && (sec >= 0.0))
1522 return 1;
1523 else {
1524 xmon = mon;
1525 xday = -day;
1526 }
1527 } else if ((day <= 0) && (sec <= 0.0)) {
1528 return -1;
1529 } else {
1530 xmon = -mon;
1531 xday = day;
1532 }
1533
1534 myear = xmon / 12;
1535 lyear = myear / 4;
1536 minday = (myear * 365) + (lyear != 0 ? lyear - 1 : 0);
1537 maxday = (myear * 365) + (lyear != 0 ? lyear + 1 : 0);
1538
1539 xmon = xmon % 12;
1540 minday += dayRange[0][xmon];
1541 maxday += dayRange[1][xmon];
1542
1543 if (maxday < xday)
1544 return 1;
1545 else if (minday > xday)
1546 return -1;
1547
1548 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00001549 return 2;
1550}
1551
1552/*
1553 * macros for adding date/times and durations
1554 */
1555#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
1556#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
1557#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
1558#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
1559
1560/**
1561 * _xmlSchemaDateAdd:
1562 * @dt: an #xmlSchemaValPtr
1563 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
1564 *
1565 * Compute a new date/time from @dt and @dur. This function assumes @dt
1566 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
1567 * or #XML_SCHEMAS_GYEAR.
1568 *
1569 * Returns date/time pointer or NULL.
1570 */
1571static xmlSchemaValPtr
1572_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
1573{
1574 xmlSchemaValPtr ret;
1575 long carry, tempdays, temp;
1576 xmlSchemaValDatePtr r, d;
1577 xmlSchemaValDurationPtr u;
1578
1579 if ((dt == NULL) || (dur == NULL))
1580 return NULL;
1581
1582 ret = xmlSchemaNewValue(dt->type);
1583 if (ret == NULL)
1584 return NULL;
1585
1586 r = &(ret->value.date);
1587 d = &(dt->value.date);
1588 u = &(dur->value.dur);
1589
1590 /* normalization */
1591 if (d->mon == 0)
1592 d->mon = 1;
1593
1594 /* normalize for time zone offset */
1595 u->sec -= (d->tzo * 60);
1596 d->tzo = 0;
1597
1598 /* normalization */
1599 if (d->day == 0)
1600 d->day = 1;
1601
1602 /* month */
1603 carry = d->mon + u->mon;
1604 r->mon = MODULO_RANGE(carry, 1, 13);
1605 carry = FQUOTIENT_RANGE(carry, 1, 13);
1606
1607 /* year (may be modified later) */
1608 r->year = d->year + carry;
1609 if (r->year == 0) {
1610 if (d->year > 0)
1611 r->year--;
1612 else
1613 r->year++;
1614 }
1615
1616 /* time zone */
1617 r->tzo = d->tzo;
1618 r->tz_flag = d->tz_flag;
1619
1620 /* seconds */
1621 r->sec = d->sec + u->sec;
1622 carry = FQUOTIENT((long)r->sec, 60);
1623 if (r->sec != 0.0) {
1624 r->sec = MODULO(r->sec, 60.0);
1625 }
1626
1627 /* minute */
1628 carry += d->min;
1629 r->min = MODULO(carry, 60);
1630 carry = FQUOTIENT(carry, 60);
1631
1632 /* hours */
1633 carry += d->hour;
1634 r->hour = MODULO(carry, 24);
1635 carry = FQUOTIENT(carry, 24);
1636
1637 /*
1638 * days
1639 * Note we use tempdays because the temporary values may need more
1640 * than 5 bits
1641 */
1642 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
1643 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
1644 tempdays = MAX_DAYINMONTH(r->year, r->mon);
1645 else if (d->day < 1)
1646 tempdays = 1;
1647 else
1648 tempdays = d->day;
1649
1650 tempdays += u->day + carry;
1651
1652 while (1) {
1653 if (tempdays < 1) {
1654 long tmon = MODULO_RANGE(r->mon-1, 1, 13);
1655 long tyr = r->year + FQUOTIENT_RANGE(r->mon-1, 1, 13);
1656 if (tyr == 0)
1657 tyr--;
1658 tempdays += MAX_DAYINMONTH(tyr, tmon);
1659 carry = -1;
1660 } else if (tempdays > MAX_DAYINMONTH(r->year, r->mon)) {
1661 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
1662 carry = 1;
1663 } else
1664 break;
1665
1666 temp = r->mon + carry;
1667 r->mon = MODULO_RANGE(temp, 1, 13);
1668 r->year = r->year + FQUOTIENT_RANGE(temp, 1, 13);
1669 if (r->year == 0) {
1670 if (temp < 1)
1671 r->year--;
1672 else
1673 r->year++;
1674 }
1675 }
1676
1677 r->day = tempdays;
1678
1679 /*
1680 * adjust the date/time type to the date values
1681 */
1682 if (ret->type != XML_SCHEMAS_DATETIME) {
1683 if ((r->hour) || (r->min) || (r->sec))
1684 ret->type = XML_SCHEMAS_DATETIME;
1685 else if (ret->type != XML_SCHEMAS_DATE) {
1686 if ((r->mon != 1) && (r->day != 1))
1687 ret->type = XML_SCHEMAS_DATE;
1688 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
1689 ret->type = XML_SCHEMAS_GYEARMONTH;
1690 }
1691 }
1692
1693 return ret;
1694}
1695
1696/**
1697 * xmlSchemaDupVal:
1698 * @v: value to duplicate
1699 *
1700 * returns a duplicated value.
1701 */
1702static xmlSchemaValPtr
1703xmlSchemaDupVal (xmlSchemaValPtr v)
1704{
1705 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
1706 if (ret == NULL)
1707 return ret;
1708
1709 memcpy(ret, v, sizeof(xmlSchemaVal));
1710 return ret;
1711}
1712
1713/**
1714 * xmlSchemaDateNormalize:
1715 * @dt: an #xmlSchemaValPtr
1716 *
1717 * Normalize @dt to GMT time.
1718 *
1719 */
1720static xmlSchemaValPtr
1721xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
1722{
1723 xmlSchemaValPtr dur, ret;
1724
1725 if (dt == NULL)
1726 return NULL;
1727
1728 if (((dt->type != XML_SCHEMAS_TIME) &&
1729 (dt->type != XML_SCHEMAS_DATETIME)) || (dt->value.date.tzo == 0))
1730 return xmlSchemaDupVal(dt);
1731
1732 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1733 if (dur == NULL)
1734 return NULL;
1735
1736 dur->value.date.sec -= offset;
1737
1738 ret = _xmlSchemaDateAdd(dt, dur);
1739 if (ret == NULL)
1740 return NULL;
1741
1742 xmlSchemaFreeValue(dur);
1743
1744 /* ret->value.date.tzo = 0; */
1745 return ret;
1746}
1747
1748/**
1749 * _xmlSchemaDateCastYMToDays:
1750 * @dt: an #xmlSchemaValPtr
1751 *
1752 * Convert mon and year of @dt to total number of days. Take the
1753 * number of years since (or before) 1 AD and add the number of leap
1754 * years. This is a function because negative
1755 * years must be handled a little differently and there is no zero year.
1756 *
1757 * Returns number of days.
1758 */
1759static long
1760_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
1761{
1762 long ret;
1763
1764 if (dt->value.date.year < 0)
1765 ret = (dt->value.date.year * 365) +
1766 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
1767 ((dt->value.date.year+1)/400)) +
1768 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
1769 else
1770 ret = ((dt->value.date.year-1) * 365) +
1771 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
1772 ((dt->value.date.year-1)/400)) +
1773 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
1774
1775 return ret;
1776}
1777
1778/**
1779 * TIME_TO_NUMBER:
1780 * @dt: an #xmlSchemaValPtr
1781 *
1782 * Calculates the number of seconds in the time portion of @dt.
1783 *
1784 * Returns seconds.
1785 */
1786#define TIME_TO_NUMBER(dt) \
1787 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
1788 (dt->value.date.min * SECS_PER_MIN)) + dt->value.date.sec)
1789
1790/**
1791 * xmlSchemaCompareDates:
1792 * @x: a first date/time value
1793 * @y: a second date/time value
1794 *
1795 * Compare 2 date/times
1796 *
1797 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
1798 * case of error
1799 */
1800static int
1801xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
1802{
1803 unsigned char xmask, ymask, xor_mask, and_mask;
1804 xmlSchemaValPtr p1, p2, q1, q2;
1805 long p1d, p2d, q1d, q2d;
1806
1807 if ((x == NULL) || (y == NULL))
1808 return -2;
1809
1810 if (x->value.date.tz_flag) {
1811
1812 if (!y->value.date.tz_flag) {
1813 p1 = xmlSchemaDateNormalize(x, 0);
1814 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
1815 /* normalize y + 14:00 */
1816 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
1817
1818 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001819 if (p1d < q1d) {
1820 xmlSchemaFreeValue(p1);
1821 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001822 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001823 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00001824 double sec;
1825
1826 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00001827 if (sec < 0.0) {
1828 xmlSchemaFreeValue(p1);
1829 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001830 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001831 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00001832 /* normalize y - 14:00 */
1833 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
1834 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001835 xmlSchemaFreeValue(p1);
1836 xmlSchemaFreeValue(q1);
1837 xmlSchemaFreeValue(q2);
Daniel Veillard5a872412002-05-22 06:40:27 +00001838 if (p1d > q2d)
1839 return 1;
1840 else if (p1d == q2d) {
1841 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
1842 if (sec > 0.0)
1843 return 1;
1844 else
1845 return 2; /* indeterminate */
1846 }
1847 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00001848 } else {
1849 xmlSchemaFreeValue(p1);
1850 xmlSchemaFreeValue(q1);
1851 }
Daniel Veillard5a872412002-05-22 06:40:27 +00001852 }
1853 } else if (y->value.date.tz_flag) {
1854 q1 = xmlSchemaDateNormalize(y, 0);
1855 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
1856
1857 /* normalize x - 14:00 */
1858 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
1859 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
1860
Daniel Veillardfdc91562002-07-01 21:52:03 +00001861 if (p1d < q1d) {
1862 xmlSchemaFreeValue(p1);
1863 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001864 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001865 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00001866 double sec;
1867
1868 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00001869 if (sec < 0.0) {
1870 xmlSchemaFreeValue(p1);
1871 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001872 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001873 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00001874 /* normalize x + 14:00 */
1875 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
1876 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
1877
Daniel Veillardfdc91562002-07-01 21:52:03 +00001878 xmlSchemaFreeValue(p1);
1879 xmlSchemaFreeValue(q1);
1880 xmlSchemaFreeValue(p2);
Daniel Veillard5a872412002-05-22 06:40:27 +00001881 if (p2d > q1d)
1882 return 1;
1883 else if (p2d == q1d) {
1884 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
1885 if (sec > 0.0)
1886 return 1;
1887 else
1888 return 2; /* indeterminate */
1889 }
1890 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00001891 } else {
1892 xmlSchemaFreeValue(p1);
1893 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001894 }
1895 }
1896
1897 /*
1898 * if the same type then calculate the difference
1899 */
1900 if (x->type == y->type) {
1901 q1 = xmlSchemaDateNormalize(y, 0);
1902 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
1903
1904 p1 = xmlSchemaDateNormalize(x, 0);
1905 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
1906
Daniel Veillardfdc91562002-07-01 21:52:03 +00001907 if (p1d < q1d) {
1908 xmlSchemaFreeValue(p1);
1909 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001910 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001911 } else if (p1d > q1d) {
1912 xmlSchemaFreeValue(p1);
1913 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001914 return 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001915 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00001916 double sec;
1917
1918 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00001919 xmlSchemaFreeValue(p1);
1920 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001921 if (sec < 0.0)
1922 return -1;
1923 else if (sec > 0.0)
1924 return 1;
1925
1926 }
1927 return 0;
1928 }
1929
1930 switch (x->type) {
1931 case XML_SCHEMAS_DATETIME:
1932 xmask = 0xf;
1933 break;
1934 case XML_SCHEMAS_DATE:
1935 xmask = 0x7;
1936 break;
1937 case XML_SCHEMAS_GYEAR:
1938 xmask = 0x1;
1939 break;
1940 case XML_SCHEMAS_GMONTH:
1941 xmask = 0x2;
1942 break;
1943 case XML_SCHEMAS_GDAY:
1944 xmask = 0x3;
1945 break;
1946 case XML_SCHEMAS_GYEARMONTH:
1947 xmask = 0x3;
1948 break;
1949 case XML_SCHEMAS_GMONTHDAY:
1950 xmask = 0x6;
1951 break;
1952 case XML_SCHEMAS_TIME:
1953 xmask = 0x8;
1954 break;
1955 default:
1956 xmask = 0;
1957 break;
1958 }
1959
1960 switch (y->type) {
1961 case XML_SCHEMAS_DATETIME:
1962 ymask = 0xf;
1963 break;
1964 case XML_SCHEMAS_DATE:
1965 ymask = 0x7;
1966 break;
1967 case XML_SCHEMAS_GYEAR:
1968 ymask = 0x1;
1969 break;
1970 case XML_SCHEMAS_GMONTH:
1971 ymask = 0x2;
1972 break;
1973 case XML_SCHEMAS_GDAY:
1974 ymask = 0x3;
1975 break;
1976 case XML_SCHEMAS_GYEARMONTH:
1977 ymask = 0x3;
1978 break;
1979 case XML_SCHEMAS_GMONTHDAY:
1980 ymask = 0x6;
1981 break;
1982 case XML_SCHEMAS_TIME:
1983 ymask = 0x8;
1984 break;
1985 default:
1986 ymask = 0;
1987 break;
1988 }
1989
1990 xor_mask = xmask ^ ymask; /* mark type differences */
1991 and_mask = xmask & ymask; /* mark field specification */
1992
1993 /* year */
1994 if (xor_mask & 1)
1995 return 2; /* indeterminate */
1996 else if (and_mask & 1) {
1997 if (x->value.date.year < y->value.date.year)
1998 return -1;
1999 else if (x->value.date.year > y->value.date.year)
2000 return 1;
2001 }
2002
2003 /* month */
2004 if (xor_mask & 2)
2005 return 2; /* indeterminate */
2006 else if (and_mask & 2) {
2007 if (x->value.date.mon < y->value.date.mon)
2008 return -1;
2009 else if (x->value.date.mon > y->value.date.mon)
2010 return 1;
2011 }
2012
2013 /* day */
2014 if (xor_mask & 4)
2015 return 2; /* indeterminate */
2016 else if (and_mask & 4) {
2017 if (x->value.date.day < y->value.date.day)
2018 return -1;
2019 else if (x->value.date.day > y->value.date.day)
2020 return 1;
2021 }
2022
2023 /* time */
2024 if (xor_mask & 8)
2025 return 2; /* indeterminate */
2026 else if (and_mask & 8) {
2027 if (x->value.date.hour < y->value.date.hour)
2028 return -1;
2029 else if (x->value.date.hour > y->value.date.hour)
2030 return 1;
2031 else if (x->value.date.min < y->value.date.min)
2032 return -1;
2033 else if (x->value.date.min > y->value.date.min)
2034 return 1;
2035 else if (x->value.date.sec < y->value.date.sec)
2036 return -1;
2037 else if (x->value.date.sec > y->value.date.sec)
2038 return 1;
2039 }
2040
Daniel Veillard070803b2002-05-03 07:29:38 +00002041 return 0;
2042}
2043
2044/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002045 * xmlSchemaCompareValues:
2046 * @x: a first value
2047 * @y: a second value
2048 *
2049 * Compare 2 values
2050 *
Daniel Veillard5a872412002-05-22 06:40:27 +00002051 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2052 * case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00002053 */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002054static int
Daniel Veillard4255d502002-04-16 15:50:10 +00002055xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
2056 if ((x == NULL) || (y == NULL))
2057 return(-2);
2058
2059 switch (x->type) {
2060 case XML_SCHEMAS_STRING:
2061 TODO
2062 case XML_SCHEMAS_DECIMAL:
2063 if (y->type == XML_SCHEMAS_DECIMAL)
2064 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00002065 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00002066 case XML_SCHEMAS_DURATION:
2067 if (y->type == XML_SCHEMAS_DURATION)
2068 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00002069 return(-2);
2070 case XML_SCHEMAS_TIME:
2071 case XML_SCHEMAS_GDAY:
2072 case XML_SCHEMAS_GMONTH:
2073 case XML_SCHEMAS_GMONTHDAY:
2074 case XML_SCHEMAS_GYEAR:
2075 case XML_SCHEMAS_GYEARMONTH:
2076 case XML_SCHEMAS_DATE:
2077 case XML_SCHEMAS_DATETIME:
2078 if ((y->type == XML_SCHEMAS_DATETIME) ||
2079 (y->type == XML_SCHEMAS_TIME) ||
2080 (y->type == XML_SCHEMAS_GDAY) ||
2081 (y->type == XML_SCHEMAS_GMONTH) ||
2082 (y->type == XML_SCHEMAS_GMONTHDAY) ||
2083 (y->type == XML_SCHEMAS_GYEAR) ||
2084 (y->type == XML_SCHEMAS_DATE) ||
2085 (y->type == XML_SCHEMAS_GYEARMONTH))
2086 return (xmlSchemaCompareDates(x, y));
2087
2088 return (-2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002089 default:
2090 TODO
2091 }
Daniel Veillard5a872412002-05-22 06:40:27 +00002092 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00002093}
2094
2095/**
2096 * xmlSchemaValidateFacet:
Daniel Veillard01c13b52002-12-10 15:19:08 +00002097 * @base: the base type
Daniel Veillard4255d502002-04-16 15:50:10 +00002098 * @facet: the facet to check
2099 * @value: the lexical repr of the value to validate
2100 * @val: the precomputed value
2101 *
2102 * Check a value against a facet condition
2103 *
2104 * Returns 0 if the element is schemas valid, a positive error code
2105 * number otherwise and -1 in case of internal or API error.
2106 */
2107int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00002108xmlSchemaValidateFacet(xmlSchemaTypePtr base ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +00002109 xmlSchemaFacetPtr facet,
Daniel Veillard4255d502002-04-16 15:50:10 +00002110 const xmlChar *value, xmlSchemaValPtr val)
2111{
2112 int ret;
2113
2114 switch (facet->type) {
2115 case XML_SCHEMA_FACET_PATTERN:
2116 ret = xmlRegexpExec(facet->regexp, value);
2117 if (ret == 1)
2118 return(0);
2119 if (ret == 0) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002120 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00002121 return(1);
2122 }
2123 return(ret);
2124 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
2125 ret = xmlSchemaCompareValues(val, facet->val);
2126 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002127 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00002128 return(-1);
2129 }
2130 if (ret == -1)
2131 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00002132 /* error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00002133 return(1);
Daniel Veillard070803b2002-05-03 07:29:38 +00002134 case XML_SCHEMA_FACET_MAXINCLUSIVE:
2135 ret = xmlSchemaCompareValues(val, facet->val);
2136 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002137 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002138 return(-1);
2139 }
2140 if ((ret == -1) || (ret == 0))
2141 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00002142 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002143 return(1);
2144 case XML_SCHEMA_FACET_MINEXCLUSIVE:
2145 ret = xmlSchemaCompareValues(val, facet->val);
2146 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002147 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002148 return(-1);
2149 }
2150 if (ret == 1)
2151 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00002152 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002153 return(1);
2154 case XML_SCHEMA_FACET_MININCLUSIVE:
2155 ret = xmlSchemaCompareValues(val, facet->val);
2156 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002157 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002158 return(-1);
2159 }
2160 if ((ret == 1) || (ret == 0))
2161 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00002162 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002163 return(1);
Daniel Veillard8651f532002-04-17 09:06:27 +00002164 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002165 /* TODO whitespaces */
Daniel Veillard8651f532002-04-17 09:06:27 +00002166 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00002167 case XML_SCHEMA_FACET_ENUMERATION:
2168 if ((facet->value != NULL) &&
2169 (xmlStrEqual(facet->value, value)))
2170 return(0);
2171 return(1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002172 case XML_SCHEMA_FACET_LENGTH:
2173 case XML_SCHEMA_FACET_MAXLENGTH:
2174 case XML_SCHEMA_FACET_MINLENGTH: {
2175 unsigned int len = 0;
2176
2177 if ((facet->val == NULL) ||
2178 (facet->val->type != XML_SCHEMAS_DECIMAL) ||
2179 (facet->val->value.decimal.frac != 0)) {
2180 return(-1);
2181 }
2182 switch (base->flags) {
2183 case XML_SCHEMAS_STRING:
2184 case XML_SCHEMAS_NORMSTRING:
2185 case XML_SCHEMAS_TOKEN:
2186 case XML_SCHEMAS_LANGUAGE:
2187 case XML_SCHEMAS_NMTOKEN:
2188 case XML_SCHEMAS_NAME:
2189 case XML_SCHEMAS_NCNAME:
2190 case XML_SCHEMAS_ID:
2191 case XML_SCHEMAS_IDREF: {
2192 len = xmlUTF8Strlen(value);
2193 break;
2194 }
2195 default:
2196 TODO
2197 }
2198 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
2199 if (len != facet->val->value.decimal.base)
2200 return(1);
2201 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
2202 if (len < facet->val->value.decimal.base)
2203 return(1);
2204 } else {
2205 if (len > facet->val->value.decimal.base)
2206 return(1);
2207 }
2208 break;
2209 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002210 default:
2211 TODO
2212 }
2213 return(0);
2214}
2215
2216#endif /* LIBXML_SCHEMAS_ENABLED */