ber_len_t - Linux


ber_len_t retrieves the length of an encoded ASN.1 BER or DER object. It is used to determine the amount of memory needed to hold the object.


ber_len_t ber_len_t(const void *buf);


  • buf: A pointer to the encoded object.




Retrieve the length of an encoded integer

#include <ber_decode.h>
int main() {
    uint8_t buf[] = {0x02, 0x01, 0x05};
    ber_len_t len = ber_len_t(buf);
    printf("Length of encoded integer: %d\n", len);
    return 0;

Handle indefinite length encodings

#include <ber_decode.h>
int main() {
    uint8_t buf[] = {0x80, 0x03, 0x01, 0x02, 0xFF};
    ber_len_t len = ber_len_t(buf);
    printf("Length of indefinite length encoding: %d\n", len);
    return 0;

Common Issues

  • Errors can occur if the buffer pointed to by buf does not contain a valid BER/DER encoded object.


ber_len_t can be combined with other BER/DER decoding functions to decode and parse objects. For example, it can be used to calculate the size of a structure that needs to be allocated to hold the decoded object.

Related Commands

  • ber_decode_sequence: Decodes a BER/DER sequence.
  • ber_decode_integer: Decodes a BER/DER integer.
  • ber_decode_string: Decodes a BER/DER string.