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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
|
/* windmc.c -- a program to compile Windows message files.
Copyright (C) 2007-2020 Free Software Foundation, Inc.
Written by Kai Tietz, Onevision.
This file is part of GNU Binutils.
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.
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., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
/* This program can read and comile Windows message format.
It is based on information taken from the following sources:
* Microsoft documentation.
* The wmc program, written by Bertho A. Stultiens (BS). */
#include "sysdep.h"
#include <assert.h>
#include <time.h>
#include "bfd.h"
#include "getopt.h"
#include "bucomm.h"
#include "libiberty.h"
#include "safe-ctype.h"
#include "obstack.h"
#include "windmc.h"
#include "windint.h"
/* Defines a message compiler element item with length and offset
information. */
typedef struct mc_msg_item
{
rc_uint_type res_len;
rc_uint_type res_off;
struct bin_messagetable_item *res;
} mc_msg_item;
int target_is_bigendian = 0;
const char *def_target_arch;
/* Globals and static variable definitions. */
/* bfd global helper struct variable. */
static struct
{
bfd *abfd;
asection *sec;
} mc_bfd;
/* Memory list. */
mc_node *mc_nodes = NULL;
static mc_node_lang **mc_nodes_lang = NULL;
static int mc_nodes_lang_count = 0;
static mc_keyword **mc_severity_codes = NULL;
static int mc_severity_codes_count = 0;
static mc_keyword **mc_facility_codes = NULL;
static int mc_facility_codes_count = 0;
/* When we are building a resource tree, we allocate everything onto
an obstack, so that we can free it all at once if we want. */
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
/* The resource building obstack. */
static struct obstack res_obstack;
/* Flag variables. */
/* Set by -C. Set the default code page to be used for input text file. */
static rc_uint_type mcset_codepage_in = 0;
/* Set by -O. Set the default code page to be used for output text files. */
static rc_uint_type mcset_codepage_out = 0;
/* Set by -b. .BIN filename should have .mc filename_ included for uniqueness. */
static int mcset_prefix_bin = 0;
/* The base name of the .mc file. */
static const char *mcset_mc_basename = "unknown";
/* Set by -e <ext>. Specify the extension for the header file. */
static const char *mcset_header_ext = ".h";
/* Set by -h <path>. Gives the path of where to create the C include file. */
static const char *mcset_header_dir = "./";
/* Set by -r <path>. Gives the path of where to create the RC include file
and the binary message resource files it includes. */
static const char *mcset_rc_dir = "./";
/* Modified by -a & -u. By -u input file is unicode, by -a is ASCII (default). */
static int mcset_text_in_is_unicode = 0;
/* Modified by -A & -U. By -U bin file is unicode (default), by -A is ASCII. */
static int mcset_bin_out_is_unicode = 1;
/* Set by -c. Sets the Customer bit in all the message ID's. */
int mcset_custom_bit = 0;
/* Set by -o. Generate OLE2 header file. Use HRESULT definition instead of
status code definition. */
static int mcset_use_hresult = 0;
/* Set by -m <msglen>. Generate a warning if the size of any message exceeds
maxmsglen characters. */
rc_uint_type mcset_max_message_length = 0;
/* Set by -d. Sets message values in header to decimal initially. */
int mcset_out_values_are_decimal = 0;
/* Set by -n. terminates all strings with null's in the message tables. */
static int mcset_automatic_null_termination = 0;
/* The type used for message id output in header. */
unichar *mcset_msg_id_typedef = NULL;
/* Set by -x path. Geberated debug C file for mapping ID's to text. */
static const char *mcset_dbg_dir = NULL;
/* getopt long name definitions. */
static const struct option long_options[] =
{
{"binprefix", no_argument, 0, 'b'},
{"target", required_argument, 0, 'F'},
{"extension", required_argument, 0, 'e'},
{"headerdir", required_argument, 0, 'h'},
{"rcdir", required_argument, 0, 'r'},
{"verbose", no_argument, 0, 'v'},
{"codepage_in", required_argument, 0, 'C'},
{"codepage_out", required_argument, 0, 'O'},
{"maxlength", required_argument, 0, 'm'},
{"ascii_in", no_argument, 0, 'a'},
{"ascii_out", no_argument, 0, 'A'},
{"unicode_in", no_argument, 0, 'u'},
{"unicode_out", no_argument, 0, 'U'},
{"customflag", no_argument, 0, 'c'},
{"decimal_values", no_argument, 0, 'd'},
{"hresult_use", no_argument, 0, 'o'},
{"nullterminate", no_argument, 0, 'n'},
{"xdbg", required_argument, 0, 'x'},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'H'},
{0, no_argument, 0, 0}
};
/* Initialize the resource building obstack. */
static void
res_init (void)
{
obstack_init (&res_obstack);
}
/* Allocate space on the resource building obstack. */
void *
res_alloc (rc_uint_type bytes)
{
return obstack_alloc (&res_obstack, (size_t) bytes);
}
static FILE *
mc_create_path_text_file (const char *path, const char *ext)
{
FILE *ret;
size_t len = 1;
char *hsz;
len += (path != NULL ? strlen (path) : 0);
len += strlen (mcset_mc_basename);
len += (ext != NULL ? strlen (ext) : 0);
hsz = xmalloc (len);
sprintf (hsz, "%s%s%s", (path != NULL ? path : ""), mcset_mc_basename,
(ext != NULL ? ext : ""));
if ((ret = fopen (hsz, "wb")) == NULL)
fatal (_("can't create %s file `%s' for output.\n"), (ext ? ext : "text"), hsz);
free (hsz);
return ret;
}
static void
usage (FILE *stream, int status)
{
fprintf (stream, _("Usage: %s [option(s)] [input-file]\n"),
program_name);
fprintf (stream, _(" The options are:\n\
-a --ascii_in Read input file as ASCII file\n\
-A --ascii_out Write binary messages as ASCII\n\
-b --binprefix .bin filename is prefixed by .mc filename_ for uniqueness.\n\
-c --customflag Set custom flags for messages\n\
-C --codepage_in=<val> Set codepage when reading mc text file\n\
-d --decimal_values Print values to text files decimal\n\
-e --extension=<extension> Set header extension used on export header file\n\
-F --target <target> Specify output target for endianness.\n\
-h --headerdir=<directory> Set the export directory for headers\n\
-u --unicode_in Read input file as UTF16 file\n\
-U --unicode_out Write binary messages as UFT16\n\
-m --maxlength=<val> Set the maximal allowed message length\n\
-n --nullterminate Automatic add a zero termination to strings\n\
-o --hresult_use Use HRESULT definition instead of status code definition\n\
-O --codepage_out=<val> Set codepage used for writing text file\n\
-r --rcdir=<directory> Set the export directory for rc files\n\
-x --xdbg=<directory> Where to create the .dbg C include file\n\
that maps message ID's to their symbolic name.\n\
"));
fprintf (stream, _("\
-H --help Print this help message\n\
-v --verbose Verbose - tells you what it's doing\n\
-V --version Print version information\n"));
list_supported_targets (program_name, stream);
if (REPORT_BUGS_TO[0] && status == 0)
fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
exit (status);
}
static void
set_endianness (bfd *abfd, const char *target)
{
const bfd_target *target_vec;
def_target_arch = NULL;
target_vec = bfd_get_target_info (target, abfd, &target_is_bigendian, NULL,
&def_target_arch);
if (! target_vec)
fatal ("Can't detect target endianness and architecture.");
if (! def_target_arch)
fatal ("Can't detect architecture.");
}
static int
probe_codepage (rc_uint_type *cp, int *is_uni, const char *pswitch, int defmode)
{
if (*is_uni == -1)
{
if (*cp != CP_UTF16)
*is_uni = defmode;
else
*is_uni = 1;
}
if (*is_uni)
{
if (*cp != 0 && *cp != CP_UTF16)
{
fprintf (stderr, _("%s: warning: "), program_name);
fprintf (stderr, _("A codepage was specified switch `%s' and UTF16.\n"), pswitch);
fprintf (stderr, _("\tcodepage settings are ignored.\n"));
}
*cp = CP_UTF16;
return 1;
}
if (*cp == CP_UTF16)
{
*is_uni = 1;
return 1;
}
if (*cp == 0)
*cp = 1252;
if (! unicode_is_valid_codepage (*cp))
fatal ("Code page 0x%x is unknown.", (unsigned int) *cp);
*is_uni = 0;
return 1;
}
mc_node *
mc_add_node (void)
{
mc_node *ret;
ret = res_alloc (sizeof (mc_node));
memset (ret, 0, sizeof (mc_node));
if (! mc_nodes)
mc_nodes = ret;
else
{
mc_node *h = mc_nodes;
while (h->next != NULL)
h = h->next;
h->next = ret;
}
return ret;
}
mc_node_lang *
mc_add_node_lang (mc_node *root, const mc_keyword *lang, rc_uint_type vid)
{
mc_node_lang *ret, *h, *p;
if (! lang || ! root)
fatal (_("try to add a ill language."));
ret = res_alloc (sizeof (mc_node_lang));
memset (ret, 0, sizeof (mc_node_lang));
ret->lang = lang;
ret->vid = vid;
if ((h = root->sub) == NULL)
root->sub = ret;
else
{
p = NULL;
while (h != NULL)
{
if (h->lang->nval > lang->nval)
break;
if (h->lang->nval == lang->nval)
{
if (h->vid > vid)
break;
if (h->vid == vid)
fatal ("double defined message id %ld.\n", (long) vid);
}
h = (p = h)->next;
}
ret->next = h;
if (! p)
root->sub = ret;
else
p->next = ret;
}
return ret;
}
static char *
convert_unicode_to_ACP (const unichar *usz)
{
char *s;
rc_uint_type l;
if (! usz)
return NULL;
codepage_from_unicode (&l, usz, &s, mcset_codepage_out);
if (! s)
fatal ("unicode string not mappable to ASCII codepage 0x%lx.\n",
(unsigned long) mcset_codepage_out);
return s;
}
static void
write_dbg_define (FILE *fp, const unichar *sym_name, const unichar *typecast)
{
char *sym;
if (!sym_name || sym_name[0] == 0)
return;
sym = convert_unicode_to_ACP (sym_name);
fprintf (fp, " {(");
if (typecast)
unicode_print (fp, typecast, unichar_len (typecast));
else
fprintf (fp, "DWORD");
fprintf (fp, ") %s, \"%s\" },\n", sym, sym);
}
static void
write_header_define (FILE *fp, const unichar *sym_name, rc_uint_type vid, const unichar *typecast, mc_node_lang *nl)
{
char *sym;
char *tdef = NULL;
if (!sym_name || sym_name[0] == 0)
{
if (nl != NULL)
{
if (mcset_out_values_are_decimal)
fprintf (fp, "//\n// MessageId: 0x%lu\n//\n", (unsigned long) vid);
else
fprintf (fp, "//\n// MessageId: 0x%lx\n//\n", (unsigned long) vid);
}
return;
}
sym = convert_unicode_to_ACP (sym_name);
if (typecast && typecast[0] != 0)
tdef = convert_unicode_to_ACP (typecast);
fprintf (fp, "//\n// MessageId: %s\n//\n", sym);
if (! mcset_out_values_are_decimal)
fprintf (fp, "#define %s %s%s%s 0x%lx\n\n", sym,
(tdef ? "(" : ""), (tdef ? tdef : ""), (tdef ? ")" : ""),
(unsigned long) vid);
else
fprintf (fp, "#define %s %s%s%s 0x%lu\n\n", sym,
(tdef ? "(" : ""), (tdef ? tdef : ""), (tdef ? ")" : ""),
(unsigned long) vid);
}
static int
sort_mc_node_lang (const void *l, const void *r)
{
const mc_node_lang *l1 = *((const mc_node_lang **)l);
const mc_node_lang *r1 = *((const mc_node_lang **)r);
if (l == r)
return 0;
if (l1->lang != r1->lang)
{
if (l1->lang->nval < r1->lang->nval)
return -1;
return 1;
}
if (l1->vid == r1->vid)
return 0;
if (l1->vid < r1->vid)
return -1;
return 1;
}
static int
sort_keyword_by_nval (const void *l, const void *r)
{
const mc_keyword *l1 = *((const mc_keyword **)l);
const mc_keyword *r1 = *((const mc_keyword **)r);
rc_uint_type len1, len2;
int e;
if (l == r)
return 0;
if (l1->nval != r1->nval)
{
if (l1->nval < r1->nval)
return -1;
return 1;
}
len1 = unichar_len (l1->usz);
len2 = unichar_len (r1->usz);
if (len1 <= len2)
e = memcmp (l1->usz, r1->usz, sizeof (unichar) * len1);
else
e = memcmp (l1->usz, r1->usz, sizeof (unichar) * len2);
if (e)
return e;
if (len1 < len2)
return -1;
else if (len1 > len2)
return 1;
return 0;
}
static void
do_sorts (void)
{
mc_node *h;
mc_node_lang *n;
const mc_keyword *k;
int i;
/* Sort message by their language and id ascending. */
mc_nodes_lang_count = 0;
h = mc_nodes;
while (h != NULL)
{
n = h->sub;
while (n != NULL)
{
mc_nodes_lang_count +=1;
n = n->next;
}
h = h->next;
}
if (mc_nodes_lang_count != 0)
{
h = mc_nodes;
i = 0;
mc_nodes_lang = xmalloc (sizeof (mc_node_lang *) * mc_nodes_lang_count);
while (h != NULL)
{
n = h->sub;
while (n != NULL)
{
mc_nodes_lang[i++] = n;
n = n->next;
}
h = h->next;
}
qsort (mc_nodes_lang, (size_t) mc_nodes_lang_count, sizeof (mc_node_lang *), sort_mc_node_lang);
}
/* Sort facility code definitions by there id ascending. */
i = 0;
while ((k = enum_facility (i)) != NULL)
++i;
mc_facility_codes_count = i;
if (i != 0)
{
mc_facility_codes = xmalloc (sizeof (mc_keyword *) * i);
i = 0;
while ((k = enum_facility (i)) != NULL)
mc_facility_codes[i++] = (mc_keyword *) k;
qsort (mc_facility_codes, (size_t) mc_facility_codes_count, sizeof (mc_keyword *), sort_keyword_by_nval);
}
/* Sort severity code definitions by there id ascending. */
i = 0;
while ((k = enum_severity (i)) != NULL)
++i;
mc_severity_codes_count = i;
if (i != 0)
{
mc_severity_codes = xmalloc (sizeof (mc_keyword *) * i);
i = 0;
while ((k = enum_severity (i)) != NULL)
mc_severity_codes[i++] = (mc_keyword *) k;
qsort (mc_severity_codes, (size_t) mc_severity_codes_count, sizeof (mc_keyword *), sort_keyword_by_nval);
}
}
static int
mc_get_block_count (mc_node_lang **nl, int elems)
{
rc_uint_type exid;
int i, ret;
if (! nl)
return 0;
i = 0;
ret = 0;
while (i < elems)
{
ret++;
exid = nl[i++]->vid;
while (i < elems && nl[i]->vid == exid + 1)
exid = nl[i++]->vid;
}
return ret;
}
static bfd *
windmc_open_as_binary (const char *filename)
{
bfd *abfd;
abfd = bfd_openw (filename, "binary");
if (! abfd)
fatal ("can't open `%s' for output", filename);
return abfd;
}
static void
target_put_16 (void *p, rc_uint_type value)
{
assert (!! p);
if (target_is_bigendian)
bfd_putb16 (value, p);
else
bfd_putl16 (value, p);
}
static void
target_put_32 (void *p, rc_uint_type value)
{
assert (!! p);
if (target_is_bigendian)
bfd_putb32 (value, p);
else
bfd_putl32 (value, p);
}
static struct bin_messagetable_item *
mc_generate_bin_item (mc_node_lang *n, rc_uint_type *res_len)
{
struct bin_messagetable_item *ret = NULL;
rc_uint_type len;
*res_len = 0;
if (mcset_bin_out_is_unicode == 1)
{
unichar *ht = n->message;
rc_uint_type txt_len;
txt_len = unichar_len (n->message);
if (mcset_automatic_null_termination && txt_len != 0)
{
while (txt_len > 0 && ht[txt_len - 1] > 0 && ht[txt_len - 1] < 0x20)
ht[--txt_len] = 0;
}
txt_len *= sizeof (unichar);
len = BIN_MESSAGETABLE_ITEM_SIZE + txt_len + sizeof (unichar);
ret = res_alloc ((len + 3) & ~3);
memset (ret, 0, (len + 3) & ~3);
target_put_16 (ret->length, (len + 3) & ~3);
target_put_16 (ret->flags, MESSAGE_RESOURCE_UNICODE);
txt_len = 0;
while (*ht != 0)
{
target_put_16 (ret->data + txt_len, *ht++);
txt_len += 2;
}
}
else
{
rc_uint_type txt_len, l;
char *cvt_txt;
codepage_from_unicode( &l, n->message, &cvt_txt, n->lang->lang_info.wincp);
if (! cvt_txt)
fatal ("Failed to convert message to language codepage.\n");
txt_len = strlen (cvt_txt);
if (mcset_automatic_null_termination && txt_len > 0)
{
while (txt_len > 0 && cvt_txt[txt_len - 1] > 0 && cvt_txt[txt_len - 1] < 0x20)
cvt_txt[--txt_len] = 0;
}
len = BIN_MESSAGETABLE_ITEM_SIZE + txt_len + 1;
ret = res_alloc ((len + 3) & ~3);
memset (ret, 0, (len + 3) & ~3);
target_put_16 (ret->length, (len + 3) & ~3);
target_put_16 (ret->flags, 0);
strcpy ((char *) ret->data, cvt_txt);
}
*res_len = (len + 3) & ~3;
return ret;
}
static void
mc_write_blocks (struct bin_messagetable *mtbl, mc_node_lang **nl, mc_msg_item *ml, int elems)
{
int i, idx = 0;
rc_uint_type exid;
if (! nl)
return;
i = 0;
while (i < elems)
{
target_put_32 (mtbl->items[idx].lowid, nl[i]->vid);
target_put_32 (mtbl->items[idx].highid, nl[i]->vid);
target_put_32 (mtbl->items[idx].offset, ml[i].res_off);
exid = nl[i++]->vid;
while (i < elems && nl[i]->vid == exid + 1)
{
target_put_32 (mtbl->items[idx].highid, nl[i]->vid);
exid = nl[i++]->vid;
}
++idx;
}
}
static void
set_windmc_bfd_content (const void *data, rc_uint_type off, rc_uint_type length)
{
if (! bfd_set_section_contents (mc_bfd.abfd, mc_bfd.sec, data, off, length))
bfd_fatal ("bfd_set_section_contents");
}
static void
windmc_write_bin (const char *filename, mc_node_lang **nl, int elems)
{
unsigned long sec_length = 1;
int block_count, i;
mc_msg_item *mi;
struct bin_messagetable *mtbl;
rc_uint_type dta_off, dta_start;
if (elems <= 0)
return;
mc_bfd.abfd = windmc_open_as_binary (filename);
mc_bfd.sec = bfd_make_section_with_flags (mc_bfd.abfd, ".data",
(SEC_HAS_CONTENTS | SEC_ALLOC
| SEC_LOAD | SEC_DATA));
if (mc_bfd.sec == NULL)
bfd_fatal ("bfd_make_section");
/* Requiring this is probably a bug in BFD. */
mc_bfd.sec->output_section = mc_bfd.sec;
block_count = mc_get_block_count (nl, elems);
dta_off = (rc_uint_type) ((BIN_MESSAGETABLE_BLOCK_SIZE * block_count) + BIN_MESSAGETABLE_SIZE - 4);
dta_start = dta_off = (dta_off + 3) & ~3;
mi = xmalloc (sizeof (mc_msg_item) * elems);
mtbl = xmalloc (dta_start);
/* Clear header region. */
memset (mtbl, 0, dta_start);
target_put_32 (mtbl->cblocks, block_count);
/* Prepare items section for output. */
for (i = 0; i < elems; i++)
{
mi[i].res_off = dta_off;
mi[i].res = mc_generate_bin_item (nl[i], &mi[i].res_len);
dta_off += mi[i].res_len;
}
sec_length = (dta_off + 3) & ~3;
if (!bfd_set_section_size (mc_bfd.sec, sec_length))
bfd_fatal ("bfd_set_section_size");
/* Make sure we write the complete block. */
set_windmc_bfd_content ("\0", sec_length - 1, 1);
/* Write block information. */
mc_write_blocks (mtbl, nl, mi, elems);
set_windmc_bfd_content (mtbl, 0, dta_start);
/* Write items. */
for (i = 0; i < elems; i++)
set_windmc_bfd_content (mi[i].res, mi[i].res_off, mi[i].res_len);
free (mtbl);
free (mi);
bfd_close (mc_bfd.abfd);
mc_bfd.abfd = NULL;
mc_bfd.sec = NULL;
}
static void
write_bin (void)
{
mc_node_lang *n = NULL;
int i, c;
if (! mc_nodes_lang_count)
return;
i = 0;
while (i < mc_nodes_lang_count)
{
char *nd;
char *filename;
if (n && n->lang == mc_nodes_lang[i]->lang)
{
i++;
continue;
}
n = mc_nodes_lang[i];
c = i + 1;
while (c < mc_nodes_lang_count && n->lang == mc_nodes_lang[c]->lang)
c++;
nd = convert_unicode_to_ACP (n->lang->sval);
/* Prepare filename for binary output. */
filename = xmalloc (strlen (nd) + 4 + 1 + strlen (mcset_mc_basename) + 1 + strlen (mcset_rc_dir));
strcpy (filename, mcset_rc_dir);
if (mcset_prefix_bin)
sprintf (filename + strlen (filename), "%s_", mcset_mc_basename);
strcat (filename, nd);
strcat (filename, ".bin");
/* Write message file. */
windmc_write_bin (filename, &mc_nodes_lang[i], (c - i));
free (filename);
i = c;
}
}
static void
write_rc (FILE *fp)
{
mc_node_lang *n;
int i, l;
fprintf (fp,
"/* Do not edit this file manually.\n"
" This file is autogenerated by windmc. */\n\n");
if (! mc_nodes_lang_count)
return;
n = NULL;
i = 0;
for (l = 0; l < mc_nodes_lang_count; l++)
{
if (n && n->lang == mc_nodes_lang[l]->lang)
continue;
++i;
n = mc_nodes_lang[l];
fprintf (fp, "\n// Country: %s\n// Language: %s\n#pragma code_page(%u)\n",
n->lang->lang_info.country, n->lang->lang_info.name,
(unsigned) n->lang->lang_info.wincp);
fprintf (fp, "LANGUAGE 0x%lx, 0x%lx\n",
(unsigned long) (n->lang->nval & 0x3ff),
(unsigned long) ((n->lang->nval & 0xffff) >> 10));
fprintf (fp, "1 MESSAGETABLE \"");
if (mcset_prefix_bin)
fprintf (fp, "%s_", mcset_mc_basename);
unicode_print (fp, n->lang->sval, unichar_len (n->lang->sval));
fprintf (fp, ".bin\"\n");
}
}
static void
write_dbg (FILE *fp)
{
mc_node *h;
fprintf (fp,
"/* Do not edit this file manually.\n"
" This file is autogenerated by windmc.\n\n"
" This file maps each message ID value in to a text string that contains\n"
" the symbolic name used for it. */\n\n");
fprintf (fp,
"struct %sSymbolicName\n"
"{\n ", mcset_mc_basename);
if (mcset_msg_id_typedef)
unicode_print (fp, mcset_msg_id_typedef, unichar_len (mcset_msg_id_typedef));
else
fprintf (fp, "DWORD");
fprintf (fp,
" MessageId;\n"
" char *SymbolicName;\n"
"} %sSymbolicNames[] =\n"
"{\n", mcset_mc_basename);
h = mc_nodes;
while (h != NULL)
{
if (h->symbol)
write_dbg_define (fp, h->symbol, mcset_msg_id_typedef);
h = h->next;
}
fprintf (fp, " { (");
if (mcset_msg_id_typedef)
unicode_print (fp, mcset_msg_id_typedef, unichar_len (mcset_msg_id_typedef));
else
fprintf (fp, "DWORD");
fprintf (fp,
") 0xffffffff, NULL }\n"
"};\n");
}
static void
write_header (FILE *fp)
{
char *s;
int i;
const mc_keyword *key;
mc_node *h;
fprintf (fp,
"/* Do not edit this file manually.\n"
" This file is autogenerated by windmc. */\n\n"
"//\n// The values are 32 bit layed out as follows:\n//\n"
"// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1\n"
"// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0\n"
"// +---+-+-+-----------------------+-------------------------------+\n"
"// |Sev|C|R| Facility | Code |\n"
"// +---+-+-+-----------------------+-------------------------------+\n//\n"
"// where\n//\n"
"// C - is the Customer code flag\n//\n"
"// R - is a reserved bit\n//\n"
"// Code - is the facility's status code\n//\n");
h = mc_nodes;
fprintf (fp, "// Sev - is the severity code\n//\n");
if (mc_severity_codes_count != 0)
{
for (i = 0; i < mc_severity_codes_count; i++)
{
key = mc_severity_codes[i];
fprintf (fp, "// %s - %02lx\n", convert_unicode_to_ACP (key->usz),
(unsigned long) key->nval);
if (key->sval && key->sval[0] != 0)
{
if (! mcset_out_values_are_decimal)
fprintf (fp, "#define %s 0x%lx\n", convert_unicode_to_ACP (key->sval),
(unsigned long) key->nval);
else
fprintf (fp, "#define %s 0x%lu\n", convert_unicode_to_ACP (key->sval),
(unsigned long) key->nval);
}
}
fprintf (fp, "//\n");
}
fprintf (fp, "// Facility - is the facility code\n//\n");
if (mc_facility_codes_count != 0)
{
for (i = 0; i < mc_facility_codes_count; i++)
{
key = mc_facility_codes[i];
fprintf (fp, "// %s - %04lx\n", convert_unicode_to_ACP (key->usz),
(unsigned long) key->nval);
if (key->sval && key->sval[0] != 0)
{
if (! mcset_out_values_are_decimal)
fprintf (fp, "#define %s 0x%lx\n", convert_unicode_to_ACP (key->sval),
(unsigned long) key->nval);
else
fprintf (fp, "#define %s 0x%lu\n", convert_unicode_to_ACP (key->sval),
(unsigned long) key->nval);
}
}
fprintf (fp, "//\n");
}
fprintf (fp, "\n");
while (h != NULL)
{
if (h->user_text)
{
s = convert_unicode_to_ACP (h->user_text);
if (s)
fprintf (fp, "%s", s);
}
if (h->symbol)
write_header_define (fp, h->symbol, h->vid, mcset_msg_id_typedef, h->sub);
h = h->next;
}
}
static const char *
mc_unify_path (const char *path)
{
char *end;
char *hsz;
if (! path || *path == 0)
return "./";
hsz = xmalloc (strlen (path) + 2);
strcpy (hsz, path);
end = hsz + strlen (hsz);
if (hsz[-1] != '/' && hsz[-1] != '\\')
strcpy (end, "/");
while ((end = strchr (hsz, '\\')) != NULL)
*end = '/';
return hsz;
}
int main (int, char **);
int
main (int argc, char **argv)
{
FILE *h_fp;
int c;
char *target, *input_filename;
int verbose;
#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
setlocale (LC_MESSAGES, "");
#endif
#if defined (HAVE_SETLOCALE)
setlocale (LC_CTYPE, "");
#endif
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
program_name = argv[0];
xmalloc_set_program_name (program_name);
bfd_set_error_program_name (program_name);
expandargv (&argc, &argv);
if (bfd_init () != BFD_INIT_MAGIC)
fatal (_("fatal error: libbfd ABI mismatch"));
set_default_bfd_target ();
target = NULL;
verbose = 0;
input_filename = NULL;
res_init ();
while ((c = getopt_long (argc, argv, "C:F:O:h:e:m:r:x:aAbcdHunoUvV", long_options,
(int *) 0)) != EOF)
{
switch (c)
{
case 'b':
mcset_prefix_bin = 1;
break;
case 'e':
{
mcset_header_ext = optarg;
if (mcset_header_ext[0] != '.' && mcset_header_ext[0] != 0)
{
char *hsz = xmalloc (strlen (mcset_header_ext) + 2);
sprintf (hsz, ".%s", mcset_header_ext);
mcset_header_ext = hsz;
}
}
break;
case 'h':
mcset_header_dir = mc_unify_path (optarg);
break;
case 'r':
mcset_rc_dir = mc_unify_path (optarg);
break;
case 'a':
mcset_text_in_is_unicode = 0;
break;
case 'x':
if (*optarg != 0)
mcset_dbg_dir = mc_unify_path (optarg);
break;
case 'A':
mcset_bin_out_is_unicode = 0;
break;
case 'd':
mcset_out_values_are_decimal = 1;
break;
case 'u':
mcset_text_in_is_unicode = 1;
break;
case 'U':
mcset_bin_out_is_unicode = 1;
break;
case 'c':
mcset_custom_bit = 1;
break;
case 'n':
mcset_automatic_null_termination = 1;
break;
case 'o':
mcset_use_hresult = 1;
fatal ("option -o is not implemented until yet.\n");
break;
case 'F':
target = optarg;
break;
case 'v':
verbose ++;
break;
case 'm':
mcset_max_message_length = strtol (optarg, (char **) NULL, 10);
break;
case 'C':
mcset_codepage_in = strtol (optarg, (char **) NULL, 10);
break;
case 'O':
mcset_codepage_out = strtol (optarg, (char **) NULL, 10);
break;
case '?':
case 'H':
usage (stdout, 0);
break;
case 'V':
print_version ("windmc");
break;
default:
usage (stderr, 1);
break;
}
}
if (input_filename == NULL && optind < argc)
{
input_filename = argv[optind];
++optind;
}
set_endianness (NULL, target);
if (input_filename == NULL)
{
fprintf (stderr, "Error: No input file was specified.\n");
usage (stderr, 1);
}
mc_set_inputfile (input_filename);
if (!probe_codepage (&mcset_codepage_in, &mcset_text_in_is_unicode, "codepage_in", 0))
usage (stderr, 1);
if (mcset_codepage_out == 0)
mcset_codepage_out = 1252;
if (! unicode_is_valid_codepage (mcset_codepage_out))
fatal ("Code page 0x%x is unknown.", (unsigned int) mcset_codepage_out);
if (mcset_codepage_out == CP_UTF16)
fatal ("UTF16 is no valid text output code page.");
if (verbose)
{
fprintf (stderr, "// Default target is %s and it is %s endian.\n",
def_target_arch, (target_is_bigendian ? "big" : "little"));
fprintf (stderr, "// Input codepage: 0x%x\n", (unsigned int) mcset_codepage_in);
fprintf (stderr, "// Output codepage: 0x%x\n", (unsigned int) mcset_codepage_out);
}
if (argc != optind)
usage (stderr, 1);
/* Initialize mcset_mc_basename. */
{
const char *bn, *bn2;
char *hsz;
bn = strrchr (input_filename, '/');
bn2 = strrchr (input_filename, '\\');
if (! bn)
bn = bn2;
if (bn && bn2 && bn < bn2)
bn = bn2;
if (! bn)
bn = input_filename;
else
bn++;
mcset_mc_basename = hsz = xstrdup (bn);
/* Cut of right-hand extension. */
if ((hsz = strrchr (hsz, '.')) != NULL)
*hsz = 0;
}
/* Load the input file and do code page transformations to UTF16. */
{
unichar *u;
rc_uint_type ul;
char *buff;
bfd_size_type flen;
FILE *fp = fopen (input_filename, "rb");
if (!fp)
fatal (_("unable to open file `%s' for input.\n"), input_filename);
fseek (fp, 0, SEEK_END);
flen = ftell (fp);
fseek (fp, 0, SEEK_SET);
buff = malloc (flen + 3);
memset (buff, 0, flen + 3);
if (fread (buff, 1, flen, fp) < flen)
fatal (_("unable to read contents of %s"), input_filename);
fclose (fp);
if (mcset_text_in_is_unicode != 1)
{
unicode_from_codepage (&ul, &u, buff, mcset_codepage_in);
if (! u)
fatal ("Failed to convert input to UFT16\n");
mc_set_content (u);
}
else
{
if ((flen & 1) != 0)
fatal (_("input file does not seems to be UFT16.\n"));
mc_set_content ((unichar *) buff);
}
free (buff);
}
while (yyparse ())
;
do_sorts ();
h_fp = mc_create_path_text_file (mcset_header_dir, mcset_header_ext);
write_header (h_fp);
fclose (h_fp);
h_fp = mc_create_path_text_file (mcset_rc_dir, ".rc");
write_rc (h_fp);
fclose (h_fp);
if (mcset_dbg_dir != NULL)
{
h_fp = mc_create_path_text_file (mcset_dbg_dir, ".dbg");
write_dbg (h_fp);
fclose (h_fp);
}
write_bin ();
if (mc_nodes_lang)
free (mc_nodes_lang);
if (mc_severity_codes)
free (mc_severity_codes);
if (mc_facility_codes)
free (mc_facility_codes);
xexit (0);
return 0;
}
|