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