unicode.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "unicode.h"
  25. /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
  26. static const char utf8_length[128] =
  27. {
  28. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
  29. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
  30. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
  31. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
  32. 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
  33. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
  34. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
  35. 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
  36. };
  37. /* first byte mask depending on UTF-8 sequence length */
  38. static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
  39. /* minimum Unicode value depending on UTF-8 sequence length */
  40. static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
  41. static inline char to_hex( char ch )
  42. {
  43. if (isdigit(ch)) return ch - '0';
  44. return tolower(ch) - 'a' + 10;
  45. }
  46. static inline WCHAR to_lower( WCHAR ch )
  47. {
  48. extern const WCHAR wine_casemap_lower[];
  49. return ch + wine_casemap_lower[wine_casemap_lower[ch >> 8] + (ch & 0xff)];
  50. }
  51. int memicmp_strW( const WCHAR *str1, const WCHAR *str2, data_size_t len )
  52. {
  53. int ret = 0;
  54. for (len /= sizeof(WCHAR); len; str1++, str2++, len--)
  55. if ((ret = to_lower(*str1) - to_lower(*str2))) break;
  56. return ret;
  57. }
  58. unsigned int hash_strW( const WCHAR *str, data_size_t len, unsigned int hash_size )
  59. {
  60. unsigned int i, hash = 0;
  61. for (i = 0; i < len / sizeof(WCHAR); i++) hash = hash * 65599 + to_lower( str[i] );
  62. return hash % hash_size;
  63. }
  64. WCHAR *ascii_to_unicode_str( const char *str, struct unicode_str *ret )
  65. {
  66. data_size_t i, len = strlen(str);
  67. WCHAR *p;
  68. ret->len = len * sizeof(WCHAR);
  69. ret->str = p = mem_alloc( ret->len );
  70. if (p) for (i = 0; i < len; i++) p[i] = (unsigned char)str[i];
  71. return p;
  72. }
  73. /* parse an escaped string back into Unicode */
  74. /* return the number of chars read from the input, or -1 on output overflow */
  75. int parse_strW( WCHAR *buffer, data_size_t *len, const char *src, char endchar )
  76. {
  77. WCHAR *dest = buffer;
  78. WCHAR *end = buffer + *len / sizeof(WCHAR);
  79. const char *p = src;
  80. unsigned char ch;
  81. while (*p && *p != endchar && dest < end)
  82. {
  83. if (*p == '\\')
  84. {
  85. p++;
  86. if (!*p) break;
  87. switch(*p)
  88. {
  89. case 'a': *dest++ = '\a'; p++; continue;
  90. case 'b': *dest++ = '\b'; p++; continue;
  91. case 'e': *dest++ = '\e'; p++; continue;
  92. case 'f': *dest++ = '\f'; p++; continue;
  93. case 'n': *dest++ = '\n'; p++; continue;
  94. case 'r': *dest++ = '\r'; p++; continue;
  95. case 't': *dest++ = '\t'; p++; continue;
  96. case 'v': *dest++ = '\v'; p++; continue;
  97. case 'x': /* hex escape */
  98. p++;
  99. if (!isxdigit(*p)) *dest = 'x';
  100. else
  101. {
  102. *dest = to_hex(*p++);
  103. if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
  104. if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
  105. if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
  106. }
  107. dest++;
  108. continue;
  109. case '0':
  110. case '1':
  111. case '2':
  112. case '3':
  113. case '4':
  114. case '5':
  115. case '6':
  116. case '7': /* octal escape */
  117. *dest = *p++ - '0';
  118. if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
  119. if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
  120. dest++;
  121. continue;
  122. }
  123. /* unrecognized escape: fall through to normal char handling */
  124. }
  125. ch = *p++;
  126. if (ch < 0x80) *dest++ = ch;
  127. else /* parse utf8 char */
  128. {
  129. int charlen = utf8_length[ch-0x80];
  130. unsigned int res = ch & utf8_mask[charlen];
  131. switch(charlen)
  132. {
  133. case 3:
  134. if ((ch = *p ^ 0x80) >= 0x40) break;
  135. res = (res << 6) | ch;
  136. p++;
  137. case 2:
  138. if ((ch = *p ^ 0x80) >= 0x40) break;
  139. res = (res << 6) | ch;
  140. p++;
  141. case 1:
  142. if ((ch = *p ^ 0x80) >= 0x40) break;
  143. res = (res << 6) | ch;
  144. p++;
  145. if (res < utf8_minval[charlen]) break;
  146. if (res > 0x10ffff) break;
  147. if (res <= 0xffff) *dest++ = res;
  148. else /* we need surrogates */
  149. {
  150. res -= 0x10000;
  151. *dest++ = 0xd800 | (res >> 10);
  152. if (dest < end) *dest++ = 0xdc00 | (res & 0x3ff);
  153. }
  154. continue;
  155. }
  156. /* ignore invalid char */
  157. }
  158. }
  159. if (dest >= end) return -1; /* overflow */
  160. *dest++ = 0;
  161. if (!*p) return -1; /* delimiter not found */
  162. *len = (dest - buffer) * sizeof(WCHAR);
  163. return p + 1 - src;
  164. }
  165. /* dump a Unicode string with proper escaping */
  166. int dump_strW( const WCHAR *str, data_size_t len, FILE *f, const char escape[2] )
  167. {
  168. static const char escapes[32] = ".......abtnvfr.............e....";
  169. char buffer[256];
  170. char *pos = buffer;
  171. int count = 0;
  172. for (len /= sizeof(WCHAR); len; str++, len--)
  173. {
  174. if (pos > buffer + sizeof(buffer) - 8)
  175. {
  176. fwrite( buffer, pos - buffer, 1, f );
  177. count += pos - buffer;
  178. pos = buffer;
  179. }
  180. if (*str > 127) /* hex escape */
  181. {
  182. if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
  183. pos += sprintf( pos, "\\x%04x", *str );
  184. else
  185. pos += sprintf( pos, "\\x%x", *str );
  186. continue;
  187. }
  188. if (*str < 32) /* octal or C escape */
  189. {
  190. if (!*str && len == 1) continue; /* do not output terminating NULL */
  191. if (escapes[*str] != '.')
  192. pos += sprintf( pos, "\\%c", escapes[*str] );
  193. else if (len > 1 && str[1] >= '0' && str[1] <= '7')
  194. pos += sprintf( pos, "\\%03o", *str );
  195. else
  196. pos += sprintf( pos, "\\%o", *str );
  197. continue;
  198. }
  199. if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
  200. *pos++ = *str;
  201. }
  202. fwrite( buffer, pos - buffer, 1, f );
  203. count += pos - buffer;
  204. return count;
  205. }