nextObject method

ASN1Object nextObject()

Return the next ASN1Object in the stream

Implementation

ASN1Object nextObject() {
  var tag = _bytes[_position]; // get current tag in stream

  // This is a special case where the tag value does not fit into
  // the lower 5 bits of the tag. We dont really handle
  // this right now other than just to wrap the bytes as an ASN1Object
  if ((tag & 0x1f) == 0x1f) {
    return ASN1Object.fromBytes(_bytes);
  }
  // decode the length, and use this to create a view into the
  // byte stream that contains the next object
  var (length, vsp) =
      ASN1Object.decodeLength(_bytes.sublist(_position), offset: 1);
  var len = length + vsp;

  // create a view into the larger stream that includes the remaining un-parsed bytes
  var offset = _position + _bytes.offsetInBytes;
  var subBytes = Uint8List.view(_bytes.buffer, offset, len);
  //print("parser _bytes=${_bytes} position=${_position} len=$l  bytes=${hex(subBytes)}");

  final ASN1Object obj;

  switch (tag & 0xc0) {
    // get highest 2 bits - these are the type
    case 0:
      obj = _doPrimitive(tag, subBytes);
      break;
    case APPLICATION_CLASS:
      // LDAP tags are APPLICATION specific. Need to parse sequences
      if (isConstructed(tag)) {
        obj = ASN1Sequence.fromBytes(subBytes);
        break;
      }
      obj = ASN1Application.fromBytes(subBytes);
      break;
    case PRIVATE_CLASS:
      obj = ASN1Private.fromBytes(subBytes);
      break;
    case CONTEXT_SPECIFIC_CLASS:
      obj = ASN1Object.fromBytes(subBytes);
      break;
    default:
      throw UnimplementedError();
  }

  _position = _position + obj.totalEncodedByteLength;
  return obj;
}