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