ReactOS  0.4.14-dev-115-g4576127
utf8.c File Reference
#include <string.h>
#include "wine/unicode.h"
Include dependency graph for utf8.c:

Go to the source code of this file.

Functions

WCHAR wine_compose (const WCHAR *str) DECLSPEC_HIDDEN
 
static unsigned int get_surrogate_value (const WCHAR *src, unsigned int srclen)
 
static int get_length_wcs_utf8 (int flags, const WCHAR *src, unsigned int srclen)
 
int wine_utf8_wcstombs (int flags, const WCHAR *src, int srclen, char *dst, int dstlen)
 
static unsigned int decode_utf8_char (unsigned char ch, const char **str, const char *strend)
 
static int get_length_mbs_utf8_compose (int flags, const char *src, int srclen)
 
static int utf8_mbstowcs_compose (int flags, const char *src, int srclen, WCHAR *dst, int dstlen)
 
static int get_length_mbs_utf8 (int flags, const char *src, int srclen)
 
int wine_utf8_mbstowcs (int flags, const char *src, int srclen, WCHAR *dst, int dstlen)
 

Variables

static const char utf8_length [128]
 
static const unsigned char utf8_mask [4] = { 0x7f, 0x1f, 0x0f, 0x07 }
 
static const unsigned int utf8_minval [4] = { 0x0, 0x80, 0x800, 0x10000 }
 

Function Documentation

◆ decode_utf8_char()

static unsigned int decode_utf8_char ( unsigned char  ch,
const char **  str,
const char strend 
)
inlinestatic

Definition at line 161 of file utf8.c.

162 {
163  unsigned int len = utf8_length[ch-0x80];
164  unsigned int res = ch & utf8_mask[len];
165  const char *end = *str + len;
166 
167  if (end > strend) return ~0;
168  switch(len)
169  {
170  case 3:
171  if ((ch = end[-3] ^ 0x80) >= 0x40) break;
172  res = (res << 6) | ch;
173  (*str)++;
174  case 2:
175  if ((ch = end[-2] ^ 0x80) >= 0x40) break;
176  res = (res << 6) | ch;
177  (*str)++;
178  case 1:
179  if ((ch = end[-1] ^ 0x80) >= 0x40) break;
180  res = (res << 6) | ch;
181  (*str)++;
182  if (res < utf8_minval[len]) break;
183  return res;
184  }
185  return ~0;
186 }
GLuint GLuint end
Definition: gl.h:1545
static const unsigned char utf8_mask[4]
Definition: utf8.c:41
static const char utf8_length[128]
Definition: utf8.c:28
const WCHAR * str
GLenum GLsizei len
Definition: glext.h:6722
GLuint res
Definition: glext.h:9613
static const unsigned int utf8_minval[4]
Definition: utf8.c:44

Referenced by get_length_mbs_utf8(), get_length_mbs_utf8_compose(), utf8_mbstowcs_compose(), and wine_utf8_mbstowcs().

◆ get_length_mbs_utf8()

static int get_length_mbs_utf8 ( int  flags,
const char src,
int  srclen 
)
inlinestatic

Definition at line 277 of file utf8.c.

278 {
279  int ret = 0;
280  unsigned int res;
281  const char *srcend = src + srclen;
282 
283  while (src < srcend)
284  {
285  unsigned char ch = *src++;
286  if (ch < 0x80) /* special fast case for 7-bit ASCII */
287  {
288  ret++;
289  continue;
290  }
291  if ((res = decode_utf8_char( ch, &src, srcend )) <= 0x10ffff)
292  {
293  if (res > 0xffff) ret++;
294  ret++;
295  }
296  else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
297  /* otherwise ignore it */
298  }
299  return ret;
300 }
#define MB_ERR_INVALID_CHARS
Definition: unicode.h:41
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
GLbitfield flags
Definition: glext.h:7161
int ret
GLenum src
Definition: glext.h:6340
GLuint res
Definition: glext.h:9613
static unsigned int decode_utf8_char(unsigned char ch, const char **str, const char *strend)
Definition: utf8.c:161

Referenced by wine_utf8_mbstowcs().

