Commit db097047 authored by Kurt Zeilenga's avatar Kurt Zeilenga
Browse files

code cleanup

parent d5e55877
......@@ -75,31 +75,30 @@ int ber_pvt_log_output(
const char *fmt,
... )
{
char buf[ 1024 ];
char buf[1024];
va_list vl;
va_start( vl, fmt );
if ( ber_int_log_proc != NULL )
{
if ( ber_int_log_proc != NULL ) {
ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl );
} else {
int level;
ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level );
buf[sizeof(buf) - 1] = '\0';
vsnprintf( buf, sizeof(buf)-1, fmt, vl );
if ( ber_log_check( LDAP_DEBUG_BER, level ) ) {
(*ber_pvt_log_print)( buf );
}
}
else
{
int level;
ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level );
buf[sizeof(buf) - 1] = '\0';
vsnprintf( buf, sizeof(buf)-1, fmt, vl );
if ( ber_log_check( LDAP_DEBUG_BER, level ) )
(*ber_pvt_log_print)( buf );
}
va_end(vl);
va_end(vl);
return 1;
}
int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
{
char buf[ 1024 ];
char buf[1024];
va_list ap;
assert( fmt != NULL );
......@@ -158,11 +157,11 @@ ber_bprint(
LDAP_CONST char *data,
ber_len_t len )
{
static const char hexdig[] = "0123456789abcdef";
static const char hexdig[] = "0123456789abcdef";
#define BP_OFFSET 9
#define BP_GRAPH 60
#define BP_LEN 80
char line[ BP_LEN ];
char line[BP_LEN];
ber_len_t i;
assert( data != NULL );
......@@ -183,23 +182,23 @@ ber_bprint(
off = i % 0x0ffffU;
line[ 2 ] = hexdig[ 0x0f & (off >> 12) ];
line[ 3 ] = hexdig[ 0x0f & (off >> 8) ];
line[ 4 ] = hexdig[ 0x0f & (off >> 4) ];
line[ 5 ] = hexdig[ 0x0f & off ];
line[ 6 ] = ':';
line[2] = hexdig[0x0f & (off >> 12)];
line[3] = hexdig[0x0f & (off >> 8)];
line[4] = hexdig[0x0f & (off >> 4)];
line[5] = hexdig[0x0f & off];
line[6] = ':';
}
off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
line[ off ] = hexdig[ 0x0f & ( data[i] >> 4 ) ];
line[ off+1 ] = hexdig[ 0x0f & data[i] ];
line[off] = hexdig[0x0f & ( data[i] >> 4 )];
line[off+1] = hexdig[0x0f & data[i]];
off = BP_GRAPH + n + ((n >= 8)?1:0);
if ( isprint( (unsigned char) data[i] )) {
line[ BP_GRAPH + n ] = data[i];
line[BP_GRAPH + n] = data[i];
} else {
line[ BP_GRAPH + n ] = '.';
line[BP_GRAPH + n] = '.';
}
}
......@@ -258,23 +257,23 @@ int ber_output_dump(
off = i % 0x0ffffU;
line[ 2 ] = hexdig[ 0x0f & (off >> 12) ];
line[ 3 ] = hexdig[ 0x0f & (off >> 8) ];
line[ 4 ] = hexdig[ 0x0f & (off >> 4) ];
line[ 5 ] = hexdig[ 0x0f & off ];
line[ 6 ] = ':';
line[2] = hexdig[0x0f & (off >> 12)];
line[3] = hexdig[0x0f & (off >> 8)];
line[4] = hexdig[0x0f & (off >> 4)];
line[5] = hexdig[0x0f & off ];
line[6] = ':';
}
off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
line[ off ] = hexdig[ 0x0f & ( data[i] >> 4 ) ];
line[ off+1 ] = hexdig[ 0x0f & data[i] ];
line[off] = hexdig[ 0x0f & ( data[i] >> 4 ) ];
line[off+1] = hexdig[ 0x0f & data[i] ];
off = BP_GRAPH + n + ((n >= 8)?1:0);
if ( isprint( (unsigned char) data[i] )) {
line[ BP_GRAPH + n ] = data[i];
line[BP_GRAPH + n] = data[i];
} else {
line[ BP_GRAPH + n ] = '.';
line[BP_GRAPH + n] = '.';
}
}
......
......@@ -39,84 +39,77 @@ static int use_syslog = 0;
static int debug2syslog(int l) {
switch (l) {
case LDAP_LEVEL_EMERG: return LOG_EMERG;
case LDAP_LEVEL_ALERT: return LOG_ALERT;
case LDAP_LEVEL_CRIT: return LOG_CRIT;
case LDAP_LEVEL_ERR: return LOG_ERR;
case LDAP_LEVEL_WARNING: return LOG_WARNING;
case LDAP_LEVEL_NOTICE: return LOG_NOTICE;
case LDAP_LEVEL_INFO: return LOG_INFO;
case LDAP_LEVEL_EMERG: return LOG_EMERG;
case LDAP_LEVEL_ALERT: return LOG_ALERT;
case LDAP_LEVEL_CRIT: return LOG_CRIT;
case LDAP_LEVEL_ERR: return LOG_ERR;
case LDAP_LEVEL_WARNING: return LOG_WARNING;
case LDAP_LEVEL_NOTICE: return LOG_NOTICE;
case LDAP_LEVEL_INFO: return LOG_INFO;
}
return LOG_DEBUG;
}
#endif
static char *lutil_levels[] = {"emergency", "alert", "critical",
"error", "warning", "notice",
"information", "entry", "args",
"results", "detail1", "detail2",
NULL};
static char *lutil_subsys[LDAP_SUBSYS_NUM] = {"global","operation", "transport",
"connection", "filter", "ber",
"config", "acl", "cache", "index",
"ldif", "tools", "slapd", "slurpd",
"backend", "back_bdb", "back_ldbm",
"back_ldap", "back_meta", "back_mon" };
static char *lutil_levels[] = {
"emergency", "alert", "critical",
"error", "warning", "notice",
"information", "entry", "args",
"results", "detail1", "detail2",
NULL };
static char *lutil_subsys[LDAP_SUBSYS_NUM] = {
"global","operation", "transport",
"connection", "filter", "ber",
"config", "acl", "cache", "index",
"ldif", "tools", "slapd", "slurpd",
"backend", "back_bdb", "back_ldbm",
"back_ldap", "back_meta", "back_mon" };
int lutil_mnem2subsys( const char *subsys )
{
int i;
for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
{
if ( !strcasecmp( subsys, lutil_subsys[i] ) )
{
return i;
int i;
for( i = 0; i < LDAP_SUBSYS_NUM; i++ ) {
if ( !strcasecmp( subsys, lutil_subsys[i] ) ) {
return i;
}
}
return -1;
}
return -1;
}
void lutil_set_all_backends( int level )
{
int i;
int i;
for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
{
if ( !strncasecmp( "back_", lutil_subsys[i], strlen("back_") ) )
{
for( i = 0; i < LDAP_SUBSYS_NUM; i++ ) {
if ( !strncasecmp( "back_", lutil_subsys[i], strlen("back_") ) ) {
ldap_loglevels[i] = level;
}
}
}
}
int lutil_mnem2level( const char *level )
{
int i;
for( i = 0; lutil_levels[i] != NULL; i++ )
{
if ( !strcasecmp( level, lutil_levels[i] ) )
{
return i;
int i;
for( i = 0; lutil_levels[i] != NULL; i++ ) {
if ( !strcasecmp( level, lutil_levels[i] ) ) {
return i;
}
}
}
return -1;
return -1;
}
static int addSubsys( const char *subsys, int level )
{
int subsys_num;
if ( !strcasecmp( subsys, "backend" ) )
{
if ( !strcasecmp( subsys, "backend" ) ) {
lutil_set_all_backends( level );
return level;
}
else
{
} else {
subsys_num = lutil_mnem2subsys(subsys);
if(subsys_num < 0)
{
if(subsys_num < 0) {
fprintf(stderr, _("Unknown Subsystem name [ %s ] - Discarded\n"),
subsys);
fflush(stderr);
......@@ -131,7 +124,7 @@ static int addSubsys( const char *subsys, int level )
int lutil_set_debug_level( const char* subsys, int level )
{
return( addSubsys( subsys, level ) );
return( addSubsys( subsys, level ) );
}
int lutil_debug_file( FILE *file )
......@@ -157,8 +150,9 @@ void lutil_log_int(
t_subsys = strdup(subsys);
for(tmp = t_subsys, i = 0; i < strlen(t_subsys); i++, tmp++)
for(tmp = t_subsys, i = 0; i < strlen(t_subsys); i++, tmp++) {
*tmp = TOUPPER( (unsigned char) *tmp );
}
#ifdef LDAP_SYSLOG
/* we're configured to use syslog */
......@@ -179,11 +173,10 @@ void lutil_log_int(
if( log_file == NULL ) {
log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
if ( log_file == NULL )
if ( log_file == NULL ) {
log_file = fopen( "openldap.log", "w" );
if ( log_file == NULL )
return;
if ( log_file == NULL ) return;
}
ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
}
......@@ -203,7 +196,7 @@ void lutil_log_int(
* Stick the time in the buffer to output when using Winsock
* as NT can't pipe to a timestamp program like Unix can.
* This, of course, makes some logs hard to read.
*/
*/
time( &now );
today = localtime( &now );
fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
......@@ -238,53 +231,50 @@ void lutil_log( const int subsys, int level, const char *fmt, ... )
void lutil_log_initialize(int argc, char **argv)
{
int i;
/*
* Start by setting the hook for the libraries to use this logging
* routine.
*/
ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
if ( argc == 0 ) return;
/*
* Now go through the command line options to set the debugging
* levels
*/
for( i = 0; i < argc; i++ )
{
int i;
/*
* Start by setting the hook for the libraries to use this logging
* routine.
*/
ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
if ( argc == 0 ) return;
/*
* Now go through the command line options to set the debugging
* levels
*/
for( i = 0; i < argc; i++ ) {
char *next = argv[i];
if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
{
char subsys[64];
int level;
char *optarg = argv[i+1];
char *index = strchr( optarg, '=' );
if ( index != NULL )
{
if ( i < argc-1 && next[0] == '-' && next[1] == 'd' ) {
char subsys[64];
int level;
char *optarg = argv[i+1];
char *index = strchr( optarg, '=' );
if ( index != NULL ) {
*index = 0;
strcpy ( subsys, optarg );
level = atoi( index+1 );
if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
lutil_set_debug_level( subsys, level );
*index = '=';
}
else
{
} else {
global_level = atoi( optarg );
ldap_loglevels[0] = global_level;
/*
* if a negative number was used, make the global level the
* maximum sane level.
*/
if ( global_level < 0 )
{
if ( global_level < 0 ) {
global_level = 65535;
ldap_loglevels[0] = 65535;
}
}
}
}
}
}
}
}
void (lutil_debug)( int debug, int level, const char *fmt, ... )
......@@ -292,18 +282,16 @@ void (lutil_debug)( int debug, int level, const char *fmt, ... )
char buffer[4096];
va_list vl;
if ( !(level & debug ) )
return;
if ( !(level & debug ) ) return;
#ifdef HAVE_WINSOCK
if( log_file == NULL ) {
log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
if ( log_file == NULL )
if ( log_file == NULL ) {
log_file = fopen( "openldap.log", "w" );
if ( log_file == NULL )
return;
if ( log_file == NULL ) return;
}
ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
}
......
......@@ -48,10 +48,11 @@ ber_get_tag( BerElement *ber )
return LBER_DEFAULT;
}
if ( ber->ber_ptr == ber->ber_buf )
if ( ber->ber_ptr == ber->ber_buf ) {
tag = *(unsigned char *)ber->ber_ptr;
else
} else {
tag = ber->ber_tag;
}
ber->ber_ptr++;
if ( (tag & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {
......@@ -118,8 +119,9 @@ ber_skip_tag( BerElement *ber, ber_len_t *len )
* greater than what we can hold in a ber_len_t.
*/
if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
if ( ber_read( ber, (char *) &lc, 1 ) != 1 ) {
return LBER_DEFAULT;
}
if ( lc & 0x80U ) {
noctets = (lc & 0x7fU);
......@@ -320,8 +322,7 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen )
tag = b->ber->ber_tag;
if ( ber_first_element( b->ber, &len, &last ) != LBER_DEFAULT ) {
for ( ; b->ber->ber_ptr < last; i++ )
{
for ( ; b->ber->ber_ptr < last; i++ ) {
if (ber_skip_tag( b->ber, &len ) == LBER_DEFAULT) break;
b->ber->ber_ptr += len;
b->ber->ber_tag = *(unsigned char *)b->ber->ber_ptr;
......@@ -330,8 +331,7 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen )
if ( rlen ) *rlen = i;
if ( i == 0 )
{
if ( i == 0 ) {
*b->res.c = NULL;
return 0;
}
......@@ -341,27 +341,26 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen )
/* Allocate the result vector */
switch (b->choice) {
case ChArray:
*b->res.c = ber_memalloc_x( (n+1) * sizeof( char * ), b->ber->ber_memctx);
if ( *b->res.c == NULL )
return LBER_DEFAULT;
*b->res.c = ber_memalloc_x( (n+1)*sizeof( char * ),
b->ber->ber_memctx);
if ( *b->res.c == NULL ) return LBER_DEFAULT;
(*b->res.c)[n] = NULL;
break;
case BvArray:
*b->res.ba = ber_memalloc_x( (n+1) * sizeof( struct berval ), b->ber->ber_memctx);
if ( *b->res.ba == NULL )
return LBER_DEFAULT;
*b->res.ba = ber_memalloc_x( (n+1)*sizeof( struct berval ),
b->ber->ber_memctx);
if ( *b->res.ba == NULL ) return LBER_DEFAULT;
(*b->res.ba)[n].bv_val = NULL;
break;
case BvVec:
*b->res.bv = ber_memalloc_x( (n+1) * sizeof( struct berval *), b->ber->ber_memctx);
if ( *b->res.bv == NULL )
return LBER_DEFAULT;
*b->res.bv = ber_memalloc_x( (n+1)*sizeof( struct berval *),
b->ber->ber_memctx);
if ( *b->res.bv == NULL ) return LBER_DEFAULT;
(*b->res.bv)[n] = NULL;
break;
case BvOff:
*b->res.ba = ber_memalloc_x( (n+1) * b->siz, b->ber->ber_memctx );
if ( *b->res.ba == NULL )
return LBER_DEFAULT;
if ( *b->res.ba == NULL ) return LBER_DEFAULT;
((struct berval *)((long)(*b->res.ba) + n*b->siz +
b->off))->bv_val = NULL;
break;
......@@ -373,8 +372,9 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen )
for (n=0; n<i; n++)
{
tag = ber_next_element( b->ber, &len, last );
if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT )
if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT ) {
goto nomem;
}
/* store my result */
switch (b->choice) {
......@@ -399,17 +399,23 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen )
}
}
return tag;
nomem:
if (b->alloc || b->choice == BvVec)
{
for (--n; n>=0; n--)
{
if (b->alloc || b->choice == BvVec) {
for (--n; n>=0; n--) {
switch(b->choice) {
case ChArray: LBER_FREE((*b->res.c)[n]); break;
case BvArray: LBER_FREE((*b->res.ba)[n].bv_val); break;
case BvVec: LBER_FREE((*b->res.bv)[n]->bv_val);
LBER_FREE((*b->res.bv)[n]); break;
default: break;
case ChArray:
LBER_FREE((*b->res.c)[n]);
break;
case BvArray:
LBER_FREE((*b->res.ba)[n].bv_val);
break;
case BvVec:
LBER_FREE((*b->res.bv)[n]->bv_val);
LBER_FREE((*b->res.bv)[n]);
break;
default:
break;
}
}
}
......@@ -438,12 +444,15 @@ ber_get_stringbv( BerElement *ber, struct berval *bv, int alloc )
}
if ( alloc ) {
if ( (bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1, ber->ber_memctx )) == NULL ) {
bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1,
ber->ber_memctx );
if ( bv->bv_val == NULL ) {
return LBER_DEFAULT;
}
if ( bv->bv_len > 0 && (ber_len_t) ber_read( ber, bv->bv_val,
bv->bv_len ) != bv->bv_len ) {
bv->bv_len ) != bv->bv_len )
{
LBER_FREE( bv->bv_val );
bv->bv_val = NULL;
return LBER_DEFAULT;
......@@ -480,7 +489,8 @@ ber_get_stringal( BerElement *ber, struct berval **bv )
assert( ber != NULL );
assert( bv != NULL );
*bv = (struct berval *) ber_memalloc_x( sizeof(struct berval), ber->ber_memctx );
*bv = (struct berval *) ber_memalloc_x( sizeof(struct berval),
ber->ber_memctx );
if ( *bv == NULL ) {
return LBER_DEFAULT;
}
......@@ -515,7 +525,8 @@ ber_get_bitstringa(
}
--datalen;
if ( (*buf = (char *) ber_memalloc_x( datalen, ber->ber_memctx )) == NULL ) {
*buf = (char *) ber_memalloc_x( datalen, ber->ber_memctx );
if ( *buf == NULL ) {
return LBER_DEFAULT;
}
......@@ -649,8 +660,9 @@ ber_scanf ( BerElement *ber,
#ifdef NEW_LOGGING
LDAP_LOG( BER, ENTRY, "ber_scanf fmt (%s) ber:\n", fmt, 0, 0 );
if ( LDAP_LOGS_TEST(BER, DETAIL2 ))
if ( LDAP_LOGS_TEST(BER, DETAIL2 )) {
BER_DUMP(( "liblber", LDAP_LEVEL_DETAIL2, ber, 1 ));
}
#else
ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
"ber_scanf fmt (%s) ber:\n", fmt );
......@@ -815,13 +827,13 @@ ber_scanf ( BerElement *ber,
va_end( ap );
if ( rc == LBER_DEFAULT ) {
/*
* Error. Reclaim malloced memory that was given to the caller.
* Set allocated pointers to NULL, "data length" outvalues to 0.
*/
va_start( ap, fmt );
/*
* Error. Reclaim malloced memory that was given to the caller.
* Set allocated pointers to NULL, "data length" outvalues to 0.
*/
va_start( ap, fmt );
for ( ; fmt_reset < fmt; fmt_reset++ ) {
for ( ; fmt_reset < fmt; fmt_reset++ ) {
switch ( *fmt_reset ) {
case '!': { /* Hook */
BERDecodeCallback *f;
......@@ -904,9 +916,9 @@ ber_scanf ( BerElement *ber,
/* format should be good */
assert( 0 );
}
}
}
va_end( ap );
va_end( ap );
}
return rc;
......
......@@ -97,20 +97,16 @@ ber_calc_lenlen( ber_len_t len )
* with bit 8 0.
*/