X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Fgetvalues.c;h=d2f3c2fa37cdd415fbfabdcbf57d8404b0fabb10;hb=3a2e98e91c3a8f93e5b37cb7e5a76708194cff77;hp=3abd28560a7da092c48c058e34281c138f40c2fd;hpb=30eb9ed76f5b1cdc834ff531f449e1d9ee3badf3;p=openldap diff --git a/libraries/libldap/getvalues.c b/libraries/libldap/getvalues.c index 3abd28560a..d2f3c2fa37 100644 --- a/libraries/libldap/getvalues.c +++ b/libraries/libldap/getvalues.c @@ -1,43 +1,53 @@ -/* - * Copyright (c) 1990 Regents of the University of Michigan. - * All rights reserved. +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . * - * getvalues.c + * Copyright 1998-2009 The OpenLDAP Foundation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted only as authorized by the OpenLDAP + * Public License. + * + * A copy of this license is available in the file LICENSE in the + * top-level directory of the distribution or, alternatively, at + * . + */ +/* Portions Copyright (c) 1990 Regents of the University of Michigan. + * All rights reserved. */ #include "portable.h" -#ifndef lint -static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n"; -#endif - #include -#include -#include +#include + +#include #include #include #include -#include "lber.h" -#include "ldap.h" +#include "ldap-int.h" char ** -ldap_get_values( LDAP *ld, LDAPMessage *entry, char *target ) +ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target ) { BerElement ber; - char attr[LDAP_MAX_ATTR_LEN]; + char *attr; int found = 0; - long len; char **vals; + assert( ld != NULL ); + assert( LDAP_VALID( ld ) ); + assert( entry != NULL ); + assert( target != NULL ); + Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 ); ber = *entry->lm_ber; /* skip sequence, dn, sequence of, and snag the first attr */ - len = sizeof(attr); - if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) { + if ( ber_scanf( &ber, "{x{{a" /*}}}*/, &attr ) == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; return( NULL ); } @@ -47,16 +57,22 @@ ldap_get_values( LDAP *ld, LDAPMessage *entry, char *target ) /* break out on success, return out on error */ while ( ! found ) { - len = sizeof(attr); - if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) { + LDAP_FREE(attr); + attr = NULL; + + if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; return( NULL ); } if ( strcasecmp( target, attr ) == 0 ) break; + } + LDAP_FREE(attr); + attr = NULL; + /* * if we get this far, we've found the attribute and are sitting * just before the set of values. @@ -71,21 +87,24 @@ ldap_get_values( LDAP *ld, LDAPMessage *entry, char *target ) } struct berval ** -ldap_get_values_len( LDAP *ld, LDAPMessage *entry, char *target ) +ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target ) { BerElement ber; - char attr[LDAP_MAX_ATTR_LEN]; + char *attr; int found = 0; - long len; struct berval **vals; + assert( ld != NULL ); + assert( LDAP_VALID( ld ) ); + assert( entry != NULL ); + assert( target != NULL ); + Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 ); ber = *entry->lm_ber; /* skip sequence, dn, sequence of, and snag the first attr */ - len = sizeof(attr); - if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) { + if ( ber_scanf( &ber, "{x{{a" /* }}} */, &attr ) == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; return( NULL ); } @@ -95,8 +114,10 @@ ldap_get_values_len( LDAP *ld, LDAPMessage *entry, char *target ) /* break out on success, return out on error */ while ( ! found ) { - len = sizeof(attr); - if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) { + LDAP_FREE( attr ); + attr = NULL; + + if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; return( NULL ); } @@ -105,6 +126,9 @@ ldap_get_values_len( LDAP *ld, LDAPMessage *entry, char *target ) break; } + LDAP_FREE( attr ); + attr = NULL; + /* * if we get this far, we've found the attribute and are sitting * just before the set of values. @@ -141,25 +165,47 @@ ldap_count_values_len( struct berval **vals ) void ldap_value_free( char **vals ) { - int i; - - if ( vals == NULL ) - return; - for ( i = 0; vals[i] != NULL; i++ ) - free( vals[i] ); - free( (char *) vals ); + LDAP_VFREE( vals ); } void ldap_value_free_len( struct berval **vals ) { - int i; + ber_bvecfree( vals ); +} - if ( vals == NULL ) - return; - for ( i = 0; vals[i] != NULL; i++ ) { - free( vals[i]->bv_val ); - free( vals[i] ); +char ** +ldap_value_dup( char *const *vals ) +{ + char **new; + int i; + + if( vals == NULL ) { + return NULL; + } + + for( i=0; vals[i]; i++ ) { + ; /* Count the number of values */ + } + + if( i == 0 ) { + return NULL; + } + + new = LDAP_MALLOC( (i+1)*sizeof(char *) ); /* Alloc array of pointers */ + if( new == NULL ) { + return NULL; + } + + for( i=0; vals[i]; i++ ) { + new[i] = LDAP_STRDUP( vals[i] ); /* Dup each value */ + if( new[i] == NULL ) { + LDAP_VFREE( new ); + return NULL; + } } - free( (char *) vals ); + new[i] = NULL; + + return new; } +