how sha256.lua call C function?

76 views
Skip to first unread message

徐威远

unread,
Jul 16, 2017, 11:59:35 PM7/16/17
to openresty-en
in sha256.lua, C struct and funcions are declared:
typedef struct SHA256state_st
{
SHA_LONG h[8];
SHA_LONG Nl,Nh;
SHA_LONG data[SHA_LBLOCK];
unsigned int num,md_len;
} SHA256_CTX;

int SHA256_Init(SHA256_CTX *c);
int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
int SHA256_Final(unsigned char *md, SHA256_CTX *c);
]]

...

function _M.new(self)
local ctx = ffi_new(ctx_ptr_type)
if C.SHA256_Init(ctx) == 0 then
return nil
end

return setmetatable({ _ctx = ctx }, mt)
end
I'm confused how luajit finds the location C funcions definded while no ffi.load() is used.
And how can I call some custom C function in default C namespace like above?
Thank you.

FQ Liu

unread,
Jul 17, 2017, 1:42:24 AM7/17/17
to openresty-en
~ % grep "int SHA256_Init" /usr/include/openssl/sha.h -n -A2
150:int SHA256_Init(SHA256_CTX *c);
151-int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
152-int SHA256_Final(unsigned char *md, SHA256_CTX *c);


Functions like SHA256_Init() is defined in openssl library, and had been loaded.


在 2017年7月17日星期一 UTC+8上午11:59:35,徐威远写道:

徐威远

unread,
Jul 17, 2017, 4:36:23 AM7/17/17
to openresty-en
Thanks for your help, I have found the  functions defined in openssl-1.0.2k\crypto\evp\e_aes_cbc_hmac_sha256.c
but I still have no idea when and where openresty load them...

在 2017年7月17日星期一 UTC+8下午1:42:24,FQ Liu写道:

FQ Liu

unread,
Jul 17, 2017, 5:23:40 AM7/17/17
to openresty-en
When nginx startup, linked library was automaticly loaded.

在 2017年7月17日星期一 UTC+8下午4:36:23,徐威远写道:
Reply all
Reply to author
Forward
0 new messages