System.Net.WebSockets.ManagedWebSocket.ApplyMask C# (CSharp) Method

ApplyMask() private static method

Applies a mask to a portion of a byte array.
private static ApplyMask ( byte toMask, int toMaskOffset, int mask, int maskIndex, long count ) : int
toMask byte The buffer to which the mask should be applied.
toMaskOffset int The offset into at which the mask should start to be applied.
mask int The four-byte mask, stored as an Int32.
maskIndex int The index into the mask.
count long The number of bytes to mask.
return int
        private static unsafe int ApplyMask(byte[] toMask, int toMaskOffset, int mask, int maskIndex, long count)
        {
            Debug.Assert(toMaskOffset <= toMask.Length - count, $"Unexpected inputs: {toMaskOffset}, {toMask.Length}, {count}");
            Debug.Assert(maskIndex < sizeof(int), $"Unexpected {nameof(maskIndex)}: {maskIndex}");

            byte* maskPtr = (byte*)&mask;
            fixed (byte* toMaskPtr = toMask)
            {
                byte* p = toMaskPtr + toMaskOffset;
                byte* end = p + count;
                while (p < end)
                {
                    *p++ ^= maskPtr[maskIndex];
                    maskIndex = (maskIndex + 1) & 3; // & 3 == faster % MaskLength
                }
                return maskIndex;
            }
        }

Same methods

ManagedWebSocket::ApplyMask ( byte toMask, int toMaskOffset, byte mask, int maskOffset, int maskOffsetIndex, long count ) : int