decrypt
[metze/wireshark/wip.git] / tools / make-usb.py
1 #!/usr/bin/env python
2 #
3 # make-usb - Creates a file containing vendor and product ids.
4 # It use the databases at
5 # http://www.linux-usb.org/usb.ids
6 # to create our file epan/dissectors/usb.c
7 #
8 # It also uses the values culled out of libgphoto2 using usb-ptp-extract-models.pl
9
10 import re
11 import sys
12
13 if sys.version_info[0] < 3:
14     import urllib
15 else:
16     import urllib.request, urllib.error, urllib.parse
17
18 MODE_IDLE           = 0
19 MODE_VENDOR_PRODUCT = 1
20 MIN_VENDORS = 2900 # 2948 as of 2015-06-28
21 MIN_PRODUCTS = 15000 # 15415 as of 2015-06-28
22
23 mode = MODE_IDLE
24
25 # Grab from linux-usb.org
26 if sys.version_info[0] < 3:
27     response = urllib.urlopen('http://www.linux-usb.org/usb.ids')
28 else:
29     response = urllib.request.urlopen('http://www.linux-usb.org/usb.ids')
30 lines = response.read().splitlines()
31
32 vendors  = dict()
33 products = dict()
34 vendors_str="static const value_string usb_vendors_vals[] = {\n"
35 products_str="static const value_string usb_products_vals[] = {\n"
36
37
38 for line in lines:
39     line = line.rstrip()
40
41     if line == "# Vendors, devices and interfaces. Please keep sorted.":
42         mode = MODE_VENDOR_PRODUCT
43         continue
44     elif line == "# List of known device classes, subclasses and protocols":
45         mode = MODE_IDLE
46         continue
47
48     if mode == MODE_VENDOR_PRODUCT:
49         if re.match("^[0-9a-f]{4}", line):
50             last_vendor=line[:4]
51             vendors[last_vendor] = re.sub("\"", "\\\"", re.sub("\?+", "?", repr(line[4:].strip())[1:-1].replace("\\", "\\\\")))
52         elif re.match("^\t[0-9a-f]{4}", line):
53             line = line.strip()
54             product = "%s%s"%(last_vendor, line[:4])
55             products[product] = re.sub("\"", "\\\"", re.sub("\?+", "?", repr(line[4:].strip())[1:-1].replace("\\", "\\\\")))
56
57
58 # Grab from libgphoto (indirectly through tools/usb-ptp-extract-models.pl)
59 u = open('tools/usb-ptp-extract-models.txt','r')
60 for line in u.readlines():
61     fields=line.split()
62     products[fields[0]]= ' '.join(fields[1:])
63
64 if (len(vendors) < MIN_VENDORS):
65     sys.stderr.write("Not enough vendors: %d\n" % len(vendors))
66     sys.exit(1)
67
68 if (len(products) < MIN_PRODUCTS):
69     sys.stderr.write("Not enough products: %d\n" % len(products))
70     sys.exit(1)
71
72 for v in sorted(vendors):
73     vendors_str += "    { 0x%s, \"%s\" },\n"%(v,vendors[v])
74
75 vendors_str += """    { 0, NULL }\n};
76 value_string_ext ext_usb_vendors_vals = VALUE_STRING_EXT_INIT(usb_vendors_vals);
77 """
78
79 for p in sorted(products):
80     products_str += "    { 0x%s, \"%s\" },\n"%(p,products[p])
81
82 products_str += """    { 0, NULL }\n};
83 value_string_ext ext_usb_products_vals = VALUE_STRING_EXT_INIT(usb_products_vals);
84 """
85
86 header="""/* usb.c
87  * USB vendor id and product ids
88  * This file was generated by running python ./tools/make-usb.py
89  * Don't change it directly.
90  *
91  * Copyright 2012, Michal Labedzki for Tieto Corporation
92  *
93  * Other values imported from libghoto2/camlibs/ptp2/library.c, music-players.h
94  *
95  * Copyright (C) 2001-2005 Mariusz Woloszyn <emsi@ipartners.pl>
96  * Copyright (C) 2003-2013 Marcus Meissner <marcus@jet.franken.de>
97  * Copyright (C) 2005 Hubert Figuiere <hfiguiere@teaser.fr>
98  * Copyright (C) 2009 Axel Waggershauser <awagger@web.de>
99  * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
100  * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se>
101  * Copyright (C) 2007 Ted Bullock
102  * Copyright (C) 2012 Sony Mobile Communications AB
103  *
104  * Wireshark - Network traffic analyzer
105  * By Gerald Combs <gerald@wireshark.org>
106  * Copyright 1998 Gerald Combs
107  *
108  * SPDX-License-Identifier: GPL-2.0-or-later
109  */
110
111 /*
112  * XXX We should probably parse a USB ID file at program start instead
113  * of generating this file.
114  */
115
116 #include "config.h"
117 #include <epan/packet.h>
118 """
119
120 f = open('epan/dissectors/usb.c', 'w')
121 f.write(header)
122 f.write("\n")
123 f.write(vendors_str)
124 f.write("\n\n")
125 f.write(products_str)
126 f.write("\n")
127 f.close()
128
129 print("Success!")