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