refactor: implement a more efficient buffer decryption algorithm for qmc cache.

This commit is contained in:
鲁树人 2022-11-21 18:15:16 +00:00
parent c3b75c8fea
commit 46415e9eea

View File

@ -16,11 +16,9 @@ import { parseBlob as metaParseBlob } from 'music-metadata-browser';
export function DecryptBuffer(buffer: Uint8Array | Buffer) {
let length = buffer.byteLength;
for (let i = 0; i < length; i++) {
buffer[i] ^= 0xf4;
if (buffer[i] <= 0x3f) buffer[i] = buffer[i] * 4;
else if (buffer[i] <= 0x7f) buffer[i] = (buffer[i] - 0x40) * 4 + 1;
else if (buffer[i] <= 0xbf) buffer[i] = (buffer[i] - 0x80) * 4 + 2;
else buffer[i] = (buffer[i] - 0xc0) * 4 + 3;
let byte = buffer[i] ^ 0xf4; // xor 0xf4
byte = ((byte & 0b0011_1111) << 2) | (byte >> 6); // rol 2
buffer[i] = byte;
}
}