-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml.h
205 lines (181 loc) · 6.07 KB
/
xml.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* xmlTrivialCallbackParser - a XML callback parser in C
* Copyright (C) 2018-2023 G. David Butler <gdb@dbSystems.com>
*
* This file is part of xmlTrivialCallbackParser
*
* xmlTrivialCallbackParser is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* xmlTrivialCallbackParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* xml "string" (fragment) is not \0 terminated */
typedef struct {
const unsigned char *s;
unsigned int l; /* entire length */
unsigned int o; /* offset of local name (character after the first ':', if any) */
} xmlSt_t;
/* callback event types */
typedef enum {
xmlTp_Eb /* element begin - elementAttributeName is NULL and ecBodyOrEaValue is '<' (l is 0) */
,xmlTp_Ea /* element attribute - elementAttributeName is NULL if the value has no name */
,xmlTp_Ec /* element content - elementAttributeName is NULL if ecBodyOrEaValue is only whitespace */
,xmlTp_Ee /* element end - elementAttributeName is NULL and ecBodyOrEaValue is '>' (l is 0) */
} xmlTp_t;
/* prototype of callback function */
/* returns 0 on success else aborts parse */
typedef int (*xmlCb_t)(
xmlTp_t
,unsigned int numberOfElementTagNames
,const xmlSt_t *elementTagName
,const xmlSt_t *elementAttributeName
,const xmlSt_t *ecBodyOrEaValue
,void *userContext
);
/* return -1 on error else offset of last char parsed */
/* provide a tag buffer large enough for the deepest level of nesting supported */
int xmlParse(
xmlCb_t
,unsigned int numberOfElementTagBuf
,xmlSt_t *elementTagBuf
,const unsigned char *xml
,unsigned int xlen
,void *userContext
);
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlDecodeBody(
unsigned char *out
,unsigned int olen
,const unsigned char *in
,unsigned int ilen
);
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlEncodeString(
unsigned char *out
,unsigned int olen
,const unsigned char *in
,unsigned int ilen
);
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlEncodeCdata(
unsigned char *out
,unsigned int olen
,const unsigned char *in
,unsigned int ilen
);
/* For <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> */
/* <foo type="xs:anyURI"></foo> */
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlDecodeUri(
unsigned char *out
,unsigned int olen
,const unsigned char *in
,unsigned int ilen
);
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlEncodeUri(
char *out
,unsigned int olen
,const unsigned char *in
,unsigned int ilen
);
/* <foo type="xs:base64Binary"></foo> */
/* estimated length of needed out buffer */
#define xmlDecodeBase64Need(inl) (((inl + 3) / 4) * 3)
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlDecodeBase64(
unsigned char *out
,unsigned int olen
,const char *in
,unsigned int ilen
);
/* estimated length of needed out buffer */
#define xmlEncodeBase64Need(inl) (((inl + 2) / 3) * 4)
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlEncodeBase64(
char *out
,unsigned int olen
,const unsigned char *in
,unsigned int ilen
);
/* <foo type="xs:hexBinary"></foo> */
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlDecodeHex(
unsigned char *out
,unsigned int olen
,const char *in
,unsigned int ilen
);
/* return -1 on error else the length of out */
/* if length returned is more than length provided, allocate needed memory and retry */
int xmlEncodeHex(
char *out
,unsigned int olen
,const unsigned char *in
,unsigned int ilen
);
/* XML document node */
typedef struct xmlNode {
struct xmlNode *parent;
struct {
xmlSt_t name;
xmlSt_t value;
} *attribute; /* array of attributes */
struct xmlNode **node; /* array of nodes */
const unsigned char *beg; /* pointer to XML element begin '<' (0=content) */
const unsigned char *end; /* pointer to XML element end '>' (0=content) */
void *user; /* user data (not used by xmlNode) */
xmlSt_t value; /* content or element name */
unsigned int attributeN; /* number of attributes */
unsigned int nodeN; /* number of nodes */
unsigned int nodeW; /* walk node */
} xmlNode_t;
/* parse an XML document of len with(out) white bodies into an allocated XML node */
/* return -1 on error else offset of last char parsed */
/* the node is allocated with all that was parseable */
int
xml2node(
void *(*realloc)(void *, unsigned long)
,xmlNode_t *node
,xmlSt_t *elementTagBuf
,const unsigned char *xml
,unsigned int numberOfElementTagBuf
,unsigned int xlen
,int whiteBody
);
/* xmlNode walk visit types */
typedef enum {
xmlNodeVisitPreorder
,xmlNodeVisitInorder
,xmlNodeVisitPostorder
,xmlNodeVisitLeaf
} xmlNodeVisit_t;
/* walk a xmlNode calling action on node with closure */
void
xmlNodeWalk(
xmlNode_t *n
,void (*action)(xmlNode_t *node, unsigned int depth, xmlNodeVisit_t visit, void *closure)
,void *closure
);
/* deallocate an xmlNode tree */
void
xmlNodeFree(
void (*free)(void *)
,xmlNode_t *node
);