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
35 #include "wingdi.h" /* for BITMAPINFOHEADER */
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
) );
87 version_t
*new_version(DWORD v
)
89 version_t
*vp
= xmalloc(sizeof(version_t
));
94 characts_t
*new_characts(DWORD c
)
96 characts_t
*cp
= xmalloc(sizeof(characts_t
));
101 language_t
*new_language(int id
, int sub
)
103 language_t
*lan
= xmalloc(sizeof(language_t
));
109 language_t
*dup_language(language_t
*l
)
112 return new_language(l
->id
, l
->sub
);
115 version_t
*dup_version(version_t
*v
)
118 return new_version(*v
);
121 characts_t
*dup_characts(characts_t
*c
)
124 return new_characts(*c
);
127 html_t
*new_html(raw_data_t
*rd
, int *memopt
)
129 html_t
*html
= xmalloc(sizeof(html_t
));
133 html
->memopt
= *memopt
;
137 html
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
141 rcdata_t
*new_rcdata(raw_data_t
*rd
, int *memopt
)
143 rcdata_t
*rc
= xmalloc(sizeof(rcdata_t
));
147 rc
->memopt
= *memopt
;
151 rc
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
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
));
160 fid
->weight
= weight
;
161 fid
->italic
= italic
;
165 user_t
*new_user(name_id_t
*type
, raw_data_t
*rd
, int *memopt
)
167 user_t
*usr
= xmalloc(sizeof(user_t
));
171 usr
->memopt
= *memopt
;
175 usr
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
180 font_t
*new_font(raw_data_t
*rd
, int *memopt
)
182 font_t
*fnt
= xmalloc(sizeof(font_t
));
186 fnt
->memopt
= *memopt
;
190 fnt
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
194 fontdir_t
*new_fontdir(raw_data_t
*rd
, int *memopt
)
196 fontdir_t
*fnd
= xmalloc(sizeof(fontdir_t
));
200 fnd
->memopt
= *memopt
;
204 fnd
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
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
262 static int convert_bitmap(char *data
, int size
)
264 BITMAPINFOHEADER
*bih
= (BITMAPINFOHEADER
*)data
;
265 BITMAPV4HEADER
*b4h
= (BITMAPV4HEADER
*)data
;
266 BITMAPOS2HEADER
*boh
= (BITMAPOS2HEADER
*)data
;
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
);
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
));
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
)
309 else if(bih
->biSize
== bisizeb
)
313 else if(bih
->biSize
== b4sizel
)
317 else if(bih
->biSize
== b4sizeb
)
319 type
|= FL_SIZEBE
| FL_V4
;
321 else if(!bih
->biSize
|| bih
->biSize
== bosizel
)
325 else if(bih
->biSize
== bosizeb
)
327 type
|= FL_SIZEBE
| FL_OS2
;
330 parser_error("Invalid bitmap format, bih->biSize = %d", bih
->biSize
);
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", type
& FL_V4
? '4' : '3');
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", type
& FL_V4
? '4' : '3');
350 #ifdef WORDS_BIGENDIAN
354 if(!(type
& FL_SIZEBE
))
357 convert_bitmap_swap_v4(b4h
);
358 else if(type
& FL_OS2
)
360 convert_bitmap_swap_os2(boh
);
363 convert_bitmap_swap_v3(bih
);
366 #ifndef WORDS_BIGENDIAN
373 convert_bitmap_swap_v4(b4h
);
374 else if(type
& FL_OS2
)
376 convert_bitmap_swap_os2(boh
);
379 convert_bitmap_swap_v3(bih
);
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
);
397 * Cursor and icon splitter functions used when allocating
398 * cursor- and icon-groups.
405 static int get_new_id(id_alloc_t
**list
, int *n
, language_t
*lan
)
409 assert(list
!= NULL
);
414 *list
= xmalloc(sizeof(id_alloc_t
));
416 (*list
)[0].lan
= *lan
;
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
;
434 static int alloc_icon_id(language_t
*lan
)
436 static id_alloc_t
*idlist
= NULL
;
439 return get_new_id(&idlist
, &nid
, lan
);
442 static int alloc_cursor_id(language_t
*lan
)
444 static id_alloc_t
*idlist
= NULL
;
447 return get_new_id(&idlist
, &nid
, lan
);
450 static void split_icons(raw_data_t
*rd
, icon_group_t
*icog
, int *nico
)
456 icon_header_t
*ih
= (icon_header_t
*)rd
->data
;
461 else if(BYTESWAP_WORD(ih
->type
) == 1)
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
));
475 ico
->id
= alloc_icon_id(icog
->lvc
.language
);
476 ico
->lvc
= icog
->lvc
;
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
));
496 /* Argh! They did not fill out the resdir structure */
497 /* The bitmap is in destination byteorder. We want native for our structures */
500 #ifdef WORDS_BIGENDIAN
505 ico
->planes
= BYTESWAP_WORD(info
.biPlanes
);
508 ico
->planes
= info
.biPlanes
;
513 /* Argh! They did not fill out the resdir structure */
514 /* The bitmap is in destination byteorder. We want native for our structures */
517 #ifdef WORDS_BIGENDIAN
522 ico
->bits
= BYTESWAP_WORD(info
.biBitCount
);
525 ico
->bits
= info
.biBitCount
;
528 ico
->data
= new_raw_data();
529 copy_raw_data(ico
->data
, rd
, ide
.offset
, ide
.ressize
);
541 icog
->iconlist
= list
;
545 static void split_cursors(raw_data_t
*rd
, cursor_group_t
*curg
, int *ncur
)
550 cursor_t
*list
= NULL
;
551 cursor_header_t
*ch
= (cursor_header_t
*)rd
->data
;
556 else if(BYTESWAP_WORD(ch
->type
) == 2)
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
));
569 cur
->id
= alloc_cursor_id(curg
->lvc
.language
);
570 cur
->lvc
= curg
->lvc
;
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 */
588 #ifdef WORDS_BIGENDIAN
593 cur
->planes
= BYTESWAP_WORD(info
.biPlanes
);
594 cur
->bits
= BYTESWAP_WORD(info
.biBitCount
);
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");
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
);
617 curg
->cursorlist
= list
;
622 icon_group_t
*new_icon_group(raw_data_t
*rd
, int *memopt
)
624 icon_group_t
*icog
= xmalloc(sizeof(icon_group_t
));
627 icog
->memopt
= *memopt
;
631 icog
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
633 split_icons(rd
, icog
, &(icog
->nicon
));
639 cursor_group_t
*new_cursor_group(raw_data_t
*rd
, int *memopt
)
641 cursor_group_t
*curg
= xmalloc(sizeof(cursor_group_t
));
644 curg
->memopt
= *memopt
;
648 curg
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
650 split_cursors(rd
, curg
, &(curg
->ncursor
));
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
678 * "IART" { dword taglength } // The artist
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 }
686 * "anih" { dword taglength } // Header structure
687 * { aniheader_t structure }
688 * "rate" { dword taglength } // The rate for each frame
690 * "seq " { dword taglength } // The frame blit-order
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
;
731 static int once
= 0; /* This will trigger only once per file! */
732 const char *anistr
= type
== res_aniico
? "icon" : "cursor";
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)",
750 ctype
== 1 ? "icontype" : "?");
753 else if(type
== res_aniico
&& ctype
!= 1 && !once
)
755 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)",
757 ctype
== 2 ? "cursortype" : "?");
760 else if(ctype
!= 1 && ctype
!= 2 && !once
)
762 parser_warning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)", anistr
, ctype
);
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 */
795 else if(!memcmp(rtp
->tag
, iart
, sizeof(iart
)))
797 /* Ignore the author's name; its a string */
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
806 rtp
= SKIP_TAG(rtp
,4);
808 else if(!memcmp(rtp
->tag
, icon
, sizeof(icon
)))
810 handle_ani_icon(rtp
, type
, isswapped
);
814 internal_error(__FILE__
, __LINE__
, "Unknown tag \"%c%c%c%c\" in RIFF file",
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
));
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
)
840 else if(rtp
->size
+ 2*sizeof(DWORD
) == rd
->size
)
843 parser_error("Animated %s has an invalid RIFF length", anistr
);
847 #ifdef WORDS_BIGENDIAN
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
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
);
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
);
905 else if(!memcmp(rtp
->tag
, rate
, sizeof(rate
)))
907 int cnt
= rtp
->size
/ sizeof(DWORD
);
908 DWORD
*dwp
= (DWORD
*)(rtp
+1);
910 for(i
= 0; i
< cnt
; i
++)
911 dwp
[i
] = BYTESWAP_DWORD(dwp
[i
]);
914 else if(!memcmp(rtp
->tag
, seq
, sizeof(seq
)))
916 int cnt
= rtp
->size
/ sizeof(DWORD
);
917 DWORD
*dwp
= (DWORD
*)(rtp
+1);
919 for(i
= 0; i
< cnt
; i
++)
920 dwp
[i
] = BYTESWAP_DWORD(dwp
[i
]);
924 internal_error(__FILE__
, __LINE__
, "Unknown tag \"%c%c%c%c\" in RIFF file",
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
);
942 ani
->memopt
= *memopt
;
946 ani
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
952 bitmap_t
*new_bitmap(raw_data_t
*rd
, int *memopt
)
954 bitmap_t
*bmp
= xmalloc(sizeof(bitmap_t
));
959 bmp
->memopt
= *memopt
;
963 bmp
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
964 rd
->size
-= convert_bitmap(rd
->data
, rd
->size
);
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
;
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
;
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
));
998 msg
->memopt
= *memopt
;
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
);
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.
1020 internal_error(__FILE__
, __LINE__
, "Messagetable contains more than 65535 blocks; cannot determine endian");
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
;
1029 if(byteorder
!= WRC_BO_BIG
) goto out
;
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
;
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 */
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
);
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
;
1075 if(byteorder
== WRC_BO_BIG
) goto out
;
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
;
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 */
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
);
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
);
1128 dst
->data
= xmalloc(len
);
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
);
1140 int *ip
= xmalloc(sizeof(int));
1145 stringtable_t
*new_stringtable(lvc_t
*lvc
)
1147 stringtable_t
*stt
= xmalloc(sizeof(stringtable_t
));
1149 memset( stt
, 0, sizeof(*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
;
1167 dlginit_t
*new_dlginit(raw_data_t
*rd
, int *memopt
)
1169 dlginit_t
*di
= xmalloc(sizeof(dlginit_t
));
1173 di
->memopt
= *memopt
;
1177 di
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
1182 style_pair_t
*new_style_pair(style_t
*style
, style_t
*exstyle
)
1184 style_pair_t
*sp
= xmalloc(sizeof(style_pair_t
));
1186 sp
->exstyle
= exstyle
;
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
;