ASN1ObjectIdentifier.fromBytes constructor

ASN1ObjectIdentifier.fromBytes(
  1. Uint8List encodedBytes
)

Creates an ASN1ObjectIdentifier entity from the given encodedBytes.

Implementation

ASN1ObjectIdentifier.fromBytes(Uint8List super.encodedBytes)
    : super.fromBytes() {
  var value = 0;
  var first = true;
  BigInt? bigValue;
  var list = <int>[];
  var sb = StringBuffer();
  for (var element in valueBytes!) {
    var b = element & 0xff;
    if (value < 0x80000000000000) {
      value = value * 128 + (b & 0x7f);
      if ((b & 0x80) == 0) {
        if (first) {
          var truncated = value ~/ 40;
          if (truncated < 2) {
            list.add(truncated);
            sb.write(truncated);
            value -= truncated * 40;
          } else {
            list.add(2);
            sb.write('2');
            value -= 80;
          }
          first = false;
        }
        list.add(value);
        sb.write('.$value');
        value = 0;
      }
    } else {
      bigValue ??= BigInt.from(value);
      bigValue = bigValue << (7);
      bigValue = bigValue | BigInt.from(b & 0x7f);
      if ((b & 0x80) == 0) {
        sb.write('.$bigValue');
        bigValue = null;
        value = 0;
      }
    }
  }
  objectIdentifierAsString = sb.toString();
  objectIdentifier = Uint8List.fromList(list);
  var identifier =
      ObjectIdentifiers.getIdentifierByIdentifier(objectIdentifierAsString);
  if (identifier != null) {
    readableName = identifier['readableName'] as String?;
  }
}