xref: /freebsd/contrib/expat/tests/handlers.h (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /* XML handler 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-2024 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
45 extern "C" {
46 #endif
47 
48 #ifndef XML_HANDLERS_H
49 #  define XML_HANDLERS_H
50 
51 #  include "expat_config.h"
52 
53 #  include "expat.h"
54 
55 /* Variable holding the expected handler userData */
56 extern const void *g_handler_data;
57 /* Count of the number of times the comment handler has been invoked */
58 extern int g_comment_count;
59 /* Count of the number of skipped entities */
60 extern int g_skip_count;
61 /* Count of the number of times the XML declaration handler is invoked */
62 extern int g_xdecl_count;
63 
64 /* Start/End Element Handlers */
65 
66 extern void XMLCALL start_element_event_handler(void *userData,
67                                                 const XML_Char *name,
68                                                 const XML_Char **atts);
69 
70 extern void XMLCALL end_element_event_handler(void *userData,
71                                               const XML_Char *name);
72 
73 #  define STRUCT_START_TAG 0
74 #  define STRUCT_END_TAG 1
75 
76 extern void XMLCALL start_element_event_handler2(void *userData,
77                                                  const XML_Char *name,
78                                                  const XML_Char **attr);
79 
80 extern void XMLCALL end_element_event_handler2(void *userData,
81                                                const XML_Char *name);
82 
83 typedef struct attrInfo {
84   const XML_Char *name;
85   const XML_Char *value;
86 } AttrInfo;
87 
88 typedef struct elementInfo {
89   const XML_Char *name;
90   int attr_count;
91   const XML_Char *id_name;
92   AttrInfo *attributes;
93 } ElementInfo;
94 
95 extern void XMLCALL counting_start_element_handler(void *userData,
96                                                    const XML_Char *name,
97                                                    const XML_Char **atts);
98 
99 extern void XMLCALL suspending_end_handler(void *userData, const XML_Char *s);
100 
101 extern void XMLCALL start_element_suspender(void *userData,
102                                             const XML_Char *name,
103                                             const XML_Char **atts);
104 
105 extern int g_triplet_start_flag;
106 extern int g_triplet_end_flag;
107 
108 extern void XMLCALL triplet_start_checker(void *userData, const XML_Char *name,
109                                           const XML_Char **atts);
110 
111 extern void XMLCALL triplet_end_checker(void *userData, const XML_Char *name);
112 
113 extern void XMLCALL overwrite_start_checker(void *userData,
114                                             const XML_Char *name,
115                                             const XML_Char **atts);
116 
117 extern void XMLCALL overwrite_end_checker(void *userData, const XML_Char *name);
118 
119 extern void XMLCALL start_element_fail(void *userData, const XML_Char *name,
120                                        const XML_Char **atts);
121 
122 extern void XMLCALL start_ns_clearing_start_element(void *userData,
123                                                     const XML_Char *prefix,
124                                                     const XML_Char *uri);
125 
126 typedef struct {
127   XML_Parser parser;
128   int deep;
129 } DataIssue240;
130 
131 extern void XMLCALL start_element_issue_240(void *userData,
132                                             const XML_Char *name,
133                                             const XML_Char **atts);
134 
135 extern void XMLCALL end_element_issue_240(void *userData, const XML_Char *name);
136 
137 /* Text encoding handlers */
138 
139 extern int XMLCALL UnknownEncodingHandler(void *data, const XML_Char *encoding,
140                                           XML_Encoding *info);
141 
142 extern int XMLCALL UnrecognisedEncodingHandler(void *data,
143                                                const XML_Char *encoding,
144                                                XML_Encoding *info);
145 
146 extern int XMLCALL unknown_released_encoding_handler(void *data,
147                                                      const XML_Char *encoding,
148                                                      XML_Encoding *info);
149 
150 extern int XMLCALL MiscEncodingHandler(void *data, const XML_Char *encoding,
151                                        XML_Encoding *info);
152 
153 extern int XMLCALL long_encoding_handler(void *userData,
154                                          const XML_Char *encoding,
155                                          XML_Encoding *info);
156 
157 /* External Entity Handlers */
158 
159 typedef struct ExtOption {
160   const XML_Char *system_id;
161   const char *parse_text;
162 } ExtOption;
163 
164 extern int XMLCALL external_entity_optioner(XML_Parser parser,
165                                             const XML_Char *context,
166                                             const XML_Char *base,
167                                             const XML_Char *systemId,
168                                             const XML_Char *publicId);
169 
170 extern int XMLCALL external_entity_loader(XML_Parser parser,
171                                           const XML_Char *context,
172                                           const XML_Char *base,
173                                           const XML_Char *systemId,
174                                           const XML_Char *publicId);
175 
176 typedef struct ext_faults {
177   const char *parse_text;
178   const char *fail_text;
179   const XML_Char *encoding;
180   enum XML_Error error;
181 } ExtFaults;
182 
183 extern int XMLCALL external_entity_faulter(XML_Parser parser,
184                                            const XML_Char *context,
185                                            const XML_Char *base,
186                                            const XML_Char *systemId,
187                                            const XML_Char *publicId);
188 extern int XMLCALL external_entity_failer__if_not_xml_ge(
189     XML_Parser parser, const XML_Char *context, const XML_Char *base,
190     const XML_Char *systemId, const XML_Char *publicId);
191 extern int XMLCALL external_entity_null_loader(XML_Parser parser,
192                                                const XML_Char *context,
193                                                const XML_Char *base,
194                                                const XML_Char *systemId,
195                                                const XML_Char *publicId);
196 
197 extern int XMLCALL external_entity_resetter(XML_Parser parser,
198                                             const XML_Char *context,
199                                             const XML_Char *base,
200                                             const XML_Char *systemId,
201                                             const XML_Char *publicId);
202 
203 extern int XMLCALL external_entity_suspender(XML_Parser parser,
204                                              const XML_Char *context,
205                                              const XML_Char *base,
206                                              const XML_Char *systemId,
207                                              const XML_Char *publicId);
208 
209 extern int XMLCALL external_entity_suspend_xmldecl(XML_Parser parser,
210                                                    const XML_Char *context,
211                                                    const XML_Char *base,
212                                                    const XML_Char *systemId,
213                                                    const XML_Char *publicId);
214 
215 extern int XMLCALL external_entity_suspending_faulter(XML_Parser parser,
216                                                       const XML_Char *context,
217                                                       const XML_Char *base,
218                                                       const XML_Char *systemId,
219                                                       const XML_Char *publicId);
220 
221 extern int XMLCALL external_entity_cr_catcher(XML_Parser parser,
222                                               const XML_Char *context,
223                                               const XML_Char *base,
224                                               const XML_Char *systemId,
225                                               const XML_Char *publicId);
226 
227 extern int XMLCALL external_entity_bad_cr_catcher(XML_Parser parser,
228                                                   const XML_Char *context,
229                                                   const XML_Char *base,
230                                                   const XML_Char *systemId,
231                                                   const XML_Char *publicId);
232 
233 extern int XMLCALL external_entity_rsqb_catcher(XML_Parser parser,
234                                                 const XML_Char *context,
235                                                 const XML_Char *base,
236                                                 const XML_Char *systemId,
237                                                 const XML_Char *publicId);
238 
239 extern int XMLCALL external_entity_good_cdata_ascii(XML_Parser parser,
240                                                     const XML_Char *context,
241                                                     const XML_Char *base,
242                                                     const XML_Char *systemId,
243                                                     const XML_Char *publicId);
244 
245 /* Entity declaration handlers */
246 
247 extern void XMLCALL entity_suspending_decl_handler(void *userData,
248                                                    const XML_Char *name,
249                                                    XML_Content *model);
250 
251 extern void XMLCALL entity_suspending_xdecl_handler(void *userData,
252                                                     const XML_Char *version,
253                                                     const XML_Char *encoding,
254                                                     int standalone);
255 
256 extern int XMLCALL external_entity_param_checker(XML_Parser parser,
257                                                  const XML_Char *context,
258                                                  const XML_Char *base,
259                                                  const XML_Char *systemId,
260                                                  const XML_Char *publicId);
261 
262 extern int XMLCALL external_entity_ref_param_checker(XML_Parser parameter,
263                                                      const XML_Char *context,
264                                                      const XML_Char *base,
265                                                      const XML_Char *systemId,
266                                                      const XML_Char *publicId);
267 
268 extern int XMLCALL external_entity_param(XML_Parser parser,
269                                          const XML_Char *context,
270                                          const XML_Char *base,
271                                          const XML_Char *systemId,
272                                          const XML_Char *publicId);
273 
274 extern int XMLCALL external_entity_load_ignore(XML_Parser parser,
275                                                const XML_Char *context,
276                                                const XML_Char *base,
277                                                const XML_Char *systemId,
278                                                const XML_Char *publicId);
279 
280 extern int XMLCALL external_entity_load_ignore_utf16(XML_Parser parser,
281                                                      const XML_Char *context,
282                                                      const XML_Char *base,
283                                                      const XML_Char *systemId,
284                                                      const XML_Char *publicId);
285 
286 extern int XMLCALL external_entity_load_ignore_utf16_be(
287     XML_Parser parser, const XML_Char *context, const XML_Char *base,
288     const XML_Char *systemId, const XML_Char *publicId);
289 
290 extern int XMLCALL external_entity_valuer(XML_Parser parser,
291                                           const XML_Char *context,
292                                           const XML_Char *base,
293                                           const XML_Char *systemId,
294                                           const XML_Char *publicId);
295 
296 extern int XMLCALL external_entity_not_standalone(XML_Parser parser,
297                                                   const XML_Char *context,
298                                                   const XML_Char *base,
299                                                   const XML_Char *systemId,
300                                                   const XML_Char *publicId);
301 
302 extern int XMLCALL external_entity_value_aborter(XML_Parser parser,
303                                                  const XML_Char *context,
304                                                  const XML_Char *base,
305                                                  const XML_Char *systemId,
306                                                  const XML_Char *publicId);
307 
308 extern int XMLCALL external_entity_public(XML_Parser parser,
309                                           const XML_Char *context,
310                                           const XML_Char *base,
311                                           const XML_Char *systemId,
312                                           const XML_Char *publicId);
313 
314 extern int XMLCALL external_entity_devaluer(XML_Parser parser,
315                                             const XML_Char *context,
316                                             const XML_Char *base,
317                                             const XML_Char *systemId,
318                                             const XML_Char *publicId);
319 
320 typedef struct ext_hdlr_data {
321   const char *parse_text;
322   XML_ExternalEntityRefHandler handler;
323 } ExtHdlrData;
324 
325 extern int XMLCALL external_entity_oneshot_loader(XML_Parser parser,
326                                                   const XML_Char *context,
327                                                   const XML_Char *base,
328                                                   const XML_Char *systemId,
329                                                   const XML_Char *publicId);
330 
331 typedef struct ExtTest2 {
332   const char *parse_text;
333   int parse_len;
334   const XML_Char *encoding;
335   CharData *storage;
336 } ExtTest2;
337 
338 extern int XMLCALL external_entity_loader2(XML_Parser parser,
339                                            const XML_Char *context,
340                                            const XML_Char *base,
341                                            const XML_Char *systemId,
342                                            const XML_Char *publicId);
343 
344 typedef struct ExtFaults2 {
345   const char *parse_text;
346   int parse_len;
347   const char *fail_text;
348   const XML_Char *encoding;
349   enum XML_Error error;
350 } ExtFaults2;
351 
352 extern int XMLCALL external_entity_faulter2(XML_Parser parser,
353                                             const XML_Char *context,
354                                             const XML_Char *base,
355                                             const XML_Char *systemId,
356                                             const XML_Char *publicId);
357 
358 extern int XMLCALL external_entity_unfinished_attlist(XML_Parser parser,
359                                                       const XML_Char *context,
360                                                       const XML_Char *base,
361                                                       const XML_Char *systemId,
362                                                       const XML_Char *publicId);
363 
364 extern int XMLCALL external_entity_handler(XML_Parser parser,
365                                            const XML_Char *context,
366                                            const XML_Char *base,
367                                            const XML_Char *systemId,
368                                            const XML_Char *publicId);
369 
370 extern int XMLCALL external_entity_duff_loader(XML_Parser parser,
371                                                const XML_Char *context,
372                                                const XML_Char *base,
373                                                const XML_Char *systemId,
374                                                const XML_Char *publicId);
375 
376 extern int XMLCALL external_entity_dbl_handler(XML_Parser parser,
377                                                const XML_Char *context,
378                                                const XML_Char *base,
379                                                const XML_Char *systemId,
380                                                const XML_Char *publicId);
381 
382 extern int XMLCALL external_entity_dbl_handler_2(XML_Parser parser,
383                                                  const XML_Char *context,
384                                                  const XML_Char *base,
385                                                  const XML_Char *systemId,
386                                                  const XML_Char *publicId);
387 
388 extern int XMLCALL external_entity_alloc_set_encoding(XML_Parser parser,
389                                                       const XML_Char *context,
390                                                       const XML_Char *base,
391                                                       const XML_Char *systemId,
392                                                       const XML_Char *publicId);
393 
394 extern int XMLCALL external_entity_reallocator(XML_Parser parser,
395                                                const XML_Char *context,
396                                                const XML_Char *base,
397                                                const XML_Char *systemId,
398                                                const XML_Char *publicId);
399 
400 extern int XMLCALL external_entity_alloc(XML_Parser parser,
401                                          const XML_Char *context,
402                                          const XML_Char *base,
403                                          const XML_Char *systemId,
404                                          const XML_Char *publicId);
405 
406 extern int XMLCALL external_entity_parser_create_alloc_fail_handler(
407     XML_Parser parser, const XML_Char *context, const XML_Char *base,
408     const XML_Char *systemId, const XML_Char *publicId);
409 
410 struct AccountingTestCase {
411   const char *primaryText;
412   const char *firstExternalText;  /* often NULL */
413   const char *secondExternalText; /* often NULL */
414   const unsigned long long expectedCountBytesIndirectExtra;
415 };
416 
417 extern int accounting_external_entity_ref_handler(XML_Parser parser,
418                                                   const XML_Char *context,
419                                                   const XML_Char *base,
420                                                   const XML_Char *systemId,
421                                                   const XML_Char *publicId);
422 
423 /* NotStandalone handlers */
424 
425 extern int XMLCALL reject_not_standalone_handler(void *userData);
426 
427 extern int XMLCALL accept_not_standalone_handler(void *userData);
428 
429 /* Attribute List handlers */
430 
431 typedef struct AttTest {
432   const char *definition;
433   const XML_Char *element_name;
434   const XML_Char *attr_name;
435   const XML_Char *attr_type;
436   const XML_Char *default_value;
437   int is_required;
438 } AttTest;
439 
440 extern void XMLCALL verify_attlist_decl_handler(
441     void *userData, const XML_Char *element_name, const XML_Char *attr_name,
442     const XML_Char *attr_type, const XML_Char *default_value, int is_required);
443 
444 /* Character data handlers */
445 
446 extern void XMLCALL clearing_aborting_character_handler(void *userData,
447                                                         const XML_Char *s,
448                                                         int len);
449 
450 extern void XMLCALL parser_stop_character_handler(void *userData,
451                                                   const XML_Char *s, int len);
452 
453 extern void XMLCALL cr_cdata_handler(void *userData, const XML_Char *s,
454                                      int len);
455 
456 extern void XMLCALL rsqb_handler(void *userData, const XML_Char *s, int len);
457 
458 typedef struct ByteTestData {
459   int start_element_len;
460   int cdata_len;
461   int total_string_len;
462 } ByteTestData;
463 
464 extern void XMLCALL byte_character_handler(void *userData, const XML_Char *s,
465                                            int len);
466 
467 extern void XMLCALL ext2_accumulate_characters(void *userData,
468                                                const XML_Char *s, int len);
469 
470 /* Handlers that record their `len` arg and a single identifying character */
471 
472 struct handler_record_entry {
473   const char *name;
474   int arg;
475 };
476 struct handler_record_list {
477   int count;
478   struct handler_record_entry entries[50]; // arbitrary big-enough max count
479 };
480 
481 extern void XMLCALL record_default_handler(void *userData, const XML_Char *s,
482                                            int len);
483 
484 extern void XMLCALL record_cdata_handler(void *userData, const XML_Char *s,
485                                          int len);
486 
487 extern void XMLCALL record_cdata_nodefault_handler(void *userData,
488                                                    const XML_Char *s, int len);
489 
490 extern void XMLCALL record_skip_handler(void *userData,
491                                         const XML_Char *entityName,
492                                         int is_parameter_entity);
493 
494 extern void XMLCALL record_element_start_handler(void *userData,
495                                                  const XML_Char *name,
496                                                  const XML_Char **atts);
497 
498 extern void XMLCALL record_element_end_handler(void *userData,
499                                                const XML_Char *name);
500 
501 extern const struct handler_record_entry *
502 _handler_record_get(const struct handler_record_list *storage, int index,
503                     const char *file, int line);
504 
505 #  define handler_record_get(storage, index)                                   \
506     _handler_record_get((storage), (index), __FILE__, __LINE__)
507 
508 #  define assert_record_handler_called(storage, index, expected_name,          \
509                                        expected_arg)                           \
510     do {                                                                       \
511       const struct handler_record_entry *e                                     \
512           = handler_record_get(storage, index);                                \
513       assert_true(strcmp(e->name, expected_name) == 0);                        \
514       assert_true(e->arg == (expected_arg));                                   \
515     } while (0)
516 
517 /* Entity Declaration Handlers */
518 #  define ENTITY_MATCH_FAIL (-1)
519 #  define ENTITY_MATCH_NOT_FOUND (0)
520 #  define ENTITY_MATCH_SUCCESS (1)
521 
522 extern void XMLCALL param_entity_match_handler(
523     void *userData, const XML_Char *entityName, int is_parameter_entity,
524     const XML_Char *value, int value_length, const XML_Char *base,
525     const XML_Char *systemId, const XML_Char *publicId,
526     const XML_Char *notationName);
527 
528 extern void param_entity_match_init(const XML_Char *name,
529                                     const XML_Char *value);
530 
531 extern int get_param_entity_match_flag(void);
532 
533 /* Misc handlers */
534 
535 extern void XMLCALL xml_decl_handler(void *userData, const XML_Char *version,
536                                      const XML_Char *encoding, int standalone);
537 
538 extern void XMLCALL param_check_skip_handler(void *userData,
539                                              const XML_Char *entityName,
540                                              int is_parameter_entity);
541 
542 extern void XMLCALL data_check_comment_handler(void *userData,
543                                                const XML_Char *data);
544 
545 extern void XMLCALL selective_aborting_default_handler(void *userData,
546                                                        const XML_Char *s,
547                                                        int len);
548 
549 extern void XMLCALL suspending_comment_handler(void *userData,
550                                                const XML_Char *data);
551 
552 extern void XMLCALL element_decl_suspender(void *userData, const XML_Char *name,
553                                            XML_Content *model);
554 
555 extern void XMLCALL accumulate_pi_characters(void *userData,
556                                              const XML_Char *target,
557                                              const XML_Char *data);
558 
559 extern void XMLCALL accumulate_comment(void *userData, const XML_Char *data);
560 
561 extern void XMLCALL accumulate_entity_decl(
562     void *userData, const XML_Char *entityName, int is_parameter_entity,
563     const XML_Char *value, int value_length, const XML_Char *base,
564     const XML_Char *systemId, const XML_Char *publicId,
565     const XML_Char *notationName);
566 
567 extern void XMLCALL accumulate_char_data(void *userData, const XML_Char *s,
568                                          int len);
569 
570 extern void XMLCALL accumulate_start_element(void *userData,
571                                              const XML_Char *name,
572                                              const XML_Char **atts);
573 
574 typedef struct default_check {
575   const XML_Char *expected;
576   const int expectedLen;
577   XML_Bool seen;
578 } DefaultCheck;
579 
580 void XMLCALL checking_default_handler(void *userData, const XML_Char *s,
581                                       int len);
582 
583 typedef struct {
584   XML_Parser parser;
585   CharData *storage;
586 } ParserPlusStorage;
587 
588 extern void XMLCALL
589 accumulate_and_suspend_comment_handler(void *userData, const XML_Char *data);
590 
591 #endif /* XML_HANDLERS_H */
592 
593 #ifdef __cplusplus
594 }
595 #endif
596