r502: modified ldb to allow the use of an external pool memory
[metze/samba/wip.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
37 /*
38   TODO:
39   a simple case folding function - will be replaced by a UTF8 aware function later
40 */
41 char *ldb_casefold(struct ldb_context *ldb, const char *s)
42 {
43         int i;
44         char *ret = ldb_strdup(ldb, s);
45         if (!s) {
46                 errno = ENOMEM;
47                 return NULL;
48         }
49         for (i=0;ret[i];i++) {
50                 ret[i] = toupper(ret[i]);
51         }
52         return ret;
53 }
54
55 /*
56   a caseless compare, optimised for 7 bit
57   TODO: doesn't yet handle UTF8
58 */
59 static int ldb_caseless_cmp(const char *s1, const char *s2)
60 {
61         int i;
62         for (i=0;s1[i] != 0;i++) {
63                 int c1 = toupper(s1[i]), c2 = toupper(s2[i]);
64                 if (c1 != c2) {
65                         return c1 - c2;
66                 }
67         }
68         return s2[i];
69 }
70
71 /*
72   compare two basedn fields
73   return 0 for match
74 */
75 int ldb_dn_cmp(const char *dn1, const char *dn2)
76 {
77         return ldb_caseless_cmp(dn1, dn2);
78 }
79
80 /*
81   compare two attributes
82   return 0 for match
83 */
84 int ldb_attr_cmp(const char *dn1, const char *dn2)
85 {
86         return ldb_caseless_cmp(dn1, dn2);
87 }