Added mappings for a few messages.
[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_t *ico;
378         icon_t *list = NULL;
379         icon_header_t *ih = (icon_header_t *)rd->data;
380         int swap = 0;
381
382         if(ih->type == 1)
383                 swap = 0;
384         else if(BYTESWAP_WORD(ih->type) == 1)
385                 swap = 1;
386         else
387                 yyerror("Icon resource data has invalid type id %d", ih->type);
388
389         cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
390         for(i = 0; i < cnt; i++)
391         {
392                 icon_dir_entry_t ide;
393                 BITMAPINFOHEADER info;
394                 memcpy(&ide, rd->data + sizeof(icon_header_t)
395                                       + i*sizeof(icon_dir_entry_t), sizeof(ide));
396
397                 ico = new_icon();
398                 ico->id = alloc_icon_id(icog->lvc.language);
399                 ico->lvc = icog->lvc;
400                 if(swap)
401                 {
402                         ide.offset = BYTESWAP_DWORD(ide.offset);
403                         ide.ressize= BYTESWAP_DWORD(ide.ressize);
404                 }
405                 if(ide.offset > rd->size
406                 || ide.offset + ide.ressize > rd->size)
407                         yyerror("Icon resource data corrupt");
408                 ico->width = ide.width;
409                 ico->height = ide.height;
410                 ico->nclr = ide.nclr;
411                 ico->planes = swap ? BYTESWAP_WORD(ide.planes) : ide.planes;
412                 ico->bits = swap ? BYTESWAP_WORD(ide.bits) : ide.bits;
413                 convert_bitmap((char *)rd->data + ide.offset, 0);
414                 memcpy(&info, rd->data + ide.offset, sizeof(info));
415                 if(!ico->planes)
416                 {
417                         /* Argh! They did not fill out the resdir structure */
418                         /* The bitmap is in destination byteorder. We want native for our structures */
419                         switch(byteorder)
420                         {
421 #ifdef WORDS_BIGENDIAN
422                         case WRC_BO_LITTLE:
423 #else
424                         case WRC_BO_BIG:
425 #endif
426                                 ico->planes = BYTESWAP_WORD(info.biPlanes);
427                                 break;
428                         default:
429                                 ico->planes = info.biPlanes;
430                         }
431                 }
432                 if(!ico->bits)
433                 {
434                         /* Argh! They did not fill out the resdir structure */
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(info.biBitCount);
444                                 break;
445                         default:
446                                 ico->bits = info.biBitCount;
447                         }
448                 }
449                 ico->data = new_raw_data();
450                 copy_raw_data(ico->data, rd, ide.offset, ide.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_t *cur;
471         cursor_t *list = NULL;
472         cursor_header_t *ch = (cursor_header_t *)rd->data;
473         int swap = 0;
474
475         if(ch->type == 2)
476                 swap = 0;
477         else if(BYTESWAP_WORD(ch->type) == 2)
478                 swap = 1;
479         else
480                 yyerror("Cursor resource data has invalid type id %d", ch->type);
481         cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
482         for(i = 0; i < cnt; i++)
483         {
484                 cursor_dir_entry_t cde;
485                 BITMAPINFOHEADER info;
486                 memcpy(&cde, rd->data + sizeof(cursor_header_t)
487                                       + i*sizeof(cursor_dir_entry_t), sizeof(cde));
488
489                 cur = new_cursor();
490                 cur->id = alloc_cursor_id(curg->lvc.language);
491                 cur->lvc = curg->lvc;
492                 if(swap)
493                 {
494                         cde.offset = BYTESWAP_DWORD(cde.offset);
495                         cde.ressize= BYTESWAP_DWORD(cde.ressize);
496                 }
497                 if(cde.offset > rd->size
498                 || cde.offset + cde.ressize > rd->size)
499                         yyerror("Cursor resource data corrupt");
500                 cur->width = cde.width;
501                 cur->height = cde.height;
502                 cur->nclr = cde.nclr;
503                 convert_bitmap((char *)rd->data + cde.offset, 0);
504                 memcpy(&info, rd->data + cde.offset, sizeof(info));
505                 /* The bitmap is in destination byteorder. We want native for our structures */
506                 switch(byteorder)
507                 {
508 #ifdef WORDS_BIGENDIAN
509                 case WRC_BO_LITTLE:
510 #else
511                 case WRC_BO_BIG:
512 #endif
513                         cur->planes = BYTESWAP_WORD(info.biPlanes);
514                         cur->bits = BYTESWAP_WORD(info.biBitCount);
515                         break;
516                 default:
517                         cur->planes = info.biPlanes;
518                         cur->bits = info.biBitCount;
519                 }
520                 if(!win32 && (cur->planes != 1 || cur->bits != 1))
521                         yywarning("Win16 cursor contains colors");
522                 cur->xhot = swap ? BYTESWAP_WORD(cde.xhot) : cde.xhot;
523                 cur->yhot = swap ? BYTESWAP_WORD(cde.yhot) : cde.yhot;
524                 cur->data = new_raw_data();
525                 copy_raw_data(cur->data, rd, cde.offset, cde.ressize);
526                 if(!list)
527                 {
528                         list = cur;
529                 }
530                 else
531                 {
532                         cur->next = list;
533                         list->prev = cur;
534                         list = cur;
535                 }
536         }
537         curg->cursorlist = list;
538         *ncur = cnt;
539 }
540
541
542 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
543 {
544         icon_group_t *icog = (icon_group_t *)xmalloc(sizeof(icon_group_t));
545         if(memopt)
546         {
547                 icog->memopt = *memopt;
548                 free(memopt);
549         }
550         else
551                 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
552         icog->lvc = rd->lvc;
553         split_icons(rd, icog, &(icog->nicon));
554         free(rd->data);
555         free(rd);
556         return icog;
557 }
558
559 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
560 {
561         cursor_group_t *curg = (cursor_group_t *)xmalloc(sizeof(cursor_group_t));
562         if(memopt)
563         {
564                 curg->memopt = *memopt;
565                 free(memopt);
566         }
567         else
568                 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
569         curg->lvc = rd->lvc;
570         split_cursors(rd, curg, &(curg->ncursor));
571         free(rd->data);
572         free(rd);
573         return curg;
574 }
575
576 /*
577  * Animated cursors and icons
578  *
579  * The format of animated cursors and icons is yet another example
580  * of bad design by "The Company". The entire RIFF structure is
581  * flawed by design because it is inconsistent and single minded:
582  * - some tags have lengths attached, others don't. The use of these
583  *   non-length tags is absolutely unclear;
584  * - the content of "icon" tags can be both icons and cursors;
585  * - tags lack proper alignment constraints. It seems that everything
586  *   is 16bit aligned, but I could not find that in any docu. Just be
587  *   prepared to eat anything;
588  * - there are no strict constraints on tag-nesting and the organization
589  *   is highly illogical;
590  *
591  * Anyhow, here is the basic structure:
592  * "RIFF" { dword taglength }
593  *      "ACON"                                  // What does it do?
594  *      "LIST" { dword taglength }
595  *              "INFO"                          // And what does this do?
596  *              "INAM" { dword taglength }      // Icon/cursor name
597  *                      {inam data}
598  *              "IART" { dword taglength }      // The artist
599  *                      {iart data}
600  *              "fram"                          // Is followed by "icon"s
601  *              "icon" { dword taglength }      // First frame
602  *                      { icon/cursor data }
603  *              "icon" { dword taglength }      // Second frame
604  *                      { icon/cursor data }
605  *                      ...                     // ...
606  *      "anih" { dword taglength }              // Header structure
607  *              { aniheader_t structure }
608  *      "rate" { dword taglength }              // The rate for each frame
609  *              { `steps' dwords }
610  *      "seq " { dword taglength }              // The frame blit-order
611  *              { `steps' dwords }
612  *
613  * Tag length are bytelength without the header and length field (i.e. -8).
614  * The "LIST" tag may occur several times and may encapsulate different
615  * tags. The `steps' is the number of "icon" tags found (actually the
616  * number of steps specified in the aniheader_t structure). The "seq "uence
617  * tag can be ommitted, in which case the sequence is equal to the sequence
618  * of "icon"s found in the file. Also "rate" may be ommitted, in which case
619  * the default from the aniheader_t structure is used.
620  *
621  * An animated cursor puts `.cur' formatted files into each "icon" tag,
622  * whereas animated icons contain `.ico' formatted files.
623  *
624  * Note about the code: Yes, it can be shorter/compressed. Some tags can be
625  * dealt with in the same code. However, this version shows what is going on
626  * and is better debug-able.
627  */
628 static const char riff[4] = "RIFF";
629 static const char acon[4] = "ACON";
630 static const char list[4] = "LIST";
631 static const char info[4] = "INFO";
632 static const char inam[4] = "INAM";
633 static const char iart[4] = "IART";
634 static const char fram[4] = "fram";
635 static const char icon[4] = "icon";
636 static const char anih[4] = "anih";
637 static const char rate[4] = "rate";
638 static const char seq[4]  = "seq ";
639
640 #define NEXT_TAG(p)     ((riff_tag_t *)(((char *)p) + (isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p)))
641
642 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
643 {
644         cursor_dir_entry_t *cdp;
645         cursor_header_t *chp;
646         int count;
647         int ctype;
648         int i;
649         static int once = 0;    /* This will trigger only once per file! */
650         const char *anistr = type == res_aniico ? "icon" : "cursor";
651         /* Notes:
652          * Both cursor and icon directories are similar
653          * Both cursor and icon headers are similar
654          */
655
656         chp = (cursor_header_t *)(rtp+1);
657         cdp = (cursor_dir_entry_t *)(chp+1);
658         count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
659         ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
660         chp->reserved   = BYTESWAP_WORD(chp->reserved);
661         chp->type       = BYTESWAP_WORD(chp->type);
662         chp->count      = BYTESWAP_WORD(chp->count);
663
664         if(type == res_anicur && ctype != 2 && !once)
665         {
666                 yywarning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)",
667                                 ctype,
668                                 ctype == 1 ? "icontype" : "?");
669                 once++;
670         }
671         else if(type == res_aniico && ctype != 1 && !once)
672         {
673                 yywarning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)",
674                                 ctype,
675                                 ctype == 2 ? "cursortype" : "?");
676                 once++;
677         }
678         else if(ctype != 1 && ctype != 2 && !once)
679         {
680                 yywarning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)", anistr, ctype);
681                 once++;
682         }
683
684         for(i = 0; i < count; i++)
685         {
686                 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
687                 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
688                 if(ofs > rtp->size || ofs+sze > rtp->size)
689                         yyerror("Animated %s's data corrupt", anistr);
690                 convert_bitmap((char *)chp + ofs, 0);
691                 cdp[i].xhot     = BYTESWAP_WORD(cdp->xhot);
692                 cdp[i].yhot     = BYTESWAP_WORD(cdp->yhot);
693                 cdp[i].ressize  = BYTESWAP_DWORD(cdp->ressize);
694                 cdp[i].offset   = BYTESWAP_DWORD(cdp->offset);
695         }
696 }
697
698 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
699 {
700         riff_tag_t *rtp = lst+1;        /* Skip the "LIST" tag */
701         
702         while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
703         {
704                 if(!memcmp(rtp->tag, info, sizeof(info)))
705                 {
706                         rtp = (riff_tag_t *)(((char *)rtp) + 4);
707                 }
708                 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
709                 {
710                         /* Ignore the icon/cursor name; its a string */
711                         rtp = NEXT_TAG(rtp);
712                 }
713                 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
714                 {
715                         /* Ignore the author's name; its a string */
716                         rtp = NEXT_TAG(rtp);
717                 }
718                 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
719                 {
720                         /* This should be followed by "icon"s, but we
721                          * simply ignore this because it is pure
722                          * non-information.
723                          */
724                         rtp = (riff_tag_t *)(((char *)rtp) + 4);
725                 }
726                 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
727                 {
728                         handle_ani_icon(rtp, type, isswapped);
729                         rtp = NEXT_TAG(rtp);
730                 }
731                 else
732                         internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
733                                        isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
734                                        isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
735                                        isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
736                                        isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
737
738                 /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
739                 if((DWORD)rtp & 1)
740                         ((char *)rtp)++;
741         }
742 }
743
744 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
745 {
746         ani_curico_t *ani = (ani_curico_t *)xmalloc(sizeof(ani_curico_t));
747         riff_tag_t *rtp;
748         int isswapped = 0;
749         int doswap;
750         const char *anistr = type == res_aniico ? "icon" : "cursor";
751
752         assert(!memcmp(rd->data, riff, sizeof(riff)));
753         assert(type == res_anicur || type == res_aniico);
754
755         rtp = (riff_tag_t *)rd->data;
756
757         if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
758                 isswapped = 1;
759         else if(rtp->size + 2*sizeof(DWORD) == rd->size)
760                 isswapped = 0;
761         else
762                 yyerror("Animated %s has an invalid RIFF length", anistr);
763
764         switch(byteorder)
765         {
766 #ifdef WORDS_BIGENDIAN
767         case WRC_BO_LITTLE:
768 #else
769         case WRC_BO_BIG:
770 #endif
771                 doswap = !isswapped;
772                 break;
773         default:
774                 doswap = isswapped;
775         }
776
777         /*
778          * When to swap what:
779          * isswapped | doswap |
780          * ----------+--------+---------------------------------
781          *     0     |    0   | read native; don't convert
782          *     1     |    0   | read swapped size; don't convert
783          *     0     |    1   | read native; convert
784          *     1     |    1   | read swapped size; convert
785          * Reading swapped size if necessary to calculate in native
786          * format. E.g. a little-endian source on a big-endian
787          * processor.
788          */
789         if(doswap)
790         {
791                 /* We only go through the RIFF file if we need to swap
792                  * bytes in words/dwords. Else we couldn't care less
793                  * what the file contains. This is consistent with
794                  * MS' rc.exe, which doesn't complain at all, eventhough
795                  * the fileformat might not be entirely correct.
796                  */
797                 rtp++;  /* Skip the "RIFF" tag */
798
799                 while((char *)rtp < (char *)rd->data + rd->size)
800                 {
801                         if(!memcmp(rtp->tag, acon, sizeof(acon)))
802                         {
803                                 rtp = (riff_tag_t *)(((char *)rtp) + 4);
804                         }
805                         else if(!memcmp(rtp->tag, list, sizeof(list)))
806                         {
807                                 handle_ani_list(rtp, type, isswapped);
808                                 rtp = NEXT_TAG(rtp);
809                         }
810                         else if(!memcmp(rtp->tag, anih, sizeof(anih)))
811                         {
812                                 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
813                                 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
814                                 ahp->frames     = BYTESWAP_DWORD(ahp->frames);
815                                 ahp->steps      = BYTESWAP_DWORD(ahp->steps);
816                                 ahp->cx         = BYTESWAP_DWORD(ahp->cx);
817                                 ahp->cy         = BYTESWAP_DWORD(ahp->cy);
818                                 ahp->bitcount   = BYTESWAP_DWORD(ahp->bitcount);
819                                 ahp->planes     = BYTESWAP_DWORD(ahp->planes);
820                                 ahp->rate       = BYTESWAP_DWORD(ahp->rate);
821                                 ahp->flags      = BYTESWAP_DWORD(ahp->flags);
822                                 rtp = NEXT_TAG(rtp);
823                         }
824                         else if(!memcmp(rtp->tag, rate, sizeof(rate)))
825                         {
826                                 int cnt = rtp->size / sizeof(DWORD);
827                                 DWORD *dwp = (DWORD *)(rtp+1);
828                                 int i;
829                                 for(i = 0; i < cnt; i++)
830                                         dwp[i] = BYTESWAP_DWORD(dwp[i]);
831                                 rtp = NEXT_TAG(rtp);
832                         }
833                         else if(!memcmp(rtp->tag, seq, sizeof(seq)))
834                         {
835                                 int cnt = rtp->size / sizeof(DWORD);
836                                 DWORD *dwp = (DWORD *)(rtp+1);
837                                 int i;
838                                 for(i = 0; i < cnt; i++)
839                                         dwp[i] = BYTESWAP_DWORD(dwp[i]);
840                                 rtp = NEXT_TAG(rtp);
841                         }
842                         else
843                                 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
844                                        isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
845                                        isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
846                                        isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
847                                        isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
848
849                         /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
850                         if((DWORD)rtp & 1)
851                                 ((char *)rtp)++;
852                 }
853
854                 /* We must end correctly here */
855                 if((char *)rtp != (char *)rd->data + rd->size)
856                         yyerror("Animated %s contains invalid field size(s)", anistr);
857         }
858
859         ani->data = rd;
860         if(memopt)
861         {
862                 ani->memopt = *memopt;
863                 free(memopt);
864         }
865         else
866                 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
867         return ani;
868 }
869 #undef NEXT_TAG
870
871 /* Bitmaps */
872 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
873 {
874         bitmap_t *bmp = (bitmap_t *)xmalloc(sizeof(bitmap_t));
875
876         bmp->data = rd;
877         if(memopt)
878         {
879                 bmp->memopt = *memopt;
880                 free(memopt);
881         }
882         else
883                 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
884         rd->size -= convert_bitmap(rd->data, rd->size);
885         return bmp;
886 }
887
888 ver_words_t *new_ver_words(int i)
889 {
890         ver_words_t *w = (ver_words_t *)xmalloc(sizeof(ver_words_t));
891         w->words = (WORD *)xmalloc(sizeof(WORD));
892         w->words[0] = (WORD)i;
893         w->nwords = 1;
894         return w;
895 }
896
897 ver_words_t *add_ver_words(ver_words_t *w, int i)
898 {
899         w->words = (WORD *)xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
900         w->words[w->nwords] = (WORD)i;
901         w->nwords++;
902         return w;
903 }
904
905 #define MSGTAB_BAD_PTR(p, b, l, r)      (((l) - ((char *)(p) - (char *)(b))) > (r))
906 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
907 {
908         messagetable_t *msg = (messagetable_t *)xmalloc(sizeof(messagetable_t));
909         msgtab_block_t *mbp;
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         if(!hi && lo)  /* Messagetable byteorder == native byteorder */
945         {
946 #ifdef WORDS_BIGENDIAN
947                 if(byteorder != WRC_BO_LITTLE) goto out;
948 #else
949                 if(byteorder != WRC_BO_BIG) goto out;
950 #endif
951                 /* Resource byteorder != native byteorder */
952
953                 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
954                 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
955                         yyerror("Messagetable's blocks are outside of defined data");
956                 for(i = 0; i < nblk; i++)
957                 {
958                         msgtab_entry_t *mep, *next_mep;
959                         DWORD id;
960
961                         mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
962
963                         for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
964                         {
965                                 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
966                                         yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i, id);
967                                 if(mep->flags == 1)     /* Docu says 'flags == 0x0001' for unicode */
968                                 {
969                                         WORD *wp = (WORD *)&mep[1];
970                                         int l = mep->length/2 - 2; /* Length included header */
971                                         int n;
972
973                                         if(mep->length & 1)
974                                                 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id, (int)i, mep->length);
975                                         for(n = 0; n < l; n++)
976                                                 wp[n] = BYTESWAP_WORD(wp[n]);
977                                                 
978                                 }
979                                 next_mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
980                                 mep->length = BYTESWAP_WORD(mep->length);
981                                 mep->flags  = BYTESWAP_WORD(mep->flags);
982                                 mep = next_mep;
983                         }
984
985                         mbp[i].idlo   = BYTESWAP_DWORD(mbp[i].idlo);
986                         mbp[i].idhi   = BYTESWAP_DWORD(mbp[i].idhi);
987                         mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
988                 }
989         }
990         if(hi && !lo)  /* Messagetable byteorder != native byteorder */
991         {
992 #ifdef WORDS_BIGENDIAN
993                 if(byteorder == WRC_BO_LITTLE) goto out;
994 #else
995                 if(byteorder == WRC_BO_BIG) goto out;
996 #endif
997                 /* Resource byteorder == native byteorder */
998
999                 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1000                 nblk = BYTESWAP_DWORD(nblk);
1001                 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1002                         yyerror("Messagetable's blocks are outside of defined data");
1003                 for(i = 0; i < nblk; i++)
1004                 {
1005                         msgtab_entry_t *mep;
1006                         DWORD id;
1007
1008                         mbp[i].idlo   = BYTESWAP_DWORD(mbp[i].idlo);
1009                         mbp[i].idhi   = BYTESWAP_DWORD(mbp[i].idhi);
1010                         mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1011                         mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1012
1013                         for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1014                         {
1015                                 mep->length = BYTESWAP_WORD(mep->length);
1016                                 mep->flags  = BYTESWAP_WORD(mep->flags);
1017
1018                                 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1019                                         yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i, id);
1020                                 if(mep->flags == 1)     /* Docu says 'flags == 0x0001' for unicode */
1021                                 {
1022                                         WORD *wp = (WORD *)&mep[1];
1023                                         int l = mep->length/2 - 2; /* Length included header */
1024                                         int n;
1025
1026                                         if(mep->length & 1)
1027                                                 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id, (int)i, mep->length);
1028                                         for(n = 0; n < l; n++)
1029                                                 wp[n] = BYTESWAP_WORD(wp[n]);
1030                                                 
1031                                 }
1032                                 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1033                         }
1034                 }
1035         }
1036
1037  out:
1038         return msg;
1039 }
1040 #undef MSGTAB_BAD_PTR
1041
1042 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1043 {
1044         assert(offs <= src->size);
1045         assert(offs + len <= src->size);
1046         if(!dst->data)
1047         {
1048                 dst->data = (char *)xmalloc(len);
1049                 dst->size = 0;
1050         }
1051         else
1052                 dst->data = (char *)xrealloc(dst->data, dst->size + len);
1053         /* dst->size holds the offset to copy to */
1054         memcpy(dst->data + dst->size, src->data + offs, len);
1055         dst->size += len;
1056 }
1057
1058 int *new_int(int i)
1059 {
1060         int *ip = (int *)xmalloc(sizeof(int));
1061         *ip = i;
1062         return ip;
1063 }
1064
1065 stringtable_t *new_stringtable(lvc_t *lvc)
1066 {
1067         stringtable_t *stt = (stringtable_t *)xmalloc(sizeof(stringtable_t));
1068
1069         if(lvc)
1070                 stt->lvc = *lvc;
1071
1072         return stt;
1073 }
1074
1075 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1076 {
1077         toolbar_t *tb = (toolbar_t *)xmalloc(sizeof(toolbar_t));
1078         tb->button_width = button_width;
1079         tb->button_height = button_height;
1080         tb->nitems = nitems;
1081         tb->items = items;
1082         return tb;
1083 }
1084
1085 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1086 {
1087         dlginit_t *di = (dlginit_t *)xmalloc(sizeof(dlginit_t));
1088         di->data = rd;
1089         if(memopt)
1090         {
1091                 di->memopt = *memopt;
1092                 free(memopt);
1093         }
1094         else
1095                 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1096
1097         return di;
1098 }
1099
1100 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1101 {
1102         style_pair_t *sp = (style_pair_t *)xmalloc(sizeof(style_pair_t));
1103         sp->style = style;
1104         sp->exstyle = exstyle;
1105         return sp;
1106 }
1107
1108 style_t *new_style(DWORD or_mask, DWORD and_mask)
1109 {
1110         style_t *st = (style_t *)xmalloc(sizeof(style_t));
1111         st->or_mask = or_mask;
1112         st->and_mask = and_mask;
1113         return st;
1114 }
1115