aboutsummaryrefslogtreecommitdiffstats
path: root/xmlschemastypes.c
blob: 200047fd2e0c2022d123ff2e3b9698c35c4023c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
 * schemastypes.c : implementation of the XML Schema Datatypes
 *             definition and validity checking
 *
 * See Copyright for the status of this software.
 *
 * Daniel Veillard <veillard@redhat.com>
 */

#define IN_LIBXML
#include "libxml.h"

#ifdef LIBXML_SCHEMAS_ENABLED

#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/hash.h>
#include <libxml/valid.h>

#include <libxml/xmlschemas.h>
#include <libxml/schemasInternals.h>
#include <libxml/xmlschemastypes.h>

#define DEBUG

#define TODO 								\
    xmlGenericError(xmlGenericErrorContext,				\
	    "Unimplemented block at %s:%d\n",				\
            __FILE__, __LINE__);

#define XML_SCHEMAS_NAMESPACE_NAME \
    (const xmlChar *)"http://www.w3.org/2001/XMLSchema"

typedef enum {
    XML_SCHEMAS_UNKNOWN = 0,
    XML_SCHEMAS_STRING,
    XML_SCHEMAS_NMTOKEN,
    XML_SCHEMAS_DECIMAL,
    XML_SCHEMAS_,
    XML_SCHEMAS_XXX
} xmlSchemaValType;

unsigned long powten[10] = {
    1, 10, 100, 1000, 10000, 100000, 1000000, 10000000L,
    100000000L, 1000000000L
};

typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
struct _xmlSchemaValDecimal {
    /* would use long long but not portable */
    unsigned long base;
    unsigned int extra;
    int sign:1;
    int frac:7;
    int total:8;
};

struct _xmlSchemaVal {
    xmlSchemaValType type;
    union {
	xmlSchemaValDecimal decimal;
    } value;
};

static int xmlSchemaTypesInitialized = 0;
static xmlHashTablePtr xmlSchemaTypesBank = NULL;

static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
static xmlSchemaTypePtr xmlSchemaTypeNmtoken = NULL;

/*
 * xmlSchemaInitBasicType:
 * @name:  the type name
 *
 * Initialize one default type
 */
static xmlSchemaTypePtr
xmlSchemaInitBasicType(const char *name) {
    xmlSchemaTypePtr ret;

    ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
    if (ret == NULL) {
	xmlGenericError(xmlGenericErrorContext,
		"Could not initilize type %s: out of memory\n", name);
	return(NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaType));
    ret->name = xmlStrdup((const xmlChar *)name);
    ret->type = XML_SCHEMA_TYPE_BASIC;
    ret->contentType = XML_SCHEMA_CONTENT_BASIC;
    xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
	             XML_SCHEMAS_NAMESPACE_NAME, ret);
    return(ret);
}

/*
 * xmlSchemaInitTypes:
 *
 * Initialize the default XML Schemas type library
 */
void
xmlSchemaInitTypes(void) {
    if (xmlSchemaTypesInitialized != 0)
	return;
    xmlSchemaTypesBank = xmlHashCreate(40);
    
    xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string");
    xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType");
    xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType");
    xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal");
    xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date");
    xmlSchemaTypePositiveIntegerDef = xmlSchemaInitBasicType("positiveInteger");
    xmlSchemaTypeNonNegativeIntegerDef =
	xmlSchemaInitBasicType("nonNegativeInteger");
    xmlSchemaTypeNmtoken = xmlSchemaInitBasicType("NMTOKEN");

    xmlSchemaTypesInitialized = 1;
}

/**
 * xmlSchemaCleanupTypes:
 *
 * Cleanup the default XML Schemas type library
 */
void	
xmlSchemaCleanupTypes(void) {
    if (xmlSchemaTypesInitialized == 0)
	return;
    xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
    xmlSchemaTypesInitialized = 0;
}

/**
 * xmlSchemaNewValue:
 * @type:  the value type
 *
 * Allocate a new simple type value
 *
 * Returns a pointer to the new value or NULL in case of error
 */
static xmlSchemaValPtr
xmlSchemaNewValue(xmlSchemaValType type) {
    xmlSchemaValPtr value;

    value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
    if (value == NULL) {
	return(NULL);
    }
    memset(value, 0, sizeof(xmlSchemaVal));
    value->type = type;
    return(value);
}

/**
 * xmlSchemaFreeValue:
 * @value:  the value to free
 *
 * Cleanup the default XML Schemas type library
 */
void	
xmlSchemaFreeValue(xmlSchemaValPtr value) {
    if (value == NULL)
	return;
    xmlFree(value);
}

