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 */
37 #define ICO_PNG_MAGIC 0x474e5089
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
) );
62 name_id_t
*new_name_id(void)
64 name_id_t
*ret
= xmalloc( sizeof(*ret
) );
65 memset( ret
, 0, sizeof(*ret
) );
69 menu_t
*new_menu(void)
71 menu_t
*ret
= xmalloc( sizeof(*ret
) );
72 memset( ret
, 0, sizeof(*ret
) );
76 menu_item_t
*new_menu_item(void)
78 menu_item_t
*ret
= xmalloc( sizeof(*ret
) );
79 memset( ret
, 0, sizeof(*ret
) );
83 control_t
*new_control(void)
85 control_t
*ret
= xmalloc( sizeof(*ret
) );
86 memset( ret
, 0, sizeof(*ret
) );
90 icon_t
*new_icon(void)
92 icon_t
*ret
= xmalloc( sizeof(*ret
) );
93 memset( ret
, 0, sizeof(*ret
) );
97 cursor_t
*new_cursor(void)
99 cursor_t
*ret
= xmalloc( sizeof(*ret
) );
100 memset( ret
, 0, sizeof(*ret
) );
104 versioninfo_t
*new_versioninfo(void)
106 versioninfo_t
*ret
= xmalloc( sizeof(*ret
) );
107 memset( ret
, 0, sizeof(*ret
) );
111 ver_value_t
*new_ver_value(void)
113 ver_value_t
*ret
= xmalloc( sizeof(*ret
) );
114 memset( ret
, 0, sizeof(*ret
) );
118 ver_block_t
*new_ver_block(void)
120 ver_block_t
*ret
= xmalloc( sizeof(*ret
) );
121 memset( ret
, 0, sizeof(*ret
) );
125 stt_entry_t
*new_stt_entry(void)
127 stt_entry_t
*ret
= xmalloc( sizeof(*ret
) );
128 memset( ret
, 0, sizeof(*ret
) );
132 accelerator_t
*new_accelerator(void)
134 accelerator_t
*ret
= xmalloc( sizeof(*ret
) );
135 memset( ret
, 0, sizeof(*ret
) );
139 event_t
*new_event(void)
141 event_t
*ret
= xmalloc( sizeof(*ret
) );
142 memset( ret
, 0, sizeof(*ret
) );
146 raw_data_t
*new_raw_data(void)
148 raw_data_t
*ret
= xmalloc( sizeof(*ret
) );
149 memset( ret
, 0, sizeof(*ret
) );
155 lvc_t
*ret
= xmalloc( sizeof(*ret
) );
156 memset( ret
, 0, sizeof(*ret
) );
160 res_count_t
*new_res_count(void)
162 res_count_t
*ret
= xmalloc( sizeof(*ret
) );
163 memset( ret
, 0, sizeof(*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
);
175 toolbar_item_t
*new_toolbar_item(void)
177 toolbar_item_t
*ret
= xmalloc( sizeof(*ret
) );
178 memset( ret
, 0, sizeof(*ret
) );
182 ani_any_t
*new_ani_any(void)
184 ani_any_t
*ret
= xmalloc( sizeof(*ret
) );
185 memset( ret
, 0, sizeof(*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
) );
194 r
->res
.overlay
= res
;
200 version_t
*new_version(DWORD v
)
202 version_t
*vp
= xmalloc(sizeof(version_t
));
207 characts_t
*new_characts(DWORD c
)
209 characts_t
*cp
= xmalloc(sizeof(characts_t
));
214 language_t
*new_language(int id
, int sub
)
216 language_t
*lan
= xmalloc(sizeof(language_t
));
222 language_t
*dup_language(language_t
*l
)
225 return new_language(l
->id
, l
->sub
);
228 version_t
*dup_version(version_t
*v
)
231 return new_version(*v
);
234 characts_t
*dup_characts(characts_t
*c
)
237 return new_characts(*c
);
240 html_t
*new_html(raw_data_t
*rd
, int *memopt
)
242 html_t
*html
= xmalloc(sizeof(html_t
));
246 html
->memopt
= *memopt
;
250 html
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
254 rcdata_t
*new_rcdata(raw_data_t
*rd
, int *memopt
)
256 rcdata_t
*rc
= xmalloc(sizeof(rcdata_t
));
260 rc
->memopt
= *memopt
;
264 rc
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
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
));
273 fid
->weight
= weight
;
274 fid
->italic
= italic
;
278 user_t
*new_user(name_id_t
*type
, raw_data_t
*rd
, int *memopt
)
280 user_t
*usr
= xmalloc(sizeof(user_t
));
284 usr
->memopt
= *memopt
;
288 usr
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
293 font_t
*new_font(raw_data_t
*rd
, int *memopt
)
295 font_t
*fnt
= xmalloc(sizeof(font_t
));
299 fnt
->memopt
= *memopt
;
303 fnt
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
307 fontdir_t
*new_fontdir(raw_data_t
*rd
, int *memopt
)
309 fontdir_t
*fnd
= xmalloc(sizeof(fontdir_t
));
313 fnd
->memopt
= *memopt
;
317 fnd
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
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
;
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
;
404 case sizeof(BITMAPOS2HEADER
):
405 case sizeof(BITMAPINFOHEADER
):
406 case sizeof(BITMAPV4HEADER
):
407 case sizeof(BITMAPV5HEADER
):
408 #ifdef WORDS_BIGENDIAN
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
419 bmsize
= BYTESWAP_DWORD( bmsize
);
422 case BYTESWAP_DWORD( ICO_PNG_MAGIC
):
423 return 0; /* nothing to convert */
425 parser_error("Invalid bitmap format, bih->biSize = %d", bih
->bV5Size
);
431 parser_warning("Bitmap signature little-endian, but size big-endian\n");
434 parser_warning("Bitmap signature big-endian, but size little-endian\n");
440 #ifdef WORDS_BIGENDIAN
444 if(!(type
& FL_SIZEBE
))
446 if (bmsize
== sizeof(BITMAPOS2HEADER
))
447 convert_bitmap_swap_os2(boh
);
449 convert_bitmap_swap(bih
, bmsize
);
452 #ifndef WORDS_BIGENDIAN
458 if (bmsize
== sizeof(BITMAPOS2HEADER
))
459 convert_bitmap_swap_os2(boh
);
461 convert_bitmap_swap(bih
, bmsize
);
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
);
478 * Cursor and icon splitter functions used when allocating
479 * cursor- and icon-groups.
486 static int get_new_id(id_alloc_t
**list
, int *n
, language_t
*lan
)
490 assert(list
!= NULL
);
495 *list
= xmalloc(sizeof(id_alloc_t
));
497 (*list
)[0].lan
= *lan
;
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
;
515 static int alloc_icon_id(language_t
*lan
)
517 static id_alloc_t
*idlist
= NULL
;
520 return get_new_id(&idlist
, &nid
, lan
);
523 static int alloc_cursor_id(language_t
*lan
)
525 static id_alloc_t
*idlist
= NULL
;
528 return get_new_id(&idlist
, &nid
, lan
);
531 static void split_icons(raw_data_t
*rd
, icon_group_t
*icog
, int *nico
)
537 icon_header_t
*ih
= (icon_header_t
*)rd
->data
;
542 else if(BYTESWAP_WORD(ih
->type
) == 1)
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
));
556 ico
->id
= alloc_icon_id(icog
->lvc
.language
);
557 ico
->lvc
= icog
->lvc
;
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
));
577 /* Argh! They did not fill out the resdir structure */
578 /* The bitmap is in destination byteorder. We want native for our structures */
581 #ifdef WORDS_BIGENDIAN
586 ico
->planes
= BYTESWAP_WORD(info
.biPlanes
);
589 ico
->planes
= info
.biPlanes
;
594 /* Argh! They did not fill out the resdir structure */
595 /* The bitmap is in destination byteorder. We want native for our structures */
598 #ifdef WORDS_BIGENDIAN
603 ico
->bits
= BYTESWAP_WORD(info
.biBitCount
);
606 ico
->bits
= info
.biBitCount
;
609 ico
->data
= new_raw_data();
610 copy_raw_data(ico
->data
, rd
, ide
.offset
, ide
.ressize
);
622 icog
->iconlist
= list
;
626 static void split_cursors(raw_data_t
*rd
, cursor_group_t
*curg
, int *ncur
)
631 cursor_t
*list
= NULL
;
632 cursor_header_t
*ch
= (cursor_header_t
*)rd
->data
;
637 else if(BYTESWAP_WORD(ch
->type
) == 2)
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
));
650 cur
->id
= alloc_cursor_id(curg
->lvc
.language
);
651 cur
->lvc
= curg
->lvc
;
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 */
669 #ifdef WORDS_BIGENDIAN
674 cur
->planes
= BYTESWAP_WORD(info
.biPlanes
);
675 cur
->bits
= BYTESWAP_WORD(info
.biBitCount
);
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
);
698 curg
->cursorlist
= list
;
703 icon_group_t
*new_icon_group(raw_data_t
*rd
, int *memopt
)
705 icon_group_t
*icog
= xmalloc(sizeof(icon_group_t
));
708 icog
->memopt
= *memopt
;
712 icog
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
714 split_icons(rd
, icog
, &(icog
->nicon
));
720 cursor_group_t
*new_cursor_group(raw_data_t
*rd
, int *memopt
)
722 cursor_group_t
*curg
= xmalloc(sizeof(cursor_group_t
));
725 curg
->memopt
= *memopt
;
729 curg
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
731 split_cursors(rd
, curg
, &(curg
->ncursor
));
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
759 * "IART" { dword taglength } // The artist
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 }
767 * "anih" { dword taglength } // Header structure
768 * { aniheader_t structure }
769 * "rate" { dword taglength } // The rate for each frame
771 * "seq " { dword taglength } // The frame blit-order
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
;
812 static int once
= 0; /* This will trigger only once per file! */
813 const char *anistr
= type
== res_aniico
? "icon" : "cursor";
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",
831 ctype
== 1 ? "icontype" : "?");
834 else if(type
== res_aniico
&& ctype
!= 1 && !once
)
836 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)\n",
838 ctype
== 2 ? "cursortype" : "?");
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
);
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 */
876 else if(!memcmp(rtp
->tag
, iart
, sizeof(iart
)))
878 /* Ignore the author's name; it's a string */
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
887 rtp
= SKIP_TAG(rtp
,4);
889 else if(!memcmp(rtp
->tag
, icon
, sizeof(icon
)))
891 handle_ani_icon(rtp
, type
, isswapped
);
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
));
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
)
921 else if(rtp
->size
+ 2*sizeof(DWORD
) == rd
->size
)
924 parser_error("Animated %s has an invalid RIFF length", anistr
);
928 #ifdef WORDS_BIGENDIAN
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
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
);
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
);
986 else if(!memcmp(rtp
->tag
, rate
, sizeof(rate
)))
988 int cnt
= rtp
->size
/ sizeof(DWORD
);
989 DWORD
*dwp
= (DWORD
*)(rtp
+1);
991 for(i
= 0; i
< cnt
; i
++)
992 dwp
[i
] = BYTESWAP_DWORD(dwp
[i
]);
995 else if(!memcmp(rtp
->tag
, seq
, sizeof(seq
)))
997 int cnt
= rtp
->size
/ sizeof(DWORD
);
998 DWORD
*dwp
= (DWORD
*)(rtp
+1);
1000 for(i
= 0; i
< cnt
; i
++)
1001 dwp
[i
] = BYTESWAP_DWORD(dwp
[i
]);
1002 rtp
= NEXT_TAG(rtp
);
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
);
1023 ani
->memopt
= *memopt
;
1027 ani
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
1033 bitmap_t
*new_bitmap(raw_data_t
*rd
, int *memopt
)
1035 bitmap_t
*bmp
= xmalloc(sizeof(bitmap_t
));
1040 bmp
->memopt
= *memopt
;
1044 bmp
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
1045 rd
->size
-= convert_bitmap(rd
->data
, rd
->size
);
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
;
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
;
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
;
1079 msg
->memopt
= *memopt
;
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
);
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.
1101 internal_error(__FILE__
, __LINE__
, "Messagetable contains more than 65535 blocks; cannot determine endian\n");
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
;
1110 if(byteorder
!= WRC_BO_BIG
) goto out
;
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
;
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 */
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
);
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
;
1156 if(byteorder
== WRC_BO_BIG
) goto out
;
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
;
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 */
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
);
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
);
1209 dst
->data
= xmalloc(len
);
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
);
1221 int *ip
= xmalloc(sizeof(int));
1226 stringtable_t
*new_stringtable(lvc_t
*lvc
)
1228 stringtable_t
*stt
= xmalloc(sizeof(stringtable_t
));
1230 memset( stt
, 0, sizeof(*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
;
1248 dlginit_t
*new_dlginit(raw_data_t
*rd
, int *memopt
)
1250 dlginit_t
*di
= xmalloc(sizeof(dlginit_t
));
1254 di
->memopt
= *memopt
;
1258 di
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
1263 style_pair_t
*new_style_pair(style_t
*style
, style_t
*exstyle
)
1265 style_pair_t
*sp
= xmalloc(sizeof(style_pair_t
));
1267 sp
->exstyle
= exstyle
;
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
;