Release 8.21.
[wine.git] / tools / wmc / utils.c
blob6d8aa7572c320a61ae73dc40f33a53389470fbf8
1 /*
2 * Utility routines
4 * Copyright 1998,2000 Bertho A. Stultiens
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.
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.
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
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
30 #include "wmc.h"
31 #include "winternl.h"
32 #include "winnls.h"
33 #include "utils.h"
35 #define SUPPRESS_YACC_ERROR_MESSAGE
37 static void generic_msg(const char *s, const char *t, va_list ap)
39 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
40 vfprintf(stderr, s, ap);
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.
51 int mcy_error(const char *s, ...)
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;
62 int xyyerror(const char *s, ...)
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;
72 int mcy_warning(const char *s, ...)
74 va_list ap;
75 va_start(ap, s);
76 generic_msg(s, "Warning", ap);
77 va_end(ap);
78 return 0;
81 void internal_error(const char *file, int line, const char *s, ...)
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);
91 void fatal_perror( const char *msg, ... )
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);
102 void error(const char *s, ...)
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);
112 void warning(const char *s, ...)
114 va_list ap;
115 va_start(ap, s);
116 fprintf(stderr, "Warning: ");
117 vfprintf(stderr, s, ap);
118 va_end(ap);
121 int unistrlen(const WCHAR *s)
123 int n;
124 for(n = 0; *s; n++, s++)
126 return n;
129 WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
131 WCHAR *t = dst;
132 while(*src)
133 *t++ = *src++;
134 *t = 0;
135 return dst;
138 WCHAR *xunistrdup(const WCHAR * str)
140 WCHAR *s;
142 assert(str != NULL);
143 s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
144 return unistrcpy(s, str);
147 int unistricmp(const WCHAR *s1, const WCHAR *s2)
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)
155 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
157 if(!once)
159 once++;
160 mcy_warning(warn);
162 i = *s1++ - *s2++;
164 else
165 i = toupper(*s1++) - toupper(*s2++);
166 if(i)
167 return i;
170 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
172 if(!once)
173 mcy_warning(warn);
174 return *s1 - *s2;
176 else
177 return toupper(*s1) - toupper(*s2);
180 int unistrcmp(const WCHAR *s1, const WCHAR *s2)
182 int i;
183 while(*s1 && *s2)
185 i = *s1++ - *s2++;
186 if(i)
187 return i;
190 return *s1 - *s2;
193 WCHAR *utf8_to_unicode( const char *src, int srclen, int *dstlen )
195 static const char utf8_length[128] =
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 */
206 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
208 const char *srcend = src + srclen;
209 int len, res;
210 WCHAR *ret, *dst;
212 dst = ret = xmalloc( (srclen + 1) * sizeof(WCHAR) );
213 while (src < srcend)
215 unsigned char ch = *src++;
216 if (ch < 0x80) /* special fast case for 7-bit ASCII */
218 *dst++ = ch;
219 continue;
221 len = utf8_length[ch - 0x80];
222 if (len && src + len <= srcend)
224 res = ch & utf8_mask[len];
225 switch (len)
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
247 res -= 0x10000;
248 *dst++ = 0xd800 | (res >> 10);
249 *dst++ = 0xdc00 | (res & 0x3ff);
251 continue;
254 *dst++ = 0xfffd;
256 *dst = 0;
257 *dstlen = dst - ret;
258 return ret;
261 char *unicode_to_utf8( const WCHAR *src, int srclen, int *dstlen )
263 char *ret, *dst;
265 dst = ret = xmalloc( srclen * 3 + 1 );
266 for ( ; srclen; srclen--, src++)
268 unsigned int ch = *src;
270 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
272 *dst++ = ch;
273 continue;
275 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
277 dst[1] = 0x80 | (ch & 0x3f);
278 ch >>= 6;
279 dst[0] = 0xc0 | ch;
280 dst += 2;
281 continue;
283 if (ch >= 0xd800 && ch <= 0xdbff && srclen > 1 && src[1] >= 0xdc00 && src[1] <= 0xdfff)
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;
299 if (ch >= 0xd800 && ch <= 0xdfff) ch = 0xfffd; /* invalid surrogate pair */
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;
309 *dst = 0;
310 *dstlen = dst - ret;
311 return ret;
314 #ifdef _WIN32
316 int is_valid_codepage(int id)
318 return IsValidCodePage( id );
321 WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
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;
331 unsigned int get_language_from_name( const char *name )
333 WCHAR nameW[LOCALE_NAME_MAX_LENGTH];
335 MultiByteToWideChar( 1252, 0, name, -1, nameW, ARRAY_SIZE(nameW) );
336 return LocaleNameToLCID( nameW, LOCALE_ALLOW_NEUTRAL_NAMES );
339 #else /* _WIN32 */
341 struct nls_info
343 unsigned short codepage;
344 unsigned short unidef;
345 unsigned short trans_unidef;
346 unsigned short *cp2uni;
347 unsigned short *dbcs_offsets;
350 static struct nls_info nlsinfo[128];
352 static void init_nls_info( struct nls_info *info, unsigned short *ptr )
354 unsigned short hdr_size = ptr[0];
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;
366 static void *load_nls_file( const char *name )
368 unsigned int i;
369 void *data;
370 size_t size;
372 for (i = 0; nlsdirs[i]; i++)
374 char *path = strmake( "%s/%s", nlsdirs[i], name );
375 if ((data = read_file( path, &size )))
377 free( path );
378 return data;
380 free( path );
382 return NULL;
385 static const struct nls_info *get_nls_info( unsigned int codepage )
387 unsigned short *data;
388 unsigned int i;
390 for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++)
391 if (nlsinfo[i].codepage == codepage) return &nlsinfo[i];
393 assert( i < ARRAY_SIZE(nlsinfo) );
395 if ((data = load_nls_file( strmake( "c_%03u.nls", codepage ))))
397 init_nls_info( &nlsinfo[i], data );
398 return &nlsinfo[i];
400 return NULL;
403 int is_valid_codepage(int cp)
405 return cp == CP_UTF8 || get_nls_info( cp );
408 WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
410 const struct nls_info *info = get_nls_info( codepage );
411 unsigned int i;
412 WCHAR dbch, *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
414 if (!info) error( "codepage %u not supported\n", codepage );
416 if (info->dbcs_offsets)
418 for (i = 0; srclen; i++, srclen--, src++)
420 unsigned short off = info->dbcs_offsets[(unsigned char)*src];
421 if (off)
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;
430 else
432 dst[i] = info->cp2uni[(unsigned char)*src];
433 if (dst[i] == info->unidef && *src != info->trans_unidef) return NULL;
437 else
439 for (i = 0; i < srclen; i++)
441 dst[i] = info->cp2uni[(unsigned char)src[i]];
442 if (dst[i] == info->unidef && src[i] != info->trans_unidef) return NULL;
445 dst[i] = 0;
446 *dstlen = i;
447 return dst;
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;
455 static void load_locale_nls(void)
457 struct
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;
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);
476 static int compare_locale_names( const char *n1, const WCHAR *n2 )
478 for (;;)
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;
488 static const NLS_LOCALE_LCNAME_INDEX *find_lcname_entry( const char *name )
490 int min = 0, max = locale_table->nb_lcnames - 1;
492 if (!name) return NULL;
493 while (min <= max)
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];
502 return NULL;
505 static const NLS_LOCALE_DATA *get_locale_data( UINT idx )
507 ULONG offset = locale_table->locales_offset + idx * locale_table->locale_size;
508 return (const NLS_LOCALE_DATA *)((const char *)locale_table + offset);
511 unsigned int get_language_from_name( const char *name )
513 const NLS_LOCALE_LCNAME_INDEX *entry;
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;
520 #endif /* _WIN32 */
522 unsigned char *output_buffer;
523 size_t output_buffer_pos;
524 size_t output_buffer_size;