2013-11-05 08:25:17 +08:00
|
|
|
package sftp
|
|
|
|
|
2013-11-05 08:36:47 +08:00
|
|
|
import (
|
|
|
|
// "reflect"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2013-11-05 08:25:17 +08:00
|
|
|
func marshalUint32(b []byte, v uint32) []byte {
|
|
|
|
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalUint64(b []byte, v uint64) []byte {
|
|
|
|
return marshalUint32(marshalUint32(b, uint32(v>>24)), uint32(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalString(b []byte, v string) []byte {
|
|
|
|
return append(marshalUint32(b, uint32(len(v))), v...)
|
|
|
|
}
|
2013-11-05 08:36:47 +08:00
|
|
|
|
|
|
|
func marshal(b []byte, v interface{}) []byte {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case uint32:
|
|
|
|
return marshalUint32(b, v)
|
|
|
|
case uint64:
|
|
|
|
return marshalUint64(b, v)
|
|
|
|
case string:
|
|
|
|
return marshalString(b, v)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("marshal(%#v): cannot handle type %T", v, v))
|
|
|
|
}
|
|
|
|
}
|