diff options
Diffstat (limited to 'modules/json/json.go')
-rw-r--r-- | modules/json/json.go | 30 |
1 files changed, 30 insertions, 0 deletions
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 +} |