summaryrefslogtreecommitdiff
path: root/base/gp_wutf8.c
blob: b7b1d075893a5cff4608162ba89c4774e1efc6e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Copyright (C) 2001-2021 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


#include "windows_.h"

int utf8_to_wchar(wchar_t *out, const char *in)
{
    unsigned int i;
    unsigned int len = 1;
    unsigned char c;

    if (out) {
        while (i = *(unsigned char *)in++) {
            if (i < 0x80) {
                *out++ = (wchar_t)i;
                len++;
            } else if ((i & 0xE0) == 0xC0) {
                i &= 0x1F;
                c = (unsigned char)*in++;
                if ((c & 0xC0) != 0x80)
                    return -1;
                i = (i<<6) | (c & 0x3f);
                *out++ = (wchar_t)i;
                len++;
            } else if ((i & 0xF0) == 0xE0) {
                i &= 0xF;
                c = (unsigned char)*in++;
                if ((c & 0xC0) != 0x80)
                    return -1;
                i = (i<<6) | (c & 0x3f);
                c = (unsigned char)*in++;
                if ((c & 0xC0) != 0x80)
                    return -1;
                i = (i<<6) | (c & 0x3f);
                *out++ = (wchar_t)i;
                len++;
            } else {
                return -1;
            }
        }
        *out = 0;
    } else {
        while (i = *(unsigned char *)in++) {
            if (i < 0x80) {
                len++;
            } else if ((i & 0xE0) == 0xC0) {
                in++;
                len++;
            } else if ((i & 0xF0) == 0xE0) {
                in+=2;
                len++;
            } else {
                return -1;
            }
        }
    }
    return len;
}

int wchar_to_utf8(char *out, const wchar_t *in)
{
    unsigned int i;
    unsigned int len = 1;

    if (out) {
        while (i = (unsigned int)*in++) {
            if (i < 0x80) {
                *out++ = (char)i;
                len++;
            } else if (i < 0x800) {
                *out++ = 0xC0 | ( i>> 6        );
                *out++ = 0x80 | ( i      & 0x3F);
                len+=2;
            } else /* if (i < 0x10000) */ {
                *out++ = 0xE0 | ( i>>12        );
                *out++ = 0x80 | ((i>> 6) & 0x3F);
                *out++ = 0x80 | ( i      & 0x3F);
                len+=3;
            }
        }
        *out = 0;
    } else {
        while (i = (unsigned int)*in++) {
            if (i < 0x80) {
                len++;
            } else if (i < 0x800) {
                len += 2;
            } else /* if (i < 0x10000) */ {
                len += 3;
            }
        }
    }
    return len;
}