Feature: QMC Decoder v2 #23
57
algo/qmc/cipher_static.go
Normal file
57
algo/qmc/cipher_static.go
Normal file
@ -0,0 +1,57 @@
|
||||
package qmc
|
||||
|
||||
func NewStaticCipher() *staticCipher {
|
||||
return &defaultStaticCipher
|
||||
}
|
||||
|
||||
var defaultStaticCipher = staticCipher{}
|
||||
|
||||
type staticCipher struct{}
|
||||
|
||||
func (c *staticCipher) Decrypt(buf []byte, offset int) {
|
||||
for i := 0; i < len(buf); i++ {
|
||||
buf[i] ^= c.getMask(offset + i)
|
||||
}
|
||||
}
|
||||
func (c *staticCipher) getMask(offset int) byte {
|
||||
if offset > 0x7FFF {
|
||||
offset %= 0x7FFF
|
||||
}
|
||||
idx := (offset*offset + 27) & 0xff
|
||||
return staticCipherBox[idx]
|
||||
}
|
||||
|
||||
var staticCipherBox = [...]byte{
|
||||
0x77, 0x48, 0x32, 0x73, 0xDE, 0xF2, 0xC0, 0xC8, //0x00
|
||||
0x95, 0xEC, 0x30, 0xB2, 0x51, 0xC3, 0xE1, 0xA0, //0x08
|
||||
0x9E, 0xE6, 0x9D, 0xCF, 0xFA, 0x7F, 0x14, 0xD1, //0x10
|
||||
0xCE, 0xB8, 0xDC, 0xC3, 0x4A, 0x67, 0x93, 0xD6, //0x18
|
||||
0x28, 0xC2, 0x91, 0x70, 0xCA, 0x8D, 0xA2, 0xA4, //0x20
|
||||
0xF0, 0x08, 0x61, 0x90, 0x7E, 0x6F, 0xA2, 0xE0, //0x28
|
||||
0xEB, 0xAE, 0x3E, 0xB6, 0x67, 0xC7, 0x92, 0xF4, //0x30
|
||||
0x91, 0xB5, 0xF6, 0x6C, 0x5E, 0x84, 0x40, 0xF7, //0x38
|
||||
0xF3, 0x1B, 0x02, 0x7F, 0xD5, 0xAB, 0x41, 0x89, //0x40
|
||||
0x28, 0xF4, 0x25, 0xCC, 0x52, 0x11, 0xAD, 0x43, //0x48
|
||||
0x68, 0xA6, 0x41, 0x8B, 0x84, 0xB5, 0xFF, 0x2C, //0x50
|
||||
0x92, 0x4A, 0x26, 0xD8, 0x47, 0x6A, 0x7C, 0x95, //0x58
|
||||
0x61, 0xCC, 0xE6, 0xCB, 0xBB, 0x3F, 0x47, 0x58, //0x60
|
||||
0x89, 0x75, 0xC3, 0x75, 0xA1, 0xD9, 0xAF, 0xCC, //0x68
|
||||
0x08, 0x73, 0x17, 0xDC, 0xAA, 0x9A, 0xA2, 0x16, //0x70
|
||||
0x41, 0xD8, 0xA2, 0x06, 0xC6, 0x8B, 0xFC, 0x66, //0x78
|
||||
0x34, 0x9F, 0xCF, 0x18, 0x23, 0xA0, 0x0A, 0x74, //0x80
|
||||
0xE7, 0x2B, 0x27, 0x70, 0x92, 0xE9, 0xAF, 0x37, //0x88
|
||||
0xE6, 0x8C, 0xA7, 0xBC, 0x62, 0x65, 0x9C, 0xC2, //0x90
|
||||
0x08, 0xC9, 0x88, 0xB3, 0xF3, 0x43, 0xAC, 0x74, //0x98
|
||||
0x2C, 0x0F, 0xD4, 0xAF, 0xA1, 0xC3, 0x01, 0x64, //0xA0
|
||||
0x95, 0x4E, 0x48, 0x9F, 0xF4, 0x35, 0x78, 0x95, //0xA8
|
||||
0x7A, 0x39, 0xD6, 0x6A, 0xA0, 0x6D, 0x40, 0xE8, //0xB0
|
||||
0x4F, 0xA8, 0xEF, 0x11, 0x1D, 0xF3, 0x1B, 0x3F, //0xB8
|
||||
0x3F, 0x07, 0xDD, 0x6F, 0x5B, 0x19, 0x30, 0x19, //0xC0
|
||||
0xFB, 0xEF, 0x0E, 0x37, 0xF0, 0x0E, 0xCD, 0x16, //0xC8
|
||||
0x49, 0xFE, 0x53, 0x47, 0x13, 0x1A, 0xBD, 0xA4, //0xD0
|
||||
0xF1, 0x40, 0x19, 0x60, 0x0E, 0xED, 0x68, 0x09, //0xD8
|
||||
0x06, 0x5F, 0x4D, 0xCF, 0x3D, 0x1A, 0xFE, 0x20, //0xE0
|
||||
0x77, 0xE4, 0xD9, 0xDA, 0xF9, 0xA4, 0x2B, 0x76, //0xE8
|
||||
0x1C, 0x71, 0xDB, 0x00, 0xBC, 0xFD, 0x0C, 0x6C, //0xF0
|
||||
0xA5, 0x47, 0xF7, 0xF6, 0x00, 0x79, 0x4A, 0x11, //0xF8
|
||||
}
|
@ -4,6 +4,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
)
|
||||
|
||||
@ -13,7 +14,7 @@ var (
|
||||
ErrQmcKeyLength = errors.New("unexpected decoded qmc key length")
|
||||
)
|
||||
|
||||
type Decoder struct {
|
||||
type OldDecoder struct {
|
||||
file []byte
|
||||
maskDetector func(encodedData []byte) (*Key256Mask, error)
|
||||
mask *Key256Mask
|
||||
@ -23,14 +24,14 @@ type Decoder struct {
|
||||
}
|
||||
|
||||
func NewMflac256Decoder(data []byte) common.Decoder {
|
||||
return &Decoder{file: data, maskDetector: detectMflac256Mask, audioExt: "flac"}
|
||||
return &OldDecoder{file: data, maskDetector: detectMflac256Mask, audioExt: "flac"}
|
||||
}
|
||||
|
||||
func NewMgg256Decoder(data []byte) common.Decoder {
|
||||
return &Decoder{file: data, maskDetector: detectMgg256Mask, audioExt: "ogg"}
|
||||
return &OldDecoder{file: data, maskDetector: detectMgg256Mask, audioExt: "ogg"}
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
func (d *OldDecoder) Validate() error {
|
||||
if nil != d.mask {
|
||||
return nil
|
||||
}
|
||||
@ -45,7 +46,7 @@ func (d *Decoder) Validate() error {
|
||||
return errors.New("no mask or mask detector found")
|
||||
}
|
||||
|
||||
func (d *Decoder) validateKey() error {
|
||||
func (d *OldDecoder) validateKey() error {
|
||||
lenData := len(d.file)
|
||||
if lenData < 4 {
|
||||
return ErrQmcFileLength
|
||||
@ -70,33 +71,33 @@ func (d *Decoder) validateKey() error {
|
||||
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode() error {
|
||||
func (d *OldDecoder) Decode() error {
|
||||
d.audio = d.mask.Decrypt(d.file)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Decoder) GetCoverImage() []byte {
|
||||
func (d OldDecoder) GetCoverImage() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Decoder) GetAudioData() []byte {
|
||||
func (d OldDecoder) GetAudioData() []byte {
|
||||
return d.audio
|
||||
}
|
||||
|
||||
func (d Decoder) GetAudioExt() string {
|
||||
func (d OldDecoder) GetAudioExt() string {
|
||||
if d.audioExt != "" {
|
||||
return "." + d.audioExt
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (d Decoder) GetMeta() common.Meta {
|
||||
func (d OldDecoder) GetMeta() common.Meta {
|
||||
return nil
|
||||
}
|
||||
|
||||
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
|
||||
return func(file []byte) common.Decoder {
|
||||
return &Decoder{file: file, audioExt: ext, mask: getDefaultMask()}
|
||||
return &OldDecoder{file: file, audioExt: ext, mask: getDefaultMask()}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,10 +6,13 @@ import (
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
)
|
||||
|
||||
type Mflac0Decoder struct {
|
||||
type Decoder struct {
|
||||
r io.ReadSeeker
|
||||
fileExt string
|
||||
|
||||
audioLen int
|
||||
decodedKey []byte
|
||||
@ -20,15 +23,9 @@ type Mflac0Decoder struct {
|
||||
rawMetaExtra2 int
|
||||
}
|
||||
|
||||
func (d *Mflac0Decoder) Read(p []byte) (int, error) {
|
||||
if d.cipher != nil {
|
||||
return d.readRC4(p)
|
||||
} else {
|
||||
panic("not impl")
|
||||
//return d.readPlain(p)
|
||||
}
|
||||
}
|
||||
func (d *Mflac0Decoder) readRC4(p []byte) (int, error) {
|
||||
// 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 := len(p)
|
||||
if d.audioLen-d.offset <= 0 {
|
||||
return 0, io.EOF
|
||||
@ -45,25 +42,25 @@ func (d *Mflac0Decoder) readRC4(p []byte) (int, error) {
|
||||
return m, err
|
||||
}
|
||||
|
||||
func NewMflac0Decoder(r io.ReadSeeker) (*Mflac0Decoder, error) {
|
||||
d := &Mflac0Decoder{r: r}
|
||||
func NewDecoder(r io.ReadSeeker) (*Decoder, error) {
|
||||
d := &Decoder{r: r}
|
||||
err := d.searchKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(d.decodedKey) == 0 {
|
||||
return nil, errors.New("invalid decoded key")
|
||||
} else if len(d.decodedKey) > 300 {
|
||||
if len(d.decodedKey) > 300 {
|
||||
d.cipher, err = NewRC4Cipher(d.decodedKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
} else if len(d.decodedKey) != 0 {
|
||||
d.cipher, err = NewMapCipher(d.decodedKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
d.cipher = NewStaticCipher()
|
||||
}
|
||||
|
||||
_, err = d.r.Seek(0, io.SeekStart)
|
||||
@ -74,8 +71,32 @@ func NewMflac0Decoder(r io.ReadSeeker) (*Mflac0Decoder, error) {
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *Mflac0Decoder) searchKey() error {
|
||||
if _, err := d.r.Seek(-4, io.SeekEnd); err != nil {
|
||||
func (d *Decoder) Validate() error {
|
||||
buf := make([]byte, 16)
|
||||
if _, err := io.ReadFull(d.r, buf); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := d.r.Seek(0, io.SeekStart)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.cipher.Decrypt(buf, 0)
|
||||
fileExt, ok := common.SniffAll(buf)
|
||||
if !ok {
|
||||
return errors.New("detect file type failed")
|
||||
}
|
||||
d.fileExt = fileExt
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Decoder) GetFileExt() string {
|
||||
return d.fileExt
|
||||
}
|
||||
|
||||
func (d *Decoder) searchKey() error {
|
||||
fileSizeM4, err := d.r.Seek(-4, io.SeekEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
||||
@ -88,17 +109,18 @@ func (d *Mflac0Decoder) searchKey() error {
|
||||
}
|
||||
} else {
|
||||
size := binary.LittleEndian.Uint32(buf)
|
||||
if size < 0x300 {
|
||||
if size < 0x300 && size != 0 {
|
||||
return d.readRawKey(int64(size))
|
||||
} else {
|
||||
// todo: try to use fixed key
|
||||
panic("not impl")
|
||||
// try to use default static cipher
|
||||
d.audioLen = int(fileSizeM4 + 4)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Mflac0Decoder) readRawKey(rawKeyLen int64) error {
|
||||
func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
||||
audioLen, err := d.r.Seek(-(4 + rawKeyLen), io.SeekEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -118,7 +140,7 @@ func (d *Mflac0Decoder) readRawKey(rawKeyLen int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Mflac0Decoder) readRawMetaQTag() error {
|
||||
func (d *Decoder) readRawMetaQTag() error {
|
||||
// get raw meta data len
|
||||
if _, err := d.r.Seek(-8, io.SeekEnd); err != nil {
|
||||
return err
|
@ -29,23 +29,24 @@ func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
|
||||
func TestMflac0Decoder_Read(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
filename string
|
||||
wantErr bool
|
||||
}{
|
||||
{"mflac0_rc4(512)", "mflac0_rc4", false},
|
||||
{"mflac_map(256)", "mflac_map", false},
|
||||
{"mflac0_rc4", false},
|
||||
{"mflac_map", false},
|
||||
{"qmc0_static", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
raw, target, err := loadTestDataQmcDecoder(tt.filename)
|
||||
raw, target, err := loadTestDataQmcDecoder(tt.name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d, err := NewMflac0Decoder(bytes.NewReader(raw))
|
||||
d, err := NewDecoder(bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
buf := make([]byte, len(target))
|
||||
if _, err := io.ReadFull(d, buf); err != nil {
|
||||
@ -59,3 +60,37 @@ func TestMflac0Decoder_Read(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMflac0Decoder_Validate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
fileExt string
|
||||
wantErr bool
|
||||
}{
|
||||
{"mflac0_rc4", ".flac", false},
|
||||
{"mflac_map", ".flac", false},
|
||||
{"qmc0_static", ".mp3", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
raw, _, err := loadTestDataQmcDecoder(tt.name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d, err := NewDecoder(bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := d.Validate(); err != nil {
|
||||
t.Errorf("read bytes from decoder error = %v", err)
|
||||
return
|
||||
}
|
||||
if tt.fileExt != d.GetFileExt() {
|
||||
t.Errorf("Decrypt() got = %v, want %v", d.GetFileExt(), tt.fileExt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
BIN
algo/qmc/testdata/qmc0_static_raw.bin
vendored
Normal file
BIN
algo/qmc/testdata/qmc0_static_raw.bin
vendored
Normal file
Binary file not shown.
0
algo/qmc/testdata/qmc0_static_suffix.bin
vendored
Normal file
0
algo/qmc/testdata/qmc0_static_suffix.bin
vendored
Normal file
BIN
algo/qmc/testdata/qmc0_static_target.bin
vendored
Normal file
BIN
algo/qmc/testdata/qmc0_static_target.bin
vendored
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user