LCOV - code coverage report
Current view: top level - lib/src/device_info - vendor_index.dart (source / functions) Coverage Total Hit
Test: lcov.info Lines: 97.8 % 93 91
Test Date: 2026-11-15 16:52:47 Functions: - 0 0

            Line data    Source code
       1              : import 'dart:convert';
       2              : import 'dart:typed_data';
       3              : 
       4              : import 'package:network_tools/src/device_info/vendor_index.data.dart';
       5              : import 'package:network_tools/src/device_info/vendor_index_layout.dart';
       6              : import 'package:universal_io/io.dart';
       7              : 
       8              : /// One row of the vendor index.
       9              : typedef VendorIndexEntry = ({
      10              :   String blockType,
      11              :   String lastUpdate,
      12              :   String name,
      13              :   String prefix,
      14              :   String private,
      15              : });
      16              : 
      17              : /// Read-only view over the packed MAC vendor index.
      18              : ///
      19              : /// The payload is decoded once, on first use, and lookups are binary searches
      20              : /// over the decoded bytes: no per-entry Dart objects are ever materialised.
      21              : class PackedVendorIndex {
      22           10 :   PackedVendorIndex._(this._bytes) : _data = ByteData.sublistView(_bytes) {
      23           15 :     if (_data.getUint32(0, Endian.little) != kVendorIndexMagic ||
      24           15 :         _data.getUint32(4, Endian.little) != kVendorIndexFormatVersion) {
      25            0 :       throw StateError(
      26              :         'The packed vendor index in vendor_index.data.dart does not match '
      27              :         'this reader. Run `dart run tool/build_vendor_index.dart`.',
      28              :       );
      29              :     }
      30           15 :     _n6 = _data.getUint32(8, Endian.little);
      31           15 :     _n7 = _data.getUint32(12, Endian.little);
      32           15 :     _n9 = _data.getUint32(16, Endian.little);
      33           15 :     _maL = _data.getUint32(20, Endian.little);
      34           15 :     _maM = _data.getUint32(24, Endian.little);
      35           15 :     _sub = _data.getUint32(28, Endian.little);
      36           15 :     _names = _data.getUint32(32, Endian.little);
      37           15 :     _dates = _data.getUint32(36, Endian.little);
      38            5 :     _readDateTable();
      39              :   }
      40              : 
      41              :   /// Decodes an already inflated payload, as built by
      42              :   /// `tool/build_vendor_index.dart`. Used by the parity test to compare the
      43              :   /// committed bytes against a fresh CSV build.
      44            2 :   PackedVendorIndex.fromBytes(Uint8List bytes) : this._(bytes);
      45              : 
      46              :   /// Decodes the shipped payload. Costs roughly ten milliseconds; see
      47              :   /// [vendorIndex] for the cached instance.
      48            5 :   factory PackedVendorIndex.decode() {
      49            5 :     final gzipped = base64.decode(kVendorIndexBase64);
      50           15 :     return PackedVendorIndex._(Uint8List.fromList(gzip.decode(gzipped)));
      51              :   }
      52              : 
      53              :   final Uint8List _bytes;
      54              :   final ByteData _data;
      55              : 
      56              :   late final int _n6;
      57              :   late final int _n7;
      58              :   late final int _n9;
      59              :   late final int _maL;
      60              :   late final int _maM;
      61              :   late final int _sub;
      62              :   late final int _names;
      63              :   late final int _dates;
      64              :   final List<String> _dateValues = [];
      65              : 
      66              :   /// The inflated payload bytes, exposed for the parity test.
      67            2 :   Uint8List get packedBytes => _bytes;
      68              : 
      69              :   /// Number of assignable prefixes in the index.
      70           30 :   int get length => _n6 + _n7 + _n9;
      71              : 
      72            5 :   void _readDateTable() {
      73           20 :     var p = _sub + _n9 * kVendorIndexLongRecordSize;
      74           10 :     final count = _data.getUint16(p, Endian.little);
      75            5 :     p += 2;
      76           10 :     for (var i = 0; i < count; i++) {
      77           30 :       final start = _dates + _data.getUint16(p + i * 2, Endian.little);
      78           15 :       _dateValues.add(_utf8At(start));
      79              :     }
      80              :   }
      81              : 
      82            5 :   String _utf8At(int start) {
      83              :     var end = start;
      84           15 :     while (_bytes[end] != 0) {
      85            5 :       end++;
      86              :     }
      87           10 :     return _utf8Decoder.convert(_bytes, start, end);
      88              :   }
      89              : 
      90              :   /// Longest match wins: an MA-S, MA-M or IAB sub-block is registered to its
      91              :   /// own vendor even when the MA-L block containing it is not.
      92              :   ///
      93              :   /// [hex] is an upper-case, separator-free MAC address as produced by
      94              :   /// `VendorTable.normalizedHex`.
      95            2 :   VendorIndexEntry? entryFor(String hex) {
      96            4 :     if (hex.length >= 9) {
      97            2 :       final entry = _probeLong(hex);
      98              :       if (entry != null) return entry;
      99              :     }
     100            4 :     if (hex.length >= 7) {
     101            8 :       final entry = _probeFixed(_maM, _n7, hex.substring(0, 7));
     102              :       if (entry != null) return entry;
     103              :     }
     104            4 :     if (hex.length >= 6) {
     105            8 :       return _probeFixed(_maL, _n6, hex.substring(0, 6));
     106              :     }
     107              :     return null;
     108              :   }
     109              : 
     110            2 :   VendorIndexEntry? _probeLong(String hex) {
     111            4 :     final top = int.parse(hex.substring(0, 8), radix: 16);
     112            4 :     final last = _hexValue(hex.codeUnitAt(8));
     113              :     var low = 0;
     114            4 :     var high = _n9 - 1;
     115            2 :     while (low <= high) {
     116            4 :       final mid = (low + high) >> 1;
     117            6 :       final p = _sub + mid * kVendorIndexLongRecordSize;
     118            4 :       final key = _data.getUint32(p, Endian.little);
     119            2 :       if (top < key) {
     120            2 :         high = mid - 1;
     121            2 :       } else if (top > key) {
     122            2 :         low = mid + 1;
     123              :       } else {
     124            6 :         final digit = _data.getUint8(p + 4);
     125            2 :         if (last < digit) {
     126            2 :           high = mid - 1;
     127            2 :         } else if (last > digit) {
     128            2 :           low = mid + 1;
     129              :         } else {
     130            8 :           return _entry(p + 5, p + 9, hex.substring(0, 9));
     131              :         }
     132              :       }
     133              :     }
     134              :     return null;
     135              :   }
     136              : 
     137            2 :   VendorIndexEntry? _probeFixed(int section, int count, String prefix) {
     138            2 :     if (count == 0) return null;
     139            2 :     final key = int.parse(prefix, radix: 16);
     140              :     var low = 0;
     141            2 :     var high = count - 1;
     142            2 :     while (low <= high) {
     143            4 :       final mid = (low + high) >> 1;
     144            4 :       final p = section + mid * kVendorIndexRecordSize;
     145            4 :       final value = _data.getUint32(p, Endian.little);
     146            2 :       if (key < value) {
     147            2 :         high = mid - 1;
     148            2 :       } else if (key > value) {
     149            2 :         low = mid + 1;
     150              :       } else {
     151            6 :         return _entry(p + 4, p + 8, prefix);
     152              :       }
     153              :     }
     154              :     return null;
     155              :   }
     156              : 
     157            2 :   VendorIndexEntry _entry(int valueOffset, int metaOffset, String prefix) => (
     158              :     prefix: prefix,
     159           10 :     name: _utf8At(_data.getUint32(valueOffset, Endian.little) + _names),
     160            6 :     private: kVendorPrivateCodes[_data.getUint8(metaOffset)],
     161            8 :     blockType: kVendorBlockTypeCodes[_data.getUint8(metaOffset + 1)],
     162           10 :     lastUpdate: _dateValues[_data.getUint16(metaOffset + 2, Endian.little)],
     163              :   );
     164              : 
     165              :   /// Every assignable prefix in the index, upper-case hex, six, seven then
     166              :   /// nine characters long.
     167            4 :   Iterable<String> get prefixes => _prefixes();
     168              : 
     169            2 :   Iterable<String> _prefixes() sync* {
     170            6 :     for (var i = 0; i < _n6; i++) {
     171            2 :       yield _hex(
     172           10 :         _data.getUint32(_maL + i * kVendorIndexRecordSize, Endian.little),
     173              :         6,
     174              :       );
     175              :     }
     176            6 :     for (var i = 0; i < _n7; i++) {
     177            2 :       yield _hex(
     178           10 :         _data.getUint32(_maM + i * kVendorIndexRecordSize, Endian.little),
     179              :         7,
     180              :       );
     181              :     }
     182            6 :     for (var i = 0; i < _n9; i++) {
     183            6 :       final p = _sub + i * kVendorIndexLongRecordSize;
     184              :       // MA-S/IAB keys are stored as the first eight hex digits plus the ninth.
     185            2 :       yield _hex(
     186           14 :         (_data.getUint32(p, Endian.little) << 4) | _data.getUint8(p + 4),
     187              :         9,
     188              :       );
     189              :     }
     190              :   }
     191              : 
     192            2 :   String _hex(int value, int width) =>
     193            6 :       value.toRadixString(16).padLeft(width, '0').toUpperCase();
     194              : }
     195              : 
     196            2 : int _hexValue(int codeUnit) {
     197            6 :   if (codeUnit >= 0x30 && codeUnit <= 0x39) return codeUnit - 0x30; // 0-9
     198            6 :   if (codeUnit >= 0x41 && codeUnit <= 0x46) return codeUnit - 0x37; // A-F
     199            0 :   return codeUnit - 0x57; // a-f
     200              : }
     201              : 
     202              : const _utf8Decoder = Utf8Decoder(allowMalformed: true);
     203              : 
     204              : PackedVendorIndex? _vendorIndexInstance;
     205              : 
     206              : /// The vendor index, decoded on first access and cached for the isolate.
     207            5 : PackedVendorIndex get vendorIndex =>
     208            5 :     _vendorIndexInstance ??= PackedVendorIndex.decode();
        

Generated by: LCOV version 2.0-1