-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
109 lines (97 loc) · 2.75 KB
/
convert.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package convc
/*
#include <stdlib.h>
*/
import "C"
import (
"reflect"
"unsafe"
)
// Directly convert a string to a byte slice.
// This is highly unsafe, do not modify the resulting slice!
func StringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
// Reverse of StringToBytes.
//
// Directly convert a byte slice to a string.
// This is highly unsafe, do not modify the original slice!
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// Convert the given string to a byte pointer.
// This is highly unsafe.
func StringToPointer(s string) *uint8 {
return &StringToBytes(s)[0]
}
// Convert the given byte slice to a byte pointer.
func BytesToPointer(b []byte) *uint8 {
return &b[0]
}
// Convert the given pointer and size to a string.
// This is highly unsafe.
func PointerToString(p *uint8, size int) string {
return *(*string)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(p)),
Len: size,
Cap: size,
}))
}
// Convert the given pointer and size to a byte slice.
// This is highly unsafe.
func PointerToBytes(p *uint8, size int) []byte {
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(p)),
Len: size,
Cap: size,
}))
}
// Malloc the given number of bytes.
// The resulting memory is not known to the Go garbage collector.
// Call the resulting free function to deallocate the allocated memory.
func Malloc(size int) (bytes []byte, free func()) {
ptr := (*uint8)(unsafe.Pointer(C.calloc(C.size_t(size), 1)))
return PointerToBytes(ptr, size), func() { C.free(unsafe.Pointer(ptr)) }
}
// Copy the given string to a newly Malloc'd C string.
func StringToC(s string) (p *uint8, free func()) {
a, f := Malloc(len(s) + 1)
copy(a, s)
return BytesToPointer(a), f
}
// Copy the given byte slice to a newly Malloc'd C string.
func BytesToC(b []byte) (p *uint8, free func()) {
a, f := Malloc(len(b) + 1)
copy(a, b)
return BytesToPointer(a), f
}
// Copy the given strings to a newly Malloc'd C buffer.
// It returns a list of nul-terminated C strings.
func MultiStringToC(ss ...string) (p **uint8, free func()) {
if len(ss) == 0 {
return nil, func() {}
}
// Allocate enough space for pointers and nul characters
var q *uint8
ptrsize := len(ss) * int(unsafe.Sizeof(q))
size := ptrsize + len(ss)
// And for all strings
for _, s := range ss {
size += len(s)
}
bytes, free := Malloc(size)
pointers := *(*[]*uint8)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(BytesToPointer(bytes))),
Len: len(ss),
Cap: len(ss),
}))
bytes = bytes[ptrsize:]
b := bytes
for i, s := range ss {
cur := b[:len(s)+1 : len(s)+1]
copy(cur, s)
pointers[i] = BytesToPointer(cur)
b = b[len(s)+1:]
}
return (**uint8)(unsafe.Pointer(&pointers[0])), free
}