1355604Sdelphij/* This file is included!
2355604Sdelphij                            __  __            _
3355604Sdelphij                         ___\ \/ /_ __   __ _| |_
4355604Sdelphij                        / _ \\  /| '_ \ / _` | __|
5355604Sdelphij                       |  __//  \| |_) | (_| | |_
6355604Sdelphij                        \___/_/\_\ .__/ \__,_|\__|
7355604Sdelphij                                 |_| XML parser
8355604Sdelphij
9355604Sdelphij   Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10355604Sdelphij   Copyright (c) 2000-2017 Expat development team
11355604Sdelphij   Licensed under the MIT license:
12355604Sdelphij
13355604Sdelphij   Permission is  hereby granted,  free of charge,  to any  person obtaining
14355604Sdelphij   a  copy  of  this  software   and  associated  documentation  files  (the
15355604Sdelphij   "Software"),  to  deal in  the  Software  without restriction,  including
16355604Sdelphij   without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17355604Sdelphij   distribute, sublicense, and/or sell copies of the Software, and to permit
18355604Sdelphij   persons  to whom  the Software  is  furnished to  do so,  subject to  the
19355604Sdelphij   following conditions:
20355604Sdelphij
21355604Sdelphij   The above copyright  notice and this permission notice  shall be included
22355604Sdelphij   in all copies or substantial portions of the Software.
23355604Sdelphij
24355604Sdelphij   THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25355604Sdelphij   EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26355604Sdelphij   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27355604Sdelphij   NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28355604Sdelphij   DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29355604Sdelphij   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30355604Sdelphij   USE OR OTHER DEALINGS IN THE SOFTWARE.
31178848Scokane*/
32178848Scokane
33178848Scokane#ifdef XML_TOK_NS_C
34178848Scokane
35104349Sphkconst ENCODING *
36355604SdelphijNS(XmlGetUtf8InternalEncoding)(void) {
37104349Sphk  return &ns(internal_utf8_encoding).enc;
38104349Sphk}
39104349Sphk
40104349Sphkconst ENCODING *
41355604SdelphijNS(XmlGetUtf16InternalEncoding)(void) {
42355604Sdelphij#  if BYTEORDER == 1234
43104349Sphk  return &ns(internal_little2_encoding).enc;
44355604Sdelphij#  elif BYTEORDER == 4321
45104349Sphk  return &ns(internal_big2_encoding).enc;
46355604Sdelphij#  else
47104349Sphk  const short n = 1;
48355604Sdelphij  return (*(const char *)&n ? &ns(internal_little2_encoding).enc
49355604Sdelphij                            : &ns(internal_big2_encoding).enc);
50355604Sdelphij#  endif
51104349Sphk}
52104349Sphk
53355604Sdelphijstatic const ENCODING *const NS(encodings)[] = {
54355604Sdelphij    &ns(latin1_encoding).enc, &ns(ascii_encoding).enc,
55355604Sdelphij    &ns(utf8_encoding).enc,   &ns(big2_encoding).enc,
56355604Sdelphij    &ns(big2_encoding).enc,   &ns(little2_encoding).enc,
57355604Sdelphij    &ns(utf8_encoding).enc /* NO_ENC */
58104349Sphk};
59104349Sphk
60178848Scokanestatic int PTRCALL
61104349SphkNS(initScanProlog)(const ENCODING *enc, const char *ptr, const char *end,
62355604Sdelphij                   const char **nextTokPtr) {
63355604Sdelphij  return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_PROLOG_STATE,
64355604Sdelphij                  ptr, end, nextTokPtr);
65104349Sphk}
66104349Sphk
67178848Scokanestatic int PTRCALL
68104349SphkNS(initScanContent)(const ENCODING *enc, const char *ptr, const char *end,
69355604Sdelphij                    const char **nextTokPtr) {
70355604Sdelphij  return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_CONTENT_STATE,
71355604Sdelphij                  ptr, end, nextTokPtr);
72104349Sphk}
73104349Sphk
74104349Sphkint
75104349SphkNS(XmlInitEncoding)(INIT_ENCODING *p, const ENCODING **encPtr,
76355604Sdelphij                    const char *name) {
77104349Sphk  int i = getEncodingIndex(name);
78104349Sphk  if (i == UNKNOWN_ENC)
79104349Sphk    return 0;
80104349Sphk  SET_INIT_ENC_INDEX(p, i);
81104349Sphk  p->initEnc.scanners[XML_PROLOG_STATE] = NS(initScanProlog);
82104349Sphk  p->initEnc.scanners[XML_CONTENT_STATE] = NS(initScanContent);
83104349Sphk  p->initEnc.updatePosition = initUpdatePosition;
84104349Sphk  p->encPtr = encPtr;
85104349Sphk  *encPtr = &(p->initEnc);
86104349Sphk  return 1;
87104349Sphk}
88104349Sphk
89104349Sphkstatic const ENCODING *
90355604SdelphijNS(findEncoding)(const ENCODING *enc, const char *ptr, const char *end) {
91355604Sdelphij#  define ENCODING_MAX 128
92104349Sphk  char buf[ENCODING_MAX];
93104349Sphk  char *p = buf;
94104349Sphk  int i;
95104349Sphk  XmlUtf8Convert(enc, &ptr, end, &p, p + ENCODING_MAX - 1);
96104349Sphk  if (ptr != end)
97104349Sphk    return 0;
98104349Sphk  *p = 0;
99104349Sphk  if (streqci(buf, KW_UTF_16) && enc->minBytesPerChar == 2)
100104349Sphk    return enc;
101104349Sphk  i = getEncodingIndex(buf);
102104349Sphk  if (i == UNKNOWN_ENC)
103104349Sphk    return 0;
104104349Sphk  return NS(encodings)[i];
105104349Sphk}
106104349Sphk
107104349Sphkint
108355604SdelphijNS(XmlParseXmlDecl)(int isGeneralTextEntity, const ENCODING *enc,
109355604Sdelphij                    const char *ptr, const char *end, const char **badPtr,
110355604Sdelphij                    const char **versionPtr, const char **versionEndPtr,
111355604Sdelphij                    const char **encodingName, const ENCODING **encoding,
112355604Sdelphij                    int *standalone) {
113355604Sdelphij  return doParseXmlDecl(NS(findEncoding), isGeneralTextEntity, enc, ptr, end,
114355604Sdelphij                        badPtr, versionPtr, versionEndPtr, encodingName,
115355604Sdelphij                        encoding, standalone);
116104349Sphk}
117178848Scokane
118178848Scokane#endif /* XML_TOK_NS_C */
119