VirtualBox

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

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

USBIdDatabaseGenerator: Reduced the string copying during parsing.

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