From d658c7c6b08312725b081f0346fca979ca2910b0 Mon Sep 17 00:00:00 2001 From: Aelita Styles Date: Fri, 4 Jan 2019 08:38:34 +0000 Subject: [PATCH] Fix UUID being read/written incorrectly --- binary/v1/types.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/binary/v1/types.go b/binary/v1/types.go index 30e3cb9..5ee5ceb 100644 --- a/binary/v1/types.go +++ b/binary/v1/types.go @@ -122,6 +122,18 @@ func ToTime(t time.Time) Time { return Time(t3) } +// Flips a UUID buffer into the right order +func uuidFlip(id *uuid.UUID) { + for i := 3; i >= 0; i-- { + opp := 7-i + id[i], id[opp] = id[opp], id[i] + } + for i := 3; i >= 0; i-- { + opp := 15-i + id[i+8], id[opp] = id[opp], id[i+8] + } +} + // WriteType writes object type code func WriteType(w io.Writer, code byte) error { return WriteByte(w, code) @@ -250,6 +262,7 @@ func WriteOUUID(w io.Writer, v uuid.UUID) error { if err := WriteType(w, typeUUID); err != nil { return err } + uuidFlip(&v) return binary.Write(w, binary.LittleEndian, v) } @@ -714,6 +727,7 @@ func ReadOString(r io.Reader) (string, error) { func ReadUUID(r io.Reader) (uuid.UUID, error) { var o uuid.UUID err := binary.Read(r, binary.LittleEndian, &o) + uuidFlip(&o) return o, err }