summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/value.c
blob: ad17806982cd235b1f099b708f12e00273dd4ab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/* value.c - routines for dealing with values */

#undef DEBUG                    /* disable counters */
#include <prcountr.h>
#include "slap.h"
#include "slapi-private.h"

/*
 * Functions needed when a berval is embedded in a struct or
 * allocated on the stack rather than the heap.
 */

static void ber_bvdone(struct berval *bvp)
{
    if (bvp == NULL) return;

    slapi_ch_free_string(&bvp->bv_val);
    bvp->bv_len = 0;

    return;
}

static void ber_bvcpy(struct berval *bvd, const struct berval *bvs)
{
    size_t len;

    if (bvd == NULL || bvs == NULL) return;

    len = bvs->bv_len;
    bvd->bv_val = slapi_ch_malloc(len+1);
    bvd->bv_len = len;
    memcpy(bvd->bv_val, bvs->bv_val, len);
    bvd->bv_val[len] = '\0';

    return;
}

/* <=========================== Slapi_Value ==========================> */

#ifdef VALUE_DEBUG
static void value_dump( const Slapi_Value *value, const char *text);
#define VALUE_DUMP(value,name) value_dump(value,name)
#else
#define VALUE_DUMP(value,name) ((void)0)
#endif

static int counters_created= 0;
PR_DEFINE_COUNTER(slapi_value_counter_created);
PR_DEFINE_COUNTER(slapi_value_counter_deleted);
PR_DEFINE_COUNTER(slapi_value_counter_exist);

Slapi_Value *
slapi_value_new()
{
    return value_new(NULL,CSN_TYPE_NONE,NULL);
}

Slapi_Value *
slapi_value_new_berval(const struct berval *bval)
{
    return value_new(bval,CSN_TYPE_NONE,NULL);
}

Slapi_Value *
slapi_value_new_value(const Slapi_Value *v)
{
    return slapi_value_dup(v);
}

Slapi_Value *
slapi_value_new_string(const char *s)
{
    Slapi_Value *v= value_new(NULL,CSN_TYPE_UNKNOWN,NULL);
	slapi_value_set_string(v, s);
	return v;
}

Slapi_Value *
slapi_value_new_string_passin(char *s)
{
    Slapi_Value *v= value_new(NULL,CSN_TYPE_UNKNOWN,NULL);
	slapi_value_set_string_passin(v, s);
	return v;
}

Slapi_Value *
slapi_value_init(Slapi_Value *v)
{
    return value_init(v,NULL,CSN_TYPE_NONE,NULL);
}

Slapi_Value *
slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
{
    return value_init(v,bval,CSN_TYPE_NONE,NULL);
}

Slapi_Value *
slapi_value_init_string(Slapi_Value *v,const char *s)
{
    value_init(v,NULL,CSN_TYPE_UNKNOWN,NULL);
	slapi_value_set_string(v,s);
	return v;
}

Slapi_Value *
slapi_value_init_string_passin(Slapi_Value *v, char *s)
{
    value_init(v,NULL,CSN_TYPE_UNKNOWN,NULL);
	slapi_value_set_string_passin(v,s);
	return v;
}

Slapi_Value *
slapi_value_dup(const Slapi_Value *v)
{
	Slapi_Value *newvalue= value_new(&v->bv,CSN_TYPE_UNKNOWN,NULL);
	newvalue->v_csnset= csnset_dup(v->v_csnset);
	return newvalue;

}

Slapi_Value *
value_new(const struct berval *bval,CSNType t,const CSN *csn)
{
    Slapi_Value *v;
    v = (Slapi_Value *)slapi_ch_malloc(sizeof(Slapi_Value));
	value_init(v, bval, t, csn);
	if(!counters_created)
	{
		PR_CREATE_COUNTER(slapi_value_counter_created,"Slapi_Value","created","");
		PR_CREATE_COUNTER(slapi_value_counter_deleted,"Slapi_Value","deleted","");
		PR_CREATE_COUNTER(slapi_value_counter_exist,"Slapi_Value","exist","");
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_value_counter_created);
    PR_INCREMENT_COUNTER(slapi_value_counter_exist);
	VALUE_DUMP(v,"value_new");
    return v;
}

