From f3f6f9ef404f76f919394f477af2a4b11e278b2c Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Sun, 23 May 2021 21:40:43 +0800 Subject: [PATCH] refactor: move some utils to typescript --- src/decrypt/kgm.js | 13 +++++++------ src/decrypt/kwm.js | 9 +++++---- src/decrypt/ncm.js | 10 +++++----- src/decrypt/qmc.js | 4 ++-- src/decrypt/qmcMask.js | 14 ++++++++------ src/decrypt/raw.js | 6 ++++-- src/decrypt/util.js | 14 -------------- src/decrypt/utils.ts | 36 ++++++++++++++++++++++++++++++++++++ src/decrypt/xm.js | 6 +++--- 9 files changed, 70 insertions(+), 42 deletions(-) create mode 100644 src/decrypt/utils.ts diff --git a/src/decrypt/kgm.js b/src/decrypt/kgm.js index d009e96..8e77a2c 100644 --- a/src/decrypt/kgm.js +++ b/src/decrypt/kgm.js @@ -1,4 +1,5 @@ -import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util"; +import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util"; +import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const VprHeader = [ @@ -23,10 +24,10 @@ export async function Decrypt(file, raw_filename, raw_ext) { } const oriData = new Uint8Array(await GetArrayBuffer(file)); if (raw_ext === "vpr") { - if (!IsBytesEqual(VprHeader, oriData.slice(0, 0x10))) + if (!BytesHasPrefix(oriData, VprHeader)) return {status: false, message: "Not a valid vpr file!"} } else { - if (!IsBytesEqual(KgmHeader, oriData.slice(0, 0x10))) + if (!BytesHasPrefix(oriData, KgmHeader)) return {status: false, message: "Not a valid kgm/kgma file!"} } let bHeaderLen = new DataView(oriData.slice(0x10, 0x14).buffer) @@ -61,7 +62,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { for (let i = 0; i < dataLen; i++) audioData[i] ^= VprMaskDiff[i % 17] } - const ext = DetectAudioExt(audioData, "mp3"); + const ext = SniffAudioExt(audioData); const mime = AudioMimeType[ext]; let musicBlob = new Blob([audioData], {type: mime}); const musicMeta = await musicMetadata.parseBlob(musicBlob); @@ -71,11 +72,11 @@ export async function Decrypt(file, raw_filename, raw_ext) { status: true, title: info.title, artist: info.artist, - ext: ext, album: musicMeta.common.album, picture: imgUrl, file: URL.createObjectURL(musicBlob), - mime: mime + ext, + mime } } diff --git a/src/decrypt/kwm.js b/src/decrypt/kwm.js index b4c275c..74f1a18 100644 --- a/src/decrypt/kwm.js +++ b/src/decrypt/kwm.js @@ -1,4 +1,5 @@ -import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util"; +import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util"; +import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const MagicHeader = [ @@ -7,9 +8,9 @@ const MagicHeader = [ ] const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk" -export async function Decrypt(file, raw_filename, raw_ext) { +export async function Decrypt(file, raw_filename, _) { const oriData = new Uint8Array(await GetArrayBuffer(file)); - if (!IsBytesEqual(MagicHeader, oriData.slice(0, 0x10))) + if (!BytesHasPrefix(oriData, MagicHeader)) return {status: false, message: "Not a valid kwm file!"} let fileKey = oriData.slice(0x18, 0x20) @@ -20,7 +21,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { audioData[cur] ^= mask[cur % 0x20]; - const ext = DetectAudioExt(audioData, "mp3"); + const ext = SniffAudioExt(audioData); const mime = AudioMimeType[ext]; let musicBlob = new Blob([audioData], {type: mime}); diff --git a/src/decrypt/ncm.js b/src/decrypt/ncm.js index 18b7d57..57d61f0 100644 --- a/src/decrypt/ncm.js +++ b/src/decrypt/ncm.js @@ -1,3 +1,5 @@ +import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts"; + const CryptoJS = require("crypto-js"); const MetaFlac = require('metaflac-js'); const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857"); @@ -8,19 +10,17 @@ import jimp from 'jimp'; import { AudioMimeType, - DetectAudioExt, GetArrayBuffer, GetFileInfo, GetWebImage, - IsBytesEqual, WriteMp3Meta } from "./util" -export async function Decrypt(file, raw_filename, raw_ext) { +export async function Decrypt(file, raw_filename, _) { const fileBuffer = await GetArrayBuffer(file); const dataView = new DataView(fileBuffer); - if (!IsBytesEqual(MagicHeader, new Uint8Array(fileBuffer, 0, 8))) + if (!BytesHasPrefix(new Uint8Array(fileBuffer, 0, 8), MagicHeader)) return {status: false, message: "此ncm文件已损坏"}; const keyDataObj = getKeyData(dataView, fileBuffer, 10); @@ -41,7 +41,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { const info = GetFileInfo(artists.join("; "), musicMeta.musicName, raw_filename); if (artists.length === 0) artists.push(info.artist); - if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3"); + if (musicMeta.format === undefined) musicMeta.format = SniffAudioExt(audioData); console.log(musicMeta) const imageInfo = await GetWebImage(musicMeta.albumPic); diff --git a/src/decrypt/qmc.js b/src/decrypt/qmc.js index 8b8f8d4..27f670e 100644 --- a/src/decrypt/qmc.js +++ b/src/decrypt/qmc.js @@ -1,6 +1,5 @@ import { AudioMimeType, - DetectAudioExt, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, @@ -10,6 +9,7 @@ import { } from "./util"; import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask"; import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js' +import {SniffAudioExt} from "@/decrypt/utils.ts"; const MetaFlac = require('metaflac-js'); @@ -58,7 +58,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { } let musicDecoded = seed.Decrypt(audioData); - const ext = DetectAudioExt(musicDecoded, handler.ext); + const ext = SniffAudioExt(musicDecoded, handler.ext); const mime = AudioMimeType[ext]; let musicBlob = new Blob([musicDecoded], {type: mime}); diff --git a/src/decrypt/qmcMask.js b/src/decrypt/qmcMask.js index 82f046f..6b1a312 100644 --- a/src/decrypt/qmcMask.js +++ b/src/decrypt/qmcMask.js @@ -1,4 +1,5 @@ -import {FLAC_HEADER, IsBytesEqual, OGG_HEADER} from "./util" +import {FLAC_HEADER, OGG_HEADER} from "./util" +import {BytesEquals, BytesHasPrefix} from "@/decrypt/utils.ts"; const QMOggPublicHeader1 = [ 0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, @@ -84,7 +85,7 @@ class QmcMask { } let rowLeft = this.Matrix128.slice(lenStart + 1, lenStart + 8); let rowRight = this.Matrix128.slice(lenRightStart + 1, lenRightStart + 8).reverse(); - if (IsBytesEqual(rowLeft, rowRight)) { + if (BytesEquals(rowLeft, rowRight)) { matrix58 = matrix58.concat(rowLeft); } else { throw "decode mask-128 to mask-58 failed" @@ -151,7 +152,9 @@ export function QmcMaskDetectMflac(data) { for (let block_idx = 0; block_idx < search_len; block_idx += 128) { try { mask = new QmcMask(data.slice(block_idx, block_idx + 128)); - if (IsBytesEqual(FLAC_HEADER, mask.Decrypt(data.slice(0, FLAC_HEADER.length)))) break; + if (BytesHasPrefix(mask.Decrypt(data.slice(0, FLAC_HEADER.length)), FLAC_HEADER)) { + break; + } } catch (e) { } } @@ -186,8 +189,7 @@ export function QmcMaskDetectMgg(data) { return; } const mask = new QmcMask(matrix); - let dx = mask.Decrypt(data.slice(0, OGG_HEADER.length)); - if (!IsBytesEqual(OGG_HEADER, dx)) { + if (!BytesHasPrefix(mask.Decrypt(data.slice(0, OGG_HEADER.length)), OGG_HEADER)) { return; } @@ -270,4 +272,4 @@ function QmcGenerateOggConf(page2) { for (let i = 2; i < page2; i++) specConf.push(4) specConf.push(0) return QMOggPublicConf1.concat(specConf, QMOggPublicConf2) -} \ No newline at end of file +} diff --git a/src/decrypt/raw.js b/src/decrypt/raw.js index 38136d4..e34d3c4 100644 --- a/src/decrypt/raw.js +++ b/src/decrypt/raw.js @@ -1,11 +1,13 @@ +import {SniffAudioExt} from "@/decrypt/utils.ts"; + const musicMetadata = require("music-metadata-browser"); -import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetMetaCoverURL, GetFileInfo} from "./util"; +import {AudioMimeType, GetArrayBuffer, GetMetaCoverURL, GetFileInfo} from "./util"; export async function Decrypt(file, raw_filename, raw_ext, detect = true) { let ext = raw_ext; if (detect) { const buffer = new Uint8Array(await GetArrayBuffer(file)); - ext = DetectAudioExt(buffer, raw_ext); + ext = SniffAudioExt(buffer, raw_ext); if (ext !== raw_ext) file = new Blob([buffer], {type: AudioMimeType[ext]}) } const tag = await musicMetadata.parseBlob(file); diff --git a/src/decrypt/util.js b/src/decrypt/util.js index f77944b..302a6d2 100644 --- a/src/decrypt/util.js +++ b/src/decrypt/util.js @@ -1,5 +1,4 @@ const ID3Writer = require("browser-id3-writer"); -const musicMetadata = require("music-metadata-browser"); export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43]; export const MP3_HEADER = [0x49, 0x44, 0x33]; export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53]; @@ -64,19 +63,6 @@ export function IsBytesEqual(first, second) { }) } -/** - * @return {string} - */ -export function DetectAudioExt(data, fallbackExt) { - if (IsBytesEqual(MP3_HEADER, data.slice(0, MP3_HEADER.length))) return "mp3"; - if (IsBytesEqual(FLAC_HEADER, data.slice(0, FLAC_HEADER.length))) return "flac"; - if (IsBytesEqual(OGG_HEADER, data.slice(0, OGG_HEADER.length))) return "ogg"; - if (IsBytesEqual(M4A_HEADER, data.slice(4, 4 + M4A_HEADER.length))) return "m4a"; - if (IsBytesEqual(WMA_HEADER, data.slice(0, WMA_HEADER.length))) return "wma"; - if (IsBytesEqual(WAV_HEADER, data.slice(0, WAV_HEADER.length))) return "wav"; - return fallbackExt; -} - export async function GetWebImage(pic_url) { try { diff --git a/src/decrypt/utils.ts b/src/decrypt/utils.ts new file mode 100644 index 0000000..39c816f --- /dev/null +++ b/src/decrypt/utils.ts @@ -0,0 +1,36 @@ +export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43]; +export const MP3_HEADER = [0x49, 0x44, 0x33]; +export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53]; +export const M4A_HEADER = [0x66, 0x74, 0x79, 0x70]; +export const WMA_HEADER = [ + 0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, + 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C, +] +export const WAV_HEADER = [0x52, 0x49, 0x46, 0x46] +export const AAC_HEADER = [0xFF, 0xF1] + +export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean { + if (prefix.length > data.length) return false + return prefix.every((val, idx) => { + return val === data[idx]; + }) +} + +export function BytesEquals(data: Uint8Array, another: Uint8Array): boolean { + if (another.length != data.length) return false + return data.every((val, idx) => { + return val === another[idx]; + }) +} + +export function SniffAudioExt(data: Uint8Array, fallback_ext: string = "mp3"): string { + if (BytesHasPrefix(data, MP3_HEADER)) return ".mp3" + if (BytesHasPrefix(data, FLAC_HEADER)) return ".flac" + if (BytesHasPrefix(data, OGG_HEADER)) return ".ogg" + if (data.length >= 4 + M4A_HEADER.length && + BytesHasPrefix(data.slice(4), M4A_HEADER)) return ".m4a" + if (BytesHasPrefix(data, WAV_HEADER)) return ".wav" + if (BytesHasPrefix(data, WMA_HEADER)) return ".wma" + if (BytesHasPrefix(data, AAC_HEADER)) return ".aac" + return fallback_ext; +} diff --git a/src/decrypt/xm.js b/src/decrypt/xm.js index 171d493..740938a 100644 --- a/src/decrypt/xm.js +++ b/src/decrypt/xm.js @@ -1,6 +1,7 @@ -import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util"; +import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util"; import {Decrypt as RawDecrypt} from "./raw"; +import {BytesHasPrefix} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const MagicHeader = [0x69, 0x66, 0x6D, 0x74] @@ -14,8 +15,7 @@ const FileTypeMap = { export async function Decrypt(file, raw_filename, raw_ext) { const oriData = new Uint8Array(await GetArrayBuffer(file)); - if (!IsBytesEqual(MagicHeader, oriData.slice(0, 4)) || - !IsBytesEqual(MagicHeader2, oriData.slice(8, 12))) { + if (!BytesHasPrefix(oriData, MagicHeader) || !BytesHasPrefix(oriData.slice(8, 12), MagicHeader2)) { if (raw_ext === "xm") { return {status: false, message: "此xm文件已损坏"} } else {