diff options
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r-- | src/lua/lua_util.c | 1922 |
1 files changed, 959 insertions, 963 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 39e08c1fb..503dd8ffb 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -45,19 +45,19 @@ * Creates new event base for processing asynchronous events * @return {ev_base} new event processing base */ -LUA_FUNCTION_DEF (util, create_event_base); +LUA_FUNCTION_DEF(util, create_event_base); /*** * @function util.load_rspamd_config(filename) * Load rspamd config from the specified file * @return {confg} new configuration object suitable for access */ -LUA_FUNCTION_DEF (util, load_rspamd_config); +LUA_FUNCTION_DEF(util, load_rspamd_config); /*** * @function util.config_from_ucl(any, string) * Load rspamd config from ucl represented by any lua table * @return {confg} new configuration object suitable for access */ -LUA_FUNCTION_DEF (util, config_from_ucl); +LUA_FUNCTION_DEF(util, config_from_ucl); /*** * @function util.encode_base64(input[, str_len, [newlines_type]]) * Encodes data in base64 breaking lines if needed @@ -65,7 +65,7 @@ LUA_FUNCTION_DEF (util, config_from_ucl); * @param {number} str_len optional size of lines or 0 if split is not needed * @return {rspamd_text} encoded data chunk */ -LUA_FUNCTION_DEF (util, encode_base64); +LUA_FUNCTION_DEF(util, encode_base64); /*** * @function util.encode_qp(input[, str_len, [newlines_type]]) * Encodes data in quoted printable breaking lines if needed @@ -73,7 +73,7 @@ LUA_FUNCTION_DEF (util, encode_base64); * @param {number} str_len optional size of lines or 0 if split is not needed * @return {rspamd_text} encoded data chunk */ -LUA_FUNCTION_DEF (util, encode_qp); +LUA_FUNCTION_DEF(util, encode_qp); /*** * @function util.decode_qp(input) @@ -81,7 +81,7 @@ LUA_FUNCTION_DEF (util, encode_qp); * @param {text or string} input input data * @return {rspamd_text} decoded data chunk */ -LUA_FUNCTION_DEF (util, decode_qp); +LUA_FUNCTION_DEF(util, decode_qp); /*** * @function util.decode_base64(input) @@ -89,7 +89,7 @@ LUA_FUNCTION_DEF (util, decode_qp); * @param {text or string} input data to decode; if `rspamd{text}` is used then the string is modified **in-place** * @return {rspamd_text} decoded data chunk */ -LUA_FUNCTION_DEF (util, decode_base64); +LUA_FUNCTION_DEF(util, decode_base64); /*** * @function util.encode_base32(input, [b32type = 'default']) @@ -98,7 +98,7 @@ LUA_FUNCTION_DEF (util, decode_base64); * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} encoded data chunk */ -LUA_FUNCTION_DEF (util, encode_base32); +LUA_FUNCTION_DEF(util, encode_base32); /*** * @function util.decode_base32(input, [b32type = 'default']) * Decodes data from base32 ignoring whitespace characters @@ -106,7 +106,7 @@ LUA_FUNCTION_DEF (util, encode_base32); * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} decoded data chunk */ -LUA_FUNCTION_DEF (util, decode_base32); +LUA_FUNCTION_DEF(util, decode_base32); /*** * @function util.decode_url(input) @@ -114,7 +114,7 @@ LUA_FUNCTION_DEF (util, decode_base32); * @param {text or string} input data to decode * @return {rspamd_text} decoded data chunk */ -LUA_FUNCTION_DEF (util, decode_url); +LUA_FUNCTION_DEF(util, decode_url); /*** * @function util.tokenize_text(input[, exceptions]) @@ -123,15 +123,15 @@ LUA_FUNCTION_DEF (util, decode_url); * @param {table} exceptions, a table of pairs containing <start_pos,length> of exceptions in the input * @return {table/strings} list of strings representing words in the text */ -LUA_FUNCTION_DEF (util, tokenize_text); -LUA_FUNCTION_DEF (util, process_message); +LUA_FUNCTION_DEF(util, tokenize_text); +LUA_FUNCTION_DEF(util, process_message); /*** * @function util.tanh(num) * Calculates hyperbolic tangent of the specified floating point value * @param {number} num input number * @return {number} hyperbolic tangent of the variable */ -LUA_FUNCTION_DEF (util, tanh); +LUA_FUNCTION_DEF(util, tanh); /*** * @function util.parse_html(input) @@ -139,7 +139,7 @@ LUA_FUNCTION_DEF (util, tanh); * @param {string|text} in input HTML * @return {rspamd_text} processed text with no HTML tags */ -LUA_FUNCTION_DEF (util, parse_html); +LUA_FUNCTION_DEF(util, parse_html); /*** * @function util.levenshtein_distance(s1, s2) @@ -148,7 +148,7 @@ LUA_FUNCTION_DEF (util, parse_html); * @param {string} s2 the second string * @return {number} number of differences in two strings */ -LUA_FUNCTION_DEF (util, levenshtein_distance); +LUA_FUNCTION_DEF(util, levenshtein_distance); /*** * @function util.fold_header(name, value, [how, [stop_chars]]) @@ -160,7 +160,7 @@ LUA_FUNCTION_DEF (util, levenshtein_distance); * @param {string} stop_chars also fold header when the * @return {string} Folded value of the header */ -LUA_FUNCTION_DEF (util, fold_header); +LUA_FUNCTION_DEF(util, fold_header); /*** * @function util.is_uppercase(str) @@ -169,7 +169,7 @@ LUA_FUNCTION_DEF (util, fold_header); * @param {string} str input string * @return {bool} true if a string is all uppercase */ -LUA_FUNCTION_DEF (util, is_uppercase); +LUA_FUNCTION_DEF(util, is_uppercase); /*** * @function util.humanize_number(num) @@ -178,7 +178,7 @@ LUA_FUNCTION_DEF (util, is_uppercase); * @param {number} num number to humanize * @return {string} humanized representation of a number */ -LUA_FUNCTION_DEF (util, humanize_number); +LUA_FUNCTION_DEF(util, humanize_number); /*** * @function util.get_tld(host) @@ -187,7 +187,7 @@ LUA_FUNCTION_DEF (util, humanize_number); * @param {string} host hostname * @return {string} eSLD part of the hostname or the full hostname if eSLD was not found */ -LUA_FUNCTION_DEF (util, get_tld); +LUA_FUNCTION_DEF(util, get_tld); /*** * @function util.glob(pattern) @@ -196,7 +196,7 @@ LUA_FUNCTION_DEF (util, get_tld); * @param {string} pattern glob pattern to match ('?' and '*' are supported) * @return {table/string} list of matched files */ -LUA_FUNCTION_DEF (util, glob); +LUA_FUNCTION_DEF(util, glob); /*** * @function util.parse_mail_address(str, [pool]) @@ -220,7 +220,7 @@ LUA_FUNCTION_DEF (util, glob); * @param {rspamd_mempool} pool memory pool to use * @return {table/tables} parsed list of mail addresses */ -LUA_FUNCTION_DEF (util, parse_mail_address); +LUA_FUNCTION_DEF(util, parse_mail_address); /*** * @function util.strlen_utf8(str) @@ -229,7 +229,7 @@ LUA_FUNCTION_DEF (util, parse_mail_address); * @param {string} str utf8 encoded string * @return {number} number of characters in string */ -LUA_FUNCTION_DEF (util, strlen_utf8); +LUA_FUNCTION_DEF(util, strlen_utf8); /*** * @function util.lower_utf8(str) @@ -237,7 +237,7 @@ LUA_FUNCTION_DEF (util, strlen_utf8); * @param {string} str utf8 encoded string * @return {string} lowercased utf8 string */ -LUA_FUNCTION_DEF (util, lower_utf8); +LUA_FUNCTION_DEF(util, lower_utf8); /*** * @function util.normalize_utf8(str) @@ -250,7 +250,7 @@ LUA_FUNCTION_DEF (util, lower_utf8); * RSPAMD_UNICODE_NORM_ERROR = (1 << 2), * RSPAMD_UNICODE_NORM_OVERFLOW = (1 << 3) */ -LUA_FUNCTION_DEF (util, normalize_utf8); +LUA_FUNCTION_DEF(util, normalize_utf8); /*** @@ -259,7 +259,7 @@ LUA_FUNCTION_DEF (util, normalize_utf8); * @param {string/text} str utf8 encoded string * @return {text} transliterated string */ -LUA_FUNCTION_DEF (util, transliterate); +LUA_FUNCTION_DEF(util, transliterate); /*** * @function util.strequal_caseless(str1, str2) @@ -269,7 +269,7 @@ LUA_FUNCTION_DEF (util, transliterate); * @param {string} str2 utf8 encoded string * @return {bool} result of comparison */ -LUA_FUNCTION_DEF (util, strequal_caseless); +LUA_FUNCTION_DEF(util, strequal_caseless); /*** @@ -280,7 +280,7 @@ LUA_FUNCTION_DEF (util, strequal_caseless); * @param {string} str2 utf8 encoded string * @return {bool} result of comparison */ -LUA_FUNCTION_DEF (util, strequal_caseless_utf8); +LUA_FUNCTION_DEF(util, strequal_caseless_utf8); /*** @@ -288,14 +288,14 @@ LUA_FUNCTION_DEF (util, strequal_caseless_utf8); * Returns current number of ticks as floating point number * @return {number} number of current clock ticks (monotonically increasing) */ -LUA_FUNCTION_DEF (util, get_ticks); +LUA_FUNCTION_DEF(util, get_ticks); /*** * @function util.get_time() * Returns current time as unix time in floating point representation * @return {number} number of seconds since 01.01.1970 */ -LUA_FUNCTION_DEF (util, get_time); +LUA_FUNCTION_DEF(util, get_time); /*** * @function util.time_to_string(seconds) @@ -303,7 +303,7 @@ LUA_FUNCTION_DEF (util, get_time); * @param {number} seconds unix timestamp * @return {string} date as HTTP date */ -LUA_FUNCTION_DEF (util, time_to_string); +LUA_FUNCTION_DEF(util, time_to_string); /*** * @function util.stat(fname) @@ -324,7 +324,7 @@ LUA_FUNCTION_DEF (util, time_to_string); * print(st['size']) * end */ -LUA_FUNCTION_DEF (util, stat); +LUA_FUNCTION_DEF(util, stat); /*** * @function util.unlink(fname) @@ -333,7 +333,7 @@ LUA_FUNCTION_DEF (util, stat); * @param {string} fname filename to remove * @return {boolean,[string]} true if file has been deleted or false,'error string' */ -LUA_FUNCTION_DEF (util, unlink); +LUA_FUNCTION_DEF(util, unlink); /*** * @function util.lock_file(fname, [fd]) @@ -344,7 +344,7 @@ LUA_FUNCTION_DEF (util, unlink); * @param {number} fd use the specified fd instead of opening one * @return {number|nil,string} number if locking was successful or nil + error otherwise */ -LUA_FUNCTION_DEF (util, lock_file); +LUA_FUNCTION_DEF(util, lock_file); /*** * @function util.unlock_file(fd, [close_fd]) @@ -354,7 +354,7 @@ LUA_FUNCTION_DEF (util, lock_file); * @param {boolean} close_fd close descriptor on unlocking (default: TRUE) * @return {boolean[,string]} true if a file was unlocked */ -LUA_FUNCTION_DEF (util, unlock_file); +LUA_FUNCTION_DEF(util, unlock_file); /*** * @function util.create_file(fname, [mode]) @@ -364,7 +364,7 @@ LUA_FUNCTION_DEF (util, unlock_file); * @param {number} mode open mode (you should use octal number here) * @return {number|nil,string} file descriptor or pair nil + error string */ -LUA_FUNCTION_DEF (util, create_file); +LUA_FUNCTION_DEF(util, create_file); /*** * @function util.close_file(fd) @@ -373,7 +373,7 @@ LUA_FUNCTION_DEF (util, create_file); * @param {number} fd descriptor to close * @return {boolean[,string]} true if a file was closed */ -LUA_FUNCTION_DEF (util, close_file); +LUA_FUNCTION_DEF(util, close_file); /*** * @function util.random_hex(size) @@ -382,7 +382,7 @@ LUA_FUNCTION_DEF (util, close_file); * @param {number} len length of desired string in bytes * @return {string} string with random hex digests */ -LUA_FUNCTION_DEF (util, random_hex); +LUA_FUNCTION_DEF(util, random_hex); /*** * @function util.zstd_compress(data, [level=1]) @@ -391,7 +391,7 @@ LUA_FUNCTION_DEF (util, random_hex); * @param {string/rspamd_text} data input data * @return {rspamd_text} compressed data */ -LUA_FUNCTION_DEF (util, zstd_compress); +LUA_FUNCTION_DEF(util, zstd_compress); /*** * @function util.zstd_decompress(data) @@ -400,7 +400,7 @@ LUA_FUNCTION_DEF (util, zstd_compress); * @param {string/rspamd_text} data compressed data * @return {error,rspamd_text} pair of error + decompressed text */ -LUA_FUNCTION_DEF (util, zstd_decompress); +LUA_FUNCTION_DEF(util, zstd_decompress); /*** * @function util.gzip_decompress(data, [size_limit]) @@ -410,7 +410,7 @@ LUA_FUNCTION_DEF (util, zstd_decompress); * @param {integer} size_limit optional size limit * @return {rspamd_text} decompressed text */ -LUA_FUNCTION_DEF (util, gzip_decompress); +LUA_FUNCTION_DEF(util, gzip_decompress); /*** * @function util.inflate(data, [size_limit]) @@ -420,7 +420,7 @@ LUA_FUNCTION_DEF (util, gzip_decompress); * @param {integer} size_limit optional size limit * @return {rspamd_text} decompressed text */ -LUA_FUNCTION_DEF (util, inflate); +LUA_FUNCTION_DEF(util, inflate); /*** * @function util.gzip_compress(data, [level=1]) @@ -429,7 +429,7 @@ LUA_FUNCTION_DEF (util, inflate); * @param {string/rspamd_text} data input data * @return {rspamd_text} compressed data */ -LUA_FUNCTION_DEF (util, gzip_compress); +LUA_FUNCTION_DEF(util, gzip_compress); /*** * @function util.normalize_prob(prob, [bias = 0.5]) @@ -439,13 +439,13 @@ LUA_FUNCTION_DEF (util, gzip_compress); * @param {number} bias number to subtract for making the final solution * @return {number} normalized number */ -LUA_FUNCTION_DEF (util, normalize_prob); +LUA_FUNCTION_DEF(util, normalize_prob); /*** * @function util.is_utf_spoofed(str, [str2]) * Returns true if a string is spoofed (possibly with another string `str2`) * @return {boolean} true if a string is spoofed */ -LUA_FUNCTION_DEF (util, is_utf_spoofed); +LUA_FUNCTION_DEF(util, is_utf_spoofed); /** * @function util.is_utf_mixed_script(str) @@ -453,7 +453,7 @@ LUA_FUNCTION_DEF (util, is_utf_spoofed); * @param {string} String to check * @return {boolean} true if a string contains chars with mixed unicode script */ -LUA_FUNCTION_DEF (util, is_utf_mixed_script); +LUA_FUNCTION_DEF(util, is_utf_mixed_script); /** * @function util.is_utf_outside_range(str, range_start, range_end) @@ -463,56 +463,56 @@ LUA_FUNCTION_DEF (util, is_utf_mixed_script); * @param {number} end of character range similar to uset_addRange * @return {boolean} true if a string contains chars outside selected utf range */ -LUA_FUNCTION_DEF (util, is_utf_outside_range); +LUA_FUNCTION_DEF(util, is_utf_outside_range); /*** * @function util.get_string_stats(str) * Returns table with number of letters and digits in string * @return {table} with string stats keys are "digits" and "letters" */ -LUA_FUNCTION_DEF (util, get_string_stats); +LUA_FUNCTION_DEF(util, get_string_stats); /*** * @function util.is_valid_utf8(str) * Returns true if a string is valid UTF8 string * @return {boolean} true if a string is spoofed */ -LUA_FUNCTION_DEF (util, is_valid_utf8); +LUA_FUNCTION_DEF(util, is_valid_utf8); /*** * @function util.has_obscured_unicode(str) * Returns true if a string has obscure UTF symbols (zero width spaces, order marks), ignores invalid utf characters * @return {boolean} true if a has obscured unicode characters (+ character and offset if found) */ -LUA_FUNCTION_DEF (util, has_obscured_unicode); +LUA_FUNCTION_DEF(util, has_obscured_unicode); /*** * @function util.readline([prompt]) * Returns string read from stdin with history and editing support * @return {string} string read from the input (with line endings stripped) */ -LUA_FUNCTION_DEF (util, readline); +LUA_FUNCTION_DEF(util, readline); /*** * @function util.readpassphrase([prompt]) * Returns string read from stdin disabling echo * @return {string} string read from the input (with line endings stripped) */ -LUA_FUNCTION_DEF (util, readpassphrase); +LUA_FUNCTION_DEF(util, readpassphrase); /*** * @function util.file_exists(file) * Checks if a specified file exists and is available for reading * @return {boolean,string} true if file exists + string error if not */ -LUA_FUNCTION_DEF (util, file_exists); +LUA_FUNCTION_DEF(util, file_exists); /*** * @function util.mkdir(dir[, recursive]) * Creates a specified directory * @return {boolean[,error]} true if directory has been created */ -LUA_FUNCTION_DEF (util, mkdir); +LUA_FUNCTION_DEF(util, mkdir); /*** * @function util.umask(mask) @@ -520,14 +520,14 @@ LUA_FUNCTION_DEF (util, mkdir); * number, e.g. 0x12 (since Lua does not support octal integrals) * @return {number} old umask */ -LUA_FUNCTION_DEF (util, umask); +LUA_FUNCTION_DEF(util, umask); /*** * @function util.isatty() * Returns if stdout is a tty * @return {boolean} true in case of output being tty */ -LUA_FUNCTION_DEF (util, isatty); +LUA_FUNCTION_DEF(util, isatty); /*** * @function util.pack(fmt, ...) @@ -584,7 +584,7 @@ LUA_FUNCTION_DEF (util, isatty); * * All padding is filled with zeros by string.pack (and ignored by unpack). */ -LUA_FUNCTION_DEF (util, pack); +LUA_FUNCTION_DEF(util, pack); /*** * @function util.packsize(fmt) @@ -592,7 +592,7 @@ LUA_FUNCTION_DEF (util, pack); * Returns size of the packed binary string returned for the same `fmt` argument * by @see util.pack */ -LUA_FUNCTION_DEF (util, packsize); +LUA_FUNCTION_DEF(util, packsize); /*** * @function util.unpack(fmt, s [, pos]) @@ -601,7 +601,7 @@ LUA_FUNCTION_DEF (util, packsize); * * @returns {multiple} list of unpacked values according to `fmt` */ -LUA_FUNCTION_DEF (util, unpack); +LUA_FUNCTION_DEF(util, unpack); /*** * @function util.caseless_hash(str[, seed]) @@ -610,7 +610,7 @@ LUA_FUNCTION_DEF (util, unpack); * @param seed mandatory seed (0xdeadbabe by default) * @return {int64} boxed int64_t */ -LUA_FUNCTION_DEF (util, caseless_hash); +LUA_FUNCTION_DEF(util, caseless_hash); /*** * @function util.caseless_hash_fast(str[, seed]) @@ -619,14 +619,14 @@ LUA_FUNCTION_DEF (util, caseless_hash); * @param seed mandatory seed (0xdeadbabe by default) * @return {number} number from int64_t */ -LUA_FUNCTION_DEF (util, caseless_hash_fast); +LUA_FUNCTION_DEF(util, caseless_hash_fast); /*** * @function util.get_hostname() * Returns hostname for this machine * @return {string} hostname */ -LUA_FUNCTION_DEF (util, get_hostname); +LUA_FUNCTION_DEF(util, get_hostname); /*** * @function util.parse_content_type(ct_string, mempool) @@ -641,7 +641,7 @@ LUA_FUNCTION_DEF (util, get_hostname); * @param {rspamd_mempool} mempool needed to store temporary data (e.g. task pool) * @return table or nil if cannot parse content type */ -LUA_FUNCTION_DEF (util, parse_content_type); +LUA_FUNCTION_DEF(util, parse_content_type); /*** * @function util.mime_header_encode(hdr) @@ -649,7 +649,7 @@ LUA_FUNCTION_DEF (util, parse_content_type); * @param {string} hdr input header * @return encoded header */ -LUA_FUNCTION_DEF (util, mime_header_encode); +LUA_FUNCTION_DEF(util, mime_header_encode); /*** * @function util.btc_polymod(input_values) @@ -657,7 +657,7 @@ LUA_FUNCTION_DEF (util, mime_header_encode); * @param {table|numbers} input_values * @return {boolean} true if polymod has been successful */ -LUA_FUNCTION_DEF (util, btc_polymod); +LUA_FUNCTION_DEF(util, btc_polymod); /*** * @function util.parse_smtp_date(str[, local_tz]) @@ -666,147 +666,143 @@ LUA_FUNCTION_DEF (util, btc_polymod); * @param {boolean} local_tz convert to local tz if `true` * @return {number} time as unix timestamp (converted to float) */ -LUA_FUNCTION_DEF (util, parse_smtp_date); +LUA_FUNCTION_DEF(util, parse_smtp_date); static const struct luaL_reg utillib_f[] = { - LUA_INTERFACE_DEF (util, create_event_base), - LUA_INTERFACE_DEF (util, load_rspamd_config), - LUA_INTERFACE_DEF (util, config_from_ucl), - LUA_INTERFACE_DEF (util, process_message), - LUA_INTERFACE_DEF (util, encode_base64), - LUA_INTERFACE_DEF (util, encode_qp), - LUA_INTERFACE_DEF (util, decode_qp), - LUA_INTERFACE_DEF (util, decode_base64), - LUA_INTERFACE_DEF (util, encode_base32), - LUA_INTERFACE_DEF (util, decode_base32), - LUA_INTERFACE_DEF (util, decode_url), - LUA_INTERFACE_DEF (util, tokenize_text), - LUA_INTERFACE_DEF (util, tanh), - LUA_INTERFACE_DEF (util, parse_html), - LUA_INTERFACE_DEF (util, levenshtein_distance), - LUA_INTERFACE_DEF (util, fold_header), - LUA_INTERFACE_DEF (util, is_uppercase), - LUA_INTERFACE_DEF (util, humanize_number), - LUA_INTERFACE_DEF (util, get_tld), - LUA_INTERFACE_DEF (util, glob), + LUA_INTERFACE_DEF(util, create_event_base), + LUA_INTERFACE_DEF(util, load_rspamd_config), + LUA_INTERFACE_DEF(util, config_from_ucl), + LUA_INTERFACE_DEF(util, process_message), + LUA_INTERFACE_DEF(util, encode_base64), + LUA_INTERFACE_DEF(util, encode_qp), + LUA_INTERFACE_DEF(util, decode_qp), + LUA_INTERFACE_DEF(util, decode_base64), + LUA_INTERFACE_DEF(util, encode_base32), + LUA_INTERFACE_DEF(util, decode_base32), + LUA_INTERFACE_DEF(util, decode_url), + LUA_INTERFACE_DEF(util, tokenize_text), + LUA_INTERFACE_DEF(util, tanh), + LUA_INTERFACE_DEF(util, parse_html), + LUA_INTERFACE_DEF(util, levenshtein_distance), + LUA_INTERFACE_DEF(util, fold_header), + LUA_INTERFACE_DEF(util, is_uppercase), + LUA_INTERFACE_DEF(util, humanize_number), + LUA_INTERFACE_DEF(util, get_tld), + LUA_INTERFACE_DEF(util, glob), {"parse_addr", lua_util_parse_mail_address}, - LUA_INTERFACE_DEF (util, parse_mail_address), - LUA_INTERFACE_DEF (util, strlen_utf8), - LUA_INTERFACE_DEF (util, lower_utf8), - LUA_INTERFACE_DEF (util, normalize_utf8), - LUA_INTERFACE_DEF (util, transliterate), - LUA_INTERFACE_DEF (util, strequal_caseless), - LUA_INTERFACE_DEF (util, strequal_caseless_utf8), - LUA_INTERFACE_DEF (util, get_ticks), - LUA_INTERFACE_DEF (util, get_time), - LUA_INTERFACE_DEF (util, time_to_string), - LUA_INTERFACE_DEF (util, stat), - LUA_INTERFACE_DEF (util, unlink), - LUA_INTERFACE_DEF (util, lock_file), - LUA_INTERFACE_DEF (util, unlock_file), - LUA_INTERFACE_DEF (util, create_file), - LUA_INTERFACE_DEF (util, close_file), - LUA_INTERFACE_DEF (util, random_hex), - LUA_INTERFACE_DEF (util, zstd_compress), - LUA_INTERFACE_DEF (util, zstd_decompress), - LUA_INTERFACE_DEF (util, gzip_compress), - LUA_INTERFACE_DEF (util, gzip_decompress), - LUA_INTERFACE_DEF (util, inflate), - LUA_INTERFACE_DEF (util, normalize_prob), - LUA_INTERFACE_DEF (util, caseless_hash), - LUA_INTERFACE_DEF (util, caseless_hash_fast), - LUA_INTERFACE_DEF (util, is_utf_spoofed), - LUA_INTERFACE_DEF (util, is_utf_mixed_script), - LUA_INTERFACE_DEF (util, is_utf_outside_range), - LUA_INTERFACE_DEF (util, get_string_stats), - LUA_INTERFACE_DEF (util, is_valid_utf8), - LUA_INTERFACE_DEF (util, has_obscured_unicode), - LUA_INTERFACE_DEF (util, readline), - LUA_INTERFACE_DEF (util, readpassphrase), - LUA_INTERFACE_DEF (util, file_exists), - LUA_INTERFACE_DEF (util, mkdir), - LUA_INTERFACE_DEF (util, umask), - LUA_INTERFACE_DEF (util, isatty), - LUA_INTERFACE_DEF (util, get_hostname), - LUA_INTERFACE_DEF (util, parse_content_type), - LUA_INTERFACE_DEF (util, mime_header_encode), - LUA_INTERFACE_DEF (util, pack), - LUA_INTERFACE_DEF (util, unpack), - LUA_INTERFACE_DEF (util, packsize), - LUA_INTERFACE_DEF (util, btc_polymod), - LUA_INTERFACE_DEF (util, parse_smtp_date), - {NULL, NULL} -}; - -LUA_FUNCTION_DEF (int64, tostring); -LUA_FUNCTION_DEF (int64, fromstring); -LUA_FUNCTION_DEF (int64, tonumber); -LUA_FUNCTION_DEF (int64, hex); + LUA_INTERFACE_DEF(util, parse_mail_address), + LUA_INTERFACE_DEF(util, strlen_utf8), + LUA_INTERFACE_DEF(util, lower_utf8), + LUA_INTERFACE_DEF(util, normalize_utf8), + LUA_INTERFACE_DEF(util, transliterate), + LUA_INTERFACE_DEF(util, strequal_caseless), + LUA_INTERFACE_DEF(util, strequal_caseless_utf8), + LUA_INTERFACE_DEF(util, get_ticks), + LUA_INTERFACE_DEF(util, get_time), + LUA_INTERFACE_DEF(util, time_to_string), + LUA_INTERFACE_DEF(util, stat), + LUA_INTERFACE_DEF(util, unlink), + LUA_INTERFACE_DEF(util, lock_file), + LUA_INTERFACE_DEF(util, unlock_file), + LUA_INTERFACE_DEF(util, create_file), + LUA_INTERFACE_DEF(util, close_file), + LUA_INTERFACE_DEF(util, random_hex), + LUA_INTERFACE_DEF(util, zstd_compress), + LUA_INTERFACE_DEF(util, zstd_decompress), + LUA_INTERFACE_DEF(util, gzip_compress), + LUA_INTERFACE_DEF(util, gzip_decompress), + LUA_INTERFACE_DEF(util, inflate), + LUA_INTERFACE_DEF(util, normalize_prob), + LUA_INTERFACE_DEF(util, caseless_hash), + LUA_INTERFACE_DEF(util, caseless_hash_fast), + LUA_INTERFACE_DEF(util, is_utf_spoofed), + LUA_INTERFACE_DEF(util, is_utf_mixed_script), + LUA_INTERFACE_DEF(util, is_utf_outside_range), + LUA_INTERFACE_DEF(util, get_string_stats), + LUA_INTERFACE_DEF(util, is_valid_utf8), + LUA_INTERFACE_DEF(util, has_obscured_unicode), + LUA_INTERFACE_DEF(util, readline), + LUA_INTERFACE_DEF(util, readpassphrase), + LUA_INTERFACE_DEF(util, file_exists), + LUA_INTERFACE_DEF(util, mkdir), + LUA_INTERFACE_DEF(util, umask), + LUA_INTERFACE_DEF(util, isatty), + LUA_INTERFACE_DEF(util, get_hostname), + LUA_INTERFACE_DEF(util, parse_content_type), + LUA_INTERFACE_DEF(util, mime_header_encode), + LUA_INTERFACE_DEF(util, pack), + LUA_INTERFACE_DEF(util, unpack), + LUA_INTERFACE_DEF(util, packsize), + LUA_INTERFACE_DEF(util, btc_polymod), + LUA_INTERFACE_DEF(util, parse_smtp_date), + {NULL, NULL}}; + +LUA_FUNCTION_DEF(int64, tostring); +LUA_FUNCTION_DEF(int64, fromstring); +LUA_FUNCTION_DEF(int64, tonumber); +LUA_FUNCTION_DEF(int64, hex); static const struct luaL_reg int64lib_f[] = { - LUA_INTERFACE_DEF (int64, fromstring), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(int64, fromstring), + {NULL, NULL}}; static const struct luaL_reg int64lib_m[] = { - LUA_INTERFACE_DEF (int64, tostring), - LUA_INTERFACE_DEF (int64, tonumber), - LUA_INTERFACE_DEF (int64, hex), + LUA_INTERFACE_DEF(int64, tostring), + LUA_INTERFACE_DEF(int64, tonumber), + LUA_INTERFACE_DEF(int64, hex), {"__tostring", lua_int64_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; -LUA_FUNCTION_DEF (ev_base, loop); +LUA_FUNCTION_DEF(ev_base, loop); static const struct luaL_reg ev_baselib_m[] = { - LUA_INTERFACE_DEF (ev_base, loop), + LUA_INTERFACE_DEF(ev_base, loop), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; static gint64 -lua_check_int64 (lua_State * L, gint pos) +lua_check_int64(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{int64}"); - luaL_argcheck (L, ud != NULL, pos, "'int64' expected"); - return ud ? *((gint64 *)ud) : 0LL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{int64}"); + luaL_argcheck(L, ud != NULL, pos, "'int64' expected"); + return ud ? *((gint64 *) ud) : 0LL; } static gint -lua_util_create_event_base (lua_State *L) +lua_util_create_event_base(lua_State *L) { LUA_TRACE_POINT; struct ev_loop **pev_base; - pev_base = lua_newuserdata (L, sizeof (struct ev_loop *)); - rspamd_lua_setclass (L, "rspamd{ev_base}", -1); - *pev_base = ev_loop_new (EVFLAG_SIGNALFD|EVBACKEND_ALL); + pev_base = lua_newuserdata(L, sizeof(struct ev_loop *)); + rspamd_lua_setclass(L, "rspamd{ev_base}", -1); + *pev_base = ev_loop_new(EVFLAG_SIGNALFD | EVBACKEND_ALL); return 1; } static gint -lua_util_load_rspamd_config (lua_State *L) +lua_util_load_rspamd_config(lua_State *L) { LUA_TRACE_POINT; struct rspamd_config *cfg, **pcfg; const gchar *cfg_name; - cfg_name = luaL_checkstring (L, 1); + cfg_name = luaL_checkstring(L, 1); if (cfg_name) { - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->lua_state = L; - if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL, FALSE, NULL)) { - msg_err_config ("cannot load config from %s", cfg_name); - lua_pushnil (L); + if (rspamd_config_read(cfg, cfg_name, NULL, NULL, NULL, FALSE, NULL)) { + msg_err_config("cannot load config from %s", cfg_name); + lua_pushnil(L); } else { - rspamd_config_post_load (cfg, 0); - pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_config_post_load(cfg, 0); + pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = cfg; } } @@ -815,44 +811,50 @@ lua_util_load_rspamd_config (lua_State *L) } static gint -parse_config_options (const char *str_options) +parse_config_options(const char *str_options) { gint ret = 0; gchar **vec; const gchar *str; guint i, l; - vec = g_strsplit_set (str_options, ",;", -1); + vec = g_strsplit_set(str_options, ",;", -1); if (vec) { - l = g_strv_length (vec); - for (i = 0; i < l; i ++) { + l = g_strv_length(vec); + for (i = 0; i < l; i++) { str = vec[i]; - if (g_ascii_strcasecmp (str, "INIT_URL") == 0) { + if (g_ascii_strcasecmp(str, "INIT_URL") == 0) { ret |= RSPAMD_CONFIG_INIT_URL; - } else if (g_ascii_strcasecmp (str, "INIT_LIBS") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_LIBS") == 0) { ret |= RSPAMD_CONFIG_INIT_LIBS; - } else if (g_ascii_strcasecmp (str, "INIT_SYMCACHE") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_SYMCACHE") == 0) { ret |= RSPAMD_CONFIG_INIT_SYMCACHE; - } else if (g_ascii_strcasecmp (str, "INIT_VALIDATE") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_VALIDATE") == 0) { ret |= RSPAMD_CONFIG_INIT_VALIDATE; - } else if (g_ascii_strcasecmp (str, "INIT_NO_TLD") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_NO_TLD") == 0) { ret |= RSPAMD_CONFIG_INIT_NO_TLD; - } else if (g_ascii_strcasecmp (str, "INIT_PRELOAD_MAPS") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_PRELOAD_MAPS") == 0) { ret |= RSPAMD_CONFIG_INIT_PRELOAD_MAPS; - } else { - msg_warn ("bad type: %s", str); + } + else { + msg_warn("bad type: %s", str); } } - g_strfreev (vec); + g_strfreev(vec); } return ret; } static gint -lua_util_config_from_ucl (lua_State *L) +lua_util_config_from_ucl(lua_State *L) { LUA_TRACE_POINT; struct rspamd_config *cfg = NULL, **pcfg; @@ -863,40 +865,40 @@ lua_util_config_from_ucl (lua_State *L) gint int_options = 0; - obj = ucl_object_lua_import (L, 1); - if (lua_gettop (L) == 2) { - if (lua_type (L, 2) == LUA_TSTRING) { - str_options = lua_tostring (L, 2); + obj = ucl_object_lua_import(L, 1); + if (lua_gettop(L) == 2) { + if (lua_type(L, 2) == LUA_TSTRING) { + str_options = lua_tostring(L, 2); int_options = parse_config_options(str_options); } else { - msg_err ("config_from_ucl: second parameter is expected to be string"); - ucl_object_unref (obj); - lua_pushnil (L); + msg_err("config_from_ucl: second parameter is expected to be string"); + ucl_object_unref(obj); + lua_pushnil(L); } } if (obj) { - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->lua_state = L; cfg->rcl_obj = obj; - top = rspamd_rcl_config_init (cfg, NULL); + top = rspamd_rcl_config_init(cfg, NULL); - if (!rspamd_rcl_parse (top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { - msg_err ("rcl parse error: %s", err->message); - ucl_object_unref (obj); - lua_pushnil (L); + if (!rspamd_rcl_parse(top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { + msg_err("rcl parse error: %s", err->message); + ucl_object_unref(obj); + lua_pushnil(L); } else { if (int_options & RSPAMD_CONFIG_INIT_LIBS) { - cfg->libs_ctx = rspamd_init_libs (); + cfg->libs_ctx = rspamd_init_libs(); } - rspamd_config_post_load (cfg, int_options); - pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_config_post_load(cfg, int_options); + pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = cfg; } } @@ -905,78 +907,78 @@ lua_util_config_from_ucl (lua_State *L) } static gboolean -lua_util_task_fin (struct rspamd_task *task, void *ud) +lua_util_task_fin(struct rspamd_task *task, void *ud) { ucl_object_t **target = ud; - *target = rspamd_protocol_write_ucl (task, RSPAMD_PROTOCOL_DEFAULT); - rdns_resolver_release (task->resolver->r); + *target = rspamd_protocol_write_ucl(task, RSPAMD_PROTOCOL_DEFAULT); + rdns_resolver_release(task->resolver->r); return TRUE; } static gint -lua_util_process_message (lua_State *L) +lua_util_process_message(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *message; gsize mlen; struct rspamd_task *task; struct ev_loop *base; ucl_object_t *res = NULL; - message = luaL_checklstring (L, 2, &mlen); + message = luaL_checklstring(L, 2, &mlen); if (cfg != NULL && message != NULL) { - base = ev_loop_new (EVFLAG_SIGNALFD|EVBACKEND_ALL); - rspamd_init_filters (cfg, false, false); - task = rspamd_task_new (NULL, cfg, NULL, NULL, base, FALSE); - task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen); - rspamd_strlcpy ((gpointer)task->msg.begin, message, mlen); + base = ev_loop_new(EVFLAG_SIGNALFD | EVBACKEND_ALL); + rspamd_init_filters(cfg, false, false); + task = rspamd_task_new(NULL, cfg, NULL, NULL, base, FALSE); + task->msg.begin = rspamd_mempool_alloc(task->task_pool, mlen); + rspamd_strlcpy((gpointer) task->msg.begin, message, mlen); task->msg.len = mlen; task->fin_callback = lua_util_task_fin; task->fin_arg = &res; - task->resolver = rspamd_dns_resolver_init (NULL, base, cfg); - task->s = rspamd_session_create (task->task_pool, rspamd_task_fin, - NULL, (event_finalizer_t)rspamd_task_free, task); + task->resolver = rspamd_dns_resolver_init(NULL, base, cfg); + task->s = rspamd_session_create(task->task_pool, rspamd_task_fin, + NULL, (event_finalizer_t) rspamd_task_free, task); - if (!rspamd_task_load_message (task, NULL, message, mlen)) { - lua_pushnil (L); + if (!rspamd_task_load_message(task, NULL, message, mlen)) { + lua_pushnil(L); } else { - if (rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) { - ev_loop (base, 0); + if (rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL)) { + ev_loop(base, 0); if (res != NULL) { - ucl_object_push_lua (L, res, true); + ucl_object_push_lua(L, res, true); - ucl_object_unref (res); + ucl_object_unref(res); } else { - ucl_object_push_lua (L, - rspamd_protocol_write_ucl (task, RSPAMD_PROTOCOL_DEFAULT), - true); - rdns_resolver_release (task->resolver->r); - rspamd_session_destroy (task->s); + ucl_object_push_lua(L, + rspamd_protocol_write_ucl(task, RSPAMD_PROTOCOL_DEFAULT), + true); + rdns_resolver_release(task->resolver->r); + rspamd_session_destroy(task->s); } } else { - lua_pushnil (L); + lua_pushnil(L); } } - ev_loop_destroy (base); + ev_loop_destroy(base); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_util_encode_base64 (lua_State *L) +lua_util_encode_base64(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -986,11 +988,11 @@ lua_util_encode_base64 (lua_State *L) guint str_lim = 0; gboolean fold = FALSE; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -998,50 +1000,50 @@ lua_util_encode_base64 (lua_State *L) } } - if (lua_gettop (L) > 1) { - str_lim = luaL_checknumber (L, 2); + if (lua_gettop(L) > 1) { + str_lim = luaL_checknumber(L, 2); fold = !!(str_lim > 0); } if (s == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { if (fold) { - out = rspamd_encode_base64 (s, inlen, str_lim, &outlen); + out = rspamd_encode_base64(s, inlen, str_lim, &outlen); } else { enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF; - if (lua_type (L, 3) == LUA_TSTRING) { - const gchar *how_str = lua_tostring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + const gchar *how_str = lua_tostring(L, 3); - if (g_ascii_strcasecmp (how_str, "cr") == 0) { + if (g_ascii_strcasecmp(how_str, "cr") == 0) { how = RSPAMD_TASK_NEWLINES_CR; } - else if (g_ascii_strcasecmp (how_str, "lf") == 0) { + else if (g_ascii_strcasecmp(how_str, "lf") == 0) { how = RSPAMD_TASK_NEWLINES_LF; } - else if (g_ascii_strcasecmp (how_str, "crlf") != 0) { - return luaL_error (L, "invalid newline style: %s", how_str); + else if (g_ascii_strcasecmp(how_str, "crlf") != 0) { + return luaL_error(L, "invalid newline style: %s", how_str); } } - out = rspamd_encode_base64_fold (s, inlen, str_lim, &outlen, how); + out = rspamd_encode_base64_fold(s, inlen, str_lim, &outlen, how); } if (out != NULL) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = out; t->len = outlen; /* Need destruction */ t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } } @@ -1049,7 +1051,7 @@ lua_util_encode_base64 (lua_State *L) } static gint -lua_util_encode_qp (lua_State *L) +lua_util_encode_qp(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -1058,11 +1060,11 @@ lua_util_encode_qp (lua_State *L) gsize inlen, outlen; guint str_lim = 0; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1070,42 +1072,42 @@ lua_util_encode_qp (lua_State *L) } } - if (lua_gettop (L) > 1) { - str_lim = luaL_checknumber (L, 2); + if (lua_gettop(L) > 1) { + str_lim = luaL_checknumber(L, 2); } if (s == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF; - if (lua_type (L, 3) == LUA_TSTRING) { - const gchar *how_str = lua_tostring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + const gchar *how_str = lua_tostring(L, 3); - if (g_ascii_strcasecmp (how_str, "cr") == 0) { + if (g_ascii_strcasecmp(how_str, "cr") == 0) { how = RSPAMD_TASK_NEWLINES_CR; } - else if (g_ascii_strcasecmp (how_str, "lf") == 0) { + else if (g_ascii_strcasecmp(how_str, "lf") == 0) { how = RSPAMD_TASK_NEWLINES_LF; } - else if (g_ascii_strcasecmp (how_str, "crlf") != 0) { - return luaL_error (L, "invalid newline style: %s", how_str); + else if (g_ascii_strcasecmp(how_str, "crlf") != 0) { + return luaL_error(L, "invalid newline style: %s", how_str); } } - out = rspamd_encode_qp_fold (s, inlen, str_lim, &outlen, how); + out = rspamd_encode_qp_fold(s, inlen, str_lim, &outlen, how); if (out != NULL) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = out; t->len = outlen; /* Need destruction */ t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } } @@ -1113,7 +1115,7 @@ lua_util_encode_qp (lua_State *L) } static gint -lua_util_decode_qp (lua_State *L) +lua_util_decode_qp(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t, *out; @@ -1121,11 +1123,11 @@ lua_util_decode_qp (lua_State *L) gsize inlen = 0; gssize outlen; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1134,14 +1136,14 @@ lua_util_decode_qp (lua_State *L) } if (s == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - out = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); - out->start = g_malloc (inlen + 1); + out = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); + out->start = g_malloc(inlen + 1); out->flags = RSPAMD_TEXT_FLAG_OWN; - outlen = rspamd_decode_qp_buf (s, inlen, (gchar *)out->start, inlen + 1); + outlen = rspamd_decode_qp_buf(s, inlen, (gchar *) out->start, inlen + 1); if (outlen > 0) { out->len = outlen; @@ -1150,8 +1152,8 @@ lua_util_decode_qp (lua_State *L) /* * It removes out and frees memory on gc due to RSPAMD_TEXT_FLAG_OWN */ - lua_pop (L, 1); - lua_pushnil (L); + lua_pop(L, 1); + lua_pushnil(L); } } @@ -1159,18 +1161,18 @@ lua_util_decode_qp (lua_State *L) } static gint -lua_util_decode_base64 (lua_State *L) +lua_util_decode_base64(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; const gchar *s = NULL; gsize inlen = 0, outlen; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1179,25 +1181,25 @@ lua_util_decode_base64 (lua_State *L) } if (s != NULL) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->len = (inlen / 4) * 3 + 3; - t->start = g_malloc (t->len); + t->start = g_malloc(t->len); - rspamd_cryptobox_base64_decode (s, inlen, (guchar *)t->start, - &outlen); + rspamd_cryptobox_base64_decode(s, inlen, (guchar *) t->start, + &outlen); t->len = outlen; t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_util_encode_base32 (lua_State *L) +lua_util_encode_base32(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -1206,11 +1208,11 @@ lua_util_encode_base32 (lua_State *L) enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; gsize inlen, outlen; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1218,31 +1220,31 @@ lua_util_encode_base32 (lua_State *L) } } - if (lua_type (L, 2) == LUA_TSTRING) { - btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2)); if (btype == RSPAMD_BASE32_INVALID) { - return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2)); } } if (s == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } else { - out = rspamd_encode_base32 (s, inlen, btype); + out = rspamd_encode_base32(s, inlen, btype); if (out != NULL) { - t = lua_newuserdata (L, sizeof (*t)); - outlen = strlen (out); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + outlen = strlen(out); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = out; t->len = outlen; /* Need destruction */ t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } } @@ -1250,7 +1252,7 @@ lua_util_encode_base32 (lua_State *L) } static gint -lua_util_decode_base32 (lua_State *L) +lua_util_decode_base32(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -1258,11 +1260,11 @@ lua_util_decode_base32 (lua_State *L) gsize inlen, outlen; enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1270,52 +1272,52 @@ lua_util_decode_base32 (lua_State *L) } } - if (lua_type (L, 2) == LUA_TSTRING) { - btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2)); if (btype == RSPAMD_BASE32_INVALID) { - return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2)); } } if (s != NULL) { guchar *decoded; - decoded = rspamd_decode_base32 (s, inlen, &outlen, btype); + decoded = rspamd_decode_base32(s, inlen, &outlen, btype); if (decoded) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); - t->start = (const gchar *)decoded; + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); + t->start = (const gchar *) decoded; t->len = outlen; t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_util_decode_url (lua_State *L) +lua_util_decode_url(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t != NULL) { struct rspamd_lua_text *out = lua_new_text(L, NULL, t->len, TRUE); - out->len = rspamd_url_decode ((char *)out->start, t->start, t->len); + out->len = rspamd_url_decode((char *) out->start, t->start, t->len); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -1323,30 +1325,30 @@ lua_util_decode_url (lua_State *L) static gint -lua_util_tokenize_text (lua_State *L) +lua_util_tokenize_text(lua_State *L) { - return lua_parsers_tokenize_text (L); + return lua_parsers_tokenize_text(L); } static gint -lua_util_tanh (lua_State *L) +lua_util_tanh(lua_State *L) { LUA_TRACE_POINT; - gdouble in = luaL_checknumber (L, 1); + gdouble in = luaL_checknumber(L, 1); - lua_pushnumber (L, tanh (in)); + lua_pushnumber(L, tanh(in)); return 1; } static gint -lua_util_parse_html (lua_State *L) +lua_util_parse_html(lua_State *L) { - return lua_parsers_parse_html (L); + return lua_parsers_parse_html(L); } static gint -lua_util_levenshtein_distance (lua_State *L) +lua_util_levenshtein_distance(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t1, *t2; @@ -1355,81 +1357,81 @@ lua_util_levenshtein_distance (lua_State *L) t1 = lua_check_text_or_string(L, 1); t2 = lua_check_text_or_string(L, 2); - if (lua_isnumber (L, 3)) { + if (lua_isnumber(L, 3)) { replace_cost = lua_tointeger(L, 3); } if (t1 && t2) { - dist = rspamd_strings_levenshtein_distance (t1->start, t1->len, t2->start, t2->len, - replace_cost); + dist = rspamd_strings_levenshtein_distance(t1->start, t1->len, t2->start, t2->len, + replace_cost); } else { return luaL_error(L, "invalid arguments"); } - lua_pushinteger (L, dist); + lua_pushinteger(L, dist); return 1; } static gint -lua_util_fold_header (lua_State *L) +lua_util_fold_header(lua_State *L) { LUA_TRACE_POINT; const gchar *how, *stop_chars = NULL; struct rspamd_lua_text *name, *value; GString *folded; - name = lua_check_text_or_string (L, 1); - value = lua_check_text_or_string (L, 2); + name = lua_check_text_or_string(L, 1); + value = lua_check_text_or_string(L, 2); if (name && value) { - if (lua_isstring (L, 3)) { + if (lua_isstring(L, 3)) { - how = lua_tostring (L, 3); + how = lua_tostring(L, 3); - if (lua_isstring (L, 4)) { - stop_chars = lua_tostring (L, 4); + if (lua_isstring(L, 4)) { + stop_chars = lua_tostring(L, 4); } - if (strcmp (how, "cr") == 0) { - folded = rspamd_header_value_fold (name->start, name->len, - value->start, value->len, - 0, - RSPAMD_TASK_NEWLINES_CR, stop_chars); + if (strcmp(how, "cr") == 0) { + folded = rspamd_header_value_fold(name->start, name->len, + value->start, value->len, + 0, + RSPAMD_TASK_NEWLINES_CR, stop_chars); } - else if (strcmp (how, "lf") == 0) { - folded = rspamd_header_value_fold (name->start, name->len, - value->start, value->len, 0, - RSPAMD_TASK_NEWLINES_LF, stop_chars); + else if (strcmp(how, "lf") == 0) { + folded = rspamd_header_value_fold(name->start, name->len, + value->start, value->len, 0, + RSPAMD_TASK_NEWLINES_LF, stop_chars); } else { - folded = rspamd_header_value_fold (name->start, name->len, - value->start, value->len, 0, - RSPAMD_TASK_NEWLINES_CRLF, stop_chars); + folded = rspamd_header_value_fold(name->start, name->len, + value->start, value->len, 0, + RSPAMD_TASK_NEWLINES_CRLF, stop_chars); } } else { - folded = rspamd_header_value_fold (name->start, name->len, - value->start, value->len, 0, - RSPAMD_TASK_NEWLINES_CRLF, stop_chars); + folded = rspamd_header_value_fold(name->start, name->len, + value->start, value->len, 0, + RSPAMD_TASK_NEWLINES_CRLF, stop_chars); } if (folded) { - lua_pushlstring (L, folded->str, folded->len); - g_string_free (folded, TRUE); + lua_pushlstring(L, folded->str, folded->len); + g_string_free(folded, TRUE); return 1; } } - lua_pushnil (L); + lua_pushnil(L); return 1; } static gint -lua_util_is_uppercase (lua_State *L) +lua_util_is_uppercase(lua_State *L) { LUA_TRACE_POINT; gint32 i = 0; @@ -1439,65 +1441,65 @@ lua_util_is_uppercase (lua_State *L) struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); if (t) { while (i >= 0 && i < t->len) { - U8_NEXT (t->start, i, t->len, uc); + U8_NEXT(t->start, i, t->len, uc); if (uc < 0) { break; } - if (u_isupper (uc)) { + if (u_isupper(uc)) { nuc++; } - else if (u_islower (uc)) { + else if (u_islower(uc)) { nlc++; } } } if (nuc > 0 && nlc == 0) { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } return 1; } static gint -lua_util_humanize_number (lua_State *L) +lua_util_humanize_number(lua_State *L) { LUA_TRACE_POINT; gint64 number = luaL_checkinteger(L, 1); gchar numbuf[32]; - rspamd_snprintf (numbuf, sizeof (numbuf), "%hL", number); - lua_pushstring (L, numbuf); + rspamd_snprintf(numbuf, sizeof(numbuf), "%hL", number); + lua_pushstring(L, numbuf); return 1; } static gint -lua_util_get_tld (lua_State *L) +lua_util_get_tld(lua_State *L) { LUA_TRACE_POINT; const gchar *host; gsize hostlen; rspamd_ftok_t tld; - host = luaL_checklstring (L, 1, &hostlen); + host = luaL_checklstring(L, 1, &hostlen); if (host) { - if (!rspamd_url_find_tld (host, hostlen, &tld)) { - lua_pushlstring (L, host, hostlen); + if (!rspamd_url_find_tld(host, hostlen, &tld)) { + lua_pushlstring(L, host, hostlen); } else { - lua_pushlstring (L, tld.begin, tld.len); + lua_pushlstring(L, tld.begin, tld.len); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -1505,76 +1507,76 @@ lua_util_get_tld (lua_State *L) static gint -lua_util_glob (lua_State *L) +lua_util_glob(lua_State *L) { LUA_TRACE_POINT; const gchar *pattern; glob_t gl; gint top, i, flags = 0; - top = lua_gettop (L); - memset (&gl, 0, sizeof (gl)); + top = lua_gettop(L); + memset(&gl, 0, sizeof(gl)); - for (i = 1; i <= top; i ++, flags |= GLOB_APPEND) { - pattern = luaL_checkstring (L, i); + for (i = 1; i <= top; i++, flags |= GLOB_APPEND) { + pattern = luaL_checkstring(L, i); if (pattern) { - if (glob (pattern, flags, NULL, &gl) != 0) { + if (glob(pattern, flags, NULL, &gl) != 0) { /* There is no way to return error here, so just create an table */ - lua_createtable (L, 0, 0); - globfree (&gl); + lua_createtable(L, 0, 0); + globfree(&gl); return 1; } } } - lua_createtable (L, gl.gl_pathc, 0); + lua_createtable(L, gl.gl_pathc, 0); /* Push results */ - for (i = 0; i < (gint)gl.gl_pathc; i ++) { - lua_pushstring (L, gl.gl_pathv[i]); - lua_rawseti (L, -2, i + 1); + for (i = 0; i < (gint) gl.gl_pathc; i++) { + lua_pushstring(L, gl.gl_pathv[i]); + lua_rawseti(L, -2, i + 1); } - globfree (&gl); + globfree(&gl); return 1; } static gint -lua_util_parse_mail_address (lua_State *L) +lua_util_parse_mail_address(lua_State *L) { - return lua_parsers_parse_mail_address (L); + return lua_parsers_parse_mail_address(L); } static gint -lua_util_strlen_utf8 (lua_State *L) +lua_util_strlen_utf8(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t) { gint32 i = 0, nchars = 0; UChar32 uc; while (i < t->len) { - U8_NEXT ((guint8 *)t->start, i, t->len, uc); - nchars ++; + U8_NEXT((guint8 *) t->start, i, t->len, uc); + nchars++; } - lua_pushinteger (L, nchars); + lua_pushinteger(L, nchars); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_lower_utf8 (lua_State *L) +lua_util_lower_utf8(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -1584,55 +1586,55 @@ lua_util_lower_utf8 (lua_State *L) UBool err = 0; gint32 i = 0, j = 0; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t) { - dst = g_malloc (t->len); + dst = g_malloc(t->len); while (i < t->len && err == 0) { - U8_NEXT ((guint8 *) t->start, i, t->len, uc); - uc = u_tolower (uc); - U8_APPEND (dst, j, t->len, uc, err); + U8_NEXT((guint8 *) t->start, i, t->len, uc); + uc = u_tolower(uc); + U8_APPEND(dst, j, t->len, uc, err); } - if (lua_isstring (L, 1)) { - lua_pushlstring (L, dst, j); - g_free (dst); + if (lua_isstring(L, 1)) { + lua_pushlstring(L, dst, j); + g_free(dst); } else { - t = lua_new_text (L, dst, j, FALSE); + t = lua_new_text(L, dst, j, FALSE); /* We have actually allocated text data before */ t->flags |= RSPAMD_TEXT_FLAG_OWN; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_normalize_utf8 (lua_State *L) +lua_util_normalize_utf8(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - bool is_text = lua_type (L, 1) == LUA_TUSERDATA; + bool is_text = lua_type(L, 1) == LUA_TUSERDATA; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (!t) { return luaL_error(L, "invalid arguments"); } - char *cpy = g_malloc (t->len + 1); - memcpy (cpy, t->start, t->len); + char *cpy = g_malloc(t->len + 1); + memcpy(cpy, t->start, t->len); cpy[t->len] = '\0'; gsize len = t->len; enum rspamd_utf8_normalise_result res = rspamd_normalise_unicode_inplace(cpy, &len); if (is_text) { - struct rspamd_lua_text *out = lua_new_text (L, cpy, len, FALSE); + struct rspamd_lua_text *out = lua_new_text(L, cpy, len, FALSE); out->flags |= RSPAMD_TEXT_FLAG_OWN; } else { @@ -1646,11 +1648,11 @@ lua_util_normalize_utf8 (lua_State *L) } static gint -lua_util_transliterate (lua_State *L) +lua_util_transliterate(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (!t) { return luaL_error(L, "invalid arguments"); @@ -1664,177 +1666,177 @@ lua_util_transliterate (lua_State *L) } static gint -lua_util_strequal_caseless (lua_State *L) +lua_util_strequal_caseless(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t1, *t2; gint ret = -1; - t1 = lua_check_text_or_string (L, 1); - t2 = lua_check_text_or_string (L, 2); + t1 = lua_check_text_or_string(L, 1); + t2 = lua_check_text_or_string(L, 2); if (t1 && t2) { if (t1->len == t2->len) { - ret = rspamd_lc_cmp (t1->start, t2->start, t1->len); + ret = rspamd_lc_cmp(t1->start, t2->start, t1->len); } else { ret = t1->len - t2->len; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, (ret == 0) ? true : false); + lua_pushboolean(L, (ret == 0) ? true : false); return 1; } static gint -lua_util_strequal_caseless_utf8 (lua_State *L) +lua_util_strequal_caseless_utf8(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t1, *t2; gint ret = -1; - t1 = lua_check_text_or_string (L, 1); - t2 = lua_check_text_or_string (L, 2); + t1 = lua_check_text_or_string(L, 1); + t2 = lua_check_text_or_string(L, 2); if (t1 && t2) { ret = rspamd_utf8_strcmp_sizes(t1->start, t1->len, t2->start, t2->len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, (ret == 0) ? true : false); + lua_pushboolean(L, (ret == 0) ? true : false); return 1; } static gint -lua_util_get_ticks (lua_State *L) +lua_util_get_ticks(lua_State *L) { LUA_TRACE_POINT; gdouble ticks; gboolean rdtsc = FALSE; - if (lua_isboolean (L, 1)) { - rdtsc = lua_toboolean (L, 1); + if (lua_isboolean(L, 1)) { + rdtsc = lua_toboolean(L, 1); } - ticks = rspamd_get_ticks (rdtsc); - lua_pushnumber (L, ticks); + ticks = rspamd_get_ticks(rdtsc); + lua_pushnumber(L, ticks); return 1; } static gint -lua_util_get_time (lua_State *L) +lua_util_get_time(lua_State *L) { LUA_TRACE_POINT; - lua_pushnumber (L, ev_time ()); + lua_pushnumber(L, ev_time()); return 1; } static gint -lua_util_time_to_string (lua_State *L) +lua_util_time_to_string(lua_State *L) { LUA_TRACE_POINT; gdouble seconds; char timebuf[128]; - if (lua_isnumber (L, 1)) { - seconds = lua_tonumber (L, 1); + if (lua_isnumber(L, 1)) { + seconds = lua_tonumber(L, 1); } else { - seconds = ev_time (); + seconds = ev_time(); } - rspamd_http_date_format (timebuf, sizeof (timebuf), seconds); - lua_pushstring (L, timebuf); + rspamd_http_date_format(timebuf, sizeof(timebuf), seconds); + lua_pushstring(L, timebuf); return 1; } static gint -lua_util_stat (lua_State *L) +lua_util_stat(lua_State *L) { LUA_TRACE_POINT; const gchar *fpath; struct stat st; - fpath = luaL_checkstring (L, 1); + fpath = luaL_checkstring(L, 1); if (fpath) { - if (stat (fpath, &st) == -1) { - lua_pushstring (L, strerror (errno)); - lua_pushnil (L); + if (stat(fpath, &st) == -1) { + lua_pushstring(L, strerror(errno)); + lua_pushnil(L); } else { - lua_pushnil (L); - lua_createtable (L, 0, 3); + lua_pushnil(L); + lua_createtable(L, 0, 3); - lua_pushstring (L, "size"); - lua_pushinteger (L, st.st_size); - lua_settable (L, -3); + lua_pushstring(L, "size"); + lua_pushinteger(L, st.st_size); + lua_settable(L, -3); - lua_pushstring (L, "mtime"); - lua_pushinteger (L, st.st_mtime); - lua_settable (L, -3); + lua_pushstring(L, "mtime"); + lua_pushinteger(L, st.st_mtime); + lua_settable(L, -3); - lua_pushstring (L, "type"); - if (S_ISREG (st.st_mode)) { - lua_pushstring (L, "regular"); + lua_pushstring(L, "type"); + if (S_ISREG(st.st_mode)) { + lua_pushstring(L, "regular"); } - else if (S_ISDIR (st.st_mode)) { - lua_pushstring (L, "directory"); + else if (S_ISDIR(st.st_mode)) { + lua_pushstring(L, "directory"); } else { - lua_pushstring (L, "special"); + lua_pushstring(L, "special"); } - lua_settable (L, -3); + lua_settable(L, -3); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; } static gint -lua_util_unlink (lua_State *L) +lua_util_unlink(lua_State *L) { LUA_TRACE_POINT; const gchar *fpath; gint ret; - fpath = luaL_checkstring (L, 1); + fpath = luaL_checkstring(L, 1); if (fpath) { - ret = unlink (fpath); + ret = unlink(fpath); if (ret == -1) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_lock_file (lua_State *L) +lua_util_lock_file(lua_State *L) { LUA_TRACE_POINT; const gchar *fpath; @@ -1846,54 +1848,53 @@ lua_util_lock_file (lua_State *L) .l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = 0, - .l_len = 0 - }; + .l_len = 0}; #endif - fpath = luaL_checkstring (L, 1); + fpath = luaL_checkstring(L, 1); if (fpath) { - if (lua_isnumber (L, 2)) { - fd = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + fd = lua_tointeger(L, 2); } else { - fd = open (fpath, O_RDONLY); + fd = open(fpath, O_RDONLY); own = TRUE; } if (fd == -1) { - lua_pushnil (L); - lua_pushstring (L, strerror (errno)); + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); return 2; } #if HAVE_FLOCK - if (flock (fd, LOCK_EX) == -1) { + if (flock(fd, LOCK_EX) == -1) { #else - if (fcntl (fd, F_SETLKW, &fl) == -1) { + if (fcntl(fd, F_SETLKW, &fl) == -1) { #endif - lua_pushnil (L); - lua_pushstring (L, strerror (errno)); + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); if (own) { - close (fd); + close(fd); } return 2; } - lua_pushinteger (L, fd); + lua_pushinteger(L, fd); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_unlock_file (lua_State *L) +lua_util_unlock_file(lua_State *L) { LUA_TRACE_POINT; gint fd = -1, ret, serrno; @@ -1904,201 +1905,200 @@ lua_util_unlock_file (lua_State *L) .l_type = F_UNLCK, .l_whence = SEEK_SET, .l_start = 0, - .l_len = 0 - }; + .l_len = 0}; #endif - if (lua_isnumber (L, 1)) { - fd = lua_tointeger (L, 1); + if (lua_isnumber(L, 1)) { + fd = lua_tointeger(L, 1); - if (lua_isboolean (L, 2)) { - do_close = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + do_close = lua_toboolean(L, 2); } #if HAVE_FLOCK - ret = flock (fd, LOCK_UN); + ret = flock(fd, LOCK_UN); #else - ret = fcntl (fd, F_SETLKW, &fl); + ret = fcntl(fd, F_SETLKW, &fl); #endif if (do_close) { serrno = errno; - close (fd); + close(fd); errno = serrno; } if (ret == -1) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_create_file (lua_State *L) +lua_util_create_file(lua_State *L) { LUA_TRACE_POINT; gint fd, mode = 00644; const gchar *fpath; - fpath = luaL_checkstring (L, 1); + fpath = luaL_checkstring(L, 1); if (fpath) { - if (lua_isnumber (L, 2)) { - mode = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + mode = lua_tointeger(L, 2); } - fd = rspamd_file_xopen (fpath, O_RDWR | O_CREAT | O_TRUNC, mode, 0); + fd = rspamd_file_xopen(fpath, O_RDWR | O_CREAT | O_TRUNC, mode, 0); if (fd == -1) { - lua_pushnil (L); - lua_pushstring (L, strerror (errno)); + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushinteger (L, fd); + lua_pushinteger(L, fd); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_close_file (lua_State *L) +lua_util_close_file(lua_State *L) { LUA_TRACE_POINT; gint fd = -1; - if (lua_isnumber (L, 1)) { - fd = lua_tointeger (L, 1); + if (lua_isnumber(L, 1)) { + fd = lua_tointeger(L, 1); - if (close (fd) == -1) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + if (close(fd) == -1) { + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_random_hex (lua_State *L) +lua_util_random_hex(lua_State *L) { LUA_TRACE_POINT; gchar *buf; gint buflen; - buflen = lua_tointeger (L, 1); + buflen = lua_tointeger(L, 1); if (buflen <= 0) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - buf = g_malloc (buflen); - rspamd_random_hex (buf, buflen); - lua_pushlstring (L, buf, buflen); - g_free (buf); + buf = g_malloc(buflen); + rspamd_random_hex(buf, buflen); + lua_pushlstring(L, buf, buflen); + g_free(buf); return 1; } static gint -lua_util_zstd_compress (lua_State *L) +lua_util_zstd_compress(lua_State *L) { - return lua_compress_zstd_compress (L); + return lua_compress_zstd_compress(L); } static gint -lua_util_zstd_decompress (lua_State *L) +lua_util_zstd_decompress(lua_State *L) { - return lua_compress_zstd_decompress (L); + return lua_compress_zstd_decompress(L); } static gint -lua_util_gzip_compress (lua_State *L) +lua_util_gzip_compress(lua_State *L) { - return lua_compress_zlib_compress (L); + return lua_compress_zlib_compress(L); } static gint -lua_util_gzip_decompress (lua_State *L) +lua_util_gzip_decompress(lua_State *L) { - return lua_compress_zlib_decompress (L, true); + return lua_compress_zlib_decompress(L, true); } static gint -lua_util_inflate (lua_State *L) +lua_util_inflate(lua_State *L) { - return lua_compress_zlib_decompress (L, false); + return lua_compress_zlib_decompress(L, false); } static gint -lua_util_normalize_prob (lua_State *L) +lua_util_normalize_prob(lua_State *L) { LUA_TRACE_POINT; gdouble x, bias = 0.5; - x = lua_tonumber (L, 1); + x = lua_tonumber(L, 1); - if (lua_type (L, 2) == LUA_TNUMBER) { - bias = lua_tonumber (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + bias = lua_tonumber(L, 2); } - lua_pushnumber (L, rspamd_normalize_probability (x, bias)); + lua_pushnumber(L, rspamd_normalize_probability(x, bias)); return 1; } static gint -lua_util_caseless_hash (lua_State *L) +lua_util_caseless_hash(lua_State *L) { LUA_TRACE_POINT; guint64 seed = 0xdeadbabe, h; struct rspamd_lua_text *t = NULL; gint64 *r; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t == NULL || t->start == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TNUMBER) { - seed = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + seed = lua_tointeger(L, 2); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - seed = lua_check_int64 (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + seed = lua_check_int64(L, 2); } - h = rspamd_icase_hash (t->start, t->len, seed); - r = lua_newuserdata (L, sizeof (*r)); + h = rspamd_icase_hash(t->start, t->len, seed); + r = lua_newuserdata(L, sizeof(*r)); *r = h; - rspamd_lua_setclass (L, "rspamd{int64}", -1); + rspamd_lua_setclass(L, "rspamd{int64}", -1); return 1; } static gint -lua_util_caseless_hash_fast (lua_State *L) +lua_util_caseless_hash_fast(lua_State *L) { LUA_TRACE_POINT; guint64 seed = 0xdeadbabe, h; @@ -2108,17 +2108,17 @@ lua_util_caseless_hash_fast (lua_State *L) double d; } u; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t == NULL || t->start == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TNUMBER) { - seed = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + seed = lua_tointeger(L, 2); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - seed = lua_check_int64 (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + seed = lua_check_int64(L, 2); } /* @@ -2126,84 +2126,84 @@ lua_util_caseless_hash_fast (lua_State *L) * it is still fine for practical applications */ - h = rspamd_icase_hash (t->start, t->len, seed); + h = rspamd_icase_hash(t->start, t->len, seed); u.i = G_GUINT64_CONSTANT(0x3FF) << 52 | h >> 12; - lua_pushnumber (L, u.d - 1.0); + lua_pushnumber(L, u.d - 1.0); return 1; } static gint -lua_util_is_utf_spoofed (lua_State *L) +lua_util_is_utf_spoofed(lua_State *L) { LUA_TRACE_POINT; gsize l1, l2; gint ret, nres = 2; - const gchar *s1 = lua_tolstring (L, 1, &l1), - *s2 = lua_tolstring (L, 2, &l2); + const gchar *s1 = lua_tolstring(L, 1, &l1), + *s2 = lua_tolstring(L, 2, &l2); static USpoofChecker *spc, *spc_sgl; UErrorCode uc_err = U_ZERO_ERROR; if (s1 && s2) { if (spc == NULL) { - spc = uspoof_open (&uc_err); + spc = uspoof_open(&uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("cannot init spoof checker: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); + msg_err("cannot init spoof checker: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); return 1; } } - ret = uspoof_areConfusableUTF8 (spc, s1, l1, s2, l2, &uc_err); + ret = uspoof_areConfusableUTF8(spc, s1, l1, s2, l2, &uc_err); } else if (s1) { /* We have just s1, not s2 */ if (spc_sgl == NULL) { - spc_sgl = uspoof_open (&uc_err); + spc_sgl = uspoof_open(&uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("cannot init spoof checker: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); + msg_err("cannot init spoof checker: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); return 1; } - uspoof_setChecks (spc_sgl, - USPOOF_INVISIBLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE, - &uc_err); + uspoof_setChecks(spc_sgl, + USPOOF_INVISIBLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE, + &uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("Cannot set proper checks for uspoof: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); + msg_err("Cannot set proper checks for uspoof: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); uspoof_close(spc); return 1; } } - ret = uspoof_checkUTF8 (spc_sgl, s1, l1, NULL, &uc_err); + ret = uspoof_checkUTF8(spc_sgl, s1, l1, NULL, &uc_err); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, !!(ret != 0)); + lua_pushboolean(L, !!(ret != 0)); switch (ret) { case 0: nres = 1; break; case USPOOF_SINGLE_SCRIPT_CONFUSABLE: - lua_pushstring (L, "single"); + lua_pushstring(L, "single"); break; case USPOOF_MIXED_SCRIPT_CONFUSABLE: - lua_pushstring (L, "multiple"); + lua_pushstring(L, "multiple"); break; case USPOOF_WHOLE_SCRIPT_CONFUSABLE: - lua_pushstring (L, "whole"); + lua_pushstring(L, "whole"); break; default: - lua_pushstring (L, "unknown"); + lua_pushstring(L, "unknown"); break; } @@ -2211,11 +2211,11 @@ lua_util_is_utf_spoofed (lua_State *L) } static gint -lua_util_is_utf_mixed_script (lua_State *L) +lua_util_is_utf_mixed_script(lua_State *L) { LUA_TRACE_POINT; gsize len_of_string; - const guchar *string_to_check = lua_tolstring (L, 1, &len_of_string); + const guchar *string_to_check = lua_tolstring(L, 1, &len_of_string); UScriptCode last_script_code = USCRIPT_INVALID_CODE; UErrorCode uc_err = U_ZERO_ERROR; @@ -2224,18 +2224,18 @@ lua_util_is_utf_mixed_script (lua_State *L) UChar32 char_to_check = 0; while (index < len_of_string) { - U8_NEXT (string_to_check, index, len_of_string, char_to_check); + U8_NEXT(string_to_check, index, len_of_string, char_to_check); if (char_to_check < 0) { - return luaL_error (L, "passed string is not valid utf"); + return luaL_error(L, "passed string is not valid utf"); } - UScriptCode current_script_code = uscript_getScript (char_to_check, &uc_err); + UScriptCode current_script_code = uscript_getScript(char_to_check, &uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("cannot get unicode script for character, error: %s", - u_errorName (uc_err)); - lua_pushboolean (L, false); + msg_err("cannot get unicode script for character, error: %s", + u_errorName(uc_err)); + lua_pushboolean(L, false); return 1; } @@ -2248,7 +2248,7 @@ lua_util_is_utf_mixed_script (lua_State *L) } else { if (last_script_code != current_script_code) { - lua_pushboolean (L, true); + lua_pushboolean(L, true); return 1; } @@ -2257,22 +2257,22 @@ lua_util_is_utf_mixed_script (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, false); + lua_pushboolean(L, false); return 1; } static gint -lua_util_get_string_stats (lua_State *L) +lua_util_get_string_stats(lua_State *L) { LUA_TRACE_POINT; gint num_of_digits = 0, num_of_letters = 0; struct rspamd_lua_text *t; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t) { const gchar *p = t->start, *end = t->start + t->len; @@ -2287,100 +2287,100 @@ lua_util_get_string_stats (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_createtable (L, 0, 2); - lua_pushstring (L, "digits"); - lua_pushinteger (L, num_of_digits); - lua_settable (L, -3); - lua_pushstring (L, "letters"); - lua_pushinteger (L, num_of_letters); - lua_settable (L, -3); + lua_createtable(L, 0, 2); + lua_pushstring(L, "digits"); + lua_pushinteger(L, num_of_digits); + lua_settable(L, -3); + lua_pushstring(L, "letters"); + lua_pushinteger(L, num_of_letters); + lua_settable(L, -3); return 1; } static gint -lua_util_is_utf_outside_range (lua_State *L) +lua_util_is_utf_outside_range(lua_State *L) { LUA_TRACE_POINT; gint ret; - struct rspamd_lua_text *t = lua_check_text_or_string (L, 1); - guint32 range_start = lua_tointeger (L, 2); - guint32 range_end = lua_tointeger (L, 3); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); + guint32 range_start = lua_tointeger(L, 2); + guint32 range_end = lua_tointeger(L, 3); static rspamd_lru_hash_t *validators; if (validators == NULL) { - validators = rspamd_lru_hash_new_full (16, g_free, (GDestroyNotify) uspoof_close, g_int64_hash, g_int64_equal); + validators = rspamd_lru_hash_new_full(16, g_free, (GDestroyNotify) uspoof_close, g_int64_hash, g_int64_equal); } if (t) { guint64 hash_key = (guint64) range_end << 32 || range_start; - USpoofChecker *validator = rspamd_lru_hash_lookup (validators, &hash_key, 0); + USpoofChecker *validator = rspamd_lru_hash_lookup(validators, &hash_key, 0); UErrorCode uc_err = U_ZERO_ERROR; if (validator == NULL) { USet *allowed_chars; - guint64 *creation_hash_key = g_malloc (sizeof (guint64)); + guint64 *creation_hash_key = g_malloc(sizeof(guint64)); *creation_hash_key = hash_key; - validator = uspoof_open (&uc_err); + validator = uspoof_open(&uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("cannot init spoof checker: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); - uspoof_close (validator); - g_free (creation_hash_key); + msg_err("cannot init spoof checker: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); + uspoof_close(validator); + g_free(creation_hash_key); return 1; } - allowed_chars = uset_openEmpty (); - uset_addRange (allowed_chars, range_start, range_end); - uspoof_setAllowedChars (validator, allowed_chars, &uc_err); + allowed_chars = uset_openEmpty(); + uset_addRange(allowed_chars, range_start, range_end); + uspoof_setAllowedChars(validator, allowed_chars, &uc_err); - uspoof_setChecks (validator, - USPOOF_CHAR_LIMIT | USPOOF_ANY_CASE, &uc_err); + uspoof_setChecks(validator, + USPOOF_CHAR_LIMIT | USPOOF_ANY_CASE, &uc_err); - uset_close (allowed_chars); + uset_close(allowed_chars); if (uc_err != U_ZERO_ERROR) { - msg_err ("Cannot configure uspoof: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); - uspoof_close (validator); - g_free (creation_hash_key); + msg_err("Cannot configure uspoof: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); + uspoof_close(validator); + g_free(creation_hash_key); return 1; } - rspamd_lru_hash_insert (validators, creation_hash_key, validator, - 0, 0); + rspamd_lru_hash_insert(validators, creation_hash_key, validator, + 0, 0); } gint32 pos = 0; - ret = uspoof_checkUTF8 (validator, t->start, t->len, &pos, - &uc_err); + ret = uspoof_checkUTF8(validator, t->start, t->len, &pos, + &uc_err); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, !!(ret != 0)); + lua_pushboolean(L, !!(ret != 0)); return 1; } static gint -lua_util_get_hostname (lua_State *L) +lua_util_get_hostname(lua_State *L) { LUA_TRACE_POINT; gchar *hostbuf; gsize hostlen; - hostlen = sysconf (_SC_HOST_NAME_MAX); + hostlen = sysconf(_SC_HOST_NAME_MAX); if (hostlen <= 0) { hostlen = 256; @@ -2389,141 +2389,141 @@ lua_util_get_hostname (lua_State *L) hostlen++; } - hostbuf = g_alloca (hostlen); - memset (hostbuf, 0, hostlen); - gethostname (hostbuf, hostlen - 1); + hostbuf = g_alloca(hostlen); + memset(hostbuf, 0, hostlen); + gethostname(hostbuf, hostlen - 1); - lua_pushstring (L, hostbuf); + lua_pushstring(L, hostbuf); return 1; } static gint -lua_util_parse_content_type (lua_State *L) +lua_util_parse_content_type(lua_State *L) { - return lua_parsers_parse_content_type (L); + return lua_parsers_parse_content_type(L); } static gint -lua_util_mime_header_encode (lua_State *L) +lua_util_mime_header_encode(lua_State *L) { LUA_TRACE_POINT; gsize len; - const gchar *hdr = luaL_checklstring (L, 1, &len); + const gchar *hdr = luaL_checklstring(L, 1, &len); gchar *encoded; if (!hdr) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - encoded = rspamd_mime_header_encode (hdr, len); - lua_pushstring (L, encoded); - g_free (encoded); + encoded = rspamd_mime_header_encode(hdr, len); + lua_pushstring(L, encoded); + g_free(encoded); return 1; } static gint -lua_util_is_valid_utf8 (lua_State *L) +lua_util_is_valid_utf8(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text_or_string (L, 1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); if (t) { - goffset error_offset = rspamd_fast_utf8_validate (t->start, t->len); + goffset error_offset = rspamd_fast_utf8_validate(t->start, t->len); if (error_offset == 0) { - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); - lua_pushinteger (L, error_offset); + lua_pushboolean(L, false); + lua_pushinteger(L, error_offset); return 2; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_has_obscured_unicode (lua_State *L) +lua_util_has_obscured_unicode(lua_State *L) { LUA_TRACE_POINT; gint32 i = 0, prev_i; UChar32 uc; - struct rspamd_lua_text *t = lua_check_text_or_string (L, 1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); while (i < t->len) { prev_i = i; - U8_NEXT (t->start, i, t->len, uc); + U8_NEXT(t->start, i, t->len, uc); if (uc > 0) { - if (IS_OBSCURED_CHAR (uc)) { - lua_pushboolean (L, true); - lua_pushinteger (L, uc); /* Character */ - lua_pushinteger (L, prev_i); /* Offset */ + if (IS_OBSCURED_CHAR(uc)) { + lua_pushboolean(L, true); + lua_pushinteger(L, uc); /* Character */ + lua_pushinteger(L, prev_i); /* Offset */ return 3; } } } - lua_pushboolean (L, false); + lua_pushboolean(L, false); return 1; } static gint -lua_util_readline (lua_State *L) +lua_util_readline(lua_State *L) { LUA_TRACE_POINT; const gchar *prompt = ""; gchar *input; - if (lua_type (L, 1) == LUA_TSTRING) { - prompt = lua_tostring (L, 1); + if (lua_type(L, 1) == LUA_TSTRING) { + prompt = lua_tostring(L, 1); } #ifdef WITH_LUA_REPL static Replxx *rx_instance = NULL; if (rx_instance == NULL) { - rx_instance = replxx_init (); + rx_instance = replxx_init(); /* See https://github.com/AmokHuginnsson/replxx/issues/137 */ replxx_history_add(rx_instance, ""); } - input = (gchar *)replxx_input (rx_instance, prompt); + input = (gchar *) replxx_input(rx_instance, prompt); if (input) { - lua_pushstring (L, input); + lua_pushstring(L, input); } else { - lua_pushnil (L); + lua_pushnil(L); } #else size_t linecap = 0; ssize_t linelen; - fprintf (stdout, "%s ", prompt); + fprintf(stdout, "%s ", prompt); - linelen = getline (&input, &linecap, stdin); + linelen = getline(&input, &linecap, stdin); if (linelen > 0) { if (input[linelen - 1] == '\n') { - linelen --; + linelen--; } - lua_pushlstring (L, input, linelen); - free (input); + lua_pushlstring(L, input, linelen); + free(input); } else { - lua_pushnil (L); + lua_pushnil(L); } #endif @@ -2531,83 +2531,83 @@ lua_util_readline (lua_State *L) } static gint -lua_util_readpassphrase (lua_State *L) +lua_util_readpassphrase(lua_State *L) { LUA_TRACE_POINT; gchar test_password[8192]; gsize r; - r = rspamd_read_passphrase (test_password, sizeof (test_password), 0, NULL); + r = rspamd_read_passphrase(test_password, sizeof(test_password), 0, NULL); if (r > 0) { - lua_pushlstring (L, test_password, r); + lua_pushlstring(L, test_password, r); } else { - lua_pushnil (L); + lua_pushnil(L); } /* In fact, we still pass it to Lua which is not very safe */ - rspamd_explicit_memzero (test_password, sizeof (test_password)); + rspamd_explicit_memzero(test_password, sizeof(test_password)); return 1; } static gint -lua_util_file_exists (lua_State *L) +lua_util_file_exists(lua_State *L) { LUA_TRACE_POINT; - const gchar *fname = luaL_checkstring (L, 1); + const gchar *fname = luaL_checkstring(L, 1); gint serrno; if (fname) { - if (access (fname, R_OK) == -1) { + if (access(fname, R_OK) == -1) { serrno = errno; - lua_pushboolean (L, false); - lua_pushstring (L, strerror (serrno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(serrno)); } else { - lua_pushboolean (L, true); - lua_pushnil (L); + lua_pushboolean(L, true); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; } static gint -lua_util_mkdir (lua_State *L) +lua_util_mkdir(lua_State *L) { LUA_TRACE_POINT; - const gchar *dname = luaL_checkstring (L, 1); + const gchar *dname = luaL_checkstring(L, 1); gboolean recursive = FALSE; gint r = -1; if (dname) { - if (lua_isboolean (L, 2)) { - recursive = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + recursive = lua_toboolean(L, 2); } if (recursive) { char path[PATH_MAX]; gsize len, i; - len = rspamd_strlcpy (path, dname, sizeof (path)); + len = rspamd_strlcpy(path, dname, sizeof(path)); /* Strip last / */ if (path[len - 1] == '/') { path[len - 1] = '\0'; - len --; + len--; } - for (i = 1; i < len; i ++) { + for (i = 1; i < len; i++) { if (path[i] == '/') { path[i] = '\0'; errno = 0; - r = mkdir (path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + r = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); if (r == -1 && errno != EEXIST) { break; @@ -2618,23 +2618,23 @@ lua_util_mkdir (lua_State *L) } /* Final path component */ - r = mkdir (path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + r = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); } else { - r = mkdir (dname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + r = mkdir(dname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); } if (r == -1 && errno != EEXIST) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -2642,46 +2642,46 @@ lua_util_mkdir (lua_State *L) static gint -lua_util_umask (lua_State *L) +lua_util_umask(lua_State *L) { LUA_TRACE_POINT; mode_t mask = 0, old; - if (lua_type (L, 1) == LUA_TSTRING) { - const gchar *str = lua_tostring (L, 1); + if (lua_type(L, 1) == LUA_TSTRING) { + const gchar *str = lua_tostring(L, 1); if (str[0] == '0') { /* e.g. '022' */ - mask = strtol (str, NULL, 8); + mask = strtol(str, NULL, 8); } else { /* XXX: implement modestring parsing at some point */ - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } } - else if (lua_type (L, 1) == LUA_TNUMBER) { + else if (lua_type(L, 1) == LUA_TNUMBER) { mask = lua_tointeger(L, 1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - old = umask (mask); + old = umask(mask); - lua_pushinteger (L, old); + lua_pushinteger(L, old); return 1; } static gint -lua_util_isatty (lua_State *L) +lua_util_isatty(lua_State *L) { LUA_TRACE_POINT; - if (isatty (STDOUT_FILENO)) { - lua_pushboolean (L, true); + if (isatty(STDOUT_FILENO)) { + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } return 1; @@ -2721,31 +2721,31 @@ lua_util_isatty (lua_State *L) /* value used for padding */ #if !defined(LUA_PACKPADBYTE) -#define LUA_PACKPADBYTE 0x00 +#define LUA_PACKPADBYTE 0x00 #endif /* maximum size for the binary representation of an integer */ -#define MAXINTSIZE 16 +#define MAXINTSIZE 16 /* number of bits in a character */ -#define NB CHAR_BIT +#define NB CHAR_BIT /* mask for one character (NB 1's) */ -#define MC ((1 << NB) - 1) +#define MC ((1 << NB) - 1) /* size of a lua_Integer */ -#define SZINT ((int)sizeof(lua_Integer)) +#define SZINT ((int) sizeof(lua_Integer)) -#define MAX_SIZET ((size_t)(~(size_t)0)) +#define MAX_SIZET ((size_t) (~(size_t) 0)) -#define MAXSIZE \ - (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) +#define MAXSIZE \ + (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t) (INT_MAX)) /* dummy union to get native endianness */ static const union { int dummy; - char little; /* true if machine is little endian */ + char little; /* true if machine is little endian */ } nativeendian = {1}; @@ -2760,7 +2760,7 @@ struct cD { } u; }; -#define MAXALIGN (offsetof(struct cD, u)) +#define MAXALIGN (offsetof(struct cD, u)) /* ** Union for serializing floats @@ -2769,7 +2769,7 @@ typedef union Ftypes { float f; double d; lua_Number n; - char buff[5 * sizeof (lua_Number)]; /* enough for any float type */ + char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ } Ftypes; @@ -2786,14 +2786,14 @@ typedef struct Header { ** options for pack/unpack */ typedef enum KOption { - Kint, /* signed integers */ - Kuint, /* unsigned integers */ - Kfloat, /* floating-point numbers */ - Kchar, /* fixed-length strings */ + Kint, /* signed integers */ + Kuint, /* unsigned integers */ + Kfloat, /* floating-point numbers */ + Kchar, /* fixed-length strings */ Kstring, /* strings with prefixed length */ - Kzstr, /* zero-terminated strings */ - Kpadding, /* padding */ - Kpaddalign, /* padding for alignment */ + Kzstr, /* zero-terminated strings */ + Kpadding, /* padding */ + Kpaddalign, /* padding for alignment */ Knop /* no-op (configuration or spaces) */ } KOption; @@ -2818,51 +2818,51 @@ typedef struct luaL_Buffer_53 { #undef COMPAT53_API #if defined(__GNUC__) || defined(__clang__) -# define COMPAT53_API __attribute__((__unused__)) static +#define COMPAT53_API __attribute__((__unused__)) static #else -# define COMPAT53_API static +#define COMPAT53_API static #endif #define COMPAT53_CONCAT_HELPER(a, b) a##b #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b) #define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53) -COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B); +COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B); #define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53) -COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s); +COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s); #define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53) -COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l); +COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l); #define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53) -COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B); +COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B); #define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53) -COMPAT53_API void luaL_pushresult (luaL_Buffer_53 *B); +COMPAT53_API void luaL_pushresult(luaL_Buffer_53 *B); #undef luaL_buffinitsize #define luaL_buffinitsize(L, B, s) \ - (luaL_buffinit(L, B), luaL_prepbuffsize(B, s)) + (luaL_buffinit(L, B), luaL_prepbuffsize(B, s)) #undef luaL_prepbuffer #define luaL_prepbuffer(B) \ - luaL_prepbuffsize(B, LUAL_BUFFERSIZE) + luaL_prepbuffsize(B, LUAL_BUFFERSIZE) #undef luaL_addchar -#define luaL_addchar(B, c) \ - ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize(B, 1)), \ - ((B)->ptr[(B)->nelems++] = (c))) +#define luaL_addchar(B, c) \ + ((void) ((B)->nelems < (B)->capacity || luaL_prepbuffsize(B, 1)), \ + ((B)->ptr[(B)->nelems++] = (c))) #undef luaL_addsize #define luaL_addsize(B, s) \ - ((B)->nelems += (s)) + ((B)->nelems += (s)) #undef luaL_addstring #define luaL_addstring(B, s) \ - luaL_addlstring(B, s, strlen(s)) + luaL_addlstring(B, s, strlen(s)) #undef luaL_pushresultsize #define luaL_pushresultsize(B, s) \ - (luaL_addsize(B, s), luaL_pushresult(B)) + (luaL_addsize(B, s), luaL_pushresult(B)) COMPAT53_API void -luaL_buffinit (lua_State *L, luaL_Buffer_53 *B) +luaL_buffinit(lua_State *L, luaL_Buffer_53 *B) { /* make it crash if used via pointer to a 5.1-style luaL_Buffer */ B->b.p = NULL; @@ -2877,7 +2877,7 @@ luaL_buffinit (lua_State *L, luaL_Buffer_53 *B) COMPAT53_API char * -luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) +luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s) { if (B->capacity - B->nelems < s) { /* needs to grow */ char *newptr = NULL; @@ -2885,11 +2885,11 @@ luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) if (newcap - B->nelems < s) newcap = B->nelems + s; if (newcap < B->capacity) /* overflow */ - luaL_error (B->L2, "buffer too large"); - newptr = (char *) lua_newuserdata (B->L2, newcap); + luaL_error(B->L2, "buffer too large"); + newptr = (char *) lua_newuserdata(B->L2, newcap); memcpy(newptr, B->ptr, B->nelems); if (B->ptr != B->b.buffer) { - lua_replace (B->L2, -2); /* remove old buffer */ + lua_replace(B->L2, -2); /* remove old buffer */ } B->ptr = newptr; B->capacity = newcap; @@ -2899,34 +2899,34 @@ luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) COMPAT53_API void -luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l) +luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l) { - memcpy(luaL_prepbuffsize (B, l), s, l); + memcpy(luaL_prepbuffsize(B, l), s, l); luaL_addsize(B, l); } COMPAT53_API void -luaL_addvalue (luaL_Buffer_53 *B) +luaL_addvalue(luaL_Buffer_53 *B) { size_t len = 0; - const char *s = lua_tolstring (B->L2, -1, &len); + const char *s = lua_tolstring(B->L2, -1, &len); if (!s) - luaL_error (B->L2, "cannot convert value to string"); + luaL_error(B->L2, "cannot convert value to string"); if (B->ptr != B->b.buffer) { - lua_insert (B->L2, -2); /* userdata buffer must be at stack top */ + lua_insert(B->L2, -2); /* userdata buffer must be at stack top */ } - luaL_addlstring (B, s, len); - lua_remove (B->L2, B->ptr != B->b.buffer ? -2 : -1); + luaL_addlstring(B, s, len); + lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1); } COMPAT53_API void -luaL_pushresult (luaL_Buffer_53 *B) +luaL_pushresult(luaL_Buffer_53 *B) { - lua_pushlstring (B->L2, B->ptr, B->nelems); + lua_pushlstring(B->L2, B->ptr, B->nelems); if (B->ptr != B->b.buffer) { - lua_replace (B->L2, -2); /* remove userdata buffer */ + lua_replace(B->L2, -2); /* remove userdata buffer */ } } @@ -2937,21 +2937,21 @@ luaL_pushresult (luaL_Buffer_53 *B) ** there is no numeral */ static int -digit (int c) +digit(int c) { return '0' <= c && c <= '9'; } static int -getnum (const char **fmt, int df) +getnum(const char **fmt, int df) { - if (!digit (**fmt)) /* no number? */ - return df; /* return default value */ + if (!digit(**fmt)) /* no number? */ + return df; /* return default value */ else { int a = 0; do { a = a * 10 + (*((*fmt)++) - '0'); - } while (digit (**fmt) && a <= ((int) MAXSIZE - 9) / 10); + } while (digit(**fmt) && a <= ((int) MAXSIZE - 9) / 10); return a; } } @@ -2962,12 +2962,12 @@ getnum (const char **fmt, int df) ** than the maximum size for integers. */ static int -getnumlimit (Header *h, const char **fmt, int df) +getnumlimit(Header *h, const char **fmt, int df) { - int sz = getnum (fmt, df); + int sz = getnum(fmt, df); if (sz > MAXINTSIZE || sz <= 0) - luaL_error (h->L, "integral size (%d) out of limits [1,%d]", - sz, MAXINTSIZE); + luaL_error(h->L, "integral size (%d) out of limits [1,%d]", + sz, MAXINTSIZE); return sz; } @@ -2976,7 +2976,7 @@ getnumlimit (Header *h, const char **fmt, int df) ** Initialize Header */ static void -initheader (lua_State *L, Header *h) +initheader(lua_State *L, Header *h) { h->L = L; h->islittle = nativeendian.little; @@ -2988,60 +2988,60 @@ initheader (lua_State *L, Header *h) ** Read and classify next option. 'size' is filled with option's size. */ static KOption -getoption (Header *h, const char **fmt, int *size) +getoption(Header *h, const char **fmt, int *size) { int opt = *((*fmt)++); - *size = 0; /* default */ + *size = 0; /* default */ switch (opt) { case 'b': - *size = sizeof (char); + *size = sizeof(char); return Kint; case 'B': - *size = sizeof (char); + *size = sizeof(char); return Kuint; case 'h': - *size = sizeof (short); + *size = sizeof(short); return Kint; case 'H': - *size = sizeof (short); + *size = sizeof(short); return Kuint; case 'l': - *size = sizeof (long); + *size = sizeof(long); return Kint; case 'L': - *size = sizeof (long); + *size = sizeof(long); return Kuint; case 'j': - *size = sizeof (lua_Integer); + *size = sizeof(lua_Integer); return Kint; case 'J': - *size = sizeof (lua_Integer); + *size = sizeof(lua_Integer); return Kuint; case 'T': - *size = sizeof (size_t); + *size = sizeof(size_t); return Kuint; case 'f': - *size = sizeof (float); + *size = sizeof(float); return Kfloat; case 'd': - *size = sizeof (double); + *size = sizeof(double); return Kfloat; case 'n': - *size = sizeof (lua_Number); + *size = sizeof(lua_Number); return Kfloat; case 'i': - *size = getnumlimit (h, fmt, sizeof (int)); + *size = getnumlimit(h, fmt, sizeof(int)); return Kint; case 'I': - *size = getnumlimit (h, fmt, sizeof (int)); + *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; case 's': - *size = getnumlimit (h, fmt, sizeof (size_t)); + *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; case 'c': - *size = getnum (fmt, -1); + *size = getnum(fmt, -1); if (*size == -1) - luaL_error (h->L, "missing size for format option 'c'"); + luaL_error(h->L, "missing size for format option 'c'"); return Kchar; case 'z': return Kzstr; @@ -3062,10 +3062,10 @@ getoption (Header *h, const char **fmt, int *size) h->islittle = nativeendian.little; break; case '!': - h->maxalign = getnumlimit (h, fmt, MAXALIGN); + h->maxalign = getnumlimit(h, fmt, MAXALIGN); break; default: - luaL_error (h->L, "invalid format option '%c'", opt); + luaL_error(h->L, "invalid format option '%c'", opt); } return Knop; } @@ -3081,22 +3081,22 @@ getoption (Header *h, const char **fmt, int *size) ** despite its size. */ static KOption -getdetails (Header *h, size_t totalsize, - const char **fmt, int *psize, int *ntoalign) +getdetails(Header *h, size_t totalsize, + const char **fmt, int *psize, int *ntoalign) { - KOption opt = getoption (h, fmt, psize); - int align = *psize; /* usually, alignment follows size */ - if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ - if (**fmt == '\0' || getoption (h, fmt, &align) == Kchar || align == 0) - luaL_argerror (h->L, 1, "invalid next option for option 'X'"); + KOption opt = getoption(h, fmt, psize); + int align = *psize; /* usually, alignment follows size */ + if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ + if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) + luaL_argerror(h->L, 1, "invalid next option for option 'X'"); } - if (align <= 1 || opt == Kchar) /* need no alignment? */ + if (align <= 1 || opt == Kchar) /* need no alignment? */ *ntoalign = 0; else { - if (align > h->maxalign) /* enforce maximum alignment */ + if (align > h->maxalign) /* enforce maximum alignment */ align = h->maxalign; - if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ - luaL_argerror (h->L, 1, "format asks for alignment not power of 2"); + if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ + luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); *ntoalign = (align - (int) (totalsize & (align - 1))) & (align - 1); } return opt; @@ -3110,21 +3110,21 @@ getdetails (Header *h, size_t totalsize, ** bytes if necessary (by default they would be zeros). */ static void -packint (luaL_Buffer *b, lua_Unsigned n, +packint(luaL_Buffer *b, lua_Unsigned n, int islittle, int size, int neg) { - char *buff = luaL_prepbuffsize (b, size); + char *buff = luaL_prepbuffsize(b, size); int i; - buff[islittle ? 0 : size - 1] = (char) (n & MC); /* first byte */ + buff[islittle ? 0 : size - 1] = (char) (n & MC); /* first byte */ for (i = 1; i < size; i++) { n >>= NB; buff[islittle ? i : size - 1 - i] = (char) (n & MC); } - if (neg && size > SZINT) { /* negative number need sign extension? */ - for (i = SZINT; i < size; i++) /* correct extra bytes */ + if (neg && size > SZINT) { /* negative number need sign extension? */ + for (i = SZINT; i < size; i++) /* correct extra bytes */ buff[islittle ? i : size - 1 - i] = (char) MC; } - luaL_addsize(b, size); /* add result to buffer */ + luaL_addsize(b, size); /* add result to buffer */ } @@ -3133,8 +3133,8 @@ packint (luaL_Buffer *b, lua_Unsigned n, ** given 'islittle' is different from native endianness. */ static void -copywithendian (volatile char *dest, volatile const char *src, - int size, int islittle) +copywithendian(volatile char *dest, volatile const char *src, + int size, int islittle) { if (islittle == nativeendian.little) { while (size-- != 0) @@ -3149,135 +3149,134 @@ copywithendian (volatile char *dest, volatile const char *src, static int -lua_util_pack (lua_State *L) +lua_util_pack(lua_State *L) { luaL_Buffer b; Header h; - const char *fmt = luaL_checkstring(L, 1); /* format string */ - int arg = 1; /* current argument to pack */ - size_t totalsize = 0; /* accumulate total size of result */ - initheader (L, &h); - lua_pushnil (L); /* mark to separate arguments from string buffer */ - luaL_buffinit (L, &b); + const char *fmt = luaL_checkstring(L, 1); /* format string */ + int arg = 1; /* current argument to pack */ + size_t totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); + lua_pushnil(L); /* mark to separate arguments from string buffer */ + luaL_buffinit(L, &b); while (*fmt != '\0') { int size, ntoalign; - KOption opt = getdetails (&h, totalsize, &fmt, &size, &ntoalign); + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); totalsize += ntoalign + size; while (ntoalign-- > 0) - luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */ + luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */ arg++; switch (opt) { - case Kint: { /* signed integers */ - lua_Integer n = luaL_checkinteger (L, arg); - if (size < SZINT) { /* need overflow check? */ + case Kint: { /* signed integers */ + lua_Integer n = luaL_checkinteger(L, arg); + if (size < SZINT) { /* need overflow check? */ lua_Integer lim = (lua_Integer) 1 << ((size * NB) - 1); luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); } - packint (&b, (lua_Unsigned) n, h.islittle, size, (n < 0)); + packint(&b, (lua_Unsigned) n, h.islittle, size, (n < 0)); break; } - case Kuint: { /* unsigned integers */ - lua_Integer n = luaL_checkinteger (L, arg); - if (size < SZINT) /* need overflow check? */ + case Kuint: { /* unsigned integers */ + lua_Integer n = luaL_checkinteger(L, arg); + if (size < SZINT) /* need overflow check? */ luaL_argcheck(L, - (lua_Unsigned) n < ((lua_Unsigned) 1 << (size * NB)), - arg, - "unsigned overflow"); - packint (&b, (lua_Unsigned) n, h.islittle, size, 0); + (lua_Unsigned) n < ((lua_Unsigned) 1 << (size * NB)), + arg, + "unsigned overflow"); + packint(&b, (lua_Unsigned) n, h.islittle, size, 0); break; } - case Kfloat: { /* floating-point options */ + case Kfloat: { /* floating-point options */ volatile Ftypes u; - char *buff = luaL_prepbuffsize (&b, size); - lua_Number n = luaL_checknumber (L, arg); /* get argument */ - if (size == sizeof (u.f)) - u.f = (float) n; /* copy it into 'u' */ - else if (size == sizeof (u.d)) + char *buff = luaL_prepbuffsize(&b, size); + lua_Number n = luaL_checknumber(L, arg); /* get argument */ + if (size == sizeof(u.f)) + u.f = (float) n; /* copy it into 'u' */ + else if (size == sizeof(u.d)) u.d = (double) n; else u.n = n; /* move 'u' to final result, correcting endianness if needed */ - copywithendian (buff, u.buff, size, h.islittle); + copywithendian(buff, u.buff, size, h.islittle); luaL_addsize(&b, size); break; } - case Kchar: { /* fixed-size string */ + case Kchar: { /* fixed-size string */ size_t len; - const char *s = luaL_checklstring (L, arg, &len); + const char *s = luaL_checklstring(L, arg, &len); if ((size_t) size <= - len) /* string larger than (or equal to) needed? */ - luaL_addlstring (&b, - s, - size); /* truncate string to asked size */ - else { /* string smaller than needed */ - luaL_addlstring (&b, s, len); /* add it all */ - while (len++ < (size_t) size) /* pad extra space */ + len) /* string larger than (or equal to) needed? */ + luaL_addlstring(&b, + s, + size); /* truncate string to asked size */ + else { /* string smaller than needed */ + luaL_addlstring(&b, s, len); /* add it all */ + while (len++ < (size_t) size) /* pad extra space */ luaL_addchar(&b, LUA_PACKPADBYTE); } break; } - case Kstring: { /* strings with length count */ + case Kstring: { /* strings with length count */ size_t len; - const char *s = luaL_checklstring (L, arg, &len); - luaL_argcheck(L, size >= (int) sizeof (size_t) || - len < ((size_t) 1 << (size * NB)), - arg, "string length does not fit in given size"); - packint (&b, + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, size >= (int) sizeof(size_t) || len < ((size_t) 1 << (size * NB)), + arg, "string length does not fit in given size"); + packint(&b, (lua_Unsigned) len, h.islittle, size, - 0); /* pack length */ - luaL_addlstring (&b, s, len); + 0); /* pack length */ + luaL_addlstring(&b, s, len); totalsize += len; break; } - case Kzstr: { /* zero-terminated string */ + case Kzstr: { /* zero-terminated string */ size_t len; - const char *s = luaL_checklstring (L, arg, &len); - luaL_argcheck(L, strlen (s) == len, arg, "string contains zeros"); - luaL_addlstring (&b, s, len); - luaL_addchar(&b, '\0'); /* add zero at the end */ + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros"); + luaL_addlstring(&b, s, len); + luaL_addchar(&b, '\0'); /* add zero at the end */ totalsize += len + 1; break; } case Kpadding: - luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */ + luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */ case Kpaddalign: case Knop: - arg--; /* undo increment */ + arg--; /* undo increment */ break; } } - luaL_pushresult (&b); + luaL_pushresult(&b); return 1; } static int -lua_util_packsize (lua_State *L) +lua_util_packsize(lua_State *L) { Header h; - const char *fmt = luaL_checkstring(L, 1); /* format string */ - size_t totalsize = 0; /* accumulate total size of result */ - initheader (L, &h); + const char *fmt = luaL_checkstring(L, 1); /* format string */ + size_t totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); while (*fmt != '\0') { int size, ntoalign; - KOption opt = getdetails (&h, totalsize, &fmt, &size, &ntoalign); - size += ntoalign; /* total space used by option */ + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + size += ntoalign; /* total space used by option */ luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, - "format result too large"); + "format result too large"); totalsize += size; switch (opt) { - case Kstring: /* strings with length count */ - case Kzstr: /* zero-terminated string */ - luaL_argerror (L, 1, "variable-length format"); - /* call never return, but to avoid warnings: *//* FALLTHROUGH */ + case Kstring: /* strings with length count */ + case Kzstr: /* zero-terminated string */ + luaL_argerror(L, 1, "variable-length format"); + /* call never return, but to avoid warnings: */ /* FALLTHROUGH */ default: break; } } - lua_pushinteger (L, (lua_Integer) totalsize); + lua_pushinteger(L, (lua_Integer) totalsize); return 1; } @@ -3291,37 +3290,36 @@ lua_util_packsize (lua_State *L) ** overflow. */ static lua_Integer -unpackint (lua_State *L, const char *str, - int islittle, int size, int issigned) +unpackint(lua_State *L, const char *str, + int islittle, int size, int issigned) { lua_Unsigned res = 0; int i; int limit = (size <= SZINT) ? size : SZINT; for (i = limit - 1; i >= 0; i--) { res <<= NB; - res |= (lua_Unsigned) ( - unsigned char)str[islittle ? i : size - 1 - i]; + res |= (lua_Unsigned) (unsigned char) str[islittle ? i : size - 1 - i]; } - if (size < SZINT) { /* real size smaller than lua_Integer? */ - if (issigned) { /* needs sign extension? */ + if (size < SZINT) { /* real size smaller than lua_Integer? */ + if (issigned) { /* needs sign extension? */ lua_Unsigned mask = (lua_Unsigned) 1 << (size * NB - 1); - res = ((res ^ mask) - mask); /* do sign extension */ + res = ((res ^ mask) - mask); /* do sign extension */ } } - else if (size > SZINT) { /* must check unread bytes */ + else if (size > SZINT) { /* must check unread bytes */ int mask = (!issigned || (lua_Integer) res >= 0) ? 0 : MC; for (i = limit; i < size; i++) { if ((unsigned char) str[islittle ? i : size - 1 - i] != mask) - luaL_error (L, - "%d-byte integer does not fit into Lua Integer", - size); + luaL_error(L, + "%d-byte integer does not fit into Lua Integer", + size); } } return (lua_Integer) res; } static lua_Integer -posrelat (lua_Integer pos, size_t len) +posrelat(lua_Integer pos, size_t len) { if (pos >= 0) return pos; @@ -3332,111 +3330,111 @@ posrelat (lua_Integer pos, size_t len) } static int -lua_util_unpack (lua_State *L) +lua_util_unpack(lua_State *L) { Header h; const char *fmt = luaL_checkstring(L, 1); size_t ld; const char *data; - int n = 0; /* number of results */ + int n = 0; /* number of results */ - if (lua_type (L, 2) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, 2); + if (lua_type(L, 2) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, 2); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; ld = t->len; } else { - data = luaL_checklstring (L, 2, &ld); + data = luaL_checklstring(L, 2, &ld); } - size_t pos = (size_t) posrelat (luaL_optinteger (L, 3, 1), ld) - 1; + size_t pos = (size_t) posrelat(luaL_optinteger(L, 3, 1), ld) - 1; luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); - initheader (L, &h); + initheader(L, &h); while (*fmt != '\0') { int size, ntoalign; - KOption opt = getdetails (&h, pos, &fmt, &size, &ntoalign); + KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); if ((size_t) ntoalign + size > ~pos || pos + ntoalign + size > ld) - luaL_argerror (L, 2, "data string too short"); - pos += ntoalign; /* skip alignment */ + luaL_argerror(L, 2, "data string too short"); + pos += ntoalign; /* skip alignment */ /* stack space for item + next position */ - luaL_checkstack (L, 2, "too many results"); + luaL_checkstack(L, 2, "too many results"); n++; switch (opt) { case Kint: case Kuint: { - lua_Integer res = unpackint (L, data + pos, h.islittle, size, - (opt == Kint)); - lua_pushinteger (L, res); + lua_Integer res = unpackint(L, data + pos, h.islittle, size, + (opt == Kint)); + lua_pushinteger(L, res); break; } case Kfloat: { volatile Ftypes u; lua_Number num; - copywithendian (u.buff, data + pos, size, h.islittle); - if (size == sizeof (u.f)) + copywithendian(u.buff, data + pos, size, h.islittle); + if (size == sizeof(u.f)) num = (lua_Number) u.f; - else if (size == sizeof (u.d)) + else if (size == sizeof(u.d)) num = (lua_Number) u.d; else num = u.n; - lua_pushnumber (L, num); + lua_pushnumber(L, num); break; } case Kchar: { - lua_pushlstring (L, data + pos, size); + lua_pushlstring(L, data + pos, size); break; } case Kstring: { - size_t len = (size_t) unpackint (L, - data + pos, - h.islittle, - size, - 0); + size_t len = (size_t) unpackint(L, + data + pos, + h.islittle, + size, + 0); luaL_argcheck(L, - pos + len + size <= ld, - 2, - "data string too short"); - lua_pushlstring (L, data + pos + size, len); - pos += len; /* skip string */ + pos + len + size <= ld, + 2, + "data string too short"); + lua_pushlstring(L, data + pos + size, len); + pos += len; /* skip string */ break; } case Kzstr: { - size_t len = (int) strlen (data + pos); - lua_pushlstring (L, data + pos, len); - pos += len + 1; /* skip string plus final '\0' */ + size_t len = (int) strlen(data + pos); + lua_pushlstring(L, data + pos, len); + pos += len + 1; /* skip string plus final '\0' */ break; } case Kpaddalign: case Kpadding: case Knop: - n--; /* undo increment */ + n--; /* undo increment */ break; } pos += size; } - lua_pushinteger (L, pos + 1); /* next position */ + lua_pushinteger(L, pos + 1); /* next position */ return n + 1; } static int -lua_util_btc_polymod (lua_State *L) +lua_util_btc_polymod(lua_State *L) { guint64 c = 1; - if (lua_type (L, 1) != LUA_TTABLE) { - return luaL_error (L, "invalid arguments"); + if (lua_type(L, 1) != LUA_TTABLE) { + return luaL_error(L, "invalid arguments"); } - for (lua_pushnil (L); lua_next (L, 1); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) { guint8 c0 = c >> 35; - guint64 d = lua_tointeger (L, -1); + guint64 d = lua_tointeger(L, -1); c = ((c & 0x07ffffffff) << 5) ^ d; @@ -3448,61 +3446,60 @@ lua_util_btc_polymod (lua_State *L) } if ((c ^ 1) == 0) { - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } return 1; } static int -lua_util_parse_smtp_date (lua_State *L) +lua_util_parse_smtp_date(lua_State *L) { - return lua_parsers_parse_smtp_date (L); + return lua_parsers_parse_smtp_date(L); } static gint -lua_load_util (lua_State * L) +lua_load_util(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, utillib_f); + lua_newtable(L); + luaL_register(L, NULL, utillib_f); return 1; } static gint -lua_load_int64 (lua_State * L) +lua_load_int64(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, int64lib_f); + lua_newtable(L); + luaL_register(L, NULL, int64lib_f); return 1; } -void -luaopen_util (lua_State * L) +void luaopen_util(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{ev_base}", ev_baselib_m); - lua_pop (L, 1); - rspamd_lua_new_class (L, "rspamd{int64}", int64lib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_util", lua_load_util); - rspamd_lua_add_preload (L, "rspamd_int64", lua_load_int64); + rspamd_lua_new_class(L, "rspamd{ev_base}", ev_baselib_m); + lua_pop(L, 1); + rspamd_lua_new_class(L, "rspamd{int64}", int64lib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_util", lua_load_util); + rspamd_lua_add_preload(L, "rspamd_int64", lua_load_int64); } static int -lua_int64_tostring (lua_State *L) +lua_int64_tostring(lua_State *L) { - gint64 n = lua_check_int64 (L, 1); + gint64 n = lua_check_int64(L, 1); gchar buf[32]; bool is_signed = false; - if (lua_isboolean (L, 2)) { - is_signed = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + is_signed = lua_toboolean(L, 2); } if (is_signed) { @@ -3511,15 +3508,15 @@ lua_int64_tostring (lua_State *L) else { rspamd_snprintf(buf, sizeof(buf), "%uL", n); } - lua_pushstring (L, buf); + lua_pushstring(L, buf); return 1; } static int -lua_int64_fromstring (lua_State *L) +lua_int64_fromstring(lua_State *L) { - struct rspamd_lua_text *t = lua_check_text_or_string (L, 1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); if (t && t->len > 0) { guint64 u64; @@ -3538,67 +3535,66 @@ lua_int64_fromstring (lua_State *L) */ if (*p == '-') { neg = true; - p ++; - len --; + p++; + len--; } if (!rspamd_strtou64(p, len, &u64)) { - lua_pushnil (L); - lua_pushstring (L, "invalid number"); + lua_pushnil(L); + lua_pushstring(L, "invalid number"); return 2; } - gint64* i64_p = lua_newuserdata (L, sizeof (gint64)); - rspamd_lua_setclass (L, "rspamd{int64}", -1); - memcpy (i64_p, &u64, sizeof(u64)); + gint64 *i64_p = lua_newuserdata(L, sizeof(gint64)); + rspamd_lua_setclass(L, "rspamd{int64}", -1); + memcpy(i64_p, &u64, sizeof(u64)); if (neg) { *i64_p = -(*i64_p); } } else { - } return 1; } static int -lua_int64_tonumber (lua_State *L) +lua_int64_tonumber(lua_State *L) { - gint64 n = lua_check_int64 (L, 1); + gint64 n = lua_check_int64(L, 1); gdouble d; d = n; - lua_pushinteger (L, d); + lua_pushinteger(L, d); return 1; } static int -lua_int64_hex (lua_State *L) +lua_int64_hex(lua_State *L) { - gint64 n = lua_check_int64 (L, 1); + gint64 n = lua_check_int64(L, 1); gchar buf[32]; - rspamd_snprintf (buf, sizeof (buf), "%XL", n); - lua_pushstring (L, buf); + rspamd_snprintf(buf, sizeof(buf), "%XL", n); + lua_pushstring(L, buf); return 1; } static int -lua_ev_base_loop (lua_State *L) +lua_ev_base_loop(lua_State *L) { int flags = 0; struct ev_loop *ev_base; - ev_base = lua_check_ev_base (L, 1); - if (lua_isnumber (L, 2)) { - flags = lua_tointeger (L, 2); + ev_base = lua_check_ev_base(L, 1); + if (lua_isnumber(L, 2)) { + flags = lua_tointeger(L, 2); } - int ret = ev_run (ev_base, flags); - lua_pushinteger (L, ret); + int ret = ev_run(ev_base, flags); + lua_pushinteger(L, ret); return 1; } |