2010-01-06 15:38:33 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2010-01-07 04:11:33 +00:00
|
|
|
// Represents bencode data structure using native Go types: booleans, floats,
|
2011-12-17 16:35:20 +00:00
|
|
|
// strings, slices, and maps.
|
2010-01-06 15:38:33 +00:00
|
|
|
|
|
|
|
package bencode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Decode a bencode stream
|
|
|
|
|
|
|
|
// Decode parses the stream r and returns the
|
|
|
|
// generic bencode object representation. The object representation is a tree
|
|
|
|
// of Go data types. The data return value may be one of string,
|
2011-12-17 16:35:20 +00:00
|
|
|
// int64, uint64, []interface{} or map[string]interface{}. The slice and map
|
2010-01-06 15:38:33 +00:00
|
|
|
// elements may in turn contain any of the types listed above and so on.
|
|
|
|
//
|
|
|
|
// If Decode encounters a syntax error, it returns with err set to an
|
2012-09-16 02:00:31 +00:00
|
|
|
// instance of Error.
|
2011-12-17 16:35:20 +00:00
|
|
|
func Decode(r io.Reader) (data interface{}, err error) {
|
2010-01-06 15:38:33 +00:00
|
|
|
jb := newDecoder(nil, nil)
|
2012-09-16 02:00:31 +00:00
|
|
|
err = parse(r, jb)
|
2010-01-06 15:38:33 +00:00
|
|
|
if err == nil {
|
2010-07-21 10:28:50 +00:00
|
|
|
data = jb.Copy()
|
2010-01-06 15:38:33 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type decoder struct {
|
|
|
|
// A value being constructed.
|
|
|
|
value interface{}
|
2011-12-17 16:35:20 +00:00
|
|
|
// Container entity to flush into. Can be either []interface{} or
|
2010-01-06 15:38:33 +00:00
|
|
|
// map[string]interface{}.
|
|
|
|
container interface{}
|
|
|
|
// The index into the container interface. Either int or string.
|
|
|
|
index interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDecoder(container interface{}, key interface{}) *decoder {
|
|
|
|
return &decoder{container: container, index: key}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *decoder) Int64(i int64) { j.value = int64(i) }
|
|
|
|
|
|
|
|
func (j *decoder) Uint64(i uint64) { j.value = uint64(i) }
|
|
|
|
|
|
|
|
func (j *decoder) Float64(f float64) { j.value = float64(f) }
|
|
|
|
|
|
|
|
func (j *decoder) String(s string) { j.value = s }
|
|
|
|
|
|
|
|
func (j *decoder) Bool(b bool) { j.value = b }
|
|
|
|
|
|
|
|
func (j *decoder) Null() { j.value = nil }
|
|
|
|
|
2011-12-17 16:35:20 +00:00
|
|
|
func (j *decoder) Array() { j.value = make([]interface{}, 0, 8) }
|
2010-01-06 15:38:33 +00:00
|
|
|
|
|
|
|
func (j *decoder) Map() { j.value = make(map[string]interface{}) }
|
|
|
|
|
2012-09-16 02:46:44 +00:00
|
|
|
func (j *decoder) Elem(i int) builder {
|
2011-12-17 16:35:20 +00:00
|
|
|
v, ok := j.value.([]interface{})
|
2010-01-06 15:38:33 +00:00
|
|
|
if !ok {
|
2011-12-17 16:35:20 +00:00
|
|
|
v = make([]interface{}, 0, 8)
|
2010-01-06 15:38:33 +00:00
|
|
|
j.value = v
|
|
|
|
}
|
2012-09-03 00:45:23 +00:00
|
|
|
/* XXX There is a bug in here somewhere, but append() works fine.
|
2011-12-17 16:35:20 +00:00
|
|
|
lens := len(v)
|
|
|
|
if cap(v) <= lens {
|
|
|
|
news := make([]interface{}, 0, lens*2)
|
|
|
|
copy(news, j.value.([]interface{}))
|
|
|
|
v = news
|
2010-01-06 15:38:33 +00:00
|
|
|
}
|
2011-12-17 16:35:20 +00:00
|
|
|
v = v[0 : lens+1]
|
2012-09-03 00:45:23 +00:00
|
|
|
*/
|
|
|
|
v = append(v, nil)
|
2011-12-17 16:35:20 +00:00
|
|
|
j.value = v
|
2010-01-06 15:38:33 +00:00
|
|
|
return newDecoder(v, i)
|
|
|
|
}
|
|
|
|
|
2012-09-16 02:46:44 +00:00
|
|
|
func (j *decoder) Key(s string) builder {
|
2010-01-06 15:38:33 +00:00
|
|
|
m, ok := j.value.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
m = make(map[string]interface{})
|
|
|
|
j.value = m
|
|
|
|
}
|
|
|
|
return newDecoder(m, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *decoder) Flush() {
|
|
|
|
switch c := j.container.(type) {
|
2011-12-17 16:35:20 +00:00
|
|
|
case []interface{}:
|
2010-01-06 15:38:33 +00:00
|
|
|
index := j.index.(int)
|
2011-12-17 16:35:20 +00:00
|
|
|
c[index] = j.Copy()
|
2010-01-06 15:38:33 +00:00
|
|
|
case map[string]interface{}:
|
|
|
|
index := j.index.(string)
|
2010-07-21 10:28:50 +00:00
|
|
|
c[index] = j.Copy()
|
2010-01-06 15:38:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value built by this builder.
|
2010-07-21 10:28:50 +00:00
|
|
|
func (j *decoder) Copy() interface{} {
|
2010-01-06 15:38:33 +00:00
|
|
|
return j.value
|
|
|
|
}
|