1
0
forked from um/cli

fix: seek to audio start after validate

This commit is contained in:
Unlock Music Dev 2022-11-19 07:25:44 +08:00
parent 67ff0c44cd
commit 2880c63bf7
Signed by untrusted user: um-dev
GPG Key ID: 95202E10D3413A1D

View File

@ -19,25 +19,25 @@ func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{rd: rd} return &Decoder{rd: rd}
} }
var kgmCryptoInitializers = map[uint32]func(header *header) (common.StreamDecoder, error){ func (d *Decoder) Validate() (err error) {
3: newKgmCryptoV3,
}
func (d *Decoder) Validate() error {
if err := d.header.FromFile(d.rd); err != nil { if err := d.header.FromFile(d.rd); err != nil {
return err return err
} }
// TODO; validate crypto version // TODO; validate crypto version
initializer, ok := kgmCryptoInitializers[d.header.CryptoVersion] switch d.header.CryptoVersion {
if !ok { case 3:
d.cipher, err = newKgmCryptoV3(&d.header)
if err != nil {
return fmt.Errorf("kgm init crypto v3: %w", err)
}
default:
return fmt.Errorf("kgm: unsupported crypto version %d", d.header.CryptoVersion) return fmt.Errorf("kgm: unsupported crypto version %d", d.header.CryptoVersion)
} }
var err error // prepare for read
d.cipher, err = initializer(&d.header) if _, err := d.rd.Seek(int64(d.header.AudioOffset), io.SeekStart); err != nil {
if err != nil { return fmt.Errorf("kgm seek to audio: %w", err)
return fmt.Errorf("kgm: failed to initialize crypto: %w", err)
} }
return nil return nil