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"
|
2022-11-18 23:25:43 +00:00
|
|
|
"fmt"
|
2021-12-13 12:42:07 +00:00
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-12-13 18:19:32 +00:00
|
|
|
|
|
|
|
"github.com/unlock-music/cli/algo/common"
|
2021-12-13 12:42:07 +00:00
|
|
|
)
|
|
|
|
|
2021-12-13 18:19:32 +00:00
|
|
|
type Decoder struct {
|
2022-11-18 23:25:43 +00:00
|
|
|
raw io.ReadSeeker
|
|
|
|
audio io.Reader
|
|
|
|
offset int
|
|
|
|
audioLen int
|
2021-12-13 12:42:07 +00:00
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
cipher common.StreamDecoder
|
2021-12-13 12:42:07 +00:00
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
decodedKey []byte
|
2021-12-13 12:42:07 +00:00
|
|
|
rawMetaExtra1 int
|
|
|
|
rawMetaExtra2 int
|
|
|
|
}
|
|
|
|
|
2021-12-13 18:19:32 +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) {
|
2022-11-18 23:25:43 +00:00
|
|
|
n, err := d.audio.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
d.cipher.Decrypt(p[:n], d.offset)
|
|
|
|
d.offset += n
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
2022-11-18 23:25:43 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDecoder(r io.ReadSeeker) common.Decoder {
|
|
|
|
return &Decoder{raw: r}
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
func (d *Decoder) Validate() error {
|
|
|
|
// search & derive key
|
2021-12-13 14:54:08 +00:00
|
|
|
err := d.searchKey()
|
|
|
|
if err != nil {
|
2022-11-18 23:25:43 +00:00
|
|
|
return err
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
// check cipher type and init decode cipher
|
2021-12-13 18:19:32 +00:00
|
|
|
if len(d.decodedKey) > 300 {
|
2022-11-18 23:25:42 +00:00
|
|
|
d.cipher, err = newRC4Cipher(d.decodedKey)
|
2021-12-13 12:42:07 +00:00
|
|
|
if err != nil {
|
2022-11-18 23:25:43 +00:00
|
|
|
return err
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
2021-12-13 18:19:32 +00:00
|
|
|
} else if len(d.decodedKey) != 0 {
|
2022-11-18 23:25:42 +00:00
|
|
|
d.cipher, err = newMapCipher(d.decodedKey)
|
2021-12-13 14:54:08 +00:00
|
|
|
if err != nil {
|
2022-11-18 23:25:43 +00:00
|
|
|
return err
|
2021-12-13 14:54:08 +00:00
|
|
|
}
|
2021-12-13 18:19:32 +00:00
|
|
|
} else {
|
2022-11-18 23:25:42 +00:00
|
|
|
d.cipher = newStaticCipher()
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
// test with first 16 bytes
|
|
|
|
if err := d.validateDecode(); err != nil {
|
|
|
|
return err
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
2021-12-13 14:54:08 +00:00
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
// reset position, limit to audio, prepare for Read
|
|
|
|
if _, err := d.raw.Seek(0, io.SeekStart); err != nil {
|
2021-12-13 18:19:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-18 23:25:43 +00:00
|
|
|
d.audio = io.LimitReader(d.raw, int64(d.audioLen))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Decoder) validateDecode() error {
|
|
|
|
_, err := d.raw.Seek(0, io.SeekStart)
|
2021-12-13 18:19:32 +00:00
|
|
|
if err != nil {
|
2022-11-18 23:25:43 +00:00
|
|
|
return fmt.Errorf("qmc seek to start: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
if _, err := io.ReadFull(d.raw, buf); err != nil {
|
|
|
|
return fmt.Errorf("qmc read header: %w", err)
|
2021-12-13 18:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.cipher.Decrypt(buf, 0)
|
2022-11-18 23:25:43 +00:00
|
|
|
_, ok := common.SniffAll(buf)
|
2021-12-13 18:19:32 +00:00
|
|
|
if !ok {
|
2022-11-18 23:25:43 +00:00
|
|
|
return errors.New("qmc: detect file type failed")
|
2021-12-13 18:19:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Decoder) searchKey() error {
|
2022-11-18 23:25:43 +00:00
|
|
|
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
|
2021-12-13 18:19:32 +00:00
|
|
|
if err != nil {
|
2021-12-13 12:42:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-18 23:25:43 +00:00
|
|
|
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
|
2021-12-13 12:42:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if string(buf) == "QTag" {
|
2022-11-18 23:25:36 +00:00
|
|
|
return d.readRawMetaQTag()
|
|
|
|
} else if string(buf) == "STag" {
|
|
|
|
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
2021-12-13 14:54:08 +00:00
|
|
|
} else {
|
|
|
|
size := binary.LittleEndian.Uint32(buf)
|
2022-11-18 23:25:42 +00:00
|
|
|
if size <= 0xFFFF && size != 0 { // assume size is key len
|
2021-12-13 14:54:08 +00:00
|
|
|
return d.readRawKey(int64(size))
|
|
|
|
}
|
2022-11-18 23:25:36 +00:00
|
|
|
// try to use default static cipher
|
|
|
|
d.audioLen = int(fileSizeM4 + 4)
|
|
|
|
return nil
|
2021-12-13 14:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 18:19:32 +00:00
|
|
|
func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
2022-11-18 23:25:43 +00:00
|
|
|
audioLen, err := d.raw.Seek(-(4 + rawKeyLen), io.SeekEnd)
|
2021-12-13 14:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.audioLen = int(audioLen)
|
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
rawKeyData, err := io.ReadAll(io.LimitReader(d.raw, rawKeyLen))
|
2021-12-13 14:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:37 +00:00
|
|
|
// clean suffix NULs
|
|
|
|
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
|
|
|
|
|
2022-11-18 23:25:42 +00:00
|
|
|
d.decodedKey, err = deriveKey(rawKeyData)
|
2021-12-13 14:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-13 12:42:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-13 18:19:32 +00:00
|
|
|
func (d *Decoder) readRawMetaQTag() error {
|
2021-12-13 12:42:07 +00:00
|
|
|
// get raw meta data len
|
2022-11-18 23:25:43 +00:00
|
|
|
if _, err := d.raw.Seek(-8, io.SeekEnd); err != nil {
|
2021-12-13 12:42:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-18 23:25:43 +00:00
|
|
|
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
|
2022-11-18 23:25:43 +00:00
|
|
|
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)
|
2022-11-18 23:25:43 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:42 +00:00
|
|
|
d.decodedKey, err = deriveKey([]byte(items[0]))
|
2021-12-13 14:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.rawMetaExtra1, err = strconv.Atoi(items[1])
|
|
|
|
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
|
|
|
|
|
2022-01-08 22:30:38 +00:00
|
|
|
"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
|
|
|
|
|
2022-01-08 22:30:38 +00:00
|
|
|
"mgg", "mgg1", "mggl", //QQ Music New Ogg
|
2021-12-13 20:01:04 +00:00
|
|
|
"mflac", "mflac0", //QQ Music New Flac
|
|
|
|
}
|
|
|
|
for _, ext := range supportedExts {
|
2022-11-18 23:25:43 +00:00
|
|
|
common.RegisterDecoder(ext, false, NewDecoder)
|
2021-12-13 20:01:04 +00:00
|
|
|
}
|
|
|
|
}
|