xref: /freebsd/contrib/expat/tests/handlers.h (revision 35c0a8c449fd2b7f75029ebed5e10852240f0865)
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 } ExtHdlrData;
329 
330 extern int XMLCALL external_entity_oneshot_loader(XML_Parser parser,
331                                                   const XML_Char *context,
332                                                   const XML_Char *base,
333                                                   const XML_Char *systemId,
334                                                   const XML_Char *publicId);
335 
336 typedef struct ExtTest2 {
337   const char *parse_text;
338   int parse_len;
339   const XML_Char *encoding;
340   CharData *storage;
341 } ExtTest2;
342 
343 extern int XMLCALL external_entity_loader2(XML_Parser parser,
344                                            const XML_Char *context,
345                                            const XML_Char *base,
346                                            const XML_Char *systemId,
347                                            const XML_Char *publicId);
348 
349 typedef struct ExtFaults2 {
350   const char *parse_text;
351   int parse_len;
352   const char *fail_text;
353   const XML_Char *encoding;
354   enum XML_Error error;
355 } ExtFaults2;
356 
357 extern int XMLCALL external_entity_faulter2(XML_Parser parser,
358                                             const XML_Char *context,
359                                             const XML_Char *base,
360                                             const XML_Char *systemId,
361                                             const XML_Char *publicId);
362 
363 extern int XMLCALL external_entity_unfinished_attlist(XML_Parser parser,
364                                                       const XML_Char *context,
365                                                       const XML_Char *base,
366                                                       const XML_Char *systemId,
367                                                       const XML_Char *publicId);
368 
369 extern int XMLCALL external_entity_handler(XML_Parser parser,
370                                            const XML_Char *context,
371                                            const XML_Char *base,
372                                            const XML_Char *systemId,
373                                            const XML_Char *publicId);
374 
375 extern int XMLCALL external_entity_duff_loader(XML_Parser parser,
376                                                const XML_Char *context,
377                                                const XML_Char *base,
378                                                const XML_Char *systemId,
379                                                const XML_Char *publicId);
380 
381 extern int XMLCALL external_entity_dbl_handler(XML_Parser parser,
382                                                const XML_Char *context,
383                                                const XML_Char *base,
384                                                const XML_Char *systemId,
385                                                const XML_Char *publicId);
386 
387 extern int XMLCALL external_entity_dbl_handler_2(XML_Parser parser,
388                                                  const XML_Char *context,
389                                                  const XML_Char *base,
390                                                  const XML_Char *systemId,
391                                                  const XML_Char *publicId);
392 
393 extern int XMLCALL external_entity_alloc_set_encoding(XML_Parser parser,
394                                                       const XML_Char *context,
395                                                       const XML_Char *base,
396                                                       const XML_Char *systemId,
397                                                       const XML_Char *publicId);
398 
399 extern int XMLCALL external_entity_reallocator(XML_Parser parser,
400                                                const XML_Char *context,
401                                                const XML_Char *base,
402                                                const XML_Char *systemId,
403                                                const XML_Char *publicId);
404 
405 extern int XMLCALL external_entity_alloc(XML_Parser parser,
406                                          const XML_Char *context,
407                                          const XML_Char *base,
408                                          const XML_Char *systemId,
409                                          const XML_Char *publicId);
410 
411 extern int XMLCALL external_entity_parser_create_alloc_fail_handler(
412     XML_Parser parser, const XML_Char *context, const XML_Char *base,
413     const XML_Char *systemId, const XML_Char *publicId);
414 
415 struct AccountingTestCase {
416   const char *primaryText;
417   const char *firstExternalText;  /* often NULL */
418   const char *secondExternalText; /* often NULL */
419   const unsigned long long expectedCountBytesIndirectExtra;
420 };
421 
422 extern int accounting_external_entity_ref_handler(XML_Parser parser,
423                                                   const XML_Char *context,
424                                                   const XML_Char *base,
425                                                   const XML_Char *systemId,
426                                                   const XML_Char *publicId);
427 
428 /* NotStandalone handlers */
429 
430 extern int XMLCALL reject_not_standalone_handler(void *userData);
431 
432 extern int XMLCALL accept_not_standalone_handler(void *userData);
433 
434 /* Attribute List handlers */
435 
436 typedef struct AttTest {
437   const char *definition;
438   const XML_Char *element_name;
439   const XML_Char *attr_name;
440   const XML_Char *attr_type;
441   const XML_Char *default_value;
442   int is_required;
443 } AttTest;
444 
445 extern void XMLCALL verify_attlist_decl_handler(
446     void *userData, const XML_Char *element_name, const XML_Char *attr_name,
447     const XML_Char *attr_type, const XML_Char *default_value, int is_required);
448 
449 /* Character data handlers */
450 
451 extern void XMLCALL clearing_aborting_character_handler(void *userData,
452                                                         const XML_Char *s,
453                                                         int len);
454 
455 extern void XMLCALL parser_stop_character_handler(void *userData,
456                                                   const XML_Char *s, int len);
457 
458 extern void XMLCALL cr_cdata_handler(void *userData, const XML_Char *s,
459                                      int len);
460 
461 extern void XMLCALL rsqb_handler(void *userData, const XML_Char *s, int len);
462 
463 typedef struct ByteTestData {
464   int start_element_len;
465   int cdata_len;
466   int total_string_len;
467 } ByteTestData;
468 
469 extern void XMLCALL byte_character_handler(void *userData, const XML_Char *s,
470                                            int len);
471 
472 extern void XMLCALL ext2_accumulate_characters(void *userData,
473                                                const XML_Char *s, int len);
474 
475 /* Handlers that record their `len` arg and a single identifying character */
476 
477 struct handler_record_entry {
478   const char *name;
479   int arg;
480 };
481 struct handler_record_list {
482   int count;
483   struct handler_record_entry entries[50]; // arbitrary big-enough max count
484 };
485 
486 extern void XMLCALL record_default_handler(void *userData, const XML_Char *s,
487                                            int len);
488 
489 extern void XMLCALL record_cdata_handler(void *userData, const XML_Char *s,
490                                          int len);
491 
492 extern void XMLCALL record_cdata_nodefault_handler(void *userData,
493                                                    const XML_Char *s, int len);
494 
495 extern void XMLCALL record_skip_handler(void *userData,
496                                         const XML_Char *entityName,
497                                         int is_parameter_entity);
498 
499 extern void XMLCALL record_element_start_handler(void *userData,
500                                                  const XML_Char *name,
501                                                  const XML_Char **atts);
502 
503 extern void XMLCALL record_element_end_handler(void *userData,
504                                                const XML_Char *name);
505 
506 extern const struct handler_record_entry *
507 _handler_record_get(const struct handler_record_list *storage, int index,
508                     const char *file, int line);
509 
510 #  define handler_record_get(storage, index)                                   \
511     _handler_record_get((storage), (index), __FILE__, __LINE__)
512 
513 #  define assert_record_handler_called(storage, index, expected_name,          \
514                                        expected_arg)                           \
515     do {                                                                       \
516       const struct handler_record_entry *e                                     \
517           = handler_record_get(storage, index);                                \
518       assert_true(strcmp(e->name, expected_name) == 0);                        \
519       assert_true(e->arg == (expected_arg));                                   \
520     } while (0)
521 
522 /* Entity Declaration Handlers */
523 #  define ENTITY_MATCH_FAIL (-1)
524 #  define ENTITY_MATCH_NOT_FOUND (0)
525 #  define ENTITY_MATCH_SUCCESS (1)
526 
527 extern void XMLCALL param_entity_match_handler(
528     void *userData, const XML_Char *entityName, int is_parameter_entity,
529     const XML_Char *value, int value_length, const XML_Char *base,
530     const XML_Char *systemId, const XML_Char *publicId,
531     const XML_Char *notationName);
532 
533 extern void param_entity_match_init(const XML_Char *name,
534                                     const XML_Char *value);
535 
536 extern int get_param_entity_match_flag(void);
537 
538 /* Misc handlers */
539 
540 extern void XMLCALL xml_decl_handler(void *userData, const XML_Char *version,
541                                      const XML_Char *encoding, int standalone);
542 
543 extern void XMLCALL param_check_skip_handler(void *userData,
544                                              const XML_Char *entityName,
545                                              int is_parameter_entity);
546 
547 extern void XMLCALL data_check_comment_handler(void *userData,
548                                                const XML_Char *data);
549 
550 extern void XMLCALL selective_aborting_default_handler(void *userData,
551                                                        const XML_Char *s,
552                                                        int len);
553 
554 extern void XMLCALL suspending_comment_handler(void *userData,
555                                                const XML_Char *data);
556 
557 extern void XMLCALL element_decl_suspender(void *userData, const XML_Char *name,
558                                            XML_Content *model);
559 
560 extern void XMLCALL accumulate_pi_characters(void *userData,
561                                              const XML_Char *target,
562                                              const XML_Char *data);
563 
564 extern void XMLCALL accumulate_comment(void *userData, const XML_Char *data);
565 
566 extern void XMLCALL accumulate_entity_decl(
567     void *userData, const XML_Char *entityName, int is_parameter_entity,
568     const XML_Char *value, int value_length, const XML_Char *base,
569     const XML_Char *systemId, const XML_Char *publicId,
570     const XML_Char *notationName);
571 
572 extern void XMLCALL accumulate_start_element(void *userData,
573                                              const XML_Char *name,
574                                              const XML_Char **atts);
575 
576 extern void XMLCALL accumulate_characters(void *userData, const XML_Char *s,
577                                           int len);
578 
579 extern void XMLCALL accumulate_attribute(void *userData, const XML_Char *name,
580                                          const XML_Char **atts);
581 
582 extern void XMLCALL ext_accumulate_characters(void *userData, const XML_Char *s,
583                                               int len);
584 
585 typedef struct default_check {
586   const XML_Char *expected;
587   const int expectedLen;
588   XML_Bool seen;
589 } DefaultCheck;
590 
591 void XMLCALL checking_default_handler(void *userData, const XML_Char *s,
592                                       int len);
593 
594 typedef struct {
595   XML_Parser parser;
596   CharData *storage;
597 } ParserPlusStorage;
598 
599 extern void XMLCALL
600 accumulate_and_suspend_comment_handler(void *userData, const XML_Char *data);
601 
602 #endif /* XML_HANDLERS_H */
603 
604 #ifdef __cplusplus
605 }
606 #endif
607