Slapi_Value *
value_init(Slapi_Value *v, const struct berval *bval,CSNType t,const CSN *csn)
{
	PR_ASSERT(v!=NULL);
	memset(v,0,sizeof(Slapi_Value));
	if(csn!=NULL)
	{
		value_update_csn(v,t,csn);
	}
	slapi_value_set_berval(v,bval);
    return v;
}

void 
slapi_value_free(Slapi_Value **v)
{
    if(v!=NULL && *v!=NULL)
    {
		VALUE_DUMP(*v,"value_free");
		value_done(*v);
        slapi_ch_free((void **)v);
		*v= NULL;
        PR_INCREMENT_COUNTER(slapi_value_counter_deleted);
	    PR_DECREMENT_COUNTER(slapi_value_counter_exist);
    }
}

void
value_done(Slapi_Value *v)
{
	if(v!=NULL)
	{
	    if(NULL != v->v_csnset) 
	    {
	        csnset_free(&(v->v_csnset));
	    }
            ber_bvdone(&v->bv);
	}
}

const CSNSet *
value_get_csnset ( const Slapi_Value *value)
{
    if (value)
        return value->v_csnset;
    else
        return NULL;
}

const CSN *
value_get_csn( const Slapi_Value *value, CSNType t)
{
    const CSN *csn= NULL;
    if(NULL!=value)
    {
		csn= csnset_get_csn_of_type(value->v_csnset, t);
    }
    return csn;
}

int
value_contains_csn( const Slapi_Value *value, CSN *csn)
{
	int r= 0;
    if(NULL!=value)
    {
		r= csnset_contains(value->v_csnset, csn);
    }
    return r;
}

Slapi_Value * 
value_update_csn( Slapi_Value *value, CSNType t, const CSN *csn)
{
    if(value!=NULL)
    {
		csnset_update_csn(&value->v_csnset,t,csn);
    }
    return value;
}

Slapi_Value * 
value_add_csn( Slapi_Value *value, CSNType t, const CSN *csn)
{
    if(value!=NULL)
    {
		csnset_add_csn(&value->v_csnset,t,csn);
    }
    return value;
}

const struct berval *
slapi_value_get_berval( const Slapi_Value *value )
{
    const struct berval *bval= NULL;
    if(NULL != value)
    {
        bval = &value->bv;
    }
    return bval;
}

Slapi_Value * 
slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
{
	struct berval bv;
	bv.bv_len= len;
	bv.bv_val= (void*)val; /* We cast away the const, but we're not going to change anything */
	slapi_value_set_berval( value, &bv);
	return value;
}

Slapi_Value * 
slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
{
	slapi_value_set_berval( value, &vfrom->bv );
	csnset_free(&value->v_csnset);
	value->v_csnset= csnset_dup(vfrom->v_csnset);
	return value;
}

Slapi_Value * 
value_remove_csn( Slapi_Value *value, CSNType t)
{
    if(value!=NULL)
    {
		csnset_remove_csn(&value->v_csnset,t);
    }
    return value;
}

Slapi_Value * 
slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
{
    if(value!=NULL)
    {
        ber_bvdone(&value->bv);
        if(bval!=NULL)
        {
            ber_bvcpy(&value->bv, bval);
        }
    }
    return value;
}

int
slapi_value_set_string(Slapi_Value *value, const char *strVal)
{
  return slapi_value_set_string_passin(value, slapi_ch_strdup(strVal));
}

int
slapi_value_set_string_passin(Slapi_Value *value, char *strVal)
{
    int rc= -1;
    if(NULL != value)
    {
        ber_bvdone(&value->bv);
        value->bv.bv_val = strVal;
        value->bv.bv_len = strlen(strVal);
        rc= 0;
    }
    return rc;
}

