From bb8c0647ffaaed9626d24025a110fd8aeafe69c9 Mon Sep 17 00:00:00 2001 From: Malloc of Kuzkycyziklistan Date: Sat, 29 Apr 2017 08:06:20 -0500 Subject: [PATCH] the only reason i'm committing this is because i have to leave ere --- server/Kneesocks/Frame.cs | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/server/Kneesocks/Frame.cs b/server/Kneesocks/Frame.cs index 781a22d..f291ebb 100644 --- a/server/Kneesocks/Frame.cs +++ b/server/Kneesocks/Frame.cs @@ -18,21 +18,42 @@ namespace Kneesocks { public kOpcode Opcode { get; set; } public bool IsFinal { get; set; } public bool IsMasked { get; set; } - public byte[] Mask { get; set; } + public byte[] Mask { get; set; } = new byte[] { 0, 0, 0, 0 }; public byte Reserved { get; set; } public byte[] Content { get; set; } public byte[] MaskedContent { get { - byte[] ret = new byte[Content.Length]; + byte[] returnValue = new byte[Content.Length]; for(var i = 0; i < Content.Length; ++i) - ret[i] = (byte)(Content[i] ^ Mask[i % 4]); - return ret; + returnValue[i] = (byte)(Content[i] ^ Mask[i % 4]); + return returnValue; } } - public static FromRaw(byte[] raw) { - if(raw.Length < ) + public static Frame FromRaw(byte[] raw) { + if(raw.Length < 2) + throw new FormatException("Websocket frame cannot be less than two bytes long"); + + var rawOpcode = raw[0] & 0x0F; + if(!Enum.IsDefined(typeof(kOpcode), rawOpcode)) + throw new FormatException("Opcode '"+ rawOpcode.ToString("0x{0:X}") +"' not understood"); + + var returnFrame = new Frame { + IsFinal = (raw[0] & 0x80) != 0, + Opcode = (kOpcode)rawOpcode, + IsMasked = (raw[1] & 0x80) != 0, + Reserved = (byte)((raw[0] & 0x70) >> 4) + }; + + UInt64 frameLength = raw[1] & 0x7Ful; + int lengthOffset = + frameLength < 126 + ? 1 + : (frameLength == 126 ? 3 : 9); + if() + + return returnFrame; } } }