ASN1ObjectIdentifier.fromBytes constructor

ASN1ObjectIdentifier.fromBytes(
  1. Uint8List bytes
)

Instantiate a ASN1ObjectIdentifier from the given bytes.

Implementation

ASN1ObjectIdentifier.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
  // ignore the first 2 bytes because they are the tag and the length
  var subBytes = bytes.sublist(2, bytes.length);
  var value = 0;
  var first = true;
  BigInt? bigValue;
  var list = <int>[];
  var objId = StringBuffer();
  for (var i = 0; i != subBytes.length; i++) {
    var b = subBytes[i] & 0xff;

    if (value < 0x80000000000000) {
      value = value * 128 + (b & 0x7f);
      if ((b & 0x80) == 0) {
        if (first) {
          switch (value ~/ 40) {
            case 0:
              list.add(0);
              objId.write('0');
              break;
            case 1:
              list.add(1);
              objId.write('1');
              value -= 40;
              break;
            default:
              list.add(2);
              objId.write('2');
              value -= 80;
          }
          first = false;
        }
        list.add(value);
        objId.write('.');
        objId.write(value);
        value = 0;
      }
    } else {
      bigValue ??= BigInt.from(value);
      bigValue = bigValue << (7);
      bigValue = bigValue | BigInt.from(b & 0x7f);
      if ((b & 0x80) == 0) {
        objId.write('.');
        objId.write(bigValue);
        bigValue = null;
        value = 0;
      }
    }
  }

  oi = list;
  identifier = objId.toString();
}