xref: /openbsd-src/lib/libexpat/lib/xmltok_ns.c (revision 2c19dcf8d95af7b2b4a1f370f8f03efeacadec60)
12e724bc9Sbluhm /* This file is included!
22e724bc9Sbluhm                             __  __            _
32e724bc9Sbluhm                          ___\ \/ /_ __   __ _| |_
42e724bc9Sbluhm                         / _ \\  /| '_ \ / _` | __|
52e724bc9Sbluhm                        |  __//  \| |_) | (_| | |_
62e724bc9Sbluhm                         \___/_/\_\ .__/ \__,_|\__|
72e724bc9Sbluhm                                  |_| XML parser
82e724bc9Sbluhm 
92e724bc9Sbluhm    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
1008819b41Sbluhm    Copyright (c) 2000      Clark Cooper <coopercc@users.sourceforge.net>
1108819b41Sbluhm    Copyright (c) 2002      Greg Stein <gstein@users.sourceforge.net>
1208819b41Sbluhm    Copyright (c) 2002      Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
1308819b41Sbluhm    Copyright (c) 2002-2006 Karl Waclawek <karl@waclawek.net>
14*2c19dcf8Sbluhm    Copyright (c) 2017-2021 Sebastian Pipping <sebastian@pipping.org>
152e724bc9Sbluhm    Licensed under the MIT license:
162e724bc9Sbluhm 
172e724bc9Sbluhm    Permission is  hereby granted,  free of charge,  to any  person obtaining
182e724bc9Sbluhm    a  copy  of  this  software   and  associated  documentation  files  (the
192e724bc9Sbluhm    "Software"),  to  deal in  the  Software  without restriction,  including
202e724bc9Sbluhm    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
212e724bc9Sbluhm    distribute, sublicense, and/or sell copies of the Software, and to permit
222e724bc9Sbluhm    persons  to whom  the Software  is  furnished to  do so,  subject to  the
232e724bc9Sbluhm    following conditions:
242e724bc9Sbluhm 
252e724bc9Sbluhm    The above copyright  notice and this permission notice  shall be included
262e724bc9Sbluhm    in all copies or substantial portions of the Software.
272e724bc9Sbluhm 
282e724bc9Sbluhm    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
292e724bc9Sbluhm    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
302e724bc9Sbluhm    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
312e724bc9Sbluhm    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
322e724bc9Sbluhm    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
332e724bc9Sbluhm    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
342e724bc9Sbluhm    USE OR OTHER DEALINGS IN THE SOFTWARE.
35b26ab0f8Smatthieu */
36b26ab0f8Smatthieu 
37b26ab0f8Smatthieu #ifdef XML_TOK_NS_C
38b26ab0f8Smatthieu 
391a3ddf8cSespie const ENCODING *
NS(XmlGetUtf8InternalEncoding)4028ce3119Sbluhm NS(XmlGetUtf8InternalEncoding)(void) {
411a3ddf8cSespie   return &ns(internal_utf8_encoding).enc;
421a3ddf8cSespie }
431a3ddf8cSespie 
441a3ddf8cSespie const ENCODING *
NS(XmlGetUtf16InternalEncoding)4528ce3119Sbluhm NS(XmlGetUtf16InternalEncoding)(void) {
461a3ddf8cSespie #  if BYTEORDER == 1234
471a3ddf8cSespie   return &ns(internal_little2_encoding).enc;
481a3ddf8cSespie #  elif BYTEORDER == 4321
491a3ddf8cSespie   return &ns(internal_big2_encoding).enc;
501a3ddf8cSespie #  else
511a3ddf8cSespie   const short n = 1;
5228ce3119Sbluhm   return (*(const char *)&n ? &ns(internal_little2_encoding).enc
531a3ddf8cSespie                             : &ns(internal_big2_encoding).enc);
541a3ddf8cSespie #  endif
551a3ddf8cSespie }
561a3ddf8cSespie 
577d36914fSalek static const ENCODING *const NS(encodings)[] = {
5828ce3119Sbluhm     &ns(latin1_encoding).enc, &ns(ascii_encoding).enc,
5928ce3119Sbluhm     &ns(utf8_encoding).enc,   &ns(big2_encoding).enc,
6028ce3119Sbluhm     &ns(big2_encoding).enc,   &ns(little2_encoding).enc,
611a3ddf8cSespie     &ns(utf8_encoding).enc /* NO_ENC */
621a3ddf8cSespie };
631a3ddf8cSespie 
641a3ddf8cSespie static int PTRCALL
NS(initScanProlog)651a3ddf8cSespie NS(initScanProlog)(const ENCODING *enc, const char *ptr, const char *end,
6628ce3119Sbluhm                    const char **nextTokPtr) {
6728ce3119Sbluhm   return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_PROLOG_STATE,
6828ce3119Sbluhm                   ptr, end, nextTokPtr);
691a3ddf8cSespie }
701a3ddf8cSespie 
711a3ddf8cSespie static int PTRCALL
NS(initScanContent)721a3ddf8cSespie NS(initScanContent)(const ENCODING *enc, const char *ptr, const char *end,
7328ce3119Sbluhm                     const char **nextTokPtr) {
7428ce3119Sbluhm   return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_CONTENT_STATE,
7528ce3119Sbluhm                   ptr, end, nextTokPtr);
761a3ddf8cSespie }
771a3ddf8cSespie 
781a3ddf8cSespie int
NS(XmlInitEncoding)791a3ddf8cSespie NS(XmlInitEncoding)(INIT_ENCODING *p, const ENCODING **encPtr,
8028ce3119Sbluhm                     const char *name) {
811a3ddf8cSespie   int i = getEncodingIndex(name);
821a3ddf8cSespie   if (i == UNKNOWN_ENC)
831a3ddf8cSespie     return 0;
841a3ddf8cSespie   SET_INIT_ENC_INDEX(p, i);
851a3ddf8cSespie   p->initEnc.scanners[XML_PROLOG_STATE] = NS(initScanProlog);
861a3ddf8cSespie   p->initEnc.scanners[XML_CONTENT_STATE] = NS(initScanContent);
871a3ddf8cSespie   p->initEnc.updatePosition = initUpdatePosition;
881a3ddf8cSespie   p->encPtr = encPtr;
891a3ddf8cSespie   *encPtr = &(p->initEnc);
901a3ddf8cSespie   return 1;
911a3ddf8cSespie }
921a3ddf8cSespie 
931a3ddf8cSespie static const ENCODING *
NS(findEncoding)9428ce3119Sbluhm NS(findEncoding)(const ENCODING *enc, const char *ptr, const char *end) {
951a3ddf8cSespie #  define ENCODING_MAX 128
96*2c19dcf8Sbluhm   char buf[ENCODING_MAX] = "";
971a3ddf8cSespie   char *p = buf;
981a3ddf8cSespie   int i;
991a3ddf8cSespie   XmlUtf8Convert(enc, &ptr, end, &p, p + ENCODING_MAX - 1);
1001a3ddf8cSespie   if (ptr != end)
1011a3ddf8cSespie     return 0;
1021a3ddf8cSespie   *p = 0;
1031a3ddf8cSespie   if (streqci(buf, KW_UTF_16) && enc->minBytesPerChar == 2)
1041a3ddf8cSespie     return enc;
1051a3ddf8cSespie   i = getEncodingIndex(buf);
1061a3ddf8cSespie   if (i == UNKNOWN_ENC)
1071a3ddf8cSespie     return 0;
1081a3ddf8cSespie   return NS(encodings)[i];
1091a3ddf8cSespie }
1101a3ddf8cSespie 
1111a3ddf8cSespie int
NS(XmlParseXmlDecl)11228ce3119Sbluhm NS(XmlParseXmlDecl)(int isGeneralTextEntity, const ENCODING *enc,
11328ce3119Sbluhm                     const char *ptr, const char *end, const char **badPtr,
11428ce3119Sbluhm                     const char **versionPtr, const char **versionEndPtr,
11528ce3119Sbluhm                     const char **encodingName, const ENCODING **encoding,
11628ce3119Sbluhm                     int *standalone) {
11728ce3119Sbluhm   return doParseXmlDecl(NS(findEncoding), isGeneralTextEntity, enc, ptr, end,
11828ce3119Sbluhm                         badPtr, versionPtr, versionEndPtr, encodingName,
11928ce3119Sbluhm                         encoding, standalone);
1201a3ddf8cSespie }
121b26ab0f8Smatthieu 
122b26ab0f8Smatthieu #endif /* XML_TOK_NS_C */
123