Issue #1258 - Part 2: Use binoc-central version of ldap

This commit is contained in:
Matt A. Tobin 2019-11-03 13:17:15 -05:00 committed by Roy Tam
commit deb86ab699
94 changed files with 44 additions and 18 deletions

102
ldap/c-sdk/liblber/bprint.c Normal file
View file

@ -0,0 +1,102 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/* bprint.c - a printing utility for debuging output */
#include <string.h>
#include "lber-int.h"
#ifdef LDAP_DEBUG
/*
* Print arbitrary stuff, for debugging.
*/
#define BPLEN 48
void
lber_bprint( char *data, int len )
{
static char hexdig[] = "0123456789abcdef";
char out[ BPLEN ];
int i = 0;
memset( out, 0, BPLEN );
for ( ;; ) {
if ( len < 1 ) {
char msg[BPLEN + 80];
sprintf( msg, "\t%s\n", ( i == 0 ) ? "(end)" : out );
ber_err_print( msg );
break;
}
#ifndef HEX
if ( isgraph( (unsigned char)*data )) {
out[ i ] = ' ';
out[ i+1 ] = *data;
} else {
#endif
out[ i ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
out[ i+1 ] = hexdig[ *data & 0x0f ];
#ifndef HEX
}
#endif
i += 2;
len--;
data++;
if ( i > BPLEN - 2 ) {
char msg[BPLEN + 80];
sprintf( msg, "\t%s\n", out );
ber_err_print( msg );
memset( out, 0, BPLEN );
i = 0;
continue;
}
out[ i++ ] = ' ';
}
}
#endif
void ber_err_print( char *data )
{
#ifdef USE_DEBUG_WIN
OutputDebugString( data );
#else
fputs( data, stderr );
fflush( stderr );
#endif
}

833
ldap/c-sdk/liblber/decode.c Normal file
View file

@ -0,0 +1,833 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1990 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
/* decode.c - ber input decoding routines */
#include "lber-int.h"
/*
* Note: ber_get_tag() only uses the ber_end and ber_ptr elements of ber.
* If that changes, the ber_peek_tag() and/or ber_skip_tag() implementations
* will need to be changed.
*/
/* return the tag - LBER_DEFAULT returned means trouble */
ber_tag_t
LDAP_CALL
ber_get_tag( BerElement *ber )
{
unsigned char xbyte;
ber_tag_t tag;
char *tagp;
int i;
if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
return( LBER_DEFAULT );
if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
return( (ber_uint_t) xbyte );
tagp = (char *) &tag;
tagp[0] = xbyte;
for ( i = 1; i < sizeof(ber_int_t); i++ ) {
if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
return( LBER_DEFAULT );
tagp[i] = xbyte;
if ( ! (xbyte & LBER_MORE_TAG_MASK) )
break;
}
/* tag too big! */
if ( i == sizeof(ber_int_t) )
return( LBER_DEFAULT );
/* want leading, not trailing 0's */
return( tag >> (sizeof(ber_int_t) - i - 1) );
}
/*
* Note: ber_skip_tag() only uses the ber_end and ber_ptr elements of ber.
* If that changes, the implementation of ber_peek_tag() will need to
* be changed.
*/
ber_tag_t
LDAP_CALL
ber_skip_tag( BerElement *ber, ber_len_t *len )
{
ber_tag_t tag;
unsigned char lc;
int noctets, diff;
ber_len_t netlen;
/*
* Any ber element looks like this: tag length contents.
* Assuming everything's ok, we return the tag byte (we
* can assume a single byte), and return the length in len.
*
* Assumptions:
* 1) definite lengths
* 2) primitive encodings used whenever possible
*/
/*
* First, we read the tag.
*/
if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
return( LBER_DEFAULT );
/*
* Next, read the length. The first byte contains the length of
* the length. If bit 8 is set, the length is the long form,
* otherwise it's the short form. We don't allow a length that's
* greater than what we can hold in an unsigned long.
*/
*len = netlen = 0;
if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
return( LBER_DEFAULT );
if ( lc & 0x80 ) {
noctets = (lc & 0x7f);
if ( noctets > sizeof(ber_uint_t) )
return( LBER_DEFAULT );
diff = sizeof(ber_int_t) - noctets;
if ( ber_read( ber, (char *) &netlen + diff, noctets )
!= noctets )
return( LBER_DEFAULT );
*len = LBER_NTOHL( netlen );
} else {
*len = lc;
}
return( tag );
}
/*
* Note: Previously, we passed the "ber" parameter directly to ber_skip_tag(),
* saving and restoring the ber_ptr element only. We now take advantage
* of the fact that the only ber structure elements touched by ber_skip_tag()
* are ber_end and ber_ptr. If that changes, this code must change too.
*/
ber_tag_t
LDAP_CALL
ber_peek_tag( BerElement *ber, ber_len_t *len )
{
BerElement bercopy;
bercopy.ber_end = ber->ber_end;
bercopy.ber_ptr = ber->ber_ptr;
return( ber_skip_tag( &bercopy, len ));
}
static int
ber_getnint( BerElement *ber, ber_int_t *num, ber_slen_t len )
{
int i;
ber_int_t value;
unsigned char buffer[sizeof(ber_int_t)];
/*
* The tag and length have already been stripped off. We should
* be sitting right before len bytes of 2's complement integer,
* ready to be read straight into an int. We may have to sign
* extend after we read it in.
*/
if ( len > sizeof(ber_slen_t) )
return( -1 );
/* read into the low-order bytes of netnum */
if ( ber_read( ber, (char *) buffer, len ) != len )
return( -1 );
/* This sets the required sign extension */
if ( len != 0) {
value = 0x80 & buffer[0] ? (-1) : 0;
} else {
value = 0;
}
for ( i = 0; i < len; i++ )
value = (value << 8) | buffer[i];
*num = value;
return( len );
}
ber_tag_t
LDAP_CALL
ber_get_int( BerElement *ber, ber_int_t *num )
{
ber_tag_t tag;
ber_len_t len;
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
return( LBER_DEFAULT );
/*
* len is being demoted to a long here -- possible conversion error
*/
if ( ber_getnint( ber, num, (int)len ) != (ber_slen_t)len )
return( LBER_DEFAULT );
else
return( tag );
}
ber_tag_t
LDAP_CALL
ber_get_stringb( BerElement *ber, char *buf, ber_len_t *len )
{
ber_len_t datalen;
ber_tag_t tag;
#ifdef STR_TRANSLATION
char *transbuf;
#endif /* STR_TRANSLATION */
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
return( LBER_DEFAULT );
if ( datalen > (*len - 1) )
return( LBER_DEFAULT );
/*
* datalen is being demoted to a long here -- possible conversion error
*/
if ( ber_read( ber, buf, datalen ) != (ber_slen_t) datalen )
return( LBER_DEFAULT );
buf[datalen] = '\0';
#ifdef STR_TRANSLATION
if ( datalen > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS )
!= 0 && ber->ber_decode_translate_proc != NULL ) {
transbuf = buf;
++datalen;
if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
0 ) != 0 ) {
return( LBER_DEFAULT );
}
if ( datalen > *len ) {
NSLBERI_FREE( transbuf );
return( LBER_DEFAULT );
}
SAFEMEMCPY( buf, transbuf, datalen );
NSLBERI_FREE( transbuf );
--datalen;
}
#endif /* STR_TRANSLATION */
*len = datalen;
return( tag );
}
ber_tag_t
LDAP_CALL
ber_get_stringa( BerElement *ber, char **buf )
{
ber_len_t datalen, ndatalen;
ber_tag_t tag;
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
return( LBER_DEFAULT );
if ( ((ndatalen = (size_t)datalen + 1) < (size_t) datalen) ||
( datalen > (ber->ber_end - ber->ber_ptr) ) ||
( (*buf = (char *)NSLBERI_MALLOC( (size_t)ndatalen )) == NULL ))
return( LBER_DEFAULT );
/*
* datalen is being demoted to a long here -- possible conversion error
*/
if ( ber_read( ber, *buf, datalen ) != (ber_slen_t) datalen ) {
NSLBERI_FREE( *buf );
*buf = NULL;
return( LBER_DEFAULT );
}
(*buf)[datalen] = '\0';
#ifdef STR_TRANSLATION
if ( datalen > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS )
!= 0 && ber->ber_decode_translate_proc != NULL ) {
++datalen;
if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
!= 0 ) {
NSLBERI_FREE( *buf );
*buf = NULL;
return( LBER_DEFAULT );
}
}
#endif /* STR_TRANSLATION */
return( tag );
}
ber_tag_t
LDAP_CALL
ber_get_stringal( BerElement *ber, struct berval **bv )
{
ber_len_t len, nlen;
ber_tag_t tag;
if ( (*bv = (struct berval *)NSLBERI_MALLOC( sizeof(struct berval) ))
== NULL ) {
return( LBER_DEFAULT );
}
(*bv)->bv_val = NULL;
(*bv)->bv_len = 0;
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
NSLBERI_FREE( *bv );
*bv = NULL;
return( LBER_DEFAULT );
}
if ( ((nlen = (size_t) len + 1) < (size_t)len) ||
( len > (ber->ber_end - ber->ber_ptr) ) ||
(((*bv)->bv_val = (char *)NSLBERI_MALLOC( (size_t)nlen ))
== NULL )) {
NSLBERI_FREE( *bv );
*bv = NULL;
return( LBER_DEFAULT );
}
/*
* len is being demoted to a long here -- possible conversion error
*/
if ( ber_read( ber, (*bv)->bv_val, len ) != (ber_slen_t) len ) {
NSLBERI_FREE( (*bv)->bv_val );
(*bv)->bv_val = NULL;
NSLBERI_FREE( *bv );
*bv = NULL;
return( LBER_DEFAULT );
}
((*bv)->bv_val)[len] = '\0';
(*bv)->bv_len = len;
#ifdef STR_TRANSLATION
if ( len > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS ) != 0
&& ber->ber_decode_translate_proc != NULL ) {
++len;
if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
&len, 1 ) != 0 ) {
NSLBERI_FREE( (*bv)->bv_val );
(*bv)->bv_val = NULL;
NSLBERI_FREE( *bv );
*bv = NULL;
return( LBER_DEFAULT );
}
(*bv)->bv_len = len - 1;
}
#endif /* STR_TRANSLATION */
return( tag );
}
ber_tag_t
LDAP_CALL
ber_get_bitstringa( BerElement *ber, char **buf, ber_len_t *blen )
{
ber_len_t datalen;
ber_tag_t tag;
unsigned char unusedbits;
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
return( LBER_DEFAULT );
--datalen;
if ( (datalen > (ber->ber_end - ber->ber_ptr)) ||
( (*buf = (char *)NSLBERI_MALLOC((size_t)datalen )) == NULL ) )
return( LBER_DEFAULT );
if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
NSLBERI_FREE( *buf );
*buf = NULL;
return( LBER_DEFAULT );
}
/*
* datalen is being demoted to a long here -- possible conversion error
*/
if ( ber_read( ber, *buf, datalen ) != (ber_slen_t) datalen ) {
NSLBERI_FREE( *buf );
*buf = NULL;
return( LBER_DEFAULT );
}
*blen = datalen * 8 - unusedbits;
return( tag );
}
ber_tag_t
LDAP_CALL
ber_get_null( BerElement *ber )
{
ber_len_t len;
ber_tag_t tag;
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
return( LBER_DEFAULT );
if ( len != 0 )
return( LBER_DEFAULT );
return( tag );
}
ber_tag_t
LDAP_CALL
ber_get_boolean( BerElement *ber, ber_int_t *boolval )
{
int rc;
rc = ber_get_int( ber, boolval );
return( rc );
}
ber_tag_t
LDAP_CALL
ber_first_element( BerElement *ber, ber_len_t *len, char **last )
{
/* skip the sequence header, use the len to mark where to stop */
if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
return( LBER_ERROR );
}
*last = ber->ber_ptr + *len;
if ( *last == ber->ber_ptr ) {
return( LBER_END_OF_SEQORSET );
}
return( ber_peek_tag( ber, len ) );
}
ber_tag_t
LDAP_CALL
ber_next_element( BerElement *ber, ber_len_t *len, char *last )
{
if ( ber->ber_ptr == last ) {
return( LBER_END_OF_SEQORSET );
}
return( ber_peek_tag( ber, len ) );
}
/* VARARGS */
ber_tag_t
LDAP_C
ber_scanf( BerElement *ber, const char *fmt, ... )
{
va_list ap;
char *last, *p;
char *s, **ss, ***sss;
struct berval ***bv, **bvp, *bval;
int *i, j;
ber_int_t *l, rc, tag;
ber_tag_t *t;
ber_len_t len;
size_t array_size;
va_start( ap, fmt );
#ifdef LDAP_DEBUG
if ( lber_debug & 64 ) {
char msg[80];
sprintf( msg, "ber_scanf fmt (%s) ber:\n", fmt );
ber_err_print( msg );
ber_dump( ber, 1 );
}
#endif
for ( rc = 0, p = (char *) fmt; *p && rc != LBER_DEFAULT; p++ ) {
switch ( *p ) {
case 'a': /* octet string - allocate storage as needed */
ss = va_arg( ap, char ** );
rc = ber_get_stringa( ber, ss );
break;
case 'b': /* boolean */
i = va_arg( ap, int * );
rc = ber_get_boolean( ber, i );
break;
case 'e': /* enumerated */
case 'i': /* int */
l = va_arg( ap, ber_slen_t * );
rc = ber_get_int( ber, l );
break;
case 'l': /* length of next item */
l = va_arg( ap, ber_slen_t * );
rc = ber_peek_tag( ber, (ber_len_t *)l );
break;
case 'n': /* null */
rc = ber_get_null( ber );
break;
case 's': /* octet string - in a buffer */
s = va_arg( ap, char * );
l = va_arg( ap, ber_slen_t * );
rc = ber_get_stringb( ber, s, (ber_len_t *)l );
break;
case 'o': /* octet string in a supplied berval */
bval = va_arg( ap, struct berval * );
ber_peek_tag( ber, &bval->bv_len );
rc = ber_get_stringa( ber, &bval->bv_val );
break;
case 'O': /* octet string - allocate & include length */
bvp = va_arg( ap, struct berval ** );
rc = ber_get_stringal( ber, bvp );
break;
case 'B': /* bit string - allocate storage as needed */
ss = va_arg( ap, char ** );
l = va_arg( ap, ber_slen_t * ); /* for length, in bits */
rc = ber_get_bitstringa( ber, ss, (ber_len_t *)l );
break;
case 't': /* tag of next item */
t = va_arg( ap, ber_tag_t * );
*t = rc = ber_peek_tag( ber, &len );
break;
case 'T': /* skip tag of next item */
t = va_arg( ap, ber_tag_t * );
*t = rc = ber_skip_tag( ber, &len );
break;
case 'v': /* sequence of strings */
sss = va_arg( ap, char *** );
*sss = NULL;
j = 0;
array_size = 0;
for ( tag = ber_first_element( ber, &len, &last );
tag != LBER_DEFAULT && tag != LBER_END_OF_SEQORSET
&& rc != LBER_DEFAULT;
tag = ber_next_element( ber, &len, last ) ) {
if ( *sss == NULL ) {
/* Make room for at least 15 strings */
*sss = (char **)NSLBERI_MALLOC(16 * sizeof(char *) );
if (!*sss) {
rc = LBER_DEFAULT;
break; /* out of memory - cannot continue */
}
array_size = 16;
} else {
char **save_sss = *sss;
if ( (size_t)(j+2) > array_size) {
/* We'v overflowed our buffer */
*sss = (char **)NSLBERI_REALLOC( *sss, (array_size * 2) * sizeof(char *) );
array_size = array_size * 2;
}
if (!*sss) {
rc = LBER_DEFAULT;
ber_svecfree(save_sss);
break; /* out of memory - cannot continue */
}
}
(*sss)[j] = NULL;
rc = ber_get_stringa( ber, &((*sss)[j]) );
j++;
}
if ( rc != LBER_DEFAULT &&
tag != LBER_END_OF_SEQORSET ) {
rc = LBER_DEFAULT;
}
if ( *sss && (j > 0) ) {
(*sss)[j] = NULL;
}
break;
case 'V': /* sequence of strings + lengths */
bv = va_arg( ap, struct berval *** );
*bv = NULL;
j = 0;
for ( tag = ber_first_element( ber, &len, &last );
tag != LBER_DEFAULT && tag != LBER_END_OF_SEQORSET
&& rc != LBER_DEFAULT;
tag = ber_next_element( ber, &len, last ) ) {
if ( *bv == NULL ) {
*bv = (struct berval **)NSLBERI_MALLOC(
2 * sizeof(struct berval *) );
if (!*bv) {
rc = LBER_DEFAULT;
break; /* out of memory - cannot continue */
}
} else {
struct berval **save_bv = *bv;
*bv = (struct berval **)NSLBERI_REALLOC(
*bv,
(j + 2) * sizeof(struct berval *) );
if (!*bv) {
rc = LBER_DEFAULT;
ber_bvecfree(save_bv);
break; /* out of memory - cannot continue */
}
}
rc = ber_get_stringal( ber, &((*bv)[j]) );
j++;
}
if ( rc != LBER_DEFAULT &&
tag != LBER_END_OF_SEQORSET ) {
rc = LBER_DEFAULT;
}
if ( *bv && (j > 0) ) {
(*bv)[j] = NULL;
}
break;
case 'x': /* skip the next element - whatever it is */
if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
break;
ber->ber_ptr += len;
break;
case '{': /* begin sequence */
case '[': /* begin set */
if ( *(p + 1) != 'v' && *(p + 1) != 'V' )
rc = ber_skip_tag( ber, &len );
break;
case '}': /* end sequence */
case ']': /* end set */
break;
default:
{
char msg[80];
sprintf( msg, "unknown fmt %c\n", *p );
ber_err_print( msg );
}
rc = LBER_DEFAULT;
break;
}
}
va_end( ap );
if (rc == LBER_DEFAULT) {
va_start( ap, fmt );
for ( p--; fmt < p && *fmt; fmt++ ) {
switch ( *fmt ) {
case 'a': /* octet string - allocate storage as needed */
ss = va_arg( ap, char ** );
NSLBERI_FREE(*ss);
*ss = NULL;
break;
case 'b': /* boolean */
i = va_arg( ap, int * );
break;
case 'e': /* enumerated */
case 'i': /* int */
l = va_arg( ap, ber_slen_t * );
break;
case 'l': /* length of next item */
l = va_arg( ap, ber_slen_t * );
break;
case 'n': /* null */
break;
case 's': /* octet string - in a buffer */
s = va_arg( ap, char * );
l = va_arg( ap, ber_slen_t * );
break;
case 'o': /* octet string in a supplied berval */
bval = va_arg( ap, struct berval * );
if (bval->bv_val) NSLBERI_FREE(bval->bv_val);
memset(bval, 0, sizeof(struct berval));
break;
case 'O': /* octet string - allocate & include length */
bvp = va_arg( ap, struct berval ** );
ber_bvfree(*bvp);
bvp = NULL;
break;
case 'B': /* bit string - allocate storage as needed */
ss = va_arg( ap, char ** );
l = va_arg( ap, ber_slen_t * ); /* for length, in bits */
if (*ss) NSLBERI_FREE(*ss);
*ss = NULL;
break;
case 't': /* tag of next item */
t = va_arg( ap, ber_tag_t * );
break;
case 'T': /* skip tag of next item */
t = va_arg( ap, ber_tag_t * );
break;
case 'v': /* sequence of strings */
sss = va_arg( ap, char *** );
ber_svecfree(*sss);
*sss = NULL;
break;
case 'V': /* sequence of strings + lengths */
bv = va_arg( ap, struct berval *** );
ber_bvecfree(*bv);
*bv = NULL;
break;
case 'x': /* skip the next element - whatever it is */
break;
case '{': /* begin sequence */
case '[': /* begin set */
break;
case '}': /* end sequence */
case ']': /* end set */
break;
default:
break;
}
} /* for */
va_end( ap );
} /* if */
return( rc );
}
void
LDAP_CALL
ber_bvfree( struct berval *bv )
{
if ( bv != NULL ) {
if ( bv->bv_val != NULL ) {
NSLBERI_FREE( bv->bv_val );
}
NSLBERI_FREE( (char *) bv );
}
}
void
LDAP_CALL
ber_bvecfree( struct berval **bv )
{
int i;
if ( bv != NULL ) {
for ( i = 0; bv[i] != NULL; i++ ) {
ber_bvfree( bv[i] );
}
NSLBERI_FREE( (char *) bv );
}
}
struct berval *
LDAP_CALL
ber_bvdup( const struct berval *bv )
{
struct berval *new;
if ( (new = (struct berval *)NSLBERI_MALLOC( sizeof(struct berval) ))
== NULL ) {
return( NULL );
}
if ( bv->bv_val == NULL ) {
new->bv_val = NULL;
new->bv_len = 0;
} else {
if ( (new->bv_val = (char *)NSLBERI_MALLOC( bv->bv_len + 1 ))
== NULL ) {
NSLBERI_FREE( new );
new = NULL;
return( NULL );
}
SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
new->bv_val[bv->bv_len] = '\0';
new->bv_len = bv->bv_len;
}
return( new );
}
void
LDAP_CALL
ber_svecfree( char **vals )
{
int i;
if ( vals == NULL )
return;
for ( i = 0; vals[i] != NULL; i++ )
NSLBERI_FREE( vals[i] );
NSLBERI_FREE( (char *) vals );
}
#ifdef STR_TRANSLATION
void
LDAP_CALL
ber_set_string_translators(
BerElement *ber,
BERTranslateProc encode_proc,
BERTranslateProc decode_proc
)
{
ber->ber_encode_translate_proc = encode_proc;
ber->ber_decode_translate_proc = decode_proc;
}
#endif /* STR_TRANSLATION */

