parseType2Message function Null safety

Type2Message parseType2Message(
  1. String rawMsg,
  2. {String headerPrefix = kHeaderPrefixNTLM}
)

Extract the information from the type 2 rawMsg into an object.

Implementation

Type2Message parseType2Message(
  String rawMsg, {
  String headerPrefix = kHeaderPrefixNTLM,
}) {
  if (rawMsg.startsWith('$headerPrefix ')) {
    rawMsg = rawMsg.substring('$headerPrefix '.length);
  }

  final buf = base64Decode(rawMsg).buffer;
  final bufView = ByteData.view(buf);

  final signature = buf.asUint8List(0, 8);
  final type = bufView.getInt16(8, Endian.little);

  if (type != 2) {
    throw ArgumentError('A type 2 response was not passed!');
  }

  final targetNameLength = bufView.getInt16(12, Endian.little);
  final targetNameMaxLength = bufView.getInt16(14, Endian.little);
  final targetNameOffset = bufView.getInt32(16, Endian.little);
  final targetName = buf.asUint8List(targetNameOffset, targetNameLength);

  final negotiateFlags = bufView.getInt32(20, Endian.little);
  final serverChallenge = buf.asUint8List(24, 8);
  final reserved = buf.asUint8List(32, 8);

  int? targetInfoLength;
  int? targetInfoMaxLength;
  int? targetInfoOffset;
  Uint8List? targetInfo;
  if (negotiateFlags & flags.NTLM_NegotiateTargetInfo != 0) {
    targetInfoLength = bufView.getInt16(40, Endian.little);
    targetInfoMaxLength = bufView.getInt16(42, Endian.little);
    targetInfoOffset = bufView.getInt32(44, Endian.little);
    targetInfo = buf.asUint8List(targetInfoOffset, targetInfoLength);
  }

  return Type2Message(
    signature: signature,
    type: type,
    targetNameLength: targetNameLength,
    targetNameMaxLength: targetNameMaxLength,
    targetNameOffset: targetNameOffset,
    targetName: targetName,
    negotiateFlags: negotiateFlags,
    serverChallenge: serverChallenge,
    reserved: reserved,
    targetInfoLength: targetInfoLength,
    targetInfoMaxLength: targetInfoMaxLength,
    targetInfoOffset: targetInfoOffset,
    targetInfo: targetInfo,
  );
}