VirtualBox

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

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

USBIdDatabaseGenerator: Inline the generated source code strings - easier to read in my opinion.

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