Added codepage translation based on the language id (based on a patch
[wine] / tools / wrc / newstruc.c
1 /*
2  * Create dynamic new structures of various types
3  * and some utils in that trend.
4  *
5  * Copyright 1998 Bertho A. Stultiens
6  *
7  */
8
9 #include "config.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <assert.h>
15 #include <ctype.h>
16
17 #include "wrc.h"
18 #include "newstruc.h"
19 #include "utils.h"
20 #include "parser.h"
21
22 #include "wingdi.h"     /* for BITMAPINFOHEADER */
23
24 /* Generate new_* functions that have no parameters (NOTE: no ';') */
25 __NEW_STRUCT_FUNC(dialog)
26 __NEW_STRUCT_FUNC(dialogex)
27 __NEW_STRUCT_FUNC(name_id)
28 __NEW_STRUCT_FUNC(menu)
29 __NEW_STRUCT_FUNC(menuex)
30 __NEW_STRUCT_FUNC(menu_item)
31 __NEW_STRUCT_FUNC(menuex_item)
32 __NEW_STRUCT_FUNC(control)
33 __NEW_STRUCT_FUNC(icon)
34 __NEW_STRUCT_FUNC(cursor)
35 __NEW_STRUCT_FUNC(versioninfo)
36 __NEW_STRUCT_FUNC(ver_value)
37 __NEW_STRUCT_FUNC(ver_block)
38 __NEW_STRUCT_FUNC(stt_entry)
39 __NEW_STRUCT_FUNC(accelerator)
40 __NEW_STRUCT_FUNC(event)
41 __NEW_STRUCT_FUNC(raw_data)
42 __NEW_STRUCT_FUNC(lvc)
43 __NEW_STRUCT_FUNC(res_count)
44 __NEW_STRUCT_FUNC(string)
45 __NEW_STRUCT_FUNC(toolbar_item)
46 __NEW_STRUCT_FUNC(ani_any)
47
48 /* New instances for all types of structures */
49 /* Very inefficient (in size), but very functional :-]
50  * Especially for type-checking.
51  */
52 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
53 {
54         resource_t *r = (resource_t *)xmalloc(sizeof(resource_t));
55         r->type = t;
56         r->res.overlay = res;
57         r->memopt = memopt;
58         r->lan = lan;
59         return r;
60 }
61
62 version_t *new_version(DWORD v)
63 {
64         version_t *vp = (version_t *)xmalloc(sizeof(version_t));
65         *vp = v;
66         return vp;
67 }
68
69 characts_t *new_characts(DWORD c)
70 {
71         characts_t *cp = (characts_t *)xmalloc(sizeof(characts_t));
72         *cp = c;
73         return cp;
74 }
75
76 language_t *new_language(int id, int sub)
77 {
78         language_t *lan = (language_t *)xmalloc(sizeof(language_t));
79         lan->id = id;
80         lan->sub = sub;
81         return lan;
82 }
83
84 language_t *dup_language(language_t *l)
85 {
86         if(!l) return NULL;
87         return new_language(l->id, l->sub);
88 }
89
90 version_t *dup_version(version_t *v)
91 {
92         if(!v) return NULL;
93         return new_version(*v);
94 }
95
96 characts_t *dup_characts(characts_t *c)
97 {
98         if(!c) return NULL;
99         return new_characts(*c);
100 }
101
102 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
103 {
104         rcdata_t *rc = (rcdata_t *)xmalloc(sizeof(rcdata_t));
105         rc->data = rd;
106         if(memopt)
107         {
108                 rc->memopt = *memopt;
109                 free(memopt);
110         }
111         else
112                 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
113         return rc;
114 }
115
116 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
117 {
118         font_id_t *fid = (font_id_t *)xmalloc(sizeof(font_id_t));
119         fid->name = face;
120         fid->size = size;
121         fid->weight = weight;
122         fid->italic = italic;
123         return fid;
124 }
125
126 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
127 {
128         user_t *usr = (user_t *)xmalloc(sizeof(user_t));
129         usr->data = rd;
130         if(memopt)
131         {
132                 usr->memopt = *memopt;
133                 free(memopt);
134         }
135         else
136                 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
137         usr->type = type;
138         return usr;
139 }
140
141 font_t *new_font(raw_data_t *rd, int *memopt)
142 {
143         font_t *fnt = (font_t *)xmalloc(sizeof(font_t));
144         fnt->data = rd;
145         if(memopt)
146         {
147                 fnt->memopt = *memopt;
148                 free(memopt);
149         }
150         else
151                 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
152         return fnt;
153 }
154
155 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
156 {
157         fontdir_t *fnd = (fontdir_t *)xmalloc(sizeof(fontdir_t));
158         fnd->data = rd;
159         if(memopt)
160         {
161                 fnd->memopt = *memopt;
162                 free(memopt);
163         }
164         else
165                 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
166         return fnd;
167 }
168
169
170 /*
171  * Convert bitmaps to proper endian
172  */
173 static void convert_bitmap_swap_v3(BITMAPINFOHEADER *bih)
174 {
175         bih->biSize             = BYTESWAP_DWORD(bih->biSize);
176         bih->biWidth            = BYTESWAP_DWORD(bih->biWidth);
177         bih->biHeight           = BYTESWAP_DWORD(bih->biHeight);
178         bih->biPlanes           = BYTESWAP_WORD(bih->biPlanes);
179         bih->biBitCount         = BYTESWAP_WORD(bih->biBitCount);
180         bih->biCompression      = BYTESWAP_DWORD(bih->biCompression);
181         bih->biSizeImage        = BYTESWAP_DWORD(bih->biSizeImage);
182         bih->biXPelsPerMeter    = BYTESWAP_DWORD(bih->biXPelsPerMeter);
183         bih->biYPelsPerMeter    = BYTESWAP_DWORD(bih->biYPelsPerMeter);
184         bih->biClrUsed          = BYTESWAP_DWORD(bih->biClrUsed);
185         bih->biClrImportant     = BYTESWAP_DWORD(bih->biClrImportant);
186 }
187
188 static void convert_bitmap_swap_v4(BITMAPV4HEADER *b4h)
189 {
190         convert_bitmap_swap_v3((BITMAPINFOHEADER *)b4h);
191         b4h->bV4RedMask         = BYTESWAP_DWORD(b4h->bV4RedMask);
192         b4h->bV4GreenMask       = BYTESWAP_DWORD(b4h->bV4GreenMask);
193         b4h->bV4BlueMask        = BYTESWAP_DWORD(b4h->bV4BlueMask);
194         b4h->bV4AlphaMask       = BYTESWAP_DWORD(b4h->bV4AlphaMask);
195         b4h->bV4CSType          = BYTESWAP_DWORD(b4h->bV4CSType);
196         b4h->bV4EndPoints.ciexyzRed.ciexyzX     = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzX);
197         b4h->bV4EndPoints.ciexyzRed.ciexyzY     = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzY);
198         b4h->bV4EndPoints.ciexyzRed.ciexyzZ     = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzZ);
199         b4h->bV4EndPoints.ciexyzGreen.ciexyzX   = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzX);
200         b4h->bV4EndPoints.ciexyzGreen.ciexyzY   = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzY);
201         b4h->bV4EndPoints.ciexyzGreen.ciexyzZ   = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzZ);
202         b4h->bV4EndPoints.ciexyzBlue.ciexyzX    = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzX);
203         b4h->bV4EndPoints.ciexyzBlue.ciexyzY    = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzY);
204         b4h->bV4EndPoints.ciexyzBlue.ciexyzZ    = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzZ);
205         b4h->bV4GammaRed        = BYTESWAP_DWORD(b4h->bV4GammaRed);
206         b4h->bV4GammaGreen      = BYTESWAP_DWORD(b4h->bV4GammaGreen);
207         b4h->bV4GammaBlue       = BYTESWAP_DWORD(b4h->bV4GammaBlue);
208 }
209
210 #define FL_SIGBE        0x01
211 #define FL_SIZEBE       0x02
212 #define FL_V4           0x04
213 static int convert_bitmap(char *data, int size)
214 {
215         BITMAPINFOHEADER *bih = (BITMAPINFOHEADER *)data;
216         BITMAPV4HEADER *b4h = (BITMAPV4HEADER *)data;
217         int type = 0;
218 #ifdef WORDS_BIGENDIAN
219         DWORD bisizel = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
220         DWORD b4sizel = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
221         DWORD bisizeb = sizeof(BITMAPINFOHEADER);
222         DWORD b4sizeb = sizeof(BITMAPV4HEADER);
223 #else
224         DWORD bisizel = sizeof(BITMAPINFOHEADER);
225         DWORD b4sizel = sizeof(BITMAPV4HEADER);
226         DWORD bisizeb = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
227         DWORD b4sizeb = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
228 #endif
229
230         if(data[0] == 'B' && data[1] == 'M')
231         {
232                 /* Little endian signature */
233                 bih = (BITMAPINFOHEADER *)(data + sizeof(BITMAPFILEHEADER));
234                 b4h = (BITMAPV4HEADER *)(data + sizeof(BITMAPFILEHEADER));
235         }
236         else if(data[0] == 'M' && data[1] == 'B')
237         {
238                 type |= FL_SIGBE;       /* Big endian signature */
239                 bih = (BITMAPINFOHEADER *)(data + sizeof(BITMAPFILEHEADER));
240                 b4h = (BITMAPV4HEADER *)(data + sizeof(BITMAPFILEHEADER));
241         }
242
243         if(bih->biSize == bisizel)
244         {
245                 /* Little endian */
246         }
247         else if(bih->biSize == b4sizel)
248         {
249                 type |= FL_V4;
250         }
251         else if(bih->biSize == bisizeb)
252         {
253                 type |= FL_SIZEBE;
254         }
255         else if(bih->biSize == b4sizeb)
256         {
257                 type |= FL_SIZEBE | FL_V4;
258         }
259         else
260                 type = -1;
261
262         switch(type)
263         {
264         default:
265                 break;
266         case FL_SIZEBE:
267         case FL_SIZEBE | FL_V4:
268                 yywarning("Bitmap v%c signature little-endian, but size big-endian", type & FL_V4 ? '4' : '3');
269                 break;
270         case FL_SIGBE:
271         case FL_SIGBE | FL_V4:
272                 yywarning("Bitmap v%c signature big-endian, but size little-endian", type & FL_V4 ? '4' : '3');
273                 break;
274         case -1:
275                 yyerror("Invalid bitmap format");
276                 break;
277         }
278
279         switch(byteorder)
280         {
281 #ifdef WORDS_BIGENDIAN
282         default:
283 #endif
284         case WRC_BO_BIG:
285                 if(!(type & FL_SIZEBE))
286                 {
287                         if(type & FL_V4)
288                                 convert_bitmap_swap_v4(b4h);
289                         else
290                                 convert_bitmap_swap_v3(bih);
291                 }
292                 break;
293 #ifndef WORDS_BIGENDIAN
294         default:
295 #endif
296         case WRC_BO_LITTLE:
297                 if(type & FL_SIZEBE)
298                 {
299                         if(type & FL_V4)
300                                 convert_bitmap_swap_v4(b4h);
301                         else
302                                 convert_bitmap_swap_v3(bih);
303                 }
304                 break;
305         }
306
307         if(size && (void *)data != (void *)bih)
308         {
309                 /* We have the fileheader still attached, remove it */
310                 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
311                 return sizeof(BITMAPFILEHEADER);
312         }
313         return 0;
314 }
315 #undef FL_SIGBE
316 #undef FL_SIZEBE
317 #undef FL_V4
318
319 /*
320  * Cursor and icon splitter functions used when allocating
321  * cursor- and icon-groups.
322  */
323 typedef struct {
324         language_t      lan;
325         int             id;
326 } id_alloc_t;
327
328 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
329 {
330         int i;
331         assert(lan != NULL);
332         assert(list != NULL);
333         assert(n != NULL);
334
335         if(!*list)
336         {
337                 *list = (id_alloc_t *)xmalloc(sizeof(id_alloc_t));
338                 *n = 1;
339                 (*list)[0].lan = *lan;
340                 (*list)[0].id = 1;
341                 return 1;
342         }
343
344         for(i = 0; i < *n; i++)
345         {
346                 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
347                         return ++((*list)[i].id);
348         }
349
350         *list = (id_alloc_t *)xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
351         (*list)[*n].lan = *lan;
352         (*list)[*n].id = 1;
353         *n += 1;
354         return 1;
355 }
356
357 static int alloc_icon_id(language_t *lan)
358 {
359         static id_alloc_t *idlist = NULL;
360         static int nid = 0;
361
362         return get_new_id(&idlist, &nid, lan);
363 }
364
365 static int alloc_cursor_id(language_t *lan)
366 {
367         static id_alloc_t *idlist = NULL;
368         static int nid = 0;
369
370         return get_new_id(&idlist, &nid, lan);
371 }
372
373 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
374 {
375         int cnt;
376         int i;
377         icon_dir_entry_t *ide;
378         icon_t *ico;
379         icon_t *list = NULL;
380         icon_header_t *ih = (icon_header_t *)rd->data;
381         int swap = 0;
382
383         if(ih->type == 1)
384                 swap = 0;
385         else if(BYTESWAP_WORD(ih->type) == 1)
386                 swap = 1;
387         else
388                 yyerror("Icon resource data has invalid type id %d", ih->type);
389
390         cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
391         ide = (icon_dir_entry_t *)((char *)rd->data + sizeof(icon_header_t));
392         for(i = 0; i < cnt; i++)
393         {
394                 ico = new_icon();
395                 ico->id = alloc_icon_id(icog->lvc.language);
396                 ico->lvc = icog->lvc;
397                 if(swap)
398                 {
399                         ide[i].offset = BYTESWAP_DWORD(ide[i].offset);
400                         ide[i].ressize= BYTESWAP_DWORD(ide[i].ressize);
401                 }
402                 if(ide[i].offset > rd->size
403                 || ide[i].offset + ide[i].ressize > rd->size)
404                         yyerror("Icon resource data corrupt");
405                 ico->width = ide[i].width;
406                 ico->height = ide[i].height;
407                 ico->nclr = ide[i].nclr;
408                 ico->planes = swap ? BYTESWAP_WORD(ide[i].planes) : ide[i].planes;
409                 ico->bits = swap ? BYTESWAP_WORD(ide[i].bits) : ide[i].bits;
410                 convert_bitmap((char *)rd->data + ide[i].offset, 0);
411                 if(!ico->planes)
412                 {
413                         WORD planes;
414                         /* Argh! They did not fill out the resdir structure */
415                         planes = ((BITMAPINFOHEADER *)((char *)rd->data + ide[i].offset))->biPlanes;
416                         /* The bitmap is in destination byteorder. We want native for our structures */
417                         switch(byteorder)
418                         {
419 #ifdef WORDS_BIGENDIAN
420                         case WRC_BO_LITTLE:
421 #else
422                         case WRC_BO_BIG:
423 #endif
424                                 ico->planes = BYTESWAP_WORD(planes);
425                                 break;
426                         default:
427                                 ico->planes = planes;
428                         }
429                 }
430                 if(!ico->bits)
431                 {
432                         WORD bits;
433                         /* Argh! They did not fill out the resdir structure */
434                         bits = ((BITMAPINFOHEADER *)((char *)rd->data + ide[i].offset))->biBitCount;
435                         /* The bitmap is in destination byteorder. We want native for our structures */
436                         switch(byteorder)
437                         {
438 #ifdef WORDS_BIGENDIAN
439                         case WRC_BO_LITTLE:
440 #else
441                         case WRC_BO_BIG:
442 #endif
443                                 ico->bits = BYTESWAP_WORD(bits);
444                                 break;
445                         default:
446                                 ico->bits = bits;
447                         }
448                 }
449                 ico->data = new_raw_data();
450                 copy_raw_data(ico->data, rd, ide[i].offset, ide[i].ressize);
451                 if(!list)
452                 {
453                         list = ico;
454                 }
455                 else
456                 {
457                         ico->next = list;
458                         list->prev = ico;
459                         list = ico;
460                 }
461         }
462         icog->iconlist = list;
463         *nico = cnt;
464 }
465
466 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
467 {
468         int cnt;
469         int i;
470         cursor_dir_entry_t *cde;
471         cursor_t *cur;
472         cursor_t *list = NULL;
473         cursor_header_t *ch = (cursor_header_t *)rd->data;
474         int swap = 0;
475
476         if(ch->type == 2)
477                 swap = 0;
478         else if(BYTESWAP_WORD(ch->type) == 2)
479                 swap = 1;
480         else
481                 yyerror("Cursor resource data has invalid type id %d", ch->type);
482         cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
483         cde = (cursor_dir_entry_t *)((char *)rd->data + sizeof(cursor_header_t));
484         for(i = 0; i < cnt; i++)
485         {
486                 WORD planes;
487                 WORD bits;
488                 cur = new_cursor();
489                 cur->id = alloc_cursor_id(curg->lvc.language);
490                 cur->lvc = curg->lvc;
491                 if(swap)
492                 {
493                         cde[i].offset = BYTESWAP_DWORD(cde[i].offset);
494                         cde[i].ressize= BYTESWAP_DWORD(cde[i].ressize);
495                 }
496                 if(cde[i].offset > rd->size
497                 || cde[i].offset + cde[i].ressize > rd->size)
498                         yyerror("Cursor resource data corrupt");
499                 cur->width = cde[i].width;
500                 cur->height = cde[i].height;
501                 cur->nclr = cde[i].nclr;
502                 convert_bitmap((char *)rd->data + cde[i].offset, 0);
503                 /* The next two are to support color cursors */
504                 planes = ((BITMAPINFOHEADER *)((char *)rd->data + cde[i].offset))->biPlanes;
505                 bits = ((BITMAPINFOHEADER *)((char *)rd->data + cde[i].offset))->biBitCount;
506                 /* The bitmap is in destination byteorder. We want native for our structures */
507                 switch(byteorder)
508                 {
509 #ifdef WORDS_BIGENDIAN
510                 case WRC_BO_LITTLE:
511 #else
512                 case WRC_BO_BIG:
513 #endif
514                         cur->planes = BYTESWAP_WORD(planes);
515                         cur->bits = BYTESWAP_WORD(bits);
516                         break;
517                 default:
518                         cur->planes = planes;
519                         cur->bits = bits;
520                 }
521                 if(!win32 && (cur->planes != 1 || cur->bits != 1))
522                         yywarning("Win16 cursor contains colors");
523                 cur->xhot = swap ? BYTESWAP_WORD(cde[i].xhot) : cde[i].xhot;
524                 cur->yhot = swap ? BYTESWAP_WORD(cde[i].yhot) : cde[i].yhot;
525                 cur->data = new_raw_data();
526                 copy_raw_data(cur->data, rd, cde[i].offset, cde[i].ressize);
527                 if(!list)
528                 {
529                         list = cur;
530                 }
531                 else
532                 {
533                         cur->next = list;
534                         list->prev = cur;
535                         list = cur;
536                 }
537         }
538         curg->cursorlist = list;
539         *ncur = cnt;
540 }
541
542
543 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
544 {
545         icon_group_t *icog = (icon_group_t *)xmalloc(sizeof(icon_group_t));
546         if(memopt)
547         {
548                 icog->memopt = *memopt;
549                 free(memopt);
550         }
551         else
552                 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
553         icog->lvc = rd->lvc;
554         split_icons(rd, icog, &(icog->nicon));
555         free(rd->data);
556         free(rd);
557         return icog;
558 }
559
560 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
561 {
562         cursor_group_t *curg = (cursor_group_t *)xmalloc(sizeof(cursor_group_t));
563         if(memopt)
564         {
565                 curg->memopt = *memopt;
566                 free(memopt);
567         }
568         else
569                 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
570         curg->lvc = rd->lvc;
571         split_cursors(rd, curg, &(curg->ncursor));
572         free(rd->data);
573         free(rd);
574         return curg;
575 }
576
577 /*
578  * Animated cursors and icons
579  *
580  * The format of animated cursors and icons is yet another example
581  * of bad design by "The Company". The entire RIFF structure is
582  * flawed by design because it is inconsistent and single minded:
583  * - some tags have lengths attached, others don't. The use of these
584  *   non-length tags is absolutely unclear;
585  * - the content of "icon" tags can be both icons and cursors;
586  * - tags lack proper alignment constraints. It seems that everything
587  *   is 16bit aligned, but I could not find that in any docu. Just be
588  *   prepared to eat anything;
589  * - there are no strict constraints on tag-nesting and the organization
590  *   is highly illogical;
591  *
592  * Anyhow, here is the basic structure:
593  * "RIFF" { dword taglength }
594  *      "ACON"                                  // What does it do?
595  *      "LIST" { dword taglength }
596  *              "INFO"                          // And what does this do?
597  *              "INAM" { dword taglength }      // Icon/cursor name
598  *                      {inam data}
599  *              "IART" { dword taglength }      // The artist
600  *                      {iart data}
601  *              "fram"                          // Is followed by "icon"s
602  *              "icon" { dword taglength }      // First frame
603  *                      { icon/cursor data }
604  *              "icon" { dword taglength }      // Second frame
605  *                      { icon/cursor data }
606  *                      ...                     // ...
607  *      "anih" { dword taglength }              // Header structure
608  *              { aniheader_t structure }
609  *      "rate" { dword taglength }              // The rate for each frame
610  *              { `steps' dwords }
611  *      "seq " { dword taglength }              // The frame blit-order
612  *              { `steps' dwords }
613  *
614  * Tag length are bytelength without the header and length field (i.e. -8).
615  * The "LIST" tag may occur several times and may encapsulate different
616  * tags. The `steps' is the number of "icon" tags found (actually the
617  * number of steps specified in the aniheader_t structure). The "seq "uence
618  * tag can be ommitted, in which case the sequence is equal to the sequence
619  * of "icon"s found in the file. Also "rate" may be ommitted, in which case
620  * the default from the aniheader_t structure is used.
621  *
622  * An animated cursor puts `.cur' formatted files into each "icon" tag,
623  * whereas animated icons contain `.ico' formatted files.
624  *
625  * Note about the code: Yes, it can be shorter/compressed. Some tags can be
626  * dealt with in the same code. However, this version shows what is going on
627  * and is better debug-able.
628  */
629 static const char riff[4] = "RIFF";
630 static const char acon[4] = "ACON";
631 static const char list[4] = "LIST";
632 static const char info[4] = "INFO";
633 static const char inam[4] = "INAM";
634 static const char iart[4] = "IART";
635 static const char fram[4] = "fram";
636 static const char icon[4] = "icon";
637 static const char anih[4] = "anih";
638 static const char rate[4] = "rate";
639 static const char seq[4]  = "seq ";
640
641 #define NEXT_TAG(p)     ((riff_tag_t *)(((char *)p) + (isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p)))
642
643 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
644 {
645         cursor_dir_entry_t *cdp;
646         cursor_header_t *chp;
647         int count;
648         int ctype;
649         int i;
650         static int once = 0;    /* This will trigger only once per file! */
651         const char *anistr = type == res_aniico ? "icon" : "cursor";
652         /* Notes:
653          * Both cursor and icon directories are similar
654          * Both cursor and icon headers are similar
655          */
656
657         chp = (cursor_header_t *)(rtp+1);
658         cdp = (cursor_dir_entry_t *)(chp+1);
659         count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
660         ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
661         chp->reserved   = BYTESWAP_WORD(chp->reserved);
662         chp->type       = BYTESWAP_WORD(chp->type);
663         chp->count      = BYTESWAP_WORD(chp->count);
664
665         if(type == res_anicur && ctype != 2 && !once)
666         {
667                 yywarning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)",
668                                 ctype,
669                                 ctype == 1 ? "icontype" : "?");
670                 once++;
671         }
672         else if(type == res_aniico && ctype != 1 && !once)
673         {
674                 yywarning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)",
675                                 ctype,
676                                 ctype == 2 ? "cursortype" : "?");
677                 once++;
678         }
679         else if(ctype != 1 && ctype != 2 && !once)
680         {
681                 yywarning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)", anistr, ctype);
682                 once++;
683         }
684
685         for(i = 0; i < count; i++)
686         {
687                 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
688                 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
689                 if(ofs > rtp->size || ofs+sze > rtp->size)
690                         yyerror("Animated %s's data corrupt", anistr);
691                 convert_bitmap((char *)chp + ofs, 0);
692                 cdp[i].xhot     = BYTESWAP_WORD(cdp->xhot);
693                 cdp[i].yhot     = BYTESWAP_WORD(cdp->yhot);
694                 cdp[i].ressize  = BYTESWAP_DWORD(cdp->ressize);
695                 cdp[i].offset   = BYTESWAP_DWORD(cdp->offset);
696         }
697 }
698
699 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
700 {
701         riff_tag_t *rtp = lst+1;        /* Skip the "LIST" tag */
702         
703         while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
704         {
705                 if(!memcmp(rtp->tag, info, sizeof(info)))
706                 {
707                         rtp = (riff_tag_t *)(((char *)rtp) + 4);
708                 }
709                 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
710                 {
711                         /* Ignore the icon/cursor name; its a string */
712                         rtp = NEXT_TAG(rtp);
713                 }
714                 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
715                 {
716                         /* Ignore the author's name; its a string */
717                         rtp = NEXT_TAG(rtp);
718                 }
719                 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
720                 {
721                         /* This should be followed by "icon"s, but we
722                          * simply ignore this because it is pure
723                          * non-information.
724                          */
725                         rtp = (riff_tag_t *)(((char *)rtp) + 4);
726                 }
727                 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
728                 {
729                         handle_ani_icon(rtp, type, isswapped);
730                         rtp = NEXT_TAG(rtp);
731                 }
732                 else
733                         internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
734                                        isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
735                                        isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
736                                        isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
737                                        isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
738
739                 /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
740                 if((DWORD)rtp & 1)
741                         ((char *)rtp)++;
742         }
743 }
744
745 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
746 {
747         ani_curico_t *ani = (ani_curico_t *)xmalloc(sizeof(ani_curico_t));
748         riff_tag_t *rtp;
749         int isswapped = 0;
750         int doswap;
751         const char *anistr = type == res_aniico ? "icon" : "cursor";
752
753         assert(!memcmp(rd->data, riff, sizeof(riff)));
754         assert(type == res_anicur || type == res_aniico);
755
756         rtp = (riff_tag_t *)rd->data;
757
758         if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
759                 isswapped = 1;
760         else if(rtp->size + 2*sizeof(DWORD) == rd->size)
761                 isswapped = 0;
762         else
763                 yyerror("Animated %s has an invalid RIFF length", anistr);
764
765         switch(byteorder)
766         {
767 #ifdef WORDS_BIGENDIAN
768         case WRC_BO_LITTLE:
769 #else
770         case WRC_BO_BIG:
771 #endif
772                 doswap = !isswapped;
773                 break;
774         default:
775                 doswap = isswapped;
776         }
777
778         /*
779          * When to swap what:
780          * isswapped | doswap |
781          * ----------+--------+---------------------------------
782          *     0     |    0   | read native; don't convert
783          *     1     |    0   | read swapped size; don't convert
784          *     0     |    1   | read native; convert
785          *     1     |    1   | read swapped size; convert
786          * Reading swapped size if necessary to calculate in native
787          * format. E.g. a little-endian source on a big-endian
788          * processor.
789          */
790         if(doswap)
791         {
792                 /* We only go through the RIFF file if we need to swap
793                  * bytes in words/dwords. Else we couldn't care less
794                  * what the file contains. This is consistent with
795                  * MS' rc.exe, which doesn't complain at all, eventhough
796                  * the fileformat might not be entirely correct.
797                  */
798                 rtp++;  /* Skip the "RIFF" tag */
799
800                 while((char *)rtp < (char *)rd->data + rd->size)
801                 {
802                         if(!memcmp(rtp->tag, acon, sizeof(acon)))
803                         {
804                                 rtp = (riff_tag_t *)(((char *)rtp) + 4);
805                         }
806                         else if(!memcmp(rtp->tag, list, sizeof(list)))
807                         {
808                                 handle_ani_list(rtp, type, isswapped);
809                                 rtp = NEXT_TAG(rtp);
810                         }
811                         else if(!memcmp(rtp->tag, anih, sizeof(anih)))
812                         {
813                                 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
814                                 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
815                                 ahp->frames     = BYTESWAP_DWORD(ahp->frames);
816                                 ahp->steps      = BYTESWAP_DWORD(ahp->steps);
817                                 ahp->cx         = BYTESWAP_DWORD(ahp->cx);
818                                 ahp->cy         = BYTESWAP_DWORD(ahp->cy);
819                                 ahp->bitcount   = BYTESWAP_DWORD(ahp->bitcount);
820                                 ahp->planes     = BYTESWAP_DWORD(ahp->planes);
821                                 ahp->rate       = BYTESWAP_DWORD(ahp->rate);
822                                 ahp->flags      = BYTESWAP_DWORD(ahp->flags);
823                                 rtp = NEXT_TAG(rtp);
824                         }
825                         else if(!memcmp(rtp->tag, rate, sizeof(rate)))
826                         {
827                                 int cnt = rtp->size / sizeof(DWORD);
828                                 DWORD *dwp = (DWORD *)(rtp+1);
829                                 int i;
830                                 for(i = 0; i < cnt; i++)
831                                         dwp[i] = BYTESWAP_DWORD(dwp[i]);
832                                 rtp = NEXT_TAG(rtp);
833                         }
834                         else if(!memcmp(rtp->tag, seq, sizeof(seq)))
835                         {
836                                 int cnt = rtp->size / sizeof(DWORD);
837                                 DWORD *dwp = (DWORD *)(rtp+1);
838                                 int i;
839                                 for(i = 0; i < cnt; i++)
840                                         dwp[i] = BYTESWAP_DWORD(dwp[i]);
841                                 rtp = NEXT_TAG(rtp);
842                         }
843                         else
844                                 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
845                                        isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
846                                        isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
847                                        isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
848                                        isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
849
850                         /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
851                         if((DWORD)rtp & 1)
852                                 ((char *)rtp)++;
853                 }
854
855                 /* We must end correctly here */
856                 if((char *)rtp != (char *)rd->data + rd->size)
857                         yyerror("Animated %s contains invalid field size(s)", anistr);
858         }
859
860         ani->data = rd;
861         if(memopt)
862         {
863                 ani->memopt = *memopt;
864                 free(memopt);
865         }
866         else
867                 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
868         return ani;
869 }
870 #undef NEXT_TAG
871
872 /* Bitmaps */
873 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
874 {
875         bitmap_t *bmp = (bitmap_t *)xmalloc(sizeof(bitmap_t));
876
877         bmp->data = rd;
878         if(memopt)
879         {
880                 bmp->memopt = *memopt;
881                 free(memopt);
882         }
883         else
884                 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
885         rd->size -= convert_bitmap(rd->data, rd->size);
886         return bmp;
887 }
888
889 ver_words_t *new_ver_words(int i)
890 {
891         ver_words_t *w = (ver_words_t *)xmalloc(sizeof(ver_words_t));
892         w->words = (WORD *)xmalloc(sizeof(WORD));
893         w->words[0] = (WORD)i;
894         w->nwords = 1;
895         return w;
896 }
897
898 ver_words_t *add_ver_words(ver_words_t *w, int i)
899 {
900         w->words = (WORD *)xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
901         w->words[w->nwords] = (WORD)i;
902         w->nwords++;
903         return w;
904 }
905
906 #define MSGTAB_BAD_PTR(p, b, l, r)      (((l) - ((char *)(p) - (char *)(b))) > (r))
907 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
908 {
909         messagetable_t *msg = (messagetable_t *)xmalloc(sizeof(messagetable_t));
910         DWORD nblk;
911         DWORD i;
912         WORD lo;
913         WORD hi;
914
915         msg->data = rd;
916         if(memopt)
917         {
918                 msg->memopt = *memopt;
919                 free(memopt);
920         }
921         else
922                 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
923
924         if(rd->size < sizeof(DWORD))
925                 yyerror("Invalid messagetable, size too small");
926
927         nblk = *(DWORD *)rd->data;
928         lo = WRC_LOWORD(nblk);
929         hi = WRC_HIWORD(nblk);
930
931         /* FIXME:
932          * This test will fail for all n*2^16 blocks in the messagetable.
933          * However, no sane person would want to have so many blocks
934          * and have a table of megabytes attached.
935          * So, I will assume that we have less than 2^16 blocks in the table
936          * and all will just work out fine. Otherwise, we would need to test
937          * the ID, offset and length (and flag) fields to be very sure.
938          */
939         if(hi && lo)
940                 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian");
941         if(!hi && !lo)
942                 yyerror("Invalid messagetable block count 0");
943
944 #ifdef WORDS_BIGENDIAN
945         if(!hi && lo && byteorder != WRC_BO_LITTLE)
946 #else
947         if(hi && !lo && byteorder != WRC_BO_BIG)
948 #endif
949         {
950                 msgtab_block_t *mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
951                 nblk = BYTESWAP_DWORD(nblk);
952                 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
953                         yyerror("Messagetable's blocks are outside of defined data");
954                 for(i = 0; i < nblk; i++)
955                 {
956                         msgtab_entry_t *mep;
957                         DWORD id;
958
959                         mbp[i].idlo   = BYTESWAP_DWORD(mbp[i].idlo);
960                         mbp[i].idhi   = BYTESWAP_DWORD(mbp[i].idhi);
961                         mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
962                         mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
963                         for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
964                         {
965                                 mep->length = BYTESWAP_WORD(mep->length);
966                                 mep->flags  = BYTESWAP_WORD(mep->flags);
967                                 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
968                                         yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i, id);
969                                 if(mep->flags == 1)     /* Docu says 'flags == 0x0001' for unicode */
970                                 {
971                                         WORD *wp = (WORD *)&mep[1];
972                                         int l = mep->length/2 - 2; /* Length included header */
973                                         int n;
974
975                                         if(mep->length & 1)
976                                                 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id, (int)i, mep->length);
977                                         for(n = 0; n < l; n++)
978                                                 wp[n] = BYTESWAP_WORD(wp[n]);
979                                                 
980                                 }
981                                 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
982                         }
983                 }
984         }
985
986         return msg;
987 }
988 #undef MSGTAB_BAD_PTR
989
990 void copy_raw_data(raw_data_t *dst, raw_data_t *src, int offs, int len)
991 {
992         assert(offs <= src->size);
993         assert(offs + len <= src->size);
994         if(!dst->data)
995         {
996                 dst->data = (char *)xmalloc(len);
997                 dst->size = 0;
998         }
999         else
1000                 dst->data = (char *)xrealloc(dst->data, dst->size + len);
1001         /* dst->size holds the offset to copy to */
1002         memcpy(dst->data + dst->size, src->data + offs, len);
1003         dst->size += len;
1004 }
1005
1006 int *new_int(int i)
1007 {
1008         int *ip = (int *)xmalloc(sizeof(int));
1009         *ip = i;
1010         return ip;
1011 }
1012
1013 stringtable_t *new_stringtable(lvc_t *lvc)
1014 {
1015         stringtable_t *stt = (stringtable_t *)xmalloc(sizeof(stringtable_t));
1016
1017         if(lvc)
1018                 stt->lvc = *lvc;
1019
1020         return stt;
1021 }
1022
1023 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1024 {
1025         toolbar_t *tb = (toolbar_t *)xmalloc(sizeof(toolbar_t));
1026         tb->button_width = button_width;
1027         tb->button_height = button_height;
1028         tb->nitems = nitems;
1029         tb->items = items;
1030         return tb;
1031 }
1032
1033 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1034 {
1035         dlginit_t *di = (dlginit_t *)xmalloc(sizeof(dlginit_t));
1036         di->data = rd;
1037         if(memopt)
1038         {
1039                 di->memopt = *memopt;
1040                 free(memopt);
1041         }
1042         else
1043                 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1044
1045         return di;
1046 }
1047
1048 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1049 {
1050         style_pair_t *sp = (style_pair_t *)xmalloc(sizeof(style_pair_t));
1051         sp->style = style;
1052         sp->exstyle = exstyle;
1053         return sp;
1054 }
1055
1056 style_t *new_style(DWORD or_mask, DWORD and_mask)
1057 {
1058         style_t *st = (style_t *)xmalloc(sizeof(style_t));
1059         st->or_mask = or_mask;
1060         st->and_mask = and_mask;
1061         return st;
1062 }
1063