nextObject method

ASN1Object nextObject()

Parses the next object in the bytes.

Implementation

ASN1Object nextObject() {
  // Get the current tag in the list bytes
  var tag = bytes![_position];

  // Get the length of the value bytes for the current object
  var length = ASN1Utils.decodeLength(bytes!.sublist(_position));

  var valueStartPosition =
      ASN1Utils.calculateValueStartPosition(bytes!.sublist(_position));

  var isIndefiniteLength = false;

  if (length == -1) {
    length = ASN1Utils.calculateIndefiniteLength(bytes!, _position) + 2;
    isIndefiniteLength = true;
  } else if (_position < length + valueStartPosition) {
    length = length + valueStartPosition;
  } else {
    length = bytes!.length - _position;
  }

  // Create new view from the bytes
  var offset = _position + bytes!.offsetInBytes;
  var subBytes = Uint8List.view(bytes!.buffer, offset, length);

  // Parse the view and the tag to an ASN1Object
  var isConstructed = ASN1Utils.isConstructed(tag);
  var isPrimitive = (0xC0 & tag) == 0;
  //var isApplication = (0x40 & tag) != 0;

  ASN1Object obj;
  if (isConstructed) {
    obj = _createConstructed(tag, subBytes);
  } else if (isPrimitive) {
    obj = _createPrimitive(tag, subBytes);
  } else {
    // create a vanilla object
    obj = ASN1Object.fromBytes(subBytes);
  }

  // Update the position
  _position =
      _position + obj.totalEncodedByteLength + (isIndefiniteLength ? 2 : 0);
  return obj;
}