test: added test cases for qmc cache.

This commit is contained in:
鲁树人 2022-11-21 18:14:44 +00:00
parent 97cd7afc44
commit c3b75c8fea
3 changed files with 74 additions and 50 deletions

View File

@ -0,0 +1,20 @@
import { DecryptBuffer as DecryptQmcCacheBuffer } from '../qmccache';
import fs from 'fs';
const expectedBuffer = fs.readFileSync(__dirname + '/fixture/qmc_cache_expected.bin');
const createInputBuffer = () => {
const buffer = Buffer.alloc(256);
for (let i = buffer.byteLength; i >= 0; i--) {
buffer[i] = i;
}
return buffer;
};
describe('decrypt/qmccache', () => {
it('should decrypt specified buffer correctly', () => {
const input = createInputBuffer();
DecryptQmcCacheBuffer(input);
expect(input).toEqual(expectedBuffer);
});
});

Binary file not shown.

View File

@ -1,50 +1,54 @@
import { import {
AudioMimeType, AudioMimeType,
GetArrayBuffer, GetArrayBuffer,
GetCoverFromFile, GetCoverFromFile,
GetMetaFromFile, GetMetaFromFile,
SniffAudioExt, SniffAudioExt,
SplitFilename, SplitFilename,
} from '@/decrypt/utils'; } from '@/decrypt/utils';
import { Decrypt as QmcDecrypt, HandlerMap } from '@/decrypt/qmc'; import { Decrypt as QmcDecrypt, HandlerMap } from '@/decrypt/qmc';
import { DecryptResult } from '@/decrypt/entity'; import { DecryptResult } from '@/decrypt/entity';
import { parseBlob as metaParseBlob } from 'music-metadata-browser'; import { parseBlob as metaParseBlob } from 'music-metadata-browser';
export async function Decrypt(file: Blob, raw_filename: string, _: string): Promise<DecryptResult> { export function DecryptBuffer(buffer: Uint8Array | Buffer) {
const buffer = new Uint8Array(await GetArrayBuffer(file)); let length = buffer.byteLength;
let length = buffer.length; for (let i = 0; i < length; i++) {
for (let i = 0; i < length; i++) { buffer[i] ^= 0xf4;
buffer[i] ^= 0xf4; if (buffer[i] <= 0x3f) buffer[i] = buffer[i] * 4;
if (buffer[i] <= 0x3f) buffer[i] = buffer[i] * 4; else if (buffer[i] <= 0x7f) buffer[i] = (buffer[i] - 0x40) * 4 + 1;
else if (buffer[i] <= 0x7f) buffer[i] = (buffer[i] - 0x40) * 4 + 1; else if (buffer[i] <= 0xbf) buffer[i] = (buffer[i] - 0x80) * 4 + 2;
else if (buffer[i] <= 0xbf) buffer[i] = (buffer[i] - 0x80) * 4 + 2; else buffer[i] = (buffer[i] - 0xc0) * 4 + 3;
else buffer[i] = (buffer[i] - 0xc0) * 4 + 3; }
} }
let ext = SniffAudioExt(buffer, '');
const newName = SplitFilename(raw_filename); export async function Decrypt(file: Blob, raw_filename: string, _: string): Promise<DecryptResult> {
let audioBlob: Blob; const buffer = new Uint8Array(await GetArrayBuffer(file));
if (ext !== '' || newName.ext === 'mp3') { DecryptBuffer(buffer);
audioBlob = new Blob([buffer], { type: AudioMimeType[ext] }); let ext = SniffAudioExt(buffer, '');
} else if (newName.ext in HandlerMap) { const newName = SplitFilename(raw_filename);
audioBlob = new Blob([buffer], { type: 'application/octet-stream' }); let audioBlob: Blob;
return QmcDecrypt(audioBlob, newName.name, newName.ext); if (ext !== '' || newName.ext === 'mp3') {
} else { audioBlob = new Blob([buffer], { type: AudioMimeType[ext] });
throw '不支持的QQ音乐缓存格式'; } else if (newName.ext in HandlerMap) {
} audioBlob = new Blob([buffer], { type: 'application/octet-stream' });
const tag = await metaParseBlob(audioBlob); return QmcDecrypt(audioBlob, newName.name, newName.ext);
const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artist); } else {
throw '不支持的QQ音乐缓存格式';
return { }
title, const tag = await metaParseBlob(audioBlob);
artist, const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artist);
ext,
album: tag.common.album, return {
picture: GetCoverFromFile(tag), title,
file: URL.createObjectURL(audioBlob), artist,
blob: audioBlob, ext,
mime: AudioMimeType[ext], album: tag.common.album,
}; picture: GetCoverFromFile(tag),
} file: URL.createObjectURL(audioBlob),
blob: audioBlob,
mime: AudioMimeType[ext],
};
}