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 /* New instances for all types of structures */
49 /* Very inefficient (in size), but very functional :-]
50 * Especially for type-checking.
53 dialog_t *new_dialog(void)
55 dialog_t *ret = xmalloc( sizeof(*ret) );
56 memset( ret, 0, sizeof(*ret) );
60 name_id_t *new_name_id(void)
62 name_id_t *ret = xmalloc( sizeof(*ret) );
63 memset( ret, 0, sizeof(*ret) );
67 menu_t *new_menu(void)
69 menu_t *ret = xmalloc( sizeof(*ret) );
70 memset( ret, 0, sizeof(*ret) );
74 menu_item_t *new_menu_item(void)
76 menu_item_t *ret = xmalloc( sizeof(*ret) );
77 memset( ret, 0, sizeof(*ret) );
81 control_t *new_control(void)
83 control_t *ret = xmalloc( sizeof(*ret) );
84 memset( ret, 0, sizeof(*ret) );
88 icon_t *new_icon(void)
90 icon_t *ret = xmalloc( sizeof(*ret) );
91 memset( ret, 0, sizeof(*ret) );
95 cursor_t *new_cursor(void)
97 cursor_t *ret = xmalloc( sizeof(*ret) );
98 memset( ret, 0, sizeof(*ret) );
102 versioninfo_t *new_versioninfo(void)
104 versioninfo_t *ret = xmalloc( sizeof(*ret) );
105 memset( ret, 0, sizeof(*ret) );
109 ver_value_t *new_ver_value(void)
111 ver_value_t *ret = xmalloc( sizeof(*ret) );
112 memset( ret, 0, sizeof(*ret) );
116 ver_block_t *new_ver_block(void)
118 ver_block_t *ret = xmalloc( sizeof(*ret) );
119 memset( ret, 0, sizeof(*ret) );
123 stt_entry_t *new_stt_entry(void)
125 stt_entry_t *ret = xmalloc( sizeof(*ret) );
126 memset( ret, 0, sizeof(*ret) );
130 accelerator_t *new_accelerator(void)
132 accelerator_t *ret = xmalloc( sizeof(*ret) );
133 memset( ret, 0, sizeof(*ret) );
137 event_t *new_event(void)
139 event_t *ret = xmalloc( sizeof(*ret) );
140 memset( ret, 0, sizeof(*ret) );
144 raw_data_t *new_raw_data(void)
146 raw_data_t *ret = xmalloc( sizeof(*ret) );
147 memset( ret, 0, sizeof(*ret) );
153 lvc_t *ret = xmalloc( sizeof(*ret) );
154 memset( ret, 0, sizeof(*ret) );
158 res_count_t *new_res_count(void)
160 res_count_t *ret = xmalloc( sizeof(*ret) );
161 memset( ret, 0, sizeof(*ret) );
165 string_t *new_string(void)
167 string_t *ret = xmalloc( sizeof(*ret) );
168 memset( ret, 0, sizeof(*ret) );
169 set_location( &ret->loc );
173 toolbar_item_t *new_toolbar_item(void)
175 toolbar_item_t *ret = xmalloc( sizeof(*ret) );
176 memset( ret, 0, sizeof(*ret) );
180 ani_any_t *new_ani_any(void)
182 ani_any_t *ret = xmalloc( sizeof(*ret) );
183 memset( ret, 0, sizeof(*ret) );
187 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
189 resource_t *r = xmalloc(sizeof(resource_t));
190 memset( r, 0, sizeof(*r) );
192 r->res.overlay = res;
198 version_t *new_version(DWORD v)
200 version_t *vp = xmalloc(sizeof(version_t));
205 characts_t *new_characts(DWORD c)
207 characts_t *cp = xmalloc(sizeof(characts_t));
212 language_t *new_language(int id, int sub)
214 language_t *lan = xmalloc(sizeof(language_t));
220 language_t *dup_language(language_t *l)
223 return new_language(l->id, l->sub);
226 version_t *dup_version(version_t *v)
229 return new_version(*v);
232 characts_t *dup_characts(characts_t *c)
235 return new_characts(*c);
238 html_t *new_html(raw_data_t *rd, int *memopt)
240 html_t *html = xmalloc(sizeof(html_t));
244 html->memopt = *memopt;
248 html->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
252 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
254 rcdata_t *rc = xmalloc(sizeof(rcdata_t));
258 rc->memopt = *memopt;
262 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
266 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
268 font_id_t *fid = xmalloc(sizeof(font_id_t));
271 fid->weight = weight;
272 fid->italic = italic;
276 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
278 user_t *usr = xmalloc(sizeof(user_t));
282 usr->memopt = *memopt;
286 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
291 font_t *new_font(raw_data_t *rd, int *memopt)
293 font_t *fnt = xmalloc(sizeof(font_t));
297 fnt->memopt = *memopt;
301 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
305 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
307 fontdir_t *fnd = xmalloc(sizeof(fontdir_t));
311 fnd->memopt = *memopt;
315 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
321 * Convert bitmaps to proper endian
323 static void convert_bitmap_swap(BITMAPV5HEADER *bh, DWORD size)
325 bh->bV5Size = BYTESWAP_DWORD(bh->bV5Size);
326 bh->bV5Width = BYTESWAP_DWORD(bh->bV5Width);
327 bh->bV5Height = BYTESWAP_DWORD(bh->bV5Height);
328 bh->bV5Planes = BYTESWAP_WORD(bh->bV5Planes);
329 bh->bV5BitCount = BYTESWAP_WORD(bh->bV5BitCount);
330 bh->bV5Compression = BYTESWAP_DWORD(bh->bV5Compression);
331 bh->bV5SizeImage = BYTESWAP_DWORD(bh->bV5SizeImage);
332 bh->bV5XPelsPerMeter = BYTESWAP_DWORD(bh->bV5XPelsPerMeter);
333 bh->bV5YPelsPerMeter = BYTESWAP_DWORD(bh->bV5YPelsPerMeter);
334 bh->bV5ClrUsed = BYTESWAP_DWORD(bh->bV5ClrUsed);
335 bh->bV5ClrImportant = BYTESWAP_DWORD(bh->bV5ClrImportant);
336 if (size == sizeof(BITMAPINFOHEADER)) return;
337 bh->bV5RedMask = BYTESWAP_DWORD(bh->bV5RedMask);
338 bh->bV5GreenMask = BYTESWAP_DWORD(bh->bV5GreenMask);
339 bh->bV5BlueMask = BYTESWAP_DWORD(bh->bV5BlueMask);
340 bh->bV5AlphaMask = BYTESWAP_DWORD(bh->bV5AlphaMask);
341 bh->bV5CSType = BYTESWAP_DWORD(bh->bV5CSType);
342 bh->bV5Endpoints.ciexyzRed.ciexyzX = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzX);
343 bh->bV5Endpoints.ciexyzRed.ciexyzY = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzY);
344 bh->bV5Endpoints.ciexyzRed.ciexyzZ = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzZ);
345 bh->bV5Endpoints.ciexyzGreen.ciexyzX = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzX);
346 bh->bV5Endpoints.ciexyzGreen.ciexyzY = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzY);
347 bh->bV5Endpoints.ciexyzGreen.ciexyzZ = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzZ);
348 bh->bV5Endpoints.ciexyzBlue.ciexyzX = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzX);
349 bh->bV5Endpoints.ciexyzBlue.ciexyzY = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzY);
350 bh->bV5Endpoints.ciexyzBlue.ciexyzZ = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzZ);
351 bh->bV5GammaRed = BYTESWAP_DWORD(bh->bV5GammaRed);
352 bh->bV5GammaGreen = BYTESWAP_DWORD(bh->bV5GammaGreen);
353 bh->bV5GammaBlue = BYTESWAP_DWORD(bh->bV5GammaBlue);
354 if (size == sizeof(BITMAPV4HEADER)) return;
355 bh->bV5Intent = BYTESWAP_DWORD(bh->bV5Intent);
356 bh->bV5ProfileData = BYTESWAP_DWORD(bh->bV5ProfileData);
357 bh->bV5ProfileSize = BYTESWAP_DWORD(bh->bV5ProfileSize);
358 bh->bV5Reserved = BYTESWAP_DWORD(bh->bV5Reserved);
361 static void convert_bitmap_swap_os2(BITMAPOS2HEADER *boh)
363 boh->biSize = BYTESWAP_DWORD(boh->biSize);
364 boh->biWidth = BYTESWAP_WORD(boh->biWidth);
365 boh->biHeight = BYTESWAP_WORD(boh->biHeight);
366 boh->biPlanes = BYTESWAP_WORD(boh->biPlanes);
367 boh->biBitCount = BYTESWAP_WORD(boh->biBitCount);
370 #define FL_SIGBE 0x01
371 #define FL_SIZEBE 0x02
372 static int convert_bitmap(char *data, int size)
374 BITMAPV5HEADER *bih = (BITMAPV5HEADER *)data;
375 BITMAPOS2HEADER *boh = (BITMAPOS2HEADER *)data;
378 int returnSize = 0; /* size to be returned */
381 * Originally the bih and b4h pointers were simply incremented here,
382 * and memmoved at the end of the function. This causes alignment
383 * issues on solaris, so we do the memmove here rather than at the end.
385 if(data[0] == 'B' && data[1] == 'M')
387 /* Little endian signature */
388 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
389 returnSize = sizeof(BITMAPFILEHEADER);
391 else if(data[0] == 'M' && data[1] == 'B')
393 type |= FL_SIGBE; /* Big endian signature */
394 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
395 returnSize = sizeof(BITMAPFILEHEADER);
399 bmsize = bih->bV5Size;
400 if (bmsize >> 16) /* assume swapped */
402 #ifndef WORDS_BIGENDIAN
405 bmsize = BYTESWAP_DWORD( bmsize );
409 #ifdef WORDS_BIGENDIAN
416 case sizeof(BITMAPOS2HEADER):
417 case sizeof(BITMAPINFOHEADER):
418 case sizeof(BITMAPV4HEADER):
419 case sizeof(BITMAPV5HEADER):
422 parser_error("Invalid bitmap format, bih->biSize = %d", bih->bV5Size);
428 parser_warning("Bitmap signature little-endian, but size big-endian\n");
431 parser_warning("Bitmap signature big-endian, but size little-endian\n");
437 #ifdef WORDS_BIGENDIAN
441 if(!(type & FL_SIZEBE))
443 if (bmsize == sizeof(BITMAPOS2HEADER))
444 convert_bitmap_swap_os2(boh);
446 convert_bitmap_swap(bih, bmsize);
449 #ifndef WORDS_BIGENDIAN
455 if (bmsize == sizeof(BITMAPOS2HEADER))
456 convert_bitmap_swap_os2(boh);
458 convert_bitmap_swap(bih, bmsize);
463 if(size && (void *)data != (void *)bih)
465 /* We have the fileheader still attached, remove it */
466 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
467 return sizeof(BITMAPFILEHEADER);
475 * Cursor and icon splitter functions used when allocating
476 * cursor- and icon-groups.
483 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
487 assert(list != NULL);
492 *list = xmalloc(sizeof(id_alloc_t));
494 (*list)[0].lan = *lan;
499 for(i = 0; i < *n; i++)
501 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
502 return ++((*list)[i].id);
505 *list = xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
506 (*list)[*n].lan = *lan;
512 static int alloc_icon_id(language_t *lan)
514 static id_alloc_t *idlist = NULL;
517 return get_new_id(&idlist, &nid, lan);
520 static int alloc_cursor_id(language_t *lan)
522 static id_alloc_t *idlist = NULL;
525 return get_new_id(&idlist, &nid, lan);
528 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
534 icon_header_t *ih = (icon_header_t *)rd->data;
539 else if(BYTESWAP_WORD(ih->type) == 1)
542 parser_error("Icon resource data has invalid type id %d", ih->type);
544 cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
545 for(i = 0; i < cnt; i++)
547 icon_dir_entry_t ide;
548 BITMAPINFOHEADER info;
549 memcpy(&ide, rd->data + sizeof(icon_header_t)
550 + i*sizeof(icon_dir_entry_t), sizeof(ide));
553 ico->id = alloc_icon_id(icog->lvc.language);
554 ico->lvc = icog->lvc;
557 ide.offset = BYTESWAP_DWORD(ide.offset);
558 ide.ressize= BYTESWAP_DWORD(ide.ressize);
560 if(ide.offset > rd->size
561 || ide.offset + ide.ressize > rd->size)
562 parser_error("Icon resource data corrupt");
563 ico->width = ide.width;
564 ico->height = ide.height;
565 ico->nclr = ide.nclr;
566 ico->planes = swap ? BYTESWAP_WORD(ide.planes) : ide.planes;
567 ico->bits = swap ? BYTESWAP_WORD(ide.bits) : ide.bits;
568 memcpy(&info, rd->data + ide.offset, sizeof(info));
569 convert_bitmap((char *) &info, 0);
570 memcpy(rd->data + ide.offset, &info, sizeof(info));
574 /* Argh! They did not fill out the resdir structure */
575 /* The bitmap is in destination byteorder. We want native for our structures */
578 #ifdef WORDS_BIGENDIAN
583 ico->planes = BYTESWAP_WORD(info.biPlanes);
586 ico->planes = info.biPlanes;
591 /* Argh! They did not fill out the resdir structure */
592 /* The bitmap is in destination byteorder. We want native for our structures */
595 #ifdef WORDS_BIGENDIAN
600 ico->bits = BYTESWAP_WORD(info.biBitCount);
603 ico->bits = info.biBitCount;
606 ico->data = new_raw_data();
607 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
619 icog->iconlist = list;
623 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
628 cursor_t *list = NULL;
629 cursor_header_t *ch = (cursor_header_t *)rd->data;
634 else if(BYTESWAP_WORD(ch->type) == 2)
637 parser_error("Cursor resource data has invalid type id %d", ch->type);
638 cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
639 for(i = 0; i < cnt; i++)
641 cursor_dir_entry_t cde;
642 BITMAPINFOHEADER info;
643 memcpy(&cde, rd->data + sizeof(cursor_header_t)
644 + i*sizeof(cursor_dir_entry_t), sizeof(cde));
647 cur->id = alloc_cursor_id(curg->lvc.language);
648 cur->lvc = curg->lvc;
651 cde.offset = BYTESWAP_DWORD(cde.offset);
652 cde.ressize= BYTESWAP_DWORD(cde.ressize);
654 if(cde.offset > rd->size
655 || cde.offset + cde.ressize > rd->size)
656 parser_error("Cursor resource data corrupt");
657 cur->width = cde.width;
658 cur->height = cde.height;
659 cur->nclr = cde.nclr;
660 memcpy(&info, rd->data + cde.offset, sizeof(info));
661 convert_bitmap((char *)&info, 0);
662 memcpy(rd->data + cde.offset, &info, sizeof(info));
663 /* The bitmap is in destination byteorder. We want native for our structures */
666 #ifdef WORDS_BIGENDIAN
671 cur->planes = BYTESWAP_WORD(info.biPlanes);
672 cur->bits = BYTESWAP_WORD(info.biBitCount);
675 cur->planes = info.biPlanes;
676 cur->bits = info.biBitCount;
678 if(!win32 && (cur->planes != 1 || cur->bits != 1))
679 parser_warning("Win16 cursor contains colors\n");
680 cur->xhot = swap ? BYTESWAP_WORD(cde.xhot) : cde.xhot;
681 cur->yhot = swap ? BYTESWAP_WORD(cde.yhot) : cde.yhot;
682 cur->data = new_raw_data();
683 copy_raw_data(cur->data, rd, cde.offset, cde.ressize);
695 curg->cursorlist = list;
700 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
702 icon_group_t *icog = xmalloc(sizeof(icon_group_t));
705 icog->memopt = *memopt;
709 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
711 split_icons(rd, icog, &(icog->nicon));
717 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
719 cursor_group_t *curg = xmalloc(sizeof(cursor_group_t));
722 curg->memopt = *memopt;
726 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
728 split_cursors(rd, curg, &(curg->ncursor));
735 * Animated cursors and icons
737 * The format of animated cursors and icons is yet another example
738 * of bad design by "The Company". The entire RIFF structure is
739 * flawed by design because it is inconsistent and single minded:
740 * - some tags have lengths attached, others don't. The use of these
741 * non-length tags is absolutely unclear;
742 * - the content of "icon" tags can be both icons and cursors;
743 * - tags lack proper alignment constraints. It seems that everything
744 * is 16bit aligned, but I could not find that in any docu. Just be
745 * prepared to eat anything;
746 * - there are no strict constraints on tag-nesting and the organization
747 * is highly illogical;
749 * Anyhow, here is the basic structure:
750 * "RIFF" { dword taglength }
751 * "ACON" // What does it do?
752 * "LIST" { dword taglength }
753 * "INFO" // And what does this do?
754 * "INAM" { dword taglength } // Icon/cursor name
756 * "IART" { dword taglength } // The artist
758 * "fram" // Is followed by "icon"s
759 * "icon" { dword taglength } // First frame
760 * { icon/cursor data }
761 * "icon" { dword taglength } // Second frame
762 * { icon/cursor data }
764 * "anih" { dword taglength } // Header structure
765 * { aniheader_t structure }
766 * "rate" { dword taglength } // The rate for each frame
768 * "seq " { dword taglength } // The frame blit-order
771 * Tag length are bytelength without the header and length field (i.e. -8).
772 * The "LIST" tag may occur several times and may encapsulate different
773 * tags. The `steps' is the number of "icon" tags found (actually the
774 * number of steps specified in the aniheader_t structure). The "seq "uence
775 * tag can be omitted, in which case the sequence is equal to the sequence
776 * of "icon"s found in the file. Also "rate" may be omitted, in which case
777 * the default from the aniheader_t structure is used.
779 * An animated cursor puts `.cur' formatted files into each "icon" tag,
780 * whereas animated icons contain `.ico' formatted files.
782 * Note about the code: Yes, it can be shorter/compressed. Some tags can be
783 * dealt with in the same code. However, this version shows what is going on
784 * and is better debug-able.
786 static const char riff[4] = "RIFF";
787 static const char acon[4] = "ACON";
788 static const char list[4] = "LIST";
789 static const char info[4] = "INFO";
790 static const char inam[4] = "INAM";
791 static const char iart[4] = "IART";
792 static const char fram[4] = "fram";
793 static const char icon[4] = "icon";
794 static const char anih[4] = "anih";
795 static const char rate[4] = "rate";
796 static const char seq[4] = "seq ";
798 #define SKIP_TAG(p,size) ((riff_tag_t *)(((char *)p) + (size)))
800 #define NEXT_TAG(p) SKIP_TAG(p,(isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p))
802 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
804 cursor_dir_entry_t *cdp;
805 cursor_header_t *chp;
809 static int once = 0; /* This will trigger only once per file! */
810 const char *anistr = type == res_aniico ? "icon" : "cursor";
812 * Both cursor and icon directories are similar
813 * Both cursor and icon headers are similar
816 chp = (cursor_header_t *)(rtp+1);
817 cdp = (cursor_dir_entry_t *)(chp+1);
818 count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
819 ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
820 chp->reserved = BYTESWAP_WORD(chp->reserved);
821 chp->type = BYTESWAP_WORD(chp->type);
822 chp->count = BYTESWAP_WORD(chp->count);
824 if(type == res_anicur && ctype != 2 && !once)
826 parser_warning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)\n",
828 ctype == 1 ? "icontype" : "?");
831 else if(type == res_aniico && ctype != 1 && !once)
833 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)\n",
835 ctype == 2 ? "cursortype" : "?");
838 else if(ctype != 1 && ctype != 2 && !once)
840 parser_warning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)\n", anistr, ctype);
844 for(i = 0; i < count; i++)
846 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
847 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
848 if(ofs > rtp->size || ofs+sze > rtp->size)
849 parser_error("Animated %s's data corrupt", anistr);
850 convert_bitmap((char *)chp + ofs, 0);
851 cdp[i].xhot = BYTESWAP_WORD(cdp->xhot);
852 cdp[i].yhot = BYTESWAP_WORD(cdp->yhot);
853 cdp[i].ressize = BYTESWAP_DWORD(cdp->ressize);
854 cdp[i].offset = BYTESWAP_DWORD(cdp->offset);
858 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
860 riff_tag_t *rtp = lst+1; /* Skip the "LIST" tag */
862 while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
864 if(!memcmp(rtp->tag, info, sizeof(info)))
866 rtp = SKIP_TAG(rtp,4);
868 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
870 /* Ignore the icon/cursor name; its a string */
873 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
875 /* Ignore the author's name; it's a string */
878 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
880 /* This should be followed by "icon"s, but we
881 * simply ignore this because it is pure
884 rtp = SKIP_TAG(rtp,4);
886 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
888 handle_ani_icon(rtp, type, isswapped);
892 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
893 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
894 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
895 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
896 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
898 if((UINT_PTR)rtp & 1)
899 rtp = SKIP_TAG(rtp,1);
903 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
905 ani_curico_t *ani = xmalloc(sizeof(ani_curico_t));
909 const char *anistr = type == res_aniico ? "icon" : "cursor";
911 assert(!memcmp(rd->data, riff, sizeof(riff)));
912 assert(type == res_anicur || type == res_aniico);
914 rtp = (riff_tag_t *)rd->data;
916 if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
918 else if(rtp->size + 2*sizeof(DWORD) == rd->size)
921 parser_error("Animated %s has an invalid RIFF length", anistr);
925 #ifdef WORDS_BIGENDIAN
938 * isswapped | doswap |
939 * ----------+--------+---------------------------------
940 * 0 | 0 | read native; don't convert
941 * 1 | 0 | read swapped size; don't convert
942 * 0 | 1 | read native; convert
943 * 1 | 1 | read swapped size; convert
944 * Reading swapped size if necessary to calculate in native
945 * format. E.g. a little-endian source on a big-endian
950 /* We only go through the RIFF file if we need to swap
951 * bytes in words/dwords. Else we couldn't care less
952 * what the file contains. This is consistent with
953 * MS' rc.exe, which doesn't complain at all, even though
954 * the file format might not be entirely correct.
956 rtp++; /* Skip the "RIFF" tag */
958 while((char *)rtp < (char *)rd->data + rd->size)
960 if(!memcmp(rtp->tag, acon, sizeof(acon)))
962 rtp = SKIP_TAG(rtp,4);
964 else if(!memcmp(rtp->tag, list, sizeof(list)))
966 handle_ani_list(rtp, type, isswapped);
969 else if(!memcmp(rtp->tag, anih, sizeof(anih)))
971 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
972 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
973 ahp->frames = BYTESWAP_DWORD(ahp->frames);
974 ahp->steps = BYTESWAP_DWORD(ahp->steps);
975 ahp->cx = BYTESWAP_DWORD(ahp->cx);
976 ahp->cy = BYTESWAP_DWORD(ahp->cy);
977 ahp->bitcount = BYTESWAP_DWORD(ahp->bitcount);
978 ahp->planes = BYTESWAP_DWORD(ahp->planes);
979 ahp->rate = BYTESWAP_DWORD(ahp->rate);
980 ahp->flags = BYTESWAP_DWORD(ahp->flags);
983 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
985 int cnt = rtp->size / sizeof(DWORD);
986 DWORD *dwp = (DWORD *)(rtp+1);
988 for(i = 0; i < cnt; i++)
989 dwp[i] = BYTESWAP_DWORD(dwp[i]);
992 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
994 int cnt = rtp->size / sizeof(DWORD);
995 DWORD *dwp = (DWORD *)(rtp+1);
997 for(i = 0; i < cnt; i++)
998 dwp[i] = BYTESWAP_DWORD(dwp[i]);
1002 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
1003 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
1004 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
1005 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
1006 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
1008 if((UINT_PTR)rtp & 1)
1009 rtp = SKIP_TAG(rtp,1);
1012 /* We must end correctly here */
1013 if((char *)rtp != (char *)rd->data + rd->size)
1014 parser_error("Animated %s contains invalid field size(s)", anistr);
1020 ani->memopt = *memopt;
1024 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
1030 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
1032 bitmap_t *bmp = xmalloc(sizeof(bitmap_t));
1037 bmp->memopt = *memopt;
1041 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1042 rd->size -= convert_bitmap(rd->data, rd->size);
1046 ver_words_t *new_ver_words(int i)
1048 ver_words_t *w = xmalloc(sizeof(ver_words_t));
1049 w->words = xmalloc(sizeof(WORD));
1050 w->words[0] = (WORD)i;
1055 ver_words_t *add_ver_words(ver_words_t *w, int i)
1057 w->words = xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
1058 w->words[w->nwords] = (WORD)i;
1063 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
1064 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
1066 messagetable_t *msg = xmalloc(sizeof(messagetable_t));
1067 msgtab_block_t *mbp;
1076 msg->memopt = *memopt;
1080 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1082 if(rd->size < sizeof(DWORD))
1083 parser_error("Invalid messagetable, size too small");
1085 nblk = *(DWORD *)rd->data;
1086 lo = WRC_LOWORD(nblk);
1087 hi = WRC_HIWORD(nblk);
1090 * This test will fail for all n*2^16 blocks in the messagetable.
1091 * However, no sane person would want to have so many blocks
1092 * and have a table of megabytes attached.
1093 * So, I will assume that we have less than 2^16 blocks in the table
1094 * and all will just work out fine. Otherwise, we would need to test
1095 * the ID, offset and length (and flag) fields to be very sure.
1098 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian\n");
1100 parser_error("Invalid messagetable block count 0");
1102 if(!hi && lo) /* Messagetable byteorder == native byteorder */
1104 #ifdef WORDS_BIGENDIAN
1105 if(byteorder != WRC_BO_LITTLE) goto out;
1107 if(byteorder != WRC_BO_BIG) goto out;
1109 /* Resource byteorder != native byteorder */
1111 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1112 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1113 parser_error("Messagetable's blocks are outside of defined data");
1114 for(i = 0; i < nblk; i++)
1116 msgtab_entry_t *mep, *next_mep;
1119 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1121 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1123 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1124 parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1125 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1127 WORD *wp = (WORD *)&mep[1];
1128 int l = mep->length/2 - 2; /* Length included header */
1132 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1133 for(n = 0; n < l; n++)
1134 wp[n] = BYTESWAP_WORD(wp[n]);
1137 next_mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1138 mep->length = BYTESWAP_WORD(mep->length);
1139 mep->flags = BYTESWAP_WORD(mep->flags);
1143 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1144 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1145 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1148 if(hi && !lo) /* Messagetable byteorder != native byteorder */
1150 #ifdef WORDS_BIGENDIAN
1151 if(byteorder == WRC_BO_LITTLE) goto out;
1153 if(byteorder == WRC_BO_BIG) goto out;
1155 /* Resource byteorder == native byteorder */
1157 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1158 nblk = BYTESWAP_DWORD(nblk);
1159 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1160 parser_error("Messagetable's blocks are outside of defined data");
1161 for(i = 0; i < nblk; i++)
1163 msgtab_entry_t *mep;
1166 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1167 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1168 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1169 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1171 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1173 mep->length = BYTESWAP_WORD(mep->length);
1174 mep->flags = BYTESWAP_WORD(mep->flags);
1176 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1177 parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1178 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1180 WORD *wp = (WORD *)&mep[1];
1181 int l = mep->length/2 - 2; /* Length included header */
1185 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1186 for(n = 0; n < l; n++)
1187 wp[n] = BYTESWAP_WORD(wp[n]);
1190 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1198 #undef MSGTAB_BAD_PTR
1200 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1202 assert(offs <= src->size);
1203 assert(offs + len <= src->size);
1206 dst->data = xmalloc(len);
1210 dst->data = xrealloc(dst->data, dst->size + len);
1211 /* dst->size holds the offset to copy to */
1212 memcpy(dst->data + dst->size, src->data + offs, len);
1218 int *ip = xmalloc(sizeof(int));
1223 stringtable_t *new_stringtable(lvc_t *lvc)
1225 stringtable_t *stt = xmalloc(sizeof(stringtable_t));
1227 memset( stt, 0, sizeof(*stt) );
1234 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1236 toolbar_t *tb = xmalloc(sizeof(toolbar_t));
1237 memset( tb, 0, sizeof(*tb) );
1238 tb->button_width = button_width;
1239 tb->button_height = button_height;
1240 tb->nitems = nitems;
1245 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1247 dlginit_t *di = xmalloc(sizeof(dlginit_t));
1251 di->memopt = *memopt;
1255 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1260 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1262 style_pair_t *sp = xmalloc(sizeof(style_pair_t));
1264 sp->exstyle = exstyle;
1268 style_t *new_style(DWORD or_mask, DWORD and_mask)
1270 style_t *st = xmalloc(sizeof(style_t));
1271 st->or_mask = or_mask;
1272 st->and_mask = and_mask;