d2d1: Use D3D11 interfaces to create buffers.
[wine.git] / server / unicode.c
blob55f9954f4f43f0f9f4ad985d8fad682422800a38
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"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winternl.h"
30 #include "request.h"
31 #include "unicode.h"
32 #include "file.h"
34 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
35 static const char utf8_length[128] =
37 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
38 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
39 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
40 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
41 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
42 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
43 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
44 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
47 /* first byte mask depending on UTF-8 sequence length */
48 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
50 /* minimum Unicode value depending on UTF-8 sequence length */
51 static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
53 static unsigned short *casemap;
55 static inline char to_hex( char ch )
57 if (isdigit(ch)) return ch - '0';
58 return tolower(ch) - 'a' + 10;
61 static inline WCHAR to_lower( WCHAR ch )
63 return ch + casemap[casemap[casemap[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0x0f)];
66 int memicmp_strW( const WCHAR *str1, const WCHAR *str2, data_size_t len )
68 int ret = 0;
70 for (len /= sizeof(WCHAR); len; str1++, str2++, len--)
71 if ((ret = to_lower(*str1) - to_lower(*str2))) break;
72 return ret;
75 unsigned int hash_strW( const WCHAR *str, data_size_t len, unsigned int hash_size )
77 unsigned int i, hash = 0;
79 for (i = 0; i < len / sizeof(WCHAR); i++) hash = hash * 65599 + to_lower( str[i] );
80 return hash % hash_size;
83 WCHAR *ascii_to_unicode_str( const char *str, struct unicode_str *ret )
85 data_size_t i, len = strlen(str);
86 WCHAR *p;
88 ret->len = len * sizeof(WCHAR);
89 ret->str = p = mem_alloc( ret->len );
90 if (p) for (i = 0; i < len; i++) p[i] = (unsigned char)str[i];
91 return p;
94 /* parse an escaped string back into Unicode */
95 /* return the number of chars read from the input, or -1 on output overflow */
96 int parse_strW( WCHAR *buffer, data_size_t *len, const char *src, char endchar )
98 WCHAR *dest = buffer;
99 WCHAR *end = buffer + *len / sizeof(WCHAR);
100 const char *p = src;
101 unsigned char ch;
103 while (*p && *p != endchar && dest < end)
105 if (*p == '\\')
107 p++;
108 if (!*p) break;
109 switch(*p)
111 case 'a': *dest++ = '\a'; p++; continue;
112 case 'b': *dest++ = '\b'; p++; continue;
113 case 'e': *dest++ = '\e'; p++; continue;
114 case 'f': *dest++ = '\f'; p++; continue;
115 case 'n': *dest++ = '\n'; p++; continue;
116 case 'r': *dest++ = '\r'; p++; continue;
117 case 't': *dest++ = '\t'; p++; continue;
118 case 'v': *dest++ = '\v'; p++; continue;
119 case 'x': /* hex escape */
120 p++;
121 if (!isxdigit(*p)) *dest = 'x';
122 else
124 *dest = to_hex(*p++);
125 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
126 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
127 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
129 dest++;
130 continue;
131 case '0':
132 case '1':
133 case '2':
134 case '3':
135 case '4':
136 case '5':
137 case '6':
138 case '7': /* octal escape */
139 *dest = *p++ - '0';
140 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
141 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
142 dest++;
143 continue;
145 /* unrecognized escape: fall through to normal char handling */
149 ch = *p++;
150 if (ch < 0x80) *dest++ = ch;
151 else /* parse utf8 char */
153 int charlen = utf8_length[ch-0x80];
154 unsigned int res = ch & utf8_mask[charlen];
156 switch(charlen)
158 case 3:
159 if ((ch = *p ^ 0x80) >= 0x40) break;
160 res = (res << 6) | ch;
161 p++;
162 case 2:
163 if ((ch = *p ^ 0x80) >= 0x40) break;
164 res = (res << 6) | ch;
165 p++;
166 case 1:
167 if ((ch = *p ^ 0x80) >= 0x40) break;
168 res = (res << 6) | ch;
169 p++;
170 if (res < utf8_minval[charlen]) break;
171 if (res > 0x10ffff) break;
172 if (res <= 0xffff) *dest++ = res;
173 else /* we need surrogates */
175 res -= 0x10000;
176 *dest++ = 0xd800 | (res >> 10);
177 if (dest < end) *dest++ = 0xdc00 | (res & 0x3ff);
179 continue;
181 /* ignore invalid char */
184 if (dest >= end) return -1; /* overflow */
185 *dest++ = 0;
186 if (!*p) return -1; /* delimiter not found */
187 *len = (dest - buffer) * sizeof(WCHAR);
188 return p + 1 - src;
191 /* dump a Unicode string with proper escaping */
192 int dump_strW( const WCHAR *str, data_size_t len, FILE *f, const char escape[2] )
194 static const char escapes[32] = ".......abtnvfr.............e....";
195 char buffer[256];
196 char *pos = buffer;
197 int count = 0;
199 for (len /= sizeof(WCHAR); len; str++, len--)
201 if (pos > buffer + sizeof(buffer) - 8)
203 fwrite( buffer, pos - buffer, 1, f );
204 count += pos - buffer;
205 pos = buffer;
207 if (*str > 127) /* hex escape */
209 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
210 pos += sprintf( pos, "\\x%04x", *str );
211 else
212 pos += sprintf( pos, "\\x%x", *str );
213 continue;
215 if (*str < 32) /* octal or C escape */
217 if (!*str && len == 1) continue; /* do not output terminating NULL */
218 if (escapes[*str] != '.')
219 pos += sprintf( pos, "\\%c", escapes[*str] );
220 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
221 pos += sprintf( pos, "\\%03o", *str );
222 else
223 pos += sprintf( pos, "\\%o", *str );
224 continue;
226 if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
227 *pos++ = *str;
229 fwrite( buffer, pos - buffer, 1, f );
230 count += pos - buffer;
231 return count;
234 static char *get_nls_dir(void)
236 char *p, *dir, *ret;
237 const char *nlsdir = BIN_TO_NLSDIR;
239 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
240 dir = realpath( "/proc/self/exe", NULL );
241 #elif defined (__FreeBSD__) || defined(__DragonFly__)
242 dir = realpath( "/proc/curproc/file", NULL );
243 #else
244 dir = realpath( server_argv0, NULL );
245 #endif
246 if (!dir) return NULL;
247 if (!(p = strrchr( dir, '/' )))
249 free( dir );
250 return NULL;
252 *(++p) = 0;
253 if (p > dir + 8 && !strcmp( p - 8, "/server/" )) nlsdir = "../nls"; /* inside build tree */
254 if ((ret = malloc( strlen(dir) + strlen( nlsdir ) + 1 )))
256 strcpy( ret, dir );
257 strcat( ret, nlsdir );
259 free( dir );
260 return ret;
263 /* load the case mapping table */
264 struct fd *load_intl_file(void)
266 static const char *nls_dirs[] = { NULL, NLSDIR, "/usr/local/share/wine/nls", "/usr/share/wine/nls" };
267 static const WCHAR nt_pathW[] = {'C',':','\\','w','i','n','d','o','w','s','\\',
268 's','y','s','t','e','m','3','2','\\','l','_','i','n','t','l','.','n','l','s',0};
269 static const struct unicode_str nt_name = { nt_pathW, sizeof(nt_pathW) };
270 unsigned int i, offset, size;
271 unsigned short data;
272 char *path;
273 struct fd *fd = NULL;
274 int unix_fd;
275 mode_t mode = 0600;
277 nls_dirs[0] = get_nls_dir();
278 for (i = 0; i < ARRAY_SIZE( nls_dirs ); i++)
280 if (!nls_dirs[i]) continue;
281 if (!(path = malloc( strlen(nls_dirs[i]) + sizeof("/l_intl.nls" )))) continue;
282 strcpy( path, nls_dirs[i] );
283 strcat( path, "/l_intl.nls" );
284 if ((fd = open_fd( NULL, path, nt_name, O_RDONLY, &mode, FILE_READ_DATA,
285 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
286 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))) break;
287 free( path );
289 if (!fd) fatal_error( "failed to load l_intl.nls\n" );
290 unix_fd = get_unix_fd( fd );
291 /* read initial offset */
292 if (pread( unix_fd, &data, sizeof(data), 0 ) != sizeof(data) || !data) goto failed;
293 offset = data;
294 /* read size of uppercase table */
295 if (pread( unix_fd, &data, sizeof(data), offset * 2 ) != sizeof(data) || !data) goto failed;
296 offset += data;
297 /* read size of lowercase table */
298 if (pread( unix_fd, &data, sizeof(data), offset * 2 ) != sizeof(data) || !data) goto failed;
299 offset++;
300 size = data - 1;
301 /* read lowercase table */
302 if (!(casemap = malloc( size * 2 ))) goto failed;
303 if (pread( unix_fd, casemap, size * 2, offset * 2 ) != size * 2) goto failed;
304 free( path );
305 return fd;
307 failed:
308 fatal_error( "invalid format for casemap table %s\n", path );