parse static method

ASN1BitString parse(
  1. MutableIterable bytes
)
override

Implementation

static ASN1BitString parse(MutableIterable bytes) {
  if (bytes.length < 3) {
    throw Exception('Invalid data!');
  }

  int tag = bytes.first;
  if (tag != ASN1Type.bitStringTag) {
    throw Exception('Invalid tag!');
  }
  bytes.mutate = bytes.skip(1);

  final lengthBigInt = ASN1Object.decodeLength(bytes);
  int length = lengthBigInt.toInt();
  if (length == 0) {
    throw Exception('Invalid data');
  }

  if (bytes.length < length) {
    throw Exception('Invalid data');
  }
  Iterable<int> contentBytes = bytes.take(length);
  bytes.mutate = bytes.skip(length);

  int unusedBytes = contentBytes.first;
  contentBytes = contentBytes.skip(1);

  if (contentBytes.isEmpty && unusedBytes != 0) {
    throw Exception('Unused bytes is not 0 while bitstring is empty');
  }

  return ASN1BitString(Uint8List.fromList(contentBytes.toList()),
      unusedBits: unusedBytes);
}