1
/* $Id: USBFilter.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
3
* VirtualBox USB filter abstraction.
7
* Copyright (C) 2007 Oracle Corporation
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.
19
/*******************************************************************************
21
*******************************************************************************/
22
#include <VBox/usbfilter.h>
25
#include <iprt/string.h>
26
#include <iprt/assert.h>
27
#include <iprt/ctype.h>
30
/** @todo split this up for the sake of device drivers and such. */
34
* Initializes an USBFILTER structure.
36
* @param pFilter The filter to initialize.
37
* @param enmType The filter type. If not valid, the filter will not
38
* be properly initialized and all other calls will fail.
40
USBLIB_DECL(void) USBFilterInit(PUSBFILTER pFilter, USBFILTERTYPE enmType)
42
memset(pFilter, 0, sizeof(*pFilter));
43
AssertReturnVoid(enmType > USBFILTERTYPE_INVALID && enmType < USBFILTERTYPE_END);
44
pFilter->u32Magic = USBFILTER_MAGIC;
45
pFilter->enmType = enmType;
46
for (unsigned i = 0; i < RT_ELEMENTS(pFilter->aFields); i++)
47
pFilter->aFields[i].enmMatch = USBFILTERMATCH_IGNORE;
52
* Make a clone of the specified filter.
54
* @param pFilter The target filter.
55
* @param pToClone The source filter.
57
USBLIB_DECL(void) USBFilterClone(PUSBFILTER pFilter, PCUSBFILTER pToClone)
59
memcpy(pFilter, pToClone, sizeof(*pToClone));
64
* Deletes (invalidates) an USBFILTER structure.
66
* @param pFilter The filter to delete.
68
USBLIB_DECL(void) USBFilterDelete(PUSBFILTER pFilter)
70
pFilter->u32Magic = ~USBFILTER_MAGIC;
71
pFilter->enmType = USBFILTERTYPE_INVALID;
72
pFilter->offCurEnd = 0xfffff;
79
* @returns Next non-blank char in the string.
80
* @param psz The string.
82
DECLINLINE(const char *) usbfilterSkipBlanks(const char *psz)
84
while (RT_C_IS_BLANK(*psz))
91
* Worker for usbfilterReadNumber that parses a hexadecimal number.
93
* @returns Same as usbfilterReadNumber, except for VERR_NO_DIGITS.
94
* @param pszExpr Where to start converting, first char is a valid digit.
95
* @param ppszExpr See usbfilterReadNumber.
96
* @param pu16Val See usbfilterReadNumber.
98
static int usbfilterReadNumberHex(const char *pszExpr, const char **ppszExpr, uint16_t *pu16Val)
100
int rc = VINF_SUCCESS;
104
unsigned uDigit = *pszExpr >= 'a' && *pszExpr <= 'f'
105
? *pszExpr - 'a' + 10
106
: *pszExpr >= 'A' && *pszExpr <= 'F'
107
? *pszExpr - 'A' + 10
113
if (u32 > UINT16_MAX)
114
rc = VWRN_NUMBER_TOO_BIG;
115
} while (*++pszExpr);
117
*ppszExpr = usbfilterSkipBlanks(pszExpr);
118
*pu16Val = rc == VINF_SUCCESS ? u32 : UINT16_MAX;
124
* Worker for usbfilterReadNumber that parses a decimal number.
126
* @returns Same as usbfilterReadNumber, except for VERR_NO_DIGITS.
127
* @param pszExpr Where to start converting, first char is a valid digit.
128
* @param uBase The base - 8 or 16.
129
* @param ppszExpr See usbfilterReadNumber.
130
* @param pu16Val See usbfilterReadNumber.
132
static int usbfilterReadNumberDecimal(const char *pszExpr, unsigned uBase, const char **ppszExpr, uint16_t *pu16Val)
134
int rc = VINF_SUCCESS;
138
unsigned uDigit = *pszExpr - '0';
143
if (u32 > UINT16_MAX)
144
rc = VWRN_NUMBER_TOO_BIG;
145
} while (*++pszExpr);
147
*ppszExpr = usbfilterSkipBlanks(pszExpr);
148
*pu16Val = rc == VINF_SUCCESS ? u32 : UINT16_MAX;
154
* Reads a number from a numeric expression.
156
* @returns IPRT status code.
157
* @retval VINF_SUCCESS if all is fine. *ppszExpr and *pu16Val are updated.
158
* @retval VWRN_NUMBER_TOO_BIG if the number exceeds unsigned 16-bit, both *ppszExpr and *pu16Val are updated.
159
* @retval VERR_NO_DIGITS if there aren't any digits.
161
* @param ppszExpr Pointer to the current expression pointer.
162
* This is advanced past the expression and trailing blanks on success.
163
* @param pu16Val Where to store the value on success.
165
static int usbfilterReadNumber(const char **ppszExpr, uint16_t *pu16Val)
167
const char *pszExpr = usbfilterSkipBlanks(*ppszExpr);
168
if (!RT_C_IS_DIGIT(*pszExpr))
169
return VERR_NO_DIGITS;
173
if (pszExpr[1] == 'x' || pszExpr[1] == 'X')
175
if (!RT_C_IS_XDIGIT(pszExpr[2]))
176
return VERR_NO_DIGITS;
177
return usbfilterReadNumberHex(pszExpr + 2, ppszExpr, pu16Val);
179
if (RT_C_IS_ODIGIT(pszExpr[1]))
180
return usbfilterReadNumberDecimal(pszExpr + 1, 8, ppszExpr, pu16Val);
182
if (RT_C_IS_DIGIT(pszExpr[1]))
183
return VERR_NO_DIGITS;
185
return usbfilterReadNumberDecimal(pszExpr, 10, ppszExpr, pu16Val);
190
* Validates a numeric expression.
192
* @returns VBox status code.
193
* @retval VINF_SUCCESS if valid.
194
* @retval VERR_INVALID_PARAMETER if invalid.
195
* @retval VERR_NO_DIGITS if some expression is short of digits.
197
* @param pszExpr The numeric expression.
199
static int usbfilterValidateNumExpression(const char *pszExpr)
202
* An empty expression is fine.
208
* The string format is: "(<m>|([<m>]-[<n>]))|(<m>|([<m>]-[<n>]))+"
209
* where <m> and <n> are numbers in the decimal, hex (0xNNN) or octal (0NNN)
210
* form. Spaces are allowed around <m> and <n>.
212
unsigned cSubExpressions = 0;
216
* Skip remnants of the previous expression and any empty expressions.
217
* ('|' is the expression separator.)
219
while (*pszExpr == '|' || RT_C_IS_BLANK(*pszExpr))
225
* Parse the expression.
228
uint16_t u16First = 0;
229
uint16_t u16Last = 0;
234
rc = usbfilterReadNumber(&pszExpr, &u16Last);
239
rc = usbfilterReadNumber(&pszExpr, &u16First);
246
rc = usbfilterReadNumber(&pszExpr, &u16Last);
259
* We should either be at the end of the string or at
260
* an expression separator (|).
262
if (*pszExpr && *pszExpr != '|' )
263
return VERR_INVALID_PARAMETER;
268
return cSubExpressions ? VINF_SUCCESS : VERR_INVALID_PARAMETER;
273
* Validates a string pattern.
275
* @returns VBox status code.
276
* @retval VINF_SUCCESS if valid.
277
* @retval VERR_INVALID_PARAMETER if invalid.
279
* @param psz The string pattern.
281
static int usbfilterValidateStringPattern(const char *psz)
284
* This is only becomes important if we start doing
285
* sets ([0-9]) and such like.
292
* Thoroughly validates the USB Filter.
294
* @returns Appropriate VBox status code.
295
* @param pFilter The filter to validate.
297
USBLIB_DECL(int) USBFilterValidate(PCUSBFILTER pFilter)
299
if (!VALID_PTR(pFilter))
300
return VERR_INVALID_POINTER;
302
if (pFilter->u32Magic != USBFILTER_MAGIC)
303
return VERR_INVALID_MAGIC;
305
if ( pFilter->enmType <= USBFILTERTYPE_INVALID
306
|| pFilter->enmType >= USBFILTERTYPE_END)
308
Log(("USBFilter: %p - enmType=%d!\n", pFilter, pFilter->enmType));
309
return VERR_INVALID_PARAMETER;
312
if (pFilter->offCurEnd >= sizeof(pFilter->achStrTab))
314
Log(("USBFilter: %p - offCurEnd=%#x!\n", pFilter, pFilter->offCurEnd));
315
return VERR_INVALID_PARAMETER;
319
* Validate the string table.
321
if (pFilter->achStrTab[0])
323
Log(("USBFilter: %p - bad null string\n", pFilter));
324
return VERR_INVALID_PARAMETER;
327
const char *psz = &pFilter->achStrTab[1];
328
while (psz < &pFilter->achStrTab[pFilter->offCurEnd])
330
const char *pszEnd = RTStrEnd(psz, &pFilter->achStrTab[sizeof(pFilter->achStrTab)] - psz);
333
Log(("USBFilter: %p - string at %#x isn't terminated!\n",
334
pFilter, psz - &pFilter->achStrTab[0]));
335
return VERR_INVALID_PARAMETER;
338
uint16_t off = (uint16_t)(uintptr_t)(psz - &pFilter->achStrTab[0]);
340
for (i = 0; i < RT_ELEMENTS(pFilter->aFields); i++)
341
if ( USBFilterIsMethodString((USBFILTERMATCH)pFilter->aFields[i].enmMatch)
342
&& pFilter->aFields[i].u16Value == off)
344
if (i >= RT_ELEMENTS(pFilter->aFields))
346
Log(("USBFilter: %p - string at %#x isn't used by anyone! (%s)\n",
347
pFilter, psz - &pFilter->achStrTab[0], psz));
348
return VERR_INVALID_PARAMETER;
354
if ((uintptr_t)(psz - &pFilter->achStrTab[0] - 1) != pFilter->offCurEnd)
356
Log(("USBFilter: %p - offCurEnd=%#x currently at %#x\n",
357
pFilter, pFilter->offCurEnd, psz - &pFilter->achStrTab[0] - 1));
358
return VERR_INVALID_PARAMETER;
361
while (psz < &pFilter->achStrTab[sizeof(pFilter->achStrTab)])
365
Log(("USBFilter: %p - str tab isn't zero padded! %#x: %c\n",
366
pFilter, psz - &pFilter->achStrTab[0], *psz));
367
return VERR_INVALID_PARAMETER;
374
* Validate the fields.
377
for (unsigned i = 0; i < RT_ELEMENTS(pFilter->aFields); i++)
379
switch (pFilter->aFields[i].enmMatch)
381
case USBFILTERMATCH_IGNORE:
382
case USBFILTERMATCH_PRESENT:
383
if (pFilter->aFields[i].u16Value)
385
Log(("USBFilter: %p - #%d/%d u16Value=%d expected 0!\n",
386
pFilter, i, pFilter->aFields[i].enmMatch, pFilter->aFields[i].u16Value));
387
return VERR_INVALID_PARAMETER;
391
case USBFILTERMATCH_NUM_EXACT:
392
case USBFILTERMATCH_NUM_EXACT_NP:
393
if (!USBFilterIsNumericField((USBFILTERIDX)i))
395
Log(("USBFilter: %p - #%d / %d - not numeric field\n",
396
pFilter, i, pFilter->aFields[i].enmMatch));
397
return VERR_INVALID_PARAMETER;
401
case USBFILTERMATCH_NUM_EXPRESSION:
402
case USBFILTERMATCH_NUM_EXPRESSION_NP:
403
if (!USBFilterIsNumericField((USBFILTERIDX)i))
405
Log(("USBFilter: %p - #%d / %d - not numeric field\n",
406
pFilter, i, pFilter->aFields[i].enmMatch));
407
return VERR_INVALID_PARAMETER;
409
if ( pFilter->aFields[i].u16Value >= pFilter->offCurEnd
410
&& pFilter->offCurEnd)
412
Log(("USBFilter: %p - #%d / %d - off=%#x max=%#x\n",
413
pFilter, i, pFilter->aFields[i].enmMatch, pFilter->aFields[i].u16Value, pFilter->offCurEnd));
414
return VERR_INVALID_PARAMETER;
416
psz = &pFilter->achStrTab[pFilter->aFields[i].u16Value];
417
rc = usbfilterValidateNumExpression(psz);
420
Log(("USBFilter: %p - #%d / %d - bad num expr: %s (rc=%Rrc)\n",
421
pFilter, i, pFilter->aFields[i].enmMatch, psz, rc));
426
case USBFILTERMATCH_STR_EXACT:
427
case USBFILTERMATCH_STR_EXACT_NP:
428
if (!USBFilterIsStringField((USBFILTERIDX)i))
430
Log(("USBFilter: %p - #%d / %d - not string field\n",
431
pFilter, i, pFilter->aFields[i].enmMatch));
432
return VERR_INVALID_PARAMETER;
434
if ( pFilter->aFields[i].u16Value >= pFilter->offCurEnd
435
&& pFilter->offCurEnd)
437
Log(("USBFilter: %p - #%d / %d - off=%#x max=%#x\n",
438
pFilter, i, pFilter->aFields[i].enmMatch, pFilter->aFields[i].u16Value, pFilter->offCurEnd));
439
return VERR_INVALID_PARAMETER;
443
case USBFILTERMATCH_STR_PATTERN:
444
case USBFILTERMATCH_STR_PATTERN_NP:
445
if (!USBFilterIsStringField((USBFILTERIDX)i))
447
Log(("USBFilter: %p - #%d / %d - not string field\n",
448
pFilter, i, pFilter->aFields[i].enmMatch));
449
return VERR_INVALID_PARAMETER;
451
if ( pFilter->aFields[i].u16Value >= pFilter->offCurEnd
452
&& pFilter->offCurEnd)
454
Log(("USBFilter: %p - #%d / %d - off=%#x max=%#x\n",
455
pFilter, i, pFilter->aFields[i].enmMatch, pFilter->aFields[i].u16Value, pFilter->offCurEnd));
456
return VERR_INVALID_PARAMETER;
458
psz = &pFilter->achStrTab[pFilter->aFields[i].u16Value];
459
rc = usbfilterValidateStringPattern(psz);
462
Log(("USBFilter: %p - #%d / %d - bad string pattern: %s (rc=%Rrc)\n",
463
pFilter, i, pFilter->aFields[i].enmMatch, psz, rc));
469
Log(("USBFilter: %p - #%d enmMatch=%d!\n", pFilter, i, pFilter->aFields[i].enmMatch));
470
return VERR_INVALID_PARAMETER;
479
* Find the specified field in the string table.
481
* @returns Pointer to the string in the string table on success.
482
* NULL if the field is invalid or it doesn't have a string value.
483
* @param pFilter The filter.
484
* @param enmFieldIdx The field index.
486
DECLINLINE(const char *) usbfilterGetString(PCUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
488
if ((unsigned)enmFieldIdx < (unsigned)USBFILTERIDX_END)
490
switch (pFilter->aFields[enmFieldIdx].enmMatch)
492
case USBFILTERMATCH_NUM_EXPRESSION:
493
case USBFILTERMATCH_NUM_EXPRESSION_NP:
494
case USBFILTERMATCH_STR_EXACT:
495
case USBFILTERMATCH_STR_EXACT_NP:
496
case USBFILTERMATCH_STR_PATTERN:
497
case USBFILTERMATCH_STR_PATTERN_NP:
498
Assert(pFilter->aFields[enmFieldIdx].u16Value < sizeof(pFilter->achStrTab));
499
return &pFilter->achStrTab[pFilter->aFields[enmFieldIdx].u16Value];
502
AssertMsgFailed(("%d\n", pFilter->aFields[enmFieldIdx].enmMatch));
503
case USBFILTERMATCH_IGNORE:
504
case USBFILTERMATCH_PRESENT:
505
case USBFILTERMATCH_NUM_EXACT:
506
case USBFILTERMATCH_NUM_EXACT_NP:
515
* Gets a number value of a field.
517
* The field must contain a numeric value.
519
* @returns The field value on success, -1 on failure (invalid input / not numeric).
520
* @param pFilter The filter.
521
* @param enmFieldIdx The field index.
523
DECLINLINE(int) usbfilterGetNum(PCUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
525
if ((unsigned)enmFieldIdx < (unsigned)USBFILTERIDX_END)
527
switch (pFilter->aFields[enmFieldIdx].enmMatch)
529
case USBFILTERMATCH_NUM_EXACT:
530
case USBFILTERMATCH_NUM_EXACT_NP:
531
return pFilter->aFields[enmFieldIdx].u16Value;
534
AssertMsgFailed(("%d\n", pFilter->aFields[enmFieldIdx].enmMatch));
535
case USBFILTERMATCH_IGNORE:
536
case USBFILTERMATCH_PRESENT:
537
case USBFILTERMATCH_NUM_EXPRESSION:
538
case USBFILTERMATCH_NUM_EXPRESSION_NP:
539
case USBFILTERMATCH_STR_EXACT:
540
case USBFILTERMATCH_STR_EXACT_NP:
541
case USBFILTERMATCH_STR_PATTERN:
542
case USBFILTERMATCH_STR_PATTERN_NP:
551
* Performs simple pattern matching.
553
* @returns true on match and false on mismatch.
554
* @param pszExpr The numeric expression.
555
* @param u16Value The value to match.
557
static bool usbfilterMatchNumExpression(const char *pszExpr, uint16_t u16Value)
560
* The string format is: "(<m>|([<m>]-[<n>]))|(<m>|([<m>]-[<n>]))+"
561
* where <m> and <n> are numbers in the decimal, hex (0xNNN) or octal (0NNN)
562
* form. Spaces are allowed around <m> and <n>.
567
* Skip remnants of the previous expression and any empty expressions.
568
* ('|' is the expression separator.)
570
while (*pszExpr == '|' || RT_C_IS_BLANK(*pszExpr))
576
* Parse the expression.
579
uint16_t u16First = 0;
580
uint16_t u16Last = 0;
585
rc = usbfilterReadNumber(&pszExpr, &u16Last);
590
rc = usbfilterReadNumber(&pszExpr, &u16First);
593
pszExpr = usbfilterSkipBlanks(pszExpr);
598
rc = usbfilterReadNumber(&pszExpr, &u16Last);
608
/* On success, we should either be at the end of the string or
609
at an expression separator (|). */
610
if (RT_SUCCESS(rc) && *pszExpr && *pszExpr != '|' )
611
rc = VERR_INVALID_PARAMETER;
615
* Swap the values if the order is mixed up.
617
if (u16First > u16Last)
619
uint16_t u16Tmp = u16First;
625
* Perform the compare.
627
if ( u16Value >= u16First
628
&& u16Value <= u16Last)
634
* Skip the bad expression.
635
* ('|' is the expression separator.)
637
while (*pszExpr && *pszExpr != '|')
647
* Performs simple pattern matching.
649
* @returns true on match and false on mismatch.
650
* @param pszPattern The pattern to match against.
651
* @param psz The string to match.
653
static bool usbfilterMatchStringPattern(const char *pszPattern, const char *psz)
656
while ((ch = *pszPattern++))
661
* Matches one char or end of string.
669
* Matches zero or more characters.
671
/* skip subsequent wildcards */
672
while ( (ch = *pszPattern) == '*'
676
/* Pattern ends with a '*' and thus matches the rest of psz. */
679
/* Find the length of the following exact pattern sequence. */
680
ssize_t cchMatch = 1;
681
while ( (ch = pszPattern[cchMatch]) != '\0'
686
/* Check if the exact pattern sequence is too long. */
687
ssize_t cch = strlen(psz);
692
/* Is the rest an exact match? */
694
return memcmp(psz + cch, pszPattern, cchMatch) == 0;
697
* This is where things normally starts to get recursive or ugly.
699
* Just to make life simple, we'll skip the nasty stuff and say
700
* that we will do a maximal wildcard match and forget about any
701
* alternative matches.
703
* If somebody is bored out of their mind one day, feel free to
704
* implement correct matching without using recursion.
707
const char *pszMatch = NULL;
712
&& !strncmp(psz, pszPattern, cchMatch))
720
psz = pszMatch + cchMatch;
721
pszPattern += cchMatch;
737
* Match a filter against a device.
739
* @returns true if they match, false if not.
741
* @param pFilter The filter to match with.
742
* @param pDevice The device data. This is a filter (type ignored) that
743
* contains 'exact' values for all present fields and 'ignore'
744
* values for the non-present fields.
746
* @remark Both the filter and the device are ASSUMED to be valid because
747
* we don't wish to waste any time in this function.
749
USBLIB_DECL(bool) USBFilterMatch(PCUSBFILTER pFilter, PCUSBFILTER pDevice)
751
return USBFilterMatchRated(pFilter, pDevice) > 0;
755
#if 0 /*def IN_RING0*/ /** @todo convert to proper logging. */
756
extern "C" int printf(const char *format, ...);
757
# define dprintf(a) printf a
759
# define dprintf(a) do {} while (0)
763
* Match a filter against a device and rate the result.
765
* @returns -1 if no match, matching rate between 1 and 100 (inclusive) if matched.
767
* @param pFilter The filter to match with.
768
* @param pDevice The device data. This is a filter (type ignored) that
769
* contains 'exact' values for all present fields and 'ignore'
770
* values for the non-present fields.
772
* @remark Both the filter and the device are ASSUMED to be valid because
773
* we don't wish to waste any time in this function.
775
USBLIB_DECL(int) USBFilterMatchRated(PCUSBFILTER pFilter, PCUSBFILTER pDevice)
778
dprintf(("USBFilterMatchRated: %p %p\n", pFilter, pDevice));
780
for (unsigned i = 0; i < RT_ELEMENTS(pFilter->aFields); i++)
782
switch (pFilter->aFields[i].enmMatch)
784
case USBFILTERMATCH_IGNORE:
788
case USBFILTERMATCH_PRESENT:
789
if (pDevice->aFields[i].enmMatch == USBFILTERMATCH_IGNORE)
791
dprintf(("filter match[%d]: !present\n", i));
797
case USBFILTERMATCH_NUM_EXACT:
798
if ( pDevice->aFields[i].enmMatch == USBFILTERMATCH_IGNORE
799
|| pFilter->aFields[i].u16Value != pDevice->aFields[i].u16Value)
801
if (pDevice->aFields[i].enmMatch == USBFILTERMATCH_IGNORE)
802
dprintf(("filter match[%d]: !num_exact device=ignore\n", i));
804
dprintf(("filter match[%d]: !num_exact %#x (filter) != %#x (device)\n", i, pFilter->aFields[i].u16Value, pDevice->aFields[i].u16Value));
810
case USBFILTERMATCH_NUM_EXACT_NP:
811
if ( pDevice->aFields[i].enmMatch != USBFILTERMATCH_IGNORE
812
&& pFilter->aFields[i].u16Value != pDevice->aFields[i].u16Value)
814
dprintf(("filter match[%d]: !num_exact_np %#x (filter) != %#x (device)\n", i, pFilter->aFields[i].u16Value, pDevice->aFields[i].u16Value));
820
case USBFILTERMATCH_NUM_EXPRESSION:
821
if ( pDevice->aFields[i].enmMatch == USBFILTERMATCH_IGNORE
822
|| !usbfilterMatchNumExpression(usbfilterGetString(pFilter, (USBFILTERIDX)i),
823
pDevice->aFields[i].u16Value))
825
dprintf(("filter match[%d]: !num_expression\n", i));
831
case USBFILTERMATCH_NUM_EXPRESSION_NP:
832
if ( pDevice->aFields[i].enmMatch != USBFILTERMATCH_IGNORE
833
&& !usbfilterMatchNumExpression(usbfilterGetString(pFilter, (USBFILTERIDX)i),
834
pDevice->aFields[i].u16Value))
836
dprintf(("filter match[%d]: !num_expression_no\n", i));
842
case USBFILTERMATCH_STR_EXACT:
843
if ( pDevice->aFields[i].enmMatch == USBFILTERMATCH_IGNORE
844
|| strcmp(usbfilterGetString(pFilter, (USBFILTERIDX)i),
845
usbfilterGetString(pDevice, (USBFILTERIDX)i)))
847
dprintf(("filter match[%d]: !str_exact\n", i));
853
case USBFILTERMATCH_STR_EXACT_NP:
854
if ( pDevice->aFields[i].enmMatch != USBFILTERMATCH_IGNORE
855
&& strcmp(usbfilterGetString(pFilter, (USBFILTERIDX)i),
856
usbfilterGetString(pDevice, (USBFILTERIDX)i)))
858
dprintf(("filter match[%d]: !str_exact_np\n", i));
864
case USBFILTERMATCH_STR_PATTERN:
865
if ( pDevice->aFields[i].enmMatch == USBFILTERMATCH_IGNORE
866
|| !usbfilterMatchStringPattern(usbfilterGetString(pFilter, (USBFILTERIDX)i),
867
usbfilterGetString(pDevice, (USBFILTERIDX)i)))
869
dprintf(("filter match[%d]: !str_pattern\n", i));
875
case USBFILTERMATCH_STR_PATTERN_NP:
876
if ( pDevice->aFields[i].enmMatch != USBFILTERMATCH_IGNORE
877
&& !usbfilterMatchStringPattern(usbfilterGetString(pFilter, (USBFILTERIDX)i),
878
usbfilterGetString(pDevice, (USBFILTERIDX)i)))
880
dprintf(("filter match[%d]: !str_pattern_np\n", i));
887
AssertMsgFailed(("#%d: %d\n", i, pFilter->aFields[i].enmMatch));
892
/* iRate is the range 0..2*cFields - recalc to percent. */
893
dprintf(("filter match: iRate=%d", iRate));
894
return iRate == 2 * RT_ELEMENTS(pFilter->aFields)
896
: (iRate * 100) / (2 * RT_ELEMENTS(pFilter->aFields));
901
* Match a filter against a USBDEVICE.
903
* @returns true if they match, false if not.
905
* @param pFilter The filter to match with.
906
* @param pDevice The device to match.
908
* @remark Both the filter and the device are ASSUMED to be valid because
909
* we don't wish to waste any time in this function.
911
USBLIB_DECL(bool) USBFilterMatchDevice(PCUSBFILTER pFilter, PUSBDEVICE pDevice)
913
for (unsigned i = 0; i < RT_ELEMENTS(pFilter->aFields); i++)
915
switch (pFilter->aFields[i].enmMatch)
917
case USBFILTERMATCH_IGNORE:
920
case USBFILTERMATCH_PRESENT:
925
case USBFILTERIDX_MANUFACTURER_STR: psz = pDevice->pszManufacturer; break;
926
case USBFILTERIDX_PRODUCT_STR: psz = pDevice->pszProduct; break;
927
case USBFILTERIDX_SERIAL_NUMBER_STR: psz = pDevice->pszSerialNumber; break;
928
default: psz = ""; break;
935
case USBFILTERMATCH_NUM_EXACT:
936
case USBFILTERMATCH_NUM_EXACT_NP:
937
case USBFILTERMATCH_NUM_EXPRESSION:
938
case USBFILTERMATCH_NUM_EXPRESSION_NP:
943
case USBFILTERIDX_VENDOR_ID: u16Value = pDevice->idVendor; break;
944
case USBFILTERIDX_PRODUCT_ID: u16Value = pDevice->idProduct; break;
945
case USBFILTERIDX_DEVICE: u16Value = pDevice->bcdDevice; break;
946
case USBFILTERIDX_DEVICE_CLASS: u16Value = pDevice->bDeviceClass; break;
947
case USBFILTERIDX_DEVICE_SUB_CLASS: u16Value = pDevice->bDeviceSubClass; break;
948
case USBFILTERIDX_DEVICE_PROTOCOL: u16Value = pDevice->bDeviceProtocol; break;
949
case USBFILTERIDX_BUS: u16Value = pDevice->bBus; break;
950
case USBFILTERIDX_PORT: u16Value = pDevice->bPort; break;
951
default: u16Value = ~0; break;
954
switch (pFilter->aFields[i].enmMatch)
956
case USBFILTERMATCH_NUM_EXACT:
957
case USBFILTERMATCH_NUM_EXACT_NP:
958
if (pFilter->aFields[i].u16Value != u16Value)
961
case USBFILTERMATCH_NUM_EXPRESSION:
962
case USBFILTERMATCH_NUM_EXPRESSION_NP:
963
if (!usbfilterMatchNumExpression(usbfilterGetString(pFilter, (USBFILTERIDX)i), u16Value))
970
case USBFILTERMATCH_STR_EXACT:
971
case USBFILTERMATCH_STR_EXACT_NP:
972
case USBFILTERMATCH_STR_PATTERN:
973
case USBFILTERMATCH_STR_PATTERN_NP:
978
case USBFILTERIDX_MANUFACTURER_STR: psz = pDevice->pszManufacturer; break;
979
case USBFILTERIDX_PRODUCT_STR: psz = pDevice->pszProduct; break;
980
case USBFILTERIDX_SERIAL_NUMBER_STR: psz = pDevice->pszSerialNumber; break;
981
default: psz = NULL; break;
983
switch (pFilter->aFields[i].enmMatch)
985
case USBFILTERMATCH_STR_EXACT:
987
|| strcmp(usbfilterGetString(pFilter, (USBFILTERIDX)i), psz))
991
case USBFILTERMATCH_STR_EXACT_NP:
993
&& strcmp(usbfilterGetString(pFilter, (USBFILTERIDX)i), psz))
997
case USBFILTERMATCH_STR_PATTERN:
999
|| !usbfilterMatchStringPattern(usbfilterGetString(pFilter, (USBFILTERIDX)i), psz))
1003
case USBFILTERMATCH_STR_PATTERN_NP:
1005
&& !usbfilterMatchStringPattern(usbfilterGetString(pFilter, (USBFILTERIDX)i), psz))
1013
AssertMsgFailed(("#%d: %d\n", i, pFilter->aFields[i].enmMatch));
1023
* Checks if the two filters are identical.
1025
* @returns true if the are identical, false if they aren't.
1026
* @param pFilter The first filter.
1027
* @param pFilter2 The second filter.
1029
USBLIB_DECL(bool) USBFilterIsIdentical(PCUSBFILTER pFilter, PCUSBFILTER pFilter2)
1031
/* Lazy works here because we're darn strict with zero padding and such elsewhere. */
1032
return memcmp(pFilter, pFilter2, sizeof(*pFilter)) == 0;
1038
* Sets the filter type.
1040
* @returns VBox status code.
1041
* @retval VERR_INVALID_PARAMETER if the filter type is invalid.
1042
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1044
* @param pFilter The filter.
1045
* @param enmType The new filter type.
1047
USBLIB_DECL(int) USBFilterSetFilterType(PUSBFILTER pFilter, USBFILTERTYPE enmType)
1049
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, VERR_INVALID_MAGIC);
1050
AssertReturn(enmType > USBFILTERTYPE_INVALID && enmType < USBFILTERTYPE_END, VERR_INVALID_PARAMETER);
1052
pFilter->enmType = enmType;
1053
return VINF_SUCCESS;
1058
* Replaces the string value of a field.
1060
* This will remove any existing string value current held by the field from the
1061
* string table and then attempt to add the new value. This function can be used
1062
* to delete any assigned string before changing the type to numeric by passing
1063
* in an empty string. This works because the first byte in the string table is
1064
* reserved for the empty (NULL) string.
1066
* @returns VBox status code.
1067
* @retval VINF_SUCCESS on success.
1068
* @retval VERR_BUFFER_OVERFLOW if the string table is full.
1069
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx isn't valid.
1070
* @retval VERR_INVALID_POINTER if pszString isn't valid.
1071
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1073
* @param pFilter The filter.
1074
* @param enmFieldIdx The field index.
1075
* @param pszString The string to add.
1077
static int usbfilterSetString(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx, const char *pszString)
1082
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, VERR_INVALID_MAGIC);
1083
AssertReturn((unsigned)enmFieldIdx < (unsigned)USBFILTERIDX_END, VERR_INVALID_PARAMETER);
1084
AssertPtrReturn(pszString, VERR_INVALID_POINTER);
1086
Assert(pFilter->offCurEnd < sizeof(pFilter->achStrTab));
1087
Assert(pFilter->achStrTab[pFilter->offCurEnd] == '\0');
1090
* Remove old string value if any.
1092
if ( USBFilterIsMethodUsingStringValue((USBFILTERMATCH)pFilter->aFields[enmFieldIdx].enmMatch)
1093
&& pFilter->aFields[enmFieldIdx].u16Value != 0)
1095
uint32_t off = pFilter->aFields[enmFieldIdx].u16Value;
1096
pFilter->aFields[enmFieldIdx].u16Value = 0; /* Assign it to the NULL string. */
1098
unsigned cchShift = (unsigned)strlen(&pFilter->achStrTab[off]) + 1;
1099
ssize_t cchToMove = (pFilter->offCurEnd + 1) - (off + cchShift);
1100
Assert(cchToMove >= 0);
1103
/* We're not last - must shift the strings. */
1104
memmove(&pFilter->achStrTab[off], &pFilter->achStrTab[off + cchShift], cchToMove);
1105
for (unsigned i = 0; i < RT_ELEMENTS(pFilter->aFields); i++)
1106
if ( pFilter->aFields[i].u16Value >= off
1107
&& USBFilterIsMethodUsingStringValue((USBFILTERMATCH)pFilter->aFields[i].enmMatch))
1108
pFilter->aFields[i].u16Value -= cchShift;
1110
pFilter->offCurEnd -= cchShift;
1111
Assert(pFilter->offCurEnd < sizeof(pFilter->achStrTab));
1112
Assert(pFilter->offCurEnd + cchShift <= sizeof(pFilter->achStrTab));
1114
/* zero the unused string table (to allow lazyness/strictness elsewhere). */
1115
memset(&pFilter->achStrTab[pFilter->offCurEnd], '\0', cchShift);
1119
* Make a special case for the empty string.
1120
* (This also makes the delete logical above work correctly for the last string.)
1123
pFilter->aFields[enmFieldIdx].u16Value = 0;
1126
const size_t cch = strlen(pszString);
1127
if (pFilter->offCurEnd + cch + 2 > sizeof(pFilter->achStrTab))
1128
return VERR_BUFFER_OVERFLOW;
1130
pFilter->aFields[enmFieldIdx].u16Value = pFilter->offCurEnd + 1;
1131
memcpy(&pFilter->achStrTab[pFilter->offCurEnd + 1], pszString, cch + 1);
1132
pFilter->offCurEnd += (uint32_t)cch + 1;
1135
return VINF_SUCCESS;
1139
* Wrapper around usbfilterSetString() that deletes any string value
1140
* currently assigned to a field.
1142
* Upon successful return the field contains a null string, nothing or a number.
1144
* This function will validate the field index if there isn't any string
1145
* value to delete, thus preventing any extra validating of the index.
1147
* @returns VBox status code. See usbfilterSetString.
1148
* @param pFilter The filter.
1149
* @param enmFieldIdx The index of the field which string value should be deleted.
1151
static int usbfilterDeleteAnyStringValue(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
1153
int rc = VINF_SUCCESS;
1154
if ( USBFilterIsMethodUsingStringValue((USBFILTERMATCH)pFilter->aFields[enmFieldIdx].enmMatch)
1155
&& pFilter->aFields[enmFieldIdx].u16Value != 0)
1156
rc = usbfilterSetString(pFilter, enmFieldIdx, "");
1157
else if ((unsigned)enmFieldIdx >= (unsigned)USBFILTERIDX_END)
1158
rc = VERR_INVALID_PARAMETER;
1164
* Sets a field to always match (ignore whatever is thrown at it).
1166
* @returns VBox status code.
1167
* @retval VINF_SUCCESS on success.
1168
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx isn't valid.
1169
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1171
* @param pFilter The filter.
1172
* @param enmFieldIdx The field index. This must be a string field.
1174
USBLIB_DECL(int) USBFilterSetIgnore(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
1176
int rc = usbfilterDeleteAnyStringValue(pFilter, enmFieldIdx);
1179
pFilter->aFields[enmFieldIdx].enmMatch = USBFILTERMATCH_IGNORE;
1180
pFilter->aFields[enmFieldIdx].u16Value = 0;
1187
* Sets a field to match on device field present only.
1189
* @returns VBox status code.
1190
* @retval VINF_SUCCESS on success.
1191
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx isn't valid.
1192
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1194
* @param pFilter The filter.
1195
* @param enmFieldIdx The field index. This must be a string field.
1197
USBLIB_DECL(int) USBFilterSetPresentOnly(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
1199
int rc = usbfilterDeleteAnyStringValue(pFilter, enmFieldIdx);
1202
pFilter->aFields[enmFieldIdx].enmMatch = USBFILTERMATCH_PRESENT;
1203
pFilter->aFields[enmFieldIdx].u16Value = 0;
1210
* Sets a field to exactly match a number.
1212
* @returns VBox status code.
1213
* @retval VINF_SUCCESS on success.
1214
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx isn't valid.
1215
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1217
* @param pFilter The filter.
1218
* @param enmFieldIdx The field index. This must be a string field.
1219
* @param u16Value The string pattern.
1220
* @param fMustBePresent If set, a non-present field on the device will result in a mismatch.
1221
* If clear, a non-present field on the device will match.
1223
USBLIB_DECL(int) USBFilterSetNumExact(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx, uint16_t u16Value, bool fMustBePresent)
1225
int rc = USBFilterIsNumericField(enmFieldIdx) ? VINF_SUCCESS : VERR_INVALID_PARAMETER;
1228
rc = usbfilterDeleteAnyStringValue(pFilter, enmFieldIdx);
1231
pFilter->aFields[enmFieldIdx].u16Value = u16Value;
1232
pFilter->aFields[enmFieldIdx].enmMatch = fMustBePresent ? USBFILTERMATCH_NUM_EXACT : USBFILTERMATCH_NUM_EXACT_NP;
1241
* Sets a field to match a numeric expression.
1243
* @returns VBox status code.
1244
* @retval VINF_SUCCESS on success.
1245
* @retval VERR_BUFFER_OVERFLOW if the string table is full.
1246
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx or the numeric expression aren't valid.
1247
* @retval VERR_INVALID_POINTER if pszExpression isn't a valid pointer.
1248
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1250
* @param pFilter The filter.
1251
* @param enmFieldIdx The field index. This must be a string field.
1252
* @param pszExpression The numeric expression.
1253
* @param fMustBePresent If set, a non-present field on the device will result in a mismatch.
1254
* If clear, a non-present field on the device will match.
1256
USBLIB_DECL(int) USBFilterSetNumExpression(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx, const char *pszExpression, bool fMustBePresent)
1258
int rc = USBFilterIsNumericField(enmFieldIdx) ? VINF_SUCCESS : VERR_INVALID_PARAMETER;
1261
/* Strip leading spaces and empty sub expressions (||). */
1262
while (*pszExpression && (RT_C_IS_BLANK(*pszExpression) || *pszExpression == '|'))
1265
rc = usbfilterValidateNumExpression(pszExpression);
1268
/* We could optimize the expression further (stripping spaces, convert numbers),
1269
but it's more work than what it's worth and it could upset some users. */
1270
rc = usbfilterSetString(pFilter, enmFieldIdx, pszExpression);
1272
pFilter->aFields[enmFieldIdx].enmMatch = fMustBePresent ? USBFILTERMATCH_NUM_EXPRESSION : USBFILTERMATCH_NUM_EXPRESSION_NP;
1273
else if (rc == VERR_NO_DIGITS)
1274
rc = VERR_INVALID_PARAMETER;
1282
* Sets a field to exactly match a string.
1284
* @returns VBox status code.
1285
* @retval VINF_SUCCESS on success.
1286
* @retval VERR_BUFFER_OVERFLOW if the string table is full.
1287
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx isn't valid.
1288
* @retval VERR_INVALID_POINTER if pszPattern isn't a valid pointer.
1289
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1291
* @param pFilter The filter.
1292
* @param enmFieldIdx The field index. This must be a string field.
1293
* @param pszValue The string value.
1294
* @param fMustBePresent If set, a non-present field on the device will result in a mismatch.
1295
* If clear, a non-present field on the device will match.
1297
USBLIB_DECL(int) USBFilterSetStringExact(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx, const char *pszValue, bool fMustBePresent)
1299
int rc = USBFilterIsStringField(enmFieldIdx) ? VINF_SUCCESS : VERR_INVALID_PARAMETER;
1302
rc = usbfilterSetString(pFilter, enmFieldIdx, pszValue);
1304
pFilter->aFields[enmFieldIdx].enmMatch = fMustBePresent ? USBFILTERMATCH_STR_EXACT : USBFILTERMATCH_STR_EXACT_NP;
1311
* Sets a field to match a string pattern.
1313
* @returns VBox status code.
1314
* @retval VINF_SUCCESS on success.
1315
* @retval VERR_BUFFER_OVERFLOW if the string table is full.
1316
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx or pattern aren't valid.
1317
* @retval VERR_INVALID_POINTER if pszPattern isn't a valid pointer.
1318
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1320
* @param pFilter The filter.
1321
* @param enmFieldIdx The field index. This must be a string field.
1322
* @param pszPattern The string pattern.
1323
* @param fMustBePresent If set, a non-present field on the device will result in a mismatch.
1324
* If clear, a non-present field on the device will match.
1326
USBLIB_DECL(int) USBFilterSetStringPattern(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx, const char *pszPattern, bool fMustBePresent)
1328
int rc = USBFilterIsStringField(enmFieldIdx) ? VINF_SUCCESS : VERR_INVALID_PARAMETER;
1331
rc = usbfilterValidateStringPattern(pszPattern);
1334
rc = usbfilterSetString(pFilter, enmFieldIdx, pszPattern);
1336
pFilter->aFields[enmFieldIdx].enmMatch = fMustBePresent ? USBFILTERMATCH_STR_PATTERN : USBFILTERMATCH_STR_PATTERN_NP;
1344
* Sets the must-be-present part of a field.
1346
* This only works on field which already has matching criteria. This means
1347
* that field marked 'ignore' will not be processed and will result in a
1348
* warning status code.
1350
* @returns VBox status code.
1351
* @retval VINF_SUCCESS on success.
1352
* @retval VWRN_INVALID_PARAMETER if the field is marked 'ignore'. No assertions.
1353
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx or pattern aren't valid.
1354
* @retval VERR_INVALID_POINTER if pszPattern isn't a valid pointer.
1355
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1357
* @param pFilter The filter.
1358
* @param enmFieldIdx The field index.
1359
* @param fMustBePresent If set, a non-present field on the device will result in a mismatch.
1360
* If clear, a non-present field on the device will match.
1362
USBLIB_DECL(int) USBFilterSetMustBePresent(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx, bool fMustBePresent)
1364
AssertPtrReturn(pFilter, VERR_INVALID_POINTER);
1365
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, VERR_INVALID_MAGIC);
1366
AssertReturn((unsigned)enmFieldIdx < (unsigned)USBFILTERIDX_END, VERR_INVALID_PARAMETER);
1368
USBFILTERMATCH enmMatch = (USBFILTERMATCH)pFilter->aFields[enmFieldIdx].enmMatch;
1373
case USBFILTERMATCH_IGNORE:
1374
return VWRN_INVALID_PARAMETER;
1376
case USBFILTERMATCH_PRESENT:
1377
case USBFILTERMATCH_NUM_EXACT:
1378
case USBFILTERMATCH_NUM_EXPRESSION:
1379
case USBFILTERMATCH_STR_EXACT:
1380
case USBFILTERMATCH_STR_PATTERN:
1383
case USBFILTERMATCH_NUM_EXACT_NP:
1384
enmMatch = USBFILTERMATCH_NUM_EXACT;
1386
case USBFILTERMATCH_NUM_EXPRESSION_NP:
1387
enmMatch = USBFILTERMATCH_NUM_EXPRESSION;
1389
case USBFILTERMATCH_STR_EXACT_NP:
1390
enmMatch = USBFILTERMATCH_STR_EXACT;
1392
case USBFILTERMATCH_STR_PATTERN_NP:
1393
enmMatch = USBFILTERMATCH_STR_PATTERN;
1396
AssertMsgFailedReturn(("%p: enmFieldIdx=%d enmMatch=%d\n", pFilter, enmFieldIdx, enmMatch), VERR_INVALID_MAGIC);
1403
case USBFILTERMATCH_IGNORE:
1404
return VWRN_INVALID_PARAMETER;
1406
case USBFILTERMATCH_NUM_EXACT_NP:
1407
case USBFILTERMATCH_STR_PATTERN_NP:
1408
case USBFILTERMATCH_STR_EXACT_NP:
1409
case USBFILTERMATCH_NUM_EXPRESSION_NP:
1412
case USBFILTERMATCH_PRESENT:
1413
enmMatch = USBFILTERMATCH_IGNORE;
1415
case USBFILTERMATCH_NUM_EXACT:
1416
enmMatch = USBFILTERMATCH_NUM_EXACT_NP;
1418
case USBFILTERMATCH_NUM_EXPRESSION:
1419
enmMatch = USBFILTERMATCH_NUM_EXPRESSION_NP;
1421
case USBFILTERMATCH_STR_EXACT:
1422
enmMatch = USBFILTERMATCH_STR_EXACT_NP;
1424
case USBFILTERMATCH_STR_PATTERN:
1425
enmMatch = USBFILTERMATCH_STR_PATTERN_NP;
1429
AssertMsgFailedReturn(("%p: enmFieldIdx=%d enmMatch=%d\n", pFilter, enmFieldIdx, enmMatch), VERR_INVALID_MAGIC);
1433
pFilter->aFields[enmFieldIdx].enmMatch = enmMatch;
1434
return VINF_SUCCESS;
1439
* Gets the filter type.
1441
* @returns The filter type.
1442
* USBFILTERTYPE_INVALID if the filter is invalid.
1443
* @param pFilter The filter.
1445
USBLIB_DECL(USBFILTERTYPE) USBFilterGetFilterType(PCUSBFILTER pFilter)
1447
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, USBFILTERTYPE_INVALID);
1448
return pFilter->enmType;
1453
* Gets the matching method for a field.
1455
* @returns The matching method on success, UBFILTERMATCH_INVALID on invalid field index.
1456
* @param pFilter The filter.
1457
* @param enmFieldIdx The field index.
1459
USBLIB_DECL(USBFILTERMATCH) USBFilterGetMatchingMethod(PCUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
1461
if ( pFilter->u32Magic == USBFILTER_MAGIC
1462
&& (unsigned)enmFieldIdx < (unsigned)USBFILTERIDX_END)
1463
return (USBFILTERMATCH)pFilter->aFields[enmFieldIdx].enmMatch;
1464
return USBFILTERMATCH_INVALID;
1469
* Gets the numeric value of a field.
1471
* The field must contain a number, we're not doing any conversions for you.
1473
* @returns VBox status code.
1474
* @retval VINF_SUCCESS on success.
1475
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx isn't valid or if the field doesn't contain a number.
1476
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1478
* @param pFilter The filter.
1479
* @param enmFieldIdx The field index.
1480
* @param pu16Value Where to store the value.
1482
USBLIB_DECL(int) USBFilterQueryNum(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx, uint16_t *pu16Value)
1484
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, VERR_INVALID_MAGIC);
1485
int iValue = usbfilterGetNum(pFilter, enmFieldIdx);
1487
return VERR_INVALID_PARAMETER;
1488
*pu16Value = (uint16_t)iValue;
1489
return VINF_SUCCESS;
1494
* Gets the numeric value of a field.
1496
* The field must contain a number, we're not doing any conversions for you.
1498
* @returns The field value on success, -1 on failure (invalid input / not numeric).
1500
* @param pFilter The filter.
1501
* @param enmFieldIdx The field index.
1503
USBLIB_DECL(int) USBFilterGetNum(PCUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
1505
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, -1);
1506
return usbfilterGetNum(pFilter, enmFieldIdx);
1511
* Gets the string value of a field.
1513
* The field must contain a string, we're not doing any conversions for you.
1515
* @returns VBox status code.
1516
* @retval VINF_SUCCESS on success.
1517
* @retval VERR_BUFFER_OVERFLOW if the buffer isn't sufficient to hold the string. The buffer
1518
* will be filled with as much of the string that'll fit.
1519
* @retval VERR_INVALID_PARAMETER if the enmFieldIdx isn't valid or if the field doesn't contain a string.
1520
* @retval VERR_INVALID_MAGIC if pFilter is invalid.
1522
* @param pFilter The filter.
1523
* @param enmFieldIdx The field index.
1524
* @param pszBuf Where to store the string.
1525
* @param cchBuf The size of the buffer.
1527
USBLIB_DECL(int) USBFilterQueryString(PUSBFILTER pFilter, USBFILTERIDX enmFieldIdx, char *pszBuf, size_t cchBuf)
1529
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, VERR_INVALID_MAGIC);
1531
const char *psz = usbfilterGetString(pFilter, enmFieldIdx);
1532
if (RT_UNLIKELY(!psz))
1533
return VERR_INVALID_PARAMETER;
1535
int rc = VINF_SUCCESS;
1536
size_t cch = strlen(psz);
1538
memcpy(pszBuf, psz, cch + 1);
1541
rc = VERR_BUFFER_OVERFLOW;
1544
memcpy(pszBuf, psz, cchBuf - 1);
1545
pszBuf[cchBuf - 1] = '\0';
1554
* Gets the string table entry for a field.
1556
* @returns Pointer to the string. (readonly!)
1558
* @param pFilter The filter.
1559
* @param enmFieldIdx The field index.
1561
USBLIB_DECL(const char *) USBFilterGetString(PCUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
1563
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, NULL);
1565
const char *psz = usbfilterGetString(pFilter, enmFieldIdx);
1566
if (RT_UNLIKELY(!psz))
1573
* Gets the string length of a field containing a string.
1575
* @returns String length on success, -1 on failure (not a string, bad filter).
1576
* @param pFilter The filter.
1577
* @param enmFieldIdx The field index.
1579
USBLIB_DECL(ssize_t) USBFilterGetStringLen(PCUSBFILTER pFilter, USBFILTERIDX enmFieldIdx)
1581
if (RT_LIKELY(pFilter->u32Magic == USBFILTER_MAGIC))
1583
const char *psz = usbfilterGetString(pFilter, enmFieldIdx);
1592
* Check if any of the fields are set to something substatial.
1594
* Consider the fileter a wildcard if this returns false.
1596
* @returns true / false.
1597
* @param pFilter The filter.
1599
USBLIB_DECL(bool) USBFilterHasAnySubstatialCriteria(PCUSBFILTER pFilter)
1601
AssertReturn(pFilter->u32Magic == USBFILTER_MAGIC, false);
1603
for (unsigned i = 0; i < RT_ELEMENTS(pFilter->aFields); i++)
1605
switch (pFilter->aFields[i].enmMatch)
1607
case USBFILTERMATCH_IGNORE:
1608
case USBFILTERMATCH_PRESENT:
1611
case USBFILTERMATCH_NUM_EXACT:
1612
case USBFILTERMATCH_NUM_EXACT_NP:
1613
case USBFILTERMATCH_STR_EXACT:
1614
case USBFILTERMATCH_STR_EXACT_NP:
1617
case USBFILTERMATCH_NUM_EXPRESSION:
1618
case USBFILTERMATCH_NUM_EXPRESSION_NP:
1620
const char *psz = usbfilterGetString(pFilter, (USBFILTERIDX)i);
1623
while (*psz && (*psz == '|' || RT_C_IS_BLANK(*psz)))
1631
case USBFILTERMATCH_STR_PATTERN:
1632
case USBFILTERMATCH_STR_PATTERN_NP:
1634
const char *psz = usbfilterGetString(pFilter, (USBFILTERIDX)i);
1637
while (*psz && (*psz == '*' || *psz == '?'))
1653
* Checks whether the specified field is a numeric field or not.
1655
* @returns true / false.
1656
* @param enmFieldIdx The field index.
1658
USBLIB_DECL(bool) USBFilterIsNumericField(USBFILTERIDX enmFieldIdx)
1660
switch (enmFieldIdx)
1662
case USBFILTERIDX_VENDOR_ID:
1663
case USBFILTERIDX_PRODUCT_ID:
1664
case USBFILTERIDX_DEVICE:
1665
case USBFILTERIDX_DEVICE_CLASS:
1666
case USBFILTERIDX_DEVICE_SUB_CLASS:
1667
case USBFILTERIDX_DEVICE_PROTOCOL:
1668
case USBFILTERIDX_BUS:
1669
case USBFILTERIDX_PORT:
1673
AssertMsgFailed(("%d\n", enmFieldIdx));
1674
case USBFILTERIDX_MANUFACTURER_STR:
1675
case USBFILTERIDX_PRODUCT_STR:
1676
case USBFILTERIDX_SERIAL_NUMBER_STR:
1683
* Checks whether the specified field is a string field or not.
1685
* @returns true / false.
1686
* @param enmFieldIdx The field index.
1688
USBLIB_DECL(bool) USBFilterIsStringField(USBFILTERIDX enmFieldIdx)
1690
switch (enmFieldIdx)
1693
AssertMsgFailed(("%d\n", enmFieldIdx));
1694
case USBFILTERIDX_VENDOR_ID:
1695
case USBFILTERIDX_PRODUCT_ID:
1696
case USBFILTERIDX_DEVICE:
1697
case USBFILTERIDX_DEVICE_CLASS:
1698
case USBFILTERIDX_DEVICE_SUB_CLASS:
1699
case USBFILTERIDX_DEVICE_PROTOCOL:
1700
case USBFILTERIDX_BUS:
1701
case USBFILTERIDX_PORT:
1704
case USBFILTERIDX_MANUFACTURER_STR:
1705
case USBFILTERIDX_PRODUCT_STR:
1706
case USBFILTERIDX_SERIAL_NUMBER_STR:
1713
* Checks whether the specified matching method uses a numeric value or not.
1715
* @returns true / false.
1716
* @param enmMatchingMethod The matching method.
1718
USBLIB_DECL(bool) USBFilterIsMethodUsingNumericValue(USBFILTERMATCH enmMatchingMethod)
1720
switch (enmMatchingMethod)
1723
AssertMsgFailed(("%d\n", enmMatchingMethod));
1724
case USBFILTERMATCH_IGNORE:
1725
case USBFILTERMATCH_PRESENT:
1726
case USBFILTERMATCH_NUM_EXPRESSION:
1727
case USBFILTERMATCH_NUM_EXPRESSION_NP:
1728
case USBFILTERMATCH_STR_EXACT:
1729
case USBFILTERMATCH_STR_EXACT_NP:
1730
case USBFILTERMATCH_STR_PATTERN:
1731
case USBFILTERMATCH_STR_PATTERN_NP:
1734
case USBFILTERMATCH_NUM_EXACT:
1735
case USBFILTERMATCH_NUM_EXACT_NP:
1742
* Checks whether the specified matching method uses a string value or not.
1744
* @returns true / false.
1745
* @param enmMatchingMethod The matching method.
1747
USBLIB_DECL(bool) USBFilterIsMethodUsingStringValue(USBFILTERMATCH enmMatchingMethod)
1749
switch (enmMatchingMethod)
1752
AssertMsgFailed(("%d\n", enmMatchingMethod));
1753
case USBFILTERMATCH_IGNORE:
1754
case USBFILTERMATCH_PRESENT:
1755
case USBFILTERMATCH_NUM_EXACT:
1756
case USBFILTERMATCH_NUM_EXACT_NP:
1759
case USBFILTERMATCH_NUM_EXPRESSION:
1760
case USBFILTERMATCH_NUM_EXPRESSION_NP:
1761
case USBFILTERMATCH_STR_EXACT:
1762
case USBFILTERMATCH_STR_EXACT_NP:
1763
case USBFILTERMATCH_STR_PATTERN:
1764
case USBFILTERMATCH_STR_PATTERN_NP:
1771
* Checks if a matching method is for string fields or not.
1773
* @returns true / false.
1774
* @param enmMatchingMethod The matching method.
1776
USBLIB_DECL(bool) USBFilterIsMethodNumeric(USBFILTERMATCH enmMatchingMethod)
1778
return enmMatchingMethod >= USBFILTERMATCH_NUM_FIRST
1779
&& enmMatchingMethod <= USBFILTERMATCH_NUM_LAST;
1783
* Checks if a matching method is for string fields or not.
1785
* @returns true / false.
1786
* @param enmMatchingMethod The matching method.
1788
USBLIB_DECL(bool) USBFilterIsMethodString(USBFILTERMATCH enmMatchingMethod)
1790
return enmMatchingMethod >= USBFILTERMATCH_STR_FIRST
1791
&& enmMatchingMethod <= USBFILTERMATCH_STR_LAST;