Merge pull request #23 from unlock-music/feature/qmc-v2

Feature: QMC Decoder v2
This commit is contained in:
MengYX 2021-12-14 04:09:17 +08:00 committed by GitHub
commit 9390b45207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 914 additions and 388 deletions

View File

@ -12,15 +12,15 @@ type decoderItem struct {
decoder NewDecoderFunc
}
var decoderRegistry = make(map[string][]decoderItem)
var DecoderRegistry = make(map[string][]decoderItem)
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
decoderRegistry[ext] = append(decoderRegistry[ext],
DecoderRegistry[ext] = append(DecoderRegistry[ext],
decoderItem{noop: noop, decoder: dispatchFunc})
}
func GetDecoder(filename string, skipNoop bool) (rs []NewDecoderFunc) {
ext := strings.ToLower(strings.TrimLeft(filepath.Ext(filename), "."))
for _, dec := range decoderRegistry[ext] {
for _, dec := range DecoderRegistry[ext] {
if skipNoop && dec.noop {
continue
}

5
algo/qmc/cipher.go Normal file
View File

@ -0,0 +1,5 @@
package qmc
type streamCipher interface {
Decrypt(buf []byte, offset int)
}

39
algo/qmc/cipher_map.go Normal file
View File

@ -0,0 +1,39 @@
package qmc
import "errors"
type mapCipher struct {
key []byte
box []byte
size int
}
func NewMapCipher(key []byte) (*mapCipher, error) {
if len(key) == 0 {
return nil, errors.New("qmc/cipher_map: invalid key size")
}
c := &mapCipher{key: key, size: len(key)}
c.box = make([]byte, c.size)
return c, nil
}
func (c *mapCipher) getMask(offset int) byte {
if offset > 0x7FFF {
offset %= 0x7FFF
}
idx := (offset*offset + 71214) % c.size
return c.rotate(c.key[idx], byte(idx)&0x7)
}
func (c *mapCipher) rotate(value byte, bits byte) byte {
rotate := (bits + 4) % 8
left := value << rotate
right := value >> rotate
return left | right
}
func (c *mapCipher) Decrypt(buf []byte, offset int) {
for i := 0; i < len(buf); i++ {
buf[i] ^= c.getMask(offset + i)
}
}

View File

@ -0,0 +1,53 @@
package qmc
import (
"fmt"
"os"
"reflect"
"testing"
)
func loadTestDataMapCipher(name string) ([]byte, []byte, []byte, error) {
key, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key.bin", name))
if err != nil {
return nil, nil, nil, err
}
raw, err := os.ReadFile(fmt.Sprintf("./testdata/%s_raw.bin", name))
if err != nil {
return nil, nil, nil, err
}
target, err := os.ReadFile(fmt.Sprintf("./testdata/%s_target.bin", name))
if err != nil {
return nil, nil, nil, err
}
return key, raw, target, nil
}
func Test_mapCipher_Decrypt(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{"mflac_map", false},
{"mgg_map", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key, raw, target, err := loadTestDataMapCipher(tt.name)
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
c, err := NewMapCipher(key)
if err != nil {
t.Errorf("init mapCipher failed: %s", err)
return
}
c.Decrypt(raw, 0)
if !reflect.DeepEqual(raw, target) {
t.Error("overall")
}
})
}
}

133
algo/qmc/cipher_rc4.go Normal file
View File

@ -0,0 +1,133 @@
package qmc
import (
"errors"
)
// A rc4Cipher is an instance of RC4 using a particular key.
type rc4Cipher struct {
box []byte
key []byte
hash uint32
boxTmp []byte
}
// NewRC4Cipher creates and returns a new rc4Cipher. The key argument should be the
// RC4 key, at least 1 byte and at most 256 bytes.
func NewRC4Cipher(key []byte) (*rc4Cipher, error) {
n := len(key)
if n == 0 {
return nil, errors.New("qmc/cipher_rc4: invalid key size")
}
var c = rc4Cipher{key: key}
c.box = make([]byte, n)
c.boxTmp = make([]byte, n)
for i := 0; i < n; i++ {
c.box[i] = byte(i)
}
var j = 0
for i := 0; i < n; i++ {
j = (j + int(c.box[i]) + int(key[i%n])) % n
c.box[i], c.box[j] = c.box[j], c.box[i]
}
c.getHashBase()
return &c, nil
}
func (c *rc4Cipher) getHashBase() {
c.hash = 1
for i := 0; i < len(c.key); i++ {
v := uint32(c.key[i])
if v == 0 {
continue
}
nextHash := c.hash * v
if nextHash == 0 || nextHash <= c.hash {
break
}
c.hash = nextHash
}
}
const rc4SegmentSize = 5120
func (c *rc4Cipher) Decrypt(src []byte, offset int) {
toProcess := len(src)
processed := 0
markProcess := func(p int) (finished bool) {
offset += p
toProcess -= p
processed += p
return toProcess == 0
}
if offset < 128 {
blockSize := toProcess
if blockSize > 128-offset {
blockSize = 128 - offset
}
c.encFirstSegment(src[:blockSize], offset)
if markProcess(blockSize) {
return
}
}
if offset%rc4SegmentSize != 0 {
blockSize := toProcess
if blockSize > rc4SegmentSize-offset%rc4SegmentSize {
blockSize = rc4SegmentSize - offset%rc4SegmentSize
}
k := src[processed : processed+blockSize]
c.encASegment(k, offset)
if markProcess(blockSize) {
return
}
}
for toProcess > rc4SegmentSize {
c.encASegment(src[processed:processed+rc4SegmentSize], offset)
markProcess(rc4SegmentSize)
}
if toProcess > 0 {
c.encASegment(src[processed:], offset)
}
}
func (c *rc4Cipher) encFirstSegment(buf []byte, offset int) {
n := len(c.box)
for i := 0; i < len(buf); i++ {
idx1 := offset + i
segmentID := int(c.key[idx1%n])
idx2 := int(float64(c.hash) / float64((idx1+1)*segmentID) * 100.0)
buf[i] ^= c.key[idx2%n]
}
}
func (c *rc4Cipher) encASegment(buf []byte, offset int) {
n := len(c.box)
copy(c.boxTmp, c.box)
segmentID := (offset / rc4SegmentSize) & 0x1FF
if n <= segmentID {
return
}
idx2 := int64(float64(c.hash) /
float64((offset/rc4SegmentSize+1)*int(c.key[segmentID])) *
100.0)
skipLen := int((idx2 & 0x1FF) + int64(offset%rc4SegmentSize))
j, k := 0, 0
for i := -skipLen; i < len(buf); i++ {
j = (j + 1) % n
k = (int(c.boxTmp[j]) + k) % n
c.boxTmp[j], c.boxTmp[k] = c.boxTmp[k], c.boxTmp[j]
if i >= 0 {
buf[i] ^= c.boxTmp[int(c.boxTmp[j])+int(c.boxTmp[k])%n]
}
}
}

View File

@ -0,0 +1,90 @@
package qmc
import (
"os"
"reflect"
"testing"
)
func loadTestRC4CipherData() ([]byte, []byte, []byte, error) {
key, err := os.ReadFile("./testdata/mflac0_rc4_key.bin")
if err != nil {
return nil, nil, nil, err
}
raw, err := os.ReadFile("./testdata/mflac0_rc4_raw.bin")
if err != nil {
return nil, nil, nil, err
}
target, err := os.ReadFile("./testdata/mflac0_rc4_target.bin")
if err != nil {
return nil, nil, nil, err
}
return key, raw, target, nil
}
func Test_rc4Cipher_Decrypt(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("overall", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw, 0)
if !reflect.DeepEqual(raw, target) {
t.Error("overall")
}
})
}
func Test_rc4Cipher_encFirstSegment(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("first-block(0~128)", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[:128], 0)
if !reflect.DeepEqual(raw[:128], target[:128]) {
t.Error("first-block(0~128)")
}
})
}
func Test_rc4Cipher_encASegment(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("align-block(128~5120)", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[128:5120], 128)
if !reflect.DeepEqual(raw[128:5120], target[128:5120]) {
t.Error("align-block(128~5120)")
}
})
t.Run("simple-block(5120~10240)", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[5120:10240], 5120)
if !reflect.DeepEqual(raw[5120:10240], target[5120:10240]) {
t.Error("align-block(128~5120)")
}
})
}

