summaryrefslogtreecommitdiff
path: root/app/wlib/mswlib/utf8conv.c
blob: 5a39b349e6b2443fe5475af3d0ee2ef3095c24ab (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * \file utf8conv.c.
 *
 * UTF-8 conversion functions
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2020 Martin Fischer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <malloc.h>
#include <stdbool.h>
#include <string.h>

#include <Windows.h>

#include <wlib.h>

/**
 * Convert system codepage to UTF 8
 *
 * \param 		   inString		   The input string.
 * \param [in,out] outString	   The output string buffer.
 * \param 		   outStringLength Length of the output buffer
 *
 * \returns FALSE if it fails.
 */

bool
wSystemToUTF8(const char *inString, char *outString, unsigned outStringLength)
{
    unsigned int cnt = 2 * (unsigned int)(strlen(inString) + 1);
    char *tempBuffer = malloc(cnt);

    // convert to wide character (UTF16)
    MultiByteToWideChar(CP_ACP,
                        0,
                        inString,
                        -1,
                        (LPWSTR)tempBuffer,
                        cnt);

    // convert from wide char to UTF-8
    WideCharToMultiByte(CP_UTF8,
                        0,
                        (LPCWCH)tempBuffer,
                        -1,
                        (LPSTR)outString,
                        outStringLength,
                        NULL,
                        NULL);

    free(tempBuffer);
    return true;
}

/**
 * Convert from UTF-8 to system codepage
 *
 * \param 		   inString		   The input string.
 * \param [in,out] outString	   the output string.
 * \param 		   outStringLength Length of the output buffer.
 *
 * \returns True if it succeeds, false if it fails.
 */

bool
wUTF8ToSystem(const char *inString, char *outString, unsigned outStringLength)
{
    unsigned int cnt = 2 * (int)(strlen(inString) + 1);
    char *tempBuffer = malloc(cnt);

    // convert to wide character (UTF16)
    MultiByteToWideChar(CP_UTF8,
                        0,
                        inString,
                        -1,
                        (LPWSTR)tempBuffer,
                        cnt);


    cnt = WideCharToMultiByte(CP_ACP,
                              0,
                              (LPCWCH)tempBuffer,
                              -1,
                              (LPSTR)outString,
                              0L,
                              NULL,
                              NULL);

    if (outStringLength <= cnt) {
        return (false);
    }

    // convert from wide char to system codepage
    WideCharToMultiByte(CP_ACP,
                        0,
                        (LPCWCH)tempBuffer,
                        -1,
                        (LPSTR)outString,
                        outStringLength,
                        NULL,
                        NULL);

    free(tempBuffer);
    return true;
}

/**
 * Is passed string in correct UTF-8 format?
 * Taken from https://stackoverflow.com/questions/1031645/how-to-detect-utf-8-in-plain-c
 *
 * \param  string The string to check.
 *
 * \returns True if UTF 8, false if not.
 */

bool wIsUTF8(const char * string)
{
    if (!string) {
        return 0;
    }

    const unsigned char * bytes = (const unsigned char *)string;
    while (*bytes) {
        if ((// ASCII
                    // use bytes[0] <= 0x7F to allow ASCII control characters
                    bytes[0] == 0x09 ||
                    bytes[0] == 0x0A ||
                    bytes[0] == 0x0D ||
                    (0x20 <= bytes[0] && bytes[0] <= 0x7E)
                )
           ) {
            bytes += 1;
            continue;
        }

        if ((// non-overlong 2-byte
                    (0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
                    (0x80 <= bytes[1] && bytes[1] <= 0xBF)
                )
           ) {
            bytes += 2;
            continue;
        }

        if ((// excluding overlongs
                    bytes[0] == 0xE0 &&
                    (0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
                    (0x80 <= bytes[2] && bytes[2] <= 0xBF)
                ) ||
                (// straight 3-byte
                    ((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
                     bytes[0] == 0xEE ||
                     bytes[0] == 0xEF) &&
                    (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
                    (0x80 <= bytes[2] && bytes[2] <= 0xBF)
                ) ||
                (// excluding surrogates
                    bytes[0] == 0xED &&
                    (0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
                    (0x80 <= bytes[2] && bytes[2] <= 0xBF)
                )
           ) {
            bytes += 3;
            continue;
        }

        if ((// planes 1-3
                    bytes[0] == 0xF0 &&
                    (0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
                    (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
                    (0x80 <= bytes[3] && bytes[3] <= 0xBF)
                ) ||
                (// planes 4-15
                    (0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
                    (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
                    (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
                    (0x80 <= bytes[3] && bytes[3] <= 0xBF)
                ) ||
                (// plane 16
                    bytes[0] == 0xF4 &&
                    (0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
                    (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
                    (0x80 <= bytes[3] && bytes[3] <= 0xBF)
                )
           ) {
            bytes += 4;
            continue;
        }

        return false;
    }

    return true;
}