VirtualBox

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

Last change on this file since 78395 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.2 KB
Line 
1/* $Id: USBIdDatabaseGenerator.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * USB device vendor and product ID database - generator.
4 */
5
6/*
7 * Copyright (C) 2015-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. 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 <algorithm>
25#include <map>
26#include <string>
27#include <vector>
28
29#include <iprt/err.h>
30#include <iprt/initterm.h>
31#include <iprt/message.h>
32#include <iprt/string.h>
33#include <iprt/stream.h>
34
35#include "../include/USBIdDatabase.h"
36
37
38/*
39 * Include the string table generator.
40 */
41#define BLDPROG_STRTAB_MAX_STRLEN (USB_ID_DATABASE_MAX_STRING - 1)
42#ifdef USB_ID_DATABASE_WITH_COMPRESSION
43# define BLDPROG_STRTAB_WITH_COMPRESSION
44#else
45# undef BLDPROG_STRTAB_WITH_COMPRESSION
46#endif
47#define BLDPROG_STRTAB_WITH_CAMEL_WORDS
48#undef BLDPROG_STRTAB_PURE_ASCII
49#include <iprt/bldprog-strtab-template.cpp.h>
50
51
52
53/*********************************************************************************************************************************
54* Global Variables *
55*********************************************************************************************************************************/
56/** For verbose output. */
57static bool g_fVerbose = false;
58
59
60/*********************************************************************************************************************************
61* Defined Constants And Macros *
62*********************************************************************************************************************************/
63// error codes (complements RTEXITCODE_XXX).
64#define ERROR_OPEN_FILE (12)
65#define ERROR_IN_PARSE_LINE (13)
66#define ERROR_DUPLICATE_ENTRY (14)
67#define ERROR_WRONG_FILE_FORMAT (15)
68#define ERROR_TOO_MANY_PRODUCTS (16)
69
70
71/*********************************************************************************************************************************
72* Structures and Typedefs *
73*********************************************************************************************************************************/
74struct VendorRecord
75{
76 size_t vendorID;
77 size_t iProduct;
78 size_t cProducts;
79 std::string str;
80 BLDPROGSTRING StrRef;
81};
82
83struct ProductRecord
84{
85 size_t key;
86 size_t vendorID;
87 size_t productID;
88 std::string str;
89 BLDPROGSTRING StrRef;
90};
91
92typedef std::vector<ProductRecord> ProductsSet;
93typedef std::vector<VendorRecord> VendorsSet;
94
95
96/*********************************************************************************************************************************
97* Global Variables *
98*********************************************************************************************************************************/
99static ProductsSet g_products;
100static VendorsSet g_vendors;
101
102/** The size of all the raw strings, including terminators. */
103static size_t g_cbRawStrings = 0;
104
105
106
107bool operator < (const ProductRecord& lh, const ProductRecord& rh)
108{
109 return lh.key < rh.key;
110}
111
112bool operator < (const VendorRecord& lh, const VendorRecord& rh)
113{
114 return lh.vendorID < rh.vendorID;
115}
116
117bool operator == (const ProductRecord& lh, const ProductRecord& rh)
118{
119 return lh.key == rh.key;
120}
121
122bool operator == (const VendorRecord& lh, const VendorRecord& rh)
123{
124 return lh.vendorID == rh.vendorID;
125}
126
127
128/*
129 * Input file parsing.
130 */
131static int ParseAlias(char *pszLine, size_t& id, std::string& desc)
132{
133 /* First there's a hexadeciman number. */
134 uint32_t uVal;
135 char *pszNext;
136 int rc = RTStrToUInt32Ex(pszLine, &pszNext, 16, &uVal);
137 if ( rc == VWRN_TRAILING_CHARS
138 || rc == VWRN_TRAILING_SPACES
139 || rc == VINF_SUCCESS)
140 {
141 /* Skip the whipespace following it and at the end of the line. */
142 pszNext = RTStrStripL(pszNext);
143 if (*pszNext != '\0')
144 {
145 rc = RTStrValidateEncoding(pszNext);
146 if (RT_SUCCESS(rc))
147 {
148 size_t cchDesc = strlen(pszNext);
149 if (cchDesc <= USB_ID_DATABASE_MAX_STRING)
150 {
151 id = uVal;
152 desc = pszNext;
153 g_cbRawStrings += cchDesc + 1;
154 return RTEXITCODE_SUCCESS;
155 }
156 RTMsgError("String to long: %zu", cchDesc);
157 }
158 else
159 RTMsgError("Invalid encoding: '%s' (rc=%Rrc)", pszNext, rc);
160 }
161 else
162 RTMsgError("Error parsing '%s'", pszLine);
163 }
164 else
165 RTMsgError("Error converting number at the start of '%s': %Rrc", pszLine, rc);
166 return ERROR_IN_PARSE_LINE;
167}
168
169static int ParseUsbIds(PRTSTREAM pInStrm, const char *pszFile)
170{
171 /*
172 * State data.
173 */
174 VendorRecord vendor = { 0, 0, 0, "" };
175
176 /*
177 * Process the file line-by-line.
178 *
179 * The generic format is that we have top level entries (vendors) starting
180 * in position 0 with sub entries starting after one or more, depending on
181 * the level, tab characters.
182 *
183 * Specifically, the list of vendors and their products will always start
184 * with a vendor line followed by indented products. The first character
185 * on the vendor line is a hex digit (four in total) that makes up the
186 * vendor ID. The product lines equally starts with a 4 digit hex ID value.
187 *
188 * Other lists are assumed to have first lines that doesn't start with any
189 * lower case hex digit.
190 */
191 uint32_t iLine = 0;;
192 for (;;)
193 {
194 char szLine[_4K];
195 int rc = RTStrmGetLine(pInStrm, szLine, sizeof(szLine));
196 if (RT_SUCCESS(rc))
197 {
198 iLine++;
199
200 /* Check for vendor line. */
201 char chType = szLine[0];
202 if ( RT_C_IS_XDIGIT(chType)
203 && RT_C_IS_SPACE(szLine[4])
204 && RT_C_IS_XDIGIT(szLine[1])
205 && RT_C_IS_XDIGIT(szLine[2])
206 && RT_C_IS_XDIGIT(szLine[3]) )
207 {
208 if (ParseAlias(szLine, vendor.vendorID, vendor.str) == 0)
209 g_vendors.push_back(vendor);
210 else
211 return RTMsgErrorExit((RTEXITCODE)ERROR_IN_PARSE_LINE,
212 "%s(%d): Error in parsing vendor line: '%s'", pszFile, iLine, szLine);
213 }
214 /* Check for product line. */
215 else if (szLine[0] == '\t' && vendor.vendorID != 0)
216 {
217 ProductRecord product = { 0, vendor.vendorID, 0, "" };
218 if (ParseAlias(&szLine[1], product.productID, product.str) == 0)
219 {
220 product.key = RT_MAKE_U32(product.productID, product.vendorID);
221 Assert(product.vendorID == vendor.vendorID);
222 g_products.push_back(product);
223 }
224 else
225 return RTMsgErrorExit((RTEXITCODE)ERROR_IN_PARSE_LINE, "Error in parsing product line: '%s'", szLine);
226 }
227 /* If not a blank or comment line, it is some other kind of data.
228 So, make sure the vendor ID is cleared so we don't try process
229 the sub-items of in some other list as products. */
230 else if ( chType != '#'
231 && chType != '\0'
232 && *RTStrStripL(szLine) != '\0')
233 vendor.vendorID = 0;
234 }
235 else if (rc == VERR_EOF)
236 return RTEXITCODE_SUCCESS;
237 else
238 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTStrmGetLine failed: %Rrc", rc);
239 }
240}
241
242static void WriteSourceFile(FILE *pOut, const char *argv0, PBLDPROGSTRTAB pStrTab)
243{
244 fprintf(pOut,
245 "/** @file\n"
246 " * USB device vendor and product ID database - Autogenerated by %s\n"
247 " */\n"
248 "\n"
249 "/*\n"
250 " * Copyright (C) 2015-2016 Oracle Corporation\n"
251 " *\n"
252 " * This file is part of VirtualBox Open Source Edition(OSE), as\n"
253 " * available from http ://www.virtualbox.org. This file is free software;\n"
254 " * you can redistribute it and / or modify it under the terms of the GNU\n"
255 " * General Public License(GPL) as published by the Free Software\n"
256 " * Foundation, in version 2 as it comes in the \"COPYING\" file of the\n"
257 " * VirtualBox OSE distribution.VirtualBox OSE is distributed in the\n"
258 " * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.\n"
259 " */"
260 "\n"
261 "\n"
262 "#include \"USBIdDatabase.h\"\n"
263 "\n",
264 argv0);
265
266 BldProgStrTab_WriteStringTable(pStrTab, pOut, "", "USBIdDatabase::s_", "StrTab");
267
268 fputs("/**\n"
269 " * USB devices aliases array.\n"
270 " * Format: VendorId, ProductId, Vendor Name, Product Name\n"
271 " * The source of the list is http://www.linux-usb.org/usb.ids\n"
272 " */\n"
273 "USBIDDBPROD const USBIdDatabase::s_aProducts[] =\n"
274 "{\n", pOut);
275 for (ProductsSet::iterator itp = g_products.begin(); itp != g_products.end(); ++itp)
276 fprintf(pOut, " { 0x%04x },\n", (unsigned)itp->productID);
277 fputs("};\n"
278 "\n"
279 "\n"
280 "const RTBLDPROGSTRREF USBIdDatabase::s_aProductNames[] =\n"
281 "{\n", pOut);
282 for (ProductsSet::iterator itp = g_products.begin(); itp != g_products.end(); ++itp)
283 fprintf(pOut, "{ 0x%06x, 0x%02x },\n", itp->StrRef.offStrTab, (unsigned)itp->StrRef.cchString);
284 fputs("};\n"
285 "\n"
286 "const size_t USBIdDatabase::s_cProducts = RT_ELEMENTS(USBIdDatabase::s_aProducts);\n"
287 "\n", pOut);
288
289 fputs("USBIDDBVENDOR const USBIdDatabase::s_aVendors[] =\n"
290 "{\n", pOut);
291 for (VendorsSet::iterator itv = g_vendors.begin(); itv != g_vendors.end(); ++itv)
292 fprintf(pOut, " { 0x%04x, 0x%04x, 0x%04x },\n", (unsigned)itv->vendorID, (unsigned)itv->iProduct, (unsigned)itv->cProducts);
293 fputs("};\n"
294 "\n"
295 "\n"
296 "const RTBLDPROGSTRREF USBIdDatabase::s_aVendorNames[] =\n"
297 "{\n", pOut);
298 for (VendorsSet::iterator itv = g_vendors.begin(); itv != g_vendors.end(); ++itv)
299 fprintf(pOut, "{ 0x%06x, 0x%02x },\n", itv->StrRef.offStrTab, (unsigned)itv->StrRef.cchString);
300 fputs("};\n"
301 "\n"
302 "const size_t USBIdDatabase::s_cVendors = RT_ELEMENTS(USBIdDatabase::s_aVendors);\n"
303 "\n", pOut);
304}
305
306static int usage(FILE *pOut, const char *argv0)
307{
308 fprintf(pOut, "Usage: %s [linux.org usb list file] [custom usb list file] [-o output file]\n", argv0);
309 return RTEXITCODE_SYNTAX;
310}
311
312
313int main(int argc, char *argv[])
314{
315 /*
316 * Initialize IPRT and convert argv to UTF-8.
317 */
318 int rc = RTR3InitExe(argc, &argv, 0);
319 if (RT_FAILURE(rc))
320 return RTMsgInitFailure(rc);
321
322 /*
323 * Parse arguments and read input files.
324 */
325 if (argc < 4)
326 {
327 usage(stderr, argv[0]);
328 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Insufficient arguments.");
329 }
330 g_products.reserve(20000);
331 g_vendors.reserve(3500);
332
333 const char *pszOutFile = NULL;
334 for (int i = 1; i < argc; i++)
335 {
336 if (strcmp(argv[i], "-o") == 0)
337 {
338 pszOutFile = argv[++i];
339 continue;
340 }
341 if ( strcmp(argv[i], "-h") == 0
342 || strcmp(argv[i], "-?") == 0
343 || strcmp(argv[i], "--help") == 0)
344 {
345 usage(stdout, argv[0]);
346 return RTEXITCODE_SUCCESS;
347 }
348
349 PRTSTREAM pInStrm;
350 rc = RTStrmOpen(argv[i], "r", &pInStrm);
351 if (RT_FAILURE(rc))
352 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE,
353 "Failed to open file '%s' for reading: %Rrc", argv[i], rc);
354
355 rc = ParseUsbIds(pInStrm, argv[i]);
356 RTStrmClose(pInStrm);
357 if (rc != 0)
358 {
359 RTMsgError("Failed parsing USB devices file '%s'", argv[i]);
360 return rc;
361 }
362 }
363
364 /*
365 * Due to USBIDDBVENDOR::iProduct, there is currently a max of 64KB products.
366 * (Not a problem as we've only have less that 54K products currently.)
367 */
368 if (g_products.size() > _64K)
369 return RTMsgErrorExit((RTEXITCODE)ERROR_TOO_MANY_PRODUCTS,
370 "More than 64K products is not supported: %u products", g_products.size());
371
372 /*
373 * Sort the IDs and fill in the iProduct and cProduct members.
374 */
375 sort(g_products.begin(), g_products.end());
376 sort(g_vendors.begin(), g_vendors.end());
377
378 size_t iProduct = 0;
379 for (size_t iVendor = 0; iVendor < g_vendors.size(); iVendor++)
380 {
381 size_t const idVendor = g_vendors[iVendor].vendorID;
382 g_vendors[iVendor].iProduct = iProduct;
383 if ( iProduct < g_products.size()
384 && g_products[iProduct].vendorID <= idVendor)
385 {
386 if (g_products[iProduct].vendorID == idVendor)
387 do
388 iProduct++;
389 while ( iProduct < g_products.size()
390 && g_products[iProduct].vendorID == idVendor);
391 else
392 return RTMsgErrorExit((RTEXITCODE)ERROR_IN_PARSE_LINE, "product without vendor after sorting. impossible!");
393 }
394 g_vendors[iVendor].cProducts = iProduct - g_vendors[iVendor].iProduct;
395 }
396
397 /*
398 * Verify that all IDs are unique.
399 */
400 ProductsSet::iterator ita = adjacent_find(g_products.begin(), g_products.end());
401 if (ita != g_products.end())
402 return RTMsgErrorExit((RTEXITCODE)ERROR_DUPLICATE_ENTRY, "Duplicate alias detected: idProduct=%#06x", ita->productID);
403
404 /*
405 * Build the string table.
406 * Do string compression and create the string table.
407 */
408 BLDPROGSTRTAB StrTab;
409 if (!BldProgStrTab_Init(&StrTab, g_products.size() + g_vendors.size()))
410 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Out of memory!");
411
412 for (ProductsSet::iterator it = g_products.begin(); it != g_products.end(); ++it)
413 {
414 it->StrRef.pszString = (char *)it->str.c_str();
415 BldProgStrTab_AddString(&StrTab, &it->StrRef);
416 }
417 for (VendorsSet::iterator it = g_vendors.begin(); it != g_vendors.end(); ++it)
418 {
419 it->StrRef.pszString = (char *)it->str.c_str();
420 BldProgStrTab_AddString(&StrTab, &it->StrRef);
421 }
422
423 if (!BldProgStrTab_CompileIt(&StrTab, g_fVerbose))
424 return RTMsgErrorExit(RTEXITCODE_FAILURE, "BldProgStrTab_CompileIt failed!\n");
425
426 /*
427 * Print stats. Making a little extra effort to get it all on one line.
428 */
429 size_t const cbVendorEntry = sizeof(USBIdDatabase::s_aVendors[0]) + sizeof(USBIdDatabase::s_aVendorNames[0]);
430 size_t const cbProductEntry = sizeof(USBIdDatabase::s_aProducts[0]) + sizeof(USBIdDatabase::s_aProductNames[0]);
431
432 size_t cbOldRaw = (g_products.size() + g_vendors.size()) * sizeof(const char *) * 2 + g_cbRawStrings;
433 size_t cbRaw = g_vendors.size() * cbVendorEntry + g_products.size() * cbProductEntry + g_cbRawStrings;
434 size_t cbActual = g_vendors.size() * cbVendorEntry + g_products.size() * cbProductEntry + StrTab.cchStrTab;
435#ifdef USB_ID_DATABASE_WITH_COMPRESSION
436 cbActual += sizeof(StrTab.aCompDict);
437#endif
438
439 char szMsg1[32];
440 RTStrPrintf(szMsg1, sizeof(szMsg1),"Total %zu bytes", cbActual);
441 char szMsg2[64];
442 RTStrPrintf(szMsg2, sizeof(szMsg2)," old version %zu bytes + relocs (%zu%% save)",
443 cbOldRaw, (cbOldRaw - cbActual) * 100 / cbOldRaw);
444 if (cbActual < cbRaw)
445 RTMsgInfo("%s - saving %zu%% (%zu bytes);%s", szMsg1, (cbRaw - cbActual) * 100 / cbRaw, cbRaw - cbActual, szMsg2);
446 else
447 RTMsgInfo("%s - wasting %zu bytes;%s", szMsg1, cbActual - cbRaw, szMsg2);
448
449 /*
450 * Produce the source file.
451 */
452 if (!pszOutFile)
453 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE, "Output file is not specified.");
454
455 FILE *pOut = fopen(pszOutFile, "w");
456 if (!pOut)
457 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE, "Error opening '%s' for writing", pszOutFile);
458
459 WriteSourceFile(pOut, argv[0], &StrTab);
460
461 if (ferror(pOut))
462 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE, "Error writing '%s'!", pszOutFile);
463 if (fclose(pOut) != 0)
464 return RTMsgErrorExit((RTEXITCODE)ERROR_OPEN_FILE, "Error closing '%s'!", pszOutFile);
465
466 return RTEXITCODE_SUCCESS;
467}
468
Note: See TracBrowser for help on using the repository browser.

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