cpython/Modules/expat/xmltok.c

/*
                            __  __            _
                         ___\ \/ /_ __   __ _| |_
                        / _ \\  /| '_ \ / _` | __|
                       |  __//  \| |_) | (_| | |_
                        \___/_/\_\ .__/ \__,_|\__|
                                 |_| XML parser

   Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
   Copyright (c) 2000      Clark Cooper <[email protected]>
   Copyright (c) 2001-2003 Fred L. Drake, Jr. <[email protected]>
   Copyright (c) 2002      Greg Stein <[email protected]>
   Copyright (c) 2002-2016 Karl Waclawek <[email protected]>
   Copyright (c) 2005-2009 Steven Solie <[email protected]>
   Copyright (c) 2016-2024 Sebastian Pipping <[email protected]>
   Copyright (c) 2016      Pascal Cuoq <[email protected]>
   Copyright (c) 2016      Don Lewis <[email protected]>
   Copyright (c) 2017      Rhodri James <[email protected]>
   Copyright (c) 2017      Alexander Bluhm <[email protected]>
   Copyright (c) 2017      Benbuck Nason <[email protected]>
   Copyright (c) 2017      José Gutiérrez de la Concha <[email protected]>
   Copyright (c) 2019      David Loffredo <[email protected]>
   Copyright (c) 2021      Donghee Na <[email protected]>
   Copyright (c) 2022      Martin Ettl <[email protected]>
   Copyright (c) 2022      Sean McBride <[email protected]>
   Copyright (c) 2023      Hanno Böck <[email protected]>
   Licensed under the MIT license:

   Permission is  hereby granted,  free of charge,  to any  person obtaining
   a  copy  of  this  software   and  associated  documentation  files  (the
   "Software"),  to  deal in  the  Software  without restriction,  including
   without  limitation the  rights  to use,  copy,  modify, merge,  publish,
   distribute, sublicense, and/or sell copies of the Software, and to permit
   persons  to whom  the Software  is  furnished to  do so,  subject to  the
   following conditions:

   The above copyright  notice and this permission notice  shall be included
   in all copies or substantial portions of the Software.

   THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
   EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
   NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
   USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "expat_config.h"

#include <stddef.h>
#include <string.h> /* memcpy */
#include <stdbool.h>

#ifdef _WIN32
#  include "winconfig.h"
#endif

#include "expat_external.h"
#include "internal.h"
#include "xmltok.h"
#include "nametab.h"

#ifdef XML_DTD
#define IGNORE_SECTION_TOK_VTABLE
#else
#define IGNORE_SECTION_TOK_VTABLE
#endif

#define VTABLE1

#define VTABLE

#define UCS2_GET_NAMING(pages, hi, lo)

/* A 2 byte UTF-8 representation splits the characters 11 bits between
   the bottom 5 and 6 bits of the bytes.  We need 8 bits to index into
   pages, 3 bits to add to that index and 5 bits to generate the mask.
*/
#define UTF8_GET_NAMING2(pages, byte)

/* A 3 byte UTF-8 representation splits the characters 16 bits between
   the bottom 4, 6 and 6 bits of the bytes.  We need 8 bits to index
   into pages, 3 bits to add to that index and 5 bits to generate the
   mask.
*/
#define UTF8_GET_NAMING3(pages, byte)

/* Detection of invalid UTF-8 sequences is based on Table 3.1B
   of Unicode 3.2: https://www.unicode.org/unicode/reports/tr28/
   with the additional restriction of not allowing the Unicode
   code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
   Implementation details:
     (A & 0x80) == 0     means A < 0x80
   and
     (A & 0xC0) == 0xC0  means A > 0xBF
*/

#define UTF8_INVALID2(p)

#define UTF8_INVALID3(p)

#define UTF8_INVALID4(p)

static int PTRFASTCALL
isNever(const ENCODING *enc, const char *p) {}

static int PTRFASTCALL
utf8_isName2(const ENCODING *enc, const char *p) {}

static int PTRFASTCALL
utf8_isName3(const ENCODING *enc, const char *p) {}

#define utf8_isName4

static int PTRFASTCALL
utf8_isNmstrt2(const ENCODING *enc, const char *p) {}

static int PTRFASTCALL
utf8_isNmstrt3(const ENCODING *enc, const char *p) {}

#define utf8_isNmstrt4

static int PTRFASTCALL
utf8_isInvalid2(const ENCODING *enc, const char *p) {}

static int PTRFASTCALL
utf8_isInvalid3(const ENCODING *enc, const char *p) {}

