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