1 /* This file is included!
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10    Copyright (c) 2000-2017 Expat development team
11    Licensed under the MIT license:
12 
13    Permission is  hereby granted,  free of charge,  to any  person obtaining
14    a  copy  of  this  software   and  associated  documentation  files  (the
15    "Software"),  to  deal in  the  Software  without restriction,  including
16    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17    distribute, sublicense, and/or sell copies of the Software, and to permit
18    persons  to whom  the Software  is  furnished to  do so,  subject to  the
19    following conditions:
20 
21    The above copyright  notice and this permission notice  shall be included
22    in all copies or substantial portions of the Software.
23 
24    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30    USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32 
33 #ifdef XML_TOK_NS_C
34 
35 const ENCODING *
NS(XmlGetUtf8InternalEncoding)36 NS(XmlGetUtf8InternalEncoding)(void)
37 {
38   return &ns(internal_utf8_encoding).enc;
39 }
40 
41 const ENCODING *
NS(XmlGetUtf16InternalEncoding)42 NS(XmlGetUtf16InternalEncoding)(void)
43 {
44 #if BYTEORDER == 1234
45   return &ns(internal_little2_encoding).enc;
46 #elif BYTEORDER == 4321
47   return &ns(internal_big2_encoding).enc;
48 #else
49   const short n = 1;
50   return (*(const char *)&n
51           ? &ns(internal_little2_encoding).enc
52           : &ns(internal_big2_encoding).enc);
53 #endif
54 }
55 
56 static const ENCODING * const NS(encodings)[] = {
57   &ns(latin1_encoding).enc,
58   &ns(ascii_encoding).enc,
59   &ns(utf8_encoding).enc,
60   &ns(big2_encoding).enc,
61   &ns(big2_encoding).enc,
62   &ns(little2_encoding).enc,
63   &ns(utf8_encoding).enc /* NO_ENC */
64 };
65 
66 static int PTRCALL
NS(initScanProlog)67 NS(initScanProlog)(const ENCODING *enc, const char *ptr, const char *end,
68                    const char **nextTokPtr)
69 {
70   return initScan(NS(encodings), (const INIT_ENCODING *)enc,
71                   XML_PROLOG_STATE, ptr, end, nextTokPtr);
72 }
73 
74 static int PTRCALL
NS(initScanContent)75 NS(initScanContent)(const ENCODING *enc, const char *ptr, const char *end,
76                     const char **nextTokPtr)
77 {
78   return initScan(NS(encodings), (const INIT_ENCODING *)enc,
79                   XML_CONTENT_STATE, ptr, end, nextTokPtr);
80 }
81 
82 int
NS(XmlInitEncoding)83 NS(XmlInitEncoding)(INIT_ENCODING *p, const ENCODING **encPtr,
84                     const char *name)
85 {
86   int i = getEncodingIndex(name);
87   if (i == UNKNOWN_ENC)
88     return 0;
89   SET_INIT_ENC_INDEX(p, i);
90   p->initEnc.scanners[XML_PROLOG_STATE] = NS(initScanProlog);
91   p->initEnc.scanners[XML_CONTENT_STATE] = NS(initScanContent);
92   p->initEnc.updatePosition = initUpdatePosition;
93   p->encPtr = encPtr;
94   *encPtr = &(p->initEnc);
95   return 1;
96 }
97 
98 static const ENCODING *
NS(findEncoding)99 NS(findEncoding)(const ENCODING *enc, const char *ptr, const char *end)
100 {
101 #define ENCODING_MAX 128
102   char buf[ENCODING_MAX];
103   char *p = buf;
104   int i;
105   XmlUtf8Convert(enc, &ptr, end, &p, p + ENCODING_MAX - 1);
106   if (ptr != end)
107     return 0;
108   *p = 0;
109   if (streqci(buf, KW_UTF_16) && enc->minBytesPerChar == 2)
110     return enc;
111   i = getEncodingIndex(buf);
112   if (i == UNKNOWN_ENC)
113     return 0;
114   return NS(encodings)[i];
115 }
116 
117 int
NS(XmlParseXmlDecl)118 NS(XmlParseXmlDecl)(int isGeneralTextEntity,
119                     const ENCODING *enc,
120                     const char *ptr,
121                     const char *end,
122                     const char **badPtr,
123                     const char **versionPtr,
124                     const char **versionEndPtr,
125                     const char **encodingName,
126                     const ENCODING **encoding,
127                     int *standalone)
128 {
129   return doParseXmlDecl(NS(findEncoding),
130                         isGeneralTextEntity,
131                         enc,
132                         ptr,
133                         end,
134                         badPtr,
135                         versionPtr,
136                         versionEndPtr,
137                         encodingName,
138                         encoding,
139                         standalone);
140 }
141 
142 #endif /* XML_TOK_NS_C */
143