]> git.sur5r.net Git - openldap/blob - libraries/libldap/getvalues.c
More files that didn't get merged properly.
[openldap] / libraries / libldap / getvalues.c
1 /*
2  *  Copyright (c) 1990 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  getvalues.c
6  */
7
8 #include "portable.h"
9
10 #ifndef lint 
11 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
12 #endif
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 "lber.h"
23 #include "ldap.h"
24
25 char **
26 ldap_get_values( LDAP *ld, LDAPMessage *entry, char *target )
27 {
28         BerElement      ber;
29         char            attr[LDAP_MAX_ATTR_LEN];
30         int             found = 0;
31         long            len;
32         char            **vals;
33
34         Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 );
35
36         ber = *entry->lm_ber;
37
38         /* skip sequence, dn, sequence of, and snag the first attr */
39         len = sizeof(attr);
40         if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) {
41                 ld->ld_errno = LDAP_DECODING_ERROR;
42                 return( NULL );
43         }
44
45         if ( strcasecmp( target, attr ) == 0 )
46                 found = 1;
47
48         /* break out on success, return out on error */
49         while ( ! found ) {
50                 len = sizeof(attr);
51                 if ( ber_scanf( &ber, "x}{s", attr, &len ) == 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          * if we get this far, we've found the attribute and are sitting
62          * just before the set of values.
63          */
64
65         if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
66                 ld->ld_errno = LDAP_DECODING_ERROR;
67                 return( NULL );
68         }
69
70         return( vals );
71 }
72
73 struct berval **
74 ldap_get_values_len( LDAP *ld, LDAPMessage *entry, char *target )
75 {
76         BerElement      ber;
77         char            attr[LDAP_MAX_ATTR_LEN];
78         int             found = 0;
79         long            len;
80         struct berval   **vals;
81
82         Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 );
83
84         ber = *entry->lm_ber;
85
86         /* skip sequence, dn, sequence of, and snag the first attr */
87         len = sizeof(attr);
88         if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) {
89                 ld->ld_errno = LDAP_DECODING_ERROR;
90                 return( NULL );
91         }
92
93         if ( strcasecmp( target, attr ) == 0 )
94                 found = 1;
95
96         /* break out on success, return out on error */
97         while ( ! found ) {
98                 len = sizeof(attr);
99                 if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) {
100                         ld->ld_errno = LDAP_DECODING_ERROR;
101                         return( NULL );
102                 }
103
104                 if ( strcasecmp( target, attr ) == 0 )
105                         break;
106         }
107
108         /* 
109          * if we get this far, we've found the attribute and are sitting
110          * just before the set of values.
111          */
112
113         if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) {
114                 ld->ld_errno = LDAP_DECODING_ERROR;
115                 return( NULL );
116         }
117
118         return( vals );
119 }
120
121 int
122 ldap_count_values( char **vals )
123 {
124         int     i;
125
126         if ( vals == NULL )
127                 return( 0 );
128
129         for ( i = 0; vals[i] != NULL; i++ )
130                 ;       /* NULL */
131
132         return( i );
133 }
134
135 int
136 ldap_count_values_len( struct berval **vals )
137 {
138         return( ldap_count_values( (char **) vals ) );
139 }
140
141 void
142 ldap_value_free( char **vals )
143 {
144         int     i;
145
146         if ( vals == NULL )
147                 return;
148         for ( i = 0; vals[i] != NULL; i++ )
149                 free( vals[i] );
150         free( (char *) vals );
151 }
152
153 void
154 ldap_value_free_len( struct berval **vals )
155 {
156         int     i;
157
158         if ( vals == NULL )
159                 return;
160         for ( i = 0; vals[i] != NULL; i++ ) {
161                 free( vals[i]->bv_val );
162                 free( vals[i] );
163         }
164         free( (char *) vals );
165 }