◆ get_length_mbs_utf8_compose()

static int get_length_mbs_utf8_compose ( int  flags,
const char src,
int  srclen 
)
inlinestatic

Definition at line 189 of file utf8.c.

190 {
191  int ret = 0;
192  unsigned int res;
193  WCHAR composed[2];
194  const char *srcend = src + srclen;
195 
196  composed[0] = 0;
197  while (src < srcend)
198  {
199  unsigned char ch = *src++;
200  if (ch < 0x80) /* special fast case for 7-bit ASCII */
201  {
202  composed[0] = ch;
203  ret++;
204  continue;
205  }
206  if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
207  {
208  if (composed[0])
209  {
210  composed[1] = res;
211  if ((composed[0] = wine_compose( composed ))) continue;
212  }
213  composed[0] = res;
214  ret++;
215  }
216  else if (res <= 0x10ffff)
217  {
218  ret += 2;
219  composed[0] = 0; /* no composition for surrogates */
220  }
221  else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
222  /* otherwise ignore it */
223  }
224  return ret;
225 }
#define MB_ERR_INVALID_CHARS
Definition: unicode.h:41
WCHAR wine_compose(const WCHAR *str) DECLSPEC_HIDDEN
Definition: compose.c:395
__wchar_t WCHAR
Definition: xmlstorage.h:180
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
GLbitfield flags
Definition: glext.h:7161
int ret
GLenum src
Definition: glext.h:6340
GLuint res
Definition: glext.h:9613
static unsigned int decode_utf8_char(unsigned char ch, const char **str, const char *strend)
Definition: utf8.c:161

Referenced by utf8_mbstowcs_compose().

◆ get_length_wcs_utf8()

static int get_length_wcs_utf8 ( int  flags,
const WCHAR src,
unsigned int  srclen 
)
inlinestatic

Definition at line 62 of file utf8.c.

63 {
64  int len;
65  unsigned int val;
66 
67  for (len = 0; srclen; srclen--, src++)
68  {
69  if (*src < 0x80) /* 0x00-0x7f: 1 byte */
70  {
71  len++;
72  continue;
73  }
74  if (*src < 0x800) /* 0x80-0x7ff: 2 bytes */
75  {
76  len += 2;
77  continue;
78  }
79  if (!(val = get_surrogate_value( src, srclen )))
80  {
81  if (flags & WC_ERR_INVALID_CHARS) return -2;
82  continue;
83  }
84  if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
85  len += 3;
86  else /* 0x10000-0x10ffff: 4 bytes */
87  {
88  len += 4;
89  src++;
90  srclen--;
91  }
92  }
93  return len;
94 }
static unsigned int get_surrogate_value(const WCHAR *src, unsigned int srclen)
Definition: utf8.c:48
GLuint GLfloat * val
Definition: glext.h:7180
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
#define WC_ERR_INVALID_CHARS
Definition: unicode.h:47
GLbitfield flags
Definition: glext.h:7161
GLenum GLsizei len
Definition: glext.h:6722
GLenum src
Definition: glext.h:6340

Referenced by wine_utf8_wcstombs().

◆ get_surrogate_value()

static unsigned int get_surrogate_value ( const WCHAR src,
unsigned int  srclen 
)
inlinestatic

Definition at line 48 of file utf8.c.

49 {
50  if (src[0] >= 0xd800 && src[0] <= 0xdfff) /* surrogate pair */
51  {
52  if (src[0] > 0xdbff || /* invalid high surrogate */
53  srclen <= 1 || /* missing low surrogate */
54  src[1] < 0xdc00 || src[1] > 0xdfff) /* invalid low surrogate */
55  return 0;
56  return 0x10000 + ((src[0] & 0x3ff) << 10) + (src[1] & 0x3ff);
57  }
58  return src[0];
59 }
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
GLenum src
Definition: glext.h:6340

Referenced by get_length_wcs_utf8(), and wine_utf8_wcstombs().

◆ utf8_mbstowcs_compose()

static int utf8_mbstowcs_compose ( int  flags,
const char src,
int  srclen,
WCHAR dst,
int  dstlen 
)
static

