VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/USBIdDatabaseGenerator.cpp@ 59733

Last change on this file since 59733 was 59733, checked in by vboxsync, 9 years ago

USBIdDatabaseGenerator: Share code with oiddb2c.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/* $Id: USBIdDatabaseGenerator.cpp 59733 2016-02-19 01:42:18Z vboxsync $ */
2/** @file
3 * USB device vendor and product ID database - generator.
4 */
5
6/*
7 * Copyright (C) 2015-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <stdio.h>
23
24#include <fstream>
25#include <iostream>
26#include <iomanip>
27#include <algorithm>
28#include <map>
29#include <string>
30#include <vector>
31
32#include <iprt/initterm.h>
33#include <iprt/message.h>
34#include <iprt/string.h>
35#include <iprt/stream.h>
36
37#include "../include/USBIdDatabase.h"
38
39
40/*
41 * Include the string table generator.
42 */
43#define BLDPROG_STRTAB_MAX_STRLEN (USB_ID_DATABASE_MAX_STRING - 1)
44#ifdef USB_ID_DATABASE_WITH_COMPRESSION
45# define BLDPROG_STRTAB_WITH_COMPRESSION
46#else
47# undef BLDPROG_STRTAB_WITH_COMPRESSION
48#endif
49#define BLDPROG_STRTAB_WITH_CAMEL_WORDS
50#undef BLDPROG_STRTAB_PURE_ASCII
51#include <iprt/bldprog-strtab-template.cpp.h>
52
53
54
55/*********************************************************************************************************************************
56* Global Variables *
57*********************************************************************************************************************************/
58/** For verbose output. */
59static bool g_fVerbose = false;
60/** Output prefix for informational output. */
61#define INFO_PREF "USBIdDatabaseGenerator: Info: "
62
63
64using namespace std;
65
66static const char * const header =
67 "/** @file\n"
68 " * USB device vendor and product ID database - Autogenerated from <stupid C++ cannot do %s>\n"
69 " */\n"
70 "\n"
71 "/*\n"
72 " * Copyright (C) 2015-2016 Oracle Corporation\n"
73 " *\n"
74 " * This file is part of VirtualBox Open Source Edition(OSE), as\n"
75 " * available from http ://www.215389.xyz. This file is free software;\n"
76 " * you can redistribute it and / or modify it under the terms of the GNU\n"
77 " * General Public License(GPL) as published by the Free Software\n"
78 " * Foundation, in version 2 as it comes in the \"COPYING\" file of the\n"
79 " * VirtualBox OSE distribution.VirtualBox OSE is distributed in the\n"
80 " * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.\n"
81 " */"
82 "\n"
83 "\n"
84 "#include \"USBIdDatabase.h\"\n"
85 "\n";
86static const char * const product_header =
87 "/**\n"
88 " * USB devices aliases array.\n"
89 " * Format: VendorId, ProductId, Vendor Name, Product Name\n"
90 " * The source of the list is http://www.linux-usb.org/usb.ids\n"
91 " */\n"
92 "USBIDDBPROD const USBIdDatabase::s_aProducts[] =\n"
93 "{\n";
94const char * const product_part2 =
95 "};\n"
96 "\n"
97 "\nconst RTBLDPROGSTRREF USBIdDatabase::s_aProductNames[] =\n"
98 "{\n";
99const char * const product_footer =
100 "};\n"
101 "\n"
102 "const size_t USBIdDatabase::s_cProducts = RT_ELEMENTS(USBIdDatabase::s_aProducts);\n";
103
104const char * const vendor_header =
105 "\nUSBIDDBVENDOR const USBIdDatabase::s_aVendors[] =\n"
106 "{\n";
107const char * const vendor_part2 =
108 "};\n"
109 "\n"
110 "\nconst RTBLDPROGSTRREF USBIdDatabase::s_aVendorNames[] =\n"
111 "{\n";
112const char * const vendor_footer =
113 "};\n"
114 "\n"
115 "const size_t USBIdDatabase::s_cVendors = RT_ELEMENTS(USBIdDatabase::s_aVendors);\n";
116
117const char * const start_block = "# Vendors, devices and interfaces. Please keep sorted.";
118const char * const end_block = "# List of known device classes, subclasses and protocols";
119
120
121/*********************************************************************************************************************************
122* Defined Constants And Macros *
123*********************************************************************************************************************************/
124// error codes (complements RTEXITCODE_XXX).
125#define ERROR_OPEN_FILE (12)
126#define ERROR_IN_PARSE_LINE (13)
127#define ERROR_DUPLICATE_ENTRY (14)
128#define ERROR_WRONG_FILE_FORMAT (15)
129#define ERROR_TOO_MANY_PRODUCTS (16)
130
131
132/*********************************************************************************************************************************
133* Structures and Typedefs *
134*********************************************************************************************************************************/
135struct VendorRecord
136{
137 size_t vendorID;
138 size_t iProduct;
139 size_t cProducts;
140 std::string str;
141 BLDPROGSTRING StrRef;
142};
143
144struct ProductRecord
145{
146 size_t key;
147 size_t vendorID;
148 size_t productID;
149 std::string str;
150 BLDPROGSTRING StrRef;
151};
152
153
154namespace State
155{
156 typedef int Value;
157 enum
158 {
159 lookForStartBlock,
160 lookForEndBlock,
161 finished
162 };
163}
164
165typedef vector<ProductRecord> ProductsSet;
166typedef vector<VendorRecord> VendorsSet;
167
168
169/*********************************************************************************************************************************
170* Global Variables *
171*********************************************************************************************************************************/
172ProductsSet g_products;
173VendorsSet g_vendors;
174
175/** The size of all the raw strings, including terminators. */
176static size_t g_cbRawStrings = 0;
177
178
179
180bool operator < (const ProductRecord& lh, const ProductRecord& rh)
181{
182 return lh.key < rh.key;
183}
184
185bool operator < (const VendorRecord& lh, const VendorRecord& rh)
186{
187 return lh.vendorID < rh.vendorID;
188}
189
190bool operator == (const ProductRecord& lh, const ProductRecord& rh)
191{
192 return lh.key == rh.key;
193}
194
195bool operator == (const VendorRecord& lh, const VendorRecord& rh)
196{
197 return lh.vendorID == rh.vendorID;
198}
199
200
201/*
202 * Input file parsing.
203 */
204int ParseAlias(const string& src, size_t& id, string& desc)
205{
206 unsigned int i = 0;
207 if (sscanf(src.c_str(), "%x", &i) != 1)
208 return ERROR_IN_PARSE_LINE;
209
210 /* skip the number and following whitespace. */
211 size_t offNext = src.find_first_of(" \t", 1);
212 offNext = src.find_first_not_of(" \t", offNext);
213 if (offNext != string::npos)
214 {
215 size_t cchLength = src.length() - offNext;
216 if (cchLength <= USB_ID_DATABASE_MAX_STRING)
217 {
218 id = i;
219 desc = src.substr(offNext);
220
221 /* Check the string encoding. */
222 int rc = RTStrValidateEncoding(desc.c_str());
223 if (RT_SUCCESS(rc))
224 {
225 g_cbRawStrings += desc.length() + 1;
226 return RTEXITCODE_SUCCESS;
227 }
228
229 RTMsgError("Invalid encoding: '%s' (rc=%Rrc)", desc.c_str(), rc);
230 }
231 else
232 RTMsgError("String to long: %zu", cchLength);
233 }
234 else
235 RTMsgError("Error parsing '%s'", src.c_str());
236 return ERROR_IN_PARSE_LINE;
237}
238
239bool IsCommentOrEmptyLine(const string& str)
240{
241 size_t index = str.find_first_not_of(" \t");// skip left spaces
242 return index == string::npos || str[index] == '#';
243}
244
245bool getline(PRTSTREAM instream, string& resString)
246{
247 const size_t szBuf = 4096;
248 char buf[szBuf] = { 0 };
249
250 int rc = RTStrmGetLine(instream, buf, szBuf);
251 if (RT_SUCCESS(rc))
252 {
253 resString = buf;
254 return true;
255 }
256
257 if (rc != VERR_EOF)
258 RTMsgWarning("RTStrmGetLine failed: %Rrc", rc);
259 return false;
260}
261
262int ParseUsbIds(PRTSTREAM instream)
263{
264 State::Value state = State::lookForStartBlock;
265 string line;
266 int res = 0;
267 VendorRecord vendor = { 0, 0, 0, "" };
268
269 while (state != State::finished && getline(instream, line))
270 {
271 switch (state)
272 {
273 case State::lookForStartBlock:
274 {
275 if (line.find(start_block) != string::npos)
276 state = State::lookForEndBlock;
277 break;
278 }
279 case State::lookForEndBlock:
280 {
281 if (line.find(end_block) != string::npos)
282 state = State::finished;
283 else
284 {
285 if (!IsCommentOrEmptyLine(line))
286 {
287 if (line[0] == '\t')
288 {
289 // Parse Product line
290 // first line should be vendor
291 if (vendor.vendorID == 0)
292 return RTMsgErrorExit((RTEXITCODE)ERROR_WRONG_FILE_FORMAT,
293 "Wrong file format. Product before vendor: '%s'", line.c_str());
294 ProductRecord product = { 0, vendor.vendorID, 0, "" };
295 if (ParseAlias(line.substr(1), product.productID, product.str) != 0)
296 return RTMsgErrorExit((RTEXITCODE)ERROR_IN_PARSE_LINE,
297 "Error in parsing product line: '", line.c_str());
298 product.key = RT_MAKE_U32(product.productID, product.vendorID);
299 Assert(product.vendorID == vendor.vendorID);
300 g_products.push_back(product);
301 }
302 else
303 {
304 // Parse vendor line
305 if (ParseAlias(line, vendor.vendorID, vendor.str) != 0)
306 return RTMsgErrorExit((RTEXITCODE)ERROR_IN_PARSE_LINE,
307 "Error in parsing vendor line: '", line.c_str());
308 g_vendors.push_back(vendor);
309 }
310 }
311 }
312 break;
313 }
314 }
315 }
316 if (state == State::lookForStartBlock)
317 return RTMsgErrorExit((RTEXITCODE)ERROR_WRONG_FILE_FORMAT,
318 "wrong format of input file. Start line is not found.");
319 return 0;
320}
321
322
323static int usage(FILE *pOut, const char *argv0)
324{
325 fprintf(pOut, "Usage: %s [linux.org usb list file] [custom usb list file] [-o output file]\n", argv0);
326 return RTEXITCODE_SYNTAX;
327}
328
329
330int main(int argc, char *argv[])
331{
332 /*
333 * Initialize IPRT and convert argv to UTF-8.
334 */
335 int rc = RTR3InitExe(argc, &argv, 0);
336 if (RT_FAILURE(rc))
337 return RTMsgInitFailure(rc);
338
339 /*
340 * Parse arguments and read input files.
341 */
342 if (argc < 4)
343 {
344 usage(stderr, argv[0]);
345 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Insufficient arguments.");
346 }
347 g_products.reserve(20000);
348 g_vendors.reserve(3500);
349
350 const char *pszOutFile = NULL;
351 for (int i = 1; i < argc; i++)
352 {
353 if (strcmp(argv[i], "-o") == 0)
354 {
355 pszOutFile = argv[++i];
356 continue;
357 }
358 if ( strcmp(argv[i], "-h") == 0
359 || strcmp(argv[i], "-?") == 0
360 || strcmp(argv[i], "--help") == 0)
361 {
362 usage(stdout, argv[0]);
363 return RTEXITCODE_SUCCESS;
364 }
365
366 PRTSTREAM pInStrm;
367 rc = RTStrmOpen(argv[i], "r", &pInStrm);
368 if (RT_FAILURE(rc))
369 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE,
370 "Failed to open file '%s' for reading: %Rrc", argv[i], rc);
371
372 rc = ParseUsbIds(pInStrm);
373 RTStrmClose(pInStrm);
374 if (rc != 0)
375 {
376 RTMsgError("Failed parsing USB devices file '%s'", argv[i]);
377 return rc;
378 }
379 }
380
381 /*
382 * Due to USBIDDBVENDOR::iProduct, there is currently a max of 64KB products.
383 * (Not a problem as we've only have less that 54K products currently.)
384 */
385 if (g_products.size() > _64K)
386 return RTMsgErrorExit((RTEXITCODE)ERROR_TOO_MANY_PRODUCTS,
387 "More than 64K products is not supported: %u products", g_products.size());
388
389 /*
390 * Sort the IDs and fill in the iProduct and cProduct members.
391 */
392 sort(g_products.begin(), g_products.end());
393 sort(g_vendors.begin(), g_vendors.end());
394
395 size_t iProduct = 0;
396 for (size_t iVendor = 0; iVendor < g_vendors.size(); iVendor++)
397 {
398 size_t const idVendor = g_vendors[iVendor].vendorID;
399 g_vendors[iVendor].iProduct = iProduct;
400 if ( iProduct < g_products.size()
401 && g_products[iProduct].vendorID <= idVendor)
402 {
403 if (g_products[iProduct].vendorID == idVendor)
404 do
405 iProduct++;
406 while ( iProduct < g_products.size()
407 && g_products[iProduct].vendorID == idVendor);
408 else
409 return RTMsgErrorExit((RTEXITCODE)ERROR_IN_PARSE_LINE, "product without vendor after sorting. impossible!");
410 }
411 g_vendors[iVendor].cProducts = iProduct - g_vendors[iVendor].iProduct;
412 }
413
414 /*
415 * Verify that all IDs are unique.
416 */
417 ProductsSet::iterator ita = adjacent_find(g_products.begin(), g_products.end());
418 if (ita != g_products.end())
419 return RTMsgErrorExit((RTEXITCODE)ERROR_DUPLICATE_ENTRY, "Duplicate alias detected: idProduct=%#06x", ita->productID);
420
421 /*
422 * Build the string table.
423 * Do string compression and create the string table.
424 */
425 BLDPROGSTRTAB StrTab;
426 if (!BldProgStrTab_Init(&StrTab, g_products.size() + g_vendors.size()))
427 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Out of memory!");
428
429 for (ProductsSet::iterator it = g_products.begin(); it != g_products.end(); ++it)
430 {
431 it->StrRef.pszString = (char *)it->str.c_str();
432 BldProgStrTab_AddString(&StrTab, &it->StrRef);
433 }
434 for (VendorsSet::iterator it = g_vendors.begin(); it != g_vendors.end(); ++it)
435 {
436 it->StrRef.pszString = (char *)it->str.c_str();
437 BldProgStrTab_AddString(&StrTab, &it->StrRef);
438 }
439
440 if (!BldProgStrTab_CompileIt(&StrTab, g_fVerbose))
441 return RTMsgErrorExit(RTEXITCODE_FAILURE, "BldProgStrTab_CompileIt failed!\n");
442
443 /*
444 * Print stats.
445 */
446 size_t const cbVendorEntry = sizeof(USBIdDatabase::s_aVendors[0]) + sizeof(USBIdDatabase::s_aVendorNames[0]);
447 size_t const cbProductEntry = sizeof(USBIdDatabase::s_aProducts[0]) + sizeof(USBIdDatabase::s_aProductNames[0]);
448
449 size_t cbOldRaw = (g_products.size() + g_vendors.size()) * sizeof(const char *) * 2 + g_cbRawStrings;
450 size_t cbRaw = g_vendors.size() * cbVendorEntry + g_products.size() * cbProductEntry + g_cbRawStrings;
451 size_t cbActual = g_vendors.size() * cbVendorEntry + g_products.size() * cbProductEntry + StrTab.cchStrTab;
452#ifdef USB_ID_DATABASE_WITH_COMPRESSION
453 cbActual += sizeof(StrTab.aCompDict);
454#endif
455 cout << INFO_PREF "Total " << dec << cbActual << " bytes";
456 if (cbActual < cbRaw)
457 cout << " saving " << dec << ((cbRaw - cbActual) * 100 / cbRaw) << "% (" << (cbRaw - cbActual) << " bytes)";
458 else
459 cout << " wasting " << dec << (cbActual - cbRaw) << " bytes";
460 cout << "; old version " << cbOldRaw << " bytes + relocs ("
461 << ((cbOldRaw - cbActual) * 100 / cbOldRaw) << "% save)." << endl;
462
463
464 /*
465 * Produce the source file.
466 */
467 if (!pszOutFile)
468 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE, "Output file is not specified.");
469
470 FILE *pOut = fopen(pszOutFile, "w");
471 if (!pOut)
472 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE, "Error opening '%s' for writing", pszOutFile);
473
474 fputs(header, pOut);
475 BldProgStrTab_WriteStringTable(&StrTab, pOut, "", "USBIdDatabase::s_", "StrTab");
476
477 fputs(product_header, pOut);
478 for (ProductsSet::iterator itp = g_products.begin(); itp != g_products.end(); ++itp)
479 fprintf(pOut, " { 0x%04x },\n", itp->productID);
480
481 fputs(product_part2, pOut);
482 for (ProductsSet::iterator itp = g_products.begin(); itp != g_products.end(); ++itp)
483 fprintf(pOut, "{ 0x%06x, 0x%02x },\n", itp->StrRef.offStrTab, itp->StrRef.cchString);
484 fputs(product_footer, pOut);
485
486 fputs(vendor_header, pOut);
487 for (VendorsSet::iterator itv = g_vendors.begin(); itv != g_vendors.end(); ++itv)
488 fprintf(pOut, " { 0x%04x, 0x%04x, 0x%04x },\n", itv->vendorID, itv->iProduct, itv->cProducts);
489 fputs(vendor_part2, pOut);
490 for (VendorsSet::iterator itv = g_vendors.begin(); itv != g_vendors.end(); ++itv)
491 fprintf(pOut, "{ 0x%06x, 0x%02x },\n", itv->StrRef.offStrTab, itv->StrRef.cchString);
492 fputs(vendor_footer, pOut);
493
494 if (ferror(pOut))
495 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE, "Error writing '%s'!", pszOutFile);
496 if (fclose(pOut) != 0)
497 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE, "Error closing '%s'!", pszOutFile);
498
499 return RTEXITCODE_SUCCESS;
500}
501
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette