tracker/server/bencode.go

58 lines
1.2 KiB
Go
Raw Normal View History

2013-07-12 06:36:24 +02:00
package server
import (
"fmt"
"io"
"strconv"
"time"
)
func writeBencoded(w io.Writer, data interface{}) {
switch v := data.(type) {
case string:
str := fmt.Sprintf("%s:%s", strconv.Itoa(len(v)), v)
io.WriteString(w, str)
2014-05-08 07:09:02 +02:00
2013-07-12 06:36:24 +02:00
case int:
str := fmt.Sprintf("i%se", strconv.Itoa(v))
io.WriteString(w, str)
2014-05-08 07:09:02 +02:00
2013-07-12 06:36:24 +02:00
case uint:
str := fmt.Sprintf("i%se", strconv.FormatUint(uint64(v), 10))
io.WriteString(w, str)
2014-05-08 07:09:02 +02:00
2013-07-12 06:36:24 +02:00
case int64:
str := fmt.Sprintf("i%se", strconv.FormatInt(v, 10))
io.WriteString(w, str)
2014-05-08 07:09:02 +02:00
2013-07-12 06:36:24 +02:00
case uint64:
str := fmt.Sprintf("i%se", strconv.FormatUint(v, 10))
io.WriteString(w, str)
2014-05-08 07:09:02 +02:00
2013-07-12 06:36:24 +02:00
case time.Duration: // Assume seconds
str := fmt.Sprintf("i%se", strconv.FormatInt(int64(v/time.Second), 10))
io.WriteString(w, str)
2014-05-08 07:09:02 +02:00
2013-07-12 06:36:24 +02:00
case map[string]interface{}:
io.WriteString(w, "d")
for key, val := range v {
str := fmt.Sprintf("%s:%s", strconv.Itoa(len(key)), key)
io.WriteString(w, str)
writeBencoded(w, val)
}
io.WriteString(w, "e")
2014-05-08 07:09:02 +02:00
2013-07-12 06:36:24 +02:00
case []string:
io.WriteString(w, "l")
for _, val := range v {
writeBencoded(w, val)
}
io.WriteString(w, "e")
2014-05-08 07:09:02 +02:00
2013-07-12 06:36:24 +02:00
default:
// Although not currently necessary,
// should handle []interface{} manually; Go can't do it implicitly
2014-05-08 07:09:02 +02:00
panic("tried to bencode an unsupported type!")
2013-07-12 06:36:24 +02:00
}
}