Skip to content

Commit a06c244

Browse files
committed
add encoders y decoders for RN
1 parent a32b132 commit a06c244

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

binding/main.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,45 @@ package main
55
//typedef struct { void* message; int size; char* error; } BytesReturn;
66
import "C"
77
import (
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
1316
func 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

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
module github.com/jerson/rsa-mobile
22

3-
go 1.22.0
4-
5-
toolchain go1.23.3
3+
go 1.24.1
64

75
require (
86
github.com/google/flatbuffers v24.3.25+incompatible
7+
github.com/jerson/helpers-mobile v0.0.0-20250315011617-729f4804866f
98
github.com/lestrrat-go/jwx v1.2.30
109
github.com/stretchr/testify v1.10.0
1110
golang.org/x/crypto v0.29.0
@@ -23,5 +22,6 @@ require (
2322
github.com/lestrrat-go/option v1.0.1 // indirect
2423
github.com/pkg/errors v0.9.1 // indirect
2524
github.com/pmezard/go-difflib v1.0.0 // indirect
25+
golang.org/x/text v0.23.0 // indirect
2626
gopkg.in/yaml.v3 v3.0.1 // indirect
2727
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
77
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
88
github.com/google/flatbuffers v24.3.25+incompatible h1:CX395cjN9Kke9mmalRoL3d81AtFUxJM+yDthflgJGkI=
99
github.com/google/flatbuffers v24.3.25+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
10+
github.com/jerson/helpers-mobile v0.0.0-20250315011617-729f4804866f h1:UPhCZlAwu8U3USXLMDK0aSzqjSo6fMy7YE7VIPgmEQo=
11+
github.com/jerson/helpers-mobile v0.0.0-20250315011617-729f4804866f/go.mod h1:3SMtA29dJMJ1vROkFBxtL04KRg/8OXD0xfbDSQ4jLW0=
1012
github.com/lestrrat-go/backoff/v2 v2.0.8 h1:oNb5E5isby2kiro9AgdHLv5N5tint1AnDVVf2E2un5A=
1113
github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y=
1214
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
@@ -31,6 +33,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
3133
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3234
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
3335
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
36+
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
37+
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
3438
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3539
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3640
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)