forked from karfield/ssh2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
70 lines (58 loc) · 1.22 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package libssh
/*
#cgo pkg-config: libssh
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <libssh/libssh.h>
*/
import "C"
import (
"fmt"
"unsafe"
)
type SshString struct {
ptr C.ssh_string
}
func NewString(bytes int) SshString {
str := SshString{}
str.ptr = C.ssh_string_new(C.size_t(bytes))
return str
}
func NewStringFrom(ref string) SshString {
cstr := CString(ref)
defer cstr.Free()
str := SshString{}
str.ptr = C.ssh_string_from_char(cstr.Ptr)
return str
}
func (s SshString) Free() {
C.ssh_string_free(s.ptr)
}
func (s SshString) Destory() {
C.ssh_string_burn(s.ptr)
}
func (s SshString) String() string {
str := C.ssh_string_get_char(s.ptr)
if str == nil {
return ""
}
return C.GoString(str)
}
func (s SshString) Len() int {
return int(C.ssh_string_len(s.ptr))
}
func (s SshString) Duplicate() SshString {
return SshString{C.ssh_string_copy(s.ptr)}
}
func (s SshString) Copy(s2 SshString) error {
if ret := C.ssh_string_fill(s2.ptr, C.ssh_string_data(s.ptr), C.ssh_string_len(s.ptr)); ret < 0 {
return fmt.Errorf("ssh_string_fill() < %d", ret)
}
return nil
}
func (s SshString) Data() unsafe.Pointer {
return C.ssh_string_data(s.ptr)
}