]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
Fix uninitialized variable
[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                         if( (*vals)[nvals + j] == NULL ) break;
45                 }
46         }
47         (*vals)[nvals + j] = NULL;
48
49         return( 0 );
50 }
51
52 int
53 value_add( 
54     struct berval       ***vals,
55     struct berval       **addvals
56 )
57 {
58         int     n, nn, i, j;
59
60         for ( nn = 0; addvals != NULL && addvals[nn] != NULL; nn++ )
61                 ;       /* NULL */
62
63         if ( *vals == NULL ) {
64                 *vals = (struct berval **) ch_malloc( (nn + 1)
65                     * sizeof(struct berval *) );
66                 n = 0;
67         } else {
68                 for ( n = 0; (*vals)[n] != NULL; n++ )
69                         ;       /* NULL */
70                 *vals = (struct berval **) ch_realloc( (char *) *vals,
71                     (n + nn + 1) * sizeof(struct berval *) );
72         }
73
74         for ( i = 0, j = 0; i < nn; i++ ) {
75                 if ( addvals[i]->bv_len > 0 ) {
76                         (*vals)[n + j] = ber_bvdup( addvals[i] );
77                         if( (*vals)[n + j++] == NULL ) break;
78                 }
79         }
80         (*vals)[n + j] = NULL;
81
82         return( 0 );
83 }
84
85 void
86 value_normalize(
87     char        *s,
88     int         syntax
89 )
90 {
91         char    *d, *save;
92
93         if ( ! (syntax & SYNTAX_CIS) ) {
94                 return;
95         }
96
97         if ( syntax & SYNTAX_DN ) {
98                 (void) dn_normalize_case( s );
99                 return;
100         }
101
102         save = s;
103         for ( d = s; *s; s++ ) {
104                 if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
105                         continue;
106                 }
107                 *d++ = TOUPPER( (unsigned char) *s );
108         }
109         *d = '\0';
110 }
111
112 int
113 value_cmp(
114     struct berval       *v1,
115     struct berval       *v2,
116     int                 syntax,
117     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
118 )
119 {
120         int             rc;
121
122         if ( normalize & 1 ) {
123                 v1 = ber_bvdup( v1 );
124                 value_normalize( v1->bv_val, syntax );
125         }
126         if ( normalize & 2 ) {
127                 v2 = ber_bvdup( v2 );
128                 value_normalize( v2->bv_val, syntax );
129         }
130
131         switch ( syntax ) {
132         case SYNTAX_CIS:
133         case (SYNTAX_CIS | SYNTAX_TEL):
134         case (SYNTAX_CIS | SYNTAX_DN):
135                 rc = strcasecmp( v1->bv_val, v2->bv_val );
136                 break;
137
138         case SYNTAX_CES:
139                 rc = strcmp( v1->bv_val, v2->bv_val );
140                 break;
141
142         case SYNTAX_BIN:
143                 rc = (v1->bv_len == v2->bv_len
144                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
145                       : v1->bv_len > v2->bv_len ? 1 : -1);
146                 break;
147         }
148
149         if ( normalize & 1 ) {
150                 ber_bvfree( v1 );
151         }
152         if ( normalize & 2 ) {
153                 ber_bvfree( v2 );
154         }
155
156         return( rc );
157 }
158
159 int
160 value_find(
161     struct berval       **vals,
162     struct berval       *v,
163     int                 syntax,
164     int                 normalize
165 )
166 {
167         int     i;
168
169         for ( i = 0; vals[i] != NULL; i++ ) {
170                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
171                         return( 0 );
172                 }
173         }
174
175         return( 1 );
176 }