1
/*********************************************************************
5
Unicode related functions
7
****************************************************************************
12
Redistribution and use in source and binary forms, with or without
13
modification, are permitted provided that the following conditions are
16
* Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
18
* Redistributions in binary form must reproduce the above copyright
19
notice, this list of conditions and the following disclaimer in
20
the documentation and/or other materials provided with the
22
* Neither the name 'MAME' nor the names of its contributors may be
23
used to endorse or promote products derived from this software
24
without specific prior written permission.
26
THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
POSSIBILITY OF SUCH DAMAGE.
38
***************************************************************************/
43
/*-------------------------------------------------
44
uchar_isvalid - return true if a given
45
character is a legitimate unicode character
46
-------------------------------------------------*/
48
int uchar_isvalid(unicode_char uchar)
50
return (uchar < 0x110000) && !((uchar >= 0xd800) && (uchar <= 0xdfff));
54
/*-------------------------------------------------
55
uchar_from_utf8 - convert a UTF-8 sequence
56
into a unicode character
57
-------------------------------------------------*/
59
int uchar_from_utf8(unicode_char *uchar, const char *utf8char, size_t count)
61
unicode_char c, minchar;
65
/* validate parameters */
66
if (utf8char == NULL || count == 0)
69
/* start with the first byte */
70
c = (unsigned char) *utf8char;
74
/* based on that, determine how many additional bytes we need */
77
/* unicode char 0x00000000 - 0x0000007F */
82
else if (c >= 0xc0 && c < 0xe0)
84
/* unicode char 0x00000080 - 0x000007FF */
89
else if (c >= 0xe0 && c < 0xf0)
91
/* unicode char 0x00000800 - 0x0000FFFF */
96
else if (c >= 0xf0 && c < 0xf8)
98
/* unicode char 0x00010000 - 0x001FFFFF */
101
minchar = 0x00010000;
103
else if (c >= 0xf8 && c < 0xfc)
105
/* unicode char 0x00200000 - 0x03FFFFFF */
108
minchar = 0x00200000;
110
else if (c >= 0xfc && c < 0xfe)
112
/* unicode char 0x04000000 - 0x7FFFFFFF */
115
minchar = 0x04000000;
123
/* exceeds the count? */
127
/* we now know how long the char is, now compute it */
128
for (i = 0; i < auxlen; i++)
130
auxchar = utf8char[i];
132
/* all auxillary chars must be between 0x80-0xbf */
133
if ((auxchar & 0xc0) != 0x80)
140
/* make sure that this char is above the minimum */
149
/*-------------------------------------------------
150
uchar_from_utf16 - convert a UTF-16 sequence
151
into a unicode character
152
-------------------------------------------------*/
154
int uchar_from_utf16(unicode_char *uchar, const utf16_char *utf16char, size_t count)
158
/* validate parameters */
159
if (utf16char == NULL || count == 0)
162
/* handle the two-byte case */
163
if (utf16char[0] >= 0xd800 && utf16char[0] <= 0xdbff)
165
if (count > 1 && utf16char[1] >= 0xdc00 && utf16char[1] <= 0xdfff)
167
*uchar = 0x10000 + ((utf16char[0] & 0x3ff) * 0x400) + (utf16char[1] & 0x3ff);
172
/* handle the one-byte case */
173
else if (utf16char[0] < 0xdc00 || utf16char[0] > 0xdfff)
175
*uchar = utf16char[0];
183
/*-------------------------------------------------
184
uchar_from_utf16f - convert a UTF-16 sequence
185
into a unicode character from a flipped
187
-------------------------------------------------*/
189
int uchar_from_utf16f(unicode_char *uchar, const utf16_char *utf16char, size_t count)
191
utf16_char buf[2] = {0};
193
buf[0] = FLIPENDIAN_INT16(utf16char[0]);
195
buf[1] = FLIPENDIAN_INT16(utf16char[1]);
196
return uchar_from_utf16(uchar, buf, count);
200
/*-------------------------------------------------
201
utf8_from_uchar - convert a unicode character
202
into a UTF-8 sequence
203
-------------------------------------------------*/
205
int utf8_from_uchar(char *utf8string, size_t count, unicode_char uchar)
209
/* error on invalid characters */
210
if (!uchar_isvalid(uchar))
213
/* based on the value, output the appropriate number of bytes */
216
/* unicode char 0x00000000 - 0x0000007F */
219
utf8string[rc++] = (char) uchar;
221
else if (uchar < 0x800)
223
/* unicode char 0x00000080 - 0x000007FF */
226
utf8string[rc++] = ((char) (uchar >> 6)) | 0xC0;
227
utf8string[rc++] = ((char) ((uchar >> 0) & 0x3F)) | 0x80;
229
else if (uchar < 0x10000)
231
/* unicode char 0x00000800 - 0x0000FFFF */
234
utf8string[rc++] = ((char) (uchar >> 12)) | 0xE0;
235
utf8string[rc++] = ((char) ((uchar >> 6) & 0x3F)) | 0x80;
236
utf8string[rc++] = ((char) ((uchar >> 0) & 0x3F)) | 0x80;
238
else if (uchar < 0x00200000)
240
/* unicode char 0x00010000 - 0x001FFFFF */
243
utf8string[rc++] = ((char) (uchar >> 18)) | 0xF0;
244
utf8string[rc++] = ((char) ((uchar >> 12) & 0x3F)) | 0x80;
245
utf8string[rc++] = ((char) ((uchar >> 6) & 0x3F)) | 0x80;
246
utf8string[rc++] = ((char) ((uchar >> 0) & 0x3F)) | 0x80;
248
else if (uchar < 0x04000000)
250
/* unicode char 0x00200000 - 0x03FFFFFF */
253
utf8string[rc++] = ((char) (uchar >> 24)) | 0xF8;
254
utf8string[rc++] = ((char) ((uchar >> 18) & 0x3F)) | 0x80;
255
utf8string[rc++] = ((char) ((uchar >> 12) & 0x3F)) | 0x80;
256
utf8string[rc++] = ((char) ((uchar >> 6) & 0x3F)) | 0x80;
257
utf8string[rc++] = ((char) ((uchar >> 0) & 0x3F)) | 0x80;
259
else if (uchar < 0x80000000)
261
/* unicode char 0x04000000 - 0x7FFFFFFF */
264
utf8string[rc++] = ((char) (uchar >> 30)) | 0xFC;
265
utf8string[rc++] = ((char) ((uchar >> 24) & 0x3F)) | 0x80;
266
utf8string[rc++] = ((char) ((uchar >> 18) & 0x3F)) | 0x80;
267
utf8string[rc++] = ((char) ((uchar >> 12) & 0x3F)) | 0x80;
268
utf8string[rc++] = ((char) ((uchar >> 6) & 0x3F)) | 0x80;
269
utf8string[rc++] = ((char) ((uchar >> 0) & 0x3F)) | 0x80;
278
/*-------------------------------------------------
279
utf16_from_uchar - convert a unicode character
280
into a UTF-16 sequence
281
-------------------------------------------------*/
283
int utf16_from_uchar(utf16_char *utf16string, size_t count, unicode_char uchar)
287
/* error on invalid characters */
288
if (!uchar_isvalid(uchar))
291
/* single word case */
296
utf16string[0] = (utf16_char) uchar;
300
/* double word case */
301
else if (uchar < 0x100000)
305
utf16string[0] = ((uchar >> 10) & 0x03ff) | 0xd800;
306
utf16string[1] = ((uchar >> 0) & 0x03ff) | 0xdc00;
315
/*-------------------------------------------------
316
utf16_from_uchar - convert a unicode character
317
into a UTF-16 sequence with flipped endianness
318
-------------------------------------------------*/
320
int utf16f_from_uchar(utf16_char *utf16string, size_t count, unicode_char uchar)
323
utf16_char buf[2] = { 0, 0 };
325
rc = utf16_from_uchar(buf, count, uchar);
328
utf16string[0] = FLIPENDIAN_INT16(buf[0]);
330
utf16string[1] = FLIPENDIAN_INT16(buf[1]);
335
/*-------------------------------------------------
336
utf8_previous_char - return a pointer to the
337
previous character in a string
338
-------------------------------------------------*/
340
const char *utf8_previous_char(const char *utf8string)
342
while ((*--utf8string & 0xc0) == 0x80)
348
/*-------------------------------------------------
349
utf8_is_valid_string - return true if the
350
given string is a properly formed sequence of
352
-------------------------------------------------*/
354
int utf8_is_valid_string(const char *utf8string)
356
int remaining_length = strlen(utf8string);
358
while (*utf8string != 0)
360
unicode_char uchar = 0;
363
/* extract the current character and verify it */
364
charlen = uchar_from_utf8(&uchar, utf8string, remaining_length);
365
if (charlen <= 0 || uchar == 0 || !uchar_isvalid(uchar))
369
utf8string += charlen;
370
remaining_length -= charlen;