refactor: simplify kgm decipher

This commit is contained in:
鲁树人 2024-09-18 22:05:05 +01:00
parent 1b116a8db3
commit 73bad51e8e

View File

@ -1,5 +1,5 @@
import { DecipherInstance, DecipherOK, DecipherResult, Status } from '~/decrypt-worker/Deciphers'; import { DecipherInstance, DecipherOK, DecipherResult, Status } from '~/decrypt-worker/Deciphers';
import { KuGouDecipher, KuGouHeader } from '@unlock-music/crypto'; import { KuGou } from '@unlock-music/crypto';
import type { DecryptCommandOptions } from '~/decrypt-worker/types.ts'; import type { DecryptCommandOptions } from '~/decrypt-worker/types.ts';
import { chunkBuffer } from '~/decrypt-worker/util/buffer.ts'; import { chunkBuffer } from '~/decrypt-worker/util/buffer.ts';
@ -7,12 +7,10 @@ export class KugouMusicDecipher implements DecipherInstance {
cipherName = 'Kugou'; cipherName = 'Kugou';
async decrypt(buffer: Uint8Array, _options: DecryptCommandOptions): Promise<DecipherResult | DecipherOK> { async decrypt(buffer: Uint8Array, _options: DecryptCommandOptions): Promise<DecipherResult | DecipherOK> {
let kgm: KuGouDecipher | undefined; let kgm: KuGou | undefined;
let header: KuGouHeader | undefined;
try { try {
header = KuGouHeader.parse(buffer.subarray(0, 0x400)); kgm = KuGou.from_header(buffer.subarray(0, 0x400));
kgm = new KuGouDecipher(header);
const audioBuffer = new Uint8Array(buffer.subarray(0x400)); const audioBuffer = new Uint8Array(buffer.subarray(0x400));
for (const [block, offset] of chunkBuffer(audioBuffer)) { for (const [block, offset] of chunkBuffer(audioBuffer)) {
@ -26,7 +24,6 @@ export class KugouMusicDecipher implements DecipherInstance {
}; };
} finally { } finally {
kgm?.free(); kgm?.free();
header?.free();
} }
} }