Beispiel #1
0
    public void TestListUlongReadRedLight()
    {
        List <byte> arr = new List <byte>(sizeof(ulong));

        for (int i = 0; i < sizeof(ulong); i++)
        {
            arr.Add(0);
        }
        ByteManipulator.Write(arr, 0, (ulong)25000544);
        Assert.That(ByteManipulator.ReadUInt64(arr, 0), Is.Not.EqualTo(9));
    }
Beispiel #2
0
    public void TestListLongRead()
    {
        List <byte> arr = new List <byte>(sizeof(long));

        for (int i = 0; i < sizeof(long); i++)
        {
            arr.Add(0);
        }
        ByteManipulator.Write(arr, 0, (long)-25);
        Assert.That(ByteManipulator.ReadInt64(arr, 0), Is.EqualTo(-25));
    }
Beispiel #3
0
    public void TestListUlongRead()
    {
        List <byte> arr = new List <byte>(sizeof(ulong));

        for (int i = 0; i < sizeof(ulong); i++)
        {
            arr.Add(0);
        }
        ByteManipulator.Write(arr, 0, (ulong)18446744073709551614);
        Assert.That(ByteManipulator.ReadUInt64(arr, 0), Is.EqualTo(18446744073709551614));
    }
Beispiel #4
0
    public void TestListUshortRead()
    {
        List <byte> arr = new List <byte>(sizeof(ushort));

        for (int i = 0; i < sizeof(ushort); i++)
        {
            arr.Add(0);
        }
        ByteManipulator.Write(arr, 0, (ushort)25);
        Assert.That(ByteManipulator.ReadUInt16(arr, 0), Is.EqualTo(25));
    }
Beispiel #5
0
    public void TestListUintReadRedLight()
    {
        List <byte> arr = new List <byte>(sizeof(uint));

        for (int i = 0; i < sizeof(uint); i++)
        {
            arr.Add(0);
        }
        ByteManipulator.Write(arr, 0, (uint)25);
        Assert.That(ByteManipulator.ReadUInt32(arr, 0), Is.Not.EqualTo(9));
    }
Beispiel #6
0
    public void TestListCharReadRedLight()
    {
        List <byte> arr = new List <byte>(sizeof(char));

        for (int i = 0; i < sizeof(char); i++)
        {
            arr.Add(0);
        }
        ByteManipulator.Write(arr, 0, 'u');
        Assert.That(ByteManipulator.ReadChar(arr, 0), Is.Not.EqualTo('z'));
    }
Beispiel #7
0
    public void TestListDoubleReadRedLight()
    {
        List <byte> arr = new List <byte>(sizeof(double));

        for (int i = 0; i < sizeof(double); i++)
        {
            arr.Add(0);
        }
        ByteManipulator.Write(arr, 0, 10d);
        Assert.That(ByteManipulator.ReadDouble(arr, 0), Is.Not.EqualTo(9d));
    }
Beispiel #8
0
    public void TestListFloatReadRedLight()
    {
        List <byte> arr = new List <byte>(sizeof(float));

        for (int i = 0; i < sizeof(float); i++)
        {
            arr.Add(0);
        }
        ByteManipulator.Write(arr, 0, 10f);
        Assert.That(ByteManipulator.ReadSingle(arr, 0), Is.Not.EqualTo(9f).Within(0.0001));
    }
Beispiel #9
0
        public void TruncateZeroBytesUnitTest()
        {
            var input    = new byte[] { 0, 0, 0, 123, 45, 0, 11 };
            var expected = new byte[] { 123, 45, 0, 11 };

            var actual = ByteManipulator.TruncateMostSignificatZeroBytes(input);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i]);
            }
        }
Beispiel #10
0
        public void ExtendUnitTest()
        {
            var input    = new byte[] { 21, 5, 128, 123, 45, 0, 11 };
            var expected = new byte[] { 128, 123, 45, 0, 11 };

            var actual = ByteManipulator.BigEndianTruncate(input, 5);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i]);
            }
        }
