ntdll/tests: Add tests for address requirements in NtMapViewOfSectionEx().
[wine.git] / server / unicode.c
blobedb296be0c8af649f7be5cd7852790a2c130cb58
1 /*
2 * Unicode routines for use inside the server
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <ctype.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <unistd.h>
28 #include <limits.h>
29 #ifdef HAVE_SYS_SYSCTL_H
30 # include <sys/sysctl.h>
31 #endif
33 #include "windef.h"
34 #include "winternl.h"
35 #include "request.h"
36 #include "unicode.h"
37 #include "file.h"
39 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
40 static const char utf8_length[128] =
42 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
43 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
44 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
45 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
46 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
47 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
48 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
49 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
52 /* first byte mask depending on UTF-8 sequence length */
53 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
55 /* minimum Unicode value depending on UTF-8 sequence length */
56 static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
58 static unsigned short *casemap;
60 static inline char to_hex( char ch )
62 if (isdigit(ch)) return ch - '0';
63 return tolower(ch) - 'a' + 10;
66 static inline WCHAR to_lower( WCHAR ch )
68 return ch + casemap[casemap[casemap[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0x0f)];
71 int memicmp_strW( const WCHAR *str1, const WCHAR *str2, data_size_t len )
73 int ret = 0;
75 for (len /= sizeof(WCHAR); len; str1++, str2++, len--)
76 if ((ret = to_lower(*str1) - to_lower(*str2))) break;
77 return ret;
80 unsigned int hash_strW( const WCHAR *str, data_size_t len, unsigned int hash_size )
82 unsigned int i, hash = 0;
84 for (i = 0; i < len / sizeof(WCHAR); i++) hash = hash * 65599 + to_lower( str[i] );
85 return hash % hash_size;
88 WCHAR *ascii_to_unicode_str( const char *str, struct unicode_str *ret )
90 data_size_t i, len = strlen(str);
91 WCHAR *p;
93 ret->len = len * sizeof(WCHAR);
94 ret->str = p = mem_alloc( ret->len );
95 if (p) for (i = 0; i < len; i++) p[i] = (unsigned char)str[i];
96 return p;
99 /* parse an escaped string back into Unicode */
100 /* return the number of chars read from the input, or -1 on output overflow */
101 int parse_strW( WCHAR *buffer, data_size_t *len, const char *src, char endchar )
103 WCHAR *dest = buffer;
104 WCHAR *end = buffer + *len / sizeof(WCHAR);
105 const char *p = src;
106 unsigned char ch;
108 while (*p && *p != endchar && dest < end)
110 if (*p == '\\')
112 p++;
113 if (!*p) break;
114 switch(*p)
116 case 'a': *dest++ = '\a'; p++; continue;
117 case 'b': *dest++ = '\b'; p++; continue;
118 case 'e': *dest++ = '\e'; p++; continue;
119 case 'f': *dest++ = '\f'; p++; continue;
120 case 'n': *dest++ = '\n'; p++; continue;
121 case 'r': *dest++ = '\r'; p++; continue;
122 case 't': *dest++ = '\t'; p++; continue;
123 case 'v': *dest++ = '\v'; p++; continue;
124 case 'x': /* hex escape */
125 p++;
126 if (!isxdigit(*p)) *dest = 'x';
127 else
129 *dest = to_hex(*p++);
130 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
131 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
132 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
134 dest++;
135 continue;
136 case '0':
137 case '1':
138 case '2':
139 case '3':
140 case '4':
141 case '5':
142 case '6':
143 case '7': /* octal escape */
144 *dest = *p++ - '0';
145 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
146 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
147 dest++;
148 continue;
150 /* unrecognized escape: fall through to normal char handling */
154 ch = *p++;
155 if (ch < 0x80) *dest++ = ch;
156 else /* parse utf8 char */
158 int charlen = utf8_length[ch-0x80];
159 unsigned int res = ch & utf8_mask[charlen];
161 switch(charlen)
163 case 3:
164 if ((ch = *p ^ 0x80) >= 0x40) break;
165 res = (res << 6) | ch;
166 p++;
167 case 2:
168 if ((ch = *p ^ 0x80) >= 0x40) break;
169 res = (res << 6) | ch;
170 p++;
171 case 1:
172 if ((ch = *p ^ 0x80) >= 0x40) break;
173 res = (res << 6) | ch;
174 p++;
175 if (res < utf8_minval[charlen]) break;
176 if (res > 0x10ffff) break;
177 if (res <= 0xffff) *dest++ = res;
178 else /* we need surrogates */
180 res -= 0x10000;
181 *dest++ = 0xd800 | (res >> 10);
182 if (dest < end) *dest++ = 0xdc00 | (res & 0x3ff);
184 continue;
186 /* ignore invalid char */
189 if (dest >= end) return -1; /* overflow */
190 *dest++ = 0;
191 if (!*p) return -1; /* delimiter not found */
192 *len = (dest - buffer) * sizeof(WCHAR);
193 return p + 1 - src;
196 /* dump a Unicode string with proper escaping */
197 int dump_strW( const WCHAR *str, data_size_t len, FILE *f, const char escape[2] )
199 static const char escapes[32] = ".......abtnvfr.............e....";
200 char buffer[256];
201 char *pos = buffer;
202 int count = 0;
204 for (len /= sizeof(WCHAR); len; str++, len--)
206 if (pos > buffer + sizeof(buffer) - 8)
208 fwrite( buffer, pos - buffer, 1, f );
209 count += pos - buffer;
210 pos = buffer;
212 if (*str > 127) /* hex escape */
214 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
215 pos += sprintf( pos, "\\x%04x", *str );
216 else
217 pos += sprintf( pos, "\\x%x", *str );
218 continue;
220 if (*str < 32) /* octal or C escape */
222 if (!*str && len == 1) continue; /* do not output terminating NULL */
223 if (escapes[*str] != '.')
224 pos += sprintf( pos, "\\%c", escapes[*str] );
225 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
226 pos += sprintf( pos, "\\%03o", *str );
227 else
228 pos += sprintf( pos, "\\%o", *str );
229 continue;
231 if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
232 *pos++ = *str;
234 fwrite( buffer, pos - buffer, 1, f );
235 count += pos - buffer;
236 return count;
239 static char *get_nls_dir(void)
241 char *p, *dir, *ret;
242 const char *nlsdir = BIN_TO_NLSDIR;
244 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
245 dir = realpath( "/proc/self/exe", NULL );
246 #elif defined (__FreeBSD__) || defined(__DragonFly__)
247 static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
248 size_t dir_size = PATH_MAX;
249 dir = malloc( dir_size );
250 if (dir)
252 if (sysctl( pathname, ARRAY_SIZE( pathname ), dir, &dir_size, NULL, 0 ))
254 free( dir );
255 dir = NULL;
258 #else
259 dir = realpath( server_argv0, NULL );
260 #endif
261 if (!dir) return NULL;
262 if (!(p = strrchr( dir, '/' )))
264 free( dir );
265 return NULL;
267 *(++p) = 0;
268 if (p > dir + 8 && !strcmp( p - 8, "/server/" )) nlsdir = "../nls"; /* inside build tree */
269 if ((ret = malloc( strlen(dir) + strlen( nlsdir ) + 1 )))
271 strcpy( ret, dir );
272 strcat( ret, nlsdir );
274 free( dir );
275 return ret;
278 /* load the case mapping table */
279 struct fd *load_intl_file(void)
281 static const char *nls_dirs[] = { NULL, NLSDIR, "/usr/local/share/wine/nls", "/usr/share/wine/nls" };
282 static const WCHAR nt_pathW[] = {'C',':','\\','w','i','n','d','o','w','s','\\',
283 's','y','s','t','e','m','3','2','\\','l','_','i','n','t','l','.','n','l','s',0};
284 static const struct unicode_str nt_name = { nt_pathW, sizeof(nt_pathW) };
285 unsigned int i, offset, size;
286 unsigned short data;
287 char *path;
288 struct fd *fd = NULL;
289 int unix_fd;
290 mode_t mode = 0600;
292 nls_dirs[0] = get_nls_dir();
293 for (i = 0; i < ARRAY_SIZE( nls_dirs ); i++)
295 if (!nls_dirs[i]) continue;
296 if (!(path = malloc( strlen(nls_dirs[i]) + sizeof("/l_intl.nls" )))) continue;
297 strcpy( path, nls_dirs[i] );
298 strcat( path, "/l_intl.nls" );
299 if ((fd = open_fd( NULL, path, nt_name, O_RDONLY, &mode, FILE_READ_DATA,
300 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
301 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))) break;
302 free( path );
304 if (!fd) fatal_error( "failed to load l_intl.nls\n" );
305 unix_fd = get_unix_fd( fd );
306 /* read initial offset */
307 if (pread( unix_fd, &data, sizeof(data), 0 ) != sizeof(data) || !data) goto failed;
308 offset = data;
309 /* read size of uppercase table */
310 if (pread( unix_fd, &data, sizeof(data), offset * 2 ) != sizeof(data) || !data) goto failed;
311 offset += data;
312 /* read size of lowercase table */
313 if (pread( unix_fd, &data, sizeof(data), offset * 2 ) != sizeof(data) || !data) goto failed;
314 offset++;
315 size = data - 1;
316 /* read lowercase table */
317 if (!(casemap = malloc( size * 2 ))) goto failed;
318 if (pread( unix_fd, casemap, size * 2, offset * 2 ) != size * 2) goto failed;
319 free( path );
320 return fd;
322 failed:
323 fatal_error( "invalid format for casemap table %s\n", path );