public unsafe void SetNumExact(USBFILTERIDX enmFieldIdx, UInt16 u16Value, bool fMustBePresent) { if (!IsNumericField(enmFieldIdx)) { throw new ArgumentOutOfRangeException("enmFieldIdx"); } DeleteAnyStringValue(enmFieldIdx); fixed(USBFILTER *pFilter = &this) { USBFILTERFIELD *aFields = (USBFILTERFIELD *)pFilter->aFields; aFields[(int)enmFieldIdx].u16Value = u16Value; aFields[(int)enmFieldIdx].enmMatch = fMustBePresent ? USBFILTERMATCH.USBFILTERMATCH_NUM_EXACT : USBFILTERMATCH.USBFILTERMATCH_NUM_EXACT_NP; } }
unsafe void DeleteAnyStringValue(USBFILTERIDX enmFieldIdx) { fixed(USBFILTER *pFilter = &this) { USBFILTERFIELD *aFields = (USBFILTERFIELD *)pFilter->aFields; if (IsMethodUsingStringValue((USBFILTERMATCH)aFields[(int)enmFieldIdx].enmMatch) && aFields[(int)enmFieldIdx].u16Value != 0) { SetString(enmFieldIdx, new Byte[0]); } else if (enmFieldIdx >= USBFILTERIDX.USBFILTERIDX_END) { throw new ArgumentOutOfRangeException("enmFieldIdx"); } } }
public unsafe void SetString(USBFILTERIDX enmFieldIdx, Byte[] pszString) { fixed(USBFILTER *pFilter = &this) { USBFILTERFIELD *aFields = (USBFILTERFIELD *)pFilter->aFields; if (IsMethodUsingStringValue((USBFILTERMATCH)aFields[(int)enmFieldIdx].enmMatch) && aFields[(int)enmFieldIdx].u16Value != 0) { int off = aFields[(int)enmFieldIdx].u16Value; aFields[(int)enmFieldIdx].u16Value = 0; /* Assign it to the NULL string. */ int cchShift = strlen(&pFilter->achStrTab[off]) + 1; int cchToMove = ((int)offCurEnd + 1) - (off + cchShift); if (cchToMove > 0) { memmove(&pFilter->achStrTab[off], &pFilter->achStrTab[off + cchShift], cchToMove); for (int i = 0; i < (int)USBFILTERIDX.USBFILTERIDX_END; i++) { if (aFields[i].u16Value >= off && IsMethodUsingStringValue(aFields[i].enmMatch)) { aFields[i].u16Value -= (ushort)cchShift; } } } offCurEnd -= (uint)cchShift; memset(&pFilter->achStrTab[offCurEnd], 0, cchShift); } if (pszString.Length == 0) { aFields[(int)enmFieldIdx].u16Value = 0; } else { int cch = pszString.Length; if (this.offCurEnd + cch + 2 > 256) { throw new IndexOutOfRangeException("Buffer overflow"); } aFields[(int)enmFieldIdx].u16Value = (ushort)(this.offCurEnd + 1); for (int i = 0; i < cch + 1; i++) { pFilter->achStrTab[offCurEnd + 1 + i] = pszString[i]; } offCurEnd += (uint)cch + 1; } } }
static Boolean IsNumericField(USBFILTERIDX enmFieldIdx) { switch (enmFieldIdx) { case USBFILTERIDX.USBFILTERIDX_VENDOR_ID: case USBFILTERIDX.USBFILTERIDX_PRODUCT_ID: case USBFILTERIDX.USBFILTERIDX_DEVICE_REV: case USBFILTERIDX.USBFILTERIDX_DEVICE_CLASS: case USBFILTERIDX.USBFILTERIDX_DEVICE_SUB_CLASS: case USBFILTERIDX.USBFILTERIDX_DEVICE_PROTOCOL: case USBFILTERIDX.USBFILTERIDX_BUS: case USBFILTERIDX.USBFILTERIDX_PORT: return(true); case USBFILTERIDX.USBFILTERIDX_MANUFACTURER_STR: case USBFILTERIDX.USBFILTERIDX_PRODUCT_STR: case USBFILTERIDX.USBFILTERIDX_SERIAL_NUMBER_STR: return(false); default: throw new ArgumentOutOfRangeException("enmFieldIdx"); } }