mirror of
https://github.com/mautrix/signal.git
synced 2025-03-14 14:15:36 +00:00
Use uintptrs for store contexts
This commit is contained in:
parent
717e58cbd0
commit
a3cb6c3791
11 changed files with 74 additions and 56 deletions
1
go.mod
1
go.mod
|
@ -7,7 +7,6 @@ require (
|
|||
github.com/google/uuid v1.5.0
|
||||
github.com/gorilla/mux v1.8.0
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/mattn/go-pointer v0.0.1
|
||||
github.com/mattn/go-sqlite3 v1.14.19
|
||||
github.com/prometheus/client_golang v1.18.0
|
||||
github.com/rs/zerolog v1.31.0
|
||||
|
|
2
go.sum
2
go.sum
|
@ -28,8 +28,6 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
|
|||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
|
||||
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
|
||||
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
|
||||
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
|
||||
|
|
|
@ -23,11 +23,11 @@ package libsignalgo
|
|||
typedef const SignalProtocolAddress const_address;
|
||||
typedef const SignalPublicKey const_public_key;
|
||||
|
||||
extern int signal_get_identity_key_pair_callback(void *store_ctx, SignalPrivateKey **keyp);
|
||||
extern int signal_get_local_registration_id_callback(void *store_ctx, uint32_t *idp);
|
||||
extern int signal_save_identity_key_callback(void *store_ctx, const_address *address, const_public_key *public_key);
|
||||
extern int signal_get_identity_key_callback(void *store_ctx, SignalPublicKey **public_keyp, const_address *address);
|
||||
extern int signal_is_trusted_identity_callback(void *store_ctx, const_address *address, const_public_key *public_key, unsigned int direction);
|
||||
extern int signal_get_identity_key_pair_callback(uintptr_t store_ctx, SignalPrivateKey **keyp);
|
||||
extern int signal_get_local_registration_id_callback(uintptr_t store_ctx, uint32_t *idp);
|
||||
extern int signal_save_identity_key_callback(uintptr_t store_ctx, const_address *address, const_public_key *public_key);
|
||||
extern int signal_get_identity_key_callback(uintptr_t store_ctx, SignalPublicKey **public_keyp, const_address *address);
|
||||
extern int signal_is_trusted_identity_callback(uintptr_t store_ctx, const_address *address, const_public_key *public_key, unsigned int direction);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
@ -51,7 +51,7 @@ type IdentityKeyStore interface {
|
|||
}
|
||||
|
||||
//export signal_get_identity_key_pair_callback
|
||||
func signal_get_identity_key_pair_callback(storeCtx unsafe.Pointer, keyp **C.SignalPrivateKey) C.int {
|
||||
func signal_get_identity_key_pair_callback(storeCtx uintptr, keyp **C.SignalPrivateKey) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store IdentityKeyStore, ctx context.Context) error {
|
||||
key, err := store.GetIdentityKeyPair(ctx)
|
||||
if err != nil {
|
||||
|
@ -72,7 +72,7 @@ func signal_get_identity_key_pair_callback(storeCtx unsafe.Pointer, keyp **C.Sig
|
|||
}
|
||||
|
||||
//export signal_get_local_registration_id_callback
|
||||
func signal_get_local_registration_id_callback(storeCtx unsafe.Pointer, idp *C.uint32_t) C.int {
|
||||
func signal_get_local_registration_id_callback(storeCtx uintptr, idp *C.uint32_t) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store IdentityKeyStore, ctx context.Context) error {
|
||||
registrationID, err := store.GetLocalRegistrationID(ctx)
|
||||
if err == nil {
|
||||
|
@ -83,7 +83,7 @@ func signal_get_local_registration_id_callback(storeCtx unsafe.Pointer, idp *C.u
|
|||
}
|
||||
|
||||
//export signal_save_identity_key_callback
|
||||
func signal_save_identity_key_callback(storeCtx unsafe.Pointer, address *C.const_address, publicKey *C.const_public_key) C.int {
|
||||
func signal_save_identity_key_callback(storeCtx uintptr, address *C.const_address, publicKey *C.const_public_key) C.int {
|
||||
return wrapStoreCallbackCustomReturn(storeCtx, func(store IdentityKeyStore, ctx context.Context) (int, error) {
|
||||
publicKeyStruct := PublicKey{ptr: (*C.SignalPublicKey)(unsafe.Pointer(publicKey))}
|
||||
cloned, err := publicKeyStruct.Clone()
|
||||
|
@ -107,7 +107,7 @@ func signal_save_identity_key_callback(storeCtx unsafe.Pointer, address *C.const
|
|||
}
|
||||
|
||||
//export signal_get_identity_key_callback
|
||||
func signal_get_identity_key_callback(storeCtx unsafe.Pointer, public_keyp **C.SignalPublicKey, address *C.const_address) C.int {
|
||||
func signal_get_identity_key_callback(storeCtx uintptr, public_keyp **C.SignalPublicKey, address *C.const_address) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store IdentityKeyStore, ctx context.Context) error {
|
||||
key, err := store.GetIdentityKey(ctx, &Address{ptr: (*C.SignalProtocolAddress)(unsafe.Pointer(address))})
|
||||
if err == nil && key != nil {
|
||||
|
@ -119,7 +119,7 @@ func signal_get_identity_key_callback(storeCtx unsafe.Pointer, public_keyp **C.S
|
|||
}
|
||||
|
||||
//export signal_is_trusted_identity_callback
|
||||
func signal_is_trusted_identity_callback(storeCtx unsafe.Pointer, address *C.const_address, public_key *C.const_public_key, direction C.uint) C.int {
|
||||
func signal_is_trusted_identity_callback(storeCtx uintptr, address *C.const_address, public_key *C.const_public_key, direction C.uint) C.int {
|
||||
return wrapStoreCallbackCustomReturn(storeCtx, func(store IdentityKeyStore, ctx context.Context) (int, error) {
|
||||
trusted, err := store.IsTrustedIdentity(ctx, &Address{ptr: (*C.SignalProtocolAddress)(unsafe.Pointer(address))}, &IdentityKey{&PublicKey{ptr: (*C.SignalPublicKey)(unsafe.Pointer(public_key))}}, SignalDirection(direction))
|
||||
if err != nil {
|
||||
|
|
|
@ -22,9 +22,9 @@ package libsignalgo
|
|||
|
||||
typedef const SignalKyberPreKeyRecord const_kyber_pre_key_record;
|
||||
|
||||
extern int signal_load_kyber_pre_key_callback(void *store_ctx, SignalKyberPreKeyRecord **recordp, uint32_t id);
|
||||
extern int signal_store_kyber_pre_key_callback(void *store_ctx, uint32_t id, const_kyber_pre_key_record *record);
|
||||
extern int signal_mark_kyber_pre_key_used_callback(void *store_ctx, uint32_t id);
|
||||
extern int signal_load_kyber_pre_key_callback(uintptr_t store_ctx, SignalKyberPreKeyRecord **recordp, uint32_t id);
|
||||
extern int signal_store_kyber_pre_key_callback(uintptr_t store_ctx, uint32_t id, const_kyber_pre_key_record *record);
|
||||
extern int signal_mark_kyber_pre_key_used_callback(uintptr_t store_ctx, uint32_t id);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
@ -39,7 +39,7 @@ type KyberPreKeyStore interface {
|
|||
}
|
||||
|
||||
//export signal_load_kyber_pre_key_callback
|
||||
func signal_load_kyber_pre_key_callback(storeCtx unsafe.Pointer, keyp **C.SignalKyberPreKeyRecord, id C.uint32_t) C.int {
|
||||
func signal_load_kyber_pre_key_callback(storeCtx uintptr, keyp **C.SignalKyberPreKeyRecord, id C.uint32_t) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store KyberPreKeyStore, ctx context.Context) error {
|
||||
key, err := store.LoadKyberPreKey(ctx, uint32(id))
|
||||
if err == nil && key != nil {
|
||||
|
@ -51,7 +51,7 @@ func signal_load_kyber_pre_key_callback(storeCtx unsafe.Pointer, keyp **C.Signal
|
|||
}
|
||||
|
||||
//export signal_store_kyber_pre_key_callback
|
||||
func signal_store_kyber_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t, preKeyRecord *C.const_kyber_pre_key_record) C.int {
|
||||
func signal_store_kyber_pre_key_callback(storeCtx uintptr, id C.uint32_t, preKeyRecord *C.const_kyber_pre_key_record) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store KyberPreKeyStore, ctx context.Context) error {
|
||||
record := KyberPreKeyRecord{ptr: (*C.SignalKyberPreKeyRecord)(unsafe.Pointer(preKeyRecord))}
|
||||
cloned, err := record.Clone()
|
||||
|
@ -63,7 +63,7 @@ func signal_store_kyber_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t,
|
|||
}
|
||||
|
||||
//export signal_mark_kyber_pre_key_used_callback
|
||||
func signal_mark_kyber_pre_key_used_callback(storeCtx unsafe.Pointer, id C.uint32_t) C.int {
|
||||
func signal_mark_kyber_pre_key_used_callback(storeCtx uintptr, id C.uint32_t) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store KyberPreKeyStore, ctx context.Context) error {
|
||||
err := store.MarkKyberPreKeyUsed(ctx, uint32(id))
|
||||
return err
|
||||
|
|
|
@ -277,7 +277,7 @@ typedef int (*SignalLoadSession)(void *store_ctx, SignalSessionRecord **recordp,
|
|||
typedef int (*SignalStoreSession)(void *store_ctx, const SignalProtocolAddress *address, const SignalSessionRecord *record);
|
||||
|
||||
typedef struct {
|
||||
void *ctx;
|
||||
uintptr_t ctx;
|
||||
SignalLoadSession load_session;
|
||||
SignalStoreSession store_session;
|
||||
} SignalSessionStore;
|
||||
|
@ -293,7 +293,7 @@ typedef int (*SignalGetIdentityKey)(void *store_ctx, SignalPublicKey **public_ke
|
|||
typedef int (*SignalIsTrustedIdentity)(void *store_ctx, const SignalProtocolAddress *address, const SignalPublicKey *public_key, unsigned int direction);
|
||||
|
||||
typedef struct {
|
||||
void *ctx;
|
||||
uintptr_t ctx;
|
||||
SignalGetIdentityKeyPair get_identity_key_pair;
|
||||
SignalGetLocalRegistrationId get_local_registration_id;
|
||||
SignalSaveIdentityKey save_identity;
|
||||
|
@ -308,7 +308,7 @@ typedef int (*SignalStorePreKey)(void *store_ctx, uint32_t id, const SignalPreKe
|
|||
typedef int (*SignalRemovePreKey)(void *store_ctx, uint32_t id);
|
||||
|
||||
typedef struct {
|
||||
void *ctx;
|
||||
uintptr_t ctx;
|
||||
SignalLoadPreKey load_pre_key;
|
||||
SignalStorePreKey store_pre_key;
|
||||
SignalRemovePreKey remove_pre_key;
|
||||
|
@ -319,7 +319,7 @@ typedef int (*SignalLoadSignedPreKey)(void *store_ctx, SignalSignedPreKeyRecord
|
|||
typedef int (*SignalStoreSignedPreKey)(void *store_ctx, uint32_t id, const SignalSignedPreKeyRecord *record);
|
||||
|
||||
typedef struct {
|
||||
void *ctx;
|
||||
uintptr_t ctx;
|
||||
SignalLoadSignedPreKey load_signed_pre_key;
|
||||
SignalStoreSignedPreKey store_signed_pre_key;
|
||||
} SignalSignedPreKeyStore;
|
||||
|
@ -366,7 +366,7 @@ typedef int (*SignalStoreKyberPreKey)(void *store_ctx, uint32_t id, const Signal
|
|||
typedef int (*SignalMarkKyberPreKeyUsed)(void *store_ctx, uint32_t id);
|
||||
|
||||
typedef struct {
|
||||
void *ctx;
|
||||
uintptr_t ctx;
|
||||
SignalLoadKyberPreKey load_kyber_pre_key;
|
||||
SignalStoreKyberPreKey store_kyber_pre_key;
|
||||
SignalMarkKyberPreKeyUsed mark_kyber_pre_key_used;
|
||||
|
@ -387,7 +387,7 @@ typedef int (*SignalLoadSenderKey)(void *store_ctx, SignalSenderKeyRecord**, con
|
|||
typedef int (*SignalStoreSenderKey)(void *store_ctx, const SignalProtocolAddress*, const uint8_t (*distribution_id)[16], const SignalSenderKeyRecord*);
|
||||
|
||||
typedef struct {
|
||||
void *ctx;
|
||||
uintptr_t ctx;
|
||||
SignalLoadSenderKey load_sender_key;
|
||||
SignalStoreSenderKey store_sender_key;
|
||||
} SignalSenderKeyStore;
|
||||
|
@ -397,7 +397,7 @@ typedef int (*SignalRead)(void *ctx, uint8_t *buf, uintptr_t buf_len, uintptr_t
|
|||
typedef int (*SignalSkip)(void *ctx, uint64_t amount);
|
||||
|
||||
typedef struct {
|
||||
void *ctx;
|
||||
uintptr_t ctx;
|
||||
SignalRead read;
|
||||
SignalSkip skip;
|
||||
} SignalInputStream;
|
||||
|
|
|
@ -22,9 +22,9 @@ package libsignalgo
|
|||
|
||||
typedef const SignalPreKeyRecord const_pre_key_record;
|
||||
|
||||
extern int signal_load_pre_key_callback(void *store_ctx, SignalPreKeyRecord **recordp, uint32_t id);
|
||||
extern int signal_store_pre_key_callback(void *store_ctx, uint32_t id, const_pre_key_record *record);
|
||||
extern int signal_remove_pre_key_callback(void *store_ctx, uint32_t id);
|
||||
extern int signal_load_pre_key_callback(uintptr_t store_ctx, SignalPreKeyRecord **recordp, uint32_t id);
|
||||
extern int signal_store_pre_key_callback(uintptr_t store_ctx, uint32_t id, const_pre_key_record *record);
|
||||
extern int signal_remove_pre_key_callback(uintptr_t store_ctx, uint32_t id);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
@ -39,7 +39,7 @@ type PreKeyStore interface {
|
|||
}
|
||||
|
||||
//export signal_load_pre_key_callback
|
||||
func signal_load_pre_key_callback(storeCtx unsafe.Pointer, keyp **C.SignalPreKeyRecord, id C.uint32_t) C.int {
|
||||
func signal_load_pre_key_callback(storeCtx uintptr, keyp **C.SignalPreKeyRecord, id C.uint32_t) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store PreKeyStore, ctx context.Context) error {
|
||||
key, err := store.LoadPreKey(ctx, uint32(id))
|
||||
if err == nil && key != nil {
|
||||
|
@ -51,7 +51,7 @@ func signal_load_pre_key_callback(storeCtx unsafe.Pointer, keyp **C.SignalPreKey
|
|||
}
|
||||
|
||||
//export signal_store_pre_key_callback
|
||||
func signal_store_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t, preKeyRecord *C.const_pre_key_record) C.int {
|
||||
func signal_store_pre_key_callback(storeCtx uintptr, id C.uint32_t, preKeyRecord *C.const_pre_key_record) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store PreKeyStore, ctx context.Context) error {
|
||||
record := PreKeyRecord{ptr: (*C.SignalPreKeyRecord)(unsafe.Pointer(preKeyRecord))}
|
||||
cloned, err := record.Clone()
|
||||
|
@ -63,7 +63,7 @@ func signal_store_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t, preKe
|
|||
}
|
||||
|
||||
//export signal_remove_pre_key_callback
|
||||
func signal_remove_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t) C.int {
|
||||
func signal_remove_pre_key_callback(storeCtx uintptr, id C.uint32_t) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store PreKeyStore, ctx context.Context) error {
|
||||
return store.RemovePreKey(ctx, uint32(id))
|
||||
})
|
||||
|
|
|
@ -25,8 +25,8 @@ typedef const SignalProtocolAddress const_address;
|
|||
typedef const SignalSenderKeyRecord const_sender_key_record;
|
||||
typedef const uint8_t const_uuid_bytes[16];
|
||||
|
||||
extern int signal_load_sender_key_callback(void *store_ctx, SignalSenderKeyRecord**, const_address*, const_uuid_bytes*);
|
||||
extern int signal_store_sender_key_callback(void *store_ctx, const_address*, const_uuid_bytes*, const_sender_key_record*);
|
||||
extern int signal_load_sender_key_callback(uintptr_t store_ctx, SignalSenderKeyRecord**, const_address*, const_uuid_bytes*);
|
||||
extern int signal_store_sender_key_callback(uintptr_t store_ctx, const_address*, const_uuid_bytes*, const_sender_key_record*);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
@ -42,7 +42,7 @@ type SenderKeyStore interface {
|
|||
}
|
||||
|
||||
//export signal_load_sender_key_callback
|
||||
func signal_load_sender_key_callback(storeCtx unsafe.Pointer, recordp **C.SignalSenderKeyRecord, address *C.const_address, distributionIDBytes *C.const_uuid_bytes) C.int {
|
||||
func signal_load_sender_key_callback(storeCtx uintptr, recordp **C.SignalSenderKeyRecord, address *C.const_address, distributionIDBytes *C.const_uuid_bytes) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store SenderKeyStore, ctx context.Context) error {
|
||||
distributionID := uuid.UUID(*(*[16]byte)(unsafe.Pointer(distributionIDBytes)))
|
||||
record, err := store.LoadSenderKey(ctx, &Address{ptr: (*C.SignalProtocolAddress)(unsafe.Pointer(address))}, distributionID)
|
||||
|
@ -55,7 +55,7 @@ func signal_load_sender_key_callback(storeCtx unsafe.Pointer, recordp **C.Signal
|
|||
}
|
||||
|
||||
//export signal_store_sender_key_callback
|
||||
func signal_store_sender_key_callback(storeCtx unsafe.Pointer, address *C.const_address, distributionIDBytes *C.const_uuid_bytes, senderKeyRecord *C.const_sender_key_record) C.int {
|
||||
func signal_store_sender_key_callback(storeCtx uintptr, address *C.const_address, distributionIDBytes *C.const_uuid_bytes, senderKeyRecord *C.const_sender_key_record) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store SenderKeyStore, ctx context.Context) error {
|
||||
distributionID := uuid.UUID(*(*[16]byte)(unsafe.Pointer(distributionIDBytes)))
|
||||
record := SenderKeyRecord{ptr: (*C.SignalSenderKeyRecord)(unsafe.Pointer(senderKeyRecord))}
|
||||
|
|
|
@ -23,8 +23,8 @@ package libsignalgo
|
|||
typedef const SignalSessionRecord const_session_record;
|
||||
typedef const SignalProtocolAddress const_address;
|
||||
|
||||
extern int signal_load_session_callback(void *store_ctx, SignalSessionRecord **recordp, const_address *address);
|
||||
extern int signal_store_session_callback(void *store_ctx, const_address *address, const_session_record *record);
|
||||
extern int signal_load_session_callback(uintptr_t store_ctx, SignalSessionRecord **recordp, const_address *address);
|
||||
extern int signal_store_session_callback(uintptr_t store_ctx, const_address *address, const_session_record *record);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
@ -38,7 +38,7 @@ type SessionStore interface {
|
|||
}
|
||||
|
||||
//export signal_load_session_callback
|
||||
func signal_load_session_callback(storeCtx unsafe.Pointer, recordp **C.SignalSessionRecord, address *C.const_address) C.int {
|
||||
func signal_load_session_callback(storeCtx uintptr, recordp **C.SignalSessionRecord, address *C.const_address) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store SessionStore, ctx context.Context) error {
|
||||
record, err := store.LoadSession(ctx, &Address{ptr: (*C.SignalProtocolAddress)(unsafe.Pointer(address))})
|
||||
if err == nil && record != nil {
|
||||
|
@ -50,7 +50,7 @@ func signal_load_session_callback(storeCtx unsafe.Pointer, recordp **C.SignalSes
|
|||
}
|
||||
|
||||
//export signal_store_session_callback
|
||||
func signal_store_session_callback(storeCtx unsafe.Pointer, address *C.const_address, sessionRecord *C.const_session_record) C.int {
|
||||
func signal_store_session_callback(storeCtx uintptr, address *C.const_address, sessionRecord *C.const_session_record) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store SessionStore, ctx context.Context) error {
|
||||
record := SessionRecord{ptr: (*C.SignalSessionRecord)(unsafe.Pointer(sessionRecord))}
|
||||
cloned, err := record.Clone()
|
||||
|
|
|
@ -22,8 +22,8 @@ package libsignalgo
|
|||
|
||||
typedef const SignalSignedPreKeyRecord const_signed_pre_key_record;
|
||||
|
||||
extern int signal_load_signed_pre_key_callback(void *store_ctx, SignalSignedPreKeyRecord **recordp, uint32_t id);
|
||||
extern int signal_store_signed_pre_key_callback(void *store_ctx, uint32_t id, const_signed_pre_key_record *record);
|
||||
extern int signal_load_signed_pre_key_callback(uintptr_t store_ctx, SignalSignedPreKeyRecord **recordp, uint32_t id);
|
||||
extern int signal_store_signed_pre_key_callback(uintptr_t store_ctx, uint32_t id, const_signed_pre_key_record *record);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
@ -37,7 +37,7 @@ type SignedPreKeyStore interface {
|
|||
}
|
||||
|
||||
//export signal_load_signed_pre_key_callback
|
||||
func signal_load_signed_pre_key_callback(storeCtx unsafe.Pointer, keyp **C.SignalSignedPreKeyRecord, id C.uint32_t) C.int {
|
||||
func signal_load_signed_pre_key_callback(storeCtx uintptr, keyp **C.SignalSignedPreKeyRecord, id C.uint32_t) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store SignedPreKeyStore, ctx context.Context) error {
|
||||
key, err := store.LoadSignedPreKey(ctx, uint32(id))
|
||||
if err == nil && key != nil {
|
||||
|
@ -49,7 +49,7 @@ func signal_load_signed_pre_key_callback(storeCtx unsafe.Pointer, keyp **C.Signa
|
|||
}
|
||||
|
||||
//export signal_store_signed_pre_key_callback
|
||||
func signal_store_signed_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t, preKeyRecord *C.const_signed_pre_key_record) C.int {
|
||||
func signal_store_signed_pre_key_callback(storeCtx uintptr, id C.uint32_t, preKeyRecord *C.const_signed_pre_key_record) C.int {
|
||||
return wrapStoreCallback(storeCtx, func(store SignedPreKeyStore, ctx context.Context) error {
|
||||
record := SignedPreKeyRecord{ptr: (*C.SignalSignedPreKeyRecord)(unsafe.Pointer(preKeyRecord))}
|
||||
cloned, err := record.Clone()
|
||||
|
|
|
@ -24,9 +24,7 @@ import "C"
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"unsafe"
|
||||
|
||||
gopointer "github.com/mattn/go-pointer"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type WrappedStore[T any] struct {
|
||||
|
@ -34,10 +32,28 @@ type WrappedStore[T any] struct {
|
|||
Ctx *CallbackContext
|
||||
}
|
||||
|
||||
var nextUnsafePointer uintptr
|
||||
var stores = make(map[uintptr]any)
|
||||
var storesLock sync.RWMutex
|
||||
|
||||
func _putStore(store any) uintptr {
|
||||
storesLock.Lock()
|
||||
defer storesLock.Unlock()
|
||||
nextUnsafePointer++
|
||||
stores[nextUnsafePointer] = store
|
||||
return nextUnsafePointer
|
||||
}
|
||||
|
||||
func _loadStore(storeCtx uintptr) any {
|
||||
storesLock.RLock()
|
||||
defer storesLock.RUnlock()
|
||||
return stores[storeCtx]
|
||||
}
|
||||
|
||||
type CallbackContext struct {
|
||||
Error error
|
||||
Ctx context.Context
|
||||
Unrefs []unsafe.Pointer
|
||||
Unrefs []uintptr
|
||||
}
|
||||
|
||||
func NewCallbackContext(ctx context.Context) *CallbackContext {
|
||||
|
@ -48,19 +64,21 @@ func NewCallbackContext(ctx context.Context) *CallbackContext {
|
|||
}
|
||||
|
||||
func (ctx *CallbackContext) Unref() {
|
||||
storesLock.Lock()
|
||||
for _, ptr := range ctx.Unrefs {
|
||||
gopointer.Unref(ptr)
|
||||
delete(stores, ptr)
|
||||
}
|
||||
storesLock.Unlock()
|
||||
}
|
||||
|
||||
func wrapStore[T any](ctx *CallbackContext, store T) unsafe.Pointer {
|
||||
wrappedStore := gopointer.Save(&WrappedStore[T]{Store: store, Ctx: ctx})
|
||||
func wrapStore[T any](ctx *CallbackContext, store T) C.uintptr_t {
|
||||
wrappedStore := _putStore(&WrappedStore[T]{Store: store, Ctx: ctx})
|
||||
ctx.Unrefs = append(ctx.Unrefs, wrappedStore)
|
||||
return wrappedStore
|
||||
return C.uintptr_t(wrappedStore)
|
||||
}
|
||||
|
||||
func wrapStoreCallbackCustomReturn[T any](storeCtx unsafe.Pointer, callback func(store T, ctx context.Context) (int, error)) C.int {
|
||||
wrap := gopointer.Restore(storeCtx).(*WrappedStore[T])
|
||||
func wrapStoreCallbackCustomReturn[T any](storeCtx uintptr, callback func(store T, ctx context.Context) (int, error)) C.int {
|
||||
wrap := _loadStore(storeCtx).(*WrappedStore[T])
|
||||
retVal, err := callback(wrap.Store, wrap.Ctx.Ctx)
|
||||
if err != nil {
|
||||
wrap.Ctx.Error = err
|
||||
|
@ -68,8 +86,8 @@ func wrapStoreCallbackCustomReturn[T any](storeCtx unsafe.Pointer, callback func
|
|||
return C.int(retVal)
|
||||
}
|
||||
|
||||
func wrapStoreCallback[T any](storeCtx unsafe.Pointer, callback func(store T, ctx context.Context) error) C.int {
|
||||
wrap := gopointer.Restore(storeCtx).(*WrappedStore[T])
|
||||
func wrapStoreCallback[T any](storeCtx uintptr, callback func(store T, ctx context.Context) error) C.int {
|
||||
wrap := _loadStore(storeCtx).(*WrappedStore[T])
|
||||
if err := callback(wrap.Store, wrap.Ctx.Ctx); err != nil {
|
||||
wrap.Ctx.Error = err
|
||||
return -1
|
||||
|
|
|
@ -31,4 +31,7 @@ cd "$ORIGINAL_DIR"
|
|||
cp "${LIBSIGNAL_DIRECTORY}/target/release/libsignal_ffi.a" .
|
||||
cp "${LIBSIGNAL_DIRECTORY}/libsignal-ffi.h" .
|
||||
|
||||
#sed 's/void \*store_ctx/uintptr_t store_ctx/g' -i libsignal-ffi.h
|
||||
sed 's/void \*ctx;/uintptr_t ctx;/g' -i libsignal-ffi.h
|
||||
|
||||
echo "Files copied successfully."
|
||||
|
|
Loading…
Add table
Reference in a new issue