1
0
forked from um/web
qqmusicunlock/src/decrypt/mflac.js

100 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-11-23 10:09:33 +00:00
const musicMetadata = require("music-metadata-browser");
2020-01-21 11:03:41 +00:00
const util = require("./util");
2019-11-23 10:09:33 +00:00
export {Decrypt}
2020-01-21 11:03:41 +00:00
async function Decrypt(file, raw_filename, raw_ext) {
2019-11-23 10:09:33 +00:00
// 获取扩展名
2020-01-21 11:03:41 +00:00
if (raw_ext !== "mflac") return {
2019-11-23 10:30:59 +00:00
status: false,
message: "File type is incorrect!",
};
2019-11-23 10:09:33 +00:00
// 读取文件
2020-01-21 11:03:41 +00:00
const fileBuffer = await util.GetArrayBuffer(file);
2019-11-23 10:09:33 +00:00
const audioData = new Uint8Array(fileBuffer.slice(0, -0x170));
const audioDataLen = audioData.length;
// 转换数据
const seed = new Mask();
2020-01-21 11:03:41 +00:00
if (!seed.DetectMask(audioData)) return {
2019-11-23 10:30:59 +00:00
status: false,
message: "此音乐无法解锁目前mflac格式不提供完整支持",
};
2019-11-23 10:09:33 +00:00
for (let cur = 0; cur < audioDataLen; ++cur) {
audioData[cur] ^= seed.NextMask();
}
// 导出
const musicData = new Blob([audioData], {type: "audio/flac"});
const musicUrl = URL.createObjectURL(musicData);
// 读取Meta
let tag = await musicMetadata.parseBlob(musicData);
2020-01-21 11:03:41 +00:00
const info = util.GetFileInfo(tag.common.artist, tag.common.title, raw_filename, "flac");
let picUrl = util.GetCoverURL(tag);
// 返回
2019-11-23 10:09:33 +00:00
return {
2019-11-23 10:30:59 +00:00
status: true,
2020-01-21 11:03:41 +00:00
filename: info.filename,
title: info.title,
artist: info.artist,
2019-11-23 10:09:33 +00:00
album: tag.common.album,
2020-01-21 11:03:41 +00:00
picture: picUrl,
2019-11-23 10:09:33 +00:00
file: musicUrl,
mime: "audio/flac"
}
}
class Mask {
FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43, 0x00];
constructor() {
this.index = -1;
this.mask_index = -1;
this.mask = Array(128).fill(0x00);
}
DetectMask(data) {
let search_len = data.length - 256, mask;
for (let block_idx = 0; block_idx < search_len; block_idx += 128) {
let flag = true;
mask = data.slice(block_idx, block_idx + 128);
let next_mask = data.slice(block_idx + 128, block_idx + 256);
for (let idx = 0; idx < 128; idx++) {
if (mask[idx] !== next_mask[idx]) {
flag = false;
break;
}
}
if (!flag) continue;
for (let test_idx = 0; test_idx < this.FLAC_HEADER.length; test_idx++) {
let p = data[test_idx] ^ mask[test_idx];
if (p !== this.FLAC_HEADER[test_idx]) {
flag = false;
debugger;
break;
}
}
if (!flag) continue;
this.mask = mask;
return true;
}
return false;
}
NextMask() {
this.index++;
this.mask_index++;
if (this.index === 0x8000 || (this.index > 0x8000 && (this.index + 1) % 0x8000 === 0)) {
this.index++;
this.mask_index++;
}
if (this.mask_index >= 128) {
this.mask_index -= 128;
}
return this.mask[this.mask_index]
}
}