Beispiel #1
0
        /// <summary>Must be called for a nonempty array or object at start():</summary>
        protected internal virtual int findDataOffset()
        {
            int fsm = ObjectArrayUtil.getFirstSubMap(this.head
                                                         ());
            int offset;

            if (fsm <= 2 && this.vpack[this.start + 2] != 0)
            {
                offset = 2;
            }
            else
            {
                if (fsm <= 3 && this.vpack[this.start + 3] != 0)
                {
                    offset = 3;
                }
                else
                {
                    if (fsm <= 5 && this.vpack[this.start + 6] != 0)
                    {
                        offset = 5;
                    }
                    else
                    {
                        offset = 9;
                    }
                }
            }
            return(offset);
        }
Beispiel #2
0
        //Return the offset for the nth member from an Array or Object type
        int GetNthOffset(int index)
        {
            int  offset;
            byte head = TypeCode;

            if (head == 0x13 || head == 0x14)
            {
                // compact Array or Object
                offset = GetNthOffsetFromCompact(index);
            }
            else if (head == 0x01 || head == 0x0a)
            {
                // special case: empty Array or empty Object
                throw new IndexOutOfRangeException();
            }
            else
            {
                long n;
                int  offsetsize = ObjectArrayUtil.GetOffsetSize(head);
                long end        = NumberUtil.ToLong(vpack, start + 1, offsetsize);
                int  dataOffset = FindDataOffset();
                if (head <= 0x05)
                {
                    // array with no offset table or length
                    VPackSlice first = new VPackSlice(vpack, start + dataOffset);
                    n = (end - dataOffset) / first.GetByteSize();
                }
                else if (offsetsize < 8)
                {
                    n = NumberUtil.ToLong(vpack, start + 1 + offsetsize, offsetsize);
                }
                else
                {
                    n = NumberUtil.ToLong(vpack, (int)(start + end - offsetsize), offsetsize);
                }
                if (index >= n)
                {
                    throw new IndexOutOfRangeException();
                }
                if (head <= 0x05 || n == 1)
                {
                    // no index table, but all array items have the same length
                    // or only one item is in the array
                    // now fetch first item and determine its length
                    if (dataOffset == 0)
                    {
                        dataOffset = FindDataOffset();
                    }
                    offset = dataOffset + index * new VPackSlice(vpack, start + dataOffset).GetByteSize();
                }
                else
                {
                    long ieBase = end - n * offsetsize + index * offsetsize - (offsetsize == 8 ? 8 : 0);
                    offset = (int)NumberUtil.ToLong(vpack, (int)(start + ieBase), offsetsize);
                }
            }
            return(offset);
        }
Beispiel #3
0
        /// <summary>
        /// Must be called for a nonempty array or object at start():
        /// </summary>
        /// <returns></returns>
        internal int FindDataOffset()
        {
            int fsm = ObjectArrayUtil.GetFirstSubMap(TypeCode);
            int offset;

            if (fsm <= 2 && vpack[start + 2] != 0)
            {
                offset = 2;
            }
            else if (fsm <= 3 && vpack[start + 3] != 0)
            {
                offset = 3;
            }
            else if (fsm <= 5 && vpack[start + 6] != 0)
            {
                offset = 5;
            }
            else
            {
                offset = 9;
            }
            return(offset);
        }
Beispiel #4
0
        /// <returns>the offset for the nth member from an Array or Object type</returns>
        private int getNthOffset(int index)
        {
            int  offset;
            byte head = head();

            if (head == unchecked ((int)0x13) || head == unchecked ((int)0x14))
            {
                // compact Array or Object
                offset = this.getNthOffsetFromCompact(index);
            }
            else
            {
                if (head == unchecked ((int)0x01) || head == unchecked ((int)0x0a))
                {
                    // special case: empty Array or empty Object
                    throw new System.IndexOutOfRangeException();
                }
                else
                {
                    long n;
                    int  offsetsize = ObjectArrayUtil.getOffsetSize
                                          (head);
                    long end = NumberUtil.toLong(this.vpack, this.start
                                                 + 1, offsetsize);
                    int dataOffset = this.findDataOffset();
                    if ((sbyte)head <= unchecked ((int)0x05))
                    {
                        // array with no offset table or length
                        VPackSlice first = new VPackSlice
                                               (this.vpack, this.start + dataOffset);
                        n = (end - dataOffset) / first.getByteSize();
                    }
                    else
                    {
                        if (offsetsize < 8)
                        {
                            n = NumberUtil.toLong(this.vpack, this.start + 1 + offsetsize
                                                  , offsetsize);
                        }
                        else
                        {
                            n = NumberUtil.toLong(this.vpack, (int)(this.start +
                                                                    end - offsetsize), offsetsize);
                        }
                    }
                    if (index >= n)
                    {
                        throw new System.IndexOutOfRangeException();
                    }
                    if ((sbyte)head <= unchecked ((int)0x05) || n == 1)
                    {
                        // no index table, but all array items have the same length
                        // or only one item is in the array
                        // now fetch first item and determine its length
                        if (dataOffset == 0)
                        {
                            dataOffset = this.findDataOffset();
                        }
                        offset = dataOffset + index * new VPackSlice(this.vpack, this.start
                                                                     + dataOffset).getByteSize();
                    }
                    else
                    {
                        long ieBase = end - n * offsetsize + index * offsetsize - (offsetsize == 8 ? 8 :
                                                                                   0);
                        offset = (int)NumberUtil.toLong(this.vpack, (int
                                                                     )(this.start + ieBase), offsetsize);
                    }
                }
            }
            return(offset);
        }
Beispiel #5
0
        /// <exception cref="VPackException"/>
        public virtual VPackSlice get(string attribute)
        {
            if (!this.isObject())
            {
                throw new VPackValueTypeException(ValueType
                                                  .OBJECT);
            }
            byte       head   = head();
            VPackSlice result = new VPackSlice
                                    ();

            if (head == unchecked ((int)0x0a))
            {
                // special case, empty object
                result = new VPackSlice();
            }
            else
            {
                if (head == unchecked ((int)0x14))
                {
                    // compact Object
                    result = this.getFromCompactObject(attribute);
                }
                else
                {
                    int offsetsize = ObjectArrayUtil.getOffsetSize
                                         (head);
                    long end = NumberUtil.toLong(this.vpack, this.start
                                                 + 1, offsetsize);
                    long n;
                    if (offsetsize < 8)
                    {
                        n = NumberUtil.toLong(this.vpack, this.start + 1 + offsetsize
                                              , offsetsize);
                    }
                    else
                    {
                        n = NumberUtil.toLong(this.vpack, (int)(this.start +
                                                                end - offsetsize), offsetsize);
                    }
                    if (n == 1)
                    {
                        // Just one attribute, there is no index table!
                        VPackSlice key = new VPackSlice(this.vpack
                                                        , this.start + this.findDataOffset());
                        if (key.isString())
                        {
                            if (key.isEqualString(attribute))
                            {
                                result = new VPackSlice(this.vpack, key.start + key.getByteSize
                                                            ());
                            }
                            else
                            {
                                // no match
                                result = new VPackSlice();
                            }
                        }
                        else
                        {
                            if (key.isInteger())
                            {
                                // translate key
                                if (VPackSlice.attributeTranslator == null)
                                {
                                    throw new VPackNeedAttributeTranslatorException
                                              ();
                                }
                                if (key.translateUnchecked().isEqualString(attribute))
                                {
                                    result = new VPackSlice(this.vpack, key.start + key.getByteSize
                                                                ());
                                }
                                else
                                {
                                    // no match
                                    result = new VPackSlice();
                                }
                            }
                            else
                            {
                                // no match
                                result = new VPackSlice();
                            }
                        }
                    }
                    else
                    {
                        long ieBase = end - n * offsetsize - (offsetsize == 8 ? 8 : 0);
                        // only use binary search for attributes if we have at least
                        // this many entries
                        // otherwise we'll always use the linear search
                        long sortedSearchEntriesThreshold = 4;
                        bool sorted = head >= unchecked ((int)0x0b) && (sbyte)head <= unchecked ((int)0x0e);
                        if (sorted && n >= sortedSearchEntriesThreshold)
                        {
                            // This means, we have to handle the special case n == 1
                            // only in the linear search!
                            result = this.searchObjectKeyBinary(attribute, ieBase, offsetsize, n);
                        }
                        else
                        {
                            result = this.searchObjectKeyLinear(attribute, ieBase, offsetsize, n);
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #6
0
        public virtual int getByteSize()
        {
            long size;
            byte head        = head();
            int  valueLength = ValueLengthUtil.get(head
                                                   );

            if (valueLength != 0)
            {
                size = valueLength;
            }
            else
            {
                switch (this.type())
                {
                case ValueType.ARRAY:
                case ValueType.OBJECT:
                {
                    if (head == unchecked ((int)0x13) || head == unchecked ((int)0x14))
                    {
                        // compact Array or Object
                        size = NumberUtil.readVariableValueLength(
                            this.vpack, this.start + 1, false);
                    }
                    else
                    {
                        /* if (head <= 0x14) */
                        size = NumberUtil.toLong(this.vpack, this.start + 1,
                                                 ObjectArrayUtil.getOffsetSize(head));
                    }
                    break;
                }

                case ValueType.STRING:
                {
                    // long UTF-8 String
                    size = this.getLongStringLength() + 1 + 8;
                    break;
                }

                case ValueType.BINARY:
                {
                    size = 1 + head - unchecked ((byte)unchecked ((int)0xbf)) + this.getBinaryLengthUnchecked
                               ();
                    break;
                }

                case ValueType.BCD:
                {
                    if ((sbyte)head <= unchecked ((int)0xcf))
                    {
                        size = 1 + head + unchecked ((byte)unchecked ((int)0xc7)) + NumberUtil
                               .toLong(this.vpack, this.start + 1, head - unchecked ((byte)unchecked ((int)0xc7)));
                    }
                    else
                    {
                        size = 1 + head - unchecked ((byte)unchecked ((int)0xcf)) + NumberUtil
                               .toLong(this.vpack, this.start + 1, head - unchecked ((byte)unchecked ((int)0xcf)));
                    }
                    break;
                }

                case ValueType.CUSTOM:
                {
                    if (head == unchecked ((int)0xf4) || head == unchecked ((int)0xf5) || head == unchecked (
                            (int)0xf6))
                    {
                        size = 2 + NumberUtil.toLong(this.vpack, this.start
                                                     + 1, 1);
                    }
                    else
                    {
                        if (head == unchecked ((int)0xf7) || head == unchecked ((int)0xf8) || head == unchecked (
                                (int)0xf9))
                        {
                            size = 3 + NumberUtil.toLong(this.vpack, this.start
                                                         + 1, 2);
                        }
                        else
                        {
                            if (head == unchecked ((int)0xfa) || head == unchecked ((int)0xfb) || head == unchecked (
                                    (int)0xfc))
                            {
                                size = 5 + NumberUtil.toLong(this.vpack, this.start
                                                             + 1, 4);
                            }
                            else
                            {
                                /* if (head == 0xfd || head == 0xfe || head == 0xff) */
                                size = 9 + NumberUtil.toLong(this.vpack, this.start
                                                             + 1, 8);
                            }
                        }
                    }
                    break;
                }

                default:
                {
                    // TODO
                    throw new java.lang.InternalError();
                }
                }
            }
            return((int)size);
        }
Beispiel #7
0
        /// <returns>the number of members for an Array, Object or String</returns>
        public virtual int getLength()
        {
            long length;

            if (this.isString())
            {
                length = this.getStringLength();
            }
            else
            {
                if (!this.isArray() && !this.isObject())
                {
                    throw new VPackValueTypeException(ValueType
                                                      .ARRAY, ValueType.OBJECT, ValueType
                                                      .STRING);
                }
                else
                {
                    byte head = head();
                    if (head == unchecked ((int)0x01) || head == unchecked ((int)0x0a))
                    {
                        // empty
                        length = 0;
                    }
                    else
                    {
                        if (head == unchecked ((int)0x13) || head == unchecked ((int)0x14))
                        {
                            // compact array or object
                            long end = NumberUtil.readVariableValueLength
                                           (this.vpack, this.start + 1, false);
                            length = NumberUtil.readVariableValueLength
                                         (this.vpack, (int)(this.start + end - 1), true);
                        }
                        else
                        {
                            int offsetsize = ObjectArrayUtil.getOffsetSize
                                                 (head);
                            long end = NumberUtil.toLong(this.vpack, this.start
                                                         + 1, offsetsize);
                            if ((sbyte)head <= unchecked ((int)0x05))
                            {
                                // array with no offset table or length
                                int        dataOffset = this.findDataOffset();
                                VPackSlice first      = new VPackSlice
                                                            (this.vpack, this.start + dataOffset);
                                length = (end - dataOffset) / first.getByteSize();
                            }
                            else
                            {
                                if (offsetsize < 8)
                                {
                                    length = NumberUtil.toLong(this.vpack, this.start +
                                                               1 + offsetsize, offsetsize);
                                }
                                else
                                {
                                    length = NumberUtil.toLong(this.vpack, (int)(this.start
                                                                                 + end - offsetsize), offsetsize);
                                }
                            }
                        }
                    }
                }
            }
            return((int)length);
        }
Beispiel #8
0
        /// <summary>
        /// Return object by attribute name
        /// </summary>
        /// <param name="attribute"></param>
        /// <returns></returns>
        public VPackSlice this[string attribute]
        {
            get
            {
                if (!IsType(SliceType.Object))
                {
                    throw new VPackValueTypeException(SliceType.Object);
                }
                byte       head   = TypeCode;
                VPackSlice result = new VPackSlice();
                if (head == 0x0a)
                {
                    // special case, empty object
                    result = new VPackSlice();
                }
                else if (head == 0x14)
                {
                    // compact Object
                    result = GetFromCompactObject(attribute);
                }
                else
                {
                    int  offsetsize = ObjectArrayUtil.GetOffsetSize(head);
                    long end        = NumberUtil.ToLong(vpack, start + 1, offsetsize);
                    long n;
                    if (offsetsize < 8)
                    {
                        n = NumberUtil.ToLong(vpack, start + 1 + offsetsize, offsetsize);
                    }
                    else
                    {
                        n = NumberUtil.ToLong(vpack, (int)(start + end - offsetsize), offsetsize);
                    }
                    if (n == 1)
                    {
                        // Just one attribute, there is no index table!
                        VPackSlice key = new VPackSlice(vpack, start + FindDataOffset());

                        if (key.IsType(SliceType.String))
                        {
                            if (key.IsEqualString(attribute))
                            {
                                result = new VPackSlice(vpack, key.start + key.GetByteSize());
                            }
                            else
                            {
                                // no match
                                result = new VPackSlice();
                            }
                        }
                        else if (key.IsInteger())
                        {
                            // translate key
                            if (attributeTranslator == null)
                            {
                                throw new VPackNeedAttributeTranslatorException();
                            }
                            if (key.TranslateUnchecked().IsEqualString(attribute))
                            {
                                result = new VPackSlice(vpack, key.start + key.GetByteSize());
                            }
                            else
                            {
                                // no match
                                result = new VPackSlice();
                            }
                        }
                        else
                        {
                            // no match
                            result = new VPackSlice();
                        }
                    }
                    else
                    {
                        long ieBase = end - n * offsetsize - (offsetsize == 8 ? 8 : 0);

                        // only use binary search for attributes if we have at least
                        // this many entries
                        // otherwise we'll always use the linear search
                        long sortedSearchEntriesThreshold = 4;

                        bool sorted = head >= 0x0b && head <= 0x0e;
                        if (sorted && n >= sortedSearchEntriesThreshold)
                        {
                            // This means, we have to handle the special case n == 1
                            // only in the linear search!
                            result = SearchObjectKeyBinary(attribute, ieBase, offsetsize, n);
                        }
                        else
                        {
                            result = SearchObjectKeyLinear(attribute, ieBase, offsetsize, n);
                        }
                    }
                }
                return(result);
            }
        }
Beispiel #9
0
        internal int GetByteSize()
        {
            long size;
            byte head        = TypeCode;
            int  valueLength = ValueLengthUtil.Get(head);

            if (valueLength != 0)
            {
                size = valueLength;
            }
            else
            {
                switch (Type)
                {
                case SliceType.Array:
                case SliceType.Object:
                    if (head == 0x13 || head == 0x14)
                    {
                        // compact Array or Object
                        size = NumberUtil.ReadVariableValueLength(vpack, start + 1, false);
                    }
                    else     /* if (head <= 0x14) */
                    {
                        size = NumberUtil.ToLong(vpack, start + 1, ObjectArrayUtil.GetOffsetSize(head));
                    }
                    break;

                case SliceType.String:
                    // long UTF-8 String
                    size = GetLongStringLength() + 1 + 8;
                    break;

                case SliceType.Binary:
                    size = 1 + head - ((byte)0xbf) + GetBinaryLengthUnchecked();
                    break;

                case SliceType.Bcd:
                    if (head <= 0xcf)
                    {
                        size = 1 + head + ((byte)0xc7) + NumberUtil.ToLong(vpack, start + 1, head - ((byte)0xc7));
                    }
                    else
                    {
                        size = 1 + head - ((byte)0xcf) + NumberUtil.ToLong(vpack, start + 1, head - ((byte)0xcf));
                    }
                    break;

                case SliceType.Custom:
                    if (head == 0xf4 || head == 0xf5 || head == 0xf6)
                    {
                        size = 2 + NumberUtil.ToLong(vpack, start + 1, 1);
                    }
                    else if (head == 0xf7 || head == 0xf8 || head == 0xf9)
                    {
                        size = 3 + NumberUtil.ToLong(vpack, start + 1, 2);
                    }
                    else if (head == 0xfa || head == 0xfb || head == 0xfc)
                    {
                        size = 5 + NumberUtil.ToLong(vpack, start + 1, 4);
                    }
                    else     /* if (head == 0xfd || head == 0xfe || head == 0xff) */
                    {
                        size = 9 + NumberUtil.ToLong(vpack, start + 1, 8);
                    }
                    break;

                default:
                    // TODO
                    throw new Exception("Internal error");
                }
            }
            return((int)size);
        }