From 719bddcd76610a63dadc8555760072957a11cf30 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 10 Dec 2021 09:27:50 +0800 Subject: Move repository model into models/repo (#17933) * Some refactors related repository model * Move more methods out of repository * Move repository into models/repo * Fix test * Fix test * some improvements * Remove unnecessary function --- modules/json/json.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'modules/json') diff --git a/modules/json/json.go b/modules/json/json.go index be42b6ae6c..1cbb658261 100644 --- a/modules/json/json.go +++ b/modules/json/json.go @@ -6,6 +6,7 @@ package json import ( "bytes" + "encoding/binary" "encoding/json" "io" @@ -140,3 +141,32 @@ func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { func Valid(data []byte) bool { return json.Valid(data) } + +// UnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's +// possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe. +func UnmarshalHandleDoubleEncode(bs []byte, v interface{}) error { + err := json.Unmarshal(bs, v) + if err != nil { + ok := true + rs := []byte{} + temp := make([]byte, 2) + for _, rn := range string(bs) { + if rn > 0xffff { + ok = false + break + } + binary.LittleEndian.PutUint16(temp, uint16(rn)) + rs = append(rs, temp...) + } + if ok { + if len(rs) > 1 && rs[0] == 0xff && rs[1] == 0xfe { + rs = rs[2:] + } + err = json.Unmarshal(rs, v) + } + } + if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe { + err = json.Unmarshal(bs[2:], v) + } + return err +} -- cgit v1.2.3