// FromNData constructs a new Buffer from a "n:data" format
// (I made that name up)
962279
func FromNData(v []byte) (Buffer, error) {
	size := binary.BigEndian.Uint32(v)
	buf := make([]byte, int(size))
	copy(buf, v[4:4+size])
	return Buffer(buf), nil
}
972964
func (bi *BinaryInfo) LoadFromData(dwdata *dwarf.Data, debugFrameBytes, debugLineBytes, debugLocBytes []byte) {
	bi.closer = (*nilCloser)(nil)
	bi.sepDebugCloser = (*nilCloser)(nil)
	bi.dwarf = dwdata

	if debugFrameBytes != nil {
		bi.frameEntries = frame.Parse(debugFrameBytes, frame.DwarfEndian(debugFrameBytes), bi.staticBase)
	}

	bi.loclistInit(debugLocBytes)

	bi.loadDebugInfoMaps(debugLineBytes, nil, nil)
}
1172229
func (c *xContext) MapData(i interface{}, data map[string][]string, names ...string) error {
	var name string
	if len(names) > 0 {
		name = names[0]
	}
	return NamedStructMap(c.echo, i, data, name)
}
57288
func (c *ReactionsAddCall) FromValues(v url.Values) error {
	var tmp ReactionsAddCall
	if raw := strings.TrimSpace(v.Get("channel")); len(raw) > 0 {
		tmp.channel = raw
	}
	if raw := strings.TrimSpace(v.Get("file")); len(raw) > 0 {
		tmp.file = raw
	}
	if raw := strings.TrimSpace(v.Get("fileComment")); len(raw) > 0 {
		tmp.fileComment = raw
	}
	if raw := strings.TrimSpace(v.Get("name")); len(raw) > 0 {
		tmp.name = raw
	}
	if raw := strings.TrimSpace(v.Get("timestamp")); len(raw) > 0 {
		tmp.timestamp = raw
	}
	*c = tmp
	return nil
}
1106559
func FromBytes(b []byte) *Graph {
	p := NewParser(bytes.NewBuffer(b))
	p.Ogdl()
	return p.Graph()
}
985400
func fromStatT(fi *os.FileInfo) (*StatT, error) {
	return &StatT{
		size: (*fi).Size(),
		mode: (*fi).Mode(),
		mtim: (*fi).ModTime()}, nil
}
371103
func (enc *HTTPEncoder) EncodeData(data []byte) error {
	setContentType(enc.writer, ContentBinary)

	_, err := enc.writer.Write(data)
	if err != nil {
		http.Error(enc.writer, fmt.Sprintf("Unable to encode binary data: %s", err.Error()), http.StatusInternalServerError)
	}
	return err
}
384097
func (options *Options) FnData(data *DataFrame) string {
	return options.evalBlock(nil, data, nil)
}
1235859
func NewBlockData(height uint64) *BlockData {
	data := types.EventData{
		Tables:      make(map[string]types.EventDataTable),
		BlockHeight: height,
	}

	return &BlockData{
		Data: data,
	}
}
1334605
func SimpleData(name, value string) *SimpleElement {
	return &SimpleElement{
		StartElement: xml.StartElement{
			Name: xml.Name{Local: "SimpleData"},
			Attr: []xml.Attr{
				{Name: xml.Name{Local: "name"}, Value: name},
			},
		},
		value: value,
	}
}
1064513
func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
	// Save memory data to temporary file
	tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
	os.MkdirAll(path.Dir(tmpName), os.ModePerm)
	if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
		return nil, err
	}
	return ini.Parse(tmpName)
}
1268287
func (p *Parser) OnData(reply, endStream bool, dataBuffers [][]byte) (proxylib.OpType, int) {
	if reply {
		injected := p.injectFromQueue()
		if injected > 0 {
			return proxylib.INJECT, injected
		}
		if len(dataBuffers) == 0 {
			return proxylib.NOP, 0
		}
	}

	// TODO: don't copy data to new slices
	data := (bytes.Join(dataBuffers, []byte{}))
	log.Debugf("Data length: %d", len(data))

	linefeed := bytes.Index(data, []byte("\r\n"))
	if linefeed < 0 {
		log.Debugf("Did not receive full first line")
		if len(data) > 0 && data[len(data)-1] == '\r' {
			return proxylib.MORE, 1
		}
		return proxylib.MORE, 2
	}

	// TODO: iterate over data without copying it to new slices
	// Tokenizing in memcached is done by spaces: https://github.com/memcached/memcached/blob/master/memcached.c#L2978
	tokens := bytes.Fields(data[:linefeed])

	if !reply {
		meta := meta.MemcacheMeta{
			Command: string(tokens[0]),
		}
		command := tokens[0]

		frameLength := linefeed + 2
		hasNoreply := false
		switch {
		case p.isCommandRetrieval(command):
			// get, gets, gat, gats
			if bytes.HasPrefix(command, []byte("get")) {
				meta.Keys = tokens[1:]
			} else if bytes.HasPrefix(command, []byte("gat")) {
				meta.Keys = tokens[2:]
			}
		case p.isCommandStorage(command):
			// storage commands
			meta.Keys = tokens[1:2]
			nBytes, err := strconv.Atoi(string(tokens[4]))
			if err != nil {
				log.Error("Failed to parse storage payload length")
				return proxylib.ERROR, 0
			}
			// 2 additional bytes for terminating linefeed
			frameLength += nBytes + 2

			if command[0] == 'c' { //storage command is "cas"
				hasNoreply = len(tokens) == casWithNoreplyFields
			} else {
				hasNoreply = len(tokens) == storageWithNoreplyFields
			}
		case p.isCommandDelete(command):
			meta.Keys = tokens[1:2]
			hasNoreply = len(tokens) == deleteWithNoreplyFields
		case p.isCommandIncrDecr(command):
			meta.Keys = tokens[1:2]
			hasNoreply = len(tokens) == incrWithNoreplyFields
		case bytes.Equal(command, []byte("touch")):
			meta.Keys = tokens[1:2]
			hasNoreply = len(tokens) == touchWithNoreplyFields
		case bytes.Equal(command, []byte("slabs")),
			bytes.Equal(command, []byte("lru")),
			bytes.Equal(command, []byte("lru_crawler")),
			bytes.Equal(command, []byte("stats")),
			bytes.Equal(command, []byte("version")),
			bytes.Equal(command, []byte("misbehave")):

			meta.Keys = [][]byte{}
		case bytes.Equal(command, []byte("flush_all")),
			bytes.Equal(command, []byte("cache_memlimit")):
			meta.Keys = [][]byte{}
			hasNoreply = bytes.Equal(tokens[len(tokens)-1], []byte("noreply"))
		case bytes.Equal(command, []byte("quit")):
			meta.Keys = [][]byte{}
			hasNoreply = true
		case bytes.Equal(command, []byte("watch")):
			meta.Keys = [][]byte{}
			p.watching = true
		default:
			log.Error("Could not parse text memcache frame")
			return proxylib.ERROR, 0
		}
		logEntry := &cilium.LogEntry_GenericL7{
			GenericL7: &cilium.L7LogEntry{
				Proto: "textmemcached",
				Fields: map[string]string{
					"command": meta.Command,
					"keys":    string(bytes.Join(meta.Keys, []byte(", "))),
				},
			},
		}

		r := &replyIntent{
			command: command,
		}

		matches := p.connection.Matches(meta)

		if matches {
			r.denied = false
			if !hasNoreply {
				p.replyQueue = append(p.replyQueue, r)
			}
			p.connection.Log(cilium.EntryType_Request, logEntry)
			return proxylib.PASS, frameLength
		}

		r.denied = true
		if !hasNoreply {
			if len(p.replyQueue) == 0 {
				p.injectDeniedMessage()
			} else {
				p.replyQueue = append(p.replyQueue, r)
			}
		}
		p.connection.Log(cilium.EntryType_Denied, logEntry)
		return proxylib.DROP, frameLength
	}

	//reply
	log.Debugf("reply, parsing to figure out if we have it all")

	intent := p.replyQueue[0]

	logEntry := &cilium.LogEntry_GenericL7{
		GenericL7: &cilium.L7LogEntry{
			Proto: "textmemcached",
			Fields: map[string]string{
				"command": string(intent.command),
			},
		},
	}
	if p.watching {
		// in watch mode we pass all replied lines
		return proxylib.PASS, linefeed + 2
	}

	switch {
	case p.isErrorReply(tokens[0]),
		p.isCommandStorage(intent.command),
		p.isCommandDelete(intent.command),
		p.isCommandIncrDecr(intent.command),
		bytes.Equal(intent.command, []byte("touch")),
		bytes.Equal(intent.command, []byte("slabs")),
		bytes.Equal(intent.command, []byte("lru")),
		bytes.Equal(intent.command, []byte("flush_all")),
		bytes.Equal(intent.command, []byte("cache_memlimit")),
		bytes.Equal(intent.command, []byte("version")),
		bytes.Equal(intent.command, []byte("misbehave")):

		// passing one line of reply
		p.connection.Log(cilium.EntryType_Response, logEntry)
		p.replyQueue = p.replyQueue[1:]
		return proxylib.PASS, linefeed + 2
	case p.isCommandRetrieval(intent.command),
		bytes.Equal(intent.command, []byte("stats")):
		t, nBytes := p.untilEnd(data)
		if t == proxylib.PASS {
			p.connection.Log(cilium.EntryType_Response, logEntry)
			p.replyQueue = p.replyQueue[1:]
		}
		return t, nBytes
	case bytes.Equal(intent.command, []byte("lru_crawler")):
		// check if it's response line
		if bytes.Equal(tokens[0], []byte("OK")) ||
			bytes.Equal(tokens[0], []byte("BUSY")) ||
			bytes.Equal(tokens[0], []byte("BADCLASS")) {
			p.connection.Log(cilium.EntryType_Response, logEntry)
			p.replyQueue = p.replyQueue[1:]
			return proxylib.PASS, linefeed + 2
		}

		t, nBytes := p.untilEnd(data)
		if t == proxylib.PASS {
			p.connection.Log(cilium.EntryType_Response, logEntry)
			p.replyQueue = p.replyQueue[1:]
		}
		return t, nBytes
	}
	log.Error("Could not parse text memcache frame")
	return proxylib.ERROR, 0
}
880461
func DiffDelta(src, tgt []byte) []byte {
	return diffDelta(new(deltaIndex), src, tgt)
}
91986
func FromECDSA(priv *ecdsa.PrivateKey) []byte {
	if priv == nil {
		return nil
	}
	return math.PaddedBigBytes(priv.D, priv.Params().BitSize/8)
}
931133
func newCoerceData(name string, att *design.AttributeDefinition, pointer bool, pkg string, depth int) map[string]interface{} {
	return map[string]interface{}{
		"Name":      name,
		"VarName":   codegen.Goify(name, false),
		"Pointer":   pointer,
		"Attribute": att,
		"Pkg":       pkg,
		"Depth":     depth,
	}
}
725956
func LoadedData(listener func(*vecty.Event)) *vecty.EventListener {
	return &vecty.EventListener{Name: "loadeddata", Listener: listener}
}
121554
func (f *inFlow) onData(n uint32) error {
	f.mu.Lock()
	f.pendingData += n
	if f.pendingData+f.pendingUpdate > f.limit+f.delta {
		limit := f.limit
		rcvd := f.pendingData + f.pendingUpdate
		f.mu.Unlock()
		return fmt.Errorf("received %d-bytes data exceeding the limit %d bytes", rcvd, limit)
	}
	f.mu.Unlock()
	return nil
}