regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / epan / wslua / wslua_file_common.c
blobab0f2a65ec572ea2a737338187e4431c40c3f60b
1 /*
2 * wslua_file_common.c
4 * Wireshark's interface to the Lua Programming Language
5 * for file handling related source file internal functions.
7 * (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * SPDX-License-Identifier: GPL-2.0-or-later
16 /************
17 * The following is for handling private data for the duration of the file
18 * read_open/read/close cycle, or write_open/write/write_close cycle.
19 * In other words it handles the "priv" member of wtap and wtap_dumper,
20 * but for the Lua script's use. A Lua script can set a Lua table
21 * to CaptureInfo/CaptureInfoConst and have it saved and retrievable this way.
22 * We need to offer that, because there needs to be a way for Lua scripts
23 * to save state for a given file's operations cycle. Since there can be
24 * two files opened at the same time for the same Lua script (due to reload
25 * and other such events), the script can't just have one file state.
27 #include "config.h"
29 #include "wslua_file_common.h"
32 /* create and set the wtap->priv private data for the file instance */
33 void create_wth_priv(lua_State* L, wtap *wth) {
34 file_priv_t *priv = g_new(file_priv_t, 1);
36 if (wth->priv != NULL) {
37 g_free(priv);
38 luaL_error(L, "Cannot create wtap private data because there already is private data");
39 return;
41 priv->table_ref = LUA_NOREF;
42 wth->priv = (void*) priv;
45 /* gets the private data table from wtap */
46 int get_wth_priv_table_ref(lua_State* L, wtap *wth) {
47 file_priv_t *priv = (file_priv_t*) wth->priv;
49 if (!priv) {
50 /* shouldn't be possible */
51 luaL_error(L, "Cannot get wtap private data: it is null");
52 return LUA_NOREF;
55 /* the following might push a nil, but that's ok */
56 lua_rawgeti(L, LUA_REGISTRYINDEX, priv->table_ref);
58 return 1;
61 /* sets the private data to wtap - the table is presumed on top of stack */
62 int set_wth_priv_table_ref(lua_State* L, wtap *wth) {
63 file_priv_t *priv = (file_priv_t*) wth->priv;
65 if (!priv) {
66 /* shouldn't be possible */
67 luaL_error(L, "Cannot get wtap private data: it is null");
68 return 0;
71 if (lua_isnil(L, -1)){
72 /* user is setting it nil - ok, de-ref any previous one */
73 luaL_unref(L, LUA_REGISTRYINDEX, priv->table_ref);
74 priv->table_ref = LUA_NOREF;
75 return 0;
78 if (!lua_istable(L, -1)) {
79 luaL_error(L, "The private_table member can only be set to a table or nil");
80 return 0;
83 /* if we had a table already referenced, de-ref it first */
84 if (priv->table_ref != LUA_NOREF) {
85 luaL_unref(L, LUA_REGISTRYINDEX, priv->table_ref);
88 priv->table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
90 return 0;
93 /* remove, deref, and free the wtap->priv data */
94 void remove_wth_priv(lua_State* L, wtap *wth) {
95 file_priv_t *priv = (file_priv_t*) wth->priv;
97 if (!priv) {
98 /* shouldn't be possible */
99 luaL_error(L, "Cannot remove wtap private data: it is null");
100 return;
103 luaL_unref(L, LUA_REGISTRYINDEX, priv->table_ref);
105 g_free(wth->priv);
106 wth->priv = NULL;
109 /* create and set the wtap_dumper->priv private data for the file instance */
110 void create_wdh_priv(lua_State* L, wtap_dumper *wdh) {
111 file_priv_t *priv = g_new(file_priv_t, 1);
113 if (wdh->priv != NULL) {
114 g_free(priv);
115 luaL_error(L, "Cannot create wtap_dumper private data because there already is private data");
116 return;
118 priv->table_ref = LUA_NOREF;
119 wdh->priv = (void*) priv;
122 /* get the private data from wtap_dumper */
123 int get_wdh_priv_table_ref(lua_State* L, wtap_dumper *wdh) {
124 file_priv_t *priv = (file_priv_t*) wdh->priv;
126 if (!priv) {
127 /* shouldn't be possible */
128 luaL_error(L, "Cannot get wtap_dumper private data: it is null");
129 return LUA_NOREF;
132 /* the following might push a nil, but that's ok */
133 lua_rawgeti(L, LUA_REGISTRYINDEX, priv->table_ref);
135 return 1;
138 /* sets the private data to wtap - the table is presumed on top of stack */
139 int set_wdh_priv_table_ref(lua_State* L, wtap_dumper *wdh) {
140 file_priv_t *priv = (file_priv_t*) wdh->priv;
142 if (!priv) {
143 /* shouldn't be possible */
144 luaL_error(L, "Cannot get wtap private data: it is null");
145 return 0;
148 if (lua_isnil(L, -1)){
149 /* user is setting it nil - ok, de-ref any previous one */
150 luaL_unref(L, LUA_REGISTRYINDEX, priv->table_ref);
151 priv->table_ref = LUA_NOREF;
152 return 0;
155 if (!lua_istable(L, -1)) {
156 luaL_error(L, "The private_table member can only be set to a table or nil");
157 return 0;
160 /* if we had a table already referenced, de-ref it first */
161 if (priv->table_ref != LUA_NOREF) {
162 luaL_unref(L, LUA_REGISTRYINDEX, priv->table_ref);
165 priv->table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
167 return 0;
170 /* remove and deref the wtap_dumper->priv data */
171 void remove_wdh_priv(lua_State* L, wtap_dumper *wdh) {
172 file_priv_t *priv = (file_priv_t*) wdh->priv;
174 if (!priv) {
175 /* shouldn't be possible */
176 luaL_error(L, "Cannot remove wtap_dumper private data: it is null");
177 return;
180 luaL_unref(L, LUA_REGISTRYINDEX, priv->table_ref);
181 /* we do NOT free wtap_dumper's priv member - wtap_dump_close() free's it */
186 * Editor modelines - https://www.wireshark.org/tools/modelines.html
188 * Local variables:
189 * c-basic-offset: 4
190 * tab-width: 8
191 * indent-tabs-mode: nil
192 * End:
194 * vi: set shiftwidth=4 tabstop=8 expandtab:
195 * :indentSize=4:tabSize=8:noTabs=true: