/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> private static OSD DeserializeLLSDNotationElement(StringReader reader) { int character = ReadAndSkipWhitespace(reader); if (character < 0) { return(new OSD()); // server returned an empty file, so we're going to pass along a null LLSD object } OSD osd; int matching; switch ((char)character) { case undefNotationValue: osd = new OSD(); break; case trueNotationValueOne: osd = OSD.FromBoolean(true); break; case trueNotationValueTwo: matching = BufferCharactersEqual(reader, trueNotationValueTwoFull, 1); if (matching > 1 && matching < trueNotationValueTwoFull.Length) { throw new OSDException("Notation LLSD parsing: True value parsing error:"); } osd = OSD.FromBoolean(true); break; case trueNotationValueThree: matching = BufferCharactersEqual(reader, trueNotationValueThreeFull, 1); if (matching > 1 && matching < trueNotationValueThreeFull.Length) { throw new OSDException("Notation LLSD parsing: True value parsing error:"); } osd = OSD.FromBoolean(true); break; case falseNotationValueOne: osd = OSD.FromBoolean(false); break; case falseNotationValueTwo: matching = BufferCharactersEqual(reader, falseNotationValueTwoFull, 1); if (matching > 1 && matching < falseNotationValueTwoFull.Length) { throw new OSDException("Notation LLSD parsing: True value parsing error:"); } osd = OSD.FromBoolean(false); break; case falseNotationValueThree: matching = BufferCharactersEqual(reader, falseNotationValueThreeFull, 1); if (matching > 1 && matching < falseNotationValueThreeFull.Length) { throw new OSDException("Notation LLSD parsing: True value parsing error:"); } osd = OSD.FromBoolean(false); break; case integerNotationMarker: osd = DeserializeLLSDNotationInteger(reader); break; case realNotationMarker: osd = DeserializeLLSDNotationReal(reader); break; case uuidNotationMarker: char[] uuidBuf = new char[36]; if (reader.Read(uuidBuf, 0, 36) < 36) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in UUID."); } UUID lluuid; if (!UUID.TryParse(new String(uuidBuf), out lluuid)) { throw new OSDException("Notation LLSD parsing: Invalid UUID discovered."); } osd = OSD.FromUUID(lluuid); break; case binaryNotationMarker: byte[] bytes = Utils.EmptyBytes; int bChar = reader.Peek(); if (bChar < 0) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in binary."); } if ((char)bChar == sizeBeginNotationMarker) { throw new OSDException("Notation LLSD parsing: Raw binary encoding not supported."); } else if (Char.IsDigit((char)bChar)) { char[] charsBaseEncoding = new char[2]; if (reader.Read(charsBaseEncoding, 0, 2) < 2) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in binary."); } int baseEncoding; if (!Int32.TryParse(new String(charsBaseEncoding), out baseEncoding)) { throw new OSDException("Notation LLSD parsing: Invalid binary encoding base."); } if (baseEncoding == 64) { if (reader.Read() < 0) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in binary."); } string bytes64 = GetStringDelimitedBy(reader, doubleQuotesNotationMarker); bytes = Convert.FromBase64String(bytes64); } else { throw new OSDException("Notation LLSD parsing: Encoding base" + baseEncoding + " + not supported."); } } osd = OSD.FromBinary(bytes); break; case stringNotationMarker: int numChars = GetLengthInBrackets(reader); if (reader.Read() < 0) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in string."); } char[] chars = new char[numChars]; if (reader.Read(chars, 0, numChars) < numChars) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in string."); } if (reader.Read() < 0) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in string."); } osd = OSD.FromString(new String(chars)); break; case singleQuotesNotationMarker: string sOne = GetStringDelimitedBy(reader, singleQuotesNotationMarker); osd = OSD.FromString(sOne); break; case doubleQuotesNotationMarker: string sTwo = GetStringDelimitedBy(reader, doubleQuotesNotationMarker); osd = OSD.FromString(sTwo); break; case uriNotationMarker: if (reader.Read() < 0) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in string."); } string sUri = GetStringDelimitedBy(reader, doubleQuotesNotationMarker); Uri uri; try { uri = new Uri(sUri, UriKind.RelativeOrAbsolute); } catch { throw new OSDException("Notation LLSD parsing: Invalid Uri format detected."); } osd = OSD.FromUri(uri); break; case dateNotationMarker: if (reader.Read() < 0) { throw new OSDException("Notation LLSD parsing: Unexpected end of stream in date."); } string date = GetStringDelimitedBy(reader, doubleQuotesNotationMarker); DateTime dt; if (!DateTime.TryParse(date, out dt)) { throw new OSDException("Notation LLSD parsing: Invalid date discovered."); } osd = OSD.FromDate(dt); break; case arrayBeginNotationMarker: osd = DeserializeLLSDNotationArray(reader); break; case mapBeginNotationMarker: osd = DeserializeLLSDNotationMap(reader); break; default: throw new OSDException("Notation LLSD parsing: Unknown type marker '" + (char)character + "'."); } return(osd); }
private static OSD ParseLLSDBinaryElement(Stream stream) { SkipWhiteSpace(stream); OSD osd; int marker = stream.ReadByte(); if (marker < 0) { throw new OSDException("Binary LLSD parsing: Unexpected end of stream."); } switch ((byte)marker) { case undefBinaryValue: osd = new OSD(); break; case trueBinaryValue: osd = OSD.FromBoolean(true); break; case falseBinaryValue: osd = OSD.FromBoolean(false); break; case integerBinaryMarker: int integer = Utils.BytesToIntBig(ConsumeBytes(stream, int32Length)); osd = OSD.FromInteger(integer); break; case realBinaryMarker: double dbl = Utils.BytesToDoubleBig(ConsumeBytes(stream, doubleLength)); osd = OSD.FromReal(dbl); break; case uuidBinaryMarker: osd = OSD.FromUUID(new UUID(ConsumeBytes(stream, 16), 0)); break; case binaryBinaryMarker: int binaryLength = Utils.BytesToIntBig(ConsumeBytes(stream, int32Length)); osd = OSD.FromBinary(ConsumeBytes(stream, binaryLength)); break; case stringBinaryMarker: int stringLength = Utils.BytesToIntBig(ConsumeBytes(stream, int32Length)); string ss = Encoding.UTF8.GetString(ConsumeBytes(stream, stringLength)); osd = OSD.FromString(ss); break; case uriBinaryMarker: int uriLength = Utils.BytesToIntBig(ConsumeBytes(stream, int32Length)); string sUri = Encoding.UTF8.GetString(ConsumeBytes(stream, uriLength)); Uri uri; try { uri = new Uri(sUri, UriKind.RelativeOrAbsolute); } catch { throw new OSDException("Binary LLSD parsing: Invalid Uri format detected."); } osd = OSD.FromUri(uri); break; case dateBinaryMarker: double timestamp = Utils.BytesToDouble(ConsumeBytes(stream, doubleLength)); DateTime dateTime = DateTime.SpecifyKind(Utils.Epoch, DateTimeKind.Utc); dateTime = dateTime.AddSeconds(timestamp); osd = OSD.FromDate(dateTime.ToLocalTime()); break; case arrayBeginBinaryMarker: osd = ParseLLSDBinaryArray(stream); break; case mapBeginBinaryMarker: osd = ParseLLSDBinaryMap(stream); break; default: throw new OSDException("Binary LLSD parsing: Unknown type marker."); } return(osd); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> private static OSD ParseLLSDXmlElement(XmlTextReader reader) { SkipWhitespace(reader); if (reader.NodeType != XmlNodeType.Element) { throw new OSDException("Expected an element"); } string type = reader.LocalName; OSD ret; switch (type) { case "undef": if (reader.IsEmptyElement) { reader.Read(); return(new OSD()); } reader.Read(); SkipWhitespace(reader); ret = new OSD(); break; case "boolean": if (reader.IsEmptyElement) { reader.Read(); return(OSD.FromBoolean(false)); } if (reader.Read()) { string s = reader.ReadString().Trim(); if (!String.IsNullOrEmpty(s) && (s == "true" || s == "1")) { ret = OSD.FromBoolean(true); break; } } ret = OSD.FromBoolean(false); break; case "integer": if (reader.IsEmptyElement) { reader.Read(); return(OSD.FromInteger(0)); } if (reader.Read()) { int value = 0; Int32.TryParse(reader.ReadString().Trim(), out value); ret = OSD.FromInteger(value); break; } ret = OSD.FromInteger(0); break; case "real": if (reader.IsEmptyElement) { reader.Read(); return(OSD.FromReal(0d)); } if (reader.Read()) { double value = 0d; string str = reader.ReadString().Trim().ToLower(); if (str == "nan") { value = Double.NaN; } else { Utils.TryParseDouble(str, out value); } ret = OSD.FromReal(value); break; } ret = OSD.FromReal(0d); break; case "uuid": if (reader.IsEmptyElement) { reader.Read(); return(OSD.FromUUID(UUID.Zero)); } if (reader.Read()) { UUID value = UUID.Zero; UUID.TryParse(reader.ReadString().Trim(), out value); ret = OSD.FromUUID(value); break; } ret = OSD.FromUUID(UUID.Zero); break; case "date": if (reader.IsEmptyElement) { reader.Read(); return(OSD.FromDate(Utils.Epoch)); } if (reader.Read()) { DateTime value = Utils.Epoch; DateTime.TryParse(reader.ReadString().Trim(), out value); ret = OSD.FromDate(value); break; } ret = OSD.FromDate(Utils.Epoch); break; case "string": if (reader.IsEmptyElement) { reader.Read(); return(OSD.FromString(String.Empty)); } if (reader.Read()) { ret = OSD.FromString(reader.ReadString()); break; } ret = OSD.FromString(String.Empty); break; case "binary": if (reader.IsEmptyElement) { reader.Read(); return(OSD.FromBinary(Utils.EmptyBytes)); } if (reader.GetAttribute("encoding") != null && reader.GetAttribute("encoding") != "base64") { throw new OSDException("Unsupported binary encoding: " + reader.GetAttribute("encoding")); } if (reader.Read()) { try { ret = OSD.FromBinary(Convert.FromBase64String(reader.ReadString().Trim())); break; } catch (FormatException ex) { throw new OSDException("Binary decoding exception: " + ex.Message); } } ret = OSD.FromBinary(Utils.EmptyBytes); break; case "uri": if (reader.IsEmptyElement) { reader.Read(); return(OSD.FromUri(new Uri(String.Empty, UriKind.RelativeOrAbsolute))); } if (reader.Read()) { ret = OSD.FromUri(new Uri(reader.ReadString(), UriKind.RelativeOrAbsolute)); break; } ret = OSD.FromUri(new Uri(String.Empty, UriKind.RelativeOrAbsolute)); break; case "map": return(ParseLLSDXmlMap(reader)); case "array": return(ParseLLSDXmlArray(reader)); default: reader.Read(); ret = null; break; } if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != type) { throw new OSDException("Expected </" + type + ">"); } else { reader.Read(); return(ret); } }
public static OSD DeserializeJson(JsonData json) { switch (json.GetJsonType()) { case JsonType.Boolean: return(OSD.FromBoolean((bool)json)); case JsonType.Int: return(OSD.FromInteger((int)json)); case JsonType.Long: return(OSD.FromLong((long)json)); case JsonType.Double: return(OSD.FromReal((double)json)); case JsonType.String: string str = (string)json; if (String.IsNullOrEmpty(str)) { return(new OSD()); } else { switch (str[0]) { case 'd': if (str.StartsWith("date::")) { DateTime dt; if (DateTime.TryParse(str.Substring(6), out dt)) { return(OSD.FromDate(dt)); } } break; case 'u': if (str.StartsWith("uuid::")) { UUID id; if (UUID.TryParse(str.Substring(6), out id)) { return(OSD.FromUUID(id)); } } else if (str.StartsWith("uri::")) { try { Uri uri = new Uri(str.Substring(5)); return(OSD.FromUri(uri)); } catch (UriFormatException) { } } break; case 'b': if (str.StartsWith("b64::")) { try { byte[] data = Convert.FromBase64String(str.Substring(5)); return(OSD.FromBinary(data)); } catch (FormatException) { } } break; } return(OSD.FromString((string)json)); } case JsonType.Array: OSDArray array = new OSDArray(json.Count); for (int i = 0; i < json.Count; i++) { array.Add(DeserializeJson(json[i])); } return(array); case JsonType.Object: OSDMap map = new OSDMap(json.Count); foreach (KeyValuePair <string, JsonData> kvp in json) { map.Add(kvp.Key, DeserializeJson(kvp.Value)); } return(map); case JsonType.None: default: return(new OSD()); } }