1
0
forked from um/cli
cli/algo/qmc/qmc.go

268 lines
5.9 KiB
Go
Raw Normal View History

2021-12-13 12:42:07 +00:00
package qmc
import (
2021-12-13 20:01:04 +00:00
"bytes"
2021-12-13 12:42:07 +00:00
"encoding/binary"
"errors"
"fmt"
2024-02-12 18:41:42 +00:00
"go.uber.org/zap"
2021-12-13 12:42:07 +00:00
"io"
2022-12-05 00:54:40 +00:00
"runtime"
2021-12-13 12:42:07 +00:00
"strconv"
"strings"
2022-11-18 23:44:44 +00:00
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff"
2021-12-13 12:42:07 +00:00
)
type Decoder struct {
2022-12-04 16:06:38 +00:00
raw io.ReadSeeker // raw is the original file reader
params *common.DecoderParams
2021-12-13 12:42:07 +00:00
2022-11-19 18:47:28 +00:00
audio io.Reader // audio is the encrypted audio data
audioLen int // audioLen is the audio data length
offset int // offset is the current audio read position
decodedKey []byte // decodedKey is the decoded key for cipher
cipher common.StreamDecoder
2021-12-13 12:42:07 +00:00
2022-11-22 03:08:35 +00:00
songID int
2021-12-13 12:42:07 +00:00
rawMetaExtra2 int
2022-12-05 00:54:40 +00:00
albumID int
albumMediaID string
2022-11-22 03:08:35 +00:00
// cache
meta common.AudioMeta
cover []byte
embeddedCover bool // embeddedCover is true if the cover is embedded in the file
probeBuf *bytes.Buffer // probeBuf is the buffer for sniffing metadata, TODO: consider pipe?
2022-11-22 03:08:35 +00:00
// provider
2022-12-05 00:54:40 +00:00
logger *zap.Logger
2024-02-12 18:41:42 +00:00
footer qqMusicTagMusicEx
2021-12-13 12:42:07 +00:00
}
// Read implements io.Reader, offer the decrypted audio data.
// Validate should call before Read to check if the file is valid.
func (d *Decoder) Read(p []byte) (int, error) {
n, err := d.audio.Read(p)
if n > 0 {
d.cipher.Decrypt(p[:n], d.offset)
d.offset += n
_, _ = d.probeBuf.Write(p[:n]) // bytes.Buffer.Write never return error
2021-12-13 12:42:07 +00:00
}
return n, err
}
func NewDecoder(p *common.DecoderParams) common.Decoder {
2022-12-05 00:54:40 +00:00
return &Decoder{raw: p.Reader, params: p, logger: p.Logger}
2021-12-13 12:42:07 +00:00
}
func (d *Decoder) Validate() error {
// search & derive key
err := d.searchKey()
if err != nil {
return err
2021-12-13 12:42:07 +00:00
}
// check cipher type and init decode cipher
if len(d.decodedKey) > 300 {
d.cipher, err = newRC4Cipher(d.decodedKey)
2021-12-13 12:42:07 +00:00
if err != nil {
return err
2021-12-13 12:42:07 +00:00
}
} else if len(d.decodedKey) != 0 {
d.cipher, err = newMapCipher(d.decodedKey)
if err != nil {
return err
}
} else {
d.cipher = newStaticCipher()
2021-12-13 12:42:07 +00:00
}
// test with first 16 bytes
if err := d.validateDecode(); err != nil {
return err
2021-12-13 12:42:07 +00:00
}
// reset position, limit to audio, prepare for Read
if _, err := d.raw.Seek(0, io.SeekStart); err != nil {
return err
}
d.audio = io.LimitReader(d.raw, int64(d.audioLen))
// prepare for sniffing metadata
d.probeBuf = bytes.NewBuffer(make([]byte, 0, d.audioLen))
return nil
}
func (d *Decoder) validateDecode() error {
_, err := d.raw.Seek(0, io.SeekStart)
if err != nil {
return fmt.Errorf("qmc seek to start: %w", err)
}
2022-12-06 13:47:33 +00:00
buf := make([]byte, 64)
if _, err := io.ReadFull(d.raw, buf); err != nil {
return fmt.Errorf("qmc read header: %w", err)
}
d.cipher.Decrypt(buf, 0)
_, ok := sniff.AudioExtension(buf)
if !ok {
return errors.New("qmc: detect file type failed")
}
return nil
}
2022-12-04 16:06:38 +00:00
func (d *Decoder) searchKey() (err error) {
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
if err != nil {
2021-12-13 12:42:07 +00:00
return err
}
2022-12-05 00:54:40 +00:00
fileSize := int(fileSizeM4) + 4
//goland:noinspection GoBoolExpressions
if runtime.GOOS == "darwin" && !strings.HasPrefix(d.params.Extension, ".qmc") {
2022-12-05 00:54:40 +00:00
d.decodedKey, err = readKeyFromMMKV(d.params.FilePath, d.logger)
if err == nil {
d.audioLen = fileSize
return
}
d.logger.Warn("read key from mmkv failed", zap.Error(err))
}
2022-11-19 18:47:28 +00:00
suffixBuf := make([]byte, 4)
if _, err := io.ReadFull(d.raw, suffixBuf); err != nil {
2021-12-13 12:42:07 +00:00
return err
}
2022-11-19 18:47:28 +00:00
2024-02-12 17:17:52 +00:00
switch string(suffixBuf) {
2022-11-19 18:47:28 +00:00
case "QTag":
return d.readRawMetaQTag()
2022-11-19 18:47:28 +00:00
case "STag":
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
2024-02-12 17:17:52 +00:00
case "cex\x00":
2024-02-12 18:41:42 +00:00
audioLen, err := d.footer.Read(d.raw)
if err != nil {
return err
2024-02-05 00:41:40 +00:00
}
2024-02-12 18:41:42 +00:00
d.audioLen = int(audioLen)
d.decodedKey, err = readKeyFromMMKVCustom(d.footer.mediafile)
if err != nil {
return err
}
return nil
2022-11-19 18:47:28 +00:00
default:
size := binary.LittleEndian.Uint32(suffixBuf)
if size <= 0xFFFF && size != 0 { // assume size is key len
return d.readRawKey(int64(size))
}
2022-11-19 18:47:28 +00:00
// try to use default static cipher
2022-12-05 00:54:40 +00:00
d.audioLen = fileSize
return nil
}
2022-11-19 18:47:28 +00:00
}
func (d *Decoder) readRawKey(rawKeyLen int64) error {
audioLen, err := d.raw.Seek(-(4 + rawKeyLen), io.SeekEnd)
if err != nil {
return err
}
d.audioLen = int(audioLen)
rawKeyData, err := io.ReadAll(io.LimitReader(d.raw, rawKeyLen))
if err != nil {
return err
}
2022-11-18 23:25:37 +00:00
// clean suffix NULs
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
d.decodedKey, err = deriveKey(rawKeyData)
if err != nil {
return err
}
2021-12-13 12:42:07 +00:00
return nil
}
func (d *Decoder) readRawMetaQTag() error {
2021-12-13 12:42:07 +00:00
// get raw meta data len
if _, err := d.raw.Seek(-8, io.SeekEnd); err != nil {
2021-12-13 12:42:07 +00:00
return err
}
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
2021-12-13 12:42:07 +00:00
if err != nil {
return err
}
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
// read raw meta data
audioLen, err := d.raw.Seek(-(8 + rawMetaLen), io.SeekEnd)
2021-12-13 12:42:07 +00:00
if err != nil {
return err
}
d.audioLen = int(audioLen)
rawMetaData, err := io.ReadAll(io.LimitReader(d.raw, rawMetaLen))
2021-12-13 12:42:07 +00:00
if err != nil {
return err
}
items := strings.Split(string(rawMetaData), ",")
if len(items) != 3 {
return errors.New("invalid raw meta data")
}
d.decodedKey, err = deriveKey([]byte(items[0]))
if err != nil {
return err
2021-12-13 12:42:07 +00:00
}
2022-11-22 03:08:35 +00:00
d.songID, err = strconv.Atoi(items[1])
2021-12-13 12:42:07 +00:00
if err != nil {
return err
}
d.rawMetaExtra2, err = strconv.Atoi(items[2])
if err != nil {
return err
}
return nil
}
2021-12-13 20:01:04 +00:00
//goland:noinspection SpellCheckingInspection
func init() {
supportedExts := []string{
"qmc0", "qmc3", //QQ Music MP3
"qmc2", "qmc4", "qmc6", "qmc8", //QQ Music M4A
"qmcflac", //QQ Music FLAC
"qmcogg", //QQ Music OGG
"tkm", //QQ Music Accompaniment M4A
"bkcmp3", "bkcm4a", "bkcflac", "bkcwav", "bkcape", "bkcogg", "bkcwma", //Moo Music
2021-12-13 20:01:04 +00:00
"666c6163", //QQ Music Weiyun Flac
"6d7033", //QQ Music Weiyun Mp3
"6f6767", //QQ Music Weiyun Ogg
"6d3461", //QQ Music Weiyun M4a
"776176", //QQ Music Weiyun Wav
"mgg", "mgg1", "mggl", //QQ Music New Ogg
2022-12-06 10:42:58 +00:00
"mflac", "mflac0", "mflach", //QQ Music New Flac
2022-12-06 10:42:58 +00:00
"mmp4", // QQ Music MP4 Container, tipically used for Dolby EAC3 stream
2021-12-13 20:01:04 +00:00
}
for _, ext := range supportedExts {
common.RegisterDecoder(ext, false, NewDecoder)
2021-12-13 20:01:04 +00:00
}
}