// Composes multiple DecodeHookFuncs into one. The hooks are applied in-order
// and values produced by a hook are fed into the next hook.
541816
func composeDecodeHooks(hooks []DecodeHookFunc) DecodeHookFunc {
	return func(from, to reflect.Type, data reflect.Value) (reflect.Value, error) {
		var err error
		for _, hook := range hooks {
			data, err = hook(from, to, data)
			if err != nil {
				return data, err
			}

			// Update the `from` type to reflect changes made by the hook.
			from = data.Type()
		}
		return data, err
	}
}
1167437
func timeHookFunc(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
	// Decode time.Time
	if to == timeType {
		var timeInt int64

		switch from.Kind() {
		case reflect.Int:
			timeInt = int64(data.(int))
		case reflect.String:
			var err error
			timeInt, err = strconv.ParseInt(data.(string), 10, 64)
			if err != nil {
				return nil, fmt.Errorf("invalid time %q: %v", data, err)
			}
		}

		if timeInt > 0 {
			return time.Unix(timeInt, 0), nil
		}
	}

	return data, nil
}
613830
func customDecodeHook() mapstructure.DecodeHookFunc {
	durationHook := mapstructure.StringToTimeDurationHookFunc()
	return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
		dur, err := mapstructure.DecodeHookExec(durationHook, f, t, data)
		if err == nil {
			if _, ok := dur.(time.Duration); ok {
				return dur, nil
			}
		}

		if f.Kind() != reflect.String {
			return data, nil
		}

		raw := data.(string)
		l := len(raw)
		if l > 1 && raw[0] == '[' && raw[l-1] == ']' {
			slice := strings.Split(raw[1:l-1], ",")
			for i, v := range slice {
				slice[i] = strings.TrimSpace(v)
			}
			return slice, nil
		}

		return data, nil
	}
}
620410
func DecodeHandle(data []byte) (int64, error) {
	var h int64
	buf := bytes.NewBuffer(data)
	err := binary.Read(buf, binary.BigEndian, &h)
	return h, err
}
716300
func StringToTimeHookFunc(layout string) DecodeHookFunc {
	return func(
		f reflect.Type,
		t reflect.Type,
		data interface{}) (interface{}, error) {
		if f.Kind() != reflect.String {
			return data, nil
		}
		if t != reflect.TypeOf(time.Time{}) {
			return data, nil
		}

		// Convert it by parsing
		return time.Parse(layout, data.(string))
	}
}
1038446
func unmarshalerHook(opts *options) DecodeHookFunc {
	return func(from, to reflect.Type, data reflect.Value) (reflect.Value, error) {
		if from == to {
			return data, nil
		}

		if !reflect.PtrTo(to).Implements(opts.Unmarshaler.Interface) {
			return data, nil
		}

		// The following lines are roughly equivalent to,
		// 	value := new(foo)
		// 	err := value.Decode(...)
		// 	return *value, err
		value := reflect.New(to)
		err := opts.Unmarshaler.Unmarshal(value, decodeFrom(opts, data.Interface()))
		if err != nil {
			err = fmt.Errorf("could not decode %v from %v: %v", to, from, err)
		}
		return value.Elem(), err
	}
}
22019
func ValueFunc(ctx context.Context, label string) Func {
	if metadata := ctx.Value(key{}); metadata != nil {
		if m, ok := metadata.(md); ok {
			return m[label]
		}
	}
	return nil
}
489952
func decodeSkipNoRefs(p *advRefsDecoder) decoderStateFn {
	if len(p.line) < len(noHeadMark) {
		p.error("too short zero-id ref")
		return nil
	}

	if !bytes.HasPrefix(p.line, noHeadMark) {
		p.error("malformed zero-id ref")
		return nil
	}

	p.line = p.line[len(noHeadMark):]

	return decodeCaps
}
91852
func (d *WordDecoder) DecodeHeader(header string) (string, error) {
	// If there is no encoded-word, returns before creating a buffer.
	i := strings.Index(header, "=?")
	if i == -1 {
		return header, nil
	}

	buf := getBuffer()
	defer putBuffer(buf)

	buf.WriteString(header[:i])
	header = header[i:]

	betweenWords := false
	for {
		start := strings.Index(header, "=?")
		if start == -1 {
			break
		}
		cur := start + len("=?")

		i := strings.Index(header[cur:], "?")
		if i == -1 {
			break
		}
		charset := header[cur : cur+i]
		cur += i + len("?")

		if len(header) < cur+len("Q??=") {
			break
		}
		encoding := header[cur]
		cur++

		if header[cur] != '?' {
			break
		}
		cur++

		j := strings.Index(header[cur:], "?=")
		if j == -1 {
			break
		}
		text := header[cur : cur+j]
		end := cur + j + len("?=")

		content, err := decode(encoding, text)
		if err != nil {
			betweenWords = false
			buf.WriteString(header[:start+2])
			header = header[start+2:]
			continue
		}

		// Write characters before the encoded-word. White-space and newline
		// characters separating two encoded-words must be deleted.
		if start > 0 && (!betweenWords || hasNonWhitespace(header[:start])) {
			buf.WriteString(header[:start])
		}

		if err := d.convert(buf, charset, content); err != nil {
			return "", err
		}

		header = header[end:]
		betweenWords = true
	}

	if len(header) > 0 {
		buf.WriteString(header)
	}

	return buf.String(), nil
}
1226125
func stringToReadableDurationFunc() mapstructure.DecodeHookFunc {
	return func(
		f reflect.Type,
		t reflect.Type,
		data interface{}) (interface{}, error) {
		var v api.ReadableDuration
		if t != reflect.TypeOf(v) {
			return data, nil
		}

		switch {
		case f.Kind() == reflect.String:
			if dur, err := time.ParseDuration(data.(string)); err != nil {
				return nil, err
			} else {
				v = api.ReadableDuration(dur)
			}
			return v, nil
		default:
			return data, nil
		}
	}
}
1203610
func (m *Method) FinalFunc(f FinalFunc) *Method {
	m.finalFuncs = append(m.finalFuncs, f)
	return m
}
1015954
func (d *Decoder) decodeHeader() error {
	header, err := d.findHeader()
	if err != nil {
		return err
	}

	d.layer = 4 - (header>>17)&3
	d.protectionBit = (header >> 16) & 1
	d.bitrateIndex = (header >> 12) & 15
	d.samplingFreq = (header >> 10) & 3
	d.paddingBit = (header >> 9) & 1
	// private_bit is ignored.
	d.mode = (header >> 6) & 3
	d.modeExtension = (header >> 4) & 3
	d.copyright = (header >> 3) & 1
	d.original = (header >> 2) & 1
	d.emphasis = header & 3

	if d.mode == ModeMono {
		d.nChannels = 1
	} else {
		d.nChannels = 2
	}

	return nil
}
940501
func timeDurationToStringHookFunc() mapstructure.DecodeHookFunc {
	return func(
		f reflect.Type,
		t reflect.Type,
		data interface{}) (interface{}, error) {
		dur, ok := data.(time.Duration)
		if !ok {
			return data, nil
		}
		if t.Kind() != reflect.String {
			return data, nil
		}
		if dur == 0 {
			return "", nil
		}

		// Convert it by parsing
		return data.(time.Duration).String(), nil
	}
}
496690
func (g *Group) StructFunc(f func(*Group)) *Statement {
	s := StructFunc(f)
	g.items = append(g.items, s)
	return s
}
1334388
func createValueFuncs(funcMap FuncMap) map[string]reflect.Value {
	m := make(map[string]reflect.Value)
	addValueFuncs(m, funcMap)
	return m
}
1128166
func (d *Decoder) decodeHuffmanData3(gr, ch int) error {
	var (
		regions [3]int
		N       = 0
		bits    = d.part23Length[gr][ch] - d.part2Length[gr][ch]
	)

	// Compute the region boundaries in the big_values partition
	if d.windowSwitchingFlag[gr][ch] == 0 {
		regions = [3]int{
			d.region0Count[gr][ch] + 1,
			d.region0Count[gr][ch] + d.region1Count[gr][ch] + 2,
			2 * d.bigValues[gr][ch],
		}
		if regions[1] > 22 {
			return MalformedStream("region0_count + region1_count > 20")
		}
		regions[0] = scfBandsL[d.samplingFreq][regions[0]]
		regions[1] = scfBandsL[d.samplingFreq][regions[1]]
	} else {
		regions = [3]int{36, 576, 2 * d.bigValues[gr][ch]}
	}
	if regions[2] > 576 {
		return MalformedStream("big_values too large")
	}
	if regions[1] > regions[2] {
		regions[1] = regions[2]
	}
	if regions[0] > regions[1] {
		regions[0] = regions[1]
	}

	// Decode the big_values partition
	for r := 0; r < 3; r++ {
		htree := huffmanTrees[d.tableSelect[gr][ch][r]]
		lbits := linbits[d.tableSelect[gr][ch][r]]

		if htree == nil {
			if d.tableSelect[gr][ch][r] != 0 {
				return MalformedStream("invalid table_select")
			}
			for ; N < regions[r]; N++ {
				d.huffmanData[gr][ch][N] = 0
			}
			continue
		}

		for ; N < regions[r]; N += 2 {
			code, err := d.reservoir.readCode(htree)
			if err != nil {
				return err
			}
			bits -= code >> 16

			for i := 0; i < 2; i++ {
				value := (code >> uint(8-8*i)) & 0xff
				if value == 15 {
					lin, err := d.reservoir.readBits(lbits)
					if err != nil {
						return err
					}
					value += lin
					bits -= lbits
				}
				// We perform the first step of dequantization here, so we don't
				// need two separate buffers for the Huffman coded integers and
				// the dequantized floats.
				valueFl := pow43[value]
				if value != 0 {
					sign, err := d.reservoir.readBits(1)
					if err != nil {
						return err
					}
					if sign == 1 {
						valueFl = -valueFl
					}
				}
				d.huffmanData[gr][ch][N+i] = valueFl
			}
		}
	}

	// Decode the count1 partition
	htree := htreeA
	if d.count1TableSelect[gr][ch] == 1 {
		htree = htreeB
	}

	// To avoid overindexing huffmanData, we quit the loop when N > 572, i.e.
	// when there's no room for another 4 values.
	for ; bits > 0 && N <= 572; N += 4 {
		code, err := d.reservoir.readCode(htree)
		if err != nil {
			return err
		}
		bits -= code >> 8

		for i := 0; i < 4; i++ {
			value := (code >> uint(3-i)) & 1
			valueFl := float32(value)
			if value != 0 {
				sign, err := d.reservoir.readBits(1)
				if err != nil {
					return err
				}
				if sign == 1 {
					valueFl = -1
				}
			}
			d.huffmanData[gr][ch][N+i] = valueFl
		}
	}

	// rzero
	for ; N < 576; N++ {
		d.huffmanData[gr][ch][N] = 0
	}

	// Skip over any stuffing. We don't need these bits, so we don't care much
	// if the read is successful or not.
	if bits < 0 {
		return MalformedStream("Huffman data overread")
	}
	d.reservoir.readBits(bits)

	return nil
}
253415
func (z *ZipArchive) ExtractToFunc(destPath string, fn cae.HookFunc, entries ...string) (err error) {
	destPath = strings.Replace(destPath, "\\", "/", -1)
	isHasEntry := len(entries) > 0
	if Verbose {
		fmt.Println("Unzipping " + z.FileName + "...")
	}
	os.MkdirAll(destPath, os.ModePerm)
	for _, f := range z.File {
		f.Name = strings.Replace(f.Name, "\\", "/", -1)

		// Directory.
		if strings.HasSuffix(f.Name, "/") {
			if isHasEntry {
				if cae.IsEntry(f.Name, entries) {
					if err = fn(f.Name, f.FileInfo()); err != nil {
						continue
					}
					os.MkdirAll(path.Join(destPath, f.Name), os.ModePerm)
				}
				continue
			}
			if err = fn(f.Name, f.FileInfo()); err != nil {
				continue
			}
			os.MkdirAll(path.Join(destPath, f.Name), os.ModePerm)
			continue
		}

		// File.
		if isHasEntry {
			if cae.IsEntry(f.Name, entries) {
				if err = fn(f.Name, f.FileInfo()); err != nil {
					continue
				}
				err = extractFile(f, destPath)
			}
		} else {
			if err = fn(f.Name, f.FileInfo()); err != nil {
				continue
			}
			err = extractFile(f, destPath)
		}
		if err != nil {
			return err
		}
	}
	return nil
}