From 4637a3650a14bee4beb233d03d9e379b905523ac Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Sun, 23 May 2021 22:29:34 +0800 Subject: [PATCH] feat(decrypt/kwm): support raw .acc --- src/decrypt/kgm.js | 4 ++-- src/decrypt/{kwm.js => kwm.ts} | 24 +++++++++++++++--------- src/decrypt/ncm.js | 3 +-- src/decrypt/qmc.js | 4 +--- src/decrypt/{raw.js => raw.ts} | 11 ++++++----- src/decrypt/util.js | 9 +-------- src/decrypt/utils.ts | 22 +++++++++++++++------- src/decrypt/xm.js | 4 ++-- 8 files changed, 43 insertions(+), 38 deletions(-) rename src/decrypt/{kwm.js => kwm.ts} (66%) rename src/decrypt/{raw.js => raw.ts} (62%) diff --git a/src/decrypt/kgm.js b/src/decrypt/kgm.js index d2c1d9d..9c159f2 100644 --- a/src/decrypt/kgm.js +++ b/src/decrypt/kgm.js @@ -1,5 +1,5 @@ -import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util"; -import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {GetFileInfo, GetMetaCoverURL} from "./util"; +import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const VprHeader = [ diff --git a/src/decrypt/kwm.js b/src/decrypt/kwm.ts similarity index 66% rename from src/decrypt/kwm.js rename to src/decrypt/kwm.ts index 793a81e..1cd7a82 100644 --- a/src/decrypt/kwm.js +++ b/src/decrypt/kwm.ts @@ -1,17 +1,23 @@ -import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util"; -import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {GetFileInfo, GetMetaCoverURL} from "./util"; +import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {Decrypt as RawDecrypt} from "@/decrypt/raw.ts"; + +import {parseBlob as metaParseBlob} from "music-metadata-browser"; -const musicMetadata = require("music-metadata-browser"); const MagicHeader = [ 0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D, 0x6B, 0x75, 0x77, 0x6F, 0x2D, 0x74, 0x6D, 0x65, ] const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk" -export async function Decrypt(file, raw_filename, _) { +export async function Decrypt(file: File, raw_filename: string, _: string) { const oriData = new Uint8Array(await GetArrayBuffer(file)); - if (!BytesHasPrefix(oriData, MagicHeader)) + if (!BytesHasPrefix(oriData, MagicHeader)) { + if (SniffAudioExt(oriData) === "aac") { + return await RawDecrypt(file, raw_filename, "aac", true) + } return {status: false, message: "Not a valid kwm file!"} + } let fileKey = oriData.slice(0x18, 0x20) let mask = createMaskFromKey(fileKey) @@ -25,7 +31,7 @@ export async function Decrypt(file, raw_filename, _) { const mime = AudioMimeType[ext]; let musicBlob = new Blob([audioData], {type: mime}); - const musicMeta = await musicMetadata.parseBlob(musicBlob); + const musicMeta = await metaParseBlob(musicBlob); const info = GetFileInfo(musicMeta.common.artist, musicMeta.common.title, raw_filename); const imgUrl = GetMetaCoverURL(musicMeta); @@ -43,19 +49,19 @@ export async function Decrypt(file, raw_filename, _) { } -function createMaskFromKey(keyBytes) { +function createMaskFromKey(keyBytes: Uint8Array): Uint8Array { let keyView = new DataView(keyBytes.buffer) let keyStr = keyView.getBigUint64(0, true).toString() let keyStrTrim = trimKey(keyStr) let key = new Uint8Array(32) for (let i = 0; i < 32; i++) { - key[i] = PreDefinedKey[i].charCodeAt() ^ keyStrTrim[i].charCodeAt() + key[i] = PreDefinedKey.charCodeAt(i) ^ keyStrTrim.charCodeAt(i) } return key } -function trimKey(keyRaw) { +function trimKey(keyRaw: string): string { let lenRaw = keyRaw.length; let out = keyRaw; if (lenRaw > 32) { diff --git a/src/decrypt/ncm.js b/src/decrypt/ncm.js index 1680861..a76394e 100644 --- a/src/decrypt/ncm.js +++ b/src/decrypt/ncm.js @@ -1,4 +1,4 @@ -import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; const CryptoJS = require("crypto-js"); const MetaFlac = require('metaflac-js'); @@ -9,7 +9,6 @@ const musicMetadata = require("music-metadata-browser"); import jimp from 'jimp'; import { - AudioMimeType, GetFileInfo, GetWebImage, WriteMp3Meta diff --git a/src/decrypt/qmc.js b/src/decrypt/qmc.js index fdf42b9..0fd1e07 100644 --- a/src/decrypt/qmc.js +++ b/src/decrypt/qmc.js @@ -1,5 +1,4 @@ import { - AudioMimeType, GetFileInfo, GetMetaCoverURL, GetWebImage, @@ -8,11 +7,10 @@ import { } from "./util"; import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask"; import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js' -import {GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {AudioMimeType, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; const MetaFlac = require('metaflac-js'); -const ID3Writer = require("browser-id3-writer"); const iconv = require('iconv-lite'); const decode = iconv.decode diff --git a/src/decrypt/raw.js b/src/decrypt/raw.ts similarity index 62% rename from src/decrypt/raw.js rename to src/decrypt/raw.ts index 5a0872f..2cfbe59 100644 --- a/src/decrypt/raw.js +++ b/src/decrypt/raw.ts @@ -1,16 +1,17 @@ -import {GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {AudioMimeType, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; -const musicMetadata = require("music-metadata-browser"); -import {AudioMimeType, GetMetaCoverURL, GetFileInfo} from "./util"; -export async function Decrypt(file, raw_filename, raw_ext, detect = true) { +import {parseBlob as metaParseBlob} from "music-metadata-browser"; +import {GetMetaCoverURL, GetFileInfo} from "./util"; + +export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string, detect: boolean = true) { let ext = raw_ext; if (detect) { const buffer = new Uint8Array(await GetArrayBuffer(file)); ext = SniffAudioExt(buffer, raw_ext); if (ext !== raw_ext) file = new Blob([buffer], {type: AudioMimeType[ext]}) } - const tag = await musicMetadata.parseBlob(file); + const tag = await metaParseBlob(file); const info = GetFileInfo(tag.common.artist, tag.common.title, raw_filename); return { status: true, diff --git a/src/decrypt/util.js b/src/decrypt/util.js index 50cdec6..13e076a 100644 --- a/src/decrypt/util.js +++ b/src/decrypt/util.js @@ -1,13 +1,6 @@ const ID3Writer = require("browser-id3-writer"); -export const AudioMimeType = { - mp3: "audio/mpeg", - flac: "audio/flac", - m4a: "audio/mp4", - ogg: "audio/ogg", - wma: "audio/x-ms-wma", - wav: "audio/x-wav" -}; + export const IXAREA_API_ENDPOINT = "https://stats.ixarea.com/apis" diff --git a/src/decrypt/utils.ts b/src/decrypt/utils.ts index d76c530..74a3229 100644 --- a/src/decrypt/utils.ts +++ b/src/decrypt/utils.ts @@ -8,6 +8,14 @@ export const WMA_HEADER = [ ] export const WAV_HEADER = [0x52, 0x49, 0x46, 0x46] export const AAC_HEADER = [0xFF, 0xF1] +export const AudioMimeType: { [key: string]: string } = { + mp3: "audio/mpeg", + flac: "audio/flac", + m4a: "audio/mp4", + ogg: "audio/ogg", + wma: "audio/x-ms-wma", + wav: "audio/x-wav" +}; export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean { if (prefix.length > data.length) return false @@ -24,14 +32,14 @@ export function BytesEquals(data: Uint8Array, another: Uint8Array): boolean { } 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 (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" + 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 279e8a6..dbc20c1 100644 --- a/src/decrypt/xm.js +++ b/src/decrypt/xm.js @@ -1,7 +1,7 @@ -import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util"; +import {GetFileInfo, GetMetaCoverURL} from "./util"; import {Decrypt as RawDecrypt} from "./raw"; -import {BytesHasPrefix, GetArrayBuffer} from "@/decrypt/utils.ts"; +import {AudioMimeType, BytesHasPrefix, GetArrayBuffer} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const MagicHeader = [0x69, 0x66, 0x6D, 0x74]