2013-11-05 08:25:17 +08:00
|
|
|
package sftp
|
|
|
|
|
2013-11-05 08:36:47 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2013-11-05 11:36:38 +08:00
|
|
|
"io"
|
2013-11-05 09:03:58 +08:00
|
|
|
"reflect"
|
2013-11-05 08:36:47 +08:00
|
|
|
)
|
|
|
|
|
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 {
|
2013-12-11 07:14:59 +08:00
|
|
|
return marshalUint32(marshalUint32(b, uint32(v>>32)), uint32(v))
|
2013-11-05 08:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-11-05 11:36:38 +08:00
|
|
|
case uint8:
|
|
|
|
return append(b, v)
|
2013-11-05 08:36:47 +08:00
|
|
|
case uint32:
|
|
|
|
return marshalUint32(b, v)
|
|
|
|
case uint64:
|
|
|
|
return marshalUint64(b, v)
|
|
|
|
case string:
|
|
|
|
return marshalString(b, v)
|
|
|
|
default:
|
2013-11-05 09:03:58 +08:00
|
|
|
switch d := reflect.ValueOf(v); d.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
for i, n := 0, d.NumField(); i < n; i++ {
|
|
|
|
b = append(marshal(b, d.Field(i).Interface()))
|
|
|
|
}
|
|
|
|
return b
|
2013-11-05 11:36:38 +08:00
|
|
|
case reflect.Slice:
|
|
|
|
for i, n := 0, d.Len(); i < n; i++ {
|
|
|
|
b = append(marshal(b, d.Index(i).Interface()))
|
|
|
|
}
|
|
|
|
return b
|
2013-11-05 09:03:58 +08:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("marshal(%#v): cannot handle type %T", v, v))
|
|
|
|
}
|
2013-11-05 08:36:47 +08:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 09:23:48 +08:00
|
|
|
|
|
|
|
func unmarshalUint32(b []byte) (uint32, []byte) {
|
|
|
|
v := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
|
|
|
|
return v, b[4:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalUint64(b []byte) (uint64, []byte) {
|
|
|
|
h, b := unmarshalUint32(b)
|
|
|
|
l, b := unmarshalUint32(b)
|
|
|
|
return uint64(h)<<32 | uint64(l), b
|
|
|
|
}
|
2013-11-05 09:31:20 +08:00
|
|
|
|
|
|
|
func unmarshalString(b []byte) (string, []byte) {
|
|
|
|
n, b := unmarshalUint32(b)
|
|
|
|
return string(b[:n]), b[n:]
|
|
|
|
}
|
2013-11-05 11:36:38 +08:00
|
|
|
|
|
|
|
// sendPacket marshals p according to RFC 4234.
|
|
|
|
func sendPacket(w io.Writer, p interface{}) error {
|
|
|
|
b := make([]byte, 4) // reserve space for the header
|
|
|
|
b = marshal(b, p)
|
|
|
|
l := uint32(len(b) - 4)
|
|
|
|
b[0], b[1], b[2], b[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
|
|
|
|
_, err := w.Write(b)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func recvPacket(r io.Reader) (uint8, []byte, error) {
|
|
|
|
var b = []byte{0, 0, 0, 0}
|
|
|
|
if _, err := io.ReadFull(r, b); err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
l, _ := unmarshalUint32(b)
|
|
|
|
b = make([]byte, l)
|
|
|
|
if _, err := io.ReadFull(r, b); err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
return b[0], b[1:], nil
|
|
|
|
}
|