@@ -5,18 +5,45 @@ package main
55//typedef struct { void* message; int size; char* error; } BytesReturn;
66import "C"
77import (
8- rsaBridge "github.com/jerson/rsa-mobile/bridge"
98 "unsafe"
9+
10+ "github.com/jerson/helpers-mobile/codecs"
11+
12+ rsaBridge "github.com/jerson/rsa-mobile/bridge"
1013)
1114
1215//export RSABridgeCall
1316func RSABridgeCall (name * C.char , payload unsafe.Pointer , payloadSize C.int ) * C.BytesReturn {
14- output := (* C .BytesReturn )(C .malloc (C .size_t (C .sizeof_BytesReturn )))
17+ result , err := rsaBridge .Call (C .GoString (name ), C .GoBytes (payload , payloadSize ))
18+ return createBytesReturn (result , err )
19+ }
20+
21+ //export RSAEncodeText
22+ func RSAEncodeText (input * C.char , encoding * C.char ) * C.BytesReturn {
23+ result , err := codecs .TextEncode (C .GoString (input ), C .GoString (encoding ))
24+ return createBytesReturn (result , err )
25+ }
26+
27+ //export RSADecodeText
28+ func RSADecodeText (input unsafe.Pointer , size C.int , encoding * C.char , fatal C.int , ignoreBOM C.int , stream C.int ) * C.char {
29+ inputBytes := C .GoBytes (input , size )
30+ options := codecs.TextDecoderOptions {Fatal : fatal != 0 , IgnoreBOM : ignoreBOM != 0 }
31+ decodeOptions := codecs.TextDecodeOptions {Stream : stream != 0 }
32+ result , err := codecs .TextDecode (inputBytes , C .GoString (encoding ), options , decodeOptions )
33+ if err != nil {
34+ return C .CString ("" )
35+ }
36+ return C .CString (result )
37+ }
38+
39+ func createBytesReturn (result []byte , err error ) * C.BytesReturn {
40+ output := (* C .BytesReturn )(C .malloc (C .size_t (unsafe .Sizeof (C.BytesReturn {}))))
1541 // we should free resources on dart side
1642
17- result , err := rsaBridge .Call (C .GoString (name ), C .GoBytes (payload , payloadSize ))
1843 if err != nil {
1944 output .error = C .CString (err .Error ())
45+ output .message = nil
46+ output .size = 0
2047 return output
2148 }
2249 output .error = nil
0 commit comments