VirtualBox

source: vbox/trunk/src/VBox/Main/include/USBIdDatabase.h@ 76553

Last change on this file since 76553 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: 6.3 KB
Line 
1/* $Id: USBIdDatabase.h 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * USB device vendor and product ID database.
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#ifndef ___USBIdDatabase_h
19#define ___USBIdDatabase_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#include <iprt/assert.h>
25#include <iprt/stdint.h>
26#include <iprt/cpp/ministring.h>
27#include <iprt/bldprog-strtab.h>
28
29
30/** Saves a few bytes (~25%) on strings. */
31#define USB_ID_DATABASE_WITH_COMPRESSION
32
33/** Max string length. */
34#define USB_ID_DATABASE_MAX_STRING _1K
35
36
37AssertCompileSize(RTBLDPROGSTRREF, sizeof(uint32_t));
38
39
40/**
41 * Elements of product table.
42 */
43typedef struct USBIDDBPROD
44{
45 /** Product ID. */
46 uint16_t idProduct;
47} USBIDDBPROD;
48AssertCompileSize(USBIDDBPROD, sizeof(uint16_t));
49
50
51/**
52 * Element of vendor table.
53 */
54typedef struct USBIDDBVENDOR
55{
56 /** Vendor ID. */
57 uint16_t idVendor;
58 /** Index of the first product. */
59 uint16_t iProduct;
60 /** Number of products. */
61 uint16_t cProducts;
62} USBIDDBVENDOR;
63AssertCompileSize(USBIDDBVENDOR, sizeof(uint16_t) * 3);
64
65
66/**
67 * Wrapper for static array of Aliases.
68 */
69class USBIdDatabase
70{
71public: // For assertions and statis in the generator.
72 /** The compressed string table. */
73 static RTBLDPROGSTRTAB const s_StrTab;
74
75 /** Number of vendors in the two parallel arrays. */
76 static const size_t s_cVendors;
77 /** Vendor IDs lookup table. */
78 static const USBIDDBVENDOR s_aVendors[];
79 /** Vendor names table running parallel to s_aVendors. */
80 static const RTBLDPROGSTRREF s_aVendorNames[];
81
82 /** Number of products in the two parallel arrays. */
83 static const size_t s_cProducts;
84 /** Vendor+Product keys for lookup purposes. */
85 static const USBIDDBPROD s_aProducts[];
86 /** Product names table running parallel to s_aProducts. */
87 static const RTBLDPROGSTRREF s_aProductNames[];
88
89public:
90 static RTCString returnString(PCRTBLDPROGSTRREF pStr)
91 {
92 char szTmp[USB_ID_DATABASE_MAX_STRING * 2];
93 ssize_t cchTmp = RTBldProgStrTabQueryString(&s_StrTab, pStr->off, pStr->cch, szTmp, sizeof(szTmp));
94 return RTCString(szTmp, RT_MAX(cchTmp, 0));
95 }
96
97private:
98 /**
99 * Performs a binary lookup of @a idVendor.
100 *
101 * @returns The index in the vendor tables, UINT32_MAX if not found.
102 * @param idVendor The vendor ID.
103 */
104 static uint32_t lookupVendor(uint16_t idVendor)
105 {
106 size_t iEnd = s_cVendors;
107 if (iEnd)
108 {
109 size_t iStart = 0;
110 for (;;)
111 {
112 size_t idx = iStart + (iEnd - iStart) / 2;
113 if (s_aVendors[idx].idVendor < idVendor)
114 {
115 idx++;
116 if (idx < iEnd)
117 iStart = idx;
118 else
119 break;
120 }
121 else if (s_aVendors[idx].idVendor > idVendor)
122 {
123 if (idx != iStart)
124 iEnd = idx;
125 else
126 break;
127 }
128 else
129 return (uint32_t)idx;
130 }
131 }
132 return UINT32_MAX;
133 }
134
135 /**
136 * Performs a binary lookup of @a idProduct.
137 *
138 * @returns The index in the product tables, UINT32_MAX if not found.
139 * @param idProduct The product ID.
140 * @param iStart The index of the first entry for the vendor.
141 * @param iEnd The index of after the last entry.
142 */
143 static uint32_t lookupProduct(uint16_t idProduct, size_t iStart, size_t iEnd)
144 {
145 if (iStart < iEnd)
146 {
147 for (;;)
148 {
149 size_t idx = iStart + (iEnd - iStart) / 2;
150 if (s_aProducts[idx].idProduct < idProduct)
151 {
152 idx++;
153 if (idx < iEnd)
154 iStart = idx;
155 else
156 break;
157 }
158 else if (s_aProducts[idx].idProduct > idProduct)
159 {
160 if (idx != iStart)
161 iEnd = idx;
162 else
163 break;
164 }
165 else
166 return (uint32_t)idx;
167 }
168 }
169 return UINT32_MAX;
170 }
171
172
173public:
174 static RTCString findProduct(uint16_t idVendor, uint16_t idProduct)
175 {
176 uint32_t idxVendor = lookupVendor(idVendor);
177 if (idxVendor != UINT32_MAX)
178 {
179 uint32_t idxProduct = lookupProduct(idProduct, s_aVendors[idxVendor].iProduct,
180 s_aVendors[idxVendor].iProduct + s_aVendors[idxVendor].cProducts);
181 if (idxProduct != UINT32_MAX)
182 return returnString(&s_aProductNames[idxProduct]);
183 }
184 return RTCString();
185 }
186
187 static RTCString findVendor(uint16_t idVendor)
188 {
189 uint32_t idxVendor = lookupVendor(idVendor);
190 if (idxVendor != UINT32_MAX)
191 return returnString(&s_aVendorNames[idxVendor]);
192 return RTCString();
193 }
194
195 static RTCString findVendorAndProduct(uint16_t idVendor, uint16_t idProduct, RTCString *pstrProduct)
196 {
197 uint32_t idxVendor = lookupVendor(idVendor);
198 if (idxVendor != UINT32_MAX)
199 {
200 uint32_t idxProduct = lookupProduct(idProduct, s_aVendors[idxVendor].iProduct,
201 s_aVendors[idxVendor].iProduct + s_aVendors[idxVendor].cProducts);
202 if (idxProduct != UINT32_MAX)
203 *pstrProduct = returnString(&s_aProductNames[idxProduct]);
204 else
205 pstrProduct->setNull();
206 return returnString(&s_aVendorNames[idxVendor]);
207 }
208 pstrProduct->setNull();
209 return RTCString();
210 }
211
212};
213
214
215#endif
216
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