mirror of
				https://git.unlock-music.dev/um/web.git
				synced 2025-11-04 14:33:29 +08:00 
			
		
		
		
	Merge pull request '优化 QMCCache 解密过程' (#6) from jixunmoe/um-web:refactor/optimise-qm-cache-decode into master
Reviewed-on: https://git.unlock-music.dev/um/web/pulls/6
This commit is contained in:
		
						commit
						2872ceb3bb
					
				
							
								
								
									
										20
									
								
								src/decrypt/__test__/QmcCache.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/decrypt/__test__/QmcCache.test.ts
									
									
									
									
									
										Normal 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);
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
							
								
								
									
										
											BIN
										
									
								
								src/decrypt/__test__/fixture/qmc_cache_expected.bin
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/decrypt/__test__/fixture/qmc_cache_expected.bin
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							@ -1,50 +1,52 @@
 | 
				
			|||||||
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++) {
 | 
					    let byte = buffer[i] ^ 0xf4; // xor 0xf4
 | 
				
			||||||
    buffer[i] ^= 0xf4;
 | 
					    byte = ((byte & 0b0011_1111) << 2) | (byte >> 6); // rol 2
 | 
				
			||||||
    if (buffer[i] <= 0x3f) buffer[i] = buffer[i] * 4;
 | 
					    buffer[i] = byte;
 | 
				
			||||||
    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 buffer[i] = (buffer[i] - 0xc0) * 4 + 3;
 | 
					
 | 
				
			||||||
  }
 | 
					export async function Decrypt(file: Blob, raw_filename: string, _: string): Promise<DecryptResult> {
 | 
				
			||||||
  let ext = SniffAudioExt(buffer, '');
 | 
					  const buffer = new Uint8Array(await GetArrayBuffer(file));
 | 
				
			||||||
  const newName = SplitFilename(raw_filename);
 | 
					  DecryptBuffer(buffer);
 | 
				
			||||||
  let audioBlob: Blob;
 | 
					  let ext = SniffAudioExt(buffer, '');
 | 
				
			||||||
  if (ext !== '' || newName.ext === 'mp3') {
 | 
					  const newName = SplitFilename(raw_filename);
 | 
				
			||||||
    audioBlob = new Blob([buffer], { type: AudioMimeType[ext] });
 | 
					  let audioBlob: Blob;
 | 
				
			||||||
  } else if (newName.ext in HandlerMap) {
 | 
					  if (ext !== '' || newName.ext === 'mp3') {
 | 
				
			||||||
    audioBlob = new Blob([buffer], { type: 'application/octet-stream' });
 | 
					    audioBlob = new Blob([buffer], { type: AudioMimeType[ext] });
 | 
				
			||||||
    return QmcDecrypt(audioBlob, newName.name, newName.ext);
 | 
					  } else if (newName.ext in HandlerMap) {
 | 
				
			||||||
  } else {
 | 
					    audioBlob = new Blob([buffer], { type: 'application/octet-stream' });
 | 
				
			||||||
    throw '不支持的QQ音乐缓存格式';
 | 
					    return QmcDecrypt(audioBlob, newName.name, newName.ext);
 | 
				
			||||||
  }
 | 
					  } else {
 | 
				
			||||||
  const tag = await metaParseBlob(audioBlob);
 | 
					    throw '不支持的QQ音乐缓存格式';
 | 
				
			||||||
  const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artist);
 | 
					  }
 | 
				
			||||||
 | 
					  const tag = await metaParseBlob(audioBlob);
 | 
				
			||||||
  return {
 | 
					  const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artist);
 | 
				
			||||||
    title,
 | 
					
 | 
				
			||||||
    artist,
 | 
					  return {
 | 
				
			||||||
    ext,
 | 
					    title,
 | 
				
			||||||
    album: tag.common.album,
 | 
					    artist,
 | 
				
			||||||
    picture: GetCoverFromFile(tag),
 | 
					    ext,
 | 
				
			||||||
    file: URL.createObjectURL(audioBlob),
 | 
					    album: tag.common.album,
 | 
				
			||||||
    blob: audioBlob,
 | 
					    picture: GetCoverFromFile(tag),
 | 
				
			||||||
    mime: AudioMimeType[ext],
 | 
					    file: URL.createObjectURL(audioBlob),
 | 
				
			||||||
  };
 | 
					    blob: audioBlob,
 | 
				
			||||||
}
 | 
					    mime: AudioMimeType[ext],
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user