xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/index.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: index.c,v 1.1.1.4 2014/05/28 09:58:46 tron 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-2014 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 "portable.h"
20 
21 #include <stdio.h>
22 #include <ac/string.h>
23 #include <lutil.h>
24 
25 #include "slap.h"
26 
27 static slap_verbmasks idxstr[] = {
28 	{ BER_BVC("pres"), SLAP_INDEX_PRESENT },
29 	{ BER_BVC("eq"), SLAP_INDEX_EQUALITY },
30 	{ BER_BVC("approx"), SLAP_INDEX_APPROX },
31 	{ BER_BVC("subinitial"), SLAP_INDEX_SUBSTR_INITIAL },
32 	{ BER_BVC("subany"), SLAP_INDEX_SUBSTR_ANY },
33 	{ BER_BVC("subfinal"), SLAP_INDEX_SUBSTR_FINAL },
34 	{ BER_BVC("sub"), SLAP_INDEX_SUBSTR_DEFAULT },
35 	{ BER_BVC("substr"), 0 },
36 	{ BER_BVC("notags"), SLAP_INDEX_NOTAGS },
37 	{ BER_BVC("nolang"), 0 },	/* backwards compat */
38 	{ BER_BVC("nosubtypes"), SLAP_INDEX_NOSUBTYPES },
39 	{ BER_BVNULL, 0 }
40 };
41 
42 
43 int slap_str2index( const char *str, slap_mask_t *idx )
44 {
45 	int i;
46 
47 	i = verb_to_mask( str, idxstr );
48 	if ( BER_BVISNULL(&idxstr[i].word) ) return LDAP_OTHER;
49 	while ( !idxstr[i].mask ) i--;
50 	*idx = idxstr[i].mask;
51 
52 
53 	return LDAP_SUCCESS;
54 }
55 
56 void slap_index2bvlen( slap_mask_t idx, struct berval *bv )
57 {
58 	int i;
59 
60 	bv->bv_len = 0;
61 
62 	for ( i=0; !BER_BVISNULL( &idxstr[i].word ); i++ ) {
63 		if ( !idxstr[i].mask ) continue;
64 		if ( IS_SLAP_INDEX( idx, idxstr[i].mask )) {
65 			if ( (idxstr[i].mask & SLAP_INDEX_SUBSTR) &&
66 				((idx & SLAP_INDEX_SUBSTR_DEFAULT) != idxstr[i].mask))
67 				continue;
68 			if ( bv->bv_len ) bv->bv_len++;
69 			bv->bv_len += idxstr[i].word.bv_len;
70 		}
71 	}
72 }
73 
74 /* caller must provide buffer space, after calling index2bvlen */
75 void slap_index2bv( slap_mask_t idx, struct berval *bv )
76 {
77 	int i;
78 	char *ptr;
79 
80 	if ( !bv->bv_len ) return;
81 
82 	ptr = bv->bv_val;
83 	for ( i=0; !BER_BVISNULL( &idxstr[i].word ); i++ ) {
84 		if ( !idxstr[i].mask ) continue;
85 		if ( IS_SLAP_INDEX( idx, idxstr[i].mask )) {
86 			if ( (idxstr[i].mask & SLAP_INDEX_SUBSTR) &&
87 				((idx & SLAP_INDEX_SUBSTR_DEFAULT) != idxstr[i].mask))
88 				continue;
89 			if ( ptr != bv->bv_val ) *ptr++ = ',';
90 			ptr = lutil_strcopy( ptr, idxstr[i].word.bv_val );
91 		}
92 	}
93 }
94