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
29 #ifdef HAVE_SYS_SYSCTL_H
30 # include <sys/sysctl.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
)
75 for (len
/= sizeof(WCHAR
); len
; str1
++, str2
++, len
--)
76 if ((ret
= to_lower(*str1
) - to_lower(*str2
))) break;
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
);
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
];
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
);
108 while (*p
&& *p
!= endchar
&& dest
< end
)
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 */
126 if (!isxdigit(*p
)) *dest
= 'x';
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
++);
143 case '7': /* octal escape */
145 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
146 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
150 /* unrecognized escape: fall through to normal char handling */
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
];
164 if ((ch
= *p
^ 0x80) >= 0x40) break;
165 res
= (res
<< 6) | ch
;
168 if ((ch
= *p
^ 0x80) >= 0x40) break;
169 res
= (res
<< 6) | ch
;
172 if ((ch
= *p
^ 0x80) >= 0x40) break;
173 res
= (res
<< 6) | ch
;
175 if (res
< utf8_minval
[charlen
]) break;
176 if (res
> 0x10ffff) break;
177 if (res
<= 0xffff) *dest
++ = res
;
178 else /* we need surrogates */
181 *dest
++ = 0xd800 | (res
>> 10);
182 if (dest
< end
) *dest
++ = 0xdc00 | (res
& 0x3ff);
186 /* ignore invalid char */
189 if (dest
>= end
) return -1; /* overflow */
191 if (!*p
) return -1; /* delimiter not found */
192 *len
= (dest
- buffer
) * sizeof(WCHAR
);
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....";
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
;
212 if (*str
> 127) /* hex escape */
214 if (len
> 1 && str
[1] < 128 && isxdigit((char)str
[1]))
215 pos
+= sprintf( pos
, "\\x%04x", *str
);
217 pos
+= sprintf( pos
, "\\x%x", *str
);
220 if (*str
< 32) /* octal or C escape */
222 if (escapes
[*str
] != '.')
223 pos
+= sprintf( pos
, "\\%c", escapes
[*str
] );
224 else if (len
> 1 && str
[1] >= '0' && str
[1] <= '7')
225 pos
+= sprintf( pos
, "\\%03o", *str
);
227 pos
+= sprintf( pos
, "\\%o", *str
);
230 if (*str
== '\\' || *str
== escape
[0] || *str
== escape
[1]) *pos
++ = '\\';
233 fwrite( buffer
, pos
- buffer
, 1, f
);
234 count
+= pos
- buffer
;
238 static char *get_nls_dir(void)
241 const char *nlsdir
= BIN_TO_NLSDIR
;
243 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
244 dir
= realpath( "/proc/self/exe", NULL
);
245 #elif defined (__FreeBSD__) || defined(__DragonFly__)
246 static int pathname
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
247 size_t dir_size
= PATH_MAX
;
248 dir
= malloc( dir_size
);
251 if (sysctl( pathname
, ARRAY_SIZE( pathname
), dir
, &dir_size
, NULL
, 0 ))
258 dir
= realpath( server_argv0
, NULL
);
260 if (!dir
) return NULL
;
261 if (!(p
= strrchr( dir
, '/' )))
267 if (p
> dir
+ 8 && !strcmp( p
- 8, "/server/" )) nlsdir
= "../nls"; /* inside build tree */
268 if ((ret
= malloc( strlen(dir
) + strlen( nlsdir
) + 1 )))
271 strcat( ret
, nlsdir
);
277 /* load the case mapping table */
278 struct fd
*load_intl_file(void)
280 static const char *nls_dirs
[] = { NULL
, NLSDIR
, "/usr/local/share/wine/nls", "/usr/share/wine/nls" };
281 static const WCHAR nt_pathW
[] = {'C',':','\\','w','i','n','d','o','w','s','\\',
282 's','y','s','t','e','m','3','2','\\','l','_','i','n','t','l','.','n','l','s',0};
283 static const struct unicode_str nt_name
= { nt_pathW
, sizeof(nt_pathW
) };
284 unsigned int i
, offset
, size
;
287 struct fd
*fd
= NULL
;
291 nls_dirs
[0] = get_nls_dir();
292 for (i
= 0; i
< ARRAY_SIZE( nls_dirs
); i
++)
294 if (!nls_dirs
[i
]) continue;
295 if (!(path
= malloc( strlen(nls_dirs
[i
]) + sizeof("/l_intl.nls" )))) continue;
296 strcpy( path
, nls_dirs
[i
] );
297 strcat( path
, "/l_intl.nls" );
298 if ((fd
= open_fd( NULL
, path
, nt_name
, O_RDONLY
, &mode
, FILE_READ_DATA
,
299 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
300 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
))) break;
303 if (!fd
) fatal_error( "failed to load l_intl.nls\n" );
304 unix_fd
= get_unix_fd( fd
);
305 /* read initial offset */
306 if (pread( unix_fd
, &data
, sizeof(data
), 0 ) != sizeof(data
) || !data
) goto failed
;
308 /* read size of uppercase table */
309 if (pread( unix_fd
, &data
, sizeof(data
), offset
* 2 ) != sizeof(data
) || !data
) goto failed
;
311 /* read size of lowercase table */
312 if (pread( unix_fd
, &data
, sizeof(data
), offset
* 2 ) != sizeof(data
) || !data
) goto failed
;
315 /* read lowercase table */
316 if (!(casemap
= malloc( size
* 2 ))) goto failed
;
317 if (pread( unix_fd
, casemap
, size
* 2, offset
* 2 ) != size
* 2) goto failed
;
322 fatal_error( "invalid format for casemap table %s\n", path
);