2021-02-13 03:29:33 +00:00
/*
* 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>
wiretap: more work on file type/subtypes.
Provide a wiretap routine to get an array of all savable file
type/subtypes, sorted with pcap and pcapng at the top, followed by the
other types, sorted either by the name or the description.
Use that routine to list options for the -F flag for various commands
Rename wtap_get_savable_file_types_subtypes() to
wtap_get_savable_file_types_subtypes_for_file(), to indicate that it
provides an array of all file type/subtypes in which a given file can be
saved. Have it sort all types, other than the default type/subtype and,
if there is one, the "other" type (both of which are put at the top), by
the name or the description.
Don't allow wtap_register_file_type_subtypes() to override any existing
registrations; have them always register a new type. In that routine,
if there are any emply slots in the table, due to an entry being
unregistered, use it rather than allocating a new slot.
Don't allow unregistration of built-in types.
Rename the "dump open table" to the "file type/subtype table", as it has
entries for all types/subtypes, even if we can't write them.
Initialize that table in a routine that pre-allocates the GArray before
filling it with built-in types/subtypes, so it doesn't keep getting
reallocated.
Get rid of wtap_num_file_types_subtypes - it's just a copy of the size
of the GArray.
Don't have wtap_file_type_subtype_description() crash if handed an
file type/subtype that isn't a valid array index - just return NULL, as
we do with wtap_file_type_subtype_name().
In wtap_name_to_file_type_subtype(), don't use WTAP_FILE_TYPE_SUBTYPE_
names for the backwards-compatibility names - map those names to the
current names, and then look them up. This reduces the number of
uses of hardwired WTAP_FILE_TYPE_SUBTYPE_ values.
Clean up the type of wtap_module_count - it has no need to be a gulong.
Have built-in wiretap file handlers register names to be used for their
file type/subtypes, rather than building the table in init.lua.
Add a new Lua C function get_wtap_filetypes() to construct the
wtap_filetypes table, based on the registered names, and use it in
init.lua.
Add a #define WSLUA_INTERNAL_FUNCTION to register functions intended
only for internal use in init.lua, so they can be made available from
Lua without being documented.
Get rid of WTAP_NUM_FILE_TYPES_SUBTYPES - most code has no need to use
it, as it can just request arrays of types, and the space of
type/subtype codes can be sparse due to registration in any case, so
code has to be careful using it.
wtap_get_num_file_types_subtypes() is no longer used, so remove it. It
returns the number of elements in the file type/subtype array, which is
not necessarily the name of known file type/subtypes, as there may have
been some deregistered types, and those types do *not* get removed from
the array, they just get cleared so that they're available for future
allocation (we don't want the indices of any registered types to changes
if another type is deregistered, as those indicates are the type/subtype
values, so we can't shrink the array).
Clean up white space and remove some comments that shouldn't have been
added.
2021-02-17 06:24:47 +00:00
/*
* Solely for the function that gets the table of backwards - compatibility
* Lua names for file types / subtypes .
*/
# include <wiretap/wtap-int.h>
2021-02-13 03:29:33 +00:00
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
*/
2021-02-14 06:12:23 +00:00
# define WSLUA_ARG_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()`. */
2021-02-13 03:29:33 +00:00
lua_Number filetype = luaL_checknumber ( LS , WSLUA_ARG_file_type_subtype_description_FILETYPE ) ;
2021-02-13 08:03:51 +00:00
/* wtap_file_type_subtype_description()'s name isn't really descriptive. */
2021-02-13 03:29:33 +00:00
if ( filetype > INT_MAX ) {
/* Too big. */
lua_pushnil ( LS ) ;
} else {
2021-02-13 08:03:51 +00:00
const gchar * str = wtap_file_type_subtype_description ( ( int ) filetype ) ;
2021-02-13 03:29:33 +00:00
if ( str = = NULL )
lua_pushnil ( LS ) ;
else
lua_pushstring ( LS , str ) ;
}
2021-02-14 06:12:23 +00:00
WSLUA_RETURN ( 1 ) ; /* The description of the file type with that filetype value, or nil if there is no such file type. */
2021-02-13 03:29:33 +00:00
}
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
*/
2021-02-14 06:12:23 +00:00
# define WSLUA_ARG_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()`. */
2021-02-13 03:29:33 +00:00
lua_Number filetype = luaL_checknumber ( LS , WSLUA_ARG_file_type_subtype_name_FILETYPE ) ;
2021-02-13 08:03:51 +00:00
/* wtap_file_type_subtype_description()'s name isn't really descriptive. */
2021-02-13 03:29:33 +00:00
if ( filetype > INT_MAX ) {
/* Too big. */
lua_pushnil ( LS ) ;
} else {
2021-02-13 08:03:51 +00:00
const gchar * str = wtap_file_type_subtype_name ( ( int ) filetype ) ;
2021-02-13 03:29:33 +00:00
if ( str = = NULL )
lua_pushnil ( LS ) ;
else
lua_pushstring ( LS , str ) ;
}
2021-02-14 06:12:23 +00:00
WSLUA_RETURN ( 1 ) ; /* The name of the file type with that filetype value, or nil if there is no such file type. */
2021-02-13 03:29:33 +00:00
}
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
*/
2021-02-14 06:12:23 +00:00
# define WSLUA_ARG_name_to_file_type_subtype_NAME 1 /* The name of a file type. */
2021-02-13 03:29:33 +00:00
const char * name = luaL_checkstring ( LS , WSLUA_ARG_name_to_file_type_subtype_NAME ) ;
2021-02-13 08:03:51 +00:00
lua_Number filetype = wtap_name_to_file_type_subtype ( name ) ;
2021-02-13 03:29:33 +00:00
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. */
}
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
2021-02-23 06:23:54 +00:00
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
*/
2021-02-23 09:18:31 +00:00
lua_Number filetype = wtap_pcap_file_type_subtype ( ) ;
2021-02-23 06:23:54 +00:00
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
*/
2021-02-23 09:18:31 +00:00
lua_Number filetype = wtap_pcap_nsec_file_type_subtype ( ) ;
2021-02-23 06:23:54 +00:00
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
*/
2021-02-23 09:18:31 +00:00
lua_Number filetype = wtap_pcapng_file_type_subtype ( ) ;
2021-02-23 06:23:54 +00:00
lua_pushnumber ( LS , filetype ) ;
WSLUA_RETURN ( 1 ) ; /* The filetype value for pcapng files. */
}
wiretap: more work on file type/subtypes.
Provide a wiretap routine to get an array of all savable file
type/subtypes, sorted with pcap and pcapng at the top, followed by the
other types, sorted either by the name or the description.
Use that routine to list options for the -F flag for various commands
Rename wtap_get_savable_file_types_subtypes() to
wtap_get_savable_file_types_subtypes_for_file(), to indicate that it
provides an array of all file type/subtypes in which a given file can be
saved. Have it sort all types, other than the default type/subtype and,
if there is one, the "other" type (both of which are put at the top), by
the name or the description.
Don't allow wtap_register_file_type_subtypes() to override any existing
registrations; have them always register a new type. In that routine,
if there are any emply slots in the table, due to an entry being
unregistered, use it rather than allocating a new slot.
Don't allow unregistration of built-in types.
Rename the "dump open table" to the "file type/subtype table", as it has
entries for all types/subtypes, even if we can't write them.
Initialize that table in a routine that pre-allocates the GArray before
filling it with built-in types/subtypes, so it doesn't keep getting
reallocated.
Get rid of wtap_num_file_types_subtypes - it's just a copy of the size
of the GArray.
Don't have wtap_file_type_subtype_description() crash if handed an
file type/subtype that isn't a valid array index - just return NULL, as
we do with wtap_file_type_subtype_name().
In wtap_name_to_file_type_subtype(), don't use WTAP_FILE_TYPE_SUBTYPE_
names for the backwards-compatibility names - map those names to the
current names, and then look them up. This reduces the number of
uses of hardwired WTAP_FILE_TYPE_SUBTYPE_ values.
Clean up the type of wtap_module_count - it has no need to be a gulong.
Have built-in wiretap file handlers register names to be used for their
file type/subtypes, rather than building the table in init.lua.
Add a new Lua C function get_wtap_filetypes() to construct the
wtap_filetypes table, based on the registered names, and use it in
init.lua.
Add a #define WSLUA_INTERNAL_FUNCTION to register functions intended
only for internal use in init.lua, so they can be made available from
Lua without being documented.
Get rid of WTAP_NUM_FILE_TYPES_SUBTYPES - most code has no need to use
it, as it can just request arrays of types, and the space of
type/subtype codes can be sparse due to registration in any case, so
code has to be careful using it.
wtap_get_num_file_types_subtypes() is no longer used, so remove it. It
returns the number of elements in the file type/subtype array, which is
not necessarily the name of known file type/subtypes, as there may have
been some deregistered types, and those types do *not* get removed from
the array, they just get cleared so that they're available for future
allocation (we don't want the indices of any registered types to changes
if another type is deregistered, as those indicates are the type/subtype
values, so we can't shrink the array).
Clean up white space and remove some comments that shouldn't have been
added.
2021-02-17 06:24:47 +00:00
/*
* init . wslua - only function to return a table to assign to
* wtap_filetypes .
*/
WSLUA_INTERNAL_FUNCTION wslua_get_wtap_filetypes ( lua_State * LS ) {
/* Get the GArray from which we initialize this. */
const GArray * table = get_backwards_compatibility_lua_table ( ) ;
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
wiretap: more work on file type/subtypes.
Provide a wiretap routine to get an array of all savable file
type/subtypes, sorted with pcap and pcapng at the top, followed by the
other types, sorted either by the name or the description.
Use that routine to list options for the -F flag for various commands
Rename wtap_get_savable_file_types_subtypes() to
wtap_get_savable_file_types_subtypes_for_file(), to indicate that it
provides an array of all file type/subtypes in which a given file can be
saved. Have it sort all types, other than the default type/subtype and,
if there is one, the "other" type (both of which are put at the top), by
the name or the description.
Don't allow wtap_register_file_type_subtypes() to override any existing
registrations; have them always register a new type. In that routine,
if there are any emply slots in the table, due to an entry being
unregistered, use it rather than allocating a new slot.
Don't allow unregistration of built-in types.
Rename the "dump open table" to the "file type/subtype table", as it has
entries for all types/subtypes, even if we can't write them.
Initialize that table in a routine that pre-allocates the GArray before
filling it with built-in types/subtypes, so it doesn't keep getting
reallocated.
Get rid of wtap_num_file_types_subtypes - it's just a copy of the size
of the GArray.
Don't have wtap_file_type_subtype_description() crash if handed an
file type/subtype that isn't a valid array index - just return NULL, as
we do with wtap_file_type_subtype_name().
In wtap_name_to_file_type_subtype(), don't use WTAP_FILE_TYPE_SUBTYPE_
names for the backwards-compatibility names - map those names to the
current names, and then look them up. This reduces the number of
uses of hardwired WTAP_FILE_TYPE_SUBTYPE_ values.
Clean up the type of wtap_module_count - it has no need to be a gulong.
Have built-in wiretap file handlers register names to be used for their
file type/subtypes, rather than building the table in init.lua.
Add a new Lua C function get_wtap_filetypes() to construct the
wtap_filetypes table, based on the registered names, and use it in
init.lua.
Add a #define WSLUA_INTERNAL_FUNCTION to register functions intended
only for internal use in init.lua, so they can be made available from
Lua without being documented.
Get rid of WTAP_NUM_FILE_TYPES_SUBTYPES - most code has no need to use
it, as it can just request arrays of types, and the space of
type/subtype codes can be sparse due to registration in any case, so
code has to be careful using it.
wtap_get_num_file_types_subtypes() is no longer used, so remove it. It
returns the number of elements in the file type/subtype array, which is
not necessarily the name of known file type/subtypes, as there may have
been some deregistered types, and those types do *not* get removed from
the array, they just get cleared so that they're available for future
allocation (we don't want the indices of any registered types to changes
if another type is deregistered, as those indicates are the type/subtype
values, so we can't shrink the array).
Clean up white space and remove some comments that shouldn't have been
added.
2021-02-17 06:24:47 +00:00
/*
* 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 ;
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
wiretap: more work on file type/subtypes.
Provide a wiretap routine to get an array of all savable file
type/subtypes, sorted with pcap and pcapng at the top, followed by the
other types, sorted either by the name or the description.
Use that routine to list options for the -F flag for various commands
Rename wtap_get_savable_file_types_subtypes() to
wtap_get_savable_file_types_subtypes_for_file(), to indicate that it
provides an array of all file type/subtypes in which a given file can be
saved. Have it sort all types, other than the default type/subtype and,
if there is one, the "other" type (both of which are put at the top), by
the name or the description.
Don't allow wtap_register_file_type_subtypes() to override any existing
registrations; have them always register a new type. In that routine,
if there are any emply slots in the table, due to an entry being
unregistered, use it rather than allocating a new slot.
Don't allow unregistration of built-in types.
Rename the "dump open table" to the "file type/subtype table", as it has
entries for all types/subtypes, even if we can't write them.
Initialize that table in a routine that pre-allocates the GArray before
filling it with built-in types/subtypes, so it doesn't keep getting
reallocated.
Get rid of wtap_num_file_types_subtypes - it's just a copy of the size
of the GArray.
Don't have wtap_file_type_subtype_description() crash if handed an
file type/subtype that isn't a valid array index - just return NULL, as
we do with wtap_file_type_subtype_name().
In wtap_name_to_file_type_subtype(), don't use WTAP_FILE_TYPE_SUBTYPE_
names for the backwards-compatibility names - map those names to the
current names, and then look them up. This reduces the number of
uses of hardwired WTAP_FILE_TYPE_SUBTYPE_ values.
Clean up the type of wtap_module_count - it has no need to be a gulong.
Have built-in wiretap file handlers register names to be used for their
file type/subtypes, rather than building the table in init.lua.
Add a new Lua C function get_wtap_filetypes() to construct the
wtap_filetypes table, based on the registered names, and use it in
init.lua.
Add a #define WSLUA_INTERNAL_FUNCTION to register functions intended
only for internal use in init.lua, so they can be made available from
Lua without being documented.
Get rid of WTAP_NUM_FILE_TYPES_SUBTYPES - most code has no need to use
it, as it can just request arrays of types, and the space of
type/subtype codes can be sparse due to registration in any case, so
code has to be careful using it.
wtap_get_num_file_types_subtypes() is no longer used, so remove it. It
returns the number of elements in the file type/subtype array, which is
not necessarily the name of known file type/subtypes, as there may have
been some deregistered types, and those types do *not* get removed from
the array, they just get cleared so that they're available for future
allocation (we don't want the indices of any registered types to changes
if another type is deregistered, as those indicates are the type/subtype
values, so we can't shrink the array).
Clean up white space and remove some comments that shouldn't have been
added.
2021-02-17 06:24:47 +00:00
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 ) ;
}
WSLUA_RETURN ( 1 ) ; /* The table. */
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
}