113
ldap/c-sdk/liblber/dtest.c Normal file
View file

@ -0,0 +1,113 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1990 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
/* dtest.c - lber decoding test program */
#include <stdio.h>
#include <string.h>
#ifdef MACOS
#include <stdlib.h>
#include <console.h>
#else /* MACOS */
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif /* _WIN32 */
#endif /* MACOS */
#include "lber.h"
int
SSL_Recv( int s, char *b, unsigned l, int dummy )
{
return( read( s, b, l ) );
}
SSL_Send( int s, char *b, unsigned l, int dummy )
{
return( write( s, b, l ) );
}
static void usage( char *name )
{
fprintf( stderr, "usage: %s < berfile\n", name );
}
main( int argc, char **argv )
{
long i, fd;
ber_len_t len;
ber_tag_t tag;
BerElement *ber;
Sockbuf *sb;
extern int lber_debug;
lber_debug = 255;
if ( argc > 1 ) {
usage( argv[0] );
exit( 1 );
}
sb = ber_sockbuf_alloc();
fd = 0;
ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_DESC, &fd );
if ( (ber = der_alloc()) == NULL ) {
perror( "ber_alloc" );
exit( 1 );
}
if ( (tag = ber_get_next( sb, &len, ber )) == LBER_ERROR ) {
perror( "ber_get_next" );
exit( 1 );
}
printf( "message has tag 0x%x and length %ld\n", tag, len );
return( 0 );
}

