]> git.sur5r.net Git - openldap/blob - servers/slapd/index.c
ITS#3576 use actual backend (not glue instance) for restriction and
[openldap] / servers / slapd / index.c
1 /* index.c - index utilities */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 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
22 #include "slap.h"
23
24 static slap_verbmasks idxstr[] = {
25         { BER_BVC("pres"), SLAP_INDEX_PRESENT },
26         { BER_BVC("eq"), SLAP_INDEX_EQUALITY },
27         { BER_BVC("approx"), SLAP_INDEX_APPROX },
28         { BER_BVC("subinitial"), SLAP_INDEX_SUBSTR_INITIAL },
29         { BER_BVC("subany"), SLAP_INDEX_SUBSTR_ANY },
30         { BER_BVC("subfinal"), SLAP_INDEX_SUBSTR_FINAL },
31         { BER_BVC("sub"), SLAP_INDEX_SUBSTR_DEFAULT },
32         { BER_BVC("substr"), 0 },
33         { BER_BVC("notags"), SLAP_INDEX_NOTAGS },
34         { BER_BVC("nolang"), 0 },       /* backwards compat */
35         { BER_BVC("nosubtypes"), SLAP_INDEX_NOSUBTYPES },
36         { BER_BVNULL, 0 }
37 };
38
39
40 int slap_str2index( const char *str, slap_mask_t *idx )
41 {
42         int i;
43
44         i = verb_to_mask( str, idxstr );
45         if ( BER_BVISNULL(&idxstr[i].word) ) return LDAP_OTHER;
46         while ( !idxstr[i].mask ) i--;
47         *idx = idxstr[i].mask;
48
49
50         return LDAP_SUCCESS;
51 }
52
53 void slap_index2bvlen( slap_mask_t idx, struct berval *bv )
54 {
55         int i;
56
57         bv->bv_len = 0;
58
59         for ( i=0; !BER_BVISNULL( &idxstr[i].word ); i++ ) {
60                 if ( !idxstr[i].mask ) continue;
61                 if ( IS_SLAP_INDEX( idx, idxstr[i].mask )) {
62                         if ( (idxstr[i].mask & SLAP_INDEX_SUBSTR) &&
63                                 ((idx & SLAP_INDEX_SUBSTR_DEFAULT) != idxstr[i].mask))
64                                 continue;
65                         if ( bv->bv_len ) bv->bv_len++;
66                         bv->bv_len += idxstr[i].word.bv_len;
67                 }
68         }
69 }
70
71 /* caller must provide buffer space, after calling index2bvlen */
72 void slap_index2bv( slap_mask_t idx, struct berval *bv )
73 {
74         int i;
75         char *ptr;
76
77         if ( !bv->bv_len ) return;
78
79         ptr = bv->bv_val;
80         for ( i=0; !BER_BVISNULL( &idxstr[i].word ); i++ ) {
81                 if ( !idxstr[i].mask ) continue;
82                 if ( IS_SLAP_INDEX( idx, idxstr[i].mask )) {
83                         if ( (idxstr[i].mask & SLAP_INDEX_SUBSTR) &&
84                                 ((idx & SLAP_INDEX_SUBSTR_DEFAULT) != idxstr[i].mask))
85                                 continue;
86                         if ( ptr != bv->bv_val ) *ptr++ = ',';
87                         ptr = lutil_strcopy( ptr, idxstr[i].word.bv_val );
88                 }
89         }
90 }