2021-05-23 14:29:34 +00:00
|
|
|
import {GetFileInfo, GetMetaCoverURL} from "./util";
|
2020-04-23 12:46:08 +00:00
|
|
|
|
|
|
|
import {Decrypt as RawDecrypt} from "./raw";
|
2021-05-23 14:29:34 +00:00
|
|
|
import {AudioMimeType, BytesHasPrefix, GetArrayBuffer} from "@/decrypt/utils.ts";
|
2020-04-23 12:46:08 +00:00
|
|
|
|
|
|
|
const musicMetadata = require("music-metadata-browser");
|
|
|
|
const MagicHeader = [0x69, 0x66, 0x6D, 0x74]
|
|
|
|
const MagicHeader2 = [0xfe, 0xfe, 0xfe, 0xfe]
|
|
|
|
const FileTypeMap = {
|
|
|
|
" WAV": ".wav",
|
|
|
|
"FLAC": ".flac",
|
|
|
|
" MP3": ".mp3",
|
|
|
|
" A4M": ".m4a",
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function Decrypt(file, raw_filename, raw_ext) {
|
|
|
|
const oriData = new Uint8Array(await GetArrayBuffer(file));
|
2021-05-23 13:40:43 +00:00
|
|
|
if (!BytesHasPrefix(oriData, MagicHeader) || !BytesHasPrefix(oriData.slice(8, 12), MagicHeader2)) {
|
2020-04-23 12:46:08 +00:00
|
|
|
if (raw_ext === "xm") {
|
2020-05-14 09:36:16 +00:00
|
|
|
return {status: false, message: "此xm文件已损坏"}
|
2020-04-23 12:46:08 +00:00
|
|
|
} else {
|
|
|
|
return await RawDecrypt(file, raw_filename, raw_ext, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let typeText = (new TextDecoder()).decode(oriData.slice(4, 8))
|
|
|
|
if (!FileTypeMap.hasOwnProperty(typeText)) {
|
2020-05-14 09:36:16 +00:00
|
|
|
return {status: false, message: "未知的xm文件类型"}
|
2020-04-23 12:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let key = oriData[0xf]
|
2020-08-13 07:26:15 +00:00
|
|
|
let dataOffset = oriData[0xc] | oriData[0xd] << 8 | oriData[0xe] << 16
|
2020-04-23 12:46:08 +00:00
|
|
|
let audioData = oriData.slice(0x10);
|
|
|
|
let lenAudioData = audioData.length;
|
|
|
|
for (let cur = dataOffset; cur < lenAudioData; ++cur)
|
|
|
|
audioData[cur] = (audioData[cur] - key) ^ 0xff;
|
|
|
|
|
2020-08-13 07:25:41 +00:00
|
|
|
const ext = FileTypeMap[typeText];
|
2020-04-23 12:46:08 +00:00
|
|
|
const mime = AudioMimeType[ext];
|
|
|
|
let musicBlob = new Blob([audioData], {type: mime});
|
|
|
|
|
|
|
|
const musicMeta = await musicMetadata.parseBlob(musicBlob);
|
|
|
|
if (ext === "wav") {
|
|
|
|
//todo:未知的编码方式
|
2020-08-13 13:27:33 +00:00
|
|
|
console.log(musicMeta.common)
|
2020-04-23 12:46:08 +00:00
|
|
|
musicMeta.common.album = "";
|
|
|
|
musicMeta.common.artist = "";
|
|
|
|
musicMeta.common.title = "";
|
|
|
|
}
|
2020-08-13 13:27:33 +00:00
|
|
|
let _sep = raw_filename.indexOf("_") === -1 ? "-" : "_"
|
2020-08-13 07:33:15 +00:00
|
|
|
const info = GetFileInfo(musicMeta.common.artist, musicMeta.common.title, raw_filename, _sep);
|
2020-04-23 12:46:08 +00:00
|
|
|
|
|
|
|
const imgUrl = GetMetaCoverURL(musicMeta);
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: true,
|
|
|
|
title: info.title,
|
|
|
|
artist: info.artist,
|
|
|
|
ext: ext,
|
|
|
|
album: musicMeta.common.album,
|
|
|
|
picture: imgUrl,
|
|
|
|
file: URL.createObjectURL(musicBlob),
|
2020-05-14 09:36:16 +00:00
|
|
|
mime: mime,
|
|
|
|
rawExt: "xm"
|
2020-04-23 12:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|