Example #1
0
        public bool Write(ref Buffer buffer)
        {
            if (!buffer.HasSpace(Size))
            {
                return(false);
            }

            buffer.WriteBytes <byte>((byte)DataType.DATA_FIELD);
            buffer.WriteBytes(name);
            buffer.WriteBytes <byte>((byte)dataType);            //write data type

            if (dataType != DataType.DATA_STRING)
            {
                for (int i = 0; i < SizeOf(dataType); i++)
                {
                    buffer.WriteBytes <byte>(data[i]);
                }
            }
            else
            {
                short len = Reader.ReadBytesShort(data, 0);
                len += 2;

                for (int i = 0; i < len; i++)
                {
                    buffer.WriteBytes <byte>(data[i]);
                }
            }

            return(true);
        }
Example #2
0
        public bool Write(ref Buffer buffer)
        {
            if (!buffer.HasSpace(Size))
            {
                return(false);
            }

            if (databases.Count > 255)
            {
                throw new OverflowException("Too many databases!");
            }

            buffer.WriteBytes <ushort>(Global.MAGIC_NUMBER);
            buffer.WriteBytes <byte>((byte)databases.Count);

            uint offset = (uint)(sizeof(short) + sizeof(byte) + (sizeof(uint) * databases.Count));

            for (int i = 0; i < databases.Count; i++)
            {
                buffer.WriteBytes <uint>(offset);

                offset += (uint)databases[i].Size;
            }

            foreach (Database db in databases)
            {
                db.Write(ref buffer);
            }

            return(true);
        }
Example #3
0
        public bool Write(ref Buffer buffer)
        {
            if (!buffer.HasSpace(Size))
            {
                return(false);
            }

            buffer.WriteBytes <byte>((byte)DataType.DATA_ARRAY);
            buffer.WriteBytes(name);
            buffer.WriteBytes <byte>((byte)dataType);
            buffer.WriteBytes <uint>(count);

            uint s;

            if (dataType != DataType.DATA_STRING)
            {
                s = SizeOf(dataType) * count;
            }
            else
            {
                s = size;
            }

            buffer.Copy(data, s);

            return(true);
        }
Example #4
0
        public bool Write(ref Buffer buffer)
        {
            if (!buffer.HasSpace(Size))
            {
                return(false);
            }

            if (Fields.Count > 65535)
            {
                throw new OverflowException("Too many fields!");
            }
            if (Arrays.Count > 65535)
            {
                throw new OverflowException("Too many arrays!");
            }

            buffer.WriteBytes <byte>((byte)DataType.DATA_OBJECT);
            buffer.WriteBytes(name);
            buffer.WriteBytes <ushort>((ushort)Fields.Count);

            foreach (Field field in Fields)
            {
                field.Write(ref buffer);
            }

            buffer.WriteBytes <ushort>((ushort)Arrays.Count);

            foreach (Array array in Arrays)
            {
                array.Write(ref buffer);
            }

            return(true);
        }
Example #5
0
        public bool Write(ref Buffer buffer)
        {
            if (!buffer.HasSpace((uint)Size))
            {
                return(false);
            }

            buffer.WriteBytes <ushort>((ushort)version);

            if (version == Global.Version.VERSION_INVALID)
            {
                throw new ArgumentOutOfRangeException("version", "Invalid database version!");
            }

            switch (version)
            {
            case Global.Version.VERSION_1_0:
                if (Objects.Count > 65536)
                {
                    throw new OverflowException("Too many objects!");
                }
                if (this.Size > 4294967296)
                {
                    throw new OverflowException("Database size is too big!");                                        // 2^32, maximum database size
                }
                buffer.WriteBytes(name);
                buffer.WriteBytes <uint>((uint)Size);
                buffer.WriteBytes <ushort>((ushort)Objects.Count);

                foreach (Object obj in Objects)
                {
                    obj.Write(ref buffer);
                }

                break;

            case Global.Version.VERSION_2_0:
                if (Objects.Count > 65536)
                {
                    throw new OverflowException("Too many objects!");
                }
                if (this.Size > 4294967296)
                {
                    throw new OverflowException("Database size is too big!");                                        // 2^32, maximum database size
                }
                uint size = (uint)this.Size - sizeof(short) - sizeof(short) - (uint)name.Length - sizeof(uint);

                Buffer tempBuffer = new Buffer(size);

                buffer.WriteBytes(name);

                tempBuffer.WriteBytes((uint)this.Size);
                tempBuffer.WriteBytes((ushort)Objects.Count);

                foreach (Object obj in Objects)
                {
                    obj.Write(ref tempBuffer);
                }

                uint checksum = crc32(tempBuffer.Data, size);

                buffer.WriteBytes(checksum);
                buffer.Copy(ref tempBuffer);

                break;

            default:
                throw new ArgumentOutOfRangeException("version", "Invalid database version!");
            }

            return(true);
        }