Home | History | Annotate | Line # | Download | only in lib
      1 /* internal.h
      2 
      3    Internal definitions used by Expat.  This is not needed to compile
      4    client code.
      5 
      6    The following calling convention macros are defined for frequently
      7    called functions:
      8 
      9    FASTCALL    - Used for those internal functions that have a simple
     10                  body and a low number of arguments and local variables.
     11 
     12    PTRCALL     - Used for functions called though function pointers.
     13 
     14    PTRFASTCALL - Like PTRCALL, but for low number of arguments.
     15 
     16    inline      - Used for selected internal functions for which inlining
     17                  may improve performance on some platforms.
     18 
     19    Note: Use of these macros is based on judgement, not hard rules,
     20          and therefore subject to change.
     21                             __  __            _
     22                          ___\ \/ /_ __   __ _| |_
     23                         / _ \\  /| '_ \ / _` | __|
     24                        |  __//  \| |_) | (_| | |_
     25                         \___/_/\_\ .__/ \__,_|\__|
     26                                  |_| XML parser
     27 
     28    Copyright (c) 2002-2003 Fred L. Drake, Jr. <fdrake (at) users.sourceforge.net>
     29    Copyright (c) 2002-2006 Karl Waclawek <karl (at) waclawek.net>
     30    Copyright (c) 2003      Greg Stein <gstein (at) users.sourceforge.net>
     31    Copyright (c) 2016-2026 Sebastian Pipping <sebastian (at) pipping.org>
     32    Copyright (c) 2018      Yury Gribov <tetra2005 (at) gmail.com>
     33    Copyright (c) 2019      David Loffredo <loffredo (at) steptools.com>
     34    Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <snild (at) sony.com>
     35    Copyright (c) 2024      Taichi Haradaguchi <20001722 (at) ymail.ne.jp>
     36    Licensed under the MIT license:
     37 
     38    Permission is  hereby granted,  free of charge,  to any  person obtaining
     39    a  copy  of  this  software   and  associated  documentation  files  (the
     40    "Software"),  to  deal in  the  Software  without restriction,  including
     41    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
     42    distribute, sublicense, and/or sell copies of the Software, and to permit
     43    persons  to whom  the Software  is  furnished to  do so,  subject to  the
     44    following conditions:
     45 
     46    The above copyright  notice and this permission notice  shall be included
     47    in all copies or substantial portions of the Software.
     48 
     49    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
     50    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
     51    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
     52    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
     53    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
     54    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     55    USE OR OTHER DEALINGS IN THE SOFTWARE.
     56 */
     57 
     58 #if defined(__GNUC__) && defined(__i386__) && ! defined(__MINGW32__)
     59 /* We'll use this version by default only where we know it helps.
     60 
     61    regparm() generates warnings on Solaris boxes.   See SF bug #692878.
     62 
     63    Instability reported with egcs on a RedHat Linux 7.3.
     64    Let's comment out:
     65    #define FASTCALL __attribute__((stdcall, regparm(3)))
     66    and let's try this:
     67 */
     68 #  define FASTCALL __attribute__((regparm(3)))
     69 #  define PTRFASTCALL __attribute__((regparm(3)))
     70 #endif
     71 
     72 /* Using __fastcall seems to have an unexpected negative effect under
     73    MS VC++, especially for function pointers, so we won't use it for
     74    now on that platform. It may be reconsidered for a future release
     75    if it can be made more effective.
     76    Likely reason: __fastcall on Windows is like stdcall, therefore
     77    the compiler cannot perform stack optimizations for call clusters.
     78 */
     79 
     80 /* Make sure all of these are defined if they aren't already. */
     81 
     82 #ifndef FASTCALL
     83 #  define FASTCALL
     84 #endif
     85 
     86 #ifndef PTRCALL
     87 #  define PTRCALL
     88 #endif
     89 
     90 #ifndef PTRFASTCALL
     91 #  define PTRFASTCALL
     92 #endif
     93 
     94 #ifndef XML_MIN_SIZE
     95 #  if ! defined(__cplusplus) && ! defined(inline)
     96 #    ifdef __GNUC__
     97 #      define inline __inline
     98 #    endif /* __GNUC__ */
     99 #  endif
    100 #endif /* XML_MIN_SIZE */
    101 
    102 #ifdef __cplusplus
    103 #  define inline inline
    104 #else
    105 #  ifndef inline
    106 #    define inline
    107 #  endif
    108 #endif
    109 
    110 #include <limits.h> // ULONG_MAX
    111 #include <stddef.h> // size_t
    112 
    113 #if defined(_WIN32)                                                            \
    114     && (! defined(__USE_MINGW_ANSI_STDIO)                                      \
    115         || (1 - __USE_MINGW_ANSI_STDIO - 1 == 0))
    116 #  define EXPAT_FMT_LLX(midpart) "%" midpart "I64x"
    117 #  define EXPAT_FMT_ULL(midpart) "%" midpart "I64u"
    118 #  if defined(_WIN64) // Note: modifiers "td" and "zu" do not work for MinGW
    119 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "I64d"
    120 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "I64u"
    121 #  else
    122 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "d"
    123 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "u"
    124 #  endif
    125 #else
    126 #  define EXPAT_FMT_LLX(midpart) "%" midpart "llx"
    127 #  define EXPAT_FMT_ULL(midpart) "%" midpart "llu"
    128 #  if ! defined(ULONG_MAX)
    129 #    error Compiler did not define ULONG_MAX for us
    130 #  elif ULONG_MAX == 18446744073709551615u // 2^64-1
    131 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "ld"
    132 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "lu"
    133 #  elif defined(__wasm32__) // 32bit mode Emscripten or WASI SDK
    134 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "ld"
    135 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "zu"
    136 #  else
    137 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "d"
    138 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "u"
    139 #  endif
    140 #endif
    141 
    142 #ifndef UNUSED_P
    143 #  define UNUSED_P(p) (void)p
    144 #endif
    145 
    146 /* NOTE BEGIN If you ever patch these defaults to greater values
    147               for non-attack XML payload in your environment,
    148               please file a bug report with libexpat.  Thank you!
    149 */
    150 #define EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_MAXIMUM_AMPLIFICATION_DEFAULT   \
    151   100.0f
    152 #define EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_ACTIVATION_THRESHOLD_DEFAULT    \
    153   8388608 // 8 MiB, 2^23
    154 
    155 #define EXPAT_ALLOC_TRACKER_MAXIMUM_AMPLIFICATION_DEFAULT 100.0f
    156 #define EXPAT_ALLOC_TRACKER_ACTIVATION_THRESHOLD_DEFAULT                       \
    157   67108864 // 64 MiB, 2^26
    158 
    159 // NOTE: If function expat_alloc was user facing, EXPAT_MALLOC_ALIGNMENT would
    160 //       have to take sizeof(long double) into account
    161 #define EXPAT_MALLOC_ALIGNMENT sizeof(long long) // largest parser (sub)member
    162 #define EXPAT_MALLOC_PADDING ((EXPAT_MALLOC_ALIGNMENT) - sizeof(size_t))
    163 
    164 /* NOTE END */
    165 
    166 #include "expat.h" // so we can use type XML_Parser below
    167 
    168 #ifdef __cplusplus
    169 extern "C" {
    170 #endif
    171 
    172 void _INTERNAL_trim_to_complete_utf8_characters(const char *from,
    173                                                 const char **fromLimRef);
    174 
    175 #if defined(XML_GE) && XML_GE == 1
    176 unsigned long long testingAccountingGetCountBytesDirect(XML_Parser parser);
    177 unsigned long long testingAccountingGetCountBytesIndirect(XML_Parser parser);
    178 const char *unsignedCharToPrintable(unsigned char c);
    179 #endif
    180 
    181 extern
    182 #if ! defined(XML_TESTING)
    183     const
    184 #endif
    185     XML_Bool g_reparseDeferralEnabledDefault; // written ONLY in runtests.c
    186 #if defined(XML_TESTING)
    187 void *expat_malloc(XML_Parser parser, size_t size, int sourceLine);
    188 void expat_free(XML_Parser parser, void *ptr, int sourceLine);
    189 void *expat_realloc(XML_Parser parser, void *ptr, size_t size, int sourceLine);
    190 extern unsigned int g_bytesScanned; // used for testing only
    191 #endif
    192 
    193 #ifdef __cplusplus
    194 }
    195 #endif
    196