summaryrefslogtreecommitdiff
path: root/src/utils/validate.c
blob: ac0bcd1a25bc2c65dc83288faf5beb216e1e4d8b (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
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *			http://www.opensource.org/licenses/mit-license.php
 * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
 */

#include <inttypes.h>
#include <stddef.h>

#include "utils/validate.h"

#include <dom/core/string.h>

#include "utils/character_valid.h"
#include "utils/namespace.h"
#include "utils/utils.h"

/* An combination of various tests */
static bool is_first_char(uint32_t ch);
static bool is_name_char(uint32_t ch);

/* Test whether the character can be the first character of
 * a NCName. */
static bool is_first_char(uint32_t ch)
{
	/* Refer http://www.w3.org/TR/REC-xml/ for detail */
	if (((ch >= 'a') && (ch <= 'z')) ||
		((ch >= 'A') && (ch <= 'Z')) ||
		(ch == '_') || (ch == ':') ||
		((ch >= 0xC0) && (ch <= 0xD6)) ||
		((ch >= 0xD8) && (ch <= 0xF6)) ||
		((ch >= 0xF8) && (ch <= 0x2FF)) ||
		((ch >= 0x370) && (ch <= 0x37D)) ||
		((ch >= 0x37F) && (ch <= 0x1FFF)) ||
		((ch >= 0x200C) && (ch <= 0x200D)) ||
		((ch >= 0x2070) && (ch <= 0x218F)) ||
		((ch >= 0x2C00) && (ch <= 0x2FEF)) ||
		((ch >= 0x3001) && (ch <= 0xD7FF)) ||
		((ch >= 0xF900) && (ch <= 0xFDCF)) ||
		((ch >= 0xFDF0) && (ch <= 0xFFFD)) ||
		((ch >= 0x10000) && (ch <= 0xEFFFF)))
		return true;

	if (is_letter(ch) || ch == (uint32_t) '_' || ch == (uint32_t) ':') {
		return true;
	} 

	return false;
}

/* Test whether the character can be a part of a NCName */
static bool is_name_char(uint32_t ch)
{
	/* Refer http://www.w3.org/TR/REC-xml/ for detail */
	if (((ch >= 'a') && (ch <= 'z')) ||
		((ch >= 'A') && (ch <= 'Z')) ||
		((ch >= '0') && (ch <= '9')) || /* !start */
		(ch == '_') || (ch == ':') ||
		(ch == '-') || (ch == '.') || (ch == 0xB7) || /* !start */
		((ch >= 0xC0) && (ch <= 0xD6)) ||
		((ch >= 0xD8) && (ch <= 0xF6)) ||
		((ch >= 0xF8) && (ch <= 0x2FF)) ||
		((ch >= 0x300) && (ch <= 0x36F)) || /* !start */
		((ch >= 0x370) && (ch <= 0x37D)) ||
		((ch >= 0x37F) && (ch <= 0x1FFF)) ||
		((ch >= 0x200C) && (ch <= 0x200D)) ||
		((ch >= 0x203F) && (ch <= 0x2040)) || /* !start */
		((ch >= 0x2070) && (ch <= 0x218F)) ||
		((ch >= 0x2C00) && (ch <= 0x2FEF)) ||
		((ch >= 0x3001) && (ch <= 0xD7FF)) ||
		((ch >= 0xF900) && (ch <= 0xFDCF)) ||
		((ch >= 0xFDF0) && (ch <= 0xFFFD)) ||
		((ch >= 0x10000) && (ch <= 0xEFFFF)))
		return true;

	if (is_letter(ch) == true)
		return true;
	if (is_digit(ch) == true)
		return true;
	if (is_combining_char(ch) == true)
		return true;
	if (is_extender(ch) == true)
		return true;
	
	if (ch == (uint32_t) '.' || ch == (uint32_t) '-' || 
			ch == (uint32_t) '_' || ch == (uint32_t) ':')
		return true;

	return false;
}

/**
 * Test whether the name is a valid one according XML 1.0 standard.
 * For the standard please refer:
 *
 * http://www.w3.org/TR/2004/REC-xml-20040204/
 *
 * \param name  The name need to be tested
 * \return true if ::name is valid, false otherwise.
 */
bool _dom_validate_name(dom_string *name)
{
	uint32_t ch, len, i;
	dom_exception err;

	if (name == NULL)
		return false;

	len = dom_string_length(name);
	if (len == 0)
		return false;

	/* Test the first character of this string */
	err = dom_string_at(name, 0, &ch);
	if (err != DOM_NO_ERR)
		return false;

	if (is_first_char(ch) == false)
		return false;

	/* Test all remain characters in this string */
	for(i = 1; i < len; i++) {
		err = dom_string_at(name, i, &ch);
		if (err != DOM_NO_ERR)
			return false;

		if (is_name_char(ch) != true)
			return false;
	}

	return true;
}

/**
 * Validate whether the string is a legal NCName.
 * Refer http://www.w3.org/TR/REC-xml-names/ for detail.
 *
 * \param str  The name to validate
 * \return true if ::name is valid, false otherwise.
 */
bool _dom_validate_ncname(dom_string *name)
{
	uint32_t ch, len, i;
	dom_exception err;

	if (name == NULL)
		return false;

	len = dom_string_length(name);
	if (len == 0)
		return false;

	/* Test the first character of this string */
	err = dom_string_at(name, 0, &ch);
	if (err != DOM_NO_ERR)
		return false;

	if (is_letter(ch) == false && ch != (uint32_t) '_')
		return false;
	
	/* Test all remain characters in this string */
	for(i = 1; i < len; i++) {
		err = dom_string_at(name, i, &ch);
		if (err != DOM_NO_ERR)
			return false;

		if (is_name_char(ch) == false)
			return false;

		if (ch == (uint32_t) ':')
			return false;
	}

	return true;
}