/
xml_parser.c
272 lines (228 loc) · 7.12 KB
/
xml_parser.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* createrepo_c - Library of routines for manipulation with repodata
* Copyright (C) 2013 Tomas Mlcoch
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <glib.h>
#include <glib/gprintf.h>
#include <assert.h>
#include <errno.h>
#include "error.h"
#include "xml_parser.h"
#include "xml_parser_internal.h"
#include "misc.h"
#define ERR_DOMAIN CREATEREPO_C_ERROR
cr_ParserData *
cr_xml_parser_data(unsigned int numstates)
{
cr_ParserData *pd = g_new0(cr_ParserData, 1);
pd->content = g_malloc(CONTENT_REALLOC_STEP);
pd->acontent = CONTENT_REALLOC_STEP;
pd->swtab = g_malloc0(sizeof(cr_StatesSwitch *) * numstates);
pd->sbtab = g_malloc(sizeof(unsigned int) * numstates);
return pd;
}
void
cr_xml_parser_data_free(cr_ParserData *pd)
{
g_free(pd->content);
g_free(pd->swtab);
g_free(pd->sbtab);
g_free(pd);
}
void
cr_char_handler(void *pdata, const xmlChar *s, int len)
{
int l;
char *c;
cr_ParserData *pd = pdata;
if (pd->err)
return; /* There was an error -> do nothing */
if (!pd->docontent)
return; /* Do not store the content */
l = pd->lcontent + len + 1;
if (l > pd->acontent) {
pd->acontent = l + CONTENT_REALLOC_STEP;
pd->content = realloc(pd->content, pd->acontent);
}
c = pd->content + pd->lcontent;
pd->lcontent += len;
while (len-- > 0)
*c++ = *s++;
*c = '\0';
}
int
cr_xml_parser_warning(cr_ParserData *pd,
cr_XmlParserWarningType type,
const char *msg,
...)
{
int ret;
va_list args;
char *warn;
GError *tmp_err;
assert(pd);
assert(msg);
if (!pd->warningcb)
return CR_CB_RET_OK;
va_start(args, msg);
g_vasprintf(&warn, msg, args);
va_end(args);
tmp_err = NULL;
ret = pd->warningcb(type, warn, pd->warningcb_data, &tmp_err);
g_free(warn);
if (ret != CR_CB_RET_OK) {
if (tmp_err)
g_propagate_prefixed_error(&pd->err, tmp_err,
"Parsing interrupted: ");
else
g_set_error(&pd->err, ERR_DOMAIN, CRE_CBINTERRUPTED,
"Parsing interrupted");
}
assert(pd->err || ret == CR_CB_RET_OK);
return ret;
}
gint64
cr_xml_parser_strtoll(cr_ParserData *pd,
const char *nptr,
unsigned int base)
{
gint64 val;
char *endptr = NULL;
assert(pd);
assert(base <= 36 && base != 1);
if (!nptr)
return 0;
val = g_ascii_strtoll(nptr, &endptr, base);
if ((val == G_MAXINT64 || val == G_MININT64) && errno == ERANGE)
cr_xml_parser_warning(pd, CR_XML_WARNING_BADATTRVAL,
"Correct integer value \"%s\" caused overflow", nptr);
else if (val == 0 && *endptr != '\0')
cr_xml_parser_warning(pd, CR_XML_WARNING_BADATTRVAL,
"Conversion of \"%s\" to integer failed", nptr);
return val;
}
int
cr_newpkgcb(cr_Package **pkg,
G_GNUC_UNUSED const char *pkgId,
G_GNUC_UNUSED const char *name,
G_GNUC_UNUSED const char *arch,
G_GNUC_UNUSED void *cbdata,
GError **err)
{
assert(pkg && *pkg == NULL);
assert(!err || *err == NULL);
*pkg = cr_package_new();
return CRE_OK;
}
int
cr_xml_parser_generic(xmlParserCtxtPtr parser,
cr_ParserData *pd,
const char *path,
GError **err)
{
/* Note: This function uses .err members of cr_ParserData! */
int ret = CRE_OK;
CR_FILE *f;
GError *tmp_err = NULL;
char buf[XML_BUFFER_SIZE];
assert(parser);
assert(pd);
assert(path);
assert(!err || *err == NULL);
f = cr_open(path, CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION, &tmp_err);
if (tmp_err) {
int code = tmp_err->code;
g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", path);
return code;
}
while (1) {
int len;
len = cr_read(f, buf, XML_BUFFER_SIZE, &tmp_err);
if (tmp_err) {
ret = tmp_err->code;
g_critical("%s: Error while reading xml '%s': %s",
__func__, path, tmp_err->message);
g_propagate_prefixed_error(err, tmp_err, "Read error: ");
break;
}
if (xmlParseChunk(parser, buf, len, len == 0)) {
ret = CRE_XMLPARSER;
xmlErrorPtr xml_err = xmlCtxtGetLastError(parser);
g_critical("%s: parsing error '%s': %s",
__func__,
path,
xml_err->message);
g_set_error(err, ERR_DOMAIN, CRE_XMLPARSER,
"Parse error '%s' at line: %d (%s)",
path,
(int) xml_err->line,
(char *) xml_err->message);
break;
}
if (pd->err) {
ret = pd->err->code;
g_propagate_error(err, pd->err);
break;
}
if (len == 0)
break;
}
if (ret != CRE_OK) {
// An error already encoutentered
// just close the file without error checking
cr_close(f, NULL);
} else {
// No error encountered yet
cr_close(f, &tmp_err);
if (tmp_err) {
ret = tmp_err->code;
g_propagate_prefixed_error(err, tmp_err, "Error while closing: ");
}
}
return ret;
}
int
cr_xml_parser_generic_from_string(xmlParserCtxtPtr parser,
cr_ParserData *pd,
const char *xml_string,
GError **err)
{
/* Note: This function uses .err members of cr_ParserData! */
int ret = CRE_OK;
assert(parser);
assert(pd);
assert(xml_string);
assert(!err || *err == NULL);
if (xmlParseChunk(parser, xml_string, strlen(xml_string), 1)) {
ret = CRE_XMLPARSER;
xmlErrorPtr xml_err = xmlCtxtGetLastError(parser);
g_critical("%s: parsing error '%s': %s",
__func__,
xml_string,
xml_err->message);
g_set_error(err, ERR_DOMAIN, CRE_XMLPARSER,
"Parse error '%s' at line: %d (%s)",
xml_string,
(int) xml_err->line,
(char *) xml_err->message);
}
if (pd->err) {
ret = pd->err->code;
g_propagate_error(err, pd->err);
}
return ret;
}