B4R Question size_t* to global variable

KMatle

Expert
Licensed User
Longtime User
I'm still a noob in C :)

I have

C:
size_t olen = 0;

and later it is used as a pointer like &olen

As far as I know this is a pointer to an unsigned int variable.

Via b4r_main::_encrypted_len = olen; all works but I want to drop that extra step and use/cast the variable directly to a B4R variable. I tried (uint8_t*) and other things but it didn't work.

Could someone help me out here? Thanks.
 

candide

Active Member
Licensed User
to store a byte in B4R in a variable accessible in c++ by pointer for read/write: i think it can be done with ArrayByte
in B4R:
Dim datx(1) as byte
datx(0) = 111
in wrapper c++
uint8_t * varx = datx->data;
xxx = &varx[0];
===> xxx will be a pointer to your variable in B4R and a modification under c++ will change B4R variable value

i am not sure it is what you want !
 
Upvote 0

candide

Active Member
Licensed User
if you are using inline C, you can find several examples in this forum to modify a global variable B4R from inline C

 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I wasn't precise enough.

In inline C I use this function which returns &olen (which is size_t)

C:
mbedtls_pk_encrypt( &pk, (uint8_t*)b4r_main::_decrypted->data, b4r_main::_decrypted_len,
                                (uint8_t*)b4r_main::_encrypted->data, &olen,b4r_main::_encrypted->length,
                                mbedtls_ctr_drbg_random, &ctr_drbg ) )

As you can see I can use global variables. Later I use

C:
b4r_main::_encrypted_len = olen;

to modify encrypted_len in globals (Public encrypted_len As Int) which works fine.

Problem: I want to use b4r_main::_encrypted_len directly in the function (like the other values):

C:
mbedtls_pk_encrypt( &pk, (uint8_t*)b4r_main::_decrypted->data, b4r_main::_decrypted_len,
                               (uint8_t*)b4r_main::_encrypted->data, (uint8_t*)b4r_main::_encrypted_len,b4r_main::_encrypted->length,
                                mbedtls_ctr_drbg_random, &ctr_drbg ) )

This throws an error:

b4r_main.cpp:488:68: error: cannot convert 'uint8_t* {aka unsigned char*}' to 'size_t* {aka unsigned int*}' for argument '5' to 'int mbedtls_pk_encrypt(mbedtls_pk_context*, const unsigned char*, size_t, unsigned char*, size_t*, size_t, int (*)(void*, unsigned char*, size_t), void*)'

So how do I cast it correctly?

C:
b4r_main::_encrypted_len = olen;

works, but I want to use it directly in the function.
 
Upvote 0

candide

Active Member
Licensed User
did you solve your problem ?

from error, size_t is uint* , it should be better to have variable uint in B4R and cast can be (uint16_t*)
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Thanks for your reply. I tried several things (uint/Int), cast to uint_16t* and even unsigned_int* or size_t*. Either it didn't compile (x cannot be casted to y) or it compiled and then I go a kernal panic/crash (memory violation).

So I still use

C:
b4r_main::_encrypted_len = olen;

which works
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
When I use

C:
Public decrypted_len As UInt

or

Public decrypted_len As Int

with Inline C:

C:
...b4r_main::_encrypted_len, (uint8_t*)b4r_main::_decrypted->data, [B](size_t*)b4r_main::_decrypted_len[/B],...

It compiles but throws
Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.
 
Upvote 0
Top