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