Beispiel #11
0
    public void TestCharsReadRedLight()
    {
        char[] s   = "dngsgnsiongDDDD@@#[ffa ".ToCharArray();
        char[] s2  = new char[s.Length];
        byte[] arr = new byte[Encoding.UTF8.GetByteCount(s) + sizeof(int)];
        ByteManipulator.Write(arr, 0, s, encoder);
        int n;
        int n2;

        ByteManipulator.ReadChars(arr, 0, s2, 0, encoder, out n, out n2);
        Assert.That(new string(s2), Is.Not.EqualTo("dngsgnsiongDDDD@@#[ffa"));
    }
        private void Deserialize(byte[] rlp)
        {
            var data = RLP.Decode(rlp);

            Nonce       = ByteManipulator.GetUInt32(data[0] ?? new byte[] { 0 });
            Ammount     = new LargeInteger(data[1] ?? new byte[] { 0 });
            Fee         = new LargeInteger(data[2] ?? new byte[] { 0 });
            Source      = ByteManipulator.BigEndianTruncate(data[3], 33) ?? new byte[33];
            Destination = ByteManipulator.BigEndianTruncate(data[4], 33) ?? new byte[33];
            Signature   = ByteManipulator.BigEndianTruncate(data[5], 64) ?? new byte[64];
            Network     = data[6] ?? new byte[] { 0 };
        }
Beispiel #13
0
        /// <summary>
        /// Gets recorded data and stores it in format Single
        /// </summary>
        /// <param name="buffer">buffer to fill with audio data recorded</param>
        /// <param name="bufferOffset">buffer start index</param>
        /// <param name="dataCount">amount of data to store</param>
        /// <param name="effectiveDataCount">effective amount of data stored</param>
        /// <returns>data info</returns>
        public override VoicePacketInfo GetMicData(float[] buffer, int bufferOffset, int dataCount, out int effectiveDataCount)
        {
            effectiveDataCount = Mathf.Min(Mathf.Min(dataCount, buffer.Length - bufferOffset), MicDataAvailable);
            if (effectiveDataCount <= 0)
            {
                return(VoicePacketInfo.InvalidPacket);
            }

            readIndex = ByteManipulator.WriteFromCycle(this.cyclicAudioBuffer, readIndex, buffer, bufferOffset, dataCount);

            return(new VoicePacketInfo((ushort)clip.frequency, (byte)clip.channels, AudioDataTypeFlag.Single));
        }
Beispiel #14
0
        public long NextOffset(long start)
        {
            var buffer = new byte[LengthBytes];

            fileStream.Position = start;
            fileStream.Read(buffer);
            fileStream.Flush();

            var length = ByteManipulator.GetUInt32(buffer);

            return(start + length + LengthBytes);
        }
Beispiel #15
0
        private string PerformDecryption(
            string toDecrypt,
            JweAlg alg,
            JsonWebKey jsonWebKey,
            Func <byte[][], byte[]> callback)
        {
            try
            {
                var toDecryptSplitted                  = toDecrypt.Split('.');
                var serializedProtectedHeader          = toDecryptSplitted[0].Base64Decode();
                var encryptedContentEncryptionKeyBytes = toDecryptSplitted[1].Base64DecodeBytes();
                var ivBytes           = toDecryptSplitted[2].Base64DecodeBytes();
                var cipherText        = toDecryptSplitted[3].Base64DecodeBytes();
                var authenticationTag = toDecryptSplitted[4].Base64DecodeBytes();

                var contentEncryptionKey = _aesEncryptionHelper.DecryptContentEncryptionKey(
                    encryptedContentEncryptionKeyBytes,
                    alg,
                    jsonWebKey);
                var contentEncryptionKeySplitted = GetKeysFromContentEncryptionKey(contentEncryptionKey);

                var hmacKey   = callback(contentEncryptionKeySplitted);
                var aesCbcKey = contentEncryptionKeySplitted[1];

                // Encrypt the plain text & create cipher text.
                var decrypted = _aesEncryptionHelper.DecryptWithAesAlgorithm(
                    cipherText,
                    aesCbcKey,
                    ivBytes);

                // Calculate the additional authenticated data.
                var aad = Encoding.UTF8.GetBytes(serializedProtectedHeader);

                // Calculate the authentication tag.
                var al                   = ByteManipulator.LongToBytes(aad.Length * 8);
                var hmacInput            = ByteManipulator.Concat(aad, ivBytes, cipherText, al);
                var hmacValue            = ComputeHmac(_keySize, hmacKey, hmacInput);
                var newAuthenticationTag = ByteManipulator.SplitByteArrayInHalf(hmacValue)[0];

                // Check if the authentication tags are equal other raise an exception.
                if (!ByteManipulator.ConstantTimeEquals(newAuthenticationTag, authenticationTag))
                {
                    // TODO : raise an exception.
                    return(string.Empty);
                }

                return(decrypted);
            }
            catch (Exception ex)
            {
                throw new Exception("invalid " + toDecrypt);
            }
        }
        private uint?GetOffset(byte[] key)
        {
            uint?ret    = null;
            var  offset = LinearDictionaryCoder.Get(key);

            if (offset != null)
            {
                ret = ByteManipulator.GetUInt32(offset);
            }

            return(ret);
        }
        public void RemoveTopItem()
        {
            var topItemOffset = serialCoder.GetIndexOffset(serialCoder.Count - 1);

            serialCoder.RemoveByOffset(topItemOffset);

            var reverseKey = ByteManipulator.GetBytes((uint)topItemOffset);
            var key        = reverseLookupLinearDictionaryCoder.Get(reverseKey);

            LinearDictionaryCoder.Remove(key);
            reverseLookupLinearDictionaryCoder.Remove(reverseKey);
        }
    void SendHitToHost(int id)
    {
        byte[] data = ArrayPool <byte> .Get(sizeof(int));

        ByteManipulator.Write(data, 0, id);

        Client.SendPacketToHost(data, 0, data.Length, PacketType.ShootHitServer, Steamworks.EP2PSend.k_EP2PSendReliable);

        ArrayPool <byte> .Recycle(data);

        Debug.Log("hit");
    }