699
ldap/c-sdk/liblber/encode.c Normal file
View file

@ -0,0 +1,699 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1990 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
/* encode.c - ber output encoding routines */
#include "lber-int.h"
static int
ber_calc_taglen( ber_tag_t tag )
{
int i;
ber_int_t mask;
/* find the first non-all-zero byte in the tag */
for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
mask = (0xff << (i * 8));
/* not all zero */
if ( tag & mask )
break;
}
return( i + 1 );
}
static int
ber_put_tag( BerElement *ber, ber_tag_t tag, int nosos )
{
int taglen;
ber_tag_t ntag;
taglen = ber_calc_taglen( tag );
ntag = LBER_HTONL( tag );
return( ber_write( ber, ((char *) &ntag) + sizeof(ber_int_t) - taglen,
taglen, nosos ) );
}
static int
ber_calc_lenlen( ber_len_t len )
{
/*
* short len if it's less than 128 - one byte giving the len,
* with bit 8 0.
*/
if ( len <= 0x7F )
return( 1 );
/*
* long len otherwise - one byte with bit 8 set, giving the
* length of the length, followed by the length itself.
*/
if ( len <= 0xFF )
return( 2 );
if ( len <= 0xFFFF )
return( 3 );
if ( len <= 0xFFFFFF )
return( 4 );
return( 5 );
}
static int
ber_put_len( BerElement *ber, ber_len_t len, int nosos )
{
int i;
char lenlen;
ber_int_t mask;
ber_len_t netlen;
/*
* short len if it's less than 128 - one byte giving the len,
* with bit 8 0.
*/
if ( len <= 127 ) {
netlen = LBER_HTONL( len );
return( ber_write( ber, (char *) &netlen + sizeof(ber_int_t) - 1,
1, nosos ) );
}
/*
* long len otherwise - one byte with bit 8 set, giving the
* length of the length, followed by the length itself.
*/
/* find the first non-all-zero byte */
for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
mask = (0xff << (i * 8));
/* not all zero */
if ( len & mask )
break;
}
lenlen = ++i;
if ( lenlen > 4 )
return( -1 );
lenlen |= 0x80;
/* write the length of the length */
if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
return( -1 );
/* write the length itself */
netlen = LBER_HTONL( len );
if ( ber_write( ber, (char *) &netlen + (sizeof(ber_int_t) - i), i, nosos )
!= i )
return( -1 );
return( i + 1 );
}
static int
ber_put_int_or_enum( BerElement *ber, ber_int_t num, ber_tag_t tag )
{
int i, sign, taglen;
int len, lenlen;
ber_int_t netnum, mask;
sign = (num < 0);
/*
* high bit is set - look for first non-all-one byte
* high bit is clear - look for first non-all-zero byte
*/
for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
mask = (0xff << (i * 8));
if ( sign ) {
/* not all ones */
if ( (num & mask) != mask )
break;
} else {
/* not all zero */
if ( num & mask )
break;
}
}
/*
* we now have the "leading byte". if the high bit on this
* byte matches the sign bit, we need to "back up" a byte.
*/
mask = (num & (0x80 << (i * 8)));
if ( (mask && !sign) || (sign && !mask) )
i++;
len = i + 1;
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
return( -1 );
if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
return( -1 );
i++;
netnum = LBER_HTONL( num );
if ( ber_write( ber, (char *) &netnum + (sizeof(ber_int_t) - i), i, 0 )
== i)
/* length of tag + length + contents */
return( taglen + lenlen + i );
return( -1 );
}
int
LDAP_CALL
ber_put_enum( BerElement *ber, ber_int_t num, ber_tag_t tag )
{
if ( tag == LBER_DEFAULT )
tag = LBER_ENUMERATED;
return( ber_put_int_or_enum( ber, num, tag ) );
}
int
LDAP_CALL
ber_put_int( BerElement *ber, ber_int_t num, ber_tag_t tag )
{
if ( tag == LBER_DEFAULT )
tag = LBER_INTEGER;
return( ber_put_int_or_enum( ber, num, tag ) );
}
int
LDAP_CALL
ber_put_ostring( BerElement *ber, char *str, ber_len_t len,
ber_tag_t tag )
{
int taglen, lenlen, rc;
#ifdef STR_TRANSLATION
int free_str;
#endif /* STR_TRANSLATION */
if ( tag == LBER_DEFAULT )
tag = LBER_OCTETSTRING;
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
return( -1 );
#ifdef STR_TRANSLATION
if ( len > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS ) != 0
&& ber->ber_encode_translate_proc != NULL ) {
if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 )
!= 0 ) {
return( -1 );
}
free_str = 1;
} else {
free_str = 0;
}
#endif /* STR_TRANSLATION */
/*
* Note: below is a spot where we limit ber_write
* to signed long (instead of unsigned long)
*/
if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
ber_write( ber, str, len, 0 ) != (ber_int_t) len ) {
rc = -1;
} else {
/* return length of tag + length + contents */
rc = taglen + lenlen + len;
}
#ifdef STR_TRANSLATION
if ( free_str ) {
NSLBERI_FREE( str );
}
#endif /* STR_TRANSLATION */
return( rc );
}
int
LDAP_CALL
ber_put_string( BerElement *ber, char *str, ber_tag_t tag )
{
return( ber_put_ostring( ber, str, (ber_len_t) strlen( str ), tag ));
}
int
LDAP_CALL
ber_put_bitstring( BerElement *ber, char *str,
ber_len_t blen /* in bits */, ber_tag_t tag )
{
int taglen, lenlen, len;
unsigned char unusedbits;
if ( tag == LBER_DEFAULT )
tag = LBER_BITSTRING;
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
return( -1 );
len = ( blen + 7 ) / 8;
unusedbits = (unsigned char) (len * 8 - blen);
if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
return( -1 );
if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
return( -1 );
if ( ber_write( ber, str, len, 0 ) != len )
return( -1 );
/* return length of tag + length + unused bit count + contents */
return( taglen + 1 + lenlen + len );
}
int
LDAP_CALL
ber_put_null( BerElement *ber, ber_tag_t tag )
{
int taglen;
if ( tag == LBER_DEFAULT )
tag = LBER_NULL;
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
return( -1 );
if ( ber_put_len( ber, 0, 0 ) != 1 )
return( -1 );
return( taglen + 1 );
}
int
LDAP_CALL
ber_put_boolean( BerElement *ber, ber_int_t boolval, ber_tag_t tag )
{
int taglen;
unsigned char trueval = 0xff;
unsigned char falseval = 0x00;
if ( tag == LBER_DEFAULT )
tag = LBER_BOOLEAN;
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
return( -1 );
if ( ber_put_len( ber, 1, 0 ) != 1 )
return( -1 );
if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
!= 1 )
return( -1 );
return( taglen + 2 );
}
#define FOUR_BYTE_LEN 5
/* the idea here is roughly this: we maintain a stack of these Seqorset
* structures. This is pushed when we see the beginning of a new set or
* sequence. It is popped when we see the end of a set or sequence.
* Since we don't want to malloc and free these structures all the time,
* we pre-allocate a small set of them within the ber element structure.
* thus we need to spot when we've overflowed this stack and fall back to
* malloc'ing instead.
*/
static int
ber_start_seqorset( BerElement *ber, ber_tag_t tag )
{
Seqorset *new_sos;
/* can we fit into the local stack ? */
if (ber->ber_sos_stack_posn < SOS_STACK_SIZE) {
/* yes */
new_sos = &ber->ber_sos_stack[ber->ber_sos_stack_posn];
} else {
/* no */
if ( (new_sos = (Seqorset *)NSLBERI_MALLOC( sizeof(Seqorset)))
== NULLSEQORSET ) {
return( -1 );
}
}
ber->ber_sos_stack_posn++;
if ( ber->ber_sos == NULLSEQORSET )
new_sos->sos_first = ber->ber_ptr;
else
new_sos->sos_first = ber->ber_sos->sos_ptr;
/* Set aside room for a 4 byte length field */
new_sos->sos_ptr = new_sos->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
new_sos->sos_tag = tag;
new_sos->sos_next = ber->ber_sos;
new_sos->sos_clen = 0;
ber->ber_sos = new_sos;
if (ber->ber_sos->sos_ptr > ber->ber_end) {
nslberi_ber_realloc(ber, ber->ber_sos->sos_ptr - ber->ber_end);
}
return( 0 );
}
int
LDAP_CALL
ber_start_seq( BerElement *ber, ber_tag_t tag )
{
if ( tag == LBER_DEFAULT )
tag = LBER_SEQUENCE;
return( ber_start_seqorset( ber, tag ) );
}
int
LDAP_CALL
ber_start_set( BerElement *ber, ber_tag_t tag )
{
if ( tag == LBER_DEFAULT )
tag = LBER_SET;
return( ber_start_seqorset( ber, tag ) );
}
static int
ber_put_seqorset( BerElement *ber )
{
ber_len_t len, netlen;
int taglen, lenlen;
unsigned char ltag = 0x80 + FOUR_BYTE_LEN - 1;
Seqorset *next;
Seqorset **sos = &ber->ber_sos;
if ( *sos == NULL ) {
/* No sequence or set to put... fatal error. */
return( -1 );
}
/*
* If this is the toplevel sequence or set, we need to actually
* write the stuff out. Otherwise, it's already been put in
* the appropriate buffer and will be written when the toplevel
* one is written. In this case all we need to do is update the
* length and tag.
*/
len = (*sos)->sos_clen;
netlen = LBER_HTONL( len );
if ( sizeof(ber_int_t) > 4 && len > 0xFFFFFFFF )
return( -1 );
if ( ber->ber_options & LBER_OPT_USE_DER ) {
lenlen = ber_calc_lenlen( len );
} else {
lenlen = FOUR_BYTE_LEN;
}
if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
/* write the tag */
if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
return( -1 );
if ( ber->ber_options & LBER_OPT_USE_DER ) {
/* Write the length in the minimum # of octets */
if ( ber_put_len( ber, len, 1 ) == -1 )
return( -1 );
if (lenlen != FOUR_BYTE_LEN) {
/*
* We set aside FOUR_BYTE_LEN bytes for
* the length field. Move the data if
* we don't actually need that much
*/
SAFEMEMCPY( (*sos)->sos_first + taglen +
lenlen, (*sos)->sos_first + taglen +
FOUR_BYTE_LEN, len );
}
} else {
/* Fill FOUR_BYTE_LEN bytes for length field */
/* one byte of length length */
if ( ber_write( ber, (char *)&ltag, 1, 1 ) != 1 )
return( -1 );
/* the length itself */
if ( ber_write( ber, (char *) &netlen + sizeof(ber_int_t)
- (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1 )
!= FOUR_BYTE_LEN - 1 )
return( -1 );
}
/* The ber_ptr is at the set/seq start - move it to the end */
ber->ber_ptr += len;
} else {
ber_tag_t ntag;
/* the tag */
taglen = ber_calc_taglen( (*sos)->sos_tag );
ntag = LBER_HTONL( (*sos)->sos_tag );
SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
sizeof(ber_int_t) - taglen, taglen );
if ( ber->ber_options & LBER_OPT_USE_DER ) {
ltag = (lenlen == 1) ? (unsigned char)len :
(unsigned char) (0x80 + (lenlen - 1));
}
/* one byte of length length */
SAFEMEMCPY( (*sos)->sos_first + 1, &ltag, 1 );
if ( ber->ber_options & LBER_OPT_USE_DER ) {
if (lenlen > 1) {
/* Write the length itself */
SAFEMEMCPY( (*sos)->sos_first + 2,
(char *)&netlen + sizeof(ber_uint_t) -
(lenlen - 1),
lenlen - 1 );
}
if (lenlen != FOUR_BYTE_LEN) {
/*
* We set aside FOUR_BYTE_LEN bytes for
* the length field. Move the data if
* we don't actually need that much
*/
SAFEMEMCPY( (*sos)->sos_first + taglen +
lenlen, (*sos)->sos_first + taglen +
FOUR_BYTE_LEN, len );
}
} else {
/* the length itself */
SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
(char *) &netlen + sizeof(ber_int_t) -
(FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
}
next->sos_clen += (taglen + lenlen + len);
next->sos_ptr += (taglen + lenlen + len);
}
/* we're done with this seqorset, so free it up */
/* was this one from the local stack ? */
if (ber->ber_sos_stack_posn <= SOS_STACK_SIZE) {
/* yes */
} else {
/* no */
NSLBERI_FREE( (char *) (*sos) );
}
ber->ber_sos_stack_posn--;
*sos = next;
return( taglen + lenlen + len );
}
int
LDAP_CALL
ber_put_seq( BerElement *ber )
{
return( ber_put_seqorset( ber ) );
}
int
LDAP_CALL
ber_put_set( BerElement *ber )
{
return( ber_put_seqorset( ber ) );
}
/* VARARGS */
int
LDAP_C
ber_printf( BerElement *ber, const char *fmt, ... )
{
va_list ap;
char *s, **ss;
struct berval *bval, **bv;
int rc, i;
ber_len_t len;
va_start( ap, fmt );
#ifdef LDAP_DEBUG
if ( lber_debug & 64 ) {
char msg[80];
sprintf( msg, "ber_printf fmt (%s)\n", fmt );
ber_err_print( msg );
}
#endif
for ( rc = 0; *fmt && rc != -1; fmt++ ) {
switch ( *fmt ) {
case 'b': /* boolean */
i = va_arg( ap, int );
rc = ber_put_boolean( ber, i, ber->ber_tag );
break;
case 'i': /* int */
i = va_arg( ap, int );
rc = ber_put_int( ber, (ber_int_t)i, ber->ber_tag );
break;
case 'e': /* enumeration */
i = va_arg( ap, int );
rc = ber_put_enum( ber, (ber_int_t)i, ber->ber_tag );
break;
case 'n': /* null */
rc = ber_put_null( ber, ber->ber_tag );
break;
case 'o': /* octet string (non-null terminated) */
s = va_arg( ap, char * );
len = va_arg( ap, int );
rc = ber_put_ostring( ber, s, len, ber->ber_tag );
break;
case 'O': /* berval octet string */
if( ( bval = va_arg( ap, struct berval * ) ) == NULL )
break;
if( bval->bv_len == 0 ) {
rc = ber_put_ostring( ber, "", 0, ber->ber_tag );
} else {
rc = ber_put_ostring( ber, bval->bv_val, bval->bv_len,
ber->ber_tag );
}
break;
case 's': /* string */
s = va_arg( ap, char * );
rc = ber_put_string( ber, s, ber->ber_tag );
break;
case 'B': /* bit string */
s = va_arg( ap, char * );
len = va_arg( ap, int ); /* in bits */
rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
break;
case 't': /* tag for the next element */
ber->ber_tag = va_arg( ap, ber_tag_t );
ber->ber_usertag = 1;
break;
case 'v': /* vector of strings */
if ( (ss = va_arg( ap, char ** )) == NULL )
break;
for ( i = 0; ss[i] != NULL; i++ ) {
if ( (rc = ber_put_string( ber, ss[i],
ber->ber_tag )) == -1 )
break;
}
break;
case 'V': /* sequences of strings + lengths */
if ( (bv = va_arg( ap, struct berval ** )) == NULL )
break;
for ( i = 0; bv[i] != NULL; i++ ) {
if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
bv[i]->bv_len, ber->ber_tag )) == -1 )
break;
}
break;
case '{': /* begin sequence */
rc = ber_start_seq( ber, ber->ber_tag );
break;
case '}': /* end sequence */
rc = ber_put_seqorset( ber );
break;
case '[': /* begin set */
rc = ber_start_set( ber, ber->ber_tag );
break;
case ']': /* end set */
rc = ber_put_seqorset( ber );
break;
default: {
char msg[80];
sprintf( msg, "unknown fmt %c\n", *fmt );
ber_err_print( msg );
rc = -1;
break;
}
}
if ( ber->ber_usertag == 0 )
ber->ber_tag = LBER_DEFAULT;
else
ber->ber_usertag = 0;
}
va_end( ap );
return( rc );
}

