Esempio n. 1
0
        public Int64 read_integer()
        {
            if (this.value >= 0)
            {
                return((Int64)(this.value));
            }
            else
            {
                UInt32 sz = this.read_dword();
                if (sz == sizeof(UInt32))
                {
                    UInt64 v = this.expand64(this.read_dword());
                    return((Int64)v);
                }
                else if (sz == sizeof(UInt64))
                {
                    UInt32 low = this.read_dword();
                    UInt32 hi  = this.read_dword();
                    UInt64 v   = (UInt64)low | (UInt64)hi << 32;
                    return((Int64)v);
                }
                else
                {
                    SprotoTypeSize.error("read invalid integer size (" + sz + ")");
                }
            }

            return(0);
        }
Esempio n. 2
0
        public void write_integer(Int64 integer, int tag)
        {
            Int64 vh    = integer >> 31;
            int   sz    = (vh == 0 || vh == -1)?(sizeof(UInt32)):(sizeof(UInt64));
            int   value = 0;

            if (sz == sizeof(UInt32))
            {
                UInt32 v = (UInt32)integer;
                if (v < 0x7fff)
                {
                    value = (int)((v + 1) * 2);
                    sz    = 2;
                }
                else
                {
                    sz = this.encode_integer(v);
                }
            }
            else if (sz == sizeof(UInt64))
            {
                UInt64 v = (UInt64)integer;
                sz = this.encode_uint64(v);
            }
            else
            {
                SprotoTypeSize.error("invaild integer size.");
            }

            this.write_tag(tag, value);
        }
Esempio n. 3
0
        public List <byte[]> read_binary_list()
        {
            UInt32 sz = this.read_array_size();

            List <byte[]> bytes_list = new List <byte[]> ();

            for (UInt32 i = 0; sz > 0; i++)
            {
                if (sz < SprotoTypeSize.sizeof_length)
                {
                    SprotoTypeSize.error("error array size.");
                }

                UInt32 hsz = this.read_dword();
                sz -= (UInt32)SprotoTypeSize.sizeof_length;

                if (hsz > sz)
                {
                    SprotoTypeSize.error("error array object.");
                }

                byte[] buffer = new byte[hsz];
                this.reader.Read(buffer, 0, buffer.Length);

                bytes_list.Add(buffer);
                sz -= hsz;
            }
            return(bytes_list);
        }
Esempio n. 4
0
        public List <string> read_string_list()
        {
            UInt32 sz = this.read_array_size();

            List <string> string_list = new List <string> ();

            for (UInt32 i = 0; sz > 0; i++)
            {
                if (sz < SprotoTypeSize.sizeof_length)
                {
                    SprotoTypeSize.error("error array size.");
                }

                UInt32 hsz = this.read_dword();
                sz -= (UInt32)SprotoTypeSize.sizeof_length;

                if (hsz > sz)
                {
                    SprotoTypeSize.error("error array object.");
                }

                byte[] buffer = new byte[hsz];
                this.reader.Read(buffer, 0, buffer.Length);
                string v = System.Text.Encoding.UTF8.GetString(buffer);

                string_list.Add(v);
                sz -= hsz;
            }

            return(string_list);
        }
Esempio n. 5
0
        private T read_element <T>(SprotoTypeReader reader, UInt32 sz, out UInt32 read_size) where T : SprotoTypeBase, new()
        {
            read_size = 0;
            if (sz < SprotoTypeSize.sizeof_length)
            {
                SprotoTypeSize.error("error array size.");
            }

            UInt32 hsz = this.read_dword();

            sz        -= (UInt32)SprotoTypeSize.sizeof_length;
            read_size += (UInt32)SprotoTypeSize.sizeof_length;

            if (hsz > sz)
            {
                SprotoTypeSize.error("error array object.");
            }

            reader.Init(this.reader.Buffer, this.reader.Offset, (int)hsz);
            this.reader.Seek(this.reader.Position + (int)hsz);

            T obj = new T();

            obj.init(reader);

            read_size += hsz;
            return(obj);
        }
        public List <T> read_obj_list <T>() where T : SprotoTypeBase, new()
        {
            UInt32 sz = this.read_array_size();

            List <T> obj_list = new List <T> ();

            for (UInt32 i = 0; sz > 0; i++)
            {
                if (sz < SprotoTypeSize.sizeof_length)
                {
                    SprotoTypeSize.error("error array size.");
                }

                UInt32 hsz = this.read_dword();
                sz -= (UInt32)SprotoTypeSize.sizeof_length;

                if (hsz > sz)
                {
                    SprotoTypeSize.error("error array object.");
                }

                SprotoTypeReader reader = new SprotoTypeReader(this.reader.Buffer, this.reader.Offset, hsz);
                this.reader.Seek(this.reader.Position + hsz);

                T obj = new T();
                obj.init(reader);
                obj_list.Add(obj);

                sz -= hsz;
            }

            return(obj_list);
        }