Beispiel #19
0
        public static byte[] Encode(byte[] bytes)
        {
            if (bytes == null)
            {
                return(null);
            }
            else
            {
                bytes = ByteManipulator.TruncateMostSignificatZeroBytes(bytes);
            }

            if (bytes.Length == 1 && bytes[0] < 128)
            {
                return(new byte[] { bytes[0] });
            }

            if (bytes.Length == 0)
            {
                return(new byte[0]);
            }

            if (bytes[0] == 0)
            {
                return(new byte[0]);
            }

            if (bytes.Length <= SizeThreshold)
            {
                var newBytes = new byte[bytes.Length + 1];
                newBytes[0] = Convert.ToByte(ShortItemOffset + bytes.Length);
                Array.Copy(bytes, 0, newBytes, 1, bytes.Length);

                return(newBytes);
            }
            else
            {
                var lengthBytesCount = GetLength(bytes.Length);
                var lengthBytes      = ByteManipulator.GetBytes((uint)bytes.Length);

                var newBytes = new byte[bytes.Length + lengthBytesCount + 1];
                newBytes[0] = Convert.ToByte(LargeItemOffset + lengthBytesCount);

                for (int i = lengthBytes.Length - 1; i >= lengthBytes.Length - lengthBytesCount; i--)
                {
                    newBytes[i - 3 + lengthBytesCount] = lengthBytes[i];
                }

                Array.Copy(bytes, 0, newBytes, 1 + lengthBytesCount, bytes.Length);

                return(newBytes);
            }
        }
Beispiel #20
0
 public string GetRandomUrl()
 {
     if (coder.LinearCoder.Count != 0)
     {
         var bytes = coder.LinearCoder.Read(random.Next(0, coder.LinearCoder.Count - 1));
         bytes = ByteManipulator.TruncateMostSignificatZeroBytes(bytes.Take(bytes.Length - 4).ToArray());
         return(Encoding.UTF8.GetString(bytes));
     }
     else
     {
         return(null);
     }
 }
