// Extract the encoding from a Cid.  If Decode on the same string did
// not return an error neither will this function.
510225
func ExtractEncoding(v string) (mbase.Encoding, error) {
	if len(v) < 2 {
		return -1, ErrCidTooShort
	}

	if len(v) == 46 && v[:2] == "Qm" {
		return mbase.Base58BTC, nil
	}

	encoding := mbase.Encoding(v[0])

	// check encoding is valid
	_, err := mbase.NewEncoder(encoding)
	if err != nil {
		return -1, err
	}

	return encoding, nil
}
433866
func Decode(input interface{}, output interface{}) error {
	config := &DecoderConfig{
		Metadata: nil,
		Result:   output,
	}

	decoder, err := NewDecoder(config)
	if err != nil {
		return err
	}

	return decoder.Decode(input)
}
1265939
func (enc Encoding) DecodeString(s string) ([]byte, error) {
	if enc.convert {
		s = convertEncoding(s)
	}
	return enc.encoding.DecodeString(ensurePadding(s))
}
1246035
func ParseCIDR(cidrStr string) (string, uint, error) {
	strs := strings.Split(cidrStr, "/")
	if len(strs) != 2 {
		return "", 0, core.Errorf("invalid cidr format")
	}

	subnetStr := strs[0]
	subnetLen, err := strconv.Atoi(strs[1])
	if (IsIPv6(subnetStr) && subnetLen > 128) || err != nil || (!IsIPv6(subnetStr) && subnetLen > 32) {
		return "", 0, core.Errorf("invalid mask in gateway/mask specification ")
	}

	return subnetStr, uint(subnetLen), nil
}
336970
func (d *Decoder) Decode(v interface{}) (int, error) {
	if v == nil {
		msg := "can't unmarshal to nil interface"
		return 0, unmarshalError("Unmarshal", ErrNilInterface, msg, nil,
			nil)
	}

	vv := reflect.ValueOf(v)
	if vv.Kind() != reflect.Ptr {
		msg := fmt.Sprintf("can't unmarshal to non-pointer '%v' - use "+
			"& operator", vv.Type().String())
		err := unmarshalError("Unmarshal", ErrBadArguments, msg, nil, nil)
		return 0, err
	}
	if vv.IsNil() && !vv.CanSet() {
		msg := fmt.Sprintf("can't unmarshal to unsettable '%v' - use "+
			"& operator", vv.Type().String())
		err := unmarshalError("Unmarshal", ErrNotSettable, msg, nil, nil)
		return 0, err
	}

	return d.decode(vv)
}
22950
func extractIDFromPath(url string) (string, error) {
	result := reExtractFileID.FindStringSubmatch(url)
	if len(result) != 2 {
		return "", ErrNotFound
	}
	return result[1], nil
}
292332
func (e *encoding) decode(s string) uint64 {
	x := uint64(0)
	for i := 0; i < len(s); i++ {
		x = (x << 5) | uint64(e.dec[s[i]])
	}
	return x
}
367729
func Decode(s []byte, typ proto.Oid) interface{} {
	switch typ {
	case proto.OidText, proto.OidVarchar:
		return string(s)
	case proto.OidBytea:
		// N.B.: assumes hex bytea output
		s = s[2:] // trim off "\\x"
		d := make([]byte, hex.DecodedLen(len(s)))
		_, err := hex.Decode(d, s)
		if err != nil {
			log.Fatalf("femebe: %s", err)
		}
		return d
	case proto.OidTimestamp:
		return mustParse("2006-01-02 15:04:05", typ, s)
	case proto.OidTimestamptz:
		return mustParse("2006-01-02 15:04:05-07", typ, s)
	case proto.OidTime:
		return mustParse("15:04:05", typ, s)
	case proto.OidTimetz:
		return mustParse("15:04:05-07", typ, s)
	case proto.OidDate:
		return mustParse("2006-01-02", typ, s)
	case proto.OidBool:
		return s[0] == 't'
	case proto.OidInt8, proto.OidInt4, proto.OidInt2:
		i, err := strconv.ParseInt(string(s), 10, 64)
		if err != nil {
			log.Fatalf("femebe: %s", err)
		}
		return i
	case proto.OidFloat4, proto.OidFloat8:
		var bits int
		if typ == proto.OidFloat4 {
			bits = 32
		} else {
			bits = 64
		}
		f, err := strconv.ParseFloat(string(s), bits)
		if err != nil {
			log.Fatalf("femebe: %s", err)
		}
		return f
	default:
		return s
	}
}
707550
func GetCidEncoder(req *cmds.Request) (cidenc.Encoder, error) {
	return getCidBase(req, true)
}
413552
func IDHexDecode(s string) (ID, error) {
	m, err := mh.FromHexString(s)
	if err != nil {
		return "", err
	}
	return ID(m), err
}
132841
func (decoder StringDecoder) Decode(in *goetty.ByteBuf) (bool, interface{}, error) {
	_, data, err := in.ReadMarkedBytes()

	if err != nil {
		return true, "", err
	}

	return true, string(data), nil
}
318024
func (s *Logging) Encoding() Encoding {
	s.mutex.RLock()
	e := s.encoding
	s.mutex.RUnlock()
	return e
}
441053
func (s Latin1) Decode() []byte {
	e := charmap.Windows1252.NewDecoder()
	es, _, err := transform.Bytes(e, s)
	if err != nil {
		return s
	}
	return es
}
251202
func Decode(in string) ([]byte, error) {
	out, err := Client().Decode(in)
	Trace("Decode", err, logrus.Fields{"in": in, "out": out})
	return out, err
}
148387
func Decode(value string) (string, error) {
	value = value
	decoded := make([]byte, base64.URLEncoding.DecodedLen(len(value)))
	b, err := base64.URLEncoding.Decode(decoded, []byte(value))
	if err != nil {
		return "", err
	}
	return string(decoded[:b]), nil
}
500976
func DecodeString(data []byte, start int) (
	result interface{}, index int, err error) {

	if start >= len(data) || data[start] < '0' || data[start] > '9' {
		err = errors.New("invalid string bencode")
		return
	}

	i := find(data, start, ':')
	if i == -1 {
		err = errors.New("':' not found when decode string")
		return
	}

	length, err := strconv.Atoi(string(data[start:i]))
	if err != nil {
		return
	}

	if length < 0 {
		err = errors.New("invalid length of string")
		return
	}

	index = i + 1 + length

	if index > len(data) || index < i+1 {
		err = errors.New("out of range")
		return
	}

	result = string(data[i+1 : index])
	return
}
1319591
func Decode(in string) ([]byte, error) {
	o, err := b64.StdEncoding.DecodeString(in)
	if err != nil {
		// maybe it's in the URL variant?
		o, err = b64.URLEncoding.DecodeString(in)
		if err != nil {
			// ok, just give up...
			return nil, err
		}
	}
	return o, nil
}