我有一个使用Frida读取内存返回的ArrayBuffer。我正在将ArrayBuffer转换为字符串,然后使用TextDecoder和TextEncoder返回到ArrayBuffer,但结果在此过程中被更改。解码和重新编码后的ArrayBuffer长度总是变大。是否有广泛的解码方式?
如何将ArrayBuffer解码为String,然后返回ArrayBuffer而不会失去完整性?
示例代码:
var arrayBuff = Memory.readByteArray(pointer,2000); //Get a 2,000 byte ArrayBuffer
console.log(arrayBuff.byteLength); //Always returns 2,000
var textDecoder = new TextDecoder("utf-8");
var textEncoder = new TextEncoder("utf-8");
//Decode and encode same data without making any changes
var decoded = textDecoder.decode(arrayBuff);
var encoded = textEncoder.encode(decoded);
console.log(encoded.byteLength); //Fluctuates between but always greater than 2,000
答案 0 :(得分:2)
TextDecoder
和TextEncoder
旨在与文字配合使用。
要将任意字节序列转换为字符串并将其转换回来,最好将每个字节视为单个字符。
var arrayBuff = Memory.readByteArray(pointer,2000); //Get a 2,000 byte ArrayBuffer
console.log(arrayBuff.byteLength); //Always returns 2,000
//Decode and encode same data without making any changes
var decoded = String.fromCharCode(...new Uint8Array(arrayBuff));
var encoded = Uint8Array.from([...decoded].map(ch => ch.charCodeAt())).buffer;
console.log(encoded.byteLength);
decoded
字符串与输入缓冲区的长度完全相同,可以使用正则表达式,字符串方法等轻松操作。但要注意在内存中占用两个或更多字节的Unicode字符(例如“ π“)将不再被识别,因为它们将导致与每个字节的代码相对应的字符的串联。