193
ldap/c-sdk/liblber/etest.c Normal file
View file

@ -0,0 +1,193 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1990 Regents of the University of Michigan.
* All rights reserved.
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
/* test.c - lber encoding test program */
#include <stdio.h>
#include <string.h>
#ifdef MACOS
#include <stdlib.h>
#include <unix.h>
#include <fcntl.h>
#include <console.h>
#else /* MACOS */
#include <sys/types.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/socket.h>
#endif /* _WIN32 */
#endif /* MACOS */
#include "lber.h"
int
SSL_Recv( int s, char *b, unsigned l, int dummy )
{
return( read( s, b, l ) );
}
SSL_Send( int s, char *b, unsigned l, int dummy )
{
return( write( s, b, l ) );
}
int
getline( char *prompt, char c, char *buf, int bsize )
{
char *p;
if ( prompt != NULL ) {
fprintf( stderr, "%s: ", prompt );
} else {
fprintf( stderr, "enter value for '%c': ", c );
}
if ( fgets( buf, bsize, stdin ) == NULL ) {
return( -1 );
}
if ( (p = strchr( buf, '\n' )) != NULL ) {
*p = '\0';
}
return( 0 );
}
static void usage( char *name )
{
fprintf( stderr, "usage: %s fmtstring\n", name );
}
main( int argc, char **argv )
{
int rc, fd;
char *s, *p;
void *arg1, *arg2;
Sockbuf *sb;
BerElement *ber;
char fmt[2];
char buf[BUFSIZ];
extern int lber_debug;
lber_debug = 255;
if ( argc < 2 ) {
usage( argv[0] );
exit( 1 );
}
sb = ber_sockbuf_alloc();
fd = 1;
ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_DESC, &fd );
if ( (ber = der_alloc()) == NULL ) {
perror( "ber_alloc" );
exit( 1 );
}
rc = 0;
fmt[1] = '\0';
for ( s = argv[1]; *s; s++ ) {
switch ( *s ) {
case 'i': /* int */
case 'b': /* boolean */
case 'e': /* enumeration */
getline( NULL, *s, buf, sizeof(buf) );
arg1 = (void *) atoi( buf );
break;
case 'n': /* null */
arg1 = NULL;
break;
case 'o': /* octet string (non-null terminated) */
getline( NULL, *s, buf, sizeof(buf) );
arg1 = (void *) buf;
arg2 = (void *) strlen( buf );
break;
case 's': /* string */
getline( NULL, *s, buf, sizeof(buf) );
arg1 = (void *) buf;
break;
case 'B': /* bit string */
getline( NULL, *s, buf, sizeof(buf) );
arg1 = (void *) buf;
arg2 = (void *) strlen( buf );
break;
case 't': /* tag for the next element */
getline( NULL, *s, buf, sizeof(buf) );
arg1 = (void *) buf;
break;
case '{': /* begin sequence */
case '}': /* end sequence */
case '[': /* begin set */
case ']': /* end set */
break;
default:
fprintf( stderr, "unknown fmt %c\n", *s );
rc = -1;
break;
}
fmt[0] = *s;
if ( ber_printf( ber, fmt, arg1, arg2 ) == -1 ) {
fprintf( stderr, "ber_printf\n" );
exit( 1 );
}
}
if ( ber_flush( sb, ber, 1 ) != 0 ) {
perror( "ber_flush" );
rc = -1;
}
return( rc );
}

