2022-11-19 23:53:36 +00:00
|
|
|
package ximalaya
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"unlock-music.dev/cli/algo/common"
|
2022-11-21 22:16:40 +00:00
|
|
|
"unlock-music.dev/cli/internal/sniff"
|
2022-11-19 23:53:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Decoder struct {
|
|
|
|
rd io.ReadSeeker
|
|
|
|
offset int
|
|
|
|
|
|
|
|
audio io.Reader
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:05:38 +00:00
|
|
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
|
|
|
return &Decoder{rd: p.Reader}
|
2022-11-19 23:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Decoder) Validate() error {
|
|
|
|
encryptedHeader := make([]byte, x2mHeaderSize)
|
|
|
|
if _, err := io.ReadFull(d.rd, encryptedHeader); err != nil {
|
|
|
|
return fmt.Errorf("ximalaya read header: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // try to decode with x2m
|
|
|
|
header := decryptX2MHeader(encryptedHeader)
|
2022-11-21 22:16:40 +00:00
|
|
|
if _, ok := sniff.AudioExtension(header); ok {
|
2022-11-19 23:53:36 +00:00
|
|
|
d.audio = io.MultiReader(bytes.NewReader(header), d.rd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // try to decode with x3m
|
|
|
|
// not read file again, since x2m and x3m have the same header size
|
|
|
|
header := decryptX3MHeader(encryptedHeader)
|
2022-11-21 22:16:40 +00:00
|
|
|
if _, ok := sniff.AudioExtension(header); ok {
|
2022-11-19 23:53:36 +00:00
|
|
|
d.audio = io.MultiReader(bytes.NewReader(header), d.rd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("ximalaya: unknown format")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Decoder) Read(p []byte) (n int, err error) {
|
|
|
|
return d.audio.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.RegisterDecoder("x2m", false, NewDecoder)
|
|
|
|
common.RegisterDecoder("x3m", false, NewDecoder)
|
|
|
|
common.RegisterDecoder("xm", false, NewDecoder)
|
|
|
|
}
|