Skip to content

Commit

Permalink
Fix GUID conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Magnetto authored and Andrea Magnetto committed Jul 24, 2024
1 parent 9b84d9b commit c376f79
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func writeVarLen(w io.Writer, ti *typeInfo, out bool) (err error) {
if err = binary.Write(w, binary.LittleEndian, uint8(ti.Size)); err != nil {
return
}
ti.Writer = writeByteLenType
ti.Writer = writeGuidType
case typeBigVarBin, typeBigVarChar, typeBigBinary, typeBigChar,
typeNVarChar, typeNChar, typeXml, typeUdt:

Expand Down Expand Up @@ -455,6 +455,25 @@ func writeByteLenType(w io.Writer, ti typeInfo, buf []byte) (err error) {
return
}

func writeGuidType(w io.Writer, ti typeInfo, buf []byte) (err error) {
if !(ti.Size == 0x10 || ti.Size == 0x00) {
panic("Invalid size for BYTELEN_TYPE")
}
err = binary.Write(w, binary.LittleEndian, uint8(len(buf)))
if err != nil {
return
}
if ti.Size == 0x10 {
res := make([]byte, 0x10)
copy(res, buf)
binary.BigEndian.PutUint32(res[0:4], binary.LittleEndian.Uint32(res[0:4]))
binary.BigEndian.PutUint16(res[4:6], binary.LittleEndian.Uint16(res[4:6]))
binary.BigEndian.PutUint16(res[6:8], binary.LittleEndian.Uint16(res[6:8]))
_, err = w.Write(res)
}
return
}

func readShortLenType(ti *typeInfo, r *tdsBuffer, c *cryptoMetadata) interface{} {
var size uint16
if c != nil {
Expand Down Expand Up @@ -857,6 +876,9 @@ func decodeMoney4(buf []byte) []byte {
func decodeGuid(buf []byte) []byte {
res := make([]byte, 16)
copy(res, buf)
binary.LittleEndian.PutUint32(res[0:4], binary.BigEndian.Uint32(res[0:4]))
binary.LittleEndian.PutUint16(res[4:6], binary.BigEndian.Uint16(res[4:6]))
binary.LittleEndian.PutUint16(res[6:8], binary.BigEndian.Uint16(res[6:8]))
return res
}

Expand Down

0 comments on commit c376f79

Please sign in to comment.