All files / src/codec/ber/decoder/primitive ReadObjectIdentifier.ts

90.9% Statements 20/22
75% Branches 6/8
100% Functions 1/1
90.47% Lines 19/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56    1x               2x 2x 2x 18x 18x   12x           18x         18x 18x   12x 12x                           2x     2x 2x 2x 2x 2x 2x    
import {CursorDataView} from '../../../../CursorDataView';
 
export function readObjectIdentifier(cdv: CursorDataView, length: number) {
  // encoding of an ordered list of subidentifiers, concatenated together (8.19.2)
 
  // Each subidentifier is represented as a series of (one or more) octets.
 
  // Bit 8 of each octet indicates whether it is the last in the series:
  // - bit 8 of the last octet is zero; bit 8 of each preceding octet is one.
  // - Bits 7 to 1 of the octets in the series collectively encode the subidentifier.
  const array = [] as number[];
  let subIdentifierValue = 0;
  for (let i = 0; i < length; i++) {
    const v = cdv.read();
    if (subIdentifierValue === 0) {
      // leading octet.
      Iif (v === 0x80) {
        throw new Error(
          'The subidentifier shall be encoded in the fewest possible octets. (8.19.2)'
        );
      }
    }
    const value = v & 0b0111_1111;
    // Conceptually, these groups of bits are concatenated to form an
    // unsigned binary number whose most significant bit is bit 7 of
    // the first octet and whose least significant bit is bit 1
    // of the last octet.
    subIdentifierValue = subIdentifierValue * 128 + value;
    if ((v & 0b1000_0000) === 0) {
      // last octet
      array.push(subIdentifierValue);
      subIdentifierValue = 0;
    }
  }
 
  // (8.19.3) The number of subidentifiers (N) shall be one less than
  // the number of object identifier components in the
  // object identifier value being encoded.
 
  // (8.19.4) The numerical value of the first subidentifier is derived
  // from the values of the first two object identifiercomponents
  // in the object identifier value being encoded, using the formula:
  // (X*40) + Y
  // where X is the value of the first object identifier component and Y is the value of the second object identifier
  // component.
  Iif (array.length === 0) {
    throw new Error('Object identifier must have at least one subidentifier');
  }
  const firstSubIdentifier = array.shift() as number;
  const y = firstSubIdentifier % 40;
  const x = (firstSubIdentifier - y) / 40;
  array.unshift(y);
  array.unshift(x);
  return array.join('.');
}