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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
= (resource_t
*)xmalloc(sizeof(resource_t
));
86 version_t
*new_version(DWORD v
)
88 version_t
*vp
= (version_t
*)xmalloc(sizeof(version_t
));
93 characts_t
*new_characts(DWORD c
)
95 characts_t
*cp
= (characts_t
*)xmalloc(sizeof(characts_t
));
100 language_t
*new_language(int id
, int sub
)
102 language_t
*lan
= (language_t
*)xmalloc(sizeof(language_t
));
108 language_t
*dup_language(language_t
*l
)
111 return new_language(l
->id
, l
->sub
);
114 version_t
*dup_version(version_t
*v
)
117 return new_version(*v
);
120 characts_t
*dup_characts(characts_t
*c
)
123 return new_characts(*c
);
126 rcdata_t
*new_rcdata(raw_data_t
*rd
, int *memopt
)
128 rcdata_t
*rc
= (rcdata_t
*)xmalloc(sizeof(rcdata_t
));
132 rc
->memopt
= *memopt
;
136 rc
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
140 font_id_t
*new_font_id(int size
, string_t
*face
, int weight
, int italic
)
142 font_id_t
*fid
= (font_id_t
*)xmalloc(sizeof(font_id_t
));
145 fid
->weight
= weight
;
146 fid
->italic
= italic
;
150 user_t
*new_user(name_id_t
*type
, raw_data_t
*rd
, int *memopt
)
152 user_t
*usr
= (user_t
*)xmalloc(sizeof(user_t
));
156 usr
->memopt
= *memopt
;
160 usr
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
165 font_t
*new_font(raw_data_t
*rd
, int *memopt
)
167 font_t
*fnt
= (font_t
*)xmalloc(sizeof(font_t
));
171 fnt
->memopt
= *memopt
;
175 fnt
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
179 fontdir_t
*new_fontdir(raw_data_t
*rd
, int *memopt
)
181 fontdir_t
*fnd
= (fontdir_t
*)xmalloc(sizeof(fontdir_t
));
185 fnd
->memopt
= *memopt
;
189 fnd
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
195 * Convert bitmaps to proper endian
197 static void convert_bitmap_swap_v3(BITMAPINFOHEADER
*bih
)
199 bih
->biSize
= BYTESWAP_DWORD(bih
->biSize
);
200 bih
->biWidth
= BYTESWAP_DWORD(bih
->biWidth
);
201 bih
->biHeight
= BYTESWAP_DWORD(bih
->biHeight
);
202 bih
->biPlanes
= BYTESWAP_WORD(bih
->biPlanes
);
203 bih
->biBitCount
= BYTESWAP_WORD(bih
->biBitCount
);
204 bih
->biCompression
= BYTESWAP_DWORD(bih
->biCompression
);
205 bih
->biSizeImage
= BYTESWAP_DWORD(bih
->biSizeImage
);
206 bih
->biXPelsPerMeter
= BYTESWAP_DWORD(bih
->biXPelsPerMeter
);
207 bih
->biYPelsPerMeter
= BYTESWAP_DWORD(bih
->biYPelsPerMeter
);
208 bih
->biClrUsed
= BYTESWAP_DWORD(bih
->biClrUsed
);
209 bih
->biClrImportant
= BYTESWAP_DWORD(bih
->biClrImportant
);
212 static void convert_bitmap_swap_v4(BITMAPV4HEADER
*b4h
)
214 convert_bitmap_swap_v3((BITMAPINFOHEADER
*)b4h
);
215 b4h
->bV4RedMask
= BYTESWAP_DWORD(b4h
->bV4RedMask
);
216 b4h
->bV4GreenMask
= BYTESWAP_DWORD(b4h
->bV4GreenMask
);
217 b4h
->bV4BlueMask
= BYTESWAP_DWORD(b4h
->bV4BlueMask
);
218 b4h
->bV4AlphaMask
= BYTESWAP_DWORD(b4h
->bV4AlphaMask
);
219 b4h
->bV4CSType
= BYTESWAP_DWORD(b4h
->bV4CSType
);
220 b4h
->bV4Endpoints
.ciexyzRed
.ciexyzX
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzRed
.ciexyzX
);
221 b4h
->bV4Endpoints
.ciexyzRed
.ciexyzY
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzRed
.ciexyzY
);
222 b4h
->bV4Endpoints
.ciexyzRed
.ciexyzZ
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzRed
.ciexyzZ
);
223 b4h
->bV4Endpoints
.ciexyzGreen
.ciexyzX
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzGreen
.ciexyzX
);
224 b4h
->bV4Endpoints
.ciexyzGreen
.ciexyzY
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzGreen
.ciexyzY
);
225 b4h
->bV4Endpoints
.ciexyzGreen
.ciexyzZ
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzGreen
.ciexyzZ
);
226 b4h
->bV4Endpoints
.ciexyzBlue
.ciexyzX
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzBlue
.ciexyzX
);
227 b4h
->bV4Endpoints
.ciexyzBlue
.ciexyzY
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzBlue
.ciexyzY
);
228 b4h
->bV4Endpoints
.ciexyzBlue
.ciexyzZ
= BYTESWAP_DWORD(b4h
->bV4Endpoints
.ciexyzBlue
.ciexyzZ
);
229 b4h
->bV4GammaRed
= BYTESWAP_DWORD(b4h
->bV4GammaRed
);
230 b4h
->bV4GammaGreen
= BYTESWAP_DWORD(b4h
->bV4GammaGreen
);
231 b4h
->bV4GammaBlue
= BYTESWAP_DWORD(b4h
->bV4GammaBlue
);
234 static void convert_bitmap_swap_os2(BITMAPOS2HEADER
*boh
)
236 boh
->biSize
= BYTESWAP_DWORD(boh
->biSize
);
237 boh
->biWidth
= BYTESWAP_WORD(boh
->biWidth
);
238 boh
->biHeight
= BYTESWAP_WORD(boh
->biHeight
);
239 boh
->biPlanes
= BYTESWAP_WORD(boh
->biPlanes
);
240 boh
->biBitCount
= BYTESWAP_WORD(boh
->biBitCount
);
243 #define FL_SIGBE 0x01
244 #define FL_SIZEBE 0x02
247 static int convert_bitmap(char *data
, int size
)
249 BITMAPINFOHEADER
*bih
= (BITMAPINFOHEADER
*)data
;
250 BITMAPV4HEADER
*b4h
= (BITMAPV4HEADER
*)data
;
251 BITMAPOS2HEADER
*boh
= (BITMAPOS2HEADER
*)data
;
253 int returnSize
= 0; /* size to be returned */
254 #ifdef WORDS_BIGENDIAN
255 DWORD bisizel
= BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER
));
256 DWORD b4sizel
= BYTESWAP_DWORD(sizeof(BITMAPV4HEADER
));
257 DWORD bosizel
= BYTESWAP_DWORD(sizeof(BITMAPOS2HEADER
));
258 DWORD bisizeb
= sizeof(BITMAPINFOHEADER
);
259 DWORD b4sizeb
= sizeof(BITMAPV4HEADER
);
260 DWORD bosizeb
= sizeof(BITMAPOS2HEADER
);
262 DWORD bisizel
= sizeof(BITMAPINFOHEADER
);
263 DWORD b4sizel
= sizeof(BITMAPV4HEADER
);
264 DWORD bosizel
= sizeof(BITMAPOS2HEADER
);
265 DWORD bisizeb
= BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER
));
266 DWORD b4sizeb
= BYTESWAP_DWORD(sizeof(BITMAPV4HEADER
));
267 DWORD bosizeb
= BYTESWAP_DWORD(sizeof(BITMAPOS2HEADER
));
272 * Originally the bih and b4h pointers were simply incremented here,
273 * and memmoved at the end of the function. This causes alignment
274 * issues on solaris, so we do the memmove here rather than at the end.
276 if(data
[0] == 'B' && data
[1] == 'M')
278 /* Little endian signature */
279 memmove(data
, data
+sizeof(BITMAPFILEHEADER
), size
- sizeof(BITMAPFILEHEADER
));
280 returnSize
= sizeof(BITMAPFILEHEADER
);
282 else if(data
[0] == 'M' && data
[1] == 'B')
284 type
|= FL_SIGBE
; /* Big endian signature */
285 memmove(data
, data
+sizeof(BITMAPFILEHEADER
), size
- sizeof(BITMAPFILEHEADER
));
286 returnSize
= sizeof(BITMAPFILEHEADER
);
290 if(bih
->biSize
== bisizel
)
294 else if(bih
->biSize
== bisizeb
)
298 else if(bih
->biSize
== b4sizel
)
302 else if(bih
->biSize
== b4sizeb
)
304 type
|= FL_SIZEBE
| FL_V4
;
306 else if(!bih
->biSize
|| bih
->biSize
== bosizel
)
310 else if(bih
->biSize
== bosizeb
)
312 type
|= FL_SIZEBE
| FL_OS2
;
315 yyerror("Invalid bitmap format, bih->biSize = %ld", bih
->biSize
);
322 case FL_SIZEBE
| FL_V4
:
323 case FL_SIZEBE
| FL_OS2
:
324 yywarning("Bitmap v%c signature little-endian, but size big-endian", type
& FL_V4
? '4' : '3');
327 case FL_SIGBE
| FL_V4
:
328 case FL_SIGBE
| FL_OS2
:
329 yywarning("Bitmap v%c signature big-endian, but size little-endian", type
& FL_V4
? '4' : '3');
335 #ifdef WORDS_BIGENDIAN
339 if(!(type
& FL_SIZEBE
))
342 convert_bitmap_swap_v4(b4h
);
343 else if(type
& FL_OS2
)
345 convert_bitmap_swap_os2(boh
);
348 convert_bitmap_swap_v3(bih
);
351 #ifndef WORDS_BIGENDIAN
358 convert_bitmap_swap_v4(b4h
);
359 else if(type
& FL_OS2
)
361 convert_bitmap_swap_os2(boh
);
364 convert_bitmap_swap_v3(bih
);
369 if(size
&& (void *)data
!= (void *)bih
)
371 /* We have the fileheader still attached, remove it */
372 memmove(data
, data
+sizeof(BITMAPFILEHEADER
), size
- sizeof(BITMAPFILEHEADER
));
373 return sizeof(BITMAPFILEHEADER
);
382 * Cursor and icon splitter functions used when allocating
383 * cursor- and icon-groups.
390 static int get_new_id(id_alloc_t
**list
, int *n
, language_t
*lan
)
394 assert(list
!= NULL
);
399 *list
= (id_alloc_t
*)xmalloc(sizeof(id_alloc_t
));
401 (*list
)[0].lan
= *lan
;
406 for(i
= 0; i
< *n
; i
++)
408 if((*list
)[i
].lan
.id
== lan
->id
&& (*list
)[i
].lan
.sub
== lan
->sub
)
409 return ++((*list
)[i
].id
);
412 *list
= (id_alloc_t
*)xrealloc(*list
, sizeof(id_alloc_t
) * (*n
+1));
413 (*list
)[*n
].lan
= *lan
;
419 static int alloc_icon_id(language_t
*lan
)
421 static id_alloc_t
*idlist
= NULL
;
424 return get_new_id(&idlist
, &nid
, lan
);
427 static int alloc_cursor_id(language_t
*lan
)
429 static id_alloc_t
*idlist
= NULL
;
432 return get_new_id(&idlist
, &nid
, lan
);
435 static void split_icons(raw_data_t
*rd
, icon_group_t
*icog
, int *nico
)
441 icon_header_t
*ih
= (icon_header_t
*)rd
->data
;
446 else if(BYTESWAP_WORD(ih
->type
) == 1)
449 yyerror("Icon resource data has invalid type id %d", ih
->type
);
451 cnt
= swap
? BYTESWAP_WORD(ih
->count
) : ih
->count
;
452 for(i
= 0; i
< cnt
; i
++)
454 icon_dir_entry_t ide
;
455 BITMAPINFOHEADER info
;
456 memcpy(&ide
, rd
->data
+ sizeof(icon_header_t
)
457 + i
*sizeof(icon_dir_entry_t
), sizeof(ide
));
460 ico
->id
= alloc_icon_id(icog
->lvc
.language
);
461 ico
->lvc
= icog
->lvc
;
464 ide
.offset
= BYTESWAP_DWORD(ide
.offset
);
465 ide
.ressize
= BYTESWAP_DWORD(ide
.ressize
);
467 if(ide
.offset
> rd
->size
468 || ide
.offset
+ ide
.ressize
> rd
->size
)
469 yyerror("Icon resource data corrupt");
470 ico
->width
= ide
.width
;
471 ico
->height
= ide
.height
;
472 ico
->nclr
= ide
.nclr
;
473 ico
->planes
= swap
? BYTESWAP_WORD(ide
.planes
) : ide
.planes
;
474 ico
->bits
= swap
? BYTESWAP_WORD(ide
.bits
) : ide
.bits
;
475 memcpy(&info
, rd
->data
+ ide
.offset
, sizeof(info
));
476 convert_bitmap((char *) &info
, 0);
477 memcpy(rd
->data
+ ide
.offset
, &info
, sizeof(info
));
481 /* Argh! They did not fill out the resdir structure */
482 /* The bitmap is in destination byteorder. We want native for our structures */
485 #ifdef WORDS_BIGENDIAN
490 ico
->planes
= BYTESWAP_WORD(info
.biPlanes
);
493 ico
->planes
= info
.biPlanes
;
498 /* Argh! They did not fill out the resdir structure */
499 /* The bitmap is in destination byteorder. We want native for our structures */
502 #ifdef WORDS_BIGENDIAN
507 ico
->bits
= BYTESWAP_WORD(info
.biBitCount
);
510 ico
->bits
= info
.biBitCount
;
513 ico
->data
= new_raw_data();
514 copy_raw_data(ico
->data
, rd
, ide
.offset
, ide
.ressize
);
526 icog
->iconlist
= list
;
530 static void split_cursors(raw_data_t
*rd
, cursor_group_t
*curg
, int *ncur
)
535 cursor_t
*list
= NULL
;
536 cursor_header_t
*ch
= (cursor_header_t
*)rd
->data
;
541 else if(BYTESWAP_WORD(ch
->type
) == 2)
544 yyerror("Cursor resource data has invalid type id %d", ch
->type
);
545 cnt
= swap
? BYTESWAP_WORD(ch
->count
) : ch
->count
;
546 for(i
= 0; i
< cnt
; i
++)
548 cursor_dir_entry_t cde
;
549 BITMAPINFOHEADER info
;
550 memcpy(&cde
, rd
->data
+ sizeof(cursor_header_t
)
551 + i
*sizeof(cursor_dir_entry_t
), sizeof(cde
));
554 cur
->id
= alloc_cursor_id(curg
->lvc
.language
);
555 cur
->lvc
= curg
->lvc
;
558 cde
.offset
= BYTESWAP_DWORD(cde
.offset
);
559 cde
.ressize
= BYTESWAP_DWORD(cde
.ressize
);
561 if(cde
.offset
> rd
->size
562 || cde
.offset
+ cde
.ressize
> rd
->size
)
563 yyerror("Cursor resource data corrupt");
564 cur
->width
= cde
.width
;
565 cur
->height
= cde
.height
;
566 cur
->nclr
= cde
.nclr
;
567 memcpy(&info
, rd
->data
+ cde
.offset
, sizeof(info
));
568 convert_bitmap((char *)&info
, 0);
569 memcpy(rd
->data
+ cde
.offset
, &info
, sizeof(info
));
570 /* The bitmap is in destination byteorder. We want native for our structures */
573 #ifdef WORDS_BIGENDIAN
578 cur
->planes
= BYTESWAP_WORD(info
.biPlanes
);
579 cur
->bits
= BYTESWAP_WORD(info
.biBitCount
);
582 cur
->planes
= info
.biPlanes
;
583 cur
->bits
= info
.biBitCount
;
585 if(!win32
&& (cur
->planes
!= 1 || cur
->bits
!= 1))
586 yywarning("Win16 cursor contains colors");
587 cur
->xhot
= swap
? BYTESWAP_WORD(cde
.xhot
) : cde
.xhot
;
588 cur
->yhot
= swap
? BYTESWAP_WORD(cde
.yhot
) : cde
.yhot
;
589 cur
->data
= new_raw_data();
590 copy_raw_data(cur
->data
, rd
, cde
.offset
, cde
.ressize
);
602 curg
->cursorlist
= list
;
607 icon_group_t
*new_icon_group(raw_data_t
*rd
, int *memopt
)
609 icon_group_t
*icog
= (icon_group_t
*)xmalloc(sizeof(icon_group_t
));
612 icog
->memopt
= *memopt
;
616 icog
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
618 split_icons(rd
, icog
, &(icog
->nicon
));
624 cursor_group_t
*new_cursor_group(raw_data_t
*rd
, int *memopt
)
626 cursor_group_t
*curg
= (cursor_group_t
*)xmalloc(sizeof(cursor_group_t
));
629 curg
->memopt
= *memopt
;
633 curg
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
635 split_cursors(rd
, curg
, &(curg
->ncursor
));
642 * Animated cursors and icons
644 * The format of animated cursors and icons is yet another example
645 * of bad design by "The Company". The entire RIFF structure is
646 * flawed by design because it is inconsistent and single minded:
647 * - some tags have lengths attached, others don't. The use of these
648 * non-length tags is absolutely unclear;
649 * - the content of "icon" tags can be both icons and cursors;
650 * - tags lack proper alignment constraints. It seems that everything
651 * is 16bit aligned, but I could not find that in any docu. Just be
652 * prepared to eat anything;
653 * - there are no strict constraints on tag-nesting and the organization
654 * is highly illogical;
656 * Anyhow, here is the basic structure:
657 * "RIFF" { dword taglength }
658 * "ACON" // What does it do?
659 * "LIST" { dword taglength }
660 * "INFO" // And what does this do?
661 * "INAM" { dword taglength } // Icon/cursor name
663 * "IART" { dword taglength } // The artist
665 * "fram" // Is followed by "icon"s
666 * "icon" { dword taglength } // First frame
667 * { icon/cursor data }
668 * "icon" { dword taglength } // Second frame
669 * { icon/cursor data }
671 * "anih" { dword taglength } // Header structure
672 * { aniheader_t structure }
673 * "rate" { dword taglength } // The rate for each frame
675 * "seq " { dword taglength } // The frame blit-order
678 * Tag length are bytelength without the header and length field (i.e. -8).
679 * The "LIST" tag may occur several times and may encapsulate different
680 * tags. The `steps' is the number of "icon" tags found (actually the
681 * number of steps specified in the aniheader_t structure). The "seq "uence
682 * tag can be ommitted, in which case the sequence is equal to the sequence
683 * of "icon"s found in the file. Also "rate" may be ommitted, in which case
684 * the default from the aniheader_t structure is used.
686 * An animated cursor puts `.cur' formatted files into each "icon" tag,
687 * whereas animated icons contain `.ico' formatted files.
689 * Note about the code: Yes, it can be shorter/compressed. Some tags can be
690 * dealt with in the same code. However, this version shows what is going on
691 * and is better debug-able.
693 static const char riff
[4] = "RIFF";
694 static const char acon
[4] = "ACON";
695 static const char list
[4] = "LIST";
696 static const char info
[4] = "INFO";
697 static const char inam
[4] = "INAM";
698 static const char iart
[4] = "IART";
699 static const char fram
[4] = "fram";
700 static const char icon
[4] = "icon";
701 static const char anih
[4] = "anih";
702 static const char rate
[4] = "rate";
703 static const char seq
[4] = "seq ";
705 #define SKIP_TAG(p,size) ((riff_tag_t *)(((char *)p) + (size)))
707 #define NEXT_TAG(p) SKIP_TAG(p,(isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p))
709 static void handle_ani_icon(riff_tag_t
*rtp
, enum res_e type
, int isswapped
)
711 cursor_dir_entry_t
*cdp
;
712 cursor_header_t
*chp
;
716 static int once
= 0; /* This will trigger only once per file! */
717 const char *anistr
= type
== res_aniico
? "icon" : "cursor";
719 * Both cursor and icon directories are similar
720 * Both cursor and icon headers are similar
723 chp
= (cursor_header_t
*)(rtp
+1);
724 cdp
= (cursor_dir_entry_t
*)(chp
+1);
725 count
= isswapped
? BYTESWAP_WORD(chp
->count
) : chp
->count
;
726 ctype
= isswapped
? BYTESWAP_WORD(chp
->type
) : chp
->type
;
727 chp
->reserved
= BYTESWAP_WORD(chp
->reserved
);
728 chp
->type
= BYTESWAP_WORD(chp
->type
);
729 chp
->count
= BYTESWAP_WORD(chp
->count
);
731 if(type
== res_anicur
&& ctype
!= 2 && !once
)
733 yywarning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)",
735 ctype
== 1 ? "icontype" : "?");
738 else if(type
== res_aniico
&& ctype
!= 1 && !once
)
740 yywarning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)",
742 ctype
== 2 ? "cursortype" : "?");
745 else if(ctype
!= 1 && ctype
!= 2 && !once
)
747 yywarning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)", anistr
, ctype
);
751 for(i
= 0; i
< count
; i
++)
753 DWORD ofs
= isswapped
? BYTESWAP_DWORD(cdp
[i
].offset
) : cdp
[i
].offset
;
754 DWORD sze
= isswapped
? BYTESWAP_DWORD(cdp
[i
].ressize
) : cdp
[i
].ressize
;
755 if(ofs
> rtp
->size
|| ofs
+sze
> rtp
->size
)
756 yyerror("Animated %s's data corrupt", anistr
);
757 convert_bitmap((char *)chp
+ ofs
, 0);
758 cdp
[i
].xhot
= BYTESWAP_WORD(cdp
->xhot
);
759 cdp
[i
].yhot
= BYTESWAP_WORD(cdp
->yhot
);
760 cdp
[i
].ressize
= BYTESWAP_DWORD(cdp
->ressize
);
761 cdp
[i
].offset
= BYTESWAP_DWORD(cdp
->offset
);
765 static void handle_ani_list(riff_tag_t
*lst
, enum res_e type
, int isswapped
)
767 riff_tag_t
*rtp
= lst
+1; /* Skip the "LIST" tag */
769 while((char *)rtp
< (char *)lst
+ lst
->size
+ sizeof(*lst
))
771 if(!memcmp(rtp
->tag
, info
, sizeof(info
)))
773 rtp
= SKIP_TAG(rtp
,4);
775 else if(!memcmp(rtp
->tag
, inam
, sizeof(inam
)))
777 /* Ignore the icon/cursor name; its a string */
780 else if(!memcmp(rtp
->tag
, iart
, sizeof(iart
)))
782 /* Ignore the author's name; its a string */
785 else if(!memcmp(rtp
->tag
, fram
, sizeof(fram
)))
787 /* This should be followed by "icon"s, but we
788 * simply ignore this because it is pure
791 rtp
= SKIP_TAG(rtp
,4);
793 else if(!memcmp(rtp
->tag
, icon
, sizeof(icon
)))
795 handle_ani_icon(rtp
, type
, isswapped
);
799 internal_error(__FILE__
, __LINE__
, "Unknown tag \"%c%c%c%c\" in RIFF file",
800 isprint(rtp
->tag
[0]) ? rtp
->tag
[0] : '.',
801 isprint(rtp
->tag
[1]) ? rtp
->tag
[1] : '.',
802 isprint(rtp
->tag
[2]) ? rtp
->tag
[2] : '.',
803 isprint(rtp
->tag
[3]) ? rtp
->tag
[3] : '.');
805 /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
807 rtp
= SKIP_TAG(rtp
,1);
811 ani_curico_t
*new_ani_curico(enum res_e type
, raw_data_t
*rd
, int *memopt
)
813 ani_curico_t
*ani
= (ani_curico_t
*)xmalloc(sizeof(ani_curico_t
));
817 const char *anistr
= type
== res_aniico
? "icon" : "cursor";
819 assert(!memcmp(rd
->data
, riff
, sizeof(riff
)));
820 assert(type
== res_anicur
|| type
== res_aniico
);
822 rtp
= (riff_tag_t
*)rd
->data
;
824 if(BYTESWAP_DWORD(rtp
->size
) + 2*sizeof(DWORD
) == rd
->size
)
826 else if(rtp
->size
+ 2*sizeof(DWORD
) == rd
->size
)
829 yyerror("Animated %s has an invalid RIFF length", anistr
);
833 #ifdef WORDS_BIGENDIAN
846 * isswapped | doswap |
847 * ----------+--------+---------------------------------
848 * 0 | 0 | read native; don't convert
849 * 1 | 0 | read swapped size; don't convert
850 * 0 | 1 | read native; convert
851 * 1 | 1 | read swapped size; convert
852 * Reading swapped size if necessary to calculate in native
853 * format. E.g. a little-endian source on a big-endian
858 /* We only go through the RIFF file if we need to swap
859 * bytes in words/dwords. Else we couldn't care less
860 * what the file contains. This is consistent with
861 * MS' rc.exe, which doesn't complain at all, eventhough
862 * the fileformat might not be entirely correct.
864 rtp
++; /* Skip the "RIFF" tag */
866 while((char *)rtp
< (char *)rd
->data
+ rd
->size
)
868 if(!memcmp(rtp
->tag
, acon
, sizeof(acon
)))
870 rtp
= SKIP_TAG(rtp
,4);
872 else if(!memcmp(rtp
->tag
, list
, sizeof(list
)))
874 handle_ani_list(rtp
, type
, isswapped
);
877 else if(!memcmp(rtp
->tag
, anih
, sizeof(anih
)))
879 aniheader_t
*ahp
= (aniheader_t
*)((char *)(rtp
+1));
880 ahp
->structsize
= BYTESWAP_DWORD(ahp
->structsize
);
881 ahp
->frames
= BYTESWAP_DWORD(ahp
->frames
);
882 ahp
->steps
= BYTESWAP_DWORD(ahp
->steps
);
883 ahp
->cx
= BYTESWAP_DWORD(ahp
->cx
);
884 ahp
->cy
= BYTESWAP_DWORD(ahp
->cy
);
885 ahp
->bitcount
= BYTESWAP_DWORD(ahp
->bitcount
);
886 ahp
->planes
= BYTESWAP_DWORD(ahp
->planes
);
887 ahp
->rate
= BYTESWAP_DWORD(ahp
->rate
);
888 ahp
->flags
= BYTESWAP_DWORD(ahp
->flags
);
891 else if(!memcmp(rtp
->tag
, rate
, sizeof(rate
)))
893 int cnt
= rtp
->size
/ sizeof(DWORD
);
894 DWORD
*dwp
= (DWORD
*)(rtp
+1);
896 for(i
= 0; i
< cnt
; i
++)
897 dwp
[i
] = BYTESWAP_DWORD(dwp
[i
]);
900 else if(!memcmp(rtp
->tag
, seq
, sizeof(seq
)))
902 int cnt
= rtp
->size
/ sizeof(DWORD
);
903 DWORD
*dwp
= (DWORD
*)(rtp
+1);
905 for(i
= 0; i
< cnt
; i
++)
906 dwp
[i
] = BYTESWAP_DWORD(dwp
[i
]);
910 internal_error(__FILE__
, __LINE__
, "Unknown tag \"%c%c%c%c\" in RIFF file",
911 isprint(rtp
->tag
[0]) ? rtp
->tag
[0] : '.',
912 isprint(rtp
->tag
[1]) ? rtp
->tag
[1] : '.',
913 isprint(rtp
->tag
[2]) ? rtp
->tag
[2] : '.',
914 isprint(rtp
->tag
[3]) ? rtp
->tag
[3] : '.');
916 /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
918 rtp
= SKIP_TAG(rtp
,1);
921 /* We must end correctly here */
922 if((char *)rtp
!= (char *)rd
->data
+ rd
->size
)
923 yyerror("Animated %s contains invalid field size(s)", anistr
);
929 ani
->memopt
= *memopt
;
933 ani
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
939 bitmap_t
*new_bitmap(raw_data_t
*rd
, int *memopt
)
941 bitmap_t
*bmp
= (bitmap_t
*)xmalloc(sizeof(bitmap_t
));
946 bmp
->memopt
= *memopt
;
950 bmp
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
951 rd
->size
-= convert_bitmap(rd
->data
, rd
->size
);
955 ver_words_t
*new_ver_words(int i
)
957 ver_words_t
*w
= (ver_words_t
*)xmalloc(sizeof(ver_words_t
));
958 w
->words
= (WORD
*)xmalloc(sizeof(WORD
));
959 w
->words
[0] = (WORD
)i
;
964 ver_words_t
*add_ver_words(ver_words_t
*w
, int i
)
966 w
->words
= (WORD
*)xrealloc(w
->words
, (w
->nwords
+1) * sizeof(WORD
));
967 w
->words
[w
->nwords
] = (WORD
)i
;
972 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
973 messagetable_t
*new_messagetable(raw_data_t
*rd
, int *memopt
)
975 messagetable_t
*msg
= (messagetable_t
*)xmalloc(sizeof(messagetable_t
));
985 msg
->memopt
= *memopt
;
989 msg
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
991 if(rd
->size
< sizeof(DWORD
))
992 yyerror("Invalid messagetable, size too small");
994 nblk
= *(DWORD
*)rd
->data
;
995 lo
= WRC_LOWORD(nblk
);
996 hi
= WRC_HIWORD(nblk
);
999 * This test will fail for all n*2^16 blocks in the messagetable.
1000 * However, no sane person would want to have so many blocks
1001 * and have a table of megabytes attached.
1002 * So, I will assume that we have less than 2^16 blocks in the table
1003 * and all will just work out fine. Otherwise, we would need to test
1004 * the ID, offset and length (and flag) fields to be very sure.
1007 internal_error(__FILE__
, __LINE__
, "Messagetable contains more than 65535 blocks; cannot determine endian");
1009 yyerror("Invalid messagetable block count 0");
1011 if(!hi
&& lo
) /* Messagetable byteorder == native byteorder */
1013 #ifdef WORDS_BIGENDIAN
1014 if(byteorder
!= WRC_BO_LITTLE
) goto out
;
1016 if(byteorder
!= WRC_BO_BIG
) goto out
;
1018 /* Resource byteorder != native byteorder */
1020 mbp
= (msgtab_block_t
*)&(((DWORD
*)rd
->data
)[1]);
1021 if(MSGTAB_BAD_PTR(mbp
, rd
->data
, rd
->size
, nblk
* sizeof(*mbp
)))
1022 yyerror("Messagetable's blocks are outside of defined data");
1023 for(i
= 0; i
< nblk
; i
++)
1025 msgtab_entry_t
*mep
, *next_mep
;
1028 mep
= (msgtab_entry_t
*)(((char *)rd
->data
) + mbp
[i
].offset
);
1030 for(id
= mbp
[i
].idlo
; id
<= mbp
[i
].idhi
; id
++)
1032 if(MSGTAB_BAD_PTR(mep
, rd
->data
, rd
->size
, mep
->length
))
1033 yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i
, id
);
1034 if(mep
->flags
== 1) /* Docu says 'flags == 0x0001' for unicode */
1036 WORD
*wp
= (WORD
*)&mep
[1];
1037 int l
= mep
->length
/2 - 2; /* Length included header */
1041 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id
, (int)i
, mep
->length
);
1042 for(n
= 0; n
< l
; n
++)
1043 wp
[n
] = BYTESWAP_WORD(wp
[n
]);
1046 next_mep
= (msgtab_entry_t
*)(((char *)mep
) + mep
->length
);
1047 mep
->length
= BYTESWAP_WORD(mep
->length
);
1048 mep
->flags
= BYTESWAP_WORD(mep
->flags
);
1052 mbp
[i
].idlo
= BYTESWAP_DWORD(mbp
[i
].idlo
);
1053 mbp
[i
].idhi
= BYTESWAP_DWORD(mbp
[i
].idhi
);
1054 mbp
[i
].offset
= BYTESWAP_DWORD(mbp
[i
].offset
);
1057 if(hi
&& !lo
) /* Messagetable byteorder != native byteorder */
1059 #ifdef WORDS_BIGENDIAN
1060 if(byteorder
== WRC_BO_LITTLE
) goto out
;
1062 if(byteorder
== WRC_BO_BIG
) goto out
;
1064 /* Resource byteorder == native byteorder */
1066 mbp
= (msgtab_block_t
*)&(((DWORD
*)rd
->data
)[1]);
1067 nblk
= BYTESWAP_DWORD(nblk
);
1068 if(MSGTAB_BAD_PTR(mbp
, rd
->data
, rd
->size
, nblk
* sizeof(*mbp
)))
1069 yyerror("Messagetable's blocks are outside of defined data");
1070 for(i
= 0; i
< nblk
; i
++)
1072 msgtab_entry_t
*mep
;
1075 mbp
[i
].idlo
= BYTESWAP_DWORD(mbp
[i
].idlo
);
1076 mbp
[i
].idhi
= BYTESWAP_DWORD(mbp
[i
].idhi
);
1077 mbp
[i
].offset
= BYTESWAP_DWORD(mbp
[i
].offset
);
1078 mep
= (msgtab_entry_t
*)(((char *)rd
->data
) + mbp
[i
].offset
);
1080 for(id
= mbp
[i
].idlo
; id
<= mbp
[i
].idhi
; id
++)
1082 mep
->length
= BYTESWAP_WORD(mep
->length
);
1083 mep
->flags
= BYTESWAP_WORD(mep
->flags
);
1085 if(MSGTAB_BAD_PTR(mep
, rd
->data
, rd
->size
, mep
->length
))
1086 yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i
, id
);
1087 if(mep
->flags
== 1) /* Docu says 'flags == 0x0001' for unicode */
1089 WORD
*wp
= (WORD
*)&mep
[1];
1090 int l
= mep
->length
/2 - 2; /* Length included header */
1094 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id
, (int)i
, mep
->length
);
1095 for(n
= 0; n
< l
; n
++)
1096 wp
[n
] = BYTESWAP_WORD(wp
[n
]);
1099 mep
= (msgtab_entry_t
*)(((char *)mep
) + mep
->length
);
1107 #undef MSGTAB_BAD_PTR
1109 void copy_raw_data(raw_data_t
*dst
, raw_data_t
*src
, unsigned int offs
, int len
)
1111 assert(offs
<= src
->size
);
1112 assert(offs
+ len
<= src
->size
);
1115 dst
->data
= (char *)xmalloc(len
);
1119 dst
->data
= (char *)xrealloc(dst
->data
, dst
->size
+ len
);
1120 /* dst->size holds the offset to copy to */
1121 memcpy(dst
->data
+ dst
->size
, src
->data
+ offs
, len
);
1127 int *ip
= (int *)xmalloc(sizeof(int));
1132 stringtable_t
*new_stringtable(lvc_t
*lvc
)
1134 stringtable_t
*stt
= (stringtable_t
*)xmalloc(sizeof(stringtable_t
));
1142 toolbar_t
*new_toolbar(int button_width
, int button_height
, toolbar_item_t
*items
, int nitems
)
1144 toolbar_t
*tb
= (toolbar_t
*)xmalloc(sizeof(toolbar_t
));
1145 tb
->button_width
= button_width
;
1146 tb
->button_height
= button_height
;
1147 tb
->nitems
= nitems
;
1152 dlginit_t
*new_dlginit(raw_data_t
*rd
, int *memopt
)
1154 dlginit_t
*di
= (dlginit_t
*)xmalloc(sizeof(dlginit_t
));
1158 di
->memopt
= *memopt
;
1162 di
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
1167 style_pair_t
*new_style_pair(style_t
*style
, style_t
*exstyle
)
1169 style_pair_t
*sp
= (style_pair_t
*)xmalloc(sizeof(style_pair_t
));
1171 sp
->exstyle
= exstyle
;
1175 style_t
*new_style(DWORD or_mask
, DWORD and_mask
)
1177 style_t
*st
= (style_t
*)xmalloc(sizeof(style_t
));
1178 st
->or_mask
= or_mask
;
1179 st
->and_mask
= and_mask
;