feat(QMCv2): add decoder
(cherry picked from commit 29ac94d1fe52e666fda619f8716d2bc0b120a9ee)
This commit is contained in:
parent
910b00529e
commit
1ab05bb509
32
src/decrypt/qmc.test.ts
Normal file
32
src/decrypt/qmc.test.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import fs from "fs";
|
||||
import {QmcDecoder} from "@/decrypt/qmc";
|
||||
import {BytesEqual} from "@/decrypt/utils";
|
||||
|
||||
function loadTestDataDecoder(name: string): {
|
||||
cipherText: Uint8Array,
|
||||
clearText: Uint8Array
|
||||
} {
|
||||
const cipherBody = fs.readFileSync(`./testdata/${name}_raw.bin`);
|
||||
const cipherSuffix = fs.readFileSync(`./testdata/${name}_suffix.bin`);
|
||||
const cipherText = new Uint8Array(cipherBody.length + cipherSuffix.length);
|
||||
cipherText.set(cipherBody);
|
||||
cipherText.set(cipherSuffix, cipherBody.length);
|
||||
return {
|
||||
cipherText,
|
||||
clearText: fs.readFileSync(`testdata/${name}_target.bin`)
|
||||
}
|
||||
}
|
||||
|
||||
test("qmc: real file", async () => {
|
||||
const cases = ["mflac0_rc4", "mflac_map", "mgg_map", "qmc0_static"]
|
||||
for (const name of cases) {
|
||||
const {clearText, cipherText} = loadTestDataDecoder(name)
|
||||
const c = new QmcDecoder(cipherText)
|
||||
const buf = c.decrypt()
|
||||
|
||||
expect(BytesEqual(buf, clearText)).toBeTruthy()
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import {QmcStaticCipher} from "./qmc_cipher";
|
||||
import {QmcMapCipher, QmcRC4Cipher, QmcStaticCipher, StreamCipher} from "./qmc_cipher";
|
||||
import {
|
||||
AudioMimeType,
|
||||
GetArrayBuffer,
|
||||
@ -16,6 +16,7 @@ import {DecryptQMCv2} from "./qmcv2";
|
||||
import iconv from "iconv-lite";
|
||||
import {DecryptResult} from "@/decrypt/entity";
|
||||
import {queryAlbumCover} from "@/utils/api";
|
||||
import {QmcDecryptKey} from "@/decrypt/qmc_key";
|
||||
|
||||
interface Handler {
|
||||
ext: string
|
||||
@ -137,3 +138,63 @@ async function getCoverImage(title: string, artist?: string, album?: string): Pr
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
export class QmcDecoder {
|
||||
file: Uint8Array
|
||||
size: number
|
||||
decoded: boolean = false
|
||||
audioSize?: number
|
||||
cipher?: StreamCipher
|
||||
|
||||
constructor(file: Uint8Array) {
|
||||
this.file = file
|
||||
this.size = file.length
|
||||
this.searchKey()
|
||||
}
|
||||
|
||||
decrypt(): Uint8Array {
|
||||
if (!this.cipher) {
|
||||
throw new Error("no cipher found")
|
||||
}
|
||||
if (!this.audioSize || this.audioSize <= 0) {
|
||||
throw new Error("invalid audio size")
|
||||
}
|
||||
const audioBuf = this.file.subarray(0, this.audioSize)
|
||||
|
||||
if (!this.decoded) {
|
||||
this.cipher.decrypt(audioBuf, 0)
|
||||
this.decoded = true
|
||||
}
|
||||
|
||||
return audioBuf
|
||||
}
|
||||
|
||||
private searchKey() {
|
||||
const last4Byte = this.file.slice(this.size - 4);
|
||||
const textEnc = new TextDecoder()
|
||||
if (textEnc.decode(last4Byte) === 'QTag') {
|
||||
const sizeBuf = this.file.slice(this.size - 8, this.size - 4)
|
||||
const sizeView = new DataView(sizeBuf.buffer, sizeBuf.byteOffset)
|
||||
const keySize = sizeView.getUint32(0, false)
|
||||
this.audioSize = this.size - keySize - 8
|
||||
const rawKey = this.file.subarray(this.audioSize, this.size - 8)
|
||||
const keyEnd = rawKey.findIndex(v => v == ','.charCodeAt(0))
|
||||
const keyDec = QmcDecryptKey(rawKey.subarray(0, keyEnd))
|
||||
this.cipher = new QmcRC4Cipher(keyDec)
|
||||
} else {
|
||||
const sizeView = new DataView(last4Byte.buffer, last4Byte.byteOffset);
|
||||
const keySize = sizeView.getUint32(0, true)
|
||||
if (keySize < 0x300) {
|
||||
this.audioSize = this.size - keySize - 4
|
||||
const rawKey = this.file.subarray(this.audioSize, this.size - 4)
|
||||
const keyDec = QmcDecryptKey(rawKey)
|
||||
this.cipher = new QmcMapCipher(keyDec)
|
||||
} else {
|
||||
this.audioSize = this.size
|
||||
this.cipher = new QmcStaticCipher()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import {DecryptKey, simpleMakeKey,} from "@/decrypt/qmc_key";
|
||||
import {QmcDecryptKey, simpleMakeKey,} from "@/decrypt/qmc_key";
|
||||
import fs from "fs";
|
||||
|
||||
test("key dec: make simple key", () => {
|
||||
@ -23,7 +23,7 @@ test("key dec: real file", async () => {
|
||||
const cases = ["mflac_map", "mgg_map", "mflac0_rc4"]
|
||||
for (const name of cases) {
|
||||
const {clearText, cipherText} = loadTestDataKeyDecrypt(name)
|
||||
const buf = DecryptKey(cipherText)
|
||||
const buf = QmcDecryptKey(cipherText)
|
||||
|
||||
expect(buf).toStrictEqual(clearText)
|
||||
}
|
||||
|
@ -83,8 +83,9 @@ function decryptTencentTea(inBuf: Uint8Array, key: Uint8Array): Uint8Array {
|
||||
return outBuf
|
||||
}
|
||||
|
||||
export function DecryptKey(raw: Uint8Array): Uint8Array {
|
||||
const rawDec = Buffer.from(raw.toString(), 'base64')
|
||||
export function QmcDecryptKey(raw: Uint8Array): Uint8Array {
|
||||
const textDec = new TextDecoder()
|
||||
const rawDec = Buffer.from(textDec.decode(raw), 'base64')
|
||||
let n = rawDec.length;
|
||||
if (n < 16) {
|
||||
throw Error("key length is too short")
|
||||
|
@ -32,13 +32,20 @@ export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean {
|
||||
})
|
||||
}
|
||||
|
||||
export function BytesEqual(a: Uint8Array, b: Uint8Array,): boolean {
|
||||
if (a.length != b.length) return false
|
||||
return a.every((val, idx) => {
|
||||
return val === b[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"
|
||||
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"
|
||||
|
BIN
testdata/qmc0_static_raw.bin
vendored
Normal file
BIN
testdata/qmc0_static_raw.bin
vendored
Normal file
Binary file not shown.
0
testdata/qmc0_static_suffix.bin
vendored
Normal file
0
testdata/qmc0_static_suffix.bin
vendored
Normal file
BIN
testdata/qmc0_static_target.bin
vendored
Normal file
BIN
testdata/qmc0_static_target.bin
vendored
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user