// WriteVarInt serializes val to w using a variable number of bytes depending
// on its value.
764663
func WriteVarInt(w io.Writer, pver uint32, val uint64) error {
	if val < 0xfd {
		return binarySerializer.PutUint8(w, uint8(val))
	}

	if val <= math.MaxUint16 {
		err := binarySerializer.PutUint8(w, 0xfd)
		if err != nil {
			return err
		}
		return binarySerializer.PutUint16(w, littleEndian, uint16(val))
	}

	if val <= math.MaxUint32 {
		err := binarySerializer.PutUint8(w, 0xfe)
		if err != nil {
			return err
		}
		return binarySerializer.PutUint32(w, littleEndian, uint32(val))
	}

	err := binarySerializer.PutUint8(w, 0xff)
	if err != nil {
		return err
	}
	return binarySerializer.PutUint64(w, littleEndian, val)
}
1106706
func writeInt(w enhancedWriter, i int64) (int, error) {
	bp := numBufPool.Get().(*[]byte)
	*bp = strconv.AppendInt((*bp)[:0], i, 10)
	written, err := w.Write(*bp)
	numBufPool.Put(bp)
	return written, err
}
556818
func (w *ByteWriter) WriteVal(val interface{}, offset int) (int, error) {
	if s, isString := val.(string); isString {
		return w.WriteString(s, offset)
	}

	buf := bytes.NewBuffer(make([]byte, 0))

	err := binary.Write(buf, byteOrder, val)
	if err != nil {
		return 0, err
	}

	return w.Write(buf.Bytes(), offset)
}
1227676
func (w *ByteWriter) WriteInt64(val int64, offset int) (int, error) {
	return w.WriteVal(val, offset)
}
105576
func (w *countingWriter) Write(p []byte) (n int, err error) {
	n, err = w.w.Write(p)
	w.count += uint(n)
	return n, err
}
680602
func (variant *Variant) Write(w io.Writer) error {
	if err := newPstring(variant.Name).Write(w); err != nil {
		return err
	}
	for _, v := range variant.InitialParamValues {
		if err := binary.Write(w, byteOrder, v); err != nil {
			return err
		}
	}
	return nil
}
860905
func (w *Writer) Write(data []byte) (n int, err error) {
	return w.d.write(data)
}
116441
func (stream *Stream) WriteInt16(nval int16) {
	var val uint16
	if nval < 0 {
		val = uint16(-nval)
		stream.buf = append(stream.buf, '-')
	} else {
		val = uint16(nval)
	}
	stream.WriteUint16(val)
}
119645
func (vb *keyWriter) WriteInt64(num int64) (int, error) {
	return vb.WriteUint64(uint64(num))
}
771671
func (f *framer) writeInt(n int32) {
	f.wbuf = appendInt(f.wbuf, n)
}
1096459
func (enc *Encoder) WriteInt32(val int32) error {
	return enc.WriteInt(int(val))
}
609814
func (nw *NullIO) Write(v []byte) (int, error) {
	return len(v), nil
}
268647
func (w *Writer) WriteInt64(num int64) {
	w.b = AppendInt(w.b, num)
}
883042
func (w *ByteWriter) WriteInt32(val int32, offset int) (int, error) {
	return w.WriteVal(val, offset)
}
281783
func NewVarintWriter(w io.Writer) WriteCloser {
	return &varintWriter{
		W:    w,
		lbuf: make([]byte, binary.MaxVarintLen64),
		lock: new(sync.Mutex),
	}
}
92059
func writeVarLen(w io.Writer, ti *typeInfo) (err error) {
	switch ti.TypeId {

	case typeDateN:
		ti.Writer = writeByteLenType
	case typeTimeN, typeDateTime2N, typeDateTimeOffsetN:
		if err = binary.Write(w, binary.LittleEndian, ti.Scale); err != nil {
			return
		}
		ti.Writer = writeByteLenType
	case typeIntN, typeDecimal, typeNumeric,
		typeBitN, typeDecimalN, typeNumericN, typeFltN,
		typeMoneyN, typeDateTimeN, typeChar,
		typeVarChar, typeBinary, typeVarBinary:

		// byle len types
		if ti.Size > 0xff {
			panic("Invalid size for BYLELEN_TYPE")
		}
		if err = binary.Write(w, binary.LittleEndian, uint8(ti.Size)); err != nil {
			return
		}
		switch ti.TypeId {
		case typeDecimal, typeNumeric, typeDecimalN, typeNumericN:
			err = binary.Write(w, binary.LittleEndian, ti.Prec)
			if err != nil {
				return
			}
			err = binary.Write(w, binary.LittleEndian, ti.Scale)
			if err != nil {
				return
			}
		}
		ti.Writer = writeByteLenType
	case typeGuid:
		if !(ti.Size == 0x10 || ti.Size == 0x00) {
			panic("Invalid size for BYLELEN_TYPE")
		}
		if err = binary.Write(w, binary.LittleEndian, uint8(ti.Size)); err != nil {
			return
		}
		ti.Writer = writeByteLenType
	case typeBigVarBin, typeBigVarChar, typeBigBinary, typeBigChar,
		typeNVarChar, typeNChar, typeXml, typeUdt:

		// short len types
		if ti.Size > 8000 || ti.Size == 0 {
			if err = binary.Write(w, binary.LittleEndian, uint16(0xffff)); err != nil {
				return
			}
			ti.Writer = writePLPType
		} else {
			if err = binary.Write(w, binary.LittleEndian, uint16(ti.Size)); err != nil {
				return
			}
			ti.Writer = writeShortLenType
		}
		switch ti.TypeId {
		case typeBigVarChar, typeBigChar, typeNVarChar, typeNChar:
			if err = writeCollation(w, ti.Collation); err != nil {
				return
			}
		case typeXml:
			if err = binary.Write(w, binary.LittleEndian, ti.XmlInfo.SchemaPresent); err != nil {
				return
			}
		}
	case typeText, typeImage, typeNText, typeVariant:
		// LONGLEN_TYPE
		if err = binary.Write(w, binary.LittleEndian, uint32(ti.Size)); err != nil {
			return
		}
		if err = writeCollation(w, ti.Collation); err != nil {
			return
		}
		ti.Writer = writeLongLenType
	default:
		panic("Invalid type")
	}
	return
}
1089045
func (b *buf) Varint() (c uint64, bits uint) {
	for i := 0; i < len(b.data); i++ {
		byte := b.data[i]
		c |= uint64(byte&0x7F) << bits
		bits += 7
		if byte&0x80 == 0 {
			b.off += dwarf.Offset(i + 1)
			b.data = b.data[i+1:]
			return c, bits
		}
	}
	return 0, 0
}