]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[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++, j++ ) {
43                 if ( addvals[i]->bv_len > 0 ) {
44                         (*vals)[nvals + j] = ber_bvdup( addvals[i] );
45                         if( (*vals)[nvals + j] == NULL ) break;
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 void
87 value_normalize(
88     char        *s,
89     int         syntax
90 )
91 {
92         char    *d, *save;
93
94         if ( ! (syntax & SYNTAX_CIS) ) {
95                 return;
96         }
97
98         if ( syntax & SYNTAX_DN ) {
99                 (void) dn_normalize_case( s );
100                 return;
101         }
102
103         save = s;
104         for ( d = s; *s; s++ ) {
105                 if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
106                         continue;
107                 }
108                 *d++ = TOUPPER( (unsigned char) *s );
109         }
110         *d = '\0';
111 }
112
113 int
114 value_cmp(
115     struct berval       *v1,
116     struct berval       *v2,
117     int                 syntax,
118     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
119 )
120 {
121         int             rc;
122
123         if ( normalize & 1 ) {
124                 v1 = ber_bvdup( v1 );
125                 value_normalize( v1->bv_val, syntax );
126         }
127         if ( normalize & 2 ) {
128                 v2 = ber_bvdup( v2 );
129                 value_normalize( v2->bv_val, syntax );
130         }
131
132         switch ( syntax ) {
133         case SYNTAX_CIS:
134         case (SYNTAX_CIS | SYNTAX_TEL):
135         case (SYNTAX_CIS | SYNTAX_DN):
136                 rc = strcasecmp( v1->bv_val, v2->bv_val );
137                 break;
138
139         case SYNTAX_CES:
140                 rc = strcmp( v1->bv_val, v2->bv_val );
141                 break;
142
143         default:        /* Unknown syntax */
144         case SYNTAX_BIN:
145                 rc = (v1->bv_len == v2->bv_len
146                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
147                       : v1->bv_len > v2->bv_len ? 1 : -1);
148                 break;
149         }
150
151         if ( normalize & 1 ) {
152                 ber_bvfree( v1 );
153         }
154         if ( normalize & 2 ) {
155                 ber_bvfree( v2 );
156         }
157
158         return( rc );
159 }
160
161 int
162 value_find(
163     struct berval       **vals,
164     struct berval       *v,
165     int                 syntax,
166     int                 normalize
167 )
168 {
169         int     i;
170
171         for ( i = 0; vals[i] != NULL; i++ ) {
172                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
173                         return( 0 );
174                 }
175         }
176
177         return( 1 );
178 }