Пример #1
0
        private static int GetOTRDataLenType(OTR_DATA_LEN_TYPE otr_data_type)
        {
            if (otr_data_type == OTR_DATA_LEN_TYPE.TYPE_CTR)
                return OTRConstants.TYPE_LEN_CTR;

            if (otr_data_type == OTR_DATA_LEN_TYPE.TYPE_INT)
                return OTRConstants.TYPE_LEN_INT;

            if (otr_data_type == OTR_DATA_LEN_TYPE.TYPE_MAC)
                return OTRConstants.TYPE_LEN_MAC;

            if (otr_data_type == OTR_DATA_LEN_TYPE.TYPE_MPI)
                return OTRConstants.TYPE_LEN_MPI;

            if (otr_data_type == OTR_DATA_LEN_TYPE.TYPE_SHORT)
                return OTRConstants.TYPE_LEN_SHORT;

            if (otr_data_type == OTR_DATA_LEN_TYPE.TYPE_DATA)
                return OTRConstants.TYPE_LEN_DATA;

            if (otr_data_type == OTR_DATA_LEN_TYPE.TYPE_BYTE)
                return OTRConstants.TYPE_LEN_BYTE;

            return -1;
        }
Пример #2
0
        private static void EncodeOTRBytes(byte[] in_byte_array, OTR_DATA_LEN_TYPE otr_data_len_type, ref byte[] out_byte_array)
        {
            if (in_byte_array == null || in_byte_array.Length < 1)
                throw new ArgumentException("EncodeOTRBytes: In byte array cannot be null/empty");

            if (otr_data_len_type == OTR_DATA_LEN_TYPE.INVALID)
            throw new ArgumentException("EncodeOTRBytes: The OTR data len type is invalid");

            int _type_length = GetOTRDataLenType(otr_data_len_type);

             if (in_byte_array.Length > _type_length)
            throw new ArgumentException("EncodeOTRBytes: In byte length cannot greater than " + _type_length.ToString());

            out_byte_array = new byte[_type_length];

            Buffer.BlockCopy(in_byte_array, 0, out_byte_array, 0, out_byte_array.Length);

                if (BitConverter.IsLittleEndian == true)
                 Array.Reverse(out_byte_array);
        }
Пример #3
0
        private static int DecoupleTypeFromBytes(byte[] in_byte_array, int start_index, OTR_DATA_LEN_TYPE otr_data_len_type, ref byte[] out_byte_array)
        {
            if (in_byte_array == null || in_byte_array.Length < 1)
             throw new ArgumentException("DecoupleTypeFromBytes: In byte array cannot be null/empty");

            if (otr_data_len_type == OTR_DATA_LEN_TYPE.INVALID)
                throw new ArgumentException("DecoupleTypeFromBytes: The OTR data len type is invalid");

            if (start_index < 0)
                throw new ArgumentException("DecoupleTypeFromBytes: The start index cannot be less than 0");

            int _type_length = GetOTRDataLenType(otr_data_len_type);

            if (in_byte_array.Length < _type_length + 1)
                throw new ArgumentException("DecoupleTypeFromBytes: In byte array length cannot be less than specified type length of " + _type_length.ToString());

            int _next_start_index = 0;

            byte[] _length_byte_array = new byte[_type_length];

            Buffer.BlockCopy(in_byte_array, start_index, _length_byte_array, 0, _type_length);

            if (BitConverter.IsLittleEndian)
             Array.Reverse(_length_byte_array);

            byte[] _int_32_length = new byte[4];
            Buffer.BlockCopy(_length_byte_array, 0, _int_32_length, 0, _length_byte_array.Length);

            int _data_array_length = BitConverter.ToInt32(_int_32_length, 0);

            if (_data_array_length < 1)
                throw new InvalidDataException("DecoupleTypeFromBytes: The length of the data sub array in the in byte array is less than 1");

            _next_start_index = _data_array_length + start_index + _type_length;

            if (_next_start_index > in_byte_array.Length)
                throw new InvalidDataException("DecoupleTypeFromBytes: The extracted data length value exceeds the length of the byte array");

            out_byte_array = new byte[_data_array_length + _type_length];

            Buffer.BlockCopy(in_byte_array, start_index, out_byte_array, 0, out_byte_array.Length);

            if (_next_start_index == in_byte_array.Length)
                _next_start_index = -1;

            return _next_start_index;
        }
Пример #4
0
        private static void EncodeBytesBE(byte[] in_byte_array, OTR_DATA_LEN_TYPE otr_data_len_type, ref byte[] out_byte_array)
        {
            if (in_byte_array == null || in_byte_array.Length < 1)
                throw new ArgumentException("EncodeBytesBE: In byte array cannot be null/empty");

            if (otr_data_len_type == OTR_DATA_LEN_TYPE.INVALID)
                throw new ArgumentException("EncodeBytesBE: The OTR data len type is invalid");

            int _type_length = GetOTRDataLenType(otr_data_len_type);

            byte[] _byte_array = new byte[in_byte_array.Length];

            Buffer.BlockCopy(in_byte_array, 0, _byte_array, 0, _byte_array.Length);

            byte[] _length_byte_array = new byte[_type_length];
            byte[] _length_temp_array = BitConverter.GetBytes(_byte_array.Length);

            if (BitConverter.IsLittleEndian == false)
                Array.Reverse(_length_temp_array);

            Buffer.BlockCopy(_length_temp_array, 0, _length_byte_array, 0, _length_byte_array.Length);

            if (BitConverter.IsLittleEndian)
                Array.Reverse(_length_byte_array);

            out_byte_array = new byte[_length_byte_array.Length + _byte_array.Length];

            Buffer.BlockCopy(_length_byte_array, 0, out_byte_array, 0, _type_length);
            Buffer.BlockCopy(_byte_array, 0, out_byte_array, _type_length, _byte_array.Length);
        }
Пример #5
0
        private static int DecodeOTRBytes(byte[] in_byte_array, int start_index, OTR_DATA_LEN_TYPE otr_data_len_type, ref byte[] out_byte_array)
        {
            /* Assumes in byte array is big-endian */
            if (in_byte_array == null || in_byte_array.Length < 1)
                throw new ArgumentException("DecodeOTRBytes: In byte array cannot be null/empty");

            if (otr_data_len_type == OTR_DATA_LEN_TYPE.INVALID)
                throw new ArgumentException("DecodeOTRBytes: The OTR data len type is invalid");

            if (start_index < 0)
             throw new ArgumentException("DecodeOTRBytes: The start index cannot be less than 0");

             int _type_length = GetOTRDataLenType(otr_data_len_type);
             int _next_start_index = start_index + _type_length;

            out_byte_array = new byte [_type_length];

            Buffer.BlockCopy(in_byte_array, start_index, out_byte_array, 0, out_byte_array.Length);

            if (BitConverter.IsLittleEndian == true)
                Array.Reverse(out_byte_array);

            if (_next_start_index == in_byte_array.Length)
                _next_start_index = -1;

            return _next_start_index;
        }