aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDarius Davis <darius-wireshark@free-range.com.au>2024-02-17 18:41:43 +1000
committerGerald Combs <gerald@wireshark.org>2024-02-18 17:55:56 +0000
commitf91c6bc81257c6f45a4adf66f8303aaa5df345e2 (patch)
tree87f2eda15f113fcf8d0209968fa0dad59d753d25
parenta9064a104892e22ffae31bfdaf55f96381d60046 (diff)
Tools: Use bsearch to look up vendor in pci-ids.
-rw-r--r--epan/pci-ids.c37
-rwxr-xr-xtools/make-pci-ids.py37
2 files changed, 10 insertions, 64 deletions
diff --git a/epan/pci-ids.c b/epan/pci-ids.c
index f506c147de..04dda1b510 100644
--- a/epan/pci-ids.c
+++ b/epan/pci-ids.c
@@ -30,6 +30,7 @@
#include <config.h>
#include <stddef.h>
+#include <stdlib.h>
#include "pci-ids.h"
@@ -46525,38 +46526,10 @@ static pci_vid_index_t const pci_vid_index[] = {
{0xFFFF, 1, pci_vid_FFFF },
}; /* We have 2376 VIDs */
-static pci_vid_index_t const *get_vid_index(uint16_t vid)
+static int vid_search(const void *key, const void *tbl_entry)
{
- uint32_t start_index = 0;
- uint32_t end_index = 0;
- uint32_t idx = 0;
-
- end_index = sizeof(pci_vid_index)/sizeof(pci_vid_index[0]);
-
- while(start_index != end_index)
- {
- if(end_index - start_index == 1)
- {
- if(pci_vid_index[start_index].vid == vid)
- return &pci_vid_index[start_index];
-
- break;
- }
-
- idx = (start_index + end_index)/2;
-
- if(pci_vid_index[idx].vid < vid)
- start_index = idx;
- else
- if(pci_vid_index[idx].vid > vid)
- end_index = idx;
- else
- return &pci_vid_index[idx];
-
- }
-
- return NULL;
-
+ return (int)*(const uint16_t *)key -
+ (int)((const pci_vid_index_t *)tbl_entry)->vid;
}
const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
@@ -46566,7 +46539,7 @@ const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
pci_vid_index_t const *index_ptr;
pci_id_t const *ids_ptr;
- index_ptr = get_vid_index(vid);
+ index_ptr = bsearch(&vid, pci_vid_index, sizeof pci_vid_index / sizeof pci_vid_index[0], sizeof pci_vid_index[0], vid_search);
if(index_ptr == NULL)
return not_found;
diff --git a/tools/make-pci-ids.py b/tools/make-pci-ids.py
index 093637d729..554fcb4d63 100755
--- a/tools/make-pci-ids.py
+++ b/tools/make-pci-ids.py
@@ -35,6 +35,7 @@ CODE_PREFIX = """\
#include <config.h>
#include <stddef.h>
+#include <stdlib.h>
#include "pci-ids.h"
@@ -59,38 +60,10 @@ typedef struct
"""
CODE_POSTFIX = """
-static pci_vid_index_t const *get_vid_index(uint16_t vid)
+static int vid_search(const void *key, const void *tbl_entry)
{
- uint32_t start_index = 0;
- uint32_t end_index = 0;
- uint32_t idx = 0;
-
- end_index = sizeof(pci_vid_index)/sizeof(pci_vid_index[0]);
-
- while(start_index != end_index)
- {
- if(end_index - start_index == 1)
- {
- if(pci_vid_index[start_index].vid == vid)
- return &pci_vid_index[start_index];
-
- break;
- }
-
- idx = (start_index + end_index)/2;
-
- if(pci_vid_index[idx].vid < vid)
- start_index = idx;
- else
- if(pci_vid_index[idx].vid > vid)
- end_index = idx;
- else
- return &pci_vid_index[idx];
-
- }
-
- return NULL;
-
+ return (int)*(const uint16_t *)key -
+ (int)((const pci_vid_index_t *)tbl_entry)->vid;
}
const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
@@ -100,7 +73,7 @@ const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
pci_vid_index_t const *index_ptr;
pci_id_t const *ids_ptr;
- index_ptr = get_vid_index(vid);
+ index_ptr = bsearch(&vid, pci_vid_index, sizeof pci_vid_index / sizeof pci_vid_index[0], sizeof pci_vid_index[0], vid_search);
if(index_ptr == NULL)
return not_found;