2021-06-03 04:47:06 +00:00
|
|
|
import {
|
2021-12-18 13:55:31 +00:00
|
|
|
AudioMimeType,
|
|
|
|
GetArrayBuffer,
|
|
|
|
GetCoverFromFile,
|
|
|
|
GetMetaFromFile,
|
|
|
|
SniffAudioExt,
|
|
|
|
SplitFilename,
|
|
|
|
} from '@/decrypt/utils';
|
2021-06-03 05:00:35 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
import { Decrypt as QmcDecrypt, HandlerMap } from '@/decrypt/qmc';
|
2022-11-20 14:30:56 +00:00
|
|
|
import { DecryptQmcWasm } from '@/decrypt/qmc_wasm';
|
2021-06-03 05:00:35 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
import { DecryptResult } from '@/decrypt/entity';
|
2021-06-03 05:00:35 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
import { parseBlob as metaParseBlob } from 'music-metadata-browser';
|
2021-06-03 05:00:35 +00:00
|
|
|
|
2022-11-20 14:30:56 +00:00
|
|
|
export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string): Promise<DecryptResult> {
|
|
|
|
const buffer = await GetArrayBuffer(file);
|
|
|
|
|
|
|
|
let musicDecoded: Uint8Array | undefined;
|
|
|
|
if (globalThis.WebAssembly) {
|
|
|
|
console.log('qmc: using wasm decoder');
|
|
|
|
|
|
|
|
const qmcDecrypted = await DecryptQmcWasm(buffer, raw_ext);
|
|
|
|
// 若 qmc 检测失败,降级到 v1 再尝试一次
|
|
|
|
if (qmcDecrypted.success) {
|
|
|
|
musicDecoded = qmcDecrypted.data;
|
|
|
|
console.log('qmc wasm decoder suceeded');
|
|
|
|
} else {
|
|
|
|
console.warn('QmcWasm failed with error %s', qmcDecrypted.error || '(no error)');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!musicDecoded) {
|
|
|
|
musicDecoded = new Uint8Array(buffer);
|
|
|
|
let length = musicDecoded.length;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
musicDecoded[i] ^= 0xf4;
|
|
|
|
if (musicDecoded[i] <= 0x3f) musicDecoded[i] = musicDecoded[i] * 4;
|
|
|
|
else if (musicDecoded[i] <= 0x7f) musicDecoded[i] = (musicDecoded[i] - 0x40) * 4 + 1;
|
|
|
|
else if (musicDecoded[i] <= 0xbf) musicDecoded[i] = (musicDecoded[i] - 0x80) * 4 + 2;
|
|
|
|
else musicDecoded[i] = (musicDecoded[i] - 0xc0) * 4 + 3;
|
|
|
|
}
|
2021-12-18 13:55:31 +00:00
|
|
|
}
|
2022-11-20 14:30:56 +00:00
|
|
|
let ext = SniffAudioExt(musicDecoded, '');
|
2021-12-18 13:55:31 +00:00
|
|
|
const newName = SplitFilename(raw_filename);
|
|
|
|
let audioBlob: Blob;
|
|
|
|
if (ext !== '' || newName.ext === 'mp3') {
|
2022-11-20 14:30:56 +00:00
|
|
|
audioBlob = new Blob([musicDecoded], { type: AudioMimeType[ext] });
|
2021-12-18 13:55:31 +00:00
|
|
|
} else if (newName.ext in HandlerMap) {
|
2022-11-20 14:30:56 +00:00
|
|
|
audioBlob = new Blob([musicDecoded], { type: 'application/octet-stream' });
|
2021-12-18 13:55:31 +00:00
|
|
|
return QmcDecrypt(audioBlob, newName.name, newName.ext);
|
|
|
|
} else {
|
|
|
|
throw '不支持的QQ音乐缓存格式';
|
|
|
|
}
|
|
|
|
const tag = await metaParseBlob(audioBlob);
|
2022-11-21 22:40:02 +00:00
|
|
|
const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, String(tag.common.artists || tag.common.artist || ""));
|
2021-06-03 05:00:35 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
return {
|
|
|
|
title,
|
|
|
|
artist,
|
|
|
|
ext,
|
|
|
|
album: tag.common.album,
|
|
|
|
picture: GetCoverFromFile(tag),
|
|
|
|
file: URL.createObjectURL(audioBlob),
|
|
|
|
blob: audioBlob,
|
|
|
|
mime: AudioMimeType[ext],
|
|
|
|
};
|
2021-06-03 05:00:35 +00:00
|
|
|
}
|