Coverage Report

Created: 2026-09-27 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541_15/deps/yxml.h
Line
Count
Source
1
/* Copyright (c) 2013-2014 Yoran Heling
2
3
  Permission is hereby granted, free of charge, to any person obtaining
4
  a copy of this software and associated documentation files (the
5
  "Software"), to deal in the Software without restriction, including
6
  without limitation the rights to use, copy, modify, merge, publish,
7
  distribute, sublicense, and/or sell copies of the Software, and to
8
  permit persons to whom the Software is furnished to do so, subject to
9
  the following conditions:
10
11
  The above copyright notice and this permission notice shall be included
12
  in all copies or substantial portions of the Software.
13
14
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
*/
22
23
#ifndef YXML_H
24
#define YXML_H
25
26
#include <stdbool.h>
27
#include <stdint.h>
28
#include <stddef.h>
29
30
#if defined(_MSC_VER) && !defined(__cplusplus) && !defined(inline)
31
#define inline __inline
32
#endif
33
34
/* Full API documentation for this library can be found in the "yxml.md" file
35
 * in the yxml git repository, or online at http://dev.yorhel.nl/yxml/man */
36
37
typedef enum {
38
  YXML_EEOF        = -5, /* Unexpected EOF                             */
39
  YXML_EREF        = -4, /* Invalid character or entity reference (&whatever;) */
40
  YXML_ECLOSE      = -3, /* Close tag does not match open tag (<Tag> .. </OtherTag>) */
41
  YXML_ESTACK      = -2, /* Stack overflow (too deeply nested tags or too long element/attribute name) */
42
  YXML_ESYN        = -1, /* Syntax error (unexpected byte)             */
43
  YXML_OK          =  0, /* Character consumed, no new token present   */
44
  YXML_ELEMSTART   =  1, /* Start of an element:   '<Tag ..'           */
45
  YXML_CONTENT     =  2, /* Element content                            */
46
  YXML_ELEMEND     =  3, /* End of an element:     '.. />' or '</Tag>' */
47
  YXML_ATTRSTART   =  4, /* Attribute:             'Name=..'           */
48
  YXML_ATTRVAL     =  5, /* Attribute value                            */
49
  YXML_ATTREND     =  6, /* End of attribute       '.."'               */
50
  YXML_PISTART     =  7, /* Start of a processing instruction          */
51
  YXML_PICONTENT   =  8, /* Content of a PI                            */
52
  YXML_PIEND       =  9  /* End of a processing instruction            */
53
} yxml_ret_t;
54
55
/* When, exactly, are tokens returned?
56
 *
57
 * <TagName
58
 *   '>' ELEMSTART
59
 *   '/' ELEMSTART, '>' ELEMEND
60
 *   ' ' ELEMSTART
61
 *     '>'
62
 *     '/', '>' ELEMEND
63
 *     Attr
64
 *       '=' ATTRSTART
65
 *         "X ATTRVAL
66
 *           'Y'  ATTRVAL
67
 *             'Z'  ATTRVAL
68
 *               '"' ATTREND
69
 *                 '>'
70
 *                 '/', '>' ELEMEND
71
 *
72
 * </TagName
73
 *   '>' ELEMEND
74
 */
75
76
typedef struct {
77
  /* PUBLIC (read-only) */
78
79
  /* Name of the current element, zero-length if not in any element. Changed
80
   * after YXML_ELEMSTART. The pointer will remain valid up to and including
81
   * the next non-YXML_ATTR* token, the pointed-to buffer will remain valid
82
   * up to and including the YXML_ELEMEND for the corresponding element. */
83
  char *elem;
84
85
  /* The last read character(s) of an attribute value (YXML_ATTRVAL), element
86
   * data (YXML_CONTENT), or processing instruction (YXML_PICONTENT). Changed
87
   * after one of the respective YXML_ values is returned, and only valid
88
   * until the next yxml_parse() call. Usually, this string only consists of
89
   * a single byte, but multiple bytes are returned in the following cases:
90
   * - "<?SomePI ?x ?>": The two characters "?x"
91
   * - "<![CDATA[ ]x ]]>": The two characters "]x"
92
   * - "<![CDATA[ ]]x ]]>": The three characters "]]x"
93
   * - "&#N;" and "&#xN;", where dec(n) > 127. The referenced Unicode
94
   *   character is then encoded in multiple UTF-8 bytes.
95
   */
96
  char data[8];
97
98
  /* True if the current YXML_CONTENT or YXML_ATTRVAL event was produced from
99
   * a character or entity reference. Valid until the next yxml_parse() call. */
100
  bool isReference;
101
102
  /* Name of the current attribute. Changed after YXML_ATTRSTART, valid up to
103
   * and including the next YXML_ATTREND. */
104
  char *attr;
105
106
  /* Name/target of the current processing instruction, zero-length if not in
107
   * a PI. Changed after YXML_PISTART, valid up to (but excluding)
108
   * the next YXML_PIEND. */
109
  char *pi;
110
111
  /* Line number, byte offset within that line, and total bytes read. These
112
   * values refer to the position _after_ the last byte given to
113
   * yxml_parse(). These are useful for debugging and error reporting. */
114
  uint64_t byte;
115
  uint64_t total;
116
  uint32_t line;
117
118
  /* PRIVATE */
119
  int state;
120
  unsigned char *stack; /* Stack of element names + attribute/PI name, separated by \0. Also starts with a \0. */
121
  size_t stacksize, stacklen;
122
  unsigned reflen;
123
  unsigned quote;
124
  int nextstate; /* Used for '@' state remembering and for the "string" consuming state */
125
  unsigned ignore;
126
  unsigned char *string;
127
  bool contentmode;
128
} yxml_t;
129
130
#ifdef __cplusplus
131
extern "C" {
132
#endif
133
134
void yxml_init(yxml_t *, void *, size_t);
135
136
/* Initialize the parser for an XML content fragment without an enclosing
137
 * element. Character data, references, CDATA sections, comments, processing
138
 * instructions and balanced child elements are accepted. */
139
void yxml_init_content(yxml_t *, void *, size_t);
140
141
yxml_ret_t yxml_parse(yxml_t *, int);
142
143
/* May be called after the last character has been given to yxml_parse().
144
 * Returns YXML_OK if the XML document or content fragment is valid, YXML_EEOF
145
 * otherwise. Using this function isn't really necessary, but can be used to
146
 * detect input that doesn't end correctly. In document mode, an error is
147
 * returned when there is no complete root element. In content mode, an error
148
 * is returned for unbalanced child elements. Both modes reject input ending
149
 * inside a reference, CDATA section, comment or processing instruction. */
150
yxml_ret_t yxml_eof(yxml_t *);
151
152
#ifdef __cplusplus
153
}
154
#endif
155
156
/* Returns the length of the element name (x->elem), attribute name (x->attr),
157
 * or PI name (x->pi). This function should ONLY be used directly after the
158
 * YXML_ELEMSTART, YXML_ATTRSTART or YXML_PISTART (respectively) tokens have
159
 * been returned by yxml_parse(), calling this at any other time may not give
160
 * the correct results. This function should also NOT be used on strings other
161
 * than x->elem, x->attr or x->pi. */
162
0
static inline size_t yxml_symlen(yxml_t *x, const char *s) {
163
0
  return (size_t)((x->stack + x->stacklen) - (const unsigned char*)s);
164
0
}
Unexecuted instantiation: yxml.c:yxml_symlen
Unexecuted instantiation: ua_types_encoding_xml.c:yxml_symlen
165
166
#endif /* YXML_H */