// DecodeHeader decodes binary PDU header data.
430920
func DecodeHeader(r io.Reader) (*Header, error) {
	b := make([]byte, HeaderLen)
	_, err := io.ReadFull(r, b)
	if err != nil {
		return nil, err
	}
	l := binary.BigEndian.Uint32(b[0:4])
	if l < HeaderLen {
		return nil, fmt.Errorf("PDU too small: %d < %d", l, HeaderLen)
	}
	if l > MaxSize {
		return nil, fmt.Errorf("PDU too large: %d > %d", l, MaxSize)
	}
	hdr := &Header{
		Len:    l,
		ID:     ID(binary.BigEndian.Uint32(b[4:8])),
		Status: Status(binary.BigEndian.Uint32(b[8:12])),
		Seq:    binary.BigEndian.Uint32(b[12:16]),
	}
	return hdr, nil
}
305972
func (h *HashID) DecodeHex(hash string) (string, error) {
	numbers, err := h.DecodeInt64WithError(hash)
	if err != nil {
		return "", err
	}

	const hex = "0123456789abcdef"
	b := make([]byte, len(numbers))
	for i, n := range numbers {
		if n < 0x10 || n > 0x1f {
			return "", errors.New("invalid number")
		}
		b[i] = hex[n-0x10]
	}
	return string(b), nil
}
494870
func (u *UUID) decodeHashLike(t []byte) (err error) {
	src := t[:]
	dst := u[:]

	if _, err = hex.Decode(dst, src); err != nil {
		return err
	}
	return
}
256967
func (r ContainerDecoder) DecodeHostConfig(src io.Reader) (*container.HostConfig, error) {
	return decodeHostConfig(src)
}
389221
func decodeHash(hash []byte) (Params, []byte, []byte, error) {
	vals := strings.Split(string(hash), "$")

	// P, N, R, salt, scrypt derived key
	if len(vals) != 5 {
		return Params{}, nil, nil, ErrInvalidHash
	}

	var params Params
	var err error

	params.N, err = strconv.Atoi(vals[0])
	if err != nil {
		return params, nil, nil, ErrInvalidHash
	}

	params.R, err = strconv.Atoi(vals[1])
	if err != nil {
		return params, nil, nil, ErrInvalidHash
	}

	params.P, err = strconv.Atoi(vals[2])
	if err != nil {
		return params, nil, nil, ErrInvalidHash
	}

	salt, err := hex.DecodeString(vals[3])
	if err != nil {
		return params, nil, nil, ErrInvalidHash
	}
	params.SaltLen = len(salt)

	dk, err := hex.DecodeString(vals[4])
	if err != nil {
		return params, nil, nil, ErrInvalidHash
	}
	params.DKLen = len(dk)

	if err := params.Check(); err != nil {
		return params, nil, nil, err
	}

	return params, salt, dk, nil
}
1075874
func (m *HilSensor) Decode(buf []byte) {
	data := bytes.NewBuffer(buf)
	binary.Read(data, binary.LittleEndian, &m.TIME_USEC)
	binary.Read(data, binary.LittleEndian, &m.XACC)
	binary.Read(data, binary.LittleEndian, &m.YACC)
	binary.Read(data, binary.LittleEndian, &m.ZACC)
	binary.Read(data, binary.LittleEndian, &m.XGYRO)
	binary.Read(data, binary.LittleEndian, &m.YGYRO)
	binary.Read(data, binary.LittleEndian, &m.ZGYRO)
	binary.Read(data, binary.LittleEndian, &m.XMAG)
	binary.Read(data, binary.LittleEndian, &m.YMAG)
	binary.Read(data, binary.LittleEndian, &m.ZMAG)
	binary.Read(data, binary.LittleEndian, &m.ABS_PRESSURE)
	binary.Read(data, binary.LittleEndian, &m.DIFF_PRESSURE)
	binary.Read(data, binary.LittleEndian, &m.PRESSURE_ALT)
	binary.Read(data, binary.LittleEndian, &m.TEMPERATURE)
	binary.Read(data, binary.LittleEndian, &m.FIELDS_UPDATED)
}
703774
func (s *ReportStatus) Decode(r io.Reader) error {
	scan := pktline.NewScanner(r)
	if err := s.scanFirstLine(scan); err != nil {
		return err
	}

	if err := s.decodeReportStatus(scan.Bytes()); err != nil {
		return err
	}

	flushed := false
	for scan.Scan() {
		b := scan.Bytes()
		if isFlush(b) {
			flushed = true
			break
		}

		if err := s.decodeCommandStatus(b); err != nil {
			return err
		}
	}

	if !flushed {
		return fmt.Errorf("missing flush")
	}

	return scan.Err()
}
542395
func Decode(r io.Reader) ([]byte, MetaData, error) {
	d, err := NewDecoder(r)
	if err != nil {
		return nil, MetaData{}, err
	}

	data := make([]byte, 0, d.TotalSamples*int64(d.NChannels)*int64(d.BitsPerSample/8))
	for {
		frame, err := d.Next()
		if err == io.EOF {
			break
		} else if err != nil {
			return nil, MetaData{}, err
		}
		data = append(data, frame...)
	}

	h := md5.New()
	if _, err := h.Write(data); err != nil {
		return nil, MetaData{}, err
	}
	if !bytes.Equal(h.Sum(nil), d.MD5[:]) {
		return nil, MetaData{}, errors.New("Bad MD5 checksum")
	}
	return data, d.MetaData, nil
}
1169888
func decodeHeaders(r io.Reader) (transport.Headers, error) {
	reader := internal.NewReader(r)

	count := reader.ReadUint16()
	if count == 0 {
		return transport.Headers{}, reader.Err()
	}

	headers := transport.NewHeadersWithCapacity(int(count))
	for i := 0; i < int(count) && reader.Err() == nil; i++ {
		k := reader.ReadLen16String()
		v := reader.ReadLen16String()
		headers = headers.With(k, v)
	}

	return headers, reader.Err()
}
802676
func (m *HilControls) Decode(buf []byte) {
	data := bytes.NewBuffer(buf)
	binary.Read(data, binary.LittleEndian, &m.TIME_USEC)
	binary.Read(data, binary.LittleEndian, &m.ROLL_AILERONS)
	binary.Read(data, binary.LittleEndian, &m.PITCH_ELEVATOR)
	binary.Read(data, binary.LittleEndian, &m.YAW_RUDDER)
	binary.Read(data, binary.LittleEndian, &m.THROTTLE)
	binary.Read(data, binary.LittleEndian, &m.AUX1)
	binary.Read(data, binary.LittleEndian, &m.AUX2)
	binary.Read(data, binary.LittleEndian, &m.AUX3)
	binary.Read(data, binary.LittleEndian, &m.AUX4)
	binary.Read(data, binary.LittleEndian, &m.MODE)
	binary.Read(data, binary.LittleEndian, &m.NAV_MODE)
}
884790
func (f *PkgFilter) Decode(r io.Reader) error {
	if err := binary.Read(r, binary.BigEndian, &f.count); err != nil {
		return err
	}

	f.filter = make([]byte, f.Size()-2)
	_, err := io.ReadFull(r, f.filter)

	return err
}
1205994
func (cp *Connect) Decode(src []byte) (int, error) {
	total := 0

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

	// read protocol string
	protoName, n, err := readLPBytes(src[total:], false, cp.Type())
	total += n
	if err != nil {
		return total, err
	}

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

	// read version
	versionByte := src[total]
	total++

	// check protocol string and version
	if versionByte != Version311 && versionByte != Version31 {
		return total, makeError(cp.Type(), "invalid protocol version (%d)", versionByte)
	}

	// set version
	cp.Version = versionByte

	// check protocol version string
	if !bytes.Equal(protoName, version311Name) && !bytes.Equal(protoName, version31Name) {
		return total, makeError(cp.Type(), "invalid protocol version description (%s)", protoName)
	}

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

	// read connect flags
	connectFlags := src[total]
	total++

	// read flags
	usernameFlag := ((connectFlags >> 7) & 0x1) == 1
	passwordFlag := ((connectFlags >> 6) & 0x1) == 1
	willFlag := ((connectFlags >> 2) & 0x1) == 1
	willRetain := ((connectFlags >> 5) & 0x1) == 1
	willQOS := QOS((connectFlags >> 3) & 0x3)
	cp.CleanSession = ((connectFlags >> 1) & 0x1) == 1

	// check reserved bit
	if connectFlags&0x1 != 0 {
		return total, makeError(cp.Type(), "reserved bit 0 is not 0")
	}

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

	// check will flags
	if !willFlag && (willRetain || willQOS != 0) {
		return total, makeError(cp.Type(), "if the will flag is set to 0 the will qos and will retain fields must be set to zero")
	}

	// create will if present
	if willFlag {
		cp.Will = &Message{QOS: willQOS, Retain: willRetain}
	}

	// check auth flags
	if !usernameFlag && passwordFlag {
		return total, makeError(cp.Type(), "password flag is set but username flag is not set")
	}

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

	// read keep alive
	cp.KeepAlive = binary.BigEndian.Uint16(src[total:])
	total += 2

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

	// if the client supplies a zero-byte clientID, the client must also set CleanSession to 1
	if len(cp.ClientID) == 0 && !cp.CleanSession {
		return total, makeError(cp.Type(), "clean session must be 1 if client id is zero length")
	}

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

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

	// read username
	if usernameFlag {
		cp.Username, n, err = readLPString(src[total:], cp.Type())
		total += n
		if err != nil {
			return total, err
		}
	}

	// read password
	if passwordFlag {
		cp.Password, n, err = readLPString(src[total:], cp.Type())
		total += n
		if err != nil {
			return total, err
		}
	}

	return total, nil
}
776935
func decode(buf []byte, out interface{}) error {
	r := bytes.NewReader(buf)
	hd := codec.MsgpackHandle{}
	dec := codec.NewDecoder(r, &hd)
	return dec.Decode(out)
}
462152
func decodeHostConfig(src io.Reader) (*container.HostConfig, error) {
	decoder := json.NewDecoder(src)

	var w ContainerConfigWrapper
	if err := decoder.Decode(&w); err != nil {
		return nil, err
	}

	hc := w.getHostConfig()
	return hc, nil
}
930488
func DecodeHexFixed(dst, src []byte) error {
	// hex.Decode is wrapped because it does not error on short reads and panics on long reads.
	if len(src)%2 == 1 {
		return hex.ErrLength
	}
	if len(dst) != hex.DecodedLen(len(src)) {
		return NewHexWrongLengthError(fmt.Sprintf(
			"error decoding fixed-length hex: expected %v bytes but got %v", len(dst), hex.DecodedLen(len(src))))
	}
	n, err := hex.Decode(dst, src)
	if err != nil {
		return err
	}
	if n != len(dst) {
		return NewHexWrongLengthError(fmt.Sprintf(
			"error decoding fixed-length hex: expected %v bytes but got %v", len(dst), n))
	}
	return nil
}
332333
func (d *Decoder) Decode(idx *Index) error {
	var err error
	idx.Version, err = validateHeader(d.r)
	if err != nil {
		return err
	}

	entryCount, err := binary.ReadUint32(d.r)
	if err != nil {
		return err
	}

	if err := d.readEntries(idx, int(entryCount)); err != nil {
		return err
	}

	return d.readExtensions(idx)
}
1068699
func (d *JSONDecoder) DecodeHistoryRemove(data []byte) (*HistoryRemoveRequest, error) {
	var p HistoryRemoveRequest
	err := json.Unmarshal(data, &p)
	if err != nil {
		return nil, err
	}
	return &p, nil
}