Beispiel #21
0
        public bool ContainsKey(byte[] key)
        {
            key = ByteManipulator.BigEndianTruncate(key, KeyLength);

            if (GetIndex(key) < 0)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
    public void TestProcessReceivedDataValidPacketRedLight()
    {
        BytePacket buffer = new BytePacket(20);

        byte[] receivedData = new byte[20];
        ByteManipulator.Write(receivedData, 0, (ushort)4588);
        ByteManipulator.Write(receivedData, 2, (byte)5);
        ByteManipulator.Write(receivedData, 3, (byte)99);
        ulong           netId = 6;
        VoicePacketInfo info  = transport.ProcessReceivedData(buffer, receivedData, 0, 20, netId);

        Assert.That(info.ValidPacketInfo, Is.Not.False);
    }
    public void TestProcessReceivedDataFormatRedLight()
    {
        BytePacket buffer = new BytePacket(20);

        byte[] receivedData = new byte[20];
        ByteManipulator.Write(receivedData, 0, (ushort)4588);
        ByteManipulator.Write(receivedData, 2, (byte)5);
        ByteManipulator.Write(receivedData, 3, (byte)199);
        ulong           netId = 6;
        VoicePacketInfo info  = transport.ProcessReceivedData(buffer, receivedData, 0, 20, netId);

        Assert.That((byte)info.Format, Is.Not.EqualTo(99));
    }
    public void TestProcessReceivedDataChann()
    {
        BytePacket buffer = new BytePacket(20);

        byte[] receivedData = new byte[20];
        ByteManipulator.Write(receivedData, 0, (ushort)4588);
        ByteManipulator.Write(receivedData, 2, (byte)5);
        ByteManipulator.Write(receivedData, 3, (byte)99);
        ulong           netId = 5;
        VoicePacketInfo info  = transport.ProcessReceivedData(buffer, receivedData, 0, 20, netId);

        Assert.That(info.Channels, Is.EqualTo(5));
    }
        public uint GetTransactionCount(byte[] publicKey)
        {
            var nonceBytes = nonceCoder.Get(publicKey);

            uint ret = 0;

            if (nonceBytes != null)
            {
                ret = ByteManipulator.GetUInt32(nonceBytes) + 1;
            }

            return(ret);
        }
        public bool ApplyDiff(Dictionary <byte[], Tuple <long, LargeInteger> > diff)
        {
            var ret = true;

            foreach (var kvp in diff)
            {
                if (kvp.Value.Item2 < 0)
                {
                    ret = false;
                    break;
                }
            }

            if (ret)
            {
                foreach (var kvp in diff)
                {
                    //update balances
                    if (balanceCoder.ContainsKey(kvp.Key))
                    {
                        balanceCoder.Replace(kvp.Key, kvp.Value.Item2.GetBytes());
                    }
                    else
                    {
                        balanceCoder.Add(kvp.Key, kvp.Value.Item2.GetBytes());
                    }

                    //update nonces
                    if (nonceCoder.ContainsKey(kvp.Key))
                    {
                        if (kvp.Value.Item1 >= 0)
                        {
                            nonceCoder.Replace(kvp.Key, ByteManipulator.GetBytes((uint)kvp.Value.Item1));
                        }
                        else
                        {
                            nonceCoder.Remove(kvp.Key);
                        }
                    }
                    else
                    {
                        if (kvp.Value.Item1 >= 0)
                        {
                            nonceCoder.Add(kvp.Key, ByteManipulator.GetBytes((uint)kvp.Value.Item1));
                        }
                    }
                }
            }

            return(ret);
        }
    //OnEnemyDeath will be called when command EnemyDeath is received from host
    private void OnEnemyDeath(byte[] data, uint length, CSteamID senderId)
    {
        int   Id  = ByteManipulator.ReadInt32(data, 0);
        Enemy obj = netEntities[Id].GetComponent <Enemy>();

        if (!obj)
        {
            throw new NullReferenceException("NetId does not correspond to an enemy");
        }

        obj.Reset();
        obj.Pool.Recycle(obj.gameObject);
        OnEnemyRemoveEvent.Raise(obj.GetComponent <GameNetworkObject>());
    }
    public void TestProcessReceivedDataPayloadIntegrity2RedLight()
    {
        BytePacket buffer = new BytePacket(20);

        byte[] receivedData = new byte[20];
        ByteManipulator.Write(receivedData, 0, (ushort)4588);
        ByteManipulator.Write(receivedData, 2, (byte)5);
        ByteManipulator.Write(receivedData, 3, (byte)99);
        ByteManipulator.Write(receivedData, 15, (int)77889);
        ulong netId = 5;

        transport.ProcessReceivedData(buffer, receivedData, 0, 20, netId);
        Assert.That(buffer.ReadInt(11), Is.Not.EqualTo(0));
    }
    void ReceiveChatMessage(LobbyChatMsg_t cb)
    {
        CSteamID       user;
        EChatEntryType chatType;
        int            dataLenght = SteamMatchmaking.GetLobbyChatEntry((CSteamID)cb.m_ulSteamIDLobby, (int)cb.m_iChatID, out user, chatData, chatData.Length, out chatType);

        string name = SteamFriends.GetFriendPersonaName(user);

        if (chatType == EChatEntryType.k_EChatEntryTypeChatMsg)
        {
            int    n;
            string message = ByteManipulator.ReadString(chatData, 0, encoder, out n);
            Text.text += "\n" + name + " : " + message;
        }
    }
Beispiel #30
0
        public byte[] Get(byte[] key)
        {
            key = ByteManipulator.BigEndianTruncate(key, KeyLength);

            byte[] ret   = null;
            var    index = GetIndex(key);

            if (index != -1)
            {
                ret = LinearCoder.Read(index);
                ret = ArrayManipulator.SubArray(ret, KeyLength, DataLength);
            }

            return(ret);
        }