aboutsummaryrefslogtreecommitdiffstats
path: root/util-src
diff options
context:
space:
mode:
Diffstat (limited to 'util-src')
-rw-r--r--util-src/encodings.c134
-rw-r--r--util-src/hashes.c110
-rw-r--r--util-src/net.c6
-rw-r--r--util-src/poll.c20
-rw-r--r--util-src/pposix.c56
-rw-r--r--util-src/ringbuffer.c31
-rw-r--r--util-src/signal.c4
-rw-r--r--util-src/time.c8
8 files changed, 201 insertions, 168 deletions
diff --git a/util-src/encodings.c b/util-src/encodings.c
index e55a3f44..996a6d4c 100644
--- a/util-src/encodings.c
+++ b/util-src/encodings.c
@@ -216,7 +216,7 @@ static const char *utf8_decode(const char *o, int *val) {
* Check that a string is valid UTF-8
* Returns NULL if not
*/
-const char *check_utf8(lua_State *L, int idx, size_t *l) {
+static const char *check_utf8(lua_State *L, int idx, size_t *l) {
size_t pos, len;
const char *s = luaL_checklstring(L, idx, &len);
pos = 0;
@@ -268,30 +268,36 @@ static const luaL_Reg Reg_utf8[] = {
#include <unicode/usprep.h>
#include <unicode/ustring.h>
#include <unicode/utrace.h>
+#include <unicode/uspoof.h>
+#include <unicode/uidna.h>
static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) {
size_t input_len;
int32_t unprepped_len, prepped_len, output_len;
const char *input;
char output[1024];
+ int flags = USPREP_ALLOW_UNASSIGNED;
UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
UChar prepped[1024];
UErrorCode err = U_ZERO_ERROR;
- if(!lua_isstring(L, 1)) {
- lua_pushnil(L);
- return 1;
- }
-
- input = lua_tolstring(L, 1, &input_len);
+ input = luaL_checklstring(L, 1, &input_len);
if(input_len >= 1024) {
lua_pushnil(L);
return 1;
}
+ /* strict */
+ if(!lua_isnoneornil(L, 2)) {
+ luaL_checktype(L, 2, LUA_TBOOLEAN);
+ if(lua_toboolean(L, 2)) {
+ flags = 0;
+ }
+ }
+
u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
if(U_FAILURE(err)) {
@@ -299,7 +305,7 @@ static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
return 1;
}
- prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, USPREP_ALLOW_UNASSIGNED, NULL, &err);
+ prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, flags, NULL, &err);
if(U_FAILURE(err)) {
lua_pushnil(L);
@@ -317,22 +323,55 @@ static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
}
}
-UStringPrepProfile *icu_nameprep;
-UStringPrepProfile *icu_nodeprep;
-UStringPrepProfile *icu_resourceprep;
-UStringPrepProfile *icu_saslprep;
+static UStringPrepProfile *icu_nameprep;
+static UStringPrepProfile *icu_nodeprep;
+static UStringPrepProfile *icu_resourceprep;
+static UStringPrepProfile *icu_saslprep;
+static USpoofChecker *icu_spoofcheck;
+static UIDNA *icu_idna2008;
+
+#if (U_ICU_VERSION_MAJOR_NUM < 58)
+/* COMPAT */
+#define USPOOF_CONFUSABLE (USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_WHOLE_SCRIPT_CONFUSABLE)
+#endif
/* initialize global ICU stringprep profiles */
-void init_icu() {
+static void init_icu(void) {
UErrorCode err = U_ZERO_ERROR;
utrace_setLevel(UTRACE_VERBOSE);
icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
+ icu_spoofcheck = uspoof_open(&err);
+ uspoof_setChecks(icu_spoofcheck, USPOOF_CONFUSABLE, &err);
+ int options = UIDNA_DEFAULT;
+#if 0
+ /* COMPAT with future Unicode versions */
+ options |= UIDNA_ALLOW_UNASSIGNED;
+#endif
+#if 1
+ /* Forbid eg labels starting with _ */
+ options |= UIDNA_USE_STD3_RULES;
+#endif
+#if 0
+ /* TODO determine if we need this */
+ options |= UIDNA_CHECK_BIDI;
+#endif
+#if 0
+ /* UTS46 makes it sound like these are the responsibility of registrars */
+ options |= UIDNA_CHECK_CONTEXTJ;
+ options |= UIDNA_CHECK_CONTEXTO;
+#endif
+#if 0
+ /* This disables COMPAT with IDNA 2003 */
+ options |= UIDNA_NONTRANSITIONAL_TO_ASCII;
+ options |= UIDNA_NONTRANSITIONAL_TO_UNICODE;
+#endif
+ icu_idna2008 = uidna_openUTS46(options, &err);
if(U_FAILURE(err)) {
- fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
+ fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName(err));
}
}
@@ -362,21 +401,25 @@ static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) {
const char *s;
char string[1024];
int ret;
-
- if(!lua_isstring(L, 1)) {
- lua_pushnil(L);
- return 1;
- }
+ Stringprep_profile_flags flags = 0;
s = check_utf8(L, 1, &len);
+ /* strict */
+ if(!lua_isnoneornil(L, 2)) {
+ luaL_checktype(L, 2, LUA_TBOOLEAN);
+ if(lua_toboolean(L, 2)) {
+ flags = STRINGPREP_NO_UNASSIGNED;
+ }
+ }
+
if(s == NULL || len >= 1024 || len != strlen(s)) {
lua_pushnil(L);
return 1; /* TODO return error message */
}
strcpy(string, s);
- ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
+ ret = stringprep(string, 1024, flags, profile);
if(ret == STRINGPREP_OK) {
lua_pushstring(L, string);
@@ -425,9 +468,10 @@ static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */
return 1;
}
- dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
+ UIDNAInfo info = UIDNA_INFO_INITIALIZER;
+ dest_len = uidna_nameToASCII(icu_idna2008, ustr, ulen, dest, 256, &info, &err);
- if(U_FAILURE(err)) {
+ if(U_FAILURE(err) || info.errors) {
lua_pushnil(L);
return 1;
} else {
@@ -459,9 +503,10 @@ static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */
return 1;
}
- dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
+ UIDNAInfo info = UIDNA_INFO_INITIALIZER;
+ dest_len = uidna_nameToUnicode(icu_idna2008, ustr, ulen, dest, 1024, &info, &err);
- if(U_FAILURE(err)) {
+ if(U_FAILURE(err) || info.errors) {
lua_pushnil(L);
return 1;
} else {
@@ -477,6 +522,40 @@ static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */
}
}
+static int Lskeleton(lua_State *L) {
+ size_t len;
+ int32_t ulen, dest_len, output_len;
+ const char *s = luaL_checklstring(L, 1, &len);
+ UErrorCode err = U_ZERO_ERROR;
+ UChar ustr[1024];
+ UChar dest[1024];
+ char output[1024];
+
+ u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
+
+ if(U_FAILURE(err)) {
+ lua_pushnil(L);
+ return 1;
+ }
+
+ dest_len = uspoof_getSkeleton(icu_spoofcheck, 0, ustr, ulen, dest, 1024, &err);
+
+ if(U_FAILURE(err)) {
+ lua_pushnil(L);
+ return 1;
+ }
+
+ u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
+
+ if(U_SUCCESS(err)) {
+ lua_pushlstring(L, output, output_len);
+ return 1;
+ }
+
+ lua_pushnil(L);
+ return 1;
+}
+
#else /* USE_STRINGPREP_ICU */
/****************** libidn ********************/
@@ -558,6 +637,13 @@ LUALIB_API int luaopen_util_encodings(lua_State *L) {
luaL_setfuncs(L, Reg_utf8, 0);
lua_setfield(L, -2, "utf8");
+#ifdef USE_STRINGPREP_ICU
+ lua_newtable(L);
+ lua_pushcfunction(L, Lskeleton);
+ lua_setfield(L, -2, "skeleton");
+ lua_setfield(L, -2, "confusable");
+#endif
+
lua_pushliteral(L, "-3.14");
lua_setfield(L, -2, "version");
return 1;
diff --git a/util-src/hashes.c b/util-src/hashes.c
index 903ecb6e..84a604ef 100644
--- a/util-src/hashes.c
+++ b/util-src/hashes.c
@@ -26,6 +26,7 @@ typedef unsigned __int32 uint32_t;
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <openssl/hmac.h>
+#include <openssl/evp.h>
#if (LUA_VERSION_NUM == 501)
#define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
@@ -34,8 +35,8 @@ typedef unsigned __int32 uint32_t;
#define HMAC_IPAD 0x36363636
#define HMAC_OPAD 0x5c5c5c5c
-const char *hex_tab = "0123456789abcdef";
-void toHex(const unsigned char *in, int length, unsigned char *out) {
+static const char *hex_tab = "0123456789abcdef";
+static void toHex(const unsigned char *in, int length, unsigned char *out) {
int i;
for(i = 0; i < length; i++) {
@@ -75,44 +76,6 @@ struct hash_desc {
void *ctx, *ctxo;
};
-static void hmac(struct hash_desc *desc, const char *key, size_t key_len,
- const char *msg, size_t msg_len, unsigned char *result) {
- union xory {
- unsigned char bytes[64];
- uint32_t quadbytes[16];
- };
-
- int i;
- unsigned char hashedKey[64]; /* Maximum used digest length */
- union xory k_ipad, k_opad;
-
- if(key_len > 64) {
- desc->Init(desc->ctx);
- desc->Update(desc->ctx, key, key_len);
- desc->Final(hashedKey, desc->ctx);
- key = (const char *)hashedKey;
- key_len = desc->digestLength;
- }
-
- memcpy(k_ipad.bytes, key, key_len);
- memset(k_ipad.bytes + key_len, 0, 64 - key_len);
- memcpy(k_opad.bytes, k_ipad.bytes, 64);
-
- for(i = 0; i < 16; i++) {
- k_ipad.quadbytes[i] ^= HMAC_IPAD;
- k_opad.quadbytes[i] ^= HMAC_OPAD;
- }
-
- desc->Init(desc->ctx);
- desc->Update(desc->ctx, k_ipad.bytes, 64);
- desc->Init(desc->ctxo);
- desc->Update(desc->ctxo, k_opad.bytes, 64);
- desc->Update(desc->ctx, msg, msg_len);
- desc->Final(result, desc->ctx);
- desc->Update(desc->ctxo, result, desc->digestLength);
- desc->Final(result, desc->ctxo);
-}
-
#define MAKE_HMAC_FUNCTION(myFunc, evp, size, type) \
static int myFunc(lua_State *L) { \
unsigned char hash[size], result[2*size]; \
@@ -136,56 +99,37 @@ MAKE_HMAC_FUNCTION(Lhmac_sha256, EVP_sha256, SHA256_DIGEST_LENGTH, SHA256_CTX)
MAKE_HMAC_FUNCTION(Lhmac_sha512, EVP_sha512, SHA512_DIGEST_LENGTH, SHA512_CTX)
MAKE_HMAC_FUNCTION(Lhmac_md5, EVP_md5, MD5_DIGEST_LENGTH, MD5_CTX)
-static int LscramHi(lua_State *L) {
- union xory {
- unsigned char bytes[SHA_DIGEST_LENGTH];
- uint32_t quadbytes[SHA_DIGEST_LENGTH / 4];
- };
- int i;
- SHA_CTX ctx, ctxo;
- unsigned char Ust[SHA_DIGEST_LENGTH];
- union xory Und;
- union xory res;
- size_t str_len, salt_len;
- struct hash_desc desc;
- const char *str = luaL_checklstring(L, 1, &str_len);
- const char *salt = luaL_checklstring(L, 2, &salt_len);
- char *salt2;
- const int iter = luaL_checkinteger(L, 3);
-
- desc.Init = (int (*)(void *))SHA1_Init;
- desc.Update = (int (*)(void *, const void *, size_t))SHA1_Update;
- desc.Final = (int (*)(unsigned char *, void *))SHA1_Final;
- desc.digestLength = SHA_DIGEST_LENGTH;
- desc.ctx = &ctx;
- desc.ctxo = &ctxo;
+static int Lpbkdf2_sha1(lua_State *L) {
+ unsigned char out[SHA_DIGEST_LENGTH];
- salt2 = malloc(salt_len + 4);
+ size_t pass_len, salt_len;
+ const char *pass = luaL_checklstring(L, 1, &pass_len);
+ const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len);
+ const int iter = luaL_checkinteger(L, 3);
- if(salt2 == NULL) {
- return luaL_error(L, "Out of memory in scramHi");
+ if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha1(), SHA_DIGEST_LENGTH, out) == 0) {
+ return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed");
}
- memcpy(salt2, salt, salt_len);
- memcpy(salt2 + salt_len, "\0\0\0\1", 4);
- hmac(&desc, str, str_len, salt2, salt_len + 4, Ust);
- free(salt2);
+ lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH);
- memcpy(res.bytes, Ust, sizeof(res));
+ return 1;
+}
- for(i = 1; i < iter; i++) {
- int j;
- hmac(&desc, str, str_len, (char *)Ust, sizeof(Ust), Und.bytes);
- for(j = 0; j < SHA_DIGEST_LENGTH / 4; j++) {
- res.quadbytes[j] ^= Und.quadbytes[j];
- }
+static int Lpbkdf2_sha256(lua_State *L) {
+ unsigned char out[SHA256_DIGEST_LENGTH];
- memcpy(Ust, Und.bytes, sizeof(Ust));
- }
+ size_t pass_len, salt_len;
+ const char *pass = luaL_checklstring(L, 1, &pass_len);
+ const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len);
+ const int iter = luaL_checkinteger(L, 3);
- lua_pushlstring(L, (char *)res.bytes, SHA_DIGEST_LENGTH);
+ if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha256(), SHA256_DIGEST_LENGTH, out) == 0) {
+ return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed");
+ }
+ lua_pushlstring(L, (char *)out, SHA256_DIGEST_LENGTH);
return 1;
}
@@ -200,7 +144,9 @@ static const luaL_Reg Reg[] = {
{ "hmac_sha256", Lhmac_sha256 },
{ "hmac_sha512", Lhmac_sha512 },
{ "hmac_md5", Lhmac_md5 },
- { "scram_Hi_sha1", LscramHi },
+ { "scram_Hi_sha1", Lpbkdf2_sha1 }, /* COMPAT */
+ { "pbkdf2_hmac_sha1", Lpbkdf2_sha1 },
+ { "pbkdf2_hmac_sha256", Lpbkdf2_sha256 },
{ NULL, NULL }
};
@@ -209,7 +155,7 @@ LUALIB_API int luaopen_util_hashes(lua_State *L) {
luaL_checkversion(L);
#endif
lua_newtable(L);
- luaL_setfuncs(L, Reg, 0);;
+ luaL_setfuncs(L, Reg, 0);
lua_pushliteral(L, "-3.14");
lua_setfield(L, -2, "version");
return 1;
diff --git a/util-src/net.c b/util-src/net.c
index 5f706d81..c3b07815 100644
--- a/util-src/net.c
+++ b/util-src/net.c
@@ -36,7 +36,7 @@
/* Enumerate all locally configured IP addresses */
-const char *const type_strings[] = {
+static const char *const type_strings[] = {
"both",
"ipv4",
"ipv6",
@@ -46,8 +46,8 @@ const char *const type_strings[] = {
static int lc_local_addresses(lua_State *L) {
#ifndef _WIN32
/* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */
- const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */
- const long ip4_mask = htonl(0xffff0000);
+ const uint32_t ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */
+ const uint32_t ip4_mask = htonl(0xffff0000);
struct ifaddrs *addr = NULL, *a;
#endif
int n = 1;
diff --git a/util-src/poll.c b/util-src/poll.c
index 0ca0cf28..21cb9581 100644
--- a/util-src/poll.c
+++ b/util-src/poll.c
@@ -59,7 +59,7 @@ typedef struct Lpoll_state {
/*
* Add an FD to be watched
*/
-int Ladd(lua_State *L) {
+static int Ladd(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
@@ -137,7 +137,7 @@ int Ladd(lua_State *L) {
/*
* Set events to watch for, readable and/or writable
*/
-int Lset(lua_State *L) {
+static int Lset(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
@@ -172,6 +172,7 @@ int Lset(lua_State *L) {
lua_pushnil(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
+ return 3;
}
if(!lua_isnoneornil(L, 3)) {
@@ -200,7 +201,7 @@ int Lset(lua_State *L) {
/*
* Remove FDs
*/
-int Ldel(lua_State *L) {
+static int Ldel(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
@@ -229,6 +230,7 @@ int Ldel(lua_State *L) {
lua_pushnil(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
+ return 3;
}
FD_CLR(fd, &state->wantread);
@@ -247,7 +249,7 @@ int Ldel(lua_State *L) {
/*
* Check previously manipulated event state for FDs ready for reading or writing
*/
-int Lpushevent(lua_State *L, struct Lpoll_state *state) {
+static int Lpushevent(lua_State *L, struct Lpoll_state *state) {
#ifdef USE_EPOLL
if(state->processed > 0) {
@@ -281,7 +283,7 @@ int Lpushevent(lua_State *L, struct Lpoll_state *state) {
/*
* Wait for event
*/
-int Lwait(lua_State *L) {
+static int Lwait(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int ret = Lpushevent(L, state);
@@ -344,7 +346,7 @@ int Lwait(lua_State *L) {
/*
* Return Epoll FD
*/
-int Lgetfd(lua_State *L) {
+static int Lgetfd(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
lua_pushinteger(L, state->epoll_fd);
return 1;
@@ -353,7 +355,7 @@ int Lgetfd(lua_State *L) {
/*
* Close epoll FD
*/
-int Lgc(lua_State *L) {
+static int Lgc(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
if(state->epoll_fd == -1) {
@@ -375,7 +377,7 @@ int Lgc(lua_State *L) {
/*
* String representation
*/
-int Ltos(lua_State *L) {
+static int Ltos(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
lua_pushfstring(L, "%s: %p", STATE_MT, state);
return 1;
@@ -384,7 +386,7 @@ int Ltos(lua_State *L) {
/*
* Create a new context
*/
-int Lnew(lua_State *L) {
+static int Lnew(lua_State *L) {
/* Allocate state */
Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state));
luaL_setmetatable(L, STATE_MT);
diff --git a/util-src/pposix.c b/util-src/pposix.c
index 004f61a6..2ebf0444 100644
--- a/util-src/pposix.c
+++ b/util-src/pposix.c
@@ -25,14 +25,18 @@
#define _DEFAULT_SOURCE
#endif
#endif
+
#if defined(__APPLE__)
#ifndef _DARWIN_C_SOURCE
#define _DARWIN_C_SOURCE
#endif
#endif
+
+#if ! defined(__FreeBSD__)
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
+#endif
#include <stdlib.h>
#include <math.h>
@@ -133,7 +137,7 @@ static int lc_daemonize(lua_State *L) {
/* Syslog support */
-const char *const facility_strings[] = {
+static const char *const facility_strings[] = {
"auth",
#if !(defined(sun) || defined(__sun))
"authpriv",
@@ -159,7 +163,7 @@ const char *const facility_strings[] = {
"uucp",
NULL
};
-int facility_constants[] = {
+static int facility_constants[] = {
LOG_AUTH,
#if !(defined(sun) || defined(__sun))
LOG_AUTHPRIV,
@@ -195,9 +199,9 @@ int facility_constants[] = {
constant.
" -- syslog manpage
*/
-char *syslog_ident = NULL;
+static char *syslog_ident = NULL;
-int lc_syslog_open(lua_State *L) {
+static int lc_syslog_open(lua_State *L) {
int facility = luaL_checkoption(L, 2, "daemon", facility_strings);
facility = facility_constants[facility];
@@ -213,7 +217,7 @@ int lc_syslog_open(lua_State *L) {
return 0;
}
-const char *const level_strings[] = {
+static const char *const level_strings[] = {
"debug",
"info",
"notice",
@@ -221,7 +225,7 @@ const char *const level_strings[] = {
"error",
NULL
};
-int level_constants[] = {
+static int level_constants[] = {
LOG_DEBUG,
LOG_INFO,
LOG_NOTICE,
@@ -229,7 +233,7 @@ int level_constants[] = {
LOG_CRIT,
-1
};
-int lc_syslog_log(lua_State *L) {
+static int lc_syslog_log(lua_State *L) {
int level = level_constants[luaL_checkoption(L, 1, "notice", level_strings)];
if(lua_gettop(L) == 3) {
@@ -241,7 +245,7 @@ int lc_syslog_log(lua_State *L) {
return 0;
}
-int lc_syslog_close(lua_State *L) {
+static int lc_syslog_close(lua_State *L) {
(void)L;
closelog();
@@ -253,7 +257,7 @@ int lc_syslog_close(lua_State *L) {
return 0;
}
-int lc_syslog_setmask(lua_State *L) {
+static int lc_syslog_setmask(lua_State *L) {
int level_idx = luaL_checkoption(L, 1, "notice", level_strings);
int mask = 0;
@@ -267,24 +271,24 @@ int lc_syslog_setmask(lua_State *L) {
/* getpid */
-int lc_getpid(lua_State *L) {
+static int lc_getpid(lua_State *L) {
lua_pushinteger(L, getpid());
return 1;
}
/* UID/GID functions */
-int lc_getuid(lua_State *L) {
+static int lc_getuid(lua_State *L) {
lua_pushinteger(L, getuid());
return 1;
}
-int lc_getgid(lua_State *L) {
+static int lc_getgid(lua_State *L) {
lua_pushinteger(L, getgid());
return 1;
}
-int lc_setuid(lua_State *L) {
+static int lc_setuid(lua_State *L) {
int uid = -1;
if(lua_gettop(L) < 1) {
@@ -342,7 +346,7 @@ int lc_setuid(lua_State *L) {
return 2;
}
-int lc_setgid(lua_State *L) {
+static int lc_setgid(lua_State *L) {
int gid = -1;
if(lua_gettop(L) < 1) {
@@ -400,7 +404,7 @@ int lc_setgid(lua_State *L) {
return 2;
}
-int lc_initgroups(lua_State *L) {
+static int lc_initgroups(lua_State *L) {
int ret;
gid_t gid;
struct passwd *p;
@@ -464,7 +468,7 @@ int lc_initgroups(lua_State *L) {
return 2;
}
-int lc_umask(lua_State *L) {
+static int lc_umask(lua_State *L) {
char old_mode_string[7];
mode_t old_mode = umask(strtoul(luaL_checkstring(L, 1), NULL, 8));
@@ -475,7 +479,7 @@ int lc_umask(lua_State *L) {
return 1;
}
-int lc_mkdir(lua_State *L) {
+static int lc_mkdir(lua_State *L) {
int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR
| S_IRGRP | S_IWGRP | S_IXGRP
| S_IROTH | S_IXOTH); /* mode 775 */
@@ -500,7 +504,7 @@ int lc_mkdir(lua_State *L) {
* Example usage:
* pposix.setrlimit("NOFILE", 1000, 2000)
*/
-int string2resource(const char *s) {
+static int string2resource(const char *s) {
if(!strcmp(s, "CORE")) {
return RLIMIT_CORE;
}
@@ -550,7 +554,7 @@ int string2resource(const char *s) {
return -1;
}
-rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) {
+static rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) {
switch(lua_type(L, idx)) {
case LUA_TSTRING:
@@ -571,7 +575,7 @@ rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) {
}
}
-int lc_setrlimit(lua_State *L) {
+static int lc_setrlimit(lua_State *L) {
struct rlimit lim;
int arguments = lua_gettop(L);
int rid = -1;
@@ -610,7 +614,7 @@ int lc_setrlimit(lua_State *L) {
return 1;
}
-int lc_getrlimit(lua_State *L) {
+static int lc_getrlimit(lua_State *L) {
int arguments = lua_gettop(L);
const char *resource = NULL;
int rid = -1;
@@ -655,13 +659,13 @@ int lc_getrlimit(lua_State *L) {
return 3;
}
-int lc_abort(lua_State *L) {
+static int lc_abort(lua_State *L) {
(void)L;
abort();
return 0;
}
-int lc_uname(lua_State *L) {
+static int lc_uname(lua_State *L) {
struct utsname uname_info;
if(uname(&uname_info) != 0) {
@@ -688,7 +692,7 @@ int lc_uname(lua_State *L) {
return 1;
}
-int lc_setenv(lua_State *L) {
+static int lc_setenv(lua_State *L) {
const char *var = luaL_checkstring(L, 1);
const char *value;
@@ -717,7 +721,7 @@ int lc_setenv(lua_State *L) {
}
#ifdef WITH_MALLINFO
-int lc_meminfo(lua_State *L) {
+static int lc_meminfo(lua_State *L) {
struct mallinfo info = mallinfo();
lua_createtable(L, 0, 5);
/* This is the total size of memory allocated with sbrk by malloc, in bytes. */
@@ -745,7 +749,7 @@ int lc_meminfo(lua_State *L) {
* Attempt to allocate space first
* Truncate to original size on failure
*/
-int lc_atomic_append(lua_State *L) {
+static int lc_atomic_append(lua_State *L) {
int err;
size_t len;
diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c
index 8f9013f7..3e17cdf5 100644
--- a/util-src/ringbuffer.c
+++ b/util-src/ringbuffer.c
@@ -15,23 +15,18 @@ typedef struct {
char buffer[];
} ringbuffer;
-char readchar(ringbuffer *b) {
- b->blen--;
- return b->buffer[(b->rpos++) % b->alen];
-}
-
-void writechar(ringbuffer *b, char c) {
+static void writechar(ringbuffer *b, char c) {
b->blen++;
b->buffer[(b->wpos++) % b->alen] = c;
}
/* make sure position counters stay within the allocation */
-void modpos(ringbuffer *b) {
+static void modpos(ringbuffer *b) {
b->rpos = b->rpos % b->alen;
b->wpos = b->wpos % b->alen;
}
-int find(ringbuffer *b, const char *s, size_t l) {
+static int find(ringbuffer *b, const char *s, size_t l) {
size_t i, j;
int m;
@@ -64,7 +59,7 @@ int find(ringbuffer *b, const char *s, size_t l) {
* Find first position of a substring in buffer
* (buffer, string) -> number
*/
-int rb_find(lua_State *L) {
+static int rb_find(lua_State *L) {
size_t l, m;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
const char *s = luaL_checklstring(L, 2, &l);
@@ -82,7 +77,7 @@ int rb_find(lua_State *L) {
* Move read position forward without returning the data
* (buffer, number) -> boolean
*/
-int rb_discard(lua_State *L) {
+static int rb_discard(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
size_t r = luaL_checkinteger(L, 2);
@@ -103,7 +98,7 @@ int rb_discard(lua_State *L) {
* Read bytes from buffer
* (buffer, number, boolean?) -> string
*/
-int rb_read(lua_State *L) {
+static int rb_read(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
size_t r = luaL_checkinteger(L, 2);
int peek = lua_toboolean(L, 3);
@@ -135,7 +130,7 @@ int rb_read(lua_State *L) {
* Read buffer until first occurrence of a substring
* (buffer, string) -> string
*/
-int rb_readuntil(lua_State *L) {
+static int rb_readuntil(lua_State *L) {
size_t l, m;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
const char *s = luaL_checklstring(L, 2, &l);
@@ -154,7 +149,7 @@ int rb_readuntil(lua_State *L) {
* Write bytes into the buffer
* (buffer, string) -> integer
*/
-int rb_write(lua_State *L) {
+static int rb_write(lua_State *L) {
size_t l, w = 0;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
const char *s = luaL_checklstring(L, 2, &l);
@@ -177,31 +172,31 @@ int rb_write(lua_State *L) {
return 1;
}
-int rb_tostring(lua_State *L) {
+static int rb_tostring(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
lua_pushfstring(L, "ringbuffer: %p %d/%d", b, b->blen, b->alen);
return 1;
}
-int rb_length(lua_State *L) {
+static int rb_length(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
lua_pushinteger(L, b->blen);
return 1;
}
-int rb_size(lua_State *L) {
+static int rb_size(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
lua_pushinteger(L, b->alen);
return 1;
}
-int rb_free(lua_State *L) {
+static int rb_free(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
lua_pushinteger(L, b->alen - b->blen);
return 1;
}
-int rb_new(lua_State *L) {
+static int rb_new(lua_State *L) {
size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE));
ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size);
diff --git a/util-src/signal.c b/util-src/signal.c
index c696a3a2..835a601a 100644
--- a/util-src/signal.c
+++ b/util-src/signal.c
@@ -164,8 +164,8 @@ static lua_Hook Hsig = NULL;
static int Hmask = 0;
static int Hcount = 0;
-int signals[MAX_PENDING_SIGNALS];
-int nsig = 0;
+static int signals[MAX_PENDING_SIGNALS];
+static int nsig = 0;
static void sighook(lua_State *L, lua_Debug *ar) {
(void)ar;
diff --git a/util-src/time.c b/util-src/time.c
index bfad52ee..afef3df5 100644
--- a/util-src/time.c
+++ b/util-src/time.c
@@ -1,22 +1,22 @@
#ifndef _POSIX_C_SOURCE
-#define _POSIX_C_SOURCE 199309L
+#define _POSIX_C_SOURCE 200809L
#endif
#include <time.h>
#include <lua.h>
-lua_Number tv2number(struct timespec *tv) {
+static lua_Number tv2number(struct timespec *tv) {
return tv->tv_sec + tv->tv_nsec * 1e-9;
}
-int lc_time_realtime(lua_State *L) {
+static int lc_time_realtime(lua_State *L) {
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
lua_pushnumber(L, tv2number(&t));
return 1;
}
-int lc_time_monotonic(lua_State *L) {
+static int lc_time_monotonic(lua_State *L) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
lua_pushnumber(L, tv2number(&t));