diff --git a/dub.json b/dub.json index 65447f9..3f6a72a 100644 --- a/dub.json +++ b/dub.json @@ -4,6 +4,7 @@ ], "copyright": "Copyright © 2023, Tristan B. Kildaire", "dependencies": { + "niknaks": ">=0.3.0", "river": ">=0.3.6" }, "description": "A simple message format for automatically length-prefixing messages over any socket or stream", @@ -11,4 +12,4 @@ "license": "LGPL v3", "name": "bformat", "targetType": "library" -} +} \ No newline at end of file diff --git a/source/bformat/client.d b/source/bformat/client.d index 32fc378..60adbf6 100644 --- a/source/bformat/client.d +++ b/source/bformat/client.d @@ -6,6 +6,7 @@ module bformat.client; import std.socket : Socket; import river.core; import river.impls.sock : SockStream; +import niknaks.bits : bytesToIntegral, order, Order; /** * Bformat client to encode and decode via a @@ -76,27 +77,9 @@ public class BClient /* Response message length */ uint messageLength; - /* Little endian version you simply read if off the bone (it's already in the correct order) */ - version(LittleEndian) - { - messageLength = *cast(int*)messageLengthBytes.ptr; - } - - /* Big endian requires we byte-sapped the little-endian encoded number */ - version(BigEndian) - { - byte[] swappedLength; - swappedLength.length = 4; - - swappedLength[0] = messageLengthBytes[3]; - swappedLength[1] = messageLengthBytes[2]; - swappedLength[2] = messageLengthBytes[1]; - swappedLength[3] = messageLengthBytes[0]; - - messageLength = *cast(int*)swappedLength.ptr; - } - - + /* Order the bytes into Little endian (only flips if host order doesn't match LE) */ + messageLength = order(bytesToIntegral!(uint)(cast(ubyte[])messageLengthBytes), Order.LE); + /* Read the full message */ receiveBuffer.length = messageLength; try diff --git a/source/bformat/marshall.d b/source/bformat/marshall.d index fa0515b..84be44b 100644 --- a/source/bformat/marshall.d +++ b/source/bformat/marshall.d @@ -3,6 +3,8 @@ */ module bformat.marshall; +import niknaks.bits : toBytes, bytesToIntegral, order, Order; + /** * Decodes the provided bformat message into the * message itself @@ -22,25 +24,8 @@ public byte[] decodeMessage(byte[] bformatBytes) /* Response message length */ uint messageLength; - /* Little endian version you simply read if off the bone (it's already in the correct order) */ - version(LittleEndian) - { - messageLength = *cast(int*)messageLengthBytes.ptr; - } - - /* Big endian requires we byte-sapped the little-endian encoded number */ - version(BigEndian) - { - byte[] swappedLength; - swappedLength.length = 4; - - swappedLength[0] = messageLengthBytes[3]; - swappedLength[1] = messageLengthBytes[2]; - swappedLength[2] = messageLengthBytes[1]; - swappedLength[3] = messageLengthBytes[0]; - - messageLength = *cast(int*)swappedLength.ptr; - } + /* Order the bytes into Little endian (only flips if host order doesn't match LE) */ + messageLength = order(bytesToIntegral!(uint)(cast(ubyte[])messageLengthBytes), Order.LE); /* Read the full message */ @@ -62,27 +47,7 @@ public byte[] encodeBformat(byte[] message) byte[] messageBuffer; /* Encode the 4 byte message length header (little endian) */ - int payloadLength = cast(int)message.length; - byte* lengthBytes = cast(byte*)&payloadLength; - - /* On little endian simply get the bytes as is (it would be encoded as little endian) */ - version(LittleEndian) - { - messageBuffer ~= *(lengthBytes+0); - messageBuffer ~= *(lengthBytes+1); - messageBuffer ~= *(lengthBytes+2); - messageBuffer ~= *(lengthBytes+3); - } - - /* On Big Endian you must swap the big-endian-encoded number to be in little endian ordering */ - version(BigEndian) - { - messageBuffer ~= *(lengthBytes+3); - messageBuffer ~= *(lengthBytes+2); - messageBuffer ~= *(lengthBytes+1); - messageBuffer ~= *(lengthBytes+0); - } - + messageBuffer ~= cast(byte[])toBytes(order(cast(int)message.length, Order.LE)); /* Add the message to the buffer */ messageBuffer ~= cast(byte[])message;