From d3de91de851f185966010f0523d0adb31cb5fa64 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Thu, 9 Jun 2022 10:12:19 +0100 Subject: [PATCH] Modify stream library to support reading until a null byte --- src/core/lib/Stream.mjs | 6 ++++-- src/core/operations/ELFInfo.mjs | 5 +---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/core/lib/Stream.mjs b/src/core/lib/Stream.mjs index b2a183f4..18ce71c3 100644 --- a/src/core/lib/Stream.mjs +++ b/src/core/lib/Stream.mjs @@ -48,12 +48,14 @@ export default class Stream { * Interpret the following bytes as a string, stopping at the next null byte or * the supplied limit. * - * @param {number} numBytes + * @param {number} [numBytes=-1] * @returns {string} */ - readString(numBytes) { + readString(numBytes=-1) { if (this.position > this.length) return undefined; + if (numBytes === -1) numBytes = this.length - this.position; + let result = ""; for (let i = this.position; i < this.position + numBytes; i++) { const currentByte = this.bytes[i]; diff --git a/src/core/operations/ELFInfo.mjs b/src/core/operations/ELFInfo.mjs index 9153e24f..e5e63094 100644 --- a/src/core/operations/ELFInfo.mjs +++ b/src/core/operations/ELFInfo.mjs @@ -66,10 +66,7 @@ class ELFInfo extends Operation { const preMove = stream.position; stream.moveTo(namesOffset + nameOffset); - let nameResult = ""; - let elem = 0; - while ((elem = stream.readInt(1, endianness)) !== 0) - nameResult += String.fromCharCode(elem); + let nameResult = stream.readString(); stream.moveTo(preMove); return nameResult; }