Esempio n. 1
0
        public static void Discard(ReadBuffer rb)
        {
            if (rb.Left() < 1)
            {
                return;
            }
            byte t = rb.MoveNext();

            // positive fixint
            if (t < 0x80)
            {
                return;
            }
            // fixstr
            if (t > 0x9f && t < 0xc0)
            {
                int slen = t & 0x1f;
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackString fixed str failed");
                    return;
                }
                rb.MoveOffset(slen);
                return;
            }
            // fixmap
            if (t > 0x7f && t < 0x90)
            {
                int maplen = t & 0xf;
                UnpackDiscard(rb, maplen * 2);
                return;
            }
            // fixarray
            if (t > 0x8f && t < 0xa0)
            {
                int arylen = t & 0xf;
                UnpackDiscard(rb, arylen);
                return;
            }
            // negative fixint
            if (t > 0xdf)
            {
                return;
            }
            switch (t)
            {
            case 0xc0:    // nil
                return;

            case 0xc2:    // false
                return;

            case 0xc3:    // true
                return;

            case 0xcc:    // uint8
                rb.MoveNext();
                return;

            case 0xcd:    // uint16
            {
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackInteger uint16 failed");
                    return;
                }
                rb.MoveOffset(2);
                return;
            }

            case 0xce:    // uint32
            {
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackInteger uint failed");
                    return;
                }
                rb.MoveOffset(4);
                return;
            }

            case 0xcf:    // uint64
            {
                if (rb.Left() < 8)
                {
                    Console.WriteLine("UnpackInteger uint64 failed");
                    return;
                }
                rb.MoveOffset(8);
                return;
            }

            case 0xd0:    // int8
            {
                if (rb.Left() < 1)
                {
                    Console.WriteLine("UnpackInteger int8 failed");
                    return;
                }
                rb.MoveNext();
                return;
            }

            case 0xd1:    // int16
            {
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackInteger int16 failed");
                    return;
                }
                rb.MoveOffset(2);
                return;
            }

            case 0xd2:    // int32
            {
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackInteger int failed");
                    return;
                }
                rb.MoveOffset(4);
                return;
            }

            case 0xd3:    // int64
            {
                if (rb.Left() < 8)
                {
                    Console.WriteLine("UnpackInteger int64 failed");
                    return;
                }
                rb.MoveOffset(8);
                return;
            }

            case 0xd9:    // str8
            {
                if (rb.Left() < 1)
                {
                    Console.WriteLine("UnpackString str8 length failed");
                    return;
                }
                byte slen = rb.MoveNext();
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackString str8 failed");
                    return;
                }
                rb.MoveOffset(slen);
                return;
            }

            case 0xda:    // str16
            {
                ushort slen = 0;
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackString str16 length failed");
                    return;
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(2);
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackString str16 failed");
                    return;
                }
                rb.MoveOffset(slen);
                return;
            }

            case 0xdb:    // str32
            {
                uint slen = 0;
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackString str32 length failed");
                    return;
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(4);
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackString str32 failed");
                    return;
                }
                rb.MoveOffset((int)slen);
                return;
            }

            case 0xca:    // float
            {
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackDouble float failed");
                    return;
                }
                rb.MoveOffset(4);
                return;
            }

            case 0xcb:    // double
            {
                if (rb.Left() < 8)
                {
                    Console.WriteLine("UnpackDouble double failed");
                    return;
                }
                rb.MoveOffset(8);
                return;
            }

            case 0xdc:    //  array16
            {
                ushort slen = 0;
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackArray array16 length failed");
                    return;
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(2);
                UnpackDiscard(rb, slen);
                return;
            }

            case 0xdd:    // array32
            {
                uint slen = 0;
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackArray array32 length failed");
                    return;
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(4);
                UnpackDiscard(rb, slen);
                return;
            }

            case 0xde:    // map16
            {
                ushort slen = 0;
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackMap array16 length failed");
                    return;
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(2);
                UnpackDiscard(rb, slen * 2);
                return;
            }

            case 0xdf:    // map32
            {
                uint slen = 0;
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackMap array32 length failed");
                    return;
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(4);
                UnpackDiscard(rb, slen * 2);
                return;
            }

            case 0xc4:    // bin8
            {
                if (rb.Left() < 1)
                {
                    Console.WriteLine("UnpackBytes bin8 length failed");
                    return;
                }
                byte slen = rb.MoveNext();
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackBytes bin failed");
                    return;
                }
                rb.MoveOffset(slen);
                return;
            }

            case 0xc5:    // bin16
            {
                ushort slen = 0;
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackBytes bin16 length failed");
                    return;
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(2);
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackBytes bin16 failed");
                    return;
                }
                rb.MoveOffset(slen);
                break;
            }

            case 0xc6:    // bin32
            {
                uint slen = 0;
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackBytes bin32 length failed");
                    return;
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(4);
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackBytes bin32 failed");
                    return;
                }
                rb.MoveOffset((int)slen);
                break;
            }

            default:
            {
                Console.WriteLine("Discard unknown type " + t.ToString());
                break;
            }
            }
        }
