Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/wmc/utils.c
8586 views
1
/*
2
* Utility routines
3
*
4
* Copyright 1998,2000 Bertho A. Stultiens
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
*/
20
21
#include "config.h"
22
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <stdarg.h>
26
#include <string.h>
27
#include <assert.h>
28
#include <ctype.h>
29
30
#include "wmc.h"
31
#include "winternl.h"
32
#include "winnls.h"
33
#include "utils.h"
34
35
#define SUPPRESS_YACC_ERROR_MESSAGE
36
37
static void generic_msg(const char *s, const char *t, va_list ap)
38
{
39
fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
40
vfprintf(stderr, s, ap);
41
}
42
43
/*
44
* The yyerror routine should not exit because we use the error-token
45
* to determine the syntactic error in the source. However, YACC
46
* uses the same routine to print an error just before the error
47
* token is reduced.
48
* The extra routine 'xyyerror' is used to exit after giving a real
49
* message.
50
*/
51
int mcy_error(const char *s, ...)
52
{
53
#ifndef SUPPRESS_YACC_ERROR_MESSAGE
54
va_list ap;
55
va_start(ap, s);
56
generic_msg(s, "Yacc error", ap);
57
va_end(ap);
58
#endif
59
return 1;
60
}
61
62
int xyyerror(const char *s, ...)
63
{
64
va_list ap;
65
va_start(ap, s);
66
generic_msg(s, "Error", ap);
67
va_end(ap);
68
exit(1);
69
return 1;
70
}
71
72
int mcy_warning(const char *s, ...)
73
{
74
va_list ap;
75
va_start(ap, s);
76
generic_msg(s, "Warning", ap);
77
va_end(ap);
78
return 0;
79
}
80
81
void internal_error(const char *file, int line, const char *s, ...)
82
{
83
va_list ap;
84
va_start(ap, s);
85
fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
86
vfprintf(stderr, s, ap);
87
va_end(ap);
88
exit(3);
89
}
90
91
void error(const char *s, ...)
92
{
93
va_list ap;
94
va_start(ap, s);
95
fprintf(stderr, "Error: ");
96
vfprintf(stderr, s, ap);
97
va_end(ap);
98
exit(2);
99
}
100
101
void warning(const char *s, ...)
102
{
103
va_list ap;
104
va_start(ap, s);
105
fprintf(stderr, "Warning: ");
106
vfprintf(stderr, s, ap);
107
va_end(ap);
108
}
109
110
int unistrlen(const WCHAR *s)
111
{
112
int n;
113
for(n = 0; *s; n++, s++)
114
;
115
return n;
116
}
117
118
WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
119
{
120
WCHAR *t = dst;
121
while(*src)
122
*t++ = *src++;
123
*t = 0;
124
return dst;
125
}
126
127
WCHAR *xunistrdup(const WCHAR * str)
128
{
129
WCHAR *s;
130
131
assert(str != NULL);
132
s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
133
return unistrcpy(s, str);
134
}
135
136
int unistricmp(const WCHAR *s1, const WCHAR *s2)
137
{
138
int i;
139
int once = 0;
140
static const char warn[] = "Don't know the uppercase equivalent of non ascii characters;"
141
"comparison might yield wrong results";
142
while(*s1 && *s2)
143
{
144
if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
145
{
146
if(!once)
147
{
148
once++;
149
mcy_warning(warn);
150
}
151
i = *s1++ - *s2++;
152
}
153
else
154
i = toupper(*s1++) - toupper(*s2++);
155
if(i)
156
return i;
157
}
158
159
if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
160
{
161
if(!once)
162
mcy_warning(warn);
163
return *s1 - *s2;
164
}
165
else
166
return toupper(*s1) - toupper(*s2);
167
}
168
169
int unistrcmp(const WCHAR *s1, const WCHAR *s2)
170
{
171
int i;
172
while(*s1 && *s2)
173
{
174
i = *s1++ - *s2++;
175
if(i)
176
return i;
177
}
178
179
return *s1 - *s2;
180
}
181
182
WCHAR *utf8_to_unicode( const char *src, int srclen, int *dstlen )
183
{
184
static const char utf8_length[128] =
185
{
186
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
187
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
188
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
189
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
190
0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
191
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
192
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
193
3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
194
};
195
static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
196
197
const char *srcend = src + srclen;
198
int len, res;
199
WCHAR *ret, *dst;
200
201
dst = ret = xmalloc( (srclen + 1) * sizeof(WCHAR) );
202
while (src < srcend)
203
{
204
unsigned char ch = *src++;
205
if (ch < 0x80) /* special fast case for 7-bit ASCII */
206
{
207
*dst++ = ch;
208
continue;
209
}
210
len = utf8_length[ch - 0x80];
211
if (len && src + len <= srcend)
212
{
213
res = ch & utf8_mask[len];
214
switch (len)
215
{
216
case 3:
217
if ((ch = *src ^ 0x80) >= 0x40) break;
218
res = (res << 6) | ch;
219
src++;
220
if (res < 0x10) break;
221
case 2:
222
if ((ch = *src ^ 0x80) >= 0x40) break;
223
res = (res << 6) | ch;
224
if (res >= 0x110000 >> 6) break;
225
src++;
226
if (res < 0x20) break;
227
if (res >= 0xd800 >> 6 && res <= 0xdfff >> 6) break;
228
case 1:
229
if ((ch = *src ^ 0x80) >= 0x40) break;
230
res = (res << 6) | ch;
231
src++;
232
if (res < 0x80) break;
233
if (res <= 0xffff) *dst++ = res;
234
else
235
{
236
res -= 0x10000;
237
*dst++ = 0xd800 | (res >> 10);
238
*dst++ = 0xdc00 | (res & 0x3ff);
239
}
240
continue;
241
}
242
}
243
*dst++ = 0xfffd;
244
}
245
*dst = 0;
246
*dstlen = dst - ret;
247
return ret;
248
}
249
250
char *unicode_to_utf8( const WCHAR *src, int srclen, int *dstlen )
251
{
252
char *ret, *dst;
253
254
dst = ret = xmalloc( srclen * 3 + 1 );
255
for ( ; srclen; srclen--, src++)
256
{
257
unsigned int ch = *src;
258
259
if (ch < 0x80) /* 0x00-0x7f: 1 byte */
260
{
261
*dst++ = ch;
262
continue;
263
}
264
if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
265
{
266
dst[1] = 0x80 | (ch & 0x3f);
267
ch >>= 6;
268
dst[0] = 0xc0 | ch;
269
dst += 2;
270
continue;
271
}
272
if (ch >= 0xd800 && ch <= 0xdbff && srclen > 1 && src[1] >= 0xdc00 && src[1] <= 0xdfff)
273
{
274
/* 0x10000-0x10ffff: 4 bytes */
275
ch = 0x10000 + ((ch & 0x3ff) << 10) + (src[1] & 0x3ff);
276
dst[3] = 0x80 | (ch & 0x3f);
277
ch >>= 6;
278
dst[2] = 0x80 | (ch & 0x3f);
279
ch >>= 6;
280
dst[1] = 0x80 | (ch & 0x3f);
281
ch >>= 6;
282
dst[0] = 0xf0 | ch;
283
dst += 4;
284
src++;
285
srclen--;
286
continue;
287
}
288
if (ch >= 0xd800 && ch <= 0xdfff) ch = 0xfffd; /* invalid surrogate pair */
289
290
/* 0x800-0xffff: 3 bytes */
291
dst[2] = 0x80 | (ch & 0x3f);
292
ch >>= 6;
293
dst[1] = 0x80 | (ch & 0x3f);
294
ch >>= 6;
295
dst[0] = 0xe0 | ch;
296
dst += 3;
297
}
298
*dst = 0;
299
*dstlen = dst - ret;
300
return ret;
301
}
302
303
#ifdef _WIN32
304
305
int is_valid_codepage(int id)
306
{
307
return IsValidCodePage( id );
308
}
309
310
WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
311
{
312
WCHAR *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
313
DWORD ret = MultiByteToWideChar( codepage, MB_ERR_INVALID_CHARS, src, srclen, dst, srclen );
314
if (!ret) return NULL;
315
dst[ret] = 0;
316
*dstlen = ret;
317
return dst;
318
}
319
320
unsigned int get_language_from_name( const char *name )
321
{
322
WCHAR nameW[LOCALE_NAME_MAX_LENGTH];
323
324
MultiByteToWideChar( 1252, 0, name, -1, nameW, ARRAY_SIZE(nameW) );
325
return LocaleNameToLCID( nameW, LOCALE_ALLOW_NEUTRAL_NAMES );
326
}
327
328
#else /* _WIN32 */
329
330
struct nls_info
331
{
332
unsigned short codepage;
333
unsigned short unidef;
334
unsigned short trans_unidef;
335
unsigned short *cp2uni;
336
unsigned short *dbcs_offsets;
337
};
338
339
static struct nls_info nlsinfo[128];
340
341
static void init_nls_info( struct nls_info *info, unsigned short *ptr )
342
{
343
unsigned short hdr_size = ptr[0];
344
345
info->codepage = ptr[1];
346
info->unidef = ptr[4];
347
info->trans_unidef = ptr[6];
348
ptr += hdr_size;
349
info->cp2uni = ++ptr;
350
ptr += 256;
351
if (*ptr++) ptr += 256; /* glyph table */
352
info->dbcs_offsets = *ptr ? ptr + 1 : NULL;
353
}
354
355
static void *load_nls_file( const char *name )
356
{
357
unsigned int i;
358
void *data;
359
size_t size;
360
361
for (i = 0; nlsdirs[i]; i++)
362
{
363
char *path = strmake( "%s/%s", nlsdirs[i], name );
364
if ((data = read_file( path, &size )))
365
{
366
free( path );
367
return data;
368
}
369
free( path );
370
}
371
return NULL;
372
}
373
374
static const struct nls_info *get_nls_info( unsigned int codepage )
375
{
376
unsigned short *data;
377
unsigned int i;
378
379
for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++)
380
if (nlsinfo[i].codepage == codepage) return &nlsinfo[i];
381
382
assert( i < ARRAY_SIZE(nlsinfo) );
383
384
if ((data = load_nls_file( strmake( "c_%03u.nls", codepage ))))
385
{
386
init_nls_info( &nlsinfo[i], data );
387
return &nlsinfo[i];
388
}
389
return NULL;
390
}
391
392
int is_valid_codepage(int cp)
393
{
394
return cp == CP_UTF8 || get_nls_info( cp );
395
}
396
397
WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
398
{
399
const struct nls_info *info = get_nls_info( codepage );
400
unsigned int i;
401
WCHAR dbch, *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
402
403
if (!info) error( "codepage %u not supported\n", codepage );
404
405
if (info->dbcs_offsets)
406
{
407
for (i = 0; srclen; i++, srclen--, src++)
408
{
409
unsigned short off = info->dbcs_offsets[(unsigned char)*src];
410
if (off)
411
{
412
if (srclen == 1) return NULL;
413
dbch = (src[0] << 8) | (unsigned char)src[1];
414
src++;
415
srclen--;
416
dst[i] = info->dbcs_offsets[off + (unsigned char)*src];
417
if (dst[i] == info->unidef && dbch != info->trans_unidef) return NULL;
418
}
419
else
420
{
421
dst[i] = info->cp2uni[(unsigned char)*src];
422
if (dst[i] == info->unidef && *src != info->trans_unidef) return NULL;
423
}
424
}
425
}
426
else
427
{
428
for (i = 0; i < srclen; i++)
429
{
430
dst[i] = info->cp2uni[(unsigned char)src[i]];
431
if (dst[i] == info->unidef && src[i] != info->trans_unidef) return NULL;
432
}
433
}
434
dst[i] = 0;
435
*dstlen = i;
436
return dst;
437
}
438
439
static const NLS_LOCALE_LCID_INDEX *lcids_index;
440
static const NLS_LOCALE_HEADER *locale_table;
441
static const NLS_LOCALE_LCNAME_INDEX *lcnames_index;
442
static const WCHAR *locale_strings;
443
444
static void load_locale_nls(void)
445
{
446
struct
447
{
448
unsigned int ctypes;
449
unsigned int unknown1;
450
unsigned int unknown2;
451
unsigned int unknown3;
452
unsigned int locales;
453
unsigned int charmaps;
454
unsigned int geoids;
455
unsigned int scripts;
456
} *header;
457
458
if (!(header = load_nls_file( "locale.nls" ))) error( "unable to load locale.nls\n" );
459
locale_table = (const NLS_LOCALE_HEADER *)((char *)header + header->locales);
460
lcids_index = (const NLS_LOCALE_LCID_INDEX *)((char *)locale_table + locale_table->lcids_offset);
461
lcnames_index = (const NLS_LOCALE_LCNAME_INDEX *)((char *)locale_table + locale_table->lcnames_offset);
462
locale_strings = (const WCHAR *)((char *)locale_table + locale_table->strings_offset);
463
}
464
465
static int compare_locale_names( const char *n1, const WCHAR *n2 )
466
{
467
for (;;)
468
{
469
WCHAR ch1 = (unsigned char)*n1++;
470
WCHAR ch2 = *n2++;
471
if (ch1 >= 'a' && ch1 <= 'z') ch1 -= 'a' - 'A';
472
if (ch2 >= 'a' && ch2 <= 'z') ch2 -= 'a' - 'A';
473
if (!ch1 || ch1 != ch2) return ch1 - ch2;
474
}
475
}
476
477
static const NLS_LOCALE_LCNAME_INDEX *find_lcname_entry( const char *name )
478
{
479
int min = 0, max = locale_table->nb_lcnames - 1;
480
481
if (!name) return NULL;
482
while (min <= max)
483
{
484
int res, pos = (min + max) / 2;
485
const WCHAR *str = locale_strings + lcnames_index[pos].name;
486
res = compare_locale_names( name, str + 1 );
487
if (res < 0) max = pos - 1;
488
else if (res > 0) min = pos + 1;
489
else return &lcnames_index[pos];
490
}
491
return NULL;
492
}
493
494
static const NLS_LOCALE_DATA *get_locale_data( UINT idx )
495
{
496
ULONG offset = locale_table->locales_offset + idx * locale_table->locale_size;
497
return (const NLS_LOCALE_DATA *)((const char *)locale_table + offset);
498
}
499
500
unsigned int get_language_from_name( const char *name )
501
{
502
const NLS_LOCALE_LCNAME_INDEX *entry;
503
504
if (!locale_table) load_locale_nls();
505
if (!(entry = find_lcname_entry( name ))) return 0;
506
return get_locale_data( entry->idx )->unique_lcid;
507
}
508
509
#endif /* _WIN32 */
510
511
unsigned char *output_buffer;
512
size_t output_buffer_pos;
513
size_t output_buffer_size;
514
515