1/* Commonly used functions for the Expat test suite
2                            __  __            _
3                         ___\ \/ /_ __   __ _| |_
4                        / _ \\  /| '_ \ / _` | __|
5                       |  __//  \| |_) | (_| | |_
6                        \___/_/\_\ .__/ \__,_|\__|
7                                 |_| XML parser
8
9   Copyright (c) 2001-2006 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
10   Copyright (c) 2003      Greg Stein <gstein@users.sourceforge.net>
11   Copyright (c) 2005-2007 Steven Solie <steven@solie.ca>
12   Copyright (c) 2005-2012 Karl Waclawek <karl@waclawek.net>
13   Copyright (c) 2016-2023 Sebastian Pipping <sebastian@pipping.org>
14   Copyright (c) 2017-2022 Rhodri James <rhodri@wildebeest.org.uk>
15   Copyright (c) 2017      Joe Orton <jorton@redhat.com>
16   Copyright (c) 2017      Jos�� Guti��rrez de la Concha <jose@zeroc.com>
17   Copyright (c) 2018      Marco Maggi <marco.maggi-ipsu@poste.it>
18   Copyright (c) 2019      David Loffredo <loffredo@steptools.com>
19   Copyright (c) 2020      Tim Gates <tim.gates@iress.com>
20   Copyright (c) 2021      Donghee Na <donghee.na@python.org>
21   Copyright (c) 2023      Sony Corporation / Snild Dolkow <snild@sony.com>
22   Licensed under the MIT license:
23
24   Permission is  hereby granted,  free of charge,  to any  person obtaining
25   a  copy  of  this  software   and  associated  documentation  files  (the
26   "Software"),  to  deal in  the  Software  without restriction,  including
27   without  limitation the  rights  to use,  copy,  modify, merge,  publish,
28   distribute, sublicense, and/or sell copies of the Software, and to permit
29   persons  to whom  the Software  is  furnished to  do so,  subject to  the
30   following conditions:
31
32   The above copyright  notice and this permission notice  shall be included
33   in all copies or substantial portions of the Software.
34
35   THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
36   EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
37   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
38   NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
39   DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
40   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
41   USE OR OTHER DEALINGS IN THE SOFTWARE.
42*/
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48#ifndef XML_COMMON_H
49#  define XML_COMMON_H
50
51#  include "expat_config.h"
52#  include "minicheck.h"
53#  include "chardata.h"
54
55#  ifdef XML_LARGE_SIZE
56#    define XML_FMT_INT_MOD "ll"
57#  else
58#    define XML_FMT_INT_MOD "l"
59#  endif
60
61#  ifdef XML_UNICODE_WCHAR_T
62#    define XML_FMT_STR "ls"
63#    include <wchar.h>
64#    define xcstrlen(s) wcslen(s)
65#    define xcstrcmp(s, t) wcscmp((s), (t))
66#    define xcstrncmp(s, t, n) wcsncmp((s), (t), (n))
67#    define XCS(s) _XCS(s)
68#    define _XCS(s) L##s
69#  else
70#    ifdef XML_UNICODE
71#      error "No support for UTF-16 character without wchar_t in tests"
72#    else
73#      define XML_FMT_STR "s"
74#      define xcstrlen(s) strlen(s)
75#      define xcstrcmp(s, t) strcmp((s), (t))
76#      define xcstrncmp(s, t, n) strncmp((s), (t), (n))
77#      define XCS(s) s
78#    endif /* XML_UNICODE */
79#  endif   /* XML_UNICODE_WCHAR_T */
80
81extern XML_Parser g_parser;
82
83extern XML_Bool g_resumable;
84extern XML_Bool g_abortable;
85
86extern int g_chunkSize;
87
88extern const char *long_character_data_text;
89extern const char *long_cdata_text;
90extern const char *get_buffer_test_text;
91
92extern void tcase_add_test__ifdef_xml_dtd(TCase *tc, tcase_test_function test);
93extern void tcase_add_test__if_xml_ge(TCase *tc, tcase_test_function test);
94
95extern void basic_teardown(void);
96
97extern void _xml_failure(XML_Parser parser, const char *file, int line);
98
99#  define xml_failure(parser) _xml_failure((parser), __FILE__, __LINE__)
100
101extern enum XML_Status _XML_Parse_SINGLE_BYTES(XML_Parser parser, const char *s,
102                                               int len, int isFinal);
103
104extern void _expect_failure(const char *text, enum XML_Error errorCode,
105                            const char *errorMessage, const char *file,
106                            int lineno);
107
108#  define expect_failure(text, errorCode, errorMessage)                        \
109    _expect_failure((text), (errorCode), (errorMessage), __FILE__, __LINE__)
110
111/* Support functions for handlers to collect up character and attribute data.
112 */
113
114extern void XMLCALL accumulate_characters(void *userData, const XML_Char *s,
115                                          int len);
116
117extern void XMLCALL accumulate_attribute(void *userData, const XML_Char *name,
118                                         const XML_Char **atts);
119
120extern void _run_character_check(const char *text, const XML_Char *expected,
121                                 const char *file, int line);
122
123#  define run_character_check(text, expected)                                  \
124    _run_character_check(text, expected, __FILE__, __LINE__)
125
126extern void _run_attribute_check(const char *text, const XML_Char *expected,
127                                 const char *file, int line);
128
129#  define run_attribute_check(text, expected)                                  \
130    _run_attribute_check(text, expected, __FILE__, __LINE__)
131
132typedef struct ExtTest {
133  const char *parse_text;
134  const XML_Char *encoding;
135  CharData *storage;
136} ExtTest;
137
138extern void XMLCALL ext_accumulate_characters(void *userData, const XML_Char *s,
139                                              int len);
140
141extern void _run_ext_character_check(const char *text, ExtTest *test_data,
142                                     const XML_Char *expected, const char *file,
143                                     int line);
144
145#  define run_ext_character_check(text, test_data, expected)                   \
146    _run_ext_character_check(text, test_data, expected, __FILE__, __LINE__)
147
148#  define ALLOC_ALWAYS_SUCCEED (-1)
149#  define REALLOC_ALWAYS_SUCCEED (-1)
150
151extern int g_allocation_count;
152extern int g_reallocation_count;
153
154extern void *duff_allocator(size_t size);
155
156extern void *duff_reallocator(void *ptr, size_t size);
157
158#endif /* XML_COMMON_H */
159
160#ifdef __cplusplus
161}
162#endif
163