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
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using libsecondlife;
using System.Security.Cryptography;
using System.Text;
namespace OpenSim.Region.Capabilities
{
/// <summary>
/// Borrowed from (a older version of ) libsl for now, as their new llsd code doesn't work we our decoding code.
/// </summary>
public static class LLSD
{
/// <summary>
///
/// </summary>
public class LLSDParseException : Exception
{
public LLSDParseException(string message) : base(message) { }
}
/// <summary>
///
/// </summary>
public class LLSDSerializeException : Exception
{
public LLSDSerializeException(string message) : base(message) { }
}
/// <summary>
///
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public static object LLSDDeserialize(byte[] b)
{
return LLSDDeserialize(new MemoryStream(b, false));
}
/// <summary>
///
/// </summary>
/// <param name="st"></param>
/// <returns></returns>
public static object LLSDDeserialize(Stream st)
{
XmlTextReader reader = new XmlTextReader(st);
reader.Read();
SkipWS(reader);
if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "llsd")
throw new LLSDParseException("Expected <llsd>");
reader.Read();
object ret = LLSDParseOne(reader);
SkipWS(reader);
if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != "llsd")
throw new LLSDParseException("Expected </llsd>");
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static byte[] LLSDSerialize(object obj)
{
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
writer.Formatting = Formatting.None;
writer.WriteStartElement(String.Empty, "llsd", String.Empty);
LLSDWriteOne(writer, obj);
writer.WriteEndElement();
writer.Close();
return Encoding.UTF8.GetBytes(sw.ToString());
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="obj"></param>
public static void LLSDWriteOne(XmlTextWriter writer, object obj)
{
if (obj == null)
{
writer.WriteStartElement(String.Empty, "undef", String.Empty);
writer.WriteEndElement();
return;
}
if (obj is string)
{
writer.WriteStartElement(String.Empty, "string", String.Empty);
writer.WriteString((string)obj);
writer.WriteEndElement();
}
else if (obj is int)
{
writer.WriteStartElement(String.Empty, "integer", String.Empty);
writer.WriteString(obj.ToString());
writer.WriteEndElement();
}
else if (obj is double)
{
writer.WriteStartElement(String.Empty, "real", String.Empty);
writer.WriteString(obj.ToString());
writer.WriteEndElement();
}
else if (obj is bool)
{
bool b = (bool)obj;
writer.WriteStartElement(String.Empty, "boolean", String.Empty);
writer.WriteString(b ? "1" : "0");
writer.WriteEndElement();
}
else if (obj is ulong)
{
throw new Exception("ulong in LLSD is currently not implemented, fix me!");
}
else if (obj is LLUUID)
{
LLUUID u = (LLUUID)obj;
writer.WriteStartElement(String.Empty, "uuid", String.Empty);
writer.WriteString(u.ToStringHyphenated());
writer.WriteEndElement();
}
else if (obj is Hashtable)
{
Hashtable h = obj as Hashtable;
writer.WriteStartElement(String.Empty, "map", String.Empty);
foreach (string key in h.Keys)
{
writer.WriteStartElement(String.Empty, "key", String.Empty);
writer.WriteString(key);
writer.WriteEndElement();
LLSDWriteOne(writer, h[key]);
}
writer.WriteEndElement();
}
else if (obj is ArrayList)
{
ArrayList a = obj as ArrayList;
writer.WriteStartElement(String.Empty, "array", String.Empty);
foreach (object item in a)
{
LLSDWriteOne(writer, item);
}
writer.WriteEndElement();
}
else if (obj is byte[])
{
byte[] b = obj as byte[];
writer.WriteStartElement(String.Empty, "binary", String.Empty);
writer.WriteStartAttribute(String.Empty, "encoding", String.Empty);
writer.WriteString("base64");
writer.WriteEndAttribute();
//// Calculate the length of the base64 output
//long length = (long)(4.0d * b.Length / 3.0d);
//if (length % 4 != 0) length += 4 - (length % 4);
//// Create the char[] for base64 output and fill it
//char[] tmp = new char[length];
//int i = Convert.ToBase64CharArray(b, 0, b.Length, tmp, 0);
//writer.WriteString(new String(tmp));
writer.WriteString(Convert.ToBase64String(b));
writer.WriteEndElement();
}
else
{
throw new LLSDSerializeException("Unknown type " + obj.GetType().Name);
}
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public static object LLSDParseOne(XmlTextReader reader)
{
SkipWS(reader);
if (reader.NodeType != XmlNodeType.Element)
throw new LLSDParseException("Expected an element");
string dtype = reader.LocalName;
object ret = null;
switch (dtype)
{
case "undef":
{
if (reader.IsEmptyElement)
{
reader.Read();
return null;
}
reader.Read();
SkipWS(reader);
ret = null;
break;
}
case "boolean":
{
if (reader.IsEmptyElement)
{
reader.Read();
return false;
}
reader.Read();
string s = reader.ReadString().Trim();
if (s == String.Empty || s == "false" || s == "0")
ret = false;
else if (s == "true" || s == "1")
ret = true;
else
throw new LLSDParseException("Bad boolean value " + s);
break;
}
case "integer":
{
if (reader.IsEmptyElement)
{
reader.Read();
return 0;
}
reader.Read();
ret = Convert.ToInt32(reader.ReadString().Trim());
break;
}
case "real":
{
if (reader.IsEmptyElement)
{
reader.Read();
return 0.0f;
}
reader.Read();
ret = Convert.ToDouble(reader.ReadString().Trim());
break;
}
case "uuid":
{
if (reader.IsEmptyElement)
{
reader.Read();
return LLUUID.Zero;
}
reader.Read();
ret = new LLUUID(reader.ReadString().Trim());
break;
}
case "string":
{
if (reader.IsEmptyElement)
{
reader.Read();
return String.Empty;
}
reader.Read();
ret = reader.ReadString();
break;
}
case "binary":
{
if (reader.IsEmptyElement)
{
reader.Read();
return new byte[0];
}
if (reader.GetAttribute("encoding") != null &&
reader.GetAttribute("encoding") != "base64")
{
throw new LLSDParseException("Unknown encoding: " + reader.GetAttribute("encoding"));
}
reader.Read();
FromBase64Transform b64 = new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces);
byte[] inp = Encoding.ASCII.GetBytes(reader.ReadString());
ret = b64.TransformFinalBlock(inp, 0, inp.Length);
break;
}
case "date":
{
reader.Read();
throw new Exception("LLSD TODO: date");
}
case "map":
{
return LLSDParseMap(reader);
}
case "array":
{
return LLSDParseArray(reader);
}
default:
throw new LLSDParseException("Unknown element <" + dtype + ">");
}
if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != dtype)
{
throw new LLSDParseException("Expected </" + dtype + ">");
}
reader.Read();
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public static Hashtable LLSDParseMap(XmlTextReader reader)
{
Hashtable ret = new Hashtable();
if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "map")
throw new LLSDParseException("Expected <map>");
if (reader.IsEmptyElement)
{
reader.Read();
return ret;
}
reader.Read();
while (true)
{
SkipWS(reader);
if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "map")
{
reader.Read();
break;
}
if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "key")
throw new LLSDParseException("Expected <key>");
string key = reader.ReadString();
if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != "key")
throw new LLSDParseException("Expected </key>");
reader.Read();
object val = LLSDParseOne(reader);
ret[key] = val;
}
return ret; // TODO
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public static ArrayList LLSDParseArray(XmlTextReader reader)
{
ArrayList ret = new ArrayList();
if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "array")
throw new LLSDParseException("Expected <array>");
if (reader.IsEmptyElement)
{
reader.Read();
return ret;
}
reader.Read();
while (true)
{
SkipWS(reader);
if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "array")
{
reader.Read();
break;
}
ret.Insert(ret.Count, LLSDParseOne(reader));
}
return ret; // TODO
}
/// <summary>
///
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
private static string GetSpaces(int count)
{
StringBuilder b = new StringBuilder();
for (int i = 0; i < count; i++) b.Append(" ");
return b.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <param name="indent"></param>
/// <returns></returns>
public static String LLSDDump(object obj, int indent)
{
if (obj == null)
{
return GetSpaces(indent) + "- undef\n";
}
else if (obj is string)
{
return GetSpaces(indent) + "- string \"" + (string)obj + "\"\n";
}
else if (obj is int)
{
return GetSpaces(indent) + "- integer " + obj.ToString() + "\n";
}
else if (obj is double)
{
return GetSpaces(indent) + "- float " + obj.ToString() + "\n";
}
else if (obj is LLUUID)
{
return GetSpaces(indent) + "- uuid " + ((LLUUID)obj).ToStringHyphenated() + Environment.NewLine;
}
else if (obj is Hashtable)
{
StringBuilder ret = new StringBuilder();
ret.Append(GetSpaces(indent) + "- map" + Environment.NewLine);
Hashtable map = (Hashtable)obj;
foreach (string key in map.Keys)
{
ret.Append(GetSpaces(indent + 2) + "- key \"" + key + "\"" + Environment.NewLine);
ret.Append(LLSDDump(map[key], indent + 3));
}
return ret.ToString();
}
else if (obj is ArrayList)
{
StringBuilder ret = new StringBuilder();
ret.Append(GetSpaces(indent) + "- array\n");
ArrayList list = (ArrayList)obj;
foreach (object item in list)
{
ret.Append(LLSDDump(item, indent + 2));
}
return ret.ToString();
}
else if (obj is byte[])
{
return GetSpaces(indent) + "- binary\n" + Helpers.FieldToHexString((byte[])obj, GetSpaces(indent)) +
Environment.NewLine;
}
else
{
return GetSpaces(indent) + "- unknown type " + obj.GetType().Name + Environment.NewLine;
}
}
public static object ParseTerseLLSD(string llsd)
{
int notused;
return ParseTerseLLSD(llsd, out notused);
}
public static object ParseTerseLLSD(string llsd, out int endPos)
{
if (llsd.Length == 0)
{
endPos = 0;
return null;
}
// Identify what type of object this is
switch (llsd[0])
{
case '!':
throw new LLSDParseException("Undefined value type encountered");
case '1':
endPos = 1;
return true;
case '0':
endPos = 1;
return false;
case 'i':
{
if (llsd.Length < 2) throw new LLSDParseException("Integer value type with no value");
int value;
endPos = FindEnd(llsd, 1);
if (Int32.TryParse(llsd.Substring(1, endPos - 1), out value))
return value;
else
throw new LLSDParseException("Failed to parse integer value type");
}
case 'r':
{
if (llsd.Length < 2) throw new LLSDParseException("Real value type with no value");
double value;
endPos = FindEnd(llsd, 1);
if (Double.TryParse(llsd.Substring(1, endPos - 1), System.Globalization.NumberStyles.Float,
Helpers.EnUsCulture.NumberFormat, out value))
return value;
else
throw new LLSDParseException("Failed to parse double value type");
}
case 'u':
{
if (llsd.Length < 17) throw new LLSDParseException("LLUUID value type with no value");
LLUUID value;
endPos = FindEnd(llsd, 1);
if (LLUUID.TryParse(llsd.Substring(1, endPos - 1), out value))
return value;
else
throw new LLSDParseException("Failed to parse LLUUID value type");
}
case 'b':
//byte[] value = new byte[llsd.Length - 1];
// This isn't the actual binary LLSD format, just the terse format sent
// at login so I don't even know if there is a binary type
throw new LLSDParseException("Binary value type is unimplemented");
case 's':
case 'l':
if (llsd.Length < 2) throw new LLSDParseException("String value type with no value");
endPos = FindEnd(llsd, 1);
return llsd.Substring(1, endPos - 1);
case 'd':
// Never seen one before, don't know what the format is
throw new LLSDParseException("Date value type is unimplemented");
case '[':
{
if (llsd.IndexOf(']') == -1) throw new LLSDParseException("Invalid array");
int pos = 0;
ArrayList array = new ArrayList();
while (llsd[pos] != ']')
{
++pos;
// Advance past comma if need be
if (llsd[pos] == ',') ++pos;
// Allow a single whitespace character
if (pos < llsd.Length && llsd[pos] == ' ') ++pos;
int end;
array.Add(ParseTerseLLSD(llsd.Substring(pos), out end));
pos += end;
}
endPos = pos + 1;
return array;
}
case '{':
{
if (llsd.IndexOf('}') == -1) throw new LLSDParseException("Invalid map");
int pos = 0;
Hashtable hashtable = new Hashtable();
while (llsd[pos] != '}')
{
++pos;
// Advance past comma if need be
if (llsd[pos] == ',') ++pos;
// Allow a single whitespace character
if (pos < llsd.Length && llsd[pos] == ' ') ++pos;
if (llsd[pos] != '\'') throw new LLSDParseException("Expected a map key");
int endquote = llsd.IndexOf('\'', pos + 1);
if (endquote == -1 || (endquote + 1) >= llsd.Length || llsd[endquote + 1] != ':')
throw new LLSDParseException("Invalid map format");
string key = llsd.Substring(pos, endquote - pos);
key = key.Replace("'", String.Empty);
pos += (endquote - pos) + 2;
int end;
hashtable.Add(key, ParseTerseLLSD(llsd.Substring(pos), out end));
pos += end;
}
endPos = pos + 1;
return hashtable;
}
default:
throw new Exception("Unknown value type");
}
}
private static int FindEnd(string llsd, int start)
{
int end = llsd.IndexOfAny(new char[] { ',', ']', '}' });
if (end == -1) end = llsd.Length - 1;
return end;
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
private static void SkipWS(XmlTextReader reader)
{
while (
reader.NodeType == XmlNodeType.Comment ||
reader.NodeType == XmlNodeType.Whitespace ||
reader.NodeType == XmlNodeType.SignificantWhitespace ||
reader.NodeType == XmlNodeType.XmlDeclaration)
{
reader.Read();
}
}
}
}
|