xref: /freebsd/contrib/expat/tests/handlers.h (revision fe9278888fd4414abe2d922e469cf608005f4c65)
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 typedef struct StructParserAndElementInfo {
96   XML_Parser parser;
97   ElementInfo *info;
98 } ParserAndElementInfo;
99 
100 extern void XMLCALL counting_start_element_handler(void *userData,
101                                                    const XML_Char *name,
102                                                    const XML_Char **atts);
103 
104 extern void XMLCALL suspending_end_handler(void *userData, const XML_Char *s);
105 
106 extern void XMLCALL start_element_suspender(void *userData,
107                                             const XML_Char *name,
108                                             const XML_Char **atts);
109 
110 extern int g_triplet_start_flag;
111 extern int g_triplet_end_flag;
112 
113 extern void XMLCALL triplet_start_checker(void *userData, const XML_Char *name,
114                                           const XML_Char **atts);
115 
116 extern void XMLCALL triplet_end_checker(void *userData, const XML_Char *name);
117 
118 extern void XMLCALL overwrite_start_checker(void *userData,
119                                             const XML_Char *name,
120                                             const XML_Char **atts);
121 
122 extern void XMLCALL overwrite_end_checker(void *userData, const XML_Char *name);
123 
124 extern void XMLCALL start_element_fail(void *userData, const XML_Char *name,
125                                        const XML_Char **atts);
126 
127 extern void XMLCALL start_ns_clearing_start_element(void *userData,
128                                                     const XML_Char *prefix,
129                                                     const XML_Char *uri);
130 
131 typedef struct {
132   XML_Parser parser;
133   int deep;
134 } DataIssue240;
135 
136 extern void XMLCALL start_element_issue_240(void *userData,
137                                             const XML_Char *name,
138                                             const XML_Char **atts);
139 
140 extern void XMLCALL end_element_issue_240(void *userData, const XML_Char *name);
141 
142 /* Text encoding handlers */
143 
144 extern int XMLCALL UnknownEncodingHandler(void *data, const XML_Char *encoding,
145                                           XML_Encoding *info);
146 
147 extern int XMLCALL UnrecognisedEncodingHandler(void *data,
148                                                const XML_Char *encoding,
149                                                XML_Encoding *info);
150 
151 extern int XMLCALL unknown_released_encoding_handler(void *data,
152                                                      const XML_Char *encoding,
153                                                      XML_Encoding *info);
154 
155 extern int XMLCALL MiscEncodingHandler(void *data, const XML_Char *encoding,
156                                        XML_Encoding *info);
157 
158 extern int XMLCALL long_encoding_handler(void *userData,
159                                          const XML_Char *encoding,
160                                          XML_Encoding *info);
161 
162 /* External Entity Handlers */
163 
164 typedef struct ExtOption {
165   const XML_Char *system_id;
166   const char *parse_text;
167 } ExtOption;
168 
169 extern int XMLCALL external_entity_optioner(XML_Parser parser,
170                                             const XML_Char *context,
171                                             const XML_Char *base,
172                                             const XML_Char *systemId,
173                                             const XML_Char *publicId);
174 
175 extern int XMLCALL external_entity_loader(XML_Parser parser,
176                                           const XML_Char *context,
177                                           const XML_Char *base,
178                                           const XML_Char *systemId,
179                                           const XML_Char *publicId);
180 
181 typedef struct ext_faults {
182   const char *parse_text;
183   const char *fail_text;
184   const XML_Char *encoding;
185   enum XML_Error error;
186 } ExtFaults;
187 
188 extern int XMLCALL external_entity_faulter(XML_Parser parser,
189                                            const XML_Char *context,
190                                            const XML_Char *base,
191                                            const XML_Char *systemId,
192                                            const XML_Char *publicId);
193 extern int XMLCALL external_entity_failer__if_not_xml_ge(
194     XML_Parser parser, const XML_Char *context, const XML_Char *base,
195     const XML_Char *systemId, const XML_Char *publicId);
196 extern int XMLCALL external_entity_null_loader(XML_Parser parser,
197                                                const XML_Char *context,
198                                                const XML_Char *base,
199                                                const XML_Char *systemId,
200                                                const XML_Char *publicId);
201 
202 extern int XMLCALL external_entity_resetter(XML_Parser parser,
203                                             const XML_Char *context,
204                                             const XML_Char *base,
205                                             const XML_Char *systemId,
206                                             const XML_Char *publicId);
207 
208 extern int XMLCALL external_entity_suspender(XML_Parser parser,
209                                              const XML_Char *context,
210                                              const XML_Char *base,
211                                              const XML_Char *systemId,
212                                              const XML_Char *publicId);
213 
214 extern int XMLCALL external_entity_suspend_xmldecl(XML_Parser parser,
215                                                    const XML_Char *context,
216                                                    const XML_Char *base,
217                                                    const XML_Char *systemId,
218                                                    const XML_Char *publicId);
219 
220 extern int XMLCALL external_entity_suspending_faulter(XML_Parser parser,
221                                                       const XML_Char *context,
222                                                       const XML_Char *base,
223                                                       const XML_Char *systemId,
224                                                       const XML_Char *publicId);
225 
226 extern int XMLCALL external_entity_cr_catcher(XML_Parser parser,
227                                               const XML_Char *context,
228                                               const XML_Char *base,
229                                               const XML_Char *systemId,
230                                               const XML_Char *publicId);
231 
232 extern int XMLCALL external_entity_bad_cr_catcher(XML_Parser parser,
233                                                   const XML_Char *context,
234                                                   const XML_Char *base,
235                                                   const XML_Char *systemId,
236                                                   const XML_Char *publicId);
237 
238 extern int XMLCALL external_entity_rsqb_catcher(XML_Parser parser,
239                                                 const XML_Char *context,
240                                                 const XML_Char *base,
241                                                 const XML_Char *systemId,
242                                                 const XML_Char *publicId);
243 
244 extern int XMLCALL external_entity_good_cdata_ascii(XML_Parser parser,
245                                                     const XML_Char *context,
246                                                     const XML_Char *base,
247                                                     const XML_Char *systemId,
248                                                     const XML_Char *publicId);
249 
250 /* Entity declaration handlers */
251 
252 extern void XMLCALL entity_suspending_decl_handler(void *userData,
253                                                    const XML_Char *name,
254                                                    XML_Content *model);
255 
256 extern void XMLCALL entity_suspending_xdecl_handler(void *userData,
257                                                     const XML_Char *version,
258                                                     const XML_Char *encoding,
259                                                     int standalone);
260 
261 extern int XMLCALL external_entity_param_checker(XML_Parser parser,
262                                                  const XML_Char *context,
263                                                  const XML_Char *base,
264                                                  const XML_Char *systemId,
265                                                  const XML_Char *publicId);
266 
267 extern int XMLCALL external_entity_ref_param_checker(XML_Parser parameter,
268                                                      const XML_Char *context,
269                                                      const XML_Char *base,
270                                                      const XML_Char *systemId,
271                                                      const XML_Char *publicId);
272 
273 extern int XMLCALL external_entity_param(XML_Parser parser,
274                                          const XML_Char *context,
275                                          const XML_Char *base,
276                                          const XML_Char *systemId,
277                                          const XML_Char *publicId);
278 
279 extern int XMLCALL external_entity_load_ignore(XML_Parser parser,
280                                                const XML_Char *context,
281                                                const XML_Char *base,
282                                                const XML_Char *systemId,
283                                                const XML_Char *publicId);
284 
285 extern int XMLCALL external_entity_load_ignore_utf16(XML_Parser parser,
286                                                      const XML_Char *context,
287                                                      const XML_Char *base,
288                                                      const XML_Char *systemId,
289                                                      const XML_Char *publicId);
290 
291 extern int XMLCALL external_entity_load_ignore_utf16_be(
292     XML_Parser parser, const XML_Char *context, const XML_Char *base,
293     const XML_Char *systemId, const XML_Char *publicId);
294 
295 extern int XMLCALL external_entity_valuer(XML_Parser parser,
296                                           const XML_Char *context,
297                                           const XML_Char *base,
298                                           const XML_Char *systemId,
299                                           const XML_Char *publicId);
300 
301 extern int XMLCALL external_entity_not_standalone(XML_Parser parser,
302                                                   const XML_Char *context,
303                                                   const XML_Char *base,
304                                                   const XML_Char *systemId,
305                                                   const XML_Char *publicId);
306 
307 extern int XMLCALL external_entity_value_aborter(XML_Parser parser,
308                                                  const XML_Char *context,
309                                                  const XML_Char *base,
310                                                  const XML_Char *systemId,
311                                                  const XML_Char *publicId);
312 
313 extern int XMLCALL external_entity_public(XML_Parser parser,
314                                           const XML_Char *context,
315                                           const XML_Char *base,
316                                           const XML_Char *systemId,
317                                           const XML_Char *publicId);
318 
319 extern int XMLCALL external_entity_devaluer(XML_Parser parser,
320                                             const XML_Char *context,
321                                             const XML_Char *base,
322                                             const XML_Char *systemId,
323                                             const XML_Char *publicId);
324 
325 typedef struct ext_hdlr_data {
326   const char *parse_text;
327   XML_ExternalEntityRefHandler handler;
328   CharData *storage;
329 } ExtHdlrData;
330 
331 extern int XMLCALL external_entity_oneshot_loader(XML_Parser parser,
332                                                   const XML_Char *context,
333                                                   const XML_Char *base,
334                                                   const XML_Char *systemId,
335                                                   const XML_Char *publicId);
336 
337 typedef struct ExtTest2 {
338   const char *parse_text;
339   int parse_len;
340   const XML_Char *encoding;
341   CharData *storage;
342 } ExtTest2;
343 
344 extern int XMLCALL external_entity_loader2(XML_Parser parser,
345                                            const XML_Char *context,
346                                            const XML_Char *base,
347                                            const XML_Char *systemId,
348                                            const XML_Char *publicId);
349 
350 typedef struct ExtFaults2 {
351   const char *parse_text;
352   int parse_len;
353   const char *fail_text;
354   const XML_Char *encoding;
355   enum XML_Error error;
356 } ExtFaults2;
357 
358 extern int XMLCALL external_entity_faulter2(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_unfinished_attlist(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_handler(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_duff_loader(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(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_dbl_handler_2(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_alloc_set_encoding(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_reallocator(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_alloc(XML_Parser parser,
407                                          const XML_Char *context,
408                                          const XML_Char *base,
409                                          const XML_Char *systemId,
410                                          const XML_Char *publicId);
411 
412 extern int XMLCALL external_entity_parser_create_alloc_fail_handler(
413     XML_Parser parser, const XML_Char *context, const XML_Char *base,
414     const XML_Char *systemId, const XML_Char *publicId);
415 
416 struct AccountingTestCase {
417   const char *primaryText;
418   const char *firstExternalText;  /* often NULL */
419   const char *secondExternalText; /* often NULL */
420   const unsigned long long expectedCountBytesIndirectExtra;
421 };
422 
423 extern int accounting_external_entity_ref_handler(XML_Parser parser,
424                                                   const XML_Char *context,
425                                                   const XML_Char *base,
426                                                   const XML_Char *systemId,
427                                                   const XML_Char *publicId);
428 
429 /* NotStandalone handlers */
430 
431 extern int XMLCALL reject_not_standalone_handler(void *userData);
432 
433 extern int XMLCALL accept_not_standalone_handler(void *userData);
434 
435 /* Attribute List handlers */
436 
437 typedef struct AttTest {
438   const char *definition;
439   const XML_Char *element_name;
440   const XML_Char *attr_name;
441   const XML_Char *attr_type;
442   const XML_Char *default_value;
443   int is_required;
444 } AttTest;
445 
446 extern void XMLCALL verify_attlist_decl_handler(
447     void *userData, const XML_Char *element_name, const XML_Char *attr_name,
448     const XML_Char *attr_type, const XML_Char *default_value, int is_required);
449 
450 /* Character data handlers */
451 
452 extern void XMLCALL clearing_aborting_character_handler(void *userData,
453                                                         const XML_Char *s,
454                                                         int len);
455 
456 extern void XMLCALL parser_stop_character_handler(void *userData,
457                                                   const XML_Char *s, int len);
458 
459 extern void XMLCALL cr_cdata_handler(void *userData, const XML_Char *s,
460                                      int len);
461 
462 extern void XMLCALL rsqb_handler(void *userData, const XML_Char *s, int len);
463 
464 typedef struct ByteTestData {
465   int start_element_len;
466   int cdata_len;
467   int total_string_len;
468 } ByteTestData;
469 
470 extern void XMLCALL byte_character_handler(void *userData, const XML_Char *s,
471                                            int len);
472 
473 extern void XMLCALL ext2_accumulate_characters(void *userData,
474                                                const XML_Char *s, int len);
475 
476 /* Handlers that record their `len` arg and a single identifying character */
477 
478 struct handler_record_entry {
479   const char *name;
480   int arg;
481 };
482 struct handler_record_list {
483   int count;
484   struct handler_record_entry entries[50]; // arbitrary big-enough max count
485 };
486 
487 extern void XMLCALL record_default_handler(void *userData, const XML_Char *s,
488                                            int len);
489 
490 extern void XMLCALL record_cdata_handler(void *userData, const XML_Char *s,
491                                          int len);
492 
493 extern void XMLCALL record_cdata_nodefault_handler(void *userData,
494                                                    const XML_Char *s, int len);
495 
496 extern void XMLCALL record_skip_handler(void *userData,
497                                         const XML_Char *entityName,
498                                         int is_parameter_entity);
499 
500 extern void XMLCALL record_element_start_handler(void *userData,
501                                                  const XML_Char *name,
502                                                  const XML_Char **atts);
503 
504 extern void XMLCALL record_element_end_handler(void *userData,
505                                                const XML_Char *name);
506 
507 extern const struct handler_record_entry *
508 _handler_record_get(const struct handler_record_list *storage, int index,
509                     const char *file, int line);
510 
511 #  define handler_record_get(storage, index)                                   \
512     _handler_record_get((storage), (index), __FILE__, __LINE__)
513 
514 #  define assert_record_handler_called(storage, index, expected_name,          \
515                                        expected_arg)                           \
516     do {                                                                       \
517       const struct handler_record_entry *e                                     \
518           = handler_record_get(storage, index);                                \
519       assert_true(strcmp(e->name, expected_name) == 0);                        \
520       assert_true(e->arg == (expected_arg));                                   \
521     } while (0)
522 
523 /* Entity Declaration Handlers */
524 #  define ENTITY_MATCH_FAIL (-1)
525 #  define ENTITY_MATCH_NOT_FOUND (0)
526 #  define ENTITY_MATCH_SUCCESS (1)
527 
528 extern void XMLCALL param_entity_match_handler(
529     void *userData, const XML_Char *entityName, int is_parameter_entity,
530     const XML_Char *value, int value_length, const XML_Char *base,
531     const XML_Char *systemId, const XML_Char *publicId,
532     const XML_Char *notationName);
533 
534 extern void param_entity_match_init(const XML_Char *name,
535                                     const XML_Char *value);
536 
537 extern int get_param_entity_match_flag(void);
538 
539 /* Misc handlers */
540 
541 extern void XMLCALL xml_decl_handler(void *userData, const XML_Char *version,
542                                      const XML_Char *encoding, int standalone);
543 
544 extern void XMLCALL param_check_skip_handler(void *userData,
545                                              const XML_Char *entityName,
546                                              int is_parameter_entity);
547 
548 extern void XMLCALL data_check_comment_handler(void *userData,
549                                                const XML_Char *data);
550 
551 extern void XMLCALL selective_aborting_default_handler(void *userData,
552                                                        const XML_Char *s,
553                                                        int len);
554 
555 extern void XMLCALL suspending_comment_handler(void *userData,
556                                                const XML_Char *data);
557 
558 extern void XMLCALL element_decl_suspender(void *userData, const XML_Char *name,
559                                            XML_Content *model);
560 
561 extern void XMLCALL suspend_after_element_declaration(void *userData,
562                                                       const XML_Char *name,
563                                                       XML_Content *model);
564 
565 extern void XMLCALL accumulate_pi_characters(void *userData,
566                                              const XML_Char *target,
567                                              const XML_Char *data);
568 
569 extern void XMLCALL accumulate_comment(void *userData, const XML_Char *data);
570 
571 extern void XMLCALL accumulate_entity_decl(
572     void *userData, const XML_Char *entityName, int is_parameter_entity,
573     const XML_Char *value, int value_length, const XML_Char *base,
574     const XML_Char *systemId, const XML_Char *publicId,
575     const XML_Char *notationName);
576 
577 extern void XMLCALL accumulate_char_data_and_suspend(void *userData,
578                                                      const XML_Char *s,
579                                                      int len);
580 
581 extern void XMLCALL accumulate_start_element(void *userData,
582                                              const XML_Char *name,
583                                              const XML_Char **atts);
584 
585 extern void XMLCALL accumulate_characters(void *userData, const XML_Char *s,
586                                           int len);
587 
588 extern void XMLCALL accumulate_attribute(void *userData, const XML_Char *name,
589                                          const XML_Char **atts);
590 
591 extern void XMLCALL ext_accumulate_characters(void *userData, const XML_Char *s,
592                                               int len);
593 
594 typedef struct default_check {
595   const XML_Char *expected;
596   const int expectedLen;
597   XML_Bool seen;
598 } DefaultCheck;
599 
600 void XMLCALL checking_default_handler(void *userData, const XML_Char *s,
601                                       int len);
602 
603 typedef struct {
604   XML_Parser parser;
605   CharData *storage;
606 } ParserPlusStorage;
607 
608 extern void XMLCALL
609 accumulate_and_suspend_comment_handler(void *userData, const XML_Char *data);
610 
611 #endif /* XML_HANDLERS_H */
612 
613 #ifdef __cplusplus
614 }
615 #endif
616