57
algo/qmc/cipher_static.go Normal file
View 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
}

View File

@ -1,70 +0,0 @@
package qmc
var oggPublicHeader1 = []byte{
0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x1e, 0x01, 0x76, 0x6f, 0x72,
0x62, 0x69, 0x73, 0x00, 0x00, 0x00, 0x00, 0x02, 0x44, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xee, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x01, 0x4f, 0x67, 0x67, 0x53, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff}
var oggPublicHeader2 = []byte{
0x03, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73, 0x2c, 0x00, 0x00, 0x00, 0x58, 0x69, 0x70, 0x68, 0x2e,
0x4f, 0x72, 0x67, 0x20, 0x6c, 0x69, 0x62, 0x56, 0x6f, 0x72, 0x62, 0x69, 0x73, 0x20, 0x49, 0x20,
0x32, 0x30, 0x31, 0x35, 0x30, 0x31, 0x30, 0x35, 0x20, 0x28, 0xe2, 0x9b, 0x84, 0xe2, 0x9b, 0x84,
0xe2, 0x9b, 0x84, 0xe2, 0x9b, 0x84, 0x29, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x54,
0x49, 0x54, 0x4c, 0x45, 0x3d}
var oggPublicConfidence1 = []uint{
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0,
0, 0, 9, 9, 9, 9, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 6, 3, 3, 3, 3, 6, 6, 6, 6,
3, 3, 3, 3, 6, 6, 6, 6, 6, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 0, 0, 9, 9, 9, 9,
0, 0, 0, 0}
var oggPublicConfidence2 = []uint{
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 0, 1, 3, 3, 0, 1, 3, 3, 3,
3, 3, 3, 3, 3}
var (
defaultKey256Mask44 = []byte{
0xde, 0x51, 0xfa, 0xc3, 0x4a, 0xd6, 0xca, 0x90,
0x7e, 0x67, 0x5e, 0xf7, 0xd5, 0x52, 0x84, 0xd8,
0x47, 0x95, 0xbb, 0xa1, 0xaa, 0xc6, 0x66, 0x23,
0x92, 0x62, 0xf3, 0x74, 0xa1, 0x9f, 0xf4, 0xa0,
0x1d, 0x3f, 0x5b, 0xf0, 0x13, 0x0e, 0x09, 0x3d,
0xf9, 0xbc, 0x00, 0x11}
)
var key256MappingAll [][]int //[idx256][idx128]idx44
var key256Mapping128to44 map[int]int
func init() {
{ // init all mapping
key256MappingAll = make([][]int, 256)
for i := 0; i < 128; i++ {
realIdx := (i*i + 27) % 256
if key256MappingAll[realIdx] == nil {
key256MappingAll[realIdx] = []int{i}
} else {
key256MappingAll[realIdx] = append(key256MappingAll[realIdx], i)
}
}
}
{ // init
key256Mapping128to44 = make(map[int]int, 128)
idx44 := 0
for _, all128 := range key256MappingAll {
if all128 != nil {
for _, _i128 := range all128 {
key256Mapping128to44[_i128] = idx44
}
idx44++
}
}
}
}

