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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
|
/**
* @file llsdserialize_xml.cpp
* @brief XML parsers and formatters for LLSD
*
* Copyright (c) 2006-$CurrentYear$, Linden Research, Inc.
* $License$
*/
#include "linden_common.h"
#include "llsdserialize_xml.h"
#include <iostream>
#include <deque>
#include "apr-1/apr_base64.h"
extern "C"
{
#include "expat/expat.h"
}
/**
* LLSDXMLFormatter
*/
LLSDXMLFormatter::LLSDXMLFormatter()
{
}
// virtual
LLSDXMLFormatter::~LLSDXMLFormatter()
{
}
// virtual
S32 LLSDXMLFormatter::format(const LLSD& data, std::ostream& ostr, U32 options) const
{
std::streamsize old_precision = ostr.precision(25);
LLString post = "";
if (options & LLSDFormatter::OPTIONS_PRETTY)
{
post = "\n";
}
ostr << "<llsd>" << post;
S32 rv = format_impl(data, ostr, options, 1);
ostr << "</llsd>\n";
ostr.precision(old_precision);
return rv;
}
S32 LLSDXMLFormatter::format_impl(const LLSD& data, std::ostream& ostr, U32 options, U32 level) const
{
S32 format_count = 1;
LLString pre = "";
LLString post = "";
if (options & LLSDFormatter::OPTIONS_PRETTY)
{
for (U32 i = 0; i < level; i++)
{
pre += " ";
}
post = "\n";
}
switch(data.type())
{
case LLSD::TypeMap:
if(0 == data.size())
{
ostr << pre << "<map />" << post;
}
else
{
ostr << pre << "<map>" << post;
LLSD::map_const_iterator iter = data.beginMap();
LLSD::map_const_iterator end = data.endMap();
for(; iter != end; ++iter)
{
ostr << pre << "<key>" << escapeString((*iter).first) << "</key>" << post;
format_count += format_impl((*iter).second, ostr, options, level + 1);
}
ostr << pre << "</map>" << post;
}
break;
case LLSD::TypeArray:
if(0 == data.size())
{
ostr << pre << "<array />" << post;
}
else
{
ostr << pre << "<array>" << post;
LLSD::array_const_iterator iter = data.beginArray();
LLSD::array_const_iterator end = data.endArray();
for(; iter != end; ++iter)
{
format_count += format_impl(*iter, ostr, options, level + 1);
}
ostr << pre << "</array>" << post;
}
break;
case LLSD::TypeUndefined:
ostr << pre << "<undef />" << post;
break;
case LLSD::TypeBoolean:
ostr << pre << "<boolean>";
if(mBoolAlpha ||
#if( LL_WINDOWS || __GNUC__ > 2)
(ostr.flags() & std::ios::boolalpha)
#else
(ostr.flags() & 0x0100)
#endif
)
{
ostr << (data.asBoolean() ? "true" : "false");
}
else
{
ostr << (data.asBoolean() ? 1 : 0);
}
ostr << "</boolean>" << post;
break;
case LLSD::TypeInteger:
ostr << pre << "<integer>" << data.asInteger() << "</integer>" << post;
break;
case LLSD::TypeReal:
ostr << pre << "<real>";
if(mRealFormat.empty())
{
ostr << data.asReal();
}
else
{
formatReal(data.asReal(), ostr);
}
ostr << "</real>" << post;
break;
case LLSD::TypeUUID:
if(data.asUUID().isNull()) ostr << pre << "<uuid />" << post;
else ostr << pre << "<uuid>" << data.asUUID() << "</uuid>" << post;
break;
case LLSD::TypeString:
if(data.asString().empty()) ostr << pre << "<string />" << post;
else ostr << pre << "<string>" << escapeString(data.asString()) <<"</string>" << post;
break;
case LLSD::TypeDate:
ostr << pre << "<date>" << data.asDate() << "</date>" << post;
break;
case LLSD::TypeURI:
ostr << pre << "<uri>" << escapeString(data.asString()) << "</uri>" << post;
break;
case LLSD::TypeBinary:
{
LLSD::Binary buffer = data.asBinary();
if(buffer.empty())
{
ostr << pre << "<binary />" << post;
}
else
{
// *FIX: memory inefficient.
ostr << pre << "<binary encoding=\"base64\">";
int b64_buffer_length = apr_base64_encode_len(buffer.size());
char* b64_buffer = new char[b64_buffer_length];
b64_buffer_length = apr_base64_encode_binary(
b64_buffer,
&buffer[0],
buffer.size());
ostr.write(b64_buffer, b64_buffer_length - 1);
delete[] b64_buffer;
ostr << "</binary>" << post;
}
break;
}
default:
// *NOTE: This should never happen.
ostr << pre << "<undef />" << post;
break;
}
return format_count;
}
// static
std::string LLSDXMLFormatter::escapeString(const std::string& in)
{
std::ostringstream out;
std::string::const_iterator it = in.begin();
std::string::const_iterator end = in.end();
for(; it != end; ++it)
{
switch((*it))
{
case '<':
out << "<";
break;
case '>':
out << ">";
break;
case '&':
out << "&";
break;
case '\'':
out << "'";
break;
case '"':
out << """;
break;
default:
out << (*it);
break;
}
}
return out.str();
}
class LLSDXMLParser::Impl
{
public:
Impl();
~Impl();
LLSD parse(std::istream& input);
void parsePart(const char *buf, int len);
private:
void reset();
void startElementHandler(const XML_Char* name, const XML_Char** attributes);
void endElementHandler(const XML_Char* name);
void characterDataHandler(const XML_Char* data, int length);
static void sStartElementHandler(
void* userData, const XML_Char* name, const XML_Char** attributes);
static void sEndElementHandler(
void* userData, const XML_Char* name);
static void sCharacterDataHandler(
void* userData, const XML_Char* data, int length);
void startSkipping();
enum Element {
ELEMENT_LLSD,
ELEMENT_UNDEF,
ELEMENT_BOOL,
ELEMENT_INTEGER,
ELEMENT_REAL,
ELEMENT_STRING,
ELEMENT_UUID,
ELEMENT_DATE,
ELEMENT_URI,
ELEMENT_BINARY,
ELEMENT_MAP,
ELEMENT_ARRAY,
ELEMENT_KEY,
ELEMENT_UNKNOWN
};
static Element readElement(const XML_Char* name);
static const XML_Char* findAttribute(const XML_Char* name, const XML_Char** pairs);
XML_Parser mParser;
LLSD mResult;
bool mInLLSDElement;
bool mGracefullStop;
typedef std::deque<LLSD*> LLSDRefStack;
LLSDRefStack mStack;
int mDepth;
bool mSkipping;
int mSkipThrough;
std::string mCurrentKey;
std::ostringstream mCurrentContent;
bool mPreStaged;
};
LLSDXMLParser::Impl::Impl()
{
mParser = XML_ParserCreate(NULL);
mPreStaged = false;
reset();
}
LLSDXMLParser::Impl::~Impl()
{
XML_ParserFree(mParser);
}
bool is_eol(char c)
{
return (c == '\n' || c == '\r');
}
void clear_eol(std::istream& input)
{
char c = input.peek();
while (input.good() && is_eol(c))
{
input.get(c);
c = input.peek();
}
}
static unsigned get_till_eol(std::istream& input, char *buf, unsigned bufsize)
{
unsigned count = 0;
while (count < bufsize && input.good())
{
input.get(buf[count]);
count++;
if (is_eol(buf[count - 1]))
break;
}
return count;
}
LLSD LLSDXMLParser::Impl::parse(std::istream& input)
{
reset();
XML_Status status;
static const int BUFFER_SIZE = 1024;
void* buffer = NULL;
int count = 0;
while (input.good() && !input.eof())
{
buffer = XML_GetBuffer(mParser, BUFFER_SIZE);
/*
* If we happened to end our last buffer right at the end of the llsd, but the
* stream is still going we will get a null buffer here. Check for mGracefullStop.
*/
if (!buffer)
{
break;
}
count = get_till_eol(input, (char *)buffer, BUFFER_SIZE);
if (!count)
{
break;
}
status = XML_ParseBuffer(mParser, count, false);
if (status == XML_STATUS_ERROR)
{
break;
}
}
// *FIX.: This code is buggy - if the stream was empty or not
// good, there is not buffer to parse, both the call to
// XML_ParseBuffer and the buffer manipulations are illegal
// futhermore, it isn't clear that the expat buffer semantics are
// preserved
status = XML_ParseBuffer(mParser, 0, true);
if (status == XML_STATUS_ERROR && !mGracefullStop)
{
((char*) buffer)[count? count - 1 : 0] = '\0';
llinfos << "LLSDXMLParser::Impl::parse: XML_STATUS_ERROR parsing:" << (char*) buffer << llendl;
return LLSD();
}
clear_eol(input);
return mResult;
}
void LLSDXMLParser::Impl::reset()
{
if (mPreStaged)
{
mPreStaged = false;
return;
}
mResult.clear();
mInLLSDElement = false;
mDepth = 0;
mGracefullStop = false;
mStack.clear();
mSkipping = false;
#if( LL_WINDOWS || __GNUC__ > 2)
mCurrentKey.clear();
#else
mCurrentKey = std::string();
#endif
XML_ParserReset(mParser, "utf-8");
XML_SetUserData(mParser, this);
XML_SetElementHandler(mParser, sStartElementHandler, sEndElementHandler);
XML_SetCharacterDataHandler(mParser, sCharacterDataHandler);
}
void LLSDXMLParser::Impl::startSkipping()
{
mSkipping = true;
mSkipThrough = mDepth;
}
const XML_Char*
LLSDXMLParser::Impl::findAttribute(const XML_Char* name, const XML_Char** pairs)
{
while (NULL != pairs && NULL != *pairs)
{
if(0 == strcmp(name, *pairs))
{
return *(pairs + 1);
}
pairs += 2;
}
return NULL;
}
void LLSDXMLParser::Impl::parsePart(const char *buf, int len)
{
void * buffer = XML_GetBuffer(mParser, len);
if (buffer != NULL && buf != NULL)
{
memcpy(buffer, buf, len);
}
XML_ParseBuffer(mParser, len, false);
mPreStaged = true;
}
void LLSDXMLParser::Impl::startElementHandler(const XML_Char* name, const XML_Char** attributes)
{
mDepth += 1;
if (mSkipping)
{
return;
}
Element element = readElement(name);
mCurrentContent.str("");
switch (element)
{
case ELEMENT_LLSD:
if (mInLLSDElement) { return startSkipping(); }
mInLLSDElement = true;
return;
case ELEMENT_KEY:
if (mStack.empty() || !(mStack.back()->isMap()))
{
return startSkipping();
}
return;
case ELEMENT_BINARY:
{
const XML_Char* encoding = findAttribute("encoding", attributes);
if(encoding && strcmp("base64", encoding) != 0) { return startSkipping(); }
break;
}
default:
// all rest are values, fall through
;
}
if (!mInLLSDElement) { return startSkipping(); }
if (mStack.empty())
{
mStack.push_back(&mResult);
}
else if (mStack.back()->isMap())
{
if (mCurrentKey.empty()) { return startSkipping(); }
LLSD& map = *mStack.back();
LLSD& newElement = map[mCurrentKey];
mStack.push_back(&newElement);
#if( LL_WINDOWS || __GNUC__ > 2)
mCurrentKey.clear();
#else
mCurrentKey = std::string();
#endif
}
else if (mStack.back()->isArray())
{
LLSD& array = *mStack.back();
array.append(LLSD());
LLSD& newElement = array[array.size()-1];
mStack.push_back(&newElement);
}
else {
// improperly nested value in a non-structure
return startSkipping();
}
switch (element)
{
case ELEMENT_MAP:
*mStack.back() = LLSD::emptyMap();
break;
case ELEMENT_ARRAY:
*mStack.back() = LLSD::emptyArray();
break;
default:
// all the other values will be set in the end element handler
;
}
}
void LLSDXMLParser::Impl::endElementHandler(const XML_Char* name)
{
mDepth -= 1;
if (mSkipping)
{
if (mDepth < mSkipThrough)
{
mSkipping = false;
}
return;
}
Element element = readElement(name);
switch (element)
{
case ELEMENT_LLSD:
if (mInLLSDElement)
{
mInLLSDElement = false;
mGracefullStop = true;
XML_StopParser(mParser, false);
}
return;
case ELEMENT_KEY:
mCurrentKey = mCurrentContent.str();
return;
default:
// all rest are values, fall through
;
}
if (!mInLLSDElement) { return; }
LLSD& value = *mStack.back();
mStack.pop_back();
std::string content = mCurrentContent.str();
mCurrentContent.str("");
switch (element)
{
case ELEMENT_UNDEF:
value.clear();
break;
case ELEMENT_BOOL:
value = content == "true" || content == "1";
break;
case ELEMENT_INTEGER:
value = LLSD(content).asInteger();
break;
case ELEMENT_REAL:
value = LLSD(content).asReal();
break;
case ELEMENT_STRING:
value = content;
break;
case ELEMENT_UUID:
value = LLSD(content).asUUID();
break;
case ELEMENT_DATE:
value = LLSD(content).asDate();
break;
case ELEMENT_URI:
value = LLSD(content).asURI();
break;
case ELEMENT_BINARY:
{
S32 len = apr_base64_decode_len(content.c_str());
std::vector<U8> data;
data.resize(len);
len = apr_base64_decode_binary(&data[0], content.c_str());
data.resize(len);
value = data;
break;
}
case ELEMENT_UNKNOWN:
value.clear();
break;
default:
// other values, map and array, have already been set
break;
}
}
void LLSDXMLParser::Impl::characterDataHandler(const XML_Char* data, int length)
{
mCurrentContent.write(data, length);
}
void LLSDXMLParser::Impl::sStartElementHandler(
void* userData, const XML_Char* name, const XML_Char** attributes)
{
((LLSDXMLParser::Impl*)userData)->startElementHandler(name, attributes);
}
void LLSDXMLParser::Impl::sEndElementHandler(
void* userData, const XML_Char* name)
{
((LLSDXMLParser::Impl*)userData)->endElementHandler(name);
}
void LLSDXMLParser::Impl::sCharacterDataHandler(
void* userData, const XML_Char* data, int length)
{
((LLSDXMLParser::Impl*)userData)->characterDataHandler(data, length);
}
LLSDXMLParser::Impl::Element LLSDXMLParser::Impl::readElement(const XML_Char* name)
{
if (strcmp(name, "llsd") == 0) { return ELEMENT_LLSD; }
if (strcmp(name, "undef") == 0) { return ELEMENT_UNDEF; }
if (strcmp(name, "boolean") == 0) { return ELEMENT_BOOL; }
if (strcmp(name, "integer") == 0) { return ELEMENT_INTEGER; }
if (strcmp(name, "real") == 0) { return ELEMENT_REAL; }
if (strcmp(name, "string") == 0) { return ELEMENT_STRING; }
if (strcmp(name, "uuid") == 0) { return ELEMENT_UUID; }
if (strcmp(name, "date") == 0) { return ELEMENT_DATE; }
if (strcmp(name, "uri") == 0) { return ELEMENT_URI; }
if (strcmp(name, "binary") == 0) { return ELEMENT_BINARY; }
if (strcmp(name, "map") == 0) { return ELEMENT_MAP; }
if (strcmp(name, "array") == 0) { return ELEMENT_ARRAY; }
if (strcmp(name, "key") == 0) { return ELEMENT_KEY; }
return ELEMENT_UNKNOWN;
}
LLSDXMLParser::LLSDXMLParser()
: impl(* new Impl)
{
}
LLSDXMLParser::~LLSDXMLParser()
{
delete &impl;
}
void LLSDXMLParser::parsePart(const char *buf, int len)
{
impl.parsePart(buf, len);
}
// virtual
S32 LLSDXMLParser::parse(std::istream& input, LLSD& data) const
{
data = impl.parse(input);
return 0;
}
|