Page 1 of 1

import c lib with fixed array in function

Posted: Fri Jul 12, 2024 2:03 pm
by Thorium
I want to use libhydrogen in PureBasic to sign and verify messages.

I got a problem with importing functions with fixed arrays, PureBasic does not support it. What would be the correct definition for the function in PureBasic?

C-Code

Code: Select all

void hydro_random_buf_deterministic(void *out, size_t out_len, const uint8_t seed[hydro_random_SEEDBYTES]);
My convertion to PureBasic which throws syntax error

Code: Select all

hydro_random_buf_deterministic(*out, out_len.i, Array seed.a[#hydro_random_SEEDBYTES])
Are the arrays passed as pointers and i can just define them as pointer types or are they on the stack?

Re: import c lib with fixed array in function

Posted: Fri Jul 12, 2024 11:23 pm
by idle
if you use the c backend you should be able to call it with inlinec

Code: Select all

;ImportC "hydrolib.lib" : EndImport

;needed types 
!#define uint8_t unsigned char 
!#define size_t long long
!#define hydro_random_SEEDBYTES 32
;!void hydro_random_buf_deterministic(void *out, size_t out_len,const uint8_t seed[hydro_random_SEEDBYTES]);

;simulate calling the function 
!void hydro_random_buf_deterministic(void *out, size_t out_len, const uint8_t seed[hydro_random_SEEDBYTES])
!{  
  For a = 0 To 31 
    xx.a
    !v_xx = seed[v_a];  
    Debug xx 
  Next 
!}; 

;strucutre to use 
Structure m256 
  a.a[32]
EndStructure 

*x.m256 = AllocateMemory(32) 
For a = 0 To 31 
  *x\a[a] = a 
Next 

;call the function 
!hydro_random_buf_deterministic(0,0,p_x); 



Re: import c lib with fixed array in function

Posted: Sat Jul 13, 2024 10:29 am
by Mesa
With or without static array:
[EDIT]

Code: Select all



;according to this post http://forums.purebasic.com/english/viewtopic.php?p=582637, 
;size_t is 8 bytes on windows and 4/8 on other os.


CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  
  Structure size_t
    size_t.q
  EndStructure
  
CompilerElse
  
  CompilerIf #PB_Compiler_64Bit
    Structure size_t
      size_t.q
    EndStructure
    
  CompilerElse
    Structure size_t
      size_t.l
    EndStructure
  CompilerEndIf
CompilerEndIf

Define  out
Define out_len.size_t
;   Debug SizeOf(out_len)
out_len\size_t=10 
#hydro_random_SEEDBYTES=32
Dim seed.a(#hydro_random_SEEDBYTES);normally, you can use dynamic array in procedure in place or static array
For i=0 To #hydro_random_SEEDBYTES-1
  seed(i)=i
Next i

Procedure  hydro_random_buf_deterministic(*out.INTEGER, *outlen.size_t, Array arrayseed.a(1))
  *out\I=2
  Debug arrayseed(12)
  ProcedureReturn *outlen\size_t
EndProcedure

Debug hydro_random_buf_deterministic(@out, @out_len, seed()):Debug out
Debug ""

;== With static array ==============================================================
Structure sta
  sta.a[#hydro_random_SEEDBYTES]
EndStructure

Define seedsta.sta
For i=0 To #hydro_random_SEEDBYTES-1
  seedsta\sta[i]=i
Next i

Procedure  hydro_random_buf_deterministic2(*out, *outlen.size_t, *seedsta.sta)
  PokeI(*out,2)
  ProcedureReturn *seedsta\sta[10]
EndProcedure

Define *out = AllocateMemory(SizeOf(integer))
Debug *out   
Debug hydro_random_buf_deterministic2(@*out, @out_len, @seedsta)
Debug PeekI(@*out)

Mesa.

Re: import c lib with fixed array in function

Posted: Sat Jul 13, 2024 12:30 pm
by mk-soft
The lib requires the paramerter seek array on the stack. Not a pointer to the seek array.
Your example is always a pointer to the array.

Re: import c lib with fixed array in function

Posted: Fri Jul 19, 2024 3:05 pm
by Thorium
After some testing, the arrays can just be passed as pointers.
I use the sign/verify and hash functions of the lib.
If anyone needs compiled version of the lib and/or PureBasic headers for it. You can request them from me. Since it's not completely tested, i don't want to post them just yet.

Re: import c lib with fixed array in function

Posted: Sat Jul 20, 2024 1:58 am
by idle
Thorium wrote: Fri Jul 19, 2024 3:05 pm After some testing, the arrays can just be passed as pointers.
I use the sign/verify and hash functions of the lib.
If anyone needs compiled version of the lib and/or PureBasic headers for it. You can request them from me. Since it's not completely tested, i don't want to post them just yet.
Please post it when your done, it looks useful!