|
|
@ -1,14 +1,10 @@
|
|
|
|
import QMCCryptoModule from '@jixun/qmc2-crypto/QMC2-wasm-bundle';
|
|
|
|
import QmcCryptoModule from '@/QmcWasm/QmcWasmBundle';
|
|
|
|
import { MergeUint8Array } from '@/utils/MergeUint8Array';
|
|
|
|
import { MergeUint8Array } from '@/utils/MergeUint8Array';
|
|
|
|
import { QMCCrypto } from '@jixun/qmc2-crypto/QMCCrypto';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检测文件末端使用的缓冲区大小
|
|
|
|
|
|
|
|
const DETECTION_SIZE = 40;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 每次处理 2M 的数据
|
|
|
|
// 每次处理 2M 的数据
|
|
|
|
const DECRYPTION_BUF_SIZE = 2 * 1024 * 1024;
|
|
|
|
const DECRYPTION_BUF_SIZE = 2 *1024 * 1024;
|
|
|
|
|
|
|
|
|
|
|
|
export interface QMC2DecryptionResult {
|
|
|
|
export interface QMCDecryptionResult {
|
|
|
|
success: boolean;
|
|
|
|
success: boolean;
|
|
|
|
data: Uint8Array;
|
|
|
|
data: Uint8Array;
|
|
|
|
songId: string | number;
|
|
|
|
songId: string | number;
|
|
|
@ -16,96 +12,62 @@ export interface QMC2DecryptionResult {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 解密一个 QMC2 加密的文件。
|
|
|
|
* 解密一个 QMC 加密的文件。
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 如果检测并解密成功,返回解密后的 Uint8Array 数据。
|
|
|
|
* 如果检测并解密成功,返回解密后的 Uint8Array 数据。
|
|
|
|
* @param {ArrayBuffer} mggBlob 读入的文件 Blob
|
|
|
|
* @param {ArrayBuffer} qmcBlob 读入的文件 Blob
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export async function DecryptQMCWasm(mggBlob: ArrayBuffer): Promise<QMC2DecryptionResult> {
|
|
|
|
export async function DecryptQmcWasm(qmcBlob: ArrayBuffer, ext: string): Promise<QMCDecryptionResult> {
|
|
|
|
const result: QMC2DecryptionResult = { success: false, data: new Uint8Array(), songId: 0, error: '' };
|
|
|
|
const result: QMCDecryptionResult = { success: false, data: new Uint8Array(), songId: 0, error: '' };
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化模组
|
|
|
|
// 初始化模组
|
|
|
|
let QMCCrypto: QMCCrypto;
|
|
|
|
let QmcCrypto: any;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
QMCCrypto = await QMCCryptoModule();
|
|
|
|
QmcCrypto = await QmcCryptoModule();
|
|
|
|
} catch (err: any) {
|
|
|
|
} catch (err: any) {
|
|
|
|
result.error = err?.message || 'wasm 加载失败';
|
|
|
|
result.error = err?.message || 'wasm 加载失败';
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!QmcCrypto) {
|
|
|
|
// 申请内存块,并文件末端数据到 WASM 的内存堆
|
|
|
|
result.error = 'wasm 加载失败';
|
|
|
|
const detectionBuf = new Uint8Array(mggBlob.slice(-DETECTION_SIZE));
|
|
|
|
return result;
|
|
|
|
const pDetectionBuf = QMCCrypto._malloc(detectionBuf.length);
|
|
|
|
|
|
|
|
QMCCrypto.writeArrayToMemory(detectionBuf, pDetectionBuf);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检测结果内存块
|
|
|
|
|
|
|
|
const pDetectionResult = QMCCrypto._malloc(QMCCrypto.sizeof_qmc_detection());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 进行检测
|
|
|
|
|
|
|
|
const detectOK = QMCCrypto.detectKeyEndPosition(pDetectionResult, pDetectionBuf, detectionBuf.length);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提取结构体内容:
|
|
|
|
|
|
|
|
// (pos: i32; len: i32; error: char[??])
|
|
|
|
|
|
|
|
const position = QMCCrypto.getValue(pDetectionResult, 'i32');
|
|
|
|
|
|
|
|
const len = QMCCrypto.getValue(pDetectionResult + 4, 'i32');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result.success = detectOK;
|
|
|
|
|
|
|
|
result.error = QMCCrypto.UTF8ToString(
|
|
|
|
|
|
|
|
pDetectionResult + QMCCrypto.offsetof_error_msg(),
|
|
|
|
|
|
|
|
QMCCrypto.sizeof_error_msg(),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
const songId = QMCCrypto.UTF8ToString(pDetectionResult + QMCCrypto.offsetof_song_id(), QMCCrypto.sizeof_song_id());
|
|
|
|
|
|
|
|
if (!songId) {
|
|
|
|
|
|
|
|
console.debug('qmc2-wasm: songId not found');
|
|
|
|
|
|
|
|
} else if (/^\d+$/.test(songId)) {
|
|
|
|
|
|
|
|
result.songId = songId;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.warn('qmc2-wasm: Invalid songId: %s', songId);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 释放内存
|
|
|
|
// 申请内存块,并文件末端数据到 WASM 的内存堆
|
|
|
|
QMCCrypto._free(pDetectionBuf);
|
|
|
|
const qmcBuf = new Uint8Array(qmcBlob);
|
|
|
|
QMCCrypto._free(pDetectionResult);
|
|
|
|
const pQmcBuf = QmcCrypto._malloc(DECRYPTION_BUF_SIZE);
|
|
|
|
|
|
|
|
QmcCrypto.writeArrayToMemory(qmcBuf.slice(-DECRYPTION_BUF_SIZE), pQmcBuf);
|
|
|
|
if (!detectOK) {
|
|
|
|
|
|
|
|
|
|
|
|
// 进行解密初始化
|
|
|
|
|
|
|
|
ext = '.' + ext;
|
|
|
|
|
|
|
|
const tailSize = QmcCrypto.preDec(pQmcBuf, DECRYPTION_BUF_SIZE, ext);
|
|
|
|
|
|
|
|
if (tailSize == -1) {
|
|
|
|
|
|
|
|
result.error = QmcCrypto.getError();
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
result.songId = QmcCrypto.getSongId();
|
|
|
|
|
|
|
|
result.songId = result.songId == "0" ? 0 : result.songId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 计算解密后文件的大小。
|
|
|
|
|
|
|
|
// 之前得到的 position 为相对当前检测数据起点的偏移。
|
|
|
|
|
|
|
|
const decryptedSize = mggBlob.byteLength - DETECTION_SIZE + position;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提取嵌入到文件的 EKey
|
|
|
|
|
|
|
|
const ekey = new Uint8Array(mggBlob.slice(decryptedSize, decryptedSize + len));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 解码 UTF-8 数据到 string
|
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
const ekey_b64 = decoder.decode(ekey);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化加密与缓冲区
|
|
|
|
|
|
|
|
const hCrypto = QMCCrypto.createInstWidthEKey(ekey_b64);
|
|
|
|
|
|
|
|
const buf = QMCCrypto._malloc(DECRYPTION_BUF_SIZE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const decryptedParts = [];
|
|
|
|
const decryptedParts = [];
|
|
|
|
let offset = 0;
|
|
|
|
let offset = 0;
|
|
|
|
let bytesToDecrypt = decryptedSize;
|
|
|
|
let bytesToDecrypt = qmcBuf.length - tailSize;
|
|
|
|
while (bytesToDecrypt > 0) {
|
|
|
|
while (bytesToDecrypt > 0) {
|
|
|
|
const blockSize = Math.min(bytesToDecrypt, DECRYPTION_BUF_SIZE);
|
|
|
|
const blockSize = Math.min(bytesToDecrypt, DECRYPTION_BUF_SIZE);
|
|
|
|
|
|
|
|
|
|
|
|
// 解密一些片段
|
|
|
|
// 解密一些片段
|
|
|
|
const blockData = new Uint8Array(mggBlob.slice(offset, offset + blockSize));
|
|
|
|
const blockData = new Uint8Array(qmcBuf.slice(offset, offset + blockSize));
|
|
|
|
QMCCrypto.writeArrayToMemory(blockData, buf);
|
|
|
|
QmcCrypto.writeArrayToMemory(blockData, pQmcBuf);
|
|
|
|
QMCCrypto.decryptStream(hCrypto, buf, offset, blockSize);
|
|
|
|
decryptedParts.push(QmcCrypto.HEAPU8.slice(pQmcBuf, pQmcBuf + QmcCrypto.decBlob(pQmcBuf, blockSize, offset)));
|
|
|
|
decryptedParts.push(QMCCrypto.HEAPU8.slice(buf, buf + blockSize));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
offset += blockSize;
|
|
|
|
offset += blockSize;
|
|
|
|
bytesToDecrypt -= blockSize;
|
|
|
|
bytesToDecrypt -= blockSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QMCCrypto._free(buf);
|
|
|
|
QmcCrypto._free(pQmcBuf);
|
|
|
|
hCrypto.delete();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result.data = MergeUint8Array(decryptedParts);
|
|
|
|
result.data = MergeUint8Array(decryptedParts);
|
|
|
|
|
|
|
|
result.success = true;
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|