MAN page from RedHat Other ldap-devel-3.3-3.i386.rpm
LBER-ENCODE
Section: C Library Functions (3)
Updated: 15 June 1992
Index NAME
ber_alloc, ber_flush, ber_printf, ber_put_int, ber_put_ostring, ber_put_string, ber_put_null, ber_put_boolean, ber_put_bitstring, ber_start_seq, ber_start_set, ber_put_seq, ber_put_set - LBER simplified Basic Encoding Rules library routines for encoding
SYNOPSIS
#include <lber.h>
typedef struct berelement { char *ber_buf; char *ber_ptr; char *ber_end; struct seqorset *ber_sos; int ber_tag; int ber_usertag;} BerElement;typedef struct sockbuf { int sb_sd; BerElement sb_ber;} Sockbuf;BerElement *ber_alloc()
ber_flush(sb, ber, freeit)Sockbuf *sb;BerElement *ber;int freeit;
ber_printf(ber, fmt [, arg... ] )BerElement *ber;char *fmt;
ber_put_int(ber, num, tag)BerElement *ber;long num;char tag;
ber_put_ostring(ber, str, len, tag)BerElement *ber;char *str;unsigned long len;char tag;
ber_put_string(ber, str, tag)BerElement *ber;char *str;char tag;
ber_put_null(ber, tag)BerElement *ber;char tag;
ber_put_boolean(ber, bool, tag)BerElement *ber;int bool;char tag;
ber_put_bitstring(ber, str, blen, tag)BerElement *ber;char *str;int blen;char tag;
ber_start_seq(ber, tag)BerElement *ber;char tag;
ber_start_set(ber, tag)BerElement *ber;char tag;
ber_put_seq(ber)BerElement *ber;
ber_put_set(ber)BerElement *ber;
DESCRIPTION
These routines provide a subroutine interface to a simplifiedimplementation of the Basic Encoding Rules of ASN.1. The versionof BER these routines support is the one defined for the LDAPprotocol. The encoding rules are the same as BER, except that only definite form lengths are used, and bitstrings and octet stringsare always encoded in primitive form. In addition, these lightweightBER routines restrict tags and class to fit in a single octet (thismeans the actual tag must be less than 31). When a "tag" is specifiedin the descriptions below, it refers to the tag, class, and primitiveor constructed bit in the first octet of the encoding. Thisman page describes the encoding routines in the lber library. Seelber-decode(3) for details on the corresponding decoding routines.
Normally, the only routines that need be called by an applicationare ber_alloc() to allocate a BER element for encoding, ber_printf()to do the actual encoding, and ber_flush() to actually write theelement. The other routines are provided for thoseapplications that need more control than ber_printf() provides. Ingeneral, these routines return the length of the element encoded, or-1 if an error occurred.
The ber_alloc() routine is used to allocate a new BER element. Theber_flush() routine is used to actually write the element to a socket(or file) descriptor, once it has been fully encoded (using ber_printf()and friends). The sb structure contains the descriptor and aBerElement used for input buffering. Only the sb_sd field is relevantto the ber_flush() routine.
The ber_printf() routine is used to encode a BER element in much thesame way that sprintf(3) works. One important difference, though, isthat some state information is kept with the ber parameter sothat multiple calls can be made to ber_printf() to append things tothe end of the BER element. Ber_printf() writes to ber, a pointer to aBerElement such as returned by ber_alloc(). It interprets andformats its arguments according to the format string fmt.The format string can contain the following characters:
- b
- Boolean. An integer parameter should be supplied. A boolean elementis output.
- i
- Integer. An integer parameter should be supplied. An integer elementis output.
- B
- Bitstring. A char * pointer to the start of the bitstring is supplied,followed by the number of bits in the bitstring. A bitstring elementis output.
- n
- Null. No parameter is required. A null element is output.
- o
- Octet string. A char * is supplied, followed by the length of thestring pointed to. An octet string element is output.
- s
- Octet string. A null-terminated string is supplied. An octet stringelement is output, not including the trailing NULL octet.
- t
- Tag. An int specifying the tag to give the next element is provided.This works across calls.
- v
- Several octet strings. A null-terminated array of char *'s issupplied. Note that a construct like '{v}' is required to getan actual SEQUENCE OF octet strings.
- {
- Begin sequence. No parameter is required.
- }
- End sequence. No parameter is required.
- [
- Begin set. No parameter is required.
- ]
- End set. No parameter is required.
The ber_put_int() routine writes the integer element num tothe BER element ber.
The ber_put_boolean() routine writes the boolean value given bybool to the BER element.
The ber_put_bitstring() routine writes blen bits startingat str as a bitstring value to the given BER element. Notethat blen is the length in bits of the bitstring.
The ber_put_ostring() routine writes len bytes starting atstr to the BER element as an octet string.
The ber_put_string() routine writes the null-terminated string (minusthe terminating ' ') to the BER element as an octet string.
The ber_put_null() routine writes a NULL element to the BER element.
The ber_start_seq() routine is used to start a sequence in the BERelement. The ber_start_set() routine works similarly.The end of the sequence or set is marked by the nearest matchingcall to ber_put_seq() or ber_put_set(), respectively.
The ber_first_element() routine is used to return the tag and lengthof the first element in a set or sequence. It also returns in cookiea magic cookie parameter that should be passed to subsequent calls tober_next_element(), which returns similar information.
EXAMPLES
Assuming the following variable declarations, and that the variableshave been assigned appropriately, an lber encoding ofthe following ASN.1 object:
AlmostASearchRequest := SEQUENCE { baseObject DistinguishedName, scope ENUMERATED { baseObject (0), singleLevel (1), wholeSubtree (2) }, derefAliases ENUMERATED { neverDerefaliases (0), derefInSearching (1), derefFindingBaseObj (2), alwaysDerefAliases (3) }, sizelimit INTEGER (0 .. 65535), timelimit INTEGER (0 .. 65535), attrsOnly BOOLEAN, attributes SEQUENCE OF AttributeType }can be achieved like so:
int scope, ali, size, time, attrsonly; char *dn, **attrs; /* ... fill in values ... */ if ( (ber = ber_alloc()) == NULLBER ) /* error */ if ( ber_printf( ber, "{siiiib{v}}", dn, scope, ali, size, time, attrsonly, attrs ) == -1 ) /* error */ else /* success */ ERRORS
If an error occurs during encoding, generally these routines return -1.
NOTES
The return values for all of these functions are declared in the<lber.h> header file.
SEE ALSO
ldap-async(3)
ldap-sync(3)
ldap-parse(3)
lber-decode(3)
Yeong, W., Howes, T., and Hardcastle-Kille, S., "Lightweight Directory AccessProtocol", OSI-DS-26, April 1992.
Information Processing - Open Systems Interconnection - Model and Notation -Service Definition - Specification of Basic Encoding Rules for AbstractSyntax Notation One, International Organization for Standardization,International Standard 8825.
AUTHOR
Tim Howes, University of Michigan
Index
- NAME
- SYNOPSIS
- DESCRIPTION
- EXAMPLES
- ERRORS
- NOTES
- SEE ALSO
- AUTHOR
This document was created byman2html,using the manual pages.