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