2 * Create dynamic new structures of various types
3 * and some utils in that trend.
5 * Copyright 1998 Bertho A. Stultiens
22 #include "wingdi.h" /* for BITMAPINFOHEADER */
24 /* Generate new_* functions that have no parameters (NOTE: no ';') */
25 __NEW_STRUCT_FUNC(dialog)
26 __NEW_STRUCT_FUNC(dialogex)
27 __NEW_STRUCT_FUNC(name_id)
28 __NEW_STRUCT_FUNC(menu)
29 __NEW_STRUCT_FUNC(menuex)
30 __NEW_STRUCT_FUNC(menu_item)
31 __NEW_STRUCT_FUNC(menuex_item)
32 __NEW_STRUCT_FUNC(control)
33 __NEW_STRUCT_FUNC(icon)
34 __NEW_STRUCT_FUNC(cursor)
35 __NEW_STRUCT_FUNC(versioninfo)
36 __NEW_STRUCT_FUNC(ver_value)
37 __NEW_STRUCT_FUNC(ver_block)
38 __NEW_STRUCT_FUNC(stt_entry)
39 __NEW_STRUCT_FUNC(accelerator)
40 __NEW_STRUCT_FUNC(event)
41 __NEW_STRUCT_FUNC(raw_data)
42 __NEW_STRUCT_FUNC(lvc)
43 __NEW_STRUCT_FUNC(res_count)
44 __NEW_STRUCT_FUNC(string)
45 __NEW_STRUCT_FUNC(toolbar_item)
46 __NEW_STRUCT_FUNC(ani_any)
48 /* New instances for all types of structures */
49 /* Very inefficient (in size), but very functional :-]
50 * Especially for type-checking.
52 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
54 resource_t *r = (resource_t *)xmalloc(sizeof(resource_t));
62 version_t *new_version(DWORD v)
64 version_t *vp = (version_t *)xmalloc(sizeof(version_t));
69 characts_t *new_characts(DWORD c)
71 characts_t *cp = (characts_t *)xmalloc(sizeof(characts_t));
76 language_t *new_language(int id, int sub)
78 language_t *lan = (language_t *)xmalloc(sizeof(language_t));
84 language_t *dup_language(language_t *l)
87 return new_language(l->id, l->sub);
90 version_t *dup_version(version_t *v)
93 return new_version(*v);
96 characts_t *dup_characts(characts_t *c)
99 return new_characts(*c);
102 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
104 rcdata_t *rc = (rcdata_t *)xmalloc(sizeof(rcdata_t));
108 rc->memopt = *memopt;
112 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
116 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
118 font_id_t *fid = (font_id_t *)xmalloc(sizeof(font_id_t));
121 fid->weight = weight;
122 fid->italic = italic;
126 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
128 user_t *usr = (user_t *)xmalloc(sizeof(user_t));
132 usr->memopt = *memopt;
136 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
141 font_t *new_font(raw_data_t *rd, int *memopt)
143 font_t *fnt = (font_t *)xmalloc(sizeof(font_t));
147 fnt->memopt = *memopt;
151 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
155 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
157 fontdir_t *fnd = (fontdir_t *)xmalloc(sizeof(fontdir_t));
161 fnd->memopt = *memopt;
165 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
171 * Convert bitmaps to proper endian
173 static void convert_bitmap_swap_v3(BITMAPINFOHEADER *bih)
175 bih->biSize = BYTESWAP_DWORD(bih->biSize);
176 bih->biWidth = BYTESWAP_DWORD(bih->biWidth);
177 bih->biHeight = BYTESWAP_DWORD(bih->biHeight);
178 bih->biPlanes = BYTESWAP_WORD(bih->biPlanes);
179 bih->biBitCount = BYTESWAP_WORD(bih->biBitCount);
180 bih->biCompression = BYTESWAP_DWORD(bih->biCompression);
181 bih->biSizeImage = BYTESWAP_DWORD(bih->biSizeImage);
182 bih->biXPelsPerMeter = BYTESWAP_DWORD(bih->biXPelsPerMeter);
183 bih->biYPelsPerMeter = BYTESWAP_DWORD(bih->biYPelsPerMeter);
184 bih->biClrUsed = BYTESWAP_DWORD(bih->biClrUsed);
185 bih->biClrImportant = BYTESWAP_DWORD(bih->biClrImportant);
188 static void convert_bitmap_swap_v4(BITMAPV4HEADER *b4h)
190 convert_bitmap_swap_v3((BITMAPINFOHEADER *)b4h);
191 b4h->bV4RedMask = BYTESWAP_DWORD(b4h->bV4RedMask);
192 b4h->bV4GreenMask = BYTESWAP_DWORD(b4h->bV4GreenMask);
193 b4h->bV4BlueMask = BYTESWAP_DWORD(b4h->bV4BlueMask);
194 b4h->bV4AlphaMask = BYTESWAP_DWORD(b4h->bV4AlphaMask);
195 b4h->bV4CSType = BYTESWAP_DWORD(b4h->bV4CSType);
196 b4h->bV4EndPoints.ciexyzRed.ciexyzX = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzX);
197 b4h->bV4EndPoints.ciexyzRed.ciexyzY = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzY);
198 b4h->bV4EndPoints.ciexyzRed.ciexyzZ = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzZ);
199 b4h->bV4EndPoints.ciexyzGreen.ciexyzX = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzX);
200 b4h->bV4EndPoints.ciexyzGreen.ciexyzY = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzY);
201 b4h->bV4EndPoints.ciexyzGreen.ciexyzZ = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzZ);
202 b4h->bV4EndPoints.ciexyzBlue.ciexyzX = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzX);
203 b4h->bV4EndPoints.ciexyzBlue.ciexyzY = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzY);
204 b4h->bV4EndPoints.ciexyzBlue.ciexyzZ = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzZ);
205 b4h->bV4GammaRed = BYTESWAP_DWORD(b4h->bV4GammaRed);
206 b4h->bV4GammaGreen = BYTESWAP_DWORD(b4h->bV4GammaGreen);
207 b4h->bV4GammaBlue = BYTESWAP_DWORD(b4h->bV4GammaBlue);
210 #define FL_SIGBE 0x01
211 #define FL_SIZEBE 0x02
213 static int convert_bitmap(char *data, int size)
215 BITMAPINFOHEADER *bih = (BITMAPINFOHEADER *)data;
216 BITMAPV4HEADER *b4h = (BITMAPV4HEADER *)data;
218 #ifdef WORDS_BIGENDIAN
219 DWORD bisizel = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
220 DWORD b4sizel = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
221 DWORD bisizeb = sizeof(BITMAPINFOHEADER);
222 DWORD b4sizeb = sizeof(BITMAPV4HEADER);
224 DWORD bisizel = sizeof(BITMAPINFOHEADER);
225 DWORD b4sizel = sizeof(BITMAPV4HEADER);
226 DWORD bisizeb = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
227 DWORD b4sizeb = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
230 if(data[0] == 'B' && data[1] == 'M')
232 /* Little endian signature */
233 bih = (BITMAPINFOHEADER *)(data + sizeof(BITMAPFILEHEADER));
234 b4h = (BITMAPV4HEADER *)(data + sizeof(BITMAPFILEHEADER));
236 else if(data[0] == 'M' && data[1] == 'B')
238 type |= FL_SIGBE; /* Big endian signature */
239 bih = (BITMAPINFOHEADER *)(data + sizeof(BITMAPFILEHEADER));
240 b4h = (BITMAPV4HEADER *)(data + sizeof(BITMAPFILEHEADER));
243 if(bih->biSize == bisizel)
247 else if(bih->biSize == b4sizel)
251 else if(bih->biSize == bisizeb)
255 else if(bih->biSize == b4sizeb)
257 type |= FL_SIZEBE | FL_V4;
267 case FL_SIZEBE | FL_V4:
268 yywarning("Bitmap v%c signature little-endian, but size big-endian", type & FL_V4 ? '4' : '3');
271 case FL_SIGBE | FL_V4:
272 yywarning("Bitmap v%c signature big-endian, but size little-endian", type & FL_V4 ? '4' : '3');
275 yyerror("Invalid bitmap format");
281 #ifdef WORDS_BIGENDIAN
285 if(!(type & FL_SIZEBE))
288 convert_bitmap_swap_v4(b4h);
290 convert_bitmap_swap_v3(bih);
293 #ifndef WORDS_BIGENDIAN
300 convert_bitmap_swap_v4(b4h);
302 convert_bitmap_swap_v3(bih);
307 if(size && (void *)data != (void *)bih)
309 /* We have the fileheader still attached, remove it */
310 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
311 return sizeof(BITMAPFILEHEADER);
320 * Cursor and icon splitter functions used when allocating
321 * cursor- and icon-groups.
328 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
332 assert(list != NULL);
337 *list = (id_alloc_t *)xmalloc(sizeof(id_alloc_t));
339 (*list)[0].lan = *lan;
344 for(i = 0; i < *n; i++)
346 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
347 return ++((*list)[i].id);
350 *list = (id_alloc_t *)xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
351 (*list)[*n].lan = *lan;
357 static int alloc_icon_id(language_t *lan)
359 static id_alloc_t *idlist = NULL;
362 return get_new_id(&idlist, &nid, lan);
365 static int alloc_cursor_id(language_t *lan)
367 static id_alloc_t *idlist = NULL;
370 return get_new_id(&idlist, &nid, lan);
373 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
379 icon_header_t *ih = (icon_header_t *)rd->data;
384 else if(BYTESWAP_WORD(ih->type) == 1)
387 yyerror("Icon resource data has invalid type id %d", ih->type);
389 cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
390 for(i = 0; i < cnt; i++)
392 icon_dir_entry_t ide;
393 BITMAPINFOHEADER info;
394 memcpy(&ide, rd->data + sizeof(icon_header_t)
395 + i*sizeof(icon_dir_entry_t), sizeof(ide));
398 ico->id = alloc_icon_id(icog->lvc.language);
399 ico->lvc = icog->lvc;
402 ide.offset = BYTESWAP_DWORD(ide.offset);
403 ide.ressize= BYTESWAP_DWORD(ide.ressize);
405 if(ide.offset > rd->size
406 || ide.offset + ide.ressize > rd->size)
407 yyerror("Icon resource data corrupt");
408 ico->width = ide.width;
409 ico->height = ide.height;
410 ico->nclr = ide.nclr;
411 ico->planes = swap ? BYTESWAP_WORD(ide.planes) : ide.planes;
412 ico->bits = swap ? BYTESWAP_WORD(ide.bits) : ide.bits;
413 convert_bitmap((char *)rd->data + ide.offset, 0);
414 memcpy(&info, rd->data + ide.offset, sizeof(info));
417 /* Argh! They did not fill out the resdir structure */
418 /* The bitmap is in destination byteorder. We want native for our structures */
421 #ifdef WORDS_BIGENDIAN
426 ico->planes = BYTESWAP_WORD(info.biPlanes);
429 ico->planes = info.biPlanes;
434 /* Argh! They did not fill out the resdir structure */
435 /* The bitmap is in destination byteorder. We want native for our structures */
438 #ifdef WORDS_BIGENDIAN
443 ico->bits = BYTESWAP_WORD(info.biBitCount);
446 ico->bits = info.biBitCount;
449 ico->data = new_raw_data();
450 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
462 icog->iconlist = list;
466 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
471 cursor_t *list = NULL;
472 cursor_header_t *ch = (cursor_header_t *)rd->data;
477 else if(BYTESWAP_WORD(ch->type) == 2)
480 yyerror("Cursor resource data has invalid type id %d", ch->type);
481 cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
482 for(i = 0; i < cnt; i++)
484 cursor_dir_entry_t cde;
485 BITMAPINFOHEADER info;
486 memcpy(&cde, rd->data + sizeof(cursor_header_t)
487 + i*sizeof(cursor_dir_entry_t), sizeof(cde));
490 cur->id = alloc_cursor_id(curg->lvc.language);
491 cur->lvc = curg->lvc;
494 cde.offset = BYTESWAP_DWORD(cde.offset);
495 cde.ressize= BYTESWAP_DWORD(cde.ressize);
497 if(cde.offset > rd->size
498 || cde.offset + cde.ressize > rd->size)
499 yyerror("Cursor resource data corrupt");
500 cur->width = cde.width;
501 cur->height = cde.height;
502 cur->nclr = cde.nclr;
503 convert_bitmap((char *)rd->data + cde.offset, 0);
504 memcpy(&info, rd->data + cde.offset, sizeof(info));
505 /* The bitmap is in destination byteorder. We want native for our structures */
508 #ifdef WORDS_BIGENDIAN
513 cur->planes = BYTESWAP_WORD(info.biPlanes);
514 cur->bits = BYTESWAP_WORD(info.biBitCount);
517 cur->planes = info.biPlanes;
518 cur->bits = info.biBitCount;
520 if(!win32 && (cur->planes != 1 || cur->bits != 1))
521 yywarning("Win16 cursor contains colors");
522 cur->xhot = swap ? BYTESWAP_WORD(cde.xhot) : cde.xhot;
523 cur->yhot = swap ? BYTESWAP_WORD(cde.yhot) : cde.yhot;
524 cur->data = new_raw_data();
525 copy_raw_data(cur->data, rd, cde.offset, cde.ressize);
537 curg->cursorlist = list;
542 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
544 icon_group_t *icog = (icon_group_t *)xmalloc(sizeof(icon_group_t));
547 icog->memopt = *memopt;
551 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
553 split_icons(rd, icog, &(icog->nicon));
559 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
561 cursor_group_t *curg = (cursor_group_t *)xmalloc(sizeof(cursor_group_t));
564 curg->memopt = *memopt;
568 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
570 split_cursors(rd, curg, &(curg->ncursor));
577 * Animated cursors and icons
579 * The format of animated cursors and icons is yet another example
580 * of bad design by "The Company". The entire RIFF structure is
581 * flawed by design because it is inconsistent and single minded:
582 * - some tags have lengths attached, others don't. The use of these
583 * non-length tags is absolutely unclear;
584 * - the content of "icon" tags can be both icons and cursors;
585 * - tags lack proper alignment constraints. It seems that everything
586 * is 16bit aligned, but I could not find that in any docu. Just be
587 * prepared to eat anything;
588 * - there are no strict constraints on tag-nesting and the organization
589 * is highly illogical;
591 * Anyhow, here is the basic structure:
592 * "RIFF" { dword taglength }
593 * "ACON" // What does it do?
594 * "LIST" { dword taglength }
595 * "INFO" // And what does this do?
596 * "INAM" { dword taglength } // Icon/cursor name
598 * "IART" { dword taglength } // The artist
600 * "fram" // Is followed by "icon"s
601 * "icon" { dword taglength } // First frame
602 * { icon/cursor data }
603 * "icon" { dword taglength } // Second frame
604 * { icon/cursor data }
606 * "anih" { dword taglength } // Header structure
607 * { aniheader_t structure }
608 * "rate" { dword taglength } // The rate for each frame
610 * "seq " { dword taglength } // The frame blit-order
613 * Tag length are bytelength without the header and length field (i.e. -8).
614 * The "LIST" tag may occur several times and may encapsulate different
615 * tags. The `steps' is the number of "icon" tags found (actually the
616 * number of steps specified in the aniheader_t structure). The "seq "uence
617 * tag can be ommitted, in which case the sequence is equal to the sequence
618 * of "icon"s found in the file. Also "rate" may be ommitted, in which case
619 * the default from the aniheader_t structure is used.
621 * An animated cursor puts `.cur' formatted files into each "icon" tag,
622 * whereas animated icons contain `.ico' formatted files.
624 * Note about the code: Yes, it can be shorter/compressed. Some tags can be
625 * dealt with in the same code. However, this version shows what is going on
626 * and is better debug-able.
628 static const char riff[4] = "RIFF";
629 static const char acon[4] = "ACON";
630 static const char list[4] = "LIST";
631 static const char info[4] = "INFO";
632 static const char inam[4] = "INAM";
633 static const char iart[4] = "IART";
634 static const char fram[4] = "fram";
635 static const char icon[4] = "icon";
636 static const char anih[4] = "anih";
637 static const char rate[4] = "rate";
638 static const char seq[4] = "seq ";
640 #define NEXT_TAG(p) ((riff_tag_t *)(((char *)p) + (isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p)))
642 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
644 cursor_dir_entry_t *cdp;
645 cursor_header_t *chp;
649 static int once = 0; /* This will trigger only once per file! */
650 const char *anistr = type == res_aniico ? "icon" : "cursor";
652 * Both cursor and icon directories are similar
653 * Both cursor and icon headers are similar
656 chp = (cursor_header_t *)(rtp+1);
657 cdp = (cursor_dir_entry_t *)(chp+1);
658 count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
659 ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
660 chp->reserved = BYTESWAP_WORD(chp->reserved);
661 chp->type = BYTESWAP_WORD(chp->type);
662 chp->count = BYTESWAP_WORD(chp->count);
664 if(type == res_anicur && ctype != 2 && !once)
666 yywarning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)",
668 ctype == 1 ? "icontype" : "?");
671 else if(type == res_aniico && ctype != 1 && !once)
673 yywarning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)",
675 ctype == 2 ? "cursortype" : "?");
678 else if(ctype != 1 && ctype != 2 && !once)
680 yywarning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)", anistr, ctype);
684 for(i = 0; i < count; i++)
686 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
687 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
688 if(ofs > rtp->size || ofs+sze > rtp->size)
689 yyerror("Animated %s's data corrupt", anistr);
690 convert_bitmap((char *)chp + ofs, 0);
691 cdp[i].xhot = BYTESWAP_WORD(cdp->xhot);
692 cdp[i].yhot = BYTESWAP_WORD(cdp->yhot);
693 cdp[i].ressize = BYTESWAP_DWORD(cdp->ressize);
694 cdp[i].offset = BYTESWAP_DWORD(cdp->offset);
698 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
700 riff_tag_t *rtp = lst+1; /* Skip the "LIST" tag */
702 while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
704 if(!memcmp(rtp->tag, info, sizeof(info)))
706 rtp = (riff_tag_t *)(((char *)rtp) + 4);
708 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
710 /* Ignore the icon/cursor name; its a string */
713 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
715 /* Ignore the author's name; its a string */
718 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
720 /* This should be followed by "icon"s, but we
721 * simply ignore this because it is pure
724 rtp = (riff_tag_t *)(((char *)rtp) + 4);
726 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
728 handle_ani_icon(rtp, type, isswapped);
732 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
733 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
734 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
735 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
736 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
738 /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
744 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
746 ani_curico_t *ani = (ani_curico_t *)xmalloc(sizeof(ani_curico_t));
750 const char *anistr = type == res_aniico ? "icon" : "cursor";
752 assert(!memcmp(rd->data, riff, sizeof(riff)));
753 assert(type == res_anicur || type == res_aniico);
755 rtp = (riff_tag_t *)rd->data;
757 if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
759 else if(rtp->size + 2*sizeof(DWORD) == rd->size)
762 yyerror("Animated %s has an invalid RIFF length", anistr);
766 #ifdef WORDS_BIGENDIAN
779 * isswapped | doswap |
780 * ----------+--------+---------------------------------
781 * 0 | 0 | read native; don't convert
782 * 1 | 0 | read swapped size; don't convert
783 * 0 | 1 | read native; convert
784 * 1 | 1 | read swapped size; convert
785 * Reading swapped size if necessary to calculate in native
786 * format. E.g. a little-endian source on a big-endian
791 /* We only go through the RIFF file if we need to swap
792 * bytes in words/dwords. Else we couldn't care less
793 * what the file contains. This is consistent with
794 * MS' rc.exe, which doesn't complain at all, eventhough
795 * the fileformat might not be entirely correct.
797 rtp++; /* Skip the "RIFF" tag */
799 while((char *)rtp < (char *)rd->data + rd->size)
801 if(!memcmp(rtp->tag, acon, sizeof(acon)))
803 rtp = (riff_tag_t *)(((char *)rtp) + 4);
805 else if(!memcmp(rtp->tag, list, sizeof(list)))
807 handle_ani_list(rtp, type, isswapped);
810 else if(!memcmp(rtp->tag, anih, sizeof(anih)))
812 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
813 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
814 ahp->frames = BYTESWAP_DWORD(ahp->frames);
815 ahp->steps = BYTESWAP_DWORD(ahp->steps);
816 ahp->cx = BYTESWAP_DWORD(ahp->cx);
817 ahp->cy = BYTESWAP_DWORD(ahp->cy);
818 ahp->bitcount = BYTESWAP_DWORD(ahp->bitcount);
819 ahp->planes = BYTESWAP_DWORD(ahp->planes);
820 ahp->rate = BYTESWAP_DWORD(ahp->rate);
821 ahp->flags = BYTESWAP_DWORD(ahp->flags);
824 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
826 int cnt = rtp->size / sizeof(DWORD);
827 DWORD *dwp = (DWORD *)(rtp+1);
829 for(i = 0; i < cnt; i++)
830 dwp[i] = BYTESWAP_DWORD(dwp[i]);
833 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
835 int cnt = rtp->size / sizeof(DWORD);
836 DWORD *dwp = (DWORD *)(rtp+1);
838 for(i = 0; i < cnt; i++)
839 dwp[i] = BYTESWAP_DWORD(dwp[i]);
843 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
844 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
845 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
846 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
847 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
849 /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
854 /* We must end correctly here */
855 if((char *)rtp != (char *)rd->data + rd->size)
856 yyerror("Animated %s contains invalid field size(s)", anistr);
862 ani->memopt = *memopt;
866 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
872 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
874 bitmap_t *bmp = (bitmap_t *)xmalloc(sizeof(bitmap_t));
879 bmp->memopt = *memopt;
883 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
884 rd->size -= convert_bitmap(rd->data, rd->size);
888 ver_words_t *new_ver_words(int i)
890 ver_words_t *w = (ver_words_t *)xmalloc(sizeof(ver_words_t));
891 w->words = (WORD *)xmalloc(sizeof(WORD));
892 w->words[0] = (WORD)i;
897 ver_words_t *add_ver_words(ver_words_t *w, int i)
899 w->words = (WORD *)xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
900 w->words[w->nwords] = (WORD)i;
905 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
906 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
908 messagetable_t *msg = (messagetable_t *)xmalloc(sizeof(messagetable_t));
918 msg->memopt = *memopt;
922 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
924 if(rd->size < sizeof(DWORD))
925 yyerror("Invalid messagetable, size too small");
927 nblk = *(DWORD *)rd->data;
928 lo = WRC_LOWORD(nblk);
929 hi = WRC_HIWORD(nblk);
932 * This test will fail for all n*2^16 blocks in the messagetable.
933 * However, no sane person would want to have so many blocks
934 * and have a table of megabytes attached.
935 * So, I will assume that we have less than 2^16 blocks in the table
936 * and all will just work out fine. Otherwise, we would need to test
937 * the ID, offset and length (and flag) fields to be very sure.
940 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian");
942 yyerror("Invalid messagetable block count 0");
944 if(!hi && lo) /* Messagetable byteorder == native byteorder */
946 #ifdef WORDS_BIGENDIAN
947 if(byteorder != WRC_BO_LITTLE) goto out;
949 if(byteorder != WRC_BO_BIG) goto out;
951 /* Resource byteorder != native byteorder */
953 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
954 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
955 yyerror("Messagetable's blocks are outside of defined data");
956 for(i = 0; i < nblk; i++)
958 msgtab_entry_t *mep, *next_mep;
961 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
963 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
965 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
966 yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i, id);
967 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
969 WORD *wp = (WORD *)&mep[1];
970 int l = mep->length/2 - 2; /* Length included header */
974 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id, (int)i, mep->length);
975 for(n = 0; n < l; n++)
976 wp[n] = BYTESWAP_WORD(wp[n]);
979 next_mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
980 mep->length = BYTESWAP_WORD(mep->length);
981 mep->flags = BYTESWAP_WORD(mep->flags);
985 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
986 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
987 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
990 if(hi && !lo) /* Messagetable byteorder != native byteorder */
992 #ifdef WORDS_BIGENDIAN
993 if(byteorder == WRC_BO_LITTLE) goto out;
995 if(byteorder == WRC_BO_BIG) goto out;
997 /* Resource byteorder == native byteorder */
999 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1000 nblk = BYTESWAP_DWORD(nblk);
1001 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1002 yyerror("Messagetable's blocks are outside of defined data");
1003 for(i = 0; i < nblk; i++)
1005 msgtab_entry_t *mep;
1008 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1009 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1010 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1011 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1013 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1015 mep->length = BYTESWAP_WORD(mep->length);
1016 mep->flags = BYTESWAP_WORD(mep->flags);
1018 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1019 yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i, id);
1020 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1022 WORD *wp = (WORD *)&mep[1];
1023 int l = mep->length/2 - 2; /* Length included header */
1027 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id, (int)i, mep->length);
1028 for(n = 0; n < l; n++)
1029 wp[n] = BYTESWAP_WORD(wp[n]);
1032 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1040 #undef MSGTAB_BAD_PTR
1042 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1044 assert(offs <= src->size);
1045 assert(offs + len <= src->size);
1048 dst->data = (char *)xmalloc(len);
1052 dst->data = (char *)xrealloc(dst->data, dst->size + len);
1053 /* dst->size holds the offset to copy to */
1054 memcpy(dst->data + dst->size, src->data + offs, len);
1060 int *ip = (int *)xmalloc(sizeof(int));
1065 stringtable_t *new_stringtable(lvc_t *lvc)
1067 stringtable_t *stt = (stringtable_t *)xmalloc(sizeof(stringtable_t));
1075 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1077 toolbar_t *tb = (toolbar_t *)xmalloc(sizeof(toolbar_t));
1078 tb->button_width = button_width;
1079 tb->button_height = button_height;
1080 tb->nitems = nitems;
1085 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1087 dlginit_t *di = (dlginit_t *)xmalloc(sizeof(dlginit_t));
1091 di->memopt = *memopt;
1095 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1100 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1102 style_pair_t *sp = (style_pair_t *)xmalloc(sizeof(style_pair_t));
1104 sp->exstyle = exstyle;
1108 style_t *new_style(DWORD or_mask, DWORD and_mask)
1110 style_t *st = (style_t *)xmalloc(sizeof(style_t));
1111 st->or_mask = or_mask;
1112 st->and_mask = and_mask;