100
ldap/c-sdk/liblber/idtest.c Normal file
View file

@ -0,0 +1,100 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1990 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
/* idtest.c - ber decoding test program using isode libraries */
#include <stdio.h>
#include <psap.h>
#include <quipu/attr.h>
static usage( char *name )
{
fprintf( stderr, "usage: %s\n", name );
}
main( int argc, char **argv )
{
PE pe;
PS psin, psout, pserr;
/* read the pe from standard in */
if ( (psin = ps_alloc( std_open )) == NULLPS ) {
perror( "ps_alloc" );
exit( 1 );
}
if ( std_setup( psin, stdin ) == NOTOK ) {
perror( "std_setup" );
exit( 1 );
}
/* write the pe to standard out */
if ( (psout = ps_alloc( std_open )) == NULLPS ) {
perror( "ps_alloc" );
exit( 1 );
}
if ( std_setup( psout, stdout ) == NOTOK ) {
perror( "std_setup" );
exit( 1 );
}
/* pretty print it to standard error */
if ( (pserr = ps_alloc( std_open )) == NULLPS ) {
perror( "ps_alloc" );
exit( 1 );
}
if ( std_setup( pserr, stderr ) == NOTOK ) {
perror( "std_setup" );
exit( 1 );
}
while ( (pe = ps2pe( psin )) != NULLPE ) {
pe2pl( pserr, pe );
pe2ps( psout, pe );
}
exit( 0 );
}

