/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using OpenMetaverse;
namespace OpenSim.Framework.Capabilities
{
///
/// Borrowed from (a older version of) libsl for now, as their new llsd code doesn't work we our decoding code.
///
public static class LLSD
{
///
///
///
public class LLSDParseException : Exception
{
public LLSDParseException(string message) : base(message)
{
}
}
///
///
///
public class LLSDSerializeException : Exception
{
public LLSDSerializeException(string message) : base(message)
{
}
}
///
///
///
///
///
public static object LLSDDeserialize(byte[] b)
{
using (MemoryStream ms = new MemoryStream(b, false))
{
return LLSDDeserialize(ms);
}
}
///
///
///
///
///
public static object LLSDDeserialize(Stream st)
{
using (XmlTextReader reader = new XmlTextReader(st))
{
reader.DtdProcessing = DtdProcessing.Ignore;
reader.Read();
SkipWS(reader);
if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "llsd")
throw new LLSDParseException("Expected ");
reader.Read();
object ret = LLSDParseOne(reader);
SkipWS(reader);
if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != "llsd")
throw new LLSDParseException("Expected ");
return ret;
}
}
///
///
///
///
///
public static byte[] LLSDSerialize(object obj)
{
using(StringWriter sw = new StringWriter())
using(XmlTextWriter writer = new XmlTextWriter(sw))
{
writer.Formatting = Formatting.None;
writer.WriteStartElement(String.Empty, "llsd", String.Empty);
LLSDWriteOne(writer, obj);
writer.WriteEndElement();
writer.Flush();
return Util.UTF8.GetBytes(sw.ToString());
}
}
///
///
///
///
///
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 s)
{
writer.WriteStartElement(String.Empty, "string", String.Empty);
writer.WriteString(s);
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 || obj is float)
{
writer.WriteStartElement(String.Empty, "real", String.Empty);
writer.WriteString(obj.ToString());
writer.WriteEndElement();
}
else if (obj is bool b)
{
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 UUID u)
{
writer.WriteStartElement(String.Empty, "uuid", String.Empty);
writer.WriteString(u.ToString());
writer.WriteEndElement();
}
else if (obj is Hashtable h)
{
writer.WriteStartElement(string.Empty, "map", string.Empty);
foreach (DictionaryEntry de in h)
{
writer.WriteStartElement(string.Empty, "key", string.Empty);
writer.WriteString((string)de.Key);
writer.WriteEndElement();
LLSDWriteOne(writer, de.Value);
}
writer.WriteEndElement();
}
else if (obj is Dictionary dict)
{
writer.WriteStartElement(String.Empty, "map", String.Empty);
foreach (KeyValuePair kvp in dict)
{
writer.WriteStartElement(String.Empty, "key", String.Empty);
writer.WriteString(kvp.Key);
writer.WriteEndElement();
LLSDWriteOne(writer, kvp.Value);
}
writer.WriteEndElement();
}
else if (obj is ArrayList a)
{
writer.WriteStartElement(String.Empty, "array", String.Empty);
foreach (object item in a)
{
LLSDWriteOne(writer, item);
}
writer.WriteEndElement();
}
else if (obj is List