// Encode writes the packet bytes into the byte slice from the argument. It
// returns the number of bytes encoded and whether there's any errors along
// the way. If there is an error, the byte slice should be considered invalid.
488462
func (cp *Connect) Encode(dst []byte) (int, error) {
	total := 0

	// encode header
	n, err := headerEncode(dst[total:], 0, cp.len(), cp.Len(), CONNECT)
	total += n
	if err != nil {
		return total, err
	}

	// set default version byte
	if cp.Version == 0 {
		cp.Version = Version311
	}

	// check version byte
	if cp.Version != Version311 && cp.Version != Version31 {
		return total, makeError(cp.Type(), "unsupported protocol version %d", cp.Version)
	}

	// write version string, length has been checked beforehand
	if cp.Version == Version311 {
		n, _ = writeLPBytes(dst[total:], version311Name, cp.Type())
		total += n
	} else if cp.Version == Version31 {
		n, _ = writeLPBytes(dst[total:], version31Name, cp.Type())
		total += n
	}

	// write version value
	dst[total] = cp.Version
	total++

	var connectFlags byte

	// set username flag
	if len(cp.Username) > 0 {
		connectFlags |= 128 // 10000000
	} else {
		connectFlags &= 127 // 01111111
	}

	// set password flag
	if len(cp.Password) > 0 {
		connectFlags |= 64 // 01000000
	} else {
		connectFlags &= 191 // 10111111
	}

	// set will flag
	if cp.Will != nil {
		connectFlags |= 0x4 // 00000100

		// check will topic length
		if len(cp.Will.Topic) == 0 {
			return total, makeError(cp.Type(), "will topic is empty")
		}

		// check will qos
		if !cp.Will.QOS.Successful() {
			return total, makeError(cp.Type(), "invalid will qos level %d", cp.Will.QOS)
		}

		// set will qos flag
		connectFlags = (connectFlags & 231) | (byte(cp.Will.QOS) << 3) // 231 = 11100111

		// set will retain flag
		if cp.Will.Retain {
			connectFlags |= 32 // 00100000
		} else {
			connectFlags &= 223 // 11011111
		}

	} else {
		connectFlags &= 251 // 11111011
	}

	// check client id and clean session
	if len(cp.ClientID) == 0 && !cp.CleanSession {
		return total, makeError(cp.Type(), "clean session must be 1 if client id is zero length")
	}

	// set clean session flag
	if cp.CleanSession {
		connectFlags |= 0x2 // 00000010
	} else {
		connectFlags &= 253 // 11111101
	}

	// write connect flags
	dst[total] = connectFlags
	total++

	// write keep alive
	binary.BigEndian.PutUint16(dst[total:], cp.KeepAlive)
	total += 2

	// write client id
	n, err = writeLPString(dst[total:], cp.ClientID, cp.Type())
	total += n
	if err != nil {
		return total, err
	}

	// write will topic and payload
	if cp.Will != nil {
		n, err = writeLPString(dst[total:], cp.Will.Topic, cp.Type())
		total += n
		if err != nil {
			return total, err
		}

		n, err = writeLPBytes(dst[total:], cp.Will.Payload, cp.Type())
		total += n
		if err != nil {
			return total, err
		}
	}

	if len(cp.Username) == 0 && len(cp.Password) > 0 {
		return total, makeError(cp.Type(), "password set without username")
	}

	// write username
	if len(cp.Username) > 0 {
		n, err = writeLPString(dst[total:], cp.Username, cp.Type())
		total += n
		if err != nil {
			return total, err
		}
	}

	// write password
	if len(cp.Password) > 0 {
		n, err = writeLPString(dst[total:], cp.Password, cp.Type())
		total += n
		if err != nil {
			return total, err
		}
	}

	return total, nil
}
569187
func Encode(src []uint64) (value uint64, n int, err error) {
	if canPack(src, 240, 0) {
		return uint64(0), 240, nil
	} else if canPack(src, 120, 0) {
		return 1 << 60, 120, nil
	} else if canPack(src, 60, 1) {
		return pack60(src[:60]), 60, nil
	} else if canPack(src, 30, 2) {
		return pack30(src[:30]), 30, nil
	} else if canPack(src, 20, 3) {
		return pack20(src[:20]), 20, nil
	} else if canPack(src, 15, 4) {
		return pack15(src[:15]), 15, nil
	} else if canPack(src, 12, 5) {
		return pack12(src[:12]), 12, nil
	} else if canPack(src, 10, 6) {
		return pack10(src[:10]), 10, nil
	} else if canPack(src, 8, 7) {
		return pack8(src[:8]), 8, nil
	} else if canPack(src, 7, 8) {
		return pack7(src[:7]), 7, nil
	} else if canPack(src, 6, 10) {
		return pack6(src[:6]), 6, nil
	} else if canPack(src, 5, 12) {
		return pack5(src[:5]), 5, nil
	} else if canPack(src, 4, 15) {
		return pack4(src[:4]), 4, nil
	} else if canPack(src, 3, 20) {
		return pack3(src[:3]), 3, nil
	} else if canPack(src, 2, 30) {
		return pack2(src[:2]), 2, nil
	} else if canPack(src, 1, 60) {
		return pack1(src[:1]), 1, nil
	} else {
		if len(src) > 0 {
			return 0, 0, fmt.Errorf("value out of bounds: %v", src)
		}
		return 0, 0, nil
	}
}
1268149
func (r *OffsetResponse) encode(pe packetEncoder) (err error) {
	if err = pe.putArrayLength(len(r.Blocks)); err != nil {
		return err
	}

	for topic, partitions := range r.Blocks {
		if err = pe.putString(topic); err != nil {
			return err
		}
		if err = pe.putArrayLength(len(partitions)); err != nil {
			return err
		}
		for partition, block := range partitions {
			pe.putInt32(partition)
			if err = block.encode(pe, r.version()); err != nil {
				return err
			}
		}
	}

	return nil
}
1327864
func (e *Encoder) Encode(msg Message) error {
	e.headersBuf.Reset()

	err := encodeHeaders(e.headersBuf, msg.Headers)
	if err != nil {
		return err
	}

	crc := crc32.New(crc32IEEETable)
	hashWriter := io.MultiWriter(e.w, crc)

	headersLen := uint32(e.headersBuf.Len())
	payloadLen := uint32(len(msg.Payload))

	if err := encodePrelude(hashWriter, crc, headersLen, payloadLen); err != nil {
		return err
	}

	if headersLen > 0 {
		if _, err := io.Copy(hashWriter, e.headersBuf); err != nil {
			return err
		}
	}

	if payloadLen > 0 {
		if _, err := hashWriter.Write(msg.Payload); err != nil {
			return err
		}
	}

	msgCRC := crc.Sum32()
	return binary.Write(e.w, binary.BigEndian, msgCRC)
}
772941
func encode(bin []byte) []byte {
	e64 := base64.StdEncoding

	maxEncLen := e64.EncodedLen(len(bin))
	encBuf := make([]byte, maxEncLen)

	e64.Encode(encBuf, bin)
	return encBuf
}
1163468
func Encode(e Encoder, obj Object) ([]byte, error) {
	// TODO: reuse buffer
	buf := &bytes.Buffer{}
	if err := e.Encode(obj, buf); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}
