Replace buffer.slice() with buffer.subarray()

This commit is contained in:
Fedor Indutny
2025-06-09 14:37:30 -07:00
committed by GitHub
parent 4a6e2d297b
commit b0634f9a9d
19 changed files with 54 additions and 45 deletions

View File

@@ -143,7 +143,7 @@ export class ParseContactsTransform extends Transform {
const spaceLeftAfterRead = reader.len - (reader.pos + attachmentSize);
if (spaceLeftAfterRead >= 0) {
// We've read enough data to read the entire attachment
const avatarData = reader.buf.slice(
const avatarData = reader.buf.subarray(
reader.pos,
reader.pos + attachmentSize
);

View File

@@ -1659,7 +1659,7 @@ export default class MessageReceiver
#unpad(paddedPlaintext: Uint8Array): Uint8Array {
for (let i = paddedPlaintext.length - 1; i >= 0; i -= 1) {
if (paddedPlaintext[i] === 0x80) {
return new Uint8Array(paddedPlaintext.slice(0, i));
return new Uint8Array(paddedPlaintext.subarray(0, i));
}
if (paddedPlaintext[i] !== 0x00) {
throw new Error('Invalid padding');

View File

@@ -47,10 +47,10 @@ class ProvisioningCipherInner {
throw new Error('Bad version number on ProvisioningMessage');
}
const iv = message.slice(1, 16 + 1);
const mac = message.slice(message.byteLength - 32, message.byteLength);
const ivAndCiphertext = message.slice(0, message.byteLength - 32);
const ciphertext = message.slice(16 + 1, message.byteLength - 32);
const iv = message.subarray(1, 16 + 1);
const mac = message.subarray(message.byteLength - 32, message.byteLength);
const ivAndCiphertext = message.subarray(0, message.byteLength - 32);
const ciphertext = message.subarray(16 + 1, message.byteLength - 32);
if (!this.keyPair) {
throw new Error('ProvisioningCipher.decrypt: No keypair!');

View File

@@ -211,7 +211,7 @@ function decodeSingleResponse(
i < response.e164PniAciTriples.length;
i += TRIPLE_BYTE_SIZE
) {
const tripleBytes = response.e164PniAciTriples.slice(
const tripleBytes = response.e164PniAciTriples.subarray(
i,
i + TRIPLE_BYTE_SIZE
);
@@ -221,13 +221,13 @@ function decodeSingleResponse(
);
let offset = 0;
const e164Bytes = tripleBytes.slice(offset, offset + E164_BYTE_SIZE);
const e164Bytes = tripleBytes.subarray(offset, offset + E164_BYTE_SIZE);
offset += E164_BYTE_SIZE;
const pniBytes = tripleBytes.slice(offset, offset + UUID_BYTE_SIZE);
const pniBytes = tripleBytes.subarray(offset, offset + UUID_BYTE_SIZE);
offset += UUID_BYTE_SIZE;
const aciBytes = tripleBytes.slice(offset, offset + UUID_BYTE_SIZE);
const aciBytes = tripleBytes.subarray(offset, offset + UUID_BYTE_SIZE);
offset += UUID_BYTE_SIZE;
const e164Long = Long.fromBytesBE(Array.from(e164Bytes));