示例#1
0
        /* Private API */

        internal void SerializeTo(INameTableProvider nameTable, byte[] buffer, int bufferPos)
        {
            //Write count
            BitConverter.GetBytes((int)values.Count).CopyTo(buffer, bufferPos);
            bufferPos += 4;

            //Loop through
            foreach (var v in values)
            {
                //Lookup key name and write it's index
                short keyIndex = nameTable.GetNameTableIndex(v.Key);
                BitConverter.GetBytes(keyIndex).CopyTo(buffer, bufferPos + 0);

                //Get the type ID and write it
                short typeId = v.Value.SerTypeId;
                BitConverter.GetBytes(typeId).CopyTo(buffer, bufferPos + 2);

                //Get the length and write it
                int length = v.Value.GetLength();
                BitConverter.GetBytes(length).CopyTo(buffer, bufferPos + 4);

                //Now serialize to the buffer
                v.Value.Serialize(buffer, bufferPos + ENTRY_HEADER_BYTES, nameTable);

                //Update
                bufferPos += length + ENTRY_HEADER_BYTES;
            }
        }
 public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
 {
     for (int i = 0; i < value.Length; i++)
     {
         BitConverter.GetBytes(value[i]).CopyTo(buffer, bufferPos + (4 * i));
     }
 }
        /// <summary>
        /// Deserializes this object and returns the number of bytes read
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="bufferIndex"></param>
        /// <returns></returns>
        internal int DeserializeObject(INameTableProvider nameTable, byte[] buffer, int bufferIndex)
        {
            //Make counter for offset from buffer index. This'll be the length we eventually return
            int offset = 0;

            //Read the number of objects
            int objectCount = BitConverter.ToInt32(buffer, bufferIndex + offset);

            offset += 4;

            //Read each object
            for (int i = 0; i < objectCount; i++)
            {
                //Read object data
                short keyIndex = BitConverter.ToInt16(buffer, bufferIndex + offset);
                offset += 2;
                short typeId = BitConverter.ToInt16(buffer, bufferIndex + offset);
                offset += 2;
                int length = BitConverter.ToInt32(buffer, bufferIndex + offset);
                offset += 4;

                //Deserialize
                object value = DeserializeSingleObject(nameTable, buffer, bufferIndex + offset, typeId, length);
                offset += length;

                //Add
                Add(nameTable.GetNameTableValue(keyIndex), value);
            }

            return(offset);
        }
        private object DeserializeSingleObject(INameTableProvider nameTable, byte[] buffer, int bufferIndex, short typeId, int length)
        {
            switch (typeId)
            {
            case WriteCommitObject.SER_TYPE_INT8: return(buffer[bufferIndex]);

            case WriteCommitObject.SER_TYPE_INT16: return(BitConverter.ToInt16(buffer, bufferIndex));

            case WriteCommitObject.SER_TYPE_INT32: return(BitConverter.ToInt32(buffer, bufferIndex));

            case WriteCommitObject.SER_TYPE_INT64: return(BitConverter.ToInt64(buffer, bufferIndex));

            case WriteCommitObject.SER_TYPE_FLOAT: return(BitConverter.ToSingle(buffer, bufferIndex));

            case WriteCommitObject.SER_TYPE_INT8_ARRAY: return(DeserializeFixedArray <byte>(length, 1, (int offset) => buffer[bufferIndex + offset]));

            case WriteCommitObject.SER_TYPE_INT16_ARRAY: return(DeserializeFixedArray <short>(length, 2, (int offset) => BitConverter.ToInt16(buffer, bufferIndex + offset)));

            case WriteCommitObject.SER_TYPE_INT32_ARRAY: return(DeserializeFixedArray <int>(length, 4, (int offset) => BitConverter.ToInt32(buffer, bufferIndex + offset)));

            case WriteCommitObject.SER_TYPE_INT64_ARRAY: return(DeserializeFixedArray <long>(length, 8, (int offset) => BitConverter.ToInt64(buffer, bufferIndex + offset)));

            case WriteCommitObject.SER_TYPE_FLOAT_ARRAY: return(DeserializeFixedArray <float>(length, 4, (int offset) => BitConverter.ToSingle(buffer, bufferIndex + offset)));

            case WriteCommitObject.SER_TYPE_STRING: return(Encoding.UTF8.GetString(buffer, bufferIndex, length));

            case WriteCommitObject.SER_TYPE_STRING_ARRAY: return(DeserializeDynamicArray <string>(buffer, bufferIndex, (int offset, out int len) =>
                {
                    len = BitConverter.ToInt32(buffer, bufferIndex + offset) + 4;
                    return Encoding.UTF8.GetString(buffer, bufferIndex + offset + 4, len - 4);
                }));

            case WriteCommitObject.SER_TYPE_OBJECT:
                DatabaseObject child = new DatabaseObject();
                child.DeserializeObject(nameTable, buffer, bufferIndex);
                return(child);

            case WriteCommitObject.SER_TYPE_OBJECT_ARRAY: return(DeserializeDynamicArray <DatabaseObject>(buffer, bufferIndex, (int offset, out int len) =>
                {
                    DatabaseObject child = new DatabaseObject();
                    len = child.DeserializeObject(nameTable, buffer, bufferIndex + offset);
                    return child;
                }));

            case WriteCommitObject.SER_TYPE_BOOL: return(buffer[bufferIndex] == 0x01);

            case WriteCommitObject.SER_TYPE_BOOL_ARRAY: return(DeserializeFixedArray <bool>(length, 1, (int offset) => buffer[bufferIndex + offset] == 0x01));

            case WriteCommitObject.SER_TYPE_DOUBLE: return(BitConverter.ToDouble(buffer, bufferIndex));

            case WriteCommitObject.SER_TYPE_DOUBLE_ARRAY: return(DeserializeFixedArray <double>(length, 8, (int offset) => BitConverter.ToDouble(buffer, bufferIndex + offset)));

            case WriteCommitObject.SER_TYPE_STRING_ENUM: return(nameTable.GetNameTableValue(BitConverter.ToInt16(buffer, bufferIndex)));

            default: throw new Exception("Unknown Type ID!");
            }
        }
