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"
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
)
64 for (len
/= sizeof(WCHAR
); len
; str1
++, str2
++, len
--)
65 if ((ret
= to_lower(*str1
) - to_lower(*str2
))) break;
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
);
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
];
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
)
93 WCHAR
*end
= buffer
+ *len
/ sizeof(WCHAR
);
97 while (*p
&& *p
!= endchar
&& dest
< end
)
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 */
115 if (!isxdigit(*p
)) *dest
= 'x';
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
++);
132 case '7': /* octal escape */
134 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
135 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
139 /* unrecognized escape: fall through to normal char handling */
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
];
153 if ((ch
= *p
^ 0x80) >= 0x40) break;
154 res
= (res
<< 6) | ch
;
157 if ((ch
= *p
^ 0x80) >= 0x40) break;
158 res
= (res
<< 6) | ch
;
161 if ((ch
= *p
^ 0x80) >= 0x40) break;
162 res
= (res
<< 6) | ch
;
164 if (res
< utf8_minval
[charlen
]) break;
165 if (res
> 0x10ffff) break;
166 if (res
<= 0xffff) *dest
++ = res
;
167 else /* we need surrogates */
170 *dest
++ = 0xd800 | (res
>> 10);
171 if (dest
< end
) *dest
++ = 0xdc00 | (res
& 0x3ff);
175 /* ignore invalid char */
178 if (dest
>= end
) return -1; /* overflow */
180 if (!*p
) return -1; /* delimiter not found */
181 *len
= (dest
- buffer
) * sizeof(WCHAR
);
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....";
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
;
201 if (*str
> 127) /* hex escape */
203 if (len
> 1 && str
[1] < 128 && isxdigit((char)str
[1]))
204 pos
+= sprintf( pos
, "\\x%04x", *str
);
206 pos
+= sprintf( pos
, "\\x%x", *str
);
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
);
217 pos
+= sprintf( pos
, "\\%o", *str
);
220 if (*str
== '\\' || *str
== escape
[0] || *str
== escape
[1]) *pos
++ = '\\';
223 fwrite( buffer
, pos
- buffer
, 1, f
);
224 count
+= pos
- buffer
;