]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
9c922607015d6a971fcf08d7c62b07c37f648396
[openldap] / servers / slapd / value.c
1 /* value.c - routines for dealing with values */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16
17 #include <sys/stat.h>
18
19 #include "slap.h"
20
21 int
22 value_add( 
23     struct berval       ***vals,
24     struct berval       **addvals
25 )
26 {
27         int     n, nn, i, j;
28
29         for ( nn = 0; addvals != NULL && addvals[nn] != NULL; nn++ )
30                 ;       /* NULL */
31
32         if ( *vals == NULL ) {
33                 *vals = (struct berval **) ch_malloc( (nn + 1)
34                     * sizeof(struct berval *) );
35                 n = 0;
36         } else {
37                 for ( n = 0; (*vals)[n] != NULL; n++ )
38                         ;       /* NULL */
39                 *vals = (struct berval **) ch_realloc( (char *) *vals,
40                     (n + nn + 1) * sizeof(struct berval *) );
41         }
42
43         for ( i = 0, j = 0; i < nn; i++ ) {
44                 if ( addvals[i]->bv_len > 0 ) {
45                         (*vals)[n + j] = ber_bvdup( addvals[i] );
46                         if( (*vals)[n + j++] == NULL ) break;
47                 }
48         }
49         (*vals)[n + j] = NULL;
50
51         return( 0 );
52 }
53
54 #ifdef SLAPD_SCHEMA_NOT_COMPAT
55         /* not yet implemented */
56 #else
57 int
58 value_add_fast( 
59     struct berval       ***vals,
60     struct berval       **addvals,
61     int                 nvals,
62     int                 naddvals,
63     int                 *maxvals
64 )
65 {
66         int     need, i, j;
67
68         if ( *maxvals == 0 ) {
69                 *maxvals = 1;
70         }
71         need = nvals + naddvals + 1;
72         while ( *maxvals < need ) {
73                 *maxvals *= 2;
74                 *vals = (struct berval **) ch_realloc( (char *) *vals,
75                     *maxvals * sizeof(struct berval *) );
76         }
77
78         for ( i = 0, j = 0; i < naddvals; i++ ) {
79                 if ( addvals[i]->bv_len > 0 ) {
80                         (*vals)[nvals + j] = ber_bvdup( addvals[i] );
81                         if( (*vals)[nvals + j] != NULL ) j++;
82                 }
83         }
84         (*vals)[nvals + j] = NULL;
85
86         return( 0 );
87 }
88 #endif
89
90 #ifdef SLAPD_SCHEMA_NOT_COMPAT
91 int
92 value_normalize(
93         AttributeDescription *ad,
94         unsigned usage,
95         struct berval *in,
96         struct berval **out,
97         char **text )
98 {
99         int rc;
100         MatchingRule *mr;
101
102         switch( usage & SLAP_MR_TYPE_MASK ) {
103         case SLAP_MR_NONE:
104         case SLAP_MR_EQUALITY:
105                 mr = ad->ad_type->sat_equality;
106                 break;
107         case SLAP_MR_ORDERING:
108                 mr = ad->ad_type->sat_ordering;
109                 break;
110         case SLAP_MR_SUBSTR:
111                 mr = ad->ad_type->sat_substr;
112                 break;
113         case SLAP_MR_EXT:
114         default:
115                 assert( 0 );
116                 *text = "internal error";
117                 return LDAP_OTHER;
118         }
119
120         if( mr == NULL ) {
121                 *text = "inappropriate matching request";
122                 return LDAP_INAPPROPRIATE_MATCHING;
123         }
124
125         /* we only support equality matching of binary attributes */
126         if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
127                 *text = "inappropriate binary matching";
128                 return LDAP_INAPPROPRIATE_MATCHING;
129         }
130
131         rc = (mr->smr_normalize)( usage,
132                 ad->ad_type->sat_syntax,
133                 mr, in, out );
134
135         if( rc != LDAP_SUCCESS ) {
136                 *text = "unable to normalize value";
137                 return LDAP_INVALID_SYNTAX;
138         }
139
140         return LDAP_SUCCESS;
141 }
142
143 #else
144 void
145 value_normalize(
146     char        *s,
147     int         syntax
148 )
149 {
150         char    *d, *save;
151
152         if ( ! (syntax & SYNTAX_CIS) ) {
153                 return;
154         }
155
156         if ( syntax & SYNTAX_DN ) {
157                 (void) dn_normalize( s );
158                 return;
159         }
160
161         save = s;
162         for ( d = s; *s; s++ ) {
163                 if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
164                         continue;
165                 }
166                 *d++ = TOUPPER( (unsigned char) *s );
167         }
168         *d = '\0';
169 }
170 #endif
171
172 #ifdef SLAPD_SCHEMA_NOT_COMPAT
173         /* not yet implemented */
174 #else
175 int
176 value_cmp(
177     struct berval       *v1,
178     struct berval       *v2,
179     int                 syntax,
180     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
181 )
182 {
183         int             rc;
184
185         if ( normalize & 1 ) {
186                 v1 = ber_bvdup( v1 );
187                 value_normalize( v1->bv_val, syntax );
188         }
189         if ( normalize & 2 ) {
190                 v2 = ber_bvdup( v2 );
191                 value_normalize( v2->bv_val, syntax );
192         }
193
194         switch ( syntax ) {
195         case SYNTAX_CIS:
196         case (SYNTAX_CIS | SYNTAX_TEL):
197         case (SYNTAX_CIS | SYNTAX_DN):
198                 rc = strcasecmp( v1->bv_val, v2->bv_val );
199                 break;
200
201         case SYNTAX_CES:
202                 rc = strcmp( v1->bv_val, v2->bv_val );
203                 break;
204
205         default:        /* Unknown syntax */
206         case SYNTAX_BIN:
207                 rc = (v1->bv_len == v2->bv_len
208                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
209                       : v1->bv_len > v2->bv_len ? 1 : -1);
210                 break;
211         }
212
213         if ( normalize & 1 ) {
214                 ber_bvfree( v1 );
215         }
216         if ( normalize & 2 ) {
217                 ber_bvfree( v2 );
218         }
219
220         return( rc );
221 }
222
223 int
224 value_find(
225     struct berval       **vals,
226     struct berval       *v,
227     int                 syntax,
228     int                 normalize
229 )
230 {
231         int     i;
232
233         for ( i = 0; vals[i] != NULL; i++ ) {
234                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
235                         return( 0 );
236                 }
237         }
238
239         return( 1 );
240 }
241 #endif