forked from xdevplatform/chat-xdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffi_juicebox.go
More file actions
65 lines (56 loc) · 1.74 KB
/
Copy pathffi_juicebox.go
File metadata and controls
65 lines (56 loc) · 1.74 KB
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
//go:build !nojuicebox
package chatxdk
/*
#include "chat_xdk.h"
#include <stdlib.h>
#include <string.h>
*/
import "C"
import "unsafe"
// cPin copies PIN bytes into a NUL-terminated C buffer. The buffer holds
// secret material: release it with freePin, which wipes it before freeing,
// so the transient C copy does not linger on the heap.
func cPin(pin []byte) *C.char {
buf := C.malloc(C.size_t(len(pin) + 1))
if buf == nil {
panic("chatxdk: C.malloc failed")
}
if len(pin) > 0 {
C.memcpy(buf, unsafe.Pointer(&pin[0]), C.size_t(len(pin)))
}
*(*C.char)(unsafe.Pointer(uintptr(buf) + uintptr(len(pin)))) = 0
return (*C.char)(buf)
}
func freePin(p *C.char, n int) {
C.memset(unsafe.Pointer(p), 0, C.size_t(n))
C.free(unsafe.Pointer(p))
}
func ffiUpdateConfig(h handle, configJSON string) (string, error) {
cCfg := C.CString(configJSON)
defer C.free(unsafe.Pointer(cCfg))
return ffiResult(C.chat_xdk_update_config(h, cCfg))
}
func ffiSetup(h handle, pin []byte, configJSON string) (string, error) {
cPinBuf := cPin(pin)
defer freePin(cPinBuf, len(pin))
cCfg := C.CString(configJSON)
defer C.free(unsafe.Pointer(cCfg))
return ffiResult(C.chat_xdk_setup(h, cPinBuf, cCfg))
}
func ffiUnlock(h handle, pin []byte, configJSON string) (string, error) {
cPinBuf := cPin(pin)
defer freePin(cPinBuf, len(pin))
cCfg := C.CString(configJSON)
defer C.free(unsafe.Pointer(cCfg))
return ffiResult(C.chat_xdk_unlock(h, cPinBuf, cCfg))
}
func ffiDelete(h handle) (string, error) {
return ffiResult(C.chat_xdk_delete(h))
}
func ffiChangePin(h handle, oldPin, newPin []byte) (string, error) {
cOld := cPin(oldPin)
defer freePin(cOld, len(oldPin))
cNew := cPin(newPin)
defer freePin(cNew, len(newPin))
return ffiResult(C.chat_xdk_change_pin(h, cOld, cNew))
}