Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/wmc/utils.c
4389 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 fatal_perror( const char *msg, ... )
92
{
93
va_list valist;
94
va_start( valist, msg );
95
fprintf(stderr, "Error: ");
96
vfprintf( stderr, msg, valist );
97
perror( " " );
98
va_end( valist );
99
exit(2);
100
}
101
102
void error(const char *s, ...)
103
{
104
va_list ap;
105
va_start(ap, s);
106
fprintf(stderr, "Error: ");
107
vfprintf(stderr, s, ap);
108
va_end(ap);
109
exit(2);
110
}
111
112
void warning(const char *s, ...)
113
{
114
va_list ap;
115
va_start(ap, s);
116
fprintf(stderr, "Warning: ");
117
vfprintf(stderr, s, ap);
118
va_end(ap);
119
}
120
121
int unistrlen(const WCHAR *s)
122
{
123
int n;
124
for(n = 0; *s; n++, s++)
125
;
126
return n;
127
}
128
129
WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
130
{
131
WCHAR *t = dst;
132
while(*src)
133
*t++ = *src++;
134
*t = 0;
135
return dst;
136
}
137
138
WCHAR *xunistrdup(const WCHAR * str)
139
{
140
WCHAR *s;
141
142
assert(str != NULL);
143
s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
144
return unistrcpy(s, str);
145
}
146
147
int unistricmp(const WCHAR *s1, const WCHAR *s2)
148
{
149
int i;
150
int once = 0;
151
static const char warn[] = "Don't know the uppercase equivalent of non ascii characters;"
152
"comparison might yield wrong results";
153
while(*s1 && *s2)
154
{
155
if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
156
{
157
if(!once)
158
{
159
once++;
160
mcy_warning(warn);
161
}
162
i = *s1++ - *s2++;
163
}
164
else
165
i = toupper(*s1++) - toupper(*s2++);
166
if(i)
167
return i;
168
}
169
170
if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
171
{
172
if(!once)
173
mcy_warning(warn);
174
return *s1 - *s2;
175
}
176
else
177
return toupper(*s1) - toupper(*s2);
178
}
179
180
int unistrcmp(const WCHAR *s1, const WCHAR *s2)
181
{
182
int i;
183
while(*s1 && *s2)
184
{
185
i = *s1++ - *s2++;
186
if(i)
187
return i;
188
}
189
190
return *s1 - *s2;
191
}
192
193
WCHAR *utf8_to_unicode( const char *src, int srclen, int *dstlen )
194
{
195
static const char utf8_length[128] =
196
{
197
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
198
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
199
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
200
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
201
0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
202
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
203
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
204
3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
205
};
206
static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
207
208
const char *srcend = src + srclen;
209
int len, res;
210
WCHAR *ret, *dst;
211
212
dst = ret = xmalloc( (srclen + 1) * sizeof(WCHAR) );
213
while (src < srcend)
214
{
215
unsigned char ch = *src++;
216
if (ch < 0x80) /* special fast case for 7-bit ASCII */
217
{
218
*dst++ = ch;
219
continue;
220
}
221
len = utf8_length[ch - 0x80];
222
if (len && src + len <= srcend)
223
{
224
res = ch & utf8_mask[len];
225
switch (len)
226
{
227
case 3:
228
if ((ch = *src ^ 0x80) >= 0x40) break;
229
res = (res << 6) | ch;
230
src++;
231
if (res < 0x10) break;
232
case 2:
233
if ((ch = *src ^ 0x80) >= 0x40) break;
234
res = (res << 6) | ch;
235
if (res >= 0x110000 >> 6) break;
236
src++;
237
if (res < 0x20) break;
238
if (res >= 0xd800 >> 6 && res <= 0xdfff >> 6) break;
239
case 1:
240
if ((ch = *src ^ 0x80) >= 0x40) break;
241
res = (res << 6) | ch;
242
src++;
243
if (res < 0x80) break;
244
if (res <= 0xffff) *dst++ = res;
245
else
246
{
247
res -= 0x10000;
248
*dst++ = 0xd800 | (res >> 10);
249
*dst++ = 0xdc00 | (res & 0x3ff);
250
}
251
continue;
252
}
253
}
254
*dst++ = 0xfffd;
255
}
256
*dst = 0;
257
*dstlen = dst - ret;
258
return ret;
259
}
260
261
char *unicode_to_utf8( const WCHAR *src, int srclen, int *dstlen )
262
{
263
char *ret, *dst;
264
265
dst = ret = xmalloc( srclen * 3 + 1 );
266
for ( ; srclen; srclen--, src++)
267
{
268
unsigned int ch = *src;
269
270
if (ch < 0x80) /* 0x00-0x7f: 1 byte */
271
{
272
*dst++ = ch;
273
continue;
274
}
275
if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
276
{
277
dst[1] = 0x80 | (ch & 0x3f);
278
ch >>= 6;
279
dst[0] = 0xc0 | ch;
280
dst += 2;
281
continue;
282
}
283
if (ch >= 0xd800 && ch <= 0xdbff && srclen > 1 && src[1] >= 0xdc00 && src[1] <= 0xdfff)
284
{
285
/* 0x10000-0x10ffff: 4 bytes */
286
ch = 0x10000 + ((ch & 0x3ff) << 10) + (src[1] & 0x3ff);
287
dst[3] = 0x80 | (ch & 0x3f);
288
ch >>= 6;
289
dst[2] = 0x80 | (ch & 0x3f);
290
ch >>= 6;
291
dst[1] = 0x80 | (ch & 0x3f);
292
ch >>= 6;
293
dst[0] = 0xf0 | ch;
294
dst += 4;
295
src++;
296
srclen--;
297
continue;
298
}
299
if (ch >= 0xd800 && ch <= 0xdfff) ch = 0xfffd; /* invalid surrogate pair */
300
301
/* 0x800-0xffff: 3 bytes */
302
dst[2] = 0x80 | (ch & 0x3f);
303
ch >>= 6;
304
dst[1] = 0x80 | (ch & 0x3f);
305
ch >>= 6;
306
dst[0] = 0xe0 | ch;
307
dst += 3;
308
}
309
*dst = 0;
310
*dstlen = dst - ret;
311
return ret;
312
}
313
314
#ifdef _WIN32
315
316
int is_valid_codepage(int id)
317
{
318
return IsValidCodePage( id );
319
}
320
321
WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
322
{
323
WCHAR *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
324
DWORD ret = MultiByteToWideChar( codepage, MB_ERR_INVALID_CHARS, src, srclen, dst, srclen );
325
if (!ret) return NULL;
326
dst[ret] = 0;
327
*dstlen = ret;
328
return dst;
329
}
330
331
unsigned int get_language_from_name( const char *name )
332
{
333
WCHAR nameW[LOCALE_NAME_MAX_LENGTH];
334
335
MultiByteToWideChar( 1252, 0, name, -1, nameW, ARRAY_SIZE(nameW) );
336
return LocaleNameToLCID( nameW, LOCALE_ALLOW_NEUTRAL_NAMES );
337
}
338
339
#else /* _WIN32 */
340
341
struct nls_info
342
{
343
unsigned short codepage;
344
unsigned short unidef;
345
unsigned short trans_unidef;
346
unsigned short *cp2uni;
347
unsigned short *dbcs_offsets;
348
};
349
350
static struct nls_info nlsinfo[128];
351
352
static void init_nls_info( struct nls_info *info, unsigned short *ptr )
353
{
354
unsigned short hdr_size = ptr[0];
355
356
info->codepage = ptr[1];
357
info->unidef = ptr[4];
358
info->trans_unidef = ptr[6];
359
ptr += hdr_size;
360
info->cp2uni = ++ptr;
361
ptr += 256;
362
if (*ptr++) ptr += 256; /* glyph table */
363
info->dbcs_offsets = *ptr ? ptr + 1 : NULL;
364
}
365
366
static void *load_nls_file( const char *name )
367
{
368
unsigned int i;
369
void *data;
370
size_t size;
371
372
for (i = 0; nlsdirs[i]; i++)
373
{
374
char *path = strmake( "%s/%s", nlsdirs[i], name );
375
if ((data = read_file( path, &size )))
376
{
377
free( path );
378
return data;
379
}
380
free( path );
381
}
382
return NULL;
383
}
384
385
static const struct nls_info *get_nls_info( unsigned int codepage )
386
{
387
unsigned short *data;
388
unsigned int i;
389
390
for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++)
391
if (nlsinfo[i].codepage == codepage) return &nlsinfo[i];
392
393
assert( i < ARRAY_SIZE(nlsinfo) );
394
395
if ((data = load_nls_file( strmake( "c_%03u.nls", codepage ))))
396
{
397
init_nls_info( &nlsinfo[i], data );
398
return &nlsinfo[i];
399
}
400
return NULL;
401
}
402
403
int is_valid_codepage(int cp)
404
{
405
return cp == CP_UTF8 || get_nls_info( cp );
406
}
407
408
WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
409
{
410
const struct nls_info *info = get_nls_info( codepage );
411
unsigned int i;
412
WCHAR dbch, *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
413
414
if (!info) error( "codepage %u not supported\n", codepage );
415
416
if (info->dbcs_offsets)
417
{
418
for (i = 0; srclen; i++, srclen--, src++)
419
{
420
unsigned short off = info->dbcs_offsets[(unsigned char)*src];
421
if (off)
422
{
423
if (srclen == 1) return NULL;
424
dbch = (src[0] << 8) | (unsigned char)src[1];
425
src++;
426
srclen--;
427
dst[i] = info->dbcs_offsets[off + (unsigned char)*src];
428
if (dst[i] == info->unidef && dbch != info->trans_unidef) return NULL;
429
}
430
else
431
{
432
dst[i] = info->cp2uni[(unsigned char)*src];
433
if (dst[i] == info->unidef && *src != info->trans_unidef) return NULL;
434
}
435
}
436
}
437
else
438
{
439
for (i = 0; i < srclen; i++)
440
{
441
dst[i] = info->cp2uni[(unsigned char)src[i]];
442
if (dst[i] == info->unidef && src[i] != info->trans_unidef) return NULL;
443
}
444
}
445
dst[i] = 0;
446
*dstlen = i;
447
return dst;
448
}
449
450
static const NLS_LOCALE_LCID_INDEX *lcids_index;
451
static const NLS_LOCALE_HEADER *locale_table;
452
static const NLS_LOCALE_LCNAME_INDEX *lcnames_index;
453
static const WCHAR *locale_strings;
454
455
static void load_locale_nls(void)
456
{
457
struct
458
{
459
unsigned int ctypes;
460
unsigned int unknown1;
461
unsigned int unknown2;
462
unsigned int unknown3;
463
unsigned int locales;
464
unsigned int charmaps;
465
unsigned int geoids;
466
unsigned int scripts;
467
} *header;
468
469
if (!(header = load_nls_file( "locale.nls" ))) error( "unable to load locale.nls\n" );
470
locale_table = (const NLS_LOCALE_HEADER *)((char *)header + header->locales);
471
lcids_index = (const NLS_LOCALE_LCID_INDEX *)((char *)locale_table + locale_table->lcids_offset);
472
lcnames_index = (const NLS_LOCALE_LCNAME_INDEX *)((char *)locale_table + locale_table->lcnames_offset);
473
locale_strings = (const WCHAR *)((char *)locale_table + locale_table->strings_offset);
474
}
475
476
static int compare_locale_names( const char *n1, const WCHAR *n2 )
477
{
478
for (;;)
479
{
480
WCHAR ch1 = (unsigned char)*n1++;
481
WCHAR ch2 = *n2++;
482
if (ch1 >= 'a' && ch1 <= 'z') ch1 -= 'a' - 'A';
483
if (ch2 >= 'a' && ch2 <= 'z') ch2 -= 'a' - 'A';
484
if (!ch1 || ch1 != ch2) return ch1 - ch2;
485
}
486
}
487
488
static const NLS_LOCALE_LCNAME_INDEX *find_lcname_entry( const char *name )
489
{
490
int min = 0, max = locale_table->nb_lcnames - 1;
491
492
if (!name) return NULL;
493
while (min <= max)
494
{
495
int res, pos = (min + max) / 2;
496
const WCHAR *str = locale_strings + lcnames_index[pos].name;
497
res = compare_locale_names( name, str + 1 );
498
if (res < 0) max = pos - 1;
499
else if (res > 0) min = pos + 1;
500
else return &lcnames_index[pos];
501
}
502
return NULL;
503
}
504
505
static const NLS_LOCALE_DATA *get_locale_data( UINT idx )
506
{
507
ULONG offset = locale_table->locales_offset + idx * locale_table->locale_size;
508
return (const NLS_LOCALE_DATA *)((const char *)locale_table + offset);
509
}
510
511
unsigned int get_language_from_name( const char *name )
512
{
513
const NLS_LOCALE_LCNAME_INDEX *entry;
514
515
if (!locale_table) load_locale_nls();
516
if (!(entry = find_lcname_entry( name ))) return 0;
517
return get_locale_data( entry->idx )->unique_lcid;
518
}
519
520
#endif /* _WIN32 */
521
522
unsigned char *output_buffer;
523
size_t output_buffer_pos;
524
size_t output_buffer_size;
525
526