// UnmarshalBinary decodes msgpack encoded ServiceConfigResponse. It used
// default msgpack encoding but fixes up the uint8 strings and other problems we
// have with encoding map[string]interface{}.
513996
func (r *ServiceConfigResponse) UnmarshalBinary(data []byte) error {
	dec := codec.NewDecoderBytes(data, msgpackHandle)

	type Alias ServiceConfigResponse
	var a Alias

	if err := dec.Decode(&a); err != nil {
		return err
	}

	*r = ServiceConfigResponse(a)

	var err error

	// Fix strings and maps in the returned maps
	r.ProxyConfig, err = lib.MapWalk(r.ProxyConfig)
	if err != nil {
		return err
	}
	for k := range r.UpstreamConfigs {
		r.UpstreamConfigs[k], err = lib.MapWalk(r.UpstreamConfigs[k])
		if err != nil {
			return err
		}
	}
	return nil
}
3745
func (h *HLL) UnmarshalBinary(data []byte) error {
	var s serializable
	err := gob.NewDecoder(bytes.NewReader(data)).Decode(&s)
	if err != nil {
		return err
	}
	h.P = s.P
	h.m1 = s.M1
	h.m2 = s.M2
	h.alpha = s.Alpha
	h.format = s.Format
	h.tempSet = &s.TempSet
	h.sparseList = &s.SparseList
	h.registers = s.Registers

	if h.Hasher == nil {
		h.Hasher = MMH3Hash
	}
	return nil
}
551514
func (r *ReplyReadV3) UnmarshalBinary(data []byte) error {
	return UnmarshalBinary(data, &r.ActualSize, &r.Reserved, &r.Payload)
}
424841
func (p *BeaconFreqAnsPayload) UnmarshalBinary(data []byte) error {
	if len(data) != 1 {
		return errors.New("lorawan: 1 byte of data is expected")
	}

	p.BeaconFrequencyOK = data[0]&(1<<0) != 0

	return nil
}
235384
func (hmac *HMAC) UnmarshalBinary(data []byte) error {
	return hmac.h.UnmarshalBinary(data)
}
1337400
func (p *FragSessionStatusAnsPayload) UnmarshalBinary(data []byte) error {
	if len(data) < p.Size() {
		return fmt.Errorf("lorawan/applayer/fragmentation: %d bytes are expected", p.Size())
	}

	p.ReceivedAndIndex.NbFragReceived = binary.LittleEndian.Uint16(data[0:2]) & 0x3fff // filter out FragIndex
	p.ReceivedAndIndex.FragIndex = data[1] >> 6

	p.MissingFrag = data[2]
	p.Status.NotEnoughMatrixMemory = data[3]&0x01 != 0

	return nil
}
328008
func (p *RejoinParamSetupReqPayload) UnmarshalBinary(data []byte) error {
	if len(data) != 1 {
		return errors.New("lorawan: 1 byte of data is exepcted")
	}

	p.MaxCountN = data[0] & ((1 << 3) | (1 << 2) | (1 << 1) | 1)
	p.MaxTimeN = (data[0] & ((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4))) >> 4

	return nil
}
885629
func (p *McGroupStatusAnsPayload) UnmarshalBinary(data []byte) error {
	if len(data) == 0 {
		return errors.New("lorawan/applayer/multicastsetup: at least 1 byte is expected")
	}

	var ansGroupMaskCount int
	for i := range p.Status.AnsGroupMask {
		if data[0]&(1<<uint8(i)) != 0 {
			p.Status.AnsGroupMask[i] = true
			ansGroupMaskCount++
		}
	}

	p.Status.NbTotalGroups = (data[0] & 0x70) >> 4

	if len(data) < p.Size() {
		return fmt.Errorf("lorawan/applayer/multicastsetup: %d bytes are expected", p.Size())
	}

	for i := 0; i < ansGroupMaskCount; i++ {
		offset := 1 + (i * 5)
		item := McGroupStatusAnsPayloadItem{
			McGroupID: data[offset] & 0x03, // first two bits
		}

		if err := item.McAddr.UnmarshalBinary(data[offset+1 : offset+5]); err != nil {
			return err
		}

		p.Items = append(p.Items, item)
	}

	return nil
}
667575
func (p *DeviceAppTimePeriodicityAnsPayload) UnmarshalBinary(data []byte) error {
	if len(data) < p.Size() {
		return fmt.Errorf("lorawan/applayer/clocksync: %d bytes are expected", p.Size())
	}
	if data[0]&1 != 0 {
		p.Status.NotSupported = true
	}
	p.Time = binary.LittleEndian.Uint32(data[1:5])
	return nil
}
649708
func UnmarshalBinary(data []byte, fields ...interface{}) error {
	buf := bytes.NewBuffer(data)

	for _, p := range fields {
		switch m := p.(type) {
		case encoding.BinaryUnmarshaler:
			return m.UnmarshalBinary(buf.Bytes())
		case *[]byte:
			*m = buf.Bytes()
			return nil
		default:
			err := binary.Read(buf, binary.LittleEndian, p)
			if err != nil {
				return ProtocolError(err)
			}
		}
	}

	return nil
}
1366620
func (p *RXTimingSetupReqPayload) UnmarshalBinary(data []byte) error {
	if len(data) != 1 {
		return errors.New("lorawan: 1 byte of data is expected")
	}
	p.Delay = data[0]
	return nil
}
864430
func (n *NetID) UnmarshalBinary(data []byte) error {
	if len(data) != 3 {
		return errors.New("ttn/core: Invalid length for NetID")
	}
	copy(n[:], data)
	return nil
}
668239
func (r *RequestWriteV3) UnmarshalBinary(data []byte) error {
	return UnmarshalBinary(data, &r.Handle, &r.WriteFlags, &r.Offset, &r.RequiredSize, &r.Reserved, &r.Payload)
}
1139690
func (p *TXParamSetupReqPayload) UnmarshalBinary(data []byte) error {
	if len(data) != 1 {
		return errors.New("lorawan: 1 byte of data is expected")
	}

	if data[0]&(1<<4) > 0 {
		p.UplinkDwellTime = DwellTime400ms
	}
	if data[0]&(1<<5) > 0 {
		p.DownlinkDwelltime = DwellTime400ms
	}
	p.MaxEIRP = []uint8{8, 10, 12, 13, 14, 16, 18, 20, 21, 24, 26, 27, 29, 30, 33, 36}[data[0]&15]

	return nil
}
955213
func (p *McClassBSessionAnsPayload) UnmarshalBinary(data []byte) error {
	if len(data) == 0 {
		return errors.New("lorawan/applayer/multicastsetup: at least 1 byte is expected")
	}

	// StatusAndMcGroupID
	p.StatusAndMcGroupID.McGroupID = data[0] & 0x03
	p.StatusAndMcGroupID.DRError = data[0]&0x04 != 0
	p.StatusAndMcGroupID.FreqError = data[0]&0x08 != 0
	p.StatusAndMcGroupID.McGroupUndefined = data[0]&0x10 != 0

	if !p.StatusAndMcGroupID.hasError() {
		if len(data) < p.Size() {
			return fmt.Errorf("lorawan/applayer/multicastsetup: %d bytes are expected", p.Size())
		}

		ttsB := make([]byte, 4)
		copy(ttsB, data[1:4])
		tts := binary.LittleEndian.Uint32(ttsB)
		p.TimeToStart = &tts
	}

	return nil
}
432146
func (p *ForceDeviceResyncReqPayload) UnmarshalBinary(data []byte) error {
	if len(data) < p.Size() {
		return fmt.Errorf("lorawan/applayer/clocksync: %d bytes are expected", p.Size())
	}
	p.ForceConf.NbTransmissions = data[0] & 0x17
	return nil
}
485492
func (r *RequestOpen) UnmarshalBinary(data []byte) error {
	return UnmarshalBinary(data, &r.Request, &r.OpenMode, &r.OpenFlags, &r.Permissions, &r.FileName)
}