Release 1.5.29.
[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  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29
30 #include "wrc.h"
31 #include "newstruc.h"
32 #include "utils.h"
33 #include "parser.h"
34
35 #include "wingdi.h"     /* for BITMAPINFOHEADER */
36
37 #define ICO_PNG_MAGIC 0x474e5089
38
39 #include <pshpack2.h>
40 typedef struct
41 {
42     DWORD biSize;
43     WORD  biWidth;
44     WORD  biHeight;
45     WORD  biPlanes;
46     WORD  biBitCount;
47 } BITMAPOS2HEADER;
48 #include <poppack.h>
49
50 /* New instances for all types of structures */
51 /* Very inefficient (in size), but very functional :-]
52  * Especially for type-checking.
53  */
54
55 dialog_t *new_dialog(void)
56 {
57     dialog_t *ret = xmalloc( sizeof(*ret) );
58     memset( ret, 0, sizeof(*ret) );
59     return ret;
60 }
61
62 name_id_t *new_name_id(void)
63 {
64     name_id_t *ret = xmalloc( sizeof(*ret) );
65     memset( ret, 0, sizeof(*ret) );
66     return ret;
67 }
68
69 menu_t *new_menu(void)
70 {
71     menu_t *ret = xmalloc( sizeof(*ret) );
72     memset( ret, 0, sizeof(*ret) );
73     return ret;
74 }
75
76 menu_item_t *new_menu_item(void)
77 {
78     menu_item_t *ret = xmalloc( sizeof(*ret) );
79     memset( ret, 0, sizeof(*ret) );
80     return ret;
81 }
82
83 control_t *new_control(void)
84 {
85     control_t *ret = xmalloc( sizeof(*ret) );
86     memset( ret, 0, sizeof(*ret) );
87     return ret;
88 }
89
90 icon_t *new_icon(void)
91 {
92     icon_t *ret = xmalloc( sizeof(*ret) );
93     memset( ret, 0, sizeof(*ret) );
94     return ret;
95 }
96
97 cursor_t *new_cursor(void)
98 {
99     cursor_t *ret = xmalloc( sizeof(*ret) );
100     memset( ret, 0, sizeof(*ret) );
101     return ret;
102 }
103
104 versioninfo_t *new_versioninfo(void)
105 {
106     versioninfo_t *ret = xmalloc( sizeof(*ret) );
107     memset( ret, 0, sizeof(*ret) );
108     return ret;
109 }
110
111 ver_value_t *new_ver_value(void)
112 {
113     ver_value_t *ret = xmalloc( sizeof(*ret) );
114     memset( ret, 0, sizeof(*ret) );
115     return ret;
116 }
117
118 ver_block_t *new_ver_block(void)
119 {
120     ver_block_t *ret = xmalloc( sizeof(*ret) );
121     memset( ret, 0, sizeof(*ret) );
122     return ret;
123 }
124
125 stt_entry_t *new_stt_entry(void)
126 {
127     stt_entry_t *ret = xmalloc( sizeof(*ret) );
128     memset( ret, 0, sizeof(*ret) );
129     return ret;
130 }
131
132 accelerator_t *new_accelerator(void)
133 {
134     accelerator_t *ret = xmalloc( sizeof(*ret) );
135     memset( ret, 0, sizeof(*ret) );
136     return ret;
137 }
138
139 event_t *new_event(void)
140 {
141     event_t *ret = xmalloc( sizeof(*ret) );
142     memset( ret, 0, sizeof(*ret) );
143     return ret;
144 }
145
146 raw_data_t *new_raw_data(void)
147 {
148     raw_data_t *ret = xmalloc( sizeof(*ret) );
149     memset( ret, 0, sizeof(*ret) );
150     return ret;
151 }
152
153 lvc_t *new_lvc(void)
154 {
155     lvc_t *ret = xmalloc( sizeof(*ret) );
156     memset( ret, 0, sizeof(*ret) );
157     return ret;
158 }
159
160 res_count_t *new_res_count(void)
161 {
162     res_count_t *ret = xmalloc( sizeof(*ret) );
163     memset( ret, 0, sizeof(*ret) );
164     return ret;
165 }
166
167 string_t *new_string(void)
168 {
169     string_t *ret = xmalloc( sizeof(*ret) );
170     memset( ret, 0, sizeof(*ret) );
171     set_location( &ret->loc );
172     return ret;
173 }
174
175 toolbar_item_t *new_toolbar_item(void)
176 {
177     toolbar_item_t *ret = xmalloc( sizeof(*ret) );
178     memset( ret, 0, sizeof(*ret) );
179     return ret;
180 }
181
182 ani_any_t *new_ani_any(void)
183 {
184     ani_any_t *ret = xmalloc( sizeof(*ret) );
185     memset( ret, 0, sizeof(*ret) );
186     return ret;
187 }
188
189 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
190 {
191         resource_t *r = xmalloc(sizeof(resource_t));
192         memset( r, 0, sizeof(*r) );
193         r->type = t;
194         r->res.overlay = res;
195         r->memopt = memopt;
196         r->lan = lan;
197         return r;
198 }
199
200 version_t *new_version(DWORD v)
201 {
202         version_t *vp = xmalloc(sizeof(version_t));
203         *vp = v;
204         return vp;
205 }
206
207 characts_t *new_characts(DWORD c)
208 {
209         characts_t *cp = xmalloc(sizeof(characts_t));
210         *cp = c;
211         return cp;
212 }
213
214 language_t *new_language(int id, int sub)
215 {
216         language_t *lan = xmalloc(sizeof(language_t));
217         lan->id = id;
218         lan->sub = sub;
219         return lan;
220 }
221
222 language_t *dup_language(language_t *l)
223 {
224         if(!l) return NULL;
225         return new_language(l->id, l->sub);
226 }
227
228 version_t *dup_version(version_t *v)
229 {
230         if(!v) return NULL;
231         return new_version(*v);
232 }
233
234 characts_t *dup_characts(characts_t *c)
235 {
236         if(!c) return NULL;
237         return new_characts(*c);
238 }
239
240 html_t *new_html(raw_data_t *rd, int *memopt)
241 {
242         html_t *html = xmalloc(sizeof(html_t));
243         html->data = rd;
244         if(memopt)
245         {
246                 html->memopt = *memopt;
247                 free(memopt);
248         }
249         else
250                 html->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
251         return html;
252 }
253
254 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
255 {
256         rcdata_t *rc = xmalloc(sizeof(rcdata_t));
257         rc->data = rd;
258         if(memopt)
259         {
260                 rc->memopt = *memopt;
261                 free(memopt);
262         }
263         else
264                 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
265         return rc;
266 }
267
268 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
269 {
270         font_id_t *fid = xmalloc(sizeof(font_id_t));
271         fid->name = face;
272         fid->size = size;
273         fid->weight = weight;
274         fid->italic = italic;
275         return fid;
276 }
277
278 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
279 {
280         user_t *usr = xmalloc(sizeof(user_t));
281         usr->data = rd;
282         if(memopt)
283         {
284                 usr->memopt = *memopt;
285                 free(memopt);
286         }
287         else
288                 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
289         usr->type = type;
290         return usr;
291 }
292
293 font_t *new_font(raw_data_t *rd, int *memopt)
294 {
295         font_t *fnt = xmalloc(sizeof(font_t));
296         fnt->data = rd;
297         if(memopt)
298         {
299                 fnt->memopt = *memopt;
300                 free(memopt);
301         }
302         else
303                 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
304         return fnt;
305 }
306
307 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
308 {
309         fontdir_t *fnd = xmalloc(sizeof(fontdir_t));
310         fnd->data = rd;
311         if(memopt)
312         {
313                 fnd->memopt = *memopt;
314                 free(memopt);
315         }
316         else
317                 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
318         return fnd;
319 }
320
321
322 /*
323  * Convert bitmaps to proper endian
324  */
325 static void convert_bitmap_swap(BITMAPV5HEADER *bh, DWORD size)
326 {
327         bh->bV5Size             = BYTESWAP_DWORD(bh->bV5Size);
328         bh->bV5Width            = BYTESWAP_DWORD(bh->bV5Width);
329         bh->bV5Height           = BYTESWAP_DWORD(bh->bV5Height);
330         bh->bV5Planes           = BYTESWAP_WORD(bh->bV5Planes);
331         bh->bV5BitCount         = BYTESWAP_WORD(bh->bV5BitCount);
332         bh->bV5Compression      = BYTESWAP_DWORD(bh->bV5Compression);
333         bh->bV5SizeImage        = BYTESWAP_DWORD(bh->bV5SizeImage);
334         bh->bV5XPelsPerMeter    = BYTESWAP_DWORD(bh->bV5XPelsPerMeter);
335         bh->bV5YPelsPerMeter    = BYTESWAP_DWORD(bh->bV5YPelsPerMeter);
336         bh->bV5ClrUsed          = BYTESWAP_DWORD(bh->bV5ClrUsed);
337         bh->bV5ClrImportant     = BYTESWAP_DWORD(bh->bV5ClrImportant);
338         if (size == sizeof(BITMAPINFOHEADER)) return;
339         bh->bV5RedMask          = BYTESWAP_DWORD(bh->bV5RedMask);
340         bh->bV5GreenMask        = BYTESWAP_DWORD(bh->bV5GreenMask);
341         bh->bV5BlueMask         = BYTESWAP_DWORD(bh->bV5BlueMask);
342         bh->bV5AlphaMask        = BYTESWAP_DWORD(bh->bV5AlphaMask);
343         bh->bV5CSType           = BYTESWAP_DWORD(bh->bV5CSType);
344         bh->bV5Endpoints.ciexyzRed.ciexyzX      = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzX);
345         bh->bV5Endpoints.ciexyzRed.ciexyzY      = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzY);
346         bh->bV5Endpoints.ciexyzRed.ciexyzZ      = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzZ);
347         bh->bV5Endpoints.ciexyzGreen.ciexyzX    = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzX);
348         bh->bV5Endpoints.ciexyzGreen.ciexyzY    = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzY);
349         bh->bV5Endpoints.ciexyzGreen.ciexyzZ    = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzZ);
350         bh->bV5Endpoints.ciexyzBlue.ciexyzX     = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzX);
351         bh->bV5Endpoints.ciexyzBlue.ciexyzY     = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzY);
352         bh->bV5Endpoints.ciexyzBlue.ciexyzZ     = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzZ);
353         bh->bV5GammaRed         = BYTESWAP_DWORD(bh->bV5GammaRed);
354         bh->bV5GammaGreen       = BYTESWAP_DWORD(bh->bV5GammaGreen);
355         bh->bV5GammaBlue        = BYTESWAP_DWORD(bh->bV5GammaBlue);
356         if (size == sizeof(BITMAPV4HEADER)) return;
357         bh->bV5Intent           = BYTESWAP_DWORD(bh->bV5Intent);
358         bh->bV5ProfileData      = BYTESWAP_DWORD(bh->bV5ProfileData);
359         bh->bV5ProfileSize      = BYTESWAP_DWORD(bh->bV5ProfileSize);
360         bh->bV5Reserved         = BYTESWAP_DWORD(bh->bV5Reserved);
361 }
362
363 static void convert_bitmap_swap_os2(BITMAPOS2HEADER *boh)
364 {
365         boh->biSize             = BYTESWAP_DWORD(boh->biSize);
366         boh->biWidth            = BYTESWAP_WORD(boh->biWidth);
367         boh->biHeight           = BYTESWAP_WORD(boh->biHeight);
368         boh->biPlanes           = BYTESWAP_WORD(boh->biPlanes);
369         boh->biBitCount         = BYTESWAP_WORD(boh->biBitCount);
370 }
371
372 #define FL_SIGBE        0x01
373 #define FL_SIZEBE       0x02
374 static int convert_bitmap(char *data, int size)
375 {
376         BITMAPV5HEADER *bih = (BITMAPV5HEADER *)data;
377         BITMAPOS2HEADER *boh = (BITMAPOS2HEADER *)data;
378         DWORD bmsize;
379         int type = 0;
380         int returnSize = 0;           /* size to be returned */
381
382     /*
383      * Originally the bih and b4h pointers were simply incremented here,
384      * and memmoved at the end of the function.  This causes alignment
385      * issues on solaris, so we do the memmove here rather than at the end.
386      */
387         if(data[0] == 'B' && data[1] == 'M')
388         {
389                 /* Little endian signature */
390          memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
391          returnSize = sizeof(BITMAPFILEHEADER);
392         }
393         else if(data[0] == 'M' && data[1] == 'B')
394         {
395                 type |= FL_SIGBE;       /* Big endian signature */
396          memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
397          returnSize = sizeof(BITMAPFILEHEADER);
398
399         }
400
401         bmsize = bih->bV5Size;
402         switch (bmsize)
403         {
404         case sizeof(BITMAPOS2HEADER):
405         case sizeof(BITMAPINFOHEADER):
406         case sizeof(BITMAPV4HEADER):
407         case sizeof(BITMAPV5HEADER):
408 #ifdef WORDS_BIGENDIAN
409             type |= FL_SIZEBE;
410 #endif
411             break;
412         case BYTESWAP_DWORD( sizeof(BITMAPOS2HEADER) ):
413         case BYTESWAP_DWORD( sizeof(BITMAPINFOHEADER) ):
414         case BYTESWAP_DWORD( sizeof(BITMAPV4HEADER) ):
415         case BYTESWAP_DWORD( sizeof(BITMAPV5HEADER) ):
416 #ifndef WORDS_BIGENDIAN
417             type |= FL_SIZEBE;
418 #endif
419             bmsize = BYTESWAP_DWORD( bmsize );
420             break;
421         case ICO_PNG_MAGIC:
422         case BYTESWAP_DWORD( ICO_PNG_MAGIC ):
423             return 0;  /* nothing to convert */
424         default:
425                 parser_error("Invalid bitmap format, bih->biSize = %d", bih->bV5Size);
426         }
427
428         switch(type)
429         {
430         case FL_SIZEBE:
431             parser_warning("Bitmap signature little-endian, but size big-endian\n");
432             break;
433         case FL_SIGBE:
434             parser_warning("Bitmap signature big-endian, but size little-endian\n");
435             break;
436         }
437
438         switch(byteorder)
439         {
440 #ifdef WORDS_BIGENDIAN
441         default:
442 #endif
443         case WRC_BO_BIG:
444                 if(!(type & FL_SIZEBE))
445                 {
446                     if (bmsize == sizeof(BITMAPOS2HEADER))
447                         convert_bitmap_swap_os2(boh);
448                     else
449                         convert_bitmap_swap(bih, bmsize);
450                 }
451                 break;
452 #ifndef WORDS_BIGENDIAN
453         default:
454 #endif
455         case WRC_BO_LITTLE:
456                 if(type & FL_SIZEBE)
457                 {
458                     if (bmsize == sizeof(BITMAPOS2HEADER))
459                         convert_bitmap_swap_os2(boh);
460                     else
461                         convert_bitmap_swap(bih, bmsize);
462                 }
463                 break;
464         }
465
466         if(size && (void *)data != (void *)bih)
467         {
468                 /* We have the fileheader still attached, remove it */
469                 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
470                 return sizeof(BITMAPFILEHEADER);
471         }
472     return returnSize;
473 }
474 #undef FL_SIGBE
475 #undef FL_SIZEBE
476
477 /*
478  * Cursor and icon splitter functions used when allocating
479  * cursor- and icon-groups.
480  */
481 typedef struct {
482         language_t      lan;
483         int             id;
484 } id_alloc_t;
485
486 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
487 {
488         int i;
489         assert(lan != NULL);
490         assert(list != NULL);
491         assert(n != NULL);
492
493         if(!*list)
494         {
495                 *list = xmalloc(sizeof(id_alloc_t));
496                 *n = 1;
497                 (*list)[0].lan = *lan;
498                 (*list)[0].id = 1;
499                 return 1;
500         }
501
502         for(i = 0; i < *n; i++)
503         {
504                 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
505                         return ++((*list)[i].id);
506         }
507
508         *list = xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
509         (*list)[*n].lan = *lan;
510         (*list)[*n].id = 1;
511         *n += 1;
512         return 1;
513 }
514
515 static int alloc_icon_id(language_t *lan)
516 {
517         static id_alloc_t *idlist = NULL;
518         static int nid = 0;
519
520         return get_new_id(&idlist, &nid, lan);
521 }
522
523 static int alloc_cursor_id(language_t *lan)
524 {
525         static id_alloc_t *idlist = NULL;
526         static int nid = 0;
527
528         return get_new_id(&idlist, &nid, lan);
529 }
530
531 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
532 {
533         int cnt;
534         int i;
535         icon_t *ico;
536         icon_t *list = NULL;
537         icon_header_t *ih = (icon_header_t *)rd->data;
538         int swap = 0;
539
540         if(ih->type == 1)
541                 swap = 0;
542         else if(BYTESWAP_WORD(ih->type) == 1)
543                 swap = 1;
544         else
545                 parser_error("Icon resource data has invalid type id %d", ih->type);
546
547         cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
548         for(i = 0; i < cnt; i++)
549         {
550                 icon_dir_entry_t ide;
551                 BITMAPINFOHEADER info;
552                 memcpy(&ide, rd->data + sizeof(icon_header_t)
553                                       + i*sizeof(icon_dir_entry_t), sizeof(ide));
554
555                 ico = new_icon();
556                 ico->id = alloc_icon_id(icog->lvc.language);
557                 ico->lvc = icog->lvc;
558                 if(swap)
559                 {
560                         ide.offset = BYTESWAP_DWORD(ide.offset);
561                         ide.ressize= BYTESWAP_DWORD(ide.ressize);
562                 }
563                 if(ide.offset > rd->size
564                 || ide.offset + ide.ressize > rd->size)
565                         parser_error("Icon resource data corrupt");
566                 ico->width = ide.width;
567                 ico->height = ide.height;
568                 ico->nclr = ide.nclr;
569                 ico->planes = swap ? BYTESWAP_WORD(ide.planes) : ide.planes;
570                 ico->bits = swap ? BYTESWAP_WORD(ide.bits) : ide.bits;
571                 memcpy(&info, rd->data + ide.offset, sizeof(info));
572          convert_bitmap((char *) &info, 0);
573          memcpy(rd->data + ide.offset, &info, sizeof(info));
574
575                 if(!ico->planes)
576                 {
577                         /* Argh! They did not fill out the resdir structure */
578                         /* The bitmap is in destination byteorder. We want native for our structures */
579                         switch(byteorder)
580                         {
581 #ifdef WORDS_BIGENDIAN
582                         case WRC_BO_LITTLE:
583 #else
584                         case WRC_BO_BIG:
585 #endif
586                                 ico->planes = BYTESWAP_WORD(info.biPlanes);
587                                 break;
588                         default:
589                                 ico->planes = info.biPlanes;
590                         }
591                 }
592                 if(!ico->bits)
593                 {
594                         /* Argh! They did not fill out the resdir structure */
595                         /* The bitmap is in destination byteorder. We want native for our structures */
596                         switch(byteorder)
597                         {
598 #ifdef WORDS_BIGENDIAN
599                         case WRC_BO_LITTLE:
600 #else
601                         case WRC_BO_BIG:
602 #endif
603                                 ico->bits = BYTESWAP_WORD(info.biBitCount);
604                                 break;
605                         default:
606                                 ico->bits = info.biBitCount;
607                         }
608                 }
609                 ico->data = new_raw_data();
610                 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
611                 if(!list)
612                 {
613                         list = ico;
614                 }
615                 else
616                 {
617                         ico->next = list;
618                         list->prev = ico;
619                         list = ico;
620                 }
621         }
622         icog->iconlist = list;
623         *nico = cnt;
624 }
625
626 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
627 {
628         int cnt;
629         int i;
630         cursor_t *cur;
631         cursor_t *list = NULL;
632         cursor_header_t *ch = (cursor_header_t *)rd->data;
633         int swap = 0;
634
635         if(ch->type == 2)
636                 swap = 0;
637         else if(BYTESWAP_WORD(ch->type) == 2)
638                 swap = 1;
639         else
640                 parser_error("Cursor resource data has invalid type id %d", ch->type);
641         cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
642         for(i = 0; i < cnt; i++)
643         {
644                 cursor_dir_entry_t cde;
645                 BITMAPINFOHEADER info;
646                 memcpy(&cde, rd->data + sizeof(cursor_header_t)
647                                       + i*sizeof(cursor_dir_entry_t), sizeof(cde));
648
649                 cur = new_cursor();
650                 cur->id = alloc_cursor_id(curg->lvc.language);
651                 cur->lvc = curg->lvc;
652                 if(swap)
653                 {
654                         cde.offset = BYTESWAP_DWORD(cde.offset);
655                         cde.ressize= BYTESWAP_DWORD(cde.ressize);
656                 }
657                 if(cde.offset > rd->size
658                 || cde.offset + cde.ressize > rd->size)
659                         parser_error("Cursor resource data corrupt");
660                 cur->width = cde.width;
661                 cur->height = cde.height;
662                 cur->nclr = cde.nclr;
663                 memcpy(&info, rd->data + cde.offset, sizeof(info));
664          convert_bitmap((char *)&info, 0);
665          memcpy(rd->data + cde.offset, &info, sizeof(info));
666                 /* The bitmap is in destination byteorder. We want native for our structures */
667                 switch(byteorder)
668                 {
669 #ifdef WORDS_BIGENDIAN
670                 case WRC_BO_LITTLE:
671 #else
672                 case WRC_BO_BIG:
673 #endif
674                         cur->planes = BYTESWAP_WORD(info.biPlanes);
675                         cur->bits = BYTESWAP_WORD(info.biBitCount);
676                         break;
677                 default:
678                         cur->planes = info.biPlanes;
679                         cur->bits = info.biBitCount;
680                 }
681                 if(!win32 && (cur->planes != 1 || cur->bits != 1))
682                         parser_warning("Win16 cursor contains colors\n");
683                 cur->xhot = swap ? BYTESWAP_WORD(cde.xhot) : cde.xhot;
684                 cur->yhot = swap ? BYTESWAP_WORD(cde.yhot) : cde.yhot;
685                 cur->data = new_raw_data();
686                 copy_raw_data(cur->data, rd, cde.offset, cde.ressize);
687                 if(!list)
688                 {
689                         list = cur;
690                 }
691                 else
692                 {
693                         cur->next = list;
694                         list->prev = cur;
695                         list = cur;
696                 }
697         }
698         curg->cursorlist = list;
699         *ncur = cnt;
700 }
701
702
703 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
704 {
705         icon_group_t *icog = xmalloc(sizeof(icon_group_t));
706         if(memopt)
707         {
708                 icog->memopt = *memopt;
709                 free(memopt);
710         }
711         else
712                 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
713         icog->lvc = rd->lvc;
714         split_icons(rd, icog, &(icog->nicon));
715         free(rd->data);
716         free(rd);
717         return icog;
718 }
719
720 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
721 {
722         cursor_group_t *curg = xmalloc(sizeof(cursor_group_t));
723         if(memopt)
724         {
725                 curg->memopt = *memopt;
726                 free(memopt);
727         }
728         else
729                 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
730         curg->lvc = rd->lvc;
731         split_cursors(rd, curg, &(curg->ncursor));
732         free(rd->data);
733         free(rd);
734         return curg;
735 }
736
737 /*
738  * Animated cursors and icons
739  *
740  * The format of animated cursors and icons is yet another example
741  * of bad design by "The Company". The entire RIFF structure is
742  * flawed by design because it is inconsistent and single minded:
743  * - some tags have lengths attached, others don't. The use of these
744  *   non-length tags is absolutely unclear;
745  * - the content of "icon" tags can be both icons and cursors;
746  * - tags lack proper alignment constraints. It seems that everything
747  *   is 16bit aligned, but I could not find that in any docu. Just be
748  *   prepared to eat anything;
749  * - there are no strict constraints on tag-nesting and the organization
750  *   is highly illogical;
751  *
752  * Anyhow, here is the basic structure:
753  * "RIFF" { dword taglength }
754  *      "ACON"                                  // What does it do?
755  *      "LIST" { dword taglength }
756  *              "INFO"                          // And what does this do?
757  *              "INAM" { dword taglength }      // Icon/cursor name
758  *                      {inam data}
759  *              "IART" { dword taglength }      // The artist
760  *                      {iart data}
761  *              "fram"                          // Is followed by "icon"s
762  *              "icon" { dword taglength }      // First frame
763  *                      { icon/cursor data }
764  *              "icon" { dword taglength }      // Second frame
765  *                      { icon/cursor data }
766  *                      ...                     // ...
767  *      "anih" { dword taglength }              // Header structure
768  *              { aniheader_t structure }
769  *      "rate" { dword taglength }              // The rate for each frame
770  *              { `steps' dwords }
771  *      "seq " { dword taglength }              // The frame blit-order
772  *              { `steps' dwords }
773  *
774  * Tag length are bytelength without the header and length field (i.e. -8).
775  * The "LIST" tag may occur several times and may encapsulate different
776  * tags. The `steps' is the number of "icon" tags found (actually the
777  * number of steps specified in the aniheader_t structure). The "seq "uence
778  * tag can be omitted, in which case the sequence is equal to the sequence
779  * of "icon"s found in the file. Also "rate" may be omitted, in which case
780  * the default from the aniheader_t structure is used.
781  *
782  * An animated cursor puts `.cur' formatted files into each "icon" tag,
783  * whereas animated icons contain `.ico' formatted files.
784  *
785  * Note about the code: Yes, it can be shorter/compressed. Some tags can be
786  * dealt with in the same code. However, this version shows what is going on
787  * and is better debug-able.
788  */
789 static const char riff[4] = "RIFF";
790 static const char acon[4] = "ACON";
791 static const char list[4] = "LIST";
792 static const char info[4] = "INFO";
793 static const char inam[4] = "INAM";
794 static const char iart[4] = "IART";
795 static const char fram[4] = "fram";
796 static const char icon[4] = "icon";
797 static const char anih[4] = "anih";
798 static const char rate[4] = "rate";
799 static const char seq[4]  = "seq ";
800
801 #define SKIP_TAG(p,size)        ((riff_tag_t *)(((char *)p) + (size)))
802
803 #define NEXT_TAG(p)     SKIP_TAG(p,(isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p))
804
805 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
806 {
807         cursor_dir_entry_t *cdp;
808         cursor_header_t *chp;
809         int count;
810         int ctype;
811         int i;
812         static int once = 0;    /* This will trigger only once per file! */
813         const char *anistr = type == res_aniico ? "icon" : "cursor";
814         /* Notes:
815          * Both cursor and icon directories are similar
816          * Both cursor and icon headers are similar
817          */
818
819         chp = (cursor_header_t *)(rtp+1);
820         cdp = (cursor_dir_entry_t *)(chp+1);
821         count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
822         ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
823         chp->reserved   = BYTESWAP_WORD(chp->reserved);
824         chp->type       = BYTESWAP_WORD(chp->type);
825         chp->count      = BYTESWAP_WORD(chp->count);
826
827         if(type == res_anicur && ctype != 2 && !once)
828         {
829                 parser_warning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)\n",
830                                 ctype,
831                                 ctype == 1 ? "icontype" : "?");
832                 once++;
833         }
834         else if(type == res_aniico && ctype != 1 && !once)
835         {
836                 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)\n",
837                                 ctype,
838                                 ctype == 2 ? "cursortype" : "?");
839                 once++;
840         }
841         else if(ctype != 1 && ctype != 2 && !once)
842         {
843                 parser_warning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)\n", anistr, ctype);
844                 once++;
845         }
846
847         for(i = 0; i < count; i++)
848         {
849                 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
850                 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
851                 if(ofs > rtp->size || ofs+sze > rtp->size)
852                         parser_error("Animated %s's data corrupt", anistr);
853                 convert_bitmap((char *)chp + ofs, 0);
854                 cdp[i].xhot     = BYTESWAP_WORD(cdp->xhot);
855                 cdp[i].yhot     = BYTESWAP_WORD(cdp->yhot);
856                 cdp[i].ressize  = BYTESWAP_DWORD(cdp->ressize);
857                 cdp[i].offset   = BYTESWAP_DWORD(cdp->offset);
858         }
859 }
860
861 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
862 {
863         riff_tag_t *rtp = lst+1;        /* Skip the "LIST" tag */
864
865         while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
866         {
867                 if(!memcmp(rtp->tag, info, sizeof(info)))
868                 {
869                         rtp = SKIP_TAG(rtp,4);
870                 }
871                 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
872                 {
873                         /* Ignore the icon/cursor name; its a string */
874                         rtp = NEXT_TAG(rtp);
875                 }
876                 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
877                 {
878                         /* Ignore the author's name; it's a string */
879                         rtp = NEXT_TAG(rtp);
880                 }
881                 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
882                 {
883                         /* This should be followed by "icon"s, but we
884                          * simply ignore this because it is pure
885                          * non-information.
886                          */
887                         rtp = SKIP_TAG(rtp,4);
888                 }
889                 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
890                 {
891                         handle_ani_icon(rtp, type, isswapped);
892                         rtp = NEXT_TAG(rtp);
893                 }
894                 else
895                         internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
896                                        isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
897                                        isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
898                                        isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
899                                        isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
900
901                 if((UINT_PTR)rtp & 1)
902                         rtp = SKIP_TAG(rtp,1);
903         }
904 }
905
906 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
907 {
908         ani_curico_t *ani = xmalloc(sizeof(ani_curico_t));
909         riff_tag_t *rtp;
910         int isswapped = 0;
911         int doswap;
912         const char *anistr = type == res_aniico ? "icon" : "cursor";
913
914         assert(!memcmp(rd->data, riff, sizeof(riff)));
915         assert(type == res_anicur || type == res_aniico);
916
917         rtp = (riff_tag_t *)rd->data;
918
919         if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
920                 isswapped = 1;
921         else if(rtp->size + 2*sizeof(DWORD) == rd->size)
922                 isswapped = 0;
923         else
924                 parser_error("Animated %s has an invalid RIFF length", anistr);
925
926         switch(byteorder)
927         {
928 #ifdef WORDS_BIGENDIAN
929         case WRC_BO_LITTLE:
930 #else
931         case WRC_BO_BIG:
932 #endif
933                 doswap = !isswapped;
934                 break;
935         default:
936                 doswap = isswapped;
937         }
938
939         /*
940          * When to swap what:
941          * isswapped | doswap |
942          * ----------+--------+---------------------------------
943          *     0     |    0   | read native; don't convert
944          *     1     |    0   | read swapped size; don't convert
945          *     0     |    1   | read native; convert
946          *     1     |    1   | read swapped size; convert
947          * Reading swapped size if necessary to calculate in native
948          * format. E.g. a little-endian source on a big-endian
949          * processor.
950          */
951         if(doswap)
952         {
953                 /* We only go through the RIFF file if we need to swap
954                  * bytes in words/dwords. Else we couldn't care less
955                  * what the file contains. This is consistent with
956                  * MS' rc.exe, which doesn't complain at all, even though
957                  * the file format might not be entirely correct.
958                  */
959                 rtp++;  /* Skip the "RIFF" tag */
960
961                 while((char *)rtp < (char *)rd->data + rd->size)
962                 {
963                         if(!memcmp(rtp->tag, acon, sizeof(acon)))
964                         {
965                                 rtp = SKIP_TAG(rtp,4);
966                         }
967                         else if(!memcmp(rtp->tag, list, sizeof(list)))
968                         {
969                                 handle_ani_list(rtp, type, isswapped);
970                                 rtp = NEXT_TAG(rtp);
971                         }
972                         else if(!memcmp(rtp->tag, anih, sizeof(anih)))
973                         {
974                                 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
975                                 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
976                                 ahp->frames     = BYTESWAP_DWORD(ahp->frames);
977                                 ahp->steps      = BYTESWAP_DWORD(ahp->steps);
978                                 ahp->cx         = BYTESWAP_DWORD(ahp->cx);
979                                 ahp->cy         = BYTESWAP_DWORD(ahp->cy);
980                                 ahp->bitcount   = BYTESWAP_DWORD(ahp->bitcount);
981                                 ahp->planes     = BYTESWAP_DWORD(ahp->planes);
982                                 ahp->rate       = BYTESWAP_DWORD(ahp->rate);
983                                 ahp->flags      = BYTESWAP_DWORD(ahp->flags);
984                                 rtp = NEXT_TAG(rtp);
985                         }
986                         else if(!memcmp(rtp->tag, rate, sizeof(rate)))
987                         {
988                                 int cnt = rtp->size / sizeof(DWORD);
989                                 DWORD *dwp = (DWORD *)(rtp+1);
990                                 int i;
991                                 for(i = 0; i < cnt; i++)
992                                         dwp[i] = BYTESWAP_DWORD(dwp[i]);
993                                 rtp = NEXT_TAG(rtp);
994                         }
995                         else if(!memcmp(rtp->tag, seq, sizeof(seq)))
996                         {
997                                 int cnt = rtp->size / sizeof(DWORD);
998                                 DWORD *dwp = (DWORD *)(rtp+1);
999                                 int i;
1000                                 for(i = 0; i < cnt; i++)
1001                                         dwp[i] = BYTESWAP_DWORD(dwp[i]);
1002                                 rtp = NEXT_TAG(rtp);
1003                         }
1004                         else
1005                                 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
1006                                        isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
1007                                        isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
1008                                        isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
1009                                        isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
1010
1011                         if((UINT_PTR)rtp & 1)
1012                                 rtp = SKIP_TAG(rtp,1);
1013                 }
1014
1015                 /* We must end correctly here */
1016                 if((char *)rtp != (char *)rd->data + rd->size)
1017                         parser_error("Animated %s contains invalid field size(s)", anistr);
1018         }
1019
1020         ani->data = rd;
1021         if(memopt)
1022         {
1023                 ani->memopt = *memopt;
1024                 free(memopt);
1025         }
1026         else
1027                 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
1028         return ani;
1029 }
1030 #undef NEXT_TAG
1031
1032 /* Bitmaps */
1033 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
1034 {
1035         bitmap_t *bmp = xmalloc(sizeof(bitmap_t));
1036
1037         bmp->data = rd;
1038         if(memopt)
1039         {
1040                 bmp->memopt = *memopt;
1041                 free(memopt);
1042         }
1043         else
1044                 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1045         rd->size -= convert_bitmap(rd->data, rd->size);
1046         return bmp;
1047 }
1048
1049 ver_words_t *new_ver_words(int i)
1050 {
1051         ver_words_t *w = xmalloc(sizeof(ver_words_t));
1052         w->words = xmalloc(sizeof(WORD));
1053         w->words[0] = (WORD)i;
1054         w->nwords = 1;
1055         return w;
1056 }
1057
1058 ver_words_t *add_ver_words(ver_words_t *w, int i)
1059 {
1060         w->words = xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
1061         w->words[w->nwords] = (WORD)i;
1062         w->nwords++;
1063         return w;
1064 }
1065
1066 #define MSGTAB_BAD_PTR(p, b, l, r)      (((l) - ((char *)(p) - (char *)(b))) > (r))
1067 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
1068 {
1069         messagetable_t *msg = xmalloc(sizeof(messagetable_t));
1070         msgtab_block_t *mbp;
1071         DWORD nblk;
1072         DWORD i;
1073         WORD lo;
1074         WORD hi;
1075
1076         msg->data = rd;
1077         if(memopt)
1078         {
1079                 msg->memopt = *memopt;
1080                 free(memopt);
1081         }
1082         else
1083                 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1084
1085         if(rd->size < sizeof(DWORD))
1086                 parser_error("Invalid messagetable, size too small");
1087
1088         nblk = *(DWORD *)rd->data;
1089         lo = WRC_LOWORD(nblk);
1090         hi = WRC_HIWORD(nblk);
1091
1092         /* FIXME:
1093          * This test will fail for all n*2^16 blocks in the messagetable.
1094          * However, no sane person would want to have so many blocks
1095          * and have a table of megabytes attached.
1096          * So, I will assume that we have less than 2^16 blocks in the table
1097          * and all will just work out fine. Otherwise, we would need to test
1098          * the ID, offset and length (and flag) fields to be very sure.
1099          */
1100         if(hi && lo)
1101                 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian\n");
1102         if(!hi && !lo)
1103                 parser_error("Invalid messagetable block count 0");
1104
1105         if(!hi && lo)  /* Messagetable byteorder == native byteorder */
1106         {
1107 #ifdef WORDS_BIGENDIAN
1108                 if(byteorder != WRC_BO_LITTLE) goto out;
1109 #else
1110                 if(byteorder != WRC_BO_BIG) goto out;
1111 #endif
1112                 /* Resource byteorder != native byteorder */
1113
1114                 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1115                 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1116                         parser_error("Messagetable's blocks are outside of defined data");
1117                 for(i = 0; i < nblk; i++)
1118                 {
1119                         msgtab_entry_t *mep, *next_mep;
1120                         DWORD id;
1121
1122                         mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1123
1124                         for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1125                         {
1126                                 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1127                                         parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1128                                 if(mep->flags == 1)     /* Docu says 'flags == 0x0001' for unicode */
1129                                 {
1130                                         WORD *wp = (WORD *)&mep[1];
1131                                         int l = mep->length/2 - 2; /* Length included header */
1132                                         int n;
1133
1134                                         if(mep->length & 1)
1135                                                 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1136                                         for(n = 0; n < l; n++)
1137                                                 wp[n] = BYTESWAP_WORD(wp[n]);
1138
1139                                 }
1140                                 next_mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1141                                 mep->length = BYTESWAP_WORD(mep->length);
1142                                 mep->flags  = BYTESWAP_WORD(mep->flags);
1143                                 mep = next_mep;
1144                         }
1145
1146                         mbp[i].idlo   = BYTESWAP_DWORD(mbp[i].idlo);
1147                         mbp[i].idhi   = BYTESWAP_DWORD(mbp[i].idhi);
1148                         mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1149                 }
1150         }
1151         if(hi && !lo)  /* Messagetable byteorder != native byteorder */
1152         {
1153 #ifdef WORDS_BIGENDIAN
1154                 if(byteorder == WRC_BO_LITTLE) goto out;
1155 #else
1156                 if(byteorder == WRC_BO_BIG) goto out;
1157 #endif
1158                 /* Resource byteorder == native byteorder */
1159
1160                 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1161                 nblk = BYTESWAP_DWORD(nblk);
1162                 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1163                         parser_error("Messagetable's blocks are outside of defined data");
1164                 for(i = 0; i < nblk; i++)
1165                 {
1166                         msgtab_entry_t *mep;
1167                         DWORD id;
1168
1169                         mbp[i].idlo   = BYTESWAP_DWORD(mbp[i].idlo);
1170                         mbp[i].idhi   = BYTESWAP_DWORD(mbp[i].idhi);
1171                         mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1172                         mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1173
1174                         for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1175                         {
1176                                 mep->length = BYTESWAP_WORD(mep->length);
1177                                 mep->flags  = BYTESWAP_WORD(mep->flags);
1178
1179                                 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1180                                         parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1181                                 if(mep->flags == 1)     /* Docu says 'flags == 0x0001' for unicode */
1182                                 {
1183                                         WORD *wp = (WORD *)&mep[1];
1184                                         int l = mep->length/2 - 2; /* Length included header */
1185                                         int n;
1186
1187                                         if(mep->length & 1)
1188                                                 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1189                                         for(n = 0; n < l; n++)
1190                                                 wp[n] = BYTESWAP_WORD(wp[n]);
1191
1192                                 }
1193                                 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1194                         }
1195                 }
1196         }
1197
1198  out:
1199         return msg;
1200 }
1201 #undef MSGTAB_BAD_PTR
1202
1203 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1204 {
1205         assert(offs <= src->size);
1206         assert(offs + len <= src->size);
1207         if(!dst->data)
1208         {
1209                 dst->data = xmalloc(len);
1210                 dst->size = 0;
1211         }
1212         else
1213                 dst->data = xrealloc(dst->data, dst->size + len);
1214         /* dst->size holds the offset to copy to */
1215         memcpy(dst->data + dst->size, src->data + offs, len);
1216         dst->size += len;
1217 }
1218
1219 int *new_int(int i)
1220 {
1221         int *ip = xmalloc(sizeof(int));
1222         *ip = i;
1223         return ip;
1224 }
1225
1226 stringtable_t *new_stringtable(lvc_t *lvc)
1227 {
1228         stringtable_t *stt = xmalloc(sizeof(stringtable_t));
1229
1230         memset( stt, 0, sizeof(*stt) );
1231         if(lvc)
1232                 stt->lvc = *lvc;
1233
1234         return stt;
1235 }
1236
1237 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1238 {
1239         toolbar_t *tb = xmalloc(sizeof(toolbar_t));
1240         memset( tb, 0, sizeof(*tb) );
1241         tb->button_width = button_width;
1242         tb->button_height = button_height;
1243         tb->nitems = nitems;
1244         tb->items = items;
1245         return tb;
1246 }
1247
1248 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1249 {
1250         dlginit_t *di = xmalloc(sizeof(dlginit_t));
1251         di->data = rd;
1252         if(memopt)
1253         {
1254                 di->memopt = *memopt;
1255                 free(memopt);
1256         }
1257         else
1258                 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1259
1260         return di;
1261 }
1262
1263 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1264 {
1265         style_pair_t *sp = xmalloc(sizeof(style_pair_t));
1266         sp->style = style;
1267         sp->exstyle = exstyle;
1268         return sp;
1269 }
1270
1271 style_t *new_style(DWORD or_mask, DWORD and_mask)
1272 {
1273         style_t *st = xmalloc(sizeof(style_t));
1274         st->or_mask = or_mask;
1275         st->and_mask = and_mask;
1276         return st;
1277 }