refactor: code cleaning

This commit is contained in:
Unlock Music Dev
2022-11-20 02:47:28 +08:00
parent 8e068b9c8d
commit f6748d644d
7 changed files with 41 additions and 30 deletions

View File

@ -13,14 +13,15 @@ import (
)
type Decoder struct {
raw io.ReadSeeker
audio io.Reader
offset int
audioLen int
raw io.ReadSeeker // raw is the original file reader
cipher common.StreamDecoder
audio io.Reader // audio is the encrypted audio data
audioLen int // audioLen is the audio data length
offset int // offset is the current audio read position
decodedKey []byte // decodedKey is the decoded key for cipher
cipher common.StreamDecoder
decodedKey []byte
rawMetaExtra1 int
rawMetaExtra2 int
}
@ -100,23 +101,29 @@ func (d *Decoder) searchKey() error {
if err != nil {
return err
}
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
if err != nil {
suffixBuf := make([]byte, 4)
if _, err := io.ReadFull(d.raw, suffixBuf); err != nil {
return err
}
if string(buf) == "QTag" {
switch string(suffixBuf) {
case "QTag":
return d.readRawMetaQTag()
} else if string(buf) == "STag" {
case "STag":
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
} else {
size := binary.LittleEndian.Uint32(buf)
default:
size := binary.LittleEndian.Uint32(suffixBuf)
if size <= 0xFFFF && size != 0 { // assume size is key len
return d.readRawKey(int64(size))
}
// try to use default static cipher
d.audioLen = int(fileSizeM4 + 4)
return nil
}
}
func (d *Decoder) readRawKey(rawKeyLen int64) error {