user32: Move *RegisterDeviceNotification*() to input.c.
[wine.git] / server / unicode.c
blob8d6c411cb66d85021633b2d4262d31dff0c2fded
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <stdio.h>
27 #include "unicode.h"
29 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
30 static const char utf8_length[128] =
32 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
33 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
34 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
35 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
36 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
37 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
38 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
39 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
42 /* first byte mask depending on UTF-8 sequence length */
43 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
45 /* minimum Unicode value depending on UTF-8 sequence length */
46 static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
48 static inline char to_hex( char ch )
50 if (isdigit(ch)) return ch - '0';
51 return tolower(ch) - 'a' + 10;
54 static inline WCHAR to_lower( WCHAR ch )
56 extern const WCHAR wine_casemap_lower[];
57 return ch + wine_casemap_lower[wine_casemap_lower[ch >> 8] + (ch & 0xff)];
60 int memicmp_strW( const WCHAR *str1, const WCHAR *str2, data_size_t len )
62 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;
69 unsigned int hash_strW( const WCHAR *str, data_size_t len, unsigned int hash_size )
71 unsigned int i, hash = 0;
73 for (i = 0; i < len / sizeof(WCHAR); i++) hash = hash * 65599 + to_lower( str[i] );
74 return hash % hash_size;
77 WCHAR *ascii_to_unicode_str( const char *str, struct unicode_str *ret )
79 data_size_t i, len = strlen(str);
80 WCHAR *p;
82 ret->len = len * sizeof(WCHAR);
83 ret->str = p = mem_alloc( ret->len );
84 if (p) for (i = 0; i < len; i++) p[i] = (unsigned char)str[i];
85 return p;
88 /* parse an escaped string back into Unicode */
89 /* return the number of chars read from the input, or -1 on output overflow */
90 int parse_strW( WCHAR *buffer, data_size_t *len, const char *src, char endchar )
92 WCHAR *dest = buffer;
93 WCHAR *end = buffer + *len / sizeof(WCHAR);
94 const char *p = src;
95 unsigned char ch;
97 while (*p && *p != endchar && dest < end)
99 if (*p == '\\')
101 p++;
102 if (!*p) break;
103 switch(*p)
105 case 'a': *dest++ = '\a'; p++; continue;
106 case 'b': *dest++ = '\b'; p++; continue;
107 case 'e': *dest++ = '\e'; p++; continue;
108 case 'f': *dest++ = '\f'; p++; continue;
109 case 'n': *dest++ = '\n'; p++; continue;
110 case 'r': *dest++ = '\r'; p++; continue;
111 case 't': *dest++ = '\t'; p++; continue;
112 case 'v': *dest++ = '\v'; p++; continue;
113 case 'x': /* hex escape */
114 p++;
115 if (!isxdigit(*p)) *dest = 'x';
116 else
118 *dest = to_hex(*p++);
119 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
120 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
121 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
123 dest++;
124 continue;
125 case '0':
126 case '1':
127 case '2':
128 case '3':
129 case '4':
130 case '5':
131 case '6':
132 case '7': /* octal escape */
133 *dest = *p++ - '0';
134 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
135 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
136 dest++;
137 continue;
139 /* unrecognized escape: fall through to normal char handling */
143 ch = *p++;
144 if (ch < 0x80) *dest++ = ch;
145 else /* parse utf8 char */
147 int charlen = utf8_length[ch-0x80];
148 unsigned int res = ch & utf8_mask[charlen];
150 switch(charlen)
152 case 3:
153 if ((ch = *p ^ 0x80) >= 0x40) break;
154 res = (res << 6) | ch;
155 p++;
156 case 2:
157 if ((ch = *p ^ 0x80) >= 0x40) break;
158 res = (res << 6) | ch;
159 p++;
160 case 1:
161 if ((ch = *p ^ 0x80) >= 0x40) break;
162 res = (res << 6) | ch;
163 p++;
164 if (res < utf8_minval[charlen]) break;
165 if (res > 0x10ffff) break;
166 if (res <= 0xffff) *dest++ = res;
167 else /* we need surrogates */
169 res -= 0x10000;
170 *dest++ = 0xd800 | (res >> 10);
171 if (dest < end) *dest++ = 0xdc00 | (res & 0x3ff);
173 continue;
175 /* ignore invalid char */
178 if (dest >= end) return -1; /* overflow */
179 *dest++ = 0;
180 if (!*p) return -1; /* delimiter not found */
181 *len = (dest - buffer) * sizeof(WCHAR);
182 return p + 1 - src;
185 /* dump a Unicode string with proper escaping */
186 int dump_strW( const WCHAR *str, data_size_t len, FILE *f, const char escape[2] )
188 static const char escapes[32] = ".......abtnvfr.............e....";
189 char buffer[256];
190 char *pos = buffer;
191 int count = 0;
193 for (len /= sizeof(WCHAR); len; str++, len--)
195 if (pos > buffer + sizeof(buffer) - 8)
197 fwrite( buffer, pos - buffer, 1, f );
198 count += pos - buffer;
199 pos = buffer;
201 if (*str > 127) /* hex escape */
203 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
204 pos += sprintf( pos, "\\x%04x", *str );
205 else
206 pos += sprintf( pos, "\\x%x", *str );
207 continue;
209 if (*str < 32) /* octal or C escape */
211 if (!*str && len == 1) continue; /* do not output terminating NULL */
212 if (escapes[*str] != '.')
213 pos += sprintf( pos, "\\%c", escapes[*str] );
214 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
215 pos += sprintf( pos, "\\%03o", *str );
216 else
217 pos += sprintf( pos, "\\%o", *str );
218 continue;
220 if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
221 *pos++ = *str;
223 fwrite( buffer, pos - buffer, 1, f );
224 count += pos - buffer;
225 return count;