xref: /openbsd-src/lib/libexpat/lib/internal.h (revision c033f77099126ed63d6da1f62993cd1af88e58ab)
11a3ddf8cSespie /* internal.h
21a3ddf8cSespie 
31a3ddf8cSespie    Internal definitions used by Expat.  This is not needed to compile
41a3ddf8cSespie    client code.
51a3ddf8cSespie 
61a3ddf8cSespie    The following calling convention macros are defined for frequently
71a3ddf8cSespie    called functions:
81a3ddf8cSespie 
91a3ddf8cSespie    FASTCALL    - Used for those internal functions that have a simple
101a3ddf8cSespie                  body and a low number of arguments and local variables.
111a3ddf8cSespie 
121a3ddf8cSespie    PTRCALL     - Used for functions called though function pointers.
131a3ddf8cSespie 
141a3ddf8cSespie    PTRFASTCALL - Like PTRCALL, but for low number of arguments.
151a3ddf8cSespie 
161a3ddf8cSespie    inline      - Used for selected internal functions for which inlining
171a3ddf8cSespie                  may improve performance on some platforms.
181a3ddf8cSespie 
191a3ddf8cSespie    Note: Use of these macros is based on judgement, not hard rules,
201a3ddf8cSespie          and therefore subject to change.
212e724bc9Sbluhm                             __  __            _
222e724bc9Sbluhm                          ___\ \/ /_ __   __ _| |_
232e724bc9Sbluhm                         / _ \\  /| '_ \ / _` | __|
242e724bc9Sbluhm                        |  __//  \| |_) | (_| | |_
252e724bc9Sbluhm                         \___/_/\_\ .__/ \__,_|\__|
262e724bc9Sbluhm                                  |_| XML parser
272e724bc9Sbluhm 
2808819b41Sbluhm    Copyright (c) 2002-2003 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
2908819b41Sbluhm    Copyright (c) 2002-2006 Karl Waclawek <karl@waclawek.net>
3008819b41Sbluhm    Copyright (c) 2003      Greg Stein <gstein@users.sourceforge.net>
31*c033f770Sbluhm    Copyright (c) 2016-2024 Sebastian Pipping <sebastian@pipping.org>
3208819b41Sbluhm    Copyright (c) 2018      Yury Gribov <tetra2005@gmail.com>
3308819b41Sbluhm    Copyright (c) 2019      David Loffredo <loffredo@steptools.com>
34*c033f770Sbluhm    Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <snild@sony.com>
35*c033f770Sbluhm    Copyright (c) 2024      Taichi Haradaguchi <20001722@ymail.ne.jp>
362e724bc9Sbluhm    Licensed under the MIT license:
372e724bc9Sbluhm 
382e724bc9Sbluhm    Permission is  hereby granted,  free of charge,  to any  person obtaining
392e724bc9Sbluhm    a  copy  of  this  software   and  associated  documentation  files  (the
402e724bc9Sbluhm    "Software"),  to  deal in  the  Software  without restriction,  including
412e724bc9Sbluhm    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
422e724bc9Sbluhm    distribute, sublicense, and/or sell copies of the Software, and to permit
432e724bc9Sbluhm    persons  to whom  the Software  is  furnished to  do so,  subject to  the
442e724bc9Sbluhm    following conditions:
452e724bc9Sbluhm 
462e724bc9Sbluhm    The above copyright  notice and this permission notice  shall be included
472e724bc9Sbluhm    in all copies or substantial portions of the Software.
482e724bc9Sbluhm 
492e724bc9Sbluhm    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
502e724bc9Sbluhm    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
512e724bc9Sbluhm    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
522e724bc9Sbluhm    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
532e724bc9Sbluhm    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
542e724bc9Sbluhm    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
552e724bc9Sbluhm    USE OR OTHER DEALINGS IN THE SOFTWARE.
561a3ddf8cSespie */
571a3ddf8cSespie 
582feb5d2aSbluhm #if defined(__GNUC__) && defined(__i386__) && ! defined(__MINGW32__)
597d36914fSalek /* We'll use this version by default only where we know it helps.
607d36914fSalek 
617d36914fSalek    regparm() generates warnings on Solaris boxes.   See SF bug #692878.
627d36914fSalek 
637d36914fSalek    Instability reported with egcs on a RedHat Linux 7.3.
647d36914fSalek    Let's comment out:
651a3ddf8cSespie    #define FASTCALL __attribute__((stdcall, regparm(3)))
661a3ddf8cSespie    and let's try this:
671a3ddf8cSespie */
681a3ddf8cSespie #  define FASTCALL __attribute__((regparm(3)))
691a3ddf8cSespie #  define PTRFASTCALL __attribute__((regparm(3)))
707d36914fSalek #endif
711a3ddf8cSespie 
721a3ddf8cSespie /* Using __fastcall seems to have an unexpected negative effect under
731a3ddf8cSespie    MS VC++, especially for function pointers, so we won't use it for
741a3ddf8cSespie    now on that platform. It may be reconsidered for a future release
751a3ddf8cSespie    if it can be made more effective.
761a3ddf8cSespie    Likely reason: __fastcall on Windows is like stdcall, therefore
771a3ddf8cSespie    the compiler cannot perform stack optimizations for call clusters.
781a3ddf8cSespie */
791a3ddf8cSespie 
807d36914fSalek /* Make sure all of these are defined if they aren't already. */
811a3ddf8cSespie 
821a3ddf8cSespie #ifndef FASTCALL
831a3ddf8cSespie #  define FASTCALL
841a3ddf8cSespie #endif
851a3ddf8cSespie 
861a3ddf8cSespie #ifndef PTRCALL
871a3ddf8cSespie #  define PTRCALL
881a3ddf8cSespie #endif
891a3ddf8cSespie 
901a3ddf8cSespie #ifndef PTRFASTCALL
911a3ddf8cSespie #  define PTRFASTCALL
921a3ddf8cSespie #endif
931a3ddf8cSespie 
941a3ddf8cSespie #ifndef XML_MIN_SIZE
951a3ddf8cSespie #  if ! defined(__cplusplus) && ! defined(inline)
961a3ddf8cSespie #    ifdef __GNUC__
971a3ddf8cSespie #      define inline __inline
981a3ddf8cSespie #    endif /* __GNUC__ */
991a3ddf8cSespie #  endif
1001a3ddf8cSespie #endif /* XML_MIN_SIZE */
1011a3ddf8cSespie 
1021a3ddf8cSespie #ifdef __cplusplus
1031a3ddf8cSespie #  define inline inline
1041a3ddf8cSespie #else
1051a3ddf8cSespie #  ifndef inline
1061a3ddf8cSespie #    define inline
1071a3ddf8cSespie #  endif
1081a3ddf8cSespie #endif
1092feb5d2aSbluhm 
11008819b41Sbluhm #include <limits.h> // ULONG_MAX
11108819b41Sbluhm 
1129029d806Sbluhm #if defined(_WIN32)                                                            \
1139029d806Sbluhm     && (! defined(__USE_MINGW_ANSI_STDIO)                                      \
1149029d806Sbluhm         || (1 - __USE_MINGW_ANSI_STDIO - 1 == 0))
11508819b41Sbluhm #  define EXPAT_FMT_ULL(midpart) "%" midpart "I64u"
11608819b41Sbluhm #  if defined(_WIN64) // Note: modifiers "td" and "zu" do not work for MinGW
11708819b41Sbluhm #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "I64d"
11808819b41Sbluhm #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "I64u"
11908819b41Sbluhm #  else
12008819b41Sbluhm #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "d"
12108819b41Sbluhm #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "u"
12208819b41Sbluhm #  endif
12308819b41Sbluhm #else
12408819b41Sbluhm #  define EXPAT_FMT_ULL(midpart) "%" midpart "llu"
12508819b41Sbluhm #  if ! defined(ULONG_MAX)
12608819b41Sbluhm #    error Compiler did not define ULONG_MAX for us
12708819b41Sbluhm #  elif ULONG_MAX == 18446744073709551615u // 2^64-1
12808819b41Sbluhm #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "ld"
12908819b41Sbluhm #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "lu"
13008819b41Sbluhm #  else
13108819b41Sbluhm #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "d"
13208819b41Sbluhm #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "u"
13308819b41Sbluhm #  endif
13408819b41Sbluhm #endif
13508819b41Sbluhm 
1362feb5d2aSbluhm #ifndef UNUSED_P
13728ce3119Sbluhm #  define UNUSED_P(p) (void)p
1382feb5d2aSbluhm #endif
1392feb5d2aSbluhm 
14008819b41Sbluhm /* NOTE BEGIN If you ever patch these defaults to greater values
14108819b41Sbluhm               for non-attack XML payload in your environment,
14208819b41Sbluhm               please file a bug report with libexpat.  Thank you!
14308819b41Sbluhm */
14408819b41Sbluhm #define EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_MAXIMUM_AMPLIFICATION_DEFAULT   \
14508819b41Sbluhm   100.0f
14608819b41Sbluhm #define EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_ACTIVATION_THRESHOLD_DEFAULT    \
14708819b41Sbluhm   8388608 // 8 MiB, 2^23
14808819b41Sbluhm /* NOTE END */
14908819b41Sbluhm 
15008819b41Sbluhm #include "expat.h" // so we can use type XML_Parser below
15108819b41Sbluhm 
1522feb5d2aSbluhm #ifdef __cplusplus
1532feb5d2aSbluhm extern "C" {
1542feb5d2aSbluhm #endif
1552feb5d2aSbluhm 
15608819b41Sbluhm void _INTERNAL_trim_to_complete_utf8_characters(const char *from,
15728ce3119Sbluhm                                                 const char **fromLimRef);
1582feb5d2aSbluhm 
159*c033f770Sbluhm #if defined(XML_GE) && XML_GE == 1
16008819b41Sbluhm unsigned long long testingAccountingGetCountBytesDirect(XML_Parser parser);
16108819b41Sbluhm unsigned long long testingAccountingGetCountBytesIndirect(XML_Parser parser);
16208819b41Sbluhm const char *unsignedCharToPrintable(unsigned char c);
16308819b41Sbluhm #endif
16408819b41Sbluhm 
165*c033f770Sbluhm extern
166*c033f770Sbluhm #if ! defined(XML_TESTING)
167*c033f770Sbluhm     const
168*c033f770Sbluhm #endif
169*c033f770Sbluhm     XML_Bool g_reparseDeferralEnabledDefault; // written ONLY in runtests.c
170*c033f770Sbluhm #if defined(XML_TESTING)
171*c033f770Sbluhm extern unsigned int g_bytesScanned; // used for testing only
172*c033f770Sbluhm #endif
173bd8f1dc3Sbluhm 
1742feb5d2aSbluhm #ifdef __cplusplus
1752feb5d2aSbluhm }
1762feb5d2aSbluhm #endif
177