From 83b2a11c80c7f8390bf2a5cdd0bab5474a5e991c Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 27 Mar 2023 15:40:20 +0200 Subject: [PATCH] Encoding - Added stub class `TaggedMessage` - Added constructor, static decoder (unimplemented), `encoder (implemented), getters and setters - Added module `tristanable.encoding` --- source/tristanable/encoding.d | 109 ++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 source/tristanable/encoding.d diff --git a/source/tristanable/encoding.d b/source/tristanable/encoding.d new file mode 100644 index 0000000..4461068 --- /dev/null +++ b/source/tristanable/encoding.d @@ -0,0 +1,109 @@ +module tristanable.encoding; + +public final class TaggedMessage +{ + private ulong tag; + private byte[] data; + + this(ulong tag, byte[] data) + { + this.tag = tag; + this.data = data; + } + + /** + * Decodes the wire-formatted tristanable bytes into an instance + * of TaggedMessage whereby the tag and data can be seperately + * accessed and manipulated + * + * Params: + * encodedMessage = the wire-format encoded bytes + * Returns: an instance of TaggedMessage + */ + public static TaggedMessage decode(byte[] encodedMessage) + { + /* The decoded message */ + TaggedMessage decodedMessage; + + /* Decoded tag */ + + + + // TODO: Implement me + + return decodedMessage; + } + + /** + * Encodes the tagged message into the tristanable + * wire format ready for transmission + * + * Returns: the encoded bytes + */ + public byte[] encode() + { + /* The encoded bytes */ + byte[] encodedMessage; + + /* If on little endian, then dump 64 bit as is - little endian */ + version(LittleEndian) + { + /* Base (little first) of tag */ + byte* basePtr = cast(byte*)&tag; + + encodedMessage ~= *(basePtr+0); + encodedMessage ~= *(basePtr+1); + encodedMessage ~= *(basePtr+2); + encodedMessage ~= *(basePtr+3); + encodedMessage ~= *(basePtr+4); + encodedMessage ~= *(basePtr+5); + encodedMessage ~= *(basePtr+6); + encodedMessage ~= *(basePtr+7); + } + /* If on big endian, then traverse 64-bit number in reverse - and tack on */ + else version(BigEndian) + { + /* Base (biggest first) of tag */ + byte* highPtr = cast(byte*)&tag; + + encodedMessage ~= *(highPtr+7); + encodedMessage ~= *(highPtr+6); + encodedMessage ~= *(highPtr+5); + encodedMessage ~= *(highPtr+4); + encodedMessage ~= *(highPtr+3); + encodedMessage ~= *(highPtr+2); + encodedMessage ~= *(highPtr+1); + encodedMessage ~= *(highPtr+0); + } + /* Hail marry, mother of God, pray for our sinners, now and at the our of our death Amen */ + else + { + pragma(msg, "Not feeling scrumptious homeslice 😎️"); + } + + /* Tack on the data */ + encodedMessage ~= data; + + return encodedMessage; + } + + public byte[] getPayload() + { + return data; + } + + public ulong getTag() + { + return tag; + } + + public void setPayload(byte[] newPayload) + { + this.data = newPayload; + } + + public void setTag(ulong newTag) + { + this.tag = newTag; + } +} \ No newline at end of file