115
algo/qmc/key_dec.go Normal file
View File

@ -0,0 +1,115 @@
package qmc
import (
"encoding/base64"
"errors"
"math"
"golang.org/x/crypto/tea"
)
func simpleMakeKey(salt byte, length int) []byte {
keyBuf := make([]byte, length)
for i := 0; i < length; i++ {
tmp := math.Tan(float64(salt) + float64(i)*0.1)
keyBuf[i] = byte(math.Abs(tmp) * 100.0)
}
return keyBuf
}
func DecryptKey(rawKey []byte) ([]byte, error) {
rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey)))
n, err := base64.StdEncoding.Decode(rawKeyDec, rawKey)
if err != nil {
return nil, err
}
if n < 16 {
return nil, errors.New("key length is too short")
}
rawKeyDec = rawKeyDec[:n]
simpleKey := simpleMakeKey(106, 8)
teaKey := make([]byte, 16)
for i := 0; i < 8; i++ {
teaKey[i<<1] = simpleKey[i]
teaKey[i<<1+1] = rawKeyDec[i]
}
rs, err := decryptTencentTea(rawKeyDec[8:], teaKey)
if err != nil {
return nil, err
}
return append(rawKeyDec[:8], rs...), nil
}
func decryptTencentTea(inBuf []byte, key []byte) ([]byte, error) {
const saltLen = 2
const zeroLen = 7
if len(inBuf)%8 != 0 {
return nil, errors.New("inBuf size not a multiple of the block size")
}
if len(inBuf) < 16 {
return nil, errors.New("inBuf size too small")
}
blk, err := tea.NewCipherWithRounds(key, 32)
if err != nil {
return nil, err
}
destBuf := make([]byte, 8)
blk.Decrypt(destBuf, inBuf)
padLen := int(destBuf[0] & 0x7)
outLen := len(inBuf) - 1 - padLen - saltLen - zeroLen
if padLen+saltLen != 8 {
return nil, errors.New("invalid pad len")
}
out := make([]byte, outLen)
ivPrev := make([]byte, 8)
ivCur := inBuf[:8]
inBufPos := 8
destIdx := 1 + padLen
cryptBlock := func() {
ivPrev = ivCur
ivCur = inBuf[inBufPos : inBufPos+8]
xor8Bytes(destBuf, destBuf, inBuf[inBufPos:inBufPos+8])
blk.Decrypt(destBuf, destBuf)
inBufPos += 8
destIdx = 0
}
for i := 1; i <= saltLen; {
if destIdx < 8 {
destIdx++
i++
} else if destIdx == 8 {
cryptBlock()
}
}
outPos := 0
for outPos < outLen {
if destIdx < 8 {
out[outPos] = destBuf[destIdx] ^ ivPrev[destIdx]
destIdx++
outPos++
} else if destIdx == 8 {
cryptBlock()
}
}
for i := 1; i <= zeroLen; i++ {
if destBuf[destIdx] != ivPrev[destIdx] {
return nil, errors.New("zero check failed")
}
}
return out, nil
}
func xor8Bytes(dst, a, b []byte) {
for i := 0; i < 8; i++ {
dst[i] = a[i] ^ b[i]
}
}

56
algo/qmc/key_dec_test.go Normal file
View File

