]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kluautil: kr_string2c function
authorTomas Krizek <tomas.krizek@nic.cz>
Mon, 22 Nov 2021 16:52:19 +0000 (17:52 +0100)
committerTomas Krizek <tomas.krizek@nic.cz>
Tue, 21 Dec 2021 14:02:08 +0000 (15:02 +0100)
daemon/lua/kluautil.lua

index e73e952cfd8fe48dd3a412fda6fab98cea646bb5..d8569b9a23add42fb759058de25b0027340d966b 100644 (file)
@@ -1,5 +1,6 @@
 -- SPDX-License-Identifier: GPL-3.0-or-later
 
+local ffi = require('ffi')
 local kluautil = {}
 
 -- Get length of table
@@ -79,6 +80,15 @@ function kluautil.kr_https_fetch(url, out_file, ca_file)
        return true
 end
 
+-- Copy a lua string to C (to knot_mm_t or nil=malloc, zero-terminated).
+function kluautil.kr_string2c(str, mempool)
+       if str == nil then return nil end
+       local result = ffi.C.mm_realloc(mempool, nil, #str + 1, 0)
+       if result == nil then panic("not enough memory") end
+       ffi.copy(result, str)
+       return ffi.cast('const char *', result)
+end
+
 kluautil.list_dir = kluautil_list_dir
 
 return kluautil