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