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