unicode.c 10 KB

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