Merge b2g-inbound to m-c.
[gecko.git] / parser / expat / lib / moz_extensions.c
blob2e7a6e06e461fffbe6faab03f97833c5c4c347aa
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/. */
6 #ifdef IS_LITTLE_ENDIAN
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)
13 #else
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)
20 #endif
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)
27 int MOZ_XMLCheckQName(const char* ptr, const char* end, int ns_aware,
28 const char** colon)
30 int result = MOZ_EXPAT_VALID_QNAME;
31 int nmstrt = 1;
32 *colon = 0;
33 if (ptr == end) {
34 return MOZ_EXPAT_EMPTY_QNAME;
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;
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;
57 if (!IS_NAME_CHAR_MINBPC(ptr)) {
58 return MOZ_EXPAT_INVALID_CHARACTER;
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;
72 break;
73 default:
74 return MOZ_EXPAT_INVALID_CHARACTER;
76 ptr += 2;
77 } while (ptr != end);
78 return result;
81 int MOZ_XMLIsLetter(const char* ptr)
83 switch (BYTE_TYPE(ptr)) {
84 case BT_NONASCII:
85 if (!IS_NMSTRT_CHAR_MINBPC(ptr)) {
86 return 0;
88 /* fall through */
89 case BT_NMSTRT:
90 case BT_HEX:
91 return 1;
92 default:
93 return 0;
97 int MOZ_XMLIsNCNameChar(const char* ptr)
99 switch (BYTE_TYPE(ptr)) {
100 case BT_NONASCII:
101 if (!IS_NAME_CHAR_MINBPC(ptr)) {
102 return 0;
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;
116 int MOZ_XMLTranslateEntity(const char* ptr, const char* end, const char** next,
117 XML_Char* result)
119 const ENCODING* enc = XmlGetUtf16InternalEncodingNS();
120 int tok = PREFIX(scanRef)(enc, ptr, end, next);
121 if (tok <= XML_TOK_INVALID) {
122 return 0;
125 if (tok == XML_TOK_CHAR_REF) {
126 int n = XmlCharRefNumber(enc, ptr);
128 /* We could get away with just < 0, but better safe than sorry. */
129 if (n <= 0) {
130 return 0;
133 return XmlUtf16Encode(n, (unsigned short*)result);
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;
145 *result = ch;
146 return 1;
149 return 0;
152 #undef PREFIX
153 #undef BYTE_TYPE
154 #undef IS_NAME_CHAR_MINBPC
155 #undef IS_NMSTRT_CHAR_MINBPC