]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
place old schema codes behind -DSLAPD_SCHEMA_COMPAT
[openldap] / servers / slapd / value.c
1 /* value.c - routines for dealing with values */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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_fast( 
23     struct berval       ***vals,
24     struct berval       **addvals,
25     int                 nvals,
26     int                 naddvals,
27     int                 *maxvals
28 )
29 {
30         int     need, i, j;
31
32         if ( *maxvals == 0 ) {
33                 *maxvals = 1;
34         }
35         need = nvals + naddvals + 1;
36         while ( *maxvals < need ) {
37                 *maxvals *= 2;
38                 *vals = (struct berval **) ch_realloc( (char *) *vals,
39                     *maxvals * sizeof(struct berval *) );
40         }
41
42         for ( i = 0, j = 0; i < naddvals; i++ ) {
43                 if ( addvals[i]->bv_len > 0 ) {
44                         (*vals)[nvals + j] = ber_bvdup( addvals[i] );
45                         if( (*vals)[nvals + j] != NULL ) j++;
46                 }
47         }
48         (*vals)[nvals + j] = NULL;
49
50         return( 0 );
51 }
52
53 int
54 value_add( 
55     struct berval       ***vals,
56     struct berval       **addvals
57 )
58 {
59         int     n, nn, i, j;
60
61         for ( nn = 0; addvals != NULL && addvals[nn] != NULL; nn++ )
62                 ;       /* NULL */
63
64         if ( *vals == NULL ) {
65                 *vals = (struct berval **) ch_malloc( (nn + 1)
66                     * sizeof(struct berval *) );
67                 n = 0;
68         } else {
69                 for ( n = 0; (*vals)[n] != NULL; n++ )
70                         ;       /* NULL */
71                 *vals = (struct berval **) ch_realloc( (char *) *vals,
72                     (n + nn + 1) * sizeof(struct berval *) );
73         }
74
75         for ( i = 0, j = 0; i < nn; i++ ) {
76                 if ( addvals[i]->bv_len > 0 ) {
77                         (*vals)[n + j] = ber_bvdup( addvals[i] );
78                         if( (*vals)[n + j++] == NULL ) break;
79                 }
80         }
81         (*vals)[n + j] = NULL;
82
83         return( 0 );
84 }
85
86 #ifdef SLAPD_SCHEMA_COMPAT
87 void
88 value_normalize(
89     char        *s,
90     int         syntax
91 )
92 {
93         char    *d, *save;
94
95         if ( ! (syntax & SYNTAX_CIS) ) {
96                 return;
97         }
98
99         if ( syntax & SYNTAX_DN ) {
100                 (void) dn_normalize( s );
101                 return;
102         }
103
104         save = s;
105         for ( d = s; *s; s++ ) {
106                 if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
107                         continue;
108                 }
109                 *d++ = TOUPPER( (unsigned char) *s );
110         }
111         *d = '\0';
112 }
113
114 int
115 value_cmp(
116     struct berval       *v1,
117     struct berval       *v2,
118     int                 syntax,
119     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
120 )
121 {
122         int             rc;
123
124         if ( normalize & 1 ) {
125                 v1 = ber_bvdup( v1 );
126                 value_normalize( v1->bv_val, syntax );
127         }
128         if ( normalize & 2 ) {
129                 v2 = ber_bvdup( v2 );
130                 value_normalize( v2->bv_val, syntax );
131         }
132
133         switch ( syntax ) {
134         case SYNTAX_CIS:
135         case (SYNTAX_CIS | SYNTAX_TEL):
136         case (SYNTAX_CIS | SYNTAX_DN):
137                 rc = strcasecmp( v1->bv_val, v2->bv_val );
138                 break;
139
140         case SYNTAX_CES:
141                 rc = strcmp( v1->bv_val, v2->bv_val );
142                 break;
143
144         default:        /* Unknown syntax */
145         case SYNTAX_BIN:
146                 rc = (v1->bv_len == v2->bv_len
147                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
148                       : v1->bv_len > v2->bv_len ? 1 : -1);
149                 break;
150         }
151
152         if ( normalize & 1 ) {
153                 ber_bvfree( v1 );
154         }
155         if ( normalize & 2 ) {
156                 ber_bvfree( v2 );
157         }
158
159         return( rc );
160 }
161
162 int
163 value_find(
164     struct berval       **vals,
165     struct berval       *v,
166     int                 syntax,
167     int                 normalize
168 )
169 {
170         int     i;
171
172         for ( i = 0; vals[i] != NULL; i++ ) {
173                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
174                         return( 0 );
175                 }
176         }
177
178         return( 1 );
179 }
180 #endif