Esempio n. 7
0
        public List <double> read_double_list()
        {
            List <double> double_list = null;

            UInt32 sz = this.read_array_size();

            if (sz == 0)
            {
                return(new List <double>());
            }

            int len = this.reader.ReadByte();

            sz--;

            if (len != 8)
            {
                SprotoTypeSize.error("invalid intlen (" + len + ")");
            }

            double_list = new List <double>();
            UnionValue u = new UnionValue();

            for (int i = 0; i < sz / 8; i++)
            {
                u.integer_v = this.read_uint64();
                double_list.Add(u.real_v);
            }
            return(double_list);
        }
Esempio n. 8
0
 private void check()
 {
     if (this.pos > this.size || this.begin > this.pos)
     {
         SprotoTypeSize.error("invalid pos.");
     }
 }
Esempio n. 9
0
        public byte[] unpack(byte[] data, int len = 0)
        {
            this.clear();

            len = (len == 0)?(data.Length):(len);
            int srcsz = len;

            while (srcsz > 0)
            {
                byte header = data [len - srcsz];
                --srcsz;

                if (header == 0xff)
                {
                    if (srcsz < 0)
                    {
                        SprotoTypeSize.error("invalid unpack stream.");
                    }

                    int n = (data [len - srcsz] + 1) * 8;

                    if (srcsz < n + 1)
                    {
                        SprotoTypeSize.error("invalid unpack stream.");
                    }

                    this.buffer.Write(data, len - srcsz + 1, n);
                    srcsz -= n + 1;
                }
                else
                {
                    for (int i = 0; i < 8; i++)
                    {
                        int nz = (header >> i) & 1;
                        if (nz == 1)
                        {
                            if (srcsz < 0)
                            {
                                SprotoTypeSize.error("invalid unpack stream.");
                            }

                            this.buffer.WriteByte(data [len - srcsz]);
                            --srcsz;
                        }
                        else
                        {
                            this.buffer.WriteByte(0);
                        }
                    }
                }
            }

            byte[] unpack_data = new byte[this.buffer.Position];
            this.buffer.Seek(0, SeekOrigin.Begin);

            this.buffer.Read(unpack_data, 0, unpack_data.Length);
            return(unpack_data);;
        }
Esempio n. 10
0
        private void fill_size(int sz)
        {
            if (sz < 0)
            {
                SprotoTypeSize.error("fill invaild size.");
            }

            this.write_uint32((UInt32)sz);
        }
Esempio n. 11
0
 public bool read_boolean()
 {
     if (this.value < 0)
     {
         SprotoTypeSize.error("read invalid boolean.");
         return(false);
     }
     else
     {
         return((this.value == 0)?(false):(true));
     }
 }
Esempio n. 12
0
        public List <Int64> read_integer_list()
        {
            List <Int64> integer_list = null;

            UInt32 sz = this.read_array_size();

            if (sz == 0)
            {
                return(new List <Int64> ());
            }

            int len = this.reader.ReadByte();

            sz--;

            if (len == sizeof(UInt32))
            {
                if (sz % sizeof(UInt32) != 0)
                {
                    SprotoTypeSize.error("error array size(" + sz + ")@sizeof(Uint32)");
                }

                integer_list = new List <Int64> ();
                for (int i = 0; i < sz / sizeof(UInt32); i++)
                {
                    UInt64 v = this.expand64(this.read_dword());
                    integer_list.Add((Int64)v);
                }
            }
            else if (len == sizeof(UInt64))
            {
                if (sz % sizeof(UInt64) != 0)
                {
                    SprotoTypeSize.error("error array size(" + sz + ")@sizeof(Uint64)");
                }

                integer_list = new List <Int64> ();
                for (int i = 0; i < sz / sizeof(UInt64); i++)
                {
                    UInt32 low = this.read_dword();
                    UInt32 hi  = this.read_dword();
                    UInt64 v   = (UInt64)low | (UInt64)hi << 32;
                    integer_list.Add((Int64)v);
                }
            }
            else
            {
                SprotoTypeSize.error("error intlen(" + len + ")");
            }

            return(integer_list);
        }
