瀏覽代碼

Modify stream library to support reading until a null byte

n1073645 3 年之前
父節點
當前提交
d3de91de85
共有 2 個文件被更改,包括 5 次插入6 次删除
  1. 4 2
      src/core/lib/Stream.mjs
  2. 1 4
      src/core/operations/ELFInfo.mjs

+ 4 - 2
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];

+ 1 - 4
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;
         }