782489
func (b TCP) Encode(t *TCPFields) {
	b.encodeSubset(t.SeqNum, t.AckNum, t.Flags, t.WindowSize)
	binary.BigEndian.PutUint16(b[TCPSrcPortOffset:], t.SrcPort)
	binary.BigEndian.PutUint16(b[TCPDstPortOffset:], t.DstPort)
	b[TCPDataOffset] = (t.DataOffset / 4) << 4
	binary.BigEndian.PutUint16(b[TCPChecksumOffset:], t.Checksum)
	binary.BigEndian.PutUint16(b[TCPUrgentPtrOffset:], t.UrgentPointer)
}
596242
func (pp *Pingresp) Encode(dst []byte) (int, error) {
	return nakedEncode(dst, PINGRESP)
}
328542
func (encoder *IntLengthFieldBasedEncoder) Encode(data interface{}, out *ByteBuf) error {
	buf := NewByteBuf(32)

	if encoder.prepare != nil {
		err := encoder.prepare(data, out)
		if err != nil {
			return err
		}
	}

	err := encoder.base.Encode(data, buf)
	if err != nil {
		return err
	}

	out.WriteInt(buf.Readable())
	out.WriteByteBuf(buf)
	return nil
}
214429
func (p *Pong) Encode(w io.Writer, pver uint32) error {
	return WriteElements(w,
		p.PongBytes,
	)
}
863515
func (pp *Publish) Encode(dst []byte) (int, error) {
	total := 0

	// check topic length
	if len(pp.Message.Topic) == 0 {
		return total, makeError(pp.Type(), "topic name is empty")
	}

	flags := byte(0)

	// set dup flag
	if pp.Dup {
		flags |= 0x8 // 00001000
	} else {
		flags &= 247 // 11110111
	}

	// set retain flag
	if pp.Message.Retain {
		flags |= 0x1 // 00000001
	} else {
		flags &= 254 // 11111110
	}

	// check qos
	if !pp.Message.QOS.Successful() {
		return 0, makeError(pp.Type(), "invalid QOS level %d", pp.Message.QOS)
	}

	// check packet id
	if pp.Message.QOS > 0 && !pp.ID.Valid() {
		return total, makeError(pp.Type(), "packet id must be grater than zero")
	}

	// set qos
	flags = (flags & 249) | (byte(pp.Message.QOS) << 1) // 249 = 11111001

	// encode header
	n, err := headerEncode(dst[total:], flags, pp.len(), pp.Len(), PUBLISH)
	total += n
	if err != nil {
		return total, err
	}

	// write topic
	n, err = writeLPString(dst[total:], pp.Message.Topic, pp.Type())
	total += n
	if err != nil {
		return total, err
	}

	// write packet id
	if pp.Message.QOS != 0 {
		binary.BigEndian.PutUint16(dst[total:], uint16(pp.ID))
		total += 2
	}

	// write payload
	copy(dst[total:], pp.Message.Payload)
	total += len(pp.Message.Payload)

	return total, nil
}
1356759
func Encode(dst, src []byte) int {
	ret := EncodedLen(len(src))
	if len(dst) < ret {
		panic("dst has insufficient length")
	}
	dst = dst[:0]
	whichByte := uint(1)
	bufByte := byte(0)
	for _, v := range src {
		dst = append(dst, bufByte|(v>>whichByte))
		bufByte = (v&(1<<whichByte) - 1) << (7 - whichByte)
		if whichByte == 7 {
			dst = append(dst, bufByte)
			bufByte = 0
			whichByte = 0
		}
		whichByte++
	}
	dst = append(dst, bufByte)
	return ret
}
843740
func (f *CustomDownlinkFunctions) Encode(payload map[string]interface{}, port uint8) ([]byte, bool, error) {
	encoded, err := f.encode(payload, port)
	if err != nil {
		return nil, false, err
	}

	return encoded, true, nil
}
958152
func Encode(buf []byte, code uint64) ([]byte, error) {

	if !ValidCode(code) {
		return nil, ErrUnknownCode
	}

	start := make([]byte, 2*binary.MaxVarintLen64, 2*binary.MaxVarintLen64+len(buf))
	spot := start
	n := binary.PutUvarint(spot, code)
	spot = start[n:]
	n += binary.PutUvarint(spot, uint64(len(buf)))

	return append(start[:n], buf...), nil
}
1106427
func (enc *Encoding) Encode(src []byte) ([]byte, error) {
	if len(src) == 0 {
		return []byte{}, nil
	}
	n, ok := new(big.Int).SetString(string(src), 10)
	if !ok {
		return nil, fmt.Errorf("expecting a number but got %q", src)
	}
	bytes := make([]byte, 0, len(src))
	for _, c := range src {
		if c == '0' {
			bytes = append(bytes, enc.alphabet[0])
		} else {
			break
		}
	}
	zerocnt := len(bytes)
	mod := new(big.Int)
	zero := big.NewInt(0)
	for {
		switch n.Cmp(zero) {
		case 1:
			n.DivMod(n, radix, mod)
			bytes = append(bytes, enc.alphabet[mod.Int64()])
		case 0:
			reverse(bytes[zerocnt:])
			return bytes, nil
		default:
			return nil, fmt.Errorf("expecting a non-negative number in base58 encoding but got %s", n)
		}
	}
}
1233717
func Encode(dst, src []byte) int {
	for i, v := range src {
		dst[i*2] = hextable[v>>4]
		dst[i*2+1] = hextable[v&0x0f]
	}

	return len(src) * 2
}
1110714
func (c *ControlBeheraPasswordPolicy) Encode() *ber.Packet {
	packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Control")
	packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, ControlTypeBeheraPasswordPolicy, "Control Type ("+ControlTypeMap[ControlTypeBeheraPasswordPolicy]+")"))

	return packet
}