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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#include <string.h>
#include <iostream>
#include <map>
#include <expat.h>
#include "include/types.h"
#include "include/utime.h"
#include "rgw_xml.h"
XMLObjIter::
XMLObjIter()
{
}
XMLObjIter::
~XMLObjIter()
{
}
void XMLObjIter::
set(const XMLObjIter::map_iter_t &_cur, const XMLObjIter::map_iter_t &_end)
{
cur = _cur;
end = _end;
}
XMLObj *XMLObjIter::
get_next()
{
XMLObj *obj = NULL;
if (cur != end) {
obj = cur->second;
++cur;
}
return obj;
}
bool XMLObjIter::get_name(std::string& name) const
{
if (cur == end) {
return false;
}
name = cur->first;
return true;
}
ostream& operator<<(ostream &out, const XMLObj &obj) {
out << obj.obj_type << ": " << obj.data;
return out;
}
XMLObj::
~XMLObj()
{
}
bool XMLObj::
xml_start(XMLObj *parent, const char *el, const char **attr)
{
this->parent = parent;
obj_type = el;
for (int i = 0; attr[i]; i += 2) {
attr_map[attr[i]] = std::string(attr[i + 1]);
}
return true;
}
bool XMLObj::
xml_end(const char *el)
{
return true;
}
void XMLObj::
xml_handle_data(const char *s, int len)
{
data.append(s, len);
}
const std::string& XMLObj::
XMLObj::get_data() const
{
return data;
}
const std::string& XMLObj::
XMLObj::get_obj_type() const
{
return obj_type;
}
XMLObj *XMLObj::
XMLObj::get_parent()
{
return parent;
}
void XMLObj::
add_child(const std::string& el, XMLObj *obj)
{
children.insert(std::pair<std::string, XMLObj *>(el, obj));
}
bool XMLObj::
get_attr(const std::string& name, std::string& attr) const
{
const std::map<std::string, std::string>::const_iterator iter = attr_map.find(name);
if (iter == attr_map.end())
return false;
attr = iter->second;
return true;
}
XMLObjIter XMLObj::
find(const std::string& name)
{
XMLObjIter iter;
const XMLObjIter::const_map_iter_t first = children.find(name);
XMLObjIter::const_map_iter_t last;
if (first != children.end()) {
last = children.upper_bound(name);
}else
last = children.end();
iter.set(first, last);
return iter;
}
XMLObjIter XMLObj::find_first()
{
XMLObjIter iter;
const XMLObjIter::const_map_iter_t first = children.begin();
const XMLObjIter::const_map_iter_t last = children.end();
iter.set(first, last);
return iter;
}
XMLObj *XMLObj::
find_first(const std::string& name)
{
const XMLObjIter::const_map_iter_t first = children.find(name);
if (first != children.end())
return first->second;
return nullptr;
}
RGWXMLParser::
RGWXMLParser() : buf(nullptr), buf_len(0), cur_obj(nullptr), success(true), init_called(false)
{
p = XML_ParserCreate(nullptr);
}
RGWXMLParser::
~RGWXMLParser()
{
XML_ParserFree(p);
free(buf);
std::list<XMLObj *>::const_iterator iter;
for (iter = allocated_objs.begin(); iter != allocated_objs.end(); ++iter) {
XMLObj *obj = *iter;
delete obj;
}
}
void RGWXMLParser::call_xml_start(void* user_data, const char *el, const char **attr) {
RGWXMLParser *handler = static_cast<RGWXMLParser *>(user_data);
XMLObj * obj = handler->alloc_obj(el);
if (!obj) {
handler->unallocated_objs.push_back(XMLObj());
obj = &handler->unallocated_objs.back();
} else {
handler->allocated_objs.push_back(obj);
}
if (!obj->xml_start(handler->cur_obj, el, attr)) {
handler->success = false;
return;
}
if (handler->cur_obj) {
handler->cur_obj->add_child(el, obj);
} else {
handler->children.insert(std::pair<std::string, XMLObj *>(el, obj));
}
handler->cur_obj = obj;
handler->objs.push_back(obj);
}
void RGWXMLParser::call_xml_end(void* user_data, const char *el) {
RGWXMLParser *handler = static_cast<RGWXMLParser *>(user_data);
XMLObj *parent_obj = handler->cur_obj->get_parent();
if (!handler->cur_obj->xml_end(el)) {
handler->success = false;
return;
}
handler->cur_obj = parent_obj;
}
void RGWXMLParser::call_xml_handle_data(void* user_data, const char *s, int len)
{
RGWXMLParser *handler = static_cast<RGWXMLParser *>(user_data);
handler->cur_obj->xml_handle_data(s, len);
}
bool RGWXMLParser::init()
{
if (!p) {
return false;
}
init_called = true;
XML_SetElementHandler(p, RGWXMLParser::call_xml_start, RGWXMLParser::call_xml_end);
XML_SetCharacterDataHandler(p, RGWXMLParser::call_xml_handle_data);
XML_SetUserData(p, (void *)this);
return true;
}
bool RGWXMLParser::parse(const char *_buf, int len, int done)
{
ceph_assert(init_called);
int pos = buf_len;
char *tmp_buf;
tmp_buf = (char *)realloc(buf, buf_len + len);
if (tmp_buf == NULL){
free(buf);
buf = NULL;
return false;
} else {
buf = tmp_buf;
}
memcpy(&buf[buf_len], _buf, len);
buf_len += len;
success = true;
if (!XML_Parse(p, &buf[pos], len, done)) {
fprintf(stderr, "Parse error at line %d:\n%s\n",
(int)XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
success = false;
}
return success;
}
void decode_xml_obj(unsigned long& val, XMLObj *obj)
{
auto& s = obj->get_data();
const char *start = s.c_str();
char *p;
errno = 0;
val = strtoul(start, &p, 10);
/* Check for various possible errors */
if ((errno == ERANGE && val == ULONG_MAX) ||
(errno != 0 && val == 0)) {
throw RGWXMLDecoder::err("failed to number");
}
if (p == start) {
throw RGWXMLDecoder::err("failed to parse number");
}
while (*p != '\0') {
if (!isspace(*p)) {
throw RGWXMLDecoder::err("failed to parse number");
}
p++;
}
}
void decode_xml_obj(long& val, XMLObj *obj)
{
const std::string s = obj->get_data();
const char *start = s.c_str();
char *p;
errno = 0;
val = strtol(start, &p, 10);
/* Check for various possible errors */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
(errno != 0 && val == 0)) {
throw RGWXMLDecoder::err("failed to parse number");
}
if (p == start) {
throw RGWXMLDecoder::err("failed to parse number");
}
while (*p != '\0') {
if (!isspace(*p)) {
throw RGWXMLDecoder::err("failed to parse number");
}
p++;
}
}
void decode_xml_obj(long long& val, XMLObj *obj)
{
const std::string s = obj->get_data();
const char *start = s.c_str();
char *p;
errno = 0;
val = strtoll(start, &p, 10);
/* Check for various possible errors */
if ((errno == ERANGE && (val == LLONG_MAX || val == LLONG_MIN)) ||
(errno != 0 && val == 0)) {
throw RGWXMLDecoder::err("failed to parse number");
}
if (p == start) {
throw RGWXMLDecoder::err("failed to parse number");
}
while (*p != '\0') {
if (!isspace(*p)) {
throw RGWXMLDecoder::err("failed to parse number");
}
p++;
}
}
void decode_xml_obj(unsigned long long& val, XMLObj *obj)
{
const std::string s = obj->get_data();
const char *start = s.c_str();
char *p;
errno = 0;
val = strtoull(start, &p, 10);
/* Check for various possible errors */
if ((errno == ERANGE && val == ULLONG_MAX) ||
(errno != 0 && val == 0)) {
throw RGWXMLDecoder::err("failed to parse number");
}
if (p == start) {
throw RGWXMLDecoder::err("failed to parse number");
}
while (*p != '\0') {
if (!isspace(*p)) {
throw RGWXMLDecoder::err("failed to parse number");
}
p++;
}
}
void decode_xml_obj(int& val, XMLObj *obj)
{
long l;
decode_xml_obj(l, obj);
#if LONG_MAX > INT_MAX
if (l > INT_MAX || l < INT_MIN) {
throw RGWXMLDecoder::err("integer out of range");
}
#endif
val = (int)l;
}
void decode_xml_obj(unsigned& val, XMLObj *obj)
{
unsigned long l;
decode_xml_obj(l, obj);
#if ULONG_MAX > UINT_MAX
if (l > UINT_MAX) {
throw RGWXMLDecoder::err("unsigned integer out of range");
}
#endif
val = (unsigned)l;
}
void decode_xml_obj(bool& val, XMLObj *obj)
{
const std::string s = obj->get_data();
if (strncasecmp(s.c_str(), "true", 8) == 0) {
val = true;
return;
}
if (strncasecmp(s.c_str(), "false", 8) == 0) {
val = false;
return;
}
int i;
decode_xml_obj(i, obj);
val = (bool)i;
}
void decode_xml_obj(bufferlist& val, XMLObj *obj)
{
const std::string s = obj->get_data();
bufferlist bl;
bl.append(s.c_str(), s.size());
try {
val.decode_base64(bl);
} catch (buffer::error& err) {
throw RGWXMLDecoder::err("failed to decode base64");
}
}
void decode_xml_obj(utime_t& val, XMLObj *obj)
{
const std::string s = obj->get_data();
uint64_t epoch;
uint64_t nsec;
int r = utime_t::parse_date(s, &epoch, &nsec);
if (r == 0) {
val = utime_t(epoch, nsec);
} else {
throw RGWXMLDecoder::err("failed to decode utime_t");
}
}
void encode_xml(const char *name, const string& val, Formatter *f)
{
f->dump_string(name, val);
}
void encode_xml(const char *name, const char *val, Formatter *f)
{
f->dump_string(name, val);
}
void encode_xml(const char *name, bool val, Formatter *f)
{
std::string s;
if (val)
s = "True";
else
s = "False";
f->dump_string(name, s);
}
void encode_xml(const char *name, int val, Formatter *f)
{
f->dump_int(name, val);
}
void encode_xml(const char *name, long val, Formatter *f)
{
f->dump_int(name, val);
}
void encode_xml(const char *name, unsigned val, Formatter *f)
{
f->dump_unsigned(name, val);
}
void encode_xml(const char *name, unsigned long val, Formatter *f)
{
f->dump_unsigned(name, val);
}
void encode_xml(const char *name, unsigned long long val, Formatter *f)
{
f->dump_unsigned(name, val);
}
void encode_xml(const char *name, long long val, Formatter *f)
{
f->dump_int(name, val);
}
void encode_xml(const char *name, const utime_t& val, Formatter *f)
{
val.gmtime(f->dump_stream(name));
}
void encode_xml(const char *name, const bufferlist& bl, Formatter *f)
{
/* need to copy data from bl, as it is const bufferlist */
bufferlist src = bl;
bufferlist b64;
src.encode_base64(b64);
const std::string s(b64.c_str(), b64.length());
encode_xml(name, s, f);
}
|