xref: /freebsd/contrib/expat/lib/internal.h (revision 783d3ff6)
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@users.sourceforge.net>
29    Copyright (c) 2002-2006 Karl Waclawek <karl@waclawek.net>
30    Copyright (c) 2003      Greg Stein <gstein@users.sourceforge.net>
31    Copyright (c) 2016-2023 Sebastian Pipping <sebastian@pipping.org>
32    Copyright (c) 2018      Yury Gribov <tetra2005@gmail.com>
33    Copyright (c) 2019      David Loffredo <loffredo@steptools.com>
34    Copyright (c) 2023      Sony Corporation / Snild Dolkow <snild@sony.com>
35    Licensed under the MIT license:
36 
37    Permission is  hereby granted,  free of charge,  to any  person obtaining
38    a  copy  of  this  software   and  associated  documentation  files  (the
39    "Software"),  to  deal in  the  Software  without restriction,  including
40    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
41    distribute, sublicense, and/or sell copies of the Software, and to permit
42    persons  to whom  the Software  is  furnished to  do so,  subject to  the
43    following conditions:
44 
45    The above copyright  notice and this permission notice  shall be included
46    in all copies or substantial portions of the Software.
47 
48    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
49    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
50    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
51    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
52    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
53    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
54    USE OR OTHER DEALINGS IN THE SOFTWARE.
55 */
56 
57 #if defined(__GNUC__) && defined(__i386__) && ! defined(__MINGW32__)
58 /* We'll use this version by default only where we know it helps.
59 
60    regparm() generates warnings on Solaris boxes.   See SF bug #692878.
61 
62    Instability reported with egcs on a RedHat Linux 7.3.
63    Let's comment out:
64    #define FASTCALL __attribute__((stdcall, regparm(3)))
65    and let's try this:
66 */
67 #  define FASTCALL __attribute__((regparm(3)))
68 #  define PTRFASTCALL __attribute__((regparm(3)))
69 #endif
70 
71 /* Using __fastcall seems to have an unexpected negative effect under
72    MS VC++, especially for function pointers, so we won't use it for
73    now on that platform. It may be reconsidered for a future release
74    if it can be made more effective.
75    Likely reason: __fastcall on Windows is like stdcall, therefore
76    the compiler cannot perform stack optimizations for call clusters.
77 */
78 
79 /* Make sure all of these are defined if they aren't already. */
80 
81 #ifndef FASTCALL
82 #  define FASTCALL
83 #endif
84 
85 #ifndef PTRCALL
86 #  define PTRCALL
87 #endif
88 
89 #ifndef PTRFASTCALL
90 #  define PTRFASTCALL
91 #endif
92 
93 #ifndef XML_MIN_SIZE
94 #  if ! defined(__cplusplus) && ! defined(inline)
95 #    ifdef __GNUC__
96 #      define inline __inline
97 #    endif /* __GNUC__ */
98 #  endif
99 #endif /* XML_MIN_SIZE */
100 
101 #ifdef __cplusplus
102 #  define inline inline
103 #else
104 #  ifndef inline
105 #    define inline
106 #  endif
107 #endif
108 
109 #include <limits.h> // ULONG_MAX
110 
111 #if defined(_WIN32)                                                            \
112     && (! defined(__USE_MINGW_ANSI_STDIO)                                      \
113         || (1 - __USE_MINGW_ANSI_STDIO - 1 == 0))
114 #  define EXPAT_FMT_ULL(midpart) "%" midpart "I64u"
115 #  if defined(_WIN64) // Note: modifiers "td" and "zu" do not work for MinGW
116 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "I64d"
117 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "I64u"
118 #  else
119 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "d"
120 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "u"
121 #  endif
122 #else
123 #  define EXPAT_FMT_ULL(midpart) "%" midpart "llu"
124 #  if ! defined(ULONG_MAX)
125 #    error Compiler did not define ULONG_MAX for us
126 #  elif ULONG_MAX == 18446744073709551615u // 2^64-1
127 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "ld"
128 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "lu"
129 #  else
130 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "d"
131 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart "u"
132 #  endif
133 #endif
134 
135 #ifndef UNUSED_P
136 #  define UNUSED_P(p) (void)p
137 #endif
138 
139 /* NOTE BEGIN If you ever patch these defaults to greater values
140               for non-attack XML payload in your environment,
141               please file a bug report with libexpat.  Thank you!
142 */
143 #define EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_MAXIMUM_AMPLIFICATION_DEFAULT   \
144   100.0f
145 #define EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_ACTIVATION_THRESHOLD_DEFAULT    \
146   8388608 // 8 MiB, 2^23
147 /* NOTE END */
148 
149 #include "expat.h" // so we can use type XML_Parser below
150 
151 #ifdef __cplusplus
152 extern "C" {
153 #endif
154 
155 void _INTERNAL_trim_to_complete_utf8_characters(const char *from,
156                                                 const char **fromLimRef);
157 
158 #if XML_GE == 1
159 unsigned long long testingAccountingGetCountBytesDirect(XML_Parser parser);
160 unsigned long long testingAccountingGetCountBytesIndirect(XML_Parser parser);
161 const char *unsignedCharToPrintable(unsigned char c);
162 #endif
163 
164 extern XML_Bool g_reparseDeferralEnabledDefault; // written ONLY in runtests.c
165 extern unsigned int g_parseAttempts;             // used for testing only
166 
167 #ifdef __cplusplus
168 }
169 #endif
170