private SortedDictionary <RadioTapType, RadioTapField> ReadRadioTapFields() { var bitmasks = this.Present; var retval = new SortedDictionary <RadioTapType, RadioTapField>(); var bitIndex = 0; // create a binary reader that points to the memory immediately after the bitmasks var offset = this.header.Offset + RadioFields.PresentPosition + (bitmasks.Length) * Marshal.SizeOf(typeof(UInt32)); var br = new BinaryReader(new MemoryStream(this.header.Bytes, offset, this.Length - offset)); // now go through each of the bitmask fields looking at the least significant // bit first to retrieve each field foreach (var bmask in bitmasks) { var bmaskArray = new int[1]; bmaskArray[0] = (int)bmask; var ba = new BitArray(bmaskArray); var unhandledFieldFound = false; // look at all of the bits, note we don't want to consider the // highest bit since that indicates another bitfield that follows for (var x = 0; x < 31; x++) { if (ba[x]) { var field = RadioTapField.Parse(bitIndex, br); if (field != null) { retval[field.FieldType] = field; } else { //We have found a field that we dont handle. As we dont know how big //it is we can't handle any fields after it. We will just copy //the rest of the data around as a lump unhandledFieldFound = true; break; } } bitIndex++; } if (unhandledFieldFound) { break; } } //this will read the rest of the bytes. We pass in max value because we dont know how //much there is but this will ensure we get up to the end of the buffer this.UnhandledFieldBytes = br.ReadBytes(UInt16.MaxValue); return(retval); }
/// <summary> /// Add the specified field to the packet. /// </summary> /// <param name='field'> /// Field to be added /// </param> public void Add(RadioTapField field) { RadioTapFields[field.FieldType] = field; Length += field.Length; var presenceBit = (int)field.FieldType; var presenceField = (presenceBit / 32); if (Present.Length <= presenceField) { var newPresentFields = new UInt32[presenceField]; Array.Copy(Present, newPresentFields, Present.Length); //set bit 31 to true for every present field except the last one for (int i = 0; i < newPresentFields.Length - 1; i++) { newPresentFields[i] |= 0x80000000; } Present = newPresentFields; } Present[presenceField] |= (UInt32)(1 << presenceBit); }