@ -0,0 +1,56 @@
package qmc
import (
"fmt"
"os"
"reflect"
"testing"
)
func TestSimpleMakeKey(t *testing.T) {
expect := []byte{0x69, 0x56, 0x46, 0x38, 0x2b, 0x20, 0x15, 0x0b}
t.Run("106,8", func(t *testing.T) {
if got := simpleMakeKey(106, 8); !reflect.DeepEqual(got, expect) {
t.Errorf("simpleMakeKey() = %v, want %v", got, expect)
}
})
}
func loadDecryptKeyData(name string) ([]byte, []byte, error) {
keyRaw, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key_raw.bin", name))
if err != nil {
return nil, nil, err
}
keyDec, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key.bin", name))
if err != nil {
return nil, nil, err
}
return keyRaw, keyDec, nil
}
func TestDecryptKey(t *testing.T) {
tests := []struct {
name string
filename string
wantErr bool
}{
{"mflac0_rc4(512)", "mflac0_rc4", false},
{"mflac_map(256)", "mflac_map", false},
}
for _, tt := range tests {
raw, want, err := loadDecryptKeyData(tt.filename)
if err != nil {
t.Fatalf("load test data failed: %s", err)
}
t.Run(tt.name, func(t *testing.T) {
got, err := DecryptKey(raw)
if (err != nil) != tt.wantErr {
t.Errorf("DecryptKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("DecryptKey() got = %v..., want %v...",
string(got[:32]), string(want[:32]))
}
})
}
}

View File

@ -1,212 +0,0 @@
package qmc
import (
"bytes"
"errors"
"github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/logging"
"go.uber.org/zap"
)
var (
ErrFailToMatchMask = errors.New("can not match at least one key")
ErrTestDataLength = errors.New("invalid length of test file")
ErrMaskLength128 = errors.New("incorrect mask length 128")
ErrMaskLength44 = errors.New("incorrect mask length 44")
ErrMaskDecode = errors.New("decode mask-128 to mask-58 failed")
ErrDetectFlacMask = errors.New("can not detect mflac mask")
ErrDetectMggMask = errors.New("can not detect mgg mask")
)
type Key256Mask struct {
matrix []byte // Mask 128
}
func NewKey256FromMask128(mask128 []byte) (*Key256Mask, error) {
if len(mask128) != 128 {
return nil, ErrMaskLength128
}
q := &Key256Mask{matrix: mask128}
return q, nil
}
func NewKey256FromMask44(mask44 []byte) (*Key256Mask, error) {
mask128, err := convertKey256Mask44to128(mask44)
if err != nil {
return nil, err
}
q := &Key256Mask{matrix: mask128}
return q, nil
}
func (q *Key256Mask) GetMatrix44() ([]byte, error) {
if len(q.matrix) != 128 {
return nil, ErrMaskLength128
}
matrix44 := make([]byte, 44)
idx44 := 0
for _, it256 := range key256MappingAll {
if it256 != nil {
it256Len := len(it256)
for i := 1; i < it256Len; i++ {
if q.matrix[it256[0]] != q.matrix[it256[i]] {
return nil, ErrMaskDecode
}
}
matrix44[idx44] = q.matrix[it256[0]]
idx44++
}
}
return matrix44, nil
}
func (q *Key256Mask) Decrypt(data []byte) []byte {
dst := make([]byte, len(data))
index := -1
maskIdx := -1
for cur := 0; cur < len(data); cur++ {
index++
maskIdx++
if index == 0x8000 || (index > 0x8000 && (index+1)%0x8000 == 0) {
index++
maskIdx++
}
if maskIdx >= 128 {
maskIdx -= 128
}
dst[cur] = data[cur] ^ q.matrix[maskIdx]
}
return dst
}
func convertKey256Mask44to128(mask44 []byte) ([]byte, error) {
if len(mask44) != 44 {
return nil, ErrMaskLength44
}
mask128 := make([]byte, 128)
idx44 := 0
for _, it256 := range key256MappingAll {
if it256 != nil {
for _, idx128 := range it256 {
mask128[idx128] = mask44[idx44]
}
idx44++
}
}
return mask128, nil
}
func getDefaultMask() *Key256Mask {
y, _ := NewKey256FromMask44(defaultKey256Mask44)
return y
}
func detectMflac256Mask(input []byte) (*Key256Mask, error) {
var q *Key256Mask
var rtErr = ErrDetectFlacMask
lenData := len(input)
lenTest := 0x8000
if lenData < 0x8000 {
lenTest = lenData
}
for blockIdx := 0; blockIdx < lenTest; blockIdx += 128 {
var err error
q, err = NewKey256FromMask128(input[blockIdx : blockIdx+128])
if err != nil {
continue
}
if common.SnifferFLAC(q.Decrypt(input[:4])) {
rtErr = nil
break
}
}
return q, rtErr
}
func detectMgg256Mask(input []byte) (*Key256Mask, error) {
if len(input) < 0x100 {
return nil, ErrTestDataLength
}
matrixConf := make([]map[uint8]uint, 44) //meaning: [idx58][value]confidence
for i := uint(0); i < 44; i++ {
matrixConf[i] = make(map[uint8]uint)
}
page2size := input[0x54] ^ input[0xC] ^ oggPublicHeader1[0xC]
spHeader, spConf := generateOggFullHeader(int(page2size))
lenTest := len(spHeader)
for idx128 := 0; idx128 < lenTest; idx128++ {
confidence := spConf[idx128]
if confidence > 0 {
mask := input[idx128] ^ spHeader[idx128]
idx44 := key256Mapping128to44[idx128&0x7f] // equals: [idx128 % 128]
if _, ok2 := matrixConf[idx44][mask]; ok2 {
matrixConf[idx44][mask] += confidence
} else {
matrixConf[idx44][mask] = confidence
}
}
}
matrix := make([]uint8, 44)
var err error
for i := uint(0); i < 44; i++ {
matrix[i], err = decideMgg256MaskItemConf(matrixConf[i])
if err != nil {
return nil, err
}
}
q, err := NewKey256FromMask44(matrix)
if err != nil {
return nil, err
}
if common.SnifferOGG(q.Decrypt(input[:4])) {
return q, nil
}
return nil, ErrDetectMggMask
}
func generateOggFullHeader(pageSize int) ([]byte, []uint) {
spec := make([]byte, pageSize+1)
spec[0], spec[1], spec[pageSize] = uint8(pageSize), 0xFF, 0xFF
for i := 2; i < pageSize; i++ {
spec[i] = 0xFF
}
specConf := make([]uint, pageSize+1)
specConf[0], specConf[1], specConf[pageSize] = 6, 0, 0
for i := 2; i < pageSize; i++ {
specConf[i] = 4
}
allConf := append(oggPublicConfidence1, specConf...)
allConf = append(allConf, oggPublicConfidence2...)
allHeader := bytes.Join(
[][]byte{oggPublicHeader1, spec, oggPublicHeader2},
[]byte{},
)
return allHeader, allConf
}
func decideMgg256MaskItemConf(confidence map[uint8]uint) (uint8, error) {
lenConf := len(confidence)
if lenConf == 0 {
return 0xff, ErrFailToMatchMask
} else if lenConf > 1 {
logging.Log().Warn("there are 2 potential value for the mask", zap.Any("confidence", confidence))
}
result := uint8(0)
conf := uint(0)
for idx, item := range confidence {
if item > conf {
result = idx
conf = item
}
}
return result, nil
}

