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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "system/iconv.h"
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 static void load_case_tables(void)
43 mem_ctx = talloc_init("load_case_tables");
45 smb_panic("No memory for case_tables");
47 upcase_table = map_file(data_path(mem_ctx, "upcase.dat"), 0x20000);
48 lowcase_table = map_file(data_path(mem_ctx, "lowcase.dat"), 0x20000);
50 if (upcase_table == NULL) {
51 /* try also under codepages for testing purposes */
52 upcase_table = map_file("codepages/upcase.dat", 0x20000);
53 if (upcase_table == NULL) {
54 upcase_table = (void *)-1;
57 if (lowcase_table == NULL) {
58 /* try also under codepages for testing purposes */
59 lowcase_table = map_file("codepages/lowcase.dat", 0x20000);
60 if (lowcase_table == NULL) {
61 lowcase_table = (void *)-1;
67 Convert a codepoint_t to upper case.
69 codepoint_t toupper_w(codepoint_t val)
74 if (upcase_table == NULL) {
77 if (upcase_table == (void *)-1) {
80 if (val & 0xFFFF0000) {
83 return SVAL(upcase_table, val*2);
87 Convert a codepoint_t to lower case.
89 codepoint_t tolower_w(codepoint_t val)
94 if (lowcase_table == NULL) {
97 if (lowcase_table == (void *)-1) {
100 if (val & 0xFFFF0000) {
103 return SVAL(lowcase_table, val*2);
107 return the number of bytes occupied by a buffer in CH_UTF16 format
108 the result includes the null termination
110 size_t utf16_len(const void *buf)
114 for (len = 0; SVAL(buf,len); len += 2) ;
120 return the number of bytes occupied by a buffer in CH_UTF16 format
121 the result includes the null termination
124 size_t utf16_len_n(const void *src, size_t n)
128 for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ;
138 size_t ucs2_align(const void *base_ptr, const void *p, int flags)
140 if (flags & (STR_NOALIGN|STR_ASCII))
142 return PTR_DIFF(p, base_ptr) & 1;
146 compare two codepoints case insensitively
148 int codepoint_cmpi(codepoint_t c1, codepoint_t c2)
151 toupper_w(c1) == toupper_w(c2)) {