-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcharset.cpp
304 lines (274 loc) · 7.17 KB
/
charset.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// charset.cpp - part of TweakPNG
/*
Copyright (C) 2008 Jason Summers
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 3 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.
See the file tweakpng-src.txt for more information.
*/
// Note: I am aware of the WideCharToMultiByte and MultiByteToWideChar Windows
// API functions, but they don't work the same on all versions of Windows,
// and anyway I was interested in trying to write my own functions to do this.
#include "twpng-config.h"
#include <windows.h>
#include <tchar.h>
#include <malloc.h>
#include "tweakpng.h"
#ifdef UNICODE
int convert_tchar_to_latin1(const TCHAR *src, int srclen,
char **pdst, int *pdstlen)
{
UINT16 c;
int spos,dpos;
unsigned char *dst;
*pdst=NULL;
*pdstlen=0;
dst = (unsigned char*)malloc((size_t)srclen+1); // This memory won't all be used if there are surrogate pairs.
if(!dst) return 0;
dpos=0;
for(spos=0;spos<srclen;spos++) {
c=(UINT16)src[spos];
if( (c & 0xfc00)==0xd800 ) {
// first word of a surrogate pair
}
else if( (c & 0xfc00)==0xdc00 ) {
// second word of a surrogate pair
dst[dpos++] = '?';
}
else if(c>0xff) {
// character not representable in latin-1
dst[dpos++] = '?';
}
else {
dst[dpos++] = (unsigned char)c;
}
}
dst[dpos] = '\0';
*pdst = (char*)dst;
*pdstlen = dpos;
return 1;
}
#else
int convert_tchar_to_latin1(const TCHAR *src, int srclen,
char **pdst, int *pdstlen)
{
char *dst;
*pdst=NULL;
*pdstlen=0;
dst = (char*)malloc(srclen+1);
if(!dst) return 0;
memcpy(dst,src,srclen);
dst[srclen]='\0';
*pdst=dst;
*pdstlen=srclen;
return 1;
}
#endif
int convert_latin1_to_tchar(const char *src, int srclen,
TCHAR **pdst, int *pdstlen)
{
TCHAR *dst;
int i;
*pdst=NULL;
*pdstlen=0;
dst=(TCHAR*)malloc(sizeof(TCHAR)*((size_t)srclen+1));
if(!dst) return 0;
for(i=0;i<srclen;i++) {
dst[i] = ((unsigned char)src[i]);
}
dst[srclen]='\0';
*pdst=dst;
*pdstlen=srclen;
return 1;
}
#ifdef UNICODE
// Returns the number of bytes that the given utf8 string would
// require if converted to utf16. srclen is in bytes.
static int utf8_to_utf16_count_bytes(const void *src, int srclen)
{
int count=0;
int i;
unsigned char c;
for(i=0;i<srclen;i++) {
c= ((const unsigned char*)src)[i];
if(c<=0x7f) {
// 1-byte utf8 character (=2 bytes in utf16)
count+=2;
}
else if(c>=0x80 && c<=0xbf) {
// non-initial byte of a multi-byte utf8 character
;
}
else if(c>=0xc0 && c<=0xef) {
// 1st byte of a 2-byte or 3-byte utf8 character (=2 bytes in utf16)
count+=2;
}
else if(c>=0xf0 && c<=0xf7) {
// 1st byte of a 4-byte utf8 character (=4 bytes in utf16)
count+=4;
}
else {
// invalid byte
;
}
}
return count;
}
int convert_utf8_to_utf16(const void *src, int srclen,
WCHAR **pdst, int *pdstlen)
{
WCHAR *dst;
int pending_char;
int utf8_more_bytes_expected = 0;
unsigned char c;
int dstpos;
int i;
int memneeded;
memneeded = utf8_to_utf16_count_bytes(src,srclen);
dst = (WCHAR*)malloc((size_t)memneeded+10);
if(!dst) return 0;
dstpos=0;
pending_char=0;
for(i=0;i<srclen;i++) {
c= ((const unsigned char*)src)[i];
if(c<=0x7f) {
// 1-byte utf8 character
pending_char=0;
utf8_more_bytes_expected = 0;
dst[dstpos++] = c;
}
else if((c>=0x80 && c<=0xbf) && utf8_more_bytes_expected>0) {
// non-initial byte of a multi-byte utf8 character
pending_char = (pending_char<<6)|(c&0x3f);
utf8_more_bytes_expected--;
if(utf8_more_bytes_expected==0) {
if(pending_char>=0xd800 && pending_char<=0xdfff) {
// unrepresentable character in the surrogate-pair range
dst[dstpos++] = 0xfffd;
}
else if(pending_char<=0xffff) {
// normal single-word character
dst[dstpos++] = pending_char;
}
else {
// Character representable using a surrogate pair
// TODO: make sure this is correct.
dst[dstpos++] = 0xd800 | ((pending_char-0x10000)>>10);
dst[dstpos++] = 0xdc00 | ((pending_char-0x10000)&0x03ff);
}
}
}
else if(c>=0xc0 && c<=0xdf) {
// 1st byte of a 2-byte utf8 character
pending_char = c&0x1f;
utf8_more_bytes_expected = 1;
}
else if(c>=0xe0 && c<=0xef) {
// 1st byte of a 3-byte utf8 character
pending_char = c&0x0f;
utf8_more_bytes_expected = 2;
}
else if(c>=0xf0 && c<=0xf7) {
// 1st byte of a 4-byte utf8 character
pending_char = c&0x07;
utf8_more_bytes_expected = 3;
}
else {
// invalid byte
pending_char=0;
utf8_more_bytes_expected = 0;
}
}
dst[dstpos]= '\0';
*pdst = dst;
*pdstlen = dstpos;
return 1;
}
#endif
#ifdef UNICODE
// Returns the number of bytes that the given utf16 string would
// require if converted to utf8. srclen is in WCHARs.
static int utf16_to_utf8_count_bytes(const WCHAR *src, int srclen)
{
int i, c;
int count = 0;
for(i=0;i<srclen;i++) {
c = (int)src[i];
if(c<=0x7f) { // 1-byte utf8 character
count+=1;
}
else if(c<=0x07ff) { // 2-byte utf8 character
count+=2;
}
else if(c>=0xd800 && c<=0xdbff) {
// first word of a surrogate pair
;
}
else if(c>=0xdc00 && c<=0xdfff) {
// second word of a surrogate pair
// the surrogate pair as a whole => 4-byte utf8 character
count+=4;
}
else { // 3-byte utf8 character
count+=3;
}
}
return count;
}
int convert_utf16_to_utf8(const WCHAR *src, int srclen,
char **pdst, int *pdstlen)
{
unsigned char *dst;
int dpos;
int i;
int c;
int pending_char = 0;
int codept;
int memneeded;
*pdst = NULL;
*pdstlen=0;
memneeded = utf16_to_utf8_count_bytes(src,srclen);
dst = (unsigned char*)malloc((size_t)memneeded+10);
if(!dst) return 0;
dpos=0;
for(i=0;i<srclen;i++) {
c = (int)src[i];
if(c<=0x7f) { // 1-byte utf8 character
dst[dpos++] = (unsigned char)c;
}
else if(c<=0x07ff) { // 2-byte utf8 character
dst[dpos++] = 0xc0 | (c>>6);
dst[dpos++] = 0x80 | (c&0x3f);
}
else if(c>=0xd800 && c<=0xdbff) {
// first word of a surrogate pair
pending_char = c;
}
else if(c>=0xdc00 && c<=0xdfff) {
// second word of a surrogate pair => 4-byte utf8 character
// TODO: make sure this is correct.
codept = (pending_char & 0x03ff) << 10;
codept |= (c & 0x03ff);
codept += 0x10000;
dst[dpos++] = 0xf0 | (codept>>18);
dst[dpos++] = 0x80 | ((codept>>12)&0x3f);
dst[dpos++] = 0x80 | ((codept>>6)&0x3f);
dst[dpos++] = 0x80 | (codept&0x3f);
}
else { // 3-byte utf8 character
dst[dpos++] = 0xe0 | (c>>12);
dst[dpos++] = 0x80 | ((c>>6)&0x3f);
dst[dpos++] = 0x80 | (c&0x3f);
}
}
dst[dpos]='\0';
*pdstlen = dpos;
*pdst = (char*)dst;
return 1;
}
#endif