windowscodecs/tests: Use common wine_dbgstr_guid implementation from test.h.
[wine/multimedia.git] / tools / wrc / newstruc.c
blob52bafbd61152787e4d7a3edaa4e99683de3f20f2
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 #define ICO_PNG_MAGIC 0x474e5089
39 #include <pshpack2.h>
40 typedef struct
42 DWORD biSize;
43 WORD biWidth;
44 WORD biHeight;
45 WORD biPlanes;
46 WORD biBitCount;
47 } BITMAPOS2HEADER;
48 #include <poppack.h>
50 /* New instances for all types of structures */
51 /* Very inefficient (in size), but very functional :-]
52 * Especially for type-checking.
55 dialog_t *new_dialog(void)
57 dialog_t *ret = xmalloc( sizeof(*ret) );
58 memset( ret, 0, sizeof(*ret) );
59 return ret;
62 name_id_t *new_name_id(void)
64 name_id_t *ret = xmalloc( sizeof(*ret) );
65 memset( ret, 0, sizeof(*ret) );
66 return ret;
69 menu_t *new_menu(void)
71 menu_t *ret = xmalloc( sizeof(*ret) );
72 memset( ret, 0, sizeof(*ret) );
73 return ret;
76 menu_item_t *new_menu_item(void)
78 menu_item_t *ret = xmalloc( sizeof(*ret) );
79 memset( ret, 0, sizeof(*ret) );
80 return ret;
83 control_t *new_control(void)
85 control_t *ret = xmalloc( sizeof(*ret) );
86 memset( ret, 0, sizeof(*ret) );
87 return ret;
90 icon_t *new_icon(void)
92 icon_t *ret = xmalloc( sizeof(*ret) );
93 memset( ret, 0, sizeof(*ret) );
94 return ret;
97 cursor_t *new_cursor(void)
99 cursor_t *ret = xmalloc( sizeof(*ret) );
100 memset( ret, 0, sizeof(*ret) );
101 return ret;
104 versioninfo_t *new_versioninfo(void)
106 versioninfo_t *ret = xmalloc( sizeof(*ret) );
107 memset( ret, 0, sizeof(*ret) );
108 return ret;
111 ver_value_t *new_ver_value(void)
113 ver_value_t *ret = xmalloc( sizeof(*ret) );
114 memset( ret, 0, sizeof(*ret) );
115 return ret;
118 ver_block_t *new_ver_block(void)
120 ver_block_t *ret = xmalloc( sizeof(*ret) );
121 memset( ret, 0, sizeof(*ret) );
122 return ret;
125 stt_entry_t *new_stt_entry(void)
127 stt_entry_t *ret = xmalloc( sizeof(*ret) );
128 memset( ret, 0, sizeof(*ret) );
129 return ret;
132 accelerator_t *new_accelerator(void)
134 accelerator_t *ret = xmalloc( sizeof(*ret) );
135 memset( ret, 0, sizeof(*ret) );
136 return ret;
139 event_t *new_event(void)
141 event_t *ret = xmalloc( sizeof(*ret) );
142 memset( ret, 0, sizeof(*ret) );
143 return ret;
146 raw_data_t *new_raw_data(void)
148 raw_data_t *ret = xmalloc( sizeof(*ret) );
149 memset( ret, 0, sizeof(*ret) );
150 return ret;
153 lvc_t *new_lvc(void)
155 lvc_t *ret = xmalloc( sizeof(*ret) );
156 memset( ret, 0, sizeof(*ret) );
157 return ret;
160 res_count_t *new_res_count(void)
162 res_count_t *ret = xmalloc( sizeof(*ret) );
163 memset( ret, 0, sizeof(*ret) );
164 return ret;
167 string_t *new_string(void)
169 string_t *ret = xmalloc( sizeof(*ret) );
170 memset( ret, 0, sizeof(*ret) );
171 set_location( &ret->loc );
172 return ret;
175 toolbar_item_t *new_toolbar_item(void)
177 toolbar_item_t *ret = xmalloc( sizeof(*ret) );
178 memset( ret, 0, sizeof(*ret) );
179 return ret;
182 ani_any_t *new_ani_any(void)
184 ani_any_t *ret = xmalloc( sizeof(*ret) );
185 memset( ret, 0, sizeof(*ret) );
186 return ret;
189 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
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;
200 version_t *new_version(DWORD v)
202 version_t *vp = xmalloc(sizeof(version_t));
203 *vp = v;
204 return vp;
207 characts_t *new_characts(DWORD c)
209 characts_t *cp = xmalloc(sizeof(characts_t));
210 *cp = c;
211 return cp;
214 language_t *new_language(int id, int sub)
216 language_t *lan = xmalloc(sizeof(language_t));
217 lan->id = id;
218 lan->sub = sub;
219 return lan;
222 language_t *dup_language(language_t *l)
224 if(!l) return NULL;
225 return new_language(l->id, l->sub);
228 version_t *dup_version(version_t *v)
230 if(!v) return NULL;
231 return new_version(*v);
234 characts_t *dup_characts(characts_t *c)
236 if(!c) return NULL;
237 return new_characts(*c);
240 html_t *new_html(raw_data_t *rd, int *memopt)
242 html_t *html = xmalloc(sizeof(html_t));
243 html->data = rd;
244 if(memopt)
246 html->memopt = *memopt;
247 free(memopt);
249 else
250 html->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
251 return html;
254 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
256 rcdata_t *rc = xmalloc(sizeof(rcdata_t));
257 rc->data = rd;
258 if(memopt)
260 rc->memopt = *memopt;
261 free(memopt);
263 else
264 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
265 return rc;
268 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
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;
278 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
280 user_t *usr = xmalloc(sizeof(user_t));
281 usr->data = rd;
282 if(memopt)
284 usr->memopt = *memopt;
285 free(memopt);
287 else
288 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
289 usr->type = type;
290 return usr;
293 font_t *new_font(raw_data_t *rd, int *memopt)
295 font_t *fnt = xmalloc(sizeof(font_t));
296 fnt->data = rd;
297 if(memopt)
299 fnt->memopt = *memopt;
300 free(memopt);
302 else
303 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
304 return fnt;
307 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
309 fontdir_t *fnd = xmalloc(sizeof(fontdir_t));
310 fnd->data = rd;
311 if(memopt)
313 fnd->memopt = *memopt;
314 free(memopt);
316 else
317 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
318 return fnd;
323 * Convert bitmaps to proper endian
325 static void convert_bitmap_swap(BITMAPV5HEADER *bh, DWORD size)
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);
363 static void convert_bitmap_swap_os2(BITMAPOS2HEADER *boh)
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);
372 #define FL_SIGBE 0x01
373 #define FL_SIZEBE 0x02
374 static int convert_bitmap(char *data, int size)
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 */
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.
387 if(data[0] == 'B' && data[1] == 'M')
389 /* Little endian signature */
390 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
391 returnSize = sizeof(BITMAPFILEHEADER);
393 else if(data[0] == 'M' && data[1] == 'B')
395 type |= FL_SIGBE; /* Big endian signature */
396 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
397 returnSize = sizeof(BITMAPFILEHEADER);
401 bmsize = bih->bV5Size;
402 switch (bmsize)
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);
428 switch(type)
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;
438 switch(byteorder)
440 #ifdef WORDS_BIGENDIAN
441 default:
442 #endif
443 case WRC_BO_BIG:
444 if(!(type & FL_SIZEBE))
446 if (bmsize == sizeof(BITMAPOS2HEADER))
447 convert_bitmap_swap_os2(boh);
448 else
449 convert_bitmap_swap(bih, bmsize);
451 break;
452 #ifndef WORDS_BIGENDIAN
453 default:
454 #endif
455 case WRC_BO_LITTLE:
456 if(type & FL_SIZEBE)
458 if (bmsize == sizeof(BITMAPOS2HEADER))
459 convert_bitmap_swap_os2(boh);
460 else
461 convert_bitmap_swap(bih, bmsize);
463 break;
466 if(size && (void *)data != (void *)bih)
468 /* We have the fileheader still attached, remove it */
469 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
470 return sizeof(BITMAPFILEHEADER);
472 return returnSize;
474 #undef FL_SIGBE
475 #undef FL_SIZEBE
478 * Cursor and icon splitter functions used when allocating
479 * cursor- and icon-groups.
481 typedef struct {
482 language_t lan;
483 int id;
484 } id_alloc_t;
486 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
488 int i;
489 assert(lan != NULL);
490 assert(list != NULL);
491 assert(n != NULL);
493 if(!*list)
495 *list = xmalloc(sizeof(id_alloc_t));
496 *n = 1;
497 (*list)[0].lan = *lan;
498 (*list)[0].id = 1;
499 return 1;
502 for(i = 0; i < *n; i++)
504 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
505 return ++((*list)[i].id);
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;
515 static int alloc_icon_id(language_t *lan)
517 static id_alloc_t *idlist = NULL;
518 static int nid = 0;
520 return get_new_id(&idlist, &nid, lan);
523 static int alloc_cursor_id(language_t *lan)
525 static id_alloc_t *idlist = NULL;
526 static int nid = 0;
528 return get_new_id(&idlist, &nid, lan);
531 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
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;
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);
547 cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
548 for(i = 0; i < cnt; i++)
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));
555 ico = new_icon();
556 ico->id = alloc_icon_id(icog->lvc.language);
557 ico->lvc = icog->lvc;
558 if(swap)
560 ide.offset = BYTESWAP_DWORD(ide.offset);
561 ide.ressize= BYTESWAP_DWORD(ide.ressize);
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));
575 if(!ico->planes)
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)
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;
592 if(!ico->bits)
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)
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;
609 ico->data = new_raw_data();
610 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
611 if(!list)
613 list = ico;
615 else
617 ico->next = list;
618 list->prev = ico;
619 list = ico;
622 icog->iconlist = list;
623 *nico = cnt;
626 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
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;
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++)
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));
649 cur = new_cursor();
650 cur->id = alloc_cursor_id(curg->lvc.language);
651 cur->lvc = curg->lvc;
652 if(swap)
654 cde.offset = BYTESWAP_DWORD(cde.offset);
655 cde.ressize= BYTESWAP_DWORD(cde.ressize);
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)
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;
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)
689 list = cur;
691 else
693 cur->next = list;
694 list->prev = cur;
695 list = cur;
698 curg->cursorlist = list;
699 *ncur = cnt;
703 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
705 icon_group_t *icog = xmalloc(sizeof(icon_group_t));
706 if(memopt)
708 icog->memopt = *memopt;
709 free(memopt);
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;
720 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
722 cursor_group_t *curg = xmalloc(sizeof(cursor_group_t));
723 if(memopt)
725 curg->memopt = *memopt;
726 free(memopt);
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;
738 * Animated cursors and icons
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;
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 }
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.
782 * An animated cursor puts `.cur' formatted files into each "icon" tag,
783 * whereas animated icons contain `.ico' formatted files.
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.
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 ";
801 #define SKIP_TAG(p,size) ((riff_tag_t *)(((char *)p) + (size)))
803 #define NEXT_TAG(p) SKIP_TAG(p,(isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p))
805 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
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
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);
827 if(type == res_anicur && ctype != 2 && !once)
829 parser_warning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)\n",
830 ctype,
831 ctype == 1 ? "icontype" : "?");
832 once++;
834 else if(type == res_aniico && ctype != 1 && !once)
836 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)\n",
837 ctype,
838 ctype == 2 ? "cursortype" : "?");
839 once++;
841 else if(ctype != 1 && ctype != 2 && !once)
843 parser_warning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)\n", anistr, ctype);
844 once++;
847 for(i = 0; i < count; i++)
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);
861 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
863 riff_tag_t *rtp = lst+1; /* Skip the "LIST" tag */
865 while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
867 if(!memcmp(rtp->tag, info, sizeof(info)))
869 rtp = SKIP_TAG(rtp,4);
871 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
873 /* Ignore the icon/cursor name; its a string */
874 rtp = NEXT_TAG(rtp);
876 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
878 /* Ignore the author's name; it's a string */
879 rtp = NEXT_TAG(rtp);
881 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
883 /* This should be followed by "icon"s, but we
884 * simply ignore this because it is pure
885 * non-information.
887 rtp = SKIP_TAG(rtp,4);
889 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
891 handle_ani_icon(rtp, type, isswapped);
892 rtp = NEXT_TAG(rtp);
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] : '.');
901 if((UINT_PTR)rtp & 1)
902 rtp = SKIP_TAG(rtp,1);
906 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
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";
914 assert(!memcmp(rd->data, riff, sizeof(riff)));
915 assert(type == res_anicur || type == res_aniico);
917 rtp = (riff_tag_t *)rd->data;
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);
926 switch(byteorder)
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;
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.
951 if(doswap)
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.
959 rtp++; /* Skip the "RIFF" tag */
961 while((char *)rtp < (char *)rd->data + rd->size)
963 if(!memcmp(rtp->tag, acon, sizeof(acon)))
965 rtp = SKIP_TAG(rtp,4);
967 else if(!memcmp(rtp->tag, list, sizeof(list)))
969 handle_ani_list(rtp, type, isswapped);
970 rtp = NEXT_TAG(rtp);
972 else if(!memcmp(rtp->tag, anih, sizeof(anih)))
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);
986 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
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);
995 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
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);
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] : '.');
1011 if((UINT_PTR)rtp & 1)
1012 rtp = SKIP_TAG(rtp,1);
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);
1020 ani->data = rd;
1021 if(memopt)
1023 ani->memopt = *memopt;
1024 free(memopt);
1026 else
1027 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
1028 return ani;
1030 #undef NEXT_TAG
1032 /* Bitmaps */
1033 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
1035 bitmap_t *bmp = xmalloc(sizeof(bitmap_t));
1037 bmp->data = rd;
1038 if(memopt)
1040 bmp->memopt = *memopt;
1041 free(memopt);
1043 else
1044 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1045 rd->size -= convert_bitmap(rd->data, rd->size);
1046 return bmp;
1049 ver_words_t *new_ver_words(int i)
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;
1058 ver_words_t *add_ver_words(ver_words_t *w, int i)
1060 w->words = xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
1061 w->words[w->nwords] = (WORD)i;
1062 w->nwords++;
1063 return w;
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)
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;
1076 msg->data = rd;
1077 if(memopt)
1079 msg->memopt = *memopt;
1080 free(memopt);
1082 else
1083 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1085 if(rd->size < sizeof(DWORD))
1086 parser_error("Invalid messagetable, size too small");
1088 nblk = *(DWORD *)rd->data;
1089 lo = WRC_LOWORD(nblk);
1090 hi = WRC_HIWORD(nblk);
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.
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");
1105 if(!hi && lo) /* Messagetable byteorder == native byteorder */
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 */
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++)
1119 msgtab_entry_t *mep, *next_mep;
1120 DWORD id;
1122 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1124 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
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 */
1130 WORD *wp = (WORD *)&mep[1];
1131 int l = mep->length/2 - 2; /* Length included header */
1132 int n;
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]);
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;
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);
1151 if(hi && !lo) /* Messagetable byteorder != native byteorder */
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 */
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++)
1166 msgtab_entry_t *mep;
1167 DWORD id;
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);
1174 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1176 mep->length = BYTESWAP_WORD(mep->length);
1177 mep->flags = BYTESWAP_WORD(mep->flags);
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 */
1183 WORD *wp = (WORD *)&mep[1];
1184 int l = mep->length/2 - 2; /* Length included header */
1185 int n;
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]);
1193 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1198 out:
1199 return msg;
1201 #undef MSGTAB_BAD_PTR
1203 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1205 assert(offs <= src->size);
1206 assert(offs + len <= src->size);
1207 if(!dst->data)
1209 dst->data = xmalloc(len);
1210 dst->size = 0;
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;
1219 int *new_int(int i)
1221 int *ip = xmalloc(sizeof(int));
1222 *ip = i;
1223 return ip;
1226 stringtable_t *new_stringtable(lvc_t *lvc)
1228 stringtable_t *stt = xmalloc(sizeof(stringtable_t));
1230 memset( stt, 0, sizeof(*stt) );
1231 if(lvc)
1232 stt->lvc = *lvc;
1234 return stt;
1237 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
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;
1248 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1250 dlginit_t *di = xmalloc(sizeof(dlginit_t));
1251 di->data = rd;
1252 if(memopt)
1254 di->memopt = *memopt;
1255 free(memopt);
1257 else
1258 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1260 return di;
1263 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1265 style_pair_t *sp = xmalloc(sizeof(style_pair_t));
1266 sp->style = style;
1267 sp->exstyle = exstyle;
1268 return sp;
1271 style_t *new_style(DWORD or_mask, DWORD and_mask)
1273 style_t *st = xmalloc(sizeof(style_t));
1274 st->or_mask = or_mask;
1275 st->and_mask = and_mask;
1276 return st;