Ejemplo n.º 1
0
        /// <summary>
        /// Returns true if parsing is successful.
        /// </summary>
        /// <param name="array">Array to parse.</param>
        /// <param name="offset">Array offset at which to start.</param>
        /// <param name="result">Out. Result of parse.</param>
        public static bool TryParse(byte[] array, int offset, out KeyValuePacket result)
        {
            System.Exception ex;
            int newOffset;

            return(TryParse(array, offset, out result, out newOffset, out ex));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a value indicating whether this instance's Key is equal to the specified TagItem object's Key.
        /// </summary>
        /// <param name="obj">A TagItem object to compare to this instance.</param>
        public override bool Equals(object obj)
        {
            if (obj is KeyValuePacket)
            {
                KeyValuePacket otherKeyValuePacket = (KeyValuePacket)obj;
                return(this._key.Equals(otherKeyValuePacket._key));
            }

            string otherString = obj as string;

            if (obj != null)
            {
                return(this._key.Equals(otherString));
            }

            return(false);
        }
Ejemplo n.º 3
0
        private static bool TryParse(byte[] array, int offset, out KeyValuePacket result, out int newOffset, out System.Exception exception)
        {
            if (offset + 16 > array.Length)
            {
                result    = new KeyValuePacket();
                newOffset = offset;
                exception = new System.FormatException("Not enough bytes to start.");
                return(false);
            }

            byte[] baHeader = new byte[4];
            System.Array.Copy(array, offset + 0, baHeader, 0, 4);
            if ((baHeader[0] != 0x4B) || (baHeader[1] != 0x56) || (baHeader[2] != 0x50) || (baHeader[3] != 0x01))
            {
                result    = new KeyValuePacket();
                newOffset = offset;
                exception = new System.FormatException("Invalid header.");
                return(false);
            }

            byte[] baKeyLengthBE = new byte[4];
            System.Array.Copy(array, offset + 4, baKeyLengthBE, 0, 4);
            byte[] keyLengthLE   = new byte[] { baKeyLengthBE[3], baKeyLengthBE[2], baKeyLengthBE[1], baKeyLengthBE[0] };
            int    keyLength     = System.BitConverter.ToInt32(keyLengthLE, 0);
            int    realKeyLength = keyLength;

            if (realKeyLength == -1)
            {
                realKeyLength = 0;
            }

            byte[] baValueLengthBE = new byte[4];
            System.Array.Copy(array, offset + 8, baValueLengthBE, 0, 4);
            byte[] valueLengthLE   = new byte[] { baValueLengthBE[3], baValueLengthBE[2], baValueLengthBE[1], baValueLengthBE[0] };
            int    valueLength     = System.BitConverter.ToInt32(valueLengthLE, 0);
            int    realValueLength = valueLength;

            if (realValueLength == -1)
            {
                realValueLength = 0;
            }

            if (offset + 16 + realKeyLength + realValueLength > array.Length)
            {
                result    = new KeyValuePacket();
                newOffset = offset;
                exception = new System.FormatException("Not enough bytes.");
                return(false);
            }

            byte[] baXor = new byte[4];
            System.Array.Copy(array, offset + 12, baXor, 0, 4);
            if ((baXor[0] != (byte)(baHeader[0] ^ baKeyLengthBE[1] ^ baValueLengthBE[2])) || (baXor[1] != (byte)(baHeader[1] ^ baKeyLengthBE[2] ^ baValueLengthBE[3])) || (baXor[2] != (byte)(baHeader[2] ^ baKeyLengthBE[3] ^ baValueLengthBE[0])) || (baXor[3] != (byte)(baHeader[3] ^ baKeyLengthBE[0] ^ baValueLengthBE[1])))
            {
                result    = new KeyValuePacket();
                newOffset = offset;
                exception = new System.FormatException("Invalid checksum.");
                return(false);
            }

            string key;

            if (keyLength == -1)
            {
                key = null;
            }
            else
            {
                key = UTF8Encoding.UTF8.GetString(array, offset + 16 + 0, realKeyLength);
            }

            byte[] value;
            if (valueLength == -1)
            {
                value = null;
            }
            else
            {
                value = new byte[realValueLength];
                System.Array.Copy(array, offset + 16 + realKeyLength, value, 0, realValueLength);
            }


            result    = new KeyValuePacket(key, value);
            newOffset = offset + 16 + realKeyLength + realValueLength;
            exception = null;
            return(true);
        }