1757
ldap/c-sdk/liblber/io.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,305 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1990 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
/* lbet-int.h - internal header file for liblber */
#ifndef _LBERINT_H
#define _LBERINT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#ifdef macintosh
# include "ldap-macos.h"
#else /* macintosh */
#if !defined(BSDI) && !defined(DARWIN) && !defined(FREEBSD) && !defined(OPENBSD)
# include <malloc.h>
#endif
# include <errno.h>
# include <sys/types.h>
#if defined(SUNOS4) || defined(SCOOS)
# include <sys/time.h>
#endif
#if defined( _WINDOWS )
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <basetsd.h>
# define ssize_t SSIZE_T
# include <time.h>
/* No stderr in a 16-bit Windows DLL */
# if defined(_WINDLL) && !defined(_WIN32)
# define USE_DBG_WIN
# endif
# else
/* # include <sys/varargs.h> */
# include <sys/socket.h>
# include <netinet/in.h>
#if !defined(XP_OS2) && !defined(DARWIN)
# include <unistd.h>
#endif
# endif /* defined( _WINDOWS ) */
#endif /* macintosh */
#include <memory.h>
#include <string.h>
#include "portable.h"
#ifdef _WINDOWS
# if defined(FD_SETSIZE)
# else
# define FD_SETSIZE 256 /* set it before winsock sets it to 64! */
# endif
#include <winsock.h>
#include <io.h>
#endif /* _WINDOWS */
#ifdef XP_OS2
#include <io.h>
#endif /* XP_OS2 */
/* No stderr in a 16-bit Windows DLL */
#if defined(_WINDLL) && !defined(_WIN32)
#define stderr NULL
#endif
#include "lber.h"
#ifdef macintosh
#define NSLDAPI_LBER_SOCKET_IS_PTR
#endif
#define OLD_LBER_SEQUENCE 0x10 /* w/o constructed bit - broken */
#define OLD_LBER_SET 0x11 /* w/o constructed bit - broken */
#ifndef _IFP
#define _IFP
typedef int (LDAP_C LDAP_CALLBACK *IFP)();
#endif
typedef struct seqorset {
ber_len_t sos_clen;
ber_tag_t sos_tag;
char *sos_first;
char *sos_ptr;
struct seqorset *sos_next;
} Seqorset;
#define NULLSEQORSET ((Seqorset *) 0)
#define SOS_STACK_SIZE 8 /* depth of the pre-allocated sos structure stack */
#define MAX_TAG_SIZE (1 + sizeof(ber_int_t)) /* One byte for the length of the tag */
#define MAX_LEN_SIZE (1 + sizeof(ber_int_t)) /* One byte for the length of the length */
#define MAX_VALUE_PREFIX_SIZE (2 + sizeof(ber_int_t)) /* 1 byte for the tag and 1 for the len (msgid) */
#define BER_ARRAY_QUANTITY 7 /* 0:Tag 1:Length 2:Value-prefix 3:Value 4:Value-suffix */
#define BER_STRUCT_TAG 0 /* 5:ControlA 6:ControlB */
#define BER_STRUCT_LEN 1
#define BER_STRUCT_PRE 2
#define BER_STRUCT_VAL 3
#define BER_STRUCT_SUF 4
#define BER_STRUCT_CO1 5
#define BER_STRUCT_CO2 6
struct berelement {
ldap_x_iovec ber_struct[BER_ARRAY_QUANTITY]; /* See above */
char ber_tag_contents[MAX_TAG_SIZE];
char ber_len_contents[MAX_LEN_SIZE];
char ber_pre_contents[MAX_VALUE_PREFIX_SIZE];
char ber_suf_contents[MAX_LEN_SIZE+1];
char *ber_buf; /* update the value value when writing in case realloc is called */
char *ber_ptr;
char *ber_end;
struct seqorset *ber_sos;
ber_len_t ber_tag_len_read;
ber_tag_t ber_tag; /* Remove me someday */
ber_len_t ber_len; /* Remove me someday */
int ber_usertag;
char ber_options;
char *ber_rwptr;
BERTranslateProc ber_encode_translate_proc;
BERTranslateProc ber_decode_translate_proc;
int ber_flags;
#define LBER_FLAG_NO_FREE_BUFFER 1 /* don't free ber_buf */
unsigned int ber_buf_reallocs; /* realloc counter */
int ber_sos_stack_posn;
Seqorset ber_sos_stack[SOS_STACK_SIZE];
};
#define BER_CONTENTS_STRUCT_SIZE (sizeof(ldap_x_iovec) * BER_ARRAY_QUANTITY)
#define NULLBER ((BerElement *)NULL)
#ifdef LDAP_DEBUG
void ber_dump( BerElement *ber, int inout );
#endif
/*
* structure for read/write I/O callback functions.
*/
struct nslberi_io_fns {
LDAP_IOF_READ_CALLBACK *lbiof_read;
LDAP_IOF_WRITE_CALLBACK *lbiof_write;
};
/*
* Old structure for use with LBER_SOCKBUF_OPT_EXT_IO_FNS:
*/
struct lber_x_ext_io_fns_rev0 {
/* lbextiofn_size should always be set to LBER_X_EXTIO_FNS_SIZE */
int lbextiofn_size;
LDAP_X_EXTIOF_READ_CALLBACK *lbextiofn_read;
LDAP_X_EXTIOF_WRITE_CALLBACK *lbextiofn_write;
struct lextiof_socket_private *lbextiofn_socket_arg;
};
#define LBER_X_EXTIO_FNS_SIZE_REV0 sizeof(struct lber_x_ext_io_fns_rev0)
struct sockbuf {
LBER_SOCKET sb_sd;
BerElement sb_ber;
int sb_naddr; /* > 0 implies using CLDAP (UDP) */
void *sb_useaddr; /* pointer to sockaddr to use next */
void *sb_fromaddr; /* pointer to message source sockaddr */
void **sb_addrs; /* actually an array of pointers to
sockaddrs */
int sb_options; /* to support copying ber elements */
LBER_SOCKET sb_copyfd; /* for LBER_SOCKBUF_OPT_TO_FILE* opts */
ber_len_t sb_max_incoming;
ber_tag_t sb_valid_tag; /* valid tag to accept */
struct nslberi_io_fns
sb_io_fns; /* classic I/O callback functions */
struct lber_x_ext_io_fns
sb_ext_io_fns; /* extended I/O callback functions */
};
#define NULLSOCKBUF ((Sockbuf *)NULL)
/* needed by libldap, even in non-DEBUG builds */
void ber_err_print( char *data );
#ifndef NSLBERI_LBER_INT_FRIEND
/*
* Everything from this point on is excluded if NSLBERI_LBER_INT_FRIEND is
* defined. The code under ../libraries/libldap defines this.
*/
#define READBUFSIZ 8192
/*
* macros used to check validity of data structures and parameters
*/
#define NSLBERI_VALID_BERELEMENT_POINTER( ber ) \
( (ber) != NULLBER )
#define NSLBERI_VALID_SOCKBUF_POINTER( sb ) \
( (sb) != NULLSOCKBUF )
#define LBER_HTONL( l ) htonl( l )
#define LBER_NTOHL( l ) ntohl( l )
/* function prototypes */
#ifdef LDAP_DEBUG
void lber_bprint( char *data, int len );
#endif
void ber_err_print( char *data );
void *nslberi_malloc( size_t size );
void *nslberi_calloc( size_t nelem, size_t elsize );
void *nslberi_realloc( void *ptr, size_t size );
void nslberi_free( void *ptr );
int nslberi_ber_realloc( BerElement *ber, ber_len_t len );
/* blame: dboreham
* slapd spends much of its time doing memcpy's for the ber code.
* Most of these are single-byte, so we special-case those and speed
* things up considerably.
*/
#ifdef sunos4
#define THEMEMCPY( d, s, n ) bcopy( s, d, n )
#else /* sunos4 */
#define THEMEMCPY( d, s, n ) memmove( d, s, n )
#endif /* sunos4 */
#ifdef SAFEMEMCPY
#undef SAFEMEMCPY
#define SAFEMEMCPY(d,s,n) if (1 == n) *((char*)d) = *((char*)s); else THEMEMCPY(d,s,n);
#endif
/*
* Memory allocation done in liblber should all go through one of the
* following macros. This is so we can plug-in alternative memory
* allocators, etc. as the need arises.
*/
#define NSLBERI_MALLOC( size ) nslberi_malloc( size )
#define NSLBERI_CALLOC( nelem, elsize ) nslberi_calloc( nelem, elsize )
#define NSLBERI_REALLOC( ptr, size ) nslberi_realloc( ptr, size )
#define NSLBERI_FREE( ptr ) nslberi_free( ptr )
/* allow the library to access the debug variable */
extern int lber_debug;
#endif /* !NSLBERI_LBER_INT_FRIEND */
#ifdef __cplusplus
}
#endif
#endif /* _LBERINT_H */

View file

@ -0,0 +1,23 @@
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
include('../common.mozbuild')
Library('lber60')
SOURCES += [
'bprint.c',
'decode.c',
'encode.c',
'io.c',
]
DEFINES['USE_WAITPID'] = True
DEFINES['NEEDPROTOS'] = True
LOCAL_INCLUDES += [
'../include'
]