package navicat import ( "encoding/binary" "fmt" ) func putU32(n uint32) []byte { b := make([]byte, 4) binary.BigEndian.PutUint32(b, n) return b } func putU16(n uint16) []byte { b := make([]byte, 2) binary.BigEndian.PutUint16(b, n) return b } // AppendBlock encodes GetBlock() from ntunnel_mysql.php. // NULL is not represented here; use AppendNull for row cells. func AppendBlock(dst []byte, val []byte) []byte { if val == nil { val = []byte{} } n := len(val) if n < 254 { dst = append(dst, byte(n)) return append(dst, val...) } dst = append(dst, markLong) dst = append(dst, putU32(uint32(n))...) return append(dst, val...) } func AppendNull(dst []byte) []byte { return append(dst, markNull) } func AppendHeader(dst []byte, version uint16, errno uint32) []byte { dst = append(dst, putU32(Magic)...) dst = append(dst, putU16(version)...) dst = append(dst, putU32(errno)...) dst = append(dst, 0, 0, 0, 0, 0, 0) return dst } func AppendResultSetHeader(dst []byte, errno, affected, insertID, numFields, numRows uint32) []byte { dst = append(dst, putU32(errno)...) dst = append(dst, putU32(affected)...) dst = append(dst, putU32(insertID)...) dst = append(dst, putU32(numFields)...) dst = append(dst, putU32(numRows)...) dst = append(dst, make([]byte, 12)...) return dst } func AppendField(dst []byte, name, table string, typ, flags, length uint32) []byte { dst = AppendBlock(dst, []byte(name)) dst = AppendBlock(dst, []byte(table)) dst = append(dst, putU32(typ)...) dst = append(dst, putU32(flags)...) dst = append(dst, putU32(length)...) return dst } func knownVersion(v uint16) bool { return v == Version202 || v == Version206 } func fmtErrno(errno uint32, msg string) string { if msg == "" { return fmt.Sprintf("tunnel errno %d", errno) } return fmt.Sprintf("tunnel errno %d: %s", errno, msg) }