// decodedLen returns the length of the decoded block and the number of bytes
// that the length header occupied.
5265
func decodedLen(src []byte) (blockLen, headerLen int, err error) {
	v, n := binary.Uvarint(src)
	if n <= 0 || v > 0xffffffff {
		return 0, 0, ErrCorrupt
	}

	const wordSize = 32 << (^uint(0) >> 32 & 1)
	if wordSize == 32 && v > 0x7fffffff {
		return 0, 0, ErrTooLarge
	}
	return int(v), n, nil
}
51978
func decode(in convert.ReadSeekCloser) (*decoded, int, int, error) {
	r, err := oggvorbis.NewReader(in)
	if err != nil {
		return nil, 0, 0, err
	}
	d := &decoded{
		data:       make([]float32, r.Length()*2),
		totalBytes: int(r.Length()) * 4, // TODO: What if length is 0?
		posInBytes: 0,
		source:     in,
		decoder:    r,
	}
	runtime.SetFinalizer(d, (*decoded).Close)
	if _, err := d.Read(make([]uint8, 65536)); err != nil {
		return nil, 0, 0, err
	}
	if _, err := d.Seek(0, io.SeekStart); err != nil {
		return nil, 0, 0, err
	}
	return d, r.Channels(), r.SampleRate(), nil
}
350753
func (enc *Encoding) DecodedLen(n int) int {

	// Fast path!
	if n == enc.baseXBlockLen {
		return enc.base256BlockLen
	}

	nblocks := n / enc.baseXBlockLen
	out := nblocks * enc.base256BlockLen
	rem := n % enc.baseXBlockLen
	if rem > 0 {
		out += int(math.Floor(float64(rem) * enc.logOfBase / float64(8)))
	}
	return out
}
1270234
func decode(data *C.pn_data_t, bytes []byte) (int, error) {
	n := C.pn_data_decode(data, cPtr(bytes), cLen(bytes))
	if n == C.PN_UNDERFLOW {
		C.pn_error_clear(C.pn_data_error(data))
		return 0, EndOfData
	} else if n <= 0 {
		return 0, &UnmarshalError{s: fmt.Sprintf("unmarshal %v", PnErrorCode(n))}
	}
	return int(n), nil
}
903339
func decode(token string) ([]byte, error) {
	decoded, err := hex.DecodeString(token)
	if err != nil {
		return nil, trace.Wrap(err)
	}

	if len(decoded) != tokenLenBytes {
		return nil, trace.BadParameter("invalid CSRF token byte length, expected %v, got %v", tokenLenBytes, len(decoded))
	}

	return decoded, nil
}
52326
func DecodeFrame(r io.Reader) (frame []byte, err error) {
	var size int16
	err = binary.Read(r, binary.LittleEndian, &size)
	if err != nil {
		return
	}

	if size < 0 {
		return nil, ErrNegativeFrameSize
	}

	frame = make([]byte, size)
	err = binary.Read(r, binary.LittleEndian, &frame)
	return
}
1244804
func (sp *Suback) Decode(src []byte) (int, error) {
	total := 0

	// decode header
	hl, _, rl, err := headerDecode(src[total:], SUBACK)
	total += hl
	if err != nil {
		return total, err
	}

	// check buffer length
	if len(src) < total+2 {
		return total, makeError(sp.Type(), "insufficient buffer size, expected %d, got %d", total+2, len(src))
	}

	// check remaining length
	if rl <= 2 {
		return total, makeError(sp.Type(), "expected remaining length to be greater than 2, got %d", rl)
	}

	// read packet id
	sp.ID = ID(binary.BigEndian.Uint16(src[total:]))
	total += 2

	// check packet id
	if !sp.ID.Valid() {
		return total, makeError(sp.Type(), "packet id must be grater than zero")
	}

	// calculate number of return codes
	rcl := int(rl) - 2

	// read return codes
	sp.ReturnCodes = make([]QOS, rcl)
	for i, rc := range src[total : total+rcl] {
		sp.ReturnCodes[i] = QOS(rc)
	}
	total += len(sp.ReturnCodes)

	// validate return codes
	for i, code := range sp.ReturnCodes {
		if !code.Successful() && code != QOSFailure {
			return total, makeError(sp.Type(), "invalid return code %d for topic %d", code, i)
		}
	}

	return total, nil
}
315683
func (j *jsonEncoder) Decode(data []byte, v interface{}) error {
	return json.Unmarshal(data, v)
}
1141702
func DecodeString(src string) (body []byte, err error) {
	body, err = Encoding.DecodeString(src)
	return
}
805255
func (cp *Connack) Decode(src []byte) (int, error) {
	total := 0

	// decode header
	hl, _, rl, err := headerDecode(src, CONNACK)
	total += hl
	if err != nil {
		return total, err
	}

	// check remaining length
	if rl != 2 {
		return total, makeError(cp.Type(), "expected remaining length to be 2")
	}

	// read connack flags
	connackFlags := src[total]
	cp.SessionPresent = connackFlags&0x1 == 1
	total++

	// check flags
	if connackFlags&254 != 0 {
		return 0, makeError(cp.Type(), "bits 7-1 in acknowledge flags are not 0")
	}

	// read return code
	cp.ReturnCode = ConnackCode(src[total])
	total++

	// check return code
	if !cp.ReturnCode.Valid() {
		return 0, makeError(cp.Type(), "invalid return code (%d)", cp.ReturnCode)
	}

	return total, nil
}
547442
func matchlen(a, b []byte) (i int) {
	alen := len(a)
	blen := len(b)
	for i < alen && i < blen && a[i] == b[i] {
		i++
	}
	return i
}
872988
func (pool *ByteSlicePool) decodeBytes(b PacketReader) []byte {
	fieldLength := decodeUint16(b)
	field := pool.getByteSlice(int(fieldLength))
	b.Read(field)
	return field
}
882596
func Decode(dst, src []byte) ([]byte, error) {

	if len(src) < 4 {
		return nil, ErrCorrupt
	}

	uncompressedLen := binary.LittleEndian.Uint32(src)

	if uncompressedLen == 0 {
		return nil, nil
	}

	if uncompressedLen > MaxInputSize {
		return nil, ErrTooLarge
	}

	if dst == nil || len(dst) < int(uncompressedLen) {
		dst = make([]byte, uncompressedLen)
	}

	d := decoder{src: src, dst: dst[:uncompressedLen], spos: 4}

	decr := []uint32{0, 3, 2, 3}

	for {
		code, err := d.readByte()
		if err != nil {
			return d.dst, d.finish(err)
		}

		length := uint32(code >> mlBits)
		if length == runMask {
			ln, err := d.getLen()
			if err != nil {
				return nil, ErrCorrupt
			}
			length += ln
		}

		if int(d.spos+length) > len(d.src) || int(d.dpos+length) > len(d.dst) {
			return nil, ErrCorrupt
		}

		for ii := uint32(0); ii < length; ii++ {
			d.dst[d.dpos+ii] = d.src[d.spos+ii]
		}

		d.spos += length
		d.dpos += length

		if int(d.spos) == len(d.src) {
			return d.dst, nil
		}

		if int(d.spos+2) >= len(d.src) {
			return nil, ErrCorrupt
		}

		back := uint32(d.src[d.spos]) | uint32(d.src[d.spos+1])<<8

		if back > d.dpos {
			return nil, ErrCorrupt
		}

		d.spos += 2
		d.ref = d.dpos - back

		length = uint32(code & mlMask)
		if length == mlMask {
			ln, err := d.getLen()
			if err != nil {
				return nil, ErrCorrupt
			}
			length += ln
		}

		literal := d.dpos - d.ref

		if literal < 4 {
			if int(d.dpos+4) > len(d.dst) {
				return nil, ErrCorrupt
			}

			d.cp(4, decr[literal])
		} else {
			length += 4
		}

		if d.dpos+length > uncompressedLen {
			return nil, ErrCorrupt
		}

		d.cp(length, 0)
	}
}
1081075
func Decode(dst, src []byte) (int, error) {
	dLen := DecodedLen(len(src))
	if EncodedLen(dLen) != len(src) {
		return 0, ErrLength
	}
	if len(dst) < dLen {
		panic("dst has insufficient length")
	}
	dst = dst[:0]
	whichByte := uint(1)
	bufByte := byte(0)
	for _, v := range src {
		if (v & 0x80) != 0 {
			return len(dst), ErrBit
		}
		if whichByte > 1 {
			dst = append(dst, bufByte|(v>>(8-whichByte)))
		}
		bufByte = v << whichByte
		if whichByte == 8 {
			whichByte = 0
		}
		whichByte++
	}
	return len(dst), nil
}
87469
func DecodeStringBlock(block []byte, a *[]StringValue) ([]StringValue, error) {
	blockType := block[0]
	if blockType != BlockString {
		return nil, fmt.Errorf("invalid block type: exp %d, got %d", BlockString, blockType)
	}

	block = block[1:]

	// The first 8 bytes is the minimum timestamp of the block
	tb, vb, err := unpackBlock(block)
	if err != nil {
		return nil, err
	}

	sz := CountTimestamps(tb)

	if cap(*a) < sz {
		*a = make([]StringValue, sz)
	} else {
		*a = (*a)[:sz]
	}

	tdec := timeDecoderPool.Get(0).(*TimeDecoder)
	vdec := stringDecoderPool.Get(0).(*StringDecoder)

	var i int
	err = func(a []StringValue) error {
		// Setup our timestamp and value decoders
		tdec.Init(tb)
		err = vdec.SetBytes(vb)
		if err != nil {
			return err
		}

		// Decode both a timestamp and value
		j := 0
		for j < len(a) && tdec.Next() && vdec.Next() {
			a[j] = StringValue{unixnano: tdec.Read(), value: vdec.Read()}
			j++
		}
		i = j

		// Did timestamp decoding have an error?
		err = tdec.Error()
		if err != nil {
			return err
		}
		// Did string decoding have an error?
		return vdec.Error()
	}(*a)

	timeDecoderPool.Put(tdec)
	stringDecoderPool.Put(vdec)

	return (*a)[:i], err
}
405493
func LimitLen(b []byte, max ...interface{}) []byte {
	return b[:MinInt(len(b), max...)]
}
1086601
func (enc *Encoding) EncodedLen(n int) int {

	// Fast path!
	if n == enc.base256BlockLen {
		return enc.baseXBlockLen
	}

	nblocks := n / enc.base256BlockLen
	out := nblocks * enc.baseXBlockLen
	rem := n % enc.base256BlockLen
	if rem > 0 {
		out += int(math.Ceil(float64(rem*8) / enc.logOfBase))
	}
	return out
}