From 22357f11e253e8e6aa7f46b068a679306cc5c2a6 Mon Sep 17 00:00:00 2001 From: Parnic Date: Sun, 9 Feb 2020 14:28:03 -0600 Subject: [PATCH] Encapsulate alignment into a function to make things slightly easier This just returns the extra amount needed to add onto the string/array. Also fixed up writeSLArray() not adding slack like it should have been. This wasn't an issue previously because the only place that currently uses this function is the password feature which is a fixed 16byte length, so it was already aligned. An upcoming commit will be using this, however, so I needed the alignment to be correct. Finally, the read/write string functions were doing unnecessary conditional checks so I removed them. skipWrite() already does nothing if you tell it to skip 0, and adding 0 to readOffset won't do anything, so neither conditional is necessary at this point. --- messages/SLMessage.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/messages/SLMessage.js b/messages/SLMessage.js index 54c7749..2367551 100644 --- a/messages/SLMessage.js +++ b/messages/SLMessage.js @@ -27,17 +27,13 @@ exports.SLMessage = class SLMessage extends SmartBuffer { writeSLString(str) { this.writeInt32LE(str.length); this.writeString(str); - if (str.length % 4 !== 0) { - this.skipWrite(4 - str.length % 4); - } + this.skipWrite(SLMessage.slackForAlignment(str.length)); } readSLString() { var len = this.readInt32LE(); var str = this.readString(len); - if (len % 4 !== 0) { - this.readOffset += 4 - len % 4; - } + this.readOffset += SLMessage.slackForAlignment(len); return str; } @@ -48,9 +44,12 @@ exports.SLMessage = class SLMessage extends SmartBuffer { writeSLArray(arr) { this.writeInt32LE(arr.length); + for (var i = 0; i < arr.length; i++) { this.writeUInt8(arr[i]); } + + this.skipWrite(SLMessage.slackForAlignment(arr.length)); } skipWrite(num) { @@ -66,5 +65,9 @@ exports.SLMessage = class SLMessage extends SmartBuffer { this.dataLength = this.readInt32LE(); } + static slackForAlignment(val) { + return (4 - val % 4) % 4; + } + encode() {} };