|
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifdef IS_LITTLE_ENDIAN |
|
7 |
|
8 #define PREFIX(ident) little2_ ## ident |
|
9 #define BYTE_TYPE(p) LITTLE2_BYTE_TYPE(XmlGetUtf16InternalEncodingNS(), p) |
|
10 #define IS_NAME_CHAR_MINBPC(p) LITTLE2_IS_NAME_CHAR_MINBPC(0, p) |
|
11 #define IS_NMSTRT_CHAR_MINBPC(p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(0, p) |
|
12 |
|
13 #else |
|
14 |
|
15 #define PREFIX(ident) big2_ ## ident |
|
16 #define BYTE_TYPE(p) BIG2_BYTE_TYPE(XmlGetUtf16InternalEncodingNS(), p) |
|
17 #define IS_NAME_CHAR_MINBPC(p) BIG2_IS_NAME_CHAR_MINBPC(0, p) |
|
18 #define IS_NMSTRT_CHAR_MINBPC(p) BIG2_IS_NMSTRT_CHAR_MINBPC(0, p) |
|
19 |
|
20 #endif |
|
21 |
|
22 #define MOZ_EXPAT_VALID_QNAME (0) |
|
23 #define MOZ_EXPAT_EMPTY_QNAME (1 << 0) |
|
24 #define MOZ_EXPAT_INVALID_CHARACTER (1 << 1) |
|
25 #define MOZ_EXPAT_MALFORMED (1 << 2) |
|
26 |
|
27 int MOZ_XMLCheckQName(const char* ptr, const char* end, int ns_aware, |
|
28 const char** colon) |
|
29 { |
|
30 int result = MOZ_EXPAT_VALID_QNAME; |
|
31 int nmstrt = 1; |
|
32 *colon = 0; |
|
33 if (ptr == end) { |
|
34 return MOZ_EXPAT_EMPTY_QNAME; |
|
35 } |
|
36 do { |
|
37 switch (BYTE_TYPE(ptr)) { |
|
38 case BT_COLON: |
|
39 /* We're namespace-aware and either first or last character is a colon |
|
40 or we've already seen a colon. */ |
|
41 if (ns_aware && (nmstrt || *colon || ptr + 2 == end)) { |
|
42 return MOZ_EXPAT_MALFORMED; |
|
43 } |
|
44 *colon = ptr; |
|
45 nmstrt = ns_aware; /* e.g. "a:0" should be valid if !ns_aware */ |
|
46 break; |
|
47 case BT_NONASCII: |
|
48 if (nmstrt && !IS_NMSTRT_CHAR_MINBPC(ptr)) { |
|
49 /* If this is a valid name character and we're namespace-aware, the |
|
50 QName is malformed. Otherwise, this character's invalid at the |
|
51 start of a name (or, if we're namespace-aware, at the start of a |
|
52 localpart). */ |
|
53 return (IS_NAME_CHAR_MINBPC(ptr) && ns_aware) ? |
|
54 MOZ_EXPAT_MALFORMED : |
|
55 MOZ_EXPAT_INVALID_CHARACTER; |
|
56 } |
|
57 if (!IS_NAME_CHAR_MINBPC(ptr)) { |
|
58 return MOZ_EXPAT_INVALID_CHARACTER; |
|
59 } |
|
60 nmstrt = 0; |
|
61 break; |
|
62 case BT_NMSTRT: |
|
63 case BT_HEX: |
|
64 nmstrt = 0; |
|
65 break; |
|
66 case BT_DIGIT: |
|
67 case BT_NAME: |
|
68 case BT_MINUS: |
|
69 if (nmstrt) { |
|
70 return MOZ_EXPAT_INVALID_CHARACTER; |
|
71 } |
|
72 break; |
|
73 default: |
|
74 return MOZ_EXPAT_INVALID_CHARACTER; |
|
75 } |
|
76 ptr += 2; |
|
77 } while (ptr != end); |
|
78 return result; |
|
79 } |
|
80 |
|
81 int MOZ_XMLIsLetter(const char* ptr) |
|
82 { |
|
83 switch (BYTE_TYPE(ptr)) { |
|
84 case BT_NONASCII: |
|
85 if (!IS_NMSTRT_CHAR_MINBPC(ptr)) { |
|
86 return 0; |
|
87 } |
|
88 /* fall through */ |
|
89 case BT_NMSTRT: |
|
90 case BT_HEX: |
|
91 return 1; |
|
92 default: |
|
93 return 0; |
|
94 } |
|
95 } |
|
96 |
|
97 int MOZ_XMLIsNCNameChar(const char* ptr) |
|
98 { |
|
99 switch (BYTE_TYPE(ptr)) { |
|
100 case BT_NONASCII: |
|
101 if (!IS_NAME_CHAR_MINBPC(ptr)) { |
|
102 return 0; |
|
103 } |
|
104 /* fall through */ |
|
105 case BT_NMSTRT: |
|
106 case BT_HEX: |
|
107 case BT_DIGIT: |
|
108 case BT_NAME: |
|
109 case BT_MINUS: |
|
110 return 1; |
|
111 default: |
|
112 return 0; |
|
113 } |
|
114 } |
|
115 |
|
116 int MOZ_XMLTranslateEntity(const char* ptr, const char* end, const char** next, |
|
117 XML_Char* result) |
|
118 { |
|
119 const ENCODING* enc = XmlGetUtf16InternalEncodingNS(); |
|
120 int tok = PREFIX(scanRef)(enc, ptr, end, next); |
|
121 if (tok <= XML_TOK_INVALID) { |
|
122 return 0; |
|
123 } |
|
124 |
|
125 if (tok == XML_TOK_CHAR_REF) { |
|
126 int n = XmlCharRefNumber(enc, ptr); |
|
127 |
|
128 /* We could get away with just < 0, but better safe than sorry. */ |
|
129 if (n <= 0) { |
|
130 return 0; |
|
131 } |
|
132 |
|
133 return XmlUtf16Encode(n, (unsigned short*)result); |
|
134 } |
|
135 |
|
136 if (tok == XML_TOK_ENTITY_REF) { |
|
137 /* *next points to after the semicolon, so the entity ends at |
|
138 *next - enc->minBytesPerChar. */ |
|
139 XML_Char ch = |
|
140 (XML_Char)XmlPredefinedEntityName(enc, ptr, *next - enc->minBytesPerChar); |
|
141 if (!ch) { |
|
142 return 0; |
|
143 } |
|
144 |
|
145 *result = ch; |
|
146 return 1; |
|
147 } |
|
148 |
|
149 return 0; |
|
150 } |
|
151 |
|
152 #undef PREFIX |
|
153 #undef BYTE_TYPE |
|
154 #undef IS_NAME_CHAR_MINBPC |
|
155 #undef IS_NMSTRT_CHAR_MINBPC |