wireshark/tools/make-usb.py

98 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#
# $Id$
#
# make-usb - Creates a file containing vendor and product ids.
# It use the databases at
# http://www.linux-usb.org/usb.ids
# to create our file epan/dissectors/usb.c
#
import re
import urllib
MODE_IDLE = 0
MODE_VENDOR_PRODUCT = 1
mode = MODE_IDLE
response = urllib.urlopen('http://www.linux-usb.org/usb.ids')
lines = response.read().splitlines()
vendors="static const value_string usb_vendors_vals[] = {\n"
products="static const value_string usb_products_vals[] = {\n"
for line in lines:
line = line.rstrip()
if line == "# Vendors, devices and interfaces. Please keep sorted.":
mode = MODE_VENDOR_PRODUCT
continue
elif line == "# List of known device classes, subclasses and protocols":
mode = MODE_IDLE
continue
if mode == MODE_VENDOR_PRODUCT:
if re.match("^[0-9a-f]{4}", line):
vendors += " { 0x%s, \"%s\" },\n"%(line[:4], re.sub("\"", "\\\"", re.sub("\?+", "?", repr(line[4:].strip())[1:-1].replace("\\", "\\\\"))))
last_vendor = line[:4]
elif re.match("^\t[0-9a-f]{4}", line):
line = line.strip()
products += " { 0x%s%s, \"%s\" },\n"%(last_vendor, line[:4], re.sub("\"", "\\\"", re.sub("\?+", "?", repr(line[4:].strip())[1:-1].replace("\\", "\\\\"))))
vendors += """ { 0, NULL }\n};
value_string_ext ext_usb_vendors_vals = VALUE_STRING_EXT_INIT(usb_vendors_vals);
"""
products += """ { 0, NULL }\n};
value_string_ext ext_usb_products_vals = VALUE_STRING_EXT_INIT(usb_products_vals);
"""
header="""/* usb.c
* USB vendor id and product ids
* This file was generated by running python ./tools/make-usb.py
* Don't change it directly.
*
* Copyright 2012, Michal Labedzki for Tieto Corporation
*
* $Id$
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <epan/packet.h>
"""
f = open('epan/dissectors/usb.c', 'w')
f.write(header)
f.write("\n")
f.write(vendors)
f.write("\n\n")
f.write(products)
f.write("\n")
f.close()
print("Success!")