View File

@ -1,128 +1,263 @@
package qmc
import (
"encoding/base64"
"bytes"
"encoding/binary"
"errors"
"io"
"strconv"
"strings"
"github.com/unlock-music/cli/algo/common"
)
var (
ErrQmcFileLength = errors.New("invalid qmc file length")
ErrQmcKeyDecodeFailed = errors.New("base64 decode qmc key failed")
ErrQmcKeyLength = errors.New("unexpected decoded qmc key length")
)
type Decoder struct {
file []byte
maskDetector func(encodedData []byte) (*Key256Mask, error)
mask *Key256Mask
audioExt string
key []byte
audio []byte
r io.ReadSeeker
fileExt string
audioLen int
decodedKey []byte
cipher streamCipher
offset int
rawMetaExtra1 int
rawMetaExtra2 int
}
func NewMflac256Decoder(data []byte) common.Decoder {
return &Decoder{file: data, maskDetector: detectMflac256Mask, audioExt: "flac"}
// 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
} else if d.audioLen-d.offset < n {
n = d.audioLen - d.offset
}
m, err := d.r.Read(p[:n])
if m == 0 {
return 0, err
}
d.cipher.Decrypt(p[:m], d.offset)
d.offset += m
return m, err
}
func NewMgg256Decoder(data []byte) common.Decoder {
return &Decoder{file: data, maskDetector: detectMgg256Mask, audioExt: "ogg"}
func NewDecoder(r io.ReadSeeker) (*Decoder, error) {
d := &Decoder{r: r}
err := d.searchKey()
if err != nil {
return nil, err
}
if len(d.decodedKey) > 300 {
d.cipher, err = NewRC4Cipher(d.decodedKey)
if err != nil {
return nil, err
}
} 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)
if err != nil {
return nil, err
}
return d, nil
}
func (d *Decoder) Validate() error {
if nil != d.mask {
return nil
}
if nil != d.maskDetector {
if err := d.validateKey(); err != nil {
buf := make([]byte, 16)
if _, err := io.ReadFull(d.r, buf); err != nil {
return err
}
var err error
d.mask, err = d.maskDetector(d.file)
return err
}
return errors.New("no mask or mask detector found")
}
func (d *Decoder) validateKey() error {
lenData := len(d.file)
if lenData < 4 {
return ErrQmcFileLength
}
keyLen := binary.LittleEndian.Uint32(d.file[lenData-4:])
if lenData < int(keyLen+4) {
return ErrQmcFileLength
}
var err error
d.key, err = base64.StdEncoding.DecodeString(
string(d.file[lenData-4-int(keyLen) : lenData-4]))
_, err := d.r.Seek(0, io.SeekStart)
if err != nil {
return ErrQmcKeyDecodeFailed
return err
}
if len(d.key) != 272 {
return ErrQmcKeyLength
d.cipher.Decrypt(buf, 0)
fileExt, ok := common.SniffAll(buf)
if !ok {
return errors.New("detect file type failed")
}
d.file = d.file[:lenData-4-int(keyLen)]
return nil
}
func (d *Decoder) Decode() error {
d.audio = d.mask.Decrypt(d.file)
d.fileExt = fileExt
return nil
}
func (d Decoder) GetCoverImage() []byte {
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))
if err != nil {
return err
}
if string(buf) == "QTag" {
if err := d.readRawMetaQTag(); err != nil {
return err
}
} else {
size := binary.LittleEndian.Uint32(buf)
if size < 0x300 && size != 0 {
return d.readRawKey(int64(size))
} else {
// try to use default static cipher
d.audioLen = int(fileSizeM4 + 4)
return nil
}
}
return nil
}
func (d Decoder) GetAudioData() []byte {
return d.audio
}
func (d Decoder) GetAudioExt() string {
if d.audioExt != "" {
return "." + d.audioExt
func (d *Decoder) readRawKey(rawKeyLen int64) error {
audioLen, err := d.r.Seek(-(4 + rawKeyLen), io.SeekEnd)
if err != nil {
return err
}
d.audioLen = int(audioLen)
rawKeyData, err := io.ReadAll(io.LimitReader(d.r, rawKeyLen))
if err != nil {
return err
}
d.decodedKey, err = DecryptKey(rawKeyData)
if err != nil {
return err
}
return ""
}
func (d Decoder) 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()}
func (d *Decoder) readRawMetaQTag() error {
// get raw meta data len
if _, err := d.r.Seek(-8, io.SeekEnd); err != nil {
return err
}
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
if err != nil {
return err
}
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
// read raw meta data
audioLen, err := d.r.Seek(-(8 + rawMetaLen), io.SeekEnd)
if err != nil {
return err
}
d.audioLen = int(audioLen)
rawMetaData, err := io.ReadAll(io.LimitReader(d.r, rawMetaLen))
if err != nil {
return err
}
items := strings.Split(string(rawMetaData), ",")
if len(items) != 3 {
return errors.New("invalid raw meta data")
}
d.decodedKey, err = DecryptKey([]byte(items[0]))
if err != nil {
return err
}
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
}
//goland:noinspection SpellCheckingInspection
func init() {
common.RegisterDecoder("qmc0", false, DecoderFuncWithExt("mp3")) //QQ Music Mp3
common.RegisterDecoder("qmc3", false, DecoderFuncWithExt("mp3")) //QQ Music Mp3
supportedExts := []string{
"qmc0", "qmc3", //QQ Music MP3
"qmc2", "qmc4", "qmc6", "qmc8", //QQ Music M4A
"qmcflac", //QQ Music FLAC
"qmcogg", //QQ Music OGG
common.RegisterDecoder("qmc2", false, DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc4", false, DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc6", false, DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc8", false, DecoderFuncWithExt("m4a")) //QQ Music M4A
"tkm", //QQ Music Accompaniment M4A
"bkcmp3", //Moo Music Mp3
"bkcflac", //Moo Music Flac
common.RegisterDecoder("qmcflac", false, DecoderFuncWithExt("flac")) //QQ Music Flac
common.RegisterDecoder("qmcogg", false, DecoderFuncWithExt("ogg")) //QQ Music Ogg
common.RegisterDecoder("tkm", false, DecoderFuncWithExt("m4a")) //QQ Music Accompaniment M4a
"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
common.RegisterDecoder("bkcmp3", false, DecoderFuncWithExt("mp3")) //Moo Music Mp3
common.RegisterDecoder("bkcflac", false, DecoderFuncWithExt("flac")) //Moo Music Flac
common.RegisterDecoder("666c6163", false, DecoderFuncWithExt("flac")) //QQ Music Weiyun Flac
common.RegisterDecoder("6d7033", false, DecoderFuncWithExt("mp3")) //QQ Music Weiyun Mp3
common.RegisterDecoder("6f6767", false, DecoderFuncWithExt("ogg")) //QQ Music Weiyun Ogg
common.RegisterDecoder("6d3461", false, DecoderFuncWithExt("m4a")) //QQ Music Weiyun M4a
common.RegisterDecoder("776176", false, DecoderFuncWithExt("wav")) //QQ Music Weiyun Wav
common.RegisterDecoder("mgg", false, NewMgg256Decoder) //QQ Music New Ogg
common.RegisterDecoder("mflac", false, NewMflac256Decoder) //QQ Music New Flac
"mgg", "mgg1", //QQ Music New Ogg
"mflac", "mflac0", //QQ Music New Flac
}
for _, ext := range supportedExts {
common.RegisterDecoder(ext, false, newCompactDecoder)
}
}
type compactDecoder struct {
decoder *Decoder
createErr error
buf *bytes.Buffer
}
func newCompactDecoder(p []byte) common.Decoder {
r := bytes.NewReader(p)
d, err := NewDecoder(r)
c := compactDecoder{
decoder: d,
createErr: err,
}
return &c
}
func (c *compactDecoder) Validate() error {
if c.createErr != nil {
return c.createErr
}
return c.decoder.Validate()
}
func (c *compactDecoder) Decode() error {
if c.createErr != nil {
return c.createErr
}
c.buf = bytes.NewBuffer(nil)
_, err := io.Copy(c.buf, c.decoder)
return err
}
func (c *compactDecoder) GetCoverImage() []byte {
return nil
}
func (c *compactDecoder) GetAudioData() []byte {
return c.buf.Bytes()
}
func (c *compactDecoder) GetAudioExt() string {
if c.createErr != nil {
return ""
}
return c.decoder.GetFileExt()
}
func (c *compactDecoder) GetMeta() common.Meta {
return nil
}

98
algo/qmc/qmc_test.go Normal file
View File

@ -0,0 +1,98 @@
package qmc
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"testing"
)
func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
encBody, err := os.ReadFile(fmt.Sprintf("./testdata/%s_raw.bin", filename))
if err != nil {
return nil, nil, err
}
encSuffix, err := os.ReadFile(fmt.Sprintf("./testdata/%s_suffix.bin", filename))
if err != nil {
return nil, nil, err
}
target, err := os.ReadFile(fmt.Sprintf("./testdata/%s_target.bin", filename))
if err != nil {
return nil, nil, err
}
return bytes.Join([][]byte{encBody, encSuffix}, nil), target, nil
}
func TestMflac0Decoder_Read(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{"mflac0_rc4", false},
{"mflac_map", false},
{"mgg_map", false},
{"qmc0_static", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
raw, target, err := loadTestDataQmcDecoder(tt.name)
if err != nil {
t.Fatal(err)
}
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 {
t.Errorf("read bytes from decoder error = %v", err)
return
}
if !reflect.DeepEqual(buf, target) {
t.Errorf("Decrypt() got = %v, want %v", buf[:32], target[:32])
}
})
}
}
func TestMflac0Decoder_Validate(t *testing.T) {
tests := []struct {
name string
fileExt string
wantErr bool
}{
{"mflac0_rc4", ".flac", false},
{"mflac_map", ".flac", false},
{"mgg_map", ".ogg", 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)
}
})
}
}

