2021-05-23 15:06:21 +00:00
|
|
|
import {
|
2021-12-18 13:55:31 +00:00
|
|
|
AudioMimeType,
|
|
|
|
BytesHasPrefix,
|
|
|
|
GetArrayBuffer,
|
|
|
|
GetCoverFromFile,
|
|
|
|
GetMetaFromFile,
|
|
|
|
SniffAudioExt,
|
|
|
|
} from '@/decrypt/utils';
|
|
|
|
import { parseBlob as metaParseBlob } from 'music-metadata-browser';
|
|
|
|
import { DecryptResult } from '@/decrypt/entity';
|
2022-11-20 14:30:56 +00:00
|
|
|
import { DecryptKgmWasm } from '@/decrypt/kgm_wasm';
|
2022-04-27 23:36:20 +00:00
|
|
|
import { decryptKgmByteAtOffsetV2, decryptVprByteAtOffset } from '@jixun/kugou-crypto/dist/utils/decryptionHelper';
|
2020-07-16 14:22:32 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
//prettier-ignore
|
2020-07-16 14:22:32 +00:00
|
|
|
const VprHeader = [
|
2021-12-18 13:55:31 +00:00
|
|
|
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43,
|
|
|
|
0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31
|
|
|
|
]
|
|
|
|
//prettier-ignore
|
2020-07-16 14:22:32 +00:00
|
|
|
const KgmHeader = [
|
2021-12-18 13:55:31 +00:00
|
|
|
0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B,
|
|
|
|
0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14
|
|
|
|
]
|
2021-05-23 17:30:38 +00:00
|
|
|
|
|
|
|
export async function Decrypt(file: File, raw_filename: string, raw_ext: string): Promise<DecryptResult> {
|
2022-11-20 14:30:56 +00:00
|
|
|
const oriData = await GetArrayBuffer(file);
|
2021-12-18 13:55:31 +00:00
|
|
|
if (raw_ext === 'vpr') {
|
2022-11-20 14:30:56 +00:00
|
|
|
if (!BytesHasPrefix(new Uint8Array(oriData), VprHeader)) throw Error('Not a valid vpr file!');
|
2021-12-18 13:55:31 +00:00
|
|
|
} else {
|
2022-11-20 14:30:56 +00:00
|
|
|
if (!BytesHasPrefix(new Uint8Array(oriData), KgmHeader)) throw Error('Not a valid kgm(a) file!');
|
2021-12-18 13:55:31 +00:00
|
|
|
}
|
2022-11-20 14:30:56 +00:00
|
|
|
let musicDecoded: Uint8Array | undefined;
|
|
|
|
if (globalThis.WebAssembly) {
|
|
|
|
console.log('kgm: using wasm decoder');
|
2021-05-23 17:30:38 +00:00
|
|
|
|
2022-11-20 14:30:56 +00:00
|
|
|
const kgmDecrypted = await DecryptKgmWasm(oriData, raw_ext);
|
|
|
|
if (kgmDecrypted.success) {
|
|
|
|
musicDecoded = kgmDecrypted.data;
|
|
|
|
console.log('kgm wasm decoder suceeded');
|
|
|
|
} else {
|
2022-11-20 16:33:03 +00:00
|
|
|
console.warn('KgmWasm failed with error %s', kgmDecrypted.error || '(unknown error)');
|
2022-11-20 14:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!musicDecoded) {
|
|
|
|
musicDecoded = new Uint8Array(oriData);
|
|
|
|
let bHeaderLen = new DataView(musicDecoded.slice(0x10, 0x14).buffer);
|
|
|
|
let headerLen = bHeaderLen.getUint32(0, true);
|
|
|
|
|
|
|
|
let key1 = Array.from(musicDecoded.slice(0x1c, 0x2c));
|
|
|
|
key1.push(0);
|
2020-07-16 14:22:32 +00:00
|
|
|
|
2022-11-20 14:30:56 +00:00
|
|
|
musicDecoded = musicDecoded.slice(headerLen);
|
|
|
|
let dataLen = musicDecoded.length;
|
2020-07-16 14:22:32 +00:00
|
|
|
|
2022-11-20 14:30:56 +00:00
|
|
|
const decryptByte = raw_ext === 'vpr' ? decryptVprByteAtOffset : decryptKgmByteAtOffsetV2;
|
|
|
|
for (let i = 0; i < dataLen; i++) {
|
|
|
|
musicDecoded[i] = decryptByte(musicDecoded[i], key1, i);
|
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
}
|
2020-07-16 14:22:32 +00:00
|
|
|
|
2022-11-20 14:30:56 +00:00
|
|
|
const ext = SniffAudioExt(musicDecoded);
|
2021-12-18 13:55:31 +00:00
|
|
|
const mime = AudioMimeType[ext];
|
2022-11-20 14:30:56 +00:00
|
|
|
let musicBlob = new Blob([musicDecoded], { type: mime });
|
2021-12-18 13:55:31 +00:00
|
|
|
const musicMeta = await metaParseBlob(musicBlob);
|
2022-11-20 14:30:56 +00:00
|
|
|
const { title, artist } = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artists == undefined ? musicMeta.common.artist : musicMeta.common.artists.toString());
|
2021-12-18 13:55:31 +00:00
|
|
|
return {
|
|
|
|
album: musicMeta.common.album,
|
|
|
|
picture: GetCoverFromFile(musicMeta),
|
|
|
|
file: URL.createObjectURL(musicBlob),
|
|
|
|
blob: musicBlob,
|
|
|
|
ext,
|
|
|
|
mime,
|
|
|
|
title,
|
|
|
|
artist,
|
|
|
|
};
|
2020-07-16 14:22:32 +00:00
|
|
|
}
|