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"
25 /* these 2 tables define the unicode case handling. They are loaded
26 at startup either via mmap() or read() from the lib directory */
27 static void *upcase_table;
28 static void *lowcase_table;
31 /*******************************************************************
32 load the case handling tables
33 ********************************************************************/
34 static void load_case_tables(void)
38 mem_ctx = talloc_init("load_case_tables");
40 smb_panic("No memory for case_tables");
42 upcase_table = map_file(lib_path(mem_ctx, "upcase.dat"), 0x20000);
43 lowcase_table = map_file(lib_path(mem_ctx, "lowcase.dat"), 0x20000);
45 if (upcase_table == NULL) {
46 upcase_table = (void *)-1;
48 if (lowcase_table == NULL) {
49 lowcase_table = (void *)-1;
53 /*******************************************************************
54 Convert a codepoint_t to upper case.
55 ********************************************************************/
56 codepoint_t toupper_w(codepoint_t val)
61 if (upcase_table == (void *)-1) {
64 if (upcase_table == NULL) {
67 if (val & 0xFFFF0000) {
70 return SVAL(upcase_table, val*2);
73 /*******************************************************************
74 Convert a codepoint_t to lower case.
75 ********************************************************************/
76 codepoint_t tolower_w(codepoint_t val)
81 if (lowcase_table == (void *)-1) {
84 if (lowcase_table == NULL) {
87 if (val & 0xFFFF0000) {
90 return SVAL(lowcase_table, val*2);
93 /*******************************************************************
94 return the number of bytes occupied by a buffer in CH_UTF16 format
95 the result includes the null termination
96 ********************************************************************/
97 size_t utf16_len(const void *buf)
101 for (len = 0; SVAL(buf,len); len += 2) ;
106 /*******************************************************************
107 return the number of bytes occupied by a buffer in CH_UTF16 format
108 the result includes the null termination
110 ********************************************************************/
111 size_t utf16_len_n(const void *src, size_t n)
115 for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ;
125 size_t ucs2_align(const void *base_ptr, const void *p, int flags)
127 if (flags & (STR_NOALIGN|STR_ASCII))
129 return PTR_DIFF(p, base_ptr) & 1;
133 compare two codepoints case insensitively
135 int codepoint_cmpi(codepoint_t c1, codepoint_t c2)
138 toupper_w(c1) == toupper_w(c2)) {