ebc
This commit is contained in:
Malloc of Kuzkycyziklistan 2017-05-01 07:18:27 -05:00
parent bfb2714863
commit c204d1ded5
2 changed files with 37 additions and 8 deletions

View file

@ -28,7 +28,7 @@
</system.data> </system.data>
<connectionStrings> <connectionStrings>
<add name="ScapeDbLink" <add name="ScapeDbLink"
connectionString="metadata=res://*/DAL.ScapeDb.csdl|res://*/DAL.ScapeDb.ssdl|res://*/DAL.ScapeDb.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=C:\Users\Alec\Desktop\GitHub\circlescape\server\scape.db&quot;" connectionString="metadata=res://*/DAL.ScapeDb.csdl|res://*/DAL.ScapeDb.ssdl|res://*/DAL.ScapeDb.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=scape.db&quot;"
providerName="System.Data.EntityClient" /> providerName="System.Data.EntityClient" />
</connectionStrings> </connectionStrings>
</configuration> </configuration>

View file

@ -31,6 +31,21 @@ namespace Kneesocks {
} }
} }
public byte[] GetRaw() {
var headerSize = 2L;
var bodySize = Content.LongLength;
if(bodySize >= 0x7E && bodySize <= 0xFFFF)
headerSize += 2;
else if(bodySize > 0xFFFF)
headerSize += 8;
if(IsMasked)
headerSize += 4;
var returnValue = new byte[headerSize + bodySize];
return returnValue;
}
public static Frame FromRaw(byte[] raw) { public static Frame FromRaw(byte[] raw) {
if(raw.Length < 2) if(raw.Length < 2)
throw new FormatException("Websocket frame cannot be less than two bytes long"); throw new FormatException("Websocket frame cannot be less than two bytes long");
@ -46,16 +61,16 @@ namespace Kneesocks {
Reserved = (byte)((raw[0] & 0x70) >> 4) Reserved = (byte)((raw[0] & 0x70) >> 4)
}; };
ulong frameLength = raw[1] & 0x7Ful; ulong bodyLength = raw[1] & 0x7Ful;
int lengthOffset = int lengthOffset =
frameLength < 126 bodyLength < 126
? 1 ? 1
: (frameLength == 126 ? 3 : 9); : (bodyLength == 126 ? 3 : 9);
for(var i = lengthOffset - 1; i > 0; --i) { for(var i = lengthOffset - 1; i > 0; --i) {
var bytePos = lengthOffset - 1 - i; var bytePos = lengthOffset - 1 - i;
frameLength &= 0xFFul << bytePos; bodyLength &= 0xFFul << bytePos;
frameLength |= (ulong)raw[2 + i] << bytePos; bodyLength |= (ulong)raw[2 + i] << bytePos;
} }
if(returnFrame.IsMasked) { if(returnFrame.IsMasked) {
@ -64,8 +79,22 @@ namespace Kneesocks {
lengthOffset += 4; lengthOffset += 4;
} }
if(raw.LongLength + lengthOffset + 1 < frameLength) ulong expectedFrameLength = bodyLength + (uint)lengthOffset + 1;
throw new FormatException("Raw frame length passed in is undersized ") if(expectedFrameLength < (ulong)raw.LongLength)
throw new FormatException("Raw frame length ("+ expectedFrameLength +") is less than described size ("+ (ulong)raw.LongLength +")");
ulong counter = 0;
returnFrame.Content = raw.Skip(lengthOffset + 1)
.TakeWhile(x => counter++ < bodyLength)
.ToArray();
if(returnFrame.IsMasked) {
counter = 0;
returnFrame.Content =
returnFrame.Content.Select(
x => (byte)(x ^ returnFrame.Mask[counter++ % 4])
).ToArray();
}
return returnFrame; return returnFrame;
} }