1
algo/qmc/testdata/mflac0_rc4_key.bin vendored Normal file
View File

@ -0,0 +1 @@
dRzX3p5ZYqAlp7lLSs9Zr0rw1iEZy23bB670x4ch2w97x14Zwpk1UXbKU4C2sOS7uZ0NB5QM7ve9GnSrr2JHxP74hVNONwVV77CdOOVb807317KvtI5Yd6h08d0c5W88rdV46C235YGDjUSZj5314YTzy0b6vgh4102P7E273r911Nl464XV83Hr00rkAHkk791iMGSJH95GztN28u2Nv5s9Xx38V69o4a8aIXxbx0g1EM0623OEtbtO9zsqCJfj6MhU7T8iVS6M3q19xhq6707E6r7wzPO6Yp4BwBmgg4F95Lfl0vyF7YO6699tb5LMnr7iFx29o98hoh3O3Rd8h9Juu8P1wG7vdnO5YtRlykhUluYQblNn7XwjBJ53HAyKVraWN5dG7pv7OMl1s0RykPh0p23qfYzAAMkZ1M422pEd07TA9OCKD1iybYxWH06xj6A8mzmcnYGT9P1a5Ytg2EF5LG3IknL2r3AUz99Y751au6Cr401mfAWK68WyEBe5

