r18130: the move to system/ in libreplace broke some things ... should be
[vlendec/samba-autobuild/.git] / source4 / lib / ldb / common / ldb_utf8.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb utf8 handling
29  *
30  *  Description: case folding and case comparison for UTF8 strings
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37 #include "system/locale.h"
38
39
40 /*
41   this allow the user to pass in a caseless comparison
42   function to handle utf8 caseless comparisons
43  */
44 void ldb_set_utf8_fns(struct ldb_context *ldb,
45                         void *context,
46                         char *(*casefold)(void *, void *, const char *))
47 {
48         if (context)
49                 ldb->utf8_fns.context = context;
50         if (casefold)
51                 ldb->utf8_fns.casefold = casefold;
52 }
53
54 /*
55   a simple case folding function
56   NOTE: does not handle UTF8
57 */
58 char *ldb_casefold_default(void *context, void *mem_ctx, const char *s)
59 {
60         int i;
61         char *ret = talloc_strdup(mem_ctx, s);
62         if (!s) {
63                 errno = ENOMEM;
64                 return NULL;
65         }
66         for (i=0;ret[i];i++) {
67                 ret[i] = toupper((unsigned char)ret[i]);
68         }
69         return ret;
70 }
71
72 void ldb_set_utf8_default(struct ldb_context *ldb)
73 {
74         ldb_set_utf8_fns(ldb, NULL, ldb_casefold_default);
75 }
76
77 char *ldb_casefold(struct ldb_context *ldb, void *mem_ctx, const char *s)
78 {
79         return ldb->utf8_fns.casefold(ldb->utf8_fns.context, mem_ctx, s);
80 }
81
82 /*
83   check the attribute name is valid according to rfc2251
84   returns 1 if the name is ok
85  */
86
87 int ldb_valid_attr_name(const char *s)
88 {
89         int i;
90
91         if (!s || !s[0])
92                 return 0;
93
94         /* handle special ldb_tdb wildcard */
95         if (strcmp(s, "*") == 0) return 1;
96
97         for (i = 0; s[i]; i++) {
98                 if (! isascii(s[i])) {
99                         return 0;
100                 }
101                 if (i == 0) { /* first char must be an alpha (or our special '@' identifier) */
102                         if (! (isalpha(s[i]) || (s[i] == '@'))) {
103                                 return 0;
104                         }
105                 } else {
106                         if (! (isalnum(s[i]) || (s[i] == '-'))) {
107                                 return 0;
108                         }
109                 }
110         }
111         return 1;
112 }
113
114 /*
115   compare two attribute names
116   attribute names are restricted by rfc2251 so using
117   strcasecmp and toupper here is ok.
118   return 0 for match
119 */
120 int ldb_attr_cmp(const char *attr1, const char *attr2)
121 {
122         return strcasecmp(attr1, attr2);
123 }
124
125 char *ldb_attr_casefold(void *mem_ctx, const char *s)
126 {
127         int i;
128         char *ret = talloc_strdup(mem_ctx, s);
129         if (!s) {
130                 errno = ENOMEM;
131                 return NULL;
132         }
133         for (i = 0; ret[i]; i++) {
134                 ret[i] = toupper((unsigned char)ret[i]);
135         }
136         return ret;
137 }
138
139 /*
140   we accept either 'dn' or 'distinguishedName' for a distinguishedName
141 */
142 int ldb_attr_dn(const char *attr)
143 {
144         if (ldb_attr_cmp(attr, "dn") == 0 ||
145             ldb_attr_cmp(attr, "distinguishedName") == 0) {
146                 return 0;
147         }
148         return -1;
149 }