Definition at line 229 of file utf8.c.

230 {
231  unsigned int res;
232  const char *srcend = src + srclen;
233  WCHAR composed[2];
234  WCHAR *dstend = dst + dstlen;
235 
237 
238  composed[0] = 0;
239  while (src < srcend)
240  {
241  unsigned char ch = *src++;
242  if (ch < 0x80) /* special fast case for 7-bit ASCII */
243  {
244  if (dst >= dstend) return -1; /* overflow */
245  *dst++ = composed[0] = ch;
246  continue;
247  }
248  if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
249  {
250  if (composed[0])
251  {
252  composed[1] = res;
253  if ((composed[0] = wine_compose( composed )))
254  {
255  dst[-1] = composed[0];
256  continue;
257  }
258  }
259  if (dst >= dstend) return -1; /* overflow */
260  *dst++ = composed[0] = res;
261  }
262  else if (res <= 0x10ffff) /* we need surrogates */
263  {
264  if (dst >= dstend - 1) return -1; /* overflow */
265  res -= 0x10000;
266  *dst++ = 0xd800 | (res >> 10);
267  *dst++ = 0xdc00 | (res & 0x3ff);
268  composed[0] = 0; /* no composition for surrogates */
269  }
270  else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
271  /* otherwise ignore it */
272  }
273  return dstlen - (dstend - dst);
274 }
#define MB_ERR_INVALID_CHARS
Definition: unicode.h:41
WCHAR wine_compose(const WCHAR *str) DECLSPEC_HIDDEN
Definition: compose.c:395
__wchar_t WCHAR
Definition: xmlstorage.h:180
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
static int get_length_mbs_utf8_compose(int flags, const char *src, int srclen)
Definition: utf8.c:189
GLbitfield flags
Definition: glext.h:7161
GLenum src
Definition: glext.h:6340
static DWORD dstlen
Definition: directory.c:51
GLenum GLenum dst
Definition: glext.h:6340
GLuint res
Definition: glext.h:9613
static unsigned int decode_utf8_char(unsigned char ch, const char **str, const char *strend)
Definition: utf8.c:161

Referenced by wine_utf8_mbstowcs().

◆ wine_compose()

WCHAR wine_compose ( const WCHAR str)

Definition at line 395 of file compose.c.

396 {
397  int pos, idx = 1, start = 0, count = 70;
398  for (;;)
399  {
400  if ((pos = binary_search( str[idx], start, count - 1 )) == -1) return 0;
401  if (!idx--) return table[2 * pos + 1];
402  start = table[2 * pos + 1];
403  count = table[2 * pos + 3];
404  }
405 }
GLuint GLuint GLsizei count
Definition: gl.h:1545
static int binary_search(WCHAR ch, int low, int high)
Definition: compose.c:383
unsigned int idx
Definition: utils.c:41
const WCHAR * str
GLuint start
Definition: gl.h:1545

Referenced by get_length_mbs_utf8_compose(), and utf8_mbstowcs_compose().

◆ wine_utf8_mbstowcs()

int wine_utf8_mbstowcs ( int  flags,
const char src,
int  srclen,
WCHAR dst,
int  dstlen 
)

Definition at line 304 of file utf8.c.

