refactor: restore support for QMCv1.

(cherry picked from commit 19239f182d71e2e4362309f08706a91c00bb6bd1)
remotes/origin/HEAD
鲁树人 3 years ago
parent dbb5472f96
commit 733c1721ed

@ -1,4 +1,4 @@
import {QmcMask, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask"; import {QmcMask, QmcMaskGetDefault} from "./qmcMask";
import {toByteArray as Base64Decode} from 'base64-js' import {toByteArray as Base64Decode} from 'base64-js'
import { import {
AudioMimeType, AudioMimeType,
@ -9,7 +9,7 @@ import {
SniffAudioExt, WriteMetaToFlac, WriteMetaToMp3 SniffAudioExt, WriteMetaToFlac, WriteMetaToMp3
} from "@/decrypt/utils"; } from "@/decrypt/utils";
import {parseBlob as metaParseBlob} from "music-metadata-browser"; import {parseBlob as metaParseBlob} from "music-metadata-browser";
import {decryptMGG} from "./qmcv2"; import {DecryptQMCv2} from "./qmcv2";
import iconv from "iconv-lite"; import iconv from "iconv-lite";
@ -18,60 +18,49 @@ import {queryAlbumCover, queryKeyInfo, reportKeyUsage} from "@/utils/api";
interface Handler { interface Handler {
ext: string ext: string
detect: boolean version: number
handler(data?: Uint8Array): QmcMask | undefined
} }
export const HandlerMap: { [key: string]: Handler } = { export const HandlerMap: { [key: string]: Handler } = {
"mgg": {handler: QmcMaskDetectMgg, ext: "ogg", detect: true}, "mgg": {ext: "ogg", version: 2},
"mflac": {handler: QmcMaskDetectMflac, ext: "flac", detect: true}, "mgg1": {ext: "ogg", version: 2},
"mgg.cache": {handler: QmcMaskDetectMgg, ext: "ogg", detect: false}, "mflac": {ext: "flac", version: 2},
"mflac.cache": {handler: QmcMaskDetectMflac, ext: "flac", detect: false}, "mflac0": {ext: "flac", version: 2},
"qmc0": {handler: QmcMaskGetDefault, ext: "mp3", detect: false},
"qmc2": {handler: QmcMaskGetDefault, ext: "ogg", detect: false}, "qmc0": {ext: "mp3", version: 1},
"qmc3": {handler: QmcMaskGetDefault, ext: "mp3", detect: false}, "qmc2": {ext: "ogg", version: 1},
"qmcogg": {handler: QmcMaskGetDefault, ext: "ogg", detect: false}, "qmc3": {ext: "mp3", version: 1},
"qmcflac": {handler: QmcMaskGetDefault, ext: "flac", detect: false}, "qmcogg": {ext: "ogg", version: 1},
"bkcmp3": {handler: QmcMaskGetDefault, ext: "mp3", detect: false}, "qmcflac": {ext: "flac", version: 1},
"bkcflac": {handler: QmcMaskGetDefault, ext: "flac", detect: false}, "bkcmp3": {ext: "mp3", version: 1},
"tkm": {handler: QmcMaskGetDefault, ext: "m4a", detect: false}, "bkcflac": {ext: "flac", version: 1},
"666c6163": {handler: QmcMaskGetDefault, ext: "flac", detect: false}, "tkm": {ext: "m4a", version: 1},
"6d7033": {handler: QmcMaskGetDefault, ext: "mp3", detect: false}, "666c6163": {ext: "flac", version: 1},
"6f6767": {handler: QmcMaskGetDefault, ext: "ogg", detect: false}, "6d7033": {ext: "mp3", version: 1},
"6d3461": {handler: QmcMaskGetDefault, ext: "m4a", detect: false}, "6f6767": {ext: "ogg", version: 1},
"776176": {handler: QmcMaskGetDefault, ext: "wav", detect: false} "6d3461": {ext: "m4a", version: 1},
"776176": {ext: "wav", version: 1}
}; };
function mergeUint8(array: Uint8Array[]): Uint8Array {
// Get the total length of all arrays.
let length = 0;
array.forEach(item => {
length += item.length;
});
// Create a new array with total length and merge all source arrays.
let mergedArray = new Uint8Array(length);
let offset = 0;
array.forEach(item => {
mergedArray.set(item, offset);
offset += item.length;
});
return mergedArray;
}
export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string): Promise<DecryptResult> { export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string): Promise<DecryptResult> {
if (!(raw_ext in HandlerMap)) throw `Qmc cannot handle type: ${raw_ext}`; if (!(raw_ext in HandlerMap)) throw `Qmc cannot handle type: ${raw_ext}`;
const handler = HandlerMap[raw_ext]; const handler = HandlerMap[raw_ext];
const decodedParts = await decryptMGG(await file.arrayBuffer()); const fileBuffer = await GetArrayBuffer(file);
let musicDecoded = mergeUint8(decodedParts); let musicDecoded: Uint8Array;
if (handler.version === 1) {
const seed = QmcMaskGetDefault();
musicDecoded = seed.Decrypt(new Uint8Array(fileBuffer));
} else if (handler.version === 2) {
musicDecoded = await DecryptQMCv2(fileBuffer);
} else {
throw new Error(`不支持的加密版本: ${handler.version} (${raw_ext})`);
}
const ext = SniffAudioExt(musicDecoded, handler.ext); const ext = SniffAudioExt(musicDecoded, handler.ext);
const mime = AudioMimeType[ext]; const mime = AudioMimeType[ext];
let musicBlob = new Blob(decodedParts, {type: mime}); let musicBlob = new Blob([musicDecoded], {type: mime});
const musicMeta = await metaParseBlob(musicBlob); const musicMeta = await metaParseBlob(musicBlob);
for (let metaIdx in musicMeta.native) { for (let metaIdx in musicMeta.native) {

@ -1,21 +1,36 @@
import QMCCryptoModule from '@jixun/qmc2-crypto/QMC2-wasm-bundle'; import QMCCryptoModule from '@jixun/qmc2-crypto/QMC2-wasm-bundle';
// EOF Magic detection. // 检测文件末端使用的缓冲区大小
const DETECTION_SIZE = 40; const DETECTION_SIZE = 40;
// Process in 2m buffer size // 每次处理 2M 的数据
const DECRYPTION_BUF_SIZE = 2 * 1024 * 1024; const DECRYPTION_BUF_SIZE = 2 * 1024 * 1024;
function MergeUint8Array(array: Uint8Array[]): Uint8Array {
let length = 0;
array.forEach(item => {
length += item.length;
});
let mergedArray = new Uint8Array(length);
let offset = 0;
array.forEach(item => {
mergedArray.set(item, offset);
offset += item.length;
});
return mergedArray;
}
/** /**
* QMC2 * QMC2
* *
* Uint8Array * Uint8Array
* `null`
* @param {ArrayBuffer} mggBlob Blob * @param {ArrayBuffer} mggBlob Blob
* @param {string} name * @param {string} name
* @return {Promise<Uint8Array[]|null>} * @return {Promise<Uint8Array|null>}
*/ */
export async function decryptMGG(mggBlob: ArrayBuffer) { export async function DecryptQMCv2(mggBlob: ArrayBuffer) {
// 初始化模组 // 初始化模组
const QMCCrypto = await QMCCryptoModule(); const QMCCrypto = await QMCCryptoModule();
@ -44,11 +59,13 @@ export async function decryptMGG(mggBlob: ArrayBuffer) {
QMCCrypto._free(pDetectionBuf); QMCCrypto._free(pDetectionBuf);
QMCCrypto._free(pDetectionResult); QMCCrypto._free(pDetectionResult);
if (detectOK) { if (!detectOK) {
throw new Error("解密失败: \n " + detectionError);
}
// 计算解密后文件的大小。 // 计算解密后文件的大小。
// 之前得到的 position 为相对当前检测数据起点的偏移。 // 之前得到的 position 为相对当前检测数据起点的偏移。
const decryptedSize = mggBlob.byteLength - DETECTION_SIZE + position; const decryptedSize = mggBlob.byteLength - DETECTION_SIZE + position;
// $prog.max = decryptedSize;
// 提取嵌入到文件的 EKey // 提取嵌入到文件的 EKey
const ekey = new Uint8Array( const ekey = new Uint8Array(
@ -79,17 +96,9 @@ export async function decryptMGG(mggBlob: ArrayBuffer) {
offset += blockSize; offset += blockSize;
bytesToDecrypt -= blockSize; bytesToDecrypt -= blockSize;
// $prog.value = offset;
// 避免网页卡死,让 event loop 处理一下其它事件。
// Worker 应该不需要等待也可以?
// await new Promise((resolve) => setTimeout(resolve));
} }
QMCCrypto._free(buf); QMCCrypto._free(buf);
hCrypto.delete(); hCrypto.delete();
return decryptedParts; return MergeUint8Array(decryptedParts);
} else {
throw new Error("ERROR: could not decrypt\n " + detectionError);
}
} }
Loading…
Cancel
Save