unicode.c 10 KB

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