/**
 * xmlSchemaGetPredefinedType:
 * @name: the type name
 * @ns:  the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
 *
 * Lookup a type in the default XML Schemas type library
 *
 * Returns the type if found, NULL otherwise
 */
xmlSchemaTypePtr
xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
    if (xmlSchemaTypesInitialized == 0)
	xmlSchemaInitTypes();
    if (name == NULL)
	return(NULL);
    return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
}
/**
 * xmlSchemaValidatePredefinedType:
 * @type: the predefined type
 * @value: the value to check
 * @val:  the return computed value
 *
 * Check that a value conforms to the lexical space of the predefined type.
 * if true a value is computed and returned in @val.
 *
 * Returns 0 if this validates, a positive error code number otherwise
 *         and -1 in case of internal or API error.
 */
int
xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
	                        xmlSchemaValPtr *val) {
    xmlSchemaValPtr v;

    if (xmlSchemaTypesInitialized == 0)
	return(-1);
    if (type == NULL)
	return(-1);
    if (val != NULL)
	*val = NULL;
    if (type == xmlSchemaTypeStringDef) { 
	return(0);
    } else if (type == xmlSchemaTypeAnyTypeDef) {
	return(0);
    } else if (type == xmlSchemaTypeAnySimpleTypeDef) {
	return(0);
    } else if (type == xmlSchemaTypeNmtoken) {
	if (xmlValidateNmtokenValue(value))
	    return(0);
	return(1);
    } else if (type == xmlSchemaTypeDecimalDef) {
	const xmlChar *cur = value, *tmp;
	int frac = 0, main, neg = 0;
	unsigned long base = 0;
	if (cur == NULL)
	    return(1);
	if (*cur == '+')
	    cur++;
	else if (*cur == '-') {
	    neg = 1;
	    cur++;
	}
	tmp = cur;
	while ((*cur >= '0') && (*cur <= '9')) {
	    base = base * 10 + (*cur - '0');
	    cur++;
	}
	main = cur - tmp;
	if (*cur == '.') {
	    cur++;
	    tmp = cur;
	    while ((*cur >= '0') && (*cur <= '9')) {
		base = base * 10 + (*cur - '0');
		cur++;
	    }
	    frac = cur - tmp;
	}
	if (*cur != 0)
	    return(1);
	if (val != NULL) {
	    v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
	    if (v != NULL) {
		v->value.decimal.base = base;
		v->value.decimal.sign = neg;
		v->value.decimal.frac = frac;
		v->value.decimal.total = frac + main;
		*val = v;
	    }
	}
	return(0);
    } else if (type == xmlSchemaTypeDateDef) {
	const xmlChar *cur = value;
	if (cur == NULL)
	    return(1);
	if (*cur == '-')
	    cur++;
	if ((*cur < '0') || (*cur > '9'))
	    return(1);
	if ((*cur < '0') || (*cur > '9'))
	    return(1);
	if ((*cur < '0') || (*cur > '9'))
	    return(1);
	if ((*cur < '0') || (*cur > '9'))
	    return(1);
	while ((*cur >= '0') && (*cur <= '9'))
	    cur++;
	if (*cur != '-')
	    return(1);
	cur++;
	if ((*cur != '0') && (*cur != '1'))
	    return(1);
	if ((*cur == '0') && (cur[1] == '0'))
	    return(1);
	if ((*cur == '1') && ((cur[1] < '0') || (cur[1] > '2')))
	    return(1);
	cur += 2;
	if (*cur != '-')
	    return(1);
	cur++;
	if ((*cur < '0') || (*cur > '3'))
	    return(1);
	if ((*cur == '0') && (cur[1] == '0'))
	    return(1);
	if ((*cur == '3') && ((cur[1] < '0') || (cur[1] > '1')))
	    return(1);
	cur += 2;
	if (*cur != 0)
	    return(1);
	return(0);
    } else if (type == xmlSchemaTypePositiveIntegerDef) {
	const xmlChar *cur = value;
	unsigned long base = 0;
	int total = 0;
	if (cur == NULL)
	    return(1);
	if (*cur == '+')
	    cur++;
	while ((*cur >= '0') && (*cur <= '9')) {
	    base = base * 10 + (*cur - '0');
	    total++;
	    cur++;
	}
	if (*cur != 0)
	    return(1);
	if (val != NULL) {
	    v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
	    if (v != NULL) {
		v->value.decimal.base = base;
		v->value.decimal.sign = 0;
		v->value.decimal.frac = 0;
		v->value.decimal.total = total;
		*val = v;
	    }
	}
	return(0);
    } else if (type == xmlSchemaTypeNonNegativeIntegerDef) {
	const xmlChar *cur = value;
	unsigned long base = 0;
	int total = 0;
	int sign = 0;
	if (cur == NULL)
	    return(1);
	if (*cur == '-') {
	    sign = 1;
	    cur++;
	} else if (*cur == '+')
	    cur++;
	while ((*cur >= '0') && (*cur <= '9')) {
	    base = base * 10 + (*cur - '0');
	    total++;
	    cur++;
	}
	if (*cur != 0)
	    return(1);
	if ((sign == 1) && (base != 0))
	    return(1);
	if (val != NULL) {
	    v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
	    if (v != NULL) {
		v->value.decimal.base = base;
		v->value.decimal.sign = 0;
		v->value.decimal.frac = 0;
		v->value.decimal.total = total;
		*val = v;
	    }
	}
	return(0);
    } else {
	TODO
	return(0);
    }
}

