dcerpc-nt: add UNION_ALIGN_TO... helpers
[wireshark-sm.git] / tools / make-pci-ids.py
blob0aadef148e73d1ab196b6fa52e8ea14a6d19dac4
1 #!/usr/bin/env python3
3 # make-pci-ids - Creates a file containing PCI IDs.
4 # It use the databases from
5 # https://github.com/pciutils/pciids/raw/master/pci.ids
6 # to create our file epan/dissectors/pci-ids.c
8 # Wireshark - Network traffic analyzer
10 # By Caleb Chiu <caleb.chiu@macnica.com>
11 # Copyright 2021
13 # SPDX-License-Identifier: GPL-2.0-or-later
16 import string
17 import sys
18 import urllib.request, urllib.error, urllib.parse
20 OUTPUT_FILE = "epan/pci-ids.c"
22 MIN_VENDOR_COUNT = 2250 # 2261 on 2021-11-01
23 MIN_DEVICE_COUNT = 33000 # 33724 on 2021-11-01
25 CODE_PREFIX = """\
27 * Generated by tools/make-pci-ids.py
28 * By Caleb Chiu <caleb.chiu@macnica.com>
29 * Copyright 2021
32 * SPDX-License-Identifier: GPL-2.0-or-later
35 #include <config.h>
37 #include <stddef.h>
38 #include <stdlib.h>
40 #include "wsutil/array.h"
42 #include "pci-ids.h"
44 typedef struct
46 uint16_t vid;
47 uint16_t did;
48 uint16_t svid;
49 uint16_t ssid;
50 const char *name;
52 } pci_id_t;
54 typedef struct
56 uint16_t vid;
57 uint16_t count;
58 pci_id_t const *ids_ptr;
60 } pci_vid_index_t;
62 """
64 CODE_POSTFIX = """
65 static int vid_search(const void *key, const void *tbl_entry)
67 return (int)*(const uint16_t *)key -
68 (int)((const pci_vid_index_t *)tbl_entry)->vid;
71 const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
73 unsigned int i;
74 static const char *not_found = \"Not found\";
75 pci_vid_index_t const *index_ptr;
76 pci_id_t const *ids_ptr;
78 index_ptr = bsearch(&vid, pci_vid_index, array_length(pci_vid_index), sizeof pci_vid_index[0], vid_search);
80 if(index_ptr == NULL)
81 return not_found;
83 ids_ptr = index_ptr->ids_ptr;
84 for(i = 0; i < index_ptr->count; ids_ptr++, i++)
85 if(vid == ids_ptr->vid &&
86 did == ids_ptr->did &&
87 svid == ids_ptr->svid &&
88 ssid == ids_ptr->ssid)
89 return ids_ptr->name;
90 return not_found;
93 """
96 id_list=[]
97 count_list=[]
100 def exit_msg(msg=None, status=1):
101 if msg is not None:
102 sys.stderr.write(msg + '\n')
103 sys.exit(status)
106 def main():
107 req_headers = { 'User-Agent': 'Wireshark make-pci-ids' }
108 req = urllib.request.Request('https://github.com/pciutils/pciids/raw/master/pci.ids', headers=req_headers)
109 response = urllib.request.urlopen(req)
110 lines = response.read().decode('UTF-8', 'replace').splitlines()
112 out_lines = '''\
113 /* pci-ids.c
115 * pci-ids.c is based on the pci.ids of The PCI ID Repository at
116 * https://pci-ids.ucw.cz/, fetched indirectly via
117 * https://github.com/pciutils/pciids
119 vid = -1
120 did = -1
121 svid = -1
122 entries = 0
123 line_num = 0
125 for line in lines:
126 line = line.strip('\n')
127 line_num += 1
129 if line_num <= 15:
130 line = line.replace('#', ' ', 1)
131 line = line.lstrip()
132 line = line.replace("GNU General Public License", "GPL")
133 if line:
134 line = ' * ' + line
135 else:
136 line = ' *' + line
137 out_lines += line + '\n'
138 if line_num == 15:
139 out_lines += CODE_PREFIX
141 line = line.replace("\\","\\\\")
142 line = line.replace("\"","\\\"")
143 line = line.replace("?","?-")
144 tabs = len(line) - len(line.lstrip('\t'))
145 if tabs == 0:
146 #print line
147 words = line.split(" ", 1)
148 if len(words) < 2:
149 continue
150 if len(words[0]) != 4:
151 continue
152 if all(c in string.hexdigits for c in words[0]):
153 hex_int = int(words[0], 16)
154 if vid != -1:
155 out_lines += "}; /* pci_vid_%04X[] */\n\n" % (vid)
156 count_list.append(entries)
157 vid = hex_int
158 entries = 1
159 did = -1
160 svid = -1
161 ssid = -1
162 out_lines += "static pci_id_t const pci_vid_%04X[] = {\n" % (vid)
163 out_lines += "{0x%04X, 0xFFFF, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid, words[1].strip(), vid)
164 id_list.append(vid)
165 continue
167 if tabs == 1:
168 line = line.strip('\t')
169 words = line.split(" ", 1)
170 if len(words) < 2:
171 continue
172 if len(words[0]) != 4:
173 continue
174 if all(c in string.hexdigits for c in words[0]):
175 hex_int = int(words[0], 16)
176 did = hex_int
177 svid = -1
178 ssid = -1
179 out_lines += "{0x%04X, 0x%04X, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid, did, words[1].strip(), did)
180 entries += 1
181 continue
183 if tabs == 2:
184 line = line.strip('\t')
185 words = line.split(" ", 2)
186 if len(words[0]) != 4:
187 continue
188 if all(c in string.hexdigits for c in words[0]):
189 hex_int = int(words[0], 16)
190 svid = hex_int
192 if all(c in string.hexdigits for c in words[1]):
193 hex_int = int(words[1], 16)
194 ssid = hex_int
196 out_lines += "{0x%04X, 0x%04X, 0x%04X, 0x%04X, \"%s(0x%04X-0x%04X)\"},\n" % (vid, did, svid, ssid, words[2].strip(), svid, ssid)
197 entries += 1
198 svid = -1
199 ssid = -1
200 continue
202 out_lines += "}; /* pci_vid_%04X[] */\n" % (vid)
203 count_list.append(entries)
205 out_lines += "\nstatic pci_vid_index_t const pci_vid_index[] = {\n"
207 vendor_count = len(id_list)
208 device_count = 0
209 for i in range(vendor_count):
210 out_lines += "{0x%04X, %d, pci_vid_%04X },\n" % (id_list[i], count_list[i], id_list[i])
211 device_count += count_list[i]
213 out_lines += "}; /* We have %d VIDs */\n" % (vendor_count)
215 out_lines += CODE_POSTFIX
217 if vendor_count < MIN_VENDOR_COUNT:
218 exit_msg(f'Too few vendors. Wanted {MIN_VENDOR_COUNT}, got {vendor_count}.')
220 if device_count < MIN_DEVICE_COUNT:
221 exit_msg(f'Too few devices. Wanted {MIN_DEVICE_COUNT}, got {device_count}.')
223 with open(OUTPUT_FILE, "w", encoding="utf-8") as pci_ids_f:
224 pci_ids_f.write(out_lines)
226 if __name__ == '__main__':
227 main()