// Read the key file into a 32 byte array
769700
func readKey(filename, pathFormat string) ([keySize]byte, error) {
	var data32 [keySize]byte

	// read the data back
	data, err := readFile(fmt.Sprintf(pathFormat, filename))
	if err != nil {
		return data32, err
	}
	// decode from hex
	dst := make([]byte, len(data))
	_, err = hex.Decode(dst, data) //.StdEncoding.Decode(dst, data)
	if err != nil {
		return data32, err
	}
	// fill in the 32 byte array witht he data and return it
	copy(data32[:], dst[:keySize])
	return data32, err
}
54298
func readKeyList(r *bufio.Reader) (key string, msg []string, err error) {
	key, err = readString1(r)
	if err != nil {
		return
	}
	msg = []string{}
	for {
		var c byte
		c, err = r.ReadByte()
		if err != nil {
			return
		}
		switch elementType(c) {
		case etLIST_ITEM:
			value, err := readString2(r)
			if err != nil {
				return "", nil, err
			}
			msg = append(msg, value)
		case etLIST_END: //end of outer list
			return key, msg, nil
		default:
			return "", nil, fmt.Errorf("[readKeyList] protocol error 2")
		}
	}
	return
}
693438
func (c *client) Bytes(key string, fallback []byte) []byte {
	if v := c.Getenv(key); v != "" {
		return []byte(v)
	}

	return fallback
}
1181284
func readKeyFile(filename string) []byte {
	configdir := os.Getenv("DEVDAY_CONFIG")
	if configdir == "" {
		panic("DEVDAY_CONFIG is not set")
	}
	path := filepath.Join(configdir, filename)
	key, err := ioutil.ReadFile(path)
	if err != nil {
		panic(fmt.Sprintf("Unable to read file: %s", path))
	}

	return key
}
212220
func ReadFile(file string) ([]byte, error) {
	bytes, err := ioutil.ReadFile(file)
	if err != nil {
		return []byte(""), err
	}
	// *100 is an safe guess to ensure the file can be decoded.
	decompressed := make([]byte, len(bytes)*100)
	_, err = lzf.Decompress(bytes, decompressed)
	return decompressed, err
}
1278969
func readFile(file string) ([]byte, error) {
	theFile, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	defer theFile.Close()

	byteValue, err := ioutil.ReadAll(theFile)
	if err != nil {
		return nil, err
	}

	return byteValue, nil
}
515618
func (k *Keyfile) Load() (err error) {
	if len(k.fname) > 0 {
		var f *os.File
		f, err = os.Open(k.fname)
		if err == nil {
			err = k.read(f)
			f.Close()
		}
	}
	return
}
217978
func (kf *KeyFormat) Scan(key []byte, args ...interface{}) error {
	segments := kf.ScanBytes(key)
	if len(args) > len(segments) {
		return fmt.Errorf("KeyFormat.Scan() is provided with %d args but format only has %d segments in key %X",
			len(args), len(segments), key)
	}
	for i, a := range args {
		scan(a, segments[i])
	}
	return nil
}
1054280
func GetKey(b []byte) Key {
	for _, k := range ASCIISequences {
		if bytes.Equal(k.ASCIICode, b) {
			return k.Key
		}
	}
	return NotDefined
}
415431
func readKeyMap(r *bufio.Reader) (key string, msg map[string]interface{}, err error) {
	key, err = readString1(r)
	if err != nil {
		return
	}
	msg, err = readMap(r, false)
	return
}
523404
func (c *VaultClient) ReadKey(path, key string) (string, error) {
	data, err := c.Read(path)
	if err != nil {
		return "", err
	}
	val, ok := data[key]
	if !ok {
		return "", errors.New("key not found")
	}
	return val.(string), nil
}
32714
func (p *Params) Uint32(key string) (uint32, error) {
	v, err := strconv.ParseUint(p.Get(key), 10, 32)
	return uint32(v), err
}
1136806
func ReadBinaryFromFile(file string) []byte {
	key, err := ioutil.ReadFile(file)
	if err != nil {
		panic(fmt.Sprintf("Failed to load %q from folder: %v", file, err))
	}
	return key
}
624506
func ReadMap(file string, isGbk bool, keyField string, out interface{}) (err error) {
	//catch panic
	defer func() {
		if rerr := recover(); rerr != nil {
			err = errors.New(fmt.Sprintf("read csv file: %v, error: %v", file, rerr))
		}
	}()

	if out == nil {
		return errors.New("Cannot remake from <nil>")
	}

	outv := reflect.ValueOf(out)

	outt := outv.Type()
	outk := outt.Kind()

	if outk != reflect.Ptr {
		return errors.New("Cannot reflect into non-pointer")
	}
	mapv := outv.Elem()
	mapt := mapv.Type()
	mapk := mapv.Kind()

	if mapk != reflect.Map {
		return errors.New("Pointer must point to a slice")
	}

	//make map
	if mapv.IsNil() {
		mapv.Set(reflect.MakeMap(mapt))
	}

	elmt := mapt.Elem()
	elmIsPtr := false
	//element is ptr
	if elmt.Kind() == reflect.Ptr{
		elmt = elmt.Elem()
		elmIsPtr = true
	}

	//map field => value
	idxs := make(map[string]int)
	for i := 0; i < elmt.NumField(); i++ {
		name := elmt.Field(i).Tag.Get("csv")
		if len(name) <= 0 {
			name = elmt.Field(i).Name
		}
		idxs[format(name)] = i
	}

	err = ReadRaw(file, isGbk, func(fields []Field) error {
		elmv := reflect.Indirect(reflect.New(elmt))
		keyi := 0
		isMatchKey := false
		for _, f := range fields {
			if len(f.Name) <= 0 {
				continue
			}
			idx, ok := idxs[format(f.Name)]
			if !ok {
				continue
			}
			if f.Name == keyField {
				keyi = idx
				isMatchKey = true
			}
			fValue := elmv.Field(idx)
			setValue(&fValue, f)
		}
		if !isMatchKey{
			return errors.New(fmt.Sprintf("Primary key not found, \"%v\" not has field name \"%v\"",file, keyField))
		}
		if elmIsPtr{
			mapv.SetMapIndex(elmv.Field(keyi), elmv.Addr())
		}else{
			mapv.SetMapIndex(elmv.Field(keyi), elmv)
		}
		return nil
	})

	return err
}
1262731
func (c *Connector) Read(ctx context.Context, ei *dosa.EntityInfo, keys map[string]dosa.FieldValue, fieldsToRead []string) (map[string]dosa.FieldValue, error) {
	err := validateSchema(ei)
	if err != nil {
		return nil, err
	}

	keyName, valueName := nameOfKeyValue(ei)

	cacheKey, err := buildKey(c.keyPrefix, ei.Ref.Scope, ei.Ref.NamePrefix, ei.Def.Name, keys[keyName])
	if err != nil {
		return nil, err
	}

	cacheValue, err := c.client.Get(cacheKey)
	c.logHitRate("Read", err)
	if err != nil {
		return nil, err
	}

	result := make(map[string]dosa.FieldValue)
	// Copy original keys into response
	for k, v := range keys {
		result[k] = v
	}
	result[valueName] = cacheValue
	return result, nil
}
1346605
func (kf *KeyFormat) KeyBytes(segments ...[]byte) []byte {
	key := make([]byte, kf.length)
	key[0] = kf.prefix
	n := 1
	for i, s := range segments {
		l := kf.layout[i]
		if len(s) > l {
			panic(fmt.Errorf("length of segment %X provided to KeyFormat.KeyBytes() is longer than the %d bytes "+
				"required by layout for segment %d", s, l, i))
		}
		n += l
		// Big endian so pad on left if not given the full width for this segment
		copy(key[n-len(s):n], s)
	}
	return key[:n]
}
1054816
func (f *FileReadSeeker) Read(p []byte) (int, error) {
	return f.file.Read(p)
}