View File

@ -0,0 +1 @@
ZFJ6WDNwNVrjEJZB1o6QjkQV2ZbHSw/2Eb00q1+4z9SVWYyFWO1PcSQrJ5326ubLklmk2ab3AEyIKNUu8DFoAoAc9dpzpTmc+pdkBHjM/bW2jWx+dCyC8vMTHE+DHwaK14UEEGW47ZXMDi7PRCQ2Jpm/oXVdHTIlyrc+bRmKfMith0L2lFQ+nW8CCjV6ao5ydwkZhhNOmRdrCDcUXSJH9PveYwra9/wAmGKWSs9nemuMWKnbjp1PkcxNQexicirVTlLX7PVgRyFyzNyUXgu+R2S4WTmLwjd8UsOyW/dc2mEoYt+vY2lq1X4hFBtcQGOAZDeC+mxrN0EcW8tjS6P4TjOjiOKNMxIfMGSWkSKL3H7z5K7nR1AThW20H2bP/LcpsdaL0uZ/js1wFGpdIfFx9rnLC78itL0WwDleIqp9TBMX/NwakGgIPIbjBwfgyD8d8XKYuLEscIH0ZGdjsadB5XjybgdE3ppfeFEcQiqpnodlTaQRm3KDIF9ATClP0mTl8XlsSojsZ468xseS1Ib2iinx/0SkK3UtJDwp8DH3/+ELisgXd69Bf0pve7wbrQzzMUs9/Ogvvo6ULsIkQfApJ8cSegDYklzGXiLNH7hZYnXDLLSNejD7NvQouULSmGsBbGzhZ5If0NP/6AhSbpzqWLDlabTDgeWWnFeZpBnlK6SMxo+YFFk1Y0XLKsd69+jj

BIN
algo/qmc/testdata/mflac0_rc4_raw.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mflac0_rc4_suffix.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mflac0_rc4_target.bin vendored Normal file

Binary file not shown.

1
algo/qmc/testdata/mflac_map_key.bin vendored Normal file
View File

@ -0,0 +1 @@
yw7xWOyNQ8585Jwx3hjB49QLPKi38F89awnrQ0fq66NT9TDq1ppHNrFqhaDrU5AFk916D58I53h86304GqOFCCyFzBem68DqiXJ81bILEQwG3P3MOnoNzM820kNW9Lv9IJGNn9Xo497p82BLTm4hAX8JLBs0T2pilKvT429sK9jfg508GSk4d047Jxdz5Fou4aa33OkyFRBU3x430mgNBn04Lc9BzXUI2IGYXv3FGa9qE4Vb54kSjVv8ogbg47j3