305 {
306  unsigned int res;
307  const char *srcend = src + srclen;
308  WCHAR *dstend = dst + dstlen;
309 
311 
312  if (!dstlen) return get_length_mbs_utf8( flags, src, srclen );
313 
314  while ((dst < dstend) && (src < srcend))
315  {
316  unsigned char ch = *src++;
317  if (ch < 0x80) /* special fast case for 7-bit ASCII */
318  {
319  *dst++ = ch;
320  continue;
321  }
322  if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
323  {
324  *dst++ = res;
325  }
326  else if (res <= 0x10ffff) /* we need surrogates */
327  {
328  if (dst == dstend - 1) return -1; /* overflow */
329  res -= 0x10000;
330  *dst++ = 0xd800 | (res >> 10);
331  *dst++ = 0xdc00 | (res & 0x3ff);
332  }
333  else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
334  /* otherwise ignore it */
335  }
336  if (src < srcend) return -1; /* overflow */
337  return dstlen - (dstend - dst);
338 }
#define MB_ERR_INVALID_CHARS
Definition: unicode.h:41
#define MB_COMPOSITE
Definition: unicode.h:40
static int get_length_mbs_utf8(int flags, const char *src, int srclen)
Definition: utf8.c:277
__wchar_t WCHAR
Definition: xmlstorage.h:180
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
GLbitfield flags
Definition: glext.h:7161
GLenum src
Definition: glext.h:6340
static DWORD dstlen
Definition: directory.c:51
GLenum GLenum dst
Definition: glext.h:6340
GLuint res
Definition: glext.h:9613
static unsigned int decode_utf8_char(unsigned char ch, const char **str, const char *strend)
Definition: utf8.c:161
static int utf8_mbstowcs_compose(int flags, const char *src, int srclen, WCHAR *dst, int dstlen)
Definition: utf8.c:229

◆ wine_utf8_wcstombs()

int wine_utf8_wcstombs ( int  flags,
const WCHAR src,
int  srclen,
char dst,
int  dstlen 
)

Definition at line 98 of file utf8.c.

99 {
100  int len;
101 
102  if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
103 
104  for (len = dstlen; srclen; srclen--, src++)
105  {
106  WCHAR ch = *src;
107  unsigned int val;
108 
109  if (ch < 0x80) /* 0x00-0x7f: 1 byte */
110  {
111  if (!len--) return -1; /* overflow */
112  *dst++ = ch;
113  continue;
114  }
115 
116  if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
117  {
118  if ((len -= 2) < 0) return -1; /* overflow */
119  dst[1] = 0x80 | (ch & 0x3f);
120  ch >>= 6;
121  dst[0] = 0xc0 | ch;
122  dst += 2;
123  continue;
124  }
125 
126  if (!(val = get_surrogate_value( src, srclen )))
127  {
128  if (flags & WC_ERR_INVALID_CHARS) return -2;
129  continue;
130  }
131 
132  if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
133  {
134  if ((len -= 3) < 0) return -1; /* overflow */
135  dst[2] = 0x80 | (val & 0x3f);
136  val >>= 6;
137  dst[1] = 0x80 | (val & 0x3f);
138  val >>= 6;
139  dst[0] = 0xe0 | val;
140  dst += 3;
141  }
142  else /* 0x10000-0x10ffff: 4 bytes */
143  {
144  if ((len -= 4) < 0) return -1; /* overflow */
145  dst[3] = 0x80 | (val & 0x3f);
146  val >>= 6;
147  dst[2] = 0x80 | (val & 0x3f);
148  val >>= 6;
149  dst[1] = 0x80 | (val & 0x3f);
150  val >>= 6;
151  dst[0] = 0xf0 | val;
152  dst += 4;
153  src++;
154  srclen--;
155  }
156  }
157  return dstlen - len;
158 }
static unsigned int get_surrogate_value(const WCHAR *src, unsigned int srclen)
Definition: utf8.c:48
GLuint GLfloat * val
Definition: glext.h:7180
static int get_length_wcs_utf8(int flags, const WCHAR *src, unsigned int srclen)
Definition: utf8.c:62
__wchar_t WCHAR
Definition: xmlstorage.h:180
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
#define WC_ERR_INVALID_CHARS
Definition: unicode.h:47
GLbitfield flags
Definition: glext.h:7161
GLenum GLsizei len
Definition: glext.h:6722
GLenum src
Definition: glext.h:6340
static DWORD dstlen
Definition: directory.c:51
GLenum GLenum dst
Definition: glext.h:6340

Variable Documentation

◆ utf8_length

const char utf8_length[128]
static
Initial value:
=
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0
}

Definition at line 28 of file utf8.c.

Referenced by decode_utf8_char(), and ui_clip_handle_data().

◆ utf8_mask

const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 }
static

Definition at line 41 of file utf8.c.

Referenced by decode_utf8_char().

◆ utf8_minval

const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 }
static

Definition at line 44 of file utf8.c.

Referenced by decode_utf8_char().