/**
 * xmlSchemaCompareDecimals:
 * @x:  a first decimal value
 * @y:  a second decimal value
 *
 * Compare 2 decimals
 *
 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
 */
static int
xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
{
    xmlSchemaValPtr swp;
    int order = 1;
    unsigned long tmp;

    if ((x->value.decimal.sign) && (x->value.decimal.sign))
        order = -1;
    else if (x->value.decimal.sign)
        return (-1);
    else if (y->value.decimal.sign)
        return (1);
    if (x->value.decimal.frac == y->value.decimal.frac) {
        if (x->value.decimal.base < y->value.decimal.base)
            return (-1);
        return (x->value.decimal.base > y->value.decimal.base);
    }
    if (y->value.decimal.frac > x->value.decimal.frac) {
        swp = y;
        y = x;
        x = swp;
        order = -order;
    }
    tmp =
        x->value.decimal.base / powten[x->value.decimal.frac -
                                       y->value.decimal.frac];
    if (tmp > y->value.decimal.base)
        return (order);
    if (tmp < y->value.decimal.base)
        return (-order);
    tmp =
        y->value.decimal.base * powten[x->value.decimal.frac -
                                       y->value.decimal.frac];
    if (x->value.decimal.base < tmp)
        return (-order);
    if (x->value.decimal.base == tmp)
        return (0);
    return (order);
}

/**
 * xmlSchemaCompareValues:
 * @x:  a first value
 * @y:  a second value
 *
 * Compare 2 values
 *
 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
 */
static int
xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
    if ((x == NULL) || (y == NULL))
	return(-2);

    switch (x->type) {
	case XML_SCHEMAS_STRING:
	    TODO
	case XML_SCHEMAS_DECIMAL:
	    if (y->type == XML_SCHEMAS_DECIMAL)
		return(xmlSchemaCompareDecimals(x, y));
	    else
		return(-2);
	default:
	    TODO
    }
}

/**
 * xmlSchemaValidateFacet:
 * @type:  the type declaration
 * @facet:  the facet to check
 * @value:  the lexical repr of the value to validate
 * @val:  the precomputed value
 *
 * Check a value against a facet condition
 *
 * Returns 0 if the element is schemas valid, a positive error code
 *     number otherwise and -1 in case of internal or API error.
 */
int
xmlSchemaValidateFacet(xmlSchemaTypePtr base, xmlSchemaFacetPtr facet,
	               const xmlChar *value, xmlSchemaValPtr val)
{
    int ret;

    switch (facet->type) {
	case XML_SCHEMA_FACET_PATTERN:
	    ret = xmlRegexpExec(facet->regexp, value);
	    if (ret == 1)
		return(0);
	    if (ret == 0) {
		TODO /* error code */
		return(1);
	    }
	    return(ret);
	case XML_SCHEMA_FACET_MAXEXCLUSIVE:
	    ret = xmlSchemaCompareValues(val, facet->val);
	    if (ret == -2) {
		TODO /* error code */
		return(-1);
	    }
	    if (ret == -1)
		return(0);
	    TODO /* error code */
	    return(1);
	case XML_SCHEMA_FACET_WHITESPACE:
	    TODO /* whitespaces */
	    return(0);
	case XML_SCHEMA_FACET_MAXLENGTH:
	    if ((facet->val != NULL) &&
		(facet->val->type == XML_SCHEMAS_DECIMAL) &&
		(facet->val->value.decimal.frac == 0)) {
		int len;

		if (facet->val->value.decimal.sign == 1)
		    return(1);
                len = xmlUTF8Strlen(value);
		if (len > facet->val->value.decimal.base)
		    return(1);
		return(0);
	    }
	    TODO /* error code */
	    return(1);
	default:
	    TODO
    }
    return(0);
}

#endif /* LIBXML_SCHEMAS_ENABLED */