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 /* try also under codepages for testing purposes */
47 upcase_table = map_file("codepages/upcase.dat", 0x20000);
48 if (upcase_table == NULL) {
49 upcase_table = (void *)-1;
52 if (lowcase_table == NULL) {
53 /* try also under codepages for testing purposes */
54 lowcase_table = map_file("codepages/lowcase.dat", 0x20000);
55 if (lowcase_table == NULL) {
56 lowcase_table = (void *)-1;
61 /*******************************************************************
62 Convert a codepoint_t to upper case.
63 ********************************************************************/
64 codepoint_t toupper_w(codepoint_t val)
69 if (upcase_table == NULL) {
72 if (upcase_table == (void *)-1) {
75 if (val & 0xFFFF0000) {
78 return SVAL(upcase_table, val*2);
81 /*******************************************************************
82 Convert a codepoint_t to lower case.
83 ********************************************************************/
84 codepoint_t tolower_w(codepoint_t val)
89 if (lowcase_table == NULL) {
92 if (lowcase_table == (void *)-1) {
95 if (val & 0xFFFF0000) {
98 return SVAL(lowcase_table, val*2);
101 /*******************************************************************
102 return the number of bytes occupied by a buffer in CH_UTF16 format
103 the result includes the null termination
104 ********************************************************************/
105 size_t utf16_len(const void *buf)
109 for (len = 0; SVAL(buf,len); len += 2) ;
114 /*******************************************************************
115 return the number of bytes occupied by a buffer in CH_UTF16 format
116 the result includes the null termination
118 ********************************************************************/
119 size_t utf16_len_n(const void *src, size_t n)
123 for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ;
133 size_t ucs2_align(const void *base_ptr, const void *p, int flags)
135 if (flags & (STR_NOALIGN|STR_ASCII))
137 return PTR_DIFF(p, base_ptr) & 1;
141 compare two codepoints case insensitively
143 int codepoint_cmpi(codepoint_t c1, codepoint_t c2)
146 toupper_w(c1) == toupper_w(c2)) {