1
0
forked from um/cli

refactor: use io.Reader instead of custom method

This commit is contained in:
Unlock Music Dev 2022-11-19 07:25:43 +08:00
parent 4365628bff
commit 67ff0c44cd
Signed by untrusted user: um-dev
GPG Key ID: 95202E10D3413A1D
17 changed files with 420 additions and 460 deletions

View File

@ -1,13 +1,13 @@
package common
import "context"
import (
"context"
"io"
)
type Decoder interface {
Validate() error
Decode() error
GetAudioData() []byte
GetAudioExt() string
GetMeta() Meta
io.Reader
}
type CoverImageGetter interface {
@ -19,3 +19,7 @@ type Meta interface {
GetTitle() string
GetAlbum() string
}
type StreamDecoder interface {
Decrypt(buf []byte, offset int)
}

View File

@ -1,11 +1,12 @@
package common
import (
"io"
"path/filepath"
"strings"
)
type NewDecoderFunc func([]byte) Decoder
type NewDecoderFunc func(rd io.ReadSeeker) Decoder
type decoderItem struct {
noop bool

View File

@ -2,21 +2,32 @@ package common
import (
"errors"
"fmt"
"io"
"strings"
)
type RawDecoder struct {
file []byte
rd io.ReadSeeker
audioExt string
}
func NewRawDecoder(file []byte) Decoder {
return &RawDecoder{file: file}
func NewRawDecoder(rd io.ReadSeeker) Decoder {
return &RawDecoder{rd: rd}
}
func (d *RawDecoder) Validate() error {
header := make([]byte, 16)
if _, err := io.ReadFull(d.rd, header); err != nil {
return fmt.Errorf("read file header failed: %v", err)
}
if _, err := d.rd.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("seek file failed: %v", err)
}
for ext, sniffer := range snifferRegistry {
if sniffer(d.file) {
if sniffer(header) {
d.audioExt = strings.ToLower(ext)
return nil
}
@ -24,20 +35,8 @@ func (d *RawDecoder) Validate() error {
return errors.New("audio doesn't recognized")
}
func (d RawDecoder) Decode() error {
return nil
}
func (d RawDecoder) GetAudioData() []byte {
return d.file
}
func (d RawDecoder) GetAudioExt() string {
return d.audioExt
}
func (d RawDecoder) GetMeta() Meta {
return nil
func (d *RawDecoder) Read(p []byte) (n int, err error) {
return d.rd.Read(p)
}
func init() {

View File

@ -1,7 +1,6 @@
package kgm
import (
"bytes"
"fmt"
"io"
@ -9,66 +8,50 @@ import (
)
type Decoder struct {
header Header
initializer kgmCryptoInitializer
header header
cipher common.StreamDecoder
file []byte
audio []byte
rd io.ReadSeeker
offset int
}
type kgmCryptoInitializer func(header *Header, body io.Reader) (io.Reader, error)
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{rd: rd}
}
var kgmCryptoInitializers = map[uint32]kgmCryptoInitializer{
var kgmCryptoInitializers = map[uint32]func(header *header) (common.StreamDecoder, error){
3: newKgmCryptoV3,
}
func NewDecoder(file []byte) common.Decoder {
return &Decoder{
file: file,
}
}
func (d *Decoder) GetAudioData() []byte {
return d.audio
}
func (d *Decoder) GetAudioExt() string {
return "" // use sniffer
}
func (d *Decoder) GetMeta() common.Meta {
return nil
}
func (d *Decoder) Validate() error {
if err := d.header.FromBytes(d.file); err != nil {
if err := d.header.FromFile(d.rd); err != nil {
return err
}
// TODO; validate crypto version
var ok bool
d.initializer, ok = kgmCryptoInitializers[d.header.CryptoVersion]
initializer, ok := kgmCryptoInitializers[d.header.CryptoVersion]
if !ok {
return fmt.Errorf("kgm: unsupported crypto version %d", d.header.CryptoVersion)
}
return nil
}
func (d *Decoder) Decode() error {
d.audio = d.file[d.header.AudioOffset:]
r, err := d.initializer(&d.header, bytes.NewReader(d.audio))
var err error
d.cipher, err = initializer(&d.header)
if err != nil {
return fmt.Errorf("kgm: failed to initialize crypto: %w", err)
}
d.audio, err = io.ReadAll(r)
if err != nil {
return fmt.Errorf("kgm: failed to decrypt audio: %w", err)
}
return nil
}
func (d *Decoder) Read(buf []byte) (int, error) {
n, err := d.rd.Read(buf)
if n > 0 {
d.cipher.Decrypt(buf[:n], d.offset)
d.offset += n
}
return n, err
}
func init() {
// Kugou
common.RegisterDecoder("kgm", false, NewDecoder)

View File

@ -21,8 +21,8 @@ var (
ErrKgmMagicHeader = errors.New("kgm magic header not matched")
)
// Header is the header of a KGM file.
type Header struct {
// header is the header of a KGM file.
type header struct {
MagicHeader []byte // 0x00-0x0f: magic header
AudioOffset uint32 // 0x10-0x13: offset of audio data
CryptoVersion uint32 // 0x14-0x17: crypto version
@ -31,7 +31,7 @@ type Header struct {
CryptoKey []byte // 0x2c-0x3b: crypto key
}
func (h *Header) FromFile(rd io.ReadSeeker) error {
func (h *header) FromFile(rd io.ReadSeeker) error {
if _, err := rd.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("kgm seek start: %w", err)
}
@ -44,7 +44,7 @@ func (h *Header) FromFile(rd io.ReadSeeker) error {
return h.FromBytes(buf)
}
func (h *Header) FromBytes(buf []byte) error {
func (h *header) FromBytes(buf []byte) error {
if len(buf) < 0x3c {
return errors.New("invalid kgm header length")
}

View File

@ -3,24 +3,22 @@ package kgm
import (
"crypto/md5"
"fmt"
"io"
"github.com/unlock-music/cli/algo/common"
)
// kgmCryptoV3 is kgm file crypto v3
type kgmCryptoV3 struct {
slotBox []byte
fileBox []byte
rd io.Reader
offset int
}
var kgmV3Slot2Key = map[uint32][]byte{
1: {0x6C, 0x2C, 0x2F, 0x27},
}
func newKgmCryptoV3(header *Header, body io.Reader) (io.Reader, error) {
c := &kgmCryptoV3{rd: body}
func newKgmCryptoV3(header *header) (common.StreamDecoder, error) {
c := &kgmCryptoV3{}
slotKey, ok := kgmV3Slot2Key[header.CryptoSlot]
if !ok {
@ -33,16 +31,7 @@ func newKgmCryptoV3(header *Header, body io.Reader) (io.Reader, error) {
return c, nil
}
func (d *kgmCryptoV3) Read(buf []byte) (int, error) {
n, err := d.rd.Read(buf)
if n > 0 {
d.decrypt(buf[:n], d.offset)
d.offset += n
}
return n, err
}
func (d *kgmCryptoV3) decrypt(b []byte, offset int) {
func (d *kgmCryptoV3) Decrypt(b []byte, offset int) {
for i := 0; i < len(b); i++ {
b[i] ^= d.fileBox[(offset+i)%len(d.fileBox)]
b[i] ^= b[i] << 4

View File

@ -2,8 +2,9 @@ package kwm
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"strings"
"unicode"
@ -11,97 +12,65 @@ import (
"github.com/unlock-music/cli/algo/common"
)
var (
magicHeader = []byte{
0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D,
0x6B, 0x75, 0x77, 0x6F, 0x2D, 0x74, 0x6D, 0x65}
ErrKwFileSize = errors.New("kwm invalid file size")
ErrKwMagicHeader = errors.New("kwm magic header not matched")
)
const magicHeader = "yeelion-kuwo-tme"
const keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
type Decoder struct {
file []byte
cipher common.StreamDecoder
rd io.ReadSeeker
offset int
key []byte
outputExt string
bitrate int
mask []byte
audio []byte
}
func (d *Decoder) GetAudioData() []byte {
return d.audio
}
func (d *Decoder) GetAudioExt() string {
return "." + d.outputExt
}
func (d *Decoder) GetMeta() common.Meta {
return nil
}
func NewDecoder(data []byte) common.Decoder {
//todo: Notice the input data will be changed for now
return &Decoder{file: data}
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{rd: rd}
}
func (d *Decoder) Validate() error {
lenData := len(d.file)
if lenData < 1024 {
return ErrKwFileSize
}
if !bytes.Equal(magicHeader, d.file[:16]) {
return ErrKwMagicHeader
}
return nil
}
func generateMask(key []byte) []byte {
keyInt := binary.LittleEndian.Uint64(key)
keyStr := strconv.FormatUint(keyInt, 10)
keyStrTrim := padOrTruncate(keyStr, 32)
mask := make([]byte, 32)
for i := 0; i < 32; i++ {
mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
}
return mask
}
func (d *Decoder) parseBitrateAndType() {
bitType := string(bytes.TrimRight(d.file[0x30:0x38], string(byte(0))))
charPos := 0
for charPos = range bitType {
if !unicode.IsNumber(rune(bitType[charPos])) {
break
}
}
var err error
d.bitrate, err = strconv.Atoi(bitType[:charPos])
header := make([]byte, 0x400) // kwm header is fixed to 1024 bytes
_, err := io.ReadFull(d.rd, header)
if err != nil {
d.bitrate = 0
}
d.outputExt = strings.ToLower(bitType[charPos:])
return fmt.Errorf("kwm read header: %w", err)
}
func (d *Decoder) Decode() error {
d.parseBitrateAndType()
d.mask = generateMask(d.file[0x18:0x20])
d.audio = d.file[1024:]
dataLen := len(d.audio)
for i := 0; i < dataLen; i++ {
d.audio[i] ^= d.mask[i&0x1F] //equals: [i % 32]
// check magic header, 0x00 - 0x0F
if !bytes.Equal([]byte(magicHeader), header[:len(magicHeader)]) {
return errors.New("kwm magic header not matched")
}
d.cipher = newKwmCipher(header[0x18:0x20]) // Crypto Key, 0x18 - 0x1F
d.bitrate, d.outputExt = parseBitrateAndType(header[0x30:0x38]) // Bitrate & File Extension, 0x30 - 0x38
return nil
}
func parseBitrateAndType(header []byte) (int, string) {
tmp := strings.TrimRight(string(header), "\x00")
sep := strings.IndexFunc(tmp, func(r rune) bool {
return !unicode.IsDigit(r)
})
bitrate, _ := strconv.Atoi(tmp[:sep]) // just ignore the error
outputExt := strings.ToLower(tmp[sep:])
return bitrate, outputExt
}
func (d *Decoder) Read(b []byte) (int, error) {
n, err := d.rd.Read(b)
if n > 0 {
d.cipher.Decrypt(b[:n], d.offset)
d.offset += n
}
return n, err
}
func padOrTruncate(raw string, length int) string {
lenRaw := len(raw)
out := raw

31
algo/kwm/kwm_cipher.go Normal file
View File

@ -0,0 +1,31 @@
package kwm
import (
"encoding/binary"
"strconv"
)
type kwmCipher struct {
mask []byte
}
func newKwmCipher(key []byte) *kwmCipher {
return &kwmCipher{mask: generateMask(key)}
}
func generateMask(key []byte) []byte {
keyInt := binary.LittleEndian.Uint64(key)
keyStr := strconv.FormatUint(keyInt, 10)
keyStrTrim := padOrTruncate(keyStr, 32)
mask := make([]byte, 32)
for i := 0; i < 32; i++ {
mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
}
return mask
}
func (c kwmCipher) Decrypt(buf []byte, offset int) {
for i := range buf {
buf[i] ^= c.mask[(offset+i)&0x1F] // equivalent: [i % 32]
}
}

View File

@ -16,27 +16,30 @@ import (
"github.com/unlock-music/cli/internal/utils"
)
const magicHeader = "CTENFDAM"
var (
magicHeader = []byte{
0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D}
keyCore = []byte{
0x68, 0x7a, 0x48, 0x52, 0x41, 0x6d, 0x73, 0x6f,
0x35, 0x6b, 0x49, 0x6e, 0x62, 0x61, 0x78, 0x57}
0x35, 0x6b, 0x49, 0x6e, 0x62, 0x61, 0x78, 0x57,
}
keyMeta = []byte{
0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21,
0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28}
0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28,
}
)
func NewDecoder(data []byte) common.Decoder {
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{
file: data,
fileLen: uint32(len(data)),
rd: rd,
}
}
type Decoder struct {
file []byte
fileLen uint32
rd io.ReadSeeker
offset int
cipher common.StreamDecoder
key []byte
box []byte
@ -47,91 +50,128 @@ type Decoder struct {
cover []byte
audio []byte
offsetKey uint32
offsetMeta uint32
offsetCover uint32
offsetAudio uint32
}
func (d *Decoder) Validate() error {
if !bytes.Equal(magicHeader, d.file[:len(magicHeader)]) {
if err := d.validateMagicHeader(); err != nil {
return err
}
if _, err := d.rd.Seek(2, io.SeekCurrent); err != nil { // 2 bytes gap
return fmt.Errorf("ncm seek file: %w", err)
}
keyData, err := d.readKeyData()
if err != nil {
return err
}
if err := d.readMetaData(); err != nil {
return fmt.Errorf("read meta date failed: %w", err)
}
if _, err := d.rd.Seek(5, io.SeekCurrent); err != nil { // 5 bytes gap
return fmt.Errorf("ncm seek gap: %w", err)
}
if err := d.readCoverData(); err != nil {
return fmt.Errorf("parse ncm cover file failed: %w", err)
}
if err := d.parseMeta(); err != nil {
return fmt.Errorf("parse meta failed: %w", err)
}
d.cipher = newNcmCipher(keyData)
return nil
}
func (d *Decoder) validateMagicHeader() error {
header := make([]byte, len(magicHeader)) // 0x00 - 0x07
if _, err := d.rd.Read(header); err != nil {
return fmt.Errorf("ncm read magic header: %w", err)
}
if !bytes.Equal([]byte(magicHeader), header) {
return errors.New("ncm magic header not match")
}
d.offsetKey = 8 + 2
return nil
}
func (d *Decoder) readKeyData() error {
if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
return errors.New("invalid cover file offset")
func (d *Decoder) readKeyData() ([]byte, error) {
bKeyLen := make([]byte, 4) //
if _, err := io.ReadFull(d.rd, bKeyLen); err != nil {
return nil, fmt.Errorf("ncm read key length: %w", err)
}
bKeyLen := d.file[d.offsetKey : d.offsetKey+4]
iKeyLen := binary.LittleEndian.Uint32(bKeyLen)
d.offsetMeta = d.offsetKey + 4 + iKeyLen
bKeyRaw := make([]byte, iKeyLen)
if _, err := io.ReadFull(d.rd, bKeyRaw); err != nil {
return nil, fmt.Errorf("ncm read key data: %w", err)
}
for i := uint32(0); i < iKeyLen; i++ {
bKeyRaw[i] = d.file[i+4+d.offsetKey] ^ 0x64
bKeyRaw[i] ^= 0x64
}
d.key = utils.PKCS7UnPadding(utils.DecryptAes128Ecb(bKeyRaw, keyCore))[17:]
return nil
return utils.PKCS7UnPadding(utils.DecryptAES128ECB(bKeyRaw, keyCore))[17:], nil
}
func (d *Decoder) readMetaData() error {
if d.offsetMeta == 0 || d.offsetMeta+4 > d.fileLen {
return errors.New("invalid meta file offset")
bMetaLen := make([]byte, 4) //
if _, err := io.ReadFull(d.rd, bMetaLen); err != nil {
return fmt.Errorf("ncm read key length: %w", err)
}
bMetaLen := d.file[d.offsetMeta : d.offsetMeta+4]
iMetaLen := binary.LittleEndian.Uint32(bMetaLen)
d.offsetCover = d.offsetMeta + 4 + iMetaLen
if iMetaLen == 0 {
return errors.New("no any meta file found")
return nil // no meta data
}
// Why sub 22: Remove "163 key(Don't modify):"
bKeyRaw := make([]byte, iMetaLen-22)
for i := uint32(0); i < iMetaLen-22; i++ {
bKeyRaw[i] = d.file[d.offsetMeta+4+22+i] ^ 0x63
bMetaRaw := make([]byte, iMetaLen)
if _, err := io.ReadFull(d.rd, bMetaRaw); err != nil {
return fmt.Errorf("ncm read meta data: %w", err)
}
bMetaRaw = bMetaRaw[22:] // skip "163 key(Don't modify):"
for i := 0; i < len(bMetaRaw); i++ {
bMetaRaw[i] ^= 0x63
}
cipherText, err := base64.StdEncoding.DecodeString(string(bKeyRaw))
cipherText, err := base64.StdEncoding.DecodeString(string(bMetaRaw))
if err != nil {
return errors.New("decode ncm meta failed: " + err.Error())
}
metaRaw := utils.PKCS7UnPadding(utils.DecryptAes128Ecb(cipherText, keyMeta))
sepIdx := bytes.IndexRune(metaRaw, ':')
if sepIdx == -1 {
metaRaw := utils.PKCS7UnPadding(utils.DecryptAES128ECB(cipherText, keyMeta))
sep := bytes.IndexByte(metaRaw, ':')
if sep == -1 {
return errors.New("invalid ncm meta file")
}
d.metaType = string(metaRaw[:sepIdx])
d.metaRaw = metaRaw[sepIdx+1:]
d.metaType = string(metaRaw[:sep])
d.metaRaw = metaRaw[sep+1:]
return nil
}
func (d *Decoder) buildKeyBox() {
box := make([]byte, 256)
for i := 0; i < 256; i++ {
box[i] = byte(i)
func (d *Decoder) readCoverData() error {
bCoverCRC := make([]byte, 4)
if _, err := io.ReadFull(d.rd, bCoverCRC); err != nil {
return fmt.Errorf("ncm read cover crc: %w", err)
}
keyLen := len(d.key)
var j byte
for i := 0; i < 256; i++ {
j = box[i] + j + d.key[i%keyLen]
box[i], box[j] = box[j], box[i]
bCoverLen := make([]byte, 4) //
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
return fmt.Errorf("ncm read cover length: %w", err)
}
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
d.box = make([]byte, 256)
var _i byte
for i := 0; i < 256; i++ {
_i = byte(i + 1)
si := box[_i]
sj := box[_i+si]
d.box[i] = box[si+sj]
coverBuf := make([]byte, iCoverLen)
if _, err := io.ReadFull(d.rd, coverBuf); err != nil {
return fmt.Errorf("ncm read cover data: %w", err)
}
d.cover = coverBuf
return nil
}
func (d *Decoder) parseMeta() error {
@ -147,54 +187,13 @@ func (d *Decoder) parseMeta() error {
}
}
func (d *Decoder) readCoverData() error {
if d.offsetCover == 0 || d.offsetCover+13 > d.fileLen {
return errors.New("invalid cover file offset")
func (d *Decoder) Read(buf []byte) (int, error) {
n, err := d.rd.Read(buf)
if n > 0 {
d.cipher.Decrypt(buf[:n], d.offset)
d.offset += n
}
coverLenStart := d.offsetCover + 5 + 4
bCoverLen := d.file[coverLenStart : coverLenStart+4]
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
d.offsetAudio = coverLenStart + 4 + iCoverLen
if iCoverLen == 0 {
return nil
}
d.cover = d.file[coverLenStart+4 : coverLenStart+4+iCoverLen]
return nil
}
func (d *Decoder) readAudioData() error {
if d.offsetAudio == 0 || d.offsetAudio > d.fileLen {
return errors.New("invalid audio offset")
}
audioRaw := d.file[d.offsetAudio:]
audioLen := len(audioRaw)
d.audio = make([]byte, audioLen)
for i := uint32(0); i < uint32(audioLen); i++ {
d.audio[i] = d.box[i&0xff] ^ audioRaw[i]
}
return nil
}
func (d *Decoder) Decode() error {
if err := d.readKeyData(); err != nil {
return fmt.Errorf("read key data failed: %w", err)
}
d.buildKeyBox()
if err := d.readMetaData(); err != nil {
return fmt.Errorf("read meta date failed: %w", err)
}
if err := d.parseMeta(); err != nil {
return fmt.Errorf("parse meta failed: %w", err)
}
if err := d.readCoverData(); err != nil {
return fmt.Errorf("parse ncm cover file failed: %w", err)
}
return d.readAudioData()
return n, err
}
func (d *Decoder) GetAudioExt() string {
@ -206,10 +205,6 @@ func (d *Decoder) GetAudioExt() string {
return ""
}
func (d *Decoder) GetAudioData() []byte {
return d.audio
}
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
if d.cover != nil {
return d.cover, nil

42
algo/ncm/ncm_cipher.go Normal file
View File

@ -0,0 +1,42 @@
package ncm
type ncmCipher struct {
key []byte
box []byte
}
func newNcmCipher(key []byte) *ncmCipher {
return &ncmCipher{
key: key,
box: buildKeyBox(key),
}
}
func (c *ncmCipher) Decrypt(buf []byte, offset int) {
for i := 0; i < len(buf); i++ {
buf[i] ^= c.box[(i+offset)&0xff]
}
}
func buildKeyBox(key []byte) []byte {
box := make([]byte, 256)
for i := 0; i < 256; i++ {
box[i] = byte(i)
}
var j byte
for i := 0; i < 256; i++ {
j = box[i] + j + key[i%len(key)]
box[i], box[j] = box[j], box[i]
}
ret := make([]byte, 256)
var _i byte
for i := 0; i < 256; i++ {
_i = byte(i + 1)
si := box[_i]
sj := box[_i+si]
ret[i] = box[si+sj]
}
return ret
}

View File

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

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"strings"
@ -12,14 +13,14 @@ import (
)
type Decoder struct {
r io.ReadSeeker
fileExt string
audioLen int
decodedKey []byte
cipher streamCipher
raw io.ReadSeeker
audio io.Reader
offset int
audioLen int
cipher common.StreamDecoder
decodedKey []byte
rawMetaExtra1 int
rawMetaExtra2 int
}
@ -27,78 +28,79 @@ type Decoder struct {
// 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 {
return 0, io.EOF
} else if d.audioLen-d.offset < n {
n = d.audioLen - d.offset
n, err := d.audio.Read(p)
if n > 0 {
d.cipher.Decrypt(p[:n], d.offset)
d.offset += n
}
m, err := d.r.Read(p[:n])
if m > 0 {
d.cipher.Decrypt(p[:m], d.offset)
d.offset += m
}
return m, err
return n, err
}
func NewDecoder(r io.ReadSeeker) (*Decoder, error) {
d := &Decoder{r: r}
func NewDecoder(r io.ReadSeeker) common.Decoder {
return &Decoder{raw: r}
}
func (d *Decoder) Validate() error {
// search & derive key
err := d.searchKey()
if err != nil {
return nil, err
return err
}
// check cipher type and init decode cipher
if len(d.decodedKey) > 300 {
d.cipher, err = newRC4Cipher(d.decodedKey)
if err != nil {
return nil, err
return err
}
} else if len(d.decodedKey) != 0 {
d.cipher, err = newMapCipher(d.decodedKey)
if err != nil {
return nil, err
return 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 {
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 {
// test with first 16 bytes
if err := d.validateDecode(); err != nil {
return err
}
d.cipher.Decrypt(buf, 0)
fileExt, ok := common.SniffAll(buf)
if !ok {
return errors.New("detect file type failed")
// reset position, limit to audio, prepare for Read
if _, err := d.raw.Seek(0, io.SeekStart); err != nil {
return err
}
d.fileExt = fileExt
d.audio = io.LimitReader(d.raw, int64(d.audioLen))
return nil
}
func (d *Decoder) GetFileExt() string {
return d.fileExt
func (d *Decoder) validateDecode() error {
_, err := d.raw.Seek(0, io.SeekStart)
if err != nil {
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)
}
d.cipher.Decrypt(buf, 0)
_, ok := common.SniffAll(buf)
if !ok {
return errors.New("qmc: detect file type failed")
}
return nil
}
func (d *Decoder) searchKey() error {
fileSizeM4, err := d.r.Seek(-4, io.SeekEnd)
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
if err != nil {
return err
}
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
if err != nil {
return err
}
@ -118,13 +120,13 @@ func (d *Decoder) searchKey() error {
}
func (d *Decoder) readRawKey(rawKeyLen int64) error {
audioLen, err := d.r.Seek(-(4 + rawKeyLen), io.SeekEnd)
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.r, rawKeyLen))
rawKeyData, err := io.ReadAll(io.LimitReader(d.raw, rawKeyLen))
if err != nil {
return err
}
@ -142,22 +144,22 @@ func (d *Decoder) readRawKey(rawKeyLen int64) error {
func (d *Decoder) readRawMetaQTag() error {
// get raw meta data len
if _, err := d.r.Seek(-8, io.SeekEnd); err != nil {
if _, err := d.raw.Seek(-8, io.SeekEnd); err != nil {
return err
}
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
buf, err := io.ReadAll(io.LimitReader(d.raw, 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)
audioLen, err := d.raw.Seek(-(8 + rawMetaLen), io.SeekEnd)
if err != nil {
return err
}
d.audioLen = int(audioLen)
rawMetaData, err := io.ReadAll(io.LimitReader(d.r, rawMetaLen))
rawMetaData, err := io.ReadAll(io.LimitReader(d.raw, rawMetaLen))
if err != nil {
return err
}
@ -206,53 +208,6 @@ func init() {
"mflac", "mflac0", //QQ Music New Flac
}
for _, ext := range supportedExts {
common.RegisterDecoder(ext, false, newCompactDecoder)
common.RegisterDecoder(ext, false, NewDecoder)
}
}
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) 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
}

View File

@ -3,6 +3,8 @@ package tm
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/unlock-music/cli/algo/common"
)
@ -11,66 +13,38 @@ var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
var magicHeader = []byte{0x51, 0x51, 0x4D, 0x55} //0x15, 0x1D, 0x1A, 0x21
type Decoder struct {
file []byte
audio []byte
headerMatch bool
audioExt string
}
func (d *Decoder) GetAudioData() []byte {
return d.audio
}
func (d *Decoder) GetAudioExt() string {
if d.audioExt != "" {
return "." + d.audioExt
}
return ""
}
func (d *Decoder) GetMeta() common.Meta {
return nil
raw io.ReadSeeker
offset int
audio io.Reader
}
func (d *Decoder) Validate() error {
if len(d.file) < 8 {
return errors.New("invalid file size")
header := make([]byte, 8)
if _, err := io.ReadFull(d.raw, header); err != nil {
return fmt.Errorf("tm read header: %w", err)
}
if !bytes.Equal(magicHeader, d.file[:4]) {
return errors.New("not a valid tm file")
if !bytes.Equal(magicHeader, header[:len(magicHeader)]) {
return errors.New("tm: valid magic header")
}
d.headerMatch = true
d.audio = io.MultiReader(bytes.NewReader(replaceHeader), d.raw)
return nil
}
func (d *Decoder) Decode() error {
d.audio = d.file
if d.headerMatch {
for i := 0; i < 8; i++ {
d.audio[i] = replaceHeader[i]
}
d.audioExt = "m4a"
}
return nil
func (d *Decoder) Read(buf []byte) (int, error) {
return d.audio.Read(buf)
}
//goland:noinspection GoUnusedExportedFunction
func NewDecoder(data []byte) common.Decoder {
return &Decoder{file: data}
}
func NewTmDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{raw: rd}
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
return func(file []byte) common.Decoder {
return &Decoder{file: file, audioExt: ext}
}
}
func init() {
// QQ Music IOS M4a
common.RegisterDecoder("tm2", false, DecoderFuncWithExt("m4a"))
common.RegisterDecoder("tm6", false, DecoderFuncWithExt("m4a"))
common.RegisterDecoder("tm2", false, NewTmDecoder)
common.RegisterDecoder("tm6", false, NewTmDecoder)
// QQ Music IOS Mp3
common.RegisterDecoder("tm0", false, common.NewRawDecoder)
common.RegisterDecoder("tm3", false, common.NewRawDecoder)
}

View File

@ -3,6 +3,8 @@ package xm
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/unlock-music/cli/algo/common"
)
@ -16,22 +18,19 @@ var (
" MP3": "mp3",
" A4M": "m4a",
}
ErrFileSize = errors.New("xm invalid file size")
ErrMagicHeader = errors.New("xm magic header not matched")
)
type Decoder struct {
file []byte
headerLen uint32
rd io.ReadSeeker
offset int
cipher common.StreamDecoder
outputExt string
mask byte
audio []byte
}
func (d *Decoder) GetAudioData() []byte {
return d.audio
}
func (d *Decoder) GetAudioExt() string {
if d.outputExt != "" {
return "." + d.outputExt
@ -40,59 +39,53 @@ func (d *Decoder) GetAudioExt() string {
return ""
}
func (d *Decoder) GetMeta() common.Meta {
return nil
}
func NewDecoder(data []byte) common.Decoder {
return &Decoder{file: data}
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{rd: rd}
}
func (d *Decoder) Validate() error {
lenData := len(d.file)
if lenData < 16 {
return ErrFileSize
header := make([]byte, 16) // xm header is fixed to 16 bytes
if _, err := io.ReadFull(d.rd, header); err != nil {
return fmt.Errorf("xm read header: %w", err)
}
if !bytes.Equal(magicHeader, d.file[:4]) ||
!bytes.Equal(magicHeader2, d.file[8:12]) {
// 0x00 - 0x03 and 0x08 - 0x0B: magic header
if !bytes.Equal(magicHeader, header[:4]) || !bytes.Equal(magicHeader2, header[8:12]) {
return ErrMagicHeader
}
// 0x04 - 0x07: Audio File Type
var ok bool
d.outputExt, ok = typeMapping[string(d.file[4:8])]
d.outputExt, ok = typeMapping[string(header[4:8])]
if !ok {
return errors.New("detect unknown xm file type: " + string(d.file[4:8]))
return fmt.Errorf("xm detect unknown audio type: %s", string(header[4:8]))
}
d.headerLen = uint32(d.file[12]) | uint32(d.file[13])<<8 | uint32(d.file[14])<<16 // LittleEndian Unit24
if d.headerLen+16 > uint32(lenData) {
return ErrFileSize
}
// 0x0C - 0x0E, Encrypt Start At, LittleEndian Unit24
encStartAt := uint32(header[12]) | uint32(header[13])<<8 | uint32(header[14])<<16
// 0x0F, XOR Mask
d.cipher = newXmCipher(header[15], int(encStartAt))
return nil
}
func (d *Decoder) Decode() error {
d.mask = d.file[15]
d.audio = d.file[16:]
dataLen := uint32(len(d.audio))
for i := d.headerLen; i < dataLen; i++ {
d.audio[i] = ^(d.audio[i] - d.mask)
}
return nil
}
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
return func(file []byte) common.Decoder {
return &Decoder{file: file, outputExt: ext}
func (d *Decoder) Read(p []byte) (int, error) {
n, err := d.rd.Read(p)
if n > 0 {
d.cipher.Decrypt(p[:n], d.offset)
d.offset += n
}
return n, err
}
func init() {
// Xiami Wav/M4a/Mp3/Flac
common.RegisterDecoder("xm", false, NewDecoder)
// Xiami Typed Format
common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav"))
common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3"))
common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac"))
common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a"))
common.RegisterDecoder("wav", false, NewDecoder)
common.RegisterDecoder("mp3", false, NewDecoder)
common.RegisterDecoder("flac", false, NewDecoder)
common.RegisterDecoder("m4a", false, NewDecoder)
}

21
algo/xm/xm_cipher.go Normal file
View File

@ -0,0 +1,21 @@
package xm
type xmCipher struct {
mask byte
encryptStartAt int
}
func newXmCipher(mask byte, encryptStartAt int) *xmCipher {
return &xmCipher{
mask: mask,
encryptStartAt: encryptStartAt,
}
}
func (c *xmCipher) Decrypt(buf []byte, offset int) {
for i := 0; i < len(buf); i++ {
if offset+i >= c.encryptStartAt {
buf[i] ^= c.mask
}
}
}

View File

@ -1,8 +1,10 @@
package main
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
@ -153,10 +155,11 @@ func dealDirectory(inputDir string, outputDir string, skipNoop bool, removeSourc
}
func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFunc, removeSource bool) error {
file, err := os.ReadFile(inputFile)
file, err := os.Open(inputFile)
if err != nil {
return err
}
defer file.Close()
var dec common.Decoder
for _, decFunc := range allDec {
@ -171,26 +174,32 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
if dec == nil {
return errors.New("no any decoder can resolve the file")
}
if err := dec.Decode(); err != nil {
return errors.New("failed while decoding: " + err.Error())
header := bytes.NewBuffer(nil)
_, err = io.CopyN(header, dec, 16)
if err != nil {
return fmt.Errorf("read header failed: %w", err)
}
outData := dec.GetAudioData()
outExt := dec.GetAudioExt()
if outExt == "" {
if ext, ok := common.SniffAll(outData); ok {
outExt := ".mp3"
if ext, ok := common.SniffAll(header.Bytes()); ok {
outExt = ext
} else {
outExt = ".mp3"
}
}
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
outPath := filepath.Join(outputDir, filenameOnly+outExt)
err = os.WriteFile(outPath, outData, 0644)
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer outFile.Close()
if _, err := io.Copy(outFile, header); err != nil {
return err
}
if _, err := io.Copy(outFile, dec); err != nil {
return err
}
// if source file need to be removed
if removeSource {

View File

@ -8,7 +8,7 @@ func PKCS7UnPadding(encrypt []byte) []byte {
return encrypt[:(length - unPadding)]
}
func DecryptAes128Ecb(data, key []byte) []byte {
func DecryptAES128ECB(data, key []byte) []byte {
cipher, _ := aes.NewCipher(key)
decrypted := make([]byte, len(data))
size := 16