xref: /freebsd/contrib/expat/lib/xmltok_ns.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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 *
36 NS(XmlGetUtf8InternalEncoding)(void) {
37   return &ns(internal_utf8_encoding).enc;
38 }
39 
40 const ENCODING *
41 NS(XmlGetUtf16InternalEncoding)(void) {
42 #  if BYTEORDER == 1234
43   return &ns(internal_little2_encoding).enc;
44 #  elif BYTEORDER == 4321
45   return &ns(internal_big2_encoding).enc;
46 #  else
47   const short n = 1;
48   return (*(const char *)&n ? &ns(internal_little2_encoding).enc
49                             : &ns(internal_big2_encoding).enc);
50 #  endif
51 }
52 
53 static const ENCODING *const NS(encodings)[] = {
54     &ns(latin1_encoding).enc, &ns(ascii_encoding).enc,
55     &ns(utf8_encoding).enc,   &ns(big2_encoding).enc,
56     &ns(big2_encoding).enc,   &ns(little2_encoding).enc,
57     &ns(utf8_encoding).enc /* NO_ENC */
58 };
59 
60 static int PTRCALL
61 NS(initScanProlog)(const ENCODING *enc, const char *ptr, const char *end,
62                    const char **nextTokPtr) {
63   return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_PROLOG_STATE,
64                   ptr, end, nextTokPtr);
65 }
66 
67 static int PTRCALL
68 NS(initScanContent)(const ENCODING *enc, const char *ptr, const char *end,
69                     const char **nextTokPtr) {
70   return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_CONTENT_STATE,
71                   ptr, end, nextTokPtr);
72 }
73 
74 int
75 NS(XmlInitEncoding)(INIT_ENCODING *p, const ENCODING **encPtr,
76                     const char *name) {
77   int i = getEncodingIndex(name);
78   if (i == UNKNOWN_ENC)
79     return 0;
80   SET_INIT_ENC_INDEX(p, i);
81   p->initEnc.scanners[XML_PROLOG_STATE] = NS(initScanProlog);
82   p->initEnc.scanners[XML_CONTENT_STATE] = NS(initScanContent);
83   p->initEnc.updatePosition = initUpdatePosition;
84   p->encPtr = encPtr;
85   *encPtr = &(p->initEnc);
86   return 1;
87 }
88 
89 static const ENCODING *
90 NS(findEncoding)(const ENCODING *enc, const char *ptr, const char *end) {
91 #  define ENCODING_MAX 128
92   char buf[ENCODING_MAX];
93   char *p = buf;
94   int i;
95   XmlUtf8Convert(enc, &ptr, end, &p, p + ENCODING_MAX - 1);
96   if (ptr != end)
97     return 0;
98   *p = 0;
99   if (streqci(buf, KW_UTF_16) && enc->minBytesPerChar == 2)
100     return enc;
101   i = getEncodingIndex(buf);
102   if (i == UNKNOWN_ENC)
103     return 0;
104   return NS(encodings)[i];
105 }
106 
107 int
108 NS(XmlParseXmlDecl)(int isGeneralTextEntity, const ENCODING *enc,
109                     const char *ptr, const char *end, const char **badPtr,
110                     const char **versionPtr, const char **versionEndPtr,
111                     const char **encodingName, const ENCODING **encoding,
112                     int *standalone) {
113   return doParseXmlDecl(NS(findEncoding), isGeneralTextEntity, enc, ptr, end,
114                         badPtr, versionPtr, versionEndPtr, encodingName,
115                         encoding, standalone);
116 }
117 
118 #endif /* XML_TOK_NS_C */
119