]> git.sur5r.net Git - openldap/blob - libraries/libldap/getvalues.c
Merge in all devel changes since 2.0-alpha2.
[openldap] / libraries / libldap / getvalues.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  getvalues.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/stdlib.h>
18
19 #include <ac/ctype.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23
24 #include "ldap-int.h"
25
26 char **
27 ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
28 {
29         BerElement      ber;
30         char            *attr;
31         int             found = 0;
32         char            **vals;
33
34         assert( ld != NULL );
35         assert( LDAP_VALID( ld ) );
36         assert( entry != NULL );
37         assert( target != NULL );
38
39         Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 );
40
41         ber = *entry->lm_ber;
42
43         /* skip sequence, dn, sequence of, and snag the first attr */
44         if ( ber_scanf( &ber, "{x{{a" /*}}}*/, &attr ) == LBER_ERROR ) {
45                 ld->ld_errno = LDAP_DECODING_ERROR;
46                 return( NULL );
47         }
48
49         if ( strcasecmp( target, attr ) == 0 )
50                 found = 1;
51
52         /* break out on success, return out on error */
53         while ( ! found ) {
54                 LDAP_FREE(attr);
55                 attr = NULL;
56
57                 if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
58                         ld->ld_errno = LDAP_DECODING_ERROR;
59                         return( NULL );
60                 }
61
62                 if ( strcasecmp( target, attr ) == 0 )
63                         break;
64
65         }
66
67         LDAP_FREE(attr);
68         attr = NULL;
69
70         /* 
71          * if we get this far, we've found the attribute and are sitting
72          * just before the set of values.
73          */
74
75         if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
76                 ld->ld_errno = LDAP_DECODING_ERROR;
77                 return( NULL );
78         }
79
80         return( vals );
81 }
82
83 struct berval **
84 ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
85 {
86         BerElement      ber;
87         char            *attr;
88         int             found = 0;
89         struct berval   **vals;
90
91         assert( ld != NULL );
92         assert( LDAP_VALID( ld ) );
93         assert( entry != NULL );
94         assert( target != NULL );
95
96         Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 );
97
98         ber = *entry->lm_ber;
99
100         /* skip sequence, dn, sequence of, and snag the first attr */
101         if ( ber_scanf( &ber, "{x{{a" /* }}} */, &attr ) == LBER_ERROR ) {
102                 ld->ld_errno = LDAP_DECODING_ERROR;
103                 return( NULL );
104         }
105
106         if ( strcasecmp( target, attr ) == 0 )
107                 found = 1;
108
109         /* break out on success, return out on error */
110         while ( ! found ) {
111                 LDAP_FREE( attr );
112                 attr = NULL;
113
114                 if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
115                         ld->ld_errno = LDAP_DECODING_ERROR;
116                         return( NULL );
117                 }
118
119                 if ( strcasecmp( target, attr ) == 0 )
120                         break;
121         }
122
123         LDAP_FREE( attr );
124         attr = NULL;
125
126         /* 
127          * if we get this far, we've found the attribute and are sitting
128          * just before the set of values.
129          */
130
131         if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) {
132                 ld->ld_errno = LDAP_DECODING_ERROR;
133                 return( NULL );
134         }
135
136         return( vals );
137 }
138
139 int
140 ldap_count_values( char **vals )
141 {
142         int     i;
143
144         if ( vals == NULL )
145                 return( 0 );
146
147         for ( i = 0; vals[i] != NULL; i++ )
148                 ;       /* NULL */
149
150         return( i );
151 }
152
153 int
154 ldap_count_values_len( struct berval **vals )
155 {
156         return( ldap_count_values( (char **) vals ) );
157 }
158
159 void
160 ldap_value_free( char **vals )
161 {
162         LDAP_VFREE( vals );
163 }
164
165 void
166 ldap_value_free_len( struct berval **vals )
167 {
168         ber_bvecfree( vals );
169 }