]> git.sur5r.net Git - openldap/blob - libraries/libldap/getvalues.c
52802aa6cabe42c8cf3f6c073c0fd9bfb289f80b
[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 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <stdlib.h>
16
17 #ifdef MACOS
18 #include "macos.h"
19 #else /* MACOS */
20 #if defined( DOS ) || defined( _WIN32 )
21 #include <malloc.h>
22 #include "msdos.h"
23 #else /* DOS */
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #endif /* DOS */
27 #endif /* MACOS */
28
29 #include "lber.h"
30 #include "ldap.h"
31
32 char **
33 ldap_get_values( LDAP *ld, LDAPMessage *entry, char *target )
34 {
35         BerElement      ber;
36         char            attr[LDAP_MAX_ATTR_LEN];
37         int             found = 0;
38         long            len;
39         char            **vals;
40
41         Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 );
42
43         ber = *entry->lm_ber;
44
45         /* skip sequence, dn, sequence of, and snag the first attr */
46         len = sizeof(attr);
47         if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) {
48                 ld->ld_errno = LDAP_DECODING_ERROR;
49                 return( NULL );
50         }
51
52         if ( strcasecmp( target, attr ) == 0 )
53                 found = 1;
54
55         /* break out on success, return out on error */
56         while ( ! found ) {
57                 len = sizeof(attr);
58                 if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) {
59                         ld->ld_errno = LDAP_DECODING_ERROR;
60                         return( NULL );
61                 }
62
63                 if ( strcasecmp( target, attr ) == 0 )
64                         break;
65         }
66
67         /* 
68          * if we get this far, we've found the attribute and are sitting
69          * just before the set of values.
70          */
71
72         if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
73                 ld->ld_errno = LDAP_DECODING_ERROR;
74                 return( NULL );
75         }
76
77         return( vals );
78 }
79
80 struct berval **
81 ldap_get_values_len( LDAP *ld, LDAPMessage *entry, char *target )
82 {
83         BerElement      ber;
84         char            attr[LDAP_MAX_ATTR_LEN];
85         int             found = 0;
86         long            len;
87         struct berval   **vals;
88
89         Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 );
90
91         ber = *entry->lm_ber;
92
93         /* skip sequence, dn, sequence of, and snag the first attr */
94         len = sizeof(attr);
95         if ( ber_scanf( &ber, "{x{{s", attr, &len ) == LBER_ERROR ) {
96                 ld->ld_errno = LDAP_DECODING_ERROR;
97                 return( NULL );
98         }
99
100         if ( strcasecmp( target, attr ) == 0 )
101                 found = 1;
102
103         /* break out on success, return out on error */
104         while ( ! found ) {
105                 len = sizeof(attr);
106                 if ( ber_scanf( &ber, "x}{s", attr, &len ) == LBER_ERROR ) {
107                         ld->ld_errno = LDAP_DECODING_ERROR;
108                         return( NULL );
109                 }
110
111                 if ( strcasecmp( target, attr ) == 0 )
112                         break;
113         }
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         int     i;
152
153         if ( vals == NULL )
154                 return;
155         for ( i = 0; vals[i] != NULL; i++ )
156                 free( vals[i] );
157         free( (char *) vals );
158 }
159
160 void
161 ldap_value_free_len( struct berval **vals )
162 {
163         int     i;
164
165         if ( vals == NULL )
166                 return;
167         for ( i = 0; vals[i] != NULL; i++ ) {
168                 free( vals[i]->bv_val );
169                 free( vals[i] );
170         }
171         free( (char *) vals );
172 }