r8515: ldb_dn_cmp now uses ldb_dn_compare so that the DNs are compared
[samba.git] / source / 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/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include <ctype.h>
39
40 /*
41   TODO:
42   a simple case folding function - will be replaced by a UTF8 aware function later
43 */
44 char *ldb_casefold(void *mem_ctx, const char *s)
45 {
46         int i;
47         char *ret = talloc_strdup(mem_ctx, s);
48         if (!s) {
49                 errno = ENOMEM;
50                 return NULL;
51         }
52         for (i=0;ret[i];i++) {
53                 ret[i] = toupper((unsigned char)ret[i]);
54         }
55         return ret;
56 }
57
58 /*
59   a caseless compare, optimised for 7 bit
60   TODO: doesn't yet handle UTF8
61 */
62 int ldb_caseless_cmp(const char *s1, const char *s2)
63 {
64         int i;
65         for (i=0;s1[i] != 0;i++) {
66                 int c1 = toupper((unsigned char)s1[i]),
67                     c2 = toupper((unsigned char)s2[i]);
68                 if (c1 != c2) {
69                         return c1 - c2;
70                 }
71         }
72         return s2[i];
73 }
74
75 /*
76   compare two attributes
77   return 0 for match
78 */
79 int ldb_attr_cmp(const char *attr1, const char *attr2)
80 {
81         return ldb_caseless_cmp(attr1, attr2);
82 }
83