1 /* index.c - index utilities */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2021 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 
17 #include "portable.h"
18 
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <lutil.h>
22 
23 #include "slap.h"
24 
25 static slap_verbmasks idxstr[] = {
26 	{ BER_BVC("pres"), SLAP_INDEX_PRESENT },
27 	{ BER_BVC("eq"), SLAP_INDEX_EQUALITY },
28 	{ BER_BVC("approx"), SLAP_INDEX_APPROX },
29 	{ BER_BVC("subinitial"), SLAP_INDEX_SUBSTR_INITIAL },
30 	{ BER_BVC("subany"), SLAP_INDEX_SUBSTR_ANY },
31 	{ BER_BVC("subfinal"), SLAP_INDEX_SUBSTR_FINAL },
32 	{ BER_BVC("sub"), SLAP_INDEX_SUBSTR_DEFAULT },
33 	{ BER_BVC("substr"), 0 },
34 	{ BER_BVC("notags"), SLAP_INDEX_NOTAGS },
35 	{ BER_BVC("nolang"), 0 },	/* backwards compat */
36 	{ BER_BVC("nosubtypes"), SLAP_INDEX_NOSUBTYPES },
37 	{ BER_BVNULL, 0 }
38 };
39 
40 
slap_str2index(const char * str,slap_mask_t * idx)41 int slap_str2index( const char *str, slap_mask_t *idx )
42 {
43 	int i;
44 
45 	i = verb_to_mask( str, idxstr );
46 	if ( BER_BVISNULL(&idxstr[i].word) ) return LDAP_OTHER;
47 	while ( !idxstr[i].mask ) i--;
48 	*idx = idxstr[i].mask;
49 
50 
51 	return LDAP_SUCCESS;
52 }
53 
slap_index2bvlen(slap_mask_t idx,struct berval * bv)54 void slap_index2bvlen( slap_mask_t idx, struct berval *bv )
55 {
56 	int i;
57 
58 	bv->bv_len = 0;
59 
60 	for ( i=0; !BER_BVISNULL( &idxstr[i].word ); i++ ) {
61 		if ( !idxstr[i].mask ) continue;
62 		if ( IS_SLAP_INDEX( idx, idxstr[i].mask )) {
63 			if ( (idxstr[i].mask & SLAP_INDEX_SUBSTR) &&
64 				((idx & SLAP_INDEX_SUBSTR_DEFAULT) != idxstr[i].mask))
65 				continue;
66 			if ( bv->bv_len ) bv->bv_len++;
67 			bv->bv_len += idxstr[i].word.bv_len;
68 		}
69 	}
70 }
71 
72 /* caller must provide buffer space, after calling index2bvlen */
slap_index2bv(slap_mask_t idx,struct berval * bv)73 void slap_index2bv( slap_mask_t idx, struct berval *bv )
74 {
75 	int i;
76 	char *ptr;
77 
78 	if ( !bv->bv_len ) return;
79 
80 	ptr = bv->bv_val;
81 	for ( i=0; !BER_BVISNULL( &idxstr[i].word ); i++ ) {
82 		if ( !idxstr[i].mask ) continue;
83 		if ( IS_SLAP_INDEX( idx, idxstr[i].mask )) {
84 			if ( (idxstr[i].mask & SLAP_INDEX_SUBSTR) &&
85 				((idx & SLAP_INDEX_SUBSTR_DEFAULT) != idxstr[i].mask))
86 				continue;
87 			if ( ptr != bv->bv_val ) *ptr++ = ',';
88 			ptr = lutil_strcopy( ptr, idxstr[i].word.bv_val );
89 		}
90 	}
91 }
92