Esempio n. 2
0
        public static bool UnpackBytes(ReadBuffer rb, ref byte[] value)
        {
            byte t = rb.MoveNext();

            if (t == 0xc0)
            {
                value = null;
                return(true);
            }
            switch (t)
            {
            case 0xc4:    // bin8
            {
                if (rb.Left() < 1)
                {
                    Console.WriteLine("UnpackBytes bin8 length failed");
                    return(false);
                }
                byte slen = rb.MoveNext();
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackBytes bin failed");
                    return(false);
                }
                value = rb.CopyBytes(slen);
                rb.MoveOffset(slen);
                break;
            }

            case 0xc5:    // bin16
            {
                ushort slen = 0;
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackBytes bin16 length failed");
                    return(false);
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(2);
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackBytes bin16 failed");
                    return(false);
                }
                value = rb.CopyBytes(slen);
                rb.MoveOffset(slen);
                break;
            }

            case 0xc6:    // bin32
            {
                uint slen = 0;
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackBytes bin32 length failed");
                    return(false);
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(4);
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackBytes bin32 failed");
                    return(false);
                }
                value = rb.CopyBytes((int)slen);
                rb.MoveOffset((int)slen);
                break;
            }

            default:
            {
                Console.WriteLine("UnpackBytes unknown type " + t.ToString());
                return(false);
            }
            }
            return(true);
        }
Esempio n. 3
0
        public static bool UnpackInteger(ReadBuffer rb, ref long value)
        {
            byte t = rb.MoveNext();

            if (t == 0xc0)
            {
                return(true);
            }
            if (t < 0x80)// fixint
            {
                value = t;
                return(true);
            }
            if (t > 0xdf)// fixint_negative
            {
                value = (256 - t) * -1;
                return(true);
            }
            switch (t)
            {
            case 0xcc:    // uint8
            {
                if (rb.Left() < 1)
                {
                    Console.WriteLine("UnpackInteger uint8 failed");
                    return(false);
                }
                value = rb.MoveNext();
                break;
            }

            case 0xcd:    // uint16
            {
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackInteger uint16 failed");
                    return(false);
                }
                ushort v = 0;
                ReadNumber(rb.Data(), rb.Offset(), ref v);
                value = v;
                rb.MoveOffset(2);
                break;
            }

            case 0xce:    // uint
            {
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackInteger uint failed");
                    return(false);
                }
                uint v = 0;
                ReadNumber(rb.Data(), rb.Offset(), ref v);
                value = v;
                rb.MoveOffset(4);
                break;
            }

            case 0xcf:    // uint64
            {
                if (rb.Left() < 8)
                {
                    Console.WriteLine("UnpackInteger uint64 failed");
                    return(false);
                }
                ReadNumber(rb.Data(), rb.Offset(), ref value);
                rb.MoveOffset(8);
                break;
            }

            case 0xd0:    // int8
            {
                if (rb.Left() < 1)
                {
                    Console.WriteLine("UnpackInteger int8 failed");
                    return(false);
                }
                value = (char)rb.MoveNext();
                break;
            }

            case 0xd1:    // int16
            {
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackInteger int16 failed");
                    return(false);
                }
                short v = 0;
                ReadNumber(rb.Data(), rb.Offset(), ref v);
                value = v;
                rb.MoveOffset(2);
                break;
            }

            case 0xd2:    // int32
            {
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackInteger int failed");
                    return(false);
                }
                int v = 0;
                ReadNumber(rb.Data(), rb.Offset(), ref v);
                value = v;
                rb.MoveOffset(4);
                break;
            }

            case 0xd3:    // int64
            {
                if (rb.Left() < 8)
                {
                    Console.WriteLine("UnpackInteger int64 failed");
                    return(false);
                }
                ulong v = 0;
                ReadNumber(rb.Data(), rb.Offset(), ref v);
                value = (long)v;
                rb.MoveOffset(8);
                break;
            }

            default:
            {
                Console.WriteLine("UnpackInteger unknown integer type " + t.ToString());
                return(false);
            }
            }
            return(true);
        }
Esempio n. 4
0
        public static bool UnpackString(ReadBuffer rb, ref string value)
        {
            byte t = rb.MoveNext();

            if (t == 0xc0)
            {
                value = null;
                return(true);
            }
            if (t > 0x9f && t < 0xc0)
            {
                int slen = t & 0x1f;
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackString fixed str failed");
                    return(false);
                }
                value = rb.CopyString(slen);
                rb.MoveOffset(slen);
                return(true);
            }
            switch (t)
            {
            case 0xd9:    // str8
            {
                if (rb.Left() < 1)
                {
                    Console.WriteLine("UnpackString str8 length failed");
                    return(false);
                }
                byte slen = rb.MoveNext();
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackString str8 failed");
                    return(false);
                }
                value = rb.CopyString(slen);
                rb.MoveOffset(slen);
                break;
            }

            case 0xda:    // str16
            {
                ushort slen = 0;
                if (rb.Left() < 2)
                {
                    Console.WriteLine("UnpackString str16 length failed");
                    return(false);
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(2);
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackString str16 failed");
                    return(false);
                }
                value = rb.CopyString(slen);
                rb.MoveOffset(slen);
                break;
            }

            case 0xdb:    // str32
            {
                uint slen = 0;
                if (rb.Left() < 4)
                {
                    Console.WriteLine("UnpackString str32 length failed");
                    return(false);
                }
                ReadNumber(rb.Data(), rb.Offset(), ref slen);
                rb.MoveOffset(4);
                if (rb.Left() < slen)
                {
                    Console.WriteLine("UnpackString str32 failed");
                    return(false);
                }
                value = rb.CopyString((int)slen);
                rb.MoveOffset((int)slen);
                break;
            }

            default:
            {
                Console.WriteLine("UnpackString unknown type " + t.ToString());
                return(false);
            }
            }
            return(true);
        }
Esempio n. 5
0
 virtual public void Decode(ReadBuffer rb)
 {
 }