View File

@ -0,0 +1 @@
eXc3eFdPeU6+3f7GVeF35bMpIEIQj5JWOWt7G+jsR68Hx3BUFBavkTQ8dpPdP0XBIwPe+OfdsnTGVQqPyg3GCtQSrkgA0mwSQdr4DPzKLkEZFX+Cf1V6ChyipOuC6KT37eAxWMdV1UHf9/OCvydr1dc6SWK1ijRUcP6IAHQhiB+mZLay7XXrSPo32WjdBkn9c9sa2SLtI48atj5kfZ4oOq6QGeld2JA3Z+3wwCe6uTHthKaEHY8ufDYodEe3qqrjYpzkdx55pCtxCQa1JiNqFmJigWm4m3CDzhuJ7YqnjbD+mXxLi7BP1+z4L6nccE2h+DGHVqpGjR9+4LBpe4WHB4DrAzVp2qQRRQJxeHd1v88=

BIN
algo/qmc/testdata/mflac_map_raw.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mflac_map_suffix.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mflac_map_target.bin vendored Normal file

Binary file not shown.

1
algo/qmc/testdata/mgg_map_key.bin vendored Normal file
View File

@ -0,0 +1 @@
zGxNk54pKJ0hDkAo80wHE80ycSWQ7z4m4E846zVy2sqCn14F42Y5S7GqeR11WpOV75sDLbE5dFP992t88l0pHy1yAQ49YK6YX6c543drBYLo55Hc4Y0Fyic6LQPiGqu2bG31r8vaq9wS9v63kg0X5VbnOD6RhO4t0RRhk3ajrA7p0iIy027z0L70LZjtw6E18H0D41nz6ASTx71otdF9z1QNC0JmCl51xvnb39zPExEXyKkV47S6QsK5hFh884QJ

1
algo/qmc/testdata/mgg_map_key_raw.bin vendored Normal file
View File

@ -0,0 +1 @@
ekd4Tms1NHC53JEDO/AKVyF+I0bj0hHB7CZeoLDGSApaQB9Oo/pJTBGA/RO+nk5RXLXdHsffLiY4e8kt3LNo6qMl7S89vkiSFxx4Uoq4bGDJ7Jc+bYL6lLsa3M4sBvXS4XcPChrMDz+LmrJMGG6ua2fYyIz1d6TCRUBf1JJgCIkBbDAEeMVYc13qApitiz/apGAPmAnveCaDhfD5GxWsF+RfQ2OcnvrnIXe80Feh/0jx763DlsOBI3eIede6t5zYHokWkZmVEF1jMrnlvsgbQK2EzUWMblmLMsTKNILyZazEoKUyulqmyLO/c/KYE+USPOXPcbjlYFmLhSGHK7sQB5aBR153Yp+xh61ooh2NGAA=

BIN
algo/qmc/testdata/mgg_map_raw.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mgg_map_suffix.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mgg_map_target.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/qmc0_static_raw.bin vendored Normal file

Binary file not shown.

View File

BIN
algo/qmc/testdata/qmc0_static_target.bin vendored Normal file

Binary file not shown.

View File

@ -3,6 +3,15 @@ package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"github.com/unlock-music/cli/algo/common"
_ "github.com/unlock-music/cli/algo/kgm"
_ "github.com/unlock-music/cli/algo/kwm"
@ -11,12 +20,6 @@ import (
_ "github.com/unlock-music/cli/algo/tm"
_ "github.com/unlock-music/cli/algo/xm"
"github.com/unlock-music/cli/internal/logging"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"os"
"path/filepath"
"runtime"
"strings"
)
var AppVersion = "v0.0.6"
@ -31,6 +34,7 @@ func main() {
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false},
&cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "path to output dir", Required: false},
&cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
&cli.BoolFlag{Name: "supported-ext", Usage: "Show supported file extensions and exit", Required: false, Value: false},
},
Action: appMain,
@ -43,8 +47,21 @@ func main() {
logging.Log().Fatal("run app failed", zap.Error(err))
}
}
func printSupportedExtensions() {
exts := []string{}
for ext := range common.DecoderRegistry {
exts = append(exts, ext)
}
sort.Strings(exts)
for _, ext := range exts {
fmt.Printf("%s: %d\n", ext, len(common.DecoderRegistry[ext]))
}
}
func appMain(c *cli.Context) (err error) {
if c.Bool("supported-ext") {
printSupportedExtensions()
return nil
}
input := c.String("input")
if input == "" {
switch c.Args().Len() {

10
go.mod
View File

@ -6,12 +6,12 @@ require (
github.com/ulikunitz/xz v0.5.10
github.com/urfave/cli/v2 v2.3.0
go.uber.org/zap v1.19.1
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
)
require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
)

14
go.sum
View File

@ -1,8 +1,9 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -13,9 +14,9 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@ -26,15 +27,18 @@ github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4=
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=