]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
Merge in all -devel changes made since branch was created.
[openldap] / servers / slapd / value.c
1 /* value.c - routines for dealing with values */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/ctype.h>
12 #include <ac/socket.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15
16 #include <sys/stat.h>
17
18 #include "slap.h"
19
20 int
21 value_add_fast( 
22     struct berval       ***vals,
23     struct berval       **addvals,
24     int                 nvals,
25     int                 naddvals,
26     int                 *maxvals
27 )
28 {
29         int     need, i, j;
30
31         if ( *maxvals == 0 ) {
32                 *maxvals = 1;
33         }
34         need = nvals + naddvals + 1;
35         while ( *maxvals < need ) {
36                 *maxvals *= 2;
37                 *vals = (struct berval **) ch_realloc( (char *) *vals,
38                     *maxvals * sizeof(struct berval *) );
39         }
40
41         for ( i = 0, j = 0; i < naddvals; i++, j++ ) {
42                 if ( addvals[i]->bv_len > 0 ) {
43                         (*vals)[nvals + j] = ber_bvdup( addvals[i] );
44                 }
45         }
46         (*vals)[nvals + j] = NULL;
47
48         return( 0 );
49 }
50
51 int
52 value_add( 
53     struct berval       ***vals,
54     struct berval       **addvals
55 )
56 {
57         int     n, nn, i, j;
58
59         for ( nn = 0; addvals != NULL && addvals[nn] != NULL; nn++ )
60                 ;       /* NULL */
61
62         if ( *vals == NULL ) {
63                 *vals = (struct berval **) ch_malloc( (nn + 1)
64                     * sizeof(struct berval *) );
65                 n = 0;
66         } else {
67                 for ( n = 0; (*vals)[n] != NULL; n++ )
68                         ;       /* NULL */
69                 *vals = (struct berval **) ch_realloc( (char *) *vals,
70                     (n + nn + 1) * sizeof(struct berval *) );
71         }
72
73         for ( i = 0, j = 0; i < nn; i++ ) {
74                 if ( addvals[i]->bv_len > 0 ) {
75                         (*vals)[n + j++] = ber_bvdup( addvals[i] );
76                 }
77         }
78         (*vals)[n + j] = NULL;
79
80         return( 0 );
81 }
82
83 void
84 value_normalize(
85     char        *s,
86     int         syntax
87 )
88 {
89         char    *d, *save;
90
91         if ( ! (syntax & SYNTAX_CIS) ) {
92                 return;
93         }
94
95         if ( syntax & SYNTAX_DN ) {
96                 (void) dn_normalize_case( s );
97                 return;
98         }
99
100         save = s;
101         for ( d = s; *s; s++ ) {
102                 if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
103                         continue;
104                 }
105                 *d++ = TOUPPER( (unsigned char) *s );
106         }
107         *d = '\0';
108 }
109
110 int
111 value_cmp(
112     struct berval       *v1,
113     struct berval       *v2,
114     int                 syntax,
115     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
116 )
117 {
118         int             rc;
119
120         if ( normalize & 1 ) {
121                 v1 = ber_bvdup( v1 );
122                 value_normalize( v1->bv_val, syntax );
123         }
124         if ( normalize & 2 ) {
125                 v2 = ber_bvdup( v2 );
126                 value_normalize( v2->bv_val, syntax );
127         }
128
129         switch ( syntax ) {
130         case SYNTAX_CIS:
131         case (SYNTAX_CIS | SYNTAX_TEL):
132         case (SYNTAX_CIS | SYNTAX_DN):
133                 rc = strcasecmp( v1->bv_val, v2->bv_val );
134                 break;
135
136         case SYNTAX_CES:
137                 rc = strcmp( v1->bv_val, v2->bv_val );
138                 break;
139
140         case SYNTAX_BIN:
141                 rc = (v1->bv_len == v2->bv_len
142                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
143                       : v1->bv_len > v2->bv_len ? 1 : -1);
144                 break;
145         }
146
147         if ( normalize & 1 ) {
148                 ber_bvfree( v1 );
149         }
150         if ( normalize & 2 ) {
151                 ber_bvfree( v2 );
152         }
153
154         return( rc );
155 }
156
157 int
158 value_find(
159     struct berval       **vals,
160     struct berval       *v,
161     int                 syntax,
162     int                 normalize
163 )
164 {
165         int     i;
166
167         for ( i = 0; vals[i] != NULL; i++ ) {
168                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
169                         return( 0 );
170                 }
171         }
172
173         return( 1 );
174 }