From 28d84e4dc2bdd151c8cab9f5e1f41e8939a120e4 Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Tue, 3 Aug 2021 04:17:10 +0800 Subject: [PATCH] Fix: algorithm bugs in `qmc` Optimize: algorithm in `kgm` --- algo/kgm/kgm.go | 8 ++------ algo/kgm/mask.go | 4 ++-- algo/qmc/mask_key256.go | 4 ++-- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/algo/kgm/kgm.go b/algo/kgm/kgm.go index 9a039cb..0a72f86 100644 --- a/algo/kgm/kgm.go +++ b/algo/kgm/kgm.go @@ -75,12 +75,8 @@ func (d *Decoder) Decode() error { d.audio = make([]byte, lenData) for i := 0; i < lenData; i++ { - med8 := d.key[i%17] ^ dataEncrypted[i] - med8 ^= (med8 & 0xf) << 4 - - msk8 := maskV2PreDef[i%272] ^ maskV2[i>>4] - msk8 ^= (msk8 & 0xf) << 4 - d.audio[i] = med8 ^ msk8 + med8 := dataEncrypted[i] ^ d.key[i%17] ^ maskV2PreDef[i%(16*17)] ^ maskV2[i>>4] + d.audio[i] = med8 ^ (med8&0xf)<<4 } if d.isVpr { for i := 0; i < lenData; i++ { diff --git a/algo/kgm/mask.go b/algo/kgm/mask.go index 9ac2abf..dc9fa62 100644 --- a/algo/kgm/mask.go +++ b/algo/kgm/mask.go @@ -6,7 +6,7 @@ import ( "github.com/ulikunitz/xz" "github.com/unlock-music/cli/internal/logging" "go.uber.org/zap" - "io/ioutil" + "io" ) var maskDiffVpr = []byte{ @@ -50,7 +50,7 @@ func initMask() { if err != nil { logging.Log().Fatal("load kgm mask failed", zap.Error(err)) } - maskV2, err = ioutil.ReadAll(maskReader) + maskV2, err = io.ReadAll(maskReader) if err != nil { logging.Log().Fatal("load kgm mask failed", zap.Error(err)) } diff --git a/algo/qmc/mask_key256.go b/algo/qmc/mask_key256.go index d9491f4..b31f33d 100644 --- a/algo/qmc/mask_key256.go +++ b/algo/qmc/mask_key256.go @@ -39,7 +39,7 @@ func NewKey256FromMask44(mask44 []byte) (*Key256Mask, error) { return q, nil } -func (q *Key256Mask) getMatrix44() (mask44 []byte, err error) { +func (q *Key256Mask) GetMatrix44() ([]byte, error) { if len(q.matrix) != 128 { return nil, ErrMaskLength128 } @@ -53,7 +53,7 @@ func (q *Key256Mask) getMatrix44() (mask44 []byte, err error) { return nil, ErrMaskDecode } } - q.matrix[idx44] = q.matrix[it256[0]] + matrix44[idx44] = q.matrix[it256[0]] idx44++ } }