static int PTRFASTCALL
utf8_isInvalid4(const ENCODING *enc, const char *p) {}

struct normal_encoding {};

#define AS_NORMAL_ENCODING(enc)

#ifdef XML_MIN_SIZE

#define STANDARD_VTABLE

#else

#define STANDARD_VTABLE(E)

#endif

#define NORMAL_VTABLE(E)

#define NULL_VTABLE

static int FASTCALL checkCharRefNumber(int result);

#include "xmltok_impl.h"
#include "ascii.h"

#ifdef XML_MIN_SIZE
#define sb_isNameMin
#define sb_isNmstrtMin
#endif

#ifdef XML_MIN_SIZE
#define MINBPC
#else
/* minimum bytes per character */
#define MINBPC
#endif

#define SB_BYTE_TYPE(enc, p)

#ifdef XML_MIN_SIZE
static int PTRFASTCALL
sb_byteType(const ENCODING *enc, const char *p) {
  return SB_BYTE_TYPE(enc, p);
}
#define BYTE_TYPE
#else
#define BYTE_TYPE
#endif

#ifdef XML_MIN_SIZE
#define BYTE_TO_ASCII
static int PTRFASTCALL
sb_byteToAscii(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return *p;
}
#else
#define BYTE_TO_ASCII
#endif

#define IS_NAME_CHAR
#define IS_NMSTRT_CHAR
#ifdef XML_MIN_SIZE
#define IS_INVALID_CHAR
#else
#define IS_INVALID_CHAR
#endif

#ifdef XML_MIN_SIZE
#define IS_NAME_CHAR_MINBPC
#define IS_NMSTRT_CHAR_MINBPC
#else
#define IS_NAME_CHAR_MINBPC
#define IS_NMSTRT_CHAR_MINBPC
#endif

#ifdef XML_MIN_SIZE
#define CHAR_MATCHES
static int PTRCALL
sb_charMatches(const ENCODING *enc, const char *p, int c) {
  UNUSED_P(enc);
  return *p == c;
}
#else
/* c is an ASCII character */
#define CHAR_MATCHES
#endif

#define PREFIX
#define XML_TOK_IMPL_C
#include "xmltok_impl.c"
#undef XML_TOK_IMPL_C

#undef MINBPC
#undef BYTE_TYPE
#undef BYTE_TO_ASCII
#undef CHAR_MATCHES
#undef IS_NAME_CHAR
#undef IS_NAME_CHAR_MINBPC
#undef IS_NMSTRT_CHAR
#undef IS_NMSTRT_CHAR_MINBPC
#undef IS_INVALID_CHAR

enum {};

void
_INTERNAL_trim_to_complete_utf8_characters(const char *from,
                                           const char **fromLimRef) {}

static enum XML_Convert_Result PTRCALL
utf8_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
            char **toP, const char *toLim) {}

static enum XML_Convert_Result PTRCALL
utf8_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
             unsigned short **toP, const unsigned short *toLim) {}

#ifdef XML_NS
static const struct normal_encoding utf8_encoding_ns
    =;
#endif

static const struct normal_encoding utf8_encoding
    =;

#ifdef XML_NS

static const struct normal_encoding internal_utf8_encoding_ns
    =;

#endif

static const struct normal_encoding internal_utf8_encoding
    =;

static enum XML_Convert_Result PTRCALL
latin1_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
              char **toP, const char *toLim) {}

static enum XML_Convert_Result PTRCALL
latin1_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
               unsigned short **toP, const unsigned short *toLim) {}

#ifdef XML_NS

static const struct normal_encoding latin1_encoding_ns
    =;

#endif

static const struct normal_encoding latin1_encoding
    =;

static enum XML_Convert_Result PTRCALL
ascii_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
             char **toP, const char *toLim) {}

#ifdef XML_NS

static const struct normal_encoding ascii_encoding_ns
    =;

#endif

static const struct normal_encoding ascii_encoding
    =;

static int PTRFASTCALL
unicode_byte_type(char hi, char lo) {}

#define DEFINE_UTF16_TO_UTF8(E)

#define DEFINE_UTF16_TO_UTF16(E)

#define GET_LO
#define GET_HI

DEFINE_UTF16_TO_UTF8(little2_)
DEFINE_UTF16_TO_UTF16(little2_)

#undef GET_LO
#undef GET_HI

#define GET_LO
#define GET_HI

DEFINE_UTF16_TO_UTF8(big2_)
DEFINE_UTF16_TO_UTF16(big2_)

#undef GET_LO
#undef GET_HI

#define LITTLE2_BYTE_TYPE(enc, p)
#define LITTLE2_BYTE_TO_ASCII(p)
#define LITTLE2_CHAR_MATCHES(p, c)
#define LITTLE2_IS_NAME_CHAR_MINBPC(p)
#define LITTLE2_IS_NMSTRT_CHAR_MINBPC(p)

#ifdef XML_MIN_SIZE

static int PTRFASTCALL
little2_byteType(const ENCODING *enc, const char *p) {
  return LITTLE2_BYTE_TYPE(enc, p);
}

static int PTRFASTCALL
little2_byteToAscii(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return LITTLE2_BYTE_TO_ASCII(p);
}

static int PTRCALL
little2_charMatches(const ENCODING *enc, const char *p, int c) {
  UNUSED_P(enc);
  return LITTLE2_CHAR_MATCHES(p, c);
}

static int PTRFASTCALL
little2_isNameMin(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return LITTLE2_IS_NAME_CHAR_MINBPC(p);
}

static int PTRFASTCALL
little2_isNmstrtMin(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return LITTLE2_IS_NMSTRT_CHAR_MINBPC(p);
}

#  undef VTABLE
#define VTABLE

#else /* not XML_MIN_SIZE */

#  undef PREFIX
#define PREFIX
#define MINBPC
/* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
#define BYTE_TYPE
#define BYTE_TO_ASCII
#define CHAR_MATCHES
#define IS_NAME_CHAR
#define IS_NAME_CHAR_MINBPC
#define IS_NMSTRT_CHAR
#define IS_NMSTRT_CHAR_MINBPC

#define XML_TOK_IMPL_C
#  include "xmltok_impl.c"
#  undef XML_TOK_IMPL_C

#  undef MINBPC
#  undef BYTE_TYPE
#  undef BYTE_TO_ASCII
#  undef CHAR_MATCHES
#  undef IS_NAME_CHAR
#  undef IS_NAME_CHAR_MINBPC
#  undef IS_NMSTRT_CHAR
#  undef IS_NMSTRT_CHAR_MINBPC
#  undef IS_INVALID_CHAR

#endif /* not XML_MIN_SIZE */

#ifdef XML_NS

static const struct normal_encoding little2_encoding_ns
    =;

#endif

static const struct normal_encoding little2_encoding
    =;

#if BYTEORDER != 4321

#  ifdef XML_NS

static const struct normal_encoding internal_little2_encoding_ns
    =;

#  endif

static const struct normal_encoding internal_little2_encoding
    =;

#endif

#define BIG2_BYTE_TYPE(enc, p)
#define BIG2_BYTE_TO_ASCII(p)
#define BIG2_CHAR_MATCHES(p, c)
#define BIG2_IS_NAME_CHAR_MINBPC(p)
#define BIG2_IS_NMSTRT_CHAR_MINBPC(p)

#ifdef XML_MIN_SIZE

static int PTRFASTCALL
big2_byteType(const ENCODING *enc, const char *p) {
  return BIG2_BYTE_TYPE(enc, p);
}

static int PTRFASTCALL
big2_byteToAscii(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return BIG2_BYTE_TO_ASCII(p);
}

static int PTRCALL
big2_charMatches(const ENCODING *enc, const char *p, int c) {
  UNUSED_P(enc);
  return BIG2_CHAR_MATCHES(p, c);
}

static int PTRFASTCALL
big2_isNameMin(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return BIG2_IS_NAME_CHAR_MINBPC(p);
}

static int PTRFASTCALL
big2_isNmstrtMin(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return BIG2_IS_NMSTRT_CHAR_MINBPC(p);
}

#  undef VTABLE
#define VTABLE

#else /* not XML_MIN_SIZE */

#  undef PREFIX
#define PREFIX
#define MINBPC
/* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
#define BYTE_TYPE
#define BYTE_TO_ASCII
#define CHAR_MATCHES
#define IS_NAME_CHAR
#define IS_NAME_CHAR_MINBPC
#define IS_NMSTRT_CHAR
#define IS_NMSTRT_CHAR_MINBPC

#define XML_TOK_IMPL_C
#  include "xmltok_impl.c"
#  undef XML_TOK_IMPL_C

#  undef MINBPC
#  undef BYTE_TYPE
#  undef BYTE_TO_ASCII
#  undef CHAR_MATCHES
#  undef IS_NAME_CHAR
#  undef IS_NAME_CHAR_MINBPC
#  undef IS_NMSTRT_CHAR
#  undef IS_NMSTRT_CHAR_MINBPC
#  undef IS_INVALID_CHAR

#endif /* not XML_MIN_SIZE */

