epan: Add binary search to introspection API

This commit is contained in:
João Valverde 2021-11-27 13:33:09 +00:00
parent f4f9bf1d9e
commit 01a95db9b7
6 changed files with 49 additions and 15 deletions

View File

@ -587,6 +587,8 @@ libwireshark.so.0 libwireshark0 #MINVER#
epan_get_version_number@Base 2.5.0
epan_init@Base 2.9.0
epan_inspect_enums@Base 3.7.0
epan_inspect_enums_bsearch@Base 3.7.0
epan_inspect_enums_count@Base 3.7.0
epan_load_settings@Base 2.3.0
epan_memmem@Base 1.9.1
epan_new@Base 1.12.0~rc1

View File

@ -211,7 +211,7 @@ set(LIBWIRESHARK_NONGENERATED_FILES
guid-utils.c
iana_charsets.c
in_cksum.c
introspection-enums.c
introspection.c
ipproto.c
maxmind_db.c
media_params.c

View File

@ -14,8 +14,6 @@
* requirement to re-run the generator script.
*
*/
#include "config.h"
#include "introspection.h"
#include <epan/address.h>
#include <epan/ipproto.h>
#include <epan/proto.h>
@ -428,8 +426,3 @@ static ws_enum_t all_enums[] = {
ENUM(STR_UNICODE),
{ NULL, 0 },
};
const ws_enum_t *epan_inspect_enums(void)
{
return all_enums;
}

37
epan/introspection.c Normal file
View File

@ -0,0 +1,37 @@
/*
* Copyright 2021, João Valverde <j@v6e.pt>
*
* 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"
#include "introspection.h"
#include <string.h>
#include <stdlib.h>
#include "introspection-enums.c"
const ws_enum_t *epan_inspect_enums(void)
{
return all_enums;
}
static int compare_enum(const void *needle, const void *memb)
{
return strcmp(needle, ((const ws_enum_t *)memb)->symbol);
}
size_t epan_inspect_enums_count(void)
{
/* Exclude null terminator */
return sizeof(all_enums)/sizeof(ws_enum_t) - 1;
}
const ws_enum_t *epan_inspect_enums_bsearch(const char *needle)
{
return bsearch(needle, all_enums, epan_inspect_enums_count(),
sizeof(ws_enum_t), compare_enum);
}

View File

@ -11,6 +11,7 @@
#ifndef _INTROSPECTION_H_
#define _INTROSPECTION_H_
#include <stddef.h>
#include <ws_symbol_export.h>
typedef struct {
@ -28,4 +29,12 @@ typedef struct {
WS_DLL_PUBLIC
const ws_enum_t *epan_inspect_enums(void);
/** Returns size of enums array not including null terminator. */
WS_DLL_PUBLIC
size_t epan_inspect_enums_count(void);
/** Performs a binary search for the magic constant "needle". */
WS_DLL_PUBLIC
const ws_enum_t *epan_inspect_enums_bsearch(const char *needle);
#endif

View File

@ -44,8 +44,6 @@ source = """\
* requirement to re-run the generator script.
*
*/
#include "config.h"
#include "introspection.h"
""" % (os.path.basename(sys.argv[0]))
for f in args.infiles:
@ -68,11 +66,6 @@ for s in symbols:
source += """\
{ NULL, 0 },
};
const ws_enum_t *epan_inspect_enums(void)
{
return all_enums;
}
"""
try: