pkinit: pass kerberos_is_win2k_pkinit() over actx boundaries
[wireshark-sm.git] / tools / make-pci-ids.py
blob093637d7293d65dc69084923deb79d747d869583
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>
39 #include "pci-ids.h"
41 typedef struct
43 uint16_t vid;
44 uint16_t did;
45 uint16_t svid;
46 uint16_t ssid;
47 const char *name;
49 } pci_id_t;
51 typedef struct
53 uint16_t vid;
54 uint16_t count;
55 pci_id_t const *ids_ptr;
57 } pci_vid_index_t;
59 """
61 CODE_POSTFIX = """
62 static pci_vid_index_t const *get_vid_index(uint16_t vid)
64 uint32_t start_index = 0;
65 uint32_t end_index = 0;
66 uint32_t idx = 0;
68 end_index = sizeof(pci_vid_index)/sizeof(pci_vid_index[0]);
70 while(start_index != end_index)
72 if(end_index - start_index == 1)
74 if(pci_vid_index[start_index].vid == vid)
75 return &pci_vid_index[start_index];
77 break;
80 idx = (start_index + end_index)/2;
82 if(pci_vid_index[idx].vid < vid)
83 start_index = idx;
84 else
85 if(pci_vid_index[idx].vid > vid)
86 end_index = idx;
87 else
88 return &pci_vid_index[idx];
92 return NULL;
96 const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
98 unsigned int i;
99 static const char *not_found = \"Not found\";
100 pci_vid_index_t const *index_ptr;
101 pci_id_t const *ids_ptr;
103 index_ptr = get_vid_index(vid);
105 if(index_ptr == NULL)
106 return not_found;
108 ids_ptr = index_ptr->ids_ptr;
109 for(i = 0; i < index_ptr->count; ids_ptr++, i++)
110 if(vid == ids_ptr->vid &&
111 did == ids_ptr->did &&
112 svid == ids_ptr->svid &&
113 ssid == ids_ptr->ssid)
114 return ids_ptr->name;
115 return not_found;
121 id_list=[]
122 count_list=[]
125 def exit_msg(msg=None, status=1):
126 if msg is not None:
127 sys.stderr.write(msg + '\n')
128 sys.exit(status)
131 def main():
132 req_headers = { 'User-Agent': 'Wireshark make-pci-ids' }
133 req = urllib.request.Request('https://github.com/pciutils/pciids/raw/master/pci.ids', headers=req_headers)
134 response = urllib.request.urlopen(req)
135 lines = response.read().decode('UTF-8', 'replace').splitlines()
137 out_lines = '''\
138 /* pci-ids.c
140 * pci-ids.c is based on the pci.ids of The PCI ID Repository at
141 * https://pci-ids.ucw.cz/, fetched indirectly via
142 * https://github.com/pciutils/pciids
144 vid = -1
145 did = -1
146 svid = -1
147 entries = 0
148 line_num = 0
150 for line in lines:
151 line = line.strip('\n')
152 line_num += 1
154 if line_num <= 15:
155 line = line.replace('#', ' ', 1)
156 line = line.lstrip()
157 line = line.replace("GNU General Public License", "GPL")
158 if line:
159 line = ' * ' + line
160 else:
161 line = ' *' + line
162 out_lines += line + '\n'
163 if line_num == 15:
164 out_lines += CODE_PREFIX
166 line = line.replace("\\","\\\\")
167 line = line.replace("\"","\\\"")
168 line = line.replace("?","?-")
169 tabs = len(line) - len(line.lstrip('\t'))
170 if tabs == 0:
171 #print line
172 words = line.split(" ", 1)
173 if len(words) < 2:
174 continue
175 if len(words[0]) != 4:
176 continue
177 if all(c in string.hexdigits for c in words[0]):
178 hex_int = int(words[0], 16)
179 if vid != -1:
180 out_lines += "}; /* pci_vid_%04X[] */\n\n" % (vid)
181 count_list.append(entries)
182 vid = hex_int
183 entries = 1
184 did = -1
185 svid = -1
186 ssid = -1
187 out_lines += "static pci_id_t const pci_vid_%04X[] = {\n" % (vid)
188 out_lines += "{0x%04X, 0xFFFF, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid, words[1].strip(), vid)
189 id_list.append(vid)
190 continue
192 if tabs == 1:
193 line = line.strip('\t')
194 words = line.split(" ", 1)
195 if len(words) < 2:
196 continue
197 if len(words[0]) != 4:
198 continue
199 if all(c in string.hexdigits for c in words[0]):
200 hex_int = int(words[0], 16)
201 did = hex_int
202 svid = -1
203 ssid = -1
204 out_lines += "{0x%04X, 0x%04X, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid, did, words[1].strip(), did)
205 entries += 1
206 continue
208 if tabs == 2:
209 line = line.strip('\t')
210 words = line.split(" ", 2)
211 if len(words[0]) != 4:
212 continue
213 if all(c in string.hexdigits for c in words[0]):
214 hex_int = int(words[0], 16)
215 svid = hex_int
217 if all(c in string.hexdigits for c in words[1]):
218 hex_int = int(words[1], 16)
219 ssid = hex_int
221 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)
222 entries += 1
223 svid = -1
224 ssid = -1
225 continue
227 out_lines += "}; /* pci_vid_%04X[] */\n" % (vid)
228 count_list.append(entries)
230 out_lines += "\nstatic pci_vid_index_t const pci_vid_index[] = {\n"
232 vendor_count = len(id_list)
233 device_count = 0
234 for i in range(vendor_count):
235 out_lines += "{0x%04X, %d, pci_vid_%04X },\n" % (id_list[i], count_list[i], id_list[i])
236 device_count += count_list[i]
238 out_lines += "}; /* We have %d VIDs */\n" % (vendor_count)
240 out_lines += CODE_POSTFIX
242 if vendor_count < MIN_VENDOR_COUNT:
243 exit_msg(f'Too few vendors. Wanted {MIN_VENDOR_COUNT}, got {vendor_count}.')
245 if device_count < MIN_DEVICE_COUNT:
246 exit_msg(f'Too few devices. Wanted {MIN_DEVICE_COUNT}, got {device_count}.')
248 with open(OUTPUT_FILE, "w", encoding="utf-8") as pci_ids_f:
249 pci_ids_f.write(out_lines)
251 if __name__ == '__main__':
252 main()