def writeObjectIdentifier(oid): """ Pack a PER object identifier :param oid: object identifier (tuple of 6 integers) :type oid: (int, int, int, int, int, int) :return: str """ return writeLength(5) + Uint8.pack((oid[0] << 4) & (oid[1] & 0x0f)) + b"".join(Uint8.pack(b) for b in oid[2 :])
def writeLength(length: int) -> bytes: """ Pack structure length as expected in BER specification :param length: structure length. """ if length > 0x7f: return Uint8.pack(0x82) + Uint16BE.pack(length) else: return Uint8.pack(length)
def writeApplicationTag(tag, size): """ Pack an application tag. :param tag: application tag. :type tag: Tag :param size: the size of the application packet. :type size: int """ if tag > 30: return Uint8.pack((Class.BER_CLASS_APPL | PC.BER_CONSTRUCT) | Tag.BER_TAG_MASK) + Uint8.pack(tag) + writeLength(size) else: return Uint8.pack((Class.BER_CLASS_APPL | PC.BER_CONSTRUCT) | (Tag.BER_TAG_MASK & tag)) + writeLength(size)
def writeBoolean(value: bool) -> bytes: """ Pack a BER boolean """ boolean = Uint8.pack(0xff if value else 0) return writeUniversalTag(Tag.BER_TAG_BOOLEAN, False) + writeLength(1) + boolean
def writeEnumeration(value): """ Pack a BER enumeration value :type value: int :return: str """ return writeUniversalTag(Tag.BER_TAG_ENUMERATED, False) + writeLength(1) + Uint8.pack(value)
def writeNumericString(string: str, minValue: int) -> bytes: """ Pack a PER numeric string :param string: numeric string :param minValue: minimum string length """ length = len(string) mlength = minValue if length >= minValue: mlength = length - minValue result = b"" for i in range(0, length, 2): c1 = ord(string[i : i + 1]) if i + 1 < length: c2 = ord(string[i + 1 : i + 2]) else: c2 = 0x30 c1 = (c1 - 0x30) % 10 c2 = (c2 - 0x30) % 10 result += Uint8.pack((c1 << 4) | c2) return writeLength(mlength) + result
def writeChoice(choice): """ Pack a PER choice :param choice: choice value :type choice: int :return: str """ return Uint8.pack(choice)
def writeNumberOfSet(numberOfSet): """ Pack a PER NumberOfSet :param numberOfSet: NumberOfSet value :type numberOfSet: int :return: str """ return Uint8.pack(numberOfSet)
def writeEnumeration(enum): """ Pack a PER enumeration :param enum: enumeration value :type enum: int :return: str """ return Uint8.pack(enum)
def writeBoolean(value): """ Pack a BER boolean :type value: bool :return: str """ boolean = Uint8.pack(0xff if value else 0) return writeUniversalTag(Tag.BER_TAG_BOOLEAN, False) + writeLength(1) + boolean
def writeLength(value: int) -> bytes: """ Pack a PER length indicator """ if value > 0x7f: return Uint16BE.pack(value | 0x8000) else: return Uint8.pack(value)
def writeSelection(selection): """ Pack a PER selection :param selection: selection value :type selection: int :return: str """ return Uint8.pack(selection)
def writeUniversalTag(tag: Tag, isConstruct: bool) -> bytes: """ Pack universal tag. :param tag: BER tag :param isConstruct: True if the structure is a construct """ return Uint8.pack((Class.BER_CLASS_UNIV | berPC(isConstruct)) | (Tag.BER_TAG_MASK & tag))
def writeLength(value): """ Pack a PER length indicator :type value: int :return: str """ if value > 0x7f: return Uint16BE.pack(value | 0x8000) else: return Uint8.pack(value)
def writeInteger(value: int) -> bytes: """ Pack a PER integer """ if value <= 0xff: return writeLength(1) + Uint8.pack(value) elif value < 0xffff: return writeLength(2) + Uint16BE.pack(value) else: return writeLength(4) + Uint32BE.pack(value)
def writeInteger(value): """ Pack a BER integer :type value: int :return: str """ if value <= 0xff: return writeUniversalTag(Tag.BER_TAG_INTEGER, False) + writeLength(1) + Uint8.pack(value) elif value <= 0xffff: return writeUniversalTag(Tag.BER_TAG_INTEGER, False) + writeLength(2) + Uint16BE.pack(value) else: return writeUniversalTag(Tag.BER_TAG_INTEGER, False) + writeLength(4) + Uint32BE.pack(value)
def writeInteger(value): """ Pack a PER integer :type value: int :return: str """ if value <= 0xff: return writeLength(1) + Uint8.pack(value) elif value < 0xffff: return writeLength(2) + Uint16BE.pack(value) else: return writeLength(4) + Uint32BE.pack(value)
def writeEnumeration(enum: int) -> bytes: """ Pack a PER enumeration :param enum: enumeration value """ return Uint8.pack(enum)
def writeNumberOfSet(numberOfSet: int) -> bytes: """ Pack a PER NumberOfSet :param numberOfSet: NumberOfSet value """ return Uint8.pack(numberOfSet)
def writeSelection(selection: int) -> bytes: """ Pack a PER selection :param selection: selection value """ return Uint8.pack(selection)
def writeChoice(choice: int) -> bytes: """ Pack a PER choice :param choice: choice value """ return Uint8.pack(choice)
def writeEnumeration(value: int) -> bytes: """ Pack a BER enumeration value """ return writeUniversalTag(Tag.BER_TAG_ENUMERATED, False) + writeLength(1) + Uint8.pack(value)
def writeObjectIdentifier(oid: Tuple[int, int, int, int, int, int]) -> bytes: """ Pack a PER object identifier :param oid: object identifier (tuple of 6 integers) """ return writeLength(5) + Uint8.pack((oid[0] << 4) & (oid[1] & 0x0f)) + b"".join(Uint8.pack(b) for b in oid[2 :])