summaryrefslogtreecommitdiffstats
path: root/epan/wslua/wslua_wtap.c
blob: 524ed7c3566e4daa1075afa6dbb9f2ad60024f8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * wslua_wtap.c
 *
 * Wireshark's interface to the Lua Programming Language
 * for various libwiretap utility functions.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

/* WSLUA_MODULE Wtap Wtap Functions For Handling Capture File Types */

#include <limits.h>

#include "wslua.h"
#include <wiretap/wtap.h>

/*
 * Solely for the function that gets the table of backwards-compatibility
 * Lua names for file types/subtypes.
 */
#include <wiretap/wtap-int.h>

WSLUA_FUNCTION wslua_wtap_file_type_subtype_description(lua_State* LS) {
    /*
    Get a string describing a capture file type, given a filetype
    value for that file type.

    @since 3.2.12, 3.4.4
    */
#define WSLUA_ARG_wtap_file_type_subtype_description_FILETYPE 1 /* The type for which the description is to be fetched - a number returned by `wtap_name_to_file_type_subtype()`. */
    lua_Number filetype = luaL_checknumber(LS,WSLUA_ARG_wtap_file_type_subtype_description_FILETYPE);
    /* wtap_file_type_subtype_description()'s name isn't really descriptive. */
    if (filetype > INT_MAX) {
        /* Too big. */
        lua_pushnil(LS);
    } else {
        const gchar* str = wtap_file_type_subtype_description((int)filetype);
        if (str == NULL)
            lua_pushnil(LS);
        else
            lua_pushstring(LS,str);
    }
    WSLUA_RETURN(1); /* The description of the file type with that filetype value, or nil if there is no such file type. */
}

WSLUA_FUNCTION wslua_wtap_file_type_subtype_name(lua_State* LS) {
    /*
    Get a string giving the name for a capture file type, given a filetype
    value for that file type.

    @since 3.2.12, 3.4.4
    */
#define WSLUA_ARG_wtap_file_type_subtype_name_FILETYPE 1 /* The type for which the name is to be fetched - a number returned by `wtap_name_to_file_type_subtype()`. */
    lua_Number filetype = luaL_checknumber(LS,WSLUA_ARG_wtap_file_type_subtype_name_FILETYPE);
    /* wtap_file_type_subtype_description()'s name isn't really descriptive. */
    if (filetype > INT_MAX) {
        /* Too big. */
        lua_pushnil(LS);
    } else {
        const gchar* str = wtap_file_type_subtype_name((int)filetype);
        if (str == NULL)
            lua_pushnil(LS);
        else
           lua_pushstring(LS,str);
    }
    WSLUA_RETURN(1); /* The name of the file type with that filetype value, or nil if there is no such file type. */
}

WSLUA_FUNCTION wslua_wtap_name_to_file_type_subtype(lua_State* LS) {
    /*
    Get a filetype value for a file type, given the name for that
    file type.

    @since 3.2.12, 3.4.4
    */
#define WSLUA_ARG_wtap_name_to_file_type_subtype_NAME 1 /* The name of a file type. */
    const char* name = luaL_checkstring(LS,WSLUA_ARG_wtap_name_to_file_type_subtype_NAME);
    lua_Number filetype = wtap_name_to_file_type_subtype(name);
    if (filetype == -1)
        lua_pushnil(LS);
    else
        lua_pushnumber(LS,filetype);
    WSLUA_RETURN(1); /* The filetype value for the file type with that name, or nil if there is no such file type. */
}

WSLUA_FUNCTION wslua_wtap_pcap_file_type_subtype(lua_State* LS) {
    /*
    Get the filetype value for pcap files.

    @since 3.2.12, 3.4.4
    */
    lua_Number filetype = wtap_pcap_file_type_subtype();
    lua_pushnumber(LS,filetype);
    WSLUA_RETURN(1); /* The filetype value for pcap files. */
}

WSLUA_FUNCTION wslua_wtap_pcap_nsec_file_type_subtype(lua_State* LS) {
    /*
    Get the filetype value for nanosecond-resolution pcap files.

    @since 3.2.12, 3.4.4
    */
    lua_Number filetype = wtap_pcap_nsec_file_type_subtype();
    lua_pushnumber(LS,filetype);
    WSLUA_RETURN(1); /* The filetype value for nanosecond-resolution pcap files. */
}

WSLUA_FUNCTION wslua_wtap_pcapng_file_type_subtype(lua_State* LS) {
    /*
    Get the filetype value for pcapng files.

    @since 3.2.12, 3.4.4
    */
    lua_Number filetype = wtap_pcapng_file_type_subtype();
    lua_pushnumber(LS,filetype);
    WSLUA_RETURN(1); /* The filetype value for pcapng files. */
}

/*
 * init.wslua-only function to return a table to assign to
 * wtap_filetypes.
 */
extern void wslua_init_wtap_filetypes(lua_State* LS) {
    /* Get the GArray from which we initialize this. */
    const GArray *table = get_backwards_compatibility_lua_table();

    /*
     * Create the table; it's indexted by strings, not numbers,
     * so none of the entries will be in a sequence.
     */
    lua_createtable(LS,0,table->len);
    for (guint i = 0; i < table->len; i++) {
        struct backwards_compatibiliity_lua_name *entry;

        entry = &g_array_index(table,
            struct backwards_compatibiliity_lua_name, i);
        /*
         * Push the name and the ft, in order, so that the ft,
         * which should be the value at the top of the stack,
         * is at the top of the stack, and the name, which should
         * be the value just below that, is the value just below
         * it.
         */
        lua_pushstring(LS, entry->name);
        lua_pushnumber(LS, entry->ft);
        /*
         * The -3 is the index, relative to the top of the stack, of
         * the table; the two elements on top of it are the ft and
         * the name, so it's -3.
         */
        lua_settable(LS, -3);
    }
    lua_setglobal(LS, "wtap_filetypes");
}