xref: /freebsd/contrib/expat/lib/internal.h (revision dece5b05f577fc4c6b89af7ad48dfbdf7cc4945c)
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    inline      - Used for selected internal functions for which inlining
10                  may improve performance on some platforms.
11 
12    Note: Use of these macros is based on judgement, not hard rules,
13          and therefore subject to change.
14                             __  __            _
15                          ___\ \/ /_ __   __ _| |_
16                         / _ \\  /| '_ \ / _` | __|
17                        |  __//  \| |_) | (_| | |_
18                         \___/_/\_\ .__/ \__,_|\__|
19                                  |_| XML parser
20 
21    Copyright (c) 2002-2003 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
22    Copyright (c) 2002-2006 Karl Waclawek <karl@waclawek.net>
23    Copyright (c) 2003      Greg Stein <gstein@users.sourceforge.net>
24    Copyright (c) 2016-2026 Sebastian Pipping <sebastian@pipping.org>
25    Copyright (c) 2018      Yury Gribov <tetra2005@gmail.com>
26    Copyright (c) 2019      David Loffredo <loffredo@steptools.com>
27    Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <snild@sony.com>
28    Copyright (c) 2024      Taichi Haradaguchi <20001722@ymail.ne.jp>
29    Copyright (c) 2026      Matthew Wozniczka <mattheww@simba.com>
30    Copyright (c) 2026      Braian Plaku <braianplaku@gmail.com>
31    Copyright (c) 2026      Florian Schmaus <florian.schmaus@codasip.com>
32    Licensed under the MIT license:
33 
34    Permission is  hereby granted,  free of charge,  to any  person obtaining
35    a  copy  of  this  software   and  associated  documentation  files  (the
36    "Software"),  to  deal in  the  Software  without restriction,  including
37    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
38    distribute, sublicense, and/or sell copies of the Software, and to permit
39    persons  to whom  the Software  is  furnished to  do so,  subject to  the
40    following conditions:
41 
42    The above copyright  notice and this permission notice  shall be included
43    in all copies or substantial portions of the Software.
44 
45    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
46    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
47    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
48    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
49    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
50    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
51    USE OR OTHER DEALINGS IN THE SOFTWARE.
52 
53    SPDX-License-Identifier: MIT
54 */
55 
56 #if ! defined(INTERNAL_H)
57 #  define INTERNAL_H 1
58 
59 #  if ! defined(inline)
60 #    ifdef __GNUC__
61 #      define inline __inline
62 #    endif /* __GNUC__ */
63 #  endif
64 
65 #  ifndef inline
66 #    define inline
67 #  endif
68 
69 #  if ! defined(XML_NONTESTING_STATIC)
70 #    if defined(XML_TESTING)
71 #      define XML_NONTESTING_STATIC // empty i.e. not static
72 #    else
73 #      define XML_NONTESTING_STATIC static
74 #    endif
75 #  endif
76 
77 #  include <limits.h> // ULONG_MAX
78 #  include <stddef.h> // size_t
79 
80 #  if defined(_WIN32)                                                          \
81       && (! defined(__USE_MINGW_ANSI_STDIO)                                    \
82           || (1 - __USE_MINGW_ANSI_STDIO - 1 == 0))
83 #    define EXPAT_FMT_LLX(midpart) "%" midpart "I64x"
84 #    define EXPAT_FMT_ULL(midpart) "%" midpart "I64u"
85 #    if defined(_WIN64) // Note: modifiers "td" and "zu" do not work for MinGW
86 #      define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "I64d"
87 #      define EXPAT_FMT_SIZE_T(midpart) "%" midpart "I64u"
88 #    else
89 #      define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart "d"
90 #      define EXPAT_FMT_SIZE_T(midpart) "%" midpart "u"
91 #    endif
92 #  else
93 #    include <inttypes.h> // PRIdPTR, PRIuPTR
94 #    define EXPAT_FMT_LLX(midpart) "%" midpart "llx"
95 #    define EXPAT_FMT_ULL(midpart) "%" midpart "llu"
96 #    define EXPAT_FMT_PTRDIFF_T(midpart) "%" midpart PRIdPTR
97 #    define EXPAT_FMT_SIZE_T(midpart) "%" midpart PRIuPTR
98 #  endif
99 
100 #  ifndef UNUSED_P
101 #    define UNUSED_P(p) (void)p
102 #  endif
103 
104 /* NOTE BEGIN If you ever patch these defaults to greater values
105               for non-attack XML payload in your environment,
106               please file a bug report with libexpat.  Thank you!
107 */
108 #  define EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_MAXIMUM_AMPLIFICATION_DEFAULT \
109     100.0f
110 #  define EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_ACTIVATION_THRESHOLD_DEFAULT  \
111     8388608 // 8 MiB, 2^23
112 
113 #  define EXPAT_ALLOC_TRACKER_MAXIMUM_AMPLIFICATION_DEFAULT 100.0f
114 #  define EXPAT_ALLOC_TRACKER_ACTIVATION_THRESHOLD_DEFAULT                     \
115     67108864 // 64 MiB, 2^26
116 
117 // NOTE: If function expat_alloc was user facing, EXPAT_MALLOC_ALIGNMENT would
118 //       have to take sizeof(long double) into account
119 union expat_align {
120   long long l;
121   void *p;
122 };
123 #  define EXPAT_MALLOC_ALIGNMENT sizeof(union expat_align)
124 #  define EXPAT_MALLOC_PADDING ((EXPAT_MALLOC_ALIGNMENT) - sizeof(size_t))
125 
126 /* NOTE END */
127 
128 #  include "expat.h" // so we can use type XML_Parser below
129 
130 void _INTERNAL_trim_to_complete_utf8_characters(const char *from,
131                                                 const char **fromLimRef);
132 
133 #  if defined(XML_GE) && XML_GE == 1
134 unsigned long long testingAccountingGetCountBytesDirect(XML_Parser parser);
135 unsigned long long testingAccountingGetCountBytesIndirect(XML_Parser parser);
136 const char *unsignedCharToPrintable(unsigned char c);
137 #  endif
138 
139 extern
140 #  if ! defined(XML_TESTING)
141     const
142 #  endif
143     XML_Bool g_reparseDeferralEnabledDefault; // written ONLY in runtests.c
144 #  if defined(XML_TESTING)
145 
146 int xmlSetHashSalt(XML_Parser parser, unsigned long hash_salt);
147 
148 void *expat_malloc(XML_Parser parser, size_t size, int sourceLine);
149 void expat_free(XML_Parser parser, void *ptr, int sourceLine);
150 void *expat_realloc(XML_Parser parser, void *ptr, size_t size, int sourceLine);
151 extern unsigned int g_bytesScanned; // used for testing only
152 #  endif
153 
154 #endif // not defined INTERNAL_H
155