int
slapi_value_set_int(Slapi_Value *value, int intVal)
{
    int rc= -1;
    if(NULL != value)
    {
        char valueBuf[80];
        ber_bvdone(&value->bv);
        sprintf(valueBuf,"%d",intVal);
        value->bv.bv_val = slapi_ch_strdup(valueBuf);
        value->bv.bv_len = strlen(value->bv.bv_val);
        rc= 0;
    }
    return rc;
}

/*
 * Warning: The value may not be '\0' terminated!
 * Make sure that you know this is a C string.
 */
const char *
slapi_value_get_string(const Slapi_Value *value)
{
	const char *r= NULL;
	if(value!=NULL)
	{
		r= (const char*)value->bv.bv_val;
	}
	return r;
}

size_t
slapi_value_get_length(const Slapi_Value *value)
{
	size_t r= 0;
	if(NULL!=value)
	{
		r= value->bv.bv_len;
	}
	return r;
}

int 
slapi_value_get_int(const Slapi_Value *value)
{
	int r= 0;
	if(NULL!=value)
	{
		char *p;
        p = slapi_ch_malloc(value->bv.bv_len + 1);
        memcpy (p, value->bv.bv_val, value->bv.bv_len);
        p [value->bv.bv_len] = '\0';
        r= atoi(p);
        slapi_ch_free((void **)&p);
	}
	return r;
}

unsigned int 
slapi_value_get_uint(const Slapi_Value *value)
{
	unsigned int r= 0;
	if(NULL!=value)
	{
		char *p;
        p = slapi_ch_malloc(value->bv.bv_len + 1);
        memcpy (p, value->bv.bv_val, value->bv.bv_len);
        p [value->bv.bv_len] = '\0';
        r= (unsigned int)atoi(p);
        slapi_ch_free((void **)&p);
	}
	return r;
}

long 
slapi_value_get_long(const Slapi_Value *value)
{
	long r= 0;
	if(NULL!=value)
	{
		char *p;
        p = slapi_ch_malloc(value->bv.bv_len + 1);
        memcpy (p, value->bv.bv_val, value->bv.bv_len);
        p [value->bv.bv_len] = '\0';
        r = atol(p);
        slapi_ch_free((void **)&p);
	}
	return r;
}

unsigned long 
slapi_value_get_ulong(const Slapi_Value *value)
{
	unsigned long r= 0;
	if(NULL!=value)
	{
		char *p;
        p = slapi_ch_malloc(value->bv.bv_len + 1);
        memcpy (p, value->bv.bv_val, value->bv.bv_len);
        p [value->bv.bv_len] = '\0';
        r = (unsigned long)atol(p);
        slapi_ch_free((void **)&p);
	}
	return r;
}

int
slapi_value_compare(const Slapi_Attr *a,const Slapi_Value *v1,const Slapi_Value *v2)
{
	int r= 0;
	if(v1!=NULL && v2!=NULL)
	{
            r= slapi_attr_value_cmp( a, &v1->bv, &v2->bv);
	}
	else if(v1!=NULL && v2==NULL)
	{
            r= 1; /* v1>v2 */
	}
	else if (v1==NULL && v2!=NULL)
	{
            r= -1; /* v1<v2 */
	}
	else /* (v1==NULL && v2==NULL) */
	{
            r= 0; /* The same */
	}
	return r;
}

size_t
value_size(const Slapi_Value *v)
{
	size_t s= v->bv.bv_len;
	s += csnset_size(v->v_csnset);
        s += sizeof(Slapi_Value);
	return s;
}


#ifdef VALUE_DEBUG
static void
value_dump( const Slapi_Value *value, const char *text)
{
    LDAPDebug( LDAP_DEBUG_ANY, "Slapi_Value %s ptr=%lx\n", text, value, 0);
	/* JCM - Dump value contents... */
}
#endif