VirtualBox

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

Last change on this file since 98351 was 98292, checked in by vboxsync, 23 months ago

Main/src-server: rc -> hrc/vrc. Enabled scm rc checks. bugref:10223

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