LDAP Library Functions berdecode(3LDAP)
NAME
berdecode, beralloct, berfree, berbvdup, berinit,
berflatten, bergetnext, berskiptag, berpeektag,
berscanf, bergetint, bergetstringa, bergetstringal,
bergetstringb, bergetnull, bergetboolean,
bergetbitstring, berfirstelement, bernextelement,
berbvfree, berbvecfree - Basic Encoding Rules library
decoding functions
SYNOPSIS
cc[ flag... ] file... -lldap[ library... ]
#include
BerElement *beralloct(int options);
struct berval *berbvdup(struct berval *bv);
void berfree(BerElement *ber, int freebuf);
BerElement *berinit(struct berval *bv);
int berflatten(BerElement *ber, struct berval **bvPtr);
bergetnext(Sockbuf *sb, unsigned long *len, char *bvval);
berskiptag(BerElement **ber, unsigned long **len);
berpeektag(BerElement **ber, unsigned long **len);
bergetint(BerElement **ber, long **num);
bergetstringb(BerElement **ber, char **buf,
unsigned long **len);
bergetstringa(BerElement **ber, char ***buf);
bergetstringal(BerElement **ber, struct berval ***bv);
bergetnull(BerElement **ber);
SunOS 5.11 Last change: 27 Jan 2002 1
LDAP Library Functions berdecode(3LDAP)
bergetboolean(BerElement **ber, int **bool);
bergetbitstringa(BerElement **ber, char ***buf,
unsigned long **blen);
berfirstelement(BerElement **ber, unsigned long **len,
char ***cookie);
bernextelement(BerElement **ber, unsigned long **len,
char **cookie);
berscanf(BerElement **ber, char **fmt [, arg...]);
berbvfree(struct berval **bv);
berbvecfree(struct berval ***bvec);
DESCRIPTION
These functions provide a subfunction interface to a simpli-
fied implementation of the Basic Encoding Rules of ASN.1.
The version of BER these functions support is the one
defined for the LDAP protocol. The encoding rules are the
same as BER, except that only definite form lengths are
used, and bitstrings and octet strings are always encoded in
primitive form. In addition, these lightweight BER func-
tions restrict tags and class to fit in a single octet (this
means the actual tag must be less than 31). When a "tag"is
specified in the descriptions below, it refers to the tag,
class, and primitive or constructed bit in the first octet
of the encoding. This man page describes the decoding func-
tions in the lber library. See berencode(3LDAP) for
details on the corresponding encoding functions.
Normally, the only functions that need be called by an
application are bergetnext() to get the next BER element
and berscanf() to do the actual decoding. In some cases,
berpeektag() may also need to be called in normal usage.
The other functions are provided for those applications that
need more control than berscanf() provides.In general,
these functions return the tag of the element decoded, or -1
if an error occurred.
SunOS 5.11 Last change: 27 Jan 2002 2
LDAP Library Functions berdecode(3LDAP)
The bergetnext() function is used to read the next BER
element from the given Sockbuf, sb. A Sockbuf consists of
the descriptor (usually socket, but a file descriptor works
just as well) from which to read, and a BerElement structure
used to maintain a buffer. On the first call, the sbber
struct should be zeroed. It strips off and returns the
leading tag byte, strips off and returns the length of the
entire element in len, and sets up ber for subsequent calls
to berscanf(), and all to decode the element.
The berscanf() function is used to decode a BER element in
much the same way that scanf(3C) works. It reads from ber,
a pointer to a BerElement such as returned by
bergetnext(), interprets the bytes according to the format
string fmt, and stores the results in its additional argu-
ments. The format string contains conversion specifications
which are used to direct the interpretation of the BER ele-
ment. The format string can contain the following charac-
ters.
a Octet string. A char ** should be supplied. Memory is
allocated, filled with the contents of the octet
string, null-terminated, and returned in the parameter.
s Octet string. A char * buffer should be supplied, fol-
lowed by a pointer to an integer initialized to the
size of the buffer. Upon return, the null-terminated
octet string is put into the buffer, and the integer is
set to the actual size of the octet string.
O Octet string. A struct berval ** should be supplied,
which upon return points to a memory allocated struct
berval containing the octet string and its length.
berbvfree() can be called to free the allocated
memory.
b Boolean. A pointer to an integer should be supplied.
i Integer. A pointer to an integer should be supplied.
B Bitstring. A char ** should be supplied which will
point to the memory allocated bits, followed by an
unsigned long *, which will point to the length (in
bits) of the bitstring returned.
SunOS 5.11 Last change: 27 Jan 2002 3
LDAP Library Functions berdecode(3LDAP)
n Null. No parameter is required. The element is simply
skipped if it is recognized.
v Sequence of octet strings. A char *** should be sup-
plied, which upon return points to a memory allocated
null-terminated array of char *'s containing the octet
strings. NUL is returned if the sequence is empty.
V Sequence of octet strings with lengths. A struct berval
*** should be supplied, which upon return points to a
memory allocated, null-terminated array of struct ber-
val *'s containing the octet strings and their lengths.
NUL is returned if the sequence is empty.
berbvecfree() can be called to free the allocated
memory.
x Skip element. The next element is skipped.
{ Begin sequence. No parameter is required. The initial
sequence tag and length are skipped.
} End sequence. No parameter is required and no action
is taken.
[ Begin set. No parameter is required. The initial set
tag and length are skipped.
] End set. No parameter is required and no action is
taken.
The bergetint() function tries to interpret the next ele-
ment as an integer, returning the result in num. The tag of
whatever it finds is returned on success, -1 on failure.
The bergetstringb() function is used to read an octet
string into a preallocated buffer. The len parameter should
be initialized to the size of the buffer, and will contain
the length of the octet string read upon return. The buffer
should be big enough to take the octet string value plus a
terminating NUL byte.
SunOS 5.11 Last change: 27 Jan 2002 4
LDAP Library Functions berdecode(3LDAP)
The bergetstringa() function is used to allocate memory
space into which an octet string is read.
The bergetstringal() function is used to allocate memory
space into which an octet string and its length are read.
It takes a struct berval **, and returns the result in this
parameter.
The bergetnull() function is used to read a NUL ele-
ment. It returns the tag of the element it skips over.
The bergetboolean() function is used to read a boolean
value. It is called the same way that bergetint() is
called.
The bergetbitstringa() function is used to read a bit-
string value. It takes a char ** which will hold the allo-
cated memory bits, followed by an unsigned long *, which
will point to the length (in bits) of the bitstring
returned.
The berfirstelement() function is used to return the tag
and length of the first element in a set or sequence. It
also returns in cookie a magic cookie parameter that should
be passed to subsequent calls to bernextelement(), which
returns similar information.
beralloct() constructs and returns BerElement. A null
pointer is returned on error. The options field contains a
bitwise-or of options which are to be used when generating
the encoding of this BerElement. One option is defined and
must always be supplied:
#define LBERUSEDER 0x01
When this option is present, lengths will always be encoded
in the minimum number of octets. Note that this option does
not cause values of sets and sequences to be rearranged in
tag and byte order, so these functions are not suitable for
generating DER output as defined in X.509 and X.680
The berinit function constructs a BerElement and returns
a new BerElement containing a copy of the data in the bv
SunOS 5.11 Last change: 27 Jan 2002 5
LDAP Library Functions berdecode(3LDAP)
argument. berinit returns the null pointer on error.
berfree() frees a BerElement which is returned from the
API calls beralloct() or berinit(). Each BerElement
must be freed by the caller. The second argument freebuf
should always be set to 1 to ensure that the internal buffer
used by the BER functions is freed as well as the BerEle-
ment container itself.
berbvdup() returns a copy of a berval. The bvval field
in the returned berval points to a different area of memory
as the bvval field in the argument berval. The null
pointer is returned on error (that is, is out of memory).
The berflatten routine allocates a struct berval whose
contents are BER encoding taken from the ber argument. The
bvPtr pointer points to the returned berval, which must be
freed using berbvfree(). This routine returns 0 on suc-
cess and -1 on error.
EXAMPLES
Example 1 Assume the variable ber contains a lightweight BER
encoding of the 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 (3N)
},
sizelimit INTEGER (0 .. 65535),
timelimit INTEGER (0 .. 65535),
attrsOnly BOLEAN,
attributes SEQUENCE OF AttributeType
}
Example 2 The element can be decoded using berscanf() as
follows.
int scope, ali, size, time, attrsonly;
char *dn, **attrs;
SunOS 5.11 Last change: 27 Jan 2002 6
LDAP Library Functions berdecode(3LDAP)
if ( berscanf( ber, "{aiiiib{v}}", &dn, &scope, &ali,
&size, &time, &attrsonly, &attrs ) == -1 )
/* error */
else
/* success */
ERORS
If an error occurs during decoding, generally these func-
tions return -1.
NOTES
The return values for all of these functions are declared in
the header file. Some functions may allocate
memory which must be freed by the calling application.
ATRIBUTES
See attributes(5) for a description of the following attri-
butes:
ATRIBUTE TYPE ATRIBUTE VALUE
Availability SUNWcsl (32-bit)
SUNWcslx (64-bit)
Interface Stability Committed
SEE ALSO
berencode(3LDAP), attributes(5)
Yeong, W., Howes, T., and Hardcastle-Kille, S., "Lightweight
Directory Access Protocol", OSI-DS-26, April 1992.
Information Processing - Open Systems Interconnection -
Model and Notation - Service Definition - Specification of
Basic Encoding Rules for Abstract Syntax Notation One,
International Organization for Standardization, Interna-
tional Standard 8825.
SunOS 5.11 Last change: 27 Jan 2002 7
|