diff --git a/libsecondlife/Types.cs b/libsecondlife/Types.cs index 4a9fd8ee..90feb798 100644 --- a/libsecondlife/Types.cs +++ b/libsecondlife/Types.cs @@ -1,8 +1,9 @@ /* - * Copyright (c) 2006, 2007 Second Life Reverse Engineering Team + * Copyright (c) 2006, Second Life Reverse Engineering Team + * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: + * - 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. @@ -34,23 +35,34 @@ namespace libsecondlife /// A 128-bit Universally Unique Identifier, used throughout the Second /// Life networking protocol /// - public struct LLUUID - { - /// The Guid we are wrapping to make an LLUUID - public Guid id; - - /// Get a byte array of the 16 raw bytes making up the UUID - public byte[] Data { get { return id.ToByteArray(); } } - - /// - /// Constructor that takes a string UUID representation - /// - /// A string representation of a UUID, case - /// insensitive and can either be hyphenated or non-hyphenated - /// LLUUID("11f8aa9c-b071-4242-836b-13b7abe0d489") - public LLUUID(string val) { - id = new Guid(val); - } + [Serializable] + public class LLUUID : IXmlSerializable + { + /// Get a byte array of the 16 raw bytes making up the UUID + public byte[] Data { get { return data; } } + + /// The 16 bytes that make up the UUID + protected byte[] data; + + + /// + /// Default constructor + /// + public LLUUID() + { + data = new byte[16]; + } + + /// + /// Constructor that takes a string UUID representation + /// + /// A string representation of a UUID, case + /// insensitive and can either be hyphenated or non-hyphenated + /// LLUUID("11f8aa9c-b071-4242-836b-13b7abe0d489") + public LLUUID(string val) + { + data = StringToBytes(val); + } /// /// Constructor that takes a byte array containing a UUID @@ -59,9 +71,8 @@ namespace libsecondlife /// Beginning offset in the array public LLUUID(byte[] byteArray, int pos) { - byte[] data = new byte[16]; + data = new byte[16]; Array.Copy(byteArray, pos, data, 0, 16); - id=new Guid(data); } /// @@ -71,10 +82,9 @@ namespace libsecondlife /// 64-bit unsigned integer to convert to a UUID public LLUUID(ulong val) { - byte[] data = new byte[16]; + data = new byte[16]; byte[] bytes = BitConverter.GetBytes(val); Array.Copy(bytes, data, bytes.Length); - id=new Guid(data); } /// @@ -83,24 +93,24 @@ namespace libsecondlife /// A 16 byte array containing this UUID public byte[] GetBytes() { - return Data; + return data; } - - /// - /// Calculate an LLCRC (cyclic redundancy check) for this LLUUID - /// - /// The CRC checksum for this LLUUID - public uint CRC() - { - uint retval = 0; - byte[] _data=GetBytes(); - retval += (uint)((_data[3] << 24) + (_data[2] << 16) + (_data[1] << 8) + _data[0]); - retval += (uint)((_data[7] << 24) + (_data[6] << 16) + (_data[5] << 8) + _data[4]); - retval += (uint)((_data[11] << 24) + (_data[10] << 16) + (_data[9] << 8) + _data[8]); - retval += (uint)((_data[15] << 24) + (_data[14] << 16) + (_data[13] << 8) + _data[12]); - return retval; - } + /// + /// Calculate an LLCRC (cyclic redundancy check) for this LLUUID + /// + /// The CRC checksum for this LLUUID + public uint CRC() + { + uint retval = 0; + + retval += (uint)((Data[3] << 24) + (Data[2] << 16) + (Data[1] << 8) + Data[0]); + retval += (uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]); + retval += (uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]); + retval += (uint)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]); + + return retval; + } /// /// Combine two UUIDs together by taking the MD5 hash of a byte array @@ -112,12 +122,43 @@ namespace libsecondlife { // Build the buffer to MD5 byte[] input = new byte[32]; - Array.Copy(GetBytes(), input, 16); - Array.Copy(other.GetBytes(), 0, input, 16, 16); + Array.Copy(data, input, 16); + Array.Copy(other.Data, 0, input, 16, 16); return new LLUUID(Helpers.MD5Builder.ComputeHash(input), 0); } + /// + /// Parse the bytes for a LLUUID from a string + /// + /// String containing 32 (or 36 with hyphens) character UUID + /// + protected static byte[] StringToBytes(string val) + { + if (val.Length == 36) val = val.Replace("-", ""); + if (val.Length != 32) throw new Exception("Malformed LLUUID: " + val); + + byte[] parseData = new byte[16]; + + for (int i = 0; i < 16; ++i) + { + parseData[i] = Convert.ToByte(val.Substring(i * 2, 2), 16); + } + + return parseData; + } + + /// + /// Generate a LLUUID from a string + /// + /// A string representation of a UUID, case + /// insensitive and can either be hyphenated or non-hyphenated + /// LLUUID.Parse("11f8aa9c-b071-4242-836b-13b7abe0d489") + public static LLUUID Parse(string val) + { + return new LLUUID(StringToBytes(val), 0); + } + /// /// Generate a LLUUID from a string /// @@ -131,12 +172,12 @@ namespace libsecondlife { try { - result = new LLUUID(val); + result = Parse(val); return true; } catch (Exception) { - result = new LLUUID(0); + result = null; return false; } } @@ -150,6 +191,64 @@ namespace libsecondlife return new LLUUID(Guid.NewGuid().ToByteArray(), 0); } + /// + /// Required implementation for XML serialization + /// + /// null + public System.Xml.Schema.XmlSchema GetSchema() + { + return null; + } + + /// + /// Deserializes an XML UUID + /// + /// XmlReader containing the UUID to deserialize + public void ReadXml(System.Xml.XmlReader reader) + { + string val = reader.ReadString(); + + if (val.Length > 0) + { + if (val.Length == 36) val = val.Replace("-", ""); + + if (val.Length != 32) throw new Exception("Malformed data passed to LLUUID constructor: " + val); + + for (int i = 0; i < 16; ++i) + { + data[i] = Convert.ToByte(val.Substring(i * 2, 2), 16); + } + } + + reader.Skip(); + } + + /// + /// Serialize this UUID to XML + /// + /// XmlWriter to serialize to + public void WriteXml(System.Xml.XmlWriter writer) + { + if (this != LLUUID.Zero) + writer.WriteString(this.ToString()); + } + + /// + /// Return a hash code for this UUID, used by .NET for hash tables + /// + /// An integer composed of all the UUID bytes XORed together + public override int GetHashCode() + { + int hash = data[0]; + + for (int i = 1; i < 16; i++) + { + hash ^= data[i]; + } + + return hash; + } + /// /// Comparison function /// @@ -162,7 +261,12 @@ namespace libsecondlife LLUUID uuid = (LLUUID)o; - return uuid.id.Equals(id); + for (int i = 0; i < 16; ++i) + { + if (Data[i] != uuid.Data[i]) return false; + } + + return true; } /// @@ -171,21 +275,38 @@ namespace libsecondlife /// First LLUUID for comparison /// Second LLUUID for comparison /// True if the UUIDs are byte for byte equal, otherwise false - public static bool operator==(LLUUID lhs, LLUUID rhs) - { - return lhs.Equals(rhs); - } - + public static bool operator==(LLUUID lhs, LLUUID rhs) + { + // If both are null, or both are same instance, return true + if (System.Object.ReferenceEquals(lhs, rhs)) + { + return true; + } + + // If one is null, but not both, return false. + if (((object)lhs == null) || ((object)rhs == null)) + { + return false; + } + + for (int i = 0; i < 16; ++i) + { + if (lhs.Data[i] != rhs.Data[i]) return false; + } + + return true; + } + /// /// Not equals operator /// /// First LLUUID for comparison /// Second LLUUID for comparison /// True if the UUIDs are not equal, otherwise true - public static bool operator!=(LLUUID lhs, LLUUID rhs) - { - return !(lhs == rhs); - } + public static bool operator!=(LLUUID lhs, LLUUID rhs) + { + return !(lhs == rhs); + } /// /// XOR operator @@ -211,10 +332,10 @@ namespace libsecondlife /// A UUID in string form. Case insensitive, /// hyphenated or non-hyphenated /// A UUID built from the string representation - public static implicit operator LLUUID(string val) - { - return new LLUUID(val); - } + public static implicit operator LLUUID(string val) + { + return new LLUUID(val); + } /// /// Get a string representation of this UUID @@ -222,10 +343,17 @@ namespace libsecondlife /// A string representation of this UUID, lowercase and /// without hyphens /// 11f8aa9cb0714242836b13b7abe0d489 - public override string ToString() - { - return id.ToString("N"); - } + public override string ToString() + { + string uuid = String.Empty; + + for (int i = 0; i < 16; ++i) + { + uuid += Data[i].ToString("x2"); + } + + return uuid; + } /// /// Get a hyphenated string representation of this UUID @@ -233,21 +361,27 @@ namespace libsecondlife /// A string representation of this UUID, lowercase and /// with hyphens /// 11f8aa9c-b071-4242-836b-13b7abe0d489 - public string ToStringHyphenated() - { - return id.ToString("D"); - } - - public override int GetHashCode() - { - return id.GetHashCode(); - } - + public string ToStringHyphenated() + { + string uuid = String.Empty; + + for (int i = 0; i < 16; ++i) + { + uuid += Data[i].ToString("x2"); + } + uuid = uuid.Insert(20, "-"); + uuid = uuid.Insert(16, "-"); + uuid = uuid.Insert(12, "-"); + uuid = uuid.Insert(8, "-"); + + return uuid; + } + /// /// An LLUUID with a value of all zeroes /// public static readonly LLUUID Zero = new LLUUID(); - } + } /// /// A three-dimensional vector with floating-point values