BER_ADD_STRING(3)

NAME
ber_get_element, ber_add_sequence, ber_add_set, ber_add_null, ber_add_eoc, ber_add_integer, ber_add_enumerated, ber_add_boolean, ber_add_string, ber_add_nstring, ber_add_ostring, ber_add_bitstring, ber_add_oid, ber_add_noid, ber_add_oidstring, ber_printf_elements - create ASN.1 objects for BER encoding

SYNOPSIS
#include <sys/types.h>
#include <ber.h>
struct ber_element *
ber_get_element(unsigned int encoding);
struct ber_element *
ber_add_sequence(struct ber_element *prev);
struct ber_element *
ber_add_set(struct ber_element *prev);
struct ber_element *
ber_add_null(struct ber_element *prev);
struct ber_element *
ber_add_eoc(struct ber_element *prev);
struct ber_element *
ber_add_integer(struct ber_element *prev, long long val);
struct ber_element *
ber_add_enumerated(struct ber_element *prev, long long val);
struct ber_element *
ber_add_boolean(struct ber_element *prev, int bool);
struct ber_element *
ber_add_string(struct ber_element *prev, const char *string);
struct ber_element *
ber_add_nstring(struct ber_element *prev, const char *string, size_t size);
struct ber_element *
ber_add_ostring(struct ber_element *prev, struct ber_octetstring *ostring);
struct ber_element *
ber_add_bitstring(struct ber_element *prev, const void *buf, size_t size);
struct ber_element *
ber_add_oid(struct ber_element *prev, struct ber_oid *oid);
struct ber_element *
ber_add_noid(struct ber_element *prev, struct ber_oid *oid, int n);
struct ber_element *
ber_add_oidstring(struct ber_element *prev, const char *string);
struct ber_element *
ber_printf_elements(struct ber_element *prev, char *format, ...);

DESCRIPTION
Intermediary storage of BER elements during encoding and decoding uses the following structure:

    struct ber_element {
    	struct ber_element	*be_next;
    	unsigned int		 be_type;
    	unsigned int		 be_encoding;
    	size_t			 be_len;
    	off_t			 be_offs;
    	int			 be_free;
    	u_int8_t		 be_class;
    	void			(*be_cb)(void *, size_t);
    	void			*be_cbarg;
    	union {
    		struct ber_element	*bv_sub;
    		void			*bv_val;
    		long long		 bv_numeric;
    	} be_union;
    #define be_sub		be_union.bv_sub
    #define be_val		be_union.bv_val
    #define be_numeric	be_union.bv_numeric
    };

ber_get_element() creates a new ber_element with default values, dynamically allocates required storage, and sets be_encoding to encoding.

The ber_add_*() functions allocate a new ber_element of the respective type. If prev is an empty sequence or set, they put the new element into that sequence or set. Otherwise, unless prev is NULL, they put it behind prev. Those functions taking a second argument initialize the content of the new element from the second argument.

ber_printf_elements() creates zero or more ber_element structures. For each byte in fmt, arguments of the the types given in the following table are consumed and passed to the listed function, creating one ber_element per byte. The following bytes are valid:
byte | function | arguments
B | ber_add_bitstring() | 2: void *, size_t
b | ber_add_boolean() | 1: int
d | ber_add_integer() | 1: int
E | ber_add_enumerated() | 1: long long
e | see below | 1: struct ber_element *
i | ber_add_integer() | 1: long long
O | ber_add_oid() | 1: struct ber_oid *
o | ber_add_oidstring() | 1: char *
s | ber_add_string() | 1: char *
t | ber_set_header(3) | 2: int, unsigned int
x | ber_add_nstring() | 2: char *, size_t
(| ber_add_set() | 0
) | see below | 0
. | ber_add_eoc() | 0
0 | ber_add_null() | 0
{ | ber_add_sequence() | 0
} | see below | 0

The ‘e’ and ‘t’ bytes are special in so far as they do not create new elements. The ‘e’ byte adds an element that was already created earlier into or behind the previous element, or into and behind ber if the ‘e’ is the first byte in fmt, just like the ber_add_*() functions would add a new element. The ‘t’ byte changes the class and type of the last element, or of ber if ‘t’ is the first byte in fmt, without changing its position relative to other elements.

A closing brace or parenthesis closes an open sequence or set, if any, such that the next element will be added behind rather than into the sequence or set. Only one sequence or set can be open at any time. Nesting is not supported without multiple function calls.

RETURN VALUES
Upon successful completion, these functions return a pointer to a populated ber_element. Otherwise NULL is returned and the global variable errno is set to indicate the error.

ber_printf_elements() returns NULL without setting errno if fmt is an empty string and ber is NULL.

SEE ALSO
ber_get_string(3), ber_oid_cmp(3), ber_read_elements(3), ber_set_header(3)

STANDARDS
ITU-T Recommendation X.690, also known as ISO/IEC 8825-1: Information technology - ASN.1 encoding rules.

HISTORY
These functions first appeared as internal functions in snmpd(8) in OpenBSD 4.2 and were moved to libutil in OpenBSD 6.6.

AUTHORS
-nosplit The BER library was written by Claudio Jeker <claudio@openbsd.org>, Marc Balmer <marc@openbsd.org> and Reyk Floeter <reyk@openbsd.org>.