decodeLength static method

(int, int) decodeLength(
  1. Uint8List encodedBytes, {
  2. int offset = 1,
})

Decode the asn1 length field from the encoded bytes

This method has no side effect on an object Returns a record with (length,valueStartPosition). where length is the length in bytes of the object, and value start position is the offset where the values start within the byte array (after the tag and the lenght bytes).

Usually the first byte is the tag followed by the length encoding. offset is where the length bytes start in the byte array. For most objects, this will be right after the tag at position 1.

Implementation

static (int length, int valueStart) decodeLength(Uint8List encodedBytes,
    {int offset = 1}) {
  var valueStartPosition = offset + 1; //default
  var length = encodedBytes[offset] & 0x7F;
  if (length != encodedBytes[offset]) {
    var numLengthBytes = length;

    length = 0;
    for (var i = 0; i < numLengthBytes; i++) {
      length <<= 8;
      length |= encodedBytes[valueStartPosition++] & 0xFF;
    }
  }
  return (length, valueStartPosition);
}