示例#5
0
 public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
 {
     if (value)
     {
         buffer[bufferPos] = 0x01;
     }
     else
     {
         buffer[bufferPos] = 0x00;
     }
 }
示例#6
0
        public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
        {
            //Ensure this is inside the possible options.
            //We check this because otherwise an attacker could flood our name table and cause slowdowns
            string realValue = value;

            if (!possibleOptions.Contains(value))
            {
                realValue = possibleOptions[0];
            }

            //Look up name table entry
            short entry = nameTable.GetNameTableIndex(realValue);

            //Write
            BitConverter.GetBytes(entry).CopyTo(buffer, bufferPos);
        }
        public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
        {
            //Write string count
            BitConverter.GetBytes((int)value.Length).CopyTo(buffer, bufferPos);
            int offset = 4;

            //Write each
            for (int i = 0; i < value.Length; i += 1)
            {
                //Write string length
                BitConverter.GetBytes(valueLengths[i]).CopyTo(buffer, bufferPos + offset);
                offset += 4;

                //Write string
                Encoding.UTF8.GetBytes(value[i]).CopyTo(buffer, bufferPos + offset);
                offset += valueLengths[i];
            }
        }
示例#8
0
        public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
        {
            //Write total length
            BitConverter.GetBytes((int)value.Length).CopyTo(buffer, bufferPos);
            int offset = 4;

            //Write each
            foreach (var v in value)
            {
                //Serialize
                v.SerializeTo(nameTable, buffer, bufferPos + offset);

                //Write
                offset += v.GetLength();
            }

            //Raise exception if this failed
            if (offset != GetLength())
            {
                throw new Exception("Fatal error serializing OBJECT ARRAY data. The length expected did not match the length used.");
            }
        }
示例#9
0
 public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
 {
     Array.Copy(value, 0, buffer, bufferPos, value.Length);
 }
示例#10
0
 public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
 {
     BitConverter.GetBytes(value).CopyTo(buffer, bufferPos);
 }
示例#11
0
 public abstract void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable);
 public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
 {
     Encoding.UTF8.GetBytes(value).CopyTo(buffer, bufferPos);
 }
示例#13
0
 public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
 {
     buffer[bufferPos] = value;
 }
示例#14
0
 public override void Serialize(byte[] buffer, int bufferPos, INameTableProvider nameTable)
 {
     value.SerializeTo(nameTable, buffer, bufferPos);
 }