• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 22550237931

01 Mar 2026 06:56PM UTC coverage: 64.812% (-8.9%) from 73.687%
22550237931

Pull #14920

github

web-flow
Merge e05854a6d into 90823fa90
Pull Request #14920: draft: rust based configuration file parser and loader - v4

561 of 789 new or added lines in 4 files covered. (71.1%)

23225 existing lines in 498 files now uncovered.

131605 of 203055 relevant lines covered (64.81%)

2328818.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/util-lua-filelib.c
1
/* Copyright (C) 2025 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
#include "suricata-common.h"
19
#include "util-lua.h"
20
#include "util-lua-common.h"
21
#include "util-lua-filelib.h"
22

23
#include "lua.h"
24
#include "lauxlib.h"
25

26
static const char file_mt[] = "suricata:file:mt";
27

28
struct LuaFile {
29
    File *file;
30
};
31

32
static int LuaFileGetFile(lua_State *L)
UNCOV
33
{
×
UNCOV
34
    File *file = LuaStateGetFile(L);
×
UNCOV
35
    if (file == NULL) {
×
36
        return LuaCallbackError(L, "error: no file found");
×
37
    }
×
38

UNCOV
39
    struct LuaFile *lua_file = (struct LuaFile *)lua_newuserdata(L, sizeof(*lua_file));
×
UNCOV
40
    if (lua_file == NULL) {
×
41
        return LuaCallbackError(L, "error: fail to allocate user data");
×
42
    }
×
UNCOV
43
    lua_file->file = file;
×
44

UNCOV
45
    luaL_getmetatable(L, file_mt);
×
UNCOV
46
    lua_setmetatable(L, -2);
×
47

UNCOV
48
    return 1;
×
UNCOV
49
}
×
50

51
static int LuaFileGetFileId(lua_State *L)
UNCOV
52
{
×
UNCOV
53
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
54
    const File *file = lua_file->file;
×
UNCOV
55
    lua_pushinteger(L, file->file_store_id);
×
56

UNCOV
57
    return 1;
×
UNCOV
58
}
×
59

60
static int LuaFileGetTxId(lua_State *L)
UNCOV
61
{
×
UNCOV
62
    lua_Integer tx_id = LuaStateGetTxId(L);
×
UNCOV
63
    lua_pushinteger(L, tx_id);
×
64

UNCOV
65
    return 1;
×
UNCOV
66
}
×
67

68
static int LuaFileGetName(lua_State *L)
UNCOV
69
{
×
UNCOV
70
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
71
    const File *file = lua_file->file;
×
UNCOV
72
    lua_pushlstring(L, (char *)file->name, file->name_len);
×
73

UNCOV
74
    return 1;
×
UNCOV
75
}
×
76

77
static int LuaFileGetSize(lua_State *L)
UNCOV
78
{
×
UNCOV
79
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
80
    const File *file = lua_file->file;
×
UNCOV
81
    lua_pushinteger(L, FileTrackedSize(file));
×
82

UNCOV
83
    return 1;
×
UNCOV
84
}
×
85

86
static int LuaFileGetMagic(lua_State *L)
UNCOV
87
{
×
UNCOV
88
#ifdef HAVE_MAGIC
×
UNCOV
89
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
90
    const File *file = lua_file->file;
×
UNCOV
91
    if (file->magic != NULL) {
×
92
        lua_pushstring(L, file->magic);
×
UNCOV
93
    } else {
×
UNCOV
94
        lua_pushnil(L);
×
UNCOV
95
    }
×
96
#else
97
    lua_pushnil(L);
98
#endif
99

UNCOV
100
    return 1;
×
UNCOV
101
}
×
102

103
static void PushHex(lua_State *L, const uint8_t *buf, size_t len)
UNCOV
104
{
×
105
    /* Large enough for sha256. */
UNCOV
106
    char hex[65] = "";
×
UNCOV
107
    for (size_t i = 0; i < len; i++) {
×
UNCOV
108
        char one[3] = "";
×
UNCOV
109
        snprintf(one, sizeof(one), "%02x", buf[i]);
×
UNCOV
110
        strlcat(hex, one, sizeof(hex));
×
UNCOV
111
    }
×
112

UNCOV
113
    lua_pushstring(L, hex);
×
UNCOV
114
}
×
115

