/src/mozilla-central/parser/expat/lib/moz_extensions.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | #define PREFIX(ident) little2_ ## ident |
9 | 0 | #define BYTE_TYPE(p) LITTLE2_BYTE_TYPE(XmlGetUtf16InternalEncodingNS(), p) |
10 | 0 | #define IS_NAME_CHAR_MINBPC(p) LITTLE2_IS_NAME_CHAR_MINBPC(0, p) |
11 | 0 | #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 | 0 | #define MOZ_EXPAT_VALID_QNAME (0) |
23 | 0 | #define MOZ_EXPAT_EMPTY_QNAME (1 << 0) |
24 | 0 | #define MOZ_EXPAT_INVALID_CHARACTER (1 << 1) |
25 | 0 | #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 | 0 | { |
30 | 0 | int result = MOZ_EXPAT_VALID_QNAME; |
31 | 0 | int nmstrt = 1; |
32 | 0 | *colon = 0; |
33 | 0 | if (ptr == end) { |
34 | 0 | return MOZ_EXPAT_EMPTY_QNAME; |
35 | 0 | } |
36 | 0 | do { |
37 | 0 | switch (BYTE_TYPE(ptr)) { |
38 | 0 | case BT_COLON: |
39 | 0 | /* We're namespace-aware and either first or last character is a colon |
40 | 0 | or we've already seen a colon. */ |
41 | 0 | if (ns_aware && (nmstrt || *colon || ptr + 2 == end)) { |
42 | 0 | return MOZ_EXPAT_MALFORMED; |
43 | 0 | } |
44 | 0 | *colon = ptr; |
45 | 0 | nmstrt = ns_aware; /* e.g. "a:0" should be valid if !ns_aware */ |
46 | 0 | break; |
47 | 0 | case BT_NONASCII: |
48 | 0 | if (!IS_NAME_CHAR_MINBPC(ptr) || |
49 | 0 | (nmstrt && !*colon && !IS_NMSTRT_CHAR_MINBPC(ptr))) { |
50 | 0 | return MOZ_EXPAT_INVALID_CHARACTER; |
51 | 0 | } |
52 | 0 | if (nmstrt && *colon && !IS_NMSTRT_CHAR_MINBPC(ptr)) { |
53 | 0 | /* If a non-starting character like a number is right after the colon, |
54 | 0 | this is a namespace error, not invalid character */ |
55 | 0 | return MOZ_EXPAT_MALFORMED; |
56 | 0 | } |
57 | 0 | nmstrt = 0; |
58 | 0 | break; |
59 | 0 | case BT_NMSTRT: |
60 | 0 | case BT_HEX: |
61 | 0 | nmstrt = 0; |
62 | 0 | break; |
63 | 0 | case BT_DIGIT: |
64 | 0 | case BT_NAME: |
65 | 0 | case BT_MINUS: |
66 | 0 | if (nmstrt) { |
67 | 0 | return MOZ_EXPAT_INVALID_CHARACTER; |
68 | 0 | } |
69 | 0 | break; |
70 | 0 | default: |
71 | 0 | return MOZ_EXPAT_INVALID_CHARACTER; |
72 | 0 | } |
73 | 0 | ptr += 2; |
74 | 0 | } while (ptr != end); |
75 | 0 | return result; |
76 | 0 | } |
77 | | |
78 | | int MOZ_XMLIsLetter(const char* ptr) |
79 | 0 | { |
80 | 0 | switch (BYTE_TYPE(ptr)) { |
81 | 0 | case BT_NONASCII: |
82 | 0 | if (!IS_NMSTRT_CHAR_MINBPC(ptr)) { |
83 | 0 | return 0; |
84 | 0 | } |
85 | 0 | /* fall through */ |
86 | 0 | case BT_NMSTRT: |
87 | 0 | case BT_HEX: |
88 | 0 | return 1; |
89 | 0 | default: |
90 | 0 | return 0; |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | | int MOZ_XMLIsNCNameChar(const char* ptr) |
95 | 0 | { |
96 | 0 | switch (BYTE_TYPE(ptr)) { |
97 | 0 | case BT_NONASCII: |
98 | 0 | if (!IS_NAME_CHAR_MINBPC(ptr)) { |
99 | 0 | return 0; |
100 | 0 | } |
101 | 0 | /* fall through */ |
102 | 0 | case BT_NMSTRT: |
103 | 0 | case BT_HEX: |
104 | 0 | case BT_DIGIT: |
105 | 0 | case BT_NAME: |
106 | 0 | case BT_MINUS: |
107 | 0 | return 1; |
108 | 0 | default: |
109 | 0 | return 0; |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | | int MOZ_XMLTranslateEntity(const char* ptr, const char* end, const char** next, |
114 | | XML_Char* result) |
115 | 0 | { |
116 | 0 | // Can we assert here somehow? |
117 | 0 | // MOZ_ASSERT(*ptr == '&'); |
118 | 0 |
|
119 | 0 | const ENCODING* enc = XmlGetUtf16InternalEncodingNS(); |
120 | 0 | /* scanRef expects to be pointed to the char after the '&'. */ |
121 | 0 | int tok = PREFIX(scanRef)(enc, ptr + enc->minBytesPerChar, end, next); |
122 | 0 | if (tok <= XML_TOK_INVALID) { |
123 | 0 | return 0; |
124 | 0 | } |
125 | 0 | |
126 | 0 | if (tok == XML_TOK_CHAR_REF) { |
127 | 0 | /* XmlCharRefNumber expects to be pointed to the '&'. */ |
128 | 0 | int n = XmlCharRefNumber(enc, ptr); |
129 | 0 |
|
130 | 0 | /* We could get away with just < 0, but better safe than sorry. */ |
131 | 0 | if (n <= 0) { |
132 | 0 | return 0; |
133 | 0 | } |
134 | 0 | |
135 | 0 | return XmlUtf16Encode(n, (unsigned short*)result); |
136 | 0 | } |
137 | 0 |
|
138 | 0 | if (tok == XML_TOK_ENTITY_REF) { |
139 | 0 | /* XmlPredefinedEntityName expects to be pointed to the char after '&'. |
140 | 0 |
|
141 | 0 | *next points to after the semicolon, so the entity ends at |
142 | 0 | *next - enc->minBytesPerChar. */ |
143 | 0 | XML_Char ch = |
144 | 0 | (XML_Char)XmlPredefinedEntityName(enc, ptr + enc->minBytesPerChar, |
145 | 0 | *next - enc->minBytesPerChar); |
146 | 0 | if (!ch) { |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | |
150 | 0 | *result = ch; |
151 | 0 | return 1; |
152 | 0 | } |
153 | 0 | |
154 | 0 | return 0; |
155 | 0 | } |
156 | | |
157 | | #undef PREFIX |
158 | | #undef BYTE_TYPE |
159 | | #undef IS_NAME_CHAR_MINBPC |
160 | | #undef IS_NMSTRT_CHAR_MINBPC |