advapi32/tests: Fix test failures on Vista and higher for multiple runs.
[wine/hramrach.git] / tools / wrc / newstruc.c
blobec9abc4bcd4e88c443e1e2429bf15f72484485c2
1 /*
2 * Create dynamic new structures of various types
3 * and some utils in that trend.
5 * Copyright 1998 Bertho A. Stultiens
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.
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.
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
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
30 #include "wrc.h"
31 #include "newstruc.h"
32 #include "utils.h"
33 #include "parser.h"
35 #include "wingdi.h" /* for BITMAPINFOHEADER */
37 #include <pshpack2.h>
38 typedef struct
40 DWORD biSize;
41 WORD biWidth;
42 WORD biHeight;
43 WORD biPlanes;
44 WORD biBitCount;
45 } BITMAPOS2HEADER;
46 #include <poppack.h>
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)
72 /* New instances for all types of structures */
73 /* Very inefficient (in size), but very functional :-]
74 * Especially for type-checking.
76 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
78 resource_t *r = xmalloc(sizeof(resource_t));
79 memset( r, 0, sizeof(*r) );
80 r->type = t;
81 r->res.overlay = res;
82 r->memopt = memopt;
83 r->lan = lan;
84 return r;
87 version_t *new_version(DWORD v)
89 version_t *vp = xmalloc(sizeof(version_t));
90 *vp = v;
91 return vp;
94 characts_t *new_characts(DWORD c)
96 characts_t *cp = xmalloc(sizeof(characts_t));
97 *cp = c;
98 return cp;
101 language_t *new_language(int id, int sub)
103 language_t *lan = xmalloc(sizeof(language_t));
104 lan->id = id;
105 lan->sub = sub;
106 return lan;
109 language_t *dup_language(language_t *l)
111 if(!l) return NULL;
112 return new_language(l->id, l->sub);
115 version_t *dup_version(version_t *v)
117 if(!v) return NULL;
118 return new_version(*v);
121 characts_t *dup_characts(characts_t *c)
123 if(!c) return NULL;
124 return new_characts(*c);
127 html_t *new_html(raw_data_t *rd, int *memopt)
129 html_t *html = xmalloc(sizeof(html_t));
130 html->data = rd;
131 if(memopt)
133 html->memopt = *memopt;
134 free(memopt);
136 else
137 html->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
138 return html;
141 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
143 rcdata_t *rc = xmalloc(sizeof(rcdata_t));
144 rc->data = rd;
145 if(memopt)
147 rc->memopt = *memopt;
148 free(memopt);
150 else
151 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
152 return rc;
155 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
157 font_id_t *fid = xmalloc(sizeof(font_id_t));
158 fid->name = face;
159 fid->size = size;
160 fid->weight = weight;
161 fid->italic = italic;
162 return fid;
165 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
167 user_t *usr = xmalloc(sizeof(user_t));
168 usr->data = rd;
169 if(memopt)
171 usr->memopt = *memopt;
172 free(memopt);
174 else
175 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
176 usr->type = type;
177 return usr;
180 font_t *new_font(raw_data_t *rd, int *memopt)
182 font_t *fnt = xmalloc(sizeof(font_t));
183 fnt->data = rd;
184 if(memopt)
186 fnt->memopt = *memopt;
187 free(memopt);
189 else
190 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
191 return fnt;
194 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
196 fontdir_t *fnd = xmalloc(sizeof(fontdir_t));
197 fnd->data = rd;
198 if(memopt)
200 fnd->memopt = *memopt;
201 free(memopt);
203 else
204 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
205 return fnd;
210 * Convert bitmaps to proper endian
212 static void convert_bitmap_swap_v3(BITMAPINFOHEADER *bih)
214 bih->biSize = BYTESWAP_DWORD(bih->biSize);
215 bih->biWidth = BYTESWAP_DWORD(bih->biWidth);
216 bih->biHeight = BYTESWAP_DWORD(bih->biHeight);
217 bih->biPlanes = BYTESWAP_WORD(bih->biPlanes);
218 bih->biBitCount = BYTESWAP_WORD(bih->biBitCount);
219 bih->biCompression = BYTESWAP_DWORD(bih->biCompression);
220 bih->biSizeImage = BYTESWAP_DWORD(bih->biSizeImage);
221 bih->biXPelsPerMeter = BYTESWAP_DWORD(bih->biXPelsPerMeter);
222 bih->biYPelsPerMeter = BYTESWAP_DWORD(bih->biYPelsPerMeter);
223 bih->biClrUsed = BYTESWAP_DWORD(bih->biClrUsed);
224 bih->biClrImportant = BYTESWAP_DWORD(bih->biClrImportant);
227 static void convert_bitmap_swap_v4(BITMAPV4HEADER *b4h)
229 convert_bitmap_swap_v3((BITMAPINFOHEADER *)b4h);
230 b4h->bV4RedMask = BYTESWAP_DWORD(b4h->bV4RedMask);
231 b4h->bV4GreenMask = BYTESWAP_DWORD(b4h->bV4GreenMask);
232 b4h->bV4BlueMask = BYTESWAP_DWORD(b4h->bV4BlueMask);
233 b4h->bV4AlphaMask = BYTESWAP_DWORD(b4h->bV4AlphaMask);
234 b4h->bV4CSType = BYTESWAP_DWORD(b4h->bV4CSType);
235 b4h->bV4Endpoints.ciexyzRed.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzX);
236 b4h->bV4Endpoints.ciexyzRed.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzY);
237 b4h->bV4Endpoints.ciexyzRed.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzZ);
238 b4h->bV4Endpoints.ciexyzGreen.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzX);
239 b4h->bV4Endpoints.ciexyzGreen.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzY);
240 b4h->bV4Endpoints.ciexyzGreen.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzZ);
241 b4h->bV4Endpoints.ciexyzBlue.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzX);
242 b4h->bV4Endpoints.ciexyzBlue.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzY);
243 b4h->bV4Endpoints.ciexyzBlue.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzZ);
244 b4h->bV4GammaRed = BYTESWAP_DWORD(b4h->bV4GammaRed);
245 b4h->bV4GammaGreen = BYTESWAP_DWORD(b4h->bV4GammaGreen);
246 b4h->bV4GammaBlue = BYTESWAP_DWORD(b4h->bV4GammaBlue);
249 static void convert_bitmap_swap_os2(BITMAPOS2HEADER *boh)
251 boh->biSize = BYTESWAP_DWORD(boh->biSize);
252 boh->biWidth = BYTESWAP_WORD(boh->biWidth);
253 boh->biHeight = BYTESWAP_WORD(boh->biHeight);
254 boh->biPlanes = BYTESWAP_WORD(boh->biPlanes);
255 boh->biBitCount = BYTESWAP_WORD(boh->biBitCount);
258 #define FL_SIGBE 0x01
259 #define FL_SIZEBE 0x02
260 #define FL_V4 0x04
261 #define FL_OS2 0x08
262 static int convert_bitmap(char *data, int size)
264 BITMAPINFOHEADER *bih = (BITMAPINFOHEADER *)data;
265 BITMAPV4HEADER *b4h = (BITMAPV4HEADER *)data;
266 BITMAPOS2HEADER *boh = (BITMAPOS2HEADER *)data;
267 int type = 0;
268 int returnSize = 0; /* size to be returned */
269 #ifdef WORDS_BIGENDIAN
270 DWORD bisizel = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
271 DWORD b4sizel = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
272 DWORD bosizel = BYTESWAP_DWORD(sizeof(BITMAPOS2HEADER));
273 DWORD bisizeb = sizeof(BITMAPINFOHEADER);
274 DWORD b4sizeb = sizeof(BITMAPV4HEADER);
275 DWORD bosizeb = sizeof(BITMAPOS2HEADER);
276 #else
277 DWORD bisizel = sizeof(BITMAPINFOHEADER);
278 DWORD b4sizel = sizeof(BITMAPV4HEADER);
279 DWORD bosizel = sizeof(BITMAPOS2HEADER);
280 DWORD bisizeb = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
281 DWORD b4sizeb = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
282 DWORD bosizeb = BYTESWAP_DWORD(sizeof(BITMAPOS2HEADER));
283 #endif
287 * Originally the bih and b4h pointers were simply incremented here,
288 * and memmoved at the end of the function. This causes alignment
289 * issues on solaris, so we do the memmove here rather than at the end.
291 if(data[0] == 'B' && data[1] == 'M')
293 /* Little endian signature */
294 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
295 returnSize = sizeof(BITMAPFILEHEADER);
297 else if(data[0] == 'M' && data[1] == 'B')
299 type |= FL_SIGBE; /* Big endian signature */
300 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
301 returnSize = sizeof(BITMAPFILEHEADER);
305 if(bih->biSize == bisizel)
307 /* Little endian */
309 else if(bih->biSize == bisizeb)
311 type |= FL_SIZEBE;
313 else if(bih->biSize == b4sizel)
315 type |= FL_V4;
317 else if(bih->biSize == b4sizeb)
319 type |= FL_SIZEBE | FL_V4;
321 else if(!bih->biSize || bih->biSize == bosizel)
323 type |= FL_OS2;
325 else if(bih->biSize == bosizeb)
327 type |= FL_SIZEBE | FL_OS2;
329 else
330 parser_error("Invalid bitmap format, bih->biSize = %d", bih->biSize);
332 switch(type)
334 default:
335 break;
336 case FL_SIZEBE:
337 case FL_SIZEBE | FL_V4:
338 case FL_SIZEBE | FL_OS2:
339 parser_warning("Bitmap v%c signature little-endian, but size big-endian\n", type & FL_V4 ? '4' : '3');
340 break;
341 case FL_SIGBE:
342 case FL_SIGBE | FL_V4:
343 case FL_SIGBE | FL_OS2:
344 parser_warning("Bitmap v%c signature big-endian, but size little-endian\n", type & FL_V4 ? '4' : '3');
345 break;
348 switch(byteorder)
350 #ifdef WORDS_BIGENDIAN
351 default:
352 #endif
353 case WRC_BO_BIG:
354 if(!(type & FL_SIZEBE))
356 if(type & FL_V4)
357 convert_bitmap_swap_v4(b4h);
358 else if(type & FL_OS2)
360 convert_bitmap_swap_os2(boh);
362 else
363 convert_bitmap_swap_v3(bih);
365 break;
366 #ifndef WORDS_BIGENDIAN
367 default:
368 #endif
369 case WRC_BO_LITTLE:
370 if(type & FL_SIZEBE)
372 if(type & FL_V4)
373 convert_bitmap_swap_v4(b4h);
374 else if(type & FL_OS2)
376 convert_bitmap_swap_os2(boh);
378 else
379 convert_bitmap_swap_v3(bih);
381 break;
384 if(size && (void *)data != (void *)bih)
386 /* We have the fileheader still attached, remove it */
387 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
388 return sizeof(BITMAPFILEHEADER);
390 return returnSize;
392 #undef FL_SIGBE
393 #undef FL_SIZEBE
394 #undef FL_V4
397 * Cursor and icon splitter functions used when allocating
398 * cursor- and icon-groups.
400 typedef struct {
401 language_t lan;
402 int id;
403 } id_alloc_t;
405 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
407 int i;
408 assert(lan != NULL);
409 assert(list != NULL);
410 assert(n != NULL);
412 if(!*list)
414 *list = xmalloc(sizeof(id_alloc_t));
415 *n = 1;
416 (*list)[0].lan = *lan;
417 (*list)[0].id = 1;
418 return 1;
421 for(i = 0; i < *n; i++)
423 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
424 return ++((*list)[i].id);
427 *list = xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
428 (*list)[*n].lan = *lan;
429 (*list)[*n].id = 1;
430 *n += 1;
431 return 1;
434 static int alloc_icon_id(language_t *lan)
436 static id_alloc_t *idlist = NULL;
437 static int nid = 0;
439 return get_new_id(&idlist, &nid, lan);
442 static int alloc_cursor_id(language_t *lan)
444 static id_alloc_t *idlist = NULL;
445 static int nid = 0;
447 return get_new_id(&idlist, &nid, lan);
450 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
452 int cnt;
453 int i;
454 icon_t *ico;
455 icon_t *list = NULL;
456 icon_header_t *ih = (icon_header_t *)rd->data;
457 int swap = 0;
459 if(ih->type == 1)
460 swap = 0;
461 else if(BYTESWAP_WORD(ih->type) == 1)
462 swap = 1;
463 else
464 parser_error("Icon resource data has invalid type id %d", ih->type);
466 cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
467 for(i = 0; i < cnt; i++)
469 icon_dir_entry_t ide;
470 BITMAPINFOHEADER info;
471 memcpy(&ide, rd->data + sizeof(icon_header_t)
472 + i*sizeof(icon_dir_entry_t), sizeof(ide));
474 ico = new_icon();
475 ico->id = alloc_icon_id(icog->lvc.language);
476 ico->lvc = icog->lvc;
477 if(swap)
479 ide.offset = BYTESWAP_DWORD(ide.offset);
480 ide.ressize= BYTESWAP_DWORD(ide.ressize);
482 if(ide.offset > rd->size
483 || ide.offset + ide.ressize > rd->size)
484 parser_error("Icon resource data corrupt");
485 ico->width = ide.width;
486 ico->height = ide.height;
487 ico->nclr = ide.nclr;
488 ico->planes = swap ? BYTESWAP_WORD(ide.planes) : ide.planes;
489 ico->bits = swap ? BYTESWAP_WORD(ide.bits) : ide.bits;
490 memcpy(&info, rd->data + ide.offset, sizeof(info));
491 convert_bitmap((char *) &info, 0);
492 memcpy(rd->data + ide.offset, &info, sizeof(info));
494 if(!ico->planes)
496 /* Argh! They did not fill out the resdir structure */
497 /* The bitmap is in destination byteorder. We want native for our structures */
498 switch(byteorder)
500 #ifdef WORDS_BIGENDIAN
501 case WRC_BO_LITTLE:
502 #else
503 case WRC_BO_BIG:
504 #endif
505 ico->planes = BYTESWAP_WORD(info.biPlanes);
506 break;
507 default:
508 ico->planes = info.biPlanes;
511 if(!ico->bits)
513 /* Argh! They did not fill out the resdir structure */
514 /* The bitmap is in destination byteorder. We want native for our structures */
515 switch(byteorder)
517 #ifdef WORDS_BIGENDIAN
518 case WRC_BO_LITTLE:
519 #else
520 case WRC_BO_BIG:
521 #endif
522 ico->bits = BYTESWAP_WORD(info.biBitCount);
523 break;
524 default:
525 ico->bits = info.biBitCount;
528 ico->data = new_raw_data();
529 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
530 if(!list)
532 list = ico;
534 else
536 ico->next = list;
537 list->prev = ico;
538 list = ico;
541 icog->iconlist = list;
542 *nico = cnt;
545 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
547 int cnt;
548 int i;
549 cursor_t *cur;
550 cursor_t *list = NULL;
551 cursor_header_t *ch = (cursor_header_t *)rd->data;
552 int swap = 0;
554 if(ch->type == 2)
555 swap = 0;
556 else if(BYTESWAP_WORD(ch->type) == 2)
557 swap = 1;
558 else
559 parser_error("Cursor resource data has invalid type id %d", ch->type);
560 cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
561 for(i = 0; i < cnt; i++)
563 cursor_dir_entry_t cde;
564 BITMAPINFOHEADER info;
565 memcpy(&cde, rd->data + sizeof(cursor_header_t)
566 + i*sizeof(cursor_dir_entry_t), sizeof(cde));
568 cur = new_cursor();
569 cur->id = alloc_cursor_id(curg->lvc.language);
570 cur->lvc = curg->lvc;
571 if(swap)
573 cde.offset = BYTESWAP_DWORD(cde.offset);
574 cde.ressize= BYTESWAP_DWORD(cde.ressize);
576 if(cde.offset > rd->size
577 || cde.offset + cde.ressize > rd->size)
578 parser_error("Cursor resource data corrupt");
579 cur->width = cde.width;
580 cur->height = cde.height;
581 cur->nclr = cde.nclr;
582 memcpy(&info, rd->data + cde.offset, sizeof(info));
583 convert_bitmap((char *)&info, 0);
584 memcpy(rd->data + cde.offset, &info, sizeof(info));
585 /* The bitmap is in destination byteorder. We want native for our structures */
586 switch(byteorder)
588 #ifdef WORDS_BIGENDIAN
589 case WRC_BO_LITTLE:
590 #else
591 case WRC_BO_BIG:
592 #endif
593 cur->planes = BYTESWAP_WORD(info.biPlanes);
594 cur->bits = BYTESWAP_WORD(info.biBitCount);
595 break;
596 default:
597 cur->planes = info.biPlanes;
598 cur->bits = info.biBitCount;
600 if(!win32 && (cur->planes != 1 || cur->bits != 1))
601 parser_warning("Win16 cursor contains colors\n");
602 cur->xhot = swap ? BYTESWAP_WORD(cde.xhot) : cde.xhot;
603 cur->yhot = swap ? BYTESWAP_WORD(cde.yhot) : cde.yhot;
604 cur->data = new_raw_data();
605 copy_raw_data(cur->data, rd, cde.offset, cde.ressize);
606 if(!list)
608 list = cur;
610 else
612 cur->next = list;
613 list->prev = cur;
614 list = cur;
617 curg->cursorlist = list;
618 *ncur = cnt;
622 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
624 icon_group_t *icog = xmalloc(sizeof(icon_group_t));
625 if(memopt)
627 icog->memopt = *memopt;
628 free(memopt);
630 else
631 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
632 icog->lvc = rd->lvc;
633 split_icons(rd, icog, &(icog->nicon));
634 free(rd->data);
635 free(rd);
636 return icog;
639 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
641 cursor_group_t *curg = xmalloc(sizeof(cursor_group_t));
642 if(memopt)
644 curg->memopt = *memopt;
645 free(memopt);
647 else
648 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
649 curg->lvc = rd->lvc;
650 split_cursors(rd, curg, &(curg->ncursor));
651 free(rd->data);
652 free(rd);
653 return curg;
657 * Animated cursors and icons
659 * The format of animated cursors and icons is yet another example
660 * of bad design by "The Company". The entire RIFF structure is
661 * flawed by design because it is inconsistent and single minded:
662 * - some tags have lengths attached, others don't. The use of these
663 * non-length tags is absolutely unclear;
664 * - the content of "icon" tags can be both icons and cursors;
665 * - tags lack proper alignment constraints. It seems that everything
666 * is 16bit aligned, but I could not find that in any docu. Just be
667 * prepared to eat anything;
668 * - there are no strict constraints on tag-nesting and the organization
669 * is highly illogical;
671 * Anyhow, here is the basic structure:
672 * "RIFF" { dword taglength }
673 * "ACON" // What does it do?
674 * "LIST" { dword taglength }
675 * "INFO" // And what does this do?
676 * "INAM" { dword taglength } // Icon/cursor name
677 * {inam data}
678 * "IART" { dword taglength } // The artist
679 * {iart data}
680 * "fram" // Is followed by "icon"s
681 * "icon" { dword taglength } // First frame
682 * { icon/cursor data }
683 * "icon" { dword taglength } // Second frame
684 * { icon/cursor data }
685 * ... // ...
686 * "anih" { dword taglength } // Header structure
687 * { aniheader_t structure }
688 * "rate" { dword taglength } // The rate for each frame
689 * { `steps' dwords }
690 * "seq " { dword taglength } // The frame blit-order
691 * { `steps' dwords }
693 * Tag length are bytelength without the header and length field (i.e. -8).
694 * The "LIST" tag may occur several times and may encapsulate different
695 * tags. The `steps' is the number of "icon" tags found (actually the
696 * number of steps specified in the aniheader_t structure). The "seq "uence
697 * tag can be omitted, in which case the sequence is equal to the sequence
698 * of "icon"s found in the file. Also "rate" may be omitted, in which case
699 * the default from the aniheader_t structure is used.
701 * An animated cursor puts `.cur' formatted files into each "icon" tag,
702 * whereas animated icons contain `.ico' formatted files.
704 * Note about the code: Yes, it can be shorter/compressed. Some tags can be
705 * dealt with in the same code. However, this version shows what is going on
706 * and is better debug-able.
708 static const char riff[4] = "RIFF";
709 static const char acon[4] = "ACON";
710 static const char list[4] = "LIST";
711 static const char info[4] = "INFO";
712 static const char inam[4] = "INAM";
713 static const char iart[4] = "IART";
714 static const char fram[4] = "fram";
715 static const char icon[4] = "icon";
716 static const char anih[4] = "anih";
717 static const char rate[4] = "rate";
718 static const char seq[4] = "seq ";
720 #define SKIP_TAG(p,size) ((riff_tag_t *)(((char *)p) + (size)))
722 #define NEXT_TAG(p) SKIP_TAG(p,(isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p))
724 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
726 cursor_dir_entry_t *cdp;
727 cursor_header_t *chp;
728 int count;
729 int ctype;
730 int i;
731 static int once = 0; /* This will trigger only once per file! */
732 const char *anistr = type == res_aniico ? "icon" : "cursor";
733 /* Notes:
734 * Both cursor and icon directories are similar
735 * Both cursor and icon headers are similar
738 chp = (cursor_header_t *)(rtp+1);
739 cdp = (cursor_dir_entry_t *)(chp+1);
740 count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
741 ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
742 chp->reserved = BYTESWAP_WORD(chp->reserved);
743 chp->type = BYTESWAP_WORD(chp->type);
744 chp->count = BYTESWAP_WORD(chp->count);
746 if(type == res_anicur && ctype != 2 && !once)
748 parser_warning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)\n",
749 ctype,
750 ctype == 1 ? "icontype" : "?");
751 once++;
753 else if(type == res_aniico && ctype != 1 && !once)
755 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)\n",
756 ctype,
757 ctype == 2 ? "cursortype" : "?");
758 once++;
760 else if(ctype != 1 && ctype != 2 && !once)
762 parser_warning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)\n", anistr, ctype);
763 once++;
766 for(i = 0; i < count; i++)
768 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
769 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
770 if(ofs > rtp->size || ofs+sze > rtp->size)
771 parser_error("Animated %s's data corrupt", anistr);
772 convert_bitmap((char *)chp + ofs, 0);
773 cdp[i].xhot = BYTESWAP_WORD(cdp->xhot);
774 cdp[i].yhot = BYTESWAP_WORD(cdp->yhot);
775 cdp[i].ressize = BYTESWAP_DWORD(cdp->ressize);
776 cdp[i].offset = BYTESWAP_DWORD(cdp->offset);
780 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
782 riff_tag_t *rtp = lst+1; /* Skip the "LIST" tag */
784 while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
786 if(!memcmp(rtp->tag, info, sizeof(info)))
788 rtp = SKIP_TAG(rtp,4);
790 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
792 /* Ignore the icon/cursor name; its a string */
793 rtp = NEXT_TAG(rtp);
795 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
797 /* Ignore the author's name; it's a string */
798 rtp = NEXT_TAG(rtp);
800 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
802 /* This should be followed by "icon"s, but we
803 * simply ignore this because it is pure
804 * non-information.
806 rtp = SKIP_TAG(rtp,4);
808 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
810 handle_ani_icon(rtp, type, isswapped);
811 rtp = NEXT_TAG(rtp);
813 else
814 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
815 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
816 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
817 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
818 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
820 if((UINT_PTR)rtp & 1)
821 rtp = SKIP_TAG(rtp,1);
825 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
827 ani_curico_t *ani = xmalloc(sizeof(ani_curico_t));
828 riff_tag_t *rtp;
829 int isswapped = 0;
830 int doswap;
831 const char *anistr = type == res_aniico ? "icon" : "cursor";
833 assert(!memcmp(rd->data, riff, sizeof(riff)));
834 assert(type == res_anicur || type == res_aniico);
836 rtp = (riff_tag_t *)rd->data;
838 if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
839 isswapped = 1;
840 else if(rtp->size + 2*sizeof(DWORD) == rd->size)
841 isswapped = 0;
842 else
843 parser_error("Animated %s has an invalid RIFF length", anistr);
845 switch(byteorder)
847 #ifdef WORDS_BIGENDIAN
848 case WRC_BO_LITTLE:
849 #else
850 case WRC_BO_BIG:
851 #endif
852 doswap = !isswapped;
853 break;
854 default:
855 doswap = isswapped;
859 * When to swap what:
860 * isswapped | doswap |
861 * ----------+--------+---------------------------------
862 * 0 | 0 | read native; don't convert
863 * 1 | 0 | read swapped size; don't convert
864 * 0 | 1 | read native; convert
865 * 1 | 1 | read swapped size; convert
866 * Reading swapped size if necessary to calculate in native
867 * format. E.g. a little-endian source on a big-endian
868 * processor.
870 if(doswap)
872 /* We only go through the RIFF file if we need to swap
873 * bytes in words/dwords. Else we couldn't care less
874 * what the file contains. This is consistent with
875 * MS' rc.exe, which doesn't complain at all, even though
876 * the file format might not be entirely correct.
878 rtp++; /* Skip the "RIFF" tag */
880 while((char *)rtp < (char *)rd->data + rd->size)
882 if(!memcmp(rtp->tag, acon, sizeof(acon)))
884 rtp = SKIP_TAG(rtp,4);
886 else if(!memcmp(rtp->tag, list, sizeof(list)))
888 handle_ani_list(rtp, type, isswapped);
889 rtp = NEXT_TAG(rtp);
891 else if(!memcmp(rtp->tag, anih, sizeof(anih)))
893 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
894 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
895 ahp->frames = BYTESWAP_DWORD(ahp->frames);
896 ahp->steps = BYTESWAP_DWORD(ahp->steps);
897 ahp->cx = BYTESWAP_DWORD(ahp->cx);
898 ahp->cy = BYTESWAP_DWORD(ahp->cy);
899 ahp->bitcount = BYTESWAP_DWORD(ahp->bitcount);
900 ahp->planes = BYTESWAP_DWORD(ahp->planes);
901 ahp->rate = BYTESWAP_DWORD(ahp->rate);
902 ahp->flags = BYTESWAP_DWORD(ahp->flags);
903 rtp = NEXT_TAG(rtp);
905 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
907 int cnt = rtp->size / sizeof(DWORD);
908 DWORD *dwp = (DWORD *)(rtp+1);
909 int i;
910 for(i = 0; i < cnt; i++)
911 dwp[i] = BYTESWAP_DWORD(dwp[i]);
912 rtp = NEXT_TAG(rtp);
914 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
916 int cnt = rtp->size / sizeof(DWORD);
917 DWORD *dwp = (DWORD *)(rtp+1);
918 int i;
919 for(i = 0; i < cnt; i++)
920 dwp[i] = BYTESWAP_DWORD(dwp[i]);
921 rtp = NEXT_TAG(rtp);
923 else
924 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
925 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
926 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
927 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
928 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
930 if((UINT_PTR)rtp & 1)
931 rtp = SKIP_TAG(rtp,1);
934 /* We must end correctly here */
935 if((char *)rtp != (char *)rd->data + rd->size)
936 parser_error("Animated %s contains invalid field size(s)", anistr);
939 ani->data = rd;
940 if(memopt)
942 ani->memopt = *memopt;
943 free(memopt);
945 else
946 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
947 return ani;
949 #undef NEXT_TAG
951 /* Bitmaps */
952 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
954 bitmap_t *bmp = xmalloc(sizeof(bitmap_t));
956 bmp->data = rd;
957 if(memopt)
959 bmp->memopt = *memopt;
960 free(memopt);
962 else
963 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
964 rd->size -= convert_bitmap(rd->data, rd->size);
965 return bmp;
968 ver_words_t *new_ver_words(int i)
970 ver_words_t *w = xmalloc(sizeof(ver_words_t));
971 w->words = xmalloc(sizeof(WORD));
972 w->words[0] = (WORD)i;
973 w->nwords = 1;
974 return w;
977 ver_words_t *add_ver_words(ver_words_t *w, int i)
979 w->words = xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
980 w->words[w->nwords] = (WORD)i;
981 w->nwords++;
982 return w;
985 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
986 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
988 messagetable_t *msg = xmalloc(sizeof(messagetable_t));
989 msgtab_block_t *mbp;
990 DWORD nblk;
991 DWORD i;
992 WORD lo;
993 WORD hi;
995 msg->data = rd;
996 if(memopt)
998 msg->memopt = *memopt;
999 free(memopt);
1001 else
1002 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1004 if(rd->size < sizeof(DWORD))
1005 parser_error("Invalid messagetable, size too small");
1007 nblk = *(DWORD *)rd->data;
1008 lo = WRC_LOWORD(nblk);
1009 hi = WRC_HIWORD(nblk);
1011 /* FIXME:
1012 * This test will fail for all n*2^16 blocks in the messagetable.
1013 * However, no sane person would want to have so many blocks
1014 * and have a table of megabytes attached.
1015 * So, I will assume that we have less than 2^16 blocks in the table
1016 * and all will just work out fine. Otherwise, we would need to test
1017 * the ID, offset and length (and flag) fields to be very sure.
1019 if(hi && lo)
1020 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian\n");
1021 if(!hi && !lo)
1022 parser_error("Invalid messagetable block count 0");
1024 if(!hi && lo) /* Messagetable byteorder == native byteorder */
1026 #ifdef WORDS_BIGENDIAN
1027 if(byteorder != WRC_BO_LITTLE) goto out;
1028 #else
1029 if(byteorder != WRC_BO_BIG) goto out;
1030 #endif
1031 /* Resource byteorder != native byteorder */
1033 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1034 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1035 parser_error("Messagetable's blocks are outside of defined data");
1036 for(i = 0; i < nblk; i++)
1038 msgtab_entry_t *mep, *next_mep;
1039 DWORD id;
1041 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1043 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1045 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1046 parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1047 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1049 WORD *wp = (WORD *)&mep[1];
1050 int l = mep->length/2 - 2; /* Length included header */
1051 int n;
1053 if(mep->length & 1)
1054 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1055 for(n = 0; n < l; n++)
1056 wp[n] = BYTESWAP_WORD(wp[n]);
1059 next_mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1060 mep->length = BYTESWAP_WORD(mep->length);
1061 mep->flags = BYTESWAP_WORD(mep->flags);
1062 mep = next_mep;
1065 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1066 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1067 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1070 if(hi && !lo) /* Messagetable byteorder != native byteorder */
1072 #ifdef WORDS_BIGENDIAN
1073 if(byteorder == WRC_BO_LITTLE) goto out;
1074 #else
1075 if(byteorder == WRC_BO_BIG) goto out;
1076 #endif
1077 /* Resource byteorder == native byteorder */
1079 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1080 nblk = BYTESWAP_DWORD(nblk);
1081 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1082 parser_error("Messagetable's blocks are outside of defined data");
1083 for(i = 0; i < nblk; i++)
1085 msgtab_entry_t *mep;
1086 DWORD id;
1088 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1089 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1090 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1091 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1093 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1095 mep->length = BYTESWAP_WORD(mep->length);
1096 mep->flags = BYTESWAP_WORD(mep->flags);
1098 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1099 parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1100 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1102 WORD *wp = (WORD *)&mep[1];
1103 int l = mep->length/2 - 2; /* Length included header */
1104 int n;
1106 if(mep->length & 1)
1107 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1108 for(n = 0; n < l; n++)
1109 wp[n] = BYTESWAP_WORD(wp[n]);
1112 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1117 out:
1118 return msg;
1120 #undef MSGTAB_BAD_PTR
1122 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1124 assert(offs <= src->size);
1125 assert(offs + len <= src->size);
1126 if(!dst->data)
1128 dst->data = xmalloc(len);
1129 dst->size = 0;
1131 else
1132 dst->data = xrealloc(dst->data, dst->size + len);
1133 /* dst->size holds the offset to copy to */
1134 memcpy(dst->data + dst->size, src->data + offs, len);
1135 dst->size += len;
1138 int *new_int(int i)
1140 int *ip = xmalloc(sizeof(int));
1141 *ip = i;
1142 return ip;
1145 stringtable_t *new_stringtable(lvc_t *lvc)
1147 stringtable_t *stt = xmalloc(sizeof(stringtable_t));
1149 memset( stt, 0, sizeof(*stt) );
1150 if(lvc)
1151 stt->lvc = *lvc;
1153 return stt;
1156 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1158 toolbar_t *tb = xmalloc(sizeof(toolbar_t));
1159 memset( tb, 0, sizeof(*tb) );
1160 tb->button_width = button_width;
1161 tb->button_height = button_height;
1162 tb->nitems = nitems;
1163 tb->items = items;
1164 return tb;
1167 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1169 dlginit_t *di = xmalloc(sizeof(dlginit_t));
1170 di->data = rd;
1171 if(memopt)
1173 di->memopt = *memopt;
1174 free(memopt);
1176 else
1177 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1179 return di;
1182 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1184 style_pair_t *sp = xmalloc(sizeof(style_pair_t));
1185 sp->style = style;
1186 sp->exstyle = exstyle;
1187 return sp;
1190 style_t *new_style(DWORD or_mask, DWORD and_mask)
1192 style_t *st = xmalloc(sizeof(style_t));
1193 st->or_mask = or_mask;
1194 st->and_mask = and_mask;
1195 return st;