// ParseWebRTCSignalMessage parses the web rtc command/message
973233
func ParseWebRTCSignalMessage(msg string) (*WebRTCSignalMessage, error) {
	// All messages are text (utf-8 encoded at present)
	if !utf8.Valid([]byte(msg)) {
		return nil, errInvalidChar
	}

	w := &WebRTCSignalMessage{}
	if err := json.Unmarshal([]byte(msg), w); err != nil {
		return nil, err
	}

	if err := validateOperation(w.Type); err != nil {
		return nil, err
	}

	return w, nil
}
763126
func (s *ServerRPC) ExecSignal(ctx context.Context, req *types.ExecSignalRequest) (*types.ExecSignalResponse, error) {
	err := s.daemon.KillExec(req.ContainerID, req.ExecID, req.Signal)
	if err != nil {
		return nil, err
	}

	return &types.ExecSignalResponse{}, nil
}
1014998
func (n *NodeSigner) SignMessage(pubKey *btcec.PublicKey,
	msg []byte) (*btcec.Signature, error) {

	// If this isn't our identity public key, then we'll exit early with an
	// error as we can't sign with this key.
	if !pubKey.IsEqual(n.privKey.PubKey()) {
		return nil, fmt.Errorf("unknown public key")
	}

	// Otherwise, we'll sign the dsha256 of the target message.
	digest := chainhash.DoubleHashB(msg)
	sign, err := n.privKey.Sign(digest)
	if err != nil {
		return nil, fmt.Errorf("can't sign the message: %v", err)
	}

	return sign, nil
}
179840
func messageToHex(msg wire.Message) (string, error) {
	var buf bytes.Buffer
	if err := msg.BtcEncode(&buf, maxProtocolVersion, wire.WitnessEncoding); err != nil {
		context := fmt.Sprintf("Failed to encode msg of type %T", msg)
		return "", internalRPCError(err.Error(), context)
	}

	return hex.EncodeToString(buf.Bytes()), nil
}
170126
func handleNotifyReceived(wsc *wsClient, icmd interface{}) (interface{}, error) {
	cmd, ok := icmd.(*btcjson.NotifyReceivedCmd)
	if !ok {
		return nil, btcjson.ErrRPCInternal
	}

	// Decode addresses to validate input, but the strings slice is used
	// directly if these are all ok.
	err := checkAddressValidity(cmd.Addresses, wsc.server.cfg.ChainParams)
	if err != nil {
		return nil, err
	}

	wsc.server.ntfnMgr.RegisterTxOutAddressRequests(wsc, cmd.Addresses)
	return nil, nil
}
430014
func SignAnnouncement(signer lnwallet.MessageSigner, pubKey *btcec.PublicKey,
	msg lnwire.Message) (*btcec.Signature, error) {

	var (
		data []byte
		err  error
	)

	switch m := msg.(type) {
	case *lnwire.ChannelAnnouncement:
		data, err = m.DataToSign()
	case *lnwire.ChannelUpdate:
		data, err = m.DataToSign()
	case *lnwire.NodeAnnouncement:
		data, err = m.DataToSign()
	default:
		return nil, errors.New("can't sign message " +
			"of this format")
	}
	if err != nil {
		return nil, errors.Errorf("unable to get data to sign: %v", err)
	}

	return signer.SignMessage(pubKey, data)
}
635805
func handleNotifyNewTransactions(wsc *wsClient, icmd interface{}) (interface{}, error) {
	cmd, ok := icmd.(*btcjson.NotifyNewTransactionsCmd)
	if !ok {
		return nil, btcjson.ErrRPCInternal
	}

	wsc.verboseTxUpdates = cmd.Verbose != nil && *cmd.Verbose
	wsc.server.ntfnMgr.RegisterNewMempoolTxsUpdates(wsc)
	return nil, nil
}
1039170
func (b *Build) ParseMessage(msg *v1.ApiCommonBuildMessage) error {
	status, err := StatusToV2(msg)
	if err != nil {
		return err
	}

	var createdBy identity.Identity
	if msg.CreatedBy != "" {
		createdBy, err = identity.MakeIdentity(msg.CreatedBy)
		if err != nil {
			return err
		}
	}

	tags := strpair.ParseMap(msg.Tags)
	builder := tags.Get(v1.TagBuilder)

	address := tags.Get(v1.TagBuildAddress)
	var number *int
	if address == "" {
		address = strconv.FormatInt(msg.Id, 10)
	} else {
		parts := strings.Split(address, "/")
		if len(parts) != 3 {
			return fmt.Errorf("invalid build_address %q: expected exactly 2 slashes", address)
		}
		if msg.Bucket == "" {
			// this is a partial response message
			msg.Bucket = parts[0]
		} else if msg.Bucket != parts[0] {
			return fmt.Errorf("invalid build_address %q: expected first component to be %q", address, msg.Bucket)
		}
		if builder == "" {
			return fmt.Errorf("build_address tag is present, but builder tag is not")
		} else if parts[1] != builder {
			return fmt.Errorf("invalid build_address %q: expected second component to be %q", address, builder)
		}
		num, err := strconv.Atoi(parts[2])
		if err != nil {
			return fmt.Errorf("invalid build_address %q: expected third component to be a valid int32", address)
		}
		number = &num
	}

	input := struct{ Properties interface{} }{b.Input.Properties}
	if err := parseJSON(msg.ParametersJson, &input); err != nil {
		return errors.Annotate(err, "invalid msg.ParametersJson").Err()
	}

	output := struct {
		Properties interface{}
		Error      struct {
			Message string
		}
	}{Properties: b.Output.Properties}
	if err := parseJSON(msg.ResultDetailsJson, &output); err != nil {
		return errors.Annotate(err, "invalid msg.ResultDetailsJson").Err()
	}
	var outErr error
	if output.Error.Message != "" {
		outErr = errors.New(output.Error.Message)
	}

	project := msg.Project
	if project == "" {
		// old builds do not have project attribute.
		project = v1.ProjectFromBucket(msg.Bucket)
	}

	*b = Build{
		ID:           msg.Id,
		CreationTime: v1.ParseTimestamp(msg.CreatedTs),
		CreatedBy:    createdBy,
		Project:      msg.Project,
		Bucket:       msg.Bucket,
		Builder:      builder,
		Number:       number,
		Tags:         tags,
		Input: Input{
			Properties: input.Properties,
		},

		Status:           status,
		StatusChangeTime: v1.ParseTimestamp(msg.StatusChangedTs),
		URL:              msg.Url,
		StartTime:        v1.ParseTimestamp(msg.StartedTs),
		UpdateTime:       v1.ParseTimestamp(msg.UpdatedTs),
		Canary:           msg.Canary,
		Experimental:     msg.Experimental,

		CompletionTime: v1.ParseTimestamp(msg.CompletedTs),
		Output: Output{
			Properties: output.Properties,
			Err:        outErr,
		},
	}
	return nil
}
308774
func (ms *messageSerializer) getWebsocketCustomEvent(websocketMessage []byte) []byte {
	if len(websocketMessage) < ms.prefixAndSepIdx {
		return nil
	}
	s := websocketMessage[ms.prefixAndSepIdx:]
	evt := s[:bytes.IndexByte(s, messageSeparatorByte)]
	return evt
}
805031
func ParseMessage(data []byte, sender net.Addr) (Message, error) {
	address, idx := ReadString(data)
	msg := Message{
		Address: address,
		Sender:  sender,
	}
	data = data[idx:]
	typetags, idx := ReadString(data)
	data = data[idx:]

	// Read all arguments.
	args, err := ReadArguments([]byte(typetags), data)
	if err != nil {
		return Message{}, errors.Wrap(err, "parse message")
	}
	msg.Arguments = args

	return msg, nil
}
1291519
func FormatCloseMessage(closeCode int, text string) []byte {
	return websocket.FormatCloseMessage(closeCode, text)
}
200968
func OnSignalReceived(signalName string, deciders ...Decider) Decider {
	return OnSignalsReceived([]string{signalName}, deciders...)
}
449627
func ProcessSignal(command Command, service string) error {
	if service == "" {
		service = serviceName
	}

	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()

	s, err := m.OpenService(service)
	if err != nil {
		return fmt.Errorf("could not access service: %v", err)
	}
	defer s.Close()

	var (
		cmd svc.Cmd
		to  svc.State
	)

	switch command {
	case CommandStop, CommandQuit:
		cmd = svc.Stop
		to = svc.Stopped
	case CommandReopen:
		cmd = reopenLogCmd
		to = svc.Running
	case CommandReload:
		cmd = svc.ParamChange
		to = svc.Running
	case commandLDMode:
		cmd = ldmCmd
		to = svc.Running
	default:
		return fmt.Errorf("unknown signal %q", command)
	}

	status, err := s.Control(cmd)
	if err != nil {
		return fmt.Errorf("could not send control=%d: %v", cmd, err)
	}

	timeout := time.Now().Add(10 * time.Second)
	for status.State != to {
		if timeout.Before(time.Now()) {
			return fmt.Errorf("timeout waiting for service to go to state=%d", to)
		}
		time.Sleep(300 * time.Millisecond)
		status, err = s.Query()
		if err != nil {
			return fmt.Errorf("could not retrieve service status: %v", err)
		}
	}

	return nil
}
77706
func (g *GoWebSocket) MessageReceive(v interface{}) error {
	return websocket.Message.Receive(g.Conn, v)
}
885459
func (rca *RootCA) ParseValidateAndSignCSR(csrBytes []byte, cn, ou, org string) ([]byte, error) {
	signRequest := PrepareCSR(csrBytes, cn, ou, org)
	signer, err := rca.Signer()
	if err != nil {
		return nil, err
	}
	cert, err := signer.Sign(signRequest)
	if err != nil {
		return nil, errors.Wrap(err, "failed to sign node certificate")
	}

	return append(cert, rca.Intermediates...), nil
}
670786
func (a *VppAdapter) GetMsgNameByID(msgID uint16) (string, bool) {
	switch msgID {
	case 100:
		return "control_ping", true
	case 101:
		return "control_ping_reply", true
	case 200:
		return "sw_interface_dump", true
	case 201:
		return "sw_interface_details", true
	}

	a.access.Lock()
	defer a.access.Unlock()
	msgName, found := a.msgIDsToName[msgID]

	return msgName, found
}
271339
func parseCertificateRequest(priv crypto.Signer, csrBytes []byte) (template *x509.Certificate, err error) {

	csr, err := x509.ParseCertificateRequest(csrBytes)
	if err != nil {
		err = cferr.Wrap(cferr.CSRError, cferr.ParseFailed, err)
		return
	}

	csr.CheckSignature()
	if err != nil {
		err = cferr.Wrap(cferr.CSRError, cferr.KeyMismatch, err)
		return
	}

	template = &x509.Certificate{
		Subject:            csr.Subject,
		PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
		PublicKey:          csr.PublicKey,
		SignatureAlgorithm: signer.DefaultSigAlgo(priv),
	}

	return
}