Esempio n. 13
0
        public double read_double()
        {
            UInt32 sz = this.read_dword();

            if (sz == 8)
            {
                UnionValue v = new UnionValue();
                v.integer_v = this.read_uint64();
                return(v.real_v);
            }
            else
            {
                SprotoTypeSize.error("read invalid double size (" + sz + ")");
            }
            return(0.0);
        }
Esempio n. 14
0
        private UInt32 read_array_size()
        {
            if (this.value >= 0)
            {
                SprotoTypeSize.error("invalid array value.");
            }

            UInt32 sz = this.read_dword();

            if (sz < 0)
            {
                SprotoTypeSize.error("error array size(" + sz + ")");
            }

            return(sz);
        }
Esempio n. 15
0
        private void init()
        {
            this.fn = this.read_word();

            int header_length = SprotoTypeSize.sizeof_header + this.fn * SprotoTypeSize.sizeof_field;

            this.begin_data_pos = header_length;
            this.cur_field_pos  = this.reader.Position;

            if (this.reader.Length < header_length)
            {
                SprotoTypeSize.error("invalid decode header.");
            }

            this.reader.Seek(this.begin_data_pos);
        }
Esempio n. 16
0
        static private SprotoTypeBase _gen(Dictionary <int, KeyValuePair <Type, typeFunc> > dictionary, int tag, byte[] buffer, int offset = 0)
        {
            KeyValuePair <Type, typeFunc> v;

            if (dictionary.TryGetValue(tag, out v))
            {
                SprotoTypeBase obj = v.Value(buffer, offset);
                if (obj.GetType() != v.Key)
                {
                    SprotoTypeSize.error("sproto type: " + obj.GetType().ToString() + "not is expected. [" + v.Key.ToString() + "]");
                }
                return(obj);
            }

            return(null);
        }
Esempio n. 17
0
        static private void _set <T>(Dictionary <int, KeyValuePair <Type, typeFunc> > dictionary, int tag)
            where T : SprotoTypeBase, new()
        {
            if (dictionary.ContainsKey(tag))
            {
                SprotoTypeSize.error("redefine tag: " + tag);
            }

            typeFunc _func = delegate(byte[] buffer, int offset) {
                T obj = new T();
                obj.init(buffer, offset);
                return(obj);
            };

            KeyValuePair <Type, typeFunc> kv = new KeyValuePair <Type, typeFunc> (typeof(T), _func);

            dictionary.Add(tag, kv);
        }
Esempio n. 18
0
        private void write_tag(int tag, int value)
        {
            int stag = tag - this.lasttag - 1;

            if (stag > 0)
            {
                // skip tag
                stag = (stag - 1) * 2 + 1;
                if (stag > 0xffff)
                {
                    SprotoTypeSize.error("tag is too big.");
                }

                this.write_header_record(stag);
            }

            this.write_header_record(value);
            this.lasttag = tag;
        }
Esempio n. 19
0
        private void _expand(int sz = 0)
        {
            if (this.size - this.pos < sz)
            {
                long bak_sz = this.size;
                while (this.size - this.pos < sz)
                {
                    this.size = this.size * 2;
                }

                if (this.size >= SprotoTypeSize.encode_max_size)
                {
                    SprotoTypeSize.error("object is too large (>" + SprotoTypeSize.encode_max_size + ")");
                }

                byte[] new_buffer = new byte[this.size];
                for (long i = 0; i < bak_sz; i++)
                {
                    new_buffer [i] = this.buffer [i];
                }
                this.buffer = new_buffer;
            }
        }
