blob: a477bccf745bdfc02ee2f4a8932ece3a17ace4c8 [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
Daniel Veillard5f704af2003-03-05 10:01:43 +000088static unsigned long powten[10] = {
Daniel Veillard4255d502002-04-16 15:50:10 +000089 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 Veillardc3da18a2003-03-18 00:31:04 +00001060 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00001061 * @type: the predefined type
1062 * @value: the value to check
1063 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001064 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00001065 *
1066 * Check that a value conforms to the lexical space of the predefined type.
1067 * if true a value is computed and returned in @val.
1068 *
1069 * Returns 0 if this validates, a positive error code number otherwise
1070 * and -1 in case of internal or API error.
1071 */
1072int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001073xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
1074 xmlSchemaValPtr *val, xmlNodePtr node) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001075 xmlSchemaValPtr v;
Daniel Veillard96a4b252003-02-06 08:22:32 +00001076 int ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00001077
1078 if (xmlSchemaTypesInitialized == 0)
1079 return(-1);
1080 if (type == NULL)
1081 return(-1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001082
Daniel Veillard4255d502002-04-16 15:50:10 +00001083 if (val != NULL)
1084 *val = NULL;
1085 if (type == xmlSchemaTypeStringDef) {
1086 return(0);
1087 } else if (type == xmlSchemaTypeAnyTypeDef) {
1088 return(0);
1089 } else if (type == xmlSchemaTypeAnySimpleTypeDef) {
1090 return(0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00001091 } else if (type == xmlSchemaTypeNmtokenDef) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001092 if (xmlValidateNmtokenValue(value))
1093 return(0);
1094 return(1);
1095 } else if (type == xmlSchemaTypeDecimalDef) {
1096 const xmlChar *cur = value, *tmp;
Daniel Veillard5a872412002-05-22 06:40:27 +00001097 int frac = 0, len, neg = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001098 unsigned long base = 0;
1099 if (cur == NULL)
1100 return(1);
1101 if (*cur == '+')
1102 cur++;
1103 else if (*cur == '-') {
1104 neg = 1;
1105 cur++;
1106 }
1107 tmp = cur;
1108 while ((*cur >= '0') && (*cur <= '9')) {
1109 base = base * 10 + (*cur - '0');
1110 cur++;
1111 }
Daniel Veillard5a872412002-05-22 06:40:27 +00001112 len = cur - tmp;
Daniel Veillard4255d502002-04-16 15:50:10 +00001113 if (*cur == '.') {
1114 cur++;
1115 tmp = cur;
1116 while ((*cur >= '0') && (*cur <= '9')) {
1117 base = base * 10 + (*cur - '0');
1118 cur++;
1119 }
1120 frac = cur - tmp;
1121 }
1122 if (*cur != 0)
1123 return(1);
1124 if (val != NULL) {
1125 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1126 if (v != NULL) {
1127 v->value.decimal.base = base;
1128 v->value.decimal.sign = neg;
1129 v->value.decimal.frac = frac;
Daniel Veillard5a872412002-05-22 06:40:27 +00001130 v->value.decimal.total = frac + len;
Daniel Veillard4255d502002-04-16 15:50:10 +00001131 *val = v;
1132 }
1133 }
1134 return(0);
Daniel Veillard070803b2002-05-03 07:29:38 +00001135 } else if (type == xmlSchemaTypeDurationDef) {
Daniel Veillard5a872412002-05-22 06:40:27 +00001136 return xmlSchemaValidateDuration(type, value, val);
Daniel Veillard070803b2002-05-03 07:29:38 +00001137 } else if ((type == xmlSchemaTypeDatetimeDef) ||
1138 (type == xmlSchemaTypeTimeDef) ||
1139 (type == xmlSchemaTypeDateDef) ||
1140 (type == xmlSchemaTypeGYearDef) ||
1141 (type == xmlSchemaTypeGYearMonthDef) ||
1142 (type == xmlSchemaTypeGMonthDef) ||
1143 (type == xmlSchemaTypeGMonthDayDef) ||
1144 (type == xmlSchemaTypeGDayDef)) {
Daniel Veillard5a872412002-05-22 06:40:27 +00001145 return xmlSchemaValidateDates(type, value, val);
Daniel Veillard4255d502002-04-16 15:50:10 +00001146 } else if (type == xmlSchemaTypePositiveIntegerDef) {
1147 const xmlChar *cur = value;
1148 unsigned long base = 0;
1149 int total = 0;
1150 if (cur == NULL)
1151 return(1);
1152 if (*cur == '+')
1153 cur++;
1154 while ((*cur >= '0') && (*cur <= '9')) {
1155 base = base * 10 + (*cur - '0');
1156 total++;
1157 cur++;
1158 }
1159 if (*cur != 0)
1160 return(1);
1161 if (val != NULL) {
1162 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1163 if (v != NULL) {
1164 v->value.decimal.base = base;
1165 v->value.decimal.sign = 0;
1166 v->value.decimal.frac = 0;
1167 v->value.decimal.total = total;
1168 *val = v;
1169 }
1170 }
1171 return(0);
1172 } else if (type == xmlSchemaTypeNonNegativeIntegerDef) {
1173 const xmlChar *cur = value;
1174 unsigned long base = 0;
1175 int total = 0;
1176 int sign = 0;
1177 if (cur == NULL)
1178 return(1);
1179 if (*cur == '-') {
1180 sign = 1;
1181 cur++;
1182 } else if (*cur == '+')
1183 cur++;
1184 while ((*cur >= '0') && (*cur <= '9')) {
1185 base = base * 10 + (*cur - '0');
1186 total++;
1187 cur++;
1188 }
1189 if (*cur != 0)
1190 return(1);
1191 if ((sign == 1) && (base != 0))
1192 return(1);
1193 if (val != NULL) {
1194 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1195 if (v != NULL) {
1196 v->value.decimal.base = base;
1197 v->value.decimal.sign = 0;
1198 v->value.decimal.frac = 0;
1199 v->value.decimal.total = total;
1200 *val = v;
1201 }
1202 }
1203 return(0);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001204 } else if (type == xmlSchemaTypeIntDef) {
1205 const xmlChar *cur = value;
Daniel Veillard84d70a42002-09-16 10:51:38 +00001206 unsigned long base = 0;
Daniel Veillard96a4b252003-02-06 08:22:32 +00001207 int total = 0;
1208 int sign = 0;
Daniel Veillard84d70a42002-09-16 10:51:38 +00001209 if (cur == NULL)
1210 return(1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001211 if (*cur == '-') {
1212 sign = 1;
1213 cur++;
1214 } else if (*cur == '+')
1215 cur++;
1216 while (*cur == '0') {
1217 total++;
1218 cur++;
1219 }
1220 while ((*cur >= '0') && (*cur <= '9')) {
1221 base = base * 10 + (*cur - '0');
1222 total++;
1223 cur++;
1224 }
1225 if (*cur != 0)
1226 return(1);
1227 if ((sign == 1) && (total == 0))
1228 return(1);
1229 if (val != NULL) {
1230 v = xmlSchemaNewValue(XML_SCHEMAS_INT);
1231 if (v != NULL) {
1232 v->value.decimal.base = base;
1233 v->value.decimal.sign = sign;
1234 v->value.decimal.frac = 0;
1235 v->value.decimal.total = total;
1236 *val = v;
1237 }
1238 }
1239 return(0);
1240 } else if ((type == xmlSchemaTypeFloatDef) ||
1241 (type == xmlSchemaTypeDoubleDef)) {
1242 const xmlChar *cur = value;
1243 int neg = 0;
1244 if (cur == NULL)
1245 return(1);
1246 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
1247 cur += 3;
1248 if (*cur != 0)
1249 return(1);
1250 if (val != NULL) {
1251 if (type == xmlSchemaTypeFloatDef) {
1252 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1253 if (v != NULL) {
1254 v->value.f = (float) xmlXPathNAN;
1255 } else {
1256 xmlSchemaFreeValue(v);
1257 return(-1);
1258 }
1259 } else {
1260 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1261 if (v != NULL) {
1262 v->value.d = xmlXPathNAN;
1263 } else {
1264 xmlSchemaFreeValue(v);
1265 return(-1);
1266 }
1267 }
1268 *val = v;
1269 }
1270 return(0);
1271 }
Daniel Veillard84d70a42002-09-16 10:51:38 +00001272 if (*cur == '+')
1273 cur++;
1274 else if (*cur == '-') {
1275 neg = 1;
1276 cur++;
1277 }
Daniel Veillardd4310742003-02-18 21:12:46 +00001278 if (cur[0] == 0)
1279 return(1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001280 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
1281 cur += 3;
1282 if (*cur != 0)
1283 return(1);
1284 if (val != NULL) {
1285 if (type == xmlSchemaTypeFloatDef) {
1286 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1287 if (v != NULL) {
1288 if (neg)
1289 v->value.f = (float) xmlXPathNINF;
1290 else
1291 v->value.f = (float) xmlXPathPINF;
1292 } else {
1293 xmlSchemaFreeValue(v);
1294 return(-1);
1295 }
1296 } else {
1297 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1298 if (v != NULL) {
1299 if (neg)
1300 v->value.d = xmlXPathNINF;
1301 else
1302 v->value.d = xmlXPathPINF;
1303 } else {
1304 xmlSchemaFreeValue(v);
1305 return(-1);
1306 }
1307 }
1308 *val = v;
1309 }
1310 return(0);
1311 }
Daniel Veillard84d70a42002-09-16 10:51:38 +00001312 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard84d70a42002-09-16 10:51:38 +00001313 cur++;
1314 }
Daniel Veillard84d70a42002-09-16 10:51:38 +00001315 if (*cur == '.') {
1316 cur++;
Daniel Veillard96a4b252003-02-06 08:22:32 +00001317 while ((*cur >= '0') && (*cur <= '9'))
Daniel Veillard84d70a42002-09-16 10:51:38 +00001318 cur++;
Daniel Veillard84d70a42002-09-16 10:51:38 +00001319 }
Daniel Veillard96a4b252003-02-06 08:22:32 +00001320 if ((*cur == 'e') || (*cur == 'E')) {
1321 cur++;
1322 if (*cur == '-')
1323 cur++;
1324 while ((*cur >= '0') && (*cur <= '9'))
1325 cur++;
1326 }
1327 if (*cur != 0)
1328 return(1);
1329 if (val != NULL) {
1330 if (type == xmlSchemaTypeFloatDef) {
1331 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1332 if (v != NULL) {
1333 if (sscanf((const char *)value, "%f", &(v->value.f))==1) {
1334 *val = v;
1335 } else {
1336 xmlGenericError(xmlGenericErrorContext,
1337 "failed to scanf float %s\n", value);
1338 xmlSchemaFreeValue(v);
1339 return(1);
1340 }
1341 } else {
1342 return(-1);
1343 }
1344 } else {
1345 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1346 if (v != NULL) {
1347 if (sscanf((const char *)value, "%lf", &(v->value.d))==1) {
1348 *val = v;
1349 } else {
1350 xmlGenericError(xmlGenericErrorContext,
1351 "failed to scanf double %s\n", value);
1352 xmlSchemaFreeValue(v);
1353 return(1);
1354 }
1355 } else {
1356 return(-1);
1357 }
1358 }
1359 }
Daniel Veillardb5c05732002-09-20 13:36:25 +00001360 return(0);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001361 } else if (type == xmlSchemaTypeNameDef) {
Daniel Veillardd2298792003-02-14 16:54:11 +00001362 ret = xmlValidateName(value, 1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001363 if ((ret == 0) && (val != NULL)) {
1364 TODO;
1365 }
1366 return(ret);
1367 } else if (type == xmlSchemaTypeQNameDef) {
Daniel Veillardd2298792003-02-14 16:54:11 +00001368 ret = xmlValidateQName(value, 1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001369 if ((ret == 0) && (val != NULL)) {
1370 TODO;
1371 }
1372 return(ret);
1373 } else if (type == xmlSchemaTypeNCNameDef) {
Daniel Veillardd2298792003-02-14 16:54:11 +00001374 ret = xmlValidateNCName(value, 1);
Daniel Veillard96a4b252003-02-06 08:22:32 +00001375 if ((ret == 0) && (val != NULL)) {
1376 TODO;
1377 }
1378 return(ret);
1379 } else if (type == xmlSchemaTypeAnyURIDef) {
1380 xmlURIPtr uri;
1381
1382 uri = xmlParseURI((const char *) value);
1383 if (uri == NULL)
1384 return(1);
1385 if (val != NULL) {
1386 TODO;
1387 }
1388 xmlFreeURI(uri);
Daniel Veillardb5c05732002-09-20 13:36:25 +00001389 return(0);
Daniel Veillardc5a70f22003-02-06 23:41:59 +00001390 } else if (type == xmlSchemaTypeBooleanDef) {
1391 const xmlChar *cur = value;
1392
1393 if ((cur[0] == '0') && (cur[1] == 0))
1394 ret = 0;
1395 else if ((cur[0] == '1') && (cur[1] == 0))
1396 ret = 1;
1397 else if ((cur[0] == 't') && (cur[1] == 'r') && (cur[2] == 'u') &&
1398 (cur[3] == 'e') && (cur[4] == 0))
1399 ret = 1;
1400 else if ((cur[0] == 'f') && (cur[1] == 'a') && (cur[2] == 'l') &&
1401 (cur[3] == 's') && (cur[4] == 'e') && (cur[5] == 0))
1402 ret = 0;
1403 else
1404 return(1);
1405 if (val != NULL) {
1406 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
1407 if (v != NULL) {
1408 v->value.b = ret;
1409 *val = v;
1410 } else {
1411 return(-1);
1412 }
1413 }
1414 return(0);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001415 } else if (type == xmlSchemaTypeIdrefDef) {
1416 ret = xmlValidateNCName(value, 1);
1417 if ((ret == 0) && (val != NULL)) {
1418 TODO;
1419 }
1420 if ((ret == 0) && (node != NULL) &&
1421 (node->type == XML_ATTRIBUTE_NODE)) {
1422 xmlAttrPtr attr = (xmlAttrPtr) node;
1423
1424 xmlAddRef(NULL, node->doc, value, attr);
1425 attr->atype = XML_ATTRIBUTE_IDREF;
1426 }
1427 return(ret);
1428 } else if (type == xmlSchemaTypeIdDef) {
1429 ret = xmlValidateNCName(value, 1);
1430 if ((ret == 0) && (val != NULL)) {
1431 TODO;
1432 }
1433 if ((ret == 0) && (node != NULL) &&
1434 (node->type == XML_ATTRIBUTE_NODE)) {
1435 xmlAttrPtr attr = (xmlAttrPtr) node;
1436 xmlIDPtr res;
1437
1438 res = xmlAddID(NULL, node->doc, value, attr);
1439 if (res == NULL) {
1440 ret = 2;
1441 } else {
1442 attr->atype = XML_ATTRIBUTE_ID;
1443 }
1444 }
1445 return(ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00001446 } else {
1447 TODO
1448 return(0);
1449 }
Daniel Veillard96a4b252003-02-06 08:22:32 +00001450 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001451}
1452
1453/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001454 * xmlSchemaValidatePredefinedType:
1455 * @type: the predefined type
1456 * @value: the value to check
1457 * @val: the return computed value
1458 *
1459 * Check that a value conforms to the lexical space of the predefined type.
1460 * if true a value is computed and returned in @val.
1461 *
1462 * Returns 0 if this validates, a positive error code number otherwise
1463 * and -1 in case of internal or API error.
1464 */
1465int
1466xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
1467 xmlSchemaValPtr *val) {
1468 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
1469}
1470
1471/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001472 * xmlSchemaCompareDecimals:
1473 * @x: a first decimal value
1474 * @y: a second decimal value
1475 *
1476 * Compare 2 decimals
1477 *
1478 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
1479 */
1480static int
1481xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
1482{
1483 xmlSchemaValPtr swp;
1484 int order = 1;
1485 unsigned long tmp;
1486
1487 if ((x->value.decimal.sign) && (x->value.decimal.sign))
1488 order = -1;
1489 else if (x->value.decimal.sign)
1490 return (-1);
1491 else if (y->value.decimal.sign)
1492 return (1);
1493 if (x->value.decimal.frac == y->value.decimal.frac) {
1494 if (x->value.decimal.base < y->value.decimal.base)
1495 return (-1);
1496 return (x->value.decimal.base > y->value.decimal.base);
1497 }
1498 if (y->value.decimal.frac > x->value.decimal.frac) {
1499 swp = y;
1500 y = x;
1501 x = swp;
1502 order = -order;
1503 }
1504 tmp =
1505 x->value.decimal.base / powten[x->value.decimal.frac -
1506 y->value.decimal.frac];
1507 if (tmp > y->value.decimal.base)
1508 return (order);
1509 if (tmp < y->value.decimal.base)
1510 return (-order);
1511 tmp =
1512 y->value.decimal.base * powten[x->value.decimal.frac -
1513 y->value.decimal.frac];
1514 if (x->value.decimal.base < tmp)
1515 return (-order);
1516 if (x->value.decimal.base == tmp)
1517 return (0);
1518 return (order);
1519}
1520
1521/**
Daniel Veillard070803b2002-05-03 07:29:38 +00001522 * xmlSchemaCompareDurations:
1523 * @x: a first duration value
1524 * @y: a second duration value
1525 *
1526 * Compare 2 durations
1527 *
1528 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
1529 * case of error
1530 */
1531static int
1532xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
1533{
1534 long carry, mon, day;
1535 double sec;
1536 long xmon, xday, myear, lyear, minday, maxday;
1537 static const long dayRange [2][12] = {
1538 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
1539 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
1540
1541 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00001542 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00001543
1544 /* months */
1545 mon = x->value.dur.mon - y->value.dur.mon;
1546
1547 /* seconds */
1548 sec = x->value.dur.sec - y->value.dur.sec;
1549 carry = (long)sec / SECS_PER_DAY;
1550 sec -= (double)(carry * SECS_PER_DAY);
1551
1552 /* days */
1553 day = x->value.dur.day - y->value.dur.day + carry;
1554
1555 /* easy test */
1556 if (mon == 0) {
1557 if (day == 0)
1558 if (sec == 0.0)
1559 return 0;
1560 else if (sec < 0.0)
1561 return -1;
1562 else
1563 return 1;
1564 else if (day < 0)
1565 return -1;
1566 else
1567 return 1;
1568 }
1569
1570 if (mon > 0) {
1571 if ((day >= 0) && (sec >= 0.0))
1572 return 1;
1573 else {
1574 xmon = mon;
1575 xday = -day;
1576 }
1577 } else if ((day <= 0) && (sec <= 0.0)) {
1578 return -1;
1579 } else {
1580 xmon = -mon;
1581 xday = day;
1582 }
1583
1584 myear = xmon / 12;
1585 lyear = myear / 4;
1586 minday = (myear * 365) + (lyear != 0 ? lyear - 1 : 0);
1587 maxday = (myear * 365) + (lyear != 0 ? lyear + 1 : 0);
1588
1589 xmon = xmon % 12;
1590 minday += dayRange[0][xmon];
1591 maxday += dayRange[1][xmon];
1592
1593 if (maxday < xday)
1594 return 1;
1595 else if (minday > xday)
1596 return -1;
1597
1598 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00001599 return 2;
1600}
1601
1602/*
1603 * macros for adding date/times and durations
1604 */
1605#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
1606#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
1607#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
1608#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
1609
1610/**
1611 * _xmlSchemaDateAdd:
1612 * @dt: an #xmlSchemaValPtr
1613 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
1614 *
1615 * Compute a new date/time from @dt and @dur. This function assumes @dt
1616 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
1617 * or #XML_SCHEMAS_GYEAR.
1618 *
1619 * Returns date/time pointer or NULL.
1620 */
1621static xmlSchemaValPtr
1622_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
1623{
1624 xmlSchemaValPtr ret;
1625 long carry, tempdays, temp;
1626 xmlSchemaValDatePtr r, d;
1627 xmlSchemaValDurationPtr u;
1628
1629 if ((dt == NULL) || (dur == NULL))
1630 return NULL;
1631
1632 ret = xmlSchemaNewValue(dt->type);
1633 if (ret == NULL)
1634 return NULL;
1635
1636 r = &(ret->value.date);
1637 d = &(dt->value.date);
1638 u = &(dur->value.dur);
1639
1640 /* normalization */
1641 if (d->mon == 0)
1642 d->mon = 1;
1643
1644 /* normalize for time zone offset */
1645 u->sec -= (d->tzo * 60);
1646 d->tzo = 0;
1647
1648 /* normalization */
1649 if (d->day == 0)
1650 d->day = 1;
1651
1652 /* month */
1653 carry = d->mon + u->mon;
1654 r->mon = MODULO_RANGE(carry, 1, 13);
1655 carry = FQUOTIENT_RANGE(carry, 1, 13);
1656
1657 /* year (may be modified later) */
1658 r->year = d->year + carry;
1659 if (r->year == 0) {
1660 if (d->year > 0)
1661 r->year--;
1662 else
1663 r->year++;
1664 }
1665
1666 /* time zone */
1667 r->tzo = d->tzo;
1668 r->tz_flag = d->tz_flag;
1669
1670 /* seconds */
1671 r->sec = d->sec + u->sec;
1672 carry = FQUOTIENT((long)r->sec, 60);
1673 if (r->sec != 0.0) {
1674 r->sec = MODULO(r->sec, 60.0);
1675 }
1676
1677 /* minute */
1678 carry += d->min;
1679 r->min = MODULO(carry, 60);
1680 carry = FQUOTIENT(carry, 60);
1681
1682 /* hours */
1683 carry += d->hour;
1684 r->hour = MODULO(carry, 24);
1685 carry = FQUOTIENT(carry, 24);
1686
1687 /*
1688 * days
1689 * Note we use tempdays because the temporary values may need more
1690 * than 5 bits
1691 */
1692 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
1693 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
1694 tempdays = MAX_DAYINMONTH(r->year, r->mon);
1695 else if (d->day < 1)
1696 tempdays = 1;
1697 else
1698 tempdays = d->day;
1699
1700 tempdays += u->day + carry;
1701
1702 while (1) {
1703 if (tempdays < 1) {
1704 long tmon = MODULO_RANGE(r->mon-1, 1, 13);
1705 long tyr = r->year + FQUOTIENT_RANGE(r->mon-1, 1, 13);
1706 if (tyr == 0)
1707 tyr--;
1708 tempdays += MAX_DAYINMONTH(tyr, tmon);
1709 carry = -1;
1710 } else if (tempdays > MAX_DAYINMONTH(r->year, r->mon)) {
1711 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
1712 carry = 1;
1713 } else
1714 break;
1715
1716 temp = r->mon + carry;
1717 r->mon = MODULO_RANGE(temp, 1, 13);
1718 r->year = r->year + FQUOTIENT_RANGE(temp, 1, 13);
1719 if (r->year == 0) {
1720 if (temp < 1)
1721 r->year--;
1722 else
1723 r->year++;
1724 }
1725 }
1726
1727 r->day = tempdays;
1728
1729 /*
1730 * adjust the date/time type to the date values
1731 */
1732 if (ret->type != XML_SCHEMAS_DATETIME) {
1733 if ((r->hour) || (r->min) || (r->sec))
1734 ret->type = XML_SCHEMAS_DATETIME;
1735 else if (ret->type != XML_SCHEMAS_DATE) {
1736 if ((r->mon != 1) && (r->day != 1))
1737 ret->type = XML_SCHEMAS_DATE;
1738 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
1739 ret->type = XML_SCHEMAS_GYEARMONTH;
1740 }
1741 }
1742
1743 return ret;
1744}
1745
1746/**
1747 * xmlSchemaDupVal:
1748 * @v: value to duplicate
1749 *
1750 * returns a duplicated value.
1751 */
1752static xmlSchemaValPtr
1753xmlSchemaDupVal (xmlSchemaValPtr v)
1754{
1755 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
1756 if (ret == NULL)
1757 return ret;
1758
1759 memcpy(ret, v, sizeof(xmlSchemaVal));
1760 return ret;
1761}
1762
1763/**
1764 * xmlSchemaDateNormalize:
1765 * @dt: an #xmlSchemaValPtr
1766 *
1767 * Normalize @dt to GMT time.
1768 *
1769 */
1770static xmlSchemaValPtr
1771xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
1772{
1773 xmlSchemaValPtr dur, ret;
1774
1775 if (dt == NULL)
1776 return NULL;
1777
1778 if (((dt->type != XML_SCHEMAS_TIME) &&
1779 (dt->type != XML_SCHEMAS_DATETIME)) || (dt->value.date.tzo == 0))
1780 return xmlSchemaDupVal(dt);
1781
1782 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1783 if (dur == NULL)
1784 return NULL;
1785
1786 dur->value.date.sec -= offset;
1787
1788 ret = _xmlSchemaDateAdd(dt, dur);
1789 if (ret == NULL)
1790 return NULL;
1791
1792 xmlSchemaFreeValue(dur);
1793
1794 /* ret->value.date.tzo = 0; */
1795 return ret;
1796}
1797
1798/**
1799 * _xmlSchemaDateCastYMToDays:
1800 * @dt: an #xmlSchemaValPtr
1801 *
1802 * Convert mon and year of @dt to total number of days. Take the
1803 * number of years since (or before) 1 AD and add the number of leap
1804 * years. This is a function because negative
1805 * years must be handled a little differently and there is no zero year.
1806 *
1807 * Returns number of days.
1808 */
1809static long
1810_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
1811{
1812 long ret;
1813
1814 if (dt->value.date.year < 0)
1815 ret = (dt->value.date.year * 365) +
1816 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
1817 ((dt->value.date.year+1)/400)) +
1818 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
1819 else
1820 ret = ((dt->value.date.year-1) * 365) +
1821 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
1822 ((dt->value.date.year-1)/400)) +
1823 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
1824
1825 return ret;
1826}
1827
1828/**
1829 * TIME_TO_NUMBER:
1830 * @dt: an #xmlSchemaValPtr
1831 *
1832 * Calculates the number of seconds in the time portion of @dt.
1833 *
1834 * Returns seconds.
1835 */
1836#define TIME_TO_NUMBER(dt) \
1837 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
1838 (dt->value.date.min * SECS_PER_MIN)) + dt->value.date.sec)
1839
1840/**
1841 * xmlSchemaCompareDates:
1842 * @x: a first date/time value
1843 * @y: a second date/time value
1844 *
1845 * Compare 2 date/times
1846 *
1847 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
1848 * case of error
1849 */
1850static int
1851xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
1852{
1853 unsigned char xmask, ymask, xor_mask, and_mask;
1854 xmlSchemaValPtr p1, p2, q1, q2;
1855 long p1d, p2d, q1d, q2d;
1856
1857 if ((x == NULL) || (y == NULL))
1858 return -2;
1859
1860 if (x->value.date.tz_flag) {
1861
1862 if (!y->value.date.tz_flag) {
1863 p1 = xmlSchemaDateNormalize(x, 0);
1864 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
1865 /* normalize y + 14:00 */
1866 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
1867
1868 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001869 if (p1d < q1d) {
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 if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00001874 double sec;
1875
1876 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00001877 if (sec < 0.0) {
1878 xmlSchemaFreeValue(p1);
1879 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001880 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001881 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00001882 /* normalize y - 14:00 */
1883 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
1884 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001885 xmlSchemaFreeValue(p1);
1886 xmlSchemaFreeValue(q1);
1887 xmlSchemaFreeValue(q2);
Daniel Veillard5a872412002-05-22 06:40:27 +00001888 if (p1d > q2d)
1889 return 1;
1890 else if (p1d == q2d) {
1891 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
1892 if (sec > 0.0)
1893 return 1;
1894 else
1895 return 2; /* indeterminate */
1896 }
1897 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00001898 } else {
1899 xmlSchemaFreeValue(p1);
1900 xmlSchemaFreeValue(q1);
1901 }
Daniel Veillard5a872412002-05-22 06:40:27 +00001902 }
1903 } else if (y->value.date.tz_flag) {
1904 q1 = xmlSchemaDateNormalize(y, 0);
1905 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
1906
1907 /* normalize x - 14:00 */
1908 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
1909 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
1910
Daniel Veillardfdc91562002-07-01 21:52:03 +00001911 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 if (p1d == q1d) {
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 if (sec < 0.0) {
1920 xmlSchemaFreeValue(p1);
1921 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001922 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001923 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00001924 /* normalize x + 14:00 */
1925 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
1926 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
1927
Daniel Veillardfdc91562002-07-01 21:52:03 +00001928 xmlSchemaFreeValue(p1);
1929 xmlSchemaFreeValue(q1);
1930 xmlSchemaFreeValue(p2);
Daniel Veillard5a872412002-05-22 06:40:27 +00001931 if (p2d > q1d)
1932 return 1;
1933 else if (p2d == q1d) {
1934 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
1935 if (sec > 0.0)
1936 return 1;
1937 else
1938 return 2; /* indeterminate */
1939 }
1940 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00001941 } else {
1942 xmlSchemaFreeValue(p1);
1943 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001944 }
1945 }
1946
1947 /*
1948 * if the same type then calculate the difference
1949 */
1950 if (x->type == y->type) {
1951 q1 = xmlSchemaDateNormalize(y, 0);
1952 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
1953
1954 p1 = xmlSchemaDateNormalize(x, 0);
1955 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
1956
Daniel Veillardfdc91562002-07-01 21:52:03 +00001957 if (p1d < q1d) {
1958 xmlSchemaFreeValue(p1);
1959 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001960 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001961 } else if (p1d > q1d) {
1962 xmlSchemaFreeValue(p1);
1963 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001964 return 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00001965 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00001966 double sec;
1967
1968 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00001969 xmlSchemaFreeValue(p1);
1970 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00001971 if (sec < 0.0)
1972 return -1;
1973 else if (sec > 0.0)
1974 return 1;
1975
1976 }
1977 return 0;
1978 }
1979
1980 switch (x->type) {
1981 case XML_SCHEMAS_DATETIME:
1982 xmask = 0xf;
1983 break;
1984 case XML_SCHEMAS_DATE:
1985 xmask = 0x7;
1986 break;
1987 case XML_SCHEMAS_GYEAR:
1988 xmask = 0x1;
1989 break;
1990 case XML_SCHEMAS_GMONTH:
1991 xmask = 0x2;
1992 break;
1993 case XML_SCHEMAS_GDAY:
1994 xmask = 0x3;
1995 break;
1996 case XML_SCHEMAS_GYEARMONTH:
1997 xmask = 0x3;
1998 break;
1999 case XML_SCHEMAS_GMONTHDAY:
2000 xmask = 0x6;
2001 break;
2002 case XML_SCHEMAS_TIME:
2003 xmask = 0x8;
2004 break;
2005 default:
2006 xmask = 0;
2007 break;
2008 }
2009
2010 switch (y->type) {
2011 case XML_SCHEMAS_DATETIME:
2012 ymask = 0xf;
2013 break;
2014 case XML_SCHEMAS_DATE:
2015 ymask = 0x7;
2016 break;
2017 case XML_SCHEMAS_GYEAR:
2018 ymask = 0x1;
2019 break;
2020 case XML_SCHEMAS_GMONTH:
2021 ymask = 0x2;
2022 break;
2023 case XML_SCHEMAS_GDAY:
2024 ymask = 0x3;
2025 break;
2026 case XML_SCHEMAS_GYEARMONTH:
2027 ymask = 0x3;
2028 break;
2029 case XML_SCHEMAS_GMONTHDAY:
2030 ymask = 0x6;
2031 break;
2032 case XML_SCHEMAS_TIME:
2033 ymask = 0x8;
2034 break;
2035 default:
2036 ymask = 0;
2037 break;
2038 }
2039
2040 xor_mask = xmask ^ ymask; /* mark type differences */
2041 and_mask = xmask & ymask; /* mark field specification */
2042
2043 /* year */
2044 if (xor_mask & 1)
2045 return 2; /* indeterminate */
2046 else if (and_mask & 1) {
2047 if (x->value.date.year < y->value.date.year)
2048 return -1;
2049 else if (x->value.date.year > y->value.date.year)
2050 return 1;
2051 }
2052
2053 /* month */
2054 if (xor_mask & 2)
2055 return 2; /* indeterminate */
2056 else if (and_mask & 2) {
2057 if (x->value.date.mon < y->value.date.mon)
2058 return -1;
2059 else if (x->value.date.mon > y->value.date.mon)
2060 return 1;
2061 }
2062
2063 /* day */
2064 if (xor_mask & 4)
2065 return 2; /* indeterminate */
2066 else if (and_mask & 4) {
2067 if (x->value.date.day < y->value.date.day)
2068 return -1;
2069 else if (x->value.date.day > y->value.date.day)
2070 return 1;
2071 }
2072
2073 /* time */
2074 if (xor_mask & 8)
2075 return 2; /* indeterminate */
2076 else if (and_mask & 8) {
2077 if (x->value.date.hour < y->value.date.hour)
2078 return -1;
2079 else if (x->value.date.hour > y->value.date.hour)
2080 return 1;
2081 else if (x->value.date.min < y->value.date.min)
2082 return -1;
2083 else if (x->value.date.min > y->value.date.min)
2084 return 1;
2085 else if (x->value.date.sec < y->value.date.sec)
2086 return -1;
2087 else if (x->value.date.sec > y->value.date.sec)
2088 return 1;
2089 }
2090
Daniel Veillard070803b2002-05-03 07:29:38 +00002091 return 0;
2092}
2093
2094/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002095 * xmlSchemaCompareValues:
2096 * @x: a first value
2097 * @y: a second value
2098 *
2099 * Compare 2 values
2100 *
Daniel Veillard5a872412002-05-22 06:40:27 +00002101 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2102 * case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00002103 */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002104static int
Daniel Veillard4255d502002-04-16 15:50:10 +00002105xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
2106 if ((x == NULL) || (y == NULL))
2107 return(-2);
2108
2109 switch (x->type) {
2110 case XML_SCHEMAS_STRING:
2111 TODO
2112 case XML_SCHEMAS_DECIMAL:
2113 if (y->type == XML_SCHEMAS_DECIMAL)
2114 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00002115 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00002116 case XML_SCHEMAS_DURATION:
2117 if (y->type == XML_SCHEMAS_DURATION)
2118 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00002119 return(-2);
2120 case XML_SCHEMAS_TIME:
2121 case XML_SCHEMAS_GDAY:
2122 case XML_SCHEMAS_GMONTH:
2123 case XML_SCHEMAS_GMONTHDAY:
2124 case XML_SCHEMAS_GYEAR:
2125 case XML_SCHEMAS_GYEARMONTH:
2126 case XML_SCHEMAS_DATE:
2127 case XML_SCHEMAS_DATETIME:
2128 if ((y->type == XML_SCHEMAS_DATETIME) ||
2129 (y->type == XML_SCHEMAS_TIME) ||
2130 (y->type == XML_SCHEMAS_GDAY) ||
2131 (y->type == XML_SCHEMAS_GMONTH) ||
2132 (y->type == XML_SCHEMAS_GMONTHDAY) ||
2133 (y->type == XML_SCHEMAS_GYEAR) ||
2134 (y->type == XML_SCHEMAS_DATE) ||
2135 (y->type == XML_SCHEMAS_GYEARMONTH))
2136 return (xmlSchemaCompareDates(x, y));
2137
2138 return (-2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002139 default:
2140 TODO
2141 }
Daniel Veillard5a872412002-05-22 06:40:27 +00002142 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00002143}
2144
2145/**
2146 * xmlSchemaValidateFacet:
Daniel Veillard01c13b52002-12-10 15:19:08 +00002147 * @base: the base type
Daniel Veillard4255d502002-04-16 15:50:10 +00002148 * @facet: the facet to check
2149 * @value: the lexical repr of the value to validate
2150 * @val: the precomputed value
2151 *
2152 * Check a value against a facet condition
2153 *
2154 * Returns 0 if the element is schemas valid, a positive error code
2155 * number otherwise and -1 in case of internal or API error.
2156 */
2157int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00002158xmlSchemaValidateFacet(xmlSchemaTypePtr base ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +00002159 xmlSchemaFacetPtr facet,
Daniel Veillard4255d502002-04-16 15:50:10 +00002160 const xmlChar *value, xmlSchemaValPtr val)
2161{
2162 int ret;
2163
2164 switch (facet->type) {
2165 case XML_SCHEMA_FACET_PATTERN:
2166 ret = xmlRegexpExec(facet->regexp, value);
2167 if (ret == 1)
2168 return(0);
2169 if (ret == 0) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002170 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00002171 return(1);
2172 }
2173 return(ret);
2174 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
2175 ret = xmlSchemaCompareValues(val, facet->val);
2176 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002177 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00002178 return(-1);
2179 }
2180 if (ret == -1)
2181 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00002182 /* error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00002183 return(1);
Daniel Veillard070803b2002-05-03 07:29:38 +00002184 case XML_SCHEMA_FACET_MAXINCLUSIVE:
2185 ret = xmlSchemaCompareValues(val, facet->val);
2186 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002187 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002188 return(-1);
2189 }
2190 if ((ret == -1) || (ret == 0))
2191 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00002192 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002193 return(1);
2194 case XML_SCHEMA_FACET_MINEXCLUSIVE:
2195 ret = xmlSchemaCompareValues(val, facet->val);
2196 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002197 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002198 return(-1);
2199 }
2200 if (ret == 1)
2201 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00002202 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002203 return(1);
2204 case XML_SCHEMA_FACET_MININCLUSIVE:
2205 ret = xmlSchemaCompareValues(val, facet->val);
2206 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002207 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002208 return(-1);
2209 }
2210 if ((ret == 1) || (ret == 0))
2211 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00002212 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00002213 return(1);
Daniel Veillard8651f532002-04-17 09:06:27 +00002214 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002215 /* TODO whitespaces */
Daniel Veillard8651f532002-04-17 09:06:27 +00002216 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00002217 case XML_SCHEMA_FACET_ENUMERATION:
2218 if ((facet->value != NULL) &&
2219 (xmlStrEqual(facet->value, value)))
2220 return(0);
2221 return(1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002222 case XML_SCHEMA_FACET_LENGTH:
2223 case XML_SCHEMA_FACET_MAXLENGTH:
2224 case XML_SCHEMA_FACET_MINLENGTH: {
2225 unsigned int len = 0;
2226
2227 if ((facet->val == NULL) ||
2228 (facet->val->type != XML_SCHEMAS_DECIMAL) ||
2229 (facet->val->value.decimal.frac != 0)) {
2230 return(-1);
2231 }
2232 switch (base->flags) {
2233 case XML_SCHEMAS_STRING:
2234 case XML_SCHEMAS_NORMSTRING:
2235 case XML_SCHEMAS_TOKEN:
2236 case XML_SCHEMAS_LANGUAGE:
2237 case XML_SCHEMAS_NMTOKEN:
2238 case XML_SCHEMAS_NAME:
2239 case XML_SCHEMAS_NCNAME:
2240 case XML_SCHEMAS_ID:
2241 case XML_SCHEMAS_IDREF: {
2242 len = xmlUTF8Strlen(value);
2243 break;
2244 }
2245 default:
2246 TODO
2247 }
2248 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
2249 if (len != facet->val->value.decimal.base)
2250 return(1);
2251 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
2252 if (len < facet->val->value.decimal.base)
2253 return(1);
2254 } else {
2255 if (len > facet->val->value.decimal.base)
2256 return(1);
2257 }
2258 break;
2259 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002260 default:
2261 TODO
2262 }
2263 return(0);
2264}
2265
2266#endif /* LIBXML_SCHEMAS_ENABLED */