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