/src/open62541/deps/yxml.h
Line | Count | Source (jump to first uncovered line) |
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 <stdint.h> |
27 | | #include <stddef.h> |
28 | | |
29 | | #if defined(_MSC_VER) && !defined(__cplusplus) && !defined(inline) |
30 | | #define inline __inline |
31 | | #endif |
32 | | |
33 | | /* Full API documentation for this library can be found in the "yxml.md" file |
34 | | * in the yxml git repository, or online at http://dev.yorhel.nl/yxml/man */ |
35 | | |
36 | | typedef enum { |
37 | | YXML_EEOF = -5, /* Unexpected EOF */ |
38 | | YXML_EREF = -4, /* Invalid character or entity reference (&whatever;) */ |
39 | | YXML_ECLOSE = -3, /* Close tag does not match open tag (<Tag> .. </OtherTag>) */ |
40 | | YXML_ESTACK = -2, /* Stack overflow (too deeply nested tags or too long element/attribute name) */ |
41 | | YXML_ESYN = -1, /* Syntax error (unexpected byte) */ |
42 | | YXML_OK = 0, /* Character consumed, no new token present */ |
43 | | YXML_ELEMSTART = 1, /* Start of an element: '<Tag ..' */ |
44 | | YXML_CONTENT = 2, /* Element content */ |
45 | | YXML_ELEMEND = 3, /* End of an element: '.. />' or '</Tag>' */ |
46 | | YXML_ATTRSTART = 4, /* Attribute: 'Name=..' */ |
47 | | YXML_ATTRVAL = 5, /* Attribute value */ |
48 | | YXML_ATTREND = 6, /* End of attribute '.."' */ |
49 | | YXML_PISTART = 7, /* Start of a processing instruction */ |
50 | | YXML_PICONTENT = 8, /* Content of a PI */ |
51 | | YXML_PIEND = 9 /* End of a processing instruction */ |
52 | | } yxml_ret_t; |
53 | | |
54 | | /* When, exactly, are tokens returned? |
55 | | * |
56 | | * <TagName |
57 | | * '>' ELEMSTART |
58 | | * '/' ELEMSTART, '>' ELEMEND |
59 | | * ' ' ELEMSTART |
60 | | * '>' |
61 | | * '/', '>' ELEMEND |
62 | | * Attr |
63 | | * '=' ATTRSTART |
64 | | * "X ATTRVAL |
65 | | * 'Y' ATTRVAL |
66 | | * 'Z' ATTRVAL |
67 | | * '"' ATTREND |
68 | | * '>' |
69 | | * '/', '>' ELEMEND |
70 | | * |
71 | | * </TagName |
72 | | * '>' ELEMEND |
73 | | */ |
74 | | |
75 | | typedef struct { |
76 | | /* PUBLIC (read-only) */ |
77 | | |
78 | | /* Name of the current element, zero-length if not in any element. Changed |
79 | | * after YXML_ELEMSTART. The pointer will remain valid up to and including |
80 | | * the next non-YXML_ATTR* token, the pointed-to buffer will remain valid |
81 | | * up to and including the YXML_ELEMEND for the corresponding element. */ |
82 | | char *elem; |
83 | | |
84 | | /* The last read character(s) of an attribute value (YXML_ATTRVAL), element |
85 | | * data (YXML_CONTENT), or processing instruction (YXML_PICONTENT). Changed |
86 | | * after one of the respective YXML_ values is returned, and only valid |
87 | | * until the next yxml_parse() call. Usually, this string only consists of |
88 | | * a single byte, but multiple bytes are returned in the following cases: |
89 | | * - "<?SomePI ?x ?>": The two characters "?x" |
90 | | * - "<![CDATA[ ]x ]]>": The two characters "]x" |
91 | | * - "<![CDATA[ ]]x ]]>": The three characters "]]x" |
92 | | * - "&#N;" and "&#xN;", where dec(n) > 127. The referenced Unicode |
93 | | * character is then encoded in multiple UTF-8 bytes. |
94 | | */ |
95 | | char data[8]; |
96 | | |
97 | | /* Name of the current attribute. Changed after YXML_ATTRSTART, valid up to |
98 | | * and including the next YXML_ATTREND. */ |
99 | | char *attr; |
100 | | |
101 | | /* Name/target of the current processing instruction, zero-length if not in |
102 | | * a PI. Changed after YXML_PISTART, valid up to (but excluding) |
103 | | * the next YXML_PIEND. */ |
104 | | char *pi; |
105 | | |
106 | | /* Line number, byte offset within that line, and total bytes read. These |
107 | | * values refer to the position _after_ the last byte given to |
108 | | * yxml_parse(). These are useful for debugging and error reporting. */ |
109 | | uint64_t byte; |
110 | | uint64_t total; |
111 | | uint32_t line; |
112 | | |
113 | | /* PRIVATE */ |
114 | | int state; |
115 | | unsigned char *stack; /* Stack of element names + attribute/PI name, separated by \0. Also starts with a \0. */ |
116 | | size_t stacksize, stacklen; |
117 | | unsigned reflen; |
118 | | unsigned quote; |
119 | | int nextstate; /* Used for '@' state remembering and for the "string" consuming state */ |
120 | | unsigned ignore; |
121 | | unsigned char *string; |
122 | | } yxml_t; |
123 | | |
124 | | #ifdef __cplusplus |
125 | | extern "C" { |
126 | | #endif |
127 | | |
128 | | void yxml_init(yxml_t *, void *, size_t); |
129 | | |
130 | | yxml_ret_t yxml_parse(yxml_t *, int); |
131 | | |
132 | | /* May be called after the last character has been given to yxml_parse(). |
133 | | * Returns YXML_OK if the XML document is valid, YXML_EEOF otherwise. Using |
134 | | * this function isn't really necessary, but can be used to detect documents |
135 | | * that don't end correctly. In particular, an error is returned when the XML |
136 | | * document did not contain a (complete) root element, or when the document |
137 | | * ended while in a comment or processing instruction. */ |
138 | | yxml_ret_t yxml_eof(yxml_t *); |
139 | | |
140 | | #ifdef __cplusplus |
141 | | } |
142 | | #endif |
143 | | |
144 | | /* Returns the length of the element name (x->elem), attribute name (x->attr), |
145 | | * or PI name (x->pi). This function should ONLY be used directly after the |
146 | | * YXML_ELEMSTART, YXML_ATTRSTART or YXML_PISTART (respectively) tokens have |
147 | | * been returned by yxml_parse(), calling this at any other time may not give |
148 | | * the correct results. This function should also NOT be used on strings other |
149 | | * than x->elem, x->attr or x->pi. */ |
150 | 0 | static inline size_t yxml_symlen(yxml_t *x, const char *s) { |
151 | 0 | return (x->stack + x->stacklen) - (const unsigned char*)s; |
152 | 0 | } Unexecuted instantiation: yxml.c:yxml_symlen Unexecuted instantiation: ua_types_encoding_xml.c:yxml_symlen |
153 | | |
154 | | #endif /* YXML_H */ |