/*
         * 8 bytes data read for ulong (little endian)
         */
        public static long readLong(byte[] data, int idx)
        {
            byte[]     leading = Helper.Range(data, idx, 8);
            BigInteger val     = GetBigInteger(leading);

            return((long)val);
        }
        /*
         * 4 bytes data read for uint (little endian)
         */
        public static int readInt(byte[] data, int idx)
        {
            byte[]     leading = Helper.Range(data, idx, 4);
            BigInteger val     = GetBigInteger(leading);

            return((int)val);
        }
        public static short readShort(byte[] data, int idx)
        {
            byte[] leading = Helper.Range(data, idx, 2);

            BigInteger val = GetBigInteger(leading);

            return((short)val);
        }
        /*
         * Generic structure for array size. Each array structure begins with the leading size structure.
         * Size can be a octet, byte, uint or ulong, total size of this structure depends on the type of
         * the data, so size becomes 1, 3, 5, 9 byte(s) respectively.
         */

        public static VarSizeStr readVarSize(byte[] data, int idx)
        {
            byte[]     temp  = Helper.Range(data, idx, 1);
            byte[]     temp2 = new byte[] { 0x00 };
            byte[]     temp3 = Helper.Concat(temp, temp2);
            BigInteger temp4 = Helper.AsBigInteger(temp3);
            int        ind   = (int)temp4;

            byte[] val  = new byte[1];
            int    size = 0;

            if (ind == 0xfd)
            {
                // read next 2 bytes total 3 (ushort length string)
                size = 2;
                val  = new byte[3];
            }

            if (ind == 0xfe)
            {
                // read next 4 bytes total 5 (uint length string)
                size = 4;
                val  = new byte[5];
            }

            if (ind == 0xff)
            {
                // read next 8 bytes total 9 (ulong length string)
                size = 8;
                val  = new byte[9];
            }

            if (size > 0)
            {
                val = Helper.Range(data, idx + 1, size);

                val = Helper.Concat(val, temp2);
            }

            VarSizeStr res = new VarSizeStr();

            if (size == 0)
            {
                res.value = ind;
            }

            if (size > 0)
            {
                res.value = (int)Helper.AsBigInteger(val);
            }

            res.size = size + 1;

            return(res);
        }
        /*
         * Read byte[] from the stream. At most 9 octets reveal the size of the array.
         */
        public static DeserializationFieldResult readByteArray(byte[] data, int idx)
        {
            DeserializationFieldResult res = new DeserializationFieldResult();

            res.size  = 0;
            res.value = null;

            VarSizeStr varSizeStr = readVarSize(data, idx);

            if (varSizeStr.value == 0)
            {
                res.isEmpty = true;
                res.size    = 1;
                return(res);
            }

            res.size  = varSizeStr.size + varSizeStr.value;
            res.value = Helper.Range(data, idx + varSizeStr.size, varSizeStr.value);

            return(res);
        }
예제 #6
0
 public static byte[] SubBytes(byte[] data, int start, int length) => Helper.Range(data, start, length);