lib/util/charset use a path to dynconfig.h that works in s3 and s4
[Samba.git] / lib / util / charset / codepoints.c
blob5e0293569450ba13bae2896b3e5cf261a3b89e8f
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-2001
5 Copyright (C) Simo Sorce 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/locale.h"
23 #include "dynconfig.h"
25 /**
26 * @file
27 * @brief Unicode string manipulation
30 /* these 2 tables define the unicode case handling. They are loaded
31 at startup either via mmap() or read() from the lib directory */
32 static void *upcase_table;
33 static void *lowcase_table;
36 /*******************************************************************
37 load the case handling tables
38 ********************************************************************/
39 void load_case_tables(void)
41 TALLOC_CTX *mem_ctx;
43 mem_ctx = talloc_init("load_case_tables");
44 if (!mem_ctx) {
45 smb_panic("No memory for case_tables");
47 upcase_table = map_file(talloc_asprintf(mem_ctx, "%s/upcase.dat", dyn_CODEPAGEDIR), 0x20000);
48 lowcase_table = map_file(talloc_asprintf(mem_ctx, "%s/lowcase.dat", dyn_CODEPAGEDIR), 0x20000);
49 talloc_free(mem_ctx);
50 if (upcase_table == NULL) {
51 upcase_table = (void *)-1;
53 if (lowcase_table == NULL) {
54 lowcase_table = (void *)-1;
58 /**
59 Convert a codepoint_t to upper case.
60 **/
61 _PUBLIC_ codepoint_t toupper_m(codepoint_t val)
63 if (val < 128) {
64 return toupper(val);
66 if (upcase_table == NULL) {
67 load_case_tables();
69 if (upcase_table == (void *)-1) {
70 return val;
72 if (val & 0xFFFF0000) {
73 return val;
75 return SVAL(upcase_table, val*2);
78 /**
79 Convert a codepoint_t to lower case.
80 **/
81 _PUBLIC_ codepoint_t tolower_m(codepoint_t val)
83 if (val < 128) {
84 return tolower(val);
86 if (lowcase_table == NULL) {
87 load_case_tables();
89 if (lowcase_table == (void *)-1) {
90 return val;
92 if (val & 0xFFFF0000) {
93 return val;
95 return SVAL(lowcase_table, val*2);
98 /**
99 If we upper cased this character, would we get the same character?
101 _PUBLIC_ bool islower_m(codepoint_t val)
103 return (toupper_m(val) != val);
107 If we lower cased this character, would we get the same character?
109 _PUBLIC_ bool isupper_m(codepoint_t val)
111 return (tolower_m(val) != val);
115 compare two codepoints case insensitively
117 _PUBLIC_ int codepoint_cmpi(codepoint_t c1, codepoint_t c2)
119 if (c1 == c2 ||
120 toupper_m(c1) == toupper_m(c2)) {
121 return 0;
123 return c1 - c2;