Merge pull request '小更改 - mmkv 相关' (#93) from qmc-mmkv-cipher into master
Reviewed-on: https://git.unlock-music.dev/um/cli/pulls/93pull/95/head v0.2.0-beta.3
commit
05a1affa03
@ -1,65 +1,93 @@
|
|||||||
package qmc
|
package qmc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
bytes "bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type qqMusicTagMusicEx struct {
|
type MusicExTagV1 struct {
|
||||||
songid uint32 // Song ID
|
SongID uint32 // Song ID
|
||||||
unknown_1 uint32 // unused & unknown
|
Unknown1 uint32 // unused & unknown
|
||||||
unknown_2 uint32 // unused & unknown
|
Unknown2 uint32 // unused & unknown
|
||||||
mid string // Media ID
|
MediaID string // Media ID
|
||||||
mediafile string // real file name
|
MediaFileName string // real file name
|
||||||
unknown_3 uint32 // unused; uninitialized memory?
|
Unknown3 uint32 // unused; uninitialized memory?
|
||||||
sizeof_struct uint32 // 19.57: fixed value: 0xC0
|
|
||||||
version uint32 // 19.57: fixed value: 0x01
|
// 16 byte at the end of tag.
|
||||||
tag_magic []byte // fixed value "musicex\0" (8 bytes)
|
// TagSize should be respected when parsing.
|
||||||
|
TagSize uint32 // 19.57: fixed value: 0xC0
|
||||||
|
TagVersion uint32 // 19.57: fixed value: 0x01
|
||||||
|
TagMagic []byte // fixed value "musicex\0" (8 bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tag *qqMusicTagMusicEx) Read(raw io.ReadSeeker) (int64, error) {
|
func NewMusicExTag(f io.ReadSeeker) (*MusicExTagV1, error) {
|
||||||
_, err := raw.Seek(-16, io.SeekEnd)
|
_, err := f.Seek(-16, io.SeekEnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("musicex seek error: %w", err)
|
return nil, fmt.Errorf("musicex seek error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
footerBuf := make([]byte, 4)
|
buffer := make([]byte, 16)
|
||||||
footerBuf, err = io.ReadAll(io.LimitReader(raw, 4))
|
bytesRead, err := f.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("get musicex error: %w", err)
|
return nil, fmt.Errorf("get musicex error: %w", err)
|
||||||
|
}
|
||||||
|
if bytesRead != 16 {
|
||||||
|
return nil, fmt.Errorf("MusicExV1: read %d bytes (expected %d)", bytesRead, 16)
|
||||||
}
|
}
|
||||||
footerLen := int64(binary.LittleEndian.Uint32(footerBuf))
|
|
||||||
|
|
||||||
audioLen, err := raw.Seek(-footerLen, io.SeekEnd)
|
tag := &MusicExTagV1{
|
||||||
buf, err := io.ReadAll(io.LimitReader(raw, audioLen))
|
TagSize: binary.LittleEndian.Uint32(buffer[0x00:0x04]),
|
||||||
if err != nil {
|
TagVersion: binary.LittleEndian.Uint32(buffer[0x04:0x08]),
|
||||||
return 0, err
|
TagMagic: buffer[0x04:0x0C],
|
||||||
}
|
}
|
||||||
|
|
||||||
tag.songid = binary.LittleEndian.Uint32(buf[0:4])
|
if !bytes.Equal(tag.TagMagic, []byte("musicex\x00")) {
|
||||||
tag.unknown_1 = binary.LittleEndian.Uint32(buf[4:8])
|
return nil, errors.New("MusicEx magic mismatch")
|
||||||
tag.unknown_2 = binary.LittleEndian.Uint32(buf[8:12])
|
}
|
||||||
|
if tag.TagVersion != 1 {
|
||||||
|
return nil, errors.New(fmt.Sprintf("unsupported musicex tag version. expecting 1, got %d", tag.TagVersion))
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < 30; i++ {
|
if tag.TagSize < 0xC0 {
|
||||||
u := binary.LittleEndian.Uint16(buf[12+i*2 : 12+(i+1)*2])
|
return nil, errors.New(fmt.Sprintf("unsupported musicex tag size. expecting at least 0xC0, got 0x%02x", tag.TagSize))
|
||||||
if u == 0 {
|
}
|
||||||
break
|
|
||||||
|
buffer = make([]byte, tag.TagSize)
|
||||||
|
bytesRead, err = f.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
tag.mid += string(u)
|
if uint32(bytesRead) != tag.TagSize {
|
||||||
|
return nil, fmt.Errorf("MusicExV1: read %d bytes (expected %d)", bytesRead, tag.TagSize)
|
||||||
}
|
}
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
u := binary.LittleEndian.Uint16(buf[72+i*2 : 72+(i+1)*2])
|
tag.SongID = binary.LittleEndian.Uint32(buffer[0x00:0x04])
|
||||||
if u == 0 {
|
tag.Unknown1 = binary.LittleEndian.Uint32(buffer[0x04:0x08])
|
||||||
|
tag.Unknown2 = binary.LittleEndian.Uint32(buffer[0x08:0x0C])
|
||||||
|
tag.MediaID = readUnicodeTagName(buffer[0x0C:], 30*2)
|
||||||
|
tag.MediaFileName = readUnicodeTagName(buffer[0x48:], 50*2)
|
||||||
|
tag.Unknown3 = binary.LittleEndian.Uint32(buffer[0xAC:0xB0])
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readUnicodeTagName reads a buffer to maxLen.
|
||||||
|
// reconstruct text by skipping alternate char (ascii chars encoded in UTF-16-LE),
|
||||||
|
// until finding a zero or reaching maxLen.
|
||||||
|
func readUnicodeTagName(buffer []byte, maxLen int) string {
|
||||||
|
builder := strings.Builder{}
|
||||||
|
|
||||||
|
for i := 0; i < maxLen; i += 2 {
|
||||||
|
chr := buffer[i]
|
||||||
|
if chr != 0 {
|
||||||
|
builder.WriteByte(chr)
|
||||||
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
tag.mediafile += string(u)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tag.unknown_3 = binary.LittleEndian.Uint32(buf[173:177])
|
return builder.String()
|
||||||
tag.sizeof_struct = binary.LittleEndian.Uint32(buf[177:181])
|
|
||||||
tag.version = binary.LittleEndian.Uint32(buf[181:185])
|
|
||||||
tag.tag_magic = buf[185:193]
|
|
||||||
|
|
||||||
return audioLen, nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue