1*0c65ac1dSAntonio Huete Jimenez /*
2*0c65ac1dSAntonio Huete Jimenez __ __ _
3*0c65ac1dSAntonio Huete Jimenez ___\ \/ /_ __ __ _| |_
4*0c65ac1dSAntonio Huete Jimenez / _ \\ /| '_ \ / _` | __|
5*0c65ac1dSAntonio Huete Jimenez | __// \| |_) | (_| | |_
6*0c65ac1dSAntonio Huete Jimenez \___/_/\_\ .__/ \__,_|\__|
7*0c65ac1dSAntonio Huete Jimenez |_| XML parser
8*0c65ac1dSAntonio Huete Jimenez
9*0c65ac1dSAntonio Huete Jimenez Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net>
11*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2001-2003 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
12*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2002 Greg Stein <gstein@users.sourceforge.net>
13*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2002-2016 Karl Waclawek <karl@waclawek.net>
14*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2005-2009 Steven Solie <steven@solie.ca>
15*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2016-2022 Sebastian Pipping <sebastian@pipping.org>
16*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2016 Pascal Cuoq <cuoq@trust-in-soft.com>
17*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2016 Don Lewis <truckman@apache.org>
18*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
19*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2017 Alexander Bluhm <alexander.bluhm@gmx.net>
20*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2017 Benbuck Nason <bnason@netflix.com>
21*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2017 José Gutiérrez de la Concha <jose@zeroc.com>
22*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2019 David Loffredo <loffredo@steptools.com>
23*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2021 Dong-hee Na <donghee.na@python.org>
24*0c65ac1dSAntonio Huete Jimenez Copyright (c) 2022 Martin Ettl <ettl.martin78@googlemail.com>
25*0c65ac1dSAntonio Huete Jimenez Licensed under the MIT license:
26*0c65ac1dSAntonio Huete Jimenez
27*0c65ac1dSAntonio Huete Jimenez Permission is hereby granted, free of charge, to any person obtaining
28*0c65ac1dSAntonio Huete Jimenez a copy of this software and associated documentation files (the
29*0c65ac1dSAntonio Huete Jimenez "Software"), to deal in the Software without restriction, including
30*0c65ac1dSAntonio Huete Jimenez without limitation the rights to use, copy, modify, merge, publish,
31*0c65ac1dSAntonio Huete Jimenez distribute, sublicense, and/or sell copies of the Software, and to permit
32*0c65ac1dSAntonio Huete Jimenez persons to whom the Software is furnished to do so, subject to the
33*0c65ac1dSAntonio Huete Jimenez following conditions:
34*0c65ac1dSAntonio Huete Jimenez
35*0c65ac1dSAntonio Huete Jimenez The above copyright notice and this permission notice shall be included
36*0c65ac1dSAntonio Huete Jimenez in all copies or substantial portions of the Software.
37*0c65ac1dSAntonio Huete Jimenez
38*0c65ac1dSAntonio Huete Jimenez THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39*0c65ac1dSAntonio Huete Jimenez EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40*0c65ac1dSAntonio Huete Jimenez MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
41*0c65ac1dSAntonio Huete Jimenez NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
42*0c65ac1dSAntonio Huete Jimenez DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
43*0c65ac1dSAntonio Huete Jimenez OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
44*0c65ac1dSAntonio Huete Jimenez USE OR OTHER DEALINGS IN THE SOFTWARE.
45fb9a9224SMatthew Dillon */
46fb9a9224SMatthew Dillon
47fb9a9224SMatthew Dillon #include <expat_config.h>
48*0c65ac1dSAntonio Huete Jimenez
49*0c65ac1dSAntonio Huete Jimenez #include <stddef.h>
50*0c65ac1dSAntonio Huete Jimenez #include <string.h> /* memcpy */
51*0c65ac1dSAntonio Huete Jimenez #include <stdbool.h>
52*0c65ac1dSAntonio Huete Jimenez
53*0c65ac1dSAntonio Huete Jimenez #ifdef _WIN32
54*0c65ac1dSAntonio Huete Jimenez # include "winconfig.h"
55fb9a9224SMatthew Dillon #endif
56fb9a9224SMatthew Dillon
57fb9a9224SMatthew Dillon #include "expat_external.h"
58fb9a9224SMatthew Dillon #include "internal.h"
59fb9a9224SMatthew Dillon #include "xmltok.h"
60fb9a9224SMatthew Dillon #include "nametab.h"
61fb9a9224SMatthew Dillon
62fb9a9224SMatthew Dillon #ifdef XML_DTD
63fb9a9224SMatthew Dillon # define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
64fb9a9224SMatthew Dillon #else
65fb9a9224SMatthew Dillon # define IGNORE_SECTION_TOK_VTABLE /* as nothing */
66fb9a9224SMatthew Dillon #endif
67fb9a9224SMatthew Dillon
68fb9a9224SMatthew Dillon #define VTABLE1 \
69fb9a9224SMatthew Dillon {PREFIX(prologTok), PREFIX(contentTok), \
70fb9a9224SMatthew Dillon PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE}, \
71fb9a9224SMatthew Dillon {PREFIX(attributeValueTok), PREFIX(entityValueTok)}, \
72*0c65ac1dSAntonio Huete Jimenez PREFIX(nameMatchesAscii), PREFIX(nameLength), PREFIX(skipS), \
73*0c65ac1dSAntonio Huete Jimenez PREFIX(getAtts), PREFIX(charRefNumber), PREFIX(predefinedEntityName), \
74*0c65ac1dSAntonio Huete Jimenez PREFIX(updatePosition), PREFIX(isPublicId)
75fb9a9224SMatthew Dillon
76fb9a9224SMatthew Dillon #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
77fb9a9224SMatthew Dillon
78fb9a9224SMatthew Dillon #define UCS2_GET_NAMING(pages, hi, lo) \
79*0c65ac1dSAntonio Huete Jimenez (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1u << ((lo)&0x1F)))
80fb9a9224SMatthew Dillon
81fb9a9224SMatthew Dillon /* A 2 byte UTF-8 representation splits the characters 11 bits between
82fb9a9224SMatthew Dillon the bottom 5 and 6 bits of the bytes. We need 8 bits to index into
83fb9a9224SMatthew Dillon pages, 3 bits to add to that index and 5 bits to generate the mask.
84fb9a9224SMatthew Dillon */
85fb9a9224SMatthew Dillon #define UTF8_GET_NAMING2(pages, byte) \
86fb9a9224SMatthew Dillon (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
87*0c65ac1dSAntonio Huete Jimenez + ((((byte)[0]) & 3) << 1) + ((((byte)[1]) >> 5) & 1)] \
88*0c65ac1dSAntonio Huete Jimenez & (1u << (((byte)[1]) & 0x1F)))
89fb9a9224SMatthew Dillon
90fb9a9224SMatthew Dillon /* A 3 byte UTF-8 representation splits the characters 16 bits between
91fb9a9224SMatthew Dillon the bottom 4, 6 and 6 bits of the bytes. We need 8 bits to index
92fb9a9224SMatthew Dillon into pages, 3 bits to add to that index and 5 bits to generate the
93fb9a9224SMatthew Dillon mask.
94fb9a9224SMatthew Dillon */
95fb9a9224SMatthew Dillon #define UTF8_GET_NAMING3(pages, byte) \
96*0c65ac1dSAntonio Huete Jimenez (namingBitmap \
97*0c65ac1dSAntonio Huete Jimenez [((pages)[((((byte)[0]) & 0xF) << 4) + ((((byte)[1]) >> 2) & 0xF)] \
98fb9a9224SMatthew Dillon << 3) \
99*0c65ac1dSAntonio Huete Jimenez + ((((byte)[1]) & 3) << 1) + ((((byte)[2]) >> 5) & 1)] \
100*0c65ac1dSAntonio Huete Jimenez & (1u << (((byte)[2]) & 0x1F)))
101fb9a9224SMatthew Dillon
102fb9a9224SMatthew Dillon /* Detection of invalid UTF-8 sequences is based on Table 3.1B
103fb9a9224SMatthew Dillon of Unicode 3.2: http://www.unicode.org/unicode/reports/tr28/
104fb9a9224SMatthew Dillon with the additional restriction of not allowing the Unicode
105fb9a9224SMatthew Dillon code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
106fb9a9224SMatthew Dillon Implementation details:
107fb9a9224SMatthew Dillon (A & 0x80) == 0 means A < 0x80
108fb9a9224SMatthew Dillon and
109fb9a9224SMatthew Dillon (A & 0xC0) == 0xC0 means A > 0xBF
110fb9a9224SMatthew Dillon */
111fb9a9224SMatthew Dillon
112fb9a9224SMatthew Dillon #define UTF8_INVALID2(p) \
113fb9a9224SMatthew Dillon ((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
114fb9a9224SMatthew Dillon
115fb9a9224SMatthew Dillon #define UTF8_INVALID3(p) \
116fb9a9224SMatthew Dillon (((p)[2] & 0x80) == 0 \
117*0c65ac1dSAntonio Huete Jimenez || ((*p) == 0xEF && (p)[1] == 0xBF ? (p)[2] > 0xBD \
118*0c65ac1dSAntonio Huete Jimenez : ((p)[2] & 0xC0) == 0xC0) \
119*0c65ac1dSAntonio Huete Jimenez || ((*p) == 0xE0 \
120*0c65ac1dSAntonio Huete Jimenez ? (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
121*0c65ac1dSAntonio Huete Jimenez : ((p)[1] & 0x80) == 0 \
122*0c65ac1dSAntonio Huete Jimenez || ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
123fb9a9224SMatthew Dillon
124fb9a9224SMatthew Dillon #define UTF8_INVALID4(p) \
125*0c65ac1dSAntonio Huete Jimenez (((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 || ((p)[2] & 0x80) == 0 \
126*0c65ac1dSAntonio Huete Jimenez || ((p)[2] & 0xC0) == 0xC0 \
127*0c65ac1dSAntonio Huete Jimenez || ((*p) == 0xF0 \
128*0c65ac1dSAntonio Huete Jimenez ? (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
129*0c65ac1dSAntonio Huete Jimenez : ((p)[1] & 0x80) == 0 \
130*0c65ac1dSAntonio Huete Jimenez || ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
131fb9a9224SMatthew Dillon
132fb9a9224SMatthew Dillon static int PTRFASTCALL
isNever(const ENCODING * enc,const char * p)133*0c65ac1dSAntonio Huete Jimenez isNever(const ENCODING *enc, const char *p) {
134*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
135*0c65ac1dSAntonio Huete Jimenez UNUSED_P(p);
136fb9a9224SMatthew Dillon return 0;
137fb9a9224SMatthew Dillon }
138fb9a9224SMatthew Dillon
139fb9a9224SMatthew Dillon static int PTRFASTCALL
utf8_isName2(const ENCODING * enc,const char * p)140*0c65ac1dSAntonio Huete Jimenez utf8_isName2(const ENCODING *enc, const char *p) {
141*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
142fb9a9224SMatthew Dillon return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
143fb9a9224SMatthew Dillon }
144fb9a9224SMatthew Dillon
145fb9a9224SMatthew Dillon static int PTRFASTCALL
utf8_isName3(const ENCODING * enc,const char * p)146*0c65ac1dSAntonio Huete Jimenez utf8_isName3(const ENCODING *enc, const char *p) {
147*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
148fb9a9224SMatthew Dillon return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
149fb9a9224SMatthew Dillon }
150fb9a9224SMatthew Dillon
151fb9a9224SMatthew Dillon #define utf8_isName4 isNever
152fb9a9224SMatthew Dillon
153fb9a9224SMatthew Dillon static int PTRFASTCALL
utf8_isNmstrt2(const ENCODING * enc,const char * p)154*0c65ac1dSAntonio Huete Jimenez utf8_isNmstrt2(const ENCODING *enc, const char *p) {
155*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
156fb9a9224SMatthew Dillon return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
157fb9a9224SMatthew Dillon }
158fb9a9224SMatthew Dillon
159fb9a9224SMatthew Dillon static int PTRFASTCALL
utf8_isNmstrt3(const ENCODING * enc,const char * p)160*0c65ac1dSAntonio Huete Jimenez utf8_isNmstrt3(const ENCODING *enc, const char *p) {
161*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
162fb9a9224SMatthew Dillon return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
163fb9a9224SMatthew Dillon }
164fb9a9224SMatthew Dillon
165fb9a9224SMatthew Dillon #define utf8_isNmstrt4 isNever
166fb9a9224SMatthew Dillon
167fb9a9224SMatthew Dillon static int PTRFASTCALL
utf8_isInvalid2(const ENCODING * enc,const char * p)168*0c65ac1dSAntonio Huete Jimenez utf8_isInvalid2(const ENCODING *enc, const char *p) {
169*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
170fb9a9224SMatthew Dillon return UTF8_INVALID2((const unsigned char *)p);
171fb9a9224SMatthew Dillon }
172fb9a9224SMatthew Dillon
173fb9a9224SMatthew Dillon static int PTRFASTCALL
utf8_isInvalid3(const ENCODING * enc,const char * p)174*0c65ac1dSAntonio Huete Jimenez utf8_isInvalid3(const ENCODING *enc, const char *p) {
175*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
176fb9a9224SMatthew Dillon return UTF8_INVALID3((const unsigned char *)p);
177fb9a9224SMatthew Dillon }
178fb9a9224SMatthew Dillon
179fb9a9224SMatthew Dillon static int PTRFASTCALL
utf8_isInvalid4(const ENCODING * enc,const char * p)180*0c65ac1dSAntonio Huete Jimenez utf8_isInvalid4(const ENCODING *enc, const char *p) {
181*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
182fb9a9224SMatthew Dillon return UTF8_INVALID4((const unsigned char *)p);
183fb9a9224SMatthew Dillon }
184fb9a9224SMatthew Dillon
185fb9a9224SMatthew Dillon struct normal_encoding {
186fb9a9224SMatthew Dillon ENCODING enc;
187fb9a9224SMatthew Dillon unsigned char type[256];
188fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
189fb9a9224SMatthew Dillon int(PTRFASTCALL *byteType)(const ENCODING *, const char *);
190fb9a9224SMatthew Dillon int(PTRFASTCALL *isNameMin)(const ENCODING *, const char *);
191fb9a9224SMatthew Dillon int(PTRFASTCALL *isNmstrtMin)(const ENCODING *, const char *);
192fb9a9224SMatthew Dillon int(PTRFASTCALL *byteToAscii)(const ENCODING *, const char *);
193fb9a9224SMatthew Dillon int(PTRCALL *charMatches)(const ENCODING *, const char *, int);
194fb9a9224SMatthew Dillon #endif /* XML_MIN_SIZE */
195fb9a9224SMatthew Dillon int(PTRFASTCALL *isName2)(const ENCODING *, const char *);
196fb9a9224SMatthew Dillon int(PTRFASTCALL *isName3)(const ENCODING *, const char *);
197fb9a9224SMatthew Dillon int(PTRFASTCALL *isName4)(const ENCODING *, const char *);
198fb9a9224SMatthew Dillon int(PTRFASTCALL *isNmstrt2)(const ENCODING *, const char *);
199fb9a9224SMatthew Dillon int(PTRFASTCALL *isNmstrt3)(const ENCODING *, const char *);
200fb9a9224SMatthew Dillon int(PTRFASTCALL *isNmstrt4)(const ENCODING *, const char *);
201fb9a9224SMatthew Dillon int(PTRFASTCALL *isInvalid2)(const ENCODING *, const char *);
202fb9a9224SMatthew Dillon int(PTRFASTCALL *isInvalid3)(const ENCODING *, const char *);
203fb9a9224SMatthew Dillon int(PTRFASTCALL *isInvalid4)(const ENCODING *, const char *);
204fb9a9224SMatthew Dillon };
205fb9a9224SMatthew Dillon
206fb9a9224SMatthew Dillon #define AS_NORMAL_ENCODING(enc) ((const struct normal_encoding *)(enc))
207fb9a9224SMatthew Dillon
208fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
209fb9a9224SMatthew Dillon
210fb9a9224SMatthew Dillon # define STANDARD_VTABLE(E) \
211*0c65ac1dSAntonio Huete Jimenez E##byteType, E##isNameMin, E##isNmstrtMin, E##byteToAscii, E##charMatches,
212fb9a9224SMatthew Dillon
213fb9a9224SMatthew Dillon #else
214fb9a9224SMatthew Dillon
215fb9a9224SMatthew Dillon # define STANDARD_VTABLE(E) /* as nothing */
216fb9a9224SMatthew Dillon
217fb9a9224SMatthew Dillon #endif
218fb9a9224SMatthew Dillon
219fb9a9224SMatthew Dillon #define NORMAL_VTABLE(E) \
220*0c65ac1dSAntonio Huete Jimenez E##isName2, E##isName3, E##isName4, E##isNmstrt2, E##isNmstrt3, \
221*0c65ac1dSAntonio Huete Jimenez E##isNmstrt4, E##isInvalid2, E##isInvalid3, E##isInvalid4
222*0c65ac1dSAntonio Huete Jimenez
223*0c65ac1dSAntonio Huete Jimenez #define NULL_VTABLE \
224*0c65ac1dSAntonio Huete Jimenez /* isName2 */ NULL, /* isName3 */ NULL, /* isName4 */ NULL, \
225*0c65ac1dSAntonio Huete Jimenez /* isNmstrt2 */ NULL, /* isNmstrt3 */ NULL, /* isNmstrt4 */ NULL, \
226*0c65ac1dSAntonio Huete Jimenez /* isInvalid2 */ NULL, /* isInvalid3 */ NULL, /* isInvalid4 */ NULL
227fb9a9224SMatthew Dillon
228fb9a9224SMatthew Dillon static int FASTCALL checkCharRefNumber(int);
229fb9a9224SMatthew Dillon
230fb9a9224SMatthew Dillon #include "xmltok_impl.h"
231fb9a9224SMatthew Dillon #include "ascii.h"
232fb9a9224SMatthew Dillon
233fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
234fb9a9224SMatthew Dillon # define sb_isNameMin isNever
235fb9a9224SMatthew Dillon # define sb_isNmstrtMin isNever
236fb9a9224SMatthew Dillon #endif
237fb9a9224SMatthew Dillon
238fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
239fb9a9224SMatthew Dillon # define MINBPC(enc) ((enc)->minBytesPerChar)
240fb9a9224SMatthew Dillon #else
241fb9a9224SMatthew Dillon /* minimum bytes per character */
242fb9a9224SMatthew Dillon # define MINBPC(enc) 1
243fb9a9224SMatthew Dillon #endif
244fb9a9224SMatthew Dillon
245fb9a9224SMatthew Dillon #define SB_BYTE_TYPE(enc, p) \
246fb9a9224SMatthew Dillon (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
247fb9a9224SMatthew Dillon
248fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
249fb9a9224SMatthew Dillon static int PTRFASTCALL
sb_byteType(const ENCODING * enc,const char * p)250*0c65ac1dSAntonio Huete Jimenez sb_byteType(const ENCODING *enc, const char *p) {
251fb9a9224SMatthew Dillon return SB_BYTE_TYPE(enc, p);
252fb9a9224SMatthew Dillon }
253*0c65ac1dSAntonio Huete Jimenez # define BYTE_TYPE(enc, p) (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
254fb9a9224SMatthew Dillon #else
255fb9a9224SMatthew Dillon # define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
256fb9a9224SMatthew Dillon #endif
257fb9a9224SMatthew Dillon
258fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
259*0c65ac1dSAntonio Huete Jimenez # define BYTE_TO_ASCII(enc, p) (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
260fb9a9224SMatthew Dillon static int PTRFASTCALL
sb_byteToAscii(const ENCODING * enc,const char * p)261*0c65ac1dSAntonio Huete Jimenez sb_byteToAscii(const ENCODING *enc, const char *p) {
262*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
263fb9a9224SMatthew Dillon return *p;
264fb9a9224SMatthew Dillon }
265fb9a9224SMatthew Dillon #else
266fb9a9224SMatthew Dillon # define BYTE_TO_ASCII(enc, p) (*(p))
267fb9a9224SMatthew Dillon #endif
268fb9a9224SMatthew Dillon
269*0c65ac1dSAntonio Huete Jimenez #define IS_NAME_CHAR(enc, p, n) (AS_NORMAL_ENCODING(enc)->isName##n(enc, p))
270*0c65ac1dSAntonio Huete Jimenez #define IS_NMSTRT_CHAR(enc, p, n) (AS_NORMAL_ENCODING(enc)->isNmstrt##n(enc, p))
271*0c65ac1dSAntonio Huete Jimenez #ifdef XML_MIN_SIZE
272*0c65ac1dSAntonio Huete Jimenez # define IS_INVALID_CHAR(enc, p, n) \
273*0c65ac1dSAntonio Huete Jimenez (AS_NORMAL_ENCODING(enc)->isInvalid##n \
274*0c65ac1dSAntonio Huete Jimenez && AS_NORMAL_ENCODING(enc)->isInvalid##n(enc, p))
275*0c65ac1dSAntonio Huete Jimenez #else
276fb9a9224SMatthew Dillon # define IS_INVALID_CHAR(enc, p, n) \
277fb9a9224SMatthew Dillon (AS_NORMAL_ENCODING(enc)->isInvalid##n(enc, p))
278*0c65ac1dSAntonio Huete Jimenez #endif
279fb9a9224SMatthew Dillon
280fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
281fb9a9224SMatthew Dillon # define IS_NAME_CHAR_MINBPC(enc, p) \
282fb9a9224SMatthew Dillon (AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
283fb9a9224SMatthew Dillon # define IS_NMSTRT_CHAR_MINBPC(enc, p) \
284fb9a9224SMatthew Dillon (AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
285fb9a9224SMatthew Dillon #else
286fb9a9224SMatthew Dillon # define IS_NAME_CHAR_MINBPC(enc, p) (0)
287fb9a9224SMatthew Dillon # define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
288fb9a9224SMatthew Dillon #endif
289fb9a9224SMatthew Dillon
290fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
291fb9a9224SMatthew Dillon # define CHAR_MATCHES(enc, p, c) \
292fb9a9224SMatthew Dillon (AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
293fb9a9224SMatthew Dillon static int PTRCALL
sb_charMatches(const ENCODING * enc,const char * p,int c)294*0c65ac1dSAntonio Huete Jimenez sb_charMatches(const ENCODING *enc, const char *p, int c) {
295*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
296fb9a9224SMatthew Dillon return *p == c;
297fb9a9224SMatthew Dillon }
298fb9a9224SMatthew Dillon #else
299fb9a9224SMatthew Dillon /* c is an ASCII character */
300*0c65ac1dSAntonio Huete Jimenez # define CHAR_MATCHES(enc, p, c) (*(p) == (c))
301fb9a9224SMatthew Dillon #endif
302fb9a9224SMatthew Dillon
303fb9a9224SMatthew Dillon #define PREFIX(ident) normal_##ident
304fb9a9224SMatthew Dillon #define XML_TOK_IMPL_C
305fb9a9224SMatthew Dillon #include "xmltok_impl.c"
306fb9a9224SMatthew Dillon #undef XML_TOK_IMPL_C
307fb9a9224SMatthew Dillon
308fb9a9224SMatthew Dillon #undef MINBPC
309fb9a9224SMatthew Dillon #undef BYTE_TYPE
310fb9a9224SMatthew Dillon #undef BYTE_TO_ASCII
311fb9a9224SMatthew Dillon #undef CHAR_MATCHES
312fb9a9224SMatthew Dillon #undef IS_NAME_CHAR
313fb9a9224SMatthew Dillon #undef IS_NAME_CHAR_MINBPC
314fb9a9224SMatthew Dillon #undef IS_NMSTRT_CHAR
315fb9a9224SMatthew Dillon #undef IS_NMSTRT_CHAR_MINBPC
316fb9a9224SMatthew Dillon #undef IS_INVALID_CHAR
317fb9a9224SMatthew Dillon
318fb9a9224SMatthew Dillon enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
319fb9a9224SMatthew Dillon UTF8_cval1 = 0x00,
320fb9a9224SMatthew Dillon UTF8_cval2 = 0xc0,
321fb9a9224SMatthew Dillon UTF8_cval3 = 0xe0,
322fb9a9224SMatthew Dillon UTF8_cval4 = 0xf0
323fb9a9224SMatthew Dillon };
324fb9a9224SMatthew Dillon
325*0c65ac1dSAntonio Huete Jimenez void
_INTERNAL_trim_to_complete_utf8_characters(const char * from,const char ** fromLimRef)326*0c65ac1dSAntonio Huete Jimenez _INTERNAL_trim_to_complete_utf8_characters(const char *from,
327*0c65ac1dSAntonio Huete Jimenez const char **fromLimRef) {
328*0c65ac1dSAntonio Huete Jimenez const char *fromLim = *fromLimRef;
329*0c65ac1dSAntonio Huete Jimenez size_t walked = 0;
330*0c65ac1dSAntonio Huete Jimenez for (; fromLim > from; fromLim--, walked++) {
331*0c65ac1dSAntonio Huete Jimenez const unsigned char prev = (unsigned char)fromLim[-1];
332*0c65ac1dSAntonio Huete Jimenez if ((prev & 0xf8u)
333*0c65ac1dSAntonio Huete Jimenez == 0xf0u) { /* 4-byte character, lead by 0b11110xxx byte */
334*0c65ac1dSAntonio Huete Jimenez if (walked + 1 >= 4) {
335*0c65ac1dSAntonio Huete Jimenez fromLim += 4 - 1;
336*0c65ac1dSAntonio Huete Jimenez break;
337*0c65ac1dSAntonio Huete Jimenez } else {
338*0c65ac1dSAntonio Huete Jimenez walked = 0;
339*0c65ac1dSAntonio Huete Jimenez }
340*0c65ac1dSAntonio Huete Jimenez } else if ((prev & 0xf0u)
341*0c65ac1dSAntonio Huete Jimenez == 0xe0u) { /* 3-byte character, lead by 0b1110xxxx byte */
342*0c65ac1dSAntonio Huete Jimenez if (walked + 1 >= 3) {
343*0c65ac1dSAntonio Huete Jimenez fromLim += 3 - 1;
344*0c65ac1dSAntonio Huete Jimenez break;
345*0c65ac1dSAntonio Huete Jimenez } else {
346*0c65ac1dSAntonio Huete Jimenez walked = 0;
347*0c65ac1dSAntonio Huete Jimenez }
348*0c65ac1dSAntonio Huete Jimenez } else if ((prev & 0xe0u)
349*0c65ac1dSAntonio Huete Jimenez == 0xc0u) { /* 2-byte character, lead by 0b110xxxxx byte */
350*0c65ac1dSAntonio Huete Jimenez if (walked + 1 >= 2) {
351*0c65ac1dSAntonio Huete Jimenez fromLim += 2 - 1;
352*0c65ac1dSAntonio Huete Jimenez break;
353*0c65ac1dSAntonio Huete Jimenez } else {
354*0c65ac1dSAntonio Huete Jimenez walked = 0;
355*0c65ac1dSAntonio Huete Jimenez }
356*0c65ac1dSAntonio Huete Jimenez } else if ((prev & 0x80u)
357*0c65ac1dSAntonio Huete Jimenez == 0x00u) { /* 1-byte character, matching 0b0xxxxxxx */
358fb9a9224SMatthew Dillon break;
359fb9a9224SMatthew Dillon }
360*0c65ac1dSAntonio Huete Jimenez }
361*0c65ac1dSAntonio Huete Jimenez *fromLimRef = fromLim;
362fb9a9224SMatthew Dillon }
363fb9a9224SMatthew Dillon
364*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL
utf8_toUtf8(const ENCODING * enc,const char ** fromP,const char * fromLim,char ** toP,const char * toLim)365*0c65ac1dSAntonio Huete Jimenez utf8_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
366*0c65ac1dSAntonio Huete Jimenez char **toP, const char *toLim) {
367*0c65ac1dSAntonio Huete Jimenez bool input_incomplete = false;
368*0c65ac1dSAntonio Huete Jimenez bool output_exhausted = false;
369*0c65ac1dSAntonio Huete Jimenez
370*0c65ac1dSAntonio Huete Jimenez /* Avoid copying partial characters (due to limited space). */
371*0c65ac1dSAntonio Huete Jimenez const ptrdiff_t bytesAvailable = fromLim - *fromP;
372*0c65ac1dSAntonio Huete Jimenez const ptrdiff_t bytesStorable = toLim - *toP;
373*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
374*0c65ac1dSAntonio Huete Jimenez if (bytesAvailable > bytesStorable) {
375*0c65ac1dSAntonio Huete Jimenez fromLim = *fromP + bytesStorable;
376*0c65ac1dSAntonio Huete Jimenez output_exhausted = true;
377*0c65ac1dSAntonio Huete Jimenez }
378*0c65ac1dSAntonio Huete Jimenez
379*0c65ac1dSAntonio Huete Jimenez /* Avoid copying partial characters (from incomplete input). */
380fb9a9224SMatthew Dillon {
381*0c65ac1dSAntonio Huete Jimenez const char *const fromLimBefore = fromLim;
382*0c65ac1dSAntonio Huete Jimenez _INTERNAL_trim_to_complete_utf8_characters(*fromP, &fromLim);
383*0c65ac1dSAntonio Huete Jimenez if (fromLim < fromLimBefore) {
384*0c65ac1dSAntonio Huete Jimenez input_incomplete = true;
385*0c65ac1dSAntonio Huete Jimenez }
386*0c65ac1dSAntonio Huete Jimenez }
387*0c65ac1dSAntonio Huete Jimenez
388*0c65ac1dSAntonio Huete Jimenez {
389*0c65ac1dSAntonio Huete Jimenez const ptrdiff_t bytesToCopy = fromLim - *fromP;
390*0c65ac1dSAntonio Huete Jimenez memcpy(*toP, *fromP, bytesToCopy);
391*0c65ac1dSAntonio Huete Jimenez *fromP += bytesToCopy;
392*0c65ac1dSAntonio Huete Jimenez *toP += bytesToCopy;
393*0c65ac1dSAntonio Huete Jimenez }
394*0c65ac1dSAntonio Huete Jimenez
395*0c65ac1dSAntonio Huete Jimenez if (output_exhausted) /* needs to go first */
396*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED;
397*0c65ac1dSAntonio Huete Jimenez else if (input_incomplete)
398*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_INPUT_INCOMPLETE;
399*0c65ac1dSAntonio Huete Jimenez else
400*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_COMPLETED;
401*0c65ac1dSAntonio Huete Jimenez }
402*0c65ac1dSAntonio Huete Jimenez
403*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL
utf8_toUtf16(const ENCODING * enc,const char ** fromP,const char * fromLim,unsigned short ** toP,const unsigned short * toLim)404*0c65ac1dSAntonio Huete Jimenez utf8_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
405*0c65ac1dSAntonio Huete Jimenez unsigned short **toP, const unsigned short *toLim) {
406*0c65ac1dSAntonio Huete Jimenez enum XML_Convert_Result res = XML_CONVERT_COMPLETED;
407fb9a9224SMatthew Dillon unsigned short *to = *toP;
408fb9a9224SMatthew Dillon const char *from = *fromP;
409*0c65ac1dSAntonio Huete Jimenez while (from < fromLim && to < toLim) {
410fb9a9224SMatthew Dillon switch (((struct normal_encoding *)enc)->type[(unsigned char)*from]) {
411fb9a9224SMatthew Dillon case BT_LEAD2:
412*0c65ac1dSAntonio Huete Jimenez if (fromLim - from < 2) {
413*0c65ac1dSAntonio Huete Jimenez res = XML_CONVERT_INPUT_INCOMPLETE;
414*0c65ac1dSAntonio Huete Jimenez goto after;
415*0c65ac1dSAntonio Huete Jimenez }
416fb9a9224SMatthew Dillon *to++ = (unsigned short)(((from[0] & 0x1f) << 6) | (from[1] & 0x3f));
417fb9a9224SMatthew Dillon from += 2;
418fb9a9224SMatthew Dillon break;
419fb9a9224SMatthew Dillon case BT_LEAD3:
420*0c65ac1dSAntonio Huete Jimenez if (fromLim - from < 3) {
421*0c65ac1dSAntonio Huete Jimenez res = XML_CONVERT_INPUT_INCOMPLETE;
422*0c65ac1dSAntonio Huete Jimenez goto after;
423*0c65ac1dSAntonio Huete Jimenez }
424*0c65ac1dSAntonio Huete Jimenez *to++ = (unsigned short)(((from[0] & 0xf) << 12) | ((from[1] & 0x3f) << 6)
425*0c65ac1dSAntonio Huete Jimenez | (from[2] & 0x3f));
426fb9a9224SMatthew Dillon from += 3;
427fb9a9224SMatthew Dillon break;
428*0c65ac1dSAntonio Huete Jimenez case BT_LEAD4: {
429fb9a9224SMatthew Dillon unsigned long n;
430*0c65ac1dSAntonio Huete Jimenez if (toLim - to < 2) {
431*0c65ac1dSAntonio Huete Jimenez res = XML_CONVERT_OUTPUT_EXHAUSTED;
432fb9a9224SMatthew Dillon goto after;
433*0c65ac1dSAntonio Huete Jimenez }
434*0c65ac1dSAntonio Huete Jimenez if (fromLim - from < 4) {
435*0c65ac1dSAntonio Huete Jimenez res = XML_CONVERT_INPUT_INCOMPLETE;
436*0c65ac1dSAntonio Huete Jimenez goto after;
437*0c65ac1dSAntonio Huete Jimenez }
438fb9a9224SMatthew Dillon n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12)
439fb9a9224SMatthew Dillon | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
440fb9a9224SMatthew Dillon n -= 0x10000;
441fb9a9224SMatthew Dillon to[0] = (unsigned short)((n >> 10) | 0xD800);
442fb9a9224SMatthew Dillon to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
443fb9a9224SMatthew Dillon to += 2;
444fb9a9224SMatthew Dillon from += 4;
445*0c65ac1dSAntonio Huete Jimenez } break;
446fb9a9224SMatthew Dillon default:
447fb9a9224SMatthew Dillon *to++ = *from++;
448fb9a9224SMatthew Dillon break;
449fb9a9224SMatthew Dillon }
450fb9a9224SMatthew Dillon }
451*0c65ac1dSAntonio Huete Jimenez if (from < fromLim)
452*0c65ac1dSAntonio Huete Jimenez res = XML_CONVERT_OUTPUT_EXHAUSTED;
453fb9a9224SMatthew Dillon after:
454fb9a9224SMatthew Dillon *fromP = from;
455fb9a9224SMatthew Dillon *toP = to;
456*0c65ac1dSAntonio Huete Jimenez return res;
457fb9a9224SMatthew Dillon }
458fb9a9224SMatthew Dillon
459fb9a9224SMatthew Dillon #ifdef XML_NS
460*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding utf8_encoding_ns
461*0c65ac1dSAntonio Huete Jimenez = {{VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0},
462fb9a9224SMatthew Dillon {
463fb9a9224SMatthew Dillon # include "asciitab.h"
464fb9a9224SMatthew Dillon # include "utf8tab.h"
465fb9a9224SMatthew Dillon },
466*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)};
467fb9a9224SMatthew Dillon #endif
468fb9a9224SMatthew Dillon
469*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding utf8_encoding
470*0c65ac1dSAntonio Huete Jimenez = {{VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0},
471fb9a9224SMatthew Dillon {
472fb9a9224SMatthew Dillon #define BT_COLON BT_NMSTRT
473fb9a9224SMatthew Dillon #include "asciitab.h"
474fb9a9224SMatthew Dillon #undef BT_COLON
475fb9a9224SMatthew Dillon #include "utf8tab.h"
476fb9a9224SMatthew Dillon },
477*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)};
478fb9a9224SMatthew Dillon
479fb9a9224SMatthew Dillon #ifdef XML_NS
480fb9a9224SMatthew Dillon
481*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding internal_utf8_encoding_ns
482*0c65ac1dSAntonio Huete Jimenez = {{VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0},
483fb9a9224SMatthew Dillon {
484fb9a9224SMatthew Dillon # include "iasciitab.h"
485fb9a9224SMatthew Dillon # include "utf8tab.h"
486fb9a9224SMatthew Dillon },
487*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)};
488fb9a9224SMatthew Dillon
489fb9a9224SMatthew Dillon #endif
490fb9a9224SMatthew Dillon
491*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding internal_utf8_encoding
492*0c65ac1dSAntonio Huete Jimenez = {{VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0},
493fb9a9224SMatthew Dillon {
494fb9a9224SMatthew Dillon #define BT_COLON BT_NMSTRT
495fb9a9224SMatthew Dillon #include "iasciitab.h"
496fb9a9224SMatthew Dillon #undef BT_COLON
497fb9a9224SMatthew Dillon #include "utf8tab.h"
498fb9a9224SMatthew Dillon },
499*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)};
500fb9a9224SMatthew Dillon
501*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL
latin1_toUtf8(const ENCODING * enc,const char ** fromP,const char * fromLim,char ** toP,const char * toLim)502*0c65ac1dSAntonio Huete Jimenez latin1_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
503*0c65ac1dSAntonio Huete Jimenez char **toP, const char *toLim) {
504*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
505fb9a9224SMatthew Dillon for (;;) {
506fb9a9224SMatthew Dillon unsigned char c;
507fb9a9224SMatthew Dillon if (*fromP == fromLim)
508*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_COMPLETED;
509fb9a9224SMatthew Dillon c = (unsigned char)**fromP;
510fb9a9224SMatthew Dillon if (c & 0x80) {
511fb9a9224SMatthew Dillon if (toLim - *toP < 2)
512*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED;
513fb9a9224SMatthew Dillon *(*toP)++ = (char)((c >> 6) | UTF8_cval2);
514fb9a9224SMatthew Dillon *(*toP)++ = (char)((c & 0x3f) | 0x80);
515fb9a9224SMatthew Dillon (*fromP)++;
516*0c65ac1dSAntonio Huete Jimenez } else {
517fb9a9224SMatthew Dillon if (*toP == toLim)
518*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED;
519fb9a9224SMatthew Dillon *(*toP)++ = *(*fromP)++;
520fb9a9224SMatthew Dillon }
521fb9a9224SMatthew Dillon }
522fb9a9224SMatthew Dillon }
523fb9a9224SMatthew Dillon
524*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL
latin1_toUtf16(const ENCODING * enc,const char ** fromP,const char * fromLim,unsigned short ** toP,const unsigned short * toLim)525*0c65ac1dSAntonio Huete Jimenez latin1_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
526*0c65ac1dSAntonio Huete Jimenez unsigned short **toP, const unsigned short *toLim) {
527*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
528*0c65ac1dSAntonio Huete Jimenez while (*fromP < fromLim && *toP < toLim)
529fb9a9224SMatthew Dillon *(*toP)++ = (unsigned char)*(*fromP)++;
530*0c65ac1dSAntonio Huete Jimenez
531*0c65ac1dSAntonio Huete Jimenez if ((*toP == toLim) && (*fromP < fromLim))
532*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED;
533*0c65ac1dSAntonio Huete Jimenez else
534*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_COMPLETED;
535fb9a9224SMatthew Dillon }
536fb9a9224SMatthew Dillon
537fb9a9224SMatthew Dillon #ifdef XML_NS
538fb9a9224SMatthew Dillon
539*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding latin1_encoding_ns
540*0c65ac1dSAntonio Huete Jimenez = {{VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0},
541fb9a9224SMatthew Dillon {
542fb9a9224SMatthew Dillon # include "asciitab.h"
543fb9a9224SMatthew Dillon # include "latin1tab.h"
544fb9a9224SMatthew Dillon },
545*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(sb_) NULL_VTABLE};
546fb9a9224SMatthew Dillon
547fb9a9224SMatthew Dillon #endif
548fb9a9224SMatthew Dillon
549*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding latin1_encoding
550*0c65ac1dSAntonio Huete Jimenez = {{VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0},
551fb9a9224SMatthew Dillon {
552fb9a9224SMatthew Dillon #define BT_COLON BT_NMSTRT
553fb9a9224SMatthew Dillon #include "asciitab.h"
554fb9a9224SMatthew Dillon #undef BT_COLON
555fb9a9224SMatthew Dillon #include "latin1tab.h"
556fb9a9224SMatthew Dillon },
557*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(sb_) NULL_VTABLE};
558fb9a9224SMatthew Dillon
559*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL
ascii_toUtf8(const ENCODING * enc,const char ** fromP,const char * fromLim,char ** toP,const char * toLim)560*0c65ac1dSAntonio Huete Jimenez ascii_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
561*0c65ac1dSAntonio Huete Jimenez char **toP, const char *toLim) {
562*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
563*0c65ac1dSAntonio Huete Jimenez while (*fromP < fromLim && *toP < toLim)
564fb9a9224SMatthew Dillon *(*toP)++ = *(*fromP)++;
565*0c65ac1dSAntonio Huete Jimenez
566*0c65ac1dSAntonio Huete Jimenez if ((*toP == toLim) && (*fromP < fromLim))
567*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED;
568*0c65ac1dSAntonio Huete Jimenez else
569*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_COMPLETED;
570fb9a9224SMatthew Dillon }
571fb9a9224SMatthew Dillon
572fb9a9224SMatthew Dillon #ifdef XML_NS
573fb9a9224SMatthew Dillon
574*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding ascii_encoding_ns
575*0c65ac1dSAntonio Huete Jimenez = {{VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0},
576fb9a9224SMatthew Dillon {
577fb9a9224SMatthew Dillon # include "asciitab.h"
578fb9a9224SMatthew Dillon /* BT_NONXML == 0 */
579fb9a9224SMatthew Dillon },
580*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(sb_) NULL_VTABLE};
581fb9a9224SMatthew Dillon
582fb9a9224SMatthew Dillon #endif
583fb9a9224SMatthew Dillon
584*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding ascii_encoding
585*0c65ac1dSAntonio Huete Jimenez = {{VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0},
586fb9a9224SMatthew Dillon {
587fb9a9224SMatthew Dillon #define BT_COLON BT_NMSTRT
588fb9a9224SMatthew Dillon #include "asciitab.h"
589fb9a9224SMatthew Dillon #undef BT_COLON
590fb9a9224SMatthew Dillon /* BT_NONXML == 0 */
591fb9a9224SMatthew Dillon },
592*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(sb_) NULL_VTABLE};
593fb9a9224SMatthew Dillon
594fb9a9224SMatthew Dillon static int PTRFASTCALL
unicode_byte_type(char hi,char lo)595*0c65ac1dSAntonio Huete Jimenez unicode_byte_type(char hi, char lo) {
596fb9a9224SMatthew Dillon switch ((unsigned char)hi) {
597*0c65ac1dSAntonio Huete Jimenez /* 0xD800-0xDBFF first 16-bit code unit or high surrogate (W1) */
598*0c65ac1dSAntonio Huete Jimenez case 0xD8:
599*0c65ac1dSAntonio Huete Jimenez case 0xD9:
600*0c65ac1dSAntonio Huete Jimenez case 0xDA:
601*0c65ac1dSAntonio Huete Jimenez case 0xDB:
602fb9a9224SMatthew Dillon return BT_LEAD4;
603*0c65ac1dSAntonio Huete Jimenez /* 0xDC00-0xDFFF second 16-bit code unit or low surrogate (W2) */
604*0c65ac1dSAntonio Huete Jimenez case 0xDC:
605*0c65ac1dSAntonio Huete Jimenez case 0xDD:
606*0c65ac1dSAntonio Huete Jimenez case 0xDE:
607*0c65ac1dSAntonio Huete Jimenez case 0xDF:
608fb9a9224SMatthew Dillon return BT_TRAIL;
609fb9a9224SMatthew Dillon case 0xFF:
610fb9a9224SMatthew Dillon switch ((unsigned char)lo) {
611*0c65ac1dSAntonio Huete Jimenez case 0xFF: /* noncharacter-FFFF */
612*0c65ac1dSAntonio Huete Jimenez case 0xFE: /* noncharacter-FFFE */
613fb9a9224SMatthew Dillon return BT_NONXML;
614fb9a9224SMatthew Dillon }
615fb9a9224SMatthew Dillon break;
616fb9a9224SMatthew Dillon }
617fb9a9224SMatthew Dillon return BT_NONASCII;
618fb9a9224SMatthew Dillon }
619fb9a9224SMatthew Dillon
620fb9a9224SMatthew Dillon #define DEFINE_UTF16_TO_UTF8(E) \
621*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL E##toUtf8( \
622*0c65ac1dSAntonio Huete Jimenez const ENCODING *enc, const char **fromP, const char *fromLim, \
623*0c65ac1dSAntonio Huete Jimenez char **toP, const char *toLim) { \
624*0c65ac1dSAntonio Huete Jimenez const char *from = *fromP; \
625*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc); \
626*0c65ac1dSAntonio Huete Jimenez fromLim = from + (((fromLim - from) >> 1) << 1); /* shrink to even */ \
627*0c65ac1dSAntonio Huete Jimenez for (; from < fromLim; from += 2) { \
628fb9a9224SMatthew Dillon int plane; \
629fb9a9224SMatthew Dillon unsigned char lo2; \
630fb9a9224SMatthew Dillon unsigned char lo = GET_LO(from); \
631fb9a9224SMatthew Dillon unsigned char hi = GET_HI(from); \
632fb9a9224SMatthew Dillon switch (hi) { \
633fb9a9224SMatthew Dillon case 0: \
634fb9a9224SMatthew Dillon if (lo < 0x80) { \
635fb9a9224SMatthew Dillon if (*toP == toLim) { \
636fb9a9224SMatthew Dillon *fromP = from; \
637*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED; \
638fb9a9224SMatthew Dillon } \
639fb9a9224SMatthew Dillon *(*toP)++ = lo; \
640fb9a9224SMatthew Dillon break; \
641fb9a9224SMatthew Dillon } \
642fb9a9224SMatthew Dillon /* fall through */ \
643*0c65ac1dSAntonio Huete Jimenez case 0x1: \
644*0c65ac1dSAntonio Huete Jimenez case 0x2: \
645*0c65ac1dSAntonio Huete Jimenez case 0x3: \
646*0c65ac1dSAntonio Huete Jimenez case 0x4: \
647*0c65ac1dSAntonio Huete Jimenez case 0x5: \
648*0c65ac1dSAntonio Huete Jimenez case 0x6: \
649*0c65ac1dSAntonio Huete Jimenez case 0x7: \
650fb9a9224SMatthew Dillon if (toLim - *toP < 2) { \
651fb9a9224SMatthew Dillon *fromP = from; \
652*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED; \
653fb9a9224SMatthew Dillon } \
654fb9a9224SMatthew Dillon *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
655fb9a9224SMatthew Dillon *(*toP)++ = ((lo & 0x3f) | 0x80); \
656fb9a9224SMatthew Dillon break; \
657fb9a9224SMatthew Dillon default: \
658fb9a9224SMatthew Dillon if (toLim - *toP < 3) { \
659fb9a9224SMatthew Dillon *fromP = from; \
660*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED; \
661fb9a9224SMatthew Dillon } \
662fb9a9224SMatthew Dillon /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
663fb9a9224SMatthew Dillon *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
664fb9a9224SMatthew Dillon *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
665fb9a9224SMatthew Dillon *(*toP)++ = ((lo & 0x3f) | 0x80); \
666fb9a9224SMatthew Dillon break; \
667*0c65ac1dSAntonio Huete Jimenez case 0xD8: \
668*0c65ac1dSAntonio Huete Jimenez case 0xD9: \
669*0c65ac1dSAntonio Huete Jimenez case 0xDA: \
670*0c65ac1dSAntonio Huete Jimenez case 0xDB: \
671fb9a9224SMatthew Dillon if (toLim - *toP < 4) { \
672fb9a9224SMatthew Dillon *fromP = from; \
673*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED; \
674*0c65ac1dSAntonio Huete Jimenez } \
675*0c65ac1dSAntonio Huete Jimenez if (fromLim - from < 4) { \
676*0c65ac1dSAntonio Huete Jimenez *fromP = from; \
677*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_INPUT_INCOMPLETE; \
678fb9a9224SMatthew Dillon } \
679fb9a9224SMatthew Dillon plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
680*0c65ac1dSAntonio Huete Jimenez *(*toP)++ = (char)((plane >> 2) | UTF8_cval4); \
681fb9a9224SMatthew Dillon *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
682fb9a9224SMatthew Dillon from += 2; \
683fb9a9224SMatthew Dillon lo2 = GET_LO(from); \
684*0c65ac1dSAntonio Huete Jimenez *(*toP)++ = (((lo & 0x3) << 4) | ((GET_HI(from) & 0x3) << 2) \
685*0c65ac1dSAntonio Huete Jimenez | (lo2 >> 6) | 0x80); \
686fb9a9224SMatthew Dillon *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
687fb9a9224SMatthew Dillon break; \
688fb9a9224SMatthew Dillon } \
689fb9a9224SMatthew Dillon } \
690fb9a9224SMatthew Dillon *fromP = from; \
691*0c65ac1dSAntonio Huete Jimenez if (from < fromLim) \
692*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_INPUT_INCOMPLETE; \
693*0c65ac1dSAntonio Huete Jimenez else \
694*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_COMPLETED; \
695fb9a9224SMatthew Dillon }
696fb9a9224SMatthew Dillon
697fb9a9224SMatthew Dillon #define DEFINE_UTF16_TO_UTF16(E) \
698*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL E##toUtf16( \
699*0c65ac1dSAntonio Huete Jimenez const ENCODING *enc, const char **fromP, const char *fromLim, \
700*0c65ac1dSAntonio Huete Jimenez unsigned short **toP, const unsigned short *toLim) { \
701*0c65ac1dSAntonio Huete Jimenez enum XML_Convert_Result res = XML_CONVERT_COMPLETED; \
702*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc); \
703*0c65ac1dSAntonio Huete Jimenez fromLim = *fromP + (((fromLim - *fromP) >> 1) << 1); /* shrink to even */ \
704fb9a9224SMatthew Dillon /* Avoid copying first half only of surrogate */ \
705fb9a9224SMatthew Dillon if (fromLim - *fromP > ((toLim - *toP) << 1) \
706*0c65ac1dSAntonio Huete Jimenez && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) { \
707fb9a9224SMatthew Dillon fromLim -= 2; \
708*0c65ac1dSAntonio Huete Jimenez res = XML_CONVERT_INPUT_INCOMPLETE; \
709*0c65ac1dSAntonio Huete Jimenez } \
710*0c65ac1dSAntonio Huete Jimenez for (; *fromP < fromLim && *toP < toLim; *fromP += 2) \
711fb9a9224SMatthew Dillon *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
712*0c65ac1dSAntonio Huete Jimenez if ((*toP == toLim) && (*fromP < fromLim)) \
713*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED; \
714*0c65ac1dSAntonio Huete Jimenez else \
715*0c65ac1dSAntonio Huete Jimenez return res; \
716fb9a9224SMatthew Dillon }
717fb9a9224SMatthew Dillon
718*0c65ac1dSAntonio Huete Jimenez #define SET2(ptr, ch) (((ptr)[0] = ((ch)&0xff)), ((ptr)[1] = ((ch) >> 8)))
719fb9a9224SMatthew Dillon #define GET_LO(ptr) ((unsigned char)(ptr)[0])
720fb9a9224SMatthew Dillon #define GET_HI(ptr) ((unsigned char)(ptr)[1])
721fb9a9224SMatthew Dillon
722fb9a9224SMatthew Dillon DEFINE_UTF16_TO_UTF8(little2_)
DEFINE_UTF16_TO_UTF16(little2_)723fb9a9224SMatthew Dillon DEFINE_UTF16_TO_UTF16(little2_)
724fb9a9224SMatthew Dillon
725fb9a9224SMatthew Dillon #undef SET2
726fb9a9224SMatthew Dillon #undef GET_LO
727fb9a9224SMatthew Dillon #undef GET_HI
728fb9a9224SMatthew Dillon
729*0c65ac1dSAntonio Huete Jimenez #define SET2(ptr, ch) (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch)&0xFF)))
730fb9a9224SMatthew Dillon #define GET_LO(ptr) ((unsigned char)(ptr)[1])
731fb9a9224SMatthew Dillon #define GET_HI(ptr) ((unsigned char)(ptr)[0])
732fb9a9224SMatthew Dillon
733fb9a9224SMatthew Dillon DEFINE_UTF16_TO_UTF8(big2_)
734fb9a9224SMatthew Dillon DEFINE_UTF16_TO_UTF16(big2_)
735fb9a9224SMatthew Dillon
736fb9a9224SMatthew Dillon #undef SET2
737fb9a9224SMatthew Dillon #undef GET_LO
738fb9a9224SMatthew Dillon #undef GET_HI
739fb9a9224SMatthew Dillon
740fb9a9224SMatthew Dillon #define LITTLE2_BYTE_TYPE(enc, p) \
741*0c65ac1dSAntonio Huete Jimenez ((p)[1] == 0 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
742fb9a9224SMatthew Dillon : unicode_byte_type((p)[1], (p)[0]))
743*0c65ac1dSAntonio Huete Jimenez #define LITTLE2_BYTE_TO_ASCII(p) ((p)[1] == 0 ? (p)[0] : -1)
744*0c65ac1dSAntonio Huete Jimenez #define LITTLE2_CHAR_MATCHES(p, c) ((p)[1] == 0 && (p)[0] == (c))
745*0c65ac1dSAntonio Huete Jimenez #define LITTLE2_IS_NAME_CHAR_MINBPC(p) \
746fb9a9224SMatthew Dillon UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
747*0c65ac1dSAntonio Huete Jimenez #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(p) \
748fb9a9224SMatthew Dillon UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
749fb9a9224SMatthew Dillon
750fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
751fb9a9224SMatthew Dillon
752fb9a9224SMatthew Dillon static int PTRFASTCALL
753*0c65ac1dSAntonio Huete Jimenez little2_byteType(const ENCODING *enc, const char *p) {
754fb9a9224SMatthew Dillon return LITTLE2_BYTE_TYPE(enc, p);
755fb9a9224SMatthew Dillon }
756fb9a9224SMatthew Dillon
757fb9a9224SMatthew Dillon static int PTRFASTCALL
little2_byteToAscii(const ENCODING * enc,const char * p)758*0c65ac1dSAntonio Huete Jimenez little2_byteToAscii(const ENCODING *enc, const char *p) {
759*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
760*0c65ac1dSAntonio Huete Jimenez return LITTLE2_BYTE_TO_ASCII(p);
761fb9a9224SMatthew Dillon }
762fb9a9224SMatthew Dillon
763fb9a9224SMatthew Dillon static int PTRCALL
little2_charMatches(const ENCODING * enc,const char * p,int c)764*0c65ac1dSAntonio Huete Jimenez little2_charMatches(const ENCODING *enc, const char *p, int c) {
765*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
766*0c65ac1dSAntonio Huete Jimenez return LITTLE2_CHAR_MATCHES(p, c);
767fb9a9224SMatthew Dillon }
768fb9a9224SMatthew Dillon
769fb9a9224SMatthew Dillon static int PTRFASTCALL
little2_isNameMin(const ENCODING * enc,const char * p)770*0c65ac1dSAntonio Huete Jimenez little2_isNameMin(const ENCODING *enc, const char *p) {
771*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
772*0c65ac1dSAntonio Huete Jimenez return LITTLE2_IS_NAME_CHAR_MINBPC(p);
773fb9a9224SMatthew Dillon }
774fb9a9224SMatthew Dillon
775fb9a9224SMatthew Dillon static int PTRFASTCALL
little2_isNmstrtMin(const ENCODING * enc,const char * p)776*0c65ac1dSAntonio Huete Jimenez little2_isNmstrtMin(const ENCODING *enc, const char *p) {
777*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
778*0c65ac1dSAntonio Huete Jimenez return LITTLE2_IS_NMSTRT_CHAR_MINBPC(p);
779fb9a9224SMatthew Dillon }
780fb9a9224SMatthew Dillon
781fb9a9224SMatthew Dillon # undef VTABLE
782fb9a9224SMatthew Dillon # define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
783fb9a9224SMatthew Dillon
784fb9a9224SMatthew Dillon #else /* not XML_MIN_SIZE */
785fb9a9224SMatthew Dillon
786fb9a9224SMatthew Dillon # undef PREFIX
787fb9a9224SMatthew Dillon # define PREFIX(ident) little2_##ident
788fb9a9224SMatthew Dillon # define MINBPC(enc) 2
789fb9a9224SMatthew Dillon /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
790fb9a9224SMatthew Dillon # define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
791*0c65ac1dSAntonio Huete Jimenez # define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(p)
792*0c65ac1dSAntonio Huete Jimenez # define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(p, c)
793fb9a9224SMatthew Dillon # define IS_NAME_CHAR(enc, p, n) 0
794*0c65ac1dSAntonio Huete Jimenez # define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(p)
795fb9a9224SMatthew Dillon # define IS_NMSTRT_CHAR(enc, p, n) (0)
796*0c65ac1dSAntonio Huete Jimenez # define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(p)
797fb9a9224SMatthew Dillon
798fb9a9224SMatthew Dillon # define XML_TOK_IMPL_C
799fb9a9224SMatthew Dillon # include "xmltok_impl.c"
800fb9a9224SMatthew Dillon # undef XML_TOK_IMPL_C
801fb9a9224SMatthew Dillon
802fb9a9224SMatthew Dillon # undef MINBPC
803fb9a9224SMatthew Dillon # undef BYTE_TYPE
804fb9a9224SMatthew Dillon # undef BYTE_TO_ASCII
805fb9a9224SMatthew Dillon # undef CHAR_MATCHES
806fb9a9224SMatthew Dillon # undef IS_NAME_CHAR
807fb9a9224SMatthew Dillon # undef IS_NAME_CHAR_MINBPC
808fb9a9224SMatthew Dillon # undef IS_NMSTRT_CHAR
809fb9a9224SMatthew Dillon # undef IS_NMSTRT_CHAR_MINBPC
810fb9a9224SMatthew Dillon # undef IS_INVALID_CHAR
811fb9a9224SMatthew Dillon
812fb9a9224SMatthew Dillon #endif /* not XML_MIN_SIZE */
813fb9a9224SMatthew Dillon
814fb9a9224SMatthew Dillon #ifdef XML_NS
815fb9a9224SMatthew Dillon
816*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding little2_encoding_ns
817*0c65ac1dSAntonio Huete Jimenez = {{VTABLE, 2, 0,
818fb9a9224SMatthew Dillon # if BYTEORDER == 1234
819fb9a9224SMatthew Dillon 1
820fb9a9224SMatthew Dillon # else
821fb9a9224SMatthew Dillon 0
822fb9a9224SMatthew Dillon # endif
823fb9a9224SMatthew Dillon },
824fb9a9224SMatthew Dillon {
825fb9a9224SMatthew Dillon # include "asciitab.h"
826fb9a9224SMatthew Dillon # include "latin1tab.h"
827fb9a9224SMatthew Dillon },
828*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(little2_) NULL_VTABLE};
829fb9a9224SMatthew Dillon
830fb9a9224SMatthew Dillon #endif
831fb9a9224SMatthew Dillon
832*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding little2_encoding
833*0c65ac1dSAntonio Huete Jimenez = {{VTABLE, 2, 0,
834fb9a9224SMatthew Dillon #if BYTEORDER == 1234
835fb9a9224SMatthew Dillon 1
836fb9a9224SMatthew Dillon #else
837fb9a9224SMatthew Dillon 0
838fb9a9224SMatthew Dillon #endif
839fb9a9224SMatthew Dillon },
840fb9a9224SMatthew Dillon {
841fb9a9224SMatthew Dillon #define BT_COLON BT_NMSTRT
842fb9a9224SMatthew Dillon #include "asciitab.h"
843fb9a9224SMatthew Dillon #undef BT_COLON
844fb9a9224SMatthew Dillon #include "latin1tab.h"
845fb9a9224SMatthew Dillon },
846*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(little2_) NULL_VTABLE};
847fb9a9224SMatthew Dillon
848fb9a9224SMatthew Dillon #if BYTEORDER != 4321
849fb9a9224SMatthew Dillon
850fb9a9224SMatthew Dillon # ifdef XML_NS
851fb9a9224SMatthew Dillon
852*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding internal_little2_encoding_ns
853*0c65ac1dSAntonio Huete Jimenez = {{VTABLE, 2, 0, 1},
854fb9a9224SMatthew Dillon {
855fb9a9224SMatthew Dillon # include "iasciitab.h"
856fb9a9224SMatthew Dillon # include "latin1tab.h"
857fb9a9224SMatthew Dillon },
858*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(little2_) NULL_VTABLE};
859fb9a9224SMatthew Dillon
860fb9a9224SMatthew Dillon # endif
861fb9a9224SMatthew Dillon
862*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding internal_little2_encoding
863*0c65ac1dSAntonio Huete Jimenez = {{VTABLE, 2, 0, 1},
864fb9a9224SMatthew Dillon {
865fb9a9224SMatthew Dillon # define BT_COLON BT_NMSTRT
866fb9a9224SMatthew Dillon # include "iasciitab.h"
867fb9a9224SMatthew Dillon # undef BT_COLON
868fb9a9224SMatthew Dillon # include "latin1tab.h"
869fb9a9224SMatthew Dillon },
870*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(little2_) NULL_VTABLE};
871fb9a9224SMatthew Dillon
872fb9a9224SMatthew Dillon #endif
873fb9a9224SMatthew Dillon
874fb9a9224SMatthew Dillon #define BIG2_BYTE_TYPE(enc, p) \
875fb9a9224SMatthew Dillon ((p)[0] == 0 \
876fb9a9224SMatthew Dillon ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
877fb9a9224SMatthew Dillon : unicode_byte_type((p)[0], (p)[1]))
878*0c65ac1dSAntonio Huete Jimenez #define BIG2_BYTE_TO_ASCII(p) ((p)[0] == 0 ? (p)[1] : -1)
879*0c65ac1dSAntonio Huete Jimenez #define BIG2_CHAR_MATCHES(p, c) ((p)[0] == 0 && (p)[1] == (c))
880*0c65ac1dSAntonio Huete Jimenez #define BIG2_IS_NAME_CHAR_MINBPC(p) \
881fb9a9224SMatthew Dillon UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
882*0c65ac1dSAntonio Huete Jimenez #define BIG2_IS_NMSTRT_CHAR_MINBPC(p) \
883fb9a9224SMatthew Dillon UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
884fb9a9224SMatthew Dillon
885fb9a9224SMatthew Dillon #ifdef XML_MIN_SIZE
886fb9a9224SMatthew Dillon
887fb9a9224SMatthew Dillon static int PTRFASTCALL
big2_byteType(const ENCODING * enc,const char * p)888*0c65ac1dSAntonio Huete Jimenez big2_byteType(const ENCODING *enc, const char *p) {
889fb9a9224SMatthew Dillon return BIG2_BYTE_TYPE(enc, p);
890fb9a9224SMatthew Dillon }
891fb9a9224SMatthew Dillon
892fb9a9224SMatthew Dillon static int PTRFASTCALL
big2_byteToAscii(const ENCODING * enc,const char * p)893*0c65ac1dSAntonio Huete Jimenez big2_byteToAscii(const ENCODING *enc, const char *p) {
894*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
895*0c65ac1dSAntonio Huete Jimenez return BIG2_BYTE_TO_ASCII(p);
896fb9a9224SMatthew Dillon }
897fb9a9224SMatthew Dillon
898fb9a9224SMatthew Dillon static int PTRCALL
big2_charMatches(const ENCODING * enc,const char * p,int c)899*0c65ac1dSAntonio Huete Jimenez big2_charMatches(const ENCODING *enc, const char *p, int c) {
900*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
901*0c65ac1dSAntonio Huete Jimenez return BIG2_CHAR_MATCHES(p, c);
902fb9a9224SMatthew Dillon }
903fb9a9224SMatthew Dillon
904fb9a9224SMatthew Dillon static int PTRFASTCALL
big2_isNameMin(const ENCODING * enc,const char * p)905*0c65ac1dSAntonio Huete Jimenez big2_isNameMin(const ENCODING *enc, const char *p) {
906*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
907*0c65ac1dSAntonio Huete Jimenez return BIG2_IS_NAME_CHAR_MINBPC(p);
908fb9a9224SMatthew Dillon }
909fb9a9224SMatthew Dillon
910fb9a9224SMatthew Dillon static int PTRFASTCALL
big2_isNmstrtMin(const ENCODING * enc,const char * p)911*0c65ac1dSAntonio Huete Jimenez big2_isNmstrtMin(const ENCODING *enc, const char *p) {
912*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
913*0c65ac1dSAntonio Huete Jimenez return BIG2_IS_NMSTRT_CHAR_MINBPC(p);
914fb9a9224SMatthew Dillon }
915fb9a9224SMatthew Dillon
916fb9a9224SMatthew Dillon # undef VTABLE
917fb9a9224SMatthew Dillon # define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
918fb9a9224SMatthew Dillon
919fb9a9224SMatthew Dillon #else /* not XML_MIN_SIZE */
920fb9a9224SMatthew Dillon
921fb9a9224SMatthew Dillon # undef PREFIX
922fb9a9224SMatthew Dillon # define PREFIX(ident) big2_##ident
923fb9a9224SMatthew Dillon # define MINBPC(enc) 2
924fb9a9224SMatthew Dillon /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
925fb9a9224SMatthew Dillon # define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
926*0c65ac1dSAntonio Huete Jimenez # define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(p)
927*0c65ac1dSAntonio Huete Jimenez # define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(p, c)
928fb9a9224SMatthew Dillon # define IS_NAME_CHAR(enc, p, n) 0
929*0c65ac1dSAntonio Huete Jimenez # define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(p)
930fb9a9224SMatthew Dillon # define IS_NMSTRT_CHAR(enc, p, n) (0)
931*0c65ac1dSAntonio Huete Jimenez # define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(p)
932fb9a9224SMatthew Dillon
933fb9a9224SMatthew Dillon # define XML_TOK_IMPL_C
934fb9a9224SMatthew Dillon # include "xmltok_impl.c"
935fb9a9224SMatthew Dillon # undef XML_TOK_IMPL_C
936fb9a9224SMatthew Dillon
937fb9a9224SMatthew Dillon # undef MINBPC
938fb9a9224SMatthew Dillon # undef BYTE_TYPE
939fb9a9224SMatthew Dillon # undef BYTE_TO_ASCII
940fb9a9224SMatthew Dillon # undef CHAR_MATCHES
941fb9a9224SMatthew Dillon # undef IS_NAME_CHAR
942fb9a9224SMatthew Dillon # undef IS_NAME_CHAR_MINBPC
943fb9a9224SMatthew Dillon # undef IS_NMSTRT_CHAR
944fb9a9224SMatthew Dillon # undef IS_NMSTRT_CHAR_MINBPC
945fb9a9224SMatthew Dillon # undef IS_INVALID_CHAR
946fb9a9224SMatthew Dillon
947fb9a9224SMatthew Dillon #endif /* not XML_MIN_SIZE */
948fb9a9224SMatthew Dillon
949fb9a9224SMatthew Dillon #ifdef XML_NS
950fb9a9224SMatthew Dillon
951*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding big2_encoding_ns
952*0c65ac1dSAntonio Huete Jimenez = {{VTABLE, 2, 0,
953fb9a9224SMatthew Dillon # if BYTEORDER == 4321
954fb9a9224SMatthew Dillon 1
955fb9a9224SMatthew Dillon # else
956fb9a9224SMatthew Dillon 0
957fb9a9224SMatthew Dillon # endif
958fb9a9224SMatthew Dillon },
959fb9a9224SMatthew Dillon {
960fb9a9224SMatthew Dillon # include "asciitab.h"
961fb9a9224SMatthew Dillon # include "latin1tab.h"
962fb9a9224SMatthew Dillon },
963*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(big2_) NULL_VTABLE};
964fb9a9224SMatthew Dillon
965fb9a9224SMatthew Dillon #endif
966fb9a9224SMatthew Dillon
967*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding big2_encoding
968*0c65ac1dSAntonio Huete Jimenez = {{VTABLE, 2, 0,
969fb9a9224SMatthew Dillon #if BYTEORDER == 4321
970fb9a9224SMatthew Dillon 1
971fb9a9224SMatthew Dillon #else
972fb9a9224SMatthew Dillon 0
973fb9a9224SMatthew Dillon #endif
974fb9a9224SMatthew Dillon },
975fb9a9224SMatthew Dillon {
976fb9a9224SMatthew Dillon #define BT_COLON BT_NMSTRT
977fb9a9224SMatthew Dillon #include "asciitab.h"
978fb9a9224SMatthew Dillon #undef BT_COLON
979fb9a9224SMatthew Dillon #include "latin1tab.h"
980fb9a9224SMatthew Dillon },
981*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(big2_) NULL_VTABLE};
982fb9a9224SMatthew Dillon
983fb9a9224SMatthew Dillon #if BYTEORDER != 1234
984fb9a9224SMatthew Dillon
985fb9a9224SMatthew Dillon # ifdef XML_NS
986fb9a9224SMatthew Dillon
987*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding internal_big2_encoding_ns
988*0c65ac1dSAntonio Huete Jimenez = {{VTABLE, 2, 0, 1},
989fb9a9224SMatthew Dillon {
990fb9a9224SMatthew Dillon # include "iasciitab.h"
991fb9a9224SMatthew Dillon # include "latin1tab.h"
992fb9a9224SMatthew Dillon },
993*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(big2_) NULL_VTABLE};
994fb9a9224SMatthew Dillon
995fb9a9224SMatthew Dillon # endif
996fb9a9224SMatthew Dillon
997*0c65ac1dSAntonio Huete Jimenez static const struct normal_encoding internal_big2_encoding
998*0c65ac1dSAntonio Huete Jimenez = {{VTABLE, 2, 0, 1},
999fb9a9224SMatthew Dillon {
1000fb9a9224SMatthew Dillon # define BT_COLON BT_NMSTRT
1001fb9a9224SMatthew Dillon # include "iasciitab.h"
1002fb9a9224SMatthew Dillon # undef BT_COLON
1003fb9a9224SMatthew Dillon # include "latin1tab.h"
1004fb9a9224SMatthew Dillon },
1005*0c65ac1dSAntonio Huete Jimenez STANDARD_VTABLE(big2_) NULL_VTABLE};
1006fb9a9224SMatthew Dillon
1007fb9a9224SMatthew Dillon #endif
1008fb9a9224SMatthew Dillon
1009fb9a9224SMatthew Dillon #undef PREFIX
1010fb9a9224SMatthew Dillon
1011fb9a9224SMatthew Dillon static int FASTCALL
streqci(const char * s1,const char * s2)1012*0c65ac1dSAntonio Huete Jimenez streqci(const char *s1, const char *s2) {
1013fb9a9224SMatthew Dillon for (;;) {
1014fb9a9224SMatthew Dillon char c1 = *s1++;
1015fb9a9224SMatthew Dillon char c2 = *s2++;
1016fb9a9224SMatthew Dillon if (ASCII_a <= c1 && c1 <= ASCII_z)
1017fb9a9224SMatthew Dillon c1 += ASCII_A - ASCII_a;
1018fb9a9224SMatthew Dillon if (ASCII_a <= c2 && c2 <= ASCII_z)
1019*0c65ac1dSAntonio Huete Jimenez /* The following line will never get executed. streqci() is
1020*0c65ac1dSAntonio Huete Jimenez * only called from two places, both of which guarantee to put
1021*0c65ac1dSAntonio Huete Jimenez * upper-case strings into s2.
1022*0c65ac1dSAntonio Huete Jimenez */
1023*0c65ac1dSAntonio Huete Jimenez c2 += ASCII_A - ASCII_a; /* LCOV_EXCL_LINE */
1024fb9a9224SMatthew Dillon if (c1 != c2)
1025fb9a9224SMatthew Dillon return 0;
1026fb9a9224SMatthew Dillon if (! c1)
1027fb9a9224SMatthew Dillon break;
1028fb9a9224SMatthew Dillon }
1029fb9a9224SMatthew Dillon return 1;
1030fb9a9224SMatthew Dillon }
1031fb9a9224SMatthew Dillon
1032fb9a9224SMatthew Dillon static void PTRCALL
initUpdatePosition(const ENCODING * enc,const char * ptr,const char * end,POSITION * pos)1033*0c65ac1dSAntonio Huete Jimenez initUpdatePosition(const ENCODING *enc, const char *ptr, const char *end,
1034*0c65ac1dSAntonio Huete Jimenez POSITION *pos) {
1035*0c65ac1dSAntonio Huete Jimenez UNUSED_P(enc);
1036fb9a9224SMatthew Dillon normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
1037fb9a9224SMatthew Dillon }
1038fb9a9224SMatthew Dillon
1039fb9a9224SMatthew Dillon static int
toAscii(const ENCODING * enc,const char * ptr,const char * end)1040*0c65ac1dSAntonio Huete Jimenez toAscii(const ENCODING *enc, const char *ptr, const char *end) {
1041fb9a9224SMatthew Dillon char buf[1];
1042fb9a9224SMatthew Dillon char *p = buf;
1043fb9a9224SMatthew Dillon XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
1044fb9a9224SMatthew Dillon if (p == buf)
1045fb9a9224SMatthew Dillon return -1;
1046fb9a9224SMatthew Dillon else
1047fb9a9224SMatthew Dillon return buf[0];
1048fb9a9224SMatthew Dillon }
1049fb9a9224SMatthew Dillon
1050fb9a9224SMatthew Dillon static int FASTCALL
isSpace(int c)1051*0c65ac1dSAntonio Huete Jimenez isSpace(int c) {
1052fb9a9224SMatthew Dillon switch (c) {
1053fb9a9224SMatthew Dillon case 0x20:
1054fb9a9224SMatthew Dillon case 0xD:
1055fb9a9224SMatthew Dillon case 0xA:
1056fb9a9224SMatthew Dillon case 0x9:
1057fb9a9224SMatthew Dillon return 1;
1058fb9a9224SMatthew Dillon }
1059fb9a9224SMatthew Dillon return 0;
1060fb9a9224SMatthew Dillon }
1061fb9a9224SMatthew Dillon
1062fb9a9224SMatthew Dillon /* Return 1 if there's just optional white space or there's an S
1063fb9a9224SMatthew Dillon followed by name=val.
1064fb9a9224SMatthew Dillon */
1065fb9a9224SMatthew Dillon static int
parsePseudoAttribute(const ENCODING * enc,const char * ptr,const char * end,const char ** namePtr,const char ** nameEndPtr,const char ** valPtr,const char ** nextTokPtr)1066*0c65ac1dSAntonio Huete Jimenez parsePseudoAttribute(const ENCODING *enc, const char *ptr, const char *end,
1067*0c65ac1dSAntonio Huete Jimenez const char **namePtr, const char **nameEndPtr,
1068*0c65ac1dSAntonio Huete Jimenez const char **valPtr, const char **nextTokPtr) {
1069fb9a9224SMatthew Dillon int c;
1070fb9a9224SMatthew Dillon char open;
1071fb9a9224SMatthew Dillon if (ptr == end) {
1072fb9a9224SMatthew Dillon *namePtr = NULL;
1073fb9a9224SMatthew Dillon return 1;
1074fb9a9224SMatthew Dillon }
1075fb9a9224SMatthew Dillon if (! isSpace(toAscii(enc, ptr, end))) {
1076fb9a9224SMatthew Dillon *nextTokPtr = ptr;
1077fb9a9224SMatthew Dillon return 0;
1078fb9a9224SMatthew Dillon }
1079fb9a9224SMatthew Dillon do {
1080fb9a9224SMatthew Dillon ptr += enc->minBytesPerChar;
1081fb9a9224SMatthew Dillon } while (isSpace(toAscii(enc, ptr, end)));
1082fb9a9224SMatthew Dillon if (ptr == end) {
1083fb9a9224SMatthew Dillon *namePtr = NULL;
1084fb9a9224SMatthew Dillon return 1;
1085fb9a9224SMatthew Dillon }
1086fb9a9224SMatthew Dillon *namePtr = ptr;
1087fb9a9224SMatthew Dillon for (;;) {
1088fb9a9224SMatthew Dillon c = toAscii(enc, ptr, end);
1089fb9a9224SMatthew Dillon if (c == -1) {
1090fb9a9224SMatthew Dillon *nextTokPtr = ptr;
1091fb9a9224SMatthew Dillon return 0;
1092fb9a9224SMatthew Dillon }
1093fb9a9224SMatthew Dillon if (c == ASCII_EQUALS) {
1094fb9a9224SMatthew Dillon *nameEndPtr = ptr;
1095fb9a9224SMatthew Dillon break;
1096fb9a9224SMatthew Dillon }
1097fb9a9224SMatthew Dillon if (isSpace(c)) {
1098fb9a9224SMatthew Dillon *nameEndPtr = ptr;
1099fb9a9224SMatthew Dillon do {
1100fb9a9224SMatthew Dillon ptr += enc->minBytesPerChar;
1101fb9a9224SMatthew Dillon } while (isSpace(c = toAscii(enc, ptr, end)));
1102fb9a9224SMatthew Dillon if (c != ASCII_EQUALS) {
1103fb9a9224SMatthew Dillon *nextTokPtr = ptr;
1104fb9a9224SMatthew Dillon return 0;
1105fb9a9224SMatthew Dillon }
1106fb9a9224SMatthew Dillon break;
1107fb9a9224SMatthew Dillon }
1108fb9a9224SMatthew Dillon ptr += enc->minBytesPerChar;
1109fb9a9224SMatthew Dillon }
1110fb9a9224SMatthew Dillon if (ptr == *namePtr) {
1111fb9a9224SMatthew Dillon *nextTokPtr = ptr;
1112fb9a9224SMatthew Dillon return 0;
1113fb9a9224SMatthew Dillon }
1114fb9a9224SMatthew Dillon ptr += enc->minBytesPerChar;
1115fb9a9224SMatthew Dillon c = toAscii(enc, ptr, end);
1116fb9a9224SMatthew Dillon while (isSpace(c)) {
1117fb9a9224SMatthew Dillon ptr += enc->minBytesPerChar;
1118fb9a9224SMatthew Dillon c = toAscii(enc, ptr, end);
1119fb9a9224SMatthew Dillon }
1120fb9a9224SMatthew Dillon if (c != ASCII_QUOT && c != ASCII_APOS) {
1121fb9a9224SMatthew Dillon *nextTokPtr = ptr;
1122fb9a9224SMatthew Dillon return 0;
1123fb9a9224SMatthew Dillon }
1124fb9a9224SMatthew Dillon open = (char)c;
1125fb9a9224SMatthew Dillon ptr += enc->minBytesPerChar;
1126fb9a9224SMatthew Dillon *valPtr = ptr;
1127fb9a9224SMatthew Dillon for (;; ptr += enc->minBytesPerChar) {
1128fb9a9224SMatthew Dillon c = toAscii(enc, ptr, end);
1129fb9a9224SMatthew Dillon if (c == open)
1130fb9a9224SMatthew Dillon break;
1131*0c65ac1dSAntonio Huete Jimenez if (! (ASCII_a <= c && c <= ASCII_z) && ! (ASCII_A <= c && c <= ASCII_Z)
1132*0c65ac1dSAntonio Huete Jimenez && ! (ASCII_0 <= c && c <= ASCII_9) && c != ASCII_PERIOD
1133*0c65ac1dSAntonio Huete Jimenez && c != ASCII_MINUS && c != ASCII_UNDERSCORE) {
1134fb9a9224SMatthew Dillon *nextTokPtr = ptr;
1135fb9a9224SMatthew Dillon return 0;
1136fb9a9224SMatthew Dillon }
1137fb9a9224SMatthew Dillon }
1138fb9a9224SMatthew Dillon *nextTokPtr = ptr + enc->minBytesPerChar;
1139fb9a9224SMatthew Dillon return 1;
1140fb9a9224SMatthew Dillon }
1141fb9a9224SMatthew Dillon
1142*0c65ac1dSAntonio Huete Jimenez static const char KW_version[]
1143*0c65ac1dSAntonio Huete Jimenez = {ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'};
1144fb9a9224SMatthew Dillon
1145*0c65ac1dSAntonio Huete Jimenez static const char KW_encoding[] = {ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d,
1146*0c65ac1dSAntonio Huete Jimenez ASCII_i, ASCII_n, ASCII_g, '\0'};
1147fb9a9224SMatthew Dillon
1148*0c65ac1dSAntonio Huete Jimenez static const char KW_standalone[]
1149*0c65ac1dSAntonio Huete Jimenez = {ASCII_s, ASCII_t, ASCII_a, ASCII_n, ASCII_d, ASCII_a,
1150*0c65ac1dSAntonio Huete Jimenez ASCII_l, ASCII_o, ASCII_n, ASCII_e, '\0'};
1151fb9a9224SMatthew Dillon
1152*0c65ac1dSAntonio Huete Jimenez static const char KW_yes[] = {ASCII_y, ASCII_e, ASCII_s, '\0'};
1153fb9a9224SMatthew Dillon
1154*0c65ac1dSAntonio Huete Jimenez static const char KW_no[] = {ASCII_n, ASCII_o, '\0'};
1155fb9a9224SMatthew Dillon
1156fb9a9224SMatthew Dillon static int
doParseXmlDecl(const ENCODING * (* encodingFinder)(const ENCODING *,const char *,const char *),int isGeneralTextEntity,const ENCODING * enc,const char * ptr,const char * end,const char ** badPtr,const char ** versionPtr,const char ** versionEndPtr,const char ** encodingName,const ENCODING ** encoding,int * standalone)1157*0c65ac1dSAntonio Huete Jimenez doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *, const char *,
1158fb9a9224SMatthew Dillon const char *),
1159*0c65ac1dSAntonio Huete Jimenez int isGeneralTextEntity, const ENCODING *enc, const char *ptr,
1160*0c65ac1dSAntonio Huete Jimenez const char *end, const char **badPtr, const char **versionPtr,
1161*0c65ac1dSAntonio Huete Jimenez const char **versionEndPtr, const char **encodingName,
1162*0c65ac1dSAntonio Huete Jimenez const ENCODING **encoding, int *standalone) {
1163fb9a9224SMatthew Dillon const char *val = NULL;
1164fb9a9224SMatthew Dillon const char *name = NULL;
1165fb9a9224SMatthew Dillon const char *nameEnd = NULL;
1166fb9a9224SMatthew Dillon ptr += 5 * enc->minBytesPerChar;
1167fb9a9224SMatthew Dillon end -= 2 * enc->minBytesPerChar;
1168fb9a9224SMatthew Dillon if (! parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)
1169fb9a9224SMatthew Dillon || ! name) {
1170fb9a9224SMatthew Dillon *badPtr = ptr;
1171fb9a9224SMatthew Dillon return 0;
1172fb9a9224SMatthew Dillon }
1173fb9a9224SMatthew Dillon if (! XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
1174fb9a9224SMatthew Dillon if (! isGeneralTextEntity) {
1175fb9a9224SMatthew Dillon *badPtr = name;
1176fb9a9224SMatthew Dillon return 0;
1177fb9a9224SMatthew Dillon }
1178*0c65ac1dSAntonio Huete Jimenez } else {
1179fb9a9224SMatthew Dillon if (versionPtr)
1180fb9a9224SMatthew Dillon *versionPtr = val;
1181fb9a9224SMatthew Dillon if (versionEndPtr)
1182fb9a9224SMatthew Dillon *versionEndPtr = ptr;
1183fb9a9224SMatthew Dillon if (! parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1184fb9a9224SMatthew Dillon *badPtr = ptr;
1185fb9a9224SMatthew Dillon return 0;
1186fb9a9224SMatthew Dillon }
1187fb9a9224SMatthew Dillon if (! name) {
1188fb9a9224SMatthew Dillon if (isGeneralTextEntity) {
1189fb9a9224SMatthew Dillon /* a TextDecl must have an EncodingDecl */
1190fb9a9224SMatthew Dillon *badPtr = ptr;
1191fb9a9224SMatthew Dillon return 0;
1192fb9a9224SMatthew Dillon }
1193fb9a9224SMatthew Dillon return 1;
1194fb9a9224SMatthew Dillon }
1195fb9a9224SMatthew Dillon }
1196fb9a9224SMatthew Dillon if (XmlNameMatchesAscii(enc, name, nameEnd, KW_encoding)) {
1197fb9a9224SMatthew Dillon int c = toAscii(enc, val, end);
1198fb9a9224SMatthew Dillon if (! (ASCII_a <= c && c <= ASCII_z) && ! (ASCII_A <= c && c <= ASCII_Z)) {
1199fb9a9224SMatthew Dillon *badPtr = val;
1200fb9a9224SMatthew Dillon return 0;
1201fb9a9224SMatthew Dillon }
1202fb9a9224SMatthew Dillon if (encodingName)
1203fb9a9224SMatthew Dillon *encodingName = val;
1204fb9a9224SMatthew Dillon if (encoding)
1205fb9a9224SMatthew Dillon *encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
1206fb9a9224SMatthew Dillon if (! parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1207fb9a9224SMatthew Dillon *badPtr = ptr;
1208fb9a9224SMatthew Dillon return 0;
1209fb9a9224SMatthew Dillon }
1210fb9a9224SMatthew Dillon if (! name)
1211fb9a9224SMatthew Dillon return 1;
1212fb9a9224SMatthew Dillon }
1213fb9a9224SMatthew Dillon if (! XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone)
1214fb9a9224SMatthew Dillon || isGeneralTextEntity) {
1215fb9a9224SMatthew Dillon *badPtr = name;
1216fb9a9224SMatthew Dillon return 0;
1217fb9a9224SMatthew Dillon }
1218fb9a9224SMatthew Dillon if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
1219fb9a9224SMatthew Dillon if (standalone)
1220fb9a9224SMatthew Dillon *standalone = 1;
1221*0c65ac1dSAntonio Huete Jimenez } else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
1222fb9a9224SMatthew Dillon if (standalone)
1223fb9a9224SMatthew Dillon *standalone = 0;
1224*0c65ac1dSAntonio Huete Jimenez } else {
1225fb9a9224SMatthew Dillon *badPtr = val;
1226fb9a9224SMatthew Dillon return 0;
1227fb9a9224SMatthew Dillon }
1228fb9a9224SMatthew Dillon while (isSpace(toAscii(enc, ptr, end)))
1229fb9a9224SMatthew Dillon ptr += enc->minBytesPerChar;
1230fb9a9224SMatthew Dillon if (ptr != end) {
1231fb9a9224SMatthew Dillon *badPtr = ptr;
1232fb9a9224SMatthew Dillon return 0;
1233fb9a9224SMatthew Dillon }
1234fb9a9224SMatthew Dillon return 1;
1235fb9a9224SMatthew Dillon }
1236fb9a9224SMatthew Dillon
1237fb9a9224SMatthew Dillon static int FASTCALL
checkCharRefNumber(int result)1238*0c65ac1dSAntonio Huete Jimenez checkCharRefNumber(int result) {
1239fb9a9224SMatthew Dillon switch (result >> 8) {
1240*0c65ac1dSAntonio Huete Jimenez case 0xD8:
1241*0c65ac1dSAntonio Huete Jimenez case 0xD9:
1242*0c65ac1dSAntonio Huete Jimenez case 0xDA:
1243*0c65ac1dSAntonio Huete Jimenez case 0xDB:
1244*0c65ac1dSAntonio Huete Jimenez case 0xDC:
1245*0c65ac1dSAntonio Huete Jimenez case 0xDD:
1246*0c65ac1dSAntonio Huete Jimenez case 0xDE:
1247*0c65ac1dSAntonio Huete Jimenez case 0xDF:
1248fb9a9224SMatthew Dillon return -1;
1249fb9a9224SMatthew Dillon case 0:
1250fb9a9224SMatthew Dillon if (latin1_encoding.type[result] == BT_NONXML)
1251fb9a9224SMatthew Dillon return -1;
1252fb9a9224SMatthew Dillon break;
1253fb9a9224SMatthew Dillon case 0xFF:
1254fb9a9224SMatthew Dillon if (result == 0xFFFE || result == 0xFFFF)
1255fb9a9224SMatthew Dillon return -1;
1256fb9a9224SMatthew Dillon break;
1257fb9a9224SMatthew Dillon }
1258fb9a9224SMatthew Dillon return result;
1259fb9a9224SMatthew Dillon }
1260fb9a9224SMatthew Dillon
1261fb9a9224SMatthew Dillon int FASTCALL
XmlUtf8Encode(int c,char * buf)1262*0c65ac1dSAntonio Huete Jimenez XmlUtf8Encode(int c, char *buf) {
1263fb9a9224SMatthew Dillon enum {
1264fb9a9224SMatthew Dillon /* minN is minimum legal resulting value for N byte sequence */
1265fb9a9224SMatthew Dillon min2 = 0x80,
1266fb9a9224SMatthew Dillon min3 = 0x800,
1267fb9a9224SMatthew Dillon min4 = 0x10000
1268fb9a9224SMatthew Dillon };
1269fb9a9224SMatthew Dillon
1270fb9a9224SMatthew Dillon if (c < 0)
1271*0c65ac1dSAntonio Huete Jimenez return 0; /* LCOV_EXCL_LINE: this case is always eliminated beforehand */
1272fb9a9224SMatthew Dillon if (c < min2) {
1273fb9a9224SMatthew Dillon buf[0] = (char)(c | UTF8_cval1);
1274fb9a9224SMatthew Dillon return 1;
1275fb9a9224SMatthew Dillon }
1276fb9a9224SMatthew Dillon if (c < min3) {
1277fb9a9224SMatthew Dillon buf[0] = (char)((c >> 6) | UTF8_cval2);
1278fb9a9224SMatthew Dillon buf[1] = (char)((c & 0x3f) | 0x80);
1279fb9a9224SMatthew Dillon return 2;
1280fb9a9224SMatthew Dillon }
1281fb9a9224SMatthew Dillon if (c < min4) {
1282fb9a9224SMatthew Dillon buf[0] = (char)((c >> 12) | UTF8_cval3);
1283fb9a9224SMatthew Dillon buf[1] = (char)(((c >> 6) & 0x3f) | 0x80);
1284fb9a9224SMatthew Dillon buf[2] = (char)((c & 0x3f) | 0x80);
1285fb9a9224SMatthew Dillon return 3;
1286fb9a9224SMatthew Dillon }
1287fb9a9224SMatthew Dillon if (c < 0x110000) {
1288fb9a9224SMatthew Dillon buf[0] = (char)((c >> 18) | UTF8_cval4);
1289fb9a9224SMatthew Dillon buf[1] = (char)(((c >> 12) & 0x3f) | 0x80);
1290fb9a9224SMatthew Dillon buf[2] = (char)(((c >> 6) & 0x3f) | 0x80);
1291fb9a9224SMatthew Dillon buf[3] = (char)((c & 0x3f) | 0x80);
1292fb9a9224SMatthew Dillon return 4;
1293fb9a9224SMatthew Dillon }
1294*0c65ac1dSAntonio Huete Jimenez return 0; /* LCOV_EXCL_LINE: this case too is eliminated before calling */
1295fb9a9224SMatthew Dillon }
1296fb9a9224SMatthew Dillon
1297fb9a9224SMatthew Dillon int FASTCALL
XmlUtf16Encode(int charNum,unsigned short * buf)1298*0c65ac1dSAntonio Huete Jimenez XmlUtf16Encode(int charNum, unsigned short *buf) {
1299fb9a9224SMatthew Dillon if (charNum < 0)
1300fb9a9224SMatthew Dillon return 0;
1301fb9a9224SMatthew Dillon if (charNum < 0x10000) {
1302fb9a9224SMatthew Dillon buf[0] = (unsigned short)charNum;
1303fb9a9224SMatthew Dillon return 1;
1304fb9a9224SMatthew Dillon }
1305fb9a9224SMatthew Dillon if (charNum < 0x110000) {
1306fb9a9224SMatthew Dillon charNum -= 0x10000;
1307fb9a9224SMatthew Dillon buf[0] = (unsigned short)((charNum >> 10) + 0xD800);
1308fb9a9224SMatthew Dillon buf[1] = (unsigned short)((charNum & 0x3FF) + 0xDC00);
1309fb9a9224SMatthew Dillon return 2;
1310fb9a9224SMatthew Dillon }
1311fb9a9224SMatthew Dillon return 0;
1312fb9a9224SMatthew Dillon }
1313fb9a9224SMatthew Dillon
1314fb9a9224SMatthew Dillon struct unknown_encoding {
1315fb9a9224SMatthew Dillon struct normal_encoding normal;
1316fb9a9224SMatthew Dillon CONVERTER convert;
1317fb9a9224SMatthew Dillon void *userData;
1318fb9a9224SMatthew Dillon unsigned short utf16[256];
1319fb9a9224SMatthew Dillon char utf8[256][4];
1320fb9a9224SMatthew Dillon };
1321fb9a9224SMatthew Dillon
1322fb9a9224SMatthew Dillon #define AS_UNKNOWN_ENCODING(enc) ((const struct unknown_encoding *)(enc))
1323fb9a9224SMatthew Dillon
1324fb9a9224SMatthew Dillon int
XmlSizeOfUnknownEncoding(void)1325*0c65ac1dSAntonio Huete Jimenez XmlSizeOfUnknownEncoding(void) {
1326fb9a9224SMatthew Dillon return sizeof(struct unknown_encoding);
1327fb9a9224SMatthew Dillon }
1328fb9a9224SMatthew Dillon
1329fb9a9224SMatthew Dillon static int PTRFASTCALL
unknown_isName(const ENCODING * enc,const char * p)1330*0c65ac1dSAntonio Huete Jimenez unknown_isName(const ENCODING *enc, const char *p) {
1331fb9a9224SMatthew Dillon const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1332fb9a9224SMatthew Dillon int c = uenc->convert(uenc->userData, p);
1333fb9a9224SMatthew Dillon if (c & ~0xFFFF)
1334fb9a9224SMatthew Dillon return 0;
1335fb9a9224SMatthew Dillon return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
1336fb9a9224SMatthew Dillon }
1337fb9a9224SMatthew Dillon
1338fb9a9224SMatthew Dillon static int PTRFASTCALL
unknown_isNmstrt(const ENCODING * enc,const char * p)1339*0c65ac1dSAntonio Huete Jimenez unknown_isNmstrt(const ENCODING *enc, const char *p) {
1340fb9a9224SMatthew Dillon const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1341fb9a9224SMatthew Dillon int c = uenc->convert(uenc->userData, p);
1342fb9a9224SMatthew Dillon if (c & ~0xFFFF)
1343fb9a9224SMatthew Dillon return 0;
1344fb9a9224SMatthew Dillon return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
1345fb9a9224SMatthew Dillon }
1346fb9a9224SMatthew Dillon
1347fb9a9224SMatthew Dillon static int PTRFASTCALL
unknown_isInvalid(const ENCODING * enc,const char * p)1348*0c65ac1dSAntonio Huete Jimenez unknown_isInvalid(const ENCODING *enc, const char *p) {
1349fb9a9224SMatthew Dillon const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1350fb9a9224SMatthew Dillon int c = uenc->convert(uenc->userData, p);
1351fb9a9224SMatthew Dillon return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
1352fb9a9224SMatthew Dillon }
1353fb9a9224SMatthew Dillon
1354*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL
unknown_toUtf8(const ENCODING * enc,const char ** fromP,const char * fromLim,char ** toP,const char * toLim)1355*0c65ac1dSAntonio Huete Jimenez unknown_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
1356*0c65ac1dSAntonio Huete Jimenez char **toP, const char *toLim) {
1357fb9a9224SMatthew Dillon const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1358fb9a9224SMatthew Dillon char buf[XML_UTF8_ENCODE_MAX];
1359fb9a9224SMatthew Dillon for (;;) {
1360fb9a9224SMatthew Dillon const char *utf8;
1361fb9a9224SMatthew Dillon int n;
1362fb9a9224SMatthew Dillon if (*fromP == fromLim)
1363*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_COMPLETED;
1364fb9a9224SMatthew Dillon utf8 = uenc->utf8[(unsigned char)**fromP];
1365fb9a9224SMatthew Dillon n = *utf8++;
1366fb9a9224SMatthew Dillon if (n == 0) {
1367fb9a9224SMatthew Dillon int c = uenc->convert(uenc->userData, *fromP);
1368fb9a9224SMatthew Dillon n = XmlUtf8Encode(c, buf);
1369fb9a9224SMatthew Dillon if (n > toLim - *toP)
1370*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED;
1371fb9a9224SMatthew Dillon utf8 = buf;
1372fb9a9224SMatthew Dillon *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
1373fb9a9224SMatthew Dillon - (BT_LEAD2 - 2));
1374*0c65ac1dSAntonio Huete Jimenez } else {
1375fb9a9224SMatthew Dillon if (n > toLim - *toP)
1376*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED;
1377fb9a9224SMatthew Dillon (*fromP)++;
1378fb9a9224SMatthew Dillon }
1379*0c65ac1dSAntonio Huete Jimenez memcpy(*toP, utf8, n);
1380*0c65ac1dSAntonio Huete Jimenez *toP += n;
1381fb9a9224SMatthew Dillon }
1382fb9a9224SMatthew Dillon }
1383fb9a9224SMatthew Dillon
1384*0c65ac1dSAntonio Huete Jimenez static enum XML_Convert_Result PTRCALL
unknown_toUtf16(const ENCODING * enc,const char ** fromP,const char * fromLim,unsigned short ** toP,const unsigned short * toLim)1385*0c65ac1dSAntonio Huete Jimenez unknown_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
1386*0c65ac1dSAntonio Huete Jimenez unsigned short **toP, const unsigned short *toLim) {
1387fb9a9224SMatthew Dillon const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1388*0c65ac1dSAntonio Huete Jimenez while (*fromP < fromLim && *toP < toLim) {
1389fb9a9224SMatthew Dillon unsigned short c = uenc->utf16[(unsigned char)**fromP];
1390fb9a9224SMatthew Dillon if (c == 0) {
1391*0c65ac1dSAntonio Huete Jimenez c = (unsigned short)uenc->convert(uenc->userData, *fromP);
1392fb9a9224SMatthew Dillon *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
1393fb9a9224SMatthew Dillon - (BT_LEAD2 - 2));
1394*0c65ac1dSAntonio Huete Jimenez } else
1395fb9a9224SMatthew Dillon (*fromP)++;
1396fb9a9224SMatthew Dillon *(*toP)++ = c;
1397fb9a9224SMatthew Dillon }
1398*0c65ac1dSAntonio Huete Jimenez
1399*0c65ac1dSAntonio Huete Jimenez if ((*toP == toLim) && (*fromP < fromLim))
1400*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_OUTPUT_EXHAUSTED;
1401*0c65ac1dSAntonio Huete Jimenez else
1402*0c65ac1dSAntonio Huete Jimenez return XML_CONVERT_COMPLETED;
1403fb9a9224SMatthew Dillon }
1404fb9a9224SMatthew Dillon
1405fb9a9224SMatthew Dillon ENCODING *
XmlInitUnknownEncoding(void * mem,int * table,CONVERTER convert,void * userData)1406*0c65ac1dSAntonio Huete Jimenez XmlInitUnknownEncoding(void *mem, int *table, CONVERTER convert,
1407*0c65ac1dSAntonio Huete Jimenez void *userData) {
1408fb9a9224SMatthew Dillon int i;
1409fb9a9224SMatthew Dillon struct unknown_encoding *e = (struct unknown_encoding *)mem;
1410*0c65ac1dSAntonio Huete Jimenez memcpy(mem, &latin1_encoding, sizeof(struct normal_encoding));
1411fb9a9224SMatthew Dillon for (i = 0; i < 128; i++)
1412fb9a9224SMatthew Dillon if (latin1_encoding.type[i] != BT_OTHER
1413*0c65ac1dSAntonio Huete Jimenez && latin1_encoding.type[i] != BT_NONXML && table[i] != i)
1414fb9a9224SMatthew Dillon return 0;
1415fb9a9224SMatthew Dillon for (i = 0; i < 256; i++) {
1416fb9a9224SMatthew Dillon int c = table[i];
1417fb9a9224SMatthew Dillon if (c == -1) {
1418fb9a9224SMatthew Dillon e->normal.type[i] = BT_MALFORM;
1419fb9a9224SMatthew Dillon /* This shouldn't really get used. */
1420fb9a9224SMatthew Dillon e->utf16[i] = 0xFFFF;
1421fb9a9224SMatthew Dillon e->utf8[i][0] = 1;
1422fb9a9224SMatthew Dillon e->utf8[i][1] = 0;
1423*0c65ac1dSAntonio Huete Jimenez } else if (c < 0) {
1424fb9a9224SMatthew Dillon if (c < -4)
1425fb9a9224SMatthew Dillon return 0;
1426*0c65ac1dSAntonio Huete Jimenez /* Multi-byte sequences need a converter function */
1427*0c65ac1dSAntonio Huete Jimenez if (! convert)
1428*0c65ac1dSAntonio Huete Jimenez return 0;
1429fb9a9224SMatthew Dillon e->normal.type[i] = (unsigned char)(BT_LEAD2 - (c + 2));
1430fb9a9224SMatthew Dillon e->utf8[i][0] = 0;
1431fb9a9224SMatthew Dillon e->utf16[i] = 0;
1432*0c65ac1dSAntonio Huete Jimenez } else if (c < 0x80) {
1433fb9a9224SMatthew Dillon if (latin1_encoding.type[c] != BT_OTHER
1434*0c65ac1dSAntonio Huete Jimenez && latin1_encoding.type[c] != BT_NONXML && c != i)
1435fb9a9224SMatthew Dillon return 0;
1436fb9a9224SMatthew Dillon e->normal.type[i] = latin1_encoding.type[c];
1437fb9a9224SMatthew Dillon e->utf8[i][0] = 1;
1438fb9a9224SMatthew Dillon e->utf8[i][1] = (char)c;
1439fb9a9224SMatthew Dillon e->utf16[i] = (unsigned short)(c == 0 ? 0xFFFF : c);
1440*0c65ac1dSAntonio Huete Jimenez } else if (checkCharRefNumber(c) < 0) {
1441fb9a9224SMatthew Dillon e->normal.type[i] = BT_NONXML;
1442fb9a9224SMatthew Dillon /* This shouldn't really get used. */
1443fb9a9224SMatthew Dillon e->utf16[i] = 0xFFFF;
1444fb9a9224SMatthew Dillon e->utf8[i][0] = 1;
1445fb9a9224SMatthew Dillon e->utf8[i][1] = 0;
1446*0c65ac1dSAntonio Huete Jimenez } else {
1447fb9a9224SMatthew Dillon if (c > 0xFFFF)
1448fb9a9224SMatthew Dillon return 0;
1449fb9a9224SMatthew Dillon if (UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xff))
1450fb9a9224SMatthew Dillon e->normal.type[i] = BT_NMSTRT;
1451fb9a9224SMatthew Dillon else if (UCS2_GET_NAMING(namePages, c >> 8, c & 0xff))
1452fb9a9224SMatthew Dillon e->normal.type[i] = BT_NAME;
1453fb9a9224SMatthew Dillon else
1454fb9a9224SMatthew Dillon e->normal.type[i] = BT_OTHER;
1455fb9a9224SMatthew Dillon e->utf8[i][0] = (char)XmlUtf8Encode(c, e->utf8[i] + 1);
1456fb9a9224SMatthew Dillon e->utf16[i] = (unsigned short)c;
1457fb9a9224SMatthew Dillon }
1458fb9a9224SMatthew Dillon }
1459fb9a9224SMatthew Dillon e->userData = userData;
1460fb9a9224SMatthew Dillon e->convert = convert;
1461fb9a9224SMatthew Dillon if (convert) {
1462fb9a9224SMatthew Dillon e->normal.isName2 = unknown_isName;
1463fb9a9224SMatthew Dillon e->normal.isName3 = unknown_isName;
1464fb9a9224SMatthew Dillon e->normal.isName4 = unknown_isName;
1465fb9a9224SMatthew Dillon e->normal.isNmstrt2 = unknown_isNmstrt;
1466fb9a9224SMatthew Dillon e->normal.isNmstrt3 = unknown_isNmstrt;
1467fb9a9224SMatthew Dillon e->normal.isNmstrt4 = unknown_isNmstrt;
1468fb9a9224SMatthew Dillon e->normal.isInvalid2 = unknown_isInvalid;
1469fb9a9224SMatthew Dillon e->normal.isInvalid3 = unknown_isInvalid;
1470fb9a9224SMatthew Dillon e->normal.isInvalid4 = unknown_isInvalid;
1471fb9a9224SMatthew Dillon }
1472fb9a9224SMatthew Dillon e->normal.enc.utf8Convert = unknown_toUtf8;
1473fb9a9224SMatthew Dillon e->normal.enc.utf16Convert = unknown_toUtf16;
1474fb9a9224SMatthew Dillon return &(e->normal.enc);
1475fb9a9224SMatthew Dillon }
1476fb9a9224SMatthew Dillon
1477fb9a9224SMatthew Dillon /* If this enumeration is changed, getEncodingIndex and encodings
1478fb9a9224SMatthew Dillon must also be changed. */
1479fb9a9224SMatthew Dillon enum {
1480fb9a9224SMatthew Dillon UNKNOWN_ENC = -1,
1481fb9a9224SMatthew Dillon ISO_8859_1_ENC = 0,
1482fb9a9224SMatthew Dillon US_ASCII_ENC,
1483fb9a9224SMatthew Dillon UTF_8_ENC,
1484fb9a9224SMatthew Dillon UTF_16_ENC,
1485fb9a9224SMatthew Dillon UTF_16BE_ENC,
1486fb9a9224SMatthew Dillon UTF_16LE_ENC,
1487fb9a9224SMatthew Dillon /* must match encodingNames up to here */
1488fb9a9224SMatthew Dillon NO_ENC
1489fb9a9224SMatthew Dillon };
1490fb9a9224SMatthew Dillon
1491*0c65ac1dSAntonio Huete Jimenez static const char KW_ISO_8859_1[]
1492*0c65ac1dSAntonio Huete Jimenez = {ASCII_I, ASCII_S, ASCII_O, ASCII_MINUS, ASCII_8, ASCII_8,
1493*0c65ac1dSAntonio Huete Jimenez ASCII_5, ASCII_9, ASCII_MINUS, ASCII_1, '\0'};
1494*0c65ac1dSAntonio Huete Jimenez static const char KW_US_ASCII[]
1495*0c65ac1dSAntonio Huete Jimenez = {ASCII_U, ASCII_S, ASCII_MINUS, ASCII_A, ASCII_S,
1496*0c65ac1dSAntonio Huete Jimenez ASCII_C, ASCII_I, ASCII_I, '\0'};
1497*0c65ac1dSAntonio Huete Jimenez static const char KW_UTF_8[]
1498*0c65ac1dSAntonio Huete Jimenez = {ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'};
1499*0c65ac1dSAntonio Huete Jimenez static const char KW_UTF_16[]
1500*0c65ac1dSAntonio Huete Jimenez = {ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'};
1501*0c65ac1dSAntonio Huete Jimenez static const char KW_UTF_16BE[]
1502*0c65ac1dSAntonio Huete Jimenez = {ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1,
1503*0c65ac1dSAntonio Huete Jimenez ASCII_6, ASCII_B, ASCII_E, '\0'};
1504*0c65ac1dSAntonio Huete Jimenez static const char KW_UTF_16LE[]
1505*0c65ac1dSAntonio Huete Jimenez = {ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1,
1506*0c65ac1dSAntonio Huete Jimenez ASCII_6, ASCII_L, ASCII_E, '\0'};
1507fb9a9224SMatthew Dillon
1508fb9a9224SMatthew Dillon static int FASTCALL
getEncodingIndex(const char * name)1509*0c65ac1dSAntonio Huete Jimenez getEncodingIndex(const char *name) {
1510fb9a9224SMatthew Dillon static const char *const encodingNames[] = {
1511*0c65ac1dSAntonio Huete Jimenez KW_ISO_8859_1, KW_US_ASCII, KW_UTF_8, KW_UTF_16, KW_UTF_16BE, KW_UTF_16LE,
1512fb9a9224SMatthew Dillon };
1513fb9a9224SMatthew Dillon int i;
1514fb9a9224SMatthew Dillon if (name == NULL)
1515fb9a9224SMatthew Dillon return NO_ENC;
1516fb9a9224SMatthew Dillon for (i = 0; i < (int)(sizeof(encodingNames) / sizeof(encodingNames[0])); i++)
1517fb9a9224SMatthew Dillon if (streqci(name, encodingNames[i]))
1518fb9a9224SMatthew Dillon return i;
1519fb9a9224SMatthew Dillon return UNKNOWN_ENC;
1520fb9a9224SMatthew Dillon }
1521fb9a9224SMatthew Dillon
1522fb9a9224SMatthew Dillon /* For binary compatibility, we store the index of the encoding
1523fb9a9224SMatthew Dillon specified at initialization in the isUtf16 member.
1524fb9a9224SMatthew Dillon */
1525fb9a9224SMatthew Dillon
1526fb9a9224SMatthew Dillon #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1527fb9a9224SMatthew Dillon #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1528fb9a9224SMatthew Dillon
1529fb9a9224SMatthew Dillon /* This is what detects the encoding. encodingTable maps from
1530fb9a9224SMatthew Dillon encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
1531fb9a9224SMatthew Dillon the external (protocol) specified encoding; state is
1532fb9a9224SMatthew Dillon XML_CONTENT_STATE if we're parsing an external text entity, and
1533fb9a9224SMatthew Dillon XML_PROLOG_STATE otherwise.
1534fb9a9224SMatthew Dillon */
1535fb9a9224SMatthew Dillon
1536fb9a9224SMatthew Dillon static int
initScan(const ENCODING * const * encodingTable,const INIT_ENCODING * enc,int state,const char * ptr,const char * end,const char ** nextTokPtr)1537*0c65ac1dSAntonio Huete Jimenez initScan(const ENCODING *const *encodingTable, const INIT_ENCODING *enc,
1538*0c65ac1dSAntonio Huete Jimenez int state, const char *ptr, const char *end, const char **nextTokPtr) {
1539fb9a9224SMatthew Dillon const ENCODING **encPtr;
1540fb9a9224SMatthew Dillon
1541*0c65ac1dSAntonio Huete Jimenez if (ptr >= end)
1542fb9a9224SMatthew Dillon return XML_TOK_NONE;
1543fb9a9224SMatthew Dillon encPtr = enc->encPtr;
1544fb9a9224SMatthew Dillon if (ptr + 1 == end) {
1545fb9a9224SMatthew Dillon /* only a single byte available for auto-detection */
1546fb9a9224SMatthew Dillon #ifndef XML_DTD /* FIXME */
1547fb9a9224SMatthew Dillon /* a well-formed document entity must have more than one byte */
1548fb9a9224SMatthew Dillon if (state != XML_CONTENT_STATE)
1549fb9a9224SMatthew Dillon return XML_TOK_PARTIAL;
1550fb9a9224SMatthew Dillon #endif
1551fb9a9224SMatthew Dillon /* so we're parsing an external text entity... */
1552fb9a9224SMatthew Dillon /* if UTF-16 was externally specified, then we need at least 2 bytes */
1553fb9a9224SMatthew Dillon switch (INIT_ENC_INDEX(enc)) {
1554fb9a9224SMatthew Dillon case UTF_16_ENC:
1555fb9a9224SMatthew Dillon case UTF_16LE_ENC:
1556fb9a9224SMatthew Dillon case UTF_16BE_ENC:
1557fb9a9224SMatthew Dillon return XML_TOK_PARTIAL;
1558fb9a9224SMatthew Dillon }
1559fb9a9224SMatthew Dillon switch ((unsigned char)*ptr) {
1560fb9a9224SMatthew Dillon case 0xFE:
1561fb9a9224SMatthew Dillon case 0xFF:
1562fb9a9224SMatthew Dillon case 0xEF: /* possibly first byte of UTF-8 BOM */
1563*0c65ac1dSAntonio Huete Jimenez if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC && state == XML_CONTENT_STATE)
1564fb9a9224SMatthew Dillon break;
1565fb9a9224SMatthew Dillon /* fall through */
1566fb9a9224SMatthew Dillon case 0x00:
1567fb9a9224SMatthew Dillon case 0x3C:
1568fb9a9224SMatthew Dillon return XML_TOK_PARTIAL;
1569fb9a9224SMatthew Dillon }
1570*0c65ac1dSAntonio Huete Jimenez } else {
1571fb9a9224SMatthew Dillon switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
1572fb9a9224SMatthew Dillon case 0xFEFF:
1573*0c65ac1dSAntonio Huete Jimenez if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC && state == XML_CONTENT_STATE)
1574fb9a9224SMatthew Dillon break;
1575fb9a9224SMatthew Dillon *nextTokPtr = ptr + 2;
1576fb9a9224SMatthew Dillon *encPtr = encodingTable[UTF_16BE_ENC];
1577fb9a9224SMatthew Dillon return XML_TOK_BOM;
1578fb9a9224SMatthew Dillon /* 00 3C is handled in the default case */
1579fb9a9224SMatthew Dillon case 0x3C00:
1580fb9a9224SMatthew Dillon if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
1581fb9a9224SMatthew Dillon || INIT_ENC_INDEX(enc) == UTF_16_ENC)
1582fb9a9224SMatthew Dillon && state == XML_CONTENT_STATE)
1583fb9a9224SMatthew Dillon break;
1584fb9a9224SMatthew Dillon *encPtr = encodingTable[UTF_16LE_ENC];
1585fb9a9224SMatthew Dillon return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1586fb9a9224SMatthew Dillon case 0xFFFE:
1587*0c65ac1dSAntonio Huete Jimenez if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC && state == XML_CONTENT_STATE)
1588fb9a9224SMatthew Dillon break;
1589fb9a9224SMatthew Dillon *nextTokPtr = ptr + 2;
1590fb9a9224SMatthew Dillon *encPtr = encodingTable[UTF_16LE_ENC];
1591fb9a9224SMatthew Dillon return XML_TOK_BOM;
1592fb9a9224SMatthew Dillon case 0xEFBB:
1593fb9a9224SMatthew Dillon /* Maybe a UTF-8 BOM (EF BB BF) */
1594fb9a9224SMatthew Dillon /* If there's an explicitly specified (external) encoding
1595fb9a9224SMatthew Dillon of ISO-8859-1 or some flavour of UTF-16
1596fb9a9224SMatthew Dillon and this is an external text entity,
1597fb9a9224SMatthew Dillon don't look for the BOM,
1598fb9a9224SMatthew Dillon because it might be a legal data.
1599fb9a9224SMatthew Dillon */
1600fb9a9224SMatthew Dillon if (state == XML_CONTENT_STATE) {
1601fb9a9224SMatthew Dillon int e = INIT_ENC_INDEX(enc);
1602*0c65ac1dSAntonio Huete Jimenez if (e == ISO_8859_1_ENC || e == UTF_16BE_ENC || e == UTF_16LE_ENC
1603*0c65ac1dSAntonio Huete Jimenez || e == UTF_16_ENC)
1604fb9a9224SMatthew Dillon break;
1605fb9a9224SMatthew Dillon }
1606fb9a9224SMatthew Dillon if (ptr + 2 == end)
1607fb9a9224SMatthew Dillon return XML_TOK_PARTIAL;
1608fb9a9224SMatthew Dillon if ((unsigned char)ptr[2] == 0xBF) {
1609fb9a9224SMatthew Dillon *nextTokPtr = ptr + 3;
1610fb9a9224SMatthew Dillon *encPtr = encodingTable[UTF_8_ENC];
1611fb9a9224SMatthew Dillon return XML_TOK_BOM;
1612fb9a9224SMatthew Dillon }
1613fb9a9224SMatthew Dillon break;
1614fb9a9224SMatthew Dillon default:
1615fb9a9224SMatthew Dillon if (ptr[0] == '\0') {
1616fb9a9224SMatthew Dillon /* 0 isn't a legal data character. Furthermore a document
1617fb9a9224SMatthew Dillon entity can only start with ASCII characters. So the only
1618fb9a9224SMatthew Dillon way this can fail to be big-endian UTF-16 if it it's an
1619fb9a9224SMatthew Dillon external parsed general entity that's labelled as
1620fb9a9224SMatthew Dillon UTF-16LE.
1621fb9a9224SMatthew Dillon */
1622fb9a9224SMatthew Dillon if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
1623fb9a9224SMatthew Dillon break;
1624fb9a9224SMatthew Dillon *encPtr = encodingTable[UTF_16BE_ENC];
1625fb9a9224SMatthew Dillon return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1626*0c65ac1dSAntonio Huete Jimenez } else if (ptr[1] == '\0') {
1627fb9a9224SMatthew Dillon /* We could recover here in the case:
1628fb9a9224SMatthew Dillon - parsing an external entity
1629fb9a9224SMatthew Dillon - second byte is 0
1630fb9a9224SMatthew Dillon - no externally specified encoding
1631fb9a9224SMatthew Dillon - no encoding declaration
1632fb9a9224SMatthew Dillon by assuming UTF-16LE. But we don't, because this would mean when
1633fb9a9224SMatthew Dillon presented just with a single byte, we couldn't reliably determine
1634fb9a9224SMatthew Dillon whether we needed further bytes.
1635fb9a9224SMatthew Dillon */
1636fb9a9224SMatthew Dillon if (state == XML_CONTENT_STATE)
1637fb9a9224SMatthew Dillon break;
1638fb9a9224SMatthew Dillon *encPtr = encodingTable[UTF_16LE_ENC];
1639fb9a9224SMatthew Dillon return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1640fb9a9224SMatthew Dillon }
1641fb9a9224SMatthew Dillon break;
1642fb9a9224SMatthew Dillon }
1643fb9a9224SMatthew Dillon }
1644fb9a9224SMatthew Dillon *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
1645fb9a9224SMatthew Dillon return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1646fb9a9224SMatthew Dillon }
1647fb9a9224SMatthew Dillon
1648fb9a9224SMatthew Dillon #define NS(x) x
1649fb9a9224SMatthew Dillon #define ns(x) x
1650fb9a9224SMatthew Dillon #define XML_TOK_NS_C
1651fb9a9224SMatthew Dillon #include "xmltok_ns.c"
1652fb9a9224SMatthew Dillon #undef XML_TOK_NS_C
1653fb9a9224SMatthew Dillon #undef NS
1654fb9a9224SMatthew Dillon #undef ns
1655fb9a9224SMatthew Dillon
1656fb9a9224SMatthew Dillon #ifdef XML_NS
1657fb9a9224SMatthew Dillon
1658fb9a9224SMatthew Dillon # define NS(x) x##NS
1659fb9a9224SMatthew Dillon # define ns(x) x##_ns
1660fb9a9224SMatthew Dillon
1661fb9a9224SMatthew Dillon # define XML_TOK_NS_C
1662fb9a9224SMatthew Dillon # include "xmltok_ns.c"
1663fb9a9224SMatthew Dillon # undef XML_TOK_NS_C
1664fb9a9224SMatthew Dillon
1665fb9a9224SMatthew Dillon # undef NS
1666fb9a9224SMatthew Dillon # undef ns
1667fb9a9224SMatthew Dillon
1668fb9a9224SMatthew Dillon ENCODING *
XmlInitUnknownEncodingNS(void * mem,int * table,CONVERTER convert,void * userData)1669*0c65ac1dSAntonio Huete Jimenez XmlInitUnknownEncodingNS(void *mem, int *table, CONVERTER convert,
1670*0c65ac1dSAntonio Huete Jimenez void *userData) {
1671fb9a9224SMatthew Dillon ENCODING *enc = XmlInitUnknownEncoding(mem, table, convert, userData);
1672fb9a9224SMatthew Dillon if (enc)
1673fb9a9224SMatthew Dillon ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;
1674fb9a9224SMatthew Dillon return enc;
1675fb9a9224SMatthew Dillon }
1676fb9a9224SMatthew Dillon
1677fb9a9224SMatthew Dillon #endif /* XML_NS */
1678