#ifdef XML_NS

static const struct normal_encoding big2_encoding_ns
    =;

#endif

static const struct normal_encoding big2_encoding
    =;

#if BYTEORDER != 1234

#  ifdef XML_NS

static const struct normal_encoding internal_big2_encoding_ns
    = {{VTABLE, 2, 0, 1},
       {
#    include "iasciitab.h"
#    include "latin1tab.h"
       },
       STANDARD_VTABLE(big2_) NULL_VTABLE};

#  endif

static const struct normal_encoding internal_big2_encoding
    = {{VTABLE, 2, 0, 1},
       {
#define BT_COLON
#  include "iasciitab.h"
#  undef BT_COLON
#  include "latin1tab.h"
       },
       STANDARD_VTABLE(big2_) NULL_VTABLE};

#endif

#undef PREFIX

static int FASTCALL
streqci(const char *s1, const char *s2) {}

static void PTRCALL
initUpdatePosition(const ENCODING *enc, const char *ptr, const char *end,
                   POSITION *pos) {}

static int
toAscii(const ENCODING *enc, const char *ptr, const char *end) {}

static int FASTCALL
isSpace(int c) {}

/* Return 1 if there's just optional white space or there's an S
   followed by name=val.
*/
static int
parsePseudoAttribute(const ENCODING *enc, const char *ptr, const char *end,
                     const char **namePtr, const char **nameEndPtr,
                     const char **valPtr, const char **nextTokPtr) {}

static const char KW_version[]
    =;

static const char KW_encoding[] =;

static const char KW_standalone[]
    =;

static const char KW_yes[] =;

static const char KW_no[] =;

static int
doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *, const char *,
                                                 const char *),
               int isGeneralTextEntity, const ENCODING *enc, const char *ptr,
               const char *end, const char **badPtr, const char **versionPtr,
               const char **versionEndPtr, const char **encodingName,
               const ENCODING **encoding, int *standalone) {}

static int FASTCALL
checkCharRefNumber(int result) {}

int FASTCALL
XmlUtf8Encode(int c, char *buf) {}

int FASTCALL
XmlUtf16Encode(int charNum, unsigned short *buf) {}

struct unknown_encoding {};

#define AS_UNKNOWN_ENCODING(enc)

int
XmlSizeOfUnknownEncoding(void) {}

static int PTRFASTCALL
unknown_isName(const ENCODING *enc, const char *p) {}

static int PTRFASTCALL
unknown_isNmstrt(const ENCODING *enc, const char *p) {}

static int PTRFASTCALL
unknown_isInvalid(const ENCODING *enc, const char *p) {}

static enum XML_Convert_Result PTRCALL
unknown_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
               char **toP, const char *toLim) {}

static enum XML_Convert_Result PTRCALL
unknown_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
                unsigned short **toP, const unsigned short *toLim) {}

ENCODING *
XmlInitUnknownEncoding(void *mem, int *table, CONVERTER convert,
                       void *userData) {}

/* If this enumeration is changed, getEncodingIndex and encodings
must also be changed. */
enum {};

static const char KW_ISO_8859_1[]
    =;
static const char KW_US_ASCII[]
    =;
static const char KW_UTF_8[]
    =;
static const char KW_UTF_16[]
    =;
static const char KW_UTF_16BE[]
    =;
static const char KW_UTF_16LE[]
    =;

static int FASTCALL
getEncodingIndex(const char *name) {}

/* For binary compatibility, we store the index of the encoding
   specified at initialization in the isUtf16 member.
*/

#define INIT_ENC_INDEX(enc)
#define SET_INIT_ENC_INDEX(enc, i)

/* This is what detects the encoding.  encodingTable maps from
   encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
   the external (protocol) specified encoding; state is
   XML_CONTENT_STATE if we're parsing an external text entity, and
   XML_PROLOG_STATE otherwise.
*/

static int
initScan(const ENCODING *const *encodingTable, const INIT_ENCODING *enc,
         int state, const char *ptr, const char *end, const char **nextTokPtr) {}

#define NS
#define ns
#define XML_TOK_NS_C
#include "xmltok_ns.c"
#undef XML_TOK_NS_C
#undef NS
#undef ns

#ifdef XML_NS

#define NS
#define ns

#define XML_TOK_NS_C
#  include "xmltok_ns.c"
#  undef XML_TOK_NS_C

#  undef NS
#  undef ns

ENCODING *
XmlInitUnknownEncodingNS(void *mem, int *table, CONVERTER convert,
                         void *userData) {}

#endif /* XML_NS */