// Encode encodes the given slice of bytes to fragments including forward error correction.
// This is based on the proposed FEC code from the Fragmented Data Block Transport over
// LoRaWAN recommendation.
331777
func Encode(data []byte, fragmentSize, redundancy int) ([][]byte, error) {
	if len(data)%fragmentSize != 0 {
		return nil, errors.New("length of data must be a multiple of the given fragment-size")
	}

	// fragment the data into rows
	var dataRows [][]byte
	for i := 0; i < len(data)/fragmentSize; i++ {
		offset := i * fragmentSize
		dataRows = append(dataRows, data[offset:offset+fragmentSize])
	}
	w := len(dataRows)

	for y := 0; y < redundancy; y++ {
		s := make([]byte, fragmentSize)
		a := matrixLine(y+1, w)

		for x := 0; x < w; x++ {
			if a[x] == 1 {
				for m := 0; m < fragmentSize; m++ {
					s[m] ^= dataRows[x][m]
				}
			}
		}

		dataRows = append(dataRows, s)
	}

	return dataRows, nil
}
909884
func Encode(content string) (barcode.Barcode, error) {
	checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
	if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
		return nil, fmt.Errorf("can not encode \"%s\"", content)
	}
	resBits := new(utils.BitList)
	for i, r := range content {
		if i > 0 {
			resBits.AddBit(false)
		}
		resBits.AddBit(encodingTable[r]...)
	}
	return utils.New1DCode(barcode.TypeCodabar, content, resBits), nil
}
621200
func (enc *Encoder) Encode(pb proto.Message) error {
	buf, err := proto.Marshal(pb)
	if err != nil {
		return err
	}

	if _, err := enc.w.Write(buf); err != nil {
		return err
	}

	return nil
}
843107
func Encode(dst, src []byte) ([]byte, error) {

	if len(src) >= MaxInputSize {
		return nil, ErrTooLarge
	}

	if n := CompressBound(len(src)); len(dst) < n {
		dst = make([]byte, n)
	}

	e := encoder{src: src, dst: dst, hashTable: make([]uint32, hashTableSize)}

	binary.LittleEndian.PutUint32(dst, uint32(len(src)))
	e.dpos = 4

	var (
		step  uint32 = 1
		limit        = incompressible
	)

	for {
		if int(e.pos)+12 >= len(e.src) {
			e.writeLiterals(uint32(len(e.src))-e.anchor, 0, e.anchor)
			return e.dst[:e.dpos], nil
		}

		sequence := uint32(e.src[e.pos+3])<<24 | uint32(e.src[e.pos+2])<<16 | uint32(e.src[e.pos+1])<<8 | uint32(e.src[e.pos+0])

		hash := (sequence * 2654435761) >> hashShift
		ref := e.hashTable[hash] + uninitHash
		e.hashTable[hash] = e.pos - uninitHash

		if ((e.pos-ref)>>16) != 0 || uint32(e.src[ref+3])<<24|uint32(e.src[ref+2])<<16|uint32(e.src[ref+1])<<8|uint32(e.src[ref+0]) != sequence {
			if e.pos-e.anchor > limit {
				limit <<= 1
				step += 1 + (step >> 2)
			}
			e.pos += step
			continue
		}

		if step > 1 {
			e.hashTable[hash] = ref - uninitHash
			e.pos -= step - 1
			step = 1
			continue
		}
		limit = incompressible

		ln := e.pos - e.anchor
		back := e.pos - ref

		anchor := e.anchor

		e.pos += minMatch
		ref += minMatch
		e.anchor = e.pos

		for int(e.pos) < len(e.src)-5 && e.src[e.pos] == e.src[ref] {
			e.pos++
			ref++
		}

		mlLen := e.pos - e.anchor

		e.writeLiterals(ln, mlLen, anchor)
		e.dst[e.dpos] = uint8(back)
		e.dst[e.dpos+1] = uint8(back >> 8)
		e.dpos += 2

		if mlLen > mlMask-1 {
			mlLen -= mlMask
			for mlLen > 254 {
				mlLen -= 255

				e.dst[e.dpos] = 255
				e.dpos++
			}

			e.dst[e.dpos] = byte(mlLen)
			e.dpos++
		}

		e.anchor = e.pos
	}
}
1186353
func (g GobEncoder) Encode(v interface{}) ([]byte, error) {
	var buf bytes.Buffer
	e := gob.NewEncoder(&buf)
	err := e.Encode(v)
	return buf.Bytes(), err
}
718525
func (enc *Encoder) Encode(v interface{}) error {
	err := enc.p.marshalValue(reflect.ValueOf(v), nil, nil)
	if err != nil {
		return err
	}
	return enc.p.Flush()
}
931749
func (e *Encoder) Encode(cfg *Config) error {
	for _, s := range cfg.Sections {
		if err := e.encodeSection(s); err != nil {
			return err
		}
	}

	return 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
}
1171555
func (o *OnionErrorEncrypter) Encode(w io.Writer) error {
	_, err := w.Write(o.sharedSecret[:])
	return err
}
436391
func encode(s []byte) []byte {
	// len(s) is sufficient for UTF-8 to UTF-16 conversion if there are no
	// control code points (see table below).
	b := make([]byte, 0, len(s)+4)
	for len(s) > 0 {
		r, size := utf8.DecodeRune(s)
		if r > utf8.MaxRune {
			r, size = utf8.RuneError, 1 // Bug fix (issue 3785)
		}
		s = s[size:]
		if r1, r2 := utf16.EncodeRune(r); r1 != repl {
			b = append(b, byte(r1>>8), byte(r1))
			r = r2
		}
		b = append(b, byte(r>>8), byte(r))
	}

	// Encode as base64
	n := b64Enc.EncodedLen(len(b)) + 2
	b64 := make([]byte, n)
	b64Enc.Encode(b64[1:], b)

	// Strip padding
	n -= 2 - (len(b)+2)%3
	b64 = b64[:n]

	// Add UTF-7 shifts
	b64[0] = '&'
	b64[n-1] = '-'
	return b64
}
864944
func (codec *SyncCodec) Encode(data interface{}, out *ByteBuf) error {
	if msg, ok := data.(*notifyRaw); ok {
		out.WriteByte(cmdNotifyRaw)
		out.WriteByteBuf(msg.buf)
		releaseNotifyRaw(msg)
		return nil
	} else if msg, ok := data.(*notify); ok {
		out.WriteByte(cmdNotify)
		out.WriteUint64(msg.offset)
		releaseNotify(msg)
		return nil
	} else if msg, ok := data.(*notifySync); ok {
		out.WriteByte(cmdNotifySync)
		out.WriteUint64(msg.offset)
		releaseNotifySync(msg)
		return nil
	} else if msg, ok := data.(*notifySyncRsp); ok {
		out.WriteByte(cmdNotifySyncRsp)
		out.WriteUint64(msg.offset)
		out.WriteByte(msg.count)
		out.WriteByteBuf(msg.buf)
		releaseNotifySyncRsp(msg)
		return nil
	} else if msg, ok := data.(*notifyHB); ok {
		out.WriteByte(cmdNotifyHB)
		out.WriteUint64(msg.offset)
		return nil
	}

	return fmt.Errorf("not support msg: %v", data)
}
866235
func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) {
	d.data = data
	d.actual = nil
	d.optimised = nil

	if len(data) == 0 {
		return nil, errors.New("no data to encode")
	}

	// Classify data into unoptimised segments.
	d.classifyDataModes()

	// Optimise segments.
	err := d.optimiseDataModes()
	if err != nil {
		return nil, err
	}

	// Encode data.
	encoded := bitset.New()
	for _, s := range d.optimised {
		d.encodeDataRaw(s.data, s.dataMode, encoded)
	}

	return encoded, nil
}
1046887
func Encode(w io.Writer, v interface{}, order binary.ByteOrder) (int, error) {
	b, err := json.Marshal(v)
	if err != nil {
		return 0, err
	}
	return Write(w, bytes.NewReader(b), order)
}
655242
func (f *Flash) encode() ([]byte, error) {
	// empty flash encode to empty bytes
	if len(f.v) == 0 {
		return []byte{}, nil
	}

	buf := &bytes.Buffer{}
	err := gob.NewEncoder(buf).Encode(f.v)
	if err != nil {
		return []byte{}, err
	}
	return buf.Bytes(), nil
}
515452
func (resp *SnapshotResponse) Encode(w io.Writer) (int, error) {
	pb := &protobuf.SnapshotResponse{
		Success: proto.Bool(resp.Success),
	}
	p, err := proto.Marshal(pb)
	if err != nil {
		return -1, err
	}

	return w.Write(p)
}
1060946
func (e *snappyL2) Encode(dst *tokens, src []byte) {
	const (
		inputMargin            = 8 - 1
		minNonLiteralBlockSize = 1 + 1 + inputMargin
	)

	// Protect against e.cur wraparound.
	if e.cur > 1<<30 {
		for i := range e.table[:] {
			e.table[i] = tableEntry{}
		}
		e.cur = maxStoreBlockSize
	}

	// This check isn't in the Snappy implementation, but there, the caller
	// instead of the callee handles this case.
	if len(src) < minNonLiteralBlockSize {
		// We do not fill the token table.
		// This will be picked up by caller.
		dst.n = uint16(len(src))
		e.cur += maxStoreBlockSize
		e.prev = e.prev[:0]
		return
	}

	// sLimit is when to stop looking for offset/length copies. The inputMargin
	// lets us use a fast path for emitLiteral in the main loop, while we are
	// looking for copies.
	sLimit := int32(len(src) - inputMargin)

	// nextEmit is where in src the next emitLiteral should start from.
	nextEmit := int32(0)
	s := int32(0)
	cv := load3232(src, s)
	nextHash := hash(cv)

	for {
		// Copied from the C++ snappy implementation:
		//
		// Heuristic match skipping: If 32 bytes are scanned with no matches
		// found, start looking only at every other byte. If 32 more bytes are
		// scanned (or skipped), look at every third byte, etc.. When a match
		// is found, immediately go back to looking at every byte. This is a
		// small loss (~5% performance, ~0.1% density) for compressible data
		// due to more bookkeeping, but for non-compressible data (such as
		// JPEG) it's a huge win since the compressor quickly "realizes" the
		// data is incompressible and doesn't bother looking for matches
		// everywhere.
		//
		// The "skip" variable keeps track of how many bytes there are since
		// the last match; dividing it by 32 (ie. right-shifting by five) gives
		// the number of bytes to move ahead for each iteration.
		skip := int32(32)

		nextS := s
		var candidate tableEntry
		for {
			s = nextS
			bytesBetweenHashLookups := skip >> 5
			nextS = s + bytesBetweenHashLookups
			skip += bytesBetweenHashLookups
			if nextS > sLimit {
				goto emitRemainder
			}
			candidate = e.table[nextHash&tableMask]
			now := load3232(src, nextS)
			e.table[nextHash&tableMask] = tableEntry{offset: s + e.cur, val: cv}
			nextHash = hash(now)

			offset := s - (candidate.offset - e.cur)
			if offset > maxMatchOffset || cv != candidate.val {
				// Out of range or not matched.
				cv = now
				continue
			}
			break
		}

		// A 4-byte match has been found. We'll later see if more than 4 bytes
		// match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
		// them as literal bytes.
		emitLiteral(dst, src[nextEmit:s])

		// Call emitCopy, and then see if another emitCopy could be our next
		// move. Repeat until we find no match for the input immediately after
		// what was consumed by the last emitCopy call.
		//
		// If we exit this loop normally then we need to call emitLiteral next,
		// though we don't yet know how big the literal will be. We handle that
		// by proceeding to the next iteration of the main loop. We also can
		// exit this loop via goto if we get close to exhausting the input.
		for {
			// Invariant: we have a 4-byte match at s, and no need to emit any
			// literal bytes prior to s.

			// Extend the 4-byte match as long as possible.
			//
			s += 4
			t := candidate.offset - e.cur + 4
			l := e.matchlen(s, t, src)

			// matchToken is flate's equivalent of Snappy's emitCopy. (length,offset)
			dst.tokens[dst.n] = matchToken(uint32(l+4-baseMatchLength), uint32(s-t-baseMatchOffset))
			dst.n++
			s += l
			nextEmit = s
			if s >= sLimit {
				t += l
				// Index first pair after match end.
				if int(t+4) < len(src) && t > 0 {
					cv := load3232(src, t)
					e.table[hash(cv)&tableMask] = tableEntry{offset: t + e.cur, val: cv}
				}
				goto emitRemainder
			}

			// We could immediately start working at s now, but to improve
			// compression we first update the hash table at s-1 and at s. If
			// another emitCopy is not our next move, also calculate nextHash
			// at s+1. At least on GOARCH=amd64, these three hash calculations
			// are faster as one load64 call (with some shifts) instead of
			// three load32 calls.
			x := load6432(src, s-1)
			prevHash := hash(uint32(x))
			e.table[prevHash&tableMask] = tableEntry{offset: e.cur + s - 1, val: uint32(x)}
			x >>= 8
			currHash := hash(uint32(x))
			candidate = e.table[currHash&tableMask]
			e.table[currHash&tableMask] = tableEntry{offset: e.cur + s, val: uint32(x)}

			offset := s - (candidate.offset - e.cur)
			if offset > maxMatchOffset || uint32(x) != candidate.val {
				cv = uint32(x >> 8)
				nextHash = hash(cv)
				s++
				break
			}
		}
	}

emitRemainder:
	if int(nextEmit) < len(src) {
		emitLiteral(dst, src[nextEmit:])
	}
	e.cur += int32(len(src))
	e.prev = e.prev[:len(src)]
	copy(e.prev, src)
}
1045788
func (resp *AppendEntriesResponse) Encode(w io.Writer) (int, error) {
	b, err := proto.Marshal(resp.pb)
	if err != nil {
		return -1, err
	}

	return w.Write(b)
}