Esempio n. 20
0
        public void write_integer(List <Int64> integer_list, int tag)
        {
            if (integer_list == null || integer_list.Count <= 0)
            {
                return;
            }

            int sz_pos = this.data.Position;

            this.data.Seek(sz_pos + SprotoTypeSize.sizeof_length, SeekOrigin.Begin);

            int begin_pos = this.data.Position;
            int intlen    = sizeof(UInt32);

            this.data.Seek(begin_pos + 1, SeekOrigin.Begin);

            for (int index = 0; index < integer_list.Count; index++)
            {
                Int64 v  = integer_list [index];
                Int64 vh = v >> 31;
                int   sz = (vh == 0 || vh == -1)?(sizeof(UInt32)):(sizeof(UInt64));

                if (sz == sizeof(UInt32))
                {
                    this.write_uint32((UInt32)v);
                    if (intlen == sizeof(UInt64))
                    {
                        bool is_negative = ((v & 0x80000000) == 0) ? (false) : (true);
                        this.write_uint32_to_uint64_sign(is_negative);
                    }
                }
                else if (sz == sizeof(UInt64))
                {
                    if (intlen == sizeof(UInt32))
                    {
                        this.data.Seek(begin_pos + 1, SeekOrigin.Begin);
                        for (int i = 0; i < index; i++)
                        {
                            UInt64 value = (UInt64)(integer_list[i]);
                            this.write_uint64(value);
                        }
                        intlen = sizeof(UInt64);
                    }
                    this.write_uint64((UInt64)v);
                }
                else
                {
                    SprotoTypeSize.error("invalid integer size(" + sz + ")");
                }
            }

            // fill integer size
            int cur_pos = this.data.Position;

            this.data.Seek(begin_pos, SeekOrigin.Begin);
            this.data.WriteByte((byte)intlen);

            // fill array size
            int size = (int)(cur_pos - begin_pos);

            this.data.Seek(sz_pos, SeekOrigin.Begin);
            this.fill_size(size);

            this.data.Seek(cur_pos, SeekOrigin.Begin);
            this.write_tag(tag, 0);
        }
Esempio n. 21
0
        public byte[] pack(byte[] data, int len = 0)
        {
            this.clear();

            len = (len == 0)?(data.Length):(len);
            byte[] ff_src      = null;
            int    ff_srcstart = 0;
            long   ff_desstart = 0;

            int ff_n = 0;

            byte[] src    = data;
            int    offset = 0;

            int srcsz = len;

            for (int i = 0; i < srcsz; i += 8)
            {
                offset = i;

                int padding = i + 8 - srcsz;
                if (padding > 0)
                {
                    for (int j = 0; j < 8 - padding; j++)
                    {
                        this.tmp [j] = src [i + j];
                    }
                    for (int j = 0; j < padding; j++)
                    {
                        this.tmp [7 - j] = 0;
                    }

                    src    = this.tmp;
                    offset = 0;
                }

                int n = this.pack_seg(src, offset, ff_n);
                if (n == 10)
                {
                    // first FF
                    ff_src      = src;
                    ff_srcstart = offset;
                    ff_desstart = this.buffer.Position;
                    ff_n        = 1;
                }
                else if (n == 8 && ff_n > 0)
                {
                    ++ff_n;
                    if (ff_n == 256)
                    {
                        this.write_ff(ff_src, ff_srcstart, ff_desstart, 256 * 8);
                        ff_n = 0;
                    }
                }
                else
                {
                    if (ff_n > 0)
                    {
                        this.write_ff(ff_src, ff_srcstart, ff_desstart, ff_n * 8);
                        ff_n = 0;
                    }
                }

                this.buffer.Seek(n, SeekOrigin.Current);
            }

            if (ff_n == 1)
            {
                this.write_ff(ff_src, ff_srcstart, ff_desstart, 8);
            }
            else if (ff_n > 1)
            {
                this.write_ff(ff_src, ff_srcstart, ff_desstart, ff_src.Length - ff_srcstart);
            }

            long maxsz = (data.LongLength + 2047) / 2048 * 2 + data.LongLength;

            if (maxsz < this.buffer.Position)
            {
                SprotoTypeSize.error("packing error, return size=" + this.buffer.Position);
            }

            byte[] pack_buffer = new byte[this.buffer.Position];
            this.buffer.Seek(0, SeekOrigin.Begin);
            this.buffer.Read(pack_buffer, 0, pack_buffer.Length);

            return(pack_buffer);
        }