/*
         * Read byte[] as a string array. At most 9 octets reveal the size of the array (string element count).
         */
        public static DeserializationFieldResult readStringArray(byte[] data, int idx)
        {
            DeserializationFieldResult res = new DeserializationFieldResult {
            };
            CMap <int, string> map         = new CMap <int, string> {
            };

            map.map = new Map <int, string>();

            VarSizeStr countStr = readVarSize(data, idx);

            res.size = countStr.size;
            if (countStr.value == 0)
            {
                map.size    = 0;
                res.value   = map;
                res.isEmpty = true;
                return(res);
            }

            idx     += countStr.size;
            map.size = (int)countStr.value;
            for (int i = 0; i < map.size; ++i)
            {
                DeserializationFieldResult tempStr = readString(data, idx);

                map.map[i] = (string)tempStr.value;
                res.size   = res.size + tempStr.size;
                idx       += tempStr.size;
            }

            res.value   = map;
            res.isEmpty = false;
            return(res);
        }
        /*
         * 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);
        }