From beb635c403ebf905993bfa807a7e82af9035a5ac Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Fri, 4 Dec 2020 17:28:29 +0200 Subject: [PATCH] Byte swap when the numbers are encoded as Big Endian on BE platforms, such that they are in little endian (the protocol's spec) --- source/bmessage.d | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/source/bmessage.d b/source/bmessage.d index fbba8cf..299d522 100644 --- a/source/bmessage.d +++ b/source/bmessage.d @@ -138,10 +138,25 @@ public bool sendMessage(Socket recipient, byte[] message) /* Encode the 4 byte message length header (little endian) */ int payloadLength = cast(int)message.length; byte* lengthBytes = cast(byte*)&payloadLength; - messageBuffer ~= *(lengthBytes+0); - messageBuffer ~= *(lengthBytes+1); - messageBuffer ~= *(lengthBytes+2); - messageBuffer ~= *(lengthBytes+3); + + /* 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); + } + /* Add the message to the buffer */ messageBuffer ~= cast(byte[])message;