From 3a59fc8e538b66dc34cf8f1007a98f63be0c9cb3 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Thu, 8 Nov 2018 20:46:10 +0100 Subject: [PATCH] Reorganize mysql encoding functions --- mysql/{encoding.go => binary.go} | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) rename mysql/{encoding.go => binary.go} (89%) diff --git a/mysql/encoding.go b/mysql/binary.go similarity index 89% rename from mysql/encoding.go rename to mysql/binary.go index db48286..22fb2c2 100644 --- a/mysql/encoding.go +++ b/mysql/binary.go @@ -43,7 +43,7 @@ func EncodeUint24(data []byte, v uint32) { // DecodeUint24 decodes 3 bytes as uint32 value from a given slice of bytes. func DecodeUint24(data []byte) uint32 { - return uint32(decodeVarLen64(data, 3)) + return uint32(DecodeVarLen64(data, 3)) } // int<4> @@ -68,7 +68,7 @@ func EncodeUint48(data []byte, v uint64) { // DecodeUint48 decodes 6 bytes as uint64 value from a given slice of bytes. func DecodeUint48(data []byte) uint64 { - return decodeVarLen64(data, 6) + return DecodeVarLen64(data, 6) } // int<8> @@ -140,11 +140,11 @@ func DecodeUintLenEnc(data []byte) (v uint64, isNull bool, size int) { case 0xFB: return 0xFB, true, 1 case 0xFC: - return decodeVarLen64(data[1:], 2), false, 3 + return DecodeVarLen64(data[1:], 2), false, 3 case 0xFD: - return decodeVarLen64(data[1:], 3), false, 4 + return DecodeVarLen64(data[1:], 3), false, 4 case 0xFE: - return decodeVarLen64(data[1:], 8), false, 9 + return DecodeVarLen64(data[1:], 8), false, 9 default: return uint64(data[0]), false, 1 } @@ -160,7 +160,8 @@ func encodeVarLen64(data []byte, v uint64, s int) { } } -func decodeVarLen64(data []byte, s int) uint64 { +// DecodeVarLen64 decodes a number of given size in bytes using Little Endian. +func DecodeVarLen64(data []byte, s int) uint64 { v := uint64(data[0]) for i := 1; i < s; i++ { v |= uint64(data[i]) << uint(i*8) @@ -168,6 +169,15 @@ func decodeVarLen64(data []byte, s int) uint64 { return v } +// DecodeVarLen64BigEndian decodes a number of given size in bytes using Big Endian. +func DecodeVarLen64BigEndian(data []byte) uint64 { + var num uint64 + for i, b := range data { + num |= uint64(b) << (uint(len(data)-i-1) * 8) + } + return num +} + // Protocol::NulTerminatedString // Strings that are terminated by a 0x00 byte. // Spec: https://dev.mysql.com/doc/internals/en/string.html @@ -232,3 +242,11 @@ func DecodeStringEOF(data []byte) []byte { copy(s, data) return s } + +// DecodeBit decodes a bit into not less than 8 bytes. +func DecodeBit(data []byte, nbits int, length int) uint64 { + if nbits > 1 { + return DecodeVarLen64(data, length) + } + return uint64(data[0]) +}