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
22 #include "wine/port.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
)
70 for (len
/= sizeof(WCHAR
); len
; str1
++, str2
++, len
--)
71 if ((ret
= to_lower(*str1
) - to_lower(*str2
))) break;
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
);
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
];
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
)
99 WCHAR
*end
= buffer
+ *len
/ sizeof(WCHAR
);
103 while (*p
&& *p
!= endchar
&& dest
< end
)
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 */
121 if (!isxdigit(*p
)) *dest
= 'x';
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
++);
138 case '7': /* octal escape */
140 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
141 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
145 /* unrecognized escape: fall through to normal char handling */
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
];
159 if ((ch
= *p
^ 0x80) >= 0x40) break;
160 res
= (res
<< 6) | ch
;
163 if ((ch
= *p
^ 0x80) >= 0x40) break;
164 res
= (res
<< 6) | ch
;
167 if ((ch
= *p
^ 0x80) >= 0x40) break;
168 res
= (res
<< 6) | ch
;
170 if (res
< utf8_minval
[charlen
]) break;
171 if (res
> 0x10ffff) break;
172 if (res
<= 0xffff) *dest
++ = res
;
173 else /* we need surrogates */
176 *dest
++ = 0xd800 | (res
>> 10);
177 if (dest
< end
) *dest
++ = 0xdc00 | (res
& 0x3ff);
181 /* ignore invalid char */
184 if (dest
>= end
) return -1; /* overflow */
186 if (!*p
) return -1; /* delimiter not found */
187 *len
= (dest
- buffer
) * sizeof(WCHAR
);
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....";
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
;
207 if (*str
> 127) /* hex escape */
209 if (len
> 1 && str
[1] < 128 && isxdigit((char)str
[1]))
210 pos
+= sprintf( pos
, "\\x%04x", *str
);
212 pos
+= sprintf( pos
, "\\x%x", *str
);
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
);
223 pos
+= sprintf( pos
, "\\%o", *str
);
226 if (*str
== '\\' || *str
== escape
[0] || *str
== escape
[1]) *pos
++ = '\\';
229 fwrite( buffer
, pos
- buffer
, 1, f
);
230 count
+= pos
- buffer
;
234 static char *get_nls_dir(void)
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
);
244 dir
= realpath( server_argv0
, NULL
);
246 if (!dir
) return NULL
;
247 if (!(p
= strrchr( dir
, '/' )))
253 if (p
> dir
+ 8 && !strcmp( p
- 8, "/server/" )) nlsdir
= "../nls"; /* inside build tree */
254 if ((ret
= malloc( strlen(dir
) + strlen( nlsdir
) + 1 )))
257 strcat( ret
, nlsdir
);
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 unsigned int i
, offset
, size
;
270 struct fd
*fd
= NULL
;
274 nls_dirs
[0] = get_nls_dir();
275 for (i
= 0; i
< ARRAY_SIZE( nls_dirs
); i
++)
277 if (!nls_dirs
[i
]) continue;
278 if (!(path
= malloc( strlen(nls_dirs
[i
]) + sizeof("/l_intl.nls" )))) continue;
279 strcpy( path
, nls_dirs
[i
] );
280 strcat( path
, "/l_intl.nls" );
281 if ((fd
= open_fd( NULL
, path
, O_RDONLY
, &mode
, FILE_READ_DATA
,
282 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
283 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
))) break;
286 if (!fd
) fatal_error( "failed to load l_intl.nls\n" );
287 unix_fd
= get_unix_fd( fd
);
288 /* read initial offset */
289 if (pread( unix_fd
, &data
, sizeof(data
), 0 ) != sizeof(data
) || !data
) goto failed
;
291 /* read size of uppercase table */
292 if (pread( unix_fd
, &data
, sizeof(data
), offset
* 2 ) != sizeof(data
) || !data
) goto failed
;
294 /* read size of lowercase table */
295 if (pread( unix_fd
, &data
, sizeof(data
), offset
* 2 ) != sizeof(data
) || !data
) goto failed
;
298 /* read lowercase table */
299 if (!(casemap
= malloc( size
* 2 ))) goto failed
;
300 if (pread( unix_fd
, casemap
, size
* 2, offset
* 2 ) != size
* 2) goto failed
;
305 fatal_error( "invalid format for casemap table %s\n", path
);