116
static int LuaFileGetMd5(lua_State *L)
UNCOV
117
{
×
UNCOV
118
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
119
    const File *file = lua_file->file;
×
120

UNCOV
121
    if (file->flags & FILE_MD5) {
×
UNCOV
122
        PushHex(L, file->md5, sizeof(file->md5));
×
UNCOV
123
    } else {
×
124
        lua_pushnil(L);
×
125
    }
×
126

UNCOV
127
    return 1;
×
UNCOV
128
}
×
129

130
static int LuaFileGetSha1(lua_State *L)
UNCOV
131
{
×
UNCOV
132
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
133
    const File *file = lua_file->file;
×
134

UNCOV
135
    if (file->flags & FILE_SHA1) {
×
UNCOV
136
        PushHex(L, file->sha1, sizeof(file->sha1));
×
UNCOV
137
    } else {
×
138
        lua_pushnil(L);
×
139
    }
×
140

UNCOV
141
    return 1;
×
UNCOV
142
}
×
143

144
static int LuaFileGetSha256(lua_State *L)
UNCOV
145
{
×
UNCOV
146
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
147
    const File *file = lua_file->file;
×
148

UNCOV
149
    if (file->flags & FILE_SHA256) {
×
UNCOV
150
        PushHex(L, file->sha256, sizeof(file->sha256));
×
UNCOV
151
    } else {
×
152
        lua_pushnil(L);
×
153
    }
×
154

UNCOV
155
    return 1;
×
UNCOV
156
}
×
157

158
static int LuaFileGetState(lua_State *L)
UNCOV
159
{
×
UNCOV
160
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
161
    const File *file = lua_file->file;
×
162

UNCOV
163
    const char *state = "UNKNOWN";
×
UNCOV
164
    switch (file->state) {
×
UNCOV
165
        case FILE_STATE_CLOSED:
×
UNCOV
166
            state = "CLOSED";
×
UNCOV
167
            break;
×
168
        case FILE_STATE_TRUNCATED:
×
169
            state = "TRUNCATED";
×
170
            break;
×
171
        case FILE_STATE_ERROR:
×
172
            state = "ERROR";
×
173
            break;
×
174
        case FILE_STATE_OPENED:
×
175
            state = "OPENED";
×
176
            break;
×
177
        case FILE_STATE_NONE:
×
178
            state = "NONE";
×
179
            break;
×
180
        case FILE_STATE_MAX:
×
181
            break;
×
UNCOV
182
    }
×
183

UNCOV
184
    lua_pushstring(L, state);
×
185

UNCOV
186
    return 1;
×
UNCOV
187
}
×
188

189
static int LuaFileIsStored(lua_State *L)
UNCOV
190
{
×
UNCOV
191
    struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
×
UNCOV
192
    const File *file = lua_file->file;
×
UNCOV
193
    lua_pushboolean(L, file->flags & FILE_STORED);
×
194

UNCOV
195
    return 1;
×
UNCOV
196
}
×
197

198
static const struct luaL_Reg filelib[] = {
199
    { "get_state", LuaFileGetState },
200
    { "is_stored", LuaFileIsStored },
201
    { "file_id", LuaFileGetFileId },
202
    { "tx_id", LuaFileGetTxId },
203
    { "name", LuaFileGetName },
204
    { "size", LuaFileGetSize },
205
    { "magic", LuaFileGetMagic },
206
    { "md5", LuaFileGetMd5 },
207
    { "sha1", LuaFileGetSha1 },
208
    { "sha256", LuaFileGetSha256 },
209
    { NULL, NULL },
210
};
211

212
static const struct luaL_Reg filemodlib[] = {
213
    { "get_file", LuaFileGetFile },
214
    { NULL, NULL },
215
};
216

217
int SCLuaLoadFileLib(lua_State *L)
UNCOV
218
{
×
UNCOV
219
    luaL_newmetatable(L, file_mt);
×
UNCOV
220
    lua_pushvalue(L, -1);
×
UNCOV
221
    lua_setfield(L, -2, "__index");
×
UNCOV
222
    luaL_setfuncs(L, filelib, 0);
×
223

UNCOV
224
    luaL_newlib(L, filemodlib);
×
